org.el 904 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579
  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 <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 5.07
  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://staff.science.uva.nl/~dominik/Tools/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://www.astro.uva.nl/~dominik/Tools/org/Changes
  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.07"
  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. ss))
  103. s))
  104. (defmacro org-preserve-lc (&rest body)
  105. `(let ((_line (org-current-line))
  106. (_col (current-column)))
  107. (unwind-protect
  108. (progn ,@body)
  109. (goto-line _line)
  110. (move-to-column _col))))
  111. ;;; The custom variables
  112. (defgroup org nil
  113. "Outline-based notes management and organizer."
  114. :tag "Org"
  115. :group 'outlines
  116. :group 'hypermedia
  117. :group 'calendar)
  118. (defgroup org-startup nil
  119. "Options concerning startup of Org-mode."
  120. :tag "Org Startup"
  121. :group 'org)
  122. (defcustom org-startup-folded t
  123. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  124. This can also be configured on a per-file basis by adding one of
  125. the following lines anywhere in the buffer:
  126. #+STARTUP: fold
  127. #+STARTUP: nofold
  128. #+STARTUP: content"
  129. :group 'org-startup
  130. :type '(choice
  131. (const :tag "nofold: show all" nil)
  132. (const :tag "fold: overview" t)
  133. (const :tag "content: all headlines" content)))
  134. (defcustom org-startup-truncated t
  135. "Non-nil means, entering Org-mode will set `truncate-lines'.
  136. This is useful since some lines containing links can be very long and
  137. uninteresting. Also tables look terrible when wrapped."
  138. :group 'org-startup
  139. :type 'boolean)
  140. (defcustom org-startup-align-all-tables nil
  141. "Non-nil means, align all tables when visiting a file.
  142. This is useful when the column width in tables is forced with <N> cookies
  143. in table fields. Such tables will look correct only after the first re-align.
  144. This can also be configured on a per-file basis by adding one of
  145. the following lines anywhere in the buffer:
  146. #+STARTUP: align
  147. #+STARTUP: noalign"
  148. :group 'org-startup
  149. :type 'boolean)
  150. (defcustom org-insert-mode-line-in-empty-file nil
  151. "Non-nil means insert the first line setting Org-mode in empty files.
  152. When the function `org-mode' is called interactively in an empty file, this
  153. normally means that the file name does not automatically trigger Org-mode.
  154. To ensure that the file will always be in Org-mode in the future, a
  155. line enforcing Org-mode will be inserted into the buffer, if this option
  156. has been set."
  157. :group 'org-startup
  158. :type 'boolean)
  159. (defcustom org-replace-disputed-keys nil
  160. "Non-nil means use alternative key bindings for some keys.
  161. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  162. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  163. If you want to use Org-mode together with one of these other modes,
  164. or more generally if you would like to move some Org-mode commands to
  165. other keys, set this variable and configure the keys with the variable
  166. `org-disputed-keys'.
  167. This option is only relevant at load-time of Org-mode, and must be set
  168. *before* org.el is loaded. Changing it requires a restart of Emacs to
  169. become effective."
  170. :group 'org-startup
  171. :type 'boolean)
  172. (if (fboundp 'defvaralias)
  173. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  174. (defcustom org-disputed-keys
  175. '(([(shift up)] . [(meta p)])
  176. ([(shift down)] . [(meta n)])
  177. ([(shift left)] . [(meta -)])
  178. ([(shift right)] . [(meta +)])
  179. ([(control shift right)] . [(meta shift +)])
  180. ([(control shift left)] . [(meta shift -)]))
  181. "Keys for which Org-mode and other modes compete.
  182. This is an alist, cars are the default keys, second element specifies
  183. the alternative to use when `org-replace-disputed-keys' is t.
  184. Keys can be specified in any syntax supported by `define-key'.
  185. The value of this option takes effect only at Org-mode's startup,
  186. therefore you'll have to restart Emacs to apply it after changing."
  187. :group 'org-startup
  188. :type 'alist)
  189. (defun org-key (key)
  190. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  191. Or return the original if not disputed."
  192. (if org-replace-disputed-keys
  193. (let* ((nkey (key-description key))
  194. (x (org-find-if (lambda (x)
  195. (equal (key-description (car x)) nkey))
  196. org-disputed-keys)))
  197. (if x (cdr x) key))
  198. key))
  199. (defun org-find-if (predicate seq)
  200. (catch 'exit
  201. (while seq
  202. (if (funcall predicate (car seq))
  203. (throw 'exit (car seq))
  204. (pop seq)))))
  205. (defun org-defkey (keymap key def)
  206. "Define a key, possibly translated, as returned by `org-key'."
  207. (define-key keymap (org-key key) def))
  208. (defcustom org-ellipsis nil
  209. "The ellipsis to use in the Org-mode outline.
  210. When nil, just use the standard three dots. When a string, use that instead,
  211. When a face, use the standart 3 dots, but with the specified face.
  212. The change affects only Org-mode (which will then use its own display table).
  213. Changing this requires executing `M-x org-mode' in a buffer to become
  214. effective."
  215. :group 'org-startup
  216. :type '(choice (const :tag "Default" nil)
  217. (face :tag "Face" :value org-warning)
  218. (string :tag "String" :value "...#")))
  219. (defvar org-display-table nil
  220. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  221. (defgroup org-keywords nil
  222. "Keywords in Org-mode."
  223. :tag "Org Keywords"
  224. :group 'org)
  225. (defcustom org-deadline-string "DEADLINE:"
  226. "String to mark deadline entries.
  227. A deadline is this string, followed by a time stamp. Should be a word,
  228. terminated by a colon. You can insert a schedule keyword and
  229. a timestamp with \\[org-deadline].
  230. Changes become only effective after restarting Emacs."
  231. :group 'org-keywords
  232. :type 'string)
  233. (defcustom org-scheduled-string "SCHEDULED:"
  234. "String to mark scheduled TODO entries.
  235. A schedule is this string, followed by a time stamp. Should be a word,
  236. terminated by a colon. You can insert a schedule keyword and
  237. a timestamp with \\[org-schedule].
  238. Changes become only effective after restarting Emacs."
  239. :group 'org-keywords
  240. :type 'string)
  241. (defcustom org-closed-string "CLOSED:"
  242. "String used as the prefix for timestamps logging closing a TODO entry."
  243. :group 'org-keywords
  244. :type 'string)
  245. (defcustom org-clock-string "CLOCK:"
  246. "String used as prefix for timestamps clocking work hours on an item."
  247. :group 'org-keywords
  248. :type 'string)
  249. (defcustom org-comment-string "COMMENT"
  250. "Entries starting with this keyword will never be exported.
  251. An entry can be toggled between COMMENT and normal with
  252. \\[org-toggle-comment].
  253. Changes become only effective after restarting Emacs."
  254. :group 'org-keywords
  255. :type 'string)
  256. (defcustom org-quote-string "QUOTE"
  257. "Entries starting with this keyword will be exported in fixed-width font.
  258. Quoting applies only to the text in the entry following the headline, and does
  259. not extend beyond the next headline, even if that is lower level.
  260. An entry can be toggled between QUOTE and normal with
  261. \\[org-toggle-fixed-width-section]."
  262. :group 'org-keywords
  263. :type 'string)
  264. (defconst org-repeat-re
  265. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  266. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  267. "Regular expression for specifying repeated events.
  268. After a match, group 1 contains the repeat expression.")
  269. (defgroup org-structure nil
  270. "Options concerning the general structure of Org-mode files."
  271. :tag "Org Structure"
  272. :group 'org)
  273. (defgroup org-reveal-location nil
  274. "Options about how to make context of a location visible."
  275. :tag "Org Reveal Location"
  276. :group 'org-structure)
  277. (defcustom org-show-hierarchy-above '((default . t))
  278. "Non-nil means, show full hierarchy when revealing a location.
  279. Org-mode often shows locations in an org-mode file which might have
  280. been invisible before. When this is set, the hierarchy of headings
  281. above the exposed location is shown.
  282. Turning this off for example for sparse trees makes them very compact.
  283. Instead of t, this can also be an alist specifying this option for different
  284. contexts. Valid contexts are
  285. agenda when exposing an entry from the agenda
  286. org-goto when using the command `org-goto' on key C-c C-j
  287. occur-tree when using the command `org-occur' on key C-c /
  288. tags-tree when constructing a sparse tree based on tags matches
  289. link-search when exposing search matches associated with a link
  290. mark-goto when exposing the jump goal of a mark
  291. bookmark-jump when exposing a bookmark location
  292. isearch when exiting from an incremental search
  293. default default for all contexts not set explicitly"
  294. :group 'org-reveal-location
  295. :type '(choice
  296. (const :tag "Always" t)
  297. (const :tag "Never" nil)
  298. (repeat :greedy t :tag "Individual contexts"
  299. (cons
  300. (choice :tag "Context"
  301. (const agenda)
  302. (const org-goto)
  303. (const occur-tree)
  304. (const tags-tree)
  305. (const link-search)
  306. (const mark-goto)
  307. (const bookmark-jump)
  308. (const isearch)
  309. (const default))
  310. (boolean)))))
  311. (defcustom org-show-following-heading '((default . nil))
  312. "Non-nil means, show following heading when revealing a location.
  313. Org-mode often shows locations in an org-mode file which might have
  314. been invisible before. When this is set, the heading following the
  315. match is shown.
  316. Turning this off for example for sparse trees makes them very compact,
  317. but makes it harder to edit the location of the match. In such a case,
  318. use the command \\[org-reveal] to show more context.
  319. Instead of t, this can also be an alist specifying this option for different
  320. contexts. See `org-show-hierarchy-above' for valid contexts."
  321. :group 'org-reveal-location
  322. :type '(choice
  323. (const :tag "Always" t)
  324. (const :tag "Never" nil)
  325. (repeat :greedy t :tag "Individual contexts"
  326. (cons
  327. (choice :tag "Context"
  328. (const agenda)
  329. (const org-goto)
  330. (const occur-tree)
  331. (const tags-tree)
  332. (const link-search)
  333. (const mark-goto)
  334. (const bookmark-jump)
  335. (const isearch)
  336. (const default))
  337. (boolean)))))
  338. (defcustom org-show-siblings '((default . nil) (isearch t))
  339. "Non-nil means, show all sibling heading when revealing a location.
  340. Org-mode often shows locations in an org-mode file which might have
  341. been invisible before. When this is set, the sibling of the current entry
  342. heading are all made visible. If `org-show-hierarchy-above' is t,
  343. the same happens on each level of the hierarchy above the current entry.
  344. By default this is on for the isearch context, off for all other contexts.
  345. Turning this off for example for sparse trees makes them very compact,
  346. but makes it harder to edit the location of the match. In such a case,
  347. use the command \\[org-reveal] to show more context.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. See `org-show-hierarchy-above' for valid contexts."
  350. :group 'org-reveal-location
  351. :type '(choice
  352. (const :tag "Always" t)
  353. (const :tag "Never" nil)
  354. (repeat :greedy t :tag "Individual contexts"
  355. (cons
  356. (choice :tag "Context"
  357. (const agenda)
  358. (const org-goto)
  359. (const occur-tree)
  360. (const tags-tree)
  361. (const link-search)
  362. (const mark-goto)
  363. (const bookmark-jump)
  364. (const isearch)
  365. (const default))
  366. (boolean)))))
  367. (defgroup org-cycle nil
  368. "Options concerning visibility cycling in Org-mode."
  369. :tag "Org Cycle"
  370. :group 'org-structure)
  371. (defcustom org-drawers '("PROPERTIES")
  372. "Names of drawers. Drawers are not opened by cycling on the headline above.
  373. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  374. this:
  375. :DRAWERNAME:
  376. .....
  377. :END:
  378. The drawer \"PROPERTIES\" is special for capturing properties through
  379. the property API."
  380. :group 'org-structure
  381. :type '(repeat (string :tag "Drawer Name")))
  382. (defcustom org-cycle-global-at-bob nil
  383. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  384. This makes it possible to do global cycling without having to use S-TAB or
  385. C-u TAB. For this special case to work, the first line of the buffer
  386. must not be a headline - it may be empty ot some other text. When used in
  387. this way, `org-cycle-hook' is disables temporarily, to make sure the
  388. cursor stays at the beginning of the buffer.
  389. When this option is nil, don't do anything special at the beginning
  390. of the buffer."
  391. :group 'org-cycle
  392. :type 'boolean)
  393. (defcustom org-cycle-emulate-tab t
  394. "Where should `org-cycle' emulate TAB.
  395. nil Never
  396. white Only in completely white lines
  397. whitestart Only at the beginning of lines, before the first non-white char.
  398. t Everywhere except in headlines
  399. exc-hl-bol Everywhere except at the start of a headline
  400. If TAB is used in a place where it does not emulate TAB, the current subtree
  401. visibility is cycled."
  402. :group 'org-cycle
  403. :type '(choice (const :tag "Never" nil)
  404. (const :tag "Only in completely white lines" white)
  405. (const :tag "Before first char in a line" whitestart)
  406. (const :tag "Everywhere except in headlines" t)
  407. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  408. ))
  409. (defcustom org-cycle-separator-lines 2
  410. "Number of empty lines needed to keep an empty line between collapsed trees.
  411. If you leave an empty line between the end of a subtree and the following
  412. headline, this empty line is hidden when the subtree is folded.
  413. Org-mode will leave (exactly) one empty line visible if the number of
  414. empty lines is equal or larger to the number given in this variable.
  415. So the default 2 means, at least 2 empty lines after the end of a subtree
  416. are needed to produce free space between a collapsed subtree and the
  417. following headline.
  418. Special case: when 0, never leave empty lines in collapsed view."
  419. :group 'org-cycle
  420. :type 'integer)
  421. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  422. org-cycle-hide-drawers
  423. org-cycle-show-empty-lines
  424. org-optimize-window-after-visibility-change)
  425. "Hook that is run after `org-cycle' has changed the buffer visibility.
  426. The function(s) in this hook must accept a single argument which indicates
  427. the new state that was set by the most recent `org-cycle' command. The
  428. argument is a symbol. After a global state change, it can have the values
  429. `overview', `content', or `all'. After a local state change, it can have
  430. the values `folded', `children', or `subtree'."
  431. :group 'org-cycle
  432. :type 'hook)
  433. (defgroup org-edit-structure nil
  434. "Options concerning structure editing in Org-mode."
  435. :tag "Org Edit Structure"
  436. :group 'org-structure)
  437. (defcustom org-special-ctrl-a/e nil
  438. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  439. When t, `C-a' will bring back the cursor to the beginning of the
  440. headline text, i.e. after the stars and after a possible TODO keyword.
  441. In an item, this will be the position after the bullet.
  442. When the cursor is already at that position, another `C-a' will bring
  443. it to the beginning of the line.
  444. `C-e' will jump to the end of the headline, ignoring the presence of tags
  445. in the headline. A second `C-e' will then jump to the true end of the
  446. line, after any tags.
  447. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  448. and only a directly following, identical keypress will bring the cursor
  449. to the special positions."
  450. :group 'org-edit-structure
  451. :type '(choice
  452. (const :tag "off" nil)
  453. (const :tag "after bullet first" t)
  454. (const :tag "border first" reversed)))
  455. (if (fboundp 'defvaralias)
  456. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  457. (defcustom org-odd-levels-only nil
  458. "Non-nil means, skip even levels and only use odd levels for the outline.
  459. This has the effect that two stars are being added/taken away in
  460. promotion/demotion commands. It also influences how levels are
  461. handled by the exporters.
  462. Changing it requires restart of `font-lock-mode' to become effective
  463. for fontification also in regions already fontified.
  464. You may also set this on a per-file basis by adding one of the following
  465. lines to the buffer:
  466. #+STARTUP: odd
  467. #+STARTUP: oddeven"
  468. :group 'org-edit-structure
  469. :group 'org-font-lock
  470. :type 'boolean)
  471. (defcustom org-adapt-indentation t
  472. "Non-nil means, adapt indentation when promoting and demoting.
  473. When this is set and the *entire* text in an entry is indented, the
  474. indentation is increased by one space in a demotion command, and
  475. decreased by one in a promotion command. If any line in the entry
  476. body starts at column 0, indentation is not changed at all."
  477. :group 'org-edit-structure
  478. :type 'boolean)
  479. (defcustom org-blank-before-new-entry '((heading . nil)
  480. (plain-list-item . nil))
  481. "Should `org-insert-heading' leave a blank line before new heading/item?
  482. The value is an alist, with `heading' and `plain-list-item' as car,
  483. and a boolean flag as cdr."
  484. :group 'org-edit-structure
  485. :type '(list
  486. (cons (const heading) (boolean))
  487. (cons (const plain-list-item) (boolean))))
  488. (defcustom org-insert-heading-hook nil
  489. "Hook being run after inserting a new heading."
  490. :group 'org-edit-structure
  491. :type 'boolean)
  492. (defcustom org-enable-fixed-width-editor t
  493. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  494. This currently only means, they are never auto-wrapped.
  495. When nil, such lines will be treated like ordinary lines.
  496. See also the QUOTE keyword."
  497. :group 'org-edit-structure
  498. :type 'boolean)
  499. (defgroup org-sparse-trees nil
  500. "Options concerning sparse trees in Org-mode."
  501. :tag "Org Sparse Trees"
  502. :group 'org-structure)
  503. (defcustom org-highlight-sparse-tree-matches t
  504. "Non-nil means, highlight all matches that define a sparse tree.
  505. The highlights will automatically disappear the next time the buffer is
  506. changed by an edit command."
  507. :group 'org-sparse-trees
  508. :type 'boolean)
  509. (defcustom org-remove-highlights-with-change t
  510. "Non-nil means, any change to the buffer will remove temporary highlights.
  511. Such highlights are created by `org-occur' and `org-clock-display'.
  512. When nil, `C-c C-c needs to be used to get rid of the highlights.
  513. The highlights created by `org-preview-latex-fragment' always need
  514. `C-c C-c' to be removed."
  515. :group 'org-sparse-trees
  516. :group 'org-time
  517. :type 'boolean)
  518. (defcustom org-occur-hook '(org-first-headline-recenter)
  519. "Hook that is run after `org-occur' has constructed a sparse tree.
  520. This can be used to recenter the window to show as much of the structure
  521. as possible."
  522. :group 'org-sparse-trees
  523. :type 'hook)
  524. (defgroup org-plain-lists nil
  525. "Options concerning plain lists in Org-mode."
  526. :tag "Org Plain lists"
  527. :group 'org-structure)
  528. (defcustom org-cycle-include-plain-lists nil
  529. "Non-nil means, include plain lists into visibility cycling.
  530. This means that during cycling, plain list items will *temporarily* be
  531. interpreted as outline headlines with a level given by 1000+i where i is the
  532. indentation of the bullet. In all other operations, plain list items are
  533. not seen as headlines. For example, you cannot assign a TODO keyword to
  534. such an item."
  535. :group 'org-plain-lists
  536. :type 'boolean)
  537. (defcustom org-plain-list-ordered-item-terminator t
  538. "The character that makes a line with leading number an ordered list item.
  539. Valid values are ?. and ?\). To get both terminators, use t. While
  540. ?. may look nicer, it creates the danger that a line with leading
  541. number may be incorrectly interpreted as an item. ?\) therefore is
  542. the safe choice."
  543. :group 'org-plain-lists
  544. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  545. (const :tag "paren like in \"2)\"" ?\))
  546. (const :tab "both" t)))
  547. (defcustom org-auto-renumber-ordered-lists t
  548. "Non-nil means, automatically renumber ordered plain lists.
  549. Renumbering happens when the sequence have been changed with
  550. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  551. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  552. :group 'org-plain-lists
  553. :type 'boolean)
  554. (defcustom org-provide-checkbox-statistics t
  555. "Non-nil means, update checkbox statistics after insert and toggle.
  556. When this is set, checkbox statistics is updated each time you either insert
  557. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  558. with \\[org-ctrl-c-ctrl-c\\]."
  559. :group 'org-plain-lists
  560. :type 'boolean)
  561. (defgroup org-archive nil
  562. "Options concerning archiving in Org-mode."
  563. :tag "Org Archive"
  564. :group 'org-structure)
  565. (defcustom org-archive-tag "ARCHIVE"
  566. "The tag that marks a subtree as archived.
  567. An archived subtree does not open during visibility cycling, and does
  568. not contribute to the agenda listings."
  569. :group 'org-archive
  570. :group 'org-keywords
  571. :type 'string)
  572. (defcustom org-agenda-skip-archived-trees t
  573. "Non-nil means, the agenda will skip any items located in archived trees.
  574. An archived tree is a tree marked with the tag ARCHIVE."
  575. :group 'org-archive
  576. :group 'org-agenda-skip
  577. :type 'boolean)
  578. (defcustom org-cycle-open-archived-trees nil
  579. "Non-nil means, `org-cycle' will open archived trees.
  580. An archived tree is a tree marked with the tag ARCHIVE.
  581. When nil, archived trees will stay folded. You can still open them with
  582. normal outline commands like `show-all', but not with the cycling commands."
  583. :group 'org-archive
  584. :group 'org-cycle
  585. :type 'boolean)
  586. (defcustom org-sparse-tree-open-archived-trees nil
  587. "Non-nil means sparse tree construction shows matches in archived trees.
  588. When nil, matches in these trees are highlighted, but the trees are kept in
  589. collapsed state."
  590. :group 'org-archive
  591. :group 'org-sparse-trees
  592. :type 'boolean)
  593. (defcustom org-archive-location "%s_archive::"
  594. "The location where subtrees should be archived.
  595. This string consists of two parts, separated by a double-colon.
  596. The first part is a file name - when omitted, archiving happens in the same
  597. file. %s will be replaced by the current file name (without directory part).
  598. Archiving to a different file is useful to keep archived entries from
  599. contributing to the Org-mode Agenda.
  600. The part after the double colon is a headline. The archived entries will be
  601. filed under that headline. When omitted, the subtrees are simply filed away
  602. at the end of the file, as top-level entries.
  603. Here are a few examples:
  604. \"%s_archive::\"
  605. If the current file is Projects.org, archive in file
  606. Projects.org_archive, as top-level trees. This is the default.
  607. \"::* Archived Tasks\"
  608. Archive in the current file, under the top-level headline
  609. \"* Archived Tasks\".
  610. \"~/org/archive.org::\"
  611. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  612. \"basement::** Finished Tasks\"
  613. Archive in file ./basement (relative path), as level 3 trees
  614. below the level 2 heading \"** Finished Tasks\".
  615. You may set this option on a per-file basis by adding to the buffer a
  616. line like
  617. #+ARCHIVE: basement::** Finished Tasks"
  618. :group 'org-archive
  619. :type 'string)
  620. (defcustom org-archive-mark-done t
  621. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  622. This can be a string to set the keyword to use. When t, Org-mode will
  623. use the first keyword in its list that means done."
  624. :group 'org-archive
  625. :type '(choice
  626. (const :tag "No" nil)
  627. (const :tag "Yes" t)
  628. (string :tag "Use this keyword")))
  629. (defcustom org-archive-stamp-time t
  630. "Non-nil means, add a time stamp to entries moved to an archive file."
  631. :group 'org-archive
  632. :type 'boolean)
  633. (defcustom org-archive-save-context-info '(time file category todo itags)
  634. "Parts of context info that should be stored as properties when archiving.
  635. When a subtree is moved to an archive file, it looses information given by
  636. context, like inherited tags, the category, and possibly also the TODO
  637. state (depending on the variable `org-archive-mark-done').
  638. This variable can be a list of any of the following symbols:
  639. time The time of archiving.
  640. file The file where the entry originates.
  641. itags The local tags, in the headline of the subtree.
  642. ltags The tags the subtree inherits from further up the hierarchy.
  643. todo The pre-archive TODO state.
  644. category The category, taken from file name or #+CATEGORY lines.
  645. For each symbol present in the list, a property will be created in
  646. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  647. information."
  648. :group 'org-archive
  649. :type '(set
  650. (const :tag "File" file)
  651. (const :tag "Category" category)
  652. (const :tag "TODO state" todo)
  653. (const :tag "TODO state" priority)
  654. (const :tag "Inherited tags" itags)
  655. (const :tag "Local tags" ltags)))
  656. (defgroup org-table nil
  657. "Options concerning tables in Org-mode."
  658. :tag "Org Table"
  659. :group 'org)
  660. (defcustom org-enable-table-editor 'optimized
  661. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  662. When nil, such lines will be treated like ordinary lines.
  663. When equal to the symbol `optimized', the table editor will be optimized to
  664. do the following:
  665. - Automatic overwrite mode in front of whitespace in table fields.
  666. This makes the structure of the table stay in tact as long as the edited
  667. field does not exceed the column width.
  668. - Minimize the number of realigns. Normally, the table is aligned each time
  669. TAB or RET are pressed to move to another field. With optimization this
  670. happens only if changes to a field might have changed the column width.
  671. Optimization requires replacing the functions `self-insert-command',
  672. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  673. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  674. very good at guessing when a re-align will be necessary, but you can always
  675. force one with \\[org-ctrl-c-ctrl-c].
  676. If you would like to use the optimized version in Org-mode, but the
  677. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  678. This variable can be used to turn on and off the table editor during a session,
  679. but in order to toggle optimization, a restart is required.
  680. See also the variable `org-table-auto-blank-field'."
  681. :group 'org-table
  682. :type '(choice
  683. (const :tag "off" nil)
  684. (const :tag "on" t)
  685. (const :tag "on, optimized" optimized)))
  686. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  687. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  688. In the optimized version, the table editor takes over all simple keys that
  689. normally just insert a character. In tables, the characters are inserted
  690. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  691. for empty fields). Outside tables, the correct binding of the keys is
  692. restored.
  693. The default for this option is t if the optimized version is also used in
  694. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  695. this variable requires a restart of Emacs to become effective."
  696. :group 'org-table
  697. :type 'boolean)
  698. (defcustom orgtbl-radio-table-templates
  699. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  700. % END RECEIVE ORGTBL %n
  701. \\begin{comment}
  702. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  703. | | |
  704. \\end{comment}\n")
  705. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  706. @c END RECEIVE ORGTBL %n
  707. @ignore
  708. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  709. | | |
  710. @end ignore\n")
  711. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  712. <!-- END RECEIVE ORGTBL %n -->
  713. <!--
  714. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  715. | | |
  716. -->\n"))
  717. "Templates for radio tables in different major modes.
  718. All occurrences of %n in a template will be replaced with the name of the
  719. table, obtained by prompting the user."
  720. :group 'org-table
  721. :type '(repeat
  722. (list (symbol :tag "Major mode")
  723. (string :tag "Format"))))
  724. (defgroup org-table-settings nil
  725. "Settings for tables in Org-mode."
  726. :tag "Org Table Settings"
  727. :group 'org-table)
  728. (defcustom org-table-default-size "5x2"
  729. "The default size for newly created tables, Columns x Rows."
  730. :group 'org-table-settings
  731. :type 'string)
  732. (defcustom org-table-number-regexp
  733. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  734. "Regular expression for recognizing numbers in table columns.
  735. If a table column contains mostly numbers, it will be aligned to the
  736. right. If not, it will be aligned to the left.
  737. The default value of this option is a regular expression which allows
  738. anything which looks remotely like a number as used in scientific
  739. context. For example, all of the following will be considered a
  740. number:
  741. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  742. Other options offered by the customize interface are more restrictive."
  743. :group 'org-table-settings
  744. :type '(choice
  745. (const :tag "Positive Integers"
  746. "^[0-9]+$")
  747. (const :tag "Integers"
  748. "^[-+]?[0-9]+$")
  749. (const :tag "Floating Point Numbers"
  750. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  751. (const :tag "Floating Point Number or Integer"
  752. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  753. (const :tag "Exponential, Floating point, Integer"
  754. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  755. (const :tag "Very General Number-Like, including hex"
  756. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  757. (string :tag "Regexp:")))
  758. (defcustom org-table-number-fraction 0.5
  759. "Fraction of numbers in a column required to make the column align right.
  760. In a column all non-white fields are considered. If at least this
  761. fraction of fields is matched by `org-table-number-fraction',
  762. alignment to the right border applies."
  763. :group 'org-table-settings
  764. :type 'number)
  765. (defgroup org-table-editing nil
  766. "Bahavior of tables during editing in Org-mode."
  767. :tag "Org Table Editing"
  768. :group 'org-table)
  769. (defcustom org-table-automatic-realign t
  770. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  771. When nil, aligning is only done with \\[org-table-align], or after column
  772. removal/insertion."
  773. :group 'org-table-editing
  774. :type 'boolean)
  775. (defcustom org-table-auto-blank-field t
  776. "Non-nil means, automatically blank table field when starting to type into it.
  777. This only happens when typing immediately after a field motion
  778. command (TAB, S-TAB or RET).
  779. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  780. :group 'org-table-editing
  781. :type 'boolean)
  782. (defcustom org-table-tab-jumps-over-hlines t
  783. "Non-nil means, tab in the last column of a table with jump over a hline.
  784. If a horizontal separator line is following the current line,
  785. `org-table-next-field' can either create a new row before that line, or jump
  786. over the line. When this option is nil, a new line will be created before
  787. this line."
  788. :group 'org-table-editing
  789. :type 'boolean)
  790. (defcustom org-table-tab-recognizes-table.el t
  791. "Non-nil means, TAB will automatically notice a table.el table.
  792. When it sees such a table, it moves point into it and - if necessary -
  793. calls `table-recognize-table'."
  794. :group 'org-table-editing
  795. :type 'boolean)
  796. (defgroup org-table-calculation nil
  797. "Options concerning tables in Org-mode."
  798. :tag "Org Table Calculation"
  799. :group 'org-table)
  800. (defcustom org-table-use-standard-references t
  801. "Should org-mode work with table refrences like B3 instead of @3$2?
  802. Possible values are:
  803. nil never use them
  804. from accept as input, do not present for editing
  805. t: accept as input and present for editing"
  806. :group 'org-table-calculation
  807. :type '(choice
  808. (const :tag "Never, don't even check unser input for them" nil)
  809. (const :tag "Always, both as user input, and when editing" t)
  810. (const :tag "Convert user input, don't offer during editing" 'from)))
  811. (defcustom org-table-copy-increment t
  812. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  813. :group 'org-table-calculation
  814. :type 'boolean)
  815. (defcustom org-calc-default-modes
  816. '(calc-internal-prec 12
  817. calc-float-format (float 5)
  818. calc-angle-mode deg
  819. calc-prefer-frac nil
  820. calc-symbolic-mode nil
  821. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  822. calc-display-working-message t
  823. )
  824. "List with Calc mode settings for use in calc-eval for table formulas.
  825. The list must contain alternating symbols (Calc modes variables and values).
  826. Don't remove any of the default settings, just change the values. Org-mode
  827. relies on the variables to be present in the list."
  828. :group 'org-table-calculation
  829. :type 'plist)
  830. (defcustom org-table-formula-evaluate-inline t
  831. "Non-nil means, TAB and RET evaluate a formula in current table field.
  832. If the current field starts with an equal sign, it is assumed to be a formula
  833. which should be evaluated as described in the manual and in the documentation
  834. string of the command `org-table-eval-formula'. This feature requires the
  835. Emacs calc package.
  836. When this variable is nil, formula calculation is only available through
  837. the command \\[org-table-eval-formula]."
  838. :group 'org-table-calculation
  839. :type 'boolean)
  840. (defcustom org-table-formula-use-constants t
  841. "Non-nil means, interpret constants in formulas in tables.
  842. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  843. by the value given in `org-table-formula-constants', or by a value obtained
  844. from the `constants.el' package."
  845. :group 'org-table-calculation
  846. :type 'boolean)
  847. (defcustom org-table-formula-constants nil
  848. "Alist with constant names and values, for use in table formulas.
  849. The car of each element is a name of a constant, without the `$' before it.
  850. The cdr is the value as a string. For example, if you'd like to use the
  851. speed of light in a formula, you would configure
  852. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  853. and then use it in an equation like `$1*$c'.
  854. Constants can also be defined on a per-file basis using a line like
  855. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  856. :group 'org-table-calculation
  857. :type '(repeat
  858. (cons (string :tag "name")
  859. (string :tag "value"))))
  860. (defvar org-table-formula-constants-local nil
  861. "Local version of `org-table-formula-constants'.")
  862. (make-variable-buffer-local 'org-table-formula-constants-local)
  863. (defcustom org-table-allow-automatic-line-recalculation t
  864. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  865. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  866. :group 'org-table-calculation
  867. :type 'boolean)
  868. (defgroup org-link nil
  869. "Options concerning links in Org-mode."
  870. :tag "Org Link"
  871. :group 'org)
  872. (defvar org-link-abbrev-alist-local nil
  873. "Buffer-local version of `org-link-abbrev-alist', which see.
  874. The value of this is taken from the #+LINK lines.")
  875. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  876. (defcustom org-link-abbrev-alist nil
  877. "Alist of link abbreviations.
  878. The car of each element is a string, to be replaced at the start of a link.
  879. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  880. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  881. [[linkkey:tag][description]]
  882. If REPLACE is a string, the tag will simply be appended to create the link.
  883. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  884. also be a function that will be called with the tag as the only argument to
  885. create the link. See the manual for examples."
  886. :group 'org-link
  887. :type 'alist)
  888. (defcustom org-descriptive-links t
  889. "Non-nil means, hide link part and only show description of bracket links.
  890. Bracket links are like [[link][descritpion]]. This variable sets the initial
  891. state in new org-mode buffers. The setting can then be toggled on a
  892. per-buffer basis from the Org->Hyperlinks menu."
  893. :group 'org-link
  894. :type 'boolean)
  895. (defcustom org-link-file-path-type 'adaptive
  896. "How the path name in file links should be stored.
  897. Valid values are:
  898. relative relative to the current directory, i.e. the directory of the file
  899. into which the link is being inserted.
  900. absolute absolute path, if possible with ~ for home directory.
  901. noabbrev absolute path, no abbreviation of home directory.
  902. adaptive Use relative path for files in the current directory and sub-
  903. directories of it. For other files, use an absolute path."
  904. :group 'org-link
  905. :type '(choice
  906. (const relative)
  907. (const absolute)
  908. (const noabbrev)
  909. (const adaptive)))
  910. (defcustom org-activate-links '(bracket angle plain radio tag date)
  911. "Types of links that should be activated in Org-mode files.
  912. This is a list of symbols, each leading to the activation of a certain link
  913. type. In principle, it does not hurt to turn on most link types - there may
  914. be a small gain when turning off unused link types. The types are:
  915. bracket The recommended [[link][description]] or [[link]] links with hiding.
  916. angular Links in angular brackes that may contain whitespace like
  917. <bbdb:Carsten Dominik>.
  918. plain Plain links in normal text, no whitespace, like http://google.com.
  919. radio Text that is matched by a radio target, see manual for details.
  920. tag Tag settings in a headline (link to tag search).
  921. date Time stamps (link to calendar).
  922. Changing this variable requires a restart of Emacs to become effective."
  923. :group 'org-link
  924. :type '(set (const :tag "Double bracket links (new style)" bracket)
  925. (const :tag "Angular bracket links (old style)" angular)
  926. (const :tag "plain text links" plain)
  927. (const :tag "Radio target matches" radio)
  928. (const :tag "Tags" tag)
  929. (const :tag "Tags" target)
  930. (const :tag "Timestamps" date)))
  931. (defgroup org-link-store nil
  932. "Options concerning storing links in Org-mode"
  933. :tag "Org Store Link"
  934. :group 'org-link)
  935. (defcustom org-email-link-description-format "Email %c: %.30s"
  936. "Format of the description part of a link to an email or usenet message.
  937. The following %-excapes will be replaced by corresponding information:
  938. %F full \"From\" field
  939. %f name, taken from \"From\" field, address if no name
  940. %T full \"To\" field
  941. %t first name in \"To\" field, address if no name
  942. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  943. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  944. %s subject
  945. %m message-id.
  946. You may use normal field width specification between the % and the letter.
  947. This is for example useful to limit the length of the subject.
  948. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  949. :group 'org-link-store
  950. :type 'string)
  951. (defcustom org-from-is-user-regexp
  952. (let (r1 r2)
  953. (when (and user-mail-address (not (string= user-mail-address "")))
  954. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  955. (when (and user-full-name (not (string= user-full-name "")))
  956. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  957. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  958. "Regexp mached against the \"From:\" header of an email or usenet message.
  959. It should match if the message is from the user him/herself."
  960. :group 'org-link-store
  961. :type 'regexp)
  962. (defcustom org-context-in-file-links t
  963. "Non-nil means, file links from `org-store-link' contain context.
  964. A search string will be added to the file name with :: as separator and
  965. used to find the context when the link is activated by the command
  966. `org-open-at-point'.
  967. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  968. negates this setting for the duration of the command."
  969. :group 'org-link-store
  970. :type 'boolean)
  971. (defcustom org-keep-stored-link-after-insertion nil
  972. "Non-nil means, keep link in list for entire session.
  973. The command `org-store-link' adds a link pointing to the current
  974. location to an internal list. These links accumulate during a session.
  975. The command `org-insert-link' can be used to insert links into any
  976. Org-mode file (offering completion for all stored links). When this
  977. option is nil, every link which has been inserted once using \\[org-insert-link]
  978. will be removed from the list, to make completing the unused links
  979. more efficient."
  980. :group 'org-link-store
  981. :type 'boolean)
  982. (defcustom org-usenet-links-prefer-google nil
  983. "Non-nil means, `org-store-link' will create web links to Google groups.
  984. When nil, Gnus will be used for such links.
  985. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  986. negates this setting for the duration of the command."
  987. :group 'org-link-store
  988. :type 'boolean)
  989. (defgroup org-link-follow nil
  990. "Options concerning following links in Org-mode"
  991. :tag "Org Follow Link"
  992. :group 'org-link)
  993. (defcustom org-tab-follows-link nil
  994. "Non-nil means, on links TAB will follow the link.
  995. Needs to be set before org.el is loaded."
  996. :group 'org-link-follow
  997. :type 'boolean)
  998. (defcustom org-return-follows-link nil
  999. "Non-nil means, on links RET will follow the link.
  1000. Needs to be set before org.el is loaded."
  1001. :group 'org-link-follow
  1002. :type 'boolean)
  1003. (defcustom org-mouse-1-follows-link t
  1004. "Non-nil means, mouse-1 on a link will follow the link.
  1005. A longer mouse click will still set point. Does not wortk on XEmacs.
  1006. Needs to be set before org.el is loaded."
  1007. :group 'org-link-follow
  1008. :type 'boolean)
  1009. (defcustom org-mark-ring-length 4
  1010. "Number of different positions to be recorded in the ring
  1011. Changing this requires a restart of Emacs to work correctly."
  1012. :group 'org-link-follow
  1013. :type 'interger)
  1014. (defcustom org-link-frame-setup
  1015. '((vm . vm-visit-folder-other-frame)
  1016. (gnus . gnus-other-frame)
  1017. (file . find-file-other-window))
  1018. "Setup the frame configuration for following links.
  1019. When following a link with Emacs, it may often be useful to display
  1020. this link in another window or frame. This variable can be used to
  1021. set this up for the different types of links.
  1022. For VM, use any of
  1023. `vm-visit-folder'
  1024. `vm-visit-folder-other-frame'
  1025. For Gnus, use any of
  1026. `gnus'
  1027. `gnus-other-frame'
  1028. For FILE, use any of
  1029. `find-file'
  1030. `find-file-other-window'
  1031. `find-file-other-frame'
  1032. For the calendar, use the variable `calendar-setup'.
  1033. For BBDB, it is currently only possible to display the matches in
  1034. another window."
  1035. :group 'org-link-follow
  1036. :type '(list
  1037. (cons (const vm)
  1038. (choice
  1039. (const vm-visit-folder)
  1040. (const vm-visit-folder-other-window)
  1041. (const vm-visit-folder-other-frame)))
  1042. (cons (const gnus)
  1043. (choice
  1044. (const gnus)
  1045. (const gnus-other-frame)))
  1046. (cons (const file)
  1047. (choice
  1048. (const find-file)
  1049. (const find-file-other-window)
  1050. (const find-file-other-frame)))))
  1051. (defcustom org-display-internal-link-with-indirect-buffer nil
  1052. "Non-nil means, use indirect buffer to display infile links.
  1053. Activating internal links (from one location in a file to another location
  1054. in the same file) normally just jumps to the location. When the link is
  1055. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1056. another window. When this option is set, the other window actually displays
  1057. an indirect buffer clone of the current buffer, to avoid any visibility
  1058. changes to the current buffer."
  1059. :group 'org-link-follow
  1060. :type 'boolean)
  1061. (defcustom org-open-non-existing-files nil
  1062. "Non-nil means, `org-open-file' will open non-existing files.
  1063. When nil, an error will be generated."
  1064. :group 'org-link-follow
  1065. :type 'boolean)
  1066. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1067. "Function and arguments to call for following mailto links.
  1068. This is a list with the first element being a lisp function, and the
  1069. remaining elements being arguments to the function. In string arguments,
  1070. %a will be replaced by the address, and %s will be replaced by the subject
  1071. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1072. :group 'org-link-follow
  1073. :type '(choice
  1074. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1075. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1076. (const :tag "message-mail" (message-mail "%a" "%s"))
  1077. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1078. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1079. "Non-nil means, ask for confirmation before executing shell links.
  1080. Shell links can be dangerous, just thing about a link
  1081. [[shell:rm -rf ~/*][Google Search]]
  1082. This link would show up in your Org-mode document as \"Google Search\"
  1083. but really it would remove your entire home directory.
  1084. Therefore I *definitely* advise against setting this variable to nil.
  1085. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1086. rather than having to type \"yes\"."
  1087. :group 'org-link-follow
  1088. :type '(choice
  1089. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1090. (const :tag "with y-or-n (faster)" y-or-n-p)
  1091. (const :tag "no confirmation (dangerous)" nil)))
  1092. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1093. "Non-nil means, ask for confirmation before executing elisp links.
  1094. Elisp links can be dangerous, just think about a link
  1095. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1096. This link would show up in your Org-mode document as \"Google Search\"
  1097. but really it would remove your entire home directory.
  1098. Therefore I *definitely* advise against setting this variable to nil.
  1099. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1100. rather than having to type \"yes\"."
  1101. :group 'org-link-follow
  1102. :type '(choice
  1103. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1104. (const :tag "with y-or-n (faster)" y-or-n-p)
  1105. (const :tag "no confirmation (dangerous)" nil)))
  1106. (defconst org-file-apps-defaults-gnu
  1107. '((remote . emacs)
  1108. (t . mailcap))
  1109. "Default file applications on a UNIX or GNU/Linux system.
  1110. See `org-file-apps'.")
  1111. (defconst org-file-apps-defaults-macosx
  1112. '((remote . emacs)
  1113. (t . "open %s")
  1114. ("ps" . "gv %s")
  1115. ("ps.gz" . "gv %s")
  1116. ("eps" . "gv %s")
  1117. ("eps.gz" . "gv %s")
  1118. ("dvi" . "xdvi %s")
  1119. ("fig" . "xfig %s"))
  1120. "Default file applications on a MacOS X system.
  1121. The system \"open\" is known as a default, but we use X11 applications
  1122. for some files for which the OS does not have a good default.
  1123. See `org-file-apps'.")
  1124. (defconst org-file-apps-defaults-windowsnt
  1125. (list
  1126. '(remote . emacs)
  1127. (cons t
  1128. (list (if (featurep 'xemacs)
  1129. 'mswindows-shell-execute
  1130. 'w32-shell-execute)
  1131. "open" 'file)))
  1132. "Default file applications on a Windows NT system.
  1133. The system \"open\" is used for most files.
  1134. See `org-file-apps'.")
  1135. (defcustom org-file-apps
  1136. '(
  1137. ("txt" . emacs)
  1138. ("tex" . emacs)
  1139. ("ltx" . emacs)
  1140. ("org" . emacs)
  1141. ("el" . emacs)
  1142. ("bib" . emacs)
  1143. )
  1144. "External applications for opening `file:path' items in a document.
  1145. Org-mode uses system defaults for different file types, but
  1146. you can use this variable to set the application for a given file
  1147. extension. The entries in this list are cons cells where the car identifies
  1148. files and the cdr the corresponding command. Possible values for the
  1149. file identifier are
  1150. \"ext\" A string identifying an extension
  1151. `directory' Matches a directory
  1152. `remote' Matches a remote file, accessible through tramp or efs.
  1153. Remote files most likely should be visited through Emacs
  1154. because external applications cannot handle such paths.
  1155. t Default for all remaining files
  1156. Possible values for the command are:
  1157. `emacs' The file will be visited by the current Emacs process.
  1158. `default' Use the default application for this file type.
  1159. string A command to be executed by a shell; %s will be replaced
  1160. by the path to the file.
  1161. sexp A Lisp form which will be evaluated. The file path will
  1162. be available in the Lisp variable `file'.
  1163. For more examples, see the system specific constants
  1164. `org-file-apps-defaults-macosx'
  1165. `org-file-apps-defaults-windowsnt'
  1166. `org-file-apps-defaults-gnu'."
  1167. :group 'org-link-follow
  1168. :type '(repeat
  1169. (cons (choice :value ""
  1170. (string :tag "Extension")
  1171. (const :tag "Default for unrecognized files" t)
  1172. (const :tag "Remote file" remote)
  1173. (const :tag "Links to a directory" directory))
  1174. (choice :value ""
  1175. (const :tag "Visit with Emacs" emacs)
  1176. (const :tag "Use system default" default)
  1177. (string :tag "Command")
  1178. (sexp :tag "Lisp form")))))
  1179. (defcustom org-mhe-search-all-folders nil
  1180. "Non-nil means, that the search for the mh-message will be extended to
  1181. all folders if the message cannot be found in the folder given in the link.
  1182. Searching all folders is very efficient with one of the search engines
  1183. supported by MH-E, but will be slow with pick."
  1184. :group 'org-link-follow
  1185. :type 'boolean)
  1186. (defgroup org-remember nil
  1187. "Options concerning interaction with remember.el."
  1188. :tag "Org Remember"
  1189. :group 'org)
  1190. (defcustom org-directory "~/org"
  1191. "Directory with org files.
  1192. This directory will be used as default to prompt for org files.
  1193. Used by the hooks for remember.el."
  1194. :group 'org-remember
  1195. :type 'directory)
  1196. (defcustom org-default-notes-file "~/.notes"
  1197. "Default target for storing notes.
  1198. Used by the hooks for remember.el. This can be a string, or nil to mean
  1199. the value of `remember-data-file'.
  1200. You can set this on a per-template basis with the variable
  1201. `org-remember-templates'."
  1202. :group 'org-remember
  1203. :type '(choice
  1204. (const :tag "Default from remember-data-file" nil)
  1205. file))
  1206. (defcustom org-remember-store-without-prompt nil
  1207. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1208. In this case, you need `C-u C-c C-c' to get the prompts for
  1209. note file and headline.
  1210. When this variable is nil, `C-c C-c' give you the prompts, and
  1211. `C-u C-c C-c' trigger the fasttrack."
  1212. :group 'org-remember
  1213. :type 'boolean)
  1214. (defcustom org-remember-default-headline ""
  1215. "The headline that should be the default location in the notes file.
  1216. When filing remember notes, the cursor will start at that position.
  1217. You can set this on a per-template basis with the variable
  1218. `org-remember-templates'."
  1219. :group 'org-remember
  1220. :type 'string)
  1221. (defcustom org-remember-templates nil
  1222. "Templates for the creation of remember buffers.
  1223. When nil, just let remember make the buffer.
  1224. When not nil, this is a list of 4-element lists. In each entry, the first
  1225. element is a character, a unique key to select this template.
  1226. The second element is the template. The third element is optional and can
  1227. specify a destination file for remember items created with this template.
  1228. The default file is given by `org-default-notes-file'. An optional forth
  1229. element can specify the headline in that file that should be offered
  1230. first when the user is asked to file the entry. The default headline is
  1231. given in the variable `org-remember-default-headline'.
  1232. The template specifies the structure of the remember buffer. It should have
  1233. a first line starting with a star, to act as the org-mode headline.
  1234. Furthermore, the following %-escapes will be replaced with content:
  1235. %^{prompt} prompt the user for a string and replace this sequence with it.
  1236. %t time stamp, date only
  1237. %T time stamp with date and time
  1238. %u, %U like the above, but inactive time stamps
  1239. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1240. You may define a prompt like %^{Please specify birthday}t
  1241. %n user name (taken from `user-full-name')
  1242. %a annotation, normally the link created with org-store-link
  1243. %i initial content, the region when remember is called with C-u.
  1244. If %i is indented, the entire inserted text will be indented
  1245. as well.
  1246. %? After completing the template, position cursor here.
  1247. Apart from these general escapes, you can access information specific to the
  1248. link type that is created. For example, calling `remember' in emails or gnus
  1249. will record the author and the subject of the message, which you can access
  1250. with %:author and %:subject, respectively. Here is a complete list of what
  1251. is recorded for each link type.
  1252. Link type | Available information
  1253. -------------------+------------------------------------------------------
  1254. bbdb | %:type %:name %:company
  1255. vm, wl, mh, rmail | %:type %:subject %:message-id
  1256. | %:from %:fromname %:fromaddress
  1257. | %:to %:toname %:toaddress
  1258. | %:fromto (either \"to NAME\" or \"from NAME\")
  1259. gnus | %:group, for messages also all email fields
  1260. w3, w3m | %:type %:url
  1261. info | %:type %:file %:node
  1262. calendar | %:type %:date"
  1263. :group 'org-remember
  1264. :get (lambda (var) ; Make sure all entries have 4 elements
  1265. (mapcar (lambda (x)
  1266. (cond ((= (length x) 3) (append x '("")))
  1267. ((= (length x) 2) (append x '("" "")))
  1268. (t x)))
  1269. (default-value var)))
  1270. :type '(repeat
  1271. :tag "enabled"
  1272. (list :value (?a "\n" nil nil)
  1273. (character :tag "Selection Key")
  1274. (string :tag "Template")
  1275. (file :tag "Destination file (optional)")
  1276. (string :tag "Destination headline (optional)"))))
  1277. (defcustom org-reverse-note-order nil
  1278. "Non-nil means, store new notes at the beginning of a file or entry.
  1279. When nil, new notes will be filed to the end of a file or entry."
  1280. :group 'org-remember
  1281. :type '(choice
  1282. (const :tag "Reverse always" t)
  1283. (const :tag "Reverse never" nil)
  1284. (repeat :tag "By file name regexp"
  1285. (cons regexp boolean))))
  1286. (defgroup org-todo nil
  1287. "Options concerning TODO items in Org-mode."
  1288. :tag "Org TODO"
  1289. :group 'org)
  1290. (defgroup org-progress nil
  1291. "Options concerning Progress logging in Org-mode."
  1292. :tag "Org Progress"
  1293. :group 'org-time)
  1294. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1295. "List of TODO entry keyword sequences and their interpretation.
  1296. \\<org-mode-map>This is a list of sequences.
  1297. Each sequence starts with a symbol, either `sequence' or `type',
  1298. indicating if the keywords should be interpreted as a sequence of
  1299. action steps, or as different types of TODO items. The first
  1300. keywords are states requiring action - these states will select a headline
  1301. for inclusion into the global TODO list Org-mode produces. If one of
  1302. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1303. signify that no further action is necessary. If \"|\" is not found,
  1304. the last keyword is treated as the only DONE state of the sequence.
  1305. The command \\[org-todo] cycles an entry through these states, and one
  1306. additional state where no keyword is present. For details about this
  1307. cycling, see the manual.
  1308. TODO keywords and interpretation can also be set on a per-file basis with
  1309. the special #+SEQ_TODO and #+TYP_TODO lines.
  1310. For backward compatibility, this variable may also be just a list
  1311. of keywords - in this case the interptetation (sequence or type) will be
  1312. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1313. :group 'org-todo
  1314. :group 'org-keywords
  1315. :type '(choice
  1316. (repeat :tag "Old syntax, just keywords"
  1317. (string :tag "Keyword"))
  1318. (repeat :tag "New syntax"
  1319. (cons
  1320. (choice
  1321. :tag "Interpretation"
  1322. (const :tag "Sequence (cycling hits every state)" sequence)
  1323. (const :tag "Type (cycling directly to DONE)" type))
  1324. (repeat
  1325. (string :tag "Keyword"))))))
  1326. (defvar org-todo-keywords-1 nil)
  1327. (make-variable-buffer-local 'org-todo-keywords-1)
  1328. (defvar org-todo-tag-alist nil)
  1329. (make-variable-buffer-local 'org-todo-tag-alist)
  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-kwd-alist nil)
  1341. (make-variable-buffer-local 'org-todo-kwd-alist)
  1342. (defcustom org-todo-interpretation 'sequence
  1343. "Controls how TODO keywords are interpreted.
  1344. This variable is in principle obsolete and is only used for
  1345. backward compatibility, if the interpretation of todo keywords is
  1346. not given already in `org-todo-keywords'. See that variable for
  1347. more information."
  1348. :group 'org-todo
  1349. :group 'org-keywords
  1350. :type '(choice (const sequence)
  1351. (const type)))
  1352. (defcustom org-after-todo-state-change-hook nil
  1353. "Hook which is run after the state of a TODO item was changed.
  1354. The new state (a string with a TODO keyword, or nil) is available in the
  1355. Lisp variable `state'."
  1356. :group 'org-todo
  1357. :type 'hook)
  1358. (defcustom org-log-done nil
  1359. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1360. When the state of an entry is changed from nothing to TODO, remove a previous
  1361. closing date.
  1362. This can also be a list of symbols indicating under which conditions
  1363. the time stamp recording the action should be annotated with a short note.
  1364. Valid members of this list are
  1365. done Offer to record a note when marking entries done
  1366. state Offer to record a note whenever changing the TODO state
  1367. of an item. This is only relevant if TODO keywords are
  1368. interpreted as sequence, see variable `org-todo-interpretation'.
  1369. When `state' is set, this includes tracking `done'.
  1370. clock-out Offer to record a note when clocking out of an item.
  1371. A separate window will then pop up and allow you to type a note.
  1372. After finishing with C-c C-c, the note will be added directly after the
  1373. timestamp, as a plain list item. See also the variable
  1374. `org-log-note-headings'.
  1375. Logging can also be configured on a per-file basis by adding one of
  1376. the following lines anywhere in the buffer:
  1377. #+STARTUP: logdone
  1378. #+STARTUP: nologging
  1379. #+STARTUP: lognotedone
  1380. #+STARTUP: lognotestate
  1381. #+STARTUP: lognoteclock-out"
  1382. :group 'org-todo
  1383. :group 'org-progress
  1384. :type '(choice
  1385. (const :tag "off" nil)
  1386. (const :tag "on" t)
  1387. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1388. (const :tag "when item is marked DONE" done)
  1389. (const :tag "when TODO state changes" state)
  1390. (const :tag "when clocking out" clock-out))))
  1391. (defcustom org-log-done-with-time t
  1392. "Non-nil means, the CLOSED time stamp will contain date and time.
  1393. When nil, only the date will be recorded."
  1394. :group 'org-progress
  1395. :type 'boolean)
  1396. (defcustom org-log-note-headings
  1397. '((done . "CLOSING NOTE %t")
  1398. (state . "State %-12s %t")
  1399. (clock-out . ""))
  1400. "Headings for notes added when clocking out or closing TODO items.
  1401. The value is an alist, with the car being a symbol indicating the note
  1402. context, and the cdr is the heading to be used. The heading may also be the
  1403. empty string.
  1404. %t in the heading will be replaced by a time stamp.
  1405. %s will be replaced by the new TODO state, in double quotes.
  1406. %u will be replaced by the user name.
  1407. %U will be replaced by the full user name."
  1408. :group 'org-todo
  1409. :group 'org-progress
  1410. :type '(list :greedy t
  1411. (cons (const :tag "Heading when closing an item" done) string)
  1412. (cons (const :tag
  1413. "Heading when changing todo state (todo sequence only)"
  1414. state) string)
  1415. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1416. (defcustom org-log-states-order-reversed t
  1417. "Non-nil means, the latest state change note will be directly after heading.
  1418. When nil, the notes will be orderer according to time."
  1419. :group 'org-todo
  1420. :group 'org-progress
  1421. :type 'boolean)
  1422. (defcustom org-log-repeat t
  1423. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1424. When nil, no note will be taken."
  1425. :group 'org-todo
  1426. :group 'org-progress
  1427. :type 'boolean)
  1428. (defcustom org-clock-out-when-done t
  1429. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1430. Nil means, clock will keep running until stopped explicitly with
  1431. `C-c C-x C-o', or until the clock is started in a different item."
  1432. :group 'org-progress
  1433. :type 'boolean)
  1434. (defgroup org-priorities nil
  1435. "Priorities in Org-mode."
  1436. :tag "Org Priorities"
  1437. :group 'org-todo)
  1438. (defcustom org-highest-priority ?A
  1439. "The highest priority of TODO items. A character like ?A, ?B etc.
  1440. Must have a smaller ASCII number than `org-lowest-priority'."
  1441. :group 'org-priorities
  1442. :type 'character)
  1443. (defcustom org-lowest-priority ?C
  1444. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1445. Must have a larger ASCII number than `org-highest-priority'."
  1446. :group 'org-priorities
  1447. :type 'character)
  1448. (defcustom org-default-priority ?B
  1449. "The default priority of TODO items.
  1450. This is the priority an item get if no explicit priority is given."
  1451. :group 'org-priorities
  1452. :type 'character)
  1453. (defgroup org-time nil
  1454. "Options concerning time stamps and deadlines in Org-mode."
  1455. :tag "Org Time"
  1456. :group 'org)
  1457. (defcustom org-insert-labeled-timestamps-at-point nil
  1458. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1459. When nil, these labeled time stamps are forces into the second line of an
  1460. entry, just after the headline. When scheduling from the global TODO list,
  1461. the time stamp will always be forced into the second line."
  1462. :group 'org-time
  1463. :type 'boolean)
  1464. (defcustom org-insert-labeled-timestamps-before-properties-drawer t
  1465. "Non-nil means, always insert planning info before property drawer.
  1466. When this is nil and there is a property drawer *directly* after
  1467. the headline, move the planning info into the drawer. If the property
  1468. drawer separated from the headline by at least one line, this variable
  1469. has no effect."
  1470. :group 'org-time
  1471. :type 'boolean)
  1472. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1473. "Formats for `format-time-string' which are used for time stamps.
  1474. It is not recommended to change this constant.")
  1475. (defcustom org-time-stamp-rounding-minutes 0
  1476. "Number of minutes to round time stamps to upon insertion.
  1477. When zero, insert the time unmodified. Useful rounding numbers
  1478. should be factors of 60, so for example 5, 10, 15.
  1479. When this is not zero, you can still force an exact time-stamp by using
  1480. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1481. :group 'org-time
  1482. :type 'integer)
  1483. (defcustom org-display-custom-times nil
  1484. "Non-nil means, overlay custom formats over all time stamps.
  1485. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1486. To turn this on on a per-file basis, insert anywhere in the file:
  1487. #+STARTUP: customtime"
  1488. :group 'org-time
  1489. :set 'set-default
  1490. :type 'sexp)
  1491. (make-variable-buffer-local 'org-display-custom-times)
  1492. (defcustom org-time-stamp-custom-formats
  1493. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1494. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1495. These are overlayed over the default ISO format if the variable
  1496. `org-display-custom-times' is set. Time like %H:%M should be at the
  1497. end of the second format."
  1498. :group 'org-time
  1499. :type 'sexp)
  1500. (defun org-time-stamp-format (&optional long inactive)
  1501. "Get the right format for a time string."
  1502. (let ((f (if long (cdr org-time-stamp-formats)
  1503. (car org-time-stamp-formats))))
  1504. (if inactive
  1505. (concat "[" (substring f 1 -1) "]")
  1506. f)))
  1507. (defcustom org-deadline-warning-days 30
  1508. "No. of days before expiration during which a deadline becomes active.
  1509. This variable governs the display in sparse trees and in the agenda."
  1510. :group 'org-time
  1511. :type 'number)
  1512. (defcustom org-popup-calendar-for-date-prompt t
  1513. "Non-nil means, pop up a calendar when prompting for a date.
  1514. In the calendar, the date can be selected with mouse-1. However, the
  1515. minibuffer will also be active, and you can simply enter the date as well.
  1516. When nil, only the minibuffer will be available."
  1517. :group 'org-time
  1518. :type 'boolean)
  1519. (defcustom org-calendar-follow-timestamp-change t
  1520. "Non-nil means, make the calendar window follow timestamp changes.
  1521. When a timestamp is modified and the calendar window is visible, it will be
  1522. moved to the new date."
  1523. :group 'org-time
  1524. :type 'boolean)
  1525. (defgroup org-tags nil
  1526. "Options concerning tags in Org-mode."
  1527. :tag "Org Tags"
  1528. :group 'org)
  1529. (defcustom org-tag-alist nil
  1530. "List of tags allowed in Org-mode files.
  1531. When this list is nil, Org-mode will base TAG input on what is already in the
  1532. buffer.
  1533. The value of this variable is an alist, the car may be (and should) be a
  1534. character that is used to select that tag through the fast-tag-selection
  1535. interface. See the manual for details."
  1536. :group 'org-tags
  1537. :type '(repeat
  1538. (choice
  1539. (cons (string :tag "Tag name")
  1540. (character :tag "Access char"))
  1541. (const :tag "Start radio group" (:startgroup))
  1542. (const :tag "End radio group" (:endgroup)))))
  1543. (defcustom org-use-fast-tag-selection 'auto
  1544. "Non-nil means, use fast tag selection scheme.
  1545. This is a special interface to select and deselect tags with single keys.
  1546. When nil, fast selection is never used.
  1547. When the symbol `auto', fast selection is used if and only if selection
  1548. characters for tags have been configured, either through the variable
  1549. `org-tag-alist' or through a #+TAGS line in the buffer.
  1550. When t, fast selection is always used and selection keys are assigned
  1551. automatically if necessary."
  1552. :group 'org-tags
  1553. :type '(choice
  1554. (const :tag "Always" t)
  1555. (const :tag "Never" nil)
  1556. (const :tag "When selection characters are configured" 'auto)))
  1557. (defcustom org-fast-tag-selection-single-key nil
  1558. "Non-nil means, fast tag selection exits after first change.
  1559. When nil, you have to press RET to exit it.
  1560. During fast tag selection, you can toggle this flag with `C-c'.
  1561. This variable can also have the value `expert'. In this case, the window
  1562. displaying the tags menu is not even shown, until you press C-c again."
  1563. :group 'org-tags
  1564. :type '(choice
  1565. (const :tag "No" nil)
  1566. (const :tag "Yes" t)
  1567. (const :tag "Expert" expert)))
  1568. (defcustom org-tags-column 48
  1569. "The column to which tags should be indented in a headline.
  1570. If this number is positive, it specifies the column. If it is negative,
  1571. it means that the tags should be flushright to that column. For example,
  1572. -79 works well for a normal 80 character screen."
  1573. :group 'org-tags
  1574. :type 'integer)
  1575. (defcustom org-auto-align-tags t
  1576. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1577. These operations change the length of a headline and therefore shift
  1578. the tags around. With this options turned on, after each such operation
  1579. the tags are again aligned to `org-tags-column'."
  1580. :group 'org-tags
  1581. :type 'boolean)
  1582. (defcustom org-use-tag-inheritance t
  1583. "Non-nil means, tags in levels apply also for sublevels.
  1584. When nil, only the tags directly given in a specific line apply there.
  1585. If you turn off this option, you very likely want to turn on the
  1586. companion option `org-tags-match-list-sublevels'."
  1587. :group 'org-tags
  1588. :type 'boolean)
  1589. (defcustom org-tags-match-list-sublevels nil
  1590. "Non-nil means list also sublevels of headlines matching tag search.
  1591. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1592. the sublevels of a headline matching a tag search often also match
  1593. the same search. Listing all of them can create very long lists.
  1594. Setting this variable to nil causes subtrees of a match to be skipped.
  1595. This option is off by default, because inheritance in on. If you turn
  1596. inheritance off, you very likely want to turn this option on.
  1597. As a special case, if the tag search is restricted to TODO items, the
  1598. value of this variable is ignored and sublevels are always checked, to
  1599. make sure all corresponding TODO items find their way into the list."
  1600. :group 'org-tags
  1601. :type 'boolean)
  1602. (defvar org-tags-history nil
  1603. "History of minibuffer reads for tags.")
  1604. (defvar org-last-tags-completion-table nil
  1605. "The last used completion table for tags.")
  1606. (defgroup org-properties nil
  1607. "Options concerning properties in Org-mode."
  1608. :tag "Org Properties"
  1609. :group 'org)
  1610. (defcustom org-property-format "%-10s %s"
  1611. "How property key/value pairs should be formatted by `indent-line'.
  1612. When `indent-line' hits a property definition, it will format the line
  1613. according to this format, mainly to make sure that the values are
  1614. lined-up with respect to each other."
  1615. :group 'org-properties
  1616. :type 'string)
  1617. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1618. "The default column format, if no other format has been defined.
  1619. This variable can be set on the per-file basis by inserting a line
  1620. #+COLUMNS: %25ITEM ....."
  1621. :group 'org-properties
  1622. :type 'string)
  1623. (defcustom org-global-properties nil
  1624. "List of property/value pairs that can be inherited by any entry.
  1625. You can set buffer-local values for this by adding lines like
  1626. #+PROPERTY: NAME VALUE"
  1627. :group 'org-properties
  1628. :type '(repeat
  1629. (cons (string :tag "Property")
  1630. (string :tag "Value"))))
  1631. (defvar org-local-properties nil
  1632. "List of property/value pairs that can be inherited by any entry.
  1633. Valid for the current buffer.
  1634. This variable is populated from #+PROPERTY lines.")
  1635. (defgroup org-agenda nil
  1636. "Options concerning agenda views in Org-mode."
  1637. :tag "Org Agenda"
  1638. :group 'org)
  1639. (defvar org-category nil
  1640. "Variable used by org files to set a category for agenda display.
  1641. Such files should use a file variable to set it, for example
  1642. # -*- mode: org; org-category: \"ELisp\"
  1643. or contain a special line
  1644. #+CATEGORY: ELisp
  1645. If the file does not specify a category, then file's base name
  1646. is used instead.")
  1647. (make-variable-buffer-local 'org-category)
  1648. (defcustom org-agenda-files nil
  1649. "The files to be used for agenda display.
  1650. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1651. \\[org-remove-file]. You can also use customize to edit the list.
  1652. If the value of the variable is not a list but a single file name, then
  1653. the list of agenda files is actually stored and maintained in that file, one
  1654. agenda file per line."
  1655. :group 'org-agenda
  1656. :type '(choice
  1657. (repeat :tag "List of files" file)
  1658. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1659. (defcustom org-agenda-skip-unavailable-files nil
  1660. "t means to just skip non-reachable files in `org-agenda-files'.
  1661. Nil means to remove them, after a query, from the list."
  1662. :group 'org-agenda
  1663. :type 'boolean)
  1664. (defcustom org-agenda-confirm-kill 1
  1665. "When set, remote killing from the agenda buffer needs confirmation.
  1666. When t, a confirmation is always needed. When a number N, confirmation is
  1667. only needed when the text to be killed contains more than N non-white lines."
  1668. :group 'org-agenda
  1669. :type '(choice
  1670. (const :tag "Never" nil)
  1671. (const :tag "Always" t)
  1672. (number :tag "When more than N lines")))
  1673. (defcustom org-calendar-to-agenda-key [?c]
  1674. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1675. The command `org-calendar-goto-agenda' will be bound to this key. The
  1676. default is the character `c' because then `c' can be used to switch back and
  1677. forth between agenda and calendar."
  1678. :group 'org-agenda
  1679. :type 'sexp)
  1680. (defgroup org-agenda-export nil
  1681. "Options concerning exporting agenda views in Org-mode."
  1682. :tag "Org Agenda Export"
  1683. :group 'org-agenda)
  1684. (defcustom org-agenda-with-colors t
  1685. "Non-nil means, use colors in agenda views."
  1686. :group 'org-agenda-export
  1687. :type 'boolean)
  1688. (defcustom org-agenda-exporter-settings nil
  1689. "Alist of variable/value pairs that should be active during agenda export.
  1690. This is a good place to set uptions for ps-print and for htmlize."
  1691. :group 'org-agenda-export
  1692. :type '(repeat
  1693. (list
  1694. (variable)
  1695. (sexp :tag "Value"))))
  1696. (defcustom org-agenda-export-html-style ""
  1697. "The style specification for exported HTML Agenda files.
  1698. If this variable contains a string, it will replace the default <style>
  1699. section as produced by `htmlize'.
  1700. Since there are different ways of setting style information, this variable
  1701. needs to contain the full HTML structure to provide a style, including the
  1702. surrounding HTML tags. The style specifications should include definitions
  1703. the fonts used by the agenda, here is an example:
  1704. <style type=\"text/css\">
  1705. p { font-weight: normal; color: gray; }
  1706. .org-agenda-structure {
  1707. font-size: 110%;
  1708. color: #003399;
  1709. font-weight: 600;
  1710. }
  1711. .org-todo {
  1712. color: #cc6666;Week-agenda:
  1713. font-weight: bold;
  1714. }
  1715. .org-done {
  1716. color: #339933;
  1717. }
  1718. .title { text-align: center; }
  1719. .todo, .deadline { color: red; }
  1720. .done { color: green; }
  1721. </style>
  1722. or, if you want to keep the style in a file,
  1723. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1724. As the value of this option simply gets inserted into the HTML <head> header,
  1725. you can \"misuse\" it to also add other text to the header. However,
  1726. <style>...</style> is required, if not present the variable will be ignored."
  1727. :group 'org-agenda-export
  1728. :group 'org-export-html
  1729. :type 'string)
  1730. (defgroup org-agenda-custom-commands nil
  1731. "Options concerning agenda views in Org-mode."
  1732. :tag "Org Agenda Custom Commands"
  1733. :group 'org-agenda)
  1734. (defcustom org-agenda-custom-commands nil
  1735. "Custom commands for the agenda.
  1736. These commands will be offered on the splash screen displayed by the
  1737. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1738. (key type match options files)
  1739. key The key (a single char as a string) to be associated with the command.
  1740. type The command type, any of the following symbols:
  1741. todo Entries with a specific TODO keyword, in all agenda files.
  1742. tags Tags match in all agenda files.
  1743. tags-todo Tags match in all agenda files, TODO entries only.
  1744. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1745. tags-tree Sparse tree with all tags matches in *current* file.
  1746. occur-tree Occur sparse tree for *current* file.
  1747. match What to search for:
  1748. - a single keyword for TODO keyword searches
  1749. - a tags match expression for tags searches
  1750. - a regular expression for occur searches
  1751. options A list of option settings, similar to that in a let form, so like
  1752. this: ((opt1 val1) (opt2 val2) ...)
  1753. files A list of files file to write the produced agenda buffer to
  1754. with the command `org-store-agenda-views'.
  1755. If a file name ends in \".html\", an HTML version of the buffer
  1756. is written out. If it ends in \".ps\", a postscript version is
  1757. produced. Otherwide, only the plain text is written to the file.
  1758. You can also define a set of commands, to create a composite agenda buffer.
  1759. In this case, an entry looks like this:
  1760. (key desc (cmd1 cmd2 ...) general-options file)
  1761. where
  1762. desc A description string to be displayed in the dispatcher menu.
  1763. cmd An agenda command, similar to the above. However, tree commands
  1764. are no allowed, but instead you can get agenda and global todo list.
  1765. So valid commands for a set are:
  1766. (agenda)
  1767. (alltodo)
  1768. (stuck)
  1769. (todo \"match\" options files)
  1770. (tags \"match\" options files)
  1771. (tags-todo \"match\" options files)
  1772. Each command can carry a list of options, and another set of options can be
  1773. given for the whole set of commands. Individual command options take
  1774. precedence over the general options."
  1775. :group 'org-agenda-custom-commands
  1776. :type '(repeat
  1777. (choice :value ("a" tags "" nil)
  1778. (list :tag "Single command"
  1779. (string :tag "Key")
  1780. (choice
  1781. (const :tag "Agenda" agenda)
  1782. (const :tag "TODO list" alltodo)
  1783. (const :tag "Stuck projects" stuck)
  1784. (const :tag "Tags search (all agenda files)" tags)
  1785. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1786. (const :tag "TODO keyword search (all agenda files)" todo)
  1787. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1788. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1789. (const :tag "Occur tree (current buffer)" occur-tree)
  1790. (symbol :tag "Other, user-defined function"))
  1791. (string :tag "Match")
  1792. (repeat :tag "Local options"
  1793. (list (variable :tag "Option") (sexp :tag "Value")))
  1794. (option (repeat :tag "Export" (file :tag "Export to"))))
  1795. (list :tag "Command series, all agenda files"
  1796. (string :tag "Key")
  1797. (string :tag "Description")
  1798. (repeat
  1799. (choice
  1800. (const :tag "Agenda" (agenda))
  1801. (const :tag "TODO list" (alltodo))
  1802. (const :tag "Stuck projects" (stuck))
  1803. (list :tag "Tags search"
  1804. (const :format "" tags)
  1805. (string :tag "Match")
  1806. (repeat :tag "Local options"
  1807. (list (variable :tag "Option")
  1808. (sexp :tag "Value"))))
  1809. (list :tag "Tags search, TODO entries only"
  1810. (const :format "" tags-todo)
  1811. (string :tag "Match")
  1812. (repeat :tag "Local options"
  1813. (list (variable :tag "Option")
  1814. (sexp :tag "Value"))))
  1815. (list :tag "TODO keyword search"
  1816. (const :format "" todo)
  1817. (string :tag "Match")
  1818. (repeat :tag "Local options"
  1819. (list (variable :tag "Option")
  1820. (sexp :tag "Value"))))
  1821. (list :tag "Other, user-defined function"
  1822. (symbol :tag "function")
  1823. (string :tag "Match")
  1824. (repeat :tag "Local options"
  1825. (list (variable :tag "Option")
  1826. (sexp :tag "Value"))))))
  1827. (repeat :tag "General options"
  1828. (list (variable :tag "Option")
  1829. (sexp :tag "Value")))
  1830. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1831. (defcustom org-stuck-projects
  1832. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1833. "How to identify stuck projects.
  1834. This is a list of four items:
  1835. 1. A tags/todo matcher string that is used to identify a project.
  1836. The entire tree below a headline matched by this is considered one project.
  1837. 2. A list of TODO keywords identifying non-stuck projects.
  1838. If the project subtree contains any headline with one of these todo
  1839. keywords, the project is considered to be not stuck. If you specify
  1840. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1841. 3. A list of tags identifying non-stuck projects.
  1842. If the project subtree contains any headline with one of these tags,
  1843. the project is considered to be not stuck. If you specify \"*\" as
  1844. a tag, any tag will mark the project unstuck.
  1845. 4. An arbitrary regular expression matching non-stuck projects.
  1846. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1847. or `C-c a #' to produce the list."
  1848. :group 'org-agenda-custom-commands
  1849. :type '(list
  1850. (string :tag "Tags/TODO match to identify a project")
  1851. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1852. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1853. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1854. (defgroup org-agenda-skip nil
  1855. "Options concerning skipping parts of agenda files."
  1856. :tag "Org Agenda Skip"
  1857. :group 'org-agenda)
  1858. (defcustom org-agenda-todo-list-sublevels t
  1859. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1860. When nil, the sublevels of a TODO entry are not checked, resulting in
  1861. potentially much shorter TODO lists."
  1862. :group 'org-agenda-skip
  1863. :group 'org-todo
  1864. :type 'boolean)
  1865. (defcustom org-agenda-todo-ignore-scheduled nil
  1866. "Non-nil means, don't show scheduled entries in the global todo list.
  1867. The idea behind this is that by scheduling it, you have already taken care
  1868. of this item."
  1869. :group 'org-agenda-skip
  1870. :group 'org-todo
  1871. :type 'boolean)
  1872. (defcustom org-agenda-todo-ignore-deadlines nil
  1873. "Non-nil means, don't show near deadline entries in the global todo list.
  1874. Near means closer than `org-deadline-warning-days' days.
  1875. The idea behind this is that such items will appear in the agenda anyway."
  1876. :group 'org-agenda-skip
  1877. :group 'org-todo
  1878. :type 'boolean)
  1879. (defcustom org-agenda-skip-scheduled-if-done nil
  1880. "Non-nil means don't show scheduled items in agenda when they are done.
  1881. This is relevant for the daily/weekly agenda, not for the TODO list. And
  1882. it applied only to the actualy date of the scheduling. Warnings about
  1883. an item with a past scheduling dates are always turned off when the item
  1884. is DONE."
  1885. :group 'org-agenda-skip
  1886. :type 'boolean)
  1887. (defcustom org-agenda-skip-deadline-if-done nil
  1888. "Non-nil means don't show deadines when the corresponding item is done.
  1889. When nil, the deadline is still shown and should give you a happy feeling.
  1890. This is relevant for the daily/weekly agenda. And it applied only to the
  1891. actualy date of the deadline. Warnings about approching and past-due
  1892. deadlines are always turned off when the item is DONE."
  1893. :group 'org-agenda-skip
  1894. :type 'boolean)
  1895. (defcustom org-timeline-show-empty-dates 3
  1896. "Non-nil means, `org-timeline' also shows dates without an entry.
  1897. When nil, only the days which actually have entries are shown.
  1898. When t, all days between the first and the last date are shown.
  1899. When an integer, show also empty dates, but if there is a gap of more than
  1900. N days, just insert a special line indicating the size of the gap."
  1901. :group 'org-agenda-skip
  1902. :type '(choice
  1903. (const :tag "None" nil)
  1904. (const :tag "All" t)
  1905. (number :tag "at most")))
  1906. (defgroup org-agenda-startup nil
  1907. "Options concerning initial settings in the Agenda in Org Mode."
  1908. :tag "Org Agenda Startup"
  1909. :group 'org-agenda)
  1910. (defcustom org-finalize-agenda-hook nil
  1911. "Hook run just before displaying an agenda buffer."
  1912. :group 'org-agenda-startup
  1913. :type 'hook)
  1914. (defcustom org-agenda-mouse-1-follows-link nil
  1915. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1916. A longer mouse click will still set point. Does not wortk on XEmacs.
  1917. Needs to be set before org.el is loaded."
  1918. :group 'org-agenda-startup
  1919. :type 'boolean)
  1920. (defcustom org-agenda-start-with-follow-mode nil
  1921. "The initial value of follow-mode in a newly created agenda window."
  1922. :group 'org-agenda-startup
  1923. :type 'boolean)
  1924. (defgroup org-agenda-windows nil
  1925. "Options concerning the windows used by the Agenda in Org Mode."
  1926. :tag "Org Agenda Windows"
  1927. :group 'org-agenda)
  1928. (defcustom org-agenda-window-setup 'reorganize-frame
  1929. "How the agenda buffer should be displayed.
  1930. Possible values for this option are:
  1931. current-window Show agenda in the current window, keeping all other windows.
  1932. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1933. other-window Use `switch-to-buffer-other-window' to display agenda.
  1934. reorganize-frame Show only two windows on the current frame, the current
  1935. window and the agenda.
  1936. See also the variable `org-agenda-restore-windows-after-quit'."
  1937. :group 'org-agenda-windows
  1938. :type '(choice
  1939. (const current-window)
  1940. (const other-frame)
  1941. (const other-window)
  1942. (const reorganize-frame)))
  1943. (defcustom org-agenda-restore-windows-after-quit nil
  1944. "Non-nil means, restore window configuration open exiting agenda.
  1945. Before the window configuration is changed for displaying the agenda,
  1946. the current status is recorded. When the agenda is exited with
  1947. `q' or `x' and this option is set, the old state is restored. If
  1948. `org-agenda-window-setup' is `other-frame', the value of this
  1949. option will be ignored.."
  1950. :group 'org-agenda-windows
  1951. :type 'boolean)
  1952. (defcustom org-indirect-buffer-display 'other-window
  1953. "How should indirect tree buffers be displayed?
  1954. This applies to indirect buffers created with the commands
  1955. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1956. Valid values are:
  1957. current-window Display in the current window
  1958. other-window Just display in another window.
  1959. dedicated-frame Create one new frame, and re-use it each time.
  1960. new-frame Make a new frame each time."
  1961. :group 'org-structure
  1962. :group 'org-agenda-windows
  1963. :type '(choice
  1964. (const :tag "In current window" current-window)
  1965. (const :tag "In current frame, other window" other-window)
  1966. (const :tag "Each time a new frame" new-frame)
  1967. (const :tag "One dedicated frame" dedicated-frame)))
  1968. (defgroup org-agenda-daily/weekly nil
  1969. "Options concerning the daily/weekly agenda."
  1970. :tag "Org Agenda Daily/Weekly"
  1971. :group 'org-agenda)
  1972. (defcustom org-agenda-ndays 7
  1973. "Number of days to include in overview display.
  1974. Should be 1 or 7."
  1975. :group 'org-agenda-daily/weekly
  1976. :type 'number)
  1977. (defcustom org-agenda-start-on-weekday 1
  1978. "Non-nil means, start the overview always on the specified weekday.
  1979. 0 denotes Sunday, 1 denotes Monday etc.
  1980. When nil, always start on the current day."
  1981. :group 'org-agenda-daily/weekly
  1982. :type '(choice (const :tag "Today" nil)
  1983. (number :tag "Weekday No.")))
  1984. (defcustom org-agenda-show-all-dates t
  1985. "Non-nil means, `org-agenda' shows every day in the selected range.
  1986. When nil, only the days which actually have entries are shown."
  1987. :group 'org-agenda-daily/weekly
  1988. :type 'boolean)
  1989. (defcustom org-agenda-date-format "%A %d %B %Y"
  1990. "Format string for displaying dates in the agenda.
  1991. Used by the daily/weekly agenda and by the timeline. This should be
  1992. a format string understood by `format-time-string'.
  1993. FIXME: Not used currently, because of timezone problem."
  1994. :group 'org-agenda-daily/weekly
  1995. :type 'string)
  1996. (defcustom org-agenda-include-diary nil
  1997. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1998. :group 'org-agenda-daily/weekly
  1999. :type 'boolean)
  2000. (defcustom org-agenda-include-all-todo nil
  2001. "Set means weekly/daily agenda will always contain all TODO entries.
  2002. The TODO entries will be listed at the top of the agenda, before
  2003. the entries for specific days."
  2004. :group 'org-agenda-daily/weekly
  2005. :type 'boolean)
  2006. (defcustom org-agenda-repeating-timestamp-show-all t
  2007. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2008. When nil, only one occurence is shown, either today or the
  2009. nearest into the future."
  2010. :group 'org-agenda-daily/weekly
  2011. :type 'boolean)
  2012. (defgroup org-agenda-time-grid nil
  2013. "Options concerning the time grid in the Org-mode Agenda."
  2014. :tag "Org Agenda Time Grid"
  2015. :group 'org-agenda)
  2016. (defcustom org-agenda-use-time-grid t
  2017. "Non-nil means, show a time grid in the agenda schedule.
  2018. A time grid is a set of lines for specific times (like every two hours between
  2019. 8:00 and 20:00). The items scheduled for a day at specific times are
  2020. sorted in between these lines.
  2021. For details about when the grid will be shown, and what it will look like, see
  2022. the variable `org-agenda-time-grid'."
  2023. :group 'org-agenda-time-grid
  2024. :type 'boolean)
  2025. (defcustom org-agenda-time-grid
  2026. '((daily today require-timed)
  2027. "----------------"
  2028. (800 1000 1200 1400 1600 1800 2000))
  2029. "The settings for time grid for agenda display.
  2030. This is a list of three items. The first item is again a list. It contains
  2031. symbols specifying conditions when the grid should be displayed:
  2032. daily if the agenda shows a single day
  2033. weekly if the agenda shows an entire week
  2034. today show grid on current date, independent of daily/weekly display
  2035. require-timed show grid only if at least one item has a time specification
  2036. The second item is a string which will be places behing the grid time.
  2037. The third item is a list of integers, indicating the times that should have
  2038. a grid line."
  2039. :group 'org-agenda-time-grid
  2040. :type
  2041. '(list
  2042. (set :greedy t :tag "Grid Display Options"
  2043. (const :tag "Show grid in single day agenda display" daily)
  2044. (const :tag "Show grid in weekly agenda display" weekly)
  2045. (const :tag "Always show grid for today" today)
  2046. (const :tag "Show grid only if any timed entries are present"
  2047. require-timed)
  2048. (const :tag "Skip grid times already present in an entry"
  2049. remove-match))
  2050. (string :tag "Grid String")
  2051. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2052. (defgroup org-agenda-sorting nil
  2053. "Options concerning sorting in the Org-mode Agenda."
  2054. :tag "Org Agenda Sorting"
  2055. :group 'org-agenda)
  2056. (let ((sorting-choice
  2057. '(choice
  2058. (const time-up) (const time-down)
  2059. (const category-keep) (const category-up) (const category-down)
  2060. (const tag-down) (const tag-up)
  2061. (const priority-up) (const priority-down))))
  2062. (defcustom org-agenda-sorting-strategy
  2063. '((agenda time-up category-keep priority-down)
  2064. (todo category-keep priority-down)
  2065. (tags category-keep priority-down))
  2066. "Sorting structure for the agenda items of a single day.
  2067. This is a list of symbols which will be used in sequence to determine
  2068. if an entry should be listed before another entry. The following
  2069. symbols are recognized:
  2070. time-up Put entries with time-of-day indications first, early first
  2071. time-down Put entries with time-of-day indications first, late first
  2072. category-keep Keep the default order of categories, corresponding to the
  2073. sequence in `org-agenda-files'.
  2074. category-up Sort alphabetically by category, A-Z.
  2075. category-down Sort alphabetically by category, Z-A.
  2076. tag-up Sort alphabetically by last tag, A-Z.
  2077. tag-down Sort alphabetically by last tag, Z-A.
  2078. priority-up Sort numerically by priority, high priority last.
  2079. priority-down Sort numerically by priority, high priority first.
  2080. The different possibilities will be tried in sequence, and testing stops
  2081. if one comparison returns a \"not-equal\". For example, the default
  2082. '(time-up category-keep priority-down)
  2083. means: Pull out all entries having a specified time of day and sort them,
  2084. in order to make a time schedule for the current day the first thing in the
  2085. agenda listing for the day. Of the entries without a time indication, keep
  2086. the grouped in categories, don't sort the categories, but keep them in
  2087. the sequence given in `org-agenda-files'. Within each category sort by
  2088. priority.
  2089. Leaving out `category-keep' would mean that items will be sorted across
  2090. categories by priority."
  2091. :group 'org-agenda-sorting
  2092. :type `(choice
  2093. (repeat :tag "General" ,sorting-choice)
  2094. (list :tag "Individually"
  2095. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2096. (repeat ,sorting-choice))
  2097. (cons (const :tag "Strategy for TODO lists" todo)
  2098. (repeat ,sorting-choice))
  2099. (cons (const :tag "Strategy for Tags matches" tags)
  2100. (repeat ,sorting-choice))))))
  2101. (defcustom org-sort-agenda-notime-is-late t
  2102. "Non-nil means, items without time are considered late.
  2103. This is only relevant for sorting. When t, items which have no explicit
  2104. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2105. do have a time. When nil, the default time is before 0:00. You can use this
  2106. option to decide if the schedule for today should come before or after timeless
  2107. agenda entries."
  2108. :group 'org-agenda-sorting
  2109. :type 'boolean)
  2110. (defgroup org-agenda-prefix nil
  2111. "Options concerning the entry prefix in the Org-mode agenda display."
  2112. :tag "Org Agenda Prefix"
  2113. :group 'org-agenda)
  2114. (defcustom org-agenda-prefix-format
  2115. '((agenda . " %-12:c%?-12t% s")
  2116. (timeline . " % s")
  2117. (todo . " %-12:c")
  2118. (tags . " %-12:c"))
  2119. "Format specifications for the prefix of items in the agenda views.
  2120. An alist with four entries, for the different agenda types. The keys to the
  2121. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2122. are format strings.
  2123. This format works similar to a printf format, with the following meaning:
  2124. %c the category of the item, \"Diary\" for entries from the diary, or
  2125. as given by the CATEGORY keyword or derived from the file name.
  2126. %T the *last* tag of the item. Last because inherited tags come
  2127. first in the list.
  2128. %t the time-of-day specification if one applies to the entry, in the
  2129. format HH:MM
  2130. %s Scheduling/Deadline information, a short string
  2131. All specifiers work basically like the standard `%s' of printf, but may
  2132. contain two additional characters: A question mark just after the `%' and
  2133. a whitespace/punctuation character just before the final letter.
  2134. If the first character after `%' is a question mark, the entire field
  2135. will only be included if the corresponding value applies to the
  2136. current entry. This is useful for fields which should have fixed
  2137. width when present, but zero width when absent. For example,
  2138. \"%?-12t\" will result in a 12 character time field if a time of the
  2139. day is specified, but will completely disappear in entries which do
  2140. not contain a time.
  2141. If there is punctuation or whitespace character just before the final
  2142. format letter, this character will be appended to the field value if
  2143. the value is not empty. For example, the format \"%-12:c\" leads to
  2144. \"Diary: \" if the category is \"Diary\". If the category were be
  2145. empty, no additional colon would be interted.
  2146. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2147. - Indent the line with two space characters
  2148. - Give the category in a 12 chars wide field, padded with whitespace on
  2149. the right (because of `-'). Append a colon if there is a category
  2150. (because of `:').
  2151. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2152. time, don't put in an empty field, just skip it (because of '?').
  2153. - Finally, put the scheduling information and append a whitespace.
  2154. As another example, if you don't want the time-of-day of entries in
  2155. the prefix, you could use:
  2156. (setq org-agenda-prefix-format \" %-11:c% s\")
  2157. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2158. `org-agenda-remove-tags'."
  2159. :type '(choice
  2160. (string :tag "General format")
  2161. (list :greedy t :tag "View dependent"
  2162. (cons (const agenda) (string :tag "Format"))
  2163. (cons (const timeline) (string :tag "Format"))
  2164. (cons (const todo) (string :tag "Format"))
  2165. (cons (const tags) (string :tag "Format"))))
  2166. :group 'org-agenda-prefix)
  2167. (defvar org-prefix-format-compiled nil
  2168. "The compiled version of the most recently used prefix format.
  2169. See the variable `org-agenda-prefix-format'.")
  2170. (defcustom org-agenda-remove-times-when-in-prefix t
  2171. "Non-nil means, remove duplicate time specifications in agenda items.
  2172. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2173. time-of-day specification in a headline or diary entry is extracted and
  2174. placed into the prefix. If this option is non-nil, the original specification
  2175. \(a timestamp or -range, or just a plain time(range) specification like
  2176. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2177. cluttered.
  2178. The option can be t or nil. It may also be the symbol `beg', indicating
  2179. that the time should only be removed what it is located at the beginning of
  2180. the headline/diary entry."
  2181. :group 'org-agenda-prefix
  2182. :type '(choice
  2183. (const :tag "Always" t)
  2184. (const :tag "Never" nil)
  2185. (const :tag "When at beginning of entry" beg)))
  2186. (defcustom org-agenda-default-appointment-duration nil
  2187. "Default duration for appointments that only have a starting time.
  2188. When nil, no duration is specified in such cases.
  2189. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2190. :group 'org-agenda-prefix
  2191. :type '(choice
  2192. (integer :tag "Minutes")
  2193. (const :tag "No default duration")))
  2194. (defcustom org-agenda-remove-tags nil
  2195. "Non-nil means, remove the tags from the headline copy in the agenda.
  2196. When this is the symbol `prefix', only remove tags when
  2197. `org-agenda-prefix-format' contains a `%T' specifier."
  2198. :group 'org-agenda-prefix
  2199. :type '(choice
  2200. (const :tag "Always" t)
  2201. (const :tag "Never" nil)
  2202. (const :tag "When prefix format contains %T" prefix)))
  2203. (if (fboundp 'defvaralias)
  2204. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2205. 'org-agenda-remove-tags))
  2206. (defcustom org-agenda-align-tags-to-column 65
  2207. "Shift tags in agenda items to this column."
  2208. :group 'org-agenda-prefix
  2209. :type 'integer)
  2210. (defgroup org-latex nil
  2211. "Options for embedding LaTeX code into Org-mode"
  2212. :tag "Org LaTeX"
  2213. :group 'org)
  2214. (defcustom org-format-latex-options
  2215. '(:foreground default :background default :scale 1.0
  2216. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2217. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2218. "Options for creating images from LaTeX fragments.
  2219. This is a property list with the following properties:
  2220. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2221. `default' means use the forground of the default face.
  2222. :background the background color, or \"Transparent\".
  2223. `default' means use the background of the default face.
  2224. :scale a scaling factor for the size of the images
  2225. :html-foreground, :html-background, :html-scale
  2226. The same numbers for HTML export.
  2227. :matchers a list indicating which matchers should be used to
  2228. find LaTeX fragments. Valid members of this list are:
  2229. \"begin\" find environments
  2230. \"$\" find math expressions surrounded by $...$
  2231. \"$$\" find math expressions surrounded by $$....$$
  2232. \"\\(\" find math expressions surrounded by \\(...\\)
  2233. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2234. :group 'org-export-latex
  2235. :type 'plist)
  2236. (defcustom org-format-latex-header "\\documentclass{article}
  2237. \\usepackage{fullpage} % do not remove
  2238. \\usepackage{amssymb}
  2239. \\usepackage[usenames]{color}
  2240. \\usepackage{amsmath}
  2241. \\usepackage{latexsym}
  2242. \\usepackage[mathscr]{eucal}
  2243. \\pagestyle{empty} % do not remove"
  2244. "The document header used for processing LaTeX fragments."
  2245. :group 'org-export-latex
  2246. :type 'string)
  2247. (defgroup org-export nil
  2248. "Options for exporting org-listings."
  2249. :tag "Org Export"
  2250. :group 'org)
  2251. (defgroup org-export-general nil
  2252. "General options for exporting Org-mode files."
  2253. :tag "Org Export General"
  2254. :group 'org-export)
  2255. (defcustom org-export-publishing-directory "."
  2256. "Path to the location where exported files should be located.
  2257. This path may be relative to the directory where the Org-mode file lives.
  2258. The default is to put them into the same directory as the Org-mode file.
  2259. The variable may also be an alist with export types `:html', `:ascii',
  2260. `:ical', `:LaTeX', or `:xoxo' and the corresponding directories.
  2261. If a directory path is relative, it is interpreted relative to the
  2262. directory where the exported Org-mode files lives."
  2263. :group 'org-export-general
  2264. :type '(choice
  2265. (directory)
  2266. (repeat
  2267. (cons
  2268. (choice :tag "Type"
  2269. (const :html) (const :LaTeX)
  2270. (const :ascii) (const :ical) (const :xoxo))
  2271. (directory)))))
  2272. (defcustom org-export-language-setup
  2273. '(("en" "Author" "Date" "Table of Contents")
  2274. ("cs" "Autor" "Datum" "Obsah")
  2275. ("da" "Ophavsmand" "Dato" "Indhold")
  2276. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2277. ("es" "Autor" "Fecha" "\xccndice")
  2278. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  2279. ("it" "Autore" "Data" "Indice")
  2280. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2281. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2282. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2283. "Terms used in export text, translated to different languages.
  2284. Use the variable `org-export-default-language' to set the language,
  2285. or use the +OPTION lines for a per-file setting."
  2286. :group 'org-export-general
  2287. :type '(repeat
  2288. (list
  2289. (string :tag "HTML language tag")
  2290. (string :tag "Author")
  2291. (string :tag "Date")
  2292. (string :tag "Table of Contents"))))
  2293. (defcustom org-export-default-language "en"
  2294. "The default language of HTML export, as a string.
  2295. This should have an association in `org-export-language-setup'."
  2296. :group 'org-export-general
  2297. :type 'string)
  2298. (defcustom org-export-skip-text-before-1st-heading t
  2299. "Non-nil means, skip all text before the first headline when exporting.
  2300. When nil, that text is exported as well."
  2301. :group 'org-export-general
  2302. :type 'boolean)
  2303. (defcustom org-export-headline-levels 3
  2304. "The last level which is still exported as a headline.
  2305. Inferior levels will produce itemize lists when exported.
  2306. Note that a numeric prefix argument to an exporter function overrides
  2307. this setting.
  2308. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2309. :group 'org-export-general
  2310. :type 'number)
  2311. (defcustom org-export-with-section-numbers t
  2312. "Non-nil means, add section numbers to headlines when exporting.
  2313. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2314. :group 'org-export-general
  2315. :type 'boolean)
  2316. (defcustom org-export-with-toc t
  2317. "Non-nil means, create a table of contents in exported files.
  2318. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2319. When an integer, include levels up to N in the toc, this may then be
  2320. different from `org-export-headline-levels', but it will not be allowed
  2321. to be larger than the number of headline levels.
  2322. When nil, no table of contents is made.
  2323. Headlines which contain any TODO items will be marked with \"(*)\" in
  2324. ASCII export, and with red color in HTML output, if the option
  2325. `org-export-mark-todo-in-toc' is set.
  2326. In HTML output, the TOC will be clickable.
  2327. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2328. or \"toc:3\"."
  2329. :group 'org-export-general
  2330. :type '(choice
  2331. (const :tag "No Table of Contents" nil)
  2332. (const :tag "Full Table of Contents" t)
  2333. (integer :tag "TOC to level")))
  2334. (defcustom org-export-mark-todo-in-toc nil
  2335. "Non-nil means, mark TOC lines that contain any open TODO items."
  2336. :group 'org-export-general
  2337. :type 'boolean)
  2338. (defcustom org-export-preserve-breaks nil
  2339. "Non-nil means, preserve all line breaks when exporting.
  2340. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2341. export, line breaks will always be preserved, regardless of this variable.
  2342. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2343. :group 'org-export-general
  2344. :type 'boolean)
  2345. (defcustom org-export-with-archived-trees 'headline
  2346. "Whether subtrees with the ARCHIVE tag should be exported.
  2347. This can have three different values
  2348. nil Do not export, pretend this tree is not present
  2349. t Do export the entire tree
  2350. headline Only export the headline, but skip the tree below it."
  2351. :group 'org-export-general
  2352. :group 'org-archive
  2353. :type '(choice
  2354. (const :tag "not at all" nil)
  2355. (const :tag "headline only" 'headline)
  2356. (const :tag "entirely" t)))
  2357. (defcustom org-export-author-info t
  2358. "Non-nil means, insert author name and email into the exported file.
  2359. This option can also be set with the +OPTIONS line,
  2360. e.g. \"author-info:nil\"."
  2361. :group 'org-export-general
  2362. :type 'boolean)
  2363. (defcustom org-export-time-stamp-file t
  2364. "Non-nil means, insert a time stamp into the exported file.
  2365. The time stamp shows when the file was created.
  2366. This option can also be set with the +OPTIONS line,
  2367. e.g. \"timestamp:nil\"."
  2368. :group 'org-export-general
  2369. :type 'boolean)
  2370. (defcustom org-export-with-timestamps t
  2371. "If nil, do not export time stamps and associated keywords."
  2372. :group 'org-export-general
  2373. :type 'boolean)
  2374. (defcustom org-export-remove-timestamps-from-toc t
  2375. "If nil, remove timestamps from the table of contents entries."
  2376. :group 'org-export-general
  2377. :type 'boolean)
  2378. (defcustom org-export-with-tags 'not-in-toc
  2379. "If nil, do not export tags, just remove them from headlines.
  2380. If this is the symbol `not-in-toc', tags will be removed from table of
  2381. contents entries, but still be shown in the headlines of the document."
  2382. :group 'org-export-general
  2383. :type '(choice
  2384. (const :tag "Off" nil)
  2385. (const :tag "Not in TOC" not-in-toc)
  2386. (const :tag "On" t)))
  2387. (defcustom org-export-with-property-drawer nil
  2388. "Non-nil means, export property drawers.
  2389. When nil, these drawers are removed before export.
  2390. This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
  2391. :group 'org-export-general
  2392. :type 'boolean)
  2393. (defgroup org-export-translation nil
  2394. "Options for translating special ascii sequences for the export backends."
  2395. :tag "Org Export Translation"
  2396. :group 'org-export)
  2397. (defcustom org-export-with-emphasize t
  2398. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2399. If the export target supports emphasizing text, the word will be
  2400. typeset in bold, italic, or underlined, respectively. Works only for
  2401. single words, but you can say: I *really* *mean* *this*.
  2402. Not all export backends support this.
  2403. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2404. :group 'org-export-translation
  2405. :type 'boolean)
  2406. (defcustom org-export-with-footnotes t
  2407. "If nil, export [1] as a footnote marker.
  2408. Lines starting with [1] will be formatted as footnotes.
  2409. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2410. :group 'org-export-translation
  2411. :type 'boolean)
  2412. (defcustom org-export-with-sub-superscripts t
  2413. "Non-nil means, interpret \"_\" and \"^\" for export.
  2414. When this option is turned on, you can use TeX-like syntax for sub- and
  2415. superscripts. Several characters after \"_\" or \"^\" will be
  2416. considered as a single item - so grouping with {} is normally not
  2417. needed. For example, the following things will be parsed as single
  2418. sub- or superscripts.
  2419. 10^24 or 10^tau several digits will be considered 1 item.
  2420. 10^-12 or 10^-tau a leading sign with digits or a word
  2421. x^2-y^3 will be read as x^2 - y^3, because items are
  2422. terminated by almost any nonword/nondigit char.
  2423. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2424. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2425. sub/superscript. If you set this variable to the symbol `{}',
  2426. the braces are *required* in order to trigger interpretations as
  2427. sub/superscript. This can be helpful in documents that need \"_\"
  2428. frequently in plain text.
  2429. Not all export backends support this, but HTML does.
  2430. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2431. :group 'org-export-translation
  2432. :type '(choice
  2433. (const :tag "Always interpret" t)
  2434. (const :tag "Only with braces" {})
  2435. (const :tag "Never interpret" nil)))
  2436. (defcustom org-export-with-TeX-macros t
  2437. "Non-nil means, interpret simple TeX-like macros when exporting.
  2438. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2439. No only real TeX macros will work here, but the standard HTML entities
  2440. for math can be used as macro names as well. For a list of supported
  2441. names in HTML export, see the constant `org-html-entities'.
  2442. Not all export backends support this.
  2443. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2444. :group 'org-export-translation
  2445. :group 'org-export-latex
  2446. :type 'boolean)
  2447. (defcustom org-export-with-LaTeX-fragments nil
  2448. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2449. When set, the exporter will find LaTeX environments if the \\begin line is
  2450. the first non-white thing on a line. It will also find the math delimiters
  2451. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2452. display math.
  2453. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2454. :group 'org-export-translation
  2455. :group 'org-export-latex
  2456. :type 'boolean)
  2457. (defcustom org-export-with-fixed-width t
  2458. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2459. This can be used to have pre-formatted text, fragments of code etc. For
  2460. example:
  2461. : ;; Some Lisp examples
  2462. : (while (defc cnt)
  2463. : (ding))
  2464. will be looking just like this in also HTML. See also the QUOTE keyword.
  2465. Not all export backends support this.
  2466. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2467. :group 'org-export-translation
  2468. :type 'boolean)
  2469. (defcustom org-match-sexp-depth 3
  2470. "Number of stacked braces for sub/superscript matching.
  2471. This has to be set before loading org.el to be effective."
  2472. :group 'org-export-translation
  2473. :type 'integer)
  2474. (defgroup org-export-tables nil
  2475. "Options for exporting tables in Org-mode."
  2476. :tag "Org Export Tables"
  2477. :group 'org-export)
  2478. (defcustom org-export-with-tables t
  2479. "If non-nil, lines starting with \"|\" define a table.
  2480. For example:
  2481. | Name | Address | Birthday |
  2482. |-------------+----------+-----------|
  2483. | Arthur Dent | England | 29.2.2100 |
  2484. Not all export backends support this.
  2485. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2486. :group 'org-export-tables
  2487. :type 'boolean)
  2488. (defcustom org-export-highlight-first-table-line t
  2489. "Non-nil means, highlight the first table line.
  2490. In HTML export, this means use <th> instead of <td>.
  2491. In tables created with table.el, this applies to the first table line.
  2492. In Org-mode tables, all lines before the first horizontal separator
  2493. line will be formatted with <th> tags."
  2494. :group 'org-export-tables
  2495. :type 'boolean)
  2496. (defcustom org-export-table-remove-special-lines t
  2497. "Remove special lines and marking characters in calculating tables.
  2498. This removes the special marking character column from tables that are set
  2499. up for spreadsheet calculations. It also removes the entire lines
  2500. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2501. the values of constants may be useful to have."
  2502. :group 'org-export-tables
  2503. :type 'boolean)
  2504. (defcustom org-export-prefer-native-exporter-for-tables nil
  2505. "Non-nil means, always export tables created with table.el natively.
  2506. Natively means, use the HTML code generator in table.el.
  2507. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2508. the table does not use row- or column-spanning). This has the
  2509. advantage, that the automatic HTML conversions for math symbols and
  2510. sub/superscripts can be applied. Org-mode's HTML generator is also
  2511. much faster."
  2512. :group 'org-export-tables
  2513. :type 'boolean)
  2514. (defgroup org-export-ascii nil
  2515. "Options specific for ASCII export of Org-mode files."
  2516. :tag "Org Export ASCII"
  2517. :group 'org-export)
  2518. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2519. "Characters for underlining headings in ASCII export.
  2520. In the given sequence, these characters will be used for level 1, 2, ..."
  2521. :group 'org-export-ascii
  2522. :type '(repeat character))
  2523. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2524. "Bullet characters for headlines converted to lists in ASCII export.
  2525. The first character is is used for the first lest level generated in this
  2526. way, and so on. If there are more levels than characters given here,
  2527. the list will be repeated.
  2528. Note that plain lists will keep the same bullets as the have in the
  2529. Org-mode file."
  2530. :group 'org-export-ascii
  2531. :type '(repeat character))
  2532. (defgroup org-export-xml nil
  2533. "Options specific for XML export of Org-mode files."
  2534. :tag "Org Export XML"
  2535. :group 'org-export)
  2536. (defgroup org-export-html nil
  2537. "Options specific for HTML export of Org-mode files."
  2538. :tag "Org Export HTML"
  2539. :group 'org-export)
  2540. (defcustom org-export-html-coding-system nil
  2541. ""
  2542. :group 'org-export-html
  2543. :type 'coding-system)
  2544. (defcustom org-export-html-style
  2545. "<style type=\"text/css\">
  2546. html {
  2547. font-family: Times, serif;
  2548. font-size: 12pt;
  2549. }
  2550. .title { text-align: center; }
  2551. .todo { color: red; }
  2552. .done { color: green; }
  2553. .timestamp { color: grey }
  2554. .timestamp-kwd { color: CadetBlue }
  2555. .tag { background-color:lightblue; font-weight:normal }
  2556. .target { background-color: lavender; }
  2557. pre {
  2558. border: 1pt solid #AEBDCC;
  2559. background-color: #F3F5F7;
  2560. padding: 5pt;
  2561. font-family: courier, monospace;
  2562. }
  2563. table { border-collapse: collapse; }
  2564. td, th {
  2565. vertical-align: top;
  2566. <!--border: 1pt solid #ADB9CC;-->
  2567. }
  2568. </style>"
  2569. "The default style specification for exported HTML files.
  2570. Since there are different ways of setting style information, this variable
  2571. needs to contain the full HTML structure to provide a style, including the
  2572. surrounding HTML tags. The style specifications should include definitions
  2573. for new classes todo, done, title, and deadline. For example, legal values
  2574. would be:
  2575. <style type=\"text/css\">
  2576. p { font-weight: normal; color: gray; }
  2577. h1 { color: black; }
  2578. .title { text-align: center; }
  2579. .todo, .deadline { color: red; }
  2580. .done { color: green; }
  2581. </style>
  2582. or, if you want to keep the style in a file,
  2583. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2584. As the value of this option simply gets inserted into the HTML <head> header,
  2585. you can \"misuse\" it to add arbitrary text to the header."
  2586. :group 'org-export-html
  2587. :type 'string)
  2588. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2589. "Format for typesetting the document title in HTML export."
  2590. :group 'org-export-html
  2591. :type 'string)
  2592. (defcustom org-export-html-toplevel-hlevel 2
  2593. "The <H> level for level 1 headings in HTML export."
  2594. :group 'org-export-html
  2595. :type 'string)
  2596. (defcustom org-export-html-link-org-files-as-html t
  2597. "Non-nil means, make file links to `file.org' point to `file.html'.
  2598. When org-mode is exporting an org-mode file to HTML, links to
  2599. non-html files are directly put into a href tag in HTML.
  2600. However, links to other Org-mode files (recognized by the
  2601. extension `.org.) should become links to the corresponding html
  2602. file, assuming that the linked org-mode file will also be
  2603. converted to HTML.
  2604. When nil, the links still point to the plain `.org' file."
  2605. :group 'org-export-html
  2606. :type 'boolean)
  2607. (defcustom org-export-html-inline-images 'maybe
  2608. "Non-nil means, inline images into exported HTML pages.
  2609. This is done using an <img> tag. When nil, an anchor with href is used to
  2610. link to the image. If this option is `maybe', then images in links with
  2611. an empty description will be inlined, while images with a description will
  2612. be linked only."
  2613. :group 'org-export-html
  2614. :type '(choice (const :tag "Never" nil)
  2615. (const :tag "Always" t)
  2616. (const :tag "When there is no description" maybe)))
  2617. ;; FIXME: rename
  2618. (defcustom org-export-html-expand t
  2619. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2620. When nil, these tags will be exported as plain text and therefore
  2621. not be interpreted by a browser.
  2622. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2623. :group 'org-export-html
  2624. :type 'boolean)
  2625. (defcustom org-export-html-table-tag
  2626. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2627. "The HTML tag that is used to start a table.
  2628. This must be a <table> tag, but you may change the options like
  2629. borders and spacing."
  2630. :group 'org-export-html
  2631. :type 'string)
  2632. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2633. "The opening tag for table header fields.
  2634. This is customizable so that alignment options can be specified."
  2635. :group 'org-export-tables
  2636. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2637. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2638. "The opening tag for table data fields.
  2639. This is customizable so that alignment options can be specified."
  2640. :group 'org-export-tables
  2641. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2642. (defcustom org-export-html-with-timestamp nil
  2643. "If non-nil, write `org-export-html-html-helper-timestamp'
  2644. into the exported HTML text. Otherwise, the buffer will just be saved
  2645. to a file."
  2646. :group 'org-export-html
  2647. :type 'boolean)
  2648. (defcustom org-export-html-html-helper-timestamp
  2649. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2650. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2651. :group 'org-export-html
  2652. :type 'string)
  2653. (defgroup org-export-icalendar nil
  2654. "Options specific for iCalendar export of Org-mode files."
  2655. :tag "Org Export iCalendar"
  2656. :group 'org-export)
  2657. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2658. "The file name for the iCalendar file covering all agenda files.
  2659. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2660. The file name should be absolute."
  2661. :group 'org-export-icalendar
  2662. :type 'file)
  2663. (defcustom org-icalendar-include-todo nil
  2664. "Non-nil means, export to iCalendar files should also cover TODO items."
  2665. :group 'org-export-icalendar
  2666. :type '(choice
  2667. (const :tag "None" nil)
  2668. (const :tag "Unfinished" t)
  2669. (const :tag "All" all)))
  2670. (defcustom org-icalendar-include-sexps t
  2671. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2672. These are entries like in the diary, but directly in an Org-mode file."
  2673. :group 'org-export-icalendar
  2674. :type 'boolean)
  2675. (defcustom org-icalendar-combined-name "OrgMode"
  2676. "Calendar name for the combined iCalendar representing all agenda files."
  2677. :group 'org-export-icalendar
  2678. :type 'string)
  2679. (defgroup org-font-lock nil
  2680. "Font-lock settings for highlighting in Org-mode."
  2681. :tag "Org Font Lock"
  2682. :group 'org)
  2683. (defcustom org-level-color-stars-only nil
  2684. "Non-nil means fontify only the stars in each headline.
  2685. When nil, the entire headline is fontified.
  2686. Changing it requires restart of `font-lock-mode' to become effective
  2687. also in regions already fontified."
  2688. :group 'org-font-lock
  2689. :type 'boolean)
  2690. (defcustom org-hide-leading-stars nil
  2691. "Non-nil means, hide the first N-1 stars in a headline.
  2692. This works by using the face `org-hide' for these stars. This
  2693. face is white for a light background, and black for a dark
  2694. background. You may have to customize the face `org-hide' to
  2695. make this work.
  2696. Changing it requires restart of `font-lock-mode' to become effective
  2697. also in regions already fontified.
  2698. You may also set this on a per-file basis by adding one of the following
  2699. lines to the buffer:
  2700. #+STARTUP: hidestars
  2701. #+STARTUP: showstars"
  2702. :group 'org-font-lock
  2703. :type 'boolean)
  2704. (defcustom org-fontify-done-headline nil
  2705. "Non-nil means, change the face of a headline if it is marked DONE.
  2706. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2707. When this is non-nil, the headline after the keyword is set to the
  2708. `org-headline-done' as an additional indication."
  2709. :group 'org-font-lock
  2710. :type 'boolean)
  2711. (defcustom org-fontify-emphasized-text t
  2712. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2713. Changing this variable requires a restart of Emacs to take effect."
  2714. :group 'org-font-lock
  2715. :type 'boolean)
  2716. (defvar org-emph-re nil
  2717. "Regular expression for matching emphasis.")
  2718. (defvar org-emphasis-regexp-components) ; defined just below
  2719. (defvar org-emphasis-alist) ; defined just below
  2720. (defun org-set-emph-re (var val)
  2721. "Set variable and compute the emphasis regular expression."
  2722. (set var val)
  2723. (when (and (boundp 'org-emphasis-alist)
  2724. (boundp 'org-emphasis-regexp-components)
  2725. org-emphasis-alist org-emphasis-regexp-components)
  2726. (let* ((e org-emphasis-regexp-components)
  2727. (pre (car e))
  2728. (post (nth 1 e))
  2729. (border (nth 2 e))
  2730. (body (nth 3 e))
  2731. (nl (nth 4 e))
  2732. (stacked (nth 5 e))
  2733. (body1 (concat body "*?"))
  2734. (markers (mapconcat 'car org-emphasis-alist "")))
  2735. ;; make sure special characters appear at the right position in the class
  2736. (if (string-match "\\^" markers)
  2737. (setq markers (concat (replace-match "" t t markers) "^")))
  2738. (if (string-match "-" markers)
  2739. (setq markers (concat (replace-match "" t t markers) "-")))
  2740. (if (> nl 0)
  2741. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2742. (int-to-string nl) "\\}")))
  2743. ;; Make the regexp
  2744. (setq org-emph-re
  2745. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2746. "\\("
  2747. "\\([" markers "]\\)"
  2748. "\\("
  2749. "[^" border (if (and nil stacked) markers) "]"
  2750. body1
  2751. "[^" border (if (and nil stacked) markers) "]"
  2752. "\\)"
  2753. "\\3\\)"
  2754. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2755. (defcustom org-emphasis-regexp-components
  2756. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2757. "Components used to build the reqular expression for emphasis.
  2758. This is a list with 6 entries. Terminology: In an emphasis string
  2759. like \" *strong word* \", we call the initial space PREMATCH, the final
  2760. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2761. and \"trong wor\" is the body. The different components in this variable
  2762. specify what is allowed/forbidden in each part:
  2763. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2764. post Chars allowed as postmatch. End of line will be allowed too.
  2765. border The chars *forbidden* as border characters.
  2766. body-regexp A regexp like \".\" to match a body character. Don't use
  2767. non-shy groups here, and don't allow newline here.
  2768. newline The maximum number of newlines allowed in an emphasis exp.
  2769. stacked Non-nil means, allow stacked styles. This works only in HTML
  2770. export. When this is set, all marker characters (as given in
  2771. `org-emphasis-alist') will be allowed as pre/post, aiding
  2772. inside-out matching.
  2773. Use customize to modify this, or restart Emacs after changing it."
  2774. :group 'org-font-lock
  2775. :set 'org-set-emph-re
  2776. :type '(list
  2777. (sexp :tag "Allowed chars in pre ")
  2778. (sexp :tag "Allowed chars in post ")
  2779. (sexp :tag "Forbidden chars in border ")
  2780. (sexp :tag "Regexp for body ")
  2781. (integer :tag "number of newlines allowed")
  2782. (boolean :tag "Stacking allowed ")))
  2783. (defcustom org-emphasis-alist
  2784. '(("*" bold "<b>" "</b>")
  2785. ("/" italic "<i>" "</i>")
  2786. ("_" underline "<u>" "</u>")
  2787. ("=" org-code "<code>" "</code>")
  2788. ("+" (:strike-through t) "<del>" "</del>")
  2789. )
  2790. "Special syntax for emphasized text.
  2791. Text starting and ending with a special character will be emphasized, for
  2792. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2793. characters, the face to be used by font-lock for highlighting in Org-mode
  2794. Emacs buffers, and the HTML tags to be used for this.
  2795. Use customize to modify this, or restart Emacs after changing it."
  2796. :group 'org-font-lock
  2797. :set 'org-set-emph-re
  2798. :type '(repeat
  2799. (list
  2800. (string :tag "Marker character")
  2801. (choice
  2802. (face :tag "Font-lock-face")
  2803. (plist :tag "Face property list"))
  2804. (string :tag "HTML start tag")
  2805. (string :tag "HTML end tag"))))
  2806. ;;; The faces
  2807. (defgroup org-faces nil
  2808. "Faces in Org-mode."
  2809. :tag "Org Faces"
  2810. :group 'org-font-lock)
  2811. ;; FIXME: convert that into a macro? Not critical, because this
  2812. ;; is only executed a few times at load time.
  2813. (defun org-compatible-face (specs)
  2814. "Make a compatible face specification.
  2815. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2816. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2817. to the top of the list. The `min-colors' attribute will be removed from
  2818. any other entries, and any resulting duplicates will be removed entirely."
  2819. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2820. (let (r e a)
  2821. (while (setq e (pop specs))
  2822. (cond
  2823. ((memq (car e) '(t default)) (push e r))
  2824. ((setq a (member '(min-colors 8) (car e)))
  2825. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2826. (cdr e)))))
  2827. ((setq a (assq 'min-colors (car e)))
  2828. (setq e (cons (delq a (car e)) (cdr e)))
  2829. (or (assoc (car e) r) (push e r)))
  2830. (t (or (assoc (car e) r) (push e r)))))
  2831. (nreverse r))
  2832. specs))
  2833. (defface org-hide
  2834. '((((background light)) (:foreground "white"))
  2835. (((background dark)) (:foreground "black")))
  2836. "Face used to hide leading stars in headlines.
  2837. The forground color of this face should be equal to the background
  2838. color of the frame."
  2839. :group 'org-faces)
  2840. (defface org-level-1 ;; font-lock-function-name-face
  2841. (org-compatible-face
  2842. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2843. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2844. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2845. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2846. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2847. (t (:bold t))))
  2848. "Face used for level 1 headlines."
  2849. :group 'org-faces)
  2850. (defface org-level-2 ;; font-lock-variable-name-face
  2851. (org-compatible-face
  2852. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2853. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2854. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2855. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2856. (t (:bold t))))
  2857. "Face used for level 2 headlines."
  2858. :group 'org-faces)
  2859. (defface org-level-3 ;; font-lock-keyword-face
  2860. (org-compatible-face
  2861. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2862. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2863. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2864. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2865. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2866. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2867. (t (:bold t))))
  2868. "Face used for level 3 headlines."
  2869. :group 'org-faces)
  2870. (defface org-level-4 ;; font-lock-comment-face
  2871. (org-compatible-face
  2872. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2873. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2874. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2875. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2876. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2877. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2878. (t (:bold t))))
  2879. "Face used for level 4 headlines."
  2880. :group 'org-faces)
  2881. (defface org-level-5 ;; font-lock-type-face
  2882. (org-compatible-face
  2883. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2884. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2885. (((class color) (min-colors 8)) (:foreground "green"))))
  2886. "Face used for level 5 headlines."
  2887. :group 'org-faces)
  2888. (defface org-level-6 ;; font-lock-constant-face
  2889. (org-compatible-face
  2890. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2891. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2892. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2893. "Face used for level 6 headlines."
  2894. :group 'org-faces)
  2895. (defface org-level-7 ;; font-lock-builtin-face
  2896. (org-compatible-face
  2897. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2898. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2899. (((class color) (min-colors 8)) (:foreground "blue"))))
  2900. "Face used for level 7 headlines."
  2901. :group 'org-faces)
  2902. (defface org-level-8 ;; font-lock-string-face
  2903. (org-compatible-face
  2904. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2905. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2906. (((class color) (min-colors 8)) (:foreground "green"))))
  2907. "Face used for level 8 headlines."
  2908. :group 'org-faces)
  2909. (defface org-special-keyword ;; font-lock-string-face
  2910. (org-compatible-face
  2911. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2912. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2913. (t (:italic t))))
  2914. "Face used for special keywords."
  2915. :group 'org-faces)
  2916. (defface org-drawer ;; font-lock-function-name-face
  2917. (org-compatible-face
  2918. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2919. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2920. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2921. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2922. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2923. (t (:bold t))))
  2924. "Face used for drawers."
  2925. :group 'org-faces)
  2926. (defface org-property-value nil
  2927. "Face used for the value of a property."
  2928. :group 'org-faces)
  2929. (defface org-column
  2930. (org-compatible-face
  2931. '((((class color) (min-colors 16) (background light))
  2932. (:background "grey90"))
  2933. (((class color) (min-colors 16) (background dark))
  2934. (:background "grey30"))
  2935. (((class color) (min-colors 8))
  2936. (:background "cyan" :foreground "black"))
  2937. (t (:inverse-video t))))
  2938. "Face for column display of entry properties."
  2939. :group 'org-faces)
  2940. (when (fboundp 'set-face-attribute)
  2941. ;; Make sure that a fixed-width face is used when we have a column table.
  2942. (set-face-attribute 'org-column nil
  2943. :height (face-attribute 'default :height)
  2944. :family (face-attribute 'default :family)))
  2945. (defface org-warning ;; font-lock-warning-face
  2946. (org-compatible-face
  2947. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2948. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2949. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2950. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2951. (t (:bold t))))
  2952. "Face for deadlines and TODO keywords."
  2953. :group 'org-faces)
  2954. (defface org-archived ; similar to shadow
  2955. (org-compatible-face
  2956. '((((class color grayscale) (min-colors 88) (background light))
  2957. (:foreground "grey50"))
  2958. (((class color grayscale) (min-colors 88) (background dark))
  2959. (:foreground "grey70"))
  2960. (((class color) (min-colors 8) (background light))
  2961. (:foreground "green"))
  2962. (((class color) (min-colors 8) (background dark))
  2963. (:foreground "yellow"))))
  2964. "Face for headline with the ARCHIVE tag."
  2965. :group 'org-faces)
  2966. (defface org-link
  2967. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2968. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2969. (t (:underline t)))
  2970. "Face for links."
  2971. :group 'org-faces)
  2972. (defface org-target
  2973. '((((class color) (background light)) (:underline t))
  2974. (((class color) (background dark)) (:underline t))
  2975. (t (:underline t)))
  2976. "Face for links."
  2977. :group 'org-faces)
  2978. (defface org-date
  2979. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2980. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2981. (t (:underline t)))
  2982. "Face for links."
  2983. :group 'org-faces)
  2984. (defface org-sexp-date
  2985. '((((class color) (background light)) (:foreground "Purple"))
  2986. (((class color) (background dark)) (:foreground "Cyan"))
  2987. (t (:underline t)))
  2988. "Face for links."
  2989. :group 'org-faces)
  2990. (defface org-tag
  2991. '((t (:bold t)))
  2992. "Face for tags."
  2993. :group 'org-faces)
  2994. (defface org-todo ;; font-lock-warning-face
  2995. (org-compatible-face
  2996. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2997. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2998. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2999. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3000. (t (:inverse-video t :bold t))))
  3001. "Face for TODO keywords."
  3002. :group 'org-faces)
  3003. (defface org-done ;; font-lock-type-face
  3004. (org-compatible-face
  3005. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3006. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3007. (((class color) (min-colors 8)) (:foreground "green"))
  3008. (t (:bold t))))
  3009. "Face used for todo keywords that indicate DONE items."
  3010. :group 'org-faces)
  3011. (defface org-headline-done ;; font-lock-string-face
  3012. (org-compatible-face
  3013. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3014. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3015. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3016. "Face used to indicate that a headline is DONE.
  3017. This face is only used if `org-fontify-done-headline' is set. If applies
  3018. to the part of the headline after the DONE keyword."
  3019. :group 'org-faces)
  3020. (defcustom org-todo-keyword-faces nil
  3021. "Faces for specific TODO keywords.
  3022. This is a list of cons cells, with TODO keywords in the car
  3023. and faces in the cdr. The face can be a symbol, or a property
  3024. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3025. :group 'org-faces
  3026. :group 'org-todo
  3027. :type '(repeat
  3028. (cons
  3029. (string :tag "keyword")
  3030. (sexp :tag "face"))))
  3031. (defface org-table ;; font-lock-function-name-face
  3032. (org-compatible-face
  3033. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3034. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3035. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3036. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3037. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3038. (((class color) (min-colors 8) (background dark)))))
  3039. "Face used for tables."
  3040. :group 'org-faces)
  3041. (defface org-formula
  3042. (org-compatible-face
  3043. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3044. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3045. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3046. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3047. (t (:bold t :italic t))))
  3048. "Face for formulas."
  3049. :group 'org-faces)
  3050. (defface org-code
  3051. (org-compatible-face
  3052. '((((class color grayscale) (min-colors 88) (background light))
  3053. (:foreground "grey50"))
  3054. (((class color grayscale) (min-colors 88) (background dark))
  3055. (:foreground "grey70"))
  3056. (((class color) (min-colors 8) (background light))
  3057. (:foreground "green"))
  3058. (((class color) (min-colors 8) (background dark))
  3059. (:foreground "yellow"))))
  3060. "Face for fixed-with text like code snippets."
  3061. :group 'org-faces
  3062. :version "22.1")
  3063. (defface org-agenda-structure ;; font-lock-function-name-face
  3064. (org-compatible-face
  3065. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3066. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3067. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3068. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3069. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3070. (t (:bold t))))
  3071. "Face used in agenda for captions and dates."
  3072. :group 'org-faces)
  3073. (defface org-scheduled-today
  3074. (org-compatible-face
  3075. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3076. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3077. (((class color) (min-colors 8)) (:foreground "green"))
  3078. (t (:bold t :italic t))))
  3079. "Face for items scheduled for a certain day."
  3080. :group 'org-faces)
  3081. (defface org-scheduled-previously
  3082. (org-compatible-face
  3083. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3084. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3085. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3086. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3087. (t (:bold t))))
  3088. "Face for items scheduled previously, and not yet done."
  3089. :group 'org-faces)
  3090. (defface org-upcoming-deadline
  3091. (org-compatible-face
  3092. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3093. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3094. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3095. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3096. (t (:bold t))))
  3097. "Face for items scheduled previously, and not yet done."
  3098. :group 'org-faces)
  3099. (defcustom org-agenda-deadline-faces
  3100. '((1.0 . org-warning)
  3101. (0.5 . org-upcoming-deadline)
  3102. (0.0 . default))
  3103. "Faces for showing deadlines in the agenda.
  3104. This is a list of cons cells. The cdr of each cess is a face to be used,
  3105. and it can also just be a like like '(:foreground \"yellow\").
  3106. Each car is a fraction of the head-warning time that must have passed for
  3107. this the face in the cdr to be used for display. The numbers must be
  3108. given in descending order. The head-warning time is normally taken
  3109. from `org-deadline-warning-days', but can also be specified in the deadline
  3110. timestamp itself, like this:
  3111. DEADLINE: <2007-08-13 Mon -8d>
  3112. You may use d for days, w for weeks, m for months and y for years. Months
  3113. and years will only be treated in an approximate fashion (30.4 days for a
  3114. month and 365.24 days for a year)."
  3115. :group 'org-faces
  3116. :group 'org-agenda-daily/weekly
  3117. :type '(repeat
  3118. (cons
  3119. (number :tag "Fraction of head-warning time passed")
  3120. (sexp :tag "Face"))))
  3121. (defface org-time-grid ;; font-lock-variable-name-face
  3122. (org-compatible-face
  3123. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3124. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3125. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3126. "Face used for time grids."
  3127. :group 'org-faces)
  3128. (defconst org-level-faces
  3129. '(org-level-1 org-level-2 org-level-3 org-level-4
  3130. org-level-5 org-level-6 org-level-7 org-level-8
  3131. ))
  3132. (defcustom org-n-level-faces (length org-level-faces)
  3133. "The number different faces to be used for headlines.
  3134. Org-mode defines 8 different headline faces, so this can be at most 8.
  3135. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3136. :type 'number
  3137. :group 'org-faces)
  3138. ;;; Variables for pre-computed regular expressions, all buffer local
  3139. (defvar org-drawer-regexp nil
  3140. "Matches first line of a hidden block.")
  3141. (make-variable-buffer-local 'org-drawer-regexp)
  3142. (defvar org-todo-regexp nil
  3143. "Matches any of the TODO state keywords.")
  3144. (make-variable-buffer-local 'org-todo-regexp)
  3145. (defvar org-not-done-regexp nil
  3146. "Matches any of the TODO state keywords except the last one.")
  3147. (make-variable-buffer-local 'org-not-done-regexp)
  3148. (defvar org-todo-line-regexp nil
  3149. "Matches a headline and puts TODO state into group 2 if present.")
  3150. (make-variable-buffer-local 'org-todo-line-regexp)
  3151. (defvar org-todo-line-tags-regexp nil
  3152. "Matches a headline and puts TODO state into group 2 if present.
  3153. Also put tags into group 4 if tags are present.")
  3154. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3155. (defvar org-nl-done-regexp nil
  3156. "Matches newline followed by a headline with the DONE keyword.")
  3157. (make-variable-buffer-local 'org-nl-done-regexp)
  3158. (defvar org-looking-at-done-regexp nil
  3159. "Matches the DONE keyword a point.")
  3160. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3161. (defvar org-ds-keyword-length 12
  3162. "Maximum length of the Deadline and SCHEDULED keywords.")
  3163. (make-variable-buffer-local 'org-ds-keyword-length)
  3164. (defvar org-deadline-regexp nil
  3165. "Matches the DEADLINE keyword.")
  3166. (make-variable-buffer-local 'org-deadline-regexp)
  3167. (defvar org-deadline-time-regexp nil
  3168. "Matches the DEADLINE keyword together with a time stamp.")
  3169. (make-variable-buffer-local 'org-deadline-time-regexp)
  3170. (defvar org-deadline-line-regexp nil
  3171. "Matches the DEADLINE keyword and the rest of the line.")
  3172. (make-variable-buffer-local 'org-deadline-line-regexp)
  3173. (defvar org-scheduled-regexp nil
  3174. "Matches the SCHEDULED keyword.")
  3175. (make-variable-buffer-local 'org-scheduled-regexp)
  3176. (defvar org-scheduled-time-regexp nil
  3177. "Matches the SCHEDULED keyword together with a time stamp.")
  3178. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3179. (defvar org-closed-time-regexp nil
  3180. "Matches the CLOSED keyword together with a time stamp.")
  3181. (make-variable-buffer-local 'org-closed-time-regexp)
  3182. (defvar org-keyword-time-regexp nil
  3183. "Matches any of the 4 keywords, together with the time stamp.")
  3184. (make-variable-buffer-local 'org-keyword-time-regexp)
  3185. (defvar org-keyword-time-not-clock-regexp nil
  3186. "Matches any of the 3 keywords, together with the time stamp.")
  3187. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3188. (defvar org-maybe-keyword-time-regexp nil
  3189. "Matches a timestamp, possibly preceeded by a keyword.")
  3190. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3191. (defvar org-planning-or-clock-line-re nil
  3192. "Matches a line with planning or clock info.")
  3193. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3194. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3195. rear-nonsticky t mouse-map t fontified t)
  3196. "Properties to remove when a string without properties is wanted.")
  3197. (defsubst org-match-string-no-properties (num &optional string)
  3198. (if (featurep 'xemacs)
  3199. (let ((s (match-string num string)))
  3200. (remove-text-properties 0 (length s) org-rm-props s)
  3201. s)
  3202. (match-string-no-properties num string)))
  3203. (defsubst org-no-properties (s)
  3204. (if (fboundp 'set-text-properties)
  3205. (set-text-properties 0 (length s) nil s)
  3206. (remove-text-properties 0 (length s) org-rm-props s))
  3207. s)
  3208. (defsubst org-get-alist-option (option key)
  3209. (cond ((eq key t) t)
  3210. ((eq option t) t)
  3211. ((assoc key option) (cdr (assoc key option)))
  3212. (t (cdr (assq 'default option)))))
  3213. (defsubst org-inhibit-invisibility ()
  3214. "Modified `buffer-invisibility-spec' for Emacs 21.
  3215. Some ops with invisible text do not work correctly on Emacs 21. For these
  3216. we turn off invisibility temporarily. Use this in a `let' form."
  3217. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3218. (defsubst org-set-local (var value)
  3219. "Make VAR local in current buffer and set it to VALUE."
  3220. (set (make-variable-buffer-local var) value))
  3221. (defsubst org-mode-p ()
  3222. "Check if the current buffer is in Org-mode."
  3223. (eq major-mode 'org-mode))
  3224. (defsubst org-last (list)
  3225. "Return the last element of LIST."
  3226. (car (last list)))
  3227. (defun org-let (list &rest body)
  3228. (eval (cons 'let (cons list body))))
  3229. (put 'org-let 'lisp-indent-function 1)
  3230. (defun org-let2 (list1 list2 &rest body)
  3231. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3232. (put 'org-let2 'lisp-indent-function 2)
  3233. (defconst org-startup-options
  3234. '(("fold" org-startup-folded t)
  3235. ("overview" org-startup-folded t)
  3236. ("nofold" org-startup-folded nil)
  3237. ("showall" org-startup-folded nil)
  3238. ("content" org-startup-folded content)
  3239. ("hidestars" org-hide-leading-stars t)
  3240. ("showstars" org-hide-leading-stars nil)
  3241. ("odd" org-odd-levels-only t)
  3242. ("oddeven" org-odd-levels-only nil)
  3243. ("align" org-startup-align-all-tables t)
  3244. ("noalign" org-startup-align-all-tables nil)
  3245. ("customtime" org-display-custom-times t)
  3246. ("logging" org-log-done t)
  3247. ("logdone" org-log-done t)
  3248. ("nologging" org-log-done nil)
  3249. ("lognotedone" org-log-done done push)
  3250. ("lognotestate" org-log-done state push)
  3251. ("lognoteclock-out" org-log-done clock-out push)
  3252. ("logrepeat" org-log-repeat t)
  3253. ("nologrepeat" org-log-repeat nil)
  3254. ("constcgs" constants-unit-system cgs)
  3255. ("constSI" constants-unit-system SI))
  3256. "Variable associated with STARTUP options for org-mode.
  3257. Each element is a list of three items: The startup options as written
  3258. in the #+STARTUP line, the corresponding variable, and the value to
  3259. set this variable to if the option is found. An optional forth element PUSH
  3260. means to push this value onto the list in the variable.")
  3261. (defun org-set-regexps-and-options ()
  3262. "Precompute regular expressions for current buffer."
  3263. (when (org-mode-p)
  3264. (org-set-local 'org-todo-kwd-alist nil)
  3265. (org-set-local 'org-todo-keywords-1 nil)
  3266. (org-set-local 'org-todo-tag-alist nil)
  3267. (org-set-local 'org-done-keywords nil)
  3268. (org-set-local 'org-todo-heads nil)
  3269. (org-set-local 'org-todo-sets nil)
  3270. (let ((re (org-make-options-regexp
  3271. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO" "COLUMNS"
  3272. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3273. "CONSTANTS" "PROPERTY")))
  3274. (splitre "[ \t]+")
  3275. kwds kws0 kwsa key value cat arch tags const links hw dws
  3276. tail sep kws1 prio props)
  3277. (save-excursion
  3278. (save-restriction
  3279. (widen)
  3280. (goto-char (point-min))
  3281. (while (re-search-forward re nil t)
  3282. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3283. (cond
  3284. ((equal key "CATEGORY")
  3285. (if (string-match "[ \t]+$" value)
  3286. (setq value (replace-match "" t t value)))
  3287. (setq cat (intern value)))
  3288. ((equal key "SEQ_TODO")
  3289. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3290. ((equal key "TYP_TODO")
  3291. (push (cons 'type (org-split-string value splitre)) kwds))
  3292. ((equal key "TAGS")
  3293. (setq tags (append tags (org-split-string value splitre))))
  3294. ((equal key "COLUMNS")
  3295. (org-set-local 'org-columns-default-format value))
  3296. ((equal key "LINK")
  3297. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3298. (push (cons (match-string 1 value)
  3299. (org-trim (match-string 2 value)))
  3300. links)))
  3301. ((equal key "PRIORITIES")
  3302. (setq prio (org-split-string value " +")))
  3303. ((equal key "PROPERTY")
  3304. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3305. (push (cons (match-string 1 value) (match-string 2 value))
  3306. props)))
  3307. ((equal key "CONSTANTS")
  3308. (setq const (append const (org-split-string value splitre))))
  3309. ((equal key "STARTUP")
  3310. (let ((opts (org-split-string value splitre))
  3311. l var val)
  3312. (while (setq l (pop opts))
  3313. (when (setq l (assoc l org-startup-options))
  3314. (setq var (nth 1 l) val (nth 2 l))
  3315. (if (not (nth 3 l))
  3316. (set (make-local-variable var) val)
  3317. (if (not (listp (symbol-value var)))
  3318. (set (make-local-variable var) nil))
  3319. (set (make-local-variable var) (symbol-value var))
  3320. (add-to-list var val))))))
  3321. ((equal key "ARCHIVE")
  3322. (string-match " *$" value)
  3323. (setq arch (replace-match "" t t value))
  3324. (remove-text-properties 0 (length arch)
  3325. '(face t fontified t) arch)))
  3326. )))
  3327. (and cat (org-set-local 'org-category cat))
  3328. (when prio
  3329. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3330. (setq prio (mapcar 'string-to-char prio))
  3331. (org-set-local 'org-highest-priority (nth 0 prio))
  3332. (org-set-local 'org-lowest-priority (nth 1 prio))
  3333. (org-set-local 'org-default-priority (nth 2 prio)))
  3334. (and props (org-set-local 'org-local-properties (nreverse props)))
  3335. (and arch (org-set-local 'org-archive-location arch))
  3336. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3337. ;; Process the TODO keywords
  3338. (unless kwds
  3339. ;; Use the global values as if they had been given locally.
  3340. (setq kwds (default-value 'org-todo-keywords))
  3341. (if (stringp (car kwds))
  3342. (setq kwds (list (cons org-todo-interpretation
  3343. (default-value 'org-todo-keywords)))))
  3344. (setq kwds (reverse kwds)))
  3345. (setq kwds (nreverse kwds))
  3346. (let (inter kws)
  3347. (while (setq kws (pop kwds))
  3348. (setq inter (pop kws) sep (member "|" kws)
  3349. kws0 (delete "|" (copy-sequence kws))
  3350. kwsa nil
  3351. kws1 (mapcar (lambda (x)
  3352. (if (string-match "\\(.*\\)(\\(.\\))" x)
  3353. (progn
  3354. (push (cons (match-string 1 x)
  3355. (string-to-char
  3356. (match-string 2 x))) kwsa)
  3357. (match-string 1 x))
  3358. x))
  3359. kws0)
  3360. kwsa (if kwsa (append '((:startgroup)) kwsa '((:endgroup))))
  3361. hw (car kws1)
  3362. dws (if sep (cdr sep) (last kws1))
  3363. tail (list inter hw (car dws) (org-last dws)))
  3364. (add-to-list 'org-todo-heads hw 'append)
  3365. (push kws1 org-todo-sets)
  3366. (setq org-done-keywords (append org-done-keywords dws nil))
  3367. (setq org-todo-tag-alist (append org-todo-tag-alist kwsa))
  3368. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3369. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3370. (setq org-todo-sets (nreverse org-todo-sets)
  3371. org-todo-kwd-alist (nreverse org-todo-kwd-alist)))
  3372. ;; Process the constants
  3373. (when const
  3374. (let (e cst)
  3375. (while (setq e (pop const))
  3376. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3377. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3378. (setq org-table-formula-constants-local cst)))
  3379. ;; Process the tags.
  3380. (when tags
  3381. (let (e tgs)
  3382. (while (setq e (pop tags))
  3383. (cond
  3384. ((equal e "{") (push '(:startgroup) tgs))
  3385. ((equal e "}") (push '(:endgroup) tgs))
  3386. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3387. (push (cons (match-string 1 e)
  3388. (string-to-char (match-string 2 e)))
  3389. tgs))
  3390. (t (push (list e) tgs))))
  3391. (org-set-local 'org-tag-alist nil)
  3392. (while (setq e (pop tgs))
  3393. (or (and (stringp (car e))
  3394. (assoc (car e) org-tag-alist))
  3395. (push e org-tag-alist))))))
  3396. ;; Compute the regular expressions and other local variables
  3397. (if (not org-done-keywords)
  3398. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3399. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3400. (length org-scheduled-string)))
  3401. org-drawer-regexp
  3402. (concat "^[ \t]*:\\("
  3403. (mapconcat 'regexp-quote org-drawers "\\|")
  3404. "\\):[ \t]*$")
  3405. org-not-done-keywords
  3406. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3407. org-todo-regexp
  3408. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3409. "\\|") "\\)\\>")
  3410. org-not-done-regexp
  3411. (concat "\\<\\("
  3412. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3413. "\\)\\>")
  3414. org-todo-line-regexp
  3415. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3416. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3417. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3418. org-nl-done-regexp
  3419. (concat "\n\\*+[ \t]+"
  3420. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3421. "\\)" "\\>")
  3422. org-todo-line-tags-regexp
  3423. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3424. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3425. (org-re
  3426. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3427. org-looking-at-done-regexp
  3428. (concat "^" "\\(?:"
  3429. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3430. "\\>")
  3431. org-deadline-regexp (concat "\\<" org-deadline-string)
  3432. org-deadline-time-regexp
  3433. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3434. org-deadline-line-regexp
  3435. (concat "\\<\\(" org-deadline-string "\\).*")
  3436. org-scheduled-regexp
  3437. (concat "\\<" org-scheduled-string)
  3438. org-scheduled-time-regexp
  3439. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3440. org-closed-time-regexp
  3441. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3442. org-keyword-time-regexp
  3443. (concat "\\<\\(" org-scheduled-string
  3444. "\\|" org-deadline-string
  3445. "\\|" org-closed-string
  3446. "\\|" org-clock-string "\\)"
  3447. " *[[<]\\([^]>]+\\)[]>]")
  3448. org-keyword-time-not-clock-regexp
  3449. (concat "\\<\\(" org-scheduled-string
  3450. "\\|" org-deadline-string
  3451. "\\|" org-closed-string
  3452. "\\)"
  3453. " *[[<]\\([^]>]+\\)[]>]")
  3454. org-maybe-keyword-time-regexp
  3455. (concat "\\(\\<\\(" org-scheduled-string
  3456. "\\|" org-deadline-string
  3457. "\\|" org-closed-string
  3458. "\\|" org-clock-string "\\)\\)?"
  3459. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3460. org-planning-or-clock-line-re
  3461. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3462. "\\|" org-deadline-string
  3463. "\\|" org-closed-string "\\|" org-clock-string
  3464. "\\)\\>\\)")
  3465. )
  3466. (org-set-font-lock-defaults)))
  3467. ;;; Some variables ujsed in various places
  3468. (defvar org-window-configuration nil
  3469. "Used in various places to store a window configuration.")
  3470. (defvar org-finish-function nil
  3471. "Function to be called when `C-c C-c' is used.
  3472. This is for getting out of special buffers like remember.")
  3473. ;;; Foreign variables, to inform the compiler
  3474. ;; XEmacs only
  3475. (defvar outline-mode-menu-heading)
  3476. (defvar outline-mode-menu-show)
  3477. (defvar outline-mode-menu-hide)
  3478. (defvar zmacs-regions) ; XEmacs regions
  3479. ;; Emacs only
  3480. (defvar mark-active)
  3481. ;; Packages that org-mode interacts with
  3482. (defvar calc-embedded-close-formula)
  3483. (defvar calc-embedded-open-formula)
  3484. (defvar font-lock-unfontify-region-function)
  3485. (defvar org-goto-start-pos)
  3486. (defvar vm-message-pointer)
  3487. (defvar vm-folder-directory)
  3488. (defvar wl-summary-buffer-elmo-folder)
  3489. (defvar wl-summary-buffer-folder-name)
  3490. (defvar gnus-other-frame-object)
  3491. (defvar gnus-group-name)
  3492. (defvar gnus-article-current)
  3493. (defvar w3m-current-url)
  3494. (defvar w3m-current-title)
  3495. (defvar mh-progs)
  3496. (defvar mh-current-folder)
  3497. (defvar mh-show-folder-buffer)
  3498. (defvar mh-index-folder)
  3499. (defvar mh-searcher)
  3500. (defvar calendar-mode-map)
  3501. (defvar Info-current-file)
  3502. (defvar Info-current-node)
  3503. (defvar texmathp-why)
  3504. (defvar remember-save-after-remembering)
  3505. (defvar remember-data-file)
  3506. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3507. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3508. (defvar org-latex-regexps)
  3509. (defvar constants-unit-system)
  3510. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3511. ;; FIXME: Occasionally check by commenting these, to make sure
  3512. ;; no other functions uses these, forgetting to let-bind them.
  3513. (defvar entry)
  3514. (defvar state)
  3515. (defvar last-state)
  3516. (defvar date)
  3517. (defvar description)
  3518. ;; Defined somewhere in this file, but used before definition.
  3519. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3520. (defvar org-agenda-buffer-name)
  3521. (defvar org-agenda-undo-list)
  3522. (defvar org-agenda-pending-undo-list)
  3523. (defvar org-agenda-overriding-header)
  3524. (defvar orgtbl-mode)
  3525. (defvar org-html-entities)
  3526. (defvar org-struct-menu)
  3527. (defvar org-org-menu)
  3528. (defvar org-tbl-menu)
  3529. (defvar org-agenda-keymap)
  3530. (defvar org-category-table)
  3531. ;;;; Emacs/XEmacs compatibility
  3532. ;; Overlay compatibility functions
  3533. (defun org-make-overlay (beg end &optional buffer)
  3534. (if (featurep 'xemacs)
  3535. (make-extent beg end buffer)
  3536. (make-overlay beg end buffer)))
  3537. (defun org-delete-overlay (ovl)
  3538. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3539. (defun org-detach-overlay (ovl)
  3540. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3541. (defun org-move-overlay (ovl beg end &optional buffer)
  3542. (if (featurep 'xemacs)
  3543. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3544. (move-overlay ovl beg end buffer)))
  3545. (defun org-overlay-put (ovl prop value)
  3546. (if (featurep 'xemacs)
  3547. (set-extent-property ovl prop value)
  3548. (overlay-put ovl prop value)))
  3549. (defun org-overlay-display (ovl text &optional face evap)
  3550. "Make overlay OVL display TEXT with face FACE."
  3551. (if (featurep 'xemacs)
  3552. (let ((gl (make-glyph text)))
  3553. (and face (set-glyph-face gl face))
  3554. (set-extent-property ovl 'invisible t)
  3555. (set-extent-property ovl 'end-glyph gl))
  3556. (overlay-put ovl 'display text)
  3557. (if face (overlay-put ovl 'face face))
  3558. (if evap (overlay-put ovl 'evaporate t))))
  3559. (defun org-overlay-before-string (ovl text &optional face evap)
  3560. "Make overlay OVL display TEXT with face FACE."
  3561. (if (featurep 'xemacs)
  3562. (let ((gl (make-glyph text)))
  3563. (and face (set-glyph-face gl face))
  3564. (set-extent-property ovl 'begin-glyph gl))
  3565. (if face (org-add-props text nil 'face face))
  3566. (overlay-put ovl 'before-string text)
  3567. (if evap (overlay-put ovl 'evaporate t))))
  3568. (defun org-overlay-get (ovl prop)
  3569. (if (featurep 'xemacs)
  3570. (extent-property ovl prop)
  3571. (overlay-get ovl prop)))
  3572. (defun org-overlays-at (pos)
  3573. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3574. ;; FIXME: this is currently not used
  3575. (defun org-overlays-in (&optional start end)
  3576. (if (featurep 'xemacs)
  3577. (extent-list nil start end)
  3578. (overlays-in start end)))
  3579. (defun org-overlay-start (o)
  3580. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3581. (defun org-overlay-end (o)
  3582. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3583. ;; FIXME: this is currently not used
  3584. (defun org-find-overlays (prop &optional pos delete)
  3585. "Find all overlays specifying PROP at POS or point.
  3586. If DELETE is non-nil, delete all those overlays."
  3587. (let ((overlays (org-overlays-at (or pos (point))))
  3588. ov found)
  3589. (while (setq ov (pop overlays))
  3590. (if (org-overlay-get ov prop)
  3591. (if delete (org-delete-overlay ov) (push ov found))))
  3592. found))
  3593. ;; Region compatibility
  3594. (defun org-add-hook (hook function &optional append local)
  3595. "Add-hook, compatible with both Emacsen."
  3596. (if (and local (featurep 'xemacs))
  3597. (add-local-hook hook function append)
  3598. (add-hook hook function append local)))
  3599. (defvar org-ignore-region nil
  3600. "To temporarily disable the active region.")
  3601. (defun org-region-active-p ()
  3602. "Is `transient-mark-mode' on and the region active?
  3603. Works on both Emacs and XEmacs."
  3604. (if org-ignore-region
  3605. nil
  3606. (if (featurep 'xemacs)
  3607. (and zmacs-regions (region-active-p))
  3608. (and transient-mark-mode mark-active))))
  3609. ;; Invisibility compatibility
  3610. (defun org-add-to-invisibility-spec (arg)
  3611. "Add elements to `buffer-invisibility-spec'.
  3612. See documentation for `buffer-invisibility-spec' for the kind of elements
  3613. that can be added."
  3614. (cond
  3615. ((fboundp 'add-to-invisibility-spec)
  3616. (add-to-invisibility-spec arg))
  3617. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3618. (setq buffer-invisibility-spec (list arg)))
  3619. (t
  3620. (setq buffer-invisibility-spec
  3621. (cons arg buffer-invisibility-spec)))))
  3622. (defun org-remove-from-invisibility-spec (arg)
  3623. "Remove elements from `buffer-invisibility-spec'."
  3624. (if (fboundp 'remove-from-invisibility-spec)
  3625. (remove-from-invisibility-spec arg)
  3626. (if (consp buffer-invisibility-spec)
  3627. (setq buffer-invisibility-spec
  3628. (delete arg buffer-invisibility-spec)))))
  3629. ;; FIXME: this is currently not used
  3630. (defun org-in-invisibility-spec-p (arg)
  3631. "Is ARG a member of `buffer-invisibility-spec'?"
  3632. (if (consp buffer-invisibility-spec)
  3633. (member arg buffer-invisibility-spec)
  3634. nil))
  3635. ;;;; Define the Org-mode
  3636. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3637. (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."))
  3638. ;; We use a before-change function to check if a table might need
  3639. ;; an update.
  3640. (defvar org-table-may-need-update t
  3641. "Indicates that a table might need an update.
  3642. This variable is set by `org-before-change-function'.
  3643. `org-table-align' sets it back to nil.")
  3644. (defvar org-mode-map)
  3645. (defvar org-mode-hook nil)
  3646. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3647. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3648. (defvar org-table-buffer-is-an nil)
  3649. ;;;###autoload
  3650. (define-derived-mode org-mode outline-mode "Org"
  3651. "Outline-based notes management and organizer, alias
  3652. \"Carsten's outline-mode for keeping track of everything.\"
  3653. Org-mode develops organizational tasks around a NOTES file which
  3654. contains information about projects as plain text. Org-mode is
  3655. implemented on top of outline-mode, which is ideal to keep the content
  3656. of large files well structured. It supports ToDo items, deadlines and
  3657. time stamps, which magically appear in the diary listing of the Emacs
  3658. calendar. Tables are easily created with a built-in table editor.
  3659. Plain text URL-like links connect to websites, emails (VM), Usenet
  3660. messages (Gnus), BBDB entries, and any files related to the project.
  3661. For printing and sharing of notes, an Org-mode file (or a part of it)
  3662. can be exported as a structured ASCII or HTML file.
  3663. The following commands are available:
  3664. \\{org-mode-map}"
  3665. ;; Get rid of Outline menus, they are not needed
  3666. ;; Need to do this here because define-derived-mode sets up
  3667. ;; the keymap so late. Still, it is a waste to call this each time
  3668. ;; we switch another buffer into org-mode.
  3669. (if (featurep 'xemacs)
  3670. (when (boundp 'outline-mode-menu-heading)
  3671. ;; Assume this is Greg's port, it used easymenu
  3672. (easy-menu-remove outline-mode-menu-heading)
  3673. (easy-menu-remove outline-mode-menu-show)
  3674. (easy-menu-remove outline-mode-menu-hide))
  3675. (define-key org-mode-map [menu-bar headings] 'undefined)
  3676. (define-key org-mode-map [menu-bar hide] 'undefined)
  3677. (define-key org-mode-map [menu-bar show] 'undefined))
  3678. (easy-menu-add org-org-menu)
  3679. (easy-menu-add org-tbl-menu)
  3680. (org-install-agenda-files-menu)
  3681. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3682. (org-add-to-invisibility-spec '(org-cwidth))
  3683. (when (featurep 'xemacs)
  3684. (org-set-local 'line-move-ignore-invisible t))
  3685. (org-set-local 'outline-regexp "\\*+ ")
  3686. (setq outline-level 'org-outline-level)
  3687. (when (and org-ellipsis
  3688. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3689. (fboundp 'make-glyph-code))
  3690. (unless org-display-table
  3691. (setq org-display-table (make-display-table)))
  3692. (set-display-table-slot
  3693. org-display-table 4
  3694. (vconcat (mapcar
  3695. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3696. org-ellipsis)))
  3697. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3698. (setq buffer-display-table org-display-table))
  3699. (org-set-regexps-and-options)
  3700. ;; Calc embedded
  3701. (org-set-local 'calc-embedded-open-mode "# ")
  3702. (modify-syntax-entry ?# "<")
  3703. (modify-syntax-entry ?@ "w")
  3704. (if org-startup-truncated (setq truncate-lines t))
  3705. (org-set-local 'font-lock-unfontify-region-function
  3706. 'org-unfontify-region)
  3707. ;; Activate before-change-function
  3708. (org-set-local 'org-table-may-need-update t)
  3709. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3710. 'local)
  3711. ;; Check for running clock before killing a buffer
  3712. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3713. ;; Paragraphs and auto-filling
  3714. (org-set-autofill-regexps)
  3715. (setq indent-line-function 'org-indent-line-function)
  3716. (org-update-radio-target-regexp)
  3717. ;; Comment characters
  3718. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3719. (org-set-local 'comment-padding " ")
  3720. ;; Make isearch reveal context
  3721. (if (or (featurep 'xemacs)
  3722. (not (boundp 'outline-isearch-open-invisible-function)))
  3723. ;; Emacs 21 and XEmacs make use of the hook
  3724. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3725. ;; Emacs 22 deals with this through a special variable
  3726. (org-set-local 'outline-isearch-open-invisible-function
  3727. (lambda (&rest ignore) (org-show-context 'isearch))))
  3728. ;; If empty file that did not turn on org-mode automatically, make it to.
  3729. (if (and org-insert-mode-line-in-empty-file
  3730. (interactive-p)
  3731. (= (point-min) (point-max)))
  3732. (insert "# -*- mode: org -*-\n\n"))
  3733. (unless org-inhibit-startup
  3734. (when org-startup-align-all-tables
  3735. (let ((bmp (buffer-modified-p)))
  3736. (org-table-map-tables 'org-table-align)
  3737. (set-buffer-modified-p bmp)))
  3738. (org-cycle-hide-drawers 'all)
  3739. (cond
  3740. ((eq org-startup-folded t)
  3741. (org-cycle '(4)))
  3742. ((eq org-startup-folded 'content)
  3743. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3744. (org-cycle '(4)) (org-cycle '(4)))))))
  3745. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3746. (defsubst org-call-with-arg (command arg)
  3747. "Call COMMAND interactively, but pretend prefix are was ARG."
  3748. (let ((current-prefix-arg arg)) (call-interactively command)))
  3749. (defsubst org-current-line (&optional pos)
  3750. (save-excursion
  3751. (and pos (goto-char pos))
  3752. ;; works also in narrowed buffer, because we start at 1, not point-min
  3753. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3754. (defun org-current-time ()
  3755. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3756. (if (> org-time-stamp-rounding-minutes 0)
  3757. (let ((r org-time-stamp-rounding-minutes)
  3758. (time (decode-time)))
  3759. (apply 'encode-time
  3760. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3761. (nthcdr 2 time))))
  3762. (current-time)))
  3763. (defun org-add-props (string plist &rest props)
  3764. "Add text properties to entire string, from beginning to end.
  3765. PLIST may be a list of properties, PROPS are individual properties and values
  3766. that will be added to PLIST. Returns the string that was modified."
  3767. (add-text-properties
  3768. 0 (length string) (if props (append plist props) plist) string)
  3769. string)
  3770. (put 'org-add-props 'lisp-indent-function 2)
  3771. ;;;; Font-Lock stuff, including the activators
  3772. (defvar org-mouse-map (make-sparse-keymap))
  3773. (org-defkey org-mouse-map
  3774. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3775. (org-defkey org-mouse-map
  3776. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3777. (when org-mouse-1-follows-link
  3778. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3779. (when org-tab-follows-link
  3780. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3781. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3782. (when org-return-follows-link
  3783. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3784. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3785. (require 'font-lock)
  3786. (defconst org-non-link-chars "]\t\n\r<>")
  3787. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3788. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3789. (defvar org-link-re-with-space nil
  3790. "Matches a link with spaces, optional angular brackets around it.")
  3791. (defvar org-link-re-with-space2 nil
  3792. "Matches a link with spaces, optional angular brackets around it.")
  3793. (defvar org-angle-link-re nil
  3794. "Matches link with angular brackets, spaces are allowed.")
  3795. (defvar org-plain-link-re nil
  3796. "Matches plain link, without spaces.")
  3797. (defvar org-bracket-link-regexp nil
  3798. "Matches a link in double brackets.")
  3799. (defvar org-bracket-link-analytic-regexp nil
  3800. "Regular expression used to analyze links.
  3801. Here is what the match groups contain after a match:
  3802. 1: http:
  3803. 2: http
  3804. 3: path
  3805. 4: [desc]
  3806. 5: desc")
  3807. (defvar org-any-link-re nil
  3808. "Regular expression matching any link.")
  3809. (defun org-make-link-regexps ()
  3810. "Update the link regular expressions.
  3811. This should be called after the variable `org-link-types' has changed."
  3812. (setq org-link-re-with-space
  3813. (concat
  3814. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3815. "\\([^" org-non-link-chars " ]"
  3816. "[^" org-non-link-chars "]*"
  3817. "[^" org-non-link-chars " ]\\)>?")
  3818. org-link-re-with-space2
  3819. (concat
  3820. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3821. "\\([^" org-non-link-chars " ]"
  3822. "[^]\t\n\r]*"
  3823. "[^" org-non-link-chars " ]\\)>?")
  3824. org-angle-link-re
  3825. (concat
  3826. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3827. "\\([^" org-non-link-chars " ]"
  3828. "[^" org-non-link-chars "]*"
  3829. "\\)>")
  3830. org-plain-link-re
  3831. (concat
  3832. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3833. "\\([^]\t\n\r<>,;() ]+\\)")
  3834. org-bracket-link-regexp
  3835. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3836. org-bracket-link-analytic-regexp
  3837. (concat
  3838. "\\[\\["
  3839. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3840. "\\([^]]+\\)"
  3841. "\\]"
  3842. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3843. "\\]")
  3844. org-any-link-re
  3845. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3846. org-angle-link-re "\\)\\|\\("
  3847. org-plain-link-re "\\)")))
  3848. (org-make-link-regexps)
  3849. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3850. "Regular expression for fast time stamp matching.")
  3851. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3852. "Regular expression for fast time stamp matching.")
  3853. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3854. "Regular expression matching time strings for analysis.
  3855. This one does not require the space after the date.")
  3856. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3857. "Regular expression matching time strings for analysis.")
  3858. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
  3859. "Regular expression matching time stamps, with groups.")
  3860. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
  3861. "Regular expression matching time stamps (also [..]), with groups.")
  3862. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3863. "Regular expression matching a time stamp range.")
  3864. (defconst org-tr-regexp-both
  3865. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3866. "Regular expression matching a time stamp range.")
  3867. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3868. org-ts-regexp "\\)?")
  3869. "Regular expression matching a time stamp or time stamp range.")
  3870. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3871. org-ts-regexp-both "\\)?")
  3872. "Regular expression matching a time stamp or time stamp range.
  3873. The time stamps may be either active or inactive.")
  3874. (defvar org-emph-face nil)
  3875. (defun org-do-emphasis-faces (limit)
  3876. "Run through the buffer and add overlays to links."
  3877. (let (rtn)
  3878. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3879. (if (not (= (char-after (match-beginning 3))
  3880. (char-after (match-beginning 4))))
  3881. (progn
  3882. (setq rtn t)
  3883. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3884. 'face
  3885. (nth 1 (assoc (match-string 3)
  3886. org-emphasis-alist)))
  3887. (add-text-properties (match-beginning 2) (match-end 2)
  3888. '(font-lock-multiline t))
  3889. (backward-char 1))))
  3890. rtn))
  3891. (defun org-emphasize (&optional char)
  3892. "Insert or change an emphasis, i.e. a font like bold or italic.
  3893. If there is an active region, change that region to a new emphasis.
  3894. If there is no region, just insert the marker characters and position
  3895. the cursor between them.
  3896. CHAR should be either the marker character, or the first character of the
  3897. HTML tag associated with that emphasis. If CHAR is a space, the means
  3898. to remove the emphasis of the selected region.
  3899. If char is not given (for example in an interactive call) it
  3900. will be prompted for."
  3901. (interactive)
  3902. (let ((eal org-emphasis-alist) e det
  3903. (erc org-emphasis-regexp-components)
  3904. (prompt "")
  3905. (string "") beg end move tag c s)
  3906. (if (org-region-active-p)
  3907. (setq beg (region-beginning) end (region-end)
  3908. string (buffer-substring beg end))
  3909. (setq move t))
  3910. (while (setq e (pop eal))
  3911. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3912. c (aref tag 0))
  3913. (push (cons c (string-to-char (car e))) det)
  3914. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3915. (substring tag 1)))))
  3916. (unless char
  3917. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3918. (setq char (read-char-exclusive)))
  3919. (setq char (or (cdr (assoc char det)) char))
  3920. (if (equal char ?\ )
  3921. (setq s "" move nil)
  3922. (unless (assoc (char-to-string char) org-emphasis-alist)
  3923. (error "No such emphasis marker: \"%c\"" char))
  3924. (setq s (char-to-string char)))
  3925. (while (and (> (length string) 1)
  3926. (equal (substring string 0 1) (substring string -1))
  3927. (assoc (substring string 0 1) org-emphasis-alist))
  3928. (setq string (substring string 1 -1)))
  3929. (setq string (concat s string s))
  3930. (if beg (delete-region beg end))
  3931. (unless (or (bolp)
  3932. (string-match (concat "[" (nth 0 erc) "\n]")
  3933. (char-to-string (char-before (point)))))
  3934. (insert " "))
  3935. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3936. (char-to-string (char-after (point))))
  3937. (insert " ") (backward-char 1))
  3938. (insert string)
  3939. (and move (backward-char 1))))
  3940. (defun org-activate-plain-links (limit)
  3941. "Run through the buffer and add overlays to links."
  3942. (catch 'exit
  3943. (let (f)
  3944. (while (re-search-forward org-plain-link-re limit t)
  3945. (setq f (get-text-property (match-beginning 0) 'face))
  3946. (if (or (eq f 'org-tag)
  3947. (and (listp f) (memq 'org-tag f)))
  3948. nil
  3949. (add-text-properties (match-beginning 0) (match-end 0)
  3950. (list 'mouse-face 'highlight
  3951. 'rear-nonsticky t
  3952. 'keymap org-mouse-map
  3953. ))
  3954. (throw 'exit t))))))
  3955. (defun org-activate-angle-links (limit)
  3956. "Run through the buffer and add overlays to links."
  3957. (if (re-search-forward org-angle-link-re limit t)
  3958. (progn
  3959. (add-text-properties (match-beginning 0) (match-end 0)
  3960. (list 'mouse-face 'highlight
  3961. 'rear-nonsticky t
  3962. 'keymap org-mouse-map
  3963. ))
  3964. t)))
  3965. (defmacro org-maybe-intangible (props)
  3966. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3967. In emacs 21, invisible text is not avoided by the command loop, so the
  3968. intangible property is needed to make sure point skips this text.
  3969. In Emacs 22, this is not necessary. The intangible text property has
  3970. led to problems with flyspell. These problems are fixed in flyspell.el,
  3971. but we still avoid setting the property in Emacs 22 and later.
  3972. We use a macro so that the test can happen at compilation time."
  3973. (if (< emacs-major-version 22)
  3974. `(append '(intangible t) ,props)
  3975. props))
  3976. (defun org-activate-bracket-links (limit)
  3977. "Run through the buffer and add overlays to bracketed links."
  3978. (if (re-search-forward org-bracket-link-regexp limit t)
  3979. (let* ((help (concat "LINK: "
  3980. (org-match-string-no-properties 1)))
  3981. ;; FIXME: above we should remove the escapes.
  3982. ;; but that requires another match, protecting match data,
  3983. ;; a lot of overhead for font-lock.
  3984. (ip (org-maybe-intangible
  3985. (list 'invisible 'org-link 'rear-nonsticky t
  3986. 'keymap org-mouse-map 'mouse-face 'highlight
  3987. 'help-echo help)))
  3988. (vp (list 'rear-nonsticky t
  3989. 'keymap org-mouse-map 'mouse-face 'highlight
  3990. 'help-echo help)))
  3991. ;; We need to remove the invisible property here. Table narrowing
  3992. ;; may have made some of this invisible.
  3993. (remove-text-properties (match-beginning 0) (match-end 0)
  3994. '(invisible nil))
  3995. (if (match-end 3)
  3996. (progn
  3997. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3998. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3999. (add-text-properties (match-end 3) (match-end 0) ip))
  4000. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4001. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4002. (add-text-properties (match-end 1) (match-end 0) ip))
  4003. t)))
  4004. (defun org-activate-dates (limit)
  4005. "Run through the buffer and add overlays to dates."
  4006. (if (re-search-forward org-tsr-regexp-both limit t)
  4007. (progn
  4008. (add-text-properties (match-beginning 0) (match-end 0)
  4009. (list 'mouse-face 'highlight
  4010. 'rear-nonsticky t
  4011. 'keymap org-mouse-map))
  4012. (when org-display-custom-times
  4013. (if (match-end 3)
  4014. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4015. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4016. t)))
  4017. (defvar org-target-link-regexp nil
  4018. "Regular expression matching radio targets in plain text.")
  4019. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4020. "Regular expression matching a link target.")
  4021. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4022. "Regular expression matching a radio target.")
  4023. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4024. "Regular expression matching any target.")
  4025. (defun org-activate-target-links (limit)
  4026. "Run through the buffer and add overlays to target matches."
  4027. (when org-target-link-regexp
  4028. (let ((case-fold-search t))
  4029. (if (re-search-forward org-target-link-regexp limit t)
  4030. (progn
  4031. (add-text-properties (match-beginning 0) (match-end 0)
  4032. (list 'mouse-face 'highlight
  4033. 'rear-nonsticky t
  4034. 'keymap org-mouse-map
  4035. 'help-echo "Radio target link"
  4036. 'org-linked-text t))
  4037. t)))))
  4038. (defun org-update-radio-target-regexp ()
  4039. "Find all radio targets in this file and update the regular expression."
  4040. (interactive)
  4041. (when (memq 'radio org-activate-links)
  4042. (setq org-target-link-regexp
  4043. (org-make-target-link-regexp (org-all-targets 'radio)))
  4044. (org-restart-font-lock)))
  4045. (defun org-hide-wide-columns (limit)
  4046. (let (s e)
  4047. (setq s (text-property-any (point) (or limit (point-max))
  4048. 'org-cwidth t))
  4049. (when s
  4050. (setq e (next-single-property-change s 'org-cwidth))
  4051. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4052. (goto-char e)
  4053. t)))
  4054. (defun org-restart-font-lock ()
  4055. "Restart font-lock-mode, to force refontification."
  4056. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4057. ;; FIXME: Could font-lock-fontify-buffer be enough???
  4058. (font-lock-mode -1)
  4059. (font-lock-mode 1)))
  4060. (defun org-all-targets (&optional radio)
  4061. "Return a list of all targets in this file.
  4062. With optional argument RADIO, only find radio targets."
  4063. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4064. rtn)
  4065. (save-excursion
  4066. (goto-char (point-min))
  4067. (while (re-search-forward re nil t)
  4068. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4069. rtn)))
  4070. (defun org-make-target-link-regexp (targets)
  4071. "Make regular expression matching all strings in TARGETS.
  4072. The regular expression finds the targets also if there is a line break
  4073. between words."
  4074. (and targets
  4075. (concat
  4076. "\\<\\("
  4077. (mapconcat
  4078. (lambda (x)
  4079. (while (string-match " +" x)
  4080. (setq x (replace-match "\\s-+" t t x)))
  4081. x)
  4082. targets
  4083. "\\|")
  4084. "\\)\\>")))
  4085. (defun org-activate-tags (limit)
  4086. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4087. (progn
  4088. (add-text-properties (match-beginning 1) (match-end 1)
  4089. (list 'mouse-face 'highlight
  4090. 'rear-nonsticky t
  4091. 'keymap org-mouse-map))
  4092. t)))
  4093. (defun org-outline-level ()
  4094. (save-excursion
  4095. (looking-at outline-regexp)
  4096. (if (match-beginning 1)
  4097. (+ (org-get-string-indentation (match-string 1)) 1000)
  4098. (1- (- (match-end 0) (match-beginning 0))))))
  4099. (defvar org-font-lock-keywords nil)
  4100. (defconst org-property-re "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(\\S-.*\\)"
  4101. "Regular expression matching a property line.")
  4102. (defun org-set-font-lock-defaults ()
  4103. (let* ((em org-fontify-emphasized-text)
  4104. (lk org-activate-links)
  4105. (org-font-lock-extra-keywords
  4106. (list
  4107. ;; Headlines
  4108. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4109. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4110. ;; Table lines
  4111. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4112. (1 'org-table))
  4113. ;; Links
  4114. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4115. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4116. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4117. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4118. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4119. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4120. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4121. '(org-hide-wide-columns (0 nil append))
  4122. ;; TODO lines
  4123. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4124. '(1 (org-get-todo-face 1) t))
  4125. ;; DONE
  4126. (if org-fontify-done-headline
  4127. (list (concat "^[*]+ +\\<\\("
  4128. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4129. "\\)\\(.*\\)")
  4130. '(2 'org-headline-done t))
  4131. nil)
  4132. ;; Priorities
  4133. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4134. ;; Special keywords
  4135. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4136. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4137. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4138. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4139. ;; Emphasis
  4140. (if em
  4141. (if (featurep 'xemacs)
  4142. '(org-do-emphasis-faces (0 nil append))
  4143. '(org-do-emphasis-faces)))
  4144. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  4145. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4146. 2 'bold prepend)
  4147. (if org-provide-checkbox-statistics
  4148. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4149. (0 (org-get-checkbox-statistics-face) t)))
  4150. ;; COMMENT
  4151. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4152. "\\|" org-quote-string "\\)\\>")
  4153. '(1 'org-special-keyword t))
  4154. '("^#.*" (0 'font-lock-comment-face t))
  4155. ;; Code
  4156. '("^[ \t]*\\(:.*\\)" (1 'org-code t))
  4157. ;; Table internals
  4158. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4159. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4160. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4161. ;; Drawers
  4162. (list org-drawer-regexp '(0 'org-special-keyword t))
  4163. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4164. ;; Properties
  4165. (list org-property-re
  4166. '(1 'org-special-keyword t)
  4167. '(3 'org-property-value t))
  4168. (if org-format-transports-properties-p
  4169. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4170. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  4171. )))
  4172. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4173. ;; Now set the full font-lock-keywords
  4174. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4175. (org-set-local 'font-lock-defaults
  4176. '(org-font-lock-keywords t nil nil backward-paragraph))
  4177. (kill-local-variable 'font-lock-keywords) nil))
  4178. (defvar org-m nil)
  4179. (defvar org-l nil)
  4180. (defvar org-f nil)
  4181. (defun org-get-level-face (n)
  4182. "Get the right face for match N in font-lock matching of healdines."
  4183. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4184. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4185. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4186. (cond
  4187. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4188. ((eq n 2) org-f)
  4189. (t (if org-level-color-stars-only nil org-f))))
  4190. (defun org-get-todo-face (kwd)
  4191. "Get the right face for a TODO keyword KWD.
  4192. If KWD is a number, get the corresponding match group."
  4193. (if (numberp kwd) (setq kwd (match-string kwd)))
  4194. (or (cdr (assoc kwd org-todo-keyword-faces))
  4195. (and (member kwd org-done-keywords) 'org-done)
  4196. 'org-todo))
  4197. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4198. "Remove fontification and activation overlays from links."
  4199. (font-lock-default-unfontify-region beg end)
  4200. (let* ((buffer-undo-list t)
  4201. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4202. (inhibit-modification-hooks t)
  4203. deactivate-mark buffer-file-name buffer-file-truename)
  4204. (remove-text-properties beg end
  4205. '(mouse-face t keymap t org-linked-text t
  4206. rear-nonsticky t
  4207. invisible t intangible t))))
  4208. ;;;; Visibility cycling, including org-goto and indirect buffer
  4209. ;;; Cycling
  4210. (defvar org-cycle-global-status nil)
  4211. (make-variable-buffer-local 'org-cycle-global-status)
  4212. (defvar org-cycle-subtree-status nil)
  4213. (make-variable-buffer-local 'org-cycle-subtree-status)
  4214. ;;;###autoload
  4215. (defun org-cycle (&optional arg)
  4216. "Visibility cycling for Org-mode.
  4217. - When this function is called with a prefix argument, rotate the entire
  4218. buffer through 3 states (global cycling)
  4219. 1. OVERVIEW: Show only top-level headlines.
  4220. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4221. 3. SHOW ALL: Show everything.
  4222. - When point is at the beginning of a headline, rotate the subtree started
  4223. by this line through 3 different states (local cycling)
  4224. 1. FOLDED: Only the main headline is shown.
  4225. 2. CHILDREN: The main headline and the direct children are shown.
  4226. From this state, you can move to one of the children
  4227. and zoom in further.
  4228. 3. SUBTREE: Show the entire subtree, including body text.
  4229. - When there is a numeric prefix, go up to a heading with level ARG, do
  4230. a `show-subtree' and return to the previous cursor position. If ARG
  4231. is negative, go up that many levels.
  4232. - When point is not at the beginning of a headline, execute
  4233. `indent-relative', like TAB normally does. See the option
  4234. `org-cycle-emulate-tab' for details.
  4235. - Special case: if point is at the beginning of the buffer and there is
  4236. no headline in line 1, this function will act as if called with prefix arg.
  4237. But only if also the variable `org-cycle-global-at-bob' is t."
  4238. (interactive "P")
  4239. (let* ((outline-regexp
  4240. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4241. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4242. outline-regexp))
  4243. (bob-special (and org-cycle-global-at-bob (bobp)
  4244. (not (looking-at outline-regexp))))
  4245. (org-cycle-hook
  4246. (if bob-special
  4247. (delq 'org-optimize-window-after-visibility-change
  4248. (copy-sequence org-cycle-hook))
  4249. org-cycle-hook))
  4250. (pos (point)))
  4251. (if (or bob-special (equal arg '(4)))
  4252. ;; special case: use global cycling
  4253. (setq arg t))
  4254. (cond
  4255. ((org-at-table-p 'any)
  4256. ;; Enter the table or move to the next field in the table
  4257. (or (org-table-recognize-table.el)
  4258. (progn
  4259. (if arg (org-table-edit-field t)
  4260. (org-table-justify-field-maybe)
  4261. (call-interactively 'org-table-next-field)))))
  4262. ((eq arg t) ;; Global cycling
  4263. (cond
  4264. ((and (eq last-command this-command)
  4265. (eq org-cycle-global-status 'overview))
  4266. ;; We just created the overview - now do table of contents
  4267. ;; This can be slow in very large buffers, so indicate action
  4268. (message "CONTENTS...")
  4269. (org-content)
  4270. (message "CONTENTS...done")
  4271. (setq org-cycle-global-status 'contents)
  4272. (run-hook-with-args 'org-cycle-hook 'contents))
  4273. ((and (eq last-command this-command)
  4274. (eq org-cycle-global-status 'contents))
  4275. ;; We just showed the table of contents - now show everything
  4276. (show-all)
  4277. (message "SHOW ALL")
  4278. (setq org-cycle-global-status 'all)
  4279. (run-hook-with-args 'org-cycle-hook 'all))
  4280. (t
  4281. ;; Default action: go to overview
  4282. (org-overview)
  4283. (message "OVERVIEW")
  4284. (setq org-cycle-global-status 'overview)
  4285. (run-hook-with-args 'org-cycle-hook 'overview))))
  4286. ((and org-drawers org-drawer-regexp
  4287. (save-excursion
  4288. (beginning-of-line 1)
  4289. (looking-at org-drawer-regexp)))
  4290. ;; Toggle block visibility
  4291. (org-flag-drawer
  4292. (not (get-char-property (match-end 0) 'invisible))))
  4293. ((integerp arg)
  4294. ;; Show-subtree, ARG levels up from here.
  4295. (save-excursion
  4296. (org-back-to-heading)
  4297. (outline-up-heading (if (< arg 0) (- arg)
  4298. (- (funcall outline-level) arg)))
  4299. (org-show-subtree)))
  4300. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4301. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4302. ;; At a heading: rotate between three different views
  4303. (org-back-to-heading)
  4304. (let ((goal-column 0) eoh eol eos)
  4305. ;; First, some boundaries
  4306. (save-excursion
  4307. (org-back-to-heading)
  4308. (save-excursion
  4309. (beginning-of-line 2)
  4310. (while (and (not (eobp)) ;; this is like `next-line'
  4311. (get-char-property (1- (point)) 'invisible))
  4312. (beginning-of-line 2)) (setq eol (point)))
  4313. (outline-end-of-heading) (setq eoh (point))
  4314. (org-end-of-subtree t)
  4315. (unless (eobp)
  4316. (skip-chars-forward " \t\n")
  4317. (beginning-of-line 1) ; in case this is an item
  4318. )
  4319. (setq eos (1- (point))))
  4320. ;; Find out what to do next and set `this-command'
  4321. (cond
  4322. ((= eos eoh)
  4323. ;; Nothing is hidden behind this heading
  4324. (message "EMPTY ENTRY")
  4325. (setq org-cycle-subtree-status nil)
  4326. (save-excursion
  4327. (goto-char eos)
  4328. (outline-next-heading)
  4329. (if (org-invisible-p) (org-flag-heading nil))))
  4330. ((or (>= eol eos)
  4331. (not (string-match "\\S-" (buffer-substring eol eos))))
  4332. ;; Entire subtree is hidden in one line: open it
  4333. (org-show-entry)
  4334. (show-children)
  4335. (message "CHILDREN")
  4336. (save-excursion
  4337. (goto-char eos)
  4338. (outline-next-heading)
  4339. (if (org-invisible-p) (org-flag-heading nil)))
  4340. (setq org-cycle-subtree-status 'children)
  4341. (run-hook-with-args 'org-cycle-hook 'children))
  4342. ((and (eq last-command this-command)
  4343. (eq org-cycle-subtree-status 'children))
  4344. ;; We just showed the children, now show everything.
  4345. (org-show-subtree)
  4346. (message "SUBTREE")
  4347. (setq org-cycle-subtree-status 'subtree)
  4348. (run-hook-with-args 'org-cycle-hook 'subtree))
  4349. (t
  4350. ;; Default action: hide the subtree.
  4351. (hide-subtree)
  4352. (message "FOLDED")
  4353. (setq org-cycle-subtree-status 'folded)
  4354. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4355. ;; TAB emulation
  4356. (buffer-read-only (org-back-to-heading))
  4357. ((org-try-cdlatex-tab))
  4358. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4359. (or (not (bolp))
  4360. (not (looking-at outline-regexp))))
  4361. (call-interactively (global-key-binding "\t")))
  4362. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4363. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4364. (or (and (eq org-cycle-emulate-tab 'white)
  4365. (= (match-end 0) (point-at-eol)))
  4366. (and (eq org-cycle-emulate-tab 'whitestart)
  4367. (>= (match-end 0) pos))))
  4368. t
  4369. (eq org-cycle-emulate-tab t))
  4370. (if (and (looking-at "[ \n\r\t]")
  4371. (string-match "^[ \t]*$" (buffer-substring
  4372. (point-at-bol) (point))))
  4373. (progn
  4374. (beginning-of-line 1)
  4375. (and (looking-at "[ \t]+") (replace-match ""))))
  4376. (call-interactively (global-key-binding "\t")))
  4377. (t (save-excursion
  4378. (org-back-to-heading)
  4379. (org-cycle))))))
  4380. ;;;###autoload
  4381. (defun org-global-cycle (&optional arg)
  4382. "Cycle the global visibility. For details see `org-cycle'."
  4383. (interactive "P")
  4384. (let ((org-cycle-include-plain-lists
  4385. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4386. (if (integerp arg)
  4387. (progn
  4388. (show-all)
  4389. (hide-sublevels arg)
  4390. (setq org-cycle-global-status 'contents))
  4391. (org-cycle '(4)))))
  4392. (defun org-overview ()
  4393. "Switch to overview mode, shoing only top-level headlines.
  4394. Really, this shows all headlines with level equal or greater than the level
  4395. of the first headline in the buffer. This is important, because if the
  4396. first headline is not level one, then (hide-sublevels 1) gives confusing
  4397. results."
  4398. (interactive)
  4399. (let ((level (save-excursion
  4400. (goto-char (point-min))
  4401. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4402. (progn
  4403. (goto-char (match-beginning 0))
  4404. (funcall outline-level))))))
  4405. (and level (hide-sublevels level))))
  4406. (defun org-content (&optional arg)
  4407. "Show all headlines in the buffer, like a table of contents.
  4408. With numerical argument N, show content up to level N."
  4409. (interactive "P")
  4410. (save-excursion
  4411. ;; Visit all headings and show their offspring
  4412. (and (integerp arg) (org-overview))
  4413. (goto-char (point-max))
  4414. (catch 'exit
  4415. (while (and (progn (condition-case nil
  4416. (outline-previous-visible-heading 1)
  4417. (error (goto-char (point-min))))
  4418. t)
  4419. (looking-at outline-regexp))
  4420. (if (integerp arg)
  4421. (show-children (1- arg))
  4422. (show-branches))
  4423. (if (bobp) (throw 'exit nil))))))
  4424. (defun org-optimize-window-after-visibility-change (state)
  4425. "Adjust the window after a change in outline visibility.
  4426. This function is the default value of the hook `org-cycle-hook'."
  4427. (when (get-buffer-window (current-buffer))
  4428. (cond
  4429. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4430. ; ((eq state 'overview) (org-beginning-of-line))
  4431. ((eq state 'content) nil)
  4432. ((eq state 'all) nil)
  4433. ((eq state 'folded) nil)
  4434. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4435. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4436. (defun org-cycle-show-empty-lines (state)
  4437. "Show empty lines above all visible headlines.
  4438. The region to be covered depends on STATE when called through
  4439. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4440. entire buffer covered. Note that an empty line is only shown if there
  4441. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4442. (when (> org-cycle-separator-lines 0)
  4443. (save-excursion
  4444. (let* ((n org-cycle-separator-lines)
  4445. (re (cond
  4446. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4447. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4448. (t (let ((ns (number-to-string (- n 2))))
  4449. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4450. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4451. beg end)
  4452. (cond
  4453. ((memq state '(overview contents t))
  4454. (setq beg (point-min) end (point-max)))
  4455. ((memq state '(children folded))
  4456. (setq beg (point) end (progn (org-end-of-subtree t t)
  4457. (beginning-of-line 2)
  4458. (point)))))
  4459. (when beg
  4460. (goto-char beg)
  4461. (while (re-search-forward re end t)
  4462. (if (not (get-char-property (match-end 1) 'invisible))
  4463. (outline-flag-region
  4464. (match-beginning 1) (match-end 1) nil)))))))
  4465. ;; Never hide empty lines at the end of the file.
  4466. (save-excursion
  4467. (goto-char (point-max))
  4468. (outline-previous-heading)
  4469. (outline-end-of-heading)
  4470. (if (and (looking-at "[ \t\n]+")
  4471. (= (match-end 0) (point-max)))
  4472. (outline-flag-region (point) (match-end 0) nil))))
  4473. (defun org-subtree-end-visible-p ()
  4474. "Is the end of the current subtree visible?"
  4475. (pos-visible-in-window-p
  4476. (save-excursion (org-end-of-subtree t) (point))))
  4477. (defun org-first-headline-recenter (&optional N)
  4478. "Move cursor to the first headline and recenter the headline.
  4479. Optional argument N means, put the headline into the Nth line of the window."
  4480. (goto-char (point-min))
  4481. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4482. (beginning-of-line)
  4483. (recenter (prefix-numeric-value N))))
  4484. ;;; Org-goto
  4485. (defvar org-goto-window-configuration nil)
  4486. (defvar org-goto-marker nil)
  4487. (defvar org-goto-map
  4488. (let ((map (make-sparse-keymap)))
  4489. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  4490. (while (setq cmd (pop cmds))
  4491. (substitute-key-definition cmd cmd map global-map)))
  4492. (org-defkey map "\C-m" 'org-goto-ret)
  4493. (org-defkey map [(left)] 'org-goto-left)
  4494. (org-defkey map [(right)] 'org-goto-right)
  4495. (org-defkey map [(?q)] 'org-goto-quit)
  4496. (org-defkey map [(control ?g)] 'org-goto-quit)
  4497. (org-defkey map "\C-i" 'org-cycle)
  4498. (org-defkey map [(tab)] 'org-cycle)
  4499. (org-defkey map [(down)] 'outline-next-visible-heading)
  4500. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4501. (org-defkey map "n" 'outline-next-visible-heading)
  4502. (org-defkey map "p" 'outline-previous-visible-heading)
  4503. (org-defkey map "f" 'outline-forward-same-level)
  4504. (org-defkey map "b" 'outline-backward-same-level)
  4505. (org-defkey map "u" 'outline-up-heading)
  4506. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4507. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4508. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4509. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4510. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4511. ;; FIXME: Could we use suppress-keymap?
  4512. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4513. (while l (org-defkey map (int-to-string (pop l)) 'digit-argument)))
  4514. map))
  4515. (defconst org-goto-help
  4516. "Select a location to jump to, press RET
  4517. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  4518. (defun org-goto ()
  4519. "Go to a different location of the document, keeping current visibility.
  4520. When you want to go to a different location in a document, the fastest way
  4521. is often to fold the entire buffer and then dive into the tree. This
  4522. method has the disadvantage, that the previous location will be folded,
  4523. which may not be what you want.
  4524. This command works around this by showing a copy of the current buffer in
  4525. overview mode. You can dive into the tree in that copy, to find the
  4526. location you want to reach. When pressing RET, the command returns to the
  4527. original buffer in which the visibility is still unchanged. It then jumps
  4528. to the new location, making it and the headline hierarchy above it visible."
  4529. (interactive)
  4530. (let* ((org-goto-start-pos (point))
  4531. (selected-point
  4532. (org-get-location (current-buffer) org-goto-help)))
  4533. (if selected-point
  4534. (progn
  4535. (org-mark-ring-push org-goto-start-pos)
  4536. (goto-char selected-point)
  4537. (if (or (org-invisible-p) (org-invisible-p2))
  4538. (org-show-context 'org-goto)))
  4539. (error "Quit"))))
  4540. (defvar org-selected-point nil) ; dynamically scoped parameter
  4541. (defun org-get-location (buf help)
  4542. "Let the user select a location in the Org-mode buffer BUF.
  4543. This function uses a recursive edit. It returns the selected position
  4544. or nil."
  4545. (let (org-selected-point)
  4546. (save-excursion
  4547. (save-window-excursion
  4548. (delete-other-windows)
  4549. (switch-to-buffer (get-buffer-create "*org-goto*"))
  4550. (with-output-to-temp-buffer "*Help*"
  4551. (princ help))
  4552. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4553. (setq buffer-read-only nil)
  4554. (erase-buffer)
  4555. (insert-buffer-substring buf)
  4556. (let ((org-startup-truncated t)
  4557. (org-startup-folded t)
  4558. (org-startup-align-all-tables nil))
  4559. (org-mode))
  4560. (setq buffer-read-only t)
  4561. (if (and (boundp 'org-goto-start-pos)
  4562. (integer-or-marker-p org-goto-start-pos))
  4563. (let ((org-show-hierarchy-above t)
  4564. (org-show-siblings t)
  4565. (org-show-following-heading t))
  4566. (goto-char org-goto-start-pos)
  4567. (and (org-invisible-p) (org-show-context)))
  4568. (goto-char (point-min)))
  4569. (org-beginning-of-line)
  4570. (message "Select location and press RET")
  4571. ;; now we make sure that during selection, ony very few keys work
  4572. ;; and that it is impossible to switch to another window.
  4573. (let ((gm (current-global-map))
  4574. (overriding-local-map org-goto-map))
  4575. (unwind-protect
  4576. (progn
  4577. (use-global-map org-goto-map)
  4578. (recursive-edit))
  4579. (use-global-map gm)))))
  4580. (kill-buffer "*org-goto*")
  4581. org-selected-point))
  4582. (defun org-goto-ret (&optional arg)
  4583. "Finish `org-goto' by going to the new location."
  4584. (interactive "P")
  4585. (setq org-selected-point (point)
  4586. current-prefix-arg arg)
  4587. (throw 'exit nil))
  4588. (defun org-goto-left ()
  4589. "Finish `org-goto' by going to the new location."
  4590. (interactive)
  4591. (if (org-on-heading-p)
  4592. (progn
  4593. (beginning-of-line 1)
  4594. (setq org-selected-point (point)
  4595. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4596. (throw 'exit nil))
  4597. (error "Not on a heading")))
  4598. (defun org-goto-right ()
  4599. "Finish `org-goto' by going to the new location."
  4600. (interactive)
  4601. (if (org-on-heading-p)
  4602. (progn
  4603. (outline-end-of-subtree)
  4604. (or (eobp) (forward-char 1))
  4605. (setq org-selected-point (point)
  4606. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4607. (throw 'exit nil))
  4608. (error "Not on a heading")))
  4609. (defun org-goto-quit ()
  4610. "Finish `org-goto' without cursor motion."
  4611. (interactive)
  4612. (setq org-selected-point nil)
  4613. (throw 'exit nil))
  4614. ;;; Indirect buffer display of subtrees
  4615. (defvar org-indirect-dedicated-frame nil
  4616. "This is the frame being used for indirect tree display.")
  4617. (defvar org-last-indirect-buffer nil)
  4618. (defun org-tree-to-indirect-buffer (&optional arg)
  4619. "Create indirect buffer and narrow it to current subtree.
  4620. With numerical prefix ARG, go up to this level and then take that tree.
  4621. If ARG is negative, go up that many levels.
  4622. Normally this command removes the indirect buffer previously made
  4623. with this command. However, when called with a C-u prefix, the last buffer
  4624. is kept so that you can work with several indirect buffers at the same time.
  4625. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4626. requests that a new frame be made for the new buffer, so that the dedicated
  4627. frame is not changed."
  4628. (interactive "P")
  4629. (let ((cbuf (current-buffer))
  4630. (cwin (selected-window))
  4631. (pos (point))
  4632. beg end level heading ibuf)
  4633. (save-excursion
  4634. (org-back-to-heading t)
  4635. (when (numberp arg)
  4636. (setq level (org-outline-level))
  4637. (if (< arg 0) (setq arg (+ level arg)))
  4638. (while (> (setq level (org-outline-level)) arg)
  4639. (outline-up-heading 1 t)))
  4640. (setq beg (point)
  4641. heading (org-get-heading))
  4642. (org-end-of-subtree t) (setq end (point)))
  4643. (if (and (not arg)
  4644. (buffer-live-p org-last-indirect-buffer))
  4645. (kill-buffer org-last-indirect-buffer))
  4646. (setq ibuf (org-get-indirect-buffer cbuf)
  4647. org-last-indirect-buffer ibuf)
  4648. (cond
  4649. ((or (eq org-indirect-buffer-display 'new-frame)
  4650. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4651. (select-frame (make-frame))
  4652. (delete-other-windows)
  4653. (switch-to-buffer ibuf)
  4654. (org-set-frame-title heading))
  4655. ((eq org-indirect-buffer-display 'dedicated-frame)
  4656. (raise-frame
  4657. (select-frame (or (and org-indirect-dedicated-frame
  4658. (frame-live-p org-indirect-dedicated-frame)
  4659. org-indirect-dedicated-frame)
  4660. (setq org-indirect-dedicated-frame (make-frame)))))
  4661. (delete-other-windows)
  4662. (switch-to-buffer ibuf)
  4663. (org-set-frame-title (concat "Indirect: " heading)))
  4664. ((eq org-indirect-buffer-display 'current-window)
  4665. (switch-to-buffer ibuf))
  4666. ((eq org-indirect-buffer-display 'other-window)
  4667. (pop-to-buffer ibuf))
  4668. (t (error "Invalid value.")))
  4669. (if (featurep 'xemacs)
  4670. (save-excursion (org-mode) (turn-on-font-lock)))
  4671. (narrow-to-region beg end)
  4672. (show-all)
  4673. (goto-char pos)
  4674. (and (window-live-p cwin) (select-window cwin))))
  4675. (defun org-get-indirect-buffer (&optional buffer)
  4676. (setq buffer (or buffer (current-buffer)))
  4677. (let ((n 1) (base (buffer-name buffer)) bname)
  4678. (while (buffer-live-p
  4679. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4680. (setq n (1+ n)))
  4681. (condition-case nil
  4682. (make-indirect-buffer buffer bname 'clone)
  4683. (error (make-indirect-buffer buffer bname)))))
  4684. (defun org-set-frame-title (title)
  4685. "Set the title of the current frame to the string TITLE."
  4686. ;; FIXME: how to name a single frame in XEmacs???
  4687. (unless (featurep 'xemacs)
  4688. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4689. ;;;; Structure editing
  4690. ;;; Inserting headlines
  4691. (defun org-insert-heading (&optional force-heading)
  4692. "Insert a new heading or item with same depth at point.
  4693. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4694. If point is at the beginning of a headline, insert a sibling before the
  4695. current headline. If point is in the middle of a headline, split the headline
  4696. at that position and make the rest of the headline part of the sibling below
  4697. the current headline."
  4698. (interactive "P")
  4699. (if (= (buffer-size) 0)
  4700. (insert "\n* ")
  4701. (when (or force-heading (not (org-insert-item)))
  4702. (let* ((head (save-excursion
  4703. (condition-case nil
  4704. (progn
  4705. (org-back-to-heading)
  4706. (match-string 0))
  4707. (error "*"))))
  4708. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4709. pos)
  4710. (cond
  4711. ((and (org-on-heading-p) (bolp)
  4712. (or (bobp)
  4713. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4714. (open-line (if blank 2 1)))
  4715. ((and (bolp)
  4716. (or (bobp)
  4717. (save-excursion
  4718. (backward-char 1) (not (org-invisible-p)))))
  4719. nil)
  4720. (t (newline (if blank 2 1))))
  4721. (insert head) (just-one-space)
  4722. (setq pos (point))
  4723. (end-of-line 1)
  4724. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4725. (run-hooks 'org-insert-heading-hook)))))
  4726. (defun org-insert-todo-heading (arg)
  4727. "Insert a new heading with the same level and TODO state as current heading.
  4728. If the heading has no TODO state, or if the state is DONE, use the first
  4729. state (TODO by default). Also with prefix arg, force first state."
  4730. (interactive "P")
  4731. (when (not (org-insert-item 'checkbox))
  4732. (org-insert-heading)
  4733. (save-excursion
  4734. (org-back-to-heading)
  4735. (outline-previous-heading)
  4736. (looking-at org-todo-line-regexp))
  4737. (if (or arg
  4738. (not (match-beginning 2))
  4739. (member (match-string 2) org-done-keywords))
  4740. (insert (car org-todo-keywords-1) " ")
  4741. (insert (match-string 2) " "))))
  4742. (defun org-insert-subheading (arg)
  4743. "Insert a new subheading and demote it.
  4744. Works for outline headings and for plain lists alike."
  4745. (interactive "P")
  4746. (org-insert-heading arg)
  4747. (cond
  4748. ((org-on-heading-p) (org-do-demote))
  4749. ((org-at-item-p) (org-indent-item 1))))
  4750. (defun org-insert-todo-subheading (arg)
  4751. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4752. Works for outline headings and for plain lists alike."
  4753. (interactive "P")
  4754. (org-insert-todo-heading arg)
  4755. (cond
  4756. ((org-on-heading-p) (org-do-demote))
  4757. ((org-at-item-p) (org-indent-item 1))))
  4758. ;;; Promotion and Demotion
  4759. (defun org-promote-subtree ()
  4760. "Promote the entire subtree.
  4761. See also `org-promote'."
  4762. (interactive)
  4763. (save-excursion
  4764. (org-map-tree 'org-promote))
  4765. (org-fix-position-after-promote))
  4766. (defun org-demote-subtree ()
  4767. "Demote the entire subtree. See `org-demote'.
  4768. See also `org-promote'."
  4769. (interactive)
  4770. (save-excursion
  4771. (org-map-tree 'org-demote))
  4772. (org-fix-position-after-promote))
  4773. (defun org-do-promote ()
  4774. "Promote the current heading higher up the tree.
  4775. If the region is active in `transient-mark-mode', promote all headings
  4776. in the region."
  4777. (interactive)
  4778. (save-excursion
  4779. (if (org-region-active-p)
  4780. (org-map-region 'org-promote (region-beginning) (region-end))
  4781. (org-promote)))
  4782. (org-fix-position-after-promote))
  4783. (defun org-do-demote ()
  4784. "Demote the current heading lower down the tree.
  4785. If the region is active in `transient-mark-mode', demote all headings
  4786. in the region."
  4787. (interactive)
  4788. (save-excursion
  4789. (if (org-region-active-p)
  4790. (org-map-region 'org-demote (region-beginning) (region-end))
  4791. (org-demote)))
  4792. (org-fix-position-after-promote))
  4793. (defun org-fix-position-after-promote ()
  4794. "Make sure that after pro/demotion cursor position is right."
  4795. (let ((pos (point)))
  4796. (when (save-excursion
  4797. (beginning-of-line 1)
  4798. (looking-at org-todo-line-regexp)
  4799. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4800. (cond ((eobp) (insert " "))
  4801. ((eolp) (insert " "))
  4802. ((equal (char-after) ?\ ) (forward-char 1))))))
  4803. (defun org-reduced-level (l)
  4804. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4805. (defun org-get-legal-level (level &optional change)
  4806. "Rectify a level change under the influence of `org-odd-levels-only'
  4807. LEVEL is a current level, CHANGE is by how much the level should be
  4808. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4809. even level numbers will become the next higher odd number."
  4810. (if org-odd-levels-only
  4811. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4812. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4813. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4814. (max 1 (+ level change))))
  4815. (defun org-promote ()
  4816. "Promote the current heading higher up the tree.
  4817. If the region is active in `transient-mark-mode', promote all headings
  4818. in the region."
  4819. (org-back-to-heading t)
  4820. (let* ((level (save-match-data (funcall outline-level)))
  4821. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  4822. (diff (abs (- level (length up-head) -1))))
  4823. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4824. (replace-match up-head nil t)
  4825. ;; Fixup tag positioning
  4826. (and org-auto-align-tags (org-set-tags nil t))
  4827. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4828. (defun org-demote ()
  4829. "Demote the current heading lower down the tree.
  4830. If the region is active in `transient-mark-mode', demote all headings
  4831. in the region."
  4832. (org-back-to-heading t)
  4833. (let* ((level (save-match-data (funcall outline-level)))
  4834. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  4835. (diff (abs (- level (length down-head) -1))))
  4836. (replace-match down-head nil t)
  4837. ;; Fixup tag positioning
  4838. (and org-auto-align-tags (org-set-tags nil t))
  4839. (if org-adapt-indentation (org-fixup-indentation diff))))
  4840. (defun org-map-tree (fun)
  4841. "Call FUN for every heading underneath the current one."
  4842. (org-back-to-heading)
  4843. (let ((level (funcall outline-level)))
  4844. (save-excursion
  4845. (funcall fun)
  4846. (while (and (progn
  4847. (outline-next-heading)
  4848. (> (funcall outline-level) level))
  4849. (not (eobp)))
  4850. (funcall fun)))))
  4851. (defun org-map-region (fun beg end)
  4852. "Call FUN for every heading between BEG and END."
  4853. (let ((org-ignore-region t))
  4854. (save-excursion
  4855. (setq end (copy-marker end))
  4856. (goto-char beg)
  4857. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4858. (< (point) end))
  4859. (funcall fun))
  4860. (while (and (progn
  4861. (outline-next-heading)
  4862. (< (point) end))
  4863. (not (eobp)))
  4864. (funcall fun)))))
  4865. (defun org-fixup-indentation (diff)
  4866. "Change the indentation in the current entry by DIFF
  4867. However, if any line in the current entry has no indentation, or if it
  4868. would end up with no indentation after the change, nothing at all is done."
  4869. (save-excursion
  4870. (let ((end (save-excursion (outline-next-heading)
  4871. (point-marker)))
  4872. (prohibit (if (> diff 0)
  4873. "^\\S-"
  4874. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4875. col)
  4876. (unless (save-excursion (end-of-line 1)
  4877. (re-search-forward prohibit end t))
  4878. (while (re-search-forward "^[ \t]+" end t)
  4879. (goto-char (match-end 0))
  4880. (setq col (current-column))
  4881. (if (< diff 0) (replace-match ""))
  4882. (indent-to (+ diff col))))
  4883. (move-marker end nil))))
  4884. (defun org-convert-to-odd-levels ()
  4885. "Convert an org-mode file with all levels allowed to one with odd levels.
  4886. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4887. level 5 etc."
  4888. (interactive)
  4889. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4890. (let ((org-odd-levels-only nil) n)
  4891. (save-excursion
  4892. (goto-char (point-min))
  4893. (while (re-search-forward "^\\*\\*+ " nil t)
  4894. (setq n (- (length (match-string 0)) 2))
  4895. (while (>= (setq n (1- n)) 0)
  4896. (org-demote))
  4897. (end-of-line 1))))))
  4898. (defun org-convert-to-oddeven-levels ()
  4899. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4900. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4901. section with an even level, conversion would destroy the structure of the file. An error
  4902. is signaled in this case."
  4903. (interactive)
  4904. (goto-char (point-min))
  4905. ;; First check if there are no even levels
  4906. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4907. (org-show-context t)
  4908. (error "Not all levels are odd in this file. Conversion not possible."))
  4909. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4910. (let ((org-odd-levels-only nil) n)
  4911. (save-excursion
  4912. (goto-char (point-min))
  4913. (while (re-search-forward "^\\*\\*+ " nil t)
  4914. (setq n (/ (length (1- (match-string 0))) 2))
  4915. (while (>= (setq n (1- n)) 0)
  4916. (org-promote))
  4917. (end-of-line 1))))))
  4918. (defun org-tr-level (n)
  4919. "Make N odd if required."
  4920. (if org-odd-levels-only (1+ (/ n 2)) n))
  4921. ;;; Vertical tree motion, cutting and pasting of subtrees
  4922. (defun org-move-subtree-up (&optional arg)
  4923. "Move the current subtree up past ARG headlines of the same level."
  4924. (interactive "p")
  4925. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4926. (defun org-move-subtree-down (&optional arg)
  4927. "Move the current subtree down past ARG headlines of the same level."
  4928. (interactive "p")
  4929. (setq arg (prefix-numeric-value arg))
  4930. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4931. 'outline-get-last-sibling))
  4932. (ins-point (make-marker))
  4933. (cnt (abs arg))
  4934. beg end txt folded)
  4935. ;; Select the tree
  4936. (org-back-to-heading)
  4937. (setq beg (point))
  4938. (save-match-data
  4939. (save-excursion (outline-end-of-heading)
  4940. (setq folded (org-invisible-p)))
  4941. (outline-end-of-subtree))
  4942. (outline-next-heading)
  4943. (setq end (point))
  4944. ;; Find insertion point, with error handling
  4945. (goto-char beg)
  4946. (while (> cnt 0)
  4947. (or (and (funcall movfunc) (looking-at outline-regexp))
  4948. (progn (goto-char beg)
  4949. (error "Cannot move past superior level or buffer limit")))
  4950. (setq cnt (1- cnt)))
  4951. (if (> arg 0)
  4952. ;; Moving forward - still need to move over subtree
  4953. (progn (outline-end-of-subtree)
  4954. (outline-next-heading)
  4955. (if (not (or (looking-at (concat "^" outline-regexp))
  4956. (bolp)))
  4957. (newline))))
  4958. (move-marker ins-point (point))
  4959. (setq txt (buffer-substring beg end))
  4960. (delete-region beg end)
  4961. (insert txt)
  4962. (or (bolp) (insert "\n"))
  4963. (goto-char ins-point)
  4964. (if folded (hide-subtree))
  4965. (move-marker ins-point nil)))
  4966. (defvar org-subtree-clip ""
  4967. "Clipboard for cut and paste of subtrees.
  4968. This is actually only a copy of the kill, because we use the normal kill
  4969. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4970. (defvar org-subtree-clip-folded nil
  4971. "Was the last copied subtree folded?
  4972. This is used to fold the tree back after pasting.")
  4973. (defun org-cut-subtree ()
  4974. "Cut the current subtree into the clipboard.
  4975. This is a short-hand for marking the subtree and then cutting it."
  4976. (interactive)
  4977. (org-copy-subtree 'cut))
  4978. (defun org-copy-subtree (&optional cut)
  4979. "Cut the current subtree into the clipboard.
  4980. This is a short-hand for marking the subtree and then copying it.
  4981. If CUT is non-nil, actually cut the subtree."
  4982. (interactive)
  4983. (let (beg end folded)
  4984. (if (interactive-p)
  4985. (org-back-to-heading nil) ; take what looks like a subtree
  4986. (org-back-to-heading t)) ; take what is really there
  4987. (setq beg (point))
  4988. (save-match-data
  4989. (save-excursion (outline-end-of-heading)
  4990. (setq folded (org-invisible-p)))
  4991. (outline-end-of-subtree))
  4992. (if (equal (char-after) ?\n) (forward-char 1))
  4993. (setq end (point))
  4994. (goto-char beg)
  4995. (when (> end beg)
  4996. (setq org-subtree-clip-folded folded)
  4997. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4998. (setq org-subtree-clip (current-kill 0))
  4999. (message "%s: Subtree with %d characters"
  5000. (if cut "Cut" "Copied")
  5001. (length org-subtree-clip)))))
  5002. (defun org-paste-subtree (&optional level tree)
  5003. "Paste the clipboard as a subtree, with modification of headline level.
  5004. The entire subtree is promoted or demoted in order to match a new headline
  5005. level. By default, the new level is derived from the visible headings
  5006. before and after the insertion point, and taken to be the inferior headline
  5007. level of the two. So if the previous visible heading is level 3 and the
  5008. next is level 4 (or vice versa), level 4 will be used for insertion.
  5009. This makes sure that the subtree remains an independent subtree and does
  5010. not swallow low level entries.
  5011. You can also force a different level, either by using a numeric prefix
  5012. argument, or by inserting the heading marker by hand. For example, if the
  5013. cursor is after \"*****\", then the tree will be shifted to level 5.
  5014. If you want to insert the tree as is, just use \\[yank].
  5015. If optional TREE is given, use this text instead of the kill ring."
  5016. (interactive "P")
  5017. (unless (org-kill-is-subtree-p tree)
  5018. (error
  5019. (substitute-command-keys
  5020. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5021. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5022. (^re (concat "^\\(" outline-regexp "\\)"))
  5023. (re (concat "\\(" outline-regexp "\\)"))
  5024. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  5025. (old-level (if (string-match ^re txt)
  5026. (- (match-end 0) (match-beginning 0) 1)
  5027. -1))
  5028. (force-level (cond (level (prefix-numeric-value level))
  5029. ((string-match
  5030. ^re_ (buffer-substring (point-at-bol) (point)))
  5031. (- (match-end 0) (match-beginning 0)))
  5032. (t nil)))
  5033. (previous-level (save-excursion
  5034. (condition-case nil
  5035. (progn
  5036. (outline-previous-visible-heading 1)
  5037. (if (looking-at re)
  5038. (- (match-end 0) (match-beginning 0))
  5039. 1))
  5040. (error 1))))
  5041. (next-level (save-excursion
  5042. (condition-case nil
  5043. (progn
  5044. (outline-next-visible-heading 1)
  5045. (if (looking-at re)
  5046. (- (match-end 0) (match-beginning 0))
  5047. 1))
  5048. (error 1))))
  5049. (new-level (or force-level (max previous-level next-level)))
  5050. (shift (if (or (= old-level -1)
  5051. (= new-level -1)
  5052. (= old-level new-level))
  5053. 0
  5054. (- new-level old-level)))
  5055. (shift1 shift)
  5056. (delta (if (> shift 0) -1 1))
  5057. (func (if (> shift 0) 'org-demote 'org-promote))
  5058. (org-odd-levels-only nil)
  5059. beg end)
  5060. ;; Remove the forces level indicator
  5061. (if force-level
  5062. (delete-region (point-at-bol) (point)))
  5063. ;; Make sure we start at the beginning of an empty line
  5064. (if (not (bolp)) (insert "\n"))
  5065. (if (not (looking-at "[ \t]*$"))
  5066. (progn (insert "\n") (backward-char 1)))
  5067. ;; Paste
  5068. (setq beg (point))
  5069. (if (string-match "[ \t\r\n]+\\'" txt)
  5070. (setq txt (replace-match "\n" t t txt)))
  5071. (insert txt)
  5072. (setq end (point))
  5073. (if (looking-at "[ \t\r\n]+")
  5074. (replace-match "\n"))
  5075. (goto-char beg)
  5076. ;; Shift if necessary
  5077. (if (= shift 0)
  5078. (message "Pasted at level %d, without shift" new-level)
  5079. (save-restriction
  5080. (narrow-to-region beg end)
  5081. (while (not (= shift 0))
  5082. (org-map-region func (point-min) (point-max))
  5083. (setq shift (+ delta shift)))
  5084. (goto-char (point-min))
  5085. (message "Pasted at level %d, with shift by %d levels"
  5086. new-level shift1)))
  5087. (if (and kill-ring
  5088. (eq org-subtree-clip (current-kill 0))
  5089. org-subtree-clip-folded)
  5090. ;; The tree was folded before it was killed/copied
  5091. (hide-subtree))))
  5092. (defun org-kill-is-subtree-p (&optional txt)
  5093. "Check if the current kill is an outline subtree, or a set of trees.
  5094. Returns nil if kill does not start with a headline, or if the first
  5095. headline level is not the largest headline level in the tree.
  5096. So this will actually accept several entries of equal levels as well,
  5097. which is OK for `org-paste-subtree'.
  5098. If optional TXT is given, check this string instead of the current kill."
  5099. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5100. (start-level (and kill
  5101. (string-match (concat "\\`" outline-regexp) kill)
  5102. (- (match-end 0) (match-beginning 0))))
  5103. (re (concat "^" outline-regexp))
  5104. (start 1))
  5105. (if (not start-level)
  5106. nil ;; does not even start with a heading
  5107. (catch 'exit
  5108. (while (setq start (string-match re kill (1+ start)))
  5109. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  5110. (throw 'exit nil)))
  5111. t))))
  5112. (defun org-narrow-to-subtree ()
  5113. "Narrow buffer to the current subtree."
  5114. (interactive)
  5115. (save-excursion
  5116. (narrow-to-region
  5117. (progn (org-back-to-heading) (point))
  5118. (progn (org-end-of-subtree t t) (point)))))
  5119. ;;; Outline Sorting
  5120. (defun org-sort (with-case)
  5121. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  5122. (interactive "P")
  5123. (if (org-at-table-p)
  5124. (org-call-with-arg 'org-table-sort-lines with-case)
  5125. (org-call-with-arg 'org-sort-entries with-case)))
  5126. (defun org-sort-entries (&optional with-case sorting-type)
  5127. "Sort entries on a certain level of an outline tree.
  5128. If there is an active region, the entries in the region are sorted.
  5129. Else, if the cursor is before the first entry, sort the top-level items.
  5130. Else, the children of the entry at point are sorted.
  5131. Sorting can be alphabetically, numerically, and by date/time as given by
  5132. the first time stamp in the entry. The command prompts for the sorting
  5133. type unless it has been given to the function through the SORTING-TYPE
  5134. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  5135. Comparing entries ignores case by default. However, with an optional argument
  5136. WITH-CASE, the sorting considers case as well. With two prefix arguments
  5137. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  5138. (interactive "P")
  5139. (let ((unique (equal with-case '(16)))
  5140. start beg end entries stars re re2 p nentries (nremoved 0)
  5141. last txt what)
  5142. ;; Find beginning and end of region to sort
  5143. (cond
  5144. ((org-region-active-p)
  5145. ;; we will sort the region
  5146. (setq end (region-end)
  5147. what "region")
  5148. (goto-char (region-beginning))
  5149. (if (not (org-on-heading-p)) (outline-next-heading))
  5150. (setq start (point)))
  5151. ((or (org-on-heading-p)
  5152. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5153. ;; we will sort the children of the current headline
  5154. (org-back-to-heading)
  5155. (setq start (point) end (org-end-of-subtree) what "children")
  5156. (goto-char start)
  5157. (show-subtree)
  5158. (outline-next-heading))
  5159. (t
  5160. ;; we will sort the top-level entries in this file
  5161. (goto-char (point-min))
  5162. (or (org-on-heading-p) (outline-next-heading))
  5163. (setq start (point) end (point-max) what "top-level")
  5164. (goto-char start)
  5165. (show-all)))
  5166. (setq beg (point))
  5167. (if (>= (point) end) (error "Nothing to sort"))
  5168. (looking-at "\\(\\*+\\)")
  5169. (setq stars (match-string 1)
  5170. re (concat "^" (regexp-quote stars) " +")
  5171. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5172. txt (buffer-substring beg end))
  5173. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5174. (if (and (not (equal stars "*")) (string-match re2 txt))
  5175. (error "Region to sort contains a level above the first entry"))
  5176. ;; Make a list that can be sorted.
  5177. ;; The car is the string for comparison, the cdr is the subtree
  5178. (message "Sorting entries...")
  5179. (setq entries
  5180. (mapcar
  5181. (lambda (x)
  5182. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  5183. (cons (match-string 0 x) x))
  5184. (org-split-string txt re)))
  5185. ;; Sort the list
  5186. (save-excursion
  5187. (goto-char start)
  5188. (setq entries (org-do-sort entries what with-case sorting-type)))
  5189. ;; Delete the old stuff
  5190. (goto-char beg)
  5191. (kill-region beg end)
  5192. (setq nentries (length entries))
  5193. ;; Insert the sorted entries, and remove duplicates if this is required
  5194. (while (setq p (pop entries))
  5195. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  5196. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  5197. (insert stars " " (cdr p))))
  5198. (goto-char start)
  5199. (message "Sorting entries...done (%d entries%s)"
  5200. nentries
  5201. (if unique (format ", %d duplicates removed" nremoved) ""))))
  5202. (defvar org-priority-regexp) ; defined later in the file
  5203. (defun org-do-sort (table what &optional with-case sorting-type)
  5204. "Sort TABLE of WHAT according to SORTING-TYPE.
  5205. The user will be prompted for the SORTING-TYPE if the call to this
  5206. function does not specify it. WHAT is only for the prompt, to indicate
  5207. what is being sorted. The sorting key will be extracted from
  5208. the car of the elements of the table.
  5209. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5210. (unless sorting-type
  5211. (message
  5212. "Sort %s: [a]lphabetic. [n]umeric. [t]ime [p]riority. A/N/T/P means reversed:"
  5213. what)
  5214. (setq sorting-type (read-char-exclusive)))
  5215. (let ((dcst (downcase sorting-type))
  5216. extractfun comparefun)
  5217. ;; Define the appropriate functions
  5218. (cond
  5219. ((= dcst ?n)
  5220. (setq extractfun 'string-to-number
  5221. comparefun (if (= dcst sorting-type) '< '>)))
  5222. ((= dcst ?a)
  5223. (setq extractfun (if with-case 'identity 'downcase)
  5224. comparefun (if (= dcst sorting-type)
  5225. 'string<
  5226. (lambda (a b) (and (not (string< a b))
  5227. (not (string= a b)))))))
  5228. ((= dcst ?t)
  5229. (setq extractfun
  5230. (lambda (x)
  5231. (if (string-match org-ts-regexp x)
  5232. (time-to-seconds
  5233. (org-time-string-to-time (match-string 0 x)))
  5234. 0))
  5235. comparefun (if (= dcst sorting-type) '< '>)))
  5236. ((= dcst ?p)
  5237. (setq extractfun
  5238. (lambda (x)
  5239. (if (string-match org-priority-regexp x)
  5240. (string-to-char (match-string 2 x))
  5241. org-default-priority))
  5242. comparefun (if (= dcst sorting-type) '< '>)))
  5243. (t (error "Invalid sorting type `%c'" sorting-type)))
  5244. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5245. table)
  5246. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5247. ;;;; Plain list items, including checkboxes
  5248. ;;; Plain list items
  5249. (defun org-at-item-p ()
  5250. "Is point in a line starting a hand-formatted item?"
  5251. (let ((llt org-plain-list-ordered-item-terminator))
  5252. (save-excursion
  5253. (goto-char (point-at-bol))
  5254. (looking-at
  5255. (cond
  5256. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5257. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5258. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5259. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5260. (defun org-in-item-p ()
  5261. "It the cursor inside a plain list item.
  5262. Does not have to be the first line."
  5263. (save-excursion
  5264. (condition-case nil
  5265. (progn
  5266. (org-beginning-of-item)
  5267. (org-at-item-p)
  5268. t)
  5269. (error nil))))
  5270. (defun org-insert-item (&optional checkbox)
  5271. "Insert a new item at the current level.
  5272. Return t when things worked, nil when we are not in an item."
  5273. (when (save-excursion
  5274. (condition-case nil
  5275. (progn
  5276. (org-beginning-of-item)
  5277. (org-at-item-p)
  5278. (if (org-invisible-p) (error "Invisible item"))
  5279. t)
  5280. (error nil)))
  5281. (let* ((bul (match-string 0))
  5282. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5283. (match-end 0)))
  5284. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5285. pos)
  5286. (cond
  5287. ((and (org-at-item-p) (<= (point) eow))
  5288. ;; before the bullet
  5289. (beginning-of-line 1)
  5290. (open-line (if blank 2 1)))
  5291. ((<= (point) eow)
  5292. (beginning-of-line 1))
  5293. (t (newline (if blank 2 1))))
  5294. (insert bul (if checkbox "[ ]" ""))
  5295. (just-one-space)
  5296. (setq pos (point))
  5297. (end-of-line 1)
  5298. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5299. (org-maybe-renumber-ordered-list)
  5300. (and checkbox (org-update-checkbox-count-maybe))
  5301. t))
  5302. ;;; Checkboxes
  5303. (defun org-at-item-checkbox-p ()
  5304. "Is point at a line starting a plain-list item with a checklet?"
  5305. (and (org-at-item-p)
  5306. (save-excursion
  5307. (goto-char (match-end 0))
  5308. (skip-chars-forward " \t")
  5309. (looking-at "\\[[- X]\\]"))))
  5310. (defun org-toggle-checkbox (&optional arg)
  5311. "Toggle the checkbox in the current line."
  5312. (interactive "P")
  5313. (catch 'exit
  5314. (let (beg end status (firstnew 'unknown))
  5315. (cond
  5316. ((org-region-active-p)
  5317. (setq beg (region-beginning) end (region-end)))
  5318. ((org-on-heading-p)
  5319. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5320. ((org-at-item-checkbox-p)
  5321. (save-excursion
  5322. (replace-match
  5323. (cond (arg "[-]")
  5324. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5325. (t "[ ]"))
  5326. t t))
  5327. (throw 'exit t))
  5328. (t (error "Not at a checkbox or heading, and no active region")))
  5329. (save-excursion
  5330. (goto-char beg)
  5331. (while (< (point) end)
  5332. (when (org-at-item-checkbox-p)
  5333. (setq status (equal (match-string 0) "[X]"))
  5334. (when (eq firstnew 'unknown)
  5335. (setq firstnew (not status)))
  5336. (replace-match
  5337. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5338. (beginning-of-line 2)))))
  5339. (org-update-checkbox-count-maybe))
  5340. (defun org-update-checkbox-count-maybe ()
  5341. "Update checkbox statistics unless turned off by user."
  5342. (when org-provide-checkbox-statistics
  5343. (org-update-checkbox-count)))
  5344. (defun org-update-checkbox-count (&optional all)
  5345. "Update the checkbox statistics in the current section.
  5346. This will find all statistic cookies like [57%] and [6/12] and update them
  5347. with the current numbers. With optional prefix argument ALL, do this for
  5348. the whole buffer."
  5349. (interactive "P")
  5350. (save-excursion
  5351. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5352. (beg (condition-case nil
  5353. (progn (outline-back-to-heading) (point))
  5354. (error (point-min))))
  5355. (end (move-marker (make-marker)
  5356. (progn (outline-next-heading) (point))))
  5357. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5358. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5359. b1 e1 f1 c-on c-off lim (cstat 0))
  5360. (when all
  5361. (goto-char (point-min))
  5362. (outline-next-heading)
  5363. (setq beg (point) end (point-max)))
  5364. (goto-char beg)
  5365. (while (re-search-forward re end t)
  5366. (setq cstat (1+ cstat)
  5367. b1 (match-beginning 0)
  5368. e1 (match-end 0)
  5369. f1 (match-beginning 1)
  5370. lim (cond
  5371. ((org-on-heading-p) (outline-next-heading) (point))
  5372. ((org-at-item-p) (org-end-of-item) (point))
  5373. (t nil))
  5374. c-on 0 c-off 0)
  5375. (goto-char e1)
  5376. (when lim
  5377. (while (re-search-forward re-box lim t)
  5378. (if (member (match-string 2) '("[ ]" "[-]"))
  5379. (setq c-off (1+ c-off))
  5380. (setq c-on (1+ c-on))))
  5381. ; (delete-region b1 e1)
  5382. (goto-char b1)
  5383. (insert (if f1
  5384. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5385. (format "[%d/%d]" c-on (+ c-on c-off))))
  5386. (and (looking-at "\\[.*?\\]")
  5387. (replace-match ""))))
  5388. (when (interactive-p)
  5389. (message "Checkbox satistics updated %s (%d places)"
  5390. (if all "in entire file" "in current outline entry") cstat)))))
  5391. (defun org-get-checkbox-statistics-face ()
  5392. "Select the face for checkbox statistics.
  5393. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5394. it will be `org-todo'."
  5395. (if (match-end 1)
  5396. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5397. (if (and (> (match-end 2) (match-beginning 2))
  5398. (equal (match-string 2) (match-string 3)))
  5399. 'org-done
  5400. 'org-todo)))
  5401. (defun org-get-indentation (&optional line)
  5402. "Get the indentation of the current line, interpreting tabs.
  5403. When LINE is given, assume it represents a line and compute its indentation."
  5404. (if line
  5405. (if (string-match "^ *" (org-remove-tabs line))
  5406. (match-end 0))
  5407. (save-excursion
  5408. (beginning-of-line 1)
  5409. (skip-chars-forward " \t")
  5410. (current-column))))
  5411. (defun org-remove-tabs (s &optional width)
  5412. "Replace tabulators in S with spaces.
  5413. Assumes that s is a single line, starting in column 0."
  5414. (setq width (or width tab-width))
  5415. (while (string-match "\t" s)
  5416. (setq s (replace-match
  5417. (make-string
  5418. (- (* width (/ (+ (match-beginning 0) width) width))
  5419. (match-beginning 0)) ?\ )
  5420. t t s)))
  5421. s)
  5422. (defun org-fix-indentation (line ind)
  5423. "Fix indentation in LINE.
  5424. IND is a cons cell with target and minimum indentation.
  5425. If the current indenation in LINE is smaller than the minimum,
  5426. leave it alone. If it is larger than ind, set it to the target."
  5427. (let* ((l (org-remove-tabs line))
  5428. (i (org-get-indentation l))
  5429. (i1 (car ind)) (i2 (cdr ind)))
  5430. (if (>= i i2) (setq l (substring line i2)))
  5431. (if (> i1 0)
  5432. (concat (make-string i1 ?\ ) l)
  5433. l)))
  5434. (defcustom org-empty-line-terminates-plain-lists nil
  5435. "Non-nil means, an empty line ends all plain list levels.
  5436. When nil, empty lines are part of the preceeding item."
  5437. :group 'org-plain-lists
  5438. :type 'boolean)
  5439. (defun org-beginning-of-item ()
  5440. "Go to the beginning of the current hand-formatted item.
  5441. If the cursor is not in an item, throw an error."
  5442. (interactive)
  5443. (let ((pos (point))
  5444. (limit (save-excursion
  5445. (condition-case nil
  5446. (progn
  5447. (org-back-to-heading)
  5448. (beginning-of-line 2) (point))
  5449. (error (point-min)))))
  5450. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5451. ind ind1)
  5452. (if (org-at-item-p)
  5453. (beginning-of-line 1)
  5454. (beginning-of-line 1)
  5455. (skip-chars-forward " \t")
  5456. (setq ind (current-column))
  5457. (if (catch 'exit
  5458. (while t
  5459. (beginning-of-line 0)
  5460. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5461. (if (looking-at "[ \t]*$")
  5462. (setq ind1 ind-empty)
  5463. (skip-chars-forward " \t")
  5464. (setq ind1 (current-column)))
  5465. (if (< ind1 ind)
  5466. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5467. nil
  5468. (goto-char pos)
  5469. (error "Not in an item")))))
  5470. (defun org-end-of-item ()
  5471. "Go to the end of the current hand-formatted item.
  5472. If the cursor is not in an item, throw an error."
  5473. (interactive)
  5474. (let* ((pos (point))
  5475. ind1
  5476. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5477. (limit (save-excursion (outline-next-heading) (point)))
  5478. (ind (save-excursion
  5479. (org-beginning-of-item)
  5480. (skip-chars-forward " \t")
  5481. (current-column)))
  5482. (end (catch 'exit
  5483. (while t
  5484. (beginning-of-line 2)
  5485. (if (eobp) (throw 'exit (point)))
  5486. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5487. (if (looking-at "[ \t]*$")
  5488. (setq ind1 ind-empty)
  5489. (skip-chars-forward " \t")
  5490. (setq ind1 (current-column)))
  5491. (if (<= ind1 ind)
  5492. (throw 'exit (point-at-bol)))))))
  5493. (if end
  5494. (goto-char end)
  5495. (goto-char pos)
  5496. (error "Not in an item"))))
  5497. (defun org-next-item ()
  5498. "Move to the beginning of the next item in the current plain list.
  5499. Error if not at a plain list, or if this is the last item in the list."
  5500. (interactive)
  5501. (let (ind ind1 (pos (point)))
  5502. (org-beginning-of-item)
  5503. (setq ind (org-get-indentation))
  5504. (org-end-of-item)
  5505. (setq ind1 (org-get-indentation))
  5506. (unless (and (org-at-item-p) (= ind ind1))
  5507. (goto-char pos)
  5508. (error "On last item"))))
  5509. (defun org-previous-item ()
  5510. "Move to the beginning of the previous item in the current plain list.
  5511. Error if not at a plain list, or if this is the first item in the list."
  5512. (interactive)
  5513. (let (beg ind ind1 (pos (point)))
  5514. (org-beginning-of-item)
  5515. (setq beg (point))
  5516. (setq ind (org-get-indentation))
  5517. (goto-char beg)
  5518. (catch 'exit
  5519. (while t
  5520. (beginning-of-line 0)
  5521. (if (looking-at "[ \t]*$")
  5522. nil
  5523. (if (<= (setq ind1 (org-get-indentation)) ind)
  5524. (throw 'exit t)))))
  5525. (condition-case nil
  5526. (if (or (not (org-at-item-p))
  5527. (< ind1 (1- ind)))
  5528. (error "")
  5529. (org-beginning-of-item))
  5530. (error (goto-char pos)
  5531. (error "On first item")))))
  5532. (defun org-move-item-down ()
  5533. "Move the plain list item at point down, i.e. swap with following item.
  5534. Subitems (items with larger indentation) are considered part of the item,
  5535. so this really moves item trees."
  5536. (interactive)
  5537. (let (beg end ind ind1 (pos (point)) txt)
  5538. (org-beginning-of-item)
  5539. (setq beg (point))
  5540. (setq ind (org-get-indentation))
  5541. (org-end-of-item)
  5542. (setq end (point))
  5543. (setq ind1 (org-get-indentation))
  5544. (if (and (org-at-item-p) (= ind ind1))
  5545. (progn
  5546. (org-end-of-item)
  5547. (setq txt (buffer-substring beg end))
  5548. (save-excursion
  5549. (delete-region beg end))
  5550. (setq pos (point))
  5551. (insert txt)
  5552. (goto-char pos)
  5553. (org-maybe-renumber-ordered-list))
  5554. (goto-char pos)
  5555. (error "Cannot move this item further down"))))
  5556. (defun org-move-item-up (arg)
  5557. "Move the plain list item at point up, i.e. swap with previous item.
  5558. Subitems (items with larger indentation) are considered part of the item,
  5559. so this really moves item trees."
  5560. (interactive "p")
  5561. (let (beg end ind ind1 (pos (point)) txt)
  5562. (org-beginning-of-item)
  5563. (setq beg (point))
  5564. (setq ind (org-get-indentation))
  5565. (org-end-of-item)
  5566. (setq end (point))
  5567. (goto-char beg)
  5568. (catch 'exit
  5569. (while t
  5570. (beginning-of-line 0)
  5571. (if (looking-at "[ \t]*$")
  5572. (if org-empty-line-terminates-plain-lists
  5573. (progn
  5574. (goto-char pos)
  5575. (error "Cannot move this item further up"))
  5576. nil)
  5577. (if (<= (setq ind1 (org-get-indentation)) ind)
  5578. (throw 'exit t)))))
  5579. (condition-case nil
  5580. (org-beginning-of-item)
  5581. (error (goto-char beg)
  5582. (error "Cannot move this item further up")))
  5583. (setq ind1 (org-get-indentation))
  5584. (if (and (org-at-item-p) (= ind ind1))
  5585. (progn
  5586. (setq txt (buffer-substring beg end))
  5587. (save-excursion
  5588. (delete-region beg end))
  5589. (setq pos (point))
  5590. (insert txt)
  5591. (goto-char pos)
  5592. (org-maybe-renumber-ordered-list))
  5593. (goto-char pos)
  5594. (error "Cannot move this item further up"))))
  5595. (defun org-maybe-renumber-ordered-list ()
  5596. "Renumber the ordered list at point if setup allows it.
  5597. This tests the user option `org-auto-renumber-ordered-lists' before
  5598. doing the renumbering."
  5599. (interactive)
  5600. (when (and org-auto-renumber-ordered-lists
  5601. (org-at-item-p))
  5602. (if (match-beginning 3)
  5603. (org-renumber-ordered-list 1)
  5604. (org-fix-bullet-type 1))))
  5605. (defun org-maybe-renumber-ordered-list-safe ()
  5606. (condition-case nil
  5607. (save-excursion
  5608. (org-maybe-renumber-ordered-list))
  5609. (error nil)))
  5610. (defun org-cycle-list-bullet (&optional which)
  5611. "Cycle through the different itemize/enumerate bullets.
  5612. This cycle the entire list level through the sequence:
  5613. `-' -> `+' -> `*' -> `1.' -> `1)'
  5614. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5615. 0 meand `-', 1 means `+' etc."
  5616. (interactive "P")
  5617. (org-preserve-lc
  5618. (org-beginning-of-item-list)
  5619. (org-at-item-p)
  5620. (beginning-of-line 1)
  5621. (let ((current (match-string 0)) new)
  5622. (setq new (cond
  5623. ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5624. ((string-match "-" current) "+")
  5625. ((string-match "\\+" current)
  5626. (if (looking-at "\\S-") "1." "*"))
  5627. ((string-match "\\*" current) "1.")
  5628. ((string-match "\\." current) "1)")
  5629. ((string-match ")" current) "-")
  5630. (t (error "This should not happen"))))
  5631. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5632. (org-fix-bullet-type 1)
  5633. (org-maybe-renumber-ordered-list))))
  5634. (defun org-get-string-indentation (s)
  5635. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5636. (let ((n -1) (i 0) (w tab-width) c)
  5637. (catch 'exit
  5638. (while (< (setq n (1+ n)) (length s))
  5639. (setq c (aref s n))
  5640. (cond ((= c ?\ ) (setq i (1+ i)))
  5641. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5642. (t (throw 'exit t)))))
  5643. i))
  5644. (defun org-renumber-ordered-list (arg)
  5645. "Renumber an ordered plain list.
  5646. Cursor needs to be in the first line of an item, the line that starts
  5647. with something like \"1.\" or \"2)\"."
  5648. (interactive "p")
  5649. (unless (and (org-at-item-p)
  5650. (match-beginning 3))
  5651. (error "This is not an ordered list"))
  5652. (let ((line (org-current-line))
  5653. (col (current-column))
  5654. (ind (org-get-string-indentation
  5655. (buffer-substring (point-at-bol) (match-beginning 3))))
  5656. ;; (term (substring (match-string 3) -1))
  5657. ind1 (n (1- arg))
  5658. fmt)
  5659. ;; find where this list begins
  5660. (org-beginning-of-item-list)
  5661. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5662. (setq fmt (concat "%d" (match-string 1)))
  5663. (beginning-of-line 0)
  5664. ;; walk forward and replace these numbers
  5665. (catch 'exit
  5666. (while t
  5667. (catch 'next
  5668. (beginning-of-line 2)
  5669. (if (eobp) (throw 'exit nil))
  5670. (if (looking-at "[ \t]*$") (throw 'next nil))
  5671. (skip-chars-forward " \t") (setq ind1 (current-column))
  5672. (if (> ind1 ind) (throw 'next t))
  5673. (if (< ind1 ind) (throw 'exit t))
  5674. (if (not (org-at-item-p)) (throw 'exit nil))
  5675. (delete-region (match-beginning 2) (match-end 2))
  5676. (goto-char (match-beginning 2))
  5677. (insert (format fmt (setq n (1+ n)))))))
  5678. (goto-line line)
  5679. (move-to-column col)))
  5680. (defun org-fix-bullet-type (arg)
  5681. "Make sure all items in this list have the same bullet."
  5682. (interactive "p")
  5683. (unless (org-at-item-p) (error "This is not a list"))
  5684. (let ((line (org-current-line))
  5685. (col (current-column))
  5686. (ind (current-indentation))
  5687. ind1 bullet)
  5688. ;; find where this list begins
  5689. (org-beginning-of-item-list)
  5690. (beginning-of-line 1)
  5691. ;; find out what the bullet type is
  5692. (looking-at "[ \t]*\\(\\S-+\\)")
  5693. (setq bullet (match-string 1))
  5694. ;; walk forward and replace these numbers
  5695. (beginning-of-line 0)
  5696. (catch 'exit
  5697. (while t
  5698. (catch 'next
  5699. (beginning-of-line 2)
  5700. (if (eobp) (throw 'exit nil))
  5701. (if (looking-at "[ \t]*$") (throw 'next nil))
  5702. (skip-chars-forward " \t") (setq ind1 (current-column))
  5703. (if (> ind1 ind) (throw 'next t))
  5704. (if (< ind1 ind) (throw 'exit t))
  5705. (if (not (org-at-item-p)) (throw 'exit nil))
  5706. (skip-chars-forward " \t")
  5707. (looking-at "\\S-+")
  5708. (replace-match bullet))))
  5709. (goto-line line)
  5710. (move-to-column col)
  5711. (if (string-match "[0-9]" bullet)
  5712. (org-renumber-ordered-list 1))))
  5713. (defun org-beginning-of-item-list ()
  5714. "Go to the beginning of the current item list.
  5715. I.e. to the first item in this list."
  5716. (interactive)
  5717. (org-beginning-of-item)
  5718. (let ((pos (point-at-bol))
  5719. (ind (org-get-indentation))
  5720. ind1)
  5721. ;; find where this list begins
  5722. (catch 'exit
  5723. (while t
  5724. (catch 'next
  5725. (beginning-of-line 0)
  5726. (if (looking-at "[ \t]*$")
  5727. (throw (if (bobp) 'exit 'next) t))
  5728. (skip-chars-forward " \t") (setq ind1 (current-column))
  5729. (if (or (< ind1 ind)
  5730. (and (= ind1 ind)
  5731. (not (org-at-item-p)))
  5732. (bobp))
  5733. (throw 'exit t)
  5734. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5735. (goto-char pos)))
  5736. (defvar org-last-indent-begin-marker (make-marker))
  5737. (defvar org-last-indent-end-marker (make-marker))
  5738. (defun org-outdent-item (arg)
  5739. "Outdent a local list item."
  5740. (interactive "p")
  5741. (org-indent-item (- arg)))
  5742. (defun org-indent-item (arg)
  5743. "Indent a local list item."
  5744. (interactive "p")
  5745. (unless (org-at-item-p)
  5746. (error "Not on an item"))
  5747. (save-excursion
  5748. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5749. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5750. (setq beg org-last-indent-begin-marker
  5751. end org-last-indent-end-marker)
  5752. (org-beginning-of-item)
  5753. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5754. (org-end-of-item)
  5755. (setq end (move-marker org-last-indent-end-marker (point))))
  5756. (goto-char beg)
  5757. (setq tmp (org-item-indent-positions)
  5758. ind (car tmp)
  5759. ind-down (nth 2 tmp)
  5760. ind-up (nth 1 tmp)
  5761. delta (if (> arg 0)
  5762. (if ind-down (- ind-down ind) 2)
  5763. (if ind-up (- ind-up ind) -2)))
  5764. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5765. (while (< (point) end)
  5766. (beginning-of-line 1)
  5767. (skip-chars-forward " \t") (setq ind1 (current-column))
  5768. (delete-region (point-at-bol) (point))
  5769. (or (eolp) (indent-to-column (+ ind1 delta)))
  5770. (beginning-of-line 2))))
  5771. (org-maybe-renumber-ordered-list-safe)
  5772. (save-excursion
  5773. (beginning-of-line 0)
  5774. (condition-case nil (org-beginning-of-item) (error nil))
  5775. (org-maybe-renumber-ordered-list-safe)))
  5776. (defun org-item-indent-positions ()
  5777. "Assumes cursor in item line. FIXME"
  5778. (let* ((bolpos (point-at-bol))
  5779. (ind (org-get-indentation))
  5780. ind-down ind-up pos)
  5781. (save-excursion
  5782. (org-beginning-of-item-list)
  5783. (skip-chars-backward "\n\r \t")
  5784. (when (org-in-item-p)
  5785. (org-beginning-of-item)
  5786. (setq ind-up (org-get-indentation))))
  5787. (setq pos (point))
  5788. (save-excursion
  5789. (cond
  5790. ((and (condition-case nil (progn (org-previous-item) t)
  5791. (error nil))
  5792. (or (forward-char 1) t)
  5793. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5794. (setq ind-down (org-get-indentation)))
  5795. ((and (goto-char pos)
  5796. (org-at-item-p))
  5797. (goto-char (match-end 0))
  5798. (skip-chars-forward " \t")
  5799. (setq ind-down (current-column)))))
  5800. (list ind ind-up ind-down)))
  5801. ;;; The orgstruct minor mode
  5802. ;; Define a minor mode which can be used in other modes in order to
  5803. ;; integrate the org-mode structure editing commands.
  5804. ;; This is really a hack, because the org-mode structure commands use
  5805. ;; keys which normally belong to the major mode. Here is how it
  5806. ;; works: The minor mode defines all the keys necessary to operate the
  5807. ;; structure commands, but wraps the commands into a function which
  5808. ;; tests if the cursor is currently at a headline or a plain list
  5809. ;; item. If that is the case, the structure command is used,
  5810. ;; temporarily setting many Org-mode variables like regular
  5811. ;; expressions for filling etc. However, when any of those keys is
  5812. ;; used at a different location, function uses `key-binding' to look
  5813. ;; up if the key has an associated command in another currently active
  5814. ;; keymap (minor modes, major mode, global), and executes that
  5815. ;; command. There might be problems if any of the keys is otherwise
  5816. ;; used as a prefix key.
  5817. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5818. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5819. ;; addresses this by checking explicitly for both bindings.
  5820. (defvar orgstruct-mode-map (make-sparse-keymap)
  5821. "Keymap for the minor `orgstruct-mode'.")
  5822. ;;;###autoload
  5823. (define-minor-mode orgstruct-mode
  5824. "Toggle the minor more `orgstruct-mode'.
  5825. This mode is for using Org-mode structure commands in other modes.
  5826. The following key behave as if Org-mode was active, if the cursor
  5827. is on a headline, or on a plain list item (both in the definition
  5828. of Org-mode).
  5829. M-up Move entry/item up
  5830. M-down Move entry/item down
  5831. M-left Promote
  5832. M-right Demote
  5833. M-S-up Move entry/item up
  5834. M-S-down Move entry/item down
  5835. M-S-left Promote subtree
  5836. M-S-right Demote subtree
  5837. M-q Fill paragraph and items like in Org-mode
  5838. C-c ^ Sort entries
  5839. C-c - Cycle list bullet
  5840. TAB Cycle item visibility
  5841. M-RET Insert new heading/item
  5842. S-M-RET Insert new TODO heading / Chekbox item
  5843. C-c C-c Set tags / toggle checkbox"
  5844. nil " OrgStruct" nil
  5845. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5846. ;;;###autoload
  5847. (defun turn-on-orgstruct ()
  5848. "Unconditionally turn on `orgstruct-mode'."
  5849. (orgstruct-mode 1))
  5850. (defun orgstruct-error ()
  5851. "Error when there is no default binding for a structure key."
  5852. (interactive)
  5853. (error "This key is has no function outside structure elements"))
  5854. (defvar org-local-vars nil
  5855. "List of local variables, for use by `orgstruct-mode'")
  5856. (defun orgstruct-setup ()
  5857. "Setup orgstruct keymaps."
  5858. (let ((nfunc 0)
  5859. (bindings
  5860. (list
  5861. '([(meta up)] org-metaup)
  5862. '([(meta down)] org-metadown)
  5863. '([(meta left)] org-metaleft)
  5864. '([(meta right)] org-metaright)
  5865. '([(meta shift up)] org-shiftmetaup)
  5866. '([(meta shift down)] org-shiftmetadown)
  5867. '([(meta shift left)] org-shiftmetaleft)
  5868. '([(meta shift right)] org-shiftmetaright)
  5869. '([(shift up)] org-shiftup)
  5870. '([(shift down)] org-shiftdown)
  5871. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5872. '("\M-q" fill-paragraph)
  5873. '("\C-c^" org-sort)
  5874. '("\C-c-" org-cycle-list-bullet)))
  5875. elt key fun cmd)
  5876. (while (setq elt (pop bindings))
  5877. (setq nfunc (1+ nfunc))
  5878. (setq key (org-key (car elt))
  5879. fun (nth 1 elt)
  5880. cmd (orgstruct-make-binding fun nfunc key))
  5881. (org-defkey orgstruct-mode-map key cmd))
  5882. ;; Special treatment needed for TAB and RET
  5883. (org-defkey orgstruct-mode-map [(tab)]
  5884. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5885. (org-defkey orgstruct-mode-map "\C-i"
  5886. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5887. (org-defkey orgstruct-mode-map "\M-\C-m"
  5888. (orgstruct-make-binding 'org-insert-heading 105
  5889. "\M-\C-m" [(meta return)]))
  5890. (org-defkey orgstruct-mode-map [(meta return)]
  5891. (orgstruct-make-binding 'org-insert-heading 106
  5892. [(meta return)] "\M-\C-m"))
  5893. (org-defkey orgstruct-mode-map [(shift meta return)]
  5894. (orgstruct-make-binding 'org-insert-todo-heading 107
  5895. [(meta return)] "\M-\C-m"))
  5896. (unless org-local-vars
  5897. (setq org-local-vars (org-get-local-variables)))
  5898. t))
  5899. (defun orgstruct-make-binding (fun n &rest keys)
  5900. "Create a function for binding in the structure minor mode.
  5901. FUN is the command to call inside a table. N is used to create a unique
  5902. command name. KEYS are keys that should be checked in for a command
  5903. to execute outside of tables."
  5904. (eval
  5905. (list 'defun
  5906. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5907. '(arg)
  5908. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5909. "Outside of structure, run the binding of `"
  5910. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5911. "'.")
  5912. '(interactive "p")
  5913. (list 'if
  5914. '(org-context-p 'headline 'item)
  5915. (list 'org-run-like-in-org-mode (list 'quote fun))
  5916. (list 'let '(orgstruct-mode)
  5917. (list 'call-interactively
  5918. (append '(or)
  5919. (mapcar (lambda (k)
  5920. (list 'key-binding k))
  5921. keys)
  5922. '('orgstruct-error))))))))
  5923. (defun org-context-p (&rest contexts)
  5924. "FIXME:"
  5925. (let ((pos (point)))
  5926. (goto-char (point-at-bol))
  5927. (prog1 (or (and (memq 'table contexts)
  5928. (looking-at "[ \t]*|"))
  5929. (and (memq 'headline contexts)
  5930. (looking-at "\\*+"))
  5931. (and (memq 'item contexts)
  5932. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5933. (goto-char pos))))
  5934. (defun org-get-local-variables ()
  5935. "Return a list of all local variables in an org-mode buffer."
  5936. (let (varlist)
  5937. (with-current-buffer (get-buffer-create "*Org tmp*")
  5938. (erase-buffer)
  5939. (org-mode)
  5940. (setq varlist (buffer-local-variables)))
  5941. (kill-buffer "*Org tmp*")
  5942. (delq nil
  5943. (mapcar
  5944. (lambda (x)
  5945. (setq x
  5946. (if (symbolp x)
  5947. (list x)
  5948. (list (car x) (list 'quote (cdr x)))))
  5949. (if (string-match
  5950. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5951. (symbol-name (car x)))
  5952. x nil))
  5953. varlist))))
  5954. ;;;###autoload
  5955. (defun org-run-like-in-org-mode (cmd)
  5956. (unless org-local-vars
  5957. (setq org-local-vars (org-get-local-variables)))
  5958. (eval (list 'let org-local-vars
  5959. (list 'call-interactively (list 'quote cmd)))))
  5960. ;;;; Archiving
  5961. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5962. (defun org-archive-subtree (&optional find-done)
  5963. "Move the current subtree to the archive.
  5964. The archive can be a certain top-level heading in the current file, or in
  5965. a different file. The tree will be moved to that location, the subtree
  5966. heading be marked DONE, and the current time will be added.
  5967. When called with prefix argument FIND-DONE, find whole trees without any
  5968. open TODO items and archive them (after getting confirmation from the user).
  5969. If the cursor is not at a headline when this comand is called, try all level
  5970. 1 trees. If the cursor is on a headline, only try the direct children of
  5971. this heading."
  5972. (interactive "P")
  5973. (if find-done
  5974. (org-archive-all-done)
  5975. ;; Save all relevant TODO keyword-relatex variables
  5976. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5977. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5978. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5979. (tr-org-done-keywords org-done-keywords)
  5980. (tr-org-todo-regexp org-todo-regexp)
  5981. (tr-org-todo-line-regexp org-todo-line-regexp)
  5982. (tr-org-odd-levels-only org-odd-levels-only)
  5983. (this-buffer (current-buffer))
  5984. (org-archive-location org-archive-location)
  5985. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5986. (file (abbreviate-file-name (buffer-file-name)))
  5987. (time (format-time-string
  5988. (substring (cdr org-time-stamp-formats) 1 -1)
  5989. (current-time)))
  5990. afile heading buffer level newfile-p
  5991. category todo priority ltags itags)
  5992. ;; Try to find a local archive location
  5993. (save-excursion
  5994. (save-restriction
  5995. (widen)
  5996. (if (or (re-search-backward re nil t) (re-search-forward re nil t))
  5997. (setq org-archive-location (match-string 1)))))
  5998. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5999. (progn
  6000. (setq afile (format (match-string 1 org-archive-location)
  6001. (file-name-nondirectory buffer-file-name))
  6002. heading (match-string 2 org-archive-location)))
  6003. (error "Invalid `org-archive-location'"))
  6004. (if (> (length afile) 0)
  6005. (setq newfile-p (not (file-exists-p afile))
  6006. buffer (find-file-noselect afile))
  6007. (setq buffer (current-buffer)))
  6008. (unless buffer
  6009. (error "Cannot access file \"%s\"" afile))
  6010. (if (and (> (length heading) 0)
  6011. (string-match "^\\*+" heading))
  6012. (setq level (match-end 0))
  6013. (setq heading nil level 0))
  6014. (save-excursion
  6015. (org-back-to-heading t)
  6016. ;; Get context information that will be lost by moving the tree
  6017. (setq category (org-get-category)
  6018. todo (and (looking-at org-todo-line-regexp)
  6019. (match-string 2))
  6020. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6021. ltags (org-split-string (org-get-tags) ":")
  6022. itags (org-delete-all ltags (org-get-tags-at)))
  6023. (setq ltags (mapconcat 'identity ltags " ")
  6024. itags (mapconcat 'identity itags " "))
  6025. ;; We first only copy, in case something goes wrong
  6026. ;; we need to protect this-command, to avoid kill-region sets it,
  6027. ;; which would lead to duplication of subtrees
  6028. (let (this-command) (org-copy-subtree))
  6029. (set-buffer buffer)
  6030. ;; Enforce org-mode for the archive buffer
  6031. (if (not (org-mode-p))
  6032. ;; Force the mode for future visits.
  6033. (let ((org-insert-mode-line-in-empty-file t)
  6034. (org-inhibit-startup t))
  6035. (call-interactively 'org-mode)))
  6036. (when newfile-p
  6037. (goto-char (point-max))
  6038. (insert (format "\nArchived entries from file %s\n\n"
  6039. (buffer-file-name this-buffer))))
  6040. ;; Force the TODO keywords of the original buffer
  6041. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6042. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6043. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6044. (org-done-keywords tr-org-done-keywords)
  6045. (org-todo-regexp tr-org-todo-regexp)
  6046. (org-todo-line-regexp tr-org-todo-line-regexp)
  6047. (org-odd-levels-only
  6048. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6049. org-odd-levels-only
  6050. tr-org-odd-levels-only)))
  6051. (goto-char (point-min))
  6052. (if heading
  6053. (progn
  6054. (if (re-search-forward
  6055. (concat "^" (regexp-quote heading)
  6056. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6057. nil t)
  6058. (goto-char (match-end 0))
  6059. ;; Heading not found, just insert it at the end
  6060. (goto-char (point-max))
  6061. (or (bolp) (insert "\n"))
  6062. (insert "\n" heading "\n")
  6063. (end-of-line 0))
  6064. ;; Make the subtree visible
  6065. (show-subtree)
  6066. (org-end-of-subtree t)
  6067. (skip-chars-backward " \t\r\n")
  6068. (and (looking-at "[ \t\r\n]*")
  6069. (replace-match "\n\n")))
  6070. ;; No specific heading, just go to end of file.
  6071. (goto-char (point-max)) (insert "\n"))
  6072. ;; Paste
  6073. (org-paste-subtree (org-get-legal-level level 1))
  6074. ;; Mark the entry as done
  6075. (when (and org-archive-mark-done
  6076. (looking-at org-todo-line-regexp)
  6077. (or (not (match-end 2))
  6078. (not (member (match-string 2) org-done-keywords))))
  6079. (let (org-log-done)
  6080. (org-todo
  6081. (car (or (member org-archive-mark-done org-done-keywords)
  6082. org-done-keywords)))))
  6083. ;; Add the context info
  6084. (when org-archive-save-context-info
  6085. (let ((l org-archive-save-context-info) e n v)
  6086. (while (setq e (pop l))
  6087. (when (and (setq v (symbol-value e))
  6088. (stringp v) (string-match "\\S-" v))
  6089. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6090. (org-entry-put (point) n v)))))
  6091. ;; Save the buffer, if it is not the same buffer.
  6092. (if (not (eq this-buffer buffer)) (save-buffer))))
  6093. ;; Here we are back in the original buffer. Everything seems to have
  6094. ;; worked. So now cut the tree and finish up.
  6095. (let (this-command) (org-cut-subtree))
  6096. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  6097. (message "Subtree archived %s"
  6098. (if (eq this-buffer buffer)
  6099. (concat "under heading: " heading)
  6100. (concat "in file: " (abbreviate-file-name afile)))))))
  6101. (defun org-archive-all-done (&optional tag)
  6102. "Archive sublevels of the current tree without open TODO items.
  6103. If the cursor is not on a headline, try all level 1 trees. If
  6104. it is on a headline, try all direct children.
  6105. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  6106. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  6107. (rea (concat ".*:" org-archive-tag ":"))
  6108. (begm (make-marker))
  6109. (endm (make-marker))
  6110. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  6111. "Move subtree to archive (no open TODO items)? "))
  6112. beg end (cntarch 0))
  6113. (if (org-on-heading-p)
  6114. (progn
  6115. (setq re1 (concat "^" (regexp-quote
  6116. (make-string
  6117. (1+ (- (match-end 0) (match-beginning 0)))
  6118. ?*))
  6119. " "))
  6120. (move-marker begm (point))
  6121. (move-marker endm (org-end-of-subtree t)))
  6122. (setq re1 "^* ")
  6123. (move-marker begm (point-min))
  6124. (move-marker endm (point-max)))
  6125. (save-excursion
  6126. (goto-char begm)
  6127. (while (re-search-forward re1 endm t)
  6128. (setq beg (match-beginning 0)
  6129. end (save-excursion (org-end-of-subtree t) (point)))
  6130. (goto-char beg)
  6131. (if (re-search-forward re end t)
  6132. (goto-char end)
  6133. (goto-char beg)
  6134. (if (and (or (not tag) (not (looking-at rea)))
  6135. (y-or-n-p question))
  6136. (progn
  6137. (if tag
  6138. (org-toggle-tag org-archive-tag 'on)
  6139. (org-archive-subtree))
  6140. (setq cntarch (1+ cntarch)))
  6141. (goto-char end)))))
  6142. (message "%d trees archived" cntarch)))
  6143. (defun org-cycle-hide-drawers (state)
  6144. "Re-hide all drawers after a visibility state change."
  6145. (when (and (org-mode-p)
  6146. (not (memq state '(overview folded))))
  6147. (save-excursion
  6148. (let* ((globalp (memq state '(contents all)))
  6149. (beg (if globalp (point-min) (point)))
  6150. (end (if globalp (point-max) (org-end-of-subtree t))))
  6151. (goto-char beg)
  6152. (while (re-search-forward org-drawer-regexp end t)
  6153. (org-flag-drawer t))))))
  6154. (defun org-flag-drawer (flag)
  6155. (save-excursion
  6156. (beginning-of-line 1)
  6157. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6158. (let ((b (match-end 0)))
  6159. (if (re-search-forward
  6160. "^[ \t]*:END:"
  6161. (save-excursion (outline-next-heading) (point)) t)
  6162. (outline-flag-region b (point-at-eol) flag)
  6163. (error ":END: line missing"))))))
  6164. (defun org-cycle-hide-archived-subtrees (state)
  6165. "Re-hide all archived subtrees after a visibility state change."
  6166. (when (and (not org-cycle-open-archived-trees)
  6167. (not (memq state '(overview folded))))
  6168. (save-excursion
  6169. (let* ((globalp (memq state '(contents all)))
  6170. (beg (if globalp (point-min) (point)))
  6171. (end (if globalp (point-max) (org-end-of-subtree t))))
  6172. (org-hide-archived-subtrees beg end)
  6173. (goto-char beg)
  6174. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6175. (message (substitute-command-keys
  6176. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6177. (defun org-force-cycle-archived ()
  6178. "Cycle subtree even if it is archived."
  6179. (interactive)
  6180. (setq this-command 'org-cycle)
  6181. (let ((org-cycle-open-archived-trees t))
  6182. (call-interactively 'org-cycle)))
  6183. (defun org-hide-archived-subtrees (beg end)
  6184. "Re-hide all archived subtrees after a visibility state change."
  6185. (save-excursion
  6186. (let* ((re (concat ":" org-archive-tag ":")))
  6187. (goto-char beg)
  6188. (while (re-search-forward re end t)
  6189. (and (org-on-heading-p) (hide-subtree))
  6190. (org-end-of-subtree t)))))
  6191. (defun org-toggle-tag (tag &optional onoff)
  6192. "Toggle the tag TAG for the current line.
  6193. If ONOFF is `on' or `off', don't toggle but set to this state."
  6194. (unless (org-on-heading-p t) (error "Not on headling"))
  6195. (let (res current)
  6196. (save-excursion
  6197. (beginning-of-line)
  6198. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6199. (point-at-eol) t)
  6200. (progn
  6201. (setq current (match-string 1))
  6202. (replace-match ""))
  6203. (setq current ""))
  6204. (setq current (nreverse (org-split-string current ":")))
  6205. (cond
  6206. ((eq onoff 'on)
  6207. (setq res t)
  6208. (or (member tag current) (push tag current)))
  6209. ((eq onoff 'off)
  6210. (or (not (member tag current)) (setq current (delete tag current))))
  6211. (t (if (member tag current)
  6212. (setq current (delete tag current))
  6213. (setq res t)
  6214. (push tag current))))
  6215. (end-of-line 1)
  6216. (when current
  6217. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  6218. (org-set-tags nil t))
  6219. res))
  6220. (defun org-toggle-archive-tag (&optional arg)
  6221. "Toggle the archive tag for the current headline.
  6222. With prefix ARG, check all children of current headline and offer tagging
  6223. the children that do not contain any open TODO items."
  6224. (interactive "P")
  6225. (if arg
  6226. (org-archive-all-done 'tag)
  6227. (let (set)
  6228. (save-excursion
  6229. (org-back-to-heading t)
  6230. (setq set (org-toggle-tag org-archive-tag))
  6231. (when set (hide-subtree)))
  6232. (and set (beginning-of-line 1))
  6233. (message "Subtree %s" (if set "archived" "unarchived")))))
  6234. ;;;; Tables
  6235. ;;; The table editor
  6236. ;; Watch out: Here we are talking about two different kind of tables.
  6237. ;; Most of the code is for the tables created with the Org-mode table editor.
  6238. ;; Sometimes, we talk about tables created and edited with the table.el
  6239. ;; Emacs package. We call the former org-type tables, and the latter
  6240. ;; table.el-type tables.
  6241. (defun org-before-change-function (beg end)
  6242. "Every change indicates that a table might need an update."
  6243. (setq org-table-may-need-update t))
  6244. (defconst org-table-line-regexp "^[ \t]*|"
  6245. "Detects an org-type table line.")
  6246. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  6247. "Detects an org-type table line.")
  6248. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  6249. "Detects a table line marked for automatic recalculation.")
  6250. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  6251. "Detects a table line marked for automatic recalculation.")
  6252. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  6253. "Detects a table line marked for automatic recalculation.")
  6254. (defconst org-table-hline-regexp "^[ \t]*|-"
  6255. "Detects an org-type table hline.")
  6256. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  6257. "Detects a table-type table hline.")
  6258. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  6259. "Detects an org-type or table-type table.")
  6260. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  6261. "Searching from within a table (any type) this finds the first line
  6262. outside the table.")
  6263. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  6264. "Searching from within a table (any type) this finds the first line
  6265. outside the table.")
  6266. (defvar org-table-last-highlighted-reference nil)
  6267. (defvar org-table-formula-history nil)
  6268. (defvar org-table-column-names nil
  6269. "Alist with column names, derived from the `!' line.")
  6270. (defvar org-table-column-name-regexp nil
  6271. "Regular expression matching the current column names.")
  6272. (defvar org-table-local-parameters nil
  6273. "Alist with parameter names, derived from the `$' line.")
  6274. (defvar org-table-named-field-locations nil
  6275. "Alist with locations of named fields.")
  6276. (defvar org-table-current-line-types nil
  6277. "Table row types, non-nil only for the duration of a comand.")
  6278. (defvar org-table-current-begin-line nil
  6279. "Table begin line, non-nil only for the duration of a comand.")
  6280. (defvar org-table-current-begin-pos nil
  6281. "Table begin position, non-nil only for the duration of a comand.")
  6282. (defvar org-table-dlines nil
  6283. "Vector of data line line numbers in the current table.")
  6284. (defvar org-table-hlines nil
  6285. "Vector of hline line numbers in the current table.")
  6286. (defconst org-table-range-regexp
  6287. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6288. ;; 1 2 3 4 5
  6289. "Regular expression for matching ranges in formulas.")
  6290. (defconst org-table-range-regexp2
  6291. (concat
  6292. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6293. "\\.\\."
  6294. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6295. "Match a range for reference display.")
  6296. (defconst org-table-translate-regexp
  6297. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6298. "Match a reference that needs translation, for reference display.")
  6299. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6300. (defun org-table-create-with-table.el ()
  6301. "Use the table.el package to insert a new table.
  6302. If there is already a table at point, convert between Org-mode tables
  6303. and table.el tables."
  6304. (interactive)
  6305. (require 'table)
  6306. (cond
  6307. ((org-at-table.el-p)
  6308. (if (y-or-n-p "Convert table to Org-mode table? ")
  6309. (org-table-convert)))
  6310. ((org-at-table-p)
  6311. (if (y-or-n-p "Convert table to table.el table? ")
  6312. (org-table-convert)))
  6313. (t (call-interactively 'table-insert))))
  6314. (defun org-table-create-or-convert-from-region (arg)
  6315. "Convert region to table, or create an empty table.
  6316. If there is an active region, convert it to a table, using the function
  6317. `org-table-convert-region'.
  6318. If there is no such region, create an empty table with `org-table-create'."
  6319. (interactive "P")
  6320. (if (org-region-active-p)
  6321. (org-table-convert-region (region-beginning) (region-end) arg)
  6322. (org-table-create arg)))
  6323. (defun org-table-create (&optional size)
  6324. "Query for a size and insert a table skeleton.
  6325. SIZE is a string Columns x Rows like for example \"3x2\"."
  6326. (interactive "P")
  6327. (unless size
  6328. (setq size (read-string
  6329. (concat "Table size Columns x Rows [e.g. "
  6330. org-table-default-size "]: ")
  6331. "" nil org-table-default-size)))
  6332. (let* ((pos (point))
  6333. (indent (make-string (current-column) ?\ ))
  6334. (split (org-split-string size " *x *"))
  6335. (rows (string-to-number (nth 1 split)))
  6336. (columns (string-to-number (car split)))
  6337. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6338. "\n")))
  6339. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6340. (point-at-bol) (point)))
  6341. (beginning-of-line 1)
  6342. (newline))
  6343. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6344. (dotimes (i rows) (insert line))
  6345. (goto-char pos)
  6346. (if (> rows 1)
  6347. ;; Insert a hline after the first row.
  6348. (progn
  6349. (end-of-line 1)
  6350. (insert "\n|-")
  6351. (goto-char pos)))
  6352. (org-table-align)))
  6353. (defun org-table-convert-region (beg0 end0 &optional nspace)
  6354. "Convert region to a table.
  6355. The region goes from BEG0 to END0, but these borders will be moved
  6356. slightly, to make sure a beginning of line in the first line is included.
  6357. When NSPACE is non-nil, it indicates the minimum number of spaces that
  6358. separate columns. By default, the function first checks if every line
  6359. contains at lease one TAB. If yes, it assumes that the material is TAB
  6360. separated. If not, it assumes a single space as separator."
  6361. (interactive "rP")
  6362. (let* ((beg (min beg0 end0))
  6363. (end (max beg0 end0))
  6364. (tabsep t)
  6365. re)
  6366. (goto-char beg)
  6367. (beginning-of-line 1)
  6368. (setq beg (move-marker (make-marker) (point)))
  6369. (goto-char end)
  6370. (if (bolp) (backward-char 1) (end-of-line 1))
  6371. (setq end (move-marker (make-marker) (point)))
  6372. ;; Lets see if this is tab-separated material. If every nonempty line
  6373. ;; contains a tab, we will assume that it is tab-separated material
  6374. (if nspace
  6375. (setq tabsep nil)
  6376. (goto-char beg)
  6377. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  6378. (if nspace (setq tabsep nil))
  6379. (if tabsep
  6380. (setq re "^\\|\t")
  6381. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  6382. (max 1 (prefix-numeric-value nspace)))))
  6383. (goto-char beg)
  6384. (while (re-search-forward re end t)
  6385. (replace-match "| " t t))
  6386. (goto-char beg)
  6387. (insert " ")
  6388. (org-table-align)))
  6389. (defun org-table-import (file arg)
  6390. "Import FILE as a table.
  6391. The file is assumed to be tab-separated. Such files can be produced by most
  6392. spreadsheet and database applications. If no tabs (at least one per line)
  6393. are found, lines will be split on whitespace into fields."
  6394. (interactive "f\nP")
  6395. (or (bolp) (newline))
  6396. (let ((beg (point))
  6397. (pm (point-max)))
  6398. (insert-file-contents file)
  6399. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6400. (defun org-table-export ()
  6401. "Export table as a tab-separated file.
  6402. Such a file can be imported into a spreadsheet program like Excel."
  6403. (interactive)
  6404. (let* ((beg (org-table-begin))
  6405. (end (org-table-end))
  6406. (table (buffer-substring beg end))
  6407. (file (read-file-name "Export table to: "))
  6408. buf)
  6409. (unless (or (not (file-exists-p file))
  6410. (y-or-n-p (format "Overwrite file %s? " file)))
  6411. (error "Abort"))
  6412. (with-current-buffer (find-file-noselect file)
  6413. (setq buf (current-buffer))
  6414. (erase-buffer)
  6415. (fundamental-mode)
  6416. (insert table)
  6417. (goto-char (point-min))
  6418. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6419. (replace-match "" t t)
  6420. (end-of-line 1))
  6421. (goto-char (point-min))
  6422. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6423. (replace-match "" t t)
  6424. (goto-char (min (1+ (point)) (point-max))))
  6425. (goto-char (point-min))
  6426. (while (re-search-forward "^-[-+]*$" nil t)
  6427. (replace-match "")
  6428. (if (looking-at "\n")
  6429. (delete-char 1)))
  6430. (goto-char (point-min))
  6431. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6432. (replace-match "\t" t t))
  6433. (save-buffer))
  6434. (kill-buffer buf)))
  6435. (defvar org-table-aligned-begin-marker (make-marker)
  6436. "Marker at the beginning of the table last aligned.
  6437. Used to check if cursor still is in that table, to minimize realignment.")
  6438. (defvar org-table-aligned-end-marker (make-marker)
  6439. "Marker at the end of the table last aligned.
  6440. Used to check if cursor still is in that table, to minimize realignment.")
  6441. (defvar org-table-last-alignment nil
  6442. "List of flags for flushright alignment, from the last re-alignment.
  6443. This is being used to correctly align a single field after TAB or RET.")
  6444. (defvar org-table-last-column-widths nil
  6445. "List of max width of fields in each column.
  6446. This is being used to correctly align a single field after TAB or RET.")
  6447. (defvar org-table-overlay-coordinates nil
  6448. "Overlay coordinates after each align of a table.")
  6449. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6450. (defvar org-last-recalc-line nil)
  6451. (defconst org-narrow-column-arrow "=>"
  6452. "Used as display property in narrowed table columns.")
  6453. (defun org-table-align ()
  6454. "Align the table at point by aligning all vertical bars."
  6455. (interactive)
  6456. (let* (
  6457. ;; Limits of table
  6458. (beg (org-table-begin))
  6459. (end (org-table-end))
  6460. ;; Current cursor position
  6461. (linepos (org-current-line))
  6462. (colpos (org-table-current-column))
  6463. (winstart (window-start))
  6464. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6465. lines (new "") lengths l typenums ty fields maxfields i
  6466. column
  6467. (indent "") cnt frac
  6468. rfmt hfmt
  6469. (spaces '(1 . 1))
  6470. (sp1 (car spaces))
  6471. (sp2 (cdr spaces))
  6472. (rfmt1 (concat
  6473. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6474. (hfmt1 (concat
  6475. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6476. emptystrings links dates narrow fmax f1 len c e)
  6477. (untabify beg end)
  6478. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6479. ;; Check if we have links or dates
  6480. (goto-char beg)
  6481. (setq links (re-search-forward org-bracket-link-regexp end t))
  6482. (goto-char beg)
  6483. (setq dates (and org-display-custom-times
  6484. (re-search-forward org-ts-regexp-both end t)))
  6485. ;; Make sure the link properties are right
  6486. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6487. ;; Make sure the date properties are right
  6488. (when dates (goto-char beg) (while (org-activate-dates end)))
  6489. ;; Check if we are narrowing any columns
  6490. (goto-char beg)
  6491. (setq narrow (and org-format-transports-properties-p
  6492. (re-search-forward "<[0-9]+>" end t)))
  6493. ;; Get the rows
  6494. (setq lines (org-split-string
  6495. (buffer-substring beg end) "\n"))
  6496. ;; Store the indentation of the first line
  6497. (if (string-match "^ *" (car lines))
  6498. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6499. ;; Mark the hlines by setting the corresponding element to nil
  6500. ;; At the same time, we remove trailing space.
  6501. (setq lines (mapcar (lambda (l)
  6502. (if (string-match "^ *|-" l)
  6503. nil
  6504. (if (string-match "[ \t]+$" l)
  6505. (substring l 0 (match-beginning 0))
  6506. l)))
  6507. lines))
  6508. ;; Get the data fields by splitting the lines.
  6509. (setq fields (mapcar
  6510. (lambda (l)
  6511. (org-split-string l " *| *"))
  6512. (delq nil (copy-sequence lines))))
  6513. ;; How many fields in the longest line?
  6514. (condition-case nil
  6515. (setq maxfields (apply 'max (mapcar 'length fields)))
  6516. (error
  6517. (kill-region beg end)
  6518. (org-table-create org-table-default-size)
  6519. (error "Empty table - created default table")))
  6520. ;; A list of empty strings to fill any short rows on output
  6521. (setq emptystrings (make-list maxfields ""))
  6522. ;; Check for special formatting.
  6523. (setq i -1)
  6524. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  6525. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  6526. ;; Check if there is an explicit width specified
  6527. (when narrow
  6528. (setq c column fmax nil)
  6529. (while c
  6530. (setq e (pop c))
  6531. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  6532. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  6533. ;; Find fields that are wider than fmax, and shorten them
  6534. (when fmax
  6535. (loop for xx in column do
  6536. (when (and (stringp xx)
  6537. (> (org-string-width xx) fmax))
  6538. (org-add-props xx nil
  6539. 'help-echo
  6540. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  6541. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  6542. (unless (> f1 1)
  6543. (error "Cannot narrow field starting with wide link \"%s\""
  6544. (match-string 0 xx)))
  6545. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  6546. (add-text-properties (- f1 2) f1
  6547. (list 'display org-narrow-column-arrow)
  6548. xx)))))
  6549. ;; Get the maximum width for each column
  6550. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  6551. ;; Get the fraction of numbers, to decide about alignment of the column
  6552. (setq cnt 0 frac 0.0)
  6553. (loop for x in column do
  6554. (if (equal x "")
  6555. nil
  6556. (setq frac ( / (+ (* frac cnt)
  6557. (if (string-match org-table-number-regexp x) 1 0))
  6558. (setq cnt (1+ cnt))))))
  6559. (push (>= frac org-table-number-fraction) typenums))
  6560. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  6561. ;; Store the alignment of this table, for later editing of single fields
  6562. (setq org-table-last-alignment typenums
  6563. org-table-last-column-widths lengths)
  6564. ;; With invisible characters, `format' does not get the field width right
  6565. ;; So we need to make these fields wide by hand.
  6566. (when links
  6567. (loop for i from 0 upto (1- maxfields) do
  6568. (setq len (nth i lengths))
  6569. (loop for j from 0 upto (1- (length fields)) do
  6570. (setq c (nthcdr i (car (nthcdr j fields))))
  6571. (if (and (stringp (car c))
  6572. (string-match org-bracket-link-regexp (car c))
  6573. (< (org-string-width (car c)) len))
  6574. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  6575. ;; Compute the formats needed for output of the table
  6576. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  6577. (while (setq l (pop lengths))
  6578. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  6579. (setq rfmt (concat rfmt (format rfmt1 ty l))
  6580. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  6581. (setq rfmt (concat rfmt "\n")
  6582. hfmt (concat (substring hfmt 0 -1) "|\n"))
  6583. (setq new (mapconcat
  6584. (lambda (l)
  6585. (if l (apply 'format rfmt
  6586. (append (pop fields) emptystrings))
  6587. hfmt))
  6588. lines ""))
  6589. ;; Replace the old one
  6590. (delete-region beg end)
  6591. (move-marker end nil)
  6592. (move-marker org-table-aligned-begin-marker (point))
  6593. (insert new)
  6594. (move-marker org-table-aligned-end-marker (point))
  6595. (when (and orgtbl-mode (not (org-mode-p)))
  6596. (goto-char org-table-aligned-begin-marker)
  6597. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  6598. ;; Try to move to the old location
  6599. (goto-line winstartline)
  6600. (setq winstart (point-at-bol))
  6601. (goto-line linepos)
  6602. (set-window-start (selected-window) winstart 'noforce)
  6603. (org-table-goto-column colpos)
  6604. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  6605. (setq org-table-may-need-update nil)
  6606. ))
  6607. (defun org-string-width (s)
  6608. "Compute width of string, ignoring invisible characters.
  6609. This ignores character with invisibility property `org-link', and also
  6610. characters with property `org-cwidth', because these will become invisible
  6611. upon the next fontification round."
  6612. (let (b l)
  6613. (when (or (eq t buffer-invisibility-spec)
  6614. (assq 'org-link buffer-invisibility-spec))
  6615. (while (setq b (text-property-any 0 (length s)
  6616. 'invisible 'org-link s))
  6617. (setq s (concat (substring s 0 b)
  6618. (substring s (or (next-single-property-change
  6619. b 'invisible s) (length s)))))))
  6620. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6621. (setq s (concat (substring s 0 b)
  6622. (substring s (or (next-single-property-change
  6623. b 'org-cwidth s) (length s))))))
  6624. (setq l (string-width s) b -1)
  6625. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6626. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6627. l))
  6628. (defun org-table-begin (&optional table-type)
  6629. "Find the beginning of the table and return its position.
  6630. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6631. (save-excursion
  6632. (if (not (re-search-backward
  6633. (if table-type org-table-any-border-regexp
  6634. org-table-border-regexp)
  6635. nil t))
  6636. (progn (goto-char (point-min)) (point))
  6637. (goto-char (match-beginning 0))
  6638. (beginning-of-line 2)
  6639. (point))))
  6640. (defun org-table-end (&optional table-type)
  6641. "Find the end of the table and return its position.
  6642. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6643. (save-excursion
  6644. (if (not (re-search-forward
  6645. (if table-type org-table-any-border-regexp
  6646. org-table-border-regexp)
  6647. nil t))
  6648. (goto-char (point-max))
  6649. (goto-char (match-beginning 0)))
  6650. (point-marker)))
  6651. (defun org-table-justify-field-maybe (&optional new)
  6652. "Justify the current field, text to left, number to right.
  6653. Optional argument NEW may specify text to replace the current field content."
  6654. (cond
  6655. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6656. ((org-at-table-hline-p))
  6657. ((and (not new)
  6658. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6659. (current-buffer)))
  6660. (< (point) org-table-aligned-begin-marker)
  6661. (>= (point) org-table-aligned-end-marker)))
  6662. ;; This is not the same table, force a full re-align
  6663. (setq org-table-may-need-update t))
  6664. (t ;; realign the current field, based on previous full realign
  6665. (let* ((pos (point)) s
  6666. (col (org-table-current-column))
  6667. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6668. l f n o e)
  6669. (when (> col 0)
  6670. (skip-chars-backward "^|\n")
  6671. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6672. (progn
  6673. (setq s (match-string 1)
  6674. o (match-string 0)
  6675. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6676. e (not (= (match-beginning 2) (match-end 2))))
  6677. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6678. l (if e "|" (setq org-table-may-need-update t) ""))
  6679. n (format f s))
  6680. (if new
  6681. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6682. (setq n (format f new))
  6683. (setq n (concat new "|") org-table-may-need-update t)))
  6684. (or (equal n o)
  6685. (let (org-table-may-need-update)
  6686. (replace-match n t t))))
  6687. (setq org-table-may-need-update t))
  6688. (goto-char pos))))))
  6689. (defun org-table-next-field ()
  6690. "Go to the next field in the current table, creating new lines as needed.
  6691. Before doing so, re-align the table if necessary."
  6692. (interactive)
  6693. (org-table-maybe-eval-formula)
  6694. (org-table-maybe-recalculate-line)
  6695. (if (and org-table-automatic-realign
  6696. org-table-may-need-update)
  6697. (org-table-align))
  6698. (let ((end (org-table-end)))
  6699. (if (org-at-table-hline-p)
  6700. (end-of-line 1))
  6701. (condition-case nil
  6702. (progn
  6703. (re-search-forward "|" end)
  6704. (if (looking-at "[ \t]*$")
  6705. (re-search-forward "|" end))
  6706. (if (and (looking-at "-")
  6707. org-table-tab-jumps-over-hlines
  6708. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  6709. (goto-char (match-beginning 1)))
  6710. (if (looking-at "-")
  6711. (progn
  6712. (beginning-of-line 0)
  6713. (org-table-insert-row 'below))
  6714. (if (looking-at " ") (forward-char 1))))
  6715. (error
  6716. (org-table-insert-row 'below)))))
  6717. (defun org-table-previous-field ()
  6718. "Go to the previous field in the table.
  6719. Before doing so, re-align the table if necessary."
  6720. (interactive)
  6721. (org-table-justify-field-maybe)
  6722. (org-table-maybe-recalculate-line)
  6723. (if (and org-table-automatic-realign
  6724. org-table-may-need-update)
  6725. (org-table-align))
  6726. (if (org-at-table-hline-p)
  6727. (end-of-line 1))
  6728. (re-search-backward "|" (org-table-begin))
  6729. (re-search-backward "|" (org-table-begin))
  6730. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  6731. (re-search-backward "|" (org-table-begin)))
  6732. (if (looking-at "| ?")
  6733. (goto-char (match-end 0))))
  6734. (defun org-table-next-row ()
  6735. "Go to the next row (same column) in the current table.
  6736. Before doing so, re-align the table if necessary."
  6737. (interactive)
  6738. (org-table-maybe-eval-formula)
  6739. (org-table-maybe-recalculate-line)
  6740. (if (or (looking-at "[ \t]*$")
  6741. (save-excursion (skip-chars-backward " \t") (bolp)))
  6742. (newline)
  6743. (if (and org-table-automatic-realign
  6744. org-table-may-need-update)
  6745. (org-table-align))
  6746. (let ((col (org-table-current-column)))
  6747. (beginning-of-line 2)
  6748. (if (or (not (org-at-table-p))
  6749. (org-at-table-hline-p))
  6750. (progn
  6751. (beginning-of-line 0)
  6752. (org-table-insert-row 'below)))
  6753. (org-table-goto-column col)
  6754. (skip-chars-backward "^|\n\r")
  6755. (if (looking-at " ") (forward-char 1)))))
  6756. (defun org-table-copy-down (n)
  6757. "Copy a field down in the current column.
  6758. If the field at the cursor is empty, copy into it the content of the nearest
  6759. non-empty field above. With argument N, use the Nth non-empty field.
  6760. If the current field is not empty, it is copied down to the next row, and
  6761. the cursor is moved with it. Therefore, repeating this command causes the
  6762. column to be filled row-by-row.
  6763. If the variable `org-table-copy-increment' is non-nil and the field is an
  6764. integer or a timestamp, it will be incremented while copying. In the case of
  6765. a timestamp, if the cursor is on the year, change the year. If it is on the
  6766. month or the day, change that. Point will stay on the current date field
  6767. in order to easily repeat the interval."
  6768. (interactive "p")
  6769. (let* ((colpos (org-table-current-column))
  6770. (col (current-column))
  6771. (field (org-table-get-field))
  6772. (non-empty (string-match "[^ \t]" field))
  6773. (beg (org-table-begin))
  6774. txt)
  6775. (org-table-check-inside-data-field)
  6776. (if non-empty
  6777. (progn
  6778. (setq txt (org-trim field))
  6779. (org-table-next-row)
  6780. (org-table-blank-field))
  6781. (save-excursion
  6782. (setq txt
  6783. (catch 'exit
  6784. (while (progn (beginning-of-line 1)
  6785. (re-search-backward org-table-dataline-regexp
  6786. beg t))
  6787. (org-table-goto-column colpos t)
  6788. (if (and (looking-at
  6789. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  6790. (= (setq n (1- n)) 0))
  6791. (throw 'exit (match-string 1))))))))
  6792. (if txt
  6793. (progn
  6794. (if (and org-table-copy-increment
  6795. (string-match "^[0-9]+$" txt))
  6796. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  6797. (insert txt)
  6798. (move-to-column col)
  6799. (if (and org-table-copy-increment (org-at-timestamp-p t))
  6800. (org-timestamp-up 1)
  6801. (org-table-maybe-recalculate-line))
  6802. (org-table-align)
  6803. (move-to-column col))
  6804. (error "No non-empty field found"))))
  6805. (defun org-table-check-inside-data-field ()
  6806. "Is point inside a table data field?
  6807. I.e. not on a hline or before the first or after the last column?
  6808. This actually throws an error, so it aborts the current command."
  6809. (if (or (not (org-at-table-p))
  6810. (= (org-table-current-column) 0)
  6811. (org-at-table-hline-p)
  6812. (looking-at "[ \t]*$"))
  6813. (error "Not in table data field")))
  6814. (defvar org-table-clip nil
  6815. "Clipboard for table regions.")
  6816. (defun org-table-blank-field ()
  6817. "Blank the current table field or active region."
  6818. (interactive)
  6819. (org-table-check-inside-data-field)
  6820. (if (and (interactive-p) (org-region-active-p))
  6821. (let (org-table-clip)
  6822. (org-table-cut-region (region-beginning) (region-end)))
  6823. (skip-chars-backward "^|")
  6824. (backward-char 1)
  6825. (if (looking-at "|[^|\n]+")
  6826. (let* ((pos (match-beginning 0))
  6827. (match (match-string 0))
  6828. (len (org-string-width match)))
  6829. (replace-match (concat "|" (make-string (1- len) ?\ )))
  6830. (goto-char (+ 2 pos))
  6831. (substring match 1)))))
  6832. (defun org-table-get-field (&optional n replace)
  6833. "Return the value of the field in column N of current row.
  6834. N defaults to current field.
  6835. If REPLACE is a string, replace field with this value. The return value
  6836. is always the old value."
  6837. (and n (org-table-goto-column n))
  6838. (skip-chars-backward "^|\n")
  6839. (backward-char 1)
  6840. (if (looking-at "|[^|\r\n]*")
  6841. (let* ((pos (match-beginning 0))
  6842. (val (buffer-substring (1+ pos) (match-end 0))))
  6843. (if replace
  6844. (replace-match (concat "|" replace) t t))
  6845. (goto-char (min (point-at-eol) (+ 2 pos)))
  6846. val)
  6847. (forward-char 1) ""))
  6848. (defun org-table-field-info (arg)
  6849. "Show info about the current field, and highlight any reference at point."
  6850. (interactive "P")
  6851. (org-table-get-specials)
  6852. (save-excursion
  6853. (let* ((pos (point))
  6854. (col (org-table-current-column))
  6855. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  6856. (name (car (rassoc (list (org-current-line) col)
  6857. org-table-named-field-locations)))
  6858. (eql (org-table-get-stored-formulas))
  6859. (dline (org-table-current-dline))
  6860. (ref (format "@%d$%d" dline col))
  6861. (ref1 (org-table-convert-refs-to-an ref))
  6862. (fequation (or (assoc name eql) (assoc ref eql)))
  6863. (cequation (assoc (int-to-string col) eql))
  6864. (eqn (or fequation cequation)))
  6865. (goto-char pos)
  6866. (condition-case nil
  6867. (org-table-show-reference 'local)
  6868. (error nil))
  6869. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  6870. dline col
  6871. (if cname (concat " or $" cname) "")
  6872. dline col ref1
  6873. (if name (concat " or $" name) "")
  6874. ;; FIXME: formula info not correct if special table line
  6875. (if eqn
  6876. (concat ", formula: "
  6877. (org-table-formula-to-user
  6878. (concat
  6879. (if (string-match "^[$@]"(car eqn)) "" "$")
  6880. (car eqn) "=" (cdr eqn))))
  6881. "")))))
  6882. (defun org-table-current-column ()
  6883. "Find out which column we are in.
  6884. When called interactively, column is also displayed in echo area."
  6885. (interactive)
  6886. (if (interactive-p) (org-table-check-inside-data-field))
  6887. (save-excursion
  6888. (let ((cnt 0) (pos (point)))
  6889. (beginning-of-line 1)
  6890. (while (search-forward "|" pos t)
  6891. (setq cnt (1+ cnt)))
  6892. (if (interactive-p) (message "This is table column %d" cnt))
  6893. cnt)))
  6894. (defun org-table-current-dline ()
  6895. "Find out what table data line we are in.
  6896. Only datalins count for this."
  6897. (interactive)
  6898. (if (interactive-p) (org-table-check-inside-data-field))
  6899. (save-excursion
  6900. (let ((cnt 0) (pos (point)))
  6901. (goto-char (org-table-begin))
  6902. (while (<= (point) pos)
  6903. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  6904. (beginning-of-line 2))
  6905. (if (interactive-p) (message "This is table line %d" cnt))
  6906. cnt)))
  6907. (defun org-table-goto-column (n &optional on-delim force)
  6908. "Move the cursor to the Nth column in the current table line.
  6909. With optional argument ON-DELIM, stop with point before the left delimiter
  6910. of the field.
  6911. If there are less than N fields, just go to after the last delimiter.
  6912. However, when FORCE is non-nil, create new columns if necessary."
  6913. (interactive "p")
  6914. (let ((pos (point-at-eol)))
  6915. (beginning-of-line 1)
  6916. (when (> n 0)
  6917. (while (and (> (setq n (1- n)) -1)
  6918. (or (search-forward "|" pos t)
  6919. (and force
  6920. (progn (end-of-line 1)
  6921. (skip-chars-backward "^|")
  6922. (insert " | "))))))
  6923. ; (backward-char 2) t)))))
  6924. (when (and force (not (looking-at ".*|")))
  6925. (save-excursion (end-of-line 1) (insert " | ")))
  6926. (if on-delim
  6927. (backward-char 1)
  6928. (if (looking-at " ") (forward-char 1))))))
  6929. (defun org-at-table-p (&optional table-type)
  6930. "Return t if the cursor is inside an org-type table.
  6931. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  6932. (if org-enable-table-editor
  6933. (save-excursion
  6934. (beginning-of-line 1)
  6935. (looking-at (if table-type org-table-any-line-regexp
  6936. org-table-line-regexp)))
  6937. nil))
  6938. (defun org-at-table.el-p ()
  6939. "Return t if and only if we are at a table.el table."
  6940. (and (org-at-table-p 'any)
  6941. (save-excursion
  6942. (goto-char (org-table-begin 'any))
  6943. (looking-at org-table1-hline-regexp))))
  6944. (defun org-table-recognize-table.el ()
  6945. "If there is a table.el table nearby, recognize it and move into it."
  6946. (if org-table-tab-recognizes-table.el
  6947. (if (org-at-table.el-p)
  6948. (progn
  6949. (beginning-of-line 1)
  6950. (if (looking-at org-table-dataline-regexp)
  6951. nil
  6952. (if (looking-at org-table1-hline-regexp)
  6953. (progn
  6954. (beginning-of-line 2)
  6955. (if (looking-at org-table-any-border-regexp)
  6956. (beginning-of-line -1)))))
  6957. (if (re-search-forward "|" (org-table-end t) t)
  6958. (progn
  6959. (require 'table)
  6960. (if (table--at-cell-p (point))
  6961. t
  6962. (message "recognizing table.el table...")
  6963. (table-recognize-table)
  6964. (message "recognizing table.el table...done")))
  6965. (error "This should not happen..."))
  6966. t)
  6967. nil)
  6968. nil))
  6969. (defun org-at-table-hline-p ()
  6970. "Return t if the cursor is inside a hline in a table."
  6971. (if org-enable-table-editor
  6972. (save-excursion
  6973. (beginning-of-line 1)
  6974. (looking-at org-table-hline-regexp))
  6975. nil))
  6976. (defun org-table-insert-column ()
  6977. "Insert a new column into the table."
  6978. (interactive)
  6979. (if (not (org-at-table-p))
  6980. (error "Not at a table"))
  6981. (org-table-find-dataline)
  6982. (let* ((col (max 1 (org-table-current-column)))
  6983. (beg (org-table-begin))
  6984. (end (org-table-end))
  6985. ;; Current cursor position
  6986. (linepos (org-current-line))
  6987. (colpos col))
  6988. (goto-char beg)
  6989. (while (< (point) end)
  6990. (if (org-at-table-hline-p)
  6991. nil
  6992. (org-table-goto-column col t)
  6993. (insert "| "))
  6994. (beginning-of-line 2))
  6995. (move-marker end nil)
  6996. (goto-line linepos)
  6997. (org-table-goto-column colpos)
  6998. (org-table-align)
  6999. (org-table-fix-formulas "$" nil (1- col) 1)))
  7000. (defun org-table-find-dataline ()
  7001. "Find a dataline in the current table, which is needed for column commands."
  7002. (if (and (org-at-table-p)
  7003. (not (org-at-table-hline-p)))
  7004. t
  7005. (let ((col (current-column))
  7006. (end (org-table-end)))
  7007. (move-to-column col)
  7008. (while (and (< (point) end)
  7009. (or (not (= (current-column) col))
  7010. (org-at-table-hline-p)))
  7011. (beginning-of-line 2)
  7012. (move-to-column col))
  7013. (if (and (org-at-table-p)
  7014. (not (org-at-table-hline-p)))
  7015. t
  7016. (error
  7017. "Please position cursor in a data line for column operations")))))
  7018. (defun org-table-delete-column ()
  7019. "Delete a column from the table."
  7020. (interactive)
  7021. (if (not (org-at-table-p))
  7022. (error "Not at a table"))
  7023. (org-table-find-dataline)
  7024. (org-table-check-inside-data-field)
  7025. (let* ((col (org-table-current-column))
  7026. (beg (org-table-begin))
  7027. (end (org-table-end))
  7028. ;; Current cursor position
  7029. (linepos (org-current-line))
  7030. (colpos col))
  7031. (goto-char beg)
  7032. (while (< (point) end)
  7033. (if (org-at-table-hline-p)
  7034. nil
  7035. (org-table-goto-column col t)
  7036. (and (looking-at "|[^|\n]+|")
  7037. (replace-match "|")))
  7038. (beginning-of-line 2))
  7039. (move-marker end nil)
  7040. (goto-line linepos)
  7041. (org-table-goto-column colpos)
  7042. (org-table-align)
  7043. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7044. col -1 col)))
  7045. (defun org-table-move-column-right ()
  7046. "Move column to the right."
  7047. (interactive)
  7048. (org-table-move-column nil))
  7049. (defun org-table-move-column-left ()
  7050. "Move column to the left."
  7051. (interactive)
  7052. (org-table-move-column 'left))
  7053. (defun org-table-move-column (&optional left)
  7054. "Move the current column to the right. With arg LEFT, move to the left."
  7055. (interactive "P")
  7056. (if (not (org-at-table-p))
  7057. (error "Not at a table"))
  7058. (org-table-find-dataline)
  7059. (org-table-check-inside-data-field)
  7060. (let* ((col (org-table-current-column))
  7061. (col1 (if left (1- col) col))
  7062. (beg (org-table-begin))
  7063. (end (org-table-end))
  7064. ;; Current cursor position
  7065. (linepos (org-current-line))
  7066. (colpos (if left (1- col) (1+ col))))
  7067. (if (and left (= col 1))
  7068. (error "Cannot move column further left"))
  7069. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  7070. (error "Cannot move column further right"))
  7071. (goto-char beg)
  7072. (while (< (point) end)
  7073. (if (org-at-table-hline-p)
  7074. nil
  7075. (org-table-goto-column col1 t)
  7076. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  7077. (replace-match "|\\2|\\1|")))
  7078. (beginning-of-line 2))
  7079. (move-marker end nil)
  7080. (goto-line linepos)
  7081. (org-table-goto-column colpos)
  7082. (org-table-align)
  7083. (org-table-fix-formulas
  7084. "$" (list (cons (number-to-string col) (number-to-string colpos))
  7085. (cons (number-to-string colpos) (number-to-string col))))))
  7086. (defun org-table-move-row-down ()
  7087. "Move table row down."
  7088. (interactive)
  7089. (org-table-move-row nil))
  7090. (defun org-table-move-row-up ()
  7091. "Move table row up."
  7092. (interactive)
  7093. (org-table-move-row 'up))
  7094. (defun org-table-move-row (&optional up)
  7095. "Move the current table line down. With arg UP, move it up."
  7096. (interactive "P")
  7097. (let* ((col (current-column))
  7098. (pos (point))
  7099. (hline1p (save-excursion (beginning-of-line 1)
  7100. (looking-at org-table-hline-regexp)))
  7101. (dline1 (org-table-current-dline))
  7102. (dline2 (+ dline1 (if up -1 1)))
  7103. (tonew (if up 0 2))
  7104. txt hline2p)
  7105. (beginning-of-line tonew)
  7106. (unless (org-at-table-p)
  7107. (goto-char pos)
  7108. (error "Cannot move row further"))
  7109. (setq hline2p (looking-at org-table-hline-regexp))
  7110. (goto-char pos)
  7111. (beginning-of-line 1)
  7112. (setq pos (point))
  7113. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  7114. (delete-region (point) (1+ (point-at-eol)))
  7115. (beginning-of-line tonew)
  7116. (insert txt)
  7117. (beginning-of-line 0)
  7118. (move-to-column col)
  7119. (unless (or hline1p hline2p)
  7120. (org-table-fix-formulas
  7121. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  7122. (cons (number-to-string dline2) (number-to-string dline1)))))))
  7123. (defun org-table-insert-row (&optional arg)
  7124. "Insert a new row above the current line into the table.
  7125. With prefix ARG, insert below the current line."
  7126. (interactive "P")
  7127. (if (not (org-at-table-p))
  7128. (error "Not at a table"))
  7129. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  7130. (new (org-table-clean-line line)))
  7131. ;; Fix the first field if necessary
  7132. (if (string-match "^[ \t]*| *[#$] *|" line)
  7133. (setq new (replace-match (match-string 0 line) t t new)))
  7134. (beginning-of-line (if arg 2 1))
  7135. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  7136. (beginning-of-line 0)
  7137. (re-search-forward "| ?" (point-at-eol) t)
  7138. (and (or org-table-may-need-update org-table-overlay-coordinates)
  7139. (org-table-align))
  7140. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  7141. (defun org-table-insert-hline (&optional above)
  7142. "Insert a horizontal-line below the current line into the table.
  7143. With prefix ABOVE, insert above the current line."
  7144. (interactive "P")
  7145. (if (not (org-at-table-p))
  7146. (error "Not at a table"))
  7147. (let ((line (org-table-clean-line
  7148. (buffer-substring (point-at-bol) (point-at-eol))))
  7149. (col (current-column)))
  7150. (while (string-match "|\\( +\\)|" line)
  7151. (setq line (replace-match
  7152. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  7153. ?-) "|") t t line)))
  7154. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  7155. (beginning-of-line (if above 1 2))
  7156. (insert line "\n")
  7157. (beginning-of-line (if above 1 -1))
  7158. (move-to-column col)
  7159. (and org-table-overlay-coordinates (org-table-align))))
  7160. (defun org-table-hline-and-move (&optional same-column)
  7161. "Insert a hline and move to the row below that line."
  7162. (interactive "P")
  7163. (let ((col (org-table-current-column)))
  7164. (org-table-maybe-eval-formula)
  7165. (org-table-maybe-recalculate-line)
  7166. (org-table-insert-hline)
  7167. (end-of-line 2)
  7168. (if (looking-at "\n[ \t]*|-")
  7169. (progn (insert "\n|") (org-table-align))
  7170. (org-table-next-field))
  7171. (if same-column (org-table-goto-column col))))
  7172. (defun org-table-clean-line (s)
  7173. "Convert a table line S into a string with only \"|\" and space.
  7174. In particular, this does handle wide and invisible characters."
  7175. (if (string-match "^[ \t]*|-" s)
  7176. ;; It's a hline, just map the characters
  7177. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  7178. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  7179. (setq s (replace-match
  7180. (concat "|" (make-string (org-string-width (match-string 1 s))
  7181. ?\ ) "|")
  7182. t t s)))
  7183. s))
  7184. (defun org-table-kill-row ()
  7185. "Delete the current row or horizontal line from the table."
  7186. (interactive)
  7187. (if (not (org-at-table-p))
  7188. (error "Not at a table"))
  7189. (let ((col (current-column))
  7190. (dline (org-table-current-dline)))
  7191. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  7192. (if (not (org-at-table-p)) (beginning-of-line 0))
  7193. (move-to-column col)
  7194. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  7195. dline -1 dline)))
  7196. (defun org-table-sort-lines (with-case &optional sorting-type)
  7197. "Sort table lines according to the column at point.
  7198. The position of point indicates the column to be used for
  7199. sorting, and the range of lines is the range between the nearest
  7200. horizontal separator lines, or the entire table of no such lines
  7201. exist. If point is before the first column, you will be prompted
  7202. for the sorting column. If there is an active region, the mark
  7203. specifies the first line and the sorting column, while point
  7204. should be in the last line to be included into the sorting.
  7205. The command then prompts for the sorting type which can be
  7206. alphabetically, numerically, or by time (as given in a time stamp
  7207. in the field). Sorting in reverse order is also possible.
  7208. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  7209. If SORTING-TYPE is specified when this function is called from a Lisp
  7210. program, no prompting will take place. SORTING-TYPE must be a character,
  7211. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  7212. should be done in reverse order."
  7213. (interactive "P")
  7214. (let* ((thisline (org-current-line))
  7215. (thiscol (org-table-current-column))
  7216. beg end bcol ecol tend tbeg column lns pos)
  7217. (when (equal thiscol 0)
  7218. (if (interactive-p)
  7219. (setq thiscol
  7220. (string-to-number
  7221. (read-string "Use column N for sorting: ")))
  7222. (setq thiscol 1))
  7223. (org-table-goto-column thiscol))
  7224. (org-table-check-inside-data-field)
  7225. (if (org-region-active-p)
  7226. (progn
  7227. (setq beg (region-beginning) end (region-end))
  7228. (goto-char beg)
  7229. (setq column (org-table-current-column)
  7230. beg (point-at-bol))
  7231. (goto-char end)
  7232. (setq end (point-at-bol 2)))
  7233. (setq column (org-table-current-column)
  7234. pos (point)
  7235. tbeg (org-table-begin)
  7236. tend (org-table-end))
  7237. (if (re-search-backward org-table-hline-regexp tbeg t)
  7238. (setq beg (point-at-bol 2))
  7239. (goto-char tbeg)
  7240. (setq beg (point-at-bol 1)))
  7241. (goto-char pos)
  7242. (if (re-search-forward org-table-hline-regexp tend t)
  7243. (setq end (point-at-bol 1))
  7244. (goto-char tend)
  7245. (setq end (point-at-bol))))
  7246. (setq beg (move-marker (make-marker) beg)
  7247. end (move-marker (make-marker) end))
  7248. (untabify beg end)
  7249. (goto-char beg)
  7250. (org-table-goto-column column)
  7251. (skip-chars-backward "^|")
  7252. (setq bcol (current-column))
  7253. (org-table-goto-column (1+ column))
  7254. (skip-chars-backward "^|")
  7255. (setq ecol (1- (current-column)))
  7256. (org-table-goto-column column)
  7257. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  7258. (org-split-string (buffer-substring beg end) "\n")))
  7259. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  7260. (delete-region beg end)
  7261. (move-marker beg nil)
  7262. (move-marker end nil)
  7263. (insert (mapconcat 'cdr lns "\n") "\n")
  7264. (goto-line thisline)
  7265. (org-table-goto-column thiscol)
  7266. (message "%d lines sorted, based on column %d" (length lns) column)))
  7267. (defun org-table-cut-region (beg end)
  7268. "Copy region in table to the clipboard and blank all relevant fields."
  7269. (interactive "r")
  7270. (org-table-copy-region beg end 'cut))
  7271. (defun org-table-copy-region (beg end &optional cut)
  7272. "Copy rectangular region in table to clipboard.
  7273. A special clipboard is used which can only be accessed
  7274. with `org-table-paste-rectangle'."
  7275. (interactive "rP")
  7276. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7277. region cols
  7278. (rpl (if cut " " nil)))
  7279. (goto-char beg)
  7280. (org-table-check-inside-data-field)
  7281. (setq l01 (org-current-line)
  7282. c01 (org-table-current-column))
  7283. (goto-char end)
  7284. (org-table-check-inside-data-field)
  7285. (setq l02 (org-current-line)
  7286. c02 (org-table-current-column))
  7287. (setq l1 (min l01 l02) l2 (max l01 l02)
  7288. c1 (min c01 c02) c2 (max c01 c02))
  7289. (catch 'exit
  7290. (while t
  7291. (catch 'nextline
  7292. (if (> l1 l2) (throw 'exit t))
  7293. (goto-line l1)
  7294. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7295. (setq cols nil ic1 c1 ic2 c2)
  7296. (while (< ic1 (1+ ic2))
  7297. (push (org-table-get-field ic1 rpl) cols)
  7298. (setq ic1 (1+ ic1)))
  7299. (push (nreverse cols) region)
  7300. (setq l1 (1+ l1)))))
  7301. (setq org-table-clip (nreverse region))
  7302. (if cut (org-table-align))
  7303. org-table-clip))
  7304. (defun org-table-paste-rectangle ()
  7305. "Paste a rectangular region into a table.
  7306. The upper right corner ends up in the current field. All involved fields
  7307. will be overwritten. If the rectangle does not fit into the present table,
  7308. the table is enlarged as needed. The process ignores horizontal separator
  7309. lines."
  7310. (interactive)
  7311. (unless (and org-table-clip (listp org-table-clip))
  7312. (error "First cut/copy a region to paste!"))
  7313. (org-table-check-inside-data-field)
  7314. (let* ((clip org-table-clip)
  7315. (line (org-current-line))
  7316. (col (org-table-current-column))
  7317. (org-enable-table-editor t)
  7318. (org-table-automatic-realign nil)
  7319. c cols field)
  7320. (while (setq cols (pop clip))
  7321. (while (org-at-table-hline-p) (beginning-of-line 2))
  7322. (if (not (org-at-table-p))
  7323. (progn (end-of-line 0) (org-table-next-field)))
  7324. (setq c col)
  7325. (while (setq field (pop cols))
  7326. (org-table-goto-column c nil 'force)
  7327. (org-table-get-field nil field)
  7328. (setq c (1+ c)))
  7329. (beginning-of-line 2))
  7330. (goto-line line)
  7331. (org-table-goto-column col)
  7332. (org-table-align)))
  7333. (defun org-table-convert ()
  7334. "Convert from `org-mode' table to table.el and back.
  7335. Obviously, this only works within limits. When an Org-mode table is
  7336. converted to table.el, all horizontal separator lines get lost, because
  7337. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7338. A table.el table can be converted to an Org-mode table only if it does not
  7339. do row or column spanning. Multiline cells will become multiple cells.
  7340. Beware, Org-mode does not test if the table can be successfully converted - it
  7341. blindly applies a recipe that works for simple tables."
  7342. (interactive)
  7343. (require 'table)
  7344. (if (org-at-table.el-p)
  7345. ;; convert to Org-mode table
  7346. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7347. (end (move-marker (make-marker) (org-table-end t))))
  7348. (table-unrecognize-region beg end)
  7349. (goto-char beg)
  7350. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7351. (replace-match ""))
  7352. (goto-char beg))
  7353. (if (org-at-table-p)
  7354. ;; convert to table.el table
  7355. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7356. (end (move-marker (make-marker) (org-table-end))))
  7357. ;; first, get rid of all horizontal lines
  7358. (goto-char beg)
  7359. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7360. (replace-match ""))
  7361. ;; insert a hline before first
  7362. (goto-char beg)
  7363. (org-table-insert-hline 'above)
  7364. (beginning-of-line -1)
  7365. ;; insert a hline after each line
  7366. (while (progn (beginning-of-line 3) (< (point) end))
  7367. (org-table-insert-hline))
  7368. (goto-char beg)
  7369. (setq end (move-marker end (org-table-end)))
  7370. ;; replace "+" at beginning and ending of hlines
  7371. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7372. (replace-match "\\1+-"))
  7373. (goto-char beg)
  7374. (while (re-search-forward "-|[ \t]*$" end t)
  7375. (replace-match "-+"))
  7376. (goto-char beg)))))
  7377. (defun org-table-wrap-region (arg)
  7378. "Wrap several fields in a column like a paragraph.
  7379. This is useful if you'd like to spread the contents of a field over several
  7380. lines, in order to keep the table compact.
  7381. If there is an active region, and both point and mark are in the same column,
  7382. the text in the column is wrapped to minimum width for the given number of
  7383. lines. Generally, this makes the table more compact. A prefix ARG may be
  7384. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7385. formats the selected text to two lines. If the region was longer than two
  7386. lines, the remaining lines remain empty. A negative prefix argument reduces
  7387. the current number of lines by that amount. The wrapped text is pasted back
  7388. into the table. If you formatted it to more lines than it was before, fields
  7389. further down in the table get overwritten - so you might need to make space in
  7390. the table first.
  7391. If there is no region, the current field is split at the cursor position and
  7392. the text fragment to the right of the cursor is prepended to the field one
  7393. line down.
  7394. If there is no region, but you specify a prefix ARG, the current field gets
  7395. blank, and the content is appended to the field above."
  7396. (interactive "P")
  7397. (org-table-check-inside-data-field)
  7398. (if (org-region-active-p)
  7399. ;; There is a region: fill as a paragraph
  7400. (let* ((beg (region-beginning))
  7401. (cline (save-excursion (goto-char beg) (org-current-line)))
  7402. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7403. nlines)
  7404. (org-table-cut-region (region-beginning) (region-end))
  7405. (if (> (length (car org-table-clip)) 1)
  7406. (error "Region must be limited to single column"))
  7407. (setq nlines (if arg
  7408. (if (< arg 1)
  7409. (+ (length org-table-clip) arg)
  7410. arg)
  7411. (length org-table-clip)))
  7412. (setq org-table-clip
  7413. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7414. nil nlines)))
  7415. (goto-line cline)
  7416. (org-table-goto-column ccol)
  7417. (org-table-paste-rectangle))
  7418. ;; No region, split the current field at point
  7419. (if arg
  7420. ;; combine with field above
  7421. (let ((s (org-table-blank-field))
  7422. (col (org-table-current-column)))
  7423. (beginning-of-line 0)
  7424. (while (org-at-table-hline-p) (beginning-of-line 0))
  7425. (org-table-goto-column col)
  7426. (skip-chars-forward "^|")
  7427. (skip-chars-backward " ")
  7428. (insert " " (org-trim s))
  7429. (org-table-align))
  7430. ;; split field
  7431. (when (looking-at "\\([^|]+\\)+|")
  7432. (let ((s (match-string 1)))
  7433. (replace-match " |")
  7434. (goto-char (match-beginning 0))
  7435. (org-table-next-row)
  7436. (insert (org-trim s) " ")
  7437. (org-table-align))))))
  7438. (defvar org-field-marker nil)
  7439. (defun org-table-edit-field (arg)
  7440. "Edit table field in a different window.
  7441. This is mainly useful for fields that contain hidden parts.
  7442. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7443. it can be edited in place."
  7444. (interactive "P")
  7445. (if arg
  7446. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7447. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7448. (remove-text-properties b e '(org-cwidth t invisible t
  7449. display t intangible t))
  7450. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7451. (font-lock-fontify-block)))
  7452. (let ((pos (move-marker (make-marker) (point)))
  7453. (field (org-table-get-field))
  7454. (cw (current-window-configuration))
  7455. p)
  7456. (org-switch-to-buffer-other-window "*Org tmp*")
  7457. (erase-buffer)
  7458. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7459. (let ((org-inhibit-startup t)) (org-mode))
  7460. (goto-char (setq p (point-max)))
  7461. (insert (org-trim field))
  7462. (remove-text-properties p (point-max)
  7463. '(invisible t org-cwidth t display t
  7464. intangible t))
  7465. (goto-char p)
  7466. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7467. (org-set-local 'org-window-configuration cw)
  7468. (org-set-local 'org-field-marker pos)
  7469. (message "Edit and finish with C-c C-c"))))
  7470. (defun org-table-finish-edit-field ()
  7471. "Finish editing a table data field.
  7472. Remove all newline characters, insert the result into the table, realign
  7473. the table and kill the editing buffer."
  7474. (let ((pos org-field-marker)
  7475. (cw org-window-configuration)
  7476. (cb (current-buffer))
  7477. text)
  7478. (goto-char (point-min))
  7479. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7480. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7481. (replace-match " "))
  7482. (setq text (org-trim (buffer-string)))
  7483. (set-window-configuration cw)
  7484. (kill-buffer cb)
  7485. (select-window (get-buffer-window (marker-buffer pos)))
  7486. (goto-char pos)
  7487. (move-marker pos nil)
  7488. (org-table-check-inside-data-field)
  7489. (org-table-get-field nil text)
  7490. (org-table-align)
  7491. (message "New field value inserted")))
  7492. (defun org-trim (s)
  7493. "Remove whitespace at beginning and end of string."
  7494. (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7495. (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
  7496. s)
  7497. (defun org-wrap (string &optional width lines)
  7498. "Wrap string to either a number of lines, or a width in characters.
  7499. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7500. that costs. If there is a word longer than WIDTH, the text is actually
  7501. wrapped to the length of that word.
  7502. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7503. many lines, whatever width that takes.
  7504. The return value is a list of lines, without newlines at the end."
  7505. (let* ((words (org-split-string string "[ \t\n]+"))
  7506. (maxword (apply 'max (mapcar 'org-string-width words)))
  7507. w ll)
  7508. (cond (width
  7509. (org-do-wrap words (max maxword width)))
  7510. (lines
  7511. (setq w maxword)
  7512. (setq ll (org-do-wrap words maxword))
  7513. (if (<= (length ll) lines)
  7514. ll
  7515. (setq ll words)
  7516. (while (> (length ll) lines)
  7517. (setq w (1+ w))
  7518. (setq ll (org-do-wrap words w)))
  7519. ll))
  7520. (t (error "Cannot wrap this")))))
  7521. (defun org-do-wrap (words width)
  7522. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  7523. (let (lines line)
  7524. (while words
  7525. (setq line (pop words))
  7526. (while (and words (< (+ (length line) (length (car words))) width))
  7527. (setq line (concat line " " (pop words))))
  7528. (setq lines (push line lines)))
  7529. (nreverse lines)))
  7530. (defun org-split-string (string &optional separators)
  7531. "Splits STRING into substrings at SEPARATORS.
  7532. No empty strings are returned if there are matches at the beginning
  7533. and end of string."
  7534. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  7535. (start 0)
  7536. notfirst
  7537. (list nil))
  7538. (while (and (string-match rexp string
  7539. (if (and notfirst
  7540. (= start (match-beginning 0))
  7541. (< start (length string)))
  7542. (1+ start) start))
  7543. (< (match-beginning 0) (length string)))
  7544. (setq notfirst t)
  7545. (or (eq (match-beginning 0) 0)
  7546. (and (eq (match-beginning 0) (match-end 0))
  7547. (eq (match-beginning 0) start))
  7548. (setq list
  7549. (cons (substring string start (match-beginning 0))
  7550. list)))
  7551. (setq start (match-end 0)))
  7552. (or (eq start (length string))
  7553. (setq list
  7554. (cons (substring string start)
  7555. list)))
  7556. (nreverse list)))
  7557. (defun org-table-map-tables (function)
  7558. "Apply FUNCTION to the start of all tables in the buffer."
  7559. (save-excursion
  7560. (save-restriction
  7561. (widen)
  7562. (goto-char (point-min))
  7563. (while (re-search-forward org-table-any-line-regexp nil t)
  7564. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  7565. (beginning-of-line 1)
  7566. (if (looking-at org-table-line-regexp)
  7567. (save-excursion (funcall function)))
  7568. (re-search-forward org-table-any-border-regexp nil 1))))
  7569. (message "Mapping tables: done"))
  7570. (defvar org-timecnt) ; dynamically scoped parameter
  7571. (defun org-table-sum (&optional beg end nlast)
  7572. "Sum numbers in region of current table column.
  7573. The result will be displayed in the echo area, and will be available
  7574. as kill to be inserted with \\[yank].
  7575. If there is an active region, it is interpreted as a rectangle and all
  7576. numbers in that rectangle will be summed. If there is no active
  7577. region and point is located in a table column, sum all numbers in that
  7578. column.
  7579. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  7580. numbers are assumed to be times as well (in decimal hours) and the
  7581. numbers are added as such.
  7582. If NLAST is a number, only the NLAST fields will actually be summed."
  7583. (interactive)
  7584. (save-excursion
  7585. (let (col (org-timecnt 0) diff h m s org-table-clip)
  7586. (cond
  7587. ((and beg end)) ; beg and end given explicitly
  7588. ((org-region-active-p)
  7589. (setq beg (region-beginning) end (region-end)))
  7590. (t
  7591. (setq col (org-table-current-column))
  7592. (goto-char (org-table-begin))
  7593. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  7594. (error "No table data"))
  7595. (org-table-goto-column col)
  7596. (setq beg (point))
  7597. (goto-char (org-table-end))
  7598. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  7599. (error "No table data"))
  7600. (org-table-goto-column col)
  7601. (setq end (point))))
  7602. (let* ((items (apply 'append (org-table-copy-region beg end)))
  7603. (items1 (cond ((not nlast) items)
  7604. ((>= nlast (length items)) items)
  7605. (t (setq items (reverse items))
  7606. (setcdr (nthcdr (1- nlast) items) nil)
  7607. (nreverse items))))
  7608. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  7609. items1)))
  7610. (res (apply '+ numbers))
  7611. (sres (if (= org-timecnt 0)
  7612. (format "%g" res)
  7613. (setq diff (* 3600 res)
  7614. h (floor (/ diff 3600)) diff (mod diff 3600)
  7615. m (floor (/ diff 60)) diff (mod diff 60)
  7616. s diff)
  7617. (format "%d:%02d:%02d" h m s))))
  7618. (kill-new sres)
  7619. (if (interactive-p)
  7620. (message "%s"
  7621. (substitute-command-keys
  7622. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7623. (length numbers) sres))))
  7624. sres))))
  7625. (defun org-table-get-number-for-summing (s)
  7626. (let (n)
  7627. (if (string-match "^ *|? *" s)
  7628. (setq s (replace-match "" nil nil s)))
  7629. (if (string-match " *|? *$" s)
  7630. (setq s (replace-match "" nil nil s)))
  7631. (setq n (string-to-number s))
  7632. (cond
  7633. ((and (string-match "0" s)
  7634. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7635. ((string-match "\\`[ \t]+\\'" s) nil)
  7636. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7637. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7638. (m (string-to-number (or (match-string 2 s) "0")))
  7639. (s (string-to-number (or (match-string 4 s) "0"))))
  7640. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7641. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7642. ((equal n 0) nil)
  7643. (t n))))
  7644. (defun org-table-current-field-formula (&optional key noerror)
  7645. "Return the formula active for the current field.
  7646. Assumes that specials are in place.
  7647. If KEY is given, return the key to this formula.
  7648. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7649. (let* ((name (car (rassoc (list (org-current-line)
  7650. (org-table-current-column))
  7651. org-table-named-field-locations)))
  7652. (col (org-table-current-column))
  7653. (scol (int-to-string col))
  7654. (ref (format "@%d$%d" (org-table-current-dline) col))
  7655. (stored-list (org-table-get-stored-formulas noerror))
  7656. (ass (or (assoc name stored-list)
  7657. (assoc ref stored-list)
  7658. (assoc scol stored-list))))
  7659. (if key
  7660. (car ass)
  7661. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7662. (cdr ass))))))
  7663. (defun org-table-get-formula (&optional equation named)
  7664. "Read a formula from the minibuffer, offer stored formula as default.
  7665. When NAMED is non-nil, look for a named equation."
  7666. (let* ((stored-list (org-table-get-stored-formulas))
  7667. (name (car (rassoc (list (org-current-line)
  7668. (org-table-current-column))
  7669. org-table-named-field-locations)))
  7670. (ref (format "@%d$%d" (org-table-current-dline)
  7671. (org-table-current-column)))
  7672. (refass (assoc ref stored-list))
  7673. (scol (if named
  7674. (if name name ref)
  7675. (int-to-string (org-table-current-column))))
  7676. (dummy (and (or name refass) (not named)
  7677. (not (y-or-n-p "Replace field formula with column formula? " ))
  7678. (error "Abort")))
  7679. (name (or name ref))
  7680. (org-table-may-need-update nil)
  7681. (stored (cdr (assoc scol stored-list)))
  7682. (eq (cond
  7683. ((and stored equation (string-match "^ *=? *$" equation))
  7684. stored)
  7685. ((stringp equation)
  7686. equation)
  7687. (t (org-table-formula-from-user
  7688. (read-string
  7689. (org-table-formula-to-user
  7690. (format "%s formula %s%s="
  7691. (if named "Field" "Column")
  7692. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7693. scol))
  7694. (if stored (org-table-formula-to-user stored) "")
  7695. 'org-table-formula-history
  7696. )))))
  7697. mustsave)
  7698. (when (not (string-match "\\S-" eq))
  7699. ;; remove formula
  7700. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7701. (org-table-store-formulas stored-list)
  7702. (error "Formula removed"))
  7703. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7704. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7705. (if (and name (not named))
  7706. ;; We set the column equation, delete the named one.
  7707. (setq stored-list (delq (assoc name stored-list) stored-list)
  7708. mustsave t))
  7709. (if stored
  7710. (setcdr (assoc scol stored-list) eq)
  7711. (setq stored-list (cons (cons scol eq) stored-list)))
  7712. (if (or mustsave (not (equal stored eq)))
  7713. (org-table-store-formulas stored-list))
  7714. eq))
  7715. (defun org-table-store-formulas (alist)
  7716. "Store the list of formulas below the current table."
  7717. (setq alist (sort alist 'org-table-formula-less-p))
  7718. (save-excursion
  7719. (goto-char (org-table-end))
  7720. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  7721. (progn
  7722. ;; don't overwrite TBLFM, we might use text properties to store stuff
  7723. (goto-char (match-beginning 2))
  7724. (delete-region (match-beginning 2) (match-end 0)))
  7725. (insert "#+TBLFM:"))
  7726. (insert " "
  7727. (mapconcat (lambda (x)
  7728. (concat
  7729. (if (equal (string-to-char (car x)) ?@) "" "$")
  7730. (car x) "=" (cdr x)))
  7731. alist "::")
  7732. "\n")))
  7733. (defsubst org-table-formula-make-cmp-string (a)
  7734. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  7735. (concat
  7736. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  7737. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  7738. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  7739. (defun org-table-formula-less-p (a b)
  7740. "Compare two formulas for sorting."
  7741. (let ((as (org-table-formula-make-cmp-string (car a)))
  7742. (bs (org-table-formula-make-cmp-string (car b))))
  7743. (and as bs (string< as bs))))
  7744. (defun org-table-get-stored-formulas (&optional noerror)
  7745. "Return an alist with the stored formulas directly after current table."
  7746. (interactive)
  7747. (let (scol eq eq-alist strings string seen)
  7748. (save-excursion
  7749. (goto-char (org-table-end))
  7750. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  7751. (setq strings (org-split-string (match-string 2) " *:: *"))
  7752. (while (setq string (pop strings))
  7753. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  7754. (setq scol (if (match-end 2)
  7755. (match-string 2 string)
  7756. (match-string 1 string))
  7757. eq (match-string 3 string)
  7758. eq-alist (cons (cons scol eq) eq-alist))
  7759. (if (member scol seen)
  7760. (if noerror
  7761. (progn
  7762. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  7763. (ding)
  7764. (sit-for 2))
  7765. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  7766. (push scol seen))))))
  7767. (nreverse eq-alist)))
  7768. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  7769. "Modify the equations after the table structure has been edited.
  7770. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  7771. For all numbers larger than LIMIT, shift them by DELTA."
  7772. (save-excursion
  7773. (goto-char (org-table-end))
  7774. (when (looking-at "#\\+TBLFM:")
  7775. (let ((re (concat key "\\([0-9]+\\)"))
  7776. (re2
  7777. (when remove
  7778. (if (equal key "$")
  7779. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  7780. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  7781. s n a)
  7782. (when remove
  7783. (while (re-search-forward re2 (point-at-eol) t)
  7784. (replace-match "")))
  7785. (while (re-search-forward re (point-at-eol) t)
  7786. (setq s (match-string 1) n (string-to-number s))
  7787. (cond
  7788. ((setq a (assoc s replace))
  7789. (replace-match (concat key (cdr a)) t t))
  7790. ((and limit (> n limit))
  7791. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  7792. (defun org-table-get-specials ()
  7793. "Get the column names and local parameters for this table."
  7794. (save-excursion
  7795. (let ((beg (org-table-begin)) (end (org-table-end))
  7796. names name fields fields1 field cnt
  7797. c v l line col types dlines hlines)
  7798. (setq org-table-column-names nil
  7799. org-table-local-parameters nil
  7800. org-table-named-field-locations nil
  7801. org-table-current-begin-line nil
  7802. org-table-current-begin-pos nil
  7803. org-table-current-line-types nil)
  7804. (goto-char beg)
  7805. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  7806. (setq names (org-split-string (match-string 1) " *| *")
  7807. cnt 1)
  7808. (while (setq name (pop names))
  7809. (setq cnt (1+ cnt))
  7810. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  7811. (push (cons name (int-to-string cnt)) org-table-column-names))))
  7812. (setq org-table-column-names (nreverse org-table-column-names))
  7813. (setq org-table-column-name-regexp
  7814. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  7815. (goto-char beg)
  7816. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  7817. (setq fields (org-split-string (match-string 1) " *| *"))
  7818. (while (setq field (pop fields))
  7819. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  7820. (push (cons (match-string 1 field) (match-string 2 field))
  7821. org-table-local-parameters))))
  7822. (goto-char beg)
  7823. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  7824. (setq c (match-string 1)
  7825. fields (org-split-string (match-string 2) " *| *"))
  7826. (save-excursion
  7827. (beginning-of-line (if (equal c "_") 2 0))
  7828. (setq line (org-current-line) col 1)
  7829. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  7830. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  7831. (while (and fields1 (setq field (pop fields)))
  7832. (setq v (pop fields1) col (1+ col))
  7833. (when (and (stringp field) (stringp v)
  7834. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  7835. (push (cons field v) org-table-local-parameters)
  7836. (push (list field line col) org-table-named-field-locations))))
  7837. ;; Analyse the line types
  7838. (goto-char beg)
  7839. (setq org-table-current-begin-line (org-current-line)
  7840. org-table-current-begin-pos (point)
  7841. l org-table-current-begin-line)
  7842. (while (looking-at "[ \t]*|\\(-\\)?")
  7843. (push (if (match-end 1) 'hline 'dline) types)
  7844. (if (match-end 1) (push l hlines) (push l dlines))
  7845. (beginning-of-line 2)
  7846. (setq l (1+ l)))
  7847. (setq org-table-current-line-types (apply 'vector (nreverse types))
  7848. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  7849. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  7850. (defun org-table-maybe-eval-formula ()
  7851. "Check if the current field starts with \"=\" or \":=\".
  7852. If yes, store the formula and apply it."
  7853. ;; We already know we are in a table. Get field will only return a formula
  7854. ;; when appropriate. It might return a separator line, but no problem.
  7855. (when org-table-formula-evaluate-inline
  7856. (let* ((field (org-trim (or (org-table-get-field) "")))
  7857. named eq)
  7858. (when (string-match "^:?=\\(.*\\)" field)
  7859. (setq named (equal (string-to-char field) ?:)
  7860. eq (match-string 1 field))
  7861. (if (or (fboundp 'calc-eval)
  7862. (equal (substring eq 0 (min 2 (length eq))) "'("))
  7863. (org-table-eval-formula (if named '(4) nil)
  7864. (org-table-formula-from-user eq))
  7865. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  7866. (defvar org-recalc-commands nil
  7867. "List of commands triggering the recalculation of a line.
  7868. Will be filled automatically during use.")
  7869. (defvar org-recalc-marks
  7870. '((" " . "Unmarked: no special line, no automatic recalculation")
  7871. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  7872. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  7873. ("!" . "Column name definition line. Reference in formula as $name.")
  7874. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  7875. ("_" . "Names for values in row below this one.")
  7876. ("^" . "Names for values in row above this one.")))
  7877. (defun org-table-rotate-recalc-marks (&optional newchar)
  7878. "Rotate the recalculation mark in the first column.
  7879. If in any row, the first field is not consistent with a mark,
  7880. insert a new column for the markers.
  7881. When there is an active region, change all the lines in the region,
  7882. after prompting for the marking character.
  7883. After each change, a message will be displayed indicating the meaning
  7884. of the new mark."
  7885. (interactive)
  7886. (unless (org-at-table-p) (error "Not at a table"))
  7887. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  7888. (beg (org-table-begin))
  7889. (end (org-table-end))
  7890. (l (org-current-line))
  7891. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  7892. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  7893. (have-col
  7894. (save-excursion
  7895. (goto-char beg)
  7896. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  7897. (col (org-table-current-column))
  7898. (forcenew (car (assoc newchar org-recalc-marks)))
  7899. epos new)
  7900. (when l1
  7901. (message "Change region to what mark? Type # * ! $ or SPC: ")
  7902. (setq newchar (char-to-string (read-char-exclusive))
  7903. forcenew (car (assoc newchar org-recalc-marks))))
  7904. (if (and newchar (not forcenew))
  7905. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  7906. newchar))
  7907. (if l1 (goto-line l1))
  7908. (save-excursion
  7909. (beginning-of-line 1)
  7910. (unless (looking-at org-table-dataline-regexp)
  7911. (error "Not at a table data line")))
  7912. (unless have-col
  7913. (org-table-goto-column 1)
  7914. (org-table-insert-column)
  7915. (org-table-goto-column (1+ col)))
  7916. (setq epos (point-at-eol))
  7917. (save-excursion
  7918. (beginning-of-line 1)
  7919. (org-table-get-field
  7920. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  7921. (concat " "
  7922. (setq new (or forcenew
  7923. (cadr (member (match-string 1) marks))))
  7924. " ")
  7925. " # ")))
  7926. (if (and l1 l2)
  7927. (progn
  7928. (goto-line l1)
  7929. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  7930. (and (looking-at org-table-dataline-regexp)
  7931. (org-table-get-field 1 (concat " " new " "))))
  7932. (goto-line l1)))
  7933. (if (not (= epos (point-at-eol))) (org-table-align))
  7934. (goto-line l)
  7935. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  7936. (defun org-table-maybe-recalculate-line ()
  7937. "Recompute the current line if marked for it, and if we haven't just done it."
  7938. (interactive)
  7939. (and org-table-allow-automatic-line-recalculation
  7940. (not (and (memq last-command org-recalc-commands)
  7941. (equal org-last-recalc-line (org-current-line))))
  7942. (save-excursion (beginning-of-line 1)
  7943. (looking-at org-table-auto-recalculate-regexp))
  7944. (org-table-recalculate) t))
  7945. (defvar org-table-formula-debug nil
  7946. "Non-nil means, debug table formulas.
  7947. When nil, simply write \"#ERROR\" in corrupted fields.")
  7948. (make-variable-buffer-local 'org-table-formula-debug)
  7949. (defvar modes)
  7950. (defsubst org-set-calc-mode (var &optional value)
  7951. (if (stringp var)
  7952. (setq var (assoc var '(("D" calc-angle-mode deg)
  7953. ("R" calc-angle-mode rad)
  7954. ("F" calc-prefer-frac t)
  7955. ("S" calc-symbolic-mode t)))
  7956. value (nth 2 var) var (nth 1 var)))
  7957. (if (memq var modes)
  7958. (setcar (cdr (memq var modes)) value)
  7959. (cons var (cons value modes)))
  7960. modes)
  7961. (defun org-table-eval-formula (&optional arg equation
  7962. suppress-align suppress-const
  7963. suppress-store suppress-analysis)
  7964. "Replace the table field value at the cursor by the result of a calculation.
  7965. This function makes use of Dave Gillespie's Calc package, in my view the
  7966. most exciting program ever written for GNU Emacs. So you need to have Calc
  7967. installed in order to use this function.
  7968. In a table, this command replaces the value in the current field with the
  7969. result of a formula. It also installs the formula as the \"current\" column
  7970. formula, by storing it in a special line below the table. When called
  7971. with a `C-u' prefix, the current field must ba a named field, and the
  7972. formula is installed as valid in only this specific field.
  7973. When called with two `C-u' prefixes, insert the active equation
  7974. for the field back into the current field, so that it can be
  7975. edited there. This is useful in order to use \\[org-table-show-reference]
  7976. to check the referenced fields.
  7977. When called, the command first prompts for a formula, which is read in
  7978. the minibuffer. Previously entered formulas are available through the
  7979. history list, and the last used formula is offered as a default.
  7980. These stored formulas are adapted correctly when moving, inserting, or
  7981. deleting columns with the corresponding commands.
  7982. The formula can be any algebraic expression understood by the Calc package.
  7983. For details, see the Org-mode manual.
  7984. This function can also be called from Lisp programs and offers
  7985. additional arguments: EQUATION can be the formula to apply. If this
  7986. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  7987. used to speed-up recursive calls by by-passing unnecessary aligns.
  7988. SUPPRESS-CONST suppresses the interpretation of constants in the
  7989. formula, assuming that this has been done already outside the function.
  7990. SUPPRESS-STORE means the formula should not be stored, either because
  7991. it is already stored, or because it is a modified equation that should
  7992. not overwrite the stored one."
  7993. (interactive "P")
  7994. (org-table-check-inside-data-field)
  7995. (or suppress-analysis (org-table-get-specials))
  7996. (if (equal arg '(16))
  7997. (let ((eq (org-table-current-field-formula)))
  7998. (or eq (error "No equation active for current field"))
  7999. (org-table-get-field nil eq)
  8000. (org-table-align)
  8001. (setq org-table-may-need-update t))
  8002. (let* (fields
  8003. (ndown (if (integerp arg) arg 1))
  8004. (org-table-automatic-realign nil)
  8005. (case-fold-search nil)
  8006. (down (> ndown 1))
  8007. (formula (if (and equation suppress-store)
  8008. equation
  8009. (org-table-get-formula equation (equal arg '(4)))))
  8010. (n0 (org-table-current-column))
  8011. (modes (copy-sequence org-calc-default-modes))
  8012. (numbers nil) ; was a variable, now fixed default
  8013. (keep-empty nil)
  8014. n form form0 bw fmt x ev orig c lispp literal)
  8015. ;; Parse the format string. Since we have a lot of modes, this is
  8016. ;; a lot of work. However, I think calc still uses most of the time.
  8017. (if (string-match ";" formula)
  8018. (let ((tmp (org-split-string formula ";")))
  8019. (setq formula (car tmp)
  8020. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8021. (nth 1 tmp)))
  8022. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8023. (setq c (string-to-char (match-string 1 fmt))
  8024. n (string-to-number (match-string 2 fmt)))
  8025. (if (= c ?p)
  8026. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8027. (setq modes (org-set-calc-mode
  8028. 'calc-float-format
  8029. (list (cdr (assoc c '((?n . float) (?f . fix)
  8030. (?s . sci) (?e . eng))))
  8031. n))))
  8032. (setq fmt (replace-match "" t t fmt)))
  8033. (if (string-match "[NT]" fmt)
  8034. (setq numbers (equal (match-string 0 fmt) "N")
  8035. fmt (replace-match "" t t fmt)))
  8036. (if (string-match "L" fmt)
  8037. (setq literal t
  8038. fmt (replace-match "" t t fmt)))
  8039. (if (string-match "E" fmt)
  8040. (setq keep-empty t
  8041. fmt (replace-match "" t t fmt)))
  8042. (while (string-match "[DRFS]" fmt)
  8043. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  8044. (setq fmt (replace-match "" t t fmt)))
  8045. (unless (string-match "\\S-" fmt)
  8046. (setq fmt nil))))
  8047. (if (and (not suppress-const) org-table-formula-use-constants)
  8048. (setq formula (org-table-formula-substitute-names formula)))
  8049. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  8050. (while (> ndown 0)
  8051. (setq fields (org-split-string
  8052. (org-no-properties
  8053. (buffer-substring (point-at-bol) (point-at-eol)))
  8054. " *| *"))
  8055. (if (eq numbers t)
  8056. (setq fields (mapcar
  8057. (lambda (x) (number-to-string (string-to-number x)))
  8058. fields)))
  8059. (setq ndown (1- ndown))
  8060. (setq form (copy-sequence formula)
  8061. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  8062. (if (and lispp literal) (setq lispp 'literal))
  8063. ;; Check for old vertical references
  8064. (setq form (org-rewrite-old-row-references form))
  8065. ;; Insert complex ranges
  8066. (while (string-match org-table-range-regexp form)
  8067. (setq form
  8068. (replace-match
  8069. (save-match-data
  8070. (org-table-make-reference
  8071. (org-table-get-range (match-string 0 form) nil n0)
  8072. keep-empty numbers lispp))
  8073. t t form)))
  8074. ;; Insert simple ranges
  8075. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  8076. (setq form
  8077. (replace-match
  8078. (save-match-data
  8079. (org-table-make-reference
  8080. (org-sublist
  8081. fields (string-to-number (match-string 1 form))
  8082. (string-to-number (match-string 2 form)))
  8083. keep-empty numbers lispp))
  8084. t t form)))
  8085. (setq form0 form)
  8086. ;; Insert the references to fields in same row
  8087. (while (string-match "\\$\\([0-9]+\\)" form)
  8088. (setq n (string-to-number (match-string 1 form))
  8089. x (nth (1- (if (= n 0) n0 n)) fields))
  8090. (unless x (error "Invalid field specifier \"%s\""
  8091. (match-string 0 form)))
  8092. (setq form (replace-match
  8093. (save-match-data
  8094. (org-table-make-reference x nil numbers lispp))
  8095. t t form)))
  8096. (if lispp
  8097. (setq ev (condition-case nil
  8098. (eval (eval (read form)))
  8099. (error "#ERROR"))
  8100. ev (if (numberp ev) (number-to-string ev) ev))
  8101. (or (fboundp 'calc-eval)
  8102. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  8103. (setq ev (calc-eval (cons form modes)
  8104. (if numbers 'num))))
  8105. (when org-table-formula-debug
  8106. (with-output-to-temp-buffer "*Substitution History*"
  8107. (princ (format "Substitution history of formula
  8108. Orig: %s
  8109. $xyz-> %s
  8110. @r$c-> %s
  8111. $1-> %s\n" orig formula form0 form))
  8112. (if (listp ev)
  8113. (princ (format " %s^\nError: %s"
  8114. (make-string (car ev) ?\-) (nth 1 ev)))
  8115. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  8116. ev (or fmt "NONE")
  8117. (if fmt (format fmt (string-to-number ev)) ev)))))
  8118. (setq bw (get-buffer-window "*Substitution History*"))
  8119. (shrink-window-if-larger-than-buffer bw)
  8120. (unless (and (interactive-p) (not ndown))
  8121. (unless (let (inhibit-redisplay)
  8122. (y-or-n-p "Debugging Formula. Continue to next? "))
  8123. (org-table-align)
  8124. (error "Abort"))
  8125. (delete-window bw)
  8126. (message "")))
  8127. (if (listp ev) (setq fmt nil ev "#ERROR"))
  8128. (org-table-justify-field-maybe
  8129. (if fmt (format fmt (string-to-number ev)) ev))
  8130. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  8131. (call-interactively 'org-return)
  8132. (setq ndown 0)))
  8133. (and down (org-table-maybe-recalculate-line))
  8134. (or suppress-align (and org-table-may-need-update
  8135. (org-table-align))))))
  8136. (defun org-table-put-field-property (prop value)
  8137. (save-excursion
  8138. (put-text-property (progn (skip-chars-backward "^|") (point))
  8139. (progn (skip-chars-forward "^|") (point))
  8140. prop value)))
  8141. (defun org-table-get-range (desc &optional tbeg col highlight)
  8142. "Get a calc vector from a column, accorting to descriptor DESC.
  8143. Optional arguments TBEG and COL can give the beginning of the table and
  8144. the current column, to avoid unnecessary parsing.
  8145. HIGHLIGHT means, just highlight the range."
  8146. (if (not (equal (string-to-char desc) ?@))
  8147. (setq desc (concat "@" desc)))
  8148. (save-excursion
  8149. (or tbeg (setq tbeg (org-table-begin)))
  8150. (or col (setq col (org-table-current-column)))
  8151. (let ((thisline (org-current-line))
  8152. beg end c1 c2 r1 r2 rangep tmp)
  8153. (unless (string-match org-table-range-regexp desc)
  8154. (error "Invalid table range specifier `%s'" desc))
  8155. (setq rangep (match-end 3)
  8156. r1 (and (match-end 1) (match-string 1 desc))
  8157. r2 (and (match-end 4) (match-string 4 desc))
  8158. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  8159. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  8160. (and c1 (setq c1 (+ (string-to-number c1)
  8161. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  8162. (and c2 (setq c2 (+ (string-to-number c2)
  8163. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  8164. (if (equal r1 "") (setq r1 nil))
  8165. (if (equal r2 "") (setq r2 nil))
  8166. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  8167. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  8168. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  8169. (if (not r1) (setq r1 thisline))
  8170. (if (not r2) (setq r2 thisline))
  8171. (if (not c1) (setq c1 col))
  8172. (if (not c2) (setq c2 col))
  8173. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  8174. ;; just one field
  8175. (progn
  8176. (goto-line r1)
  8177. (while (not (looking-at org-table-dataline-regexp))
  8178. (beginning-of-line 2))
  8179. (prog1 (org-trim (org-table-get-field c1))
  8180. (if highlight (org-table-highlight-rectangle (point) (point)))))
  8181. ;; A range, return a vector
  8182. ;; First sort the numbers to get a regular ractangle
  8183. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  8184. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  8185. (goto-line r1)
  8186. (while (not (looking-at org-table-dataline-regexp))
  8187. (beginning-of-line 2))
  8188. (org-table-goto-column c1)
  8189. (setq beg (point))
  8190. (goto-line r2)
  8191. (while (not (looking-at org-table-dataline-regexp))
  8192. (beginning-of-line 0))
  8193. (org-table-goto-column c2)
  8194. (setq end (point))
  8195. (if highlight
  8196. (org-table-highlight-rectangle
  8197. beg (progn (skip-chars-forward "^|\n") (point))))
  8198. ;; return string representation of calc vector
  8199. (mapcar 'org-trim
  8200. (apply 'append (org-table-copy-region beg end)))))))
  8201. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  8202. "Analyze descriptor DESC and retrieve the corresponding line number.
  8203. The cursor is currently in line CLINE, the table begins in line BLINE,
  8204. and TABLE is a vector with line types."
  8205. (if (string-match "^[0-9]+$" desc)
  8206. (aref org-table-dlines (string-to-number desc))
  8207. (setq cline (or cline (org-current-line))
  8208. bline (or bline org-table-current-begin-line)
  8209. table (or table org-table-current-line-types))
  8210. (if (or
  8211. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  8212. ;; 1 2 3 4 5 6
  8213. (and (not (match-end 3)) (not (match-end 6)))
  8214. (and (match-end 3) (match-end 6) (not (match-end 5))))
  8215. (error "invalid row descriptor `%s'" desc))
  8216. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  8217. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  8218. (odir (and (match-end 5) (match-string 5 desc)))
  8219. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  8220. (i (- cline bline))
  8221. (rel (and (match-end 6)
  8222. (or (and (match-end 1) (not (match-end 3)))
  8223. (match-end 5)))))
  8224. (if (and hn (not hdir))
  8225. (progn
  8226. (setq i 0 hdir "+")
  8227. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  8228. (if (and (not hn) on (not odir))
  8229. (error "should never happen");;(aref org-table-dlines on)
  8230. (if (and hn (> hn 0))
  8231. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  8232. (if on
  8233. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  8234. (+ bline i)))))
  8235. (defun org-find-row-type (table i type backwards relative n)
  8236. (let ((l (length table)))
  8237. (while (> n 0)
  8238. (while (and (setq i (+ i (if backwards -1 1)))
  8239. (>= i 0) (< i l)
  8240. (not (eq (aref table i) type))
  8241. (if (and relative (eq (aref table i) 'hline))
  8242. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  8243. t)))
  8244. (setq n (1- n)))
  8245. (if (or (< i 0) (>= i l))
  8246. (error "Row descriptior leads outside table")
  8247. i)))
  8248. (defun org-rewrite-old-row-references (s)
  8249. (if (string-match "&[-+0-9I]" s)
  8250. (error "Formula contains old &row reference, please rewrite using @-syntax")
  8251. s))
  8252. (defun org-table-make-reference (elements keep-empty numbers lispp)
  8253. "Convert list ELEMENTS to something appropriate to insert into formula.
  8254. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  8255. NUMBERS indicates that everything should be converted to numbers.
  8256. LISPP means to return something appropriate for a Lisp list."
  8257. (if (stringp elements) ; just a single val
  8258. (if lispp
  8259. (if (eq lispp 'literal)
  8260. elements
  8261. (prin1-to-string (if numbers (string-to-number elements) elements)))
  8262. (if (equal elements "") (setq elements "0"))
  8263. (if numbers (number-to-string (string-to-number elements)) elements))
  8264. (unless keep-empty
  8265. (setq elements
  8266. (delq nil
  8267. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8268. elements))))
  8269. (setq elements (or elements '("0")))
  8270. (if lispp
  8271. (mapconcat
  8272. (lambda (x)
  8273. (if (eq lispp 'literal)
  8274. x
  8275. (prin1-to-string (if numbers (string-to-number x) x))))
  8276. elements " ")
  8277. (concat "[" (mapconcat
  8278. (lambda (x)
  8279. (if numbers (number-to-string (string-to-number x)) x))
  8280. elements
  8281. ",") "]"))))
  8282. (defun org-table-recalculate (&optional all noalign)
  8283. "Recalculate the current table line by applying all stored formulas.
  8284. With prefix arg ALL, do this for all lines in the table."
  8285. (interactive "P")
  8286. (or (memq this-command org-recalc-commands)
  8287. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8288. (unless (org-at-table-p) (error "Not at a table"))
  8289. (if (equal all '(16))
  8290. (org-table-iterate)
  8291. (org-table-get-specials)
  8292. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8293. (lambda (a b) (string< (car a) (car b)))))
  8294. (inhibit-redisplay (not debug-on-error))
  8295. (line-re org-table-dataline-regexp)
  8296. (thisline (org-current-line))
  8297. (thiscol (org-table-current-column))
  8298. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8299. ;; Insert constants in all formulas
  8300. (setq eqlist
  8301. (mapcar (lambda (x)
  8302. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8303. x)
  8304. eqlist))
  8305. ;; Split the equation list
  8306. (while (setq eq (pop eqlist))
  8307. (if (<= (string-to-char (car eq)) ?9)
  8308. (push eq eqlnum)
  8309. (push eq eqlname)))
  8310. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8311. (if all
  8312. (progn
  8313. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8314. (goto-char (setq beg (org-table-begin)))
  8315. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8316. ;; This is a table with marked lines, compute selected lines
  8317. (setq line-re org-table-recalculate-regexp)
  8318. ;; Move forward to the first non-header line
  8319. (if (and (re-search-forward org-table-dataline-regexp end t)
  8320. (re-search-forward org-table-hline-regexp end t)
  8321. (re-search-forward org-table-dataline-regexp end t))
  8322. (setq beg (match-beginning 0))
  8323. nil))) ;; just leave beg where it is
  8324. (setq beg (point-at-bol)
  8325. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8326. (goto-char beg)
  8327. (and all (message "Re-applying formulas to full table..."))
  8328. ;; First find the named fields, and mark them untouchanble
  8329. (remove-text-properties beg end '(org-untouchable t))
  8330. (while (setq eq (pop eqlname))
  8331. (setq name (car eq)
  8332. a (assoc name org-table-named-field-locations))
  8333. (and (not a)
  8334. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8335. (setq a (list name
  8336. (aref org-table-dlines
  8337. (string-to-number (match-string 1 name)))
  8338. (string-to-number (match-string 2 name)))))
  8339. (when (and a (or all (equal (nth 1 a) thisline)))
  8340. (message "Re-applying formula to field: %s" name)
  8341. (goto-line (nth 1 a))
  8342. (org-table-goto-column (nth 2 a))
  8343. (push (append a (list (cdr eq))) eqlname1)
  8344. ;; FIXME (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  8345. ;; FIXME 'nostore 'noanalysis)
  8346. (org-table-put-field-property :org-untouchable t)))
  8347. ;; Now evauluate the column formulas, but skip fields covered by
  8348. ;; field formulas
  8349. (goto-char beg)
  8350. (while (re-search-forward line-re end t)
  8351. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8352. ;; Unprotected line, recalculate
  8353. (and all (message "Re-applying formulas to full table...(line %d)"
  8354. (setq cnt (1+ cnt))))
  8355. (setq org-last-recalc-line (org-current-line))
  8356. (setq eql eqlnum)
  8357. (while (setq entry (pop eql))
  8358. (goto-line org-last-recalc-line)
  8359. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8360. (unless (get-text-property (point) :org-untouchable)
  8361. (org-table-eval-formula nil (cdr entry)
  8362. 'noalign 'nocst 'nostore 'noanalysis)))))
  8363. ;; Now evaluate the field formulas
  8364. (while (setq eq (pop eqlname1))
  8365. (message "Re-applying formula to field: %s" (car eq))
  8366. (goto-line (nth 1 eq))
  8367. (org-table-goto-column (nth 2 eq))
  8368. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8369. 'nostore 'noanalysis))
  8370. (goto-line thisline)
  8371. (org-table-goto-column thiscol)
  8372. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8373. (or noalign (and org-table-may-need-update (org-table-align))
  8374. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8375. ;; back to initial position
  8376. (message "Re-applying formulas...done")
  8377. (goto-line thisline)
  8378. (org-table-goto-column thiscol)
  8379. (or noalign (and org-table-may-need-update (org-table-align))
  8380. (and all (message "Re-applying formulas...done"))))))
  8381. (defun org-table-iterate (&optional arg)
  8382. "Recalculate the table until it does not change anymore."
  8383. (interactive "P")
  8384. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8385. (i 0)
  8386. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8387. thistbl)
  8388. (catch 'exit
  8389. (while (< i imax)
  8390. (setq i (1+ i))
  8391. (org-table-recalculate 'all)
  8392. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8393. (if (not (string= lasttbl thistbl))
  8394. (setq lasttbl thistbl)
  8395. (if (> i 1)
  8396. (message "Convergence after %d iterations" i)
  8397. (message "Table was already stable"))
  8398. (throw 'exit t)))
  8399. (error "No convergence after %d iterations" i))))
  8400. (defun org-table-formula-substitute-names (f)
  8401. "Replace $const with values in string F."
  8402. (let ((start 0) a (f1 f))
  8403. ;; First, check for column names
  8404. (while (setq start (string-match org-table-column-name-regexp f start))
  8405. (setq start (1+ start))
  8406. (setq a (assoc (match-string 1 f) org-table-column-names))
  8407. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8408. ;; Parameters and constants
  8409. (setq start 0)
  8410. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8411. (setq start (1+ start))
  8412. (if (setq a (save-match-data
  8413. (org-table-get-constant (match-string 1 f))))
  8414. (setq f (replace-match (concat "(" a ")") t t f))))
  8415. (if org-table-formula-debug
  8416. (put-text-property 0 (length f) :orig-formula f1 f))
  8417. f))
  8418. (defun org-table-get-constant (const)
  8419. "Find the value for a parameter or constant in a formula.
  8420. Parameters get priority."
  8421. (or (cdr (assoc const org-table-local-parameters))
  8422. (cdr (assoc const org-table-formula-constants-local))
  8423. (cdr (assoc const org-table-formula-constants))
  8424. (and (fboundp 'constants-get) (constants-get const))
  8425. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8426. (org-entry-get nil (substring const 5) 'inherit))
  8427. "#UNDEFINED_NAME"))
  8428. (defvar org-table-fedit-map
  8429. (let ((map (make-sparse-keymap)))
  8430. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  8431. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  8432. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  8433. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  8434. (org-defkey map "\C-c?" 'org-table-show-reference)
  8435. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  8436. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  8437. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  8438. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  8439. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  8440. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  8441. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  8442. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  8443. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  8444. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  8445. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  8446. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  8447. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8448. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8449. map))
  8450. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8451. '("Edit-Formulas"
  8452. ["Finish and Install" org-table-fedit-finish t]
  8453. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8454. ["Abort" org-table-fedit-abort t]
  8455. "--"
  8456. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8457. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8458. "--"
  8459. "Shift Reference at Point"
  8460. ["Up" org-table-fedit-ref-up t]
  8461. ["Down" org-table-fedit-ref-down t]
  8462. ["Left" org-table-fedit-ref-left t]
  8463. ["Right" org-table-fedit-ref-right t]
  8464. "-"
  8465. "Change Test Row for Column Formulas"
  8466. ["Up" org-table-fedit-line-up t]
  8467. ["Down" org-table-fedit-line-down t]
  8468. "--"
  8469. ["Scroll Table Window" org-table-fedit-scroll t]
  8470. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8471. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8472. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8473. org-table-overlay-coordinates)]
  8474. "--"
  8475. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8476. :style toggle :selected org-table-buffer-is-an]))
  8477. (defvar org-pos)
  8478. (defun org-table-edit-formulas ()
  8479. "Edit the formulas of the current table in a separate buffer."
  8480. (interactive)
  8481. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8482. (beginning-of-line 0))
  8483. (unless (org-at-table-p) (error "Not at a table"))
  8484. (org-table-get-specials)
  8485. (let ((key (org-table-current-field-formula 'key 'noerror))
  8486. (eql (sort (org-table-get-stored-formulas 'noerror)
  8487. 'org-table-formula-less-p))
  8488. (pos (move-marker (make-marker) (point)))
  8489. (startline 1)
  8490. (wc (current-window-configuration))
  8491. (titles '((column . "# Column Formulas\n")
  8492. (field . "# Field Formulas\n")
  8493. (named . "# Named Field Formulas\n")))
  8494. entry s type title)
  8495. (org-switch-to-buffer-other-window "*Edit Formulas*")
  8496. (erase-buffer)
  8497. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8498. (let ((font-lock-global-modes '(not fundamental-mode)))
  8499. (fundamental-mode))
  8500. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8501. (org-set-local 'org-pos pos)
  8502. (org-set-local 'org-window-configuration wc)
  8503. (use-local-map org-table-fedit-map)
  8504. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8505. (easy-menu-add org-table-fedit-menu)
  8506. (setq startline (org-current-line))
  8507. (while (setq entry (pop eql))
  8508. (setq type (cond
  8509. ((equal (string-to-char (car entry)) ?@) 'field)
  8510. ((string-match "^[0-9]" (car entry)) 'column)
  8511. (t 'named)))
  8512. (when (setq title (assq type titles))
  8513. (or (bobp) (insert "\n"))
  8514. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  8515. (setq titles (delq title titles)))
  8516. (if (equal key (car entry)) (setq startline (org-current-line)))
  8517. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  8518. (car entry) " = " (cdr entry) "\n"))
  8519. (remove-text-properties 0 (length s) '(face nil) s)
  8520. (insert s))
  8521. (if (eq org-table-use-standard-references t)
  8522. (org-table-fedit-toggle-ref-type))
  8523. (goto-line startline)
  8524. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  8525. (defun org-table-fedit-post-command ()
  8526. (when (not (memq this-command '(lisp-complete-symbol)))
  8527. (let ((win (selected-window)))
  8528. (save-excursion
  8529. (condition-case nil
  8530. (org-table-show-reference)
  8531. (error nil))
  8532. (select-window win)))))
  8533. (defun org-table-formula-to-user (s)
  8534. "Convert a formula from internal to user representation."
  8535. (if (eq org-table-use-standard-references t)
  8536. (org-table-convert-refs-to-an s)
  8537. s))
  8538. (defun org-table-formula-from-user (s)
  8539. "Convert a formula from user to internal representation."
  8540. (if org-table-use-standard-references
  8541. (org-table-convert-refs-to-rc s)
  8542. s))
  8543. (defun org-table-convert-refs-to-rc (s)
  8544. "Convert spreadsheet references from AB7 to @7$28.
  8545. Works for single references, but also for entire formulas and even the
  8546. full TBLFM line."
  8547. (let ((start 0))
  8548. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  8549. (cond
  8550. ((match-end 3)
  8551. ;; format match, just advance
  8552. (setq start (match-end 0)))
  8553. ((and (> (match-beginning 0) 0)
  8554. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  8555. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  8556. ;; 3.e5 or something like this. FIXME: is this ok????
  8557. (setq start (match-end 0)))
  8558. (t
  8559. (setq start (match-beginning 0)
  8560. s (replace-match
  8561. (if (equal (match-string 2 s) "&")
  8562. (format "$%d" (org-letters-to-number (match-string 1 s)))
  8563. (format "@%d$%d"
  8564. (string-to-number (match-string 2 s))
  8565. (org-letters-to-number (match-string 1 s))))
  8566. t t s)))))
  8567. s))
  8568. (defun org-table-convert-refs-to-an (s)
  8569. "Convert spreadsheet references from to @7$28 to AB7.
  8570. Works for single references, but also for entire formulas and even the
  8571. full TBLFM line."
  8572. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  8573. (setq s (replace-match
  8574. (format "%s%d"
  8575. (org-number-to-letters
  8576. (string-to-number (match-string 2 s)))
  8577. (string-to-number (match-string 1 s)))
  8578. t t s)))
  8579. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  8580. (setq s (replace-match (concat "\\1"
  8581. (org-number-to-letters
  8582. (string-to-number (match-string 2 s))) "&")
  8583. t nil s)))
  8584. s)
  8585. (defun org-letters-to-number (s)
  8586. "Convert a base 26 number represented by letters into an integer.
  8587. For example: AB -> 28."
  8588. (let ((n 0))
  8589. (setq s (upcase s))
  8590. (while (> (length s) 0)
  8591. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  8592. s (substring s 1)))
  8593. n))
  8594. (defun org-number-to-letters (n)
  8595. "Convert an integer into a base 26 number represented by letters.
  8596. For example: 28 -> AB."
  8597. (let ((s ""))
  8598. (while (> n 0)
  8599. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  8600. n (/ (1- n) 26)))
  8601. s))
  8602. (defun org-table-fedit-convert-buffer (function)
  8603. "Convert all references in this buffer, using FUNTION."
  8604. (let ((line (org-current-line)))
  8605. (goto-char (point-min))
  8606. (while (not (eobp))
  8607. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  8608. (delete-region (point) (point-at-eol))
  8609. (or (eobp) (forward-char 1)))
  8610. (goto-line line)))
  8611. (defun org-table-fedit-toggle-ref-type ()
  8612. "Convert all references in the buffer from B3 to @3$2 and back."
  8613. (interactive)
  8614. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  8615. (org-table-fedit-convert-buffer
  8616. (if org-table-buffer-is-an
  8617. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  8618. (message "Reference type switched to %s"
  8619. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  8620. (defun org-table-fedit-ref-up ()
  8621. "Shift the reference at point one row/hline up."
  8622. (interactive)
  8623. (org-table-fedit-shift-reference 'up))
  8624. (defun org-table-fedit-ref-down ()
  8625. "Shift the reference at point one row/hline down."
  8626. (interactive)
  8627. (org-table-fedit-shift-reference 'down))
  8628. (defun org-table-fedit-ref-left ()
  8629. "Shift the reference at point one field to the left."
  8630. (interactive)
  8631. (org-table-fedit-shift-reference 'left))
  8632. (defun org-table-fedit-ref-right ()
  8633. "Shift the reference at point one field to the right."
  8634. (interactive)
  8635. (org-table-fedit-shift-reference 'right))
  8636. (defun org-table-fedit-shift-reference (dir)
  8637. (cond
  8638. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  8639. (if (memq dir '(left right))
  8640. (org-rematch-and-replace 1 (eq dir 'left))
  8641. (error "Cannot shift reference in this direction")))
  8642. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8643. ;; A B3-like reference
  8644. (if (memq dir '(up down))
  8645. (org-rematch-and-replace 2 (eq dir 'up))
  8646. (org-rematch-and-replace 1 (eq dir 'left))))
  8647. ((org-at-regexp-p
  8648. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8649. ;; An internal reference
  8650. (if (memq dir '(up down))
  8651. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8652. (org-rematch-and-replace 5 (eq dir 'left))))))
  8653. (defun org-rematch-and-replace (n &optional decr hline)
  8654. "Re-match the group N, and replace it with the shifted refrence."
  8655. (or (match-end n) (error "Cannot shift reference in this direction"))
  8656. (goto-char (match-beginning n))
  8657. (and (looking-at (regexp-quote (match-string n)))
  8658. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8659. t t)))
  8660. (defun org-shift-refpart (ref &optional decr hline)
  8661. "Shift a refrence part REF.
  8662. If DECR is set, decrease the references row/column, else increase.
  8663. If HLINE is set, this may be a hline reference, it certainly is not
  8664. a translation reference."
  8665. (save-match-data
  8666. (let* ((sign (string-match "^[-+]" ref)) n)
  8667. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8668. (cond
  8669. ((and hline (string-match "^I+" ref))
  8670. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8671. (setq n (+ n (if decr -1 1)))
  8672. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8673. (if sign
  8674. (setq sign (if (< n 0) "-" "+") n (abs n))
  8675. (setq n (max 1 n)))
  8676. (concat sign (make-string n ?I)))
  8677. ((string-match "^[0-9]+" ref)
  8678. (setq n (string-to-number (concat sign ref)))
  8679. (setq n (+ n (if decr -1 1)))
  8680. (if sign
  8681. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8682. (number-to-string (max 1 n))))
  8683. ((string-match "^[a-zA-Z]+" ref)
  8684. (org-number-to-letters
  8685. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8686. (t (error "Cannot shift reference"))))))
  8687. (defun org-table-fedit-toggle-coordinates ()
  8688. "Toggle the display of coordinates in the refrenced table."
  8689. (interactive)
  8690. (let ((pos (marker-position org-pos)))
  8691. (with-current-buffer (marker-buffer org-pos)
  8692. (save-excursion
  8693. (goto-char pos)
  8694. (org-table-toggle-coordinate-overlays)))))
  8695. (defun org-table-fedit-finish (&optional arg)
  8696. "Parse the buffer for formula definitions and install them.
  8697. With prefix ARG, apply the new formulas to the table."
  8698. (interactive "P")
  8699. (org-table-remove-rectangle-highlight)
  8700. (if org-table-use-standard-references
  8701. (progn
  8702. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8703. (setq org-table-buffer-is-an nil)))
  8704. (let ((pos org-pos) eql var form)
  8705. (goto-char (point-min))
  8706. (while (re-search-forward
  8707. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  8708. nil t)
  8709. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  8710. form (match-string 3))
  8711. (setq form (org-trim form))
  8712. (when (not (equal form ""))
  8713. (while (string-match "[ \t]*\n[ \t]*" form)
  8714. (setq form (replace-match " " t t form)))
  8715. (when (assoc var eql)
  8716. (error "Double formulas for %s" var))
  8717. (push (cons var form) eql)))
  8718. (setq org-pos nil)
  8719. (set-window-configuration org-window-configuration)
  8720. (select-window (get-buffer-window (marker-buffer pos)))
  8721. (goto-char pos)
  8722. (unless (org-at-table-p)
  8723. (error "Lost table position - cannot install formulae"))
  8724. (org-table-store-formulas eql)
  8725. (move-marker pos nil)
  8726. (kill-buffer "*Edit Formulas*")
  8727. (if arg
  8728. (org-table-recalculate 'all)
  8729. (message "New formulas installed - press C-u C-c C-c to apply."))))
  8730. (defun org-table-fedit-abort ()
  8731. "Abort editing formulas, without installing the changes."
  8732. (interactive)
  8733. (org-table-remove-rectangle-highlight)
  8734. (let ((pos org-pos))
  8735. (set-window-configuration org-window-configuration)
  8736. (select-window (get-buffer-window (marker-buffer pos)))
  8737. (goto-char pos)
  8738. (move-marker pos nil)
  8739. (message "Formula editing aborted without installing changes")))
  8740. (defun org-table-fedit-lisp-indent ()
  8741. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  8742. (interactive)
  8743. (let ((pos (point)) beg end ind)
  8744. (beginning-of-line 1)
  8745. (cond
  8746. ((looking-at "[ \t]")
  8747. (goto-char pos)
  8748. (call-interactively 'lisp-indent-line))
  8749. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  8750. ((not (fboundp 'pp-buffer))
  8751. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  8752. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  8753. (goto-char (- (match-end 0) 2))
  8754. (setq beg (point))
  8755. (setq ind (make-string (current-column) ?\ ))
  8756. (condition-case nil (forward-sexp 1)
  8757. (error
  8758. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  8759. (setq end (point))
  8760. (save-restriction
  8761. (narrow-to-region beg end)
  8762. (if (eq last-command this-command)
  8763. (progn
  8764. (goto-char (point-min))
  8765. (setq this-command nil)
  8766. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8767. (replace-match " ")))
  8768. (pp-buffer)
  8769. (untabify (point-min) (point-max))
  8770. (goto-char (1+ (point-min)))
  8771. (while (re-search-forward "^." nil t)
  8772. (beginning-of-line 1)
  8773. (insert ind))
  8774. (goto-char (point-max))
  8775. (backward-delete-char 1)))
  8776. (goto-char beg))
  8777. (t nil))))
  8778. (defvar org-show-positions nil)
  8779. (defun org-table-show-reference (&optional local)
  8780. "Show the location/value of the $ expression at point."
  8781. (interactive)
  8782. (org-table-remove-rectangle-highlight)
  8783. (catch 'exit
  8784. (let ((pos (if local (point) org-pos))
  8785. (face2 'highlight)
  8786. (org-inhibit-highlight-removal t)
  8787. (win (selected-window))
  8788. (org-show-positions nil)
  8789. var name e what match dest)
  8790. (if local (org-table-get-specials))
  8791. (setq what (cond
  8792. ((or (org-at-regexp-p org-table-range-regexp2)
  8793. (org-at-regexp-p org-table-translate-regexp)
  8794. (org-at-regexp-p org-table-range-regexp))
  8795. (setq match
  8796. (save-match-data
  8797. (org-table-convert-refs-to-rc (match-string 0))))
  8798. 'range)
  8799. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  8800. ((org-at-regexp-p "\\$[0-9]+") 'column)
  8801. ((not local) nil)
  8802. (t (error "No reference at point")))
  8803. match (and what (or match (match-string 0))))
  8804. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  8805. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  8806. 'secondary-selection))
  8807. (org-add-hook 'before-change-functions
  8808. 'org-table-remove-rectangle-highlight)
  8809. (if (eq what 'name) (setq var (substring match 1)))
  8810. (when (eq what 'range)
  8811. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  8812. (setq match (org-table-formula-substitute-names match)))
  8813. (unless local
  8814. (save-excursion
  8815. (end-of-line 1)
  8816. (re-search-backward "^\\S-" nil t)
  8817. (beginning-of-line 1)
  8818. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  8819. (setq dest
  8820. (save-match-data
  8821. (org-table-convert-refs-to-rc (match-string 1))))
  8822. (org-table-add-rectangle-overlay
  8823. (match-beginning 1) (match-end 1) face2))))
  8824. (if (and (markerp pos) (marker-buffer pos))
  8825. (if (get-buffer-window (marker-buffer pos))
  8826. (select-window (get-buffer-window (marker-buffer pos)))
  8827. (org-switch-to-buffer-other-window (get-buffer-window
  8828. (marker-buffer pos)))))
  8829. (goto-char pos)
  8830. (org-table-force-dataline)
  8831. (when dest
  8832. (setq name (substring dest 1))
  8833. (cond
  8834. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  8835. (setq e (assoc name org-table-named-field-locations))
  8836. (goto-line (nth 1 e))
  8837. (org-table-goto-column (nth 2 e)))
  8838. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  8839. (let ((l (string-to-number (match-string 1 dest)))
  8840. (c (string-to-number (match-string 2 dest))))
  8841. (goto-line (aref org-table-dlines l))
  8842. (org-table-goto-column c)))
  8843. (t (org-table-goto-column (string-to-number name))))
  8844. (move-marker pos (point))
  8845. (org-table-highlight-rectangle nil nil face2))
  8846. (cond
  8847. ((equal dest match))
  8848. ((not match))
  8849. ((eq what 'range)
  8850. (condition-case nil
  8851. (save-excursion
  8852. (org-table-get-range match nil nil 'highlight))
  8853. (error nil)))
  8854. ((setq e (assoc var org-table-named-field-locations))
  8855. (goto-line (nth 1 e))
  8856. (org-table-goto-column (nth 2 e))
  8857. (org-table-highlight-rectangle (point) (point))
  8858. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  8859. ((setq e (assoc var org-table-column-names))
  8860. (org-table-goto-column (string-to-number (cdr e)))
  8861. (org-table-highlight-rectangle (point) (point))
  8862. (goto-char (org-table-begin))
  8863. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  8864. (org-table-end) t)
  8865. (progn
  8866. (goto-char (match-beginning 1))
  8867. (org-table-highlight-rectangle)
  8868. (message "Named column (column %s)" (cdr e)))
  8869. (error "Column name not found")))
  8870. ((eq what 'column)
  8871. ;; column number
  8872. (org-table-goto-column (string-to-number (substring match 1)))
  8873. (org-table-highlight-rectangle (point) (point))
  8874. (message "Column %s" (substring match 1)))
  8875. ((setq e (assoc var org-table-local-parameters))
  8876. (goto-char (org-table-begin))
  8877. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  8878. (progn
  8879. (goto-char (match-beginning 1))
  8880. (org-table-highlight-rectangle)
  8881. (message "Local parameter."))
  8882. (error "Parameter not found")))
  8883. (t
  8884. (cond
  8885. ((not var) (error "No reference at point"))
  8886. ((setq e (assoc var org-table-formula-constants-local))
  8887. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  8888. var (cdr e)))
  8889. ((setq e (assoc var org-table-formula-constants))
  8890. (message "Constant: $%s=%s in `org-table-formula-constants'."
  8891. var (cdr e)))
  8892. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  8893. (message "Constant: $%s=%s, from `constants.el'%s."
  8894. var e (format " (%s units)" constants-unit-system)))
  8895. (t (error "Undefined name $%s" var)))))
  8896. (goto-char pos)
  8897. (when (and org-show-positions
  8898. (not (memq this-command '(org-table-fedit-scroll
  8899. org-table-fedit-scroll-down))))
  8900. (push pos org-show-positions)
  8901. (push org-table-current-begin-pos org-show-positions)
  8902. (let ((min (apply 'min org-show-positions))
  8903. (max (apply 'max org-show-positions)))
  8904. (goto-char min) (recenter 0)
  8905. (goto-char max)
  8906. (or (pos-visible-in-window-p max) (recenter -1))))
  8907. (select-window win))))
  8908. (defun org-table-force-dataline ()
  8909. "Make sure the cursor is in a dataline in a table."
  8910. (unless (save-excursion
  8911. (beginning-of-line 1)
  8912. (looking-at org-table-dataline-regexp))
  8913. (let* ((re org-table-dataline-regexp)
  8914. (p1 (save-excursion (re-search-forward re nil 'move)))
  8915. (p2 (save-excursion (re-search-backward re nil 'move))))
  8916. (cond ((and p1 p2)
  8917. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  8918. p1 p2)))
  8919. ((or p1 p2) (goto-char (or p1 p2)))
  8920. (t (error "No table dataline around here"))))))
  8921. (defun org-table-fedit-line-up ()
  8922. "Move cursor one line up in the window showing the table."
  8923. (interactive)
  8924. (org-table-fedit-move 'previous-line))
  8925. (defun org-table-fedit-line-down ()
  8926. "Move cursor one line down in the window showing the table."
  8927. (interactive)
  8928. (org-table-fedit-move 'next-line))
  8929. (defun org-table-fedit-move (command)
  8930. "Move the cursor in the window shoinw the table.
  8931. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  8932. (let ((org-table-allow-automatic-line-recalculation nil)
  8933. (pos org-pos) (win (selected-window)) p)
  8934. (select-window (get-buffer-window (marker-buffer org-pos)))
  8935. (setq p (point))
  8936. (call-interactively command)
  8937. (while (and (org-at-table-p)
  8938. (org-at-table-hline-p))
  8939. (call-interactively command))
  8940. (or (org-at-table-p) (goto-char p))
  8941. (move-marker pos (point))
  8942. (select-window win)))
  8943. (defun org-table-fedit-scroll (N)
  8944. (interactive "p")
  8945. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  8946. (scroll-other-window N)))
  8947. (defun org-table-fedit-scroll-down (N)
  8948. (interactive "p")
  8949. (org-table-fedit-scroll (- N)))
  8950. (defvar org-table-rectangle-overlays nil)
  8951. (defun org-table-add-rectangle-overlay (beg end &optional face)
  8952. "Add a new overlay."
  8953. (let ((ov (org-make-overlay beg end)))
  8954. (org-overlay-put ov 'face (or face 'secondary-selection))
  8955. (push ov org-table-rectangle-overlays)))
  8956. (defun org-table-highlight-rectangle (&optional beg end face)
  8957. "Highlight rectangular region in a table."
  8958. (setq beg (or beg (point)) end (or end (point)))
  8959. (let ((b (min beg end))
  8960. (e (max beg end))
  8961. l1 c1 l2 c2 tmp)
  8962. (and (boundp 'org-show-positions)
  8963. (setq org-show-positions (cons b (cons e org-show-positions))))
  8964. (goto-char (min beg end))
  8965. (setq l1 (org-current-line)
  8966. c1 (org-table-current-column))
  8967. (goto-char (max beg end))
  8968. (setq l2 (org-current-line)
  8969. c2 (org-table-current-column))
  8970. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  8971. (goto-line l1)
  8972. (beginning-of-line 1)
  8973. (loop for line from l1 to l2 do
  8974. (when (looking-at org-table-dataline-regexp)
  8975. (org-table-goto-column c1)
  8976. (skip-chars-backward "^|\n") (setq beg (point))
  8977. (org-table-goto-column c2)
  8978. (skip-chars-forward "^|\n") (setq end (point))
  8979. (org-table-add-rectangle-overlay beg end face))
  8980. (beginning-of-line 2))
  8981. (goto-char b))
  8982. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  8983. (defun org-table-remove-rectangle-highlight (&rest ignore)
  8984. "Remove the rectangle overlays."
  8985. (unless org-inhibit-highlight-removal
  8986. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  8987. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  8988. (setq org-table-rectangle-overlays nil)))
  8989. (defvar org-table-coordinate-overlays nil
  8990. "Collects the cooordinate grid overlays, so that they can be removed.")
  8991. (make-variable-buffer-local 'org-table-coordinate-overlays)
  8992. (defun org-table-overlay-coordinates ()
  8993. "Add overlays to the table at point, to show row/column coordinates."
  8994. (interactive)
  8995. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8996. (setq org-table-coordinate-overlays nil)
  8997. (save-excursion
  8998. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  8999. (goto-char (org-table-begin))
  9000. (while (org-at-table-p)
  9001. (setq eol (point-at-eol))
  9002. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9003. (push ov org-table-coordinate-overlays)
  9004. (setq hline (looking-at org-table-hline-regexp))
  9005. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9006. (format "%4d" (setq id (1+ id)))))
  9007. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9008. (when hline
  9009. (setq ic 0)
  9010. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9011. (setq beg (1+ (match-beginning 0))
  9012. ic (1+ ic)
  9013. s1 (concat "$" (int-to-string ic))
  9014. s2 (org-number-to-letters ic)
  9015. str (if (eq org-table-use-standard-references t) s2 s1))
  9016. (setq ov (org-make-overlay beg (+ beg (length str))))
  9017. (push ov org-table-coordinate-overlays)
  9018. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9019. (beginning-of-line 2)))))
  9020. (defun org-table-toggle-coordinate-overlays ()
  9021. "Toggle the display of Row/Column numbers in tables."
  9022. (interactive)
  9023. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9024. (message "Row/Column number display turned %s"
  9025. (if org-table-overlay-coordinates "on" "off"))
  9026. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9027. (org-table-align))
  9028. (unless org-table-overlay-coordinates
  9029. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9030. (setq org-table-coordinate-overlays nil)))
  9031. (defun org-table-toggle-formula-debugger ()
  9032. "Toggle the formula debugger in tables."
  9033. (interactive)
  9034. (setq org-table-formula-debug (not org-table-formula-debug))
  9035. (message "Formula debugging has been turned %s"
  9036. (if org-table-formula-debug "on" "off")))
  9037. ;;; The orgtbl minor mode
  9038. ;; Define a minor mode which can be used in other modes in order to
  9039. ;; integrate the org-mode table editor.
  9040. ;; This is really a hack, because the org-mode table editor uses several
  9041. ;; keys which normally belong to the major mode, for example the TAB and
  9042. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9043. ;; necessary to operate the table editor, but wraps the commands into a
  9044. ;; function which tests if the cursor is currently inside a table. If that
  9045. ;; is the case, the table editor command is executed. However, when any of
  9046. ;; those keys is used outside a table, the function uses `key-binding' to
  9047. ;; look up if the key has an associated command in another currently active
  9048. ;; keymap (minor modes, major mode, global), and executes that command.
  9049. ;; There might be problems if any of the keys used by the table editor is
  9050. ;; otherwise used as a prefix key.
  9051. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9052. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9053. ;; addresses this by checking explicitly for both bindings.
  9054. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9055. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9056. ;; Some modes bind other commands to simple characters, for example
  9057. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9058. ;; active, this binding is ignored inside tables and replaced with a
  9059. ;; modified self-insert.
  9060. (defvar orgtbl-mode nil
  9061. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9062. table editor in arbitrary modes.")
  9063. (make-variable-buffer-local 'orgtbl-mode)
  9064. (defvar orgtbl-mode-map (make-keymap)
  9065. "Keymap for `orgtbl-mode'.")
  9066. ;;;###autoload
  9067. (defun turn-on-orgtbl ()
  9068. "Unconditionally turn on `orgtbl-mode'."
  9069. (orgtbl-mode 1))
  9070. (defvar org-old-auto-fill-inhibit-regexp nil
  9071. "Local variable used by `orgtbl-mode'")
  9072. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  9073. "Matches a line belonging to an orgtbl.")
  9074. (defconst orgtbl-extra-font-lock-keywords
  9075. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  9076. 0 (quote 'org-table) 'prepend))
  9077. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  9078. ;;;###autoload
  9079. (defun orgtbl-mode (&optional arg)
  9080. "The `org-mode' table editor as a minor mode for use in other modes."
  9081. (interactive)
  9082. (if (org-mode-p)
  9083. ;; Exit without error, in case some hook functions calls this
  9084. ;; by accident in org-mode.
  9085. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9086. (setq orgtbl-mode
  9087. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9088. (if orgtbl-mode
  9089. (progn
  9090. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9091. ;; Make sure we are first in minor-mode-map-alist
  9092. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9093. (and c (setq minor-mode-map-alist
  9094. (cons c (delq c minor-mode-map-alist)))))
  9095. (org-set-local (quote org-table-may-need-update) t)
  9096. (org-add-hook 'before-change-functions 'org-before-change-function
  9097. nil 'local)
  9098. (org-set-local 'org-old-auto-fill-inhibit-regexp
  9099. auto-fill-inhibit-regexp)
  9100. (org-set-local 'auto-fill-inhibit-regexp
  9101. (if auto-fill-inhibit-regexp
  9102. (concat orgtbl-line-start-regexp "\\|"
  9103. auto-fill-inhibit-regexp)
  9104. orgtbl-line-start-regexp))
  9105. (org-add-to-invisibility-spec '(org-cwidth))
  9106. (when (fboundp 'font-lock-add-keywords)
  9107. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  9108. (org-restart-font-lock))
  9109. (easy-menu-add orgtbl-mode-menu)
  9110. (run-hooks 'orgtbl-mode-hook))
  9111. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9112. (org-cleanup-narrow-column-properties)
  9113. (org-remove-from-invisibility-spec '(org-cwidth))
  9114. (remove-hook 'before-change-functions 'org-before-change-function t)
  9115. (when (fboundp 'font-lock-remove-keywords)
  9116. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  9117. (org-restart-font-lock))
  9118. (easy-menu-remove orgtbl-mode-menu)
  9119. (force-mode-line-update 'all))))
  9120. (defun org-cleanup-narrow-column-properties ()
  9121. "Remove all properties related to narrow-column invisibility."
  9122. (let ((s 1))
  9123. (while (setq s (text-property-any s (point-max)
  9124. 'display org-narrow-column-arrow))
  9125. (remove-text-properties s (1+ s) '(display t)))
  9126. (setq s 1)
  9127. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9128. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9129. (setq s 1)
  9130. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9131. (remove-text-properties s (1+ s) '(invisible t)))))
  9132. ;; Install it as a minor mode.
  9133. (put 'orgtbl-mode :included t)
  9134. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9135. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9136. (defun orgtbl-make-binding (fun n &rest keys)
  9137. "Create a function for binding in the table minor mode.
  9138. FUN is the command to call inside a table. N is used to create a unique
  9139. command name. KEYS are keys that should be checked in for a command
  9140. to execute outside of tables."
  9141. (eval
  9142. (list 'defun
  9143. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9144. '(arg)
  9145. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9146. "Outside of tables, run the binding of `"
  9147. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9148. "'.")
  9149. '(interactive "p")
  9150. (list 'if
  9151. '(org-at-table-p)
  9152. (list 'call-interactively (list 'quote fun))
  9153. (list 'let '(orgtbl-mode)
  9154. (list 'call-interactively
  9155. (append '(or)
  9156. (mapcar (lambda (k)
  9157. (list 'key-binding k))
  9158. keys)
  9159. '('orgtbl-error))))))))
  9160. (defun orgtbl-error ()
  9161. "Error when there is no default binding for a table key."
  9162. (interactive)
  9163. (error "This key is has no function outside tables"))
  9164. (defun orgtbl-setup ()
  9165. "Setup orgtbl keymaps."
  9166. (let ((nfunc 0)
  9167. (bindings
  9168. (list
  9169. '([(meta shift left)] org-table-delete-column)
  9170. '([(meta left)] org-table-move-column-left)
  9171. '([(meta right)] org-table-move-column-right)
  9172. '([(meta shift right)] org-table-insert-column)
  9173. '([(meta shift up)] org-table-kill-row)
  9174. '([(meta shift down)] org-table-insert-row)
  9175. '([(meta up)] org-table-move-row-up)
  9176. '([(meta down)] org-table-move-row-down)
  9177. '("\C-c\C-w" org-table-cut-region)
  9178. '("\C-c\M-w" org-table-copy-region)
  9179. '("\C-c\C-y" org-table-paste-rectangle)
  9180. '("\C-c-" org-table-insert-hline)
  9181. '("\C-c}" org-table-toggle-coordinate-overlays)
  9182. '("\C-c{" org-table-toggle-formula-debugger)
  9183. '("\C-m" org-table-next-row)
  9184. '([(shift return)] org-table-copy-down)
  9185. '("\C-c\C-q" org-table-wrap-region)
  9186. '("\C-c?" org-table-field-info)
  9187. '("\C-c " org-table-blank-field)
  9188. '("\C-c+" org-table-sum)
  9189. '("\C-c=" org-table-eval-formula)
  9190. '("\C-c'" org-table-edit-formulas)
  9191. '("\C-c`" org-table-edit-field)
  9192. '("\C-c*" org-table-recalculate)
  9193. '("\C-c|" org-table-create-or-convert-from-region)
  9194. '("\C-c^" org-table-sort-lines)
  9195. '([(control ?#)] org-table-rotate-recalc-marks)))
  9196. elt key fun cmd)
  9197. (while (setq elt (pop bindings))
  9198. (setq nfunc (1+ nfunc))
  9199. (setq key (org-key (car elt))
  9200. fun (nth 1 elt)
  9201. cmd (orgtbl-make-binding fun nfunc key))
  9202. (org-defkey orgtbl-mode-map key cmd))
  9203. ;; Special treatment needed for TAB and RET
  9204. (org-defkey orgtbl-mode-map [(return)]
  9205. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9206. (org-defkey orgtbl-mode-map "\C-m"
  9207. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9208. (org-defkey orgtbl-mode-map [(tab)]
  9209. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9210. (org-defkey orgtbl-mode-map "\C-i"
  9211. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  9212. (org-defkey orgtbl-mode-map [(shift tab)]
  9213. (orgtbl-make-binding 'org-table-previous-field 104
  9214. [(shift tab)] [(tab)] "\C-i"))
  9215. (org-defkey orgtbl-mode-map "\M-\C-m"
  9216. (orgtbl-make-binding 'org-table-wrap-region 105
  9217. "\M-\C-m" [(meta return)]))
  9218. (org-defkey orgtbl-mode-map [(meta return)]
  9219. (orgtbl-make-binding 'org-table-wrap-region 106
  9220. [(meta return)] "\M-\C-m"))
  9221. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  9222. (when orgtbl-optimized
  9223. ;; If the user wants maximum table support, we need to hijack
  9224. ;; some standard editing functions
  9225. (org-remap orgtbl-mode-map
  9226. 'self-insert-command 'orgtbl-self-insert-command
  9227. 'delete-char 'org-delete-char
  9228. 'delete-backward-char 'org-delete-backward-char)
  9229. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  9230. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9231. '("OrgTbl"
  9232. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9233. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9234. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9235. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9236. "--"
  9237. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9238. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9239. ["Copy Field from Above"
  9240. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9241. "--"
  9242. ("Column"
  9243. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9244. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9245. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9246. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9247. ("Row"
  9248. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9249. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9250. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9251. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9252. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9253. "--"
  9254. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9255. ("Rectangle"
  9256. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9257. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9258. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9259. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9260. "--"
  9261. ("Radio tables"
  9262. ["Insert table template" orgtbl-insert-radio-table
  9263. (assq major-mode orgtbl-radio-table-templates)]
  9264. ["Comment/uncomment table" orgtbl-toggle-comment t])
  9265. "--"
  9266. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9267. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9268. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9269. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9270. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9271. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9272. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9273. ["Sum Column/Rectangle" org-table-sum
  9274. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9275. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9276. ["Debug Formulas"
  9277. org-table-toggle-formula-debugger :active (org-at-table-p)
  9278. :keys "C-c {"
  9279. :style toggle :selected org-table-formula-debug]
  9280. ["Show Col/Row Numbers"
  9281. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9282. :keys "C-c }"
  9283. :style toggle :selected org-table-overlay-coordinates]
  9284. ))
  9285. t))
  9286. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9287. "If the cursor is inside a table, realign the table.
  9288. It it is a table to be sent away to a receiver, do it.
  9289. With prefix arg, also recompute table."
  9290. (interactive "P")
  9291. (let ((pos (point)) action)
  9292. (save-excursion
  9293. (beginning-of-line 1)
  9294. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9295. ((looking-at "[ \t]*|") pos)
  9296. ((looking-at "#\\+TBLFM:") 'recalc))))
  9297. (cond
  9298. ((integerp action)
  9299. (goto-char action)
  9300. (org-table-maybe-eval-formula)
  9301. (if arg
  9302. (call-interactively 'org-table-recalculate)
  9303. (org-table-maybe-recalculate-line))
  9304. (call-interactively 'org-table-align)
  9305. (orgtbl-send-table 'maybe))
  9306. ((eq action 'recalc)
  9307. (save-excursion
  9308. (beginning-of-line 1)
  9309. (skip-chars-backward " \r\n\t")
  9310. (if (org-at-table-p)
  9311. (org-call-with-arg 'org-table-recalculate t))))
  9312. (t (let (orgtbl-mode)
  9313. (call-interactively (key-binding "\C-c\C-c")))))))
  9314. (defun orgtbl-tab (arg)
  9315. "Justification and field motion for `orgtbl-mode'."
  9316. (interactive "P")
  9317. (if arg (org-table-edit-field t)
  9318. (org-table-justify-field-maybe)
  9319. (org-table-next-field)))
  9320. (defun orgtbl-ret ()
  9321. "Justification and field motion for `orgtbl-mode'."
  9322. (interactive)
  9323. (org-table-justify-field-maybe)
  9324. (org-table-next-row))
  9325. (defun orgtbl-self-insert-command (N)
  9326. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9327. If the cursor is in a table looking at whitespace, the whitespace is
  9328. overwritten, and the table is not marked as requiring realignment."
  9329. (interactive "p")
  9330. (if (and (org-at-table-p)
  9331. (or
  9332. (and org-table-auto-blank-field
  9333. (member last-command
  9334. '(orgtbl-hijacker-command-100
  9335. orgtbl-hijacker-command-101
  9336. orgtbl-hijacker-command-102
  9337. orgtbl-hijacker-command-103
  9338. orgtbl-hijacker-command-104
  9339. orgtbl-hijacker-command-105))
  9340. (org-table-blank-field))
  9341. t)
  9342. (eq N 1)
  9343. (looking-at "[^|\n]* +|"))
  9344. (let (org-table-may-need-update)
  9345. (goto-char (1- (match-end 0)))
  9346. (delete-backward-char 1)
  9347. (goto-char (match-beginning 0))
  9348. (self-insert-command N))
  9349. (setq org-table-may-need-update t)
  9350. (let (orgtbl-mode)
  9351. (call-interactively (key-binding (vector last-input-event))))))
  9352. (defun org-force-self-insert (N)
  9353. "Needed to enforce self-insert under remapping."
  9354. (interactive "p")
  9355. (self-insert-command N))
  9356. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9357. "Regula expression matching exponentials as produced by calc.")
  9358. (defvar org-table-clean-did-remove-column nil)
  9359. (defun orgtbl-export (table target)
  9360. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9361. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9362. org-table-last-alignment org-table-last-column-widths
  9363. maxcol column)
  9364. (if (not (fboundp func))
  9365. (error "Cannot export orgtbl table to %s" target))
  9366. (setq lines (org-table-clean-before-export lines))
  9367. (setq table
  9368. (mapcar
  9369. (lambda (x)
  9370. (if (string-match org-table-hline-regexp x)
  9371. 'hline
  9372. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9373. lines))
  9374. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9375. table)))
  9376. (loop for i from (1- maxcol) downto 0 do
  9377. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9378. (setq column (delq nil column))
  9379. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9380. (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))
  9381. (funcall func table nil)))
  9382. (defun orgtbl-send-table (&optional maybe)
  9383. "Send a tranformed version of this table to the receiver position.
  9384. With argument MAYBE, fail quietly if no transformation is defined for
  9385. this table."
  9386. (interactive)
  9387. (catch 'exit
  9388. (unless (org-at-table-p) (error "Not at a table"))
  9389. ;; when non-interactive, we assume align has just happened.
  9390. (when (interactive-p) (org-table-align))
  9391. (save-excursion
  9392. (goto-char (org-table-begin))
  9393. (beginning-of-line 0)
  9394. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9395. (if maybe
  9396. (throw 'exit nil)
  9397. (error "Don't know how to transform this table."))))
  9398. (let* ((name (match-string 1))
  9399. beg
  9400. (transform (intern (match-string 2)))
  9401. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9402. (skip (plist-get params :skip))
  9403. (skipcols (plist-get params :skipcols))
  9404. (txt (buffer-substring-no-properties
  9405. (org-table-begin) (org-table-end)))
  9406. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9407. (lines (org-table-clean-before-export lines))
  9408. (i0 (if org-table-clean-did-remove-column 2 1))
  9409. (table (mapcar
  9410. (lambda (x)
  9411. (if (string-match org-table-hline-regexp x)
  9412. 'hline
  9413. (org-remove-by-index
  9414. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9415. skipcols i0)))
  9416. lines))
  9417. (fun (if (= i0 2) 'cdr 'identity))
  9418. (org-table-last-alignment
  9419. (org-remove-by-index (funcall fun org-table-last-alignment)
  9420. skipcols i0))
  9421. (org-table-last-column-widths
  9422. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9423. skipcols i0)))
  9424. (unless (fboundp transform)
  9425. (error "No such transformation function %s" transform))
  9426. (setq txt (funcall transform table params))
  9427. ;; Find the insertion place
  9428. (save-excursion
  9429. (goto-char (point-min))
  9430. (unless (re-search-forward
  9431. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9432. (error "Don't know where to insert translated table"))
  9433. (goto-char (match-beginning 0))
  9434. (beginning-of-line 2)
  9435. (setq beg (point))
  9436. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9437. (error "Cannot find end of insertion region"))
  9438. (beginning-of-line 1)
  9439. (delete-region beg (point))
  9440. (goto-char beg)
  9441. (insert txt "\n"))
  9442. (message "Table converted and installed at receiver location"))))
  9443. (defun org-remove-by-index (list indices &optional i0)
  9444. "Remove the elements in LIST with indices in INDICES.
  9445. First element has index 0, or I0 if given."
  9446. (if (not indices)
  9447. list
  9448. (if (integerp indices) (setq indices (list indices)))
  9449. (setq i0 (1- (or i0 0)))
  9450. (delq :rm (mapcar (lambda (x)
  9451. (setq i0 (1+ i0))
  9452. (if (memq i0 indices) :rm x))
  9453. list))))
  9454. (defun orgtbl-toggle-comment ()
  9455. "Comment or uncomment the orgtbl at point."
  9456. (interactive)
  9457. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9458. (re2 (concat "^" orgtbl-line-start-regexp))
  9459. (commented (save-excursion (beginning-of-line 1)
  9460. (cond ((looking-at re1) t)
  9461. ((looking-at re2) nil)
  9462. (t (error "Not at an org table")))))
  9463. (re (if commented re1 re2))
  9464. beg end)
  9465. (save-excursion
  9466. (beginning-of-line 1)
  9467. (while (looking-at re) (beginning-of-line 0))
  9468. (beginning-of-line 2)
  9469. (setq beg (point))
  9470. (while (looking-at re) (beginning-of-line 2))
  9471. (setq end (point)))
  9472. (comment-region beg end (if commented '(4) nil))))
  9473. (defun orgtbl-insert-radio-table ()
  9474. "Insert a radio table template appropriate for this major mode."
  9475. (interactive)
  9476. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9477. (txt (nth 1 e))
  9478. name pos)
  9479. (unless e (error "No radio table setup defined for %s" major-mode))
  9480. (setq name (read-string "Table name: "))
  9481. (while (string-match "%n" txt)
  9482. (setq txt (replace-match name t t txt)))
  9483. (or (bolp) (insert "\n"))
  9484. (setq pos (point))
  9485. (insert txt)
  9486. (goto-char pos)))
  9487. (defun org-get-param (params header i sym &optional hsym)
  9488. "Get parameter value for symbol SYM.
  9489. If this is a header line, actually get the value for the symbol with an
  9490. additional \"h\" inserted after the colon.
  9491. If the value is a protperty list, get the element for the current column.
  9492. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9493. (let ((val (plist-get params sym)))
  9494. (and hsym header (setq val (or (plist-get params hsym) val)))
  9495. (if (consp val) (plist-get val i) val)))
  9496. (defun orgtbl-to-generic (table params)
  9497. "Convert the orgtbl-mode TABLE to some other format.
  9498. This generic routine can be used for many standard cases.
  9499. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9500. separator line, or a list of fields for that line.
  9501. PARAMS is a property list of parameters that can influence the conversion.
  9502. For the generic converter, some parameters are obligatory: You need to
  9503. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9504. :splice, you must have :tstart and :tend.
  9505. Valid parameters are
  9506. :tstart String to start the table. Ignored when :splice is t.
  9507. :tend String to end the table. Ignored when :splice is t.
  9508. :splice When set to t, return only table body lines, don't wrap
  9509. them into :tstart and :tend. Default is nil.
  9510. :hline String to be inserted on horizontal separation lines.
  9511. May be nil to ignore hlines.
  9512. :lstart String to start a new table line.
  9513. :lend String to end a table line
  9514. :sep Separator between two fields
  9515. :lfmt Format for entire line, with enough %s to capture all fields.
  9516. If this is present, :lstart, :lend, and :sep are ignored.
  9517. :fmt A format to be used to wrap the field, should contain
  9518. %s for the original field value. For example, to wrap
  9519. everything in dollars, you could use :fmt \"$%s$\".
  9520. This may also be a property list with column numbers and
  9521. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9522. :hlstart :hlend :hlsep :hlfmt :hfmt
  9523. Same as above, specific for the header lines in the table.
  9524. All lines before the first hline are treated as header.
  9525. If any of these is not present, the data line value is used.
  9526. :efmt Use this format to print numbers with exponentials.
  9527. The format should have %s twice for inserting mantissa
  9528. and exponent, for example \"%s\\\\times10^{%s}\". This
  9529. may also be a property list with column numbers and
  9530. formats. :fmt will still be applied after :efmt.
  9531. In addition to this, the parameters :skip and :skipcols are always handled
  9532. directly by `orgtbl-send-table'. See manual."
  9533. (interactive)
  9534. (let* ((p params)
  9535. (splicep (plist-get p :splice))
  9536. (hline (plist-get p :hline))
  9537. rtn line i fm efm lfmt h)
  9538. ;; Do we have a header?
  9539. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  9540. (setq h t))
  9541. ;; Put header
  9542. (unless splicep
  9543. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  9544. ;; Now loop over all lines
  9545. (while (setq line (pop table))
  9546. (if (eq line 'hline)
  9547. ;; A horizontal separator line
  9548. (progn (if hline (push hline rtn))
  9549. (setq h nil)) ; no longer in header
  9550. ;; A normal line. Convert the fields, push line onto the result list
  9551. (setq i 0)
  9552. (setq line
  9553. (mapcar
  9554. (lambda (f)
  9555. (setq i (1+ i)
  9556. fm (org-get-param p h i :fmt :hfmt)
  9557. efm (org-get-param p h i :efmt))
  9558. (if (and efm (string-match orgtbl-exp-regexp f))
  9559. (setq f (format
  9560. efm (match-string 1 f) (match-string 2 f))))
  9561. (if fm (setq f (format fm f)))
  9562. f)
  9563. line))
  9564. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  9565. (push (apply 'format lfmt line) rtn)
  9566. (push (concat
  9567. (org-get-param p h i :lstart :hlstart)
  9568. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  9569. (org-get-param p h i :lend :hlend))
  9570. rtn))))
  9571. (unless splicep
  9572. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  9573. (mapconcat 'identity (nreverse rtn) "\n")))
  9574. (defun orgtbl-to-latex (table params)
  9575. "Convert the orgtbl-mode TABLE to LaTeX.
  9576. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9577. separator line, or a list of fields for that line.
  9578. PARAMS is a property list of parameters that can influence the conversion.
  9579. Supports all parameters from `orgtbl-to-generic'. Most important for
  9580. LaTeX are:
  9581. :splice When set to t, return only table body lines, don't wrap
  9582. them into a tabular environment. Default is nil.
  9583. :fmt A format to be used to wrap the field, should contain %s for the
  9584. original field value. For example, to wrap everything in dollars,
  9585. use :fmt \"$%s$\". This may also be a property list with column
  9586. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9587. :efmt Format for transforming numbers with exponentials. The format
  9588. should have %s twice for inserting mantissa and exponent, for
  9589. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  9590. This may also be a property list with column numbers and formats.
  9591. The general parameters :skip and :skipcols have already been applied when
  9592. this function is called."
  9593. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  9594. org-table-last-alignment ""))
  9595. (params2
  9596. (list
  9597. :tstart (concat "\\begin{tabular}{" alignment "}")
  9598. :tend "\\end{tabular}"
  9599. :lstart "" :lend " \\\\" :sep " & "
  9600. :efmt "%s\\,(%s)" :hline "\\hline")))
  9601. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9602. (defun orgtbl-to-html (table params)
  9603. "Convert the orgtbl-mode TABLE to LaTeX.
  9604. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9605. separator line, or a list of fields for that line.
  9606. PARAMS is a property list of parameters that can influence the conversion.
  9607. Currently this function recognizes the following parameters:
  9608. :splice When set to t, return only table body lines, don't wrap
  9609. them into a <table> environment. Default is nil.
  9610. The general parameters :skip and :skipcols have already been applied when
  9611. this function is called. The function does *not* use `orgtbl-to-generic',
  9612. so you cannot specify parameters for it."
  9613. (let* ((splicep (plist-get params :splice))
  9614. html)
  9615. ;; Just call the formatter we already have
  9616. ;; We need to make text lines for it, so put the fields back together.
  9617. (setq html (org-format-org-table-html
  9618. (mapcar
  9619. (lambda (x)
  9620. (if (eq x 'hline)
  9621. "|----+----|"
  9622. (concat "| " (mapconcat 'identity x " | ") " |")))
  9623. table)
  9624. splicep))
  9625. (if (string-match "\n+\\'" html)
  9626. (setq html (replace-match "" t t html)))
  9627. html))
  9628. (defun orgtbl-to-texinfo (table params)
  9629. "Convert the orgtbl-mode TABLE to TeXInfo.
  9630. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9631. separator line, or a list of fields for that line.
  9632. PARAMS is a property list of parameters that can influence the conversion.
  9633. Supports all parameters from `orgtbl-to-generic'. Most important for
  9634. TeXInfo are:
  9635. :splice nil/t When set to t, return only table body lines, don't wrap
  9636. them into a multitable environment. Default is nil.
  9637. :fmt fmt A format to be used to wrap the field, should contain
  9638. %s for the original field value. For example, to wrap
  9639. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  9640. This may also be a property list with column numbers and
  9641. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  9642. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  9643. are computed automatically from the width of the columns
  9644. under org-mode.
  9645. The general parameters :skip and :skipcols have already been applied when
  9646. this function is called."
  9647. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9648. (colfrac (or (plist-get params :cf)
  9649. (mapconcat
  9650. (lambda (x) (format "%.3f" (/ (float x) total)))
  9651. org-table-last-column-widths " ")))
  9652. (params2
  9653. (list
  9654. :tstart (concat "@multitable @columnfractions " colfrac)
  9655. :tend "@end multitable"
  9656. :lstart "@item " :lend "" :sep " @tab "
  9657. :hlstart "@headitem ")))
  9658. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9659. ;;;; Link Stuff
  9660. ;;; Link abbreviations
  9661. (defun org-link-expand-abbrev (link)
  9662. "Apply replacements as defined in `org-link-abbrev-alist."
  9663. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  9664. (let* ((key (match-string 1 link))
  9665. (as (or (assoc key org-link-abbrev-alist-local)
  9666. (assoc key org-link-abbrev-alist)))
  9667. (tag (and (match-end 2) (match-string 3 link)))
  9668. rpl)
  9669. (if (not as)
  9670. link
  9671. (setq rpl (cdr as))
  9672. (cond
  9673. ((symbolp rpl) (funcall rpl tag))
  9674. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9675. (t (concat rpl tag)))))
  9676. link))
  9677. ;;; Storing and inserting links
  9678. (defvar org-insert-link-history nil
  9679. "Minibuffer history for links inserted with `org-insert-link'.")
  9680. (defvar org-stored-links nil
  9681. "Contains the links stored with `org-store-link'.")
  9682. (defvar org-store-link-plist nil
  9683. "Plist with info about the most recently link created with `org-store-link'.")
  9684. (defvar org-link-protocols nil
  9685. "Link protocols added to Org-mode using `org-add-link-type'.")
  9686. (defvar org-store-link-functions nil
  9687. "List of functions that are called to create and store a link.
  9688. Each function will be called in turn until one returns a non-nil
  9689. value. Each function should check if it is responsible for creating
  9690. this link (for example by looking at the major mode).
  9691. If not, it must exit and return nil.
  9692. If yes, it should return a non-nil value after a calling
  9693. `org-store-link-properties' with a list of properties and values.
  9694. Special properties are:
  9695. :type The link prefix. like \"http\". This must be given.
  9696. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  9697. This is obligatory as well.
  9698. :description Optional default description for the second pair
  9699. of brackets in an Org-mode link. The user can still change
  9700. this when inserting this link into an Org-mode buffer.
  9701. In addition to these, any additional properties can be specified
  9702. and then used in remember templates.")
  9703. (defun org-add-link-type (type &optional follow publish)
  9704. "Add TYPE to the list of `org-link-types'.
  9705. Re-compute all regular expressions depending on `org-link-types'
  9706. FOLLOW and PUBLISH are two functions. Both take the link path as
  9707. an argument.
  9708. FOLLOW should do whatever is necessary to follow the link, for example
  9709. to find a file or display a mail message.
  9710. PUBLISH takes the path and retuns the string that should be used when
  9711. this document is published."
  9712. (add-to-list 'org-link-types type t)
  9713. (org-make-link-regexps)
  9714. (add-to-list 'org-link-protocols
  9715. (list type follow publish)))
  9716. (defun org-add-agenda-custom-command (entry)
  9717. "Replace or add a command in `org-agenda-custom-commands'.
  9718. This is mostly for hacking and trying a new command - once the command
  9719. works you probably want to add it to `org-agenda-custom-commands' for good."
  9720. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  9721. (if ass
  9722. (setcdr ass (cdr entry))
  9723. (push entry org-agenda-custom-commands))))
  9724. ;;;###autoload
  9725. (defun org-store-link (arg)
  9726. "\\<org-mode-map>Store an org-link to the current location.
  9727. This link can later be inserted into an org-buffer with
  9728. \\[org-insert-link].
  9729. For some link types, a prefix arg is interpreted:
  9730. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9731. For file links, arg negates `org-context-in-file-links'."
  9732. (interactive "P")
  9733. (setq org-store-link-plist nil) ; reset
  9734. (let (link cpltxt desc description search txt)
  9735. (cond
  9736. ((run-hook-with-args-until-success 'org-store-link-functions)
  9737. (setq link (plist-get org-store-link-plist :link)
  9738. desc (or (plist-get org-store-link-plist :description) link)))
  9739. ((eq major-mode 'bbdb-mode)
  9740. (let ((name (bbdb-record-name (bbdb-current-record)))
  9741. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  9742. (setq cpltxt (concat "bbdb:" (or name company))
  9743. link (org-make-link cpltxt))
  9744. (org-store-link-props :type "bbdb" :name name :company company)))
  9745. ((eq major-mode 'Info-mode)
  9746. (setq link (org-make-link "info:"
  9747. (file-name-nondirectory Info-current-file)
  9748. ":" Info-current-node))
  9749. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9750. ":" Info-current-node))
  9751. (org-store-link-props :type "info" :file Info-current-file
  9752. :node Info-current-node))
  9753. ((eq major-mode 'calendar-mode)
  9754. (let ((cd (calendar-cursor-to-date)))
  9755. (setq link
  9756. (format-time-string
  9757. (car org-time-stamp-formats)
  9758. (apply 'encode-time
  9759. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9760. nil nil nil))))
  9761. (org-store-link-props :type "calendar" :date cd)))
  9762. ((or (eq major-mode 'vm-summary-mode)
  9763. (eq major-mode 'vm-presentation-mode))
  9764. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9765. (vm-follow-summary-cursor)
  9766. (save-excursion
  9767. (vm-select-folder-buffer)
  9768. (let* ((message (car vm-message-pointer))
  9769. (folder buffer-file-name)
  9770. (subject (vm-su-subject message))
  9771. (to (vm-get-header-contents message "To"))
  9772. (from (vm-get-header-contents message "From"))
  9773. (message-id (vm-su-message-id message)))
  9774. (org-store-link-props :type "vm" :from from :to to :subject subject
  9775. :message-id message-id)
  9776. (setq message-id (org-remove-angle-brackets message-id))
  9777. (setq folder (abbreviate-file-name folder))
  9778. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9779. folder)
  9780. (setq folder (replace-match "" t t folder)))
  9781. (setq cpltxt (org-email-link-description))
  9782. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9783. ((eq major-mode 'wl-summary-mode)
  9784. (let* ((msgnum (wl-summary-message-number))
  9785. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9786. msgnum 'message-id))
  9787. (wl-message-entity
  9788. (if (fboundp 'elmo-message-entity)
  9789. (elmo-message-entity
  9790. wl-summary-buffer-elmo-folder msgnum)
  9791. (elmo-msgdb-overview-get-entity
  9792. msgnum (wl-summary-buffer-msgdb))))
  9793. (from (wl-summary-line-from))
  9794. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  9795. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  9796. (wl-summary-line-subject))))
  9797. (org-store-link-props :type "wl" :from from :to to
  9798. :subject subject :message-id message-id)
  9799. (setq message-id (org-remove-angle-brackets message-id))
  9800. (setq cpltxt (org-email-link-description))
  9801. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9802. "#" message-id))))
  9803. ((or (equal major-mode 'mh-folder-mode)
  9804. (equal major-mode 'mh-show-mode))
  9805. (let ((from (org-mhe-get-header "From:"))
  9806. (to (org-mhe-get-header "To:"))
  9807. (message-id (org-mhe-get-header "Message-Id:"))
  9808. (subject (org-mhe-get-header "Subject:")))
  9809. (org-store-link-props :type "mh" :from from :to to
  9810. :subject subject :message-id message-id)
  9811. (setq cpltxt (org-email-link-description))
  9812. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9813. (org-remove-angle-brackets message-id)))))
  9814. ((eq major-mode 'rmail-mode)
  9815. (save-excursion
  9816. (save-restriction
  9817. (rmail-narrow-to-non-pruned-header)
  9818. (let ((folder buffer-file-name)
  9819. (message-id (mail-fetch-field "message-id"))
  9820. (from (mail-fetch-field "from"))
  9821. (to (mail-fetch-field "to"))
  9822. (subject (mail-fetch-field "subject")))
  9823. (org-store-link-props
  9824. :type "rmail" :from from :to to
  9825. :subject subject :message-id message-id)
  9826. (setq message-id (org-remove-angle-brackets message-id))
  9827. (setq cpltxt (org-email-link-description))
  9828. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9829. ((eq major-mode 'gnus-group-mode)
  9830. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9831. (gnus-group-group-name)) ; version
  9832. ((fboundp 'gnus-group-name)
  9833. (gnus-group-name))
  9834. (t "???"))))
  9835. (unless group (error "Not on a group"))
  9836. (org-store-link-props :type "gnus" :group group)
  9837. (setq cpltxt (concat
  9838. (if (org-xor arg org-usenet-links-prefer-google)
  9839. "http://groups.google.com/groups?group="
  9840. "gnus:")
  9841. group)
  9842. link (org-make-link cpltxt))))
  9843. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9844. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9845. (let* ((group gnus-newsgroup-name)
  9846. (article (gnus-summary-article-number))
  9847. (header (gnus-summary-article-header article))
  9848. (from (mail-header-from header))
  9849. (message-id (mail-header-id header))
  9850. (date (mail-header-date header))
  9851. (subject (gnus-summary-subject-string)))
  9852. (org-store-link-props :type "gnus" :from from :subject subject
  9853. :message-id message-id :group group)
  9854. (setq cpltxt (org-email-link-description))
  9855. (if (org-xor arg org-usenet-links-prefer-google)
  9856. (setq link
  9857. (concat
  9858. cpltxt "\n "
  9859. (format "http://groups.google.com/groups?as_umsgid=%s"
  9860. (org-fixup-message-id-for-http message-id))))
  9861. (setq link (org-make-link "gnus:" group
  9862. "#" (number-to-string article))))))
  9863. ((eq major-mode 'w3-mode)
  9864. (setq cpltxt (url-view-url t)
  9865. link (org-make-link cpltxt))
  9866. (org-store-link-props :type "w3" :url (url-view-url t)))
  9867. ((eq major-mode 'w3m-mode)
  9868. (setq cpltxt (or w3m-current-title w3m-current-url)
  9869. link (org-make-link w3m-current-url))
  9870. (org-store-link-props :type "w3m" :url (url-view-url t)))
  9871. ((setq search (run-hook-with-args-until-success
  9872. 'org-create-file-search-functions))
  9873. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9874. "::" search))
  9875. (setq cpltxt (or description link)))
  9876. ((eq major-mode 'image-mode)
  9877. (setq cpltxt (concat "file:"
  9878. (abbreviate-file-name buffer-file-name))
  9879. link (org-make-link cpltxt))
  9880. (org-store-link-props :type "image" :file buffer-file-name))
  9881. ((eq major-mode 'dired-mode)
  9882. ;; link to the file in the current line
  9883. (setq cpltxt (concat "file:"
  9884. (abbreviate-file-name
  9885. (expand-file-name
  9886. (dired-get-filename nil t))))
  9887. link (org-make-link cpltxt)))
  9888. ((and buffer-file-name (org-mode-p))
  9889. ;; Just link to current headline
  9890. (setq cpltxt (concat "file:"
  9891. (abbreviate-file-name buffer-file-name)))
  9892. ;; Add a context search string
  9893. (when (org-xor org-context-in-file-links arg)
  9894. ;; Check if we are on a target
  9895. (if (org-in-regexp "<<\\(.*?\\)>>")
  9896. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9897. (setq txt (cond
  9898. ((org-on-heading-p) nil)
  9899. ((org-region-active-p)
  9900. (buffer-substring (region-beginning) (region-end)))
  9901. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9902. (when (or (null txt) (string-match "\\S-" txt))
  9903. (setq cpltxt
  9904. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9905. desc "NONE"))))
  9906. (if (string-match "::\\'" cpltxt)
  9907. (setq cpltxt (substring cpltxt 0 -2)))
  9908. (setq link (org-make-link cpltxt)))
  9909. (buffer-file-name
  9910. ;; Just link to this file here.
  9911. (setq cpltxt (concat "file:"
  9912. (abbreviate-file-name buffer-file-name)))
  9913. ;; Add a context string
  9914. (when (org-xor org-context-in-file-links arg)
  9915. (setq txt (if (org-region-active-p)
  9916. (buffer-substring (region-beginning) (region-end))
  9917. (buffer-substring (point-at-bol) (point-at-eol))))
  9918. ;; Only use search option if there is some text.
  9919. (when (string-match "\\S-" txt)
  9920. (setq cpltxt
  9921. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9922. desc "NONE")))
  9923. (setq link (org-make-link cpltxt)))
  9924. ((interactive-p)
  9925. (error "Cannot link to a buffer which is not visiting a file"))
  9926. (t (setq link nil)))
  9927. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9928. (setq link (or link cpltxt)
  9929. desc (or desc cpltxt))
  9930. (if (equal desc "NONE") (setq desc nil))
  9931. (if (and (interactive-p) link)
  9932. (progn
  9933. (setq org-stored-links
  9934. (cons (list cpltxt link desc) org-stored-links))
  9935. (message "Stored: %s" (or cpltxt link)))
  9936. (and link (org-make-link-string link desc)))))
  9937. (defun org-store-link-props (&rest plist)
  9938. "Store link properties, extract names and addresses."
  9939. (let (x adr)
  9940. (when (setq x (plist-get plist :from))
  9941. (setq adr (mail-extract-address-components x))
  9942. (plist-put plist :fromname (car adr))
  9943. (plist-put plist :fromaddress (nth 1 adr)))
  9944. (when (setq x (plist-get plist :to))
  9945. (setq adr (mail-extract-address-components x))
  9946. (plist-put plist :toname (car adr))
  9947. (plist-put plist :toaddress (nth 1 adr))))
  9948. (let ((from (plist-get plist :from))
  9949. (to (plist-get plist :to)))
  9950. (when (and from to org-from-is-user-regexp)
  9951. (plist-put plist :fromto
  9952. (if (string-match org-from-is-user-regexp from)
  9953. (concat "to %t")
  9954. (concat "from %f")))))
  9955. (setq org-store-link-plist plist))
  9956. (defun org-email-link-description (&optional fmt)
  9957. "Return the description part of an email link.
  9958. This takes information from `org-store-link-plist' and formats it
  9959. according to FMT (default from `org-email-link-description-format')."
  9960. (setq fmt (or fmt org-email-link-description-format))
  9961. (let* ((p org-store-link-plist)
  9962. (to (plist-get p :toaddress))
  9963. (from (plist-get p :fromaddress))
  9964. (table
  9965. (list
  9966. (cons "%c" (plist-get p :fromto))
  9967. (cons "%F" (plist-get p :from))
  9968. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9969. (cons "%T" (plist-get p :to))
  9970. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9971. (cons "%s" (plist-get p :subject))
  9972. (cons "%m" (plist-get p :message-id)))))
  9973. (when (string-match "%c" fmt)
  9974. ;; Check if the user wrote this message
  9975. (if (and org-from-is-user-regexp from to
  9976. (save-match-data (string-match org-from-is-user-regexp from)))
  9977. (setq fmt (replace-match "to %t" t t fmt))
  9978. (setq fmt (replace-match "from %f" t t fmt))))
  9979. (org-replace-escapes fmt table)))
  9980. (defun org-make-org-heading-search-string (&optional string heading)
  9981. "Make search string for STRING or current headline."
  9982. (interactive)
  9983. (let ((s (or string (org-get-heading))))
  9984. (unless (and string (not heading))
  9985. ;; We are using a headline, clean up garbage in there.
  9986. (if (string-match org-todo-regexp s)
  9987. (setq s (replace-match "" t t s)))
  9988. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  9989. (setq s (replace-match "" t t s)))
  9990. (setq s (org-trim s))
  9991. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9992. org-comment-string "\\)") s)
  9993. (setq s (replace-match "" t t s)))
  9994. (while (string-match org-ts-regexp s)
  9995. (setq s (replace-match "" t t s))))
  9996. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9997. (setq s (replace-match " " t t s)))
  9998. (or string (setq s (concat "*" s))) ; Add * for headlines
  9999. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10000. (defun org-make-link (&rest strings)
  10001. "Concatenate STRINGS."
  10002. (apply 'concat strings))
  10003. (defun org-make-link-string (link &optional description)
  10004. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10005. (unless (string-match "\\S-" link)
  10006. (error "Empty link"))
  10007. (when (stringp description)
  10008. ;; Remove brackets from the description, they are fatal.
  10009. (while (string-match "\\[\\|\\]" description)
  10010. (setq description (replace-match "" t t description))))
  10011. (when (equal (org-link-escape link) description)
  10012. ;; No description needed, it is identical
  10013. (setq description nil))
  10014. (when (and (not description)
  10015. (not (equal link (org-link-escape link))))
  10016. (setq description link))
  10017. (concat "[[" (org-link-escape link) "]"
  10018. (if description (concat "[" description "]") "")
  10019. "]"))
  10020. (defconst org-link-escape-chars
  10021. '((" " . "%20")
  10022. ("\340" . "%E0") ; `a
  10023. ("\342" . "%E2") ; ^a
  10024. ("\347" . "%E7") ; ,c
  10025. ("\350" . "%E8") ; `e
  10026. ("\351" . "%E9") ; 'e
  10027. ("\352" . "%EA") ; ^e
  10028. ("\356" . "%EE") ; ^i
  10029. ("\364" . "%F4") ; ^o
  10030. ("\371" . "%F9") ; `u
  10031. ("\373" . "%FB") ; ^u
  10032. (";" . "%3B")
  10033. ("?" . "%3F")
  10034. ("=" . "%3D")
  10035. ("+" . "%2B")
  10036. )
  10037. "Association list of escapes for some characters problematic in links.")
  10038. (defun org-link-escape (text)
  10039. "Escape charaters in TEXT that are problematic for links."
  10040. (when text
  10041. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  10042. org-link-escape-chars "\\|")))
  10043. (while (string-match re text)
  10044. (setq text
  10045. (replace-match
  10046. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  10047. t t text)))
  10048. text)))
  10049. (defun org-link-unescape (text)
  10050. "Reverse the action of `org-link-escape'."
  10051. (when text
  10052. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10053. org-link-escape-chars "\\|")))
  10054. (while (string-match re text)
  10055. (setq text
  10056. (replace-match
  10057. (car (rassoc (match-string 0 text) org-link-escape-chars))
  10058. t t text)))
  10059. text)))
  10060. (defun org-xor (a b)
  10061. "Exclusive or."
  10062. (if a (not b) b))
  10063. (defun org-get-header (header)
  10064. "Find a header field in the current buffer."
  10065. (save-excursion
  10066. (goto-char (point-min))
  10067. (let ((case-fold-search t) s)
  10068. (cond
  10069. ((eq header 'from)
  10070. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10071. (setq s (match-string 1)))
  10072. (while (string-match "\"" s)
  10073. (setq s (replace-match "" t t s)))
  10074. (if (string-match "[<(].*" s)
  10075. (setq s (replace-match "" t t s))))
  10076. ((eq header 'message-id)
  10077. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10078. (setq s (match-string 1))))
  10079. ((eq header 'subject)
  10080. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  10081. (setq s (match-string 1)))))
  10082. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  10083. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  10084. s)))
  10085. (defun org-fixup-message-id-for-http (s)
  10086. "Replace special characters in a message id, so it can be used in an http query."
  10087. (while (string-match "<" s)
  10088. (setq s (replace-match "%3C" t t s)))
  10089. (while (string-match ">" s)
  10090. (setq s (replace-match "%3E" t t s)))
  10091. (while (string-match "@" s)
  10092. (setq s (replace-match "%40" t t s)))
  10093. s)
  10094. ;;;###autoload
  10095. (defun org-insert-link-global ()
  10096. "Insert a link like Org-mode does.
  10097. This command can be called in any mode to insert a link in Org-mode syntax."
  10098. (interactive)
  10099. (org-run-like-in-org-mode 'org-insert-link))
  10100. (defun org-insert-link (&optional complete-file)
  10101. "Insert a link. At the prompt, enter the link.
  10102. Completion can be used to select a link previously stored with
  10103. `org-store-link'. When the empty string is entered (i.e. if you just
  10104. press RET at the prompt), the link defaults to the most recently
  10105. stored link. As SPC triggers completion in the minibuffer, you need to
  10106. use M-SPC or C-q SPC to force the insertion of a space character.
  10107. You will also be prompted for a description, and if one is given, it will
  10108. be displayed in the buffer instead of the link.
  10109. If there is already a link at point, this command will allow you to edit link
  10110. and description parts.
  10111. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10112. selected using completion. The path to the file will be relative to
  10113. the current directory if the file is in the current directory or a
  10114. subdirectory. Otherwise, the link will be the absolute path as
  10115. completed in the minibuffer (i.e. normally ~/path/to/file).
  10116. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10117. is in the current directory or below.
  10118. With three \\[universal-argument] prefixes, negate the meaning of
  10119. `org-keep-stored-link-after-insertion'."
  10120. (interactive "P")
  10121. (let ((wcf (current-window-configuration))
  10122. (region (if (org-region-active-p)
  10123. (prog1 (buffer-substring (region-beginning) (region-end))
  10124. (delete-region (region-beginning) (region-end)))))
  10125. tmphist ; byte-compile incorrectly complains about this
  10126. link desc entry remove file)
  10127. (cond
  10128. ((org-in-regexp org-bracket-link-regexp 1)
  10129. ;; We do have a link at point, and we are going to edit it.
  10130. (setq remove (list (match-beginning 0) (match-end 0)))
  10131. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10132. (setq link (read-string "Link: "
  10133. (org-link-unescape
  10134. (org-match-string-no-properties 1)))))
  10135. ((or (org-in-regexp org-angle-link-re)
  10136. (org-in-regexp org-plain-link-re))
  10137. ;; Convert to bracket link
  10138. (setq remove (list (match-beginning 0) (match-end 0))
  10139. link (read-string "Link: "
  10140. (org-remove-angle-brackets (match-string 0)))))
  10141. ((equal complete-file '(4))
  10142. ;; Completing read for file names.
  10143. (setq file (read-file-name "File: "))
  10144. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10145. (pwd1 (file-name-as-directory (abbreviate-file-name
  10146. (expand-file-name ".")))))
  10147. (cond
  10148. ((equal complete-file '(16))
  10149. (setq link (org-make-link
  10150. "file:"
  10151. (abbreviate-file-name (expand-file-name file)))))
  10152. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10153. (setq link (org-make-link "file:" (match-string 1 file))))
  10154. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10155. (expand-file-name file))
  10156. (setq link (org-make-link
  10157. "file:" (match-string 1 (expand-file-name file)))))
  10158. (t (setq link (org-make-link "file:" file))))))
  10159. (t
  10160. ;; Read link, with completion for stored links.
  10161. (with-output-to-temp-buffer "*Org Links*"
  10162. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  10163. (when org-stored-links
  10164. (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
  10165. (princ (mapconcat 'car (reverse org-stored-links) "\n"))))
  10166. (let ((cw (selected-window)))
  10167. (select-window (get-buffer-window "*Org Links*"))
  10168. (shrink-window-if-larger-than-buffer)
  10169. (setq truncate-lines t)
  10170. (select-window cw))
  10171. ;; Fake a link history, containing the stored links.
  10172. (setq tmphist (append (mapcar 'car org-stored-links)
  10173. org-insert-link-history))
  10174. (unwind-protect
  10175. (setq link (org-completing-read
  10176. "Link: "
  10177. (append
  10178. (mapcar (lambda (x) (list (concat (car x) ":")))
  10179. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  10180. (mapcar (lambda (x) (list (concat x ":")))
  10181. org-link-types))
  10182. nil nil nil
  10183. 'tmphist
  10184. (or (car (car org-stored-links)))))
  10185. (set-window-configuration wcf)
  10186. (kill-buffer "*Org Links*"))
  10187. (setq entry (assoc link org-stored-links))
  10188. (or entry (push link org-insert-link-history))
  10189. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10190. (not org-keep-stored-link-after-insertion))
  10191. (setq org-stored-links (delq (assoc link org-stored-links)
  10192. org-stored-links)))
  10193. (setq link (if entry (nth 1 entry) link)
  10194. desc (or region desc (nth 2 entry)))))
  10195. (if (string-match org-plain-link-re link)
  10196. ;; URL-like link, normalize the use of angular brackets.
  10197. (setq link (org-make-link (org-remove-angle-brackets link))))
  10198. ;; Check if we are linking to the current file with a search option
  10199. ;; If yes, simplify the link by using only the search option.
  10200. (when (and buffer-file-name
  10201. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10202. (let* ((path (match-string 1 link))
  10203. (case-fold-search nil)
  10204. (search (match-string 2 link)))
  10205. (save-match-data
  10206. (if (equal (file-truename buffer-file-name) (file-truename path))
  10207. ;; We are linking to this same file, with a search option
  10208. (setq link search)))))
  10209. ;; Check if we can/should use a relative path. If yes, simplify the link
  10210. (when (string-match "\\<file:\\(.*\\)" link)
  10211. (let* ((path (match-string 1 link))
  10212. (case-fold-search nil))
  10213. (cond
  10214. ((eq org-link-file-path-type 'absolute)
  10215. (setq path (abbreviate-file-name (expand-file-name path))))
  10216. ((eq org-link-file-path-type 'noabbrev)
  10217. (setq path (expand-file-name path)))
  10218. ((eq org-link-file-path-type 'relative)
  10219. (setq path (file-relative-name path)))
  10220. (t
  10221. (save-match-data
  10222. (if (string-match (concat "^" (regexp-quote
  10223. (file-name-as-directory
  10224. (expand-file-name "."))))
  10225. (expand-file-name path))
  10226. ;; We are linking a file with relative path name.
  10227. (setq path (substring (expand-file-name path)
  10228. (match-end 0)))))))
  10229. (setq link (concat "file:" path))))
  10230. (setq desc (read-string "Description: " desc))
  10231. (unless (string-match "\\S-" desc) (setq desc nil))
  10232. (if remove (apply 'delete-region remove))
  10233. (insert (org-make-link-string link desc))))
  10234. (defun org-completing-read (&rest args)
  10235. (let ((minibuffer-local-completion-map
  10236. (copy-keymap minibuffer-local-completion-map)))
  10237. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  10238. (apply 'completing-read args)))
  10239. ;;; Opening/following a link
  10240. (defvar org-link-search-failed nil)
  10241. (defun org-next-link ()
  10242. "Move forward to the next link.
  10243. If the link is in hidden text, expose it."
  10244. (interactive)
  10245. (when (and org-link-search-failed (eq this-command last-command))
  10246. (goto-char (point-min))
  10247. (message "Link search wrapped back to beginning of buffer"))
  10248. (setq org-link-search-failed nil)
  10249. (let* ((pos (point))
  10250. (ct (org-context))
  10251. (a (assoc :link ct)))
  10252. (if a (goto-char (nth 2 a)))
  10253. (if (re-search-forward org-any-link-re nil t)
  10254. (progn
  10255. (goto-char (match-beginning 0))
  10256. (if (org-invisible-p) (org-show-context)))
  10257. (goto-char pos)
  10258. (setq org-link-search-failed t)
  10259. (error "No further link found"))))
  10260. (defun org-previous-link ()
  10261. "Move backward to the previous link.
  10262. If the link is in hidden text, expose it."
  10263. (interactive)
  10264. (when (and org-link-search-failed (eq this-command last-command))
  10265. (goto-char (point-max))
  10266. (message "Link search wrapped back to end of buffer"))
  10267. (setq org-link-search-failed nil)
  10268. (let* ((pos (point))
  10269. (ct (org-context))
  10270. (a (assoc :link ct)))
  10271. (if a (goto-char (nth 1 a)))
  10272. (if (re-search-backward org-any-link-re nil t)
  10273. (progn
  10274. (goto-char (match-beginning 0))
  10275. (if (org-invisible-p) (org-show-context)))
  10276. (goto-char pos)
  10277. (setq org-link-search-failed t)
  10278. (error "No further link found"))))
  10279. (defun org-find-file-at-mouse (ev)
  10280. "Open file link or URL at mouse."
  10281. (interactive "e")
  10282. (mouse-set-point ev)
  10283. (org-open-at-point 'in-emacs))
  10284. (defun org-open-at-mouse (ev)
  10285. "Open file link or URL at mouse."
  10286. (interactive "e")
  10287. (mouse-set-point ev)
  10288. (org-open-at-point))
  10289. (defvar org-window-config-before-follow-link nil
  10290. "The window configuration before following a link.
  10291. This is saved in case the need arises to restore it.")
  10292. (defvar org-open-link-marker (make-marker)
  10293. "Marker pointing to the location where `org-open-at-point; was called.")
  10294. ;;;###autoload
  10295. (defun org-open-at-point-global ()
  10296. "Follow a link like Org-mode does.
  10297. This command can be called in any mode to follow a link that has
  10298. Org-mode syntax."
  10299. (interactive)
  10300. (org-run-like-in-org-mode 'org-open-at-point))
  10301. (defun org-open-at-point (&optional in-emacs)
  10302. "Open link at or after point.
  10303. If there is no link at point, this function will search forward up to
  10304. the end of the current subtree.
  10305. Normally, files will be opened by an appropriate application. If the
  10306. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  10307. (interactive "P")
  10308. (move-marker org-open-link-marker (point))
  10309. (setq org-window-config-before-follow-link (current-window-configuration))
  10310. (org-remove-occur-highlights nil nil t)
  10311. (if (org-at-timestamp-p t)
  10312. (org-follow-timestamp-link)
  10313. (let (type path link line search (pos (point)))
  10314. (catch 'match
  10315. (save-excursion
  10316. (skip-chars-forward "^]\n\r")
  10317. (when (org-in-regexp org-bracket-link-regexp)
  10318. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  10319. (while (string-match " *\n *" link)
  10320. (setq link (replace-match " " t t link)))
  10321. (setq link (org-link-expand-abbrev link))
  10322. (if (string-match org-link-re-with-space2 link)
  10323. (setq type (match-string 1 link) path (match-string 2 link))
  10324. (setq type "thisfile" path link))
  10325. (throw 'match t)))
  10326. (when (get-text-property (point) 'org-linked-text)
  10327. (setq type "thisfile"
  10328. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  10329. (1+ (point)) (point))
  10330. path (buffer-substring
  10331. (previous-single-property-change pos 'org-linked-text)
  10332. (next-single-property-change pos 'org-linked-text)))
  10333. (throw 'match t))
  10334. (save-excursion
  10335. (when (or (org-in-regexp org-angle-link-re)
  10336. (org-in-regexp org-plain-link-re))
  10337. (setq type (match-string 1) path (match-string 2))
  10338. (throw 'match t)))
  10339. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  10340. (setq type "tree-match"
  10341. path (match-string 1))
  10342. (throw 'match t))
  10343. (save-excursion
  10344. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  10345. (setq type "tags"
  10346. path (match-string 1))
  10347. (while (string-match ":" path)
  10348. (setq path (replace-match "+" t t path)))
  10349. (throw 'match t))))
  10350. (unless path
  10351. (error "No link found"))
  10352. ;; Remove any trailing spaces in path
  10353. (if (string-match " +\\'" path)
  10354. (setq path (replace-match "" t t path)))
  10355. (cond
  10356. ((assoc type org-link-protocols)
  10357. (funcall (nth 1 (assoc type org-link-protocols)) path))
  10358. ((equal type "mailto")
  10359. (let ((cmd (car org-link-mailto-program))
  10360. (args (cdr org-link-mailto-program)) args1
  10361. (address path) (subject "") a)
  10362. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10363. (setq address (match-string 1 path)
  10364. subject (org-link-escape (match-string 2 path))))
  10365. (while args
  10366. (cond
  10367. ((not (stringp (car args))) (push (pop args) args1))
  10368. (t (setq a (pop args))
  10369. (if (string-match "%a" a)
  10370. (setq a (replace-match address t t a)))
  10371. (if (string-match "%s" a)
  10372. (setq a (replace-match subject t t a)))
  10373. (push a args1))))
  10374. (apply cmd (nreverse args1))))
  10375. ((member type '("http" "https" "ftp" "news"))
  10376. (browse-url (concat type ":" path)))
  10377. ((string= type "tags")
  10378. (org-tags-view in-emacs path))
  10379. ((string= type "thisfile")
  10380. (if in-emacs
  10381. (switch-to-buffer-other-window
  10382. (org-get-buffer-for-internal-link (current-buffer)))
  10383. (org-mark-ring-push))
  10384. (let ((cmd `(org-link-search
  10385. ,path
  10386. ,(cond ((equal in-emacs '(4)) 'occur)
  10387. ((equal in-emacs '(16)) 'org-occur)
  10388. (t nil))
  10389. ,pos)))
  10390. (condition-case nil (eval cmd)
  10391. (error (progn (widen) (eval cmd))))))
  10392. ((string= type "tree-match")
  10393. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10394. ((string= type "file")
  10395. (if (string-match "::\\([0-9]+\\)\\'" path)
  10396. (setq line (string-to-number (match-string 1 path))
  10397. path (substring path 0 (match-beginning 0)))
  10398. (if (string-match "::\\(.+\\)\\'" path)
  10399. (setq search (match-string 1 path)
  10400. path (substring path 0 (match-beginning 0)))))
  10401. (org-open-file path in-emacs line search))
  10402. ((string= type "news")
  10403. (org-follow-gnus-link path))
  10404. ((string= type "bbdb")
  10405. (org-follow-bbdb-link path))
  10406. ((string= type "info")
  10407. (org-follow-info-link path))
  10408. ((string= type "gnus")
  10409. (let (group article)
  10410. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10411. (error "Error in Gnus link"))
  10412. (setq group (match-string 1 path)
  10413. article (match-string 3 path))
  10414. (org-follow-gnus-link group article)))
  10415. ((string= type "vm")
  10416. (let (folder article)
  10417. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10418. (error "Error in VM link"))
  10419. (setq folder (match-string 1 path)
  10420. article (match-string 3 path))
  10421. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10422. (org-follow-vm-link folder article in-emacs)))
  10423. ((string= type "wl")
  10424. (let (folder article)
  10425. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10426. (error "Error in Wanderlust link"))
  10427. (setq folder (match-string 1 path)
  10428. article (match-string 3 path))
  10429. (org-follow-wl-link folder article)))
  10430. ((string= type "mhe")
  10431. (let (folder article)
  10432. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10433. (error "Error in MHE link"))
  10434. (setq folder (match-string 1 path)
  10435. article (match-string 3 path))
  10436. (org-follow-mhe-link folder article)))
  10437. ((string= type "rmail")
  10438. (let (folder article)
  10439. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10440. (error "Error in RMAIL link"))
  10441. (setq folder (match-string 1 path)
  10442. article (match-string 3 path))
  10443. (org-follow-rmail-link folder article)))
  10444. ((string= type "shell")
  10445. (let ((cmd path))
  10446. ;; FIXME: the following is only for backward compatibility
  10447. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10448. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10449. (if (or (not org-confirm-shell-link-function)
  10450. (funcall org-confirm-shell-link-function
  10451. (format "Execute \"%s\" in shell? "
  10452. (org-add-props cmd nil
  10453. 'face 'org-warning))))
  10454. (progn
  10455. (message "Executing %s" cmd)
  10456. (shell-command cmd))
  10457. (error "Abort"))))
  10458. ((string= type "elisp")
  10459. (let ((cmd path))
  10460. (if (or (not org-confirm-elisp-link-function)
  10461. (funcall org-confirm-elisp-link-function
  10462. (format "Execute \"%s\" as elisp? "
  10463. (org-add-props cmd nil
  10464. 'face 'org-warning))))
  10465. (message "%s => %s" cmd (eval (read cmd)))
  10466. (error "Abort"))))
  10467. (t
  10468. (browse-url-at-point)))))
  10469. (move-marker org-open-link-marker nil))
  10470. ;;; File search
  10471. (defvar org-create-file-search-functions nil
  10472. "List of functions to construct the right search string for a file link.
  10473. These functions are called in turn with point at the location to
  10474. which the link should point.
  10475. A function in the hook should first test if it would like to
  10476. handle this file type, for example by checking the major-mode or
  10477. the file extension. If it decides not to handle this file, it
  10478. should just return nil to give other functions a chance. If it
  10479. does handle the file, it must return the search string to be used
  10480. when following the link. The search string will be part of the
  10481. file link, given after a double colon, and `org-open-at-point'
  10482. will automatically search for it. If special measures must be
  10483. taken to make the search successful, another function should be
  10484. added to the companion hook `org-execute-file-search-functions',
  10485. which see.
  10486. A function in this hook may also use `setq' to set the variable
  10487. `description' to provide a suggestion for the descriptive text to
  10488. be used for this link when it gets inserted into an Org-mode
  10489. buffer with \\[org-insert-link].")
  10490. (defvar org-execute-file-search-functions nil
  10491. "List of functions to execute a file search triggered by a link.
  10492. Functions added to this hook must accept a single argument, the
  10493. search string that was part of the file link, the part after the
  10494. double colon. The function must first check if it would like to
  10495. handle this search, for example by checking the major-mode or the
  10496. file extension. If it decides not to handle this search, it
  10497. should just return nil to give other functions a chance. If it
  10498. does handle the search, it must return a non-nil value to keep
  10499. other functions from trying.
  10500. Each function can access the current prefix argument through the
  10501. variable `current-prefix-argument'. Note that a single prefix is
  10502. used to force opening a link in Emacs, so it may be good to only
  10503. use a numeric or double prefix to guide the search function.
  10504. In case this is needed, a function in this hook can also restore
  10505. the window configuration before `org-open-at-point' was called using:
  10506. (set-window-configuration org-window-config-before-follow-link)")
  10507. (defun org-link-search (s &optional type avoid-pos)
  10508. "Search for a link search option.
  10509. If S is surrounded by forward slashes, it is interpreted as a
  10510. regular expression. In org-mode files, this will create an `org-occur'
  10511. sparse tree. In ordinary files, `occur' will be used to list matches.
  10512. If the current buffer is in `dired-mode', grep will be used to search
  10513. in all files. If AVOID-POS is given, ignore matches near that position."
  10514. (let ((case-fold-search t)
  10515. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  10516. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  10517. (append '(("") (" ") ("\t") ("\n"))
  10518. org-emphasis-alist)
  10519. "\\|") "\\)"))
  10520. (pos (point))
  10521. (pre "") (post "")
  10522. words re0 re1 re2 re3 re4 re5 re2a reall)
  10523. (cond
  10524. ;; First check if there are any special
  10525. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  10526. ;; Now try the builtin stuff
  10527. ((save-excursion
  10528. (goto-char (point-min))
  10529. (and
  10530. (re-search-forward
  10531. (concat "<<" (regexp-quote s0) ">>") nil t)
  10532. (setq pos (match-beginning 0))))
  10533. ;; There is an exact target for this
  10534. (goto-char pos))
  10535. ((string-match "^/\\(.*\\)/$" s)
  10536. ;; A regular expression
  10537. (cond
  10538. ((org-mode-p)
  10539. (org-occur (match-string 1 s)))
  10540. ;;((eq major-mode 'dired-mode)
  10541. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  10542. (t (org-do-occur (match-string 1 s)))))
  10543. (t
  10544. ;; A normal search strings
  10545. (when (equal (string-to-char s) ?*)
  10546. ;; Anchor on headlines, post may include tags.
  10547. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10548. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  10549. s (substring s 1)))
  10550. (remove-text-properties
  10551. 0 (length s)
  10552. '(face nil mouse-face nil keymap nil fontified nil) s)
  10553. ;; Make a series of regular expressions to find a match
  10554. (setq words (org-split-string s "[ \n\r\t]+")
  10555. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10556. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10557. "\\)" markers)
  10558. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  10559. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10560. re1 (concat pre re2 post)
  10561. re3 (concat pre re4 post)
  10562. re5 (concat pre ".*" re4)
  10563. re2 (concat pre re2)
  10564. re2a (concat pre re2a)
  10565. re4 (concat pre re4)
  10566. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10567. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10568. re5 "\\)"
  10569. ))
  10570. (cond
  10571. ((eq type 'org-occur) (org-occur reall))
  10572. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10573. (t (goto-char (point-min))
  10574. (if (or (org-search-not-self 1 re0 nil t)
  10575. (org-search-not-self 1 re1 nil t)
  10576. (org-search-not-self 1 re2 nil t)
  10577. (org-search-not-self 1 re2a nil t)
  10578. (org-search-not-self 1 re3 nil t)
  10579. (org-search-not-self 1 re4 nil t)
  10580. (org-search-not-self 1 re5 nil t)
  10581. )
  10582. (goto-char (match-beginning 1))
  10583. (goto-char pos)
  10584. (error "No match")))))
  10585. (t
  10586. ;; Normal string-search
  10587. (goto-char (point-min))
  10588. (if (search-forward s nil t)
  10589. (goto-char (match-beginning 0))
  10590. (error "No match"))))
  10591. (and (org-mode-p) (org-show-context 'link-search))))
  10592. (defun org-search-not-self (group &rest args)
  10593. "Execute `re-search-forward', but only accept matches that do not
  10594. enclose the position of `org-open-link-marker'."
  10595. (let ((m org-open-link-marker))
  10596. (catch 'exit
  10597. (while (apply 're-search-forward args)
  10598. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10599. (goto-char (match-end group))
  10600. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10601. (> (match-beginning 0) (marker-position m))
  10602. (< (match-end 0) (marker-position m)))
  10603. (save-match-data
  10604. (or (not (org-in-regexp
  10605. org-bracket-link-analytic-regexp 1))
  10606. (not (match-end 4)) ; no description
  10607. (and (<= (match-beginning 4) (point))
  10608. (>= (match-end 4) (point))))))
  10609. (throw 'exit (point))))))))
  10610. (defun org-get-buffer-for-internal-link (buffer)
  10611. "Return a buffer to be used for displaying the link target of internal links."
  10612. (cond
  10613. ((not org-display-internal-link-with-indirect-buffer)
  10614. buffer)
  10615. ((string-match "(Clone)$" (buffer-name buffer))
  10616. (message "Buffer is already a clone, not making another one")
  10617. ;; we also do not modify visibility in this case
  10618. buffer)
  10619. (t ; make a new indirect buffer for displaying the link
  10620. (let* ((bn (buffer-name buffer))
  10621. (ibn (concat bn "(Clone)"))
  10622. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10623. (with-current-buffer ib (org-overview))
  10624. ib))))
  10625. (defun org-do-occur (regexp &optional cleanup)
  10626. "Call the Emacs command `occur'.
  10627. If CLEANUP is non-nil, remove the printout of the regular expression
  10628. in the *Occur* buffer. This is useful if the regex is long and not useful
  10629. to read."
  10630. (occur regexp)
  10631. (when cleanup
  10632. (let ((cwin (selected-window)) win beg end)
  10633. (when (setq win (get-buffer-window "*Occur*"))
  10634. (select-window win))
  10635. (goto-char (point-min))
  10636. (when (re-search-forward "match[a-z]+" nil t)
  10637. (setq beg (match-end 0))
  10638. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10639. (setq end (1- (match-beginning 0)))))
  10640. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10641. (goto-char (point-min))
  10642. (select-window cwin))))
  10643. ;;; The mark ring for links jumps
  10644. (defvar org-mark-ring nil
  10645. "Mark ring for positions before jumps in Org-mode.")
  10646. (defvar org-mark-ring-last-goto nil
  10647. "Last position in the mark ring used to go back.")
  10648. ;; Fill and close the ring
  10649. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10650. (loop for i from 1 to org-mark-ring-length do
  10651. (push (make-marker) org-mark-ring))
  10652. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10653. org-mark-ring)
  10654. (defun org-mark-ring-push (&optional pos buffer)
  10655. "Put the current position or POS into the mark ring and rotate it."
  10656. (interactive)
  10657. (setq pos (or pos (point)))
  10658. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10659. (move-marker (car org-mark-ring)
  10660. (or pos (point))
  10661. (or buffer (current-buffer)))
  10662. (message
  10663. (substitute-command-keys
  10664. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10665. (defun org-mark-ring-goto (&optional n)
  10666. "Jump to the previous position in the mark ring.
  10667. With prefix arg N, jump back that many stored positions. When
  10668. called several times in succession, walk through the entire ring.
  10669. Org-mode commands jumping to a different position in the current file,
  10670. or to another Org-mode file, automatically push the old position
  10671. onto the ring."
  10672. (interactive "p")
  10673. (let (p m)
  10674. (if (eq last-command this-command)
  10675. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10676. (setq p org-mark-ring))
  10677. (setq org-mark-ring-last-goto p)
  10678. (setq m (car p))
  10679. (switch-to-buffer (marker-buffer m))
  10680. (goto-char m)
  10681. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10682. (defun org-remove-angle-brackets (s)
  10683. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10684. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10685. s)
  10686. (defun org-add-angle-brackets (s)
  10687. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10688. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10689. s)
  10690. ;;; Following specific links
  10691. (defun org-follow-timestamp-link ()
  10692. (cond
  10693. ((org-at-date-range-p t)
  10694. (let ((org-agenda-start-on-weekday)
  10695. (t1 (match-string 1))
  10696. (t2 (match-string 2)))
  10697. (setq t1 (time-to-days (org-time-string-to-time t1))
  10698. t2 (time-to-days (org-time-string-to-time t2)))
  10699. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  10700. ((org-at-timestamp-p t)
  10701. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10702. (substring (match-string 1) 0 10)))
  10703. 1))
  10704. (t (error "This should not happen"))))
  10705. (defun org-follow-bbdb-link (name)
  10706. "Follow a BBDB link to NAME."
  10707. (require 'bbdb)
  10708. (let ((inhibit-redisplay (not debug-on-error))
  10709. (bbdb-electric-p nil))
  10710. (catch 'exit
  10711. ;; Exact match on name
  10712. (bbdb-name (concat "\\`" name "\\'") nil)
  10713. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10714. ;; Exact match on name
  10715. (bbdb-company (concat "\\`" name "\\'") nil)
  10716. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10717. ;; Partial match on name
  10718. (bbdb-name name nil)
  10719. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10720. ;; Partial match on company
  10721. (bbdb-company name nil)
  10722. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10723. ;; General match including network address and notes
  10724. (bbdb name nil)
  10725. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  10726. (delete-window (get-buffer-window "*BBDB*"))
  10727. (error "No matching BBDB record")))))
  10728. (defun org-follow-info-link (name)
  10729. "Follow an info file & node link to NAME."
  10730. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  10731. (string-match "\\(.*\\)" name))
  10732. (progn
  10733. (require 'info)
  10734. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  10735. (Info-find-node (match-string 1 name) (match-string 2 name))
  10736. (Info-find-node (match-string 1 name) "Top")))
  10737. (message (concat "Could not open: " name))))
  10738. (defun org-follow-gnus-link (&optional group article)
  10739. "Follow a Gnus link to GROUP and ARTICLE."
  10740. (require 'gnus)
  10741. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  10742. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  10743. (cond ((and group article)
  10744. (gnus-group-read-group 1 nil group)
  10745. (gnus-summary-goto-article (string-to-number article) nil t))
  10746. (group (gnus-group-jump-to-group group))))
  10747. (defun org-follow-vm-link (&optional folder article readonly)
  10748. "Follow a VM link to FOLDER and ARTICLE."
  10749. (require 'vm)
  10750. (setq article (org-add-angle-brackets article))
  10751. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  10752. ;; ange-ftp or efs or tramp access
  10753. (let ((user (or (match-string 1 folder) (user-login-name)))
  10754. (host (match-string 2 folder))
  10755. (file (match-string 3 folder)))
  10756. (cond
  10757. ((featurep 'tramp)
  10758. ;; use tramp to access the file
  10759. (if (featurep 'xemacs)
  10760. (setq folder (format "[%s@%s]%s" user host file))
  10761. (setq folder (format "/%s@%s:%s" user host file))))
  10762. (t
  10763. ;; use ange-ftp or efs
  10764. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  10765. (setq folder (format "/%s@%s:%s" user host file))))))
  10766. (when folder
  10767. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  10768. (sit-for 0.1)
  10769. (when article
  10770. (vm-select-folder-buffer)
  10771. (widen)
  10772. (let ((case-fold-search t))
  10773. (goto-char (point-min))
  10774. (if (not (re-search-forward
  10775. (concat "^" "message-id: *" (regexp-quote article))))
  10776. (error "Could not find the specified message in this folder"))
  10777. (vm-isearch-update)
  10778. (vm-isearch-narrow)
  10779. (vm-beginning-of-message)
  10780. (vm-summarize)))))
  10781. (defun org-follow-wl-link (folder article)
  10782. "Follow a Wanderlust link to FOLDER and ARTICLE."
  10783. (if (and (string= folder "%")
  10784. article
  10785. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  10786. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  10787. ;; Thus, we recompose folder and article ids.
  10788. (setq folder (format "%s#%s" folder (match-string 1 article))
  10789. article (match-string 3 article)))
  10790. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  10791. (error "No such folder: %s" folder))
  10792. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  10793. (and article
  10794. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  10795. (wl-summary-redisplay)))
  10796. (defun org-follow-rmail-link (folder article)
  10797. "Follow an RMAIL link to FOLDER and ARTICLE."
  10798. (setq article (org-add-angle-brackets article))
  10799. (let (message-number)
  10800. (save-excursion
  10801. (save-window-excursion
  10802. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10803. (setq message-number
  10804. (save-restriction
  10805. (widen)
  10806. (goto-char (point-max))
  10807. (if (re-search-backward
  10808. (concat "^Message-ID:\\s-+" (regexp-quote
  10809. (or article "")))
  10810. nil t)
  10811. (rmail-what-message))))))
  10812. (if message-number
  10813. (progn
  10814. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10815. (rmail-show-message message-number)
  10816. message-number)
  10817. (error "Message not found"))))
  10818. ;;; mh-e integration based on planner-mode
  10819. (defun org-mhe-get-message-real-folder ()
  10820. "Return the name of the current message real folder, so if you use
  10821. sequences, it will now work."
  10822. (save-excursion
  10823. (let* ((folder
  10824. (if (equal major-mode 'mh-folder-mode)
  10825. mh-current-folder
  10826. ;; Refer to the show buffer
  10827. mh-show-folder-buffer))
  10828. (end-index
  10829. (if (boundp 'mh-index-folder)
  10830. (min (length mh-index-folder) (length folder))))
  10831. )
  10832. ;; a simple test on mh-index-data does not work, because
  10833. ;; mh-index-data is always nil in a show buffer.
  10834. (if (and (boundp 'mh-index-folder)
  10835. (string= mh-index-folder (substring folder 0 end-index)))
  10836. (if (equal major-mode 'mh-show-mode)
  10837. (save-window-excursion
  10838. (let (pop-up-frames)
  10839. (when (buffer-live-p (get-buffer folder))
  10840. (progn
  10841. (pop-to-buffer folder)
  10842. (org-mhe-get-message-folder-from-index)
  10843. )
  10844. )))
  10845. (org-mhe-get-message-folder-from-index)
  10846. )
  10847. folder
  10848. )
  10849. )))
  10850. (defun org-mhe-get-message-folder-from-index ()
  10851. "Returns the name of the message folder in a index folder buffer."
  10852. (save-excursion
  10853. (mh-index-previous-folder)
  10854. (re-search-forward "^\\(+.*\\)$" nil t)
  10855. (message (match-string 1))))
  10856. (defun org-mhe-get-message-folder ()
  10857. "Return the name of the current message folder. Be careful if you
  10858. use sequences."
  10859. (save-excursion
  10860. (if (equal major-mode 'mh-folder-mode)
  10861. mh-current-folder
  10862. ;; Refer to the show buffer
  10863. mh-show-folder-buffer)))
  10864. (defun org-mhe-get-message-num ()
  10865. "Return the number of the current message. Be careful if you
  10866. use sequences."
  10867. (save-excursion
  10868. (if (equal major-mode 'mh-folder-mode)
  10869. (mh-get-msg-num nil)
  10870. ;; Refer to the show buffer
  10871. (mh-show-buffer-message-number))))
  10872. (defun org-mhe-get-header (header)
  10873. "Return a header of the message in folder mode. This will create a
  10874. show buffer for the corresponding message. If you have a more clever
  10875. idea..."
  10876. (let* ((folder (org-mhe-get-message-folder))
  10877. (num (org-mhe-get-message-num))
  10878. (buffer (get-buffer-create (concat "show-" folder)))
  10879. (header-field))
  10880. (with-current-buffer buffer
  10881. (mh-display-msg num folder)
  10882. (if (equal major-mode 'mh-folder-mode)
  10883. (mh-header-display)
  10884. (mh-show-header-display))
  10885. (set-buffer buffer)
  10886. (setq header-field (mh-get-header-field header))
  10887. (if (equal major-mode 'mh-folder-mode)
  10888. (mh-show)
  10889. (mh-show-show))
  10890. header-field)))
  10891. (defun org-follow-mhe-link (folder article)
  10892. "Follow an MHE link to FOLDER and ARTICLE.
  10893. If ARTICLE is nil FOLDER is shown. If the configuration variable
  10894. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  10895. ARTICLE is searched in all folders. Indexed searches (swish++,
  10896. namazu, and others supported by MH-E) will always search in all
  10897. folders."
  10898. (require 'mh-e)
  10899. (require 'mh-search)
  10900. (require 'mh-utils)
  10901. (mh-find-path)
  10902. (if (not article)
  10903. (mh-visit-folder (mh-normalize-folder-name folder))
  10904. (setq article (org-add-angle-brackets article))
  10905. (mh-search-choose)
  10906. (if (equal mh-searcher 'pick)
  10907. (progn
  10908. (mh-search folder (list "--message-id" article))
  10909. (when (and org-mhe-search-all-folders
  10910. (not (org-mhe-get-message-real-folder)))
  10911. (kill-this-buffer)
  10912. (mh-search "+" (list "--message-id" article))))
  10913. (mh-search "+" article))
  10914. (if (org-mhe-get-message-real-folder)
  10915. (mh-show-msg 1)
  10916. (kill-this-buffer)
  10917. (error "Message not found"))))
  10918. ;;; BibTeX links
  10919. ;; Use the custom search meachnism to construct and use search strings for
  10920. ;; file links to BibTeX database entries.
  10921. (defun org-create-file-search-in-bibtex ()
  10922. "Create the search string and description for a BibTeX database entry."
  10923. (when (eq major-mode 'bibtex-mode)
  10924. ;; yes, we want to construct this search string.
  10925. ;; Make a good description for this entry, using names, year and the title
  10926. ;; Put it into the `description' variable which is dynamically scoped.
  10927. (let ((bibtex-autokey-names 1)
  10928. (bibtex-autokey-names-stretch 1)
  10929. (bibtex-autokey-name-case-convert-function 'identity)
  10930. (bibtex-autokey-name-separator " & ")
  10931. (bibtex-autokey-additional-names " et al.")
  10932. (bibtex-autokey-year-length 4)
  10933. (bibtex-autokey-name-year-separator " ")
  10934. (bibtex-autokey-titlewords 3)
  10935. (bibtex-autokey-titleword-separator " ")
  10936. (bibtex-autokey-titleword-case-convert-function 'identity)
  10937. (bibtex-autokey-titleword-length 'infty)
  10938. (bibtex-autokey-year-title-separator ": "))
  10939. (setq description (bibtex-generate-autokey)))
  10940. ;; Now parse the entry, get the key and return it.
  10941. (save-excursion
  10942. (bibtex-beginning-of-entry)
  10943. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  10944. (defun org-execute-file-search-in-bibtex (s)
  10945. "Find the link search string S as a key for a database entry."
  10946. (when (eq major-mode 'bibtex-mode)
  10947. ;; Yes, we want to do the search in this file.
  10948. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  10949. (goto-char (point-min))
  10950. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  10951. (regexp-quote s) "[ \t\n]*,") nil t)
  10952. (goto-char (match-beginning 0)))
  10953. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  10954. ;; Use double prefix to indicate that any web link should be browsed
  10955. (let ((b (current-buffer)) (p (point)))
  10956. ;; Restore the window configuration because we just use the web link
  10957. (set-window-configuration org-window-config-before-follow-link)
  10958. (save-excursion (set-buffer b) (goto-char p)
  10959. (bibtex-url)))
  10960. (recenter 0)) ; Move entry start to beginning of window
  10961. ;; return t to indicate that the search is done.
  10962. t))
  10963. ;; Finally add the functions to the right hooks.
  10964. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  10965. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  10966. ;; end of Bibtex link setup
  10967. ;;; Following file links
  10968. (defun org-open-file (path &optional in-emacs line search)
  10969. "Open the file at PATH.
  10970. First, this expands any special file name abbreviations. Then the
  10971. configuration variable `org-file-apps' is checked if it contains an
  10972. entry for this file type, and if yes, the corresponding command is launched.
  10973. If no application is found, Emacs simply visits the file.
  10974. With optional argument IN-EMACS, Emacs will visit the file.
  10975. Optional LINE specifies a line to go to, optional SEARCH a string to
  10976. search for. If LINE or SEARCH is given, the file will always be
  10977. opened in Emacs.
  10978. If the file does not exist, an error is thrown."
  10979. (setq in-emacs (or in-emacs line search))
  10980. (let* ((file (if (equal path "")
  10981. buffer-file-name
  10982. (substitute-in-file-name (expand-file-name path))))
  10983. (apps (append org-file-apps (org-default-apps)))
  10984. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10985. (dirp (if remp nil (file-directory-p file)))
  10986. (dfile (downcase file))
  10987. (old-buffer (current-buffer))
  10988. (old-pos (point))
  10989. (old-mode major-mode)
  10990. ext cmd)
  10991. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10992. (setq ext (match-string 1 dfile))
  10993. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10994. (setq ext (match-string 1 dfile))))
  10995. (if in-emacs
  10996. (setq cmd 'emacs)
  10997. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10998. (and dirp (cdr (assoc 'directory apps)))
  10999. (cdr (assoc ext apps))
  11000. (cdr (assoc t apps)))))
  11001. (when (eq cmd 'mailcap)
  11002. (require 'mailcap)
  11003. (mailcap-parse-mailcaps)
  11004. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11005. (command (mailcap-mime-info mime-type)))
  11006. (if (stringp command)
  11007. (setq cmd command)
  11008. (setq cmd 'emacs))))
  11009. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11010. (not (file-exists-p file))
  11011. (not org-open-non-existing-files))
  11012. (error "No such file: %s" file))
  11013. (cond
  11014. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  11015. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  11016. (if (string-match "['\"]%s['\"]" cmd)
  11017. (setq cmd (replace-match "%s" t t cmd)))
  11018. (setq cmd (format cmd (shell-quote-argument file)))
  11019. (save-window-excursion
  11020. (shell-command (concat cmd " &"))))
  11021. ((or (stringp cmd)
  11022. (eq cmd 'emacs))
  11023. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  11024. (widen)
  11025. (if line (goto-line line)
  11026. (if search (org-link-search search))))
  11027. ((consp cmd)
  11028. (eval cmd))
  11029. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  11030. (and (org-mode-p) (eq old-mode 'org-mode)
  11031. (or (not (equal old-buffer (current-buffer)))
  11032. (not (equal old-pos (point))))
  11033. (org-mark-ring-push old-pos old-buffer))))
  11034. (defun org-default-apps ()
  11035. "Return the default applications for this operating system."
  11036. (cond
  11037. ((eq system-type 'darwin)
  11038. org-file-apps-defaults-macosx)
  11039. ((eq system-type 'windows-nt)
  11040. org-file-apps-defaults-windowsnt)
  11041. (t org-file-apps-defaults-gnu)))
  11042. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  11043. (defun org-file-remote-p (file)
  11044. "Test whether FILE specifies a location on a remote system.
  11045. Return non-nil if the location is indeed remote.
  11046. For example, the filename \"/user@host:/foo\" specifies a location
  11047. on the system \"/user@host:\"."
  11048. (cond ((fboundp 'file-remote-p)
  11049. (file-remote-p file))
  11050. ((fboundp 'tramp-handle-file-remote-p)
  11051. (tramp-handle-file-remote-p file))
  11052. ((and (boundp 'ange-ftp-name-format)
  11053. (string-match (car ange-ftp-name-format) file))
  11054. t)
  11055. (t nil)))
  11056. ;;;; Hooks for remember.el
  11057. ;;;###autoload
  11058. (defun org-remember-annotation ()
  11059. "Return a link to the current location as an annotation for remember.el.
  11060. If you are using Org-mode files as target for data storage with
  11061. remember.el, then the annotations should include a link compatible with the
  11062. conventions in Org-mode. This function returns such a link."
  11063. (org-store-link nil))
  11064. (defconst org-remember-help
  11065. "Select a destination location for the note.
  11066. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  11067. RET at beg-of-buf -> Append to file as level 2 headline
  11068. RET on headline -> Store as sublevel entry to current headline
  11069. <left>/<right> -> before/after current headline, same headings level")
  11070. ;;;###autoload
  11071. (defun org-remember-apply-template (&optional use-char skip-interactive)
  11072. "Initialize *remember* buffer with template, invoke `org-mode'.
  11073. This function should be placed into `remember-mode-hook' and in fact requires
  11074. to be run from that hook to fucntion properly."
  11075. (if org-remember-templates
  11076. (let* ((char (or use-char
  11077. (if (= (length org-remember-templates) 1)
  11078. (caar org-remember-templates)
  11079. (message "Select template: %s"
  11080. (mapconcat
  11081. (lambda (x) (char-to-string (car x)))
  11082. org-remember-templates " "))
  11083. (read-char-exclusive))))
  11084. (entry (cdr (assoc char org-remember-templates)))
  11085. (tpl (car entry))
  11086. (plist-p (if org-store-link-plist t nil))
  11087. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  11088. (string-match "\\S-" (nth 1 entry)))
  11089. (nth 1 entry)
  11090. org-default-notes-file))
  11091. (headline (nth 2 entry))
  11092. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  11093. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  11094. (v-u (concat "[" (substring v-t 1 -1) "]"))
  11095. (v-U (concat "[" (substring v-T 1 -1) "]"))
  11096. (v-i initial) ; defined in `remember-mode'
  11097. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  11098. (v-n user-full-name)
  11099. (org-startup-folded nil)
  11100. org-time-was-given org-end-time-was-given x prompt char time)
  11101. (setq org-store-link-plist
  11102. (append (list :annotation v-a :initial v-i)
  11103. org-store-link-plist))
  11104. (unless tpl (setq tpl "") (message "No template") (ding))
  11105. (erase-buffer)
  11106. (insert (substitute-command-keys
  11107. (format
  11108. "## `%sC-c C-c' to file directly, `%sC-c C-c' to file interactively.
  11109. ## Target file \"%s\", headline \"%s\"
  11110. ## To switch templates, use `\\[org-remember]'.\n\n"
  11111. (if org-remember-store-without-prompt "" "C-u ")
  11112. (if org-remember-store-without-prompt "C-u " "")
  11113. (abbreviate-file-name (or file org-default-notes-file))
  11114. (or headline ""))))
  11115. (insert tpl) (goto-char (point-min))
  11116. ;; Simple %-escapes
  11117. (while (re-search-forward "%\\([tTuUai]\\)" nil t)
  11118. (when (and initial (equal (match-string 0) "%i"))
  11119. (save-match-data
  11120. (let* ((lead (buffer-substring
  11121. (point-at-bol) (match-beginning 0))))
  11122. (setq v-i (mapconcat 'identity
  11123. (org-split-string initial "\n")
  11124. (concat "\n" lead))))))
  11125. (replace-match
  11126. (or (eval (intern (concat "v-" (match-string 1)))) "")
  11127. t t))
  11128. ;; From the property list
  11129. (when plist-p
  11130. (goto-char (point-min))
  11131. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  11132. (and (setq x (plist-get org-store-link-plist
  11133. (intern (match-string 1))))
  11134. (replace-match x t t))))
  11135. ;; Turn on org-mode in the remember buffer, set local variables
  11136. (org-mode)
  11137. (org-set-local 'org-finish-function 'remember-buffer)
  11138. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  11139. (org-set-local 'org-default-notes-file file))
  11140. (if (and headline (stringp headline) (string-match "\\S-" headline))
  11141. (org-set-local 'org-remember-default-headline headline))
  11142. ;; Interactive template entries
  11143. (goto-char (point-min))
  11144. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  11145. (setq char (if (match-end 3) (match-string 3))
  11146. prompt (if (match-end 2) (match-string 2)))
  11147. (goto-char (match-beginning 0))
  11148. (replace-match "")
  11149. (cond
  11150. ((member char '("G" "g"))
  11151. (let* ((org-last-tags-completion-table
  11152. (org-global-tags-completion-table
  11153. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  11154. (org-add-colon-after-tag-completion t)
  11155. (ins (completing-read
  11156. (if prompt (concat prompt ": ") "Tags: ")
  11157. 'org-tags-completion-function nil nil nil
  11158. 'org-tags-history)))
  11159. (setq ins (mapconcat 'identity
  11160. (org-split-string ins (org-re "[^[:alnum:]]+"))
  11161. ":"))
  11162. (when (string-match "\\S-" ins)
  11163. (or (equal (char-before) ?:) (insert ":"))
  11164. (insert ins)
  11165. (or (equal (char-after) ?:) (insert ":")))))
  11166. (char
  11167. (setq org-time-was-given (equal (upcase char) char))
  11168. (setq time (org-read-date (equal (upcase char) "U") t nil
  11169. prompt))
  11170. (org-insert-time-stamp time org-time-was-given
  11171. (member char '("u" "U"))
  11172. nil nil (list org-end-time-was-given)))
  11173. (t
  11174. (insert (read-string
  11175. (if prompt (concat prompt ": ") "Enter string"))))))
  11176. (goto-char (point-min))
  11177. (if (re-search-forward "%\\?" nil t)
  11178. (replace-match "")
  11179. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  11180. (org-mode)
  11181. (org-set-local 'org-finish-function 'remember-buffer)))
  11182. ;;;###autoload
  11183. (defun org-remember ()
  11184. "Call `remember'. If this is already a remember buffer, re-apply template.
  11185. If there is an active region, make sure remember uses it as initial content
  11186. of the remember buffer."
  11187. (interactive)
  11188. (if (eq org-finish-function 'remember-buffer)
  11189. (progn
  11190. (when (< (length org-remember-templates) 2)
  11191. (error "No other template available"))
  11192. (erase-buffer)
  11193. (let ((annotation (plist-get org-store-link-plist :annotation))
  11194. (initial (plist-get org-store-link-plist :initial)))
  11195. (org-remember-apply-template))
  11196. (message "Press C-c C-c to remember data"))
  11197. (if (org-region-active-p)
  11198. (remember (buffer-substring (point) (mark)))
  11199. (call-interactively 'remember))))
  11200. ;;;###autoload
  11201. (defun org-remember-handler ()
  11202. "Store stuff from remember.el into an org file.
  11203. First prompts for an org file. If the user just presses return, the value
  11204. of `org-default-notes-file' is used.
  11205. Then the command offers the headings tree of the selected file in order to
  11206. file the text at a specific location.
  11207. You can either immediately press RET to get the note appended to the
  11208. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  11209. find a better place. Then press RET or <left> or <right> in insert the note.
  11210. Key Cursor position Note gets inserted
  11211. -----------------------------------------------------------------------------
  11212. RET buffer-start as level 2 heading at end of file
  11213. RET on headline as sublevel of the heading at cursor
  11214. RET no heading at cursor position, level taken from context.
  11215. Or use prefix arg to specify level manually.
  11216. <left> on headline as same level, before current heading
  11217. <right> on headline as same level, after current heading
  11218. So the fastest way to store the note is to press RET RET to append it to
  11219. the default file. This way your current train of thought is not
  11220. interrupted, in accordance with the principles of remember.el.
  11221. You can also get the fast execution without prompting by using
  11222. C-u C-c C-c to exit the remember buffer. See also the variable
  11223. `org-remember-store-without-prompt'.
  11224. Before being stored away, the function ensures that the text has a
  11225. headline, i.e. a first line that starts with a \"*\". If not, a headline
  11226. is constructed from the current date and some additional data.
  11227. If the variable `org-adapt-indentation' is non-nil, the entire text is
  11228. also indented so that it starts in the same column as the headline
  11229. \(i.e. after the stars).
  11230. See also the variable `org-reverse-note-order'."
  11231. (goto-char (point-min))
  11232. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  11233. (replace-match ""))
  11234. (catch 'quit
  11235. (let* ((txt (buffer-substring (point-min) (point-max)))
  11236. (fastp (org-xor (equal current-prefix-arg '(4))
  11237. org-remember-store-without-prompt))
  11238. (file (if fastp org-default-notes-file (org-get-org-file)))
  11239. (heading org-remember-default-headline)
  11240. (visiting (org-find-base-buffer-visiting file))
  11241. (org-startup-folded nil)
  11242. (org-startup-align-all-tables nil)
  11243. (org-goto-start-pos 1)
  11244. spos level indent reversed)
  11245. (setq current-prefix-arg nil)
  11246. ;; Modify text so that it becomes a nice subtree which can be inserted
  11247. ;; into an org tree.
  11248. (let* ((lines (split-string txt "\n"))
  11249. first)
  11250. (setq first (car lines) lines (cdr lines))
  11251. (if (string-match "^\\*+ " first)
  11252. ;; Is already a headline
  11253. (setq indent nil)
  11254. ;; We need to add a headline: Use time and first buffer line
  11255. (setq lines (cons first lines)
  11256. first (concat "* " (current-time-string)
  11257. " (" (remember-buffer-desc) ")")
  11258. indent " "))
  11259. (if (and org-adapt-indentation indent)
  11260. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  11261. (setq txt (concat first "\n"
  11262. (mapconcat 'identity lines "\n"))))
  11263. ;; Find the file
  11264. (if (not visiting) (find-file-noselect file))
  11265. (with-current-buffer (or visiting (get-file-buffer file))
  11266. (save-excursion
  11267. (save-restriction
  11268. (widen)
  11269. (and (goto-char (point-min))
  11270. (not (re-search-forward "^\\* " nil t))
  11271. (insert "\n* Notes\n"))
  11272. (setq reversed (org-notes-order-reversed-p))
  11273. ;; Find the default location
  11274. (when (and heading (stringp heading) (string-match "\\S-" heading))
  11275. (goto-char (point-min))
  11276. (if (re-search-forward
  11277. (concat "^\\*+[ \t]+" (regexp-quote heading)
  11278. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  11279. nil t)
  11280. (setq org-goto-start-pos (match-beginning 0))))
  11281. ;; Ask the User for a location
  11282. (setq spos (if fastp
  11283. org-goto-start-pos
  11284. (org-get-location (current-buffer) org-remember-help)))
  11285. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  11286. ; not handle this note
  11287. (goto-char spos)
  11288. (cond ((and (bobp) (not reversed))
  11289. ;; Put it at the end, one level below level 1
  11290. (save-restriction
  11291. (widen)
  11292. (goto-char (point-max))
  11293. (if (not (bolp)) (newline))
  11294. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  11295. ((and (bobp) reversed)
  11296. ;; Put it at the start, as level 1
  11297. (save-restriction
  11298. (widen)
  11299. (goto-char (point-min))
  11300. (re-search-forward "^\\*+ " nil t)
  11301. (beginning-of-line 1)
  11302. (org-paste-subtree 1 txt)))
  11303. ((and (org-on-heading-p t) (not current-prefix-arg))
  11304. ;; Put it below this entry, at the beg/end of the subtree
  11305. (org-back-to-heading t)
  11306. (setq level (funcall outline-level))
  11307. (if reversed
  11308. (outline-next-heading)
  11309. (org-end-of-subtree t))
  11310. (if (not (bolp)) (newline))
  11311. (beginning-of-line 1)
  11312. (org-paste-subtree (org-get-legal-level level 1) txt))
  11313. (t
  11314. ;; Put it right there, with automatic level determined by
  11315. ;; org-paste-subtree or from prefix arg
  11316. (org-paste-subtree
  11317. (if (numberp current-prefix-arg) current-prefix-arg)
  11318. txt)))
  11319. (when remember-save-after-remembering
  11320. (save-buffer)
  11321. (if (not visiting) (kill-buffer (current-buffer)))))))))
  11322. t) ;; return t to indicate that we took care of this note.
  11323. (defun org-get-org-file ()
  11324. "Read a filename, with default directory `org-directory'."
  11325. (let ((default (or org-default-notes-file remember-data-file)))
  11326. (read-file-name (format "File name [%s]: " default)
  11327. (file-name-as-directory org-directory)
  11328. default)))
  11329. (defun org-notes-order-reversed-p ()
  11330. "Check if the current file should receive notes in reversed order."
  11331. (cond
  11332. ((not org-reverse-note-order) nil)
  11333. ((eq t org-reverse-note-order) t)
  11334. ((not (listp org-reverse-note-order)) nil)
  11335. (t (catch 'exit
  11336. (let ((all org-reverse-note-order)
  11337. entry)
  11338. (while (setq entry (pop all))
  11339. (if (string-match (car entry) buffer-file-name)
  11340. (throw 'exit (cdr entry))))
  11341. nil)))))
  11342. ;;;; Dynamic blocks
  11343. (defun org-find-dblock (name)
  11344. "Find the first dynamic block with name NAME in the buffer.
  11345. If not found, stay at current position and return nil."
  11346. (let (pos)
  11347. (save-excursion
  11348. (goto-char (point-min))
  11349. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  11350. nil t)
  11351. (match-beginning 0))))
  11352. (if pos (goto-char pos))
  11353. pos))
  11354. (defconst org-dblock-start-re
  11355. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  11356. "Matches the startline of a dynamic block, with parameters.")
  11357. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  11358. "Matches the end of a dyhamic block.")
  11359. (defun org-create-dblock (plist)
  11360. "Create a dynamic block section, with parameters taken from PLIST.
  11361. PLIST must containe a :name entry which is used as name of the block."
  11362. (unless (bolp) (newline))
  11363. (let ((name (plist-get plist :name)))
  11364. (insert "#+BEGIN: " name)
  11365. (while plist
  11366. (if (eq (car plist) :name)
  11367. (setq plist (cddr plist))
  11368. (insert " " (prin1-to-string (pop plist)))))
  11369. (insert "\n\n#+END:\n")
  11370. (beginning-of-line -2)))
  11371. (defun org-prepare-dblock ()
  11372. "Prepare dynamic block for refresh.
  11373. This empties the block, puts the cursor at the insert position and returns
  11374. the property list including an extra property :name with the block name."
  11375. (unless (looking-at org-dblock-start-re)
  11376. (error "Not at a dynamic block"))
  11377. (let* ((begdel (1+ (match-end 0)))
  11378. (name (match-string 1))
  11379. (params (append (list :name name)
  11380. (read (concat "(" (match-string 3) ")")))))
  11381. (unless (re-search-forward org-dblock-end-re nil t)
  11382. (error "Dynamic block not terminated"))
  11383. (delete-region begdel (match-beginning 0))
  11384. (goto-char begdel)
  11385. (open-line 1)
  11386. params))
  11387. (defun org-map-dblocks (&optional command)
  11388. "Apply COMMAND to all dynamic blocks in the current buffer.
  11389. If COMMAND is not given, use `org-update-dblock'."
  11390. (let ((cmd (or command 'org-update-dblock))
  11391. pos)
  11392. (save-excursion
  11393. (goto-char (point-min))
  11394. (while (re-search-forward org-dblock-start-re nil t)
  11395. (goto-char (setq pos (match-beginning 0)))
  11396. (condition-case nil
  11397. (funcall cmd)
  11398. (error (message "Error during update of dynamic block")))
  11399. (goto-char pos)
  11400. (unless (re-search-forward org-dblock-end-re nil t)
  11401. (error "Dynamic block not terminated"))))))
  11402. (defun org-dblock-update (&optional arg)
  11403. "User command for updating dynamic blocks.
  11404. Update the dynamic block at point. With prefix ARG, update all dynamic
  11405. blocks in the buffer."
  11406. (interactive "P")
  11407. (if arg
  11408. (org-update-all-dblocks)
  11409. (or (looking-at org-dblock-start-re)
  11410. (org-beginning-of-dblock))
  11411. (org-update-dblock)))
  11412. (defun org-update-dblock ()
  11413. "Update the dynamic block at point
  11414. This means to empty the block, parse for parameters and then call
  11415. the correct writing function."
  11416. (let* ((pos (point))
  11417. (params (org-prepare-dblock))
  11418. (name (plist-get params :name))
  11419. (cmd (intern (concat "org-dblock-write:" name))))
  11420. (funcall cmd params)
  11421. (goto-char pos)))
  11422. (defun org-beginning-of-dblock ()
  11423. "Find the beginning of the dynamic block at point.
  11424. Error if there is no scuh block at point."
  11425. (let ((pos (point))
  11426. beg)
  11427. (end-of-line 1)
  11428. (if (and (re-search-backward org-dblock-start-re nil t)
  11429. (setq beg (match-beginning 0))
  11430. (re-search-forward org-dblock-end-re nil t)
  11431. (> (match-end 0) pos))
  11432. (goto-char beg)
  11433. (goto-char pos)
  11434. (error "Not in a dynamic block"))))
  11435. (defun org-update-all-dblocks ()
  11436. "Update all dynamic blocks in the buffer.
  11437. This function can be used in a hook."
  11438. (when (org-mode-p)
  11439. (org-map-dblocks 'org-update-dblock)))
  11440. ;;;; Completion
  11441. (defun org-complete (&optional arg)
  11442. "Perform completion on word at point.
  11443. At the beginning of a headline, this completes TODO keywords as given in
  11444. `org-todo-keywords'.
  11445. If the current word is preceded by a backslash, completes the TeX symbols
  11446. that are supported for HTML support.
  11447. If the current word is preceded by \"#+\", completes special words for
  11448. setting file options.
  11449. In the line after \"#+STARTUP:, complete valid keywords.\"
  11450. At all other locations, this simply calls `ispell-complete-word'."
  11451. (interactive "P")
  11452. (catch 'exit
  11453. (let* ((end (point))
  11454. (beg1 (save-excursion
  11455. (skip-chars-backward (org-re "[:alnum:]_@"))
  11456. (point)))
  11457. (beg (save-excursion
  11458. (skip-chars-backward "a-zA-Z0-9_:$")
  11459. (point)))
  11460. (confirm (lambda (x) (stringp (car x))))
  11461. (searchhead (equal (char-before beg) ?*))
  11462. (tag (and (equal (char-before beg1) ?:)
  11463. (equal (char-after (point-at-bol)) ?*)))
  11464. (prop (and (equal (char-before beg1) ?:)
  11465. (not (equal (char-after (point-at-bol)) ?*))))
  11466. (texp (equal (char-before beg) ?\\))
  11467. (link (equal (char-before beg) ?\[))
  11468. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  11469. beg)
  11470. "#+"))
  11471. (startup (string-match "^#\\+STARTUP:.*"
  11472. (buffer-substring (point-at-bol) (point))))
  11473. (completion-ignore-case opt)
  11474. (type nil)
  11475. (tbl nil)
  11476. (table (cond
  11477. (opt
  11478. (setq type :opt)
  11479. (mapcar (lambda (x)
  11480. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  11481. (cons (match-string 2 x) (match-string 1 x)))
  11482. (org-split-string (org-get-current-options) "\n")))
  11483. (startup
  11484. (setq type :startup)
  11485. org-startup-options)
  11486. (link (append org-link-abbrev-alist-local
  11487. org-link-abbrev-alist))
  11488. (texp
  11489. (setq type :tex)
  11490. org-html-entities)
  11491. ((string-match "\\`\\*+[ \t]+\\'"
  11492. (buffer-substring (point-at-bol) beg))
  11493. (setq type :todo)
  11494. (mapcar 'list org-todo-keywords-1))
  11495. (searchhead
  11496. (setq type :searchhead)
  11497. (save-excursion
  11498. (goto-char (point-min))
  11499. (while (re-search-forward org-todo-line-regexp nil t)
  11500. (push (list
  11501. (org-make-org-heading-search-string
  11502. (match-string 3) t))
  11503. tbl)))
  11504. tbl)
  11505. (tag (setq type :tag beg beg1)
  11506. (or org-tag-alist (org-get-buffer-tags)))
  11507. (prop (setq type :prop beg beg1)
  11508. (mapcar 'list (org-buffer-property-keys)))
  11509. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  11510. (pattern (buffer-substring-no-properties beg end))
  11511. (completion (try-completion pattern table confirm)))
  11512. (cond ((eq completion t)
  11513. (if (equal type :opt)
  11514. (insert (substring (cdr (assoc (upcase pattern) table))
  11515. (length pattern)))
  11516. (if (memq type '(:tag :prop)) (insert ":"))))
  11517. ((null completion)
  11518. (message "Can't find completion for \"%s\"" pattern)
  11519. (ding))
  11520. ((not (string= pattern completion))
  11521. (delete-region beg end)
  11522. (if (string-match " +$" completion)
  11523. (setq completion (replace-match "" t t completion)))
  11524. (insert completion)
  11525. (if (get-buffer-window "*Completions*")
  11526. (delete-window (get-buffer-window "*Completions*")))
  11527. (if (assoc completion table)
  11528. (if (eq type :todo) (insert " ")
  11529. (if (memq type '(:tag :prop)) (insert ":"))))
  11530. (if (and (equal type :opt) (assoc completion table))
  11531. (message "%s" (substitute-command-keys
  11532. "Press \\[org-complete] again to insert example settings"))))
  11533. (t
  11534. (message "Making completion list...")
  11535. (let ((list (sort (all-completions pattern table confirm)
  11536. 'string<)))
  11537. (with-output-to-temp-buffer "*Completions*"
  11538. (condition-case nil
  11539. ;; Protection needed for XEmacs and emacs 21
  11540. (display-completion-list list pattern)
  11541. (error (display-completion-list list)))))
  11542. (message "Making completion list...%s" "done"))))))
  11543. ;;;; TODO, DEADLINE, Comments
  11544. (defun org-toggle-comment ()
  11545. "Change the COMMENT state of an entry."
  11546. (interactive)
  11547. (save-excursion
  11548. (org-back-to-heading)
  11549. (if (looking-at (concat outline-regexp
  11550. "\\( *\\<" org-comment-string "\\>\\)"))
  11551. (replace-match "" t t nil 1)
  11552. (if (looking-at outline-regexp)
  11553. (progn
  11554. (goto-char (match-end 0))
  11555. (insert org-comment-string " "))))))
  11556. (defvar org-last-todo-state-is-todo nil
  11557. "This is non-nil when the last TODO state change led to a TODO state.
  11558. If the last change removed the TODO tag or switched to DONE, then
  11559. this is nil.")
  11560. (defvar org-setting-tags nil) ; dynamically skiped
  11561. (defun org-todo (&optional arg)
  11562. "Change the TODO state of an item.
  11563. The state of an item is given by a keyword at the start of the heading,
  11564. like
  11565. *** TODO Write paper
  11566. *** DONE Call mom
  11567. The different keywords are specified in the variable `org-todo-keywords'.
  11568. By default the available states are \"TODO\" and \"DONE\".
  11569. So for this example: when the item starts with TODO, it is changed to DONE.
  11570. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11571. DONE are present, add TODO at the beginning of the heading.
  11572. With C-u prefix arg, use completion to determine the new state.
  11573. With numeric prefix arg, switch to that state.
  11574. For calling through lisp, arg is also interpreted in the following way:
  11575. 'none -> empty state
  11576. \"\"(empty string) -> switch to empty state
  11577. 'done -> switch to DONE
  11578. 'nextset -> switch to the next set of keywords
  11579. 'previousset -> switch to the previous set of keywords
  11580. \"WAITING\" -> switch to the specified keyword, but only if it
  11581. really is a member of `org-todo-keywords'."
  11582. (interactive "P")
  11583. (save-excursion
  11584. (org-back-to-heading)
  11585. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  11586. (or (looking-at (concat " +" org-todo-regexp " *"))
  11587. (looking-at " *"))
  11588. (let* ((this (match-string 1))
  11589. (head (org-get-todo-sequence-head this))
  11590. (ass (assoc head org-todo-kwd-alist))
  11591. (interpret (nth 1 ass))
  11592. (done-word (nth 3 ass))
  11593. (final-done-word (nth 4 ass))
  11594. (last-state (or this ""))
  11595. (completion-ignore-case t)
  11596. (member (member this org-todo-keywords-1))
  11597. (tail (cdr member))
  11598. (state (cond
  11599. ((equal arg '(4))
  11600. ;; Read a state with completion
  11601. (completing-read "State: " (mapcar (lambda(x) (list x))
  11602. org-todo-keywords-1)
  11603. nil t))
  11604. ((eq arg 'right)
  11605. (if this
  11606. (if tail (car tail) nil)
  11607. (car org-todo-keywords-1)))
  11608. ((eq arg 'left)
  11609. (if (equal member org-todo-keywords-1)
  11610. nil
  11611. (if this
  11612. (nth (- (length org-todo-keywords-1) (length tail) 2)
  11613. org-todo-keywords-1)
  11614. (org-last org-todo-keywords-1))))
  11615. (arg
  11616. ;; user or caller requests a specific state
  11617. (cond
  11618. ((equal arg "") nil)
  11619. ((eq arg 'none) nil)
  11620. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11621. ((eq arg 'nextset)
  11622. (or (car (cdr (member head org-todo-heads)))
  11623. (car org-todo-heads)))
  11624. ((eq arg 'previousset)
  11625. (let ((org-todo-heads (reverse org-todo-heads)))
  11626. (or (car (cdr (member head org-todo-heads)))
  11627. (car org-todo-heads))))
  11628. ((car (member arg org-todo-keywords-1)))
  11629. ((nth (1- (prefix-numeric-value arg))
  11630. org-todo-keywords-1))))
  11631. ((null member) (or head (car org-todo-keywords-1)))
  11632. ((equal this final-done-word) nil) ;; -> make empty
  11633. ((null tail) nil) ;; -> first entry
  11634. ((eq interpret 'sequence)
  11635. (car tail))
  11636. ((memq interpret '(type priority))
  11637. (if (eq this-command last-command)
  11638. (car tail)
  11639. (if (> (length tail) 0)
  11640. (or done-word (car org-done-keywords))
  11641. nil)))
  11642. (t nil)))
  11643. (next (if state (concat " " state " ") " "))
  11644. dostates)
  11645. (replace-match next t t)
  11646. (unless head
  11647. (setq head (org-get-todo-sequence-head state)
  11648. ass (assoc head org-todo-kwd-alist)
  11649. interpret (nth 1 ass)
  11650. done-word (nth 3 ass)
  11651. final-done-word (nth 4 ass)))
  11652. (when (memq arg '(nextset previousset))
  11653. (message "Keyword-Set %d/%d: %s"
  11654. (- (length org-todo-sets) -1
  11655. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  11656. (length org-todo-sets)
  11657. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  11658. (setq org-last-todo-state-is-todo
  11659. (not (member state org-done-keywords)))
  11660. (when (and org-log-done (not (memq arg '(nextset previousset))))
  11661. (setq dostates (and (eq interpret 'sequence)
  11662. (listp org-log-done) (memq 'state org-log-done)))
  11663. (cond
  11664. ((and state (not this))
  11665. ;; FIXME: should we remove CLOSED already then state is nil?
  11666. (org-add-planning-info nil nil 'closed)
  11667. (and dostates (org-add-log-maybe 'state state 'findpos)))
  11668. ((and state dostates)
  11669. (org-add-log-maybe 'state state 'findpos))
  11670. ((member state org-done-keywords)
  11671. ;; Planning info calls the note-setting command.
  11672. ;; FIXME: We used to remove scheduling info....
  11673. ; (org-add-planning-info 'closed (org-current-time)
  11674. ; (if (org-get-repeat) nil 'scheduled))
  11675. (org-add-planning-info 'closed (org-current-time))
  11676. (org-add-log-maybe 'done state 'findpos))))
  11677. ;; Fixup tag positioning
  11678. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11679. (run-hooks 'org-after-todo-state-change-hook)
  11680. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  11681. (if (and arg (not (member state org-done-keywords)))
  11682. (setq head (org-get-todo-sequence-head state)))
  11683. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  11684. ;; Fixup cursor location if close to the keyword
  11685. (if (and (outline-on-heading-p)
  11686. (not (bolp))
  11687. (save-excursion (beginning-of-line 1)
  11688. (looking-at org-todo-line-regexp))
  11689. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11690. (progn
  11691. (goto-char (or (match-end 2) (match-end 1)))
  11692. (just-one-space))))
  11693. (defun org-get-todo-sequence-head (kwd)
  11694. "Return the head of the TODO sequence to which KWD belongs.
  11695. If KWD is not set, check if there is a text property remembering the
  11696. right sequence."
  11697. (let (p)
  11698. (cond
  11699. ((not kwd)
  11700. (or (get-text-property (point-at-bol) 'org-todo-head)
  11701. (progn
  11702. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11703. nil (point-at-eol)))
  11704. (get-text-property p 'org-todo-head))))
  11705. ((not (member kwd org-todo-keywords-1))
  11706. (car org-todo-keywords-1))
  11707. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11708. (defun org-get-repeat ()
  11709. "Check if tere is a deadline/schedule with repeater in this entry."
  11710. (save-match-data
  11711. (save-excursion
  11712. (org-back-to-heading t)
  11713. (if (re-search-forward
  11714. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  11715. (match-string 1)))))
  11716. (defvar org-last-changed-timestamp)
  11717. (defvar org-log-post-message)
  11718. (defun org-auto-repeat-maybe ()
  11719. "Check if the current headline contains a repeated deadline/schedule.
  11720. If yes, set TODO state back to what it was and change the base date
  11721. of repeating deadline/scheduled time stamps to new date.
  11722. This function should be run in the `org-after-todo-state-change-hook'."
  11723. ;; last-state is dynamically scoped into this function
  11724. (let* ((repeat (org-get-repeat))
  11725. (aa (assoc last-state org-todo-kwd-alist))
  11726. (interpret (nth 1 aa))
  11727. (head (nth 2 aa))
  11728. (done-word (nth 3 aa))
  11729. (whata '(("d" . day) ("m" . month) ("y" . year)))
  11730. (msg "Entry repeats: ")
  11731. (org-log-done)
  11732. re type n what ts)
  11733. (when repeat
  11734. (org-todo (if (eq interpret 'type) last-state head))
  11735. (when (and org-log-repeat
  11736. (not (memq 'org-add-log-note
  11737. (default-value 'post-command-hook))))
  11738. ;; Make sure a note is taken
  11739. (let ((org-log-done '(done)))
  11740. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  11741. 'findpos)))
  11742. (org-back-to-heading t)
  11743. (org-add-planning-info nil nil 'closed)
  11744. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11745. org-deadline-time-regexp "\\)"))
  11746. (while (re-search-forward
  11747. re (save-excursion (outline-next-heading) (point)) t)
  11748. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  11749. ts (match-string (if (match-end 2) 2 4)))
  11750. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  11751. (setq n (string-to-number (match-string 1 ts))
  11752. what (match-string 2 ts))
  11753. (if (equal what "w") (setq n (* n 7) what "d"))
  11754. (org-timestamp-change n (cdr (assoc what whata))))
  11755. (setq msg (concat msg type org-last-changed-timestamp " ")))
  11756. (setq org-log-post-message msg)
  11757. (message msg))))
  11758. (defun org-show-todo-tree (arg)
  11759. "Make a compact tree which shows all headlines marked with TODO.
  11760. The tree will show the lines where the regexp matches, and all higher
  11761. headlines above the match.
  11762. With \\[universal-argument] prefix, also show the DONE entries.
  11763. With a numeric prefix N, construct a sparse tree for the Nth element
  11764. of `org-todo-keywords-1'."
  11765. (interactive "P")
  11766. (let ((case-fold-search nil)
  11767. (kwd-re
  11768. (cond ((null arg) org-not-done-regexp)
  11769. ((equal arg '(4))
  11770. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  11771. (mapcar 'list org-todo-keywords-1))))
  11772. (concat "\\("
  11773. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11774. "\\)\\>")))
  11775. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11776. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11777. org-todo-keywords-1)))
  11778. (t (error "Invalid prefix argument: %s" arg)))))
  11779. (message "%d TODO entries found"
  11780. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  11781. (defun org-deadline ()
  11782. "Insert the DEADLINE: string to make a deadline.
  11783. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11784. to modify it to the correct date."
  11785. (interactive)
  11786. (org-add-planning-info 'deadline nil 'closed))
  11787. (defun org-schedule ()
  11788. "Insert the SCHEDULED: string to schedule a TODO item.
  11789. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11790. to modify it to the correct date."
  11791. (interactive)
  11792. (org-add-planning-info 'scheduled nil 'closed))
  11793. (defun org-add-planning-info (what &optional time &rest remove)
  11794. "Insert new timestamp with keyword in the line directly after the headline.
  11795. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  11796. If non is given, the user is prompted for a date.
  11797. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11798. be removed."
  11799. (interactive)
  11800. (let (org-time-was-given org-end-time-was-given)
  11801. (when what (setq time (or time (org-read-date nil 'to-time))))
  11802. (when (and org-insert-labeled-timestamps-at-point
  11803. (member what '(scheduled deadline)))
  11804. (insert
  11805. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11806. (org-insert-time-stamp time org-time-was-given
  11807. nil nil nil (list org-end-time-was-given))
  11808. (setq what nil))
  11809. (save-excursion
  11810. (save-restriction
  11811. (let (col list elt ts buffer-invisibility-spec)
  11812. (org-back-to-heading t)
  11813. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  11814. (goto-char (match-end 1))
  11815. (setq col (current-column))
  11816. (goto-char (match-end 0))
  11817. (if (eobp) (insert "\n"))
  11818. (forward-char 1)
  11819. (when (and (not org-insert-labeled-timestamps-before-properties-drawer)
  11820. (looking-at "[ \t]*:PROPERTIES:[ \t]*$"))
  11821. (goto-char (match-end 0))
  11822. (if (eobp) (insert "\n"))
  11823. (forward-char 1))
  11824. (if (and (not (looking-at outline-regexp))
  11825. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11826. "[^\r\n]*"))
  11827. (not (equal (match-string 1) org-clock-string)))
  11828. (narrow-to-region (match-beginning 0) (match-end 0))
  11829. (insert-before-markers "\n")
  11830. (backward-char 1)
  11831. (narrow-to-region (point) (point))
  11832. (indent-to-column col))
  11833. ;; Check if we have to remove something.
  11834. (setq list (cons what remove))
  11835. (while list
  11836. (setq elt (pop list))
  11837. (goto-char (point-min))
  11838. (when (or (and (eq elt 'scheduled)
  11839. (re-search-forward org-scheduled-time-regexp nil t))
  11840. (and (eq elt 'deadline)
  11841. (re-search-forward org-deadline-time-regexp nil t))
  11842. (and (eq elt 'closed)
  11843. (re-search-forward org-closed-time-regexp nil t)))
  11844. (replace-match "")
  11845. (if (looking-at "--+<[^>]+>") (replace-match ""))
  11846. (if (looking-at " +") (replace-match ""))))
  11847. (goto-char (point-max))
  11848. (when what
  11849. (insert
  11850. (if (not (equal (char-before) ?\ )) " " "")
  11851. (cond ((eq what 'scheduled) org-scheduled-string)
  11852. ((eq what 'deadline) org-deadline-string)
  11853. ((eq what 'closed) org-closed-string))
  11854. " ")
  11855. (org-insert-time-stamp
  11856. time
  11857. (or org-time-was-given
  11858. (and (eq what 'closed) org-log-done-with-time))
  11859. (eq what 'closed)
  11860. nil nil (list org-end-time-was-given))
  11861. (end-of-line 1))
  11862. (goto-char (point-min))
  11863. (widen)
  11864. (if (looking-at "[ \t]+\r?\n")
  11865. (replace-match ""))
  11866. ts)))))
  11867. (defvar org-log-note-marker (make-marker))
  11868. (defvar org-log-note-purpose nil)
  11869. (defvar org-log-note-state nil)
  11870. (defvar org-log-note-window-configuration nil)
  11871. (defvar org-log-note-return-to (make-marker))
  11872. (defvar org-log-post-message nil
  11873. "Message to be displayed after a log note has been stored.
  11874. The auto-repeater uses this.")
  11875. (defun org-add-log-maybe (&optional purpose state findpos)
  11876. "Set up the post command hook to take a note."
  11877. (save-excursion
  11878. (when (and (listp org-log-done)
  11879. (memq purpose org-log-done))
  11880. (when findpos
  11881. (org-back-to-heading t)
  11882. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  11883. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11884. "[^\r\n]*\\)?"))
  11885. (goto-char (match-end 0))
  11886. (unless org-log-states-order-reversed
  11887. (and (= (char-after) ?\n) (forward-char 1))
  11888. (org-skip-over-state-notes)
  11889. (skip-chars-backward " \t\n\r")))
  11890. (move-marker org-log-note-marker (point))
  11891. (setq org-log-note-purpose purpose)
  11892. (setq org-log-note-state state)
  11893. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  11894. (defun org-skip-over-state-notes ()
  11895. "Skip past the list of State notes in an entry."
  11896. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11897. (while (looking-at "[ \t]*- State")
  11898. (condition-case nil
  11899. (org-next-item)
  11900. (error (org-end-of-item)))))
  11901. (defun org-add-log-note (&optional purpose)
  11902. "Pop up a window for taking a note, and add this note later at point."
  11903. (remove-hook 'post-command-hook 'org-add-log-note)
  11904. (setq org-log-note-window-configuration (current-window-configuration))
  11905. (delete-other-windows)
  11906. (move-marker org-log-note-return-to (point))
  11907. (switch-to-buffer (marker-buffer org-log-note-marker))
  11908. (goto-char org-log-note-marker)
  11909. (org-switch-to-buffer-other-window "*Org Note*")
  11910. (erase-buffer)
  11911. (let ((org-inhibit-startup t)) (org-mode))
  11912. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  11913. (cond
  11914. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11915. ((eq org-log-note-purpose 'done) "closed todo item")
  11916. ((eq org-log-note-purpose 'state) "state change")
  11917. (t (error "This should not happen")))))
  11918. (org-set-local 'org-finish-function 'org-store-log-note))
  11919. (defun org-store-log-note ()
  11920. "Finish taking a log note, and insert it to where it belongs."
  11921. (let ((txt (buffer-string))
  11922. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11923. lines ind)
  11924. (kill-buffer (current-buffer))
  11925. (if (string-match "^#.*\n[ \t\n]*" txt)
  11926. (setq txt (replace-match "" t t txt)))
  11927. (if (string-match "\\s-+\\'" txt)
  11928. (setq txt (replace-match "" t t txt)))
  11929. (setq lines (org-split-string txt "\n"))
  11930. (when (and note (string-match "\\S-" note))
  11931. (setq note
  11932. (org-replace-escapes
  11933. note
  11934. (list (cons "%u" (user-login-name))
  11935. (cons "%U" user-full-name)
  11936. (cons "%t" (format-time-string
  11937. (org-time-stamp-format 'long 'inactive)
  11938. (current-time)))
  11939. (cons "%s" (if org-log-note-state
  11940. (concat "\"" org-log-note-state "\"")
  11941. "")))))
  11942. (if lines (setq note (concat note " \\\\")))
  11943. (push note lines))
  11944. (when lines
  11945. (save-excursion
  11946. (set-buffer (marker-buffer org-log-note-marker))
  11947. (save-excursion
  11948. (goto-char org-log-note-marker)
  11949. (move-marker org-log-note-marker nil)
  11950. (end-of-line 1)
  11951. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  11952. (insert " - " (pop lines))
  11953. (org-indent-line-function)
  11954. (beginning-of-line 1)
  11955. (looking-at "[ \t]*")
  11956. (setq ind (concat (match-string 0) " "))
  11957. (end-of-line 1)
  11958. (while lines (insert "\n" ind (pop lines)))))))
  11959. (set-window-configuration org-log-note-window-configuration)
  11960. (with-current-buffer (marker-buffer org-log-note-return-to)
  11961. (goto-char org-log-note-return-to))
  11962. (move-marker org-log-note-return-to nil)
  11963. (and org-log-post-message (message org-log-post-message)))
  11964. (defvar org-occur-highlights nil)
  11965. (make-variable-buffer-local 'org-occur-highlights)
  11966. (defun org-occur (regexp &optional keep-previous callback)
  11967. "Make a compact tree which shows all matches of REGEXP.
  11968. The tree will show the lines where the regexp matches, and all higher
  11969. headlines above the match. It will also show the heading after the match,
  11970. to make sure editing the matching entry is easy.
  11971. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11972. call to `org-occur' will be kept, to allow stacking of calls to this
  11973. command.
  11974. If CALLBACK is non-nil, it is a function which is called to confirm
  11975. that the match should indeed be shown."
  11976. (interactive "sRegexp: \nP")
  11977. (or keep-previous (org-remove-occur-highlights nil nil t))
  11978. (let ((cnt 0))
  11979. (save-excursion
  11980. (goto-char (point-min))
  11981. (if (or (not keep-previous) ; do not want to keep
  11982. (not org-occur-highlights)) ; no previous matches
  11983. ;; hide everything
  11984. (org-overview))
  11985. (while (re-search-forward regexp nil t)
  11986. (when (or (not callback)
  11987. (save-match-data (funcall callback)))
  11988. (setq cnt (1+ cnt))
  11989. (when org-highlight-sparse-tree-matches
  11990. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11991. (org-show-context 'occur-tree))))
  11992. (when org-remove-highlights-with-change
  11993. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11994. nil 'local))
  11995. (unless org-sparse-tree-open-archived-trees
  11996. (org-hide-archived-subtrees (point-min) (point-max)))
  11997. (run-hooks 'org-occur-hook)
  11998. (if (interactive-p)
  11999. (message "%d match(es) for regexp %s" cnt regexp))
  12000. cnt))
  12001. (defun org-show-context (&optional key)
  12002. "Make sure point and context and visible.
  12003. How much context is shown depends upon the variables
  12004. `org-show-hierarchy-above', `org-show-following-heading'. and
  12005. `org-show-siblings'."
  12006. (let ((heading-p (org-on-heading-p t))
  12007. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12008. (following-p (org-get-alist-option org-show-following-heading key))
  12009. (siblings-p (org-get-alist-option org-show-siblings key)))
  12010. (catch 'exit
  12011. ;; Show heading or entry text
  12012. (if heading-p
  12013. (org-flag-heading nil) ; only show the heading
  12014. (and (or (org-invisible-p) (org-invisible-p2))
  12015. (org-show-hidden-entry))) ; show entire entry
  12016. (when following-p
  12017. ;; Show next sibling, or heading below text
  12018. (save-excursion
  12019. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12020. (org-flag-heading nil))))
  12021. (when siblings-p (org-show-siblings))
  12022. (when hierarchy-p
  12023. ;; show all higher headings, possibly with siblings
  12024. (save-excursion
  12025. (while (and (condition-case nil
  12026. (progn (org-up-heading-all 1) t)
  12027. (error nil))
  12028. (not (bobp)))
  12029. (org-flag-heading nil)
  12030. (when siblings-p (org-show-siblings))))))))
  12031. (defun org-reveal (&optional siblings)
  12032. "Show current entry, hierarchy above it, and the following headline.
  12033. This can be used to show a consistent set of context around locations
  12034. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12035. not t for the search context.
  12036. With optional argument SIBLINGS, on each level of the hierarchy all
  12037. siblings are shown. This repairs the tree structure to what it would
  12038. look like when opened with hierarchical calls to `org-cycle'."
  12039. (interactive "P")
  12040. (let ((org-show-hierarchy-above t)
  12041. (org-show-following-heading t)
  12042. (org-show-siblings (if siblings t org-show-siblings)))
  12043. (org-show-context nil)))
  12044. (defun org-highlight-new-match (beg end)
  12045. "Highlight from BEG to END and mark the highlight is an occur headline."
  12046. (let ((ov (org-make-overlay beg end)))
  12047. (org-overlay-put ov 'face 'secondary-selection)
  12048. (push ov org-occur-highlights)))
  12049. (defun org-remove-occur-highlights (&optional beg end noremove)
  12050. "Remove the occur highlights from the buffer.
  12051. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12052. from the `before-change-functions' in the current buffer."
  12053. (interactive)
  12054. (unless org-inhibit-highlight-removal
  12055. (mapc 'org-delete-overlay org-occur-highlights)
  12056. (setq org-occur-highlights nil)
  12057. (unless noremove
  12058. (remove-hook 'before-change-functions
  12059. 'org-remove-occur-highlights 'local))))
  12060. ;;;; Priorities
  12061. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12062. "Regular expression matching the priority indicator.")
  12063. (defvar org-remove-priority-next-time nil)
  12064. (defun org-priority-up ()
  12065. "Increase the priority of the current item."
  12066. (interactive)
  12067. (org-priority 'up))
  12068. (defun org-priority-down ()
  12069. "Decrease the priority of the current item."
  12070. (interactive)
  12071. (org-priority 'down))
  12072. (defun org-priority (&optional action)
  12073. "Change the priority of an item by ARG.
  12074. ACTION can be `set', `up', `down', or a character."
  12075. (interactive)
  12076. (setq action (or action 'set))
  12077. (let (current new news have remove)
  12078. (save-excursion
  12079. (org-back-to-heading)
  12080. (if (looking-at org-priority-regexp)
  12081. (setq current (string-to-char (match-string 2))
  12082. have t)
  12083. (setq current org-default-priority))
  12084. (cond
  12085. ((or (eq action 'set) (integerp action))
  12086. (if (integerp action)
  12087. (setq new action)
  12088. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  12089. (setq new (read-char-exclusive)))
  12090. (cond ((equal new ?\ ) (setq remove t))
  12091. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12092. (error "Priority must be between `%c' and `%c'"
  12093. org-highest-priority org-lowest-priority))))
  12094. ((eq action 'up)
  12095. (setq new (1- current)))
  12096. ((eq action 'down)
  12097. (setq new (1+ current)))
  12098. (t (error "Invalid action")))
  12099. (setq new (min (max org-highest-priority (upcase new)) org-lowest-priority))
  12100. (setq news (format "%c" new))
  12101. (if have
  12102. (if remove
  12103. (replace-match "" t t nil 1)
  12104. (replace-match news t t nil 2))
  12105. (if remove
  12106. (error "No priority cookie found in line")
  12107. (looking-at org-todo-line-regexp)
  12108. (if (match-end 2)
  12109. (progn
  12110. (goto-char (match-end 2))
  12111. (insert " [#" news "]"))
  12112. (goto-char (match-beginning 3))
  12113. (insert "[#" news "] ")))))
  12114. (org-preserve-lc (org-set-tags nil 'align))
  12115. (if remove
  12116. (message "Priority removed")
  12117. (message "Priority of current item set to %s" news))))
  12118. (defun org-get-priority (s)
  12119. "Find priority cookie and return priority."
  12120. (save-match-data
  12121. (if (not (string-match org-priority-regexp s))
  12122. (* 1000 (- org-lowest-priority org-default-priority))
  12123. (* 1000 (- org-lowest-priority
  12124. (string-to-char (match-string 2 s)))))))
  12125. ;;;; Tags
  12126. (defun org-scan-tags (action matcher &optional todo-only)
  12127. "Scan headline tags with inheritance and produce output ACTION.
  12128. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  12129. evaluated, testing if a given set of tags qualifies a headline for
  12130. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  12131. are included in the output."
  12132. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  12133. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12134. (org-re
  12135. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  12136. (props (list 'face nil
  12137. 'done-face 'org-done
  12138. 'undone-face nil
  12139. 'mouse-face 'highlight
  12140. 'org-not-done-regexp org-not-done-regexp
  12141. 'org-todo-regexp org-todo-regexp
  12142. 'keymap org-agenda-keymap
  12143. 'help-echo
  12144. (format "mouse-2 or RET jump to org file %s"
  12145. (abbreviate-file-name buffer-file-name))))
  12146. (case-fold-search nil)
  12147. lspos
  12148. tags tags-list tags-alist (llast 0) rtn level category i txt
  12149. todo marker entry priority)
  12150. (save-excursion
  12151. (goto-char (point-min))
  12152. (when (eq action 'sparse-tree) (org-overview))
  12153. (while (re-search-forward re nil t)
  12154. (catch :skip
  12155. (setq todo (if (match-end 1) (match-string 2))
  12156. tags (if (match-end 4) (match-string 4)))
  12157. (goto-char (setq lspos (1+ (match-beginning 0))))
  12158. (setq level (org-reduced-level (funcall outline-level))
  12159. category (org-get-category))
  12160. (setq i llast llast level)
  12161. ;; remove tag lists from same and sublevels
  12162. (while (>= i level)
  12163. (when (setq entry (assoc i tags-alist))
  12164. (setq tags-alist (delete entry tags-alist)))
  12165. (setq i (1- i)))
  12166. ;; add the nex tags
  12167. (when tags
  12168. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  12169. tags-alist
  12170. (cons (cons level tags) tags-alist)))
  12171. ;; compile tags for current headline
  12172. (setq tags-list
  12173. (if org-use-tag-inheritance
  12174. (apply 'append (mapcar 'cdr tags-alist))
  12175. tags))
  12176. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  12177. (eval matcher)
  12178. (or (not org-agenda-skip-archived-trees)
  12179. (not (member org-archive-tag tags-list))))
  12180. (and (eq action 'agenda) (org-agenda-skip))
  12181. ;; list this headline
  12182. (if (eq action 'sparse-tree)
  12183. (progn
  12184. (org-show-context 'tags-tree))
  12185. (setq txt (org-format-agenda-item
  12186. ""
  12187. (concat
  12188. (if org-tags-match-list-sublevels
  12189. (make-string (1- level) ?.) "")
  12190. (org-get-heading))
  12191. category tags-list)
  12192. priority (org-get-priority txt))
  12193. (goto-char lspos)
  12194. (setq marker (org-agenda-new-marker))
  12195. (org-add-props txt props
  12196. 'org-marker marker 'org-hd-marker marker 'org-category category
  12197. 'priority priority 'type "tagsmatch")
  12198. (push txt rtn))
  12199. ;; if we are to skip sublevels, jump to end of subtree
  12200. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  12201. (when (and (eq action 'sparse-tree)
  12202. (not org-sparse-tree-open-archived-trees))
  12203. (org-hide-archived-subtrees (point-min) (point-max)))
  12204. (nreverse rtn)))
  12205. (defvar todo-only) ;; dynamically scoped
  12206. (defun org-tags-sparse-tree (&optional todo-only match)
  12207. "Create a sparse tree according to tags string MATCH.
  12208. MATCH can contain positive and negative selection of tags, like
  12209. \"+WORK+URGENT-WITHBOSS\".
  12210. If optional argument TODO_ONLY is non-nil, only select lines that are
  12211. also TODO lines."
  12212. (interactive "P")
  12213. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12214. (defvar org-cached-props nil)
  12215. (defun org-cached-entry-get (pom property)
  12216. (cdr (assoc property (or org-cached-props
  12217. (setq org-cached-props
  12218. (org-entry-properties pom))))))
  12219. (defun org-global-tags-completion-table (&optional files)
  12220. "Return the list of all tags in all agenda buffer/files."
  12221. (save-excursion
  12222. (org-uniquify
  12223. (apply 'append
  12224. (mapcar
  12225. (lambda (file)
  12226. (set-buffer (find-file-noselect file))
  12227. (org-get-buffer-tags))
  12228. (if (and files (car files))
  12229. files
  12230. (org-agenda-files)))))))
  12231. (defun org-make-tags-matcher (match)
  12232. "Create the TAGS//TODO matcher form for the selection string MATCH."
  12233. ;; todo-only is scoped dynamically into this function, and the function
  12234. ;; may change it it the matcher asksk for it.
  12235. (unless match
  12236. ;; Get a new match request, with completion
  12237. (let ((org-last-tags-completion-table
  12238. (org-global-tags-completion-table)))
  12239. (setq match (completing-read
  12240. "Match: " 'org-tags-completion-function nil nil nil
  12241. 'org-tags-history))))
  12242. ;; Parse the string and create a lisp form
  12243. (let ((match0 match)
  12244. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  12245. minus tag mm
  12246. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12247. orterms term orlist re-p level-p prop-p pn pv)
  12248. (if (string-match "/+" match)
  12249. ;; match contains also a todo-matching request
  12250. (progn
  12251. (setq tagsmatch (substring match 0 (match-beginning 0))
  12252. todomatch (substring match (match-end 0)))
  12253. (if (string-match "^!" todomatch)
  12254. (setq todo-only t todomatch (substring todomatch 1)))
  12255. (if (string-match "^\\s-*$" todomatch)
  12256. (setq todomatch nil)))
  12257. ;; only matching tags
  12258. (setq tagsmatch match todomatch nil))
  12259. ;; Make the tags matcher
  12260. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12261. (setq tagsmatcher t)
  12262. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12263. (while (setq term (pop orterms))
  12264. (while (and (equal (substring term -1) "\\") orterms)
  12265. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12266. (while (string-match re term)
  12267. (setq minus (and (match-end 1)
  12268. (equal (match-string 1 term) "-"))
  12269. tag (match-string 2 term)
  12270. re-p (equal (string-to-char tag) ?{)
  12271. level-p (match-end 3)
  12272. prop-p (match-end 4)
  12273. mm (cond
  12274. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12275. (level-p `(= level ,(string-to-number
  12276. (match-string 3 term))))
  12277. (prop-p
  12278. (setq pn (match-string 4 term)
  12279. pv (match-string 5 term)
  12280. re-p (equal (string-to-char pv) ?{)
  12281. pv (substring pv 1 -1))
  12282. (if re-p
  12283. `(string-match ,pv (org-cached-entry-get nil ,pn))
  12284. `(equal ,pv (org-cached-entry-get nil ,pn))))
  12285. (t `(member ,(downcase tag) tags-list)))
  12286. mm (if minus (list 'not mm) mm)
  12287. term (substring term (match-end 0)))
  12288. (push mm tagsmatcher))
  12289. (push (if (> (length tagsmatcher) 1)
  12290. (cons 'and tagsmatcher)
  12291. (car tagsmatcher))
  12292. orlist)
  12293. (setq tagsmatcher nil))
  12294. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12295. (setq tagsmatcher
  12296. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12297. ;; Make the todo matcher
  12298. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12299. (setq todomatcher t)
  12300. (setq orterms (org-split-string todomatch "|") orlist nil)
  12301. (while (setq term (pop orterms))
  12302. (while (string-match re term)
  12303. (setq minus (and (match-end 1)
  12304. (equal (match-string 1 term) "-"))
  12305. kwd (match-string 2 term)
  12306. re-p (equal (string-to-char kwd) ?{)
  12307. term (substring term (match-end 0))
  12308. mm (if re-p
  12309. `(string-match ,(substring kwd 1 -1) todo)
  12310. (list 'equal 'todo kwd))
  12311. mm (if minus (list 'not mm) mm))
  12312. (push mm todomatcher))
  12313. (push (if (> (length todomatcher) 1)
  12314. (cons 'and todomatcher)
  12315. (car todomatcher))
  12316. orlist)
  12317. (setq todomatcher nil))
  12318. (setq todomatcher (if (> (length orlist) 1)
  12319. (cons 'or orlist) (car orlist))))
  12320. ;; Return the string and lisp forms of the matcher
  12321. (setq matcher (if todomatcher
  12322. (list 'and tagsmatcher todomatcher)
  12323. tagsmatcher))
  12324. (cons match0 matcher)))
  12325. (defun org-match-any-p (re list)
  12326. "Does re match any element of list?"
  12327. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12328. (delq nil list))
  12329. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  12330. (defvar org-tags-overlay (org-make-overlay 1 1))
  12331. (org-detach-overlay org-tags-overlay)
  12332. (defun org-align-tags-here (to-col)
  12333. ;; Assumes that this is a headline
  12334. (let ((pos (point)) (col (current-column)) tags)
  12335. (beginning-of-line 1)
  12336. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12337. (< pos (match-beginning 2)))
  12338. (progn
  12339. (setq tags (match-string 2))
  12340. (goto-char (match-beginning 1))
  12341. (insert " ")
  12342. (delete-region (point) (1+ (match-end 0)))
  12343. (backward-char 1)
  12344. (move-to-column
  12345. (max (1+ (current-column))
  12346. (1+ col)
  12347. (if (> to-col 0)
  12348. to-col
  12349. (- (abs to-col) (length tags))))
  12350. t)
  12351. (insert tags)
  12352. (move-to-column (min (current-column) col) t))
  12353. (goto-char pos))))
  12354. (defun org-set-tags (&optional arg just-align)
  12355. "Set the tags for the current headline.
  12356. With prefix ARG, realign all tags in headings in the current buffer."
  12357. (interactive "P")
  12358. (let* ((re (concat "^" outline-regexp))
  12359. (current (org-get-tags))
  12360. (col (current-column))
  12361. (org-setting-tags t)
  12362. table current-tags inherited-tags ; computed below when needed
  12363. tags p0 c0 c1 rpl)
  12364. (if arg
  12365. (save-excursion
  12366. (goto-char (point-min))
  12367. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12368. (while (re-search-forward re nil t)
  12369. (org-set-tags nil t)
  12370. (end-of-line 1)))
  12371. (message "All tags realigned to column %d" org-tags-column))
  12372. (if just-align
  12373. (setq tags current)
  12374. ;; Get a new set of tags from the user
  12375. (save-excursion
  12376. (setq table (or org-tag-alist (org-get-buffer-tags))
  12377. org-last-tags-completion-table table
  12378. current-tags (org-split-string current ":")
  12379. inherited-tags (nreverse
  12380. (nthcdr (length current-tags)
  12381. (nreverse (org-get-tags-at))))
  12382. tags
  12383. (if (or (eq t org-use-fast-tag-selection)
  12384. (and org-use-fast-tag-selection
  12385. (delq nil (mapcar 'cdr table))))
  12386. (org-fast-tag-selection current-tags inherited-tags
  12387. table org-todo-tag-alist)
  12388. (let ((org-add-colon-after-tag-completion t))
  12389. (org-trim
  12390. (completing-read "Tags: " 'org-tags-completion-function
  12391. nil nil current 'org-tags-history))))))
  12392. (while (string-match "[-+&]+" tags)
  12393. ;; No boolean logic, just a list
  12394. (setq tags (replace-match ":" t t tags))))
  12395. (if (string-match "\\`[\t ]*\\'" tags)
  12396. (setq tags "")
  12397. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12398. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12399. ;; Insert new tags at the correct column
  12400. (beginning-of-line 1)
  12401. (cond
  12402. ((and (equal current "") (equal tags "")))
  12403. ((re-search-forward
  12404. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12405. (point-at-eol) t)
  12406. (if (equal tags "")
  12407. (setq rpl "")
  12408. (goto-char (match-beginning 0))
  12409. (setq c0 (current-column) p0 (point)
  12410. c1 (max (1+ c0) (if (> org-tags-column 0)
  12411. org-tags-column
  12412. (- (- org-tags-column) (length tags))))
  12413. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12414. (replace-match rpl t t)
  12415. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  12416. tags)
  12417. (t (error "Tags alignment failed")))
  12418. (move-to-column col))))
  12419. (defun org-change-tag-in-region (beg end tag off)
  12420. "Add or remove TAG for each entry in the region.
  12421. This works in the agenda, and also in an org-mode buffer."
  12422. (interactive
  12423. (list (region-beginning) (region-end)
  12424. (let ((org-last-tags-completion-table
  12425. (if (org-mode-p)
  12426. (org-get-buffer-tags)
  12427. (org-global-tags-completion-table))))
  12428. (completing-read
  12429. "Tag: " 'org-tags-completion-function nil nil nil
  12430. 'org-tags-history))
  12431. (progn
  12432. (message "[s]et or [r]emove? ")
  12433. (equal (read-char-exclusive) ?r))))
  12434. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12435. (let ((agendap (equal major-mode 'org-agenda-mode))
  12436. l1 l2 m buf pos newhead (cnt 0))
  12437. (goto-char end)
  12438. (setq l2 (1- (org-current-line)))
  12439. (goto-char beg)
  12440. (setq l1 (org-current-line))
  12441. (loop for l from l1 to l2 do
  12442. (goto-line l)
  12443. (setq m (get-text-property (point) 'org-hd-marker))
  12444. (when (or (and (org-mode-p) (org-on-heading-p))
  12445. (and agendap m))
  12446. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12447. pos (if agendap m (point)))
  12448. (with-current-buffer buf
  12449. (save-excursion
  12450. (save-restriction
  12451. (goto-char pos)
  12452. (setq cnt (1+ cnt))
  12453. (org-toggle-tag tag (if off 'off 'on))
  12454. (setq newhead (org-get-heading)))))
  12455. (and agendap (org-agenda-change-all-lines newhead m))))
  12456. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12457. (defun org-tags-completion-function (string predicate &optional flag)
  12458. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12459. (confirm (lambda (x) (stringp (car x)))))
  12460. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  12461. (setq s1 (match-string 1 string)
  12462. s2 (match-string 2 string))
  12463. (setq s1 "" s2 string))
  12464. (cond
  12465. ((eq flag nil)
  12466. ;; try completion
  12467. (setq rtn (try-completion s2 ctable confirm))
  12468. (if (stringp rtn)
  12469. (setq rtn
  12470. (concat s1 s2 (substring rtn (length s2))
  12471. (if (and org-add-colon-after-tag-completion
  12472. (assoc rtn ctable))
  12473. ":" ""))))
  12474. rtn)
  12475. ((eq flag t)
  12476. ;; all-completions
  12477. (all-completions s2 ctable confirm)
  12478. )
  12479. ((eq flag 'lambda)
  12480. ;; exact match?
  12481. (assoc s2 ctable)))
  12482. ))
  12483. (defun org-fast-tag-insert (kwd tags face &optional end)
  12484. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  12485. (insert (format "%-12s" (concat kwd ":"))
  12486. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12487. (or end "")))
  12488. (defun org-fast-tag-show-exit (flag)
  12489. (save-excursion
  12490. (goto-line 3)
  12491. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12492. (replace-match ""))
  12493. (when flag
  12494. (end-of-line 1)
  12495. (move-to-column (- (window-width) 19) t)
  12496. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12497. (defun org-set-current-tags-overlay (current prefix)
  12498. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12499. (if (featurep 'xemacs)
  12500. (org-overlay-display org-tags-overlay (concat prefix s)
  12501. 'secondary-selection)
  12502. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12503. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12504. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12505. "Fast tag selection with single keys.
  12506. CURRENT is the current list of tags in the headline, INHERITED is the
  12507. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12508. possibly with grouping information. TODO-TABLE is a similar table with
  12509. TODO keywords, should these have keys assigned to them.
  12510. If the keys are nil, a-z are automatically assigned.
  12511. Returns the new tags string, or nil to not change the current settings."
  12512. (let* ((fulltable (append table todo-table))
  12513. (maxlen (apply 'max (mapcar
  12514. (lambda (x)
  12515. (if (stringp (car x)) (string-width (car x)) 0))
  12516. fulltable)))
  12517. (buf (current-buffer))
  12518. (expert (eq org-fast-tag-selection-single-key 'expert))
  12519. (buffer-tags nil)
  12520. (fwidth (+ maxlen 3 1 3))
  12521. (ncol (/ (- (window-width) 4) fwidth))
  12522. (i-face 'org-done)
  12523. (c-face 'org-todo)
  12524. tg cnt e c char c1 c2 ntable tbl rtn
  12525. ov-start ov-end ov-prefix
  12526. (exit-after-next org-fast-tag-selection-single-key)
  12527. groups ingroup)
  12528. (save-excursion
  12529. (beginning-of-line 1)
  12530. (if (looking-at
  12531. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12532. (setq ov-start (match-beginning 1)
  12533. ov-end (match-end 1)
  12534. ov-prefix "")
  12535. (setq ov-start (1- (point-at-eol))
  12536. ov-end (1+ ov-start))
  12537. (skip-chars-forward "^\n\r")
  12538. (setq ov-prefix
  12539. (concat
  12540. (buffer-substring (1- (point)) (point))
  12541. (if (> (current-column) org-tags-column)
  12542. " "
  12543. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12544. (org-move-overlay org-tags-overlay ov-start ov-end)
  12545. (save-window-excursion
  12546. (if expert
  12547. (set-buffer (get-buffer-create " *Org tags*"))
  12548. (delete-other-windows)
  12549. (split-window-vertically)
  12550. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12551. (erase-buffer)
  12552. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12553. (org-fast-tag-insert "Current" current c-face "\n\n")
  12554. (org-fast-tag-show-exit exit-after-next)
  12555. (org-set-current-tags-overlay current ov-prefix)
  12556. (setq tbl fulltable char ?a cnt 0)
  12557. (while (setq e (pop tbl))
  12558. (cond
  12559. ((equal e '(:startgroup))
  12560. (push '() groups) (setq ingroup t)
  12561. (when (not (= cnt 0))
  12562. (setq cnt 0)
  12563. (insert "\n"))
  12564. (insert "{ "))
  12565. ((equal e '(:endgroup))
  12566. (setq ingroup nil cnt 0)
  12567. (insert "}\n"))
  12568. (t
  12569. (setq tg (car e) c2 nil)
  12570. (if (cdr e)
  12571. (setq c (cdr e))
  12572. ;; automatically assign a character.
  12573. (setq c1 (string-to-char
  12574. (downcase (substring
  12575. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12576. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12577. (while (or (rassoc char ntable) (rassoc char table))
  12578. (setq char (1+ char)))
  12579. (setq c2 c1))
  12580. (setq c (or c2 char)))
  12581. (if ingroup (push tg (car groups)))
  12582. (setq tg (org-add-props tg nil 'face
  12583. (cond
  12584. ((member tg current) c-face)
  12585. ((member tg inherited) i-face)
  12586. (t nil))))
  12587. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12588. (insert "[" c "] " tg (make-string
  12589. (- fwidth 4 (length tg)) ?\ ))
  12590. (push (cons tg c) ntable)
  12591. (when (= (setq cnt (1+ cnt)) ncol)
  12592. (insert "\n")
  12593. (if ingroup (insert " "))
  12594. (setq cnt 0)))))
  12595. (setq ntable (nreverse ntable))
  12596. (insert "\n")
  12597. (goto-char (point-min))
  12598. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12599. (fit-window-to-buffer))
  12600. (setq rtn
  12601. (catch 'exit
  12602. (while t
  12603. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  12604. (if groups " [!] no groups" " [!]groups")
  12605. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12606. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12607. (cond
  12608. ((= c ?\r) (throw 'exit t))
  12609. ((= c ?!)
  12610. (setq groups (not groups))
  12611. (goto-char (point-min))
  12612. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12613. ((= c ?\C-c)
  12614. (if (not expert)
  12615. (org-fast-tag-show-exit
  12616. (setq exit-after-next (not exit-after-next)))
  12617. (setq expert nil)
  12618. (delete-other-windows)
  12619. (split-window-vertically)
  12620. (org-switch-to-buffer-other-window " *Org tags*")
  12621. (and (fboundp 'fit-window-to-buffer)
  12622. (fit-window-to-buffer))))
  12623. ((or (= c ?\C-g)
  12624. (and (= c ?q) (not (rassoc c ntable))))
  12625. (org-detach-overlay org-tags-overlay)
  12626. (setq quit-flag t))
  12627. ((= c ?\ )
  12628. (setq current nil)
  12629. (if exit-after-next (setq exit-after-next 'now)))
  12630. ((= c ?\t)
  12631. (condition-case nil
  12632. (setq tg (completing-read
  12633. "Tag: "
  12634. (or buffer-tags
  12635. (with-current-buffer buf
  12636. (org-get-buffer-tags)))))
  12637. (quit (setq tg "")))
  12638. (when (string-match "\\S-" tg)
  12639. (add-to-list 'buffer-tags (list tg))
  12640. (if (member tg current)
  12641. (setq current (delete tg current))
  12642. (push tg current)))
  12643. (if exit-after-next (setq exit-after-next 'now)))
  12644. ((setq e (rassoc c todo-table) tg (car e))
  12645. (with-current-buffer buf
  12646. (save-excursion (org-todo tg)))
  12647. (if exit-after-next (setq exit-after-next 'now)))
  12648. ((setq e (rassoc c ntable) tg (car e))
  12649. (if (member tg current)
  12650. (setq current (delete tg current))
  12651. (loop for g in groups do
  12652. (if (member tg g)
  12653. (mapcar (lambda (x)
  12654. (setq current (delete x current)))
  12655. g)))
  12656. (push tg current))
  12657. (if exit-after-next (setq exit-after-next 'now))))
  12658. ;; Create a sorted list
  12659. (setq current
  12660. (sort current
  12661. (lambda (a b)
  12662. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12663. (if (eq exit-after-next 'now) (throw 'exit t))
  12664. (goto-char (point-min))
  12665. (beginning-of-line 2)
  12666. (delete-region (point) (point-at-eol))
  12667. (org-fast-tag-insert "Current" current c-face)
  12668. (org-set-current-tags-overlay current ov-prefix)
  12669. (while (re-search-forward
  12670. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  12671. (setq tg (match-string 1))
  12672. (add-text-properties (match-beginning 1) (match-end 1)
  12673. (list 'face
  12674. (cond
  12675. ((member tg current) c-face)
  12676. ((member tg inherited) i-face)
  12677. (t nil)))))
  12678. (goto-char (point-min)))))
  12679. (org-detach-overlay org-tags-overlay)
  12680. (if rtn
  12681. (mapconcat 'identity current ":")
  12682. nil))))
  12683. (defun org-get-tags ()
  12684. "Get the TAGS string in the current headline."
  12685. (unless (org-on-heading-p t)
  12686. (error "Not on a heading"))
  12687. (save-excursion
  12688. (beginning-of-line 1)
  12689. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12690. (org-match-string-no-properties 1)
  12691. "")))
  12692. (defun org-get-buffer-tags ()
  12693. "Get a table of all tags used in the buffer, for completion."
  12694. (let (tags)
  12695. (save-excursion
  12696. (goto-char (point-min))
  12697. (while (re-search-forward
  12698. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  12699. (when (equal (char-after (point-at-bol 0)) ?*)
  12700. (mapc (lambda (x) (add-to-list 'tags x))
  12701. (org-split-string (org-match-string-no-properties 1) ":")))))
  12702. (mapcar 'list tags)))
  12703. ;;;; Properties
  12704. ;;; Setting and retrieving properties
  12705. (defconst org-special-properties
  12706. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED"
  12707. "CLOCK" "PRIORITY")
  12708. "The special properties valid in Org-mode.
  12709. These are properties that are not defined in the property drawer,
  12710. but in some other way.")
  12711. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12712. "Regular expression matching the first line of a property drawer.")
  12713. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12714. "Regular expression matching the first line of a property drawer.")
  12715. (defun org-property-action ()
  12716. "Do an action on properties."
  12717. (interactive)
  12718. (let (c prop)
  12719. (org-at-property-p)
  12720. (setq prop (match-string 2))
  12721. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12722. (setq c (read-char-exclusive))
  12723. (cond
  12724. ((equal c ?s)
  12725. (call-interactively 'org-set-property))
  12726. ((equal c ?d)
  12727. (call-interactively 'org-delete-property))
  12728. ((equal c ?D)
  12729. (call-interactively 'org-delete-property-globally))
  12730. ((equal c ?c)
  12731. (call-interactively 'org-compute-property-at-point))
  12732. (t (error "No such property action %c" c)))))
  12733. (defun org-at-property-p ()
  12734. "Is the cursor in a property line?"
  12735. ;; FIXME: Does not check if we are actually in the drawer.
  12736. ;; FIXME: also returns true on any drawers.....
  12737. ;; This is used by C-c C-c for property action.
  12738. (save-excursion
  12739. (beginning-of-line 1)
  12740. (looking-at "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(.*\\)")))
  12741. (defmacro org-with-point-at (pom &rest body)
  12742. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  12743. (declare (indent 1) (debug t))
  12744. `(save-excursion
  12745. (if (markerp pom) (set-buffer (marker-buffer pom)))
  12746. (save-excursion
  12747. (goto-char (or pom (point)))
  12748. ,@body)))
  12749. (defun org-get-property-block (&optional beg end force)
  12750. "Return the (beg . end) range of the body of the property drawer.
  12751. BEG and END can be beginning and end of subtree, if not given
  12752. they will be found.
  12753. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12754. (catch 'exit
  12755. (save-excursion
  12756. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12757. (end (or end (progn (outline-next-heading) (point)))))
  12758. (goto-char beg)
  12759. (if (re-search-forward org-property-start-re end t)
  12760. (setq beg (1+ (match-end 0)))
  12761. (if force
  12762. (save-excursion
  12763. (org-insert-property-drawer)
  12764. (setq end (progn (outline-next-heading) (point))))
  12765. (throw 'exit nil))
  12766. (goto-char beg)
  12767. (if (re-search-forward org-property-start-re end t)
  12768. (setq beg (1+ (match-end 0)))))
  12769. (if (re-search-forward org-property-end-re end t)
  12770. (setq end (match-beginning 0))
  12771. (or force (throw 'exit nil))
  12772. (goto-char beg)
  12773. (setq end beg)
  12774. (org-indent-line-function)
  12775. (insert ":END:\n"))
  12776. (cons beg end)))))
  12777. (defun org-entry-properties (&optional pom which)
  12778. "Get all properties of the entry at point-or-marker POM.
  12779. This includes the TODO keyword, the tags, time strings for deadline,
  12780. scheduled, and clocking, and any additional properties defined in the
  12781. entry. The return value is an alist, keys may occur multiple times
  12782. if the property key was used several times.
  12783. POM may also be nil, in which case the current entry is used.
  12784. If WHICH is nil or `all', get all properties. If WHICH is
  12785. `special' or `standard', only get that subclass."
  12786. (setq which (or which 'all))
  12787. (org-with-point-at pom
  12788. (let ((clockstr (substring org-clock-string 0 -1))
  12789. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  12790. beg end range props sum-props key value)
  12791. (save-excursion
  12792. (when (condition-case nil (org-back-to-heading t) (error nil))
  12793. (setq beg (point))
  12794. (setq sum-props (get-text-property (point) 'org-summaries))
  12795. (outline-next-heading)
  12796. (setq end (point))
  12797. (when (memq which '(all special))
  12798. ;; Get the special properties, like TODO and tags
  12799. (goto-char beg)
  12800. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  12801. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12802. (when (looking-at org-priority-regexp)
  12803. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12804. (when (and (setq value (org-get-tags)) (string-match "\\S-" value))
  12805. (push (cons "TAGS" value) props))
  12806. (when (setq value (org-get-tags-at))
  12807. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  12808. props))
  12809. (while (re-search-forward org-keyword-time-regexp end t)
  12810. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  12811. (unless (member key excluded) (push key excluded))
  12812. (push (cons key
  12813. (if (equal key clockstr)
  12814. (org-no-properties
  12815. (org-trim
  12816. (buffer-substring
  12817. (match-beginning 2) (point-at-eol))))
  12818. (org-match-string-no-properties 2)))
  12819. props)))
  12820. (when (memq which '(all standard))
  12821. ;; Get the standard properties, like :PORP: ...
  12822. (setq range (org-get-property-block beg end))
  12823. (when range
  12824. (goto-char (car range))
  12825. (while (re-search-forward
  12826. "^[ \t]*:\\([a-zA-Z][a-zA-Z_0-9]*\\):[ \t]*\\(\\S-.*\\)?"
  12827. (cdr range) t)
  12828. (setq key (org-match-string-no-properties 1)
  12829. value (org-trim (or (org-match-string-no-properties 2) "")))
  12830. (unless (member key excluded)
  12831. (push (cons key (or value "")) props)))))
  12832. (append sum-props (nreverse props)))))))
  12833. (defun org-entry-get (pom property &optional inherit)
  12834. "Get value of PROPERTY for entry at point-or-marker POM.
  12835. If INHERIT is non-nil and the entry does not have the property,
  12836. then also check higher levels of the hierarchy.
  12837. If the property is present but empty, the return value is the empty string.
  12838. If the property is not present at all, nil is returned."
  12839. (org-with-point-at pom
  12840. (if inherit
  12841. (org-entry-get-with-inheritance property)
  12842. (if (member property org-special-properties)
  12843. ;; We need a special property. Use brute force, get all properties.
  12844. (cdr (assoc property (org-entry-properties nil 'special)))
  12845. (let ((range (org-get-property-block)))
  12846. (if (and range
  12847. (goto-char (car range))
  12848. (re-search-forward
  12849. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  12850. (cdr range) t))
  12851. ;; Found the property, return it.
  12852. (if (match-end 1)
  12853. (org-match-string-no-properties 1)
  12854. "")))))))
  12855. (defun org-entry-delete (pom property)
  12856. "Delete the property PROPERTY from entry at point-or-marker POM."
  12857. (org-with-point-at pom
  12858. (if (member property org-special-properties)
  12859. nil ; cannot delete these properties.
  12860. (let ((range (org-get-property-block)))
  12861. (if (and range
  12862. (goto-char (car range))
  12863. (re-search-forward
  12864. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  12865. (cdr range) t))
  12866. (progn
  12867. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12868. t)
  12869. nil)))))
  12870. (defvar org-entry-property-inherited-from (make-marker))
  12871. (defun org-entry-get-with-inheritance (property)
  12872. "Get entry property, and search higher levels if not present."
  12873. (let (tmp)
  12874. (save-excursion
  12875. (catch 'ex
  12876. (while t
  12877. (when (setq tmp (org-entry-get nil property))
  12878. (org-back-to-heading t)
  12879. (move-marker org-entry-property-inherited-from (point))
  12880. (throw 'ex tmp))
  12881. (condition-case nil
  12882. (org-up-heading-all 1)
  12883. (error (throw 'ex nil))))))
  12884. (or tmp (cdr (assoc property org-local-properties))
  12885. (cdr (assoc property org-global-properties)))))
  12886. (defun org-entry-put (pom property value)
  12887. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12888. (org-with-point-at pom
  12889. (org-back-to-heading t)
  12890. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12891. range)
  12892. (cond
  12893. ((equal property "TODO")
  12894. (when (and (stringp value) (string-match "\\S-" value)
  12895. (not (member value org-todo-keywords-1)))
  12896. (error "\"%s\" is not a valid TODO state" value))
  12897. (if (or (not value)
  12898. (not (string-match "\\S-" value)))
  12899. (setq value 'none))
  12900. (org-todo value)
  12901. (org-set-tags nil 'align))
  12902. ((equal property "PRIORITY")
  12903. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12904. (string-to-char value) ?\ ))
  12905. (org-set-tags nil 'align))
  12906. ((equal property "SCHEDULED")
  12907. (if (re-search-forward org-scheduled-time-regexp end t)
  12908. (cond
  12909. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12910. ((eq value 'later) (org-timestamp-change 1 'day))
  12911. (t (call-interactively 'org-schedule)))
  12912. (call-interactively 'org-schedule)))
  12913. ((equal property "DEADLINE")
  12914. (if (re-search-forward org-deadline-time-regexp end t)
  12915. (cond
  12916. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12917. ((eq value 'later) (org-timestamp-change 1 'day))
  12918. (t (call-interactively 'org-deadline)))
  12919. (call-interactively 'org-deadline)))
  12920. ((member property org-special-properties)
  12921. (error "The %s property can not yet be set with `org-entry-put'"
  12922. property))
  12923. (t ; a non-special property
  12924. (setq range (org-get-property-block beg end 'force))
  12925. (goto-char (car range))
  12926. (if (re-search-forward
  12927. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12928. (progn
  12929. (delete-region (match-beginning 1) (match-end 1))
  12930. (goto-char (match-beginning 1)))
  12931. (goto-char (cdr range))
  12932. (insert "\n")
  12933. (backward-char 1)
  12934. (org-indent-line-function)
  12935. (insert ":" property ":"))
  12936. (and value (insert " " value))
  12937. (org-indent-line-function))))))
  12938. (defun org-buffer-property-keys (&optional include-specials)
  12939. "Get all property keys in the current buffer."
  12940. (let (rtn range)
  12941. (save-excursion
  12942. (save-restriction
  12943. (widen)
  12944. (goto-char (point-min))
  12945. (while (re-search-forward org-property-start-re nil t)
  12946. (setq range (org-get-property-block))
  12947. (goto-char (car range))
  12948. (while (re-search-forward "^[ \t]*:\\([a-zA-Z0-9]+\\):" (cdr range) t)
  12949. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12950. (outline-next-heading))))
  12951. (when include-specials
  12952. (setq rtn (append org-special-properties rtn)))
  12953. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12954. (defun org-insert-property-drawer ()
  12955. "Insert a property drawer into the current entry."
  12956. (interactive)
  12957. (org-back-to-heading t)
  12958. (let ((beg (point))
  12959. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12960. end hiddenp)
  12961. (outline-next-heading)
  12962. (setq end (point))
  12963. (goto-char beg)
  12964. (while (re-search-forward re end t))
  12965. (setq hiddenp (org-invisible-p))
  12966. (end-of-line 1)
  12967. (and (= (char-after) ?\n) (forward-char 1))
  12968. (org-skip-over-state-notes)
  12969. (end-of-line 0)
  12970. (insert "\n:PROPERTIES:\n:END:")
  12971. (beginning-of-line 0)
  12972. (org-indent-line-function)
  12973. (beginning-of-line 2)
  12974. (org-indent-line-function)
  12975. (beginning-of-line 0)
  12976. (if hiddenp
  12977. (save-excursion
  12978. (org-back-to-heading t)
  12979. (hide-entry))
  12980. (org-flag-drawer t))))
  12981. (defun org-set-property (property value)
  12982. "In the current entry, set PROPERTY to VALUE."
  12983. (interactive
  12984. (let* ((prop (completing-read "Property: "
  12985. (mapcar 'list (org-buffer-property-keys))))
  12986. (cur (org-entry-get nil prop))
  12987. (allowed (org-property-get-allowed-values nil prop 'table))
  12988. (val (if allowed
  12989. (completing-read "Value: " allowed nil 'req-match)
  12990. (read-string
  12991. (concat "Value" (if (and cur (string-match "\\S-" cur))
  12992. (concat "[" cur "]") "")
  12993. ": ")
  12994. "" cur))))
  12995. (list prop (if (equal val "") cur val))))
  12996. (unless (equal (org-entry-get nil property) value)
  12997. (org-entry-put nil property value)))
  12998. (defun org-delete-property (property)
  12999. "In the current entry, delete PROPERTY."
  13000. (interactive
  13001. (let* ((prop (completing-read
  13002. "Property: " (org-entry-properties nil 'standard))))
  13003. (list prop)))
  13004. (message (concat "Property " property
  13005. (if (org-entry-delete nil property)
  13006. " deleted"
  13007. " was not present in the entry"))))
  13008. (defun org-delete-property-globally (property)
  13009. "Remove PROPERTY globally, from all entries."
  13010. (interactive
  13011. (let* ((prop (completing-read
  13012. "Globally remove property: "
  13013. (mapcar 'list (org-buffer-property-keys)))))
  13014. (list prop)))
  13015. (save-excursion
  13016. (save-restriction
  13017. (widen)
  13018. (goto-char (point-min))
  13019. (let ((cnt 0))
  13020. (while (re-search-forward
  13021. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  13022. nil t)
  13023. (setq cnt (1+ cnt))
  13024. (replace-match ""))
  13025. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13026. (defvar org-columns-current-fmt-compiled) ; defined below
  13027. (defun org-compute-property-at-point ()
  13028. "FIXME:"
  13029. (interactive)
  13030. (unless (org-at-property-p)
  13031. (error "Not at a property"))
  13032. (let ((prop (org-match-string-no-properties 2)))
  13033. (org-columns-get-format-and-top-level)
  13034. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13035. (error "No operator defined for property %s" prop))
  13036. (org-columns-compute prop)))
  13037. (defun org-property-get-allowed-values (pom property &optional table)
  13038. "Get allowed values for the property PROPERTY.
  13039. When TABLE is non-nil, return an alist that can directly be used for
  13040. completion."
  13041. (let (vals)
  13042. (cond
  13043. ((equal property "TODO")
  13044. (setq vals (org-with-point-at pom
  13045. (append org-todo-keywords-1 '("")))))
  13046. ((equal property "PRIORITY")
  13047. (let ((n org-lowest-priority))
  13048. (while (>= n org-highest-priority)
  13049. (push (char-to-string n) vals)
  13050. (setq n (1- n)))))
  13051. ((member property org-special-properties))
  13052. (t
  13053. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13054. (when (and vals (string-match "\\S-" vals))
  13055. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13056. (setq vals (mapcar (lambda (x)
  13057. (cond ((stringp x) x)
  13058. ((numberp x) (number-to-string x))
  13059. ((symbolp x) (symbol-name x))
  13060. (t "???")))
  13061. vals)))))
  13062. (if table (mapcar 'list vals) vals)))
  13063. (defun org-property-previous-allowed-value (&optional previous)
  13064. "Switch to the next allowed value for this property."
  13065. (interactive)
  13066. (org-property-next-allowed-value t))
  13067. (defun org-property-next-allowed-value (&optional previous)
  13068. "Switch to the next allowed value for this property."
  13069. (interactive)
  13070. (unless (org-at-property-p)
  13071. (error "Not at a property"))
  13072. (let* ((key (match-string 2))
  13073. (value (match-string 3))
  13074. (allowed (or (org-property-get-allowed-values (point) key)
  13075. (and (member value '("[ ]" "[-]" "[X]"))
  13076. '("[ ]" "[X]"))))
  13077. nval)
  13078. (unless allowed
  13079. (error "Allowed values for this property have not been defined"))
  13080. (if previous (setq allowed (reverse allowed)))
  13081. (if (member value allowed)
  13082. (setq nval (car (cdr (member value allowed)))))
  13083. (setq nval (or nval (car allowed)))
  13084. (if (equal nval value)
  13085. (error "Only one allowed value for this property"))
  13086. (org-at-property-p)
  13087. (replace-match (concat " :" key ": " nval) t t)
  13088. (org-indent-line-function)
  13089. (beginning-of-line 1)
  13090. (skip-chars-forward " \t")))
  13091. ;;; Column View
  13092. (defvar org-columns-overlays nil
  13093. "Holds the list of current column overlays.")
  13094. (defvar org-columns-current-fmt nil
  13095. "Local variable, holds the currently active column format.")
  13096. (defvar org-columns-current-fmt-compiled nil
  13097. "Local variable, holds the currently active column format.
  13098. This is the compiled version of the format.")
  13099. (defvar org-columns-current-maxwidths nil
  13100. "Loval variable, holds the currently active maximum column widths.")
  13101. (defvar org-columns-begin-marker (make-marker)
  13102. "Points to the position where last a column creation command was called.")
  13103. (defvar org-columns-top-level-marker (make-marker)
  13104. "Points to the position where current columns region starts.")
  13105. (defvar org-columns-map (make-sparse-keymap)
  13106. "The keymap valid in column display.")
  13107. (defun org-columns-content ()
  13108. "Switch to contents view while in columns view."
  13109. (interactive)
  13110. (org-overview)
  13111. (org-content))
  13112. (org-defkey org-columns-map "c" 'org-columns-content)
  13113. (org-defkey org-columns-map "o" 'org-overview)
  13114. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  13115. (org-defkey org-columns-map "v" 'org-columns-show-value)
  13116. (org-defkey org-columns-map "q" 'org-columns-quit)
  13117. (org-defkey org-columns-map "r" 'org-columns-redo)
  13118. (org-defkey org-columns-map [left] 'backward-char)
  13119. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  13120. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  13121. (org-defkey org-columns-map [right] 'forward-char)
  13122. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  13123. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  13124. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-next-allowed-value)
  13125. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  13126. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  13127. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  13128. (org-defkey org-columns-map "<" 'org-columns-narrow)
  13129. (org-defkey org-columns-map ">" 'org-columns-widen)
  13130. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  13131. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  13132. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  13133. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  13134. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  13135. '("Column"
  13136. ["Edit property" org-columns-edit-value t]
  13137. ["Next allowed value" org-columns-next-allowed-value t]
  13138. ["Previous allowed value" org-columns-previous-allowed-value t]
  13139. ["Show full value" org-columns-show-value t]
  13140. ["Edit allowed" org-columns-edit-allowed t]
  13141. "--"
  13142. ["Edit column attributes" org-columns-edit-attributes t]
  13143. ["Increase column width" org-columns-widen t]
  13144. ["Decrease column width" org-columns-narrow t]
  13145. "--"
  13146. ["Move column right" org-columns-move-right t]
  13147. ["Move column left" org-columns-move-left t]
  13148. ["Add column" org-columns-new t]
  13149. ["Delete column" org-columns-delete t]
  13150. "--"
  13151. ["CONTENTS" org-columns-content t]
  13152. ["OVERVIEW" org-overview t]
  13153. ["Refresh columns display" org-columns-redo t]
  13154. "--"
  13155. ["Quit" org-columns-quit t]))
  13156. (defun org-columns-new-overlay (beg end &optional string face)
  13157. "Create a new column overlay and add it to the list."
  13158. (let ((ov (org-make-overlay beg end)))
  13159. (org-overlay-put ov 'face (or face 'secondary-selection))
  13160. (org-overlay-display ov string face)
  13161. (push ov org-columns-overlays)
  13162. ov))
  13163. (defun org-columns-display-here (&optional props)
  13164. "Overlay the current line with column display."
  13165. (interactive)
  13166. (let* ((fmt org-columns-current-fmt-compiled)
  13167. (beg (point-at-bol))
  13168. (level-face (save-excursion
  13169. (beginning-of-line 1)
  13170. (looking-at "\\(\\**\\)\\(\\* \\)")
  13171. (org-get-level-face 2)))
  13172. (color (list :foreground
  13173. (face-attribute (or level-face 'default) :foreground)))
  13174. props pom property ass width f string ov column)
  13175. ;; Check if the entry is in another buffer.
  13176. (unless props
  13177. (if (eq major-mode 'org-agenda-mode)
  13178. (setq pom (or (get-text-property (point) 'org-hd-marker)
  13179. (get-text-property (point) 'org-marker))
  13180. props (if pom (org-entry-properties pom) nil))
  13181. (setq props (org-entry-properties nil))))
  13182. ;; Walk the format
  13183. (while (setq column (pop fmt))
  13184. (setq property (car column)
  13185. ass (if (equal property "ITEM")
  13186. (cons "ITEM"
  13187. (save-match-data
  13188. (org-no-properties
  13189. (org-remove-tabs
  13190. (buffer-substring-no-properties
  13191. (point-at-bol) (point-at-eol))))))
  13192. (assoc property props))
  13193. width (or (cdr (assoc property org-columns-current-maxwidths))
  13194. (nth 2 column))
  13195. f (format "%%-%d.%ds | " width width)
  13196. string (format f (or (cdr ass) "")))
  13197. ;; Create the overlay
  13198. (org-unmodified
  13199. (setq ov (org-columns-new-overlay
  13200. beg (setq beg (1+ beg)) string
  13201. (list color 'org-column)))
  13202. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  13203. (org-overlay-put ov 'keymap org-columns-map)
  13204. (org-overlay-put ov 'org-columns-key property)
  13205. (org-overlay-put ov 'org-columns-value (cdr ass))
  13206. (org-overlay-put ov 'org-columns-pom pom)
  13207. (org-overlay-put ov 'org-columns-format f))
  13208. (if (or (not (char-after beg))
  13209. (equal (char-after beg) ?\n))
  13210. (let ((inhibit-read-only t))
  13211. (save-excursion
  13212. (goto-char beg)
  13213. (insert " ")))))
  13214. ;; Make the rest of the line disappear.
  13215. (org-unmodified
  13216. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  13217. (org-overlay-put ov 'invisible t)
  13218. (org-overlay-put ov 'keymap org-columns-map)
  13219. (org-overlay-put ov 'intangible t)
  13220. (push ov org-columns-overlays)
  13221. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  13222. (org-overlay-put ov 'keymap org-columns-map)
  13223. (push ov org-columns-overlays)
  13224. (let ((inhibit-read-only t))
  13225. (put-text-property (max (point-min) (1- (point-at-bol)))
  13226. (min (point-max) (1+ (point-at-eol)))
  13227. 'read-only "Type `e' to edit property")))))
  13228. (defvar org-previous-header-line-format nil
  13229. "The header line format before column view was turned on.")
  13230. (defvar org-columns-inhibit-recalculation nil
  13231. "Inhibit recomputing of columns on column view startup.")
  13232. (defvar header-line-format)
  13233. (defun org-columns-display-here-title ()
  13234. "Overlay the newline before the current line with the table title."
  13235. (interactive)
  13236. (let ((fmt org-columns-current-fmt-compiled)
  13237. string (title "")
  13238. property width f column str)
  13239. (while (setq column (pop fmt))
  13240. (setq property (car column)
  13241. str (or (nth 1 column) property)
  13242. width (or (cdr (assoc property org-columns-current-maxwidths))
  13243. (nth 2 column))
  13244. f (format "%%-%d.%ds | " width width)
  13245. string (format f str)
  13246. title (concat title string)))
  13247. (setq title (concat
  13248. (org-add-props " " nil 'display '(space :align-to 0))
  13249. (org-add-props title nil 'face '(:weight bold :underline t))))
  13250. (org-set-local 'org-previous-header-line-format header-line-format)
  13251. (setq header-line-format title)))
  13252. (defun org-columns-remove-overlays ()
  13253. "Remove all currently active column overlays."
  13254. (interactive)
  13255. (when (marker-buffer org-columns-begin-marker)
  13256. (with-current-buffer (marker-buffer org-columns-begin-marker)
  13257. (when (local-variable-p 'org-previous-header-line-format)
  13258. (setq header-line-format org-previous-header-line-format)
  13259. (kill-local-variable 'org-previous-header-line-format))
  13260. (move-marker org-columns-begin-marker nil)
  13261. (move-marker org-columns-top-level-marker nil)
  13262. (org-unmodified
  13263. (mapc 'org-delete-overlay org-columns-overlays)
  13264. (setq org-columns-overlays nil)
  13265. (let ((inhibit-read-only t))
  13266. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  13267. (defun org-columns-show-value ()
  13268. "Show the full value of the property."
  13269. (interactive)
  13270. (let ((value (get-char-property (point) 'org-columns-value)))
  13271. (message "Value is: %s" (or value ""))))
  13272. (defun org-columns-quit ()
  13273. "Remove the column overlays and in this way exit column editing."
  13274. (interactive)
  13275. (org-unmodified
  13276. (org-columns-remove-overlays)
  13277. (let ((inhibit-read-only t))
  13278. ;; FIXME: is this safe???
  13279. ;; or are there other reasons why there may be a read-only property????
  13280. (remove-text-properties (point-min) (point-max) '(read-only t))))
  13281. (when (eq major-mode 'org-agenda-mode)
  13282. (message "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  13283. (defun org-columns-edit-value ()
  13284. "Edit the value of the property at point in column view.
  13285. Where possible, use the standard interface for changing this line."
  13286. (interactive)
  13287. (let* ((col (current-column))
  13288. (key (get-char-property (point) 'org-columns-key))
  13289. (value (get-char-property (point) 'org-columns-value))
  13290. (bol (point-at-bol)) (eol (point-at-eol))
  13291. (pom (or (get-text-property bol 'org-hd-marker)
  13292. (point))) ; keep despite of compiler waring
  13293. (line-overlays
  13294. (delq nil (mapcar (lambda (x)
  13295. (and (eq (overlay-buffer x) (current-buffer))
  13296. (>= (overlay-start x) bol)
  13297. (<= (overlay-start x) eol)
  13298. x))
  13299. org-columns-overlays)))
  13300. nval eval allowed)
  13301. (when (equal key "ITEM")
  13302. (error "Cannot edit item headline from here"))
  13303. (cond
  13304. ((equal key "TODO")
  13305. (setq eval '(org-with-point-at pom
  13306. (let ((current-prefix-arg '(4))) (org-todo '(4))))))
  13307. ((equal key "PRIORITY")
  13308. (setq eval '(org-with-point-at pom
  13309. (call-interactively 'org-priority))))
  13310. ((equal key "TAGS")
  13311. (setq eval '(org-with-point-at pom
  13312. (let ((org-fast-tag-selection-single-key
  13313. (if (eq org-fast-tag-selection-single-key 'expert)
  13314. t org-fast-tag-selection-single-key)))
  13315. (call-interactively 'org-set-tags)))))
  13316. ((equal key "DEADLINE")
  13317. (setq eval '(org-with-point-at pom
  13318. (call-interactively 'org-deadline))))
  13319. ((equal key "SCHEDULED")
  13320. (setq eval '(org-with-point-at pom
  13321. (call-interactively 'org-schedule))))
  13322. (t
  13323. (setq allowed (org-property-get-allowed-values pom key 'table))
  13324. (if allowed
  13325. (setq nval (completing-read "Value: " allowed nil t))
  13326. (setq nval (read-string "Edit: " value)))
  13327. (setq nval (org-trim nval))
  13328. (when (not (equal nval value))
  13329. (setq eval '(org-entry-put pom key nval)))))
  13330. (when eval
  13331. (let ((inhibit-read-only t))
  13332. (remove-text-properties (1- bol) eol '(read-only t))
  13333. (unwind-protect
  13334. (progn
  13335. (setq org-columns-overlays
  13336. (org-delete-all line-overlays org-columns-overlays))
  13337. (mapc 'org-delete-overlay line-overlays)
  13338. (org-columns-eval eval))
  13339. (org-columns-display-here))))
  13340. (move-to-column col)
  13341. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13342. (org-columns-update key))))
  13343. (defun org-columns-edit-allowed ()
  13344. "Edit the list of allowed values for the current property."
  13345. (interactive)
  13346. (let* ((col (current-column))
  13347. (key (get-char-property (point) 'org-columns-key))
  13348. (key1 (concat key "_ALL"))
  13349. (value (get-char-property (point) 'org-columns-value))
  13350. (allowed (org-entry-get (point) key1 t))
  13351. nval)
  13352. (setq nval (read-string "Allowed: " allowed))
  13353. (org-entry-put
  13354. (cond ((marker-position org-entry-property-inherited-from)
  13355. org-entry-property-inherited-from)
  13356. ((marker-position org-columns-top-level-marker)
  13357. org-columns-top-level-marker))
  13358. key1 nval)))
  13359. (defun org-columns-eval (form)
  13360. (let (hidep)
  13361. (save-excursion
  13362. (beginning-of-line 1)
  13363. (next-line 1)
  13364. (setq hidep (org-on-heading-p 1)))
  13365. (eval form)
  13366. (and hidep (hide-entry))))
  13367. (defun org-columns-previous-allowed-value ()
  13368. "Switch to the previous allowed value for this column."
  13369. (interactive)
  13370. (org-columns-next-allowed-value t))
  13371. (defun org-columns-next-allowed-value (&optional previous)
  13372. "Switch to the next allowed value for this column."
  13373. (interactive)
  13374. (let* ((col (current-column))
  13375. (key (get-char-property (point) 'org-columns-key))
  13376. (value (get-char-property (point) 'org-columns-value))
  13377. (bol (point-at-bol)) (eol (point-at-eol))
  13378. (pom (or (get-text-property bol 'org-hd-marker)
  13379. (point))) ; keep despite of compiler waring
  13380. (line-overlays
  13381. (delq nil (mapcar (lambda (x)
  13382. (and (eq (overlay-buffer x) (current-buffer))
  13383. (>= (overlay-start x) bol)
  13384. (<= (overlay-start x) eol)
  13385. x))
  13386. org-columns-overlays)))
  13387. (allowed (or (org-property-get-allowed-values pom key)
  13388. (and (equal
  13389. (nth 4 (assoc key org-columns-current-fmt-compiled))
  13390. 'checkbox) '("[ ]" "[X]"))))
  13391. nval)
  13392. (when (equal key "ITEM")
  13393. (error "Cannot edit item headline from here"))
  13394. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  13395. (error "Allowed values for this property have not been defined"))
  13396. (if (member key '("SCHEDULED" "DEADLINE"))
  13397. (setq nval (if previous 'earlier 'later))
  13398. (if previous (setq allowed (reverse allowed)))
  13399. (if (member value allowed)
  13400. (setq nval (car (cdr (member value allowed)))))
  13401. (setq nval (or nval (car allowed)))
  13402. (if (equal nval value)
  13403. (error "Only one allowed value for this property")))
  13404. (let ((inhibit-read-only t))
  13405. (remove-text-properties (1- bol) eol '(read-only t))
  13406. (unwind-protect
  13407. (progn
  13408. (setq org-columns-overlays
  13409. (org-delete-all line-overlays org-columns-overlays))
  13410. (mapc 'org-delete-overlay line-overlays)
  13411. (org-columns-eval '(org-entry-put pom key nval)))
  13412. (org-columns-display-here)))
  13413. (move-to-column col)
  13414. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13415. (org-columns-update key))))
  13416. (defun org-verify-version (task)
  13417. (cond
  13418. ((eq task 'columns)
  13419. (if (or (featurep 'xemacs)
  13420. (< emacs-major-version 22))
  13421. (error "Emacs 22 is required for the columns feature")))))
  13422. (defun org-columns-get-format-and-top-level ()
  13423. (let (fmt)
  13424. (when (condition-case nil (org-back-to-heading) (error nil))
  13425. (move-marker org-entry-property-inherited-from nil)
  13426. (setq fmt (org-entry-get nil "COLUMNS" t)))
  13427. (setq fmt (or fmt org-columns-default-format))
  13428. (org-set-local 'org-columns-current-fmt fmt)
  13429. (org-columns-compile-format fmt)
  13430. (if (marker-position org-entry-property-inherited-from)
  13431. (move-marker org-columns-top-level-marker
  13432. org-entry-property-inherited-from)
  13433. (move-marker org-columns-top-level-marker (point)))
  13434. fmt))
  13435. (defun org-columns ()
  13436. "Turn on column view on an org-mode file."
  13437. (interactive)
  13438. (org-verify-version 'columns)
  13439. (org-columns-remove-overlays)
  13440. (move-marker org-columns-begin-marker (point))
  13441. (let (beg end fmt cache maxwidths)
  13442. (setq fmt (org-columns-get-format-and-top-level))
  13443. (save-excursion
  13444. (goto-char org-columns-top-level-marker)
  13445. (setq beg (point))
  13446. (unless org-columns-inhibit-recalculation
  13447. (org-columns-compute-all))
  13448. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  13449. (point-max)))
  13450. (goto-char beg)
  13451. ;; Get and cache the properties
  13452. (while (re-search-forward (concat "^" outline-regexp) end t)
  13453. (push (cons (org-current-line) (org-entry-properties)) cache))
  13454. (when cache
  13455. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13456. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13457. (org-columns-display-here-title)
  13458. (mapc (lambda (x)
  13459. (goto-line (car x))
  13460. (org-columns-display-here (cdr x)))
  13461. cache)))))
  13462. (defun org-columns-new (&optional prop title width op fmt)
  13463. "Insert a new column, to the leeft o the current column."
  13464. (interactive)
  13465. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  13466. cell)
  13467. (setq prop (completing-read
  13468. "Property: " (mapcar 'list (org-buffer-property-keys t))
  13469. nil nil prop))
  13470. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  13471. (setq width (read-string "Column width: " (if width (number-to-string width))))
  13472. (if (string-match "\\S-" width)
  13473. (setq width (string-to-number width))
  13474. (setq width nil))
  13475. (setq fmt (completing-read "Summary [none]: "
  13476. '(("none") ("add_numbers") ("add_times") ("checkbox"))
  13477. nil t))
  13478. (if (string-match "\\S-" fmt)
  13479. (setq fmt (intern fmt))
  13480. (setq fmt nil))
  13481. (if (eq fmt 'none) (setq fmt nil))
  13482. (if editp
  13483. (progn
  13484. (setcar editp prop)
  13485. (setcdr editp (list title width nil fmt)))
  13486. (setq cell (nthcdr (1- (current-column))
  13487. org-columns-current-fmt-compiled))
  13488. (setcdr cell (cons (list prop title width nil fmt)
  13489. (cdr cell))))
  13490. (org-columns-store-format)
  13491. (org-columns-redo)))
  13492. (defun org-columns-delete ()
  13493. "Delete the column at point from columns view."
  13494. (interactive)
  13495. (let* ((n (current-column))
  13496. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  13497. (when (y-or-n-p
  13498. (format "Are you sure you want to remove column \"%s\"? " title))
  13499. (setq org-columns-current-fmt-compiled
  13500. (delq (nth n org-columns-current-fmt-compiled)
  13501. org-columns-current-fmt-compiled))
  13502. (org-columns-store-format)
  13503. (org-columns-redo)
  13504. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  13505. (backward-char 1)))))
  13506. (defun org-columns-edit-attributes ()
  13507. "Edit the attributes of the current column."
  13508. (interactive)
  13509. (let* ((n (current-column))
  13510. (info (nth n org-columns-current-fmt-compiled)))
  13511. (apply 'org-columns-new info)))
  13512. (defun org-columns-widen (arg)
  13513. "Make the column wider by ARG characters."
  13514. (interactive "p")
  13515. (let* ((n (current-column))
  13516. (entry (nth n org-columns-current-fmt-compiled))
  13517. (width (or (nth 2 entry)
  13518. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  13519. (setq width (max 1 (+ width arg)))
  13520. (setcar (nthcdr 2 entry) width)
  13521. (org-columns-store-format)
  13522. (org-columns-redo)))
  13523. (defun org-columns-narrow (arg)
  13524. "Make the column nrrower by ARG characters."
  13525. (interactive "p")
  13526. (org-columns-widen (- arg)))
  13527. (defun org-columns-move-right ()
  13528. "Swap this column with the one to the right."
  13529. (interactive)
  13530. (let* ((n (current-column))
  13531. (cell (nthcdr n org-columns-current-fmt-compiled))
  13532. e)
  13533. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  13534. (error "Cannot shift this column further to the right"))
  13535. (setq e (car cell))
  13536. (setcar cell (car (cdr cell)))
  13537. (setcdr cell (cons e (cdr (cdr cell))))
  13538. (org-columns-store-format)
  13539. (org-columns-redo)
  13540. (forward-char 1)))
  13541. (defun org-columns-move-left ()
  13542. "Swap this column with the one to the left."
  13543. (interactive)
  13544. (let* ((n (current-column)))
  13545. (when (= n 0)
  13546. (error "Cannot shift this column further to the left"))
  13547. (backward-char 1)
  13548. (org-columns-move-right)
  13549. (backward-char 1)))
  13550. (defun org-columns-store-format ()
  13551. "Store the text version of the current columns format in appropriate place.
  13552. This is either in the COLUMNS property of the node starting the current column
  13553. display, or in the #+COLUMNS line of the current buffer."
  13554. (let (fmt)
  13555. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  13556. (if (marker-position org-columns-top-level-marker)
  13557. (save-excursion
  13558. (goto-char org-columns-top-level-marker)
  13559. (if (org-entry-get nil "COLUMNS")
  13560. (org-entry-put nil "COLUMNS" fmt)
  13561. (goto-char (point-min))
  13562. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  13563. (replace-match (concat "#+COLUMNS: " fmt t t)))))
  13564. (setq org-columns-current-fmt fmt))))
  13565. (defvar org-overriding-columns-format nil
  13566. "When set, overrides any other definition.")
  13567. (defvar org-agenda-view-columns-initially nil
  13568. "When set, switch to columns view immediately after creating the agenda.")
  13569. (defun org-agenda-columns ()
  13570. "Turn on column view in the agenda."
  13571. (interactive)
  13572. (org-verify-version 'columns)
  13573. (org-columns-remove-overlays)
  13574. (move-marker org-columns-begin-marker (point))
  13575. (let (fmt cache maxwidths m)
  13576. (cond
  13577. ((and (local-variable-p 'org-overriding-columns-format)
  13578. org-overriding-columns-format)
  13579. (setq fmt org-overriding-columns-format))
  13580. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  13581. (setq fmt (org-entry-get m "COLUMNS" t)))
  13582. ((and (boundp 'org-columns-current-fmt)
  13583. (local-variable-p 'org-columns-current-fmt)
  13584. org-columns-current-fmt)
  13585. (setq fmt org-columns-current-fmt))
  13586. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  13587. (setq m (get-text-property m 'org-hd-marker))
  13588. (setq fmt (org-entry-get m "COLUMNS" t))))
  13589. (setq fmt (or fmt org-columns-default-format))
  13590. (org-set-local 'org-columns-current-fmt fmt)
  13591. (org-columns-compile-format fmt)
  13592. (save-excursion
  13593. ;; Get and cache the properties
  13594. (goto-char (point-min))
  13595. (while (not (eobp))
  13596. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  13597. (get-text-property (point) 'org-marker)))
  13598. (push (cons (org-current-line) (org-entry-properties m)) cache))
  13599. (beginning-of-line 2))
  13600. (when cache
  13601. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13602. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13603. (org-columns-display-here-title)
  13604. (mapc (lambda (x)
  13605. (goto-line (car x))
  13606. (org-columns-display-here (cdr x)))
  13607. cache)))))
  13608. (defun org-columns-get-autowidth-alist (s cache)
  13609. "Derive the maximum column widths from the format and the cache."
  13610. (let ((start 0) rtn)
  13611. (while (string-match "%\\([a-zA-Z]\\S-*\\)" s start)
  13612. (push (cons (match-string 1 s) 1) rtn)
  13613. (setq start (match-end 0)))
  13614. (mapc (lambda (x)
  13615. (setcdr x (apply 'max
  13616. (mapcar
  13617. (lambda (y)
  13618. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  13619. cache))))
  13620. rtn)
  13621. rtn))
  13622. (defun org-columns-compute-all ()
  13623. "Compute all columns that have operators defined."
  13624. (org-unmodified
  13625. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  13626. (let ((columns org-columns-current-fmt-compiled) col)
  13627. (while (setq col (pop columns))
  13628. (when (nth 3 col)
  13629. (save-excursion
  13630. (org-columns-compute (car col)))))))
  13631. (defun org-columns-update (property)
  13632. "Recompute PROPERTY, and update the columns display for it."
  13633. (org-columns-compute property)
  13634. (let (fmt val pos)
  13635. (save-excursion
  13636. (mapc (lambda (ov)
  13637. (when (equal (org-overlay-get ov 'org-columns-key) property)
  13638. (setq pos (org-overlay-start ov))
  13639. (goto-char pos)
  13640. (when (setq val (cdr (assoc property
  13641. (get-text-property (point-at-bol) 'org-summaries))))
  13642. (setq fmt (org-overlay-get ov 'org-columns-format))
  13643. (org-overlay-put ov 'display (format fmt val)))))
  13644. org-columns-overlays))))
  13645. (defun org-columns-compute (property)
  13646. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  13647. (interactive)
  13648. (let* ((re (concat "^" outline-regexp))
  13649. (lmax 30) ; Does anyone use deeper levels???
  13650. (lsum (make-vector lmax 0))
  13651. (level 0)
  13652. (ass (assoc property org-columns-current-fmt-compiled))
  13653. (format (nth 4 ass))
  13654. (beg org-columns-top-level-marker)
  13655. last-level val end sumpos sum-alist sum str)
  13656. (save-excursion
  13657. ;; Find the region to compute
  13658. (goto-char beg)
  13659. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  13660. (goto-char end)
  13661. ;; Walk the tree from the back and do the computations
  13662. (while (re-search-backward re beg t)
  13663. (setq sumpos (match-beginning 0)
  13664. last-level level
  13665. level (org-outline-level)
  13666. val (org-entry-get nil property))
  13667. (cond
  13668. ((< level last-level)
  13669. ;; put the sum of lower levels here as a property
  13670. (setq sum (aref lsum last-level)
  13671. str (org-column-number-to-string sum format)
  13672. sum-alist (get-text-property sumpos 'org-summaries))
  13673. (if (assoc property sum-alist)
  13674. (setcdr (assoc property sum-alist) str)
  13675. (push (cons property str) sum-alist)
  13676. (org-unmodified
  13677. (add-text-properties sumpos (1+ sumpos)
  13678. (list 'org-summaries sum-alist))))
  13679. (when val ;?????????????????????????????????? and force?????
  13680. (org-entry-put nil property str))
  13681. ;; add current to current level accumulator
  13682. (aset lsum level (+ (aref lsum level) sum))
  13683. ;; clear accumulators for deeper levels
  13684. (loop for l from (1+ level) to (1- lmax) do (aset lsum l 0)))
  13685. ((>= level last-level)
  13686. ;; add what we have here to the accumulator for this level
  13687. (aset lsum level (+ (aref lsum level)
  13688. (org-column-string-to-number (or val "0") format))))
  13689. (t (error "This should not happen")))))))
  13690. (defun org-columns-redo ()
  13691. "Construct the column display again."
  13692. (interactive)
  13693. (message "Recomputing columns...")
  13694. (save-excursion
  13695. (if (marker-position org-columns-begin-marker)
  13696. (goto-char org-columns-begin-marker))
  13697. (org-columns-remove-overlays)
  13698. (if (org-mode-p)
  13699. (call-interactively 'org-columns)
  13700. (call-interactively 'org-agenda-columns)))
  13701. (message "Recomputing columns...done"))
  13702. (defun org-columns-not-in-agenda ()
  13703. (if (eq major-mode 'org-agenda-mode)
  13704. (error "This command is only allowed in Org-mode buffers")))
  13705. (defun org-string-to-number (s)
  13706. "Convert string to number, and interpret hh:mm:ss."
  13707. (if (not (string-match ":" s))
  13708. (string-to-number s)
  13709. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13710. (while l
  13711. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13712. sum)))
  13713. (defun org-column-number-to-string (n fmt)
  13714. "Convert a computed column number to a string value, according to FMT."
  13715. (cond
  13716. ((eq fmt 'add_times)
  13717. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  13718. (format "%d:%02d" h m)))
  13719. ((eq fmt 'checkbox)
  13720. (cond ((= n (floor n)) "[X]")
  13721. ((> n 1.) "[-]")
  13722. (t "[ ]")))
  13723. (t (number-to-string n))))
  13724. (defun org-column-string-to-number (s fmt)
  13725. "Convert a column value to a number that can be used for column computing."
  13726. (cond
  13727. ((string-match ":" s)
  13728. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13729. (while l
  13730. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13731. sum))
  13732. ((eq fmt 'checkbox)
  13733. (if (equal s "[X]") 1. 0.000001))
  13734. (t (string-to-number s))))
  13735. (defun org-columns-uncompile-format (cfmt)
  13736. "Turn the compiled columns format back into a string representation."
  13737. (let ((rtn "") e s prop title op width fmt)
  13738. (while (setq e (pop cfmt))
  13739. (setq prop (car e)
  13740. title (nth 1 e)
  13741. width (nth 2 e)
  13742. op (nth 3 e)
  13743. fmt (nth 4 e))
  13744. (cond
  13745. ((eq fmt 'add_times) (setq op ":"))
  13746. ((eq fmt 'checkbox) (setq op "X"))
  13747. ((eq fmt 'add_numbers) (setq op "+")))
  13748. (if (equal title prop) (setq title nil))
  13749. (setq s (concat "%" (if width (number-to-string width))
  13750. prop
  13751. (if title (concat "(" title ")"))
  13752. (if op (concat "{" op "}"))))
  13753. (setq rtn (concat rtn " " s)))
  13754. (org-trim rtn)))
  13755. (defun org-columns-compile-format (fmt)
  13756. "FIXME"
  13757. (let ((start 0) width prop title op f)
  13758. (setq org-columns-current-fmt-compiled nil)
  13759. (while (string-match "%\\([0-9]+\\)?\\([a-zA-Z_0-9]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*"
  13760. fmt start)
  13761. (setq start (match-end 0)
  13762. width (match-string 1 fmt)
  13763. prop (match-string 2 fmt)
  13764. title (or (match-string 3 fmt) prop)
  13765. op (match-string 4 fmt)
  13766. f nil)
  13767. (if width (setq width (string-to-number width)))
  13768. (cond
  13769. ((equal op "+") (setq f 'add_numbers))
  13770. ((equal op ":") (setq f 'add_times))
  13771. ((equal op "X") (setq f 'checkbox)))
  13772. (push (list prop title width op f) org-columns-current-fmt-compiled))
  13773. (setq org-columns-current-fmt-compiled
  13774. (nreverse org-columns-current-fmt-compiled))))
  13775. ;;;; Timestamps
  13776. (defvar org-last-changed-timestamp nil)
  13777. (defvar org-time-was-given) ; dynamically scoped parameter
  13778. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13779. (defvar org-ts-what) ; dynamically scoped parameter
  13780. (defun org-time-stamp (arg)
  13781. "Prompt for a date/time and insert a time stamp.
  13782. If the user specifies a time like HH:MM, or if this command is called
  13783. with a prefix argument, the time stamp will contain date and time.
  13784. Otherwise, only the date will be included. All parts of a date not
  13785. specified by the user will be filled in from the current date/time.
  13786. So if you press just return without typing anything, the time stamp
  13787. will represent the current date/time. If there is already a timestamp
  13788. at the cursor, it will be modified."
  13789. (interactive "P")
  13790. (let (org-time-was-given org-end-time-was-given time)
  13791. (cond
  13792. ((and (org-at-timestamp-p)
  13793. (eq last-command 'org-time-stamp)
  13794. (eq this-command 'org-time-stamp))
  13795. (insert "--")
  13796. (setq time (let ((this-command this-command))
  13797. (org-read-date arg 'totime)))
  13798. (org-insert-time-stamp time (or org-time-was-given arg)))
  13799. ((org-at-timestamp-p)
  13800. (setq time (let ((this-command this-command))
  13801. (org-read-date arg 'totime)))
  13802. (when (org-at-timestamp-p) ; just to get the match data
  13803. (replace-match "")
  13804. (setq org-last-changed-timestamp
  13805. (org-insert-time-stamp
  13806. time (or org-time-was-given arg)
  13807. nil nil nil (list org-end-time-was-given))))
  13808. (message "Timestamp updated"))
  13809. (t
  13810. (setq time (let ((this-command this-command))
  13811. (org-read-date arg 'totime)))
  13812. (org-insert-time-stamp time (or org-time-was-given arg)
  13813. nil nil nil (list org-end-time-was-given))))))
  13814. (defun org-time-stamp-inactive (&optional arg)
  13815. "Insert an inactive time stamp.
  13816. An inactive time stamp is enclosed in square brackets instead of angle
  13817. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13818. does not link to the calendar and cannot be changed with the S-cursor keys.
  13819. So these are more for recording a certain time/date."
  13820. (interactive "P")
  13821. (let (org-time-was-given org-end-time-was-given time)
  13822. (setq time (org-read-date arg 'totime))
  13823. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  13824. nil nil (list org-end-time-was-given))))
  13825. (defvar org-date-ovl (org-make-overlay 1 1))
  13826. (org-overlay-put org-date-ovl 'face 'org-warning)
  13827. (org-detach-overlay org-date-ovl)
  13828. (defvar org-ans1) ; dynamically scoped parameter
  13829. (defvar org-ans2) ; dynamically scoped parameter
  13830. (defvar org-plain-time-of-day-regexp) ; defined below
  13831. (defun org-read-date (&optional with-time to-time from-string prompt)
  13832. "Read a date and make things smooth for the user.
  13833. The prompt will suggest to enter an ISO date, but you can also enter anything
  13834. which will at least partially be understood by `parse-time-string'.
  13835. Unrecognized parts of the date will default to the current day, month, year,
  13836. hour and minute. For example,
  13837. 3-2-5 --> 2003-02-05
  13838. feb 15 --> currentyear-02-15
  13839. sep 12 9 --> 2009-09-12
  13840. 12:45 --> today 12:45
  13841. 22 sept 0:34 --> currentyear-09-22 0:34
  13842. 12 --> currentyear-currentmonth-12
  13843. Fri --> nearest Friday (today or later)
  13844. +4 --> four days from today (only if +N is the only thing given)
  13845. etc.
  13846. The function understands only English month and weekday abbreviations,
  13847. but this can be configured with the variables `parse-time-months' and
  13848. `parse-time-weekdays'.
  13849. While prompting, a calendar is popped up - you can also select the
  13850. date with the mouse (button 1). The calendar shows a period of three
  13851. months. To scroll it to other months, use the keys `>' and `<'.
  13852. If you don't like the calendar, turn it off with
  13853. \(setq org-popup-calendar-for-date-prompt nil)
  13854. With optional argument TO-TIME, the date will immediately be converted
  13855. to an internal time.
  13856. With an optional argument WITH-TIME, the prompt will suggest to also
  13857. insert a time. Note that when WITH-TIME is not set, you can still
  13858. enter a time, and this function will inform the calling routine about
  13859. this change. The calling routine may then choose to change the format
  13860. used to insert the time stamp into the buffer to include the time."
  13861. (require 'parse-time)
  13862. (let* ((org-time-stamp-rounding-minutes
  13863. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  13864. (ct (org-current-time))
  13865. (default-time
  13866. ;; Default time is either today, or, when entering a range,
  13867. ;; the range start.
  13868. (if (save-excursion
  13869. (re-search-backward
  13870. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13871. (- (point) 20) t))
  13872. (apply
  13873. 'encode-time
  13874. (mapcar (lambda(x) (or x 0))
  13875. (parse-time-string (match-string 1))))
  13876. ct))
  13877. (calendar-move-hook nil)
  13878. (view-diary-entries-initially nil)
  13879. (view-calendar-holidays-initially nil)
  13880. (timestr (format-time-string
  13881. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  13882. (prompt (concat (if prompt (concat prompt " ") "")
  13883. (format "Date and/or time (default [%s]): " timestr)))
  13884. ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
  13885. second minute hour day month year tl wday wday1 pm)
  13886. (cond
  13887. (from-string (setq ans from-string))
  13888. (org-popup-calendar-for-date-prompt
  13889. (save-excursion
  13890. (save-window-excursion
  13891. (calendar)
  13892. (calendar-forward-day (- (time-to-days default-time)
  13893. (calendar-absolute-from-gregorian
  13894. (calendar-current-date))))
  13895. (org-eval-in-calendar nil t)
  13896. (let* ((old-map (current-local-map))
  13897. (map (copy-keymap calendar-mode-map))
  13898. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13899. (org-defkey map (kbd "RET") 'org-calendar-select)
  13900. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  13901. 'org-calendar-select-mouse)
  13902. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  13903. 'org-calendar-select-mouse)
  13904. (org-defkey minibuffer-local-map [(meta shift left)]
  13905. (lambda () (interactive)
  13906. (org-eval-in-calendar '(calendar-backward-month 1))))
  13907. (org-defkey minibuffer-local-map [(meta shift right)]
  13908. (lambda () (interactive)
  13909. (org-eval-in-calendar '(calendar-forward-month 1))))
  13910. (org-defkey minibuffer-local-map [(shift up)]
  13911. (lambda () (interactive)
  13912. (org-eval-in-calendar '(calendar-backward-week 1))))
  13913. (org-defkey minibuffer-local-map [(shift down)]
  13914. (lambda () (interactive)
  13915. (org-eval-in-calendar '(calendar-forward-week 1))))
  13916. (org-defkey minibuffer-local-map [(shift left)]
  13917. (lambda () (interactive)
  13918. (org-eval-in-calendar '(calendar-backward-day 1))))
  13919. (org-defkey minibuffer-local-map [(shift right)]
  13920. (lambda () (interactive)
  13921. (org-eval-in-calendar '(calendar-forward-day 1))))
  13922. (org-defkey minibuffer-local-map ">"
  13923. (lambda () (interactive)
  13924. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13925. (org-defkey minibuffer-local-map "<"
  13926. (lambda () (interactive)
  13927. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13928. (unwind-protect
  13929. (progn
  13930. (use-local-map map)
  13931. (setq org-ans0 (read-string prompt "" nil nil))
  13932. ;; org-ans0: from prompt
  13933. ;; org-ans1: from mouse click
  13934. ;; org-ans2: from calendar motion
  13935. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13936. (use-local-map old-map))))))
  13937. (t ; Naked prompt only
  13938. (setq ans (read-string prompt "" nil timestr))))
  13939. (org-detach-overlay org-date-ovl)
  13940. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0)
  13941. (setq deltadays (string-to-number ans) ans ""))
  13942. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  13943. (when (string-match
  13944. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13945. (setq year (if (match-end 2)
  13946. (string-to-number (match-string 2 ans))
  13947. (string-to-number (format-time-string "%Y")))
  13948. month (string-to-number (match-string 3 ans))
  13949. day (string-to-number (match-string 4 ans)))
  13950. (if (< year 100) (setq year (+ 2000 year)))
  13951. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13952. t nil ans)))
  13953. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13954. ;; If there is a time with am/pm, and *no* time without it, we convert
  13955. ;; so that matching will be successful.
  13956. ;; FIXME: make this replace twice, so that we catch the end time.
  13957. (when (and (not (string-match "[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13958. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13959. (setq hour (string-to-number (match-string 1 ans))
  13960. minute (if (match-end 3) (string-to-number (match-string 3 ans)) 0)
  13961. pm (equal ?p (string-to-char (downcase (match-string 4 ans)))))
  13962. (if (and (= hour 12) (not pm))
  13963. (setq hour 0)
  13964. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13965. (setq ans (replace-match (format "%02d:%02d" hour minute) t t ans)))
  13966. ;; Check if there is a time range
  13967. (when (and (boundp 'org-end-time-was-given)
  13968. (string-match org-plain-time-of-day-regexp ans)
  13969. (match-end 8))
  13970. (setq org-end-time-was-given (match-string 8 ans))
  13971. (setq ans (concat (substring ans 0 (match-beginning 7))
  13972. (substring ans (match-end 7)))))
  13973. (setq tl (parse-time-string ans)
  13974. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  13975. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  13976. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  13977. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  13978. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  13979. second (or (nth 0 tl) 0)
  13980. wday (nth 6 tl))
  13981. (setq day (+ day deltadays))
  13982. (when (and wday (not (nth 3 tl)))
  13983. ;; Weekday was given, but no day, so pick that day in the week
  13984. ;; on or after the derived date.
  13985. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13986. (unless (equal wday wday1)
  13987. (setq day (+ day (% (- wday wday1 -7) 7)))))
  13988. (if (and (boundp 'org-time-was-given)
  13989. (nth 2 tl))
  13990. (setq org-time-was-given t))
  13991. (if (< year 100) (setq year (+ 2000 year)))
  13992. (if to-time
  13993. (encode-time second minute hour day month year)
  13994. (if (or (nth 1 tl) (nth 2 tl))
  13995. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  13996. (format "%04d-%02d-%02d" year month day)))))
  13997. (defun org-eval-in-calendar (form &optional keepdate)
  13998. "Eval FORM in the calendar window and return to current window.
  13999. Also, store the cursor date in variable org-ans2."
  14000. (let ((sw (selected-window)))
  14001. (select-window (get-buffer-window "*Calendar*"))
  14002. (eval form)
  14003. (when (and (not keepdate) (calendar-cursor-to-date))
  14004. (let* ((date (calendar-cursor-to-date))
  14005. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14006. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14007. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14008. (select-window sw)
  14009. ;; Update the prompt to show new default date
  14010. (save-excursion
  14011. (goto-char (point-min))
  14012. (when (and org-ans2
  14013. (re-search-forward "\\[[-0-9]+\\]" nil t)
  14014. (get-text-property (match-end 0) 'field))
  14015. (let ((inhibit-read-only t))
  14016. (replace-match (concat "[" org-ans2 "]") t t)
  14017. (add-text-properties (point-min) (1+ (match-end 0))
  14018. (text-properties-at (1+ (point-min)))))))))
  14019. (defun org-calendar-select ()
  14020. "Return to `org-read-date' with the date currently selected.
  14021. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14022. (interactive)
  14023. (when (calendar-cursor-to-date)
  14024. (let* ((date (calendar-cursor-to-date))
  14025. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14026. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14027. (if (active-minibuffer-window) (exit-minibuffer))))
  14028. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14029. "Insert a date stamp for the date given by the internal TIME.
  14030. WITH-HM means, use the stamp format that includes the time of the day.
  14031. INACTIVE means use square brackets instead of angular ones, so that the
  14032. stamp will not contribute to the agenda.
  14033. PRE and POST are optional strings to be inserted before and after the
  14034. stamp.
  14035. The command returns the inserted time stamp."
  14036. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14037. stamp)
  14038. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14039. (insert (or pre ""))
  14040. (insert (setq stamp (format-time-string fmt time)))
  14041. (when (listp extra)
  14042. (setq extra (car extra))
  14043. (if (and (stringp extra)
  14044. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14045. (setq extra (format "-%02d:%02d"
  14046. (string-to-number (match-string 1 extra))
  14047. (string-to-number (match-string 2 extra))))
  14048. (setq extra nil)))
  14049. (when extra
  14050. (backward-char 1)
  14051. (insert extra)
  14052. (forward-char 1))
  14053. (insert (or post ""))
  14054. stamp))
  14055. (defun org-toggle-time-stamp-overlays ()
  14056. "Toggle the use of custom time stamp formats."
  14057. (interactive)
  14058. (setq org-display-custom-times (not org-display-custom-times))
  14059. (unless org-display-custom-times
  14060. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14061. (while (setq p (next-single-property-change p 'display))
  14062. (if (and (get-text-property p 'display)
  14063. (eq (get-text-property p 'face) 'org-date))
  14064. (remove-text-properties
  14065. p (setq p (next-single-property-change p 'display))
  14066. '(display t))))
  14067. (set-buffer-modified-p bmp)))
  14068. (if (featurep 'xemacs)
  14069. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14070. (org-restart-font-lock)
  14071. (setq org-table-may-need-update t)
  14072. (if org-display-custom-times
  14073. (message "Time stamps are overlayed with custom format")
  14074. (message "Time stamp overlays removed")))
  14075. (defun org-display-custom-time (beg end)
  14076. "Overlay modified time stamp format over timestamp between BED and END."
  14077. (let* ((ts (buffer-substring beg end))
  14078. t1 w1 with-hm tf time str w2 (off 0))
  14079. (save-match-data
  14080. (setq t1 (org-parse-time-string ts t))
  14081. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  14082. (setq off (- (match-end 0) (match-beginning 0)))))
  14083. (setq end (- end off))
  14084. (setq w1 (- end beg)
  14085. with-hm (and (nth 1 t1) (nth 2 t1))
  14086. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14087. time (org-fix-decoded-time t1)
  14088. str (org-add-props
  14089. (format-time-string
  14090. (substring tf 1 -1) (apply 'encode-time time))
  14091. nil 'mouse-face 'highlight)
  14092. w2 (length str))
  14093. (if (not (= w2 w1))
  14094. (add-text-properties (1+ beg) (+ 2 beg)
  14095. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14096. (if (featurep 'xemacs)
  14097. (progn
  14098. (put-text-property beg end 'invisible t)
  14099. (put-text-property beg end 'end-glyph (make-glyph str)))
  14100. (put-text-property beg end 'display str))))
  14101. (defun org-translate-time (string)
  14102. "Translate all timestamps in STRING to custom format.
  14103. But do this only if the variable `org-display-custom-times' is set."
  14104. (when org-display-custom-times
  14105. (save-match-data
  14106. (let* ((start 0)
  14107. (re org-ts-regexp-both)
  14108. t1 with-hm inactive tf time str beg end)
  14109. (while (setq start (string-match re string start))
  14110. (setq beg (match-beginning 0)
  14111. end (match-end 0)
  14112. t1 (save-match-data
  14113. (org-parse-time-string (substring string beg end) t))
  14114. with-hm (and (nth 1 t1) (nth 2 t1))
  14115. inactive (equal (substring string beg (1+ beg)) "[")
  14116. tf (funcall (if with-hm 'cdr 'car)
  14117. org-time-stamp-custom-formats)
  14118. time (org-fix-decoded-time t1)
  14119. str (format-time-string
  14120. (concat
  14121. (if inactive "[" "<") (substring tf 1 -1)
  14122. (if inactive "]" ">"))
  14123. (apply 'encode-time time))
  14124. string (replace-match str t t string)
  14125. start (+ start (length str)))))))
  14126. string)
  14127. (defun org-fix-decoded-time (time)
  14128. "Set 0 instead of nil for the first 6 elements of time.
  14129. Don't touch the rest."
  14130. (let ((n 0))
  14131. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14132. (defun org-days-to-time (timestamp-string)
  14133. "Difference between TIMESTAMP-STRING and now in days."
  14134. (- (time-to-days (org-time-string-to-time timestamp-string))
  14135. (time-to-days (current-time))))
  14136. (defun org-deadline-close (timestamp-string &optional ndays)
  14137. "Is the time in TIMESTAMP-STRING close to the current date?"
  14138. (and (< (org-days-to-time timestamp-string)
  14139. (or ndays org-deadline-warning-days))
  14140. (not (org-entry-is-done-p))))
  14141. (defun org-calendar-select-mouse (ev)
  14142. "Return to `org-read-date' with the date currently selected.
  14143. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14144. (interactive "e")
  14145. (mouse-set-point ev)
  14146. (when (calendar-cursor-to-date)
  14147. (let* ((date (calendar-cursor-to-date))
  14148. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14149. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14150. (if (active-minibuffer-window) (exit-minibuffer))))
  14151. (defun org-check-deadlines (ndays)
  14152. "Check if there are any deadlines due or past due.
  14153. A deadline is considered due if it happens within `org-deadline-warning-days'
  14154. days from today's date. If the deadline appears in an entry marked DONE,
  14155. it is not shown. The prefix arg NDAYS can be used to test that many
  14156. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14157. (interactive "P")
  14158. (let* ((org-warn-days
  14159. (cond
  14160. ((equal ndays '(4)) 100000)
  14161. (ndays (prefix-numeric-value ndays))
  14162. (t org-deadline-warning-days)))
  14163. (case-fold-search nil)
  14164. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14165. (callback
  14166. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14167. (message "%d deadlines past-due or due within %d days"
  14168. (org-occur regexp nil callback)
  14169. org-warn-days)))
  14170. (defun org-evaluate-time-range (&optional to-buffer)
  14171. "Evaluate a time range by computing the difference between start and end.
  14172. Normally the result is just printed in the echo area, but with prefix arg
  14173. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14174. If the time range is actually in a table, the result is inserted into the
  14175. next column.
  14176. For time difference computation, a year is assumed to be exactly 365
  14177. days in order to avoid rounding problems."
  14178. (interactive "P")
  14179. (or
  14180. (org-clock-update-time-maybe)
  14181. (save-excursion
  14182. (unless (org-at-date-range-p)
  14183. (goto-char (point-at-bol))
  14184. (re-search-forward org-tr-regexp (point-at-eol) t))
  14185. (if (not (org-at-date-range-p))
  14186. (error "Not at a time-stamp range, and none found in current line")))
  14187. (let* ((ts1 (match-string 1))
  14188. (ts2 (match-string 2))
  14189. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14190. (match-end (match-end 0))
  14191. (time1 (org-time-string-to-time ts1))
  14192. (time2 (org-time-string-to-time ts2))
  14193. (t1 (time-to-seconds time1))
  14194. (t2 (time-to-seconds time2))
  14195. (diff (abs (- t2 t1)))
  14196. (negative (< (- t2 t1) 0))
  14197. ;; (ys (floor (* 365 24 60 60)))
  14198. (ds (* 24 60 60))
  14199. (hs (* 60 60))
  14200. (fy "%dy %dd %02d:%02d")
  14201. (fy1 "%dy %dd")
  14202. (fd "%dd %02d:%02d")
  14203. (fd1 "%dd")
  14204. (fh "%02d:%02d")
  14205. y d h m align)
  14206. (if havetime
  14207. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14208. y 0
  14209. d (floor (/ diff ds)) diff (mod diff ds)
  14210. h (floor (/ diff hs)) diff (mod diff hs)
  14211. m (floor (/ diff 60)))
  14212. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14213. y 0
  14214. d (floor (+ (/ diff ds) 0.5))
  14215. h 0 m 0))
  14216. (if (not to-buffer)
  14217. (message (org-make-tdiff-string y d h m))
  14218. (when (org-at-table-p)
  14219. (goto-char match-end)
  14220. (setq align t)
  14221. (and (looking-at " *|") (goto-char (match-end 0))))
  14222. (if (looking-at
  14223. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14224. (replace-match ""))
  14225. (if negative (insert " -"))
  14226. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14227. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14228. (insert " " (format fh h m))))
  14229. (if align (org-table-align))
  14230. (message "Time difference inserted")))))
  14231. (defun org-make-tdiff-string (y d h m)
  14232. (let ((fmt "")
  14233. (l nil))
  14234. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14235. l (push y l)))
  14236. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14237. l (push d l)))
  14238. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14239. l (push h l)))
  14240. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14241. l (push m l)))
  14242. (apply 'format fmt (nreverse l))))
  14243. (defun org-time-string-to-time (s)
  14244. (apply 'encode-time (org-parse-time-string s)))
  14245. (defun org-time-string-to-absolute (s &optional daynr)
  14246. "Convert a time stamp to an absolute day number.
  14247. If there is a specifyer for a cyclic time stamp, get the closest date to
  14248. DAYNR."
  14249. (cond
  14250. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14251. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14252. daynr
  14253. (+ daynr 1000)))
  14254. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14255. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14256. (time-to-days (current-time))) (match-string 0 s)))
  14257. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  14258. (defun org-calendar-holiday ()
  14259. "List of holidays, for Diary display in Org-mode."
  14260. (let ((hl (check-calendar-holidays date)))
  14261. (if hl (mapconcat 'identity hl "; "))))
  14262. (defun org-diary-sexp-entry (sexp entry date)
  14263. "Process a SEXP diary ENTRY for DATE."
  14264. (require 'diary-lib)
  14265. (let ((result (if calendar-debug-sexp
  14266. (let ((stack-trace-on-error t))
  14267. (eval (car (read-from-string sexp))))
  14268. (condition-case nil
  14269. (eval (car (read-from-string sexp)))
  14270. (error
  14271. (beep)
  14272. (message "Bad sexp at line %d in %s: %s"
  14273. (org-current-line)
  14274. (buffer-file-name) sexp)
  14275. (sleep-for 2))))))
  14276. (cond ((stringp result) result)
  14277. ((and (consp result)
  14278. (stringp (cdr result))) (cdr result))
  14279. (result entry)
  14280. (t nil))))
  14281. (defun org-diary-to-ical-string (frombuf)
  14282. "FIXME"
  14283. (let* ((tmpdir (if (featurep 'xemacs)
  14284. (temp-directory)
  14285. temporary-file-directory))
  14286. (tmpfile (make-temp-name
  14287. (expand-file-name "orgics" tmpdir)))
  14288. buf rtn b e)
  14289. (save-excursion
  14290. (set-buffer frombuf)
  14291. (icalendar-export-region (point-min) (point-max) tmpfile)
  14292. (setq buf (find-buffer-visiting tmpfile))
  14293. (set-buffer buf)
  14294. (goto-char (point-min))
  14295. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14296. (setq b (match-beginning 0)))
  14297. (goto-char (point-max))
  14298. (if (re-search-backward "^END:VEVENT" nil t)
  14299. (setq e (match-end 0)))
  14300. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14301. (kill-buffer buf)
  14302. (kill-buffer frombuf)
  14303. (delete-file tmpfile)
  14304. rtn))
  14305. (defun org-closest-date (start current change)
  14306. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  14307. ;; Make the proper lists from the dates
  14308. (catch 'exit
  14309. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14310. dn dw sday cday n1 n2
  14311. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14312. (setq start (org-date-to-gregorian start)
  14313. current (org-date-to-gregorian
  14314. (if org-agenda-repeating-timestamp-show-all
  14315. current
  14316. (time-to-days (current-time))))
  14317. sday (calendar-absolute-from-gregorian start)
  14318. cday (calendar-absolute-from-gregorian current))
  14319. (if (<= cday sday) (throw 'exit sday))
  14320. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14321. (setq dn (string-to-number (match-string 1 change))
  14322. dw (cdr (assoc (match-string 2 change) a1)))
  14323. (error "Invalid change specifyer: %s" change))
  14324. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14325. (cond
  14326. ((eq dw 'day)
  14327. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14328. n2 (+ n1 dn)))
  14329. ((eq dw 'year)
  14330. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14331. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14332. (setq date1 (list m d y1)
  14333. n1 (calendar-absolute-from-gregorian date1)
  14334. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14335. n2 (calendar-absolute-from-gregorian date2)))
  14336. ((eq dw 'month)
  14337. ;; approx number of month between the tow dates
  14338. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14339. ;; How often does dn fit in there?
  14340. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14341. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14342. m (+ m nm)
  14343. ny (floor (/ m 12))
  14344. y (+ y ny)
  14345. m (- m (* ny 12)))
  14346. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14347. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14348. (setq m2 (+ m dn) y2 y)
  14349. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14350. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14351. (while (< n2 cday)
  14352. (setq n1 n2 m m2 y y2)
  14353. (setq m2 (+ m dn) y2 y)
  14354. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14355. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14356. (if org-agenda-repeating-timestamp-show-all
  14357. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  14358. (if (= cday n1) n1 n2)))))
  14359. (defun org-date-to-gregorian (date)
  14360. "Turn any specification of DATE into a gregorian date for the calendar."
  14361. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14362. ((and (listp date) (= (length date) 3)) date)
  14363. ((stringp date)
  14364. (setq date (org-parse-time-string date))
  14365. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14366. ((listp date)
  14367. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14368. (defun org-parse-time-string (s &optional nodefault)
  14369. "Parse the standard Org-mode time string.
  14370. This should be a lot faster than the normal `parse-time-string'.
  14371. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14372. hour and minute fields will be nil if not given."
  14373. (if (string-match org-ts-regexp0 s)
  14374. (list 0
  14375. (if (or (match-beginning 8) (not nodefault))
  14376. (string-to-number (or (match-string 8 s) "0")))
  14377. (if (or (match-beginning 7) (not nodefault))
  14378. (string-to-number (or (match-string 7 s) "0")))
  14379. (string-to-number (match-string 4 s))
  14380. (string-to-number (match-string 3 s))
  14381. (string-to-number (match-string 2 s))
  14382. nil nil nil)
  14383. (make-list 9 0)))
  14384. (defun org-timestamp-up (&optional arg)
  14385. "Increase the date item at the cursor by one.
  14386. If the cursor is on the year, change the year. If it is on the month or
  14387. the day, change that.
  14388. With prefix ARG, change by that many units."
  14389. (interactive "p")
  14390. (org-timestamp-change (prefix-numeric-value arg)))
  14391. (defun org-timestamp-down (&optional arg)
  14392. "Decrease the date item at the cursor by one.
  14393. If the cursor is on the year, change the year. If it is on the month or
  14394. the day, change that.
  14395. With prefix ARG, change by that many units."
  14396. (interactive "p")
  14397. (org-timestamp-change (- (prefix-numeric-value arg))))
  14398. (defun org-timestamp-up-day (&optional arg)
  14399. "Increase the date in the time stamp by one day.
  14400. With prefix ARG, change that many days."
  14401. (interactive "p")
  14402. (if (and (not (org-at-timestamp-p t))
  14403. (org-on-heading-p))
  14404. (org-todo 'up)
  14405. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  14406. (defun org-timestamp-down-day (&optional arg)
  14407. "Decrease the date in the time stamp by one day.
  14408. With prefix ARG, change that many days."
  14409. (interactive "p")
  14410. (if (and (not (org-at-timestamp-p t))
  14411. (org-on-heading-p))
  14412. (org-todo 'down)
  14413. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  14414. (defsubst org-pos-in-match-range (pos n)
  14415. (and (match-beginning n)
  14416. (<= (match-beginning n) pos)
  14417. (>= (match-end n) pos)))
  14418. (defun org-at-timestamp-p (&optional inactive-ok)
  14419. "Determine if the cursor is in or at a timestamp."
  14420. (interactive)
  14421. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14422. (pos (point))
  14423. (ans (or (looking-at tsr)
  14424. (save-excursion
  14425. (skip-chars-backward "^[<\n\r\t")
  14426. (if (> (point) 1) (backward-char 1))
  14427. (and (looking-at tsr)
  14428. (> (- (match-end 0) pos) -1))))))
  14429. (and (boundp 'org-ts-what)
  14430. (setq org-ts-what
  14431. (cond
  14432. ((org-pos-in-match-range pos 2) 'year)
  14433. ((org-pos-in-match-range pos 3) 'month)
  14434. ((org-pos-in-match-range pos 7) 'hour)
  14435. ((org-pos-in-match-range pos 8) 'minute)
  14436. ((or (org-pos-in-match-range pos 4)
  14437. (org-pos-in-match-range pos 5)) 'day)
  14438. ((and (> pos (or (match-end 8) (match-end 5)))
  14439. (< pos (match-end 0)))
  14440. (- pos (or (match-end 8) (match-end 5))))
  14441. (t 'day))))
  14442. ans))
  14443. (defun org-timestamp-change (n &optional what)
  14444. "Change the date in the time stamp at point.
  14445. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14446. `year', `minute', `second'. If WHAT is not given, the cursor position
  14447. in the timestamp determines what will be changed."
  14448. (let ((pos (point))
  14449. with-hm inactive
  14450. org-ts-what
  14451. extra
  14452. ts time time0)
  14453. (if (not (org-at-timestamp-p t))
  14454. (error "Not at a timestamp"))
  14455. (if (and (not what) (not (eq org-ts-what 'day))
  14456. org-display-custom-times
  14457. (get-text-property (point) 'display)
  14458. (not (get-text-property (1- (point)) 'display)))
  14459. (setq org-ts-what 'day))
  14460. (setq org-ts-what (or what org-ts-what)
  14461. inactive (= (char-after (match-beginning 0)) ?\[)
  14462. ts (match-string 0))
  14463. (replace-match "")
  14464. (if (string-match
  14465. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  14466. ts)
  14467. (setq extra (match-string 1 ts)))
  14468. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14469. (setq with-hm t))
  14470. (setq time0 (org-parse-time-string ts))
  14471. (setq time
  14472. (apply 'encode-time
  14473. (append
  14474. (list (or (car time0) 0))
  14475. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  14476. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  14477. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  14478. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  14479. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  14480. (nthcdr 6 time0))))
  14481. (when (integerp org-ts-what)
  14482. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  14483. (if (eq what 'calendar)
  14484. (let ((cal-date
  14485. (save-excursion
  14486. (save-match-data
  14487. (set-buffer "*Calendar*")
  14488. (calendar-cursor-to-date)))))
  14489. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14490. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14491. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14492. (setcar time0 (or (car time0) 0))
  14493. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14494. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  14495. (setq time (apply 'encode-time time0))))
  14496. (setq org-last-changed-timestamp
  14497. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14498. (org-clock-update-time-maybe)
  14499. (goto-char pos)
  14500. ;; Try to recenter the calendar window, if any
  14501. (if (and org-calendar-follow-timestamp-change
  14502. (get-buffer-window "*Calendar*" t)
  14503. (memq org-ts-what '(day month year)))
  14504. (org-recenter-calendar (time-to-days time)))))
  14505. (defun org-modify-ts-extra (s pos n)
  14506. "FIXME"
  14507. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14508. ng h m new)
  14509. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14510. (cond
  14511. ((or (org-pos-in-match-range pos 2)
  14512. (org-pos-in-match-range pos 3))
  14513. (setq m (string-to-number (match-string 3 s))
  14514. h (string-to-number (match-string 2 s)))
  14515. (if (org-pos-in-match-range pos 2)
  14516. (setq h (+ h n))
  14517. (setq m (+ m n)))
  14518. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14519. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14520. (setq h (min 24 (max 0 h)))
  14521. (setq ng 1 new (format "-%02d:%02d" h m)))
  14522. ((org-pos-in-match-range pos 6)
  14523. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14524. ((org-pos-in-match-range pos 5)
  14525. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  14526. (when ng
  14527. (setq s (concat
  14528. (substring s 0 (match-beginning ng))
  14529. new
  14530. (substring s (match-end ng))))))
  14531. s))
  14532. (defun org-recenter-calendar (date)
  14533. "If the calendar is visible, recenter it to DATE."
  14534. (let* ((win (selected-window))
  14535. (cwin (get-buffer-window "*Calendar*" t))
  14536. (calendar-move-hook nil))
  14537. (when cwin
  14538. (select-window cwin)
  14539. (calendar-goto-date (if (listp date) date
  14540. (calendar-gregorian-from-absolute date)))
  14541. (select-window win))))
  14542. (defun org-goto-calendar (&optional arg)
  14543. "Go to the Emacs calendar at the current date.
  14544. If there is a time stamp in the current line, go to that date.
  14545. A prefix ARG can be used to force the current date."
  14546. (interactive "P")
  14547. (let ((tsr org-ts-regexp) diff
  14548. (calendar-move-hook nil)
  14549. (view-calendar-holidays-initially nil)
  14550. (view-diary-entries-initially nil))
  14551. (if (or (org-at-timestamp-p)
  14552. (save-excursion
  14553. (beginning-of-line 1)
  14554. (looking-at (concat ".*" tsr))))
  14555. (let ((d1 (time-to-days (current-time)))
  14556. (d2 (time-to-days
  14557. (org-time-string-to-time (match-string 1)))))
  14558. (setq diff (- d2 d1))))
  14559. (calendar)
  14560. (calendar-goto-today)
  14561. (if (and diff (not arg)) (calendar-forward-day diff))))
  14562. (defun org-date-from-calendar ()
  14563. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14564. If there is already a time stamp at the cursor position, update it."
  14565. (interactive)
  14566. (org-timestamp-change 0 'calendar))
  14567. ;;; The clock for measuring work time.
  14568. (defvar org-mode-line-string "")
  14569. (put 'org-mode-line-string 'risky-local-variable t)
  14570. (defvar org-mode-line-timer nil)
  14571. (defvar org-clock-heading "")
  14572. (defvar org-clock-start-time "")
  14573. (defun org-update-mode-line ()
  14574. (let* ((delta (- (time-to-seconds (current-time))
  14575. (time-to-seconds org-clock-start-time)))
  14576. (h (floor delta 3600))
  14577. (m (floor (- delta (* 3600 h)) 60)))
  14578. (setq org-mode-line-string
  14579. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  14580. 'help-echo "Org-mode clock is running"))
  14581. (force-mode-line-update)))
  14582. (defvar org-clock-marker (make-marker)
  14583. "Marker recording the last clock-in.")
  14584. (defvar org-clock-mode-line-entry nil
  14585. "Information for the modeline about the running clock.")
  14586. (defun org-clock-in ()
  14587. "Start the clock on the current item.
  14588. If necessary, clock-out of the currently active clock."
  14589. (interactive)
  14590. (org-clock-out t)
  14591. (let (ts)
  14592. (save-excursion
  14593. (org-back-to-heading t)
  14594. (if (looking-at org-todo-line-regexp)
  14595. (setq org-clock-heading (match-string 3))
  14596. (setq org-clock-heading "???"))
  14597. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  14598. (beginning-of-line 2)
  14599. (while
  14600. (or (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14601. (not (equal (match-string 1) org-clock-string)))
  14602. (and (looking-at "[ \t]*:PROPERTIES:")
  14603. (not org-insert-labeled-timestamps-before-properties-drawer)))
  14604. ;; Scheduling info, or properties drawer, move one line further
  14605. (beginning-of-line 2)
  14606. (or (bolp) (newline)))
  14607. (insert "\n") (backward-char 1)
  14608. (indent-relative)
  14609. (insert org-clock-string " ")
  14610. (setq org-clock-start-time (current-time))
  14611. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14612. (move-marker org-clock-marker (point) (buffer-base-buffer))
  14613. (or global-mode-string (setq global-mode-string '("")))
  14614. (or (memq 'org-mode-line-string global-mode-string)
  14615. (setq global-mode-string
  14616. (append global-mode-string '(org-mode-line-string))))
  14617. (org-update-mode-line)
  14618. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  14619. (message "Clock started at %s" ts))))
  14620. (defun org-clock-out (&optional fail-quietly)
  14621. "Stop the currently running clock.
  14622. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  14623. (interactive)
  14624. (catch 'exit
  14625. (if (not (marker-buffer org-clock-marker))
  14626. (if fail-quietly (throw 'exit t) (error "No active clock")))
  14627. (let (ts te s h m)
  14628. (save-excursion
  14629. (set-buffer (marker-buffer org-clock-marker))
  14630. (goto-char org-clock-marker)
  14631. (beginning-of-line 1)
  14632. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14633. (equal (match-string 1) org-clock-string))
  14634. (setq ts (match-string 2))
  14635. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  14636. (goto-char (match-end 0))
  14637. (delete-region (point) (point-at-eol))
  14638. (insert "--")
  14639. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14640. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  14641. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  14642. h (floor (/ s 3600))
  14643. s (- s (* 3600 h))
  14644. m (floor (/ s 60))
  14645. s (- s (* 60 s)))
  14646. (insert " => " (format "%2d:%02d" h m))
  14647. (move-marker org-clock-marker nil)
  14648. (org-add-log-maybe 'clock-out)
  14649. (when org-mode-line-timer
  14650. (cancel-timer org-mode-line-timer)
  14651. (setq org-mode-line-timer nil))
  14652. (setq global-mode-string
  14653. (delq 'org-mode-line-string global-mode-string))
  14654. (force-mode-line-update)
  14655. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  14656. (defun org-clock-cancel ()
  14657. "Cancel the running clock be removing the start timestamp."
  14658. (interactive)
  14659. (if (not (marker-buffer org-clock-marker))
  14660. (error "No active clock"))
  14661. (save-excursion
  14662. (set-buffer (marker-buffer org-clock-marker))
  14663. (goto-char org-clock-marker)
  14664. (delete-region (1- (point-at-bol)) (point-at-eol)))
  14665. (message "Clock canceled"))
  14666. (defvar org-clock-file-total-minutes nil
  14667. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  14668. (make-variable-buffer-local 'org-clock-file-total-minutes)
  14669. (defun org-clock-sum (&optional tstart tend)
  14670. "Sum the times for each subtree.
  14671. Puts the resulting times in minutes as a text property on each headline."
  14672. (interactive)
  14673. (let* ((bmp (buffer-modified-p))
  14674. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  14675. org-clock-string
  14676. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  14677. (lmax 30)
  14678. (ltimes (make-vector lmax 0))
  14679. (t1 0)
  14680. (level 0)
  14681. ts te dt
  14682. time)
  14683. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  14684. (save-excursion
  14685. (goto-char (point-max))
  14686. (while (re-search-backward re nil t)
  14687. (cond
  14688. ((match-end 2)
  14689. ;; Two time stamps
  14690. (setq ts (match-string 2)
  14691. te (match-string 3)
  14692. ts (time-to-seconds
  14693. (apply 'encode-time (org-parse-time-string ts)))
  14694. te (time-to-seconds
  14695. (apply 'encode-time (org-parse-time-string te)))
  14696. ts (if tstart (max ts tstart) ts)
  14697. te (if tend (min te tend) te)
  14698. dt (- te ts)
  14699. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  14700. ((match-end 4)
  14701. ;; A naket time
  14702. (setq t1 (+ t1 (string-to-number (match-string 5))
  14703. (* 60 (string-to-number (match-string 4))))))
  14704. (t ;; A headline
  14705. (setq level (- (match-end 1) (match-beginning 1)))
  14706. (when (or (> t1 0) (> (aref ltimes level) 0))
  14707. (loop for l from 0 to level do
  14708. (aset ltimes l (+ (aref ltimes l) t1)))
  14709. (setq t1 0 time (aref ltimes level))
  14710. (loop for l from level to (1- lmax) do
  14711. (aset ltimes l 0))
  14712. (goto-char (match-beginning 0))
  14713. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  14714. (setq org-clock-file-total-minutes (aref ltimes 0)))
  14715. (set-buffer-modified-p bmp)))
  14716. (defun org-clock-display (&optional total-only)
  14717. "Show subtree times in the entire buffer.
  14718. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  14719. in the echo area."
  14720. (interactive)
  14721. (org-remove-clock-overlays)
  14722. (let (time h m p)
  14723. (org-clock-sum)
  14724. (unless total-only
  14725. (save-excursion
  14726. (goto-char (point-min))
  14727. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14728. (goto-char p)
  14729. (when (setq time (get-text-property p :org-clock-minutes))
  14730. (org-put-clock-overlay time (funcall outline-level))))
  14731. (setq h (/ org-clock-file-total-minutes 60)
  14732. m (- org-clock-file-total-minutes (* 60 h)))
  14733. ;; Arrange to remove the overlays upon next change.
  14734. (when org-remove-highlights-with-change
  14735. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  14736. nil 'local))))
  14737. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  14738. (defvar org-clock-overlays nil)
  14739. (make-variable-buffer-local 'org-clock-overlays)
  14740. (defun org-put-clock-overlay (time &optional level)
  14741. "Put an overlays on the current line, displaying TIME.
  14742. If LEVEL is given, prefix time with a corresponding number of stars.
  14743. This creates a new overlay and stores it in `org-clock-overlays', so that it
  14744. will be easy to remove."
  14745. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  14746. (l (if level (org-get-legal-level level 0) 0))
  14747. (off 0)
  14748. ov tx)
  14749. (move-to-column c)
  14750. (unless (eolp) (skip-chars-backward "^ \t"))
  14751. (skip-chars-backward " \t")
  14752. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  14753. tx (concat (buffer-substring (1- (point)) (point))
  14754. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  14755. (org-add-props (format "%s %2d:%02d%s"
  14756. (make-string l ?*) h m
  14757. (make-string (- 10 l) ?\ ))
  14758. '(face secondary-selection))
  14759. ""))
  14760. (if (not (featurep 'xemacs))
  14761. (org-overlay-put ov 'display tx)
  14762. (org-overlay-put ov 'invisible t)
  14763. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  14764. (push ov org-clock-overlays)))
  14765. (defun org-remove-clock-overlays (&optional beg end noremove)
  14766. "Remove the occur highlights from the buffer.
  14767. BEG and END are ignored. If NOREMOVE is nil, remove this function
  14768. from the `before-change-functions' in the current buffer."
  14769. (interactive)
  14770. (unless org-inhibit-highlight-removal
  14771. (mapc 'org-delete-overlay org-clock-overlays)
  14772. (setq org-clock-overlays nil)
  14773. (unless noremove
  14774. (remove-hook 'before-change-functions
  14775. 'org-remove-clock-overlays 'local))))
  14776. (defun org-clock-out-if-current ()
  14777. "Clock out if the current entry contains the running clock.
  14778. This is used to stop the clock after a TODO entry is marked DONE,
  14779. and is only done if the variable `org-clock-out-when-done' is not nil."
  14780. (when (and org-clock-out-when-done
  14781. (member state org-done-keywords)
  14782. (equal (marker-buffer org-clock-marker) (current-buffer))
  14783. (< (point) org-clock-marker)
  14784. (> (save-excursion (outline-next-heading) (point))
  14785. org-clock-marker))
  14786. ;; Clock out, but don't accept a logging message for this.
  14787. (let ((org-log-done (if (and (listp org-log-done)
  14788. (member 'clock-out org-log-done))
  14789. '(done)
  14790. org-log-done)))
  14791. (org-clock-out))))
  14792. (add-hook 'org-after-todo-state-change-hook
  14793. 'org-clock-out-if-current)
  14794. (defun org-check-running-clock ()
  14795. "Check if the current buffer contains the running clock.
  14796. If yes, offer to stop it and to save the buffer with the changes."
  14797. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  14798. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  14799. (buffer-name))))
  14800. (org-clock-out)
  14801. (when (y-or-n-p "Save changed buffer?")
  14802. (save-buffer))))
  14803. (defun org-clock-report ()
  14804. "Create a table containing a report about clocked time.
  14805. If the buffer contains lines
  14806. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  14807. #+END: clocktable
  14808. then the table will be inserted between these lines, replacing whatever
  14809. is was there before. If these lines are not in the buffer, the table
  14810. is inserted at point, surrounded by the special lines.
  14811. The BEGIN line can contain parameters. Allowed are:
  14812. :maxlevel The maximum level to be included in the table. Default is 3.
  14813. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  14814. (interactive)
  14815. (org-remove-clock-overlays)
  14816. (unless (org-find-dblock "clocktable")
  14817. (org-create-dblock (list :name "clocktable"
  14818. :maxlevel 2 :emphasize nil)))
  14819. (org-update-dblock))
  14820. (defun org-clock-update-time-maybe ()
  14821. "If this is a CLOCK line, update it and return t.
  14822. Otherwise, return nil."
  14823. (interactive)
  14824. (save-excursion
  14825. (beginning-of-line 1)
  14826. (skip-chars-forward " \t")
  14827. (when (looking-at org-clock-string)
  14828. (let ((re (concat "[ \t]*" org-clock-string
  14829. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  14830. "\\([ \t]*=>.*\\)?"))
  14831. ts te h m s)
  14832. (if (not (looking-at re))
  14833. nil
  14834. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  14835. (end-of-line 1)
  14836. (setq ts (match-string 1)
  14837. te (match-string 2))
  14838. (setq s (- (time-to-seconds
  14839. (apply 'encode-time (org-parse-time-string te)))
  14840. (time-to-seconds
  14841. (apply 'encode-time (org-parse-time-string ts))))
  14842. h (floor (/ s 3600))
  14843. s (- s (* 3600 h))
  14844. m (floor (/ s 60))
  14845. s (- s (* 60 s)))
  14846. (insert " => " (format "%2d:%02d" h m))
  14847. t)))))
  14848. (defun org-clock-special-range (key &optional time as-strings)
  14849. "Return two times bordering a special time range.
  14850. Key is a symbol specifying the range and can be one of `today', `yesterday',
  14851. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  14852. A week starts Monday 0:00 and ends Sunday 24:00.
  14853. The range is determined relative to TIME. TIME defaults to the current time.
  14854. The return value is a cons cell with two internal times like the ones
  14855. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  14856. the returned times will be formatted strings."
  14857. (let* ((tm (decode-time (or time (current-time))))
  14858. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  14859. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  14860. (dow (nth 6 tm))
  14861. s1 m1 h1 d1 month1 y1 diff ts te fm)
  14862. (cond
  14863. ((eq key 'today)
  14864. (setq h 0 m 0 h1 24 m1 0))
  14865. ((eq key 'yesterday)
  14866. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  14867. ((eq key 'thisweek)
  14868. (setq diff (if (= dow 0) 6 (1- dow))
  14869. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14870. ((eq key 'lastweek)
  14871. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  14872. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14873. ((eq key 'thismonth)
  14874. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  14875. ((eq key 'lastmonth)
  14876. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  14877. ((eq key 'thisyear)
  14878. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  14879. ((eq key 'lastyear)
  14880. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  14881. (t (error "No such time block %s" key)))
  14882. (setq ts (encode-time s m h d month y)
  14883. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  14884. (or d1 d) (or month1 month) (or y1 y)))
  14885. (setq fm (cdr org-time-stamp-formats))
  14886. (if as-strings
  14887. (cons (format-time-string fm ts) (format-time-string fm te))
  14888. (cons ts te))))
  14889. (defun org-dblock-write:clocktable (params)
  14890. "Write the standard clocktable."
  14891. (let ((hlchars '((1 . "*") (2 . ?/)))
  14892. (emph nil)
  14893. (ins (make-marker))
  14894. ipos time h m p level hlc hdl maxlevel
  14895. ts te cc block)
  14896. (setq maxlevel (or (plist-get params :maxlevel) 3)
  14897. emph (plist-get params :emphasize)
  14898. ts (plist-get params :tstart)
  14899. te (plist-get params :tend)
  14900. block (plist-get params :block))
  14901. (when block
  14902. (setq cc (org-clock-special-range block nil t)
  14903. ts (car cc) te (cdr cc)))
  14904. (if ts (setq ts (time-to-seconds
  14905. (apply 'encode-time (org-parse-time-string ts)))))
  14906. (if te (setq te (time-to-seconds
  14907. (apply 'encode-time (org-parse-time-string te)))))
  14908. (move-marker ins (point))
  14909. (setq ipos (point))
  14910. (insert-before-markers "Clock summary at ["
  14911. (substring
  14912. (format-time-string (cdr org-time-stamp-formats))
  14913. 1 -1)
  14914. "]."
  14915. (if block
  14916. (format " Considered range is /%s/." block)
  14917. "")
  14918. "\n\n|L|Headline|Time|\n")
  14919. (org-clock-sum ts te)
  14920. (setq h (/ org-clock-file-total-minutes 60)
  14921. m (- org-clock-file-total-minutes (* 60 h)))
  14922. (insert-before-markers "|-\n|0|" "*Total file time*| "
  14923. (format "*%d:%02d*" h m)
  14924. "|\n")
  14925. (goto-char (point-min))
  14926. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14927. (goto-char p)
  14928. (when (setq time (get-text-property p :org-clock-minutes))
  14929. (save-excursion
  14930. (beginning-of-line 1)
  14931. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  14932. (setq level (- (match-end 1) (match-beginning 1)))
  14933. (<= level maxlevel))
  14934. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  14935. hdl (match-string 2)
  14936. h (/ time 60)
  14937. m (- time (* 60 h)))
  14938. (goto-char ins)
  14939. (if (= level 1) (insert-before-markers "|-\n"))
  14940. (insert-before-markers
  14941. "| " (int-to-string level) "|" hlc hdl hlc " |"
  14942. (make-string (1- level) ?|)
  14943. hlc
  14944. (format "%d:%02d" h m)
  14945. hlc
  14946. " |\n")))))
  14947. (goto-char ins)
  14948. (backward-delete-char 1)
  14949. (goto-char ipos)
  14950. (skip-chars-forward "^|")
  14951. (org-table-align)))
  14952. ;; FIXME: I don't think anybody uses this, ask David
  14953. (defun org-collect-clock-time-entries ()
  14954. "Return an internal list with clocking information.
  14955. This list has one entry for each CLOCK interval.
  14956. FIXME: describe the elements."
  14957. (interactive)
  14958. (let ((re (concat "^[ \t]*" org-clock-string
  14959. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  14960. rtn beg end next cont level title total closedp leafp
  14961. clockpos titlepos h m donep)
  14962. (save-excursion
  14963. (org-clock-sum)
  14964. (goto-char (point-min))
  14965. (while (re-search-forward re nil t)
  14966. (setq clockpos (match-beginning 0)
  14967. beg (match-string 1) end (match-string 2)
  14968. cont (match-end 0))
  14969. (setq beg (apply 'encode-time (org-parse-time-string beg))
  14970. end (apply 'encode-time (org-parse-time-string end)))
  14971. (org-back-to-heading t)
  14972. (setq donep (org-entry-is-done-p))
  14973. (setq titlepos (point)
  14974. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  14975. h (/ total 60) m (- total (* 60 h))
  14976. total (cons h m))
  14977. (looking-at "\\(\\*+\\) +\\(.*\\)")
  14978. (setq level (- (match-end 1) (match-beginning 1))
  14979. title (org-match-string-no-properties 2))
  14980. (save-excursion (outline-next-heading) (setq next (point)))
  14981. (setq closedp (re-search-forward org-closed-time-regexp next t))
  14982. (goto-char next)
  14983. (setq leafp (and (looking-at "^\\*+ ")
  14984. (<= (- (match-end 0) (point)) level)))
  14985. (push (list beg end clockpos closedp donep
  14986. total title titlepos level leafp)
  14987. rtn)
  14988. (goto-char cont)))
  14989. (nreverse rtn)))
  14990. ;;;; Agenda, and Diary Integration
  14991. ;;; Define the Org-agenda-mode
  14992. (defvar org-agenda-mode-map (make-sparse-keymap)
  14993. "Keymap for `org-agenda-mode'.")
  14994. (defvar org-agenda-menu) ; defined later in this file.
  14995. (defvar org-agenda-follow-mode nil)
  14996. (defvar org-agenda-show-log nil)
  14997. (defvar org-agenda-redo-command nil)
  14998. (defvar org-agenda-mode-hook nil)
  14999. (defvar org-agenda-type nil)
  15000. (defvar org-agenda-force-single-file nil)
  15001. (defun org-agenda-mode ()
  15002. "Mode for time-sorted view on action items in Org-mode files.
  15003. The following commands are available:
  15004. \\{org-agenda-mode-map}"
  15005. (interactive)
  15006. (kill-all-local-variables)
  15007. (setq org-agenda-undo-list nil
  15008. org-agenda-pending-undo-list nil)
  15009. (setq major-mode 'org-agenda-mode)
  15010. ;; Keep global-font-lock-mode from turning on font-lock-mode
  15011. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  15012. (setq mode-name "Org-Agenda")
  15013. (use-local-map org-agenda-mode-map)
  15014. (easy-menu-add org-agenda-menu)
  15015. (if org-startup-truncated (setq truncate-lines t))
  15016. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  15017. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  15018. ;; Make sure properties are removed when copying text
  15019. (when (boundp 'buffer-substring-filters)
  15020. (org-set-local 'buffer-substring-filters
  15021. (cons (lambda (x)
  15022. (set-text-properties 0 (length x) nil x) x)
  15023. buffer-substring-filters)))
  15024. (unless org-agenda-keep-modes
  15025. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  15026. org-agenda-show-log nil))
  15027. (easy-menu-change
  15028. '("Agenda") "Agenda Files"
  15029. (append
  15030. (list
  15031. (vector
  15032. (if (get 'org-agenda-files 'org-restrict)
  15033. "Restricted to single file"
  15034. "Edit File List")
  15035. '(org-edit-agenda-file-list)
  15036. (not (get 'org-agenda-files 'org-restrict)))
  15037. "--")
  15038. (mapcar 'org-file-menu-entry (org-agenda-files))))
  15039. (org-agenda-set-mode-name)
  15040. (apply
  15041. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  15042. (list 'org-agenda-mode-hook)))
  15043. (substitute-key-definition 'undo 'org-agenda-undo
  15044. org-agenda-mode-map global-map)
  15045. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  15046. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  15047. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  15048. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  15049. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  15050. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  15051. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  15052. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  15053. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  15054. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  15055. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  15056. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  15057. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  15058. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  15059. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  15060. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  15061. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  15062. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  15063. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  15064. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  15065. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  15066. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  15067. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  15068. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  15069. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  15070. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  15071. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  15072. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  15073. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  15074. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  15075. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  15076. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  15077. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  15078. (while l (org-defkey org-agenda-mode-map
  15079. (int-to-string (pop l)) 'digit-argument)))
  15080. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  15081. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  15082. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  15083. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  15084. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  15085. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  15086. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  15087. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  15088. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  15089. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  15090. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  15091. (org-defkey org-agenda-mode-map "n" 'next-line)
  15092. (org-defkey org-agenda-mode-map "p" 'previous-line)
  15093. (org-defkey org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  15094. (org-defkey org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  15095. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  15096. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  15097. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  15098. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  15099. (eval-after-load "calendar"
  15100. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  15101. 'org-calendar-goto-agenda))
  15102. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  15103. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  15104. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  15105. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  15106. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  15107. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  15108. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  15109. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  15110. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  15111. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  15112. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  15113. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  15114. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  15115. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  15116. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  15117. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  15118. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  15119. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  15120. "Local keymap for agenda entries from Org-mode.")
  15121. (org-defkey org-agenda-keymap
  15122. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  15123. (org-defkey org-agenda-keymap
  15124. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  15125. (when org-agenda-mouse-1-follows-link
  15126. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  15127. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  15128. '("Agenda"
  15129. ("Agenda Files")
  15130. "--"
  15131. ["Show" org-agenda-show t]
  15132. ["Go To (other window)" org-agenda-goto t]
  15133. ["Go To (this window)" org-agenda-switch-to t]
  15134. ["Follow Mode" org-agenda-follow-mode
  15135. :style toggle :selected org-agenda-follow-mode :active t]
  15136. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  15137. "--"
  15138. ["Cycle TODO" org-agenda-todo t]
  15139. ["Archive subtree" org-agenda-archive t]
  15140. ["Delete subtree" org-agenda-kill t]
  15141. "--"
  15142. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  15143. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  15144. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  15145. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  15146. "--"
  15147. ("Tags and Properties"
  15148. ["Show all Tags" org-agenda-show-tags t]
  15149. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  15150. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  15151. "--"
  15152. ["Column View" org-columns t])
  15153. ("Date/Schedule"
  15154. ["Schedule" org-agenda-schedule t]
  15155. ["Set Deadline" org-agenda-deadline t]
  15156. "--"
  15157. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  15158. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  15159. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  15160. ("Priority"
  15161. ["Set Priority" org-agenda-priority t]
  15162. ["Increase Priority" org-agenda-priority-up t]
  15163. ["Decrease Priority" org-agenda-priority-down t]
  15164. ["Show Priority" org-agenda-show-priority t])
  15165. ("Calendar/Diary"
  15166. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  15167. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  15168. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  15169. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  15170. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  15171. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  15172. "--"
  15173. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  15174. "--"
  15175. ("View"
  15176. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  15177. :style radio :selected (equal org-agenda-ndays 1)]
  15178. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  15179. :style radio :selected (equal org-agenda-ndays 7)]
  15180. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  15181. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  15182. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  15183. :style radio :selected (member org-agenda-ndays '(365 366))]
  15184. "--"
  15185. ["Show Logbook entries" org-agenda-log-mode
  15186. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  15187. ["Include Diary" org-agenda-toggle-diary
  15188. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  15189. ["Use Time Grid" org-agenda-toggle-time-grid
  15190. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  15191. ["Write view to file" org-write-agenda t]
  15192. ["Rebuild buffer" org-agenda-redo t]
  15193. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  15194. "--"
  15195. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  15196. "--"
  15197. ["Quit" org-agenda-quit t]
  15198. ["Exit and Release Buffers" org-agenda-exit t]
  15199. ))
  15200. ;;; Agenda undo
  15201. (defvar org-agenda-allow-remote-undo t
  15202. "Non-nil means, allow remote undo from the agenda buffer.")
  15203. (defvar org-agenda-undo-list nil
  15204. "List of undoable operations in the agenda since last refresh.")
  15205. (defvar org-agenda-undo-has-started-in nil
  15206. "Buffers that have already seen `undo-start' in the current undo sequence.")
  15207. (defvar org-agenda-pending-undo-list nil
  15208. "In a series of undo commands, this is the list of remaning undo items.")
  15209. (defmacro org-if-unprotected (&rest body)
  15210. "Execute BODY if there is no `org-protected' text property at point."
  15211. (declare (debug t))
  15212. `(unless (get-text-property (point) 'org-protected)
  15213. ,@body))
  15214. (defmacro org-with-remote-undo (_buffer &rest _body)
  15215. "Execute BODY while recording undo information in two buffers."
  15216. (declare (indent 1) (debug t))
  15217. `(let ((_cline (org-current-line))
  15218. (_cmd this-command)
  15219. (_buf1 (current-buffer))
  15220. (_buf2 ,_buffer)
  15221. (_undo1 buffer-undo-list)
  15222. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  15223. _c1 _c2)
  15224. ,@_body
  15225. (when org-agenda-allow-remote-undo
  15226. (setq _c1 (org-verify-change-for-undo
  15227. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  15228. _c2 (org-verify-change-for-undo
  15229. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  15230. (when (or _c1 _c2)
  15231. ;; make sure there are undo boundaries
  15232. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  15233. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  15234. ;; remember which buffer to undo
  15235. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  15236. org-agenda-undo-list)))))
  15237. (defun org-agenda-undo ()
  15238. "Undo a remote editing step in the agenda.
  15239. This undoes changes both in the agenda buffer and in the remote buffer
  15240. that have been changed along."
  15241. (interactive)
  15242. (or org-agenda-allow-remote-undo
  15243. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  15244. (if (not (eq this-command last-command))
  15245. (setq org-agenda-undo-has-started-in nil
  15246. org-agenda-pending-undo-list org-agenda-undo-list))
  15247. (if (not org-agenda-pending-undo-list)
  15248. (error "No further undo information"))
  15249. (let* ((entry (pop org-agenda-pending-undo-list))
  15250. buf line cmd rembuf)
  15251. (setq cmd (pop entry) line (pop entry))
  15252. (setq rembuf (nth 2 entry))
  15253. (org-with-remote-undo rembuf
  15254. (while (bufferp (setq buf (pop entry)))
  15255. (if (pop entry)
  15256. (with-current-buffer buf
  15257. (let ((last-undo-buffer buf)
  15258. (inhibit-read-only t))
  15259. (unless (memq buf org-agenda-undo-has-started-in)
  15260. (push buf org-agenda-undo-has-started-in)
  15261. (make-local-variable 'pending-undo-list)
  15262. (undo-start))
  15263. (while (and pending-undo-list
  15264. (listp pending-undo-list)
  15265. (not (car pending-undo-list)))
  15266. (pop pending-undo-list))
  15267. (undo-more 1))))))
  15268. (goto-line line)
  15269. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  15270. (defun org-verify-change-for-undo (l1 l2)
  15271. "Verify that a real change occurred between the undo lists L1 and L2."
  15272. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  15273. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  15274. (not (eq l1 l2)))
  15275. ;;; Agenda dispatch
  15276. (defvar org-agenda-restrict nil)
  15277. (defvar org-agenda-restrict-begin (make-marker))
  15278. (defvar org-agenda-restrict-end (make-marker))
  15279. (defvar org-agenda-last-dispatch-buffer nil)
  15280. ;;;###autoload
  15281. (defun org-agenda (arg)
  15282. "Dispatch agenda commands to collect entries to the agenda buffer.
  15283. Prompts for a character to select a command. Any prefix arg will be passed
  15284. on to the selected command. The default selections are:
  15285. g
  15286. a Call `org-agenda-list' to display the agenda for current day or week.
  15287. t Call `org-todo-list' to display the global todo list.
  15288. T Call `org-todo-list' to display the global todo list, select only
  15289. entries with a specific TODO keyword (the user gets a prompt).
  15290. m Call `org-tags-view' to display headlines with tags matching
  15291. a condition (the user is prompted for the condition).
  15292. M Like `m', but select only TODO entries, no ordinary headlines.
  15293. L Create a timeline for the current buffer.
  15294. e Export views to associated files.
  15295. More commands can be added by configuring the variable
  15296. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  15297. searches can be pre-defined in this way.
  15298. If the current buffer is in Org-mode and visiting a file, you can also
  15299. first press `1' to indicate that the agenda should be temporarily (until the
  15300. next use of \\[org-agenda]) restricted to the current file."
  15301. (interactive "P")
  15302. (catch 'exit
  15303. (let* ((buf (current-buffer))
  15304. (bfn (buffer-file-name (buffer-base-buffer)))
  15305. (restrict-ok (and bfn (org-mode-p)))
  15306. (custom org-agenda-custom-commands)
  15307. c entry key type match lprops)
  15308. ;; Turn off restriction
  15309. (put 'org-agenda-files 'org-restrict nil)
  15310. (setq org-agenda-restrict nil)
  15311. (move-marker org-agenda-restrict-begin nil)
  15312. (move-marker org-agenda-restrict-end nil)
  15313. ;; Delete old local properties
  15314. (put 'org-agenda-redo-command 'org-lprops nil)
  15315. ;; Remember where this call originated
  15316. (setq org-agenda-last-dispatch-buffer (current-buffer))
  15317. (save-window-excursion
  15318. (delete-other-windows)
  15319. (org-switch-to-buffer-other-window " *Agenda Commands*")
  15320. (erase-buffer)
  15321. (insert (eval-when-compile
  15322. (let ((header
  15323. "Press key for an agenda command:
  15324. -------------------------------- C Configure custom agenda commands
  15325. a Agenda for current week or day e Export agenda views
  15326. t List of all TODO entries T Entries with special TODO kwd
  15327. m Match a TAGS query M Like m, but only TODO entries
  15328. L Timeline for current buffer # List stuck projects (!=configure)
  15329. ")
  15330. (start 0))
  15331. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  15332. (setq start (match-end 0))
  15333. (add-text-properties (match-beginning 2) (match-end 2)
  15334. '(face bold) header))
  15335. header)))
  15336. (while (setq entry (pop custom))
  15337. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  15338. (insert (format "\n%-4s%-14s: %s"
  15339. (org-add-props (copy-sequence key)
  15340. '(face bold))
  15341. (cond
  15342. ((stringp type) type)
  15343. ((eq type 'agenda) "Agenda for current week or day")
  15344. ((eq type 'alltodo) "List of all TODO entries")
  15345. ((eq type 'stuck) "List of stuck projects")
  15346. ((eq type 'todo) "TODO keyword")
  15347. ((eq type 'tags) "Tags query")
  15348. ((eq type 'tags-todo) "Tags (TODO)")
  15349. ((eq type 'tags-tree) "Tags tree")
  15350. ((eq type 'todo-tree) "TODO kwd tree")
  15351. ((eq type 'occur-tree) "Occur tree")
  15352. ((functionp type) (symbol-name type))
  15353. (t "???"))
  15354. (if (stringp match)
  15355. (org-add-props match nil 'face 'org-warning)
  15356. (format "set of %d commands" (length match))))))
  15357. (if restrict-ok
  15358. (insert "\n"
  15359. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  15360. (goto-char (point-min))
  15361. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  15362. (message "Press key for agenda command%s"
  15363. (if restrict-ok ", or [1] or [0] to restrict" ""))
  15364. (setq c (read-char-exclusive))
  15365. (message "")
  15366. (when (memq c '(?L ?1 ?0))
  15367. (if restrict-ok
  15368. (put 'org-agenda-files 'org-restrict (list bfn))
  15369. (error "Cannot restrict agenda to current buffer"))
  15370. (with-current-buffer " *Agenda Commands*"
  15371. (goto-char (point-max))
  15372. (delete-region (point-at-bol) (point))
  15373. (goto-char (point-min)))
  15374. (when (eq c ?0)
  15375. (setq org-agenda-restrict t)
  15376. (with-current-buffer buf
  15377. (if (org-region-active-p)
  15378. (progn
  15379. (move-marker org-agenda-restrict-begin (region-beginning))
  15380. (move-marker org-agenda-restrict-end (region-end)))
  15381. (save-excursion
  15382. (org-back-to-heading t)
  15383. (move-marker org-agenda-restrict-begin (point))
  15384. (move-marker org-agenda-restrict-end
  15385. (progn (org-end-of-subtree t)))))))
  15386. (unless (eq c ?L)
  15387. (message "Press key for agenda command%s"
  15388. (if restrict-ok " (restricted to current file)" ""))
  15389. (setq c (read-char-exclusive)))
  15390. (message "")))
  15391. (require 'calendar) ; FIXME: can we avoid this for some commands?
  15392. ;; For example the todo list should not need it (but does...)
  15393. (cond
  15394. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  15395. (if (symbolp (nth 1 entry))
  15396. (progn
  15397. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  15398. lprops (nth 3 entry))
  15399. (put 'org-agenda-redo-command 'org-lprops lprops)
  15400. (cond
  15401. ((eq type 'agenda)
  15402. (org-let lprops '(org-agenda-list current-prefix-arg)))
  15403. ((eq type 'alltodo)
  15404. (org-let lprops '(org-todo-list current-prefix-arg)))
  15405. ((eq type 'stuck)
  15406. (org-let lprops '(org-agenda-list-stuck-projects
  15407. current-prefix-arg)))
  15408. ((eq type 'tags)
  15409. (org-let lprops '(org-tags-view current-prefix-arg match)))
  15410. ((eq type 'tags-todo)
  15411. (org-let lprops '(org-tags-view '(4) match)))
  15412. ((eq type 'todo)
  15413. (org-let lprops '(org-todo-list match)))
  15414. ((eq type 'tags-tree)
  15415. (org-check-for-org-mode)
  15416. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  15417. ((eq type 'todo-tree)
  15418. (org-check-for-org-mode)
  15419. (org-let lprops
  15420. '(org-occur (concat "^" outline-regexp "[ \t]*"
  15421. (regexp-quote match) "\\>"))))
  15422. ((eq type 'occur-tree)
  15423. (org-check-for-org-mode)
  15424. (org-let lprops '(org-occur match)))
  15425. ((fboundp type)
  15426. (org-let lprops '(funcall type match)))
  15427. (t (error "Invalid custom agenda command type %s" type))))
  15428. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  15429. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  15430. ((equal c ?a) (call-interactively 'org-agenda-list))
  15431. ((equal c ?t) (call-interactively 'org-todo-list))
  15432. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  15433. ((equal c ?m) (call-interactively 'org-tags-view))
  15434. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  15435. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  15436. ((equal c ?L)
  15437. (unless restrict-ok
  15438. (error "This is not an Org-mode file"))
  15439. (org-call-with-arg 'org-timeline arg))
  15440. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  15441. ((equal c ?!) (customize-variable 'org-stuck-projects))
  15442. (t (error "Invalid key"))))))
  15443. (defun org-run-agenda-series (name series)
  15444. (org-prepare-agenda name)
  15445. (let* ((org-agenda-multi t)
  15446. (redo (list 'org-run-agenda-series name (list 'quote series)))
  15447. (cmds (car series))
  15448. (gprops (nth 1 series))
  15449. match ;; The byte compiler incorrectly complains about this. Keep it!
  15450. cmd type lprops)
  15451. (while (setq cmd (pop cmds))
  15452. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  15453. (cond
  15454. ((eq type 'agenda)
  15455. (org-let2 gprops lprops
  15456. '(call-interactively 'org-agenda-list)))
  15457. ((eq type 'alltodo)
  15458. (org-let2 gprops lprops
  15459. '(call-interactively 'org-todo-list)))
  15460. ((eq type 'stuck)
  15461. (org-let2 gprops lprops
  15462. '(call-interactively 'org-agenda-list-stuck-projects)))
  15463. ((eq type 'tags)
  15464. (org-let2 gprops lprops
  15465. '(org-tags-view current-prefix-arg match)))
  15466. ((eq type 'tags-todo)
  15467. (org-let2 gprops lprops
  15468. '(org-tags-view '(4) match)))
  15469. ((eq type 'todo)
  15470. (org-let2 gprops lprops
  15471. '(org-todo-list match)))
  15472. ((fboundp type)
  15473. (org-let2 gprops lprops
  15474. '(funcall type match)))
  15475. (t (error "Invalid type in command series"))))
  15476. (widen)
  15477. (setq org-agenda-redo-command redo)
  15478. (goto-char (point-min)))
  15479. (org-finalize-agenda))
  15480. ;;;###autoload
  15481. (defmacro org-batch-agenda (cmd-key &rest parameters)
  15482. "Run an agenda command in batch mode and send the result to STDOUT.
  15483. If CMD-KEY is a string of length 1, it is used as a key in
  15484. `org-agenda-custom-commands' and triggers this command. If it is a
  15485. longer string is is used as a tags/todo match string.
  15486. Paramters are alternating variable names and values that will be bound
  15487. before running the agenda command."
  15488. (let (pars)
  15489. (while parameters
  15490. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15491. (if (> (length cmd-key) 1)
  15492. (eval (list 'let (nreverse pars)
  15493. (list 'org-tags-view nil cmd-key)))
  15494. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15495. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15496. (set-buffer org-agenda-buffer-name)
  15497. (princ (org-encode-for-stdout (buffer-string)))))
  15498. (defun org-encode-for-stdout (string)
  15499. (if (fboundp 'encode-coding-string)
  15500. (encode-coding-string string buffer-file-coding-system)
  15501. string))
  15502. (defvar org-agenda-info nil)
  15503. ;;;###autoload
  15504. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  15505. "Run an agenda command in batch mode and send the result to STDOUT.
  15506. If CMD-KEY is a string of length 1, it is used as a key in
  15507. `org-agenda-custom-commands' and triggers this command. If it is a
  15508. longer string is is used as a tags/todo match string.
  15509. Paramters are alternating variable names and values that will be bound
  15510. before running the agenda command.
  15511. The output gives a line for each selected agenda item. Each
  15512. item is a list of comma-separated values, like this:
  15513. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  15514. category The category of the item
  15515. head The headline, without TODO kwd, TAGS and PRIORITY
  15516. type The type of the agenda entry, can be
  15517. todo selected in TODO match
  15518. tagsmatch selected in tags match
  15519. diary imported from diary
  15520. deadline a deadline on given date
  15521. scheduled scheduled on given date
  15522. timestamp entry has timestamp on given date
  15523. closed entry was closed on given date
  15524. upcoming-deadline warning about deadline
  15525. past-scheduled forwarded scheduled item
  15526. block entry has date block including g. date
  15527. todo The todo keyword, if any
  15528. tags All tags including inherited ones, separated by colons
  15529. date The relevant date, like 2007-2-14
  15530. time The time, like 15:00-16:50
  15531. extra Sting with extra planning info
  15532. priority-l The priority letter if any was given
  15533. priority-n The computed numerical priority
  15534. agenda-day The day in the agenda where this is listed"
  15535. (let (pars)
  15536. (while parameters
  15537. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15538. (push (list 'org-agenda-remove-tags t) pars)
  15539. (if (> (length cmd-key) 1)
  15540. (eval (list 'let (nreverse pars)
  15541. (list 'org-tags-view nil cmd-key)))
  15542. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15543. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15544. (set-buffer org-agenda-buffer-name)
  15545. (let* ((lines (org-split-string (buffer-string) "\n"))
  15546. line)
  15547. (while (setq line (pop lines))
  15548. (catch 'next
  15549. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  15550. (setq org-agenda-info
  15551. (org-fix-agenda-info (text-properties-at 0 line)))
  15552. (princ
  15553. (org-encode-for-stdout
  15554. (mapconcat 'org-agenda-export-csv-mapper
  15555. '(org-category txt type todo tags date time-of-day extra
  15556. priority-letter priority agenda-day)
  15557. ",")))
  15558. (princ "\n"))))))
  15559. (defun org-fix-agenda-info (props)
  15560. "FIXME"
  15561. (let (tmp re)
  15562. (when (setq tmp (plist-get props 'tags))
  15563. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  15564. (when (setq tmp (plist-get props 'date))
  15565. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15566. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15567. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  15568. (setq tmp (calendar-date-string tmp)))
  15569. (setq props (plist-put props 'date tmp)))
  15570. (when (setq tmp (plist-get props 'day))
  15571. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15572. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15573. (setq tmp (calendar-date-string tmp)))
  15574. (setq props (plist-put props 'day tmp))
  15575. (setq props (plist-put props 'agenda-day tmp)))
  15576. (when (setq tmp (plist-get props 'txt))
  15577. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  15578. (plist-put props 'priority-letter (match-string 1 tmp))
  15579. (setq tmp (replace-match "" t t tmp)))
  15580. (when (and (setq re (plist-get props 'org-todo-regexp))
  15581. (setq re (concat "\\`\\.*" re " ?"))
  15582. (string-match re tmp))
  15583. (plist-put props 'todo (match-string 1 tmp))
  15584. (setq tmp (replace-match "" t t tmp)))
  15585. (plist-put props 'txt tmp)))
  15586. props)
  15587. (defun org-agenda-export-csv-mapper (prop)
  15588. (let ((res (plist-get org-agenda-info prop)))
  15589. (setq res
  15590. (cond
  15591. ((not res) "")
  15592. ((stringp res) res)
  15593. (t (prin1-to-string res))))
  15594. (while (string-match "," res)
  15595. (setq res (replace-match ";" t t res)))
  15596. (org-trim res)))
  15597. ;;;###autoload
  15598. (defun org-store-agenda-views (&rest parameters)
  15599. (interactive)
  15600. (eval (list 'org-batch-store-agenda-views)))
  15601. ;; FIXME, why is this a macro?????
  15602. ;;;###autoload
  15603. (defmacro org-batch-store-agenda-views (&rest parameters)
  15604. "Run all custom agenda commands that have a file argument."
  15605. (let ((cmds org-agenda-custom-commands)
  15606. (pop-up-frames nil)
  15607. (dir default-directory)
  15608. pars cmd thiscmdkey files opts)
  15609. (while parameters
  15610. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15611. (setq pars (reverse pars))
  15612. (save-window-excursion
  15613. (while cmds
  15614. (setq cmd (pop cmds)
  15615. thiscmdkey (car cmd)
  15616. opts (nth 3 cmd)
  15617. files (nth 4 cmd))
  15618. (if (stringp files) (setq files (list files)))
  15619. (when files
  15620. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  15621. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15622. '(org-agenda nil))))
  15623. (set-buffer org-agenda-buffer-name)
  15624. (while files
  15625. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15626. (list 'org-write-agenda
  15627. (expand-file-name (pop files) dir) t))))
  15628. (and (get-buffer org-agenda-buffer-name)
  15629. (kill-buffer org-agenda-buffer-name)))))))
  15630. (defun org-write-agenda (file &optional nosettings)
  15631. "Write the current buffer (an agenda view) as a file.
  15632. Depending on the extension of the file name, plain text (.txt),
  15633. HTML (.html or .htm) or Postscript (.ps) is produced.
  15634. If NOSETTINGS is given, do not scope the settings of
  15635. `org-agenda-exporter-settings' into the export commands. This is used when
  15636. the settings have already been scoped and we do not wish to overrule other,
  15637. higher priority settings."
  15638. (interactive "FWrite agenda to file: ")
  15639. (if (not (file-writable-p file))
  15640. (error "Cannot write agenda to file %s" file))
  15641. (cond
  15642. ((string-match "\\.html?\\'" file) (require 'htmlize))
  15643. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  15644. (org-let (if nosettings nil org-agenda-exporter-settings)
  15645. '(save-excursion
  15646. (save-window-excursion
  15647. (cond
  15648. ((string-match "\\.html?\\'" file)
  15649. (set-buffer (htmlize-buffer (current-buffer)))
  15650. (when (and org-agenda-export-html-style
  15651. (string-match "<style>" org-agenda-export-html-style))
  15652. ;; replace <style> section with org-agenda-export-html-style
  15653. (goto-char (point-min))
  15654. (kill-region (- (search-forward "<style") 6)
  15655. (search-forward "</style>"))
  15656. (insert org-agenda-export-html-style))
  15657. (write-file file)
  15658. (kill-buffer (current-buffer))
  15659. (message "HTML written to %s" file))
  15660. ((string-match "\\.ps\\'" file)
  15661. (ps-print-buffer-with-faces file)
  15662. (message "Postscript written to %s" file))
  15663. (t
  15664. (let ((bs (buffer-string)))
  15665. (find-file file)
  15666. (insert bs)
  15667. (save-buffer 0)
  15668. (kill-buffer (current-buffer))
  15669. (message "Plain text written to %s" file))))))
  15670. (set-buffer org-agenda-buffer-name)))
  15671. (defmacro org-no-read-only (&rest body)
  15672. "Inhibit read-only for BODY."
  15673. `(let ((inhibit-read-only t)) ,@body))
  15674. (defun org-check-for-org-mode ()
  15675. "Make sure current buffer is in org-mode. Error if not."
  15676. (or (org-mode-p)
  15677. (error "Cannot execute org-mode agenda command on buffer in %s."
  15678. major-mode)))
  15679. (defun org-fit-agenda-window ()
  15680. "Fit the window to the buffer size."
  15681. (and (memq org-agenda-window-setup '(reorganize-frame))
  15682. (fboundp 'fit-window-to-buffer)
  15683. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  15684. (/ (frame-height) 2))))
  15685. ;;; Agenda file list
  15686. (defun org-agenda-files (&optional unrestricted)
  15687. "Get the list of agenda files.
  15688. Optional UNRESTRICTED means return the full list even if a restriction
  15689. is currently in place."
  15690. (let ((files
  15691. (cond
  15692. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15693. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15694. ((listp org-agenda-files) org-agenda-files)
  15695. (t (error "Invalid value of `org-agenda-files'")))))
  15696. (if org-agenda-skip-unavailable-files
  15697. (delq nil
  15698. (mapcar (function
  15699. (lambda (file)
  15700. (and (file-readable-p file) file)))
  15701. files))
  15702. files))) ; `org-check-agenda-file' will remove them from the list
  15703. (defun org-edit-agenda-file-list ()
  15704. "Edit the list of agenda files.
  15705. Depending on setup, this either uses customize to edit the variable
  15706. `org-agenda-files', or it visits the file that is holding the list. In the
  15707. latter case, the buffer is set up in a way that saving it automatically kills
  15708. the buffer and restores the previous window configuration."
  15709. (interactive)
  15710. (if (stringp org-agenda-files)
  15711. (let ((cw (current-window-configuration)))
  15712. (find-file org-agenda-files)
  15713. (org-set-local 'org-window-configuration cw)
  15714. (org-add-hook 'after-save-hook
  15715. (lambda ()
  15716. (set-window-configuration
  15717. (prog1 org-window-configuration
  15718. (kill-buffer (current-buffer))))
  15719. (org-install-agenda-files-menu)
  15720. (message "New agenda file list installed"))
  15721. nil 'local)
  15722. (message (substitute-command-keys
  15723. "Edit list and finish with \\[save-buffer]")))
  15724. (customize-variable 'org-agenda-files)))
  15725. (defun org-store-new-agenda-file-list (list)
  15726. "Set new value for the agenda file list and save it correcly."
  15727. (if (stringp org-agenda-files)
  15728. (let ((f org-agenda-files) b)
  15729. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  15730. (with-temp-file f
  15731. (insert (mapconcat 'identity list "\n") "\n")))
  15732. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  15733. (setq org-agenda-files list)
  15734. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15735. (defun org-read-agenda-file-list ()
  15736. "Read the list of agenda files from a file."
  15737. (when (stringp org-agenda-files)
  15738. (with-temp-buffer
  15739. (insert-file-contents org-agenda-files)
  15740. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  15741. ;;;###autoload
  15742. (defun org-cycle-agenda-files ()
  15743. "Cycle through the files in `org-agenda-files'.
  15744. If the current buffer visits an agenda file, find the next one in the list.
  15745. If the current buffer does not, find the first agenda file."
  15746. (interactive)
  15747. (let* ((fs (org-agenda-files t))
  15748. (files (append fs (list (car fs))))
  15749. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15750. file)
  15751. (unless files (error "No agenda files"))
  15752. (catch 'exit
  15753. (while (setq file (pop files))
  15754. (if (equal (file-truename file) tcf)
  15755. (when (car files)
  15756. (find-file (car files))
  15757. (throw 'exit t))))
  15758. (find-file (car fs)))
  15759. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  15760. (defun org-agenda-file-to-front (&optional to-end)
  15761. "Move/add the current file to the top of the agenda file list.
  15762. If the file is not present in the list, it is added to the front. If it is
  15763. present, it is moved there. With optional argument TO-END, add/move to the
  15764. end of the list."
  15765. (interactive "P")
  15766. (let ((org-agenda-skip-unavailable-files nil)
  15767. (file-alist (mapcar (lambda (x)
  15768. (cons (file-truename x) x))
  15769. (org-agenda-files t)))
  15770. (ctf (file-truename buffer-file-name))
  15771. x had)
  15772. (setq x (assoc ctf file-alist) had x)
  15773. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15774. (if to-end
  15775. (setq file-alist (append (delq x file-alist) (list x)))
  15776. (setq file-alist (cons x (delq x file-alist))))
  15777. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15778. (org-install-agenda-files-menu)
  15779. (message "File %s to %s of agenda file list"
  15780. (if had "moved" "added") (if to-end "end" "front"))))
  15781. (defun org-remove-file (&optional file)
  15782. "Remove current file from the list of files in variable `org-agenda-files'.
  15783. These are the files which are being checked for agenda entries.
  15784. Optional argument FILE means, use this file instead of the current."
  15785. (interactive)
  15786. (let* ((org-agenda-skip-unavailable-files nil)
  15787. (file (or file buffer-file-name))
  15788. (true-file (file-truename file))
  15789. (afile (abbreviate-file-name file))
  15790. (files (delq nil (mapcar
  15791. (lambda (x)
  15792. (if (equal true-file
  15793. (file-truename x))
  15794. nil x))
  15795. (org-agenda-files t)))))
  15796. (if (not (= (length files) (length (org-agenda-files t))))
  15797. (progn
  15798. (org-store-new-agenda-file-list files)
  15799. (org-install-agenda-files-menu)
  15800. (message "Removed file: %s" afile))
  15801. (message "File was not in list: %s" afile))))
  15802. (defun org-file-menu-entry (file)
  15803. (vector file (list 'find-file file) t))
  15804. (defun org-check-agenda-file (file)
  15805. "Make sure FILE exists. If not, ask user what to do."
  15806. (when (not (file-exists-p file))
  15807. (message "non-existent file %s. [R]emove from list or [A]bort?"
  15808. (abbreviate-file-name file))
  15809. (let ((r (downcase (read-char-exclusive))))
  15810. (cond
  15811. ((equal r ?r)
  15812. (org-remove-file file)
  15813. (throw 'nextfile t))
  15814. (t (error "Abort"))))))
  15815. ;;; Agenda prepare and finalize
  15816. (defvar org-agenda-multi nil) ; dynammically scoped
  15817. (defvar org-agenda-buffer-name "*Org Agenda*")
  15818. (defvar org-pre-agenda-window-conf nil)
  15819. (defvar org-agenda-name nil)
  15820. (defun org-prepare-agenda (&optional name)
  15821. (setq org-todo-keywords-for-agenda nil)
  15822. (setq org-done-keywords-for-agenda nil)
  15823. (if org-agenda-multi
  15824. (progn
  15825. (setq buffer-read-only nil)
  15826. (goto-char (point-max))
  15827. (unless (bobp)
  15828. (insert "\n" (make-string (window-width) ?=) "\n"))
  15829. (narrow-to-region (point) (point-max)))
  15830. (org-agenda-maybe-reset-markers 'force)
  15831. (org-prepare-agenda-buffers (org-agenda-files))
  15832. (setq org-todo-keywords-for-agenda
  15833. (org-uniquify org-todo-keywords-for-agenda))
  15834. (setq org-done-keywords-for-agenda
  15835. (org-uniquify org-done-keywords-for-agenda))
  15836. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  15837. (awin (get-buffer-window abuf)))
  15838. (cond
  15839. ((equal (current-buffer) abuf) nil)
  15840. (awin (select-window awin))
  15841. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  15842. ((equal org-agenda-window-setup 'current-window)
  15843. (switch-to-buffer abuf))
  15844. ((equal org-agenda-window-setup 'other-window)
  15845. (org-switch-to-buffer-other-window abuf))
  15846. ((equal org-agenda-window-setup 'other-frame)
  15847. (switch-to-buffer-other-frame abuf))
  15848. ((equal org-agenda-window-setup 'reorganize-frame)
  15849. (delete-other-windows)
  15850. (org-switch-to-buffer-other-window abuf))))
  15851. (setq buffer-read-only nil)
  15852. (erase-buffer)
  15853. (org-agenda-mode)
  15854. (and name (not org-agenda-name)
  15855. (org-set-local 'org-agenda-name name)))
  15856. (setq buffer-read-only nil))
  15857. (defun org-finalize-agenda ()
  15858. "Finishing touch for the agenda buffer, called just before displaying it."
  15859. (unless org-agenda-multi
  15860. (save-excursion
  15861. (let ((inhibit-read-only t))
  15862. (goto-char (point-min))
  15863. (while (org-activate-bracket-links (point-max))
  15864. (add-text-properties (match-beginning 0) (match-end 0)
  15865. '(face org-link)))
  15866. (org-agenda-align-tags)
  15867. (unless org-agenda-with-colors
  15868. (remove-text-properties (point-min) (point-max) '(face nil))))
  15869. (if (and (boundp 'org-overriding-columns-format)
  15870. org-overriding-columns-format)
  15871. (org-set-local 'org-overriding-columns-format
  15872. org-overriding-columns-format))
  15873. (if (and (boundp 'org-agenda-view-columns-initially)
  15874. org-agenda-view-columns-initially)
  15875. (org-agenda-columns))
  15876. (run-hooks 'org-finalize-agenda-hook))))
  15877. (defun org-prepare-agenda-buffers (files)
  15878. "Create buffers for all agenda files, protect archived trees and comments."
  15879. (interactive)
  15880. (let ((pa '(:org-archived t))
  15881. (pc '(:org-comment t))
  15882. (pall '(:org-archived t :org-comment t))
  15883. (inhibit-read-only t)
  15884. (rea (concat ":" org-archive-tag ":"))
  15885. bmp file re)
  15886. (save-excursion
  15887. (save-restriction
  15888. (while (setq file (pop files))
  15889. (org-check-agenda-file file)
  15890. (set-buffer (org-get-agenda-file-buffer file))
  15891. (widen)
  15892. (setq bmp (buffer-modified-p))
  15893. (setq org-todo-keywords-for-agenda
  15894. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15895. (setq org-done-keywords-for-agenda
  15896. (append org-done-keywords-for-agenda org-done-keywords))
  15897. (save-excursion
  15898. (remove-text-properties (point-min) (point-max) pall)
  15899. (when org-agenda-skip-archived-trees
  15900. (goto-char (point-min))
  15901. (while (re-search-forward rea nil t)
  15902. (if (org-on-heading-p t)
  15903. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15904. (goto-char (point-min))
  15905. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  15906. (while (re-search-forward re nil t)
  15907. (add-text-properties
  15908. (match-beginning 0) (org-end-of-subtree t) pc)))
  15909. (set-buffer-modified-p bmp))))))
  15910. (defvar org-agenda-skip-function nil
  15911. "Function to be called at each match during agenda construction.
  15912. If this function returns nil, the current match should not be skipped.
  15913. Otherwise, the function must return a position from where the search
  15914. should be continued.
  15915. This may also be a Lisp form, it will be evaluated.
  15916. Never set this variable using `setq' or so, because then it will apply
  15917. to all future agenda commands. Instead, bind it with `let' to scope
  15918. it dynamically into the agenda-constructing command. A good way to set
  15919. it is through options in org-agenda-custom-commands.")
  15920. (defun org-agenda-skip ()
  15921. "Throw to `:skip' in places that should be skipped.
  15922. Also moves point to the end of the skipped region, so that search can
  15923. continue from there."
  15924. (let ((p (point-at-bol)) to fp)
  15925. (and org-agenda-skip-archived-trees
  15926. (get-text-property p :org-archived)
  15927. (org-end-of-subtree t)
  15928. (throw :skip t))
  15929. (and (get-text-property p :org-comment)
  15930. (org-end-of-subtree t)
  15931. (throw :skip t))
  15932. (if (equal (char-after p) ?#) (throw :skip t))
  15933. (when (and (or (setq fp (functionp org-agenda-skip-function))
  15934. (consp org-agenda-skip-function))
  15935. (setq to (save-excursion
  15936. (save-match-data
  15937. (if fp
  15938. (funcall org-agenda-skip-function)
  15939. (eval org-agenda-skip-function))))))
  15940. (goto-char to)
  15941. (throw :skip t))))
  15942. (defvar org-agenda-markers nil
  15943. "List of all currently active markers created by `org-agenda'.")
  15944. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  15945. "Creation time of the last agenda marker.")
  15946. (defun org-agenda-new-marker (&optional pos)
  15947. "Return a new agenda marker.
  15948. Org-mode keeps a list of these markers and resets them when they are
  15949. no longer in use."
  15950. (let ((m (copy-marker (or pos (point)))))
  15951. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  15952. (push m org-agenda-markers)
  15953. m))
  15954. (defun org-agenda-maybe-reset-markers (&optional force)
  15955. "Reset markers created by `org-agenda'. But only if they are old enough."
  15956. (if (or (and force (not org-agenda-multi))
  15957. (> (- (time-to-seconds (current-time))
  15958. org-agenda-last-marker-time)
  15959. 5))
  15960. (while org-agenda-markers
  15961. (move-marker (pop org-agenda-markers) nil))))
  15962. (defvar org-agenda-new-buffers nil
  15963. "Buffers created to visit agenda files.")
  15964. (defun org-get-agenda-file-buffer (file)
  15965. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15966. it to the list of buffers which might be released later."
  15967. (let ((buf (org-find-base-buffer-visiting file)))
  15968. (if buf
  15969. buf ; just return it
  15970. ;; Make a new buffer and remember it
  15971. (setq buf (find-file-noselect file))
  15972. (if buf (push buf org-agenda-new-buffers))
  15973. buf)))
  15974. (defun org-release-buffers (blist)
  15975. "Release all buffers in list, asking the user for confirmation when needed.
  15976. When a buffer is unmodified, it is just killed. When modified, it is saved
  15977. \(if the user agrees) and then killed."
  15978. (let (buf file)
  15979. (while (setq buf (pop blist))
  15980. (setq file (buffer-file-name buf))
  15981. (when (and (buffer-modified-p buf)
  15982. file
  15983. (y-or-n-p (format "Save file %s? " file)))
  15984. (with-current-buffer buf (save-buffer)))
  15985. (kill-buffer buf))))
  15986. (defvar org-category-table nil)
  15987. (defun org-get-category-table ()
  15988. "Get the table of categories and positions in current buffer."
  15989. (let (tbl)
  15990. (save-excursion
  15991. (save-restriction
  15992. (widen)
  15993. (goto-char (point-min))
  15994. (while (re-search-forward "^#\\+CATEGORY:[ \t]*\\(.*\\)"
  15995. nil t)
  15996. (push (cons (match-beginning 1)
  15997. (org-trim (match-string 1))) tbl))))
  15998. tbl))
  15999. (defun org-get-category (&optional pos)
  16000. "Get the category applying to position POS."
  16001. (if (not org-category-table)
  16002. (cond
  16003. ((null org-category)
  16004. (setq org-category
  16005. (if buffer-file-name
  16006. (file-name-sans-extension
  16007. (file-name-nondirectory buffer-file-name))
  16008. "???")))
  16009. ((symbolp org-category) (symbol-name org-category))
  16010. (t org-category))
  16011. (let ((tbl org-category-table)
  16012. (pos (or pos (point))))
  16013. (while (and tbl (> (caar tbl) pos))
  16014. (pop tbl))
  16015. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  16016. org-category-table))))))
  16017. ;;; Agenda timeline
  16018. (defun org-timeline (&optional include-all)
  16019. "Show a time-sorted view of the entries in the current org file.
  16020. Only entries with a time stamp of today or later will be listed. With
  16021. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  16022. under the current date.
  16023. If the buffer contains an active region, only check the region for
  16024. dates."
  16025. (interactive "P")
  16026. (require 'calendar)
  16027. (org-compile-prefix-format 'timeline)
  16028. (org-set-sorting-strategy 'timeline)
  16029. (let* ((dopast t)
  16030. (dotodo include-all)
  16031. (doclosed org-agenda-show-log)
  16032. (entry buffer-file-name)
  16033. (date (calendar-current-date))
  16034. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  16035. (end (if (org-region-active-p) (region-end) (point-max)))
  16036. (day-numbers (org-get-all-dates beg end 'no-ranges
  16037. t doclosed ; always include today
  16038. org-timeline-show-empty-dates))
  16039. (today (time-to-days (current-time)))
  16040. (past t)
  16041. args
  16042. s e rtn d emptyp)
  16043. (setq org-agenda-redo-command
  16044. (list 'progn
  16045. (list 'org-switch-to-buffer-other-window (current-buffer))
  16046. (list 'org-timeline (list 'quote include-all))))
  16047. (if (not dopast)
  16048. ;; Remove past dates from the list of dates.
  16049. (setq day-numbers (delq nil (mapcar (lambda(x)
  16050. (if (>= x today) x nil))
  16051. day-numbers))))
  16052. (org-prepare-agenda (concat "Timeline "
  16053. (file-name-nondirectory buffer-file-name)))
  16054. (if doclosed (push :closed args))
  16055. (push :timestamp args)
  16056. (push :sexp args)
  16057. (if dotodo (push :todo args))
  16058. (while (setq d (pop day-numbers))
  16059. (if (and (listp d) (eq (car d) :omitted))
  16060. (progn
  16061. (setq s (point))
  16062. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  16063. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  16064. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  16065. (if (and (>= d today)
  16066. dopast
  16067. past)
  16068. (progn
  16069. (setq past nil)
  16070. (insert (make-string 79 ?-) "\n")))
  16071. (setq date (calendar-gregorian-from-absolute d))
  16072. (setq s (point))
  16073. (setq rtn (and (not emptyp)
  16074. (apply 'org-agenda-get-day-entries
  16075. entry date args)))
  16076. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  16077. (progn
  16078. (insert (calendar-day-name date) " "
  16079. (number-to-string (extract-calendar-day date)) " "
  16080. (calendar-month-name (extract-calendar-month date)) " "
  16081. (number-to-string (extract-calendar-year date)) "\n")
  16082. ; FIXME: this gives a timezone problem
  16083. ; (insert (format-time-string org-agenda-date-format
  16084. ; (calendar-time-from-absolute d 0))
  16085. ; "\n")
  16086. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  16087. (put-text-property s (1- (point)) 'org-date-line t)
  16088. (if (equal d today)
  16089. (put-text-property s (1- (point)) 'org-today t))
  16090. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  16091. (put-text-property s (1- (point)) 'day d)))))
  16092. (goto-char (point-min))
  16093. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  16094. (point-min)))
  16095. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  16096. (org-finalize-agenda)
  16097. (setq buffer-read-only t)))
  16098. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  16099. "Return a list of all relevant day numbers from BEG to END buffer positions.
  16100. If NO-RANGES is non-nil, include only the start and end dates of a range,
  16101. not every single day in the range. If FORCE-TODAY is non-nil, make
  16102. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  16103. inactive time stamps (those in square brackets) are included.
  16104. When EMPTY is non-nil, also include days without any entries."
  16105. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  16106. dates dates1 date day day1 day2 ts1 ts2)
  16107. (if force-today
  16108. (setq dates (list (time-to-days (current-time)))))
  16109. (save-excursion
  16110. (goto-char beg)
  16111. (while (re-search-forward re end t)
  16112. (setq day (time-to-days (org-time-string-to-time
  16113. (substring (match-string 1) 0 10))))
  16114. (or (memq day dates) (push day dates)))
  16115. (unless no-ranges
  16116. (goto-char beg)
  16117. (while (re-search-forward org-tr-regexp end t)
  16118. (setq ts1 (substring (match-string 1) 0 10)
  16119. ts2 (substring (match-string 2) 0 10)
  16120. day1 (time-to-days (org-time-string-to-time ts1))
  16121. day2 (time-to-days (org-time-string-to-time ts2)))
  16122. (while (< (setq day1 (1+ day1)) day2)
  16123. (or (memq day1 dates) (push day1 dates)))))
  16124. (setq dates (sort dates '<))
  16125. (when empty
  16126. (while (setq day (pop dates))
  16127. (setq day2 (car dates))
  16128. (push day dates1)
  16129. (when (and day2 empty)
  16130. (if (or (eq empty t)
  16131. (and (numberp empty) (<= (- day2 day) empty)))
  16132. (while (< (setq day (1+ day)) day2)
  16133. (push (list day) dates1))
  16134. (push (cons :omitted (- day2 day)) dates1))))
  16135. (setq dates (nreverse dates1)))
  16136. dates)))
  16137. ;;; Agenda Daily/Weekly
  16138. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  16139. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  16140. (defvar org-agenda-last-arguments nil
  16141. "The arguments of the previous call to org-agenda")
  16142. (defvar org-starting-day nil) ; local variable in the agenda buffer
  16143. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  16144. (defvar org-include-all-loc nil) ; local variable
  16145. ;;;###autoload
  16146. (defun org-agenda-list (&optional include-all start-day ndays)
  16147. "Produce a weekly view from all files in variable `org-agenda-files'.
  16148. The view will be for the current week, but from the overview buffer you
  16149. will be able to go to other weeks.
  16150. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  16151. also be shown, under the current date.
  16152. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  16153. on the days are also shown. See the variable `org-log-done' for how
  16154. to turn on logging.
  16155. START-DAY defaults to TODAY, or to the most recent match for the weekday
  16156. given in `org-agenda-start-on-weekday'.
  16157. NDAYS defaults to `org-agenda-ndays'."
  16158. (interactive "P")
  16159. (setq ndays (or ndays org-agenda-ndays)
  16160. start-day (or start-day org-agenda-start-day))
  16161. (if org-agenda-overriding-arguments
  16162. (setq include-all (car org-agenda-overriding-arguments)
  16163. start-day (nth 1 org-agenda-overriding-arguments)
  16164. ndays (nth 2 org-agenda-overriding-arguments)))
  16165. (if (stringp start-day)
  16166. ;; Convert to an absolute day number
  16167. (setq start-day (time-to-days (org-read-date nil t start-day))))
  16168. (setq org-agenda-last-arguments (list include-all start-day ndays))
  16169. (org-compile-prefix-format 'agenda)
  16170. (org-set-sorting-strategy 'agenda)
  16171. (require 'calendar)
  16172. (let* ((org-agenda-start-on-weekday
  16173. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  16174. org-agenda-start-on-weekday nil))
  16175. (thefiles (org-agenda-files))
  16176. (files thefiles)
  16177. (today (time-to-days (current-time)))
  16178. (sd (or start-day today))
  16179. (start (if (or (null org-agenda-start-on-weekday)
  16180. (< org-agenda-ndays 7))
  16181. sd
  16182. (let* ((nt (calendar-day-of-week
  16183. (calendar-gregorian-from-absolute sd)))
  16184. (n1 org-agenda-start-on-weekday)
  16185. (d (- nt n1)))
  16186. (- sd (+ (if (< d 0) 7 0) d)))))
  16187. (day-numbers (list start))
  16188. (inhibit-redisplay (not debug-on-error))
  16189. s e rtn rtnall file date d start-pos end-pos todayp nd)
  16190. (setq org-agenda-redo-command
  16191. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  16192. ;; Make the list of days
  16193. (setq ndays (or ndays org-agenda-ndays)
  16194. nd ndays)
  16195. (while (> ndays 1)
  16196. (push (1+ (car day-numbers)) day-numbers)
  16197. (setq ndays (1- ndays)))
  16198. (setq day-numbers (nreverse day-numbers))
  16199. (org-prepare-agenda "Day/Week")
  16200. (org-set-local 'org-starting-day (car day-numbers))
  16201. (org-set-local 'org-include-all-loc include-all)
  16202. (org-set-local 'org-agenda-span
  16203. (org-agenda-ndays-to-span nd))
  16204. (when (and (or include-all org-agenda-include-all-todo)
  16205. (member today day-numbers))
  16206. (setq files thefiles
  16207. rtnall nil)
  16208. (while (setq file (pop files))
  16209. (catch 'nextfile
  16210. (org-check-agenda-file file)
  16211. (setq date (calendar-gregorian-from-absolute today)
  16212. rtn (org-agenda-get-day-entries
  16213. file date :todo))
  16214. (setq rtnall (append rtnall rtn))))
  16215. (when rtnall
  16216. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  16217. (add-text-properties (point-min) (1- (point))
  16218. (list 'face 'org-agenda-structure))
  16219. (insert (org-finalize-agenda-entries rtnall) "\n")))
  16220. (setq s (point))
  16221. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  16222. "-agenda:\n")
  16223. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  16224. 'org-date-line t))
  16225. (while (setq d (pop day-numbers))
  16226. (setq date (calendar-gregorian-from-absolute d)
  16227. s (point))
  16228. (if (or (setq todayp (= d today))
  16229. (and (not start-pos) (= d sd)))
  16230. (setq start-pos (point))
  16231. (if (and start-pos (not end-pos))
  16232. (setq end-pos (point))))
  16233. (setq files thefiles
  16234. rtnall nil)
  16235. (while (setq file (pop files))
  16236. (catch 'nextfile
  16237. (org-check-agenda-file file)
  16238. (if org-agenda-show-log
  16239. (setq rtn (org-agenda-get-day-entries
  16240. file date
  16241. :deadline :scheduled :timestamp :sexp :closed))
  16242. (setq rtn (org-agenda-get-day-entries
  16243. file date
  16244. :deadline :scheduled :sexp :timestamp)))
  16245. (setq rtnall (append rtnall rtn))))
  16246. (if org-agenda-include-diary
  16247. (progn
  16248. (require 'diary-lib)
  16249. (setq rtn (org-get-entries-from-diary date))
  16250. (setq rtnall (append rtnall rtn))))
  16251. (if (or rtnall org-agenda-show-all-dates)
  16252. (progn
  16253. (insert (format "%-9s %2d %s %4d\n"
  16254. (calendar-day-name date)
  16255. (extract-calendar-day date)
  16256. (calendar-month-name (extract-calendar-month date))
  16257. (extract-calendar-year date)))
  16258. ; FIXME: this gives a timezone problem
  16259. ; (insert (format-time-string org-agenda-date-format
  16260. ; (calendar-time-from-absolute d 0)) "\n")
  16261. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  16262. (put-text-property s (1- (point)) 'org-date-line t)
  16263. (if todayp (put-text-property s (1- (point)) 'org-today t))
  16264. (if rtnall (insert
  16265. (org-finalize-agenda-entries
  16266. (org-agenda-add-time-grid-maybe
  16267. rtnall nd todayp))
  16268. "\n"))
  16269. (put-text-property s (1- (point)) 'day d))))
  16270. (goto-char (point-min))
  16271. (org-fit-agenda-window)
  16272. (unless (and (pos-visible-in-window-p (point-min))
  16273. (pos-visible-in-window-p (point-max)))
  16274. (goto-char (1- (point-max)))
  16275. (recenter -1)
  16276. (if (not (pos-visible-in-window-p (or start-pos 1)))
  16277. (progn
  16278. (goto-char (or start-pos 1))
  16279. (recenter 1))))
  16280. (goto-char (or start-pos 1))
  16281. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  16282. (org-finalize-agenda)
  16283. (setq buffer-read-only t)
  16284. (message "")))
  16285. (defun org-agenda-ndays-to-span (n)
  16286. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  16287. ;;; Agenda TODO list
  16288. (defvar org-select-this-todo-keyword nil)
  16289. (defvar org-last-arg nil)
  16290. ;;;###autoload
  16291. (defun org-todo-list (arg)
  16292. "Show all TODO entries from all agenda file in a single list.
  16293. The prefix arg can be used to select a specific TODO keyword and limit
  16294. the list to these. When using \\[universal-argument], you will be prompted
  16295. for a keyword. A numeric prefix directly selects the Nth keyword in
  16296. `org-todo-keywords-1'."
  16297. (interactive "P")
  16298. (require 'calendar)
  16299. (org-compile-prefix-format 'todo)
  16300. (org-set-sorting-strategy 'todo)
  16301. (org-prepare-agenda "TODO")
  16302. (let* ((today (time-to-days (current-time)))
  16303. (date (calendar-gregorian-from-absolute today))
  16304. (kwds org-todo-keywords-for-agenda)
  16305. (completion-ignore-case t)
  16306. (org-select-this-todo-keyword
  16307. (if (stringp arg) arg
  16308. (and arg (integerp arg) (> arg 0)
  16309. (nth (1- arg) kwds))))
  16310. rtn rtnall files file pos)
  16311. (when (equal arg '(4))
  16312. (setq org-select-this-todo-keyword
  16313. (completing-read "Keyword (or KWD1|K2D2|...): "
  16314. (mapcar 'list kwds) nil nil)))
  16315. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  16316. (org-set-local 'org-last-arg arg)
  16317. (setq org-agenda-redo-command
  16318. '(org-todo-list (or current-prefix-arg org-last-arg)))
  16319. (setq files (org-agenda-files)
  16320. rtnall nil)
  16321. (while (setq file (pop files))
  16322. (catch 'nextfile
  16323. (org-check-agenda-file file)
  16324. (setq rtn (org-agenda-get-day-entries file date :todo))
  16325. (setq rtnall (append rtnall rtn))))
  16326. (if org-agenda-overriding-header
  16327. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16328. nil 'face 'org-agenda-structure) "\n")
  16329. (insert "Global list of TODO items of type: ")
  16330. (add-text-properties (point-min) (1- (point))
  16331. (list 'face 'org-agenda-structure))
  16332. (setq pos (point))
  16333. (insert (or org-select-this-todo-keyword "ALL") "\n")
  16334. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16335. (setq pos (point))
  16336. (unless org-agenda-multi
  16337. (insert "Available with `N r': (0)ALL")
  16338. (let ((n 0) s)
  16339. (mapc (lambda (x)
  16340. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  16341. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  16342. (insert "\n "))
  16343. (insert " " s))
  16344. kwds))
  16345. (insert "\n"))
  16346. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16347. (when rtnall
  16348. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16349. (goto-char (point-min))
  16350. (org-fit-agenda-window)
  16351. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  16352. (org-finalize-agenda)
  16353. (setq buffer-read-only t)))
  16354. ;;; Agenda tags match
  16355. ;;;###autoload
  16356. (defun org-tags-view (&optional todo-only match)
  16357. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  16358. The prefix arg TODO-ONLY limits the search to TODO entries."
  16359. (interactive "P")
  16360. (org-compile-prefix-format 'tags)
  16361. (org-set-sorting-strategy 'tags)
  16362. (let* ((org-tags-match-list-sublevels
  16363. (if todo-only t org-tags-match-list-sublevels))
  16364. (completion-ignore-case t)
  16365. rtn rtnall files file pos matcher
  16366. buffer)
  16367. (setq matcher (org-make-tags-matcher match)
  16368. match (car matcher) matcher (cdr matcher))
  16369. (org-prepare-agenda (concat "TAGS " match))
  16370. (setq org-agenda-redo-command
  16371. (list 'org-tags-view (list 'quote todo-only)
  16372. (list 'if 'current-prefix-arg nil match)))
  16373. (setq files (org-agenda-files)
  16374. rtnall nil)
  16375. (while (setq file (pop files))
  16376. (catch 'nextfile
  16377. (org-check-agenda-file file)
  16378. (setq buffer (if (file-exists-p file)
  16379. (org-get-agenda-file-buffer file)
  16380. (error "No such file %s" file)))
  16381. (if (not buffer)
  16382. ;; If file does not exist, merror message to agenda
  16383. (setq rtn (list
  16384. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16385. rtnall (append rtnall rtn))
  16386. (with-current-buffer buffer
  16387. (unless (org-mode-p)
  16388. (error "Agenda file %s is not in `org-mode'" file))
  16389. (setq org-category-table (org-get-category-table))
  16390. (save-excursion
  16391. (save-restriction
  16392. (if org-agenda-restrict
  16393. (narrow-to-region org-agenda-restrict-begin
  16394. org-agenda-restrict-end)
  16395. (widen))
  16396. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  16397. (setq rtnall (append rtnall rtn))))))))
  16398. (if org-agenda-overriding-header
  16399. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16400. nil 'face 'org-agenda-structure) "\n")
  16401. (insert "Headlines with TAGS match: ")
  16402. (add-text-properties (point-min) (1- (point))
  16403. (list 'face 'org-agenda-structure))
  16404. (setq pos (point))
  16405. (insert match "\n")
  16406. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16407. (setq pos (point))
  16408. (unless org-agenda-multi
  16409. (insert "Press `C-u r' to search again with new search string\n"))
  16410. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16411. (when rtnall
  16412. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16413. (goto-char (point-min))
  16414. (org-fit-agenda-window)
  16415. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  16416. (org-finalize-agenda)
  16417. (setq buffer-read-only t)))
  16418. ;;; Agenda Finding stuck projects
  16419. (defvar org-agenda-skip-regexp nil
  16420. "Regular expression used in skipping subtrees for the agenda.
  16421. This is basically a temporary global variable that can be set and then
  16422. used by user-defined selections using `org-agenda-skip-function'.")
  16423. (defvar org-agenda-overriding-header nil
  16424. "When this is set during todo and tags searches, will replace header.")
  16425. (defun org-agenda-skip-subtree-when-regexp-matches ()
  16426. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  16427. If yes, it returns the end position of this tree, causing agenda commands
  16428. to skip this subtree. This is a function that can be put into
  16429. `org-agenda-skip-function' for the duration of a command."
  16430. (let ((end (save-excursion (org-end-of-subtree t)))
  16431. skip)
  16432. (save-excursion
  16433. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  16434. (and skip end)))
  16435. (defun org-agenda-skip-entry-if (&rest conditions)
  16436. "Skip entry is any of CONDITIONS is true.
  16437. See `org-agenda-skip-if for details."
  16438. (org-agenda-skip-if nil conditions))
  16439. (defun org-agenda-skip-subtree-if (&rest conditions)
  16440. "Skip entry is any of CONDITIONS is true.
  16441. See `org-agenda-skip-if for details."
  16442. (org-agenda-skip-if t conditions))
  16443. (defun org-agenda-skip-if (subtree conditions)
  16444. "Checks current entity for CONDITIONS.
  16445. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  16446. the entry, i.e. the text before the next heading is checked.
  16447. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  16448. from different tests. Valid conditions are:
  16449. scheduled Check if there is a scheduled cookie
  16450. notscheduled Check if there is no scheduled cookie
  16451. deadline Check if there is a deadline
  16452. notdeadline Check if there is no deadline
  16453. regexp Check if regexp matches
  16454. notregexp Check if regexp does not match.
  16455. The regexp is taken from the conditions list, it must com right after the
  16456. `regexp' of `notregexp' element.
  16457. If any of these conditions is met, this function returns the end point of
  16458. the entity, causing the search to continue from there. This is a function
  16459. that can be put into `org-agenda-skip-function' for the duration of a command."
  16460. (let (beg end m r)
  16461. (org-back-to-heading t)
  16462. (setq beg (point)
  16463. end (if subtree
  16464. (progn (org-end-of-subtree t) (point))
  16465. (progn (outline-next-heading) (1- (point)))))
  16466. (goto-char beg)
  16467. (and
  16468. (or
  16469. (and (memq 'scheduled conditions)
  16470. (re-search-forward org-scheduled-time-regexp end t))
  16471. (and (memq 'notscheduled conditions)
  16472. (not (re-search-forward org-scheduled-time-regexp end t)))
  16473. (and (memq 'deadline conditions)
  16474. (re-search-forward org-deadline-time-regexp end t))
  16475. (and (memq 'notdeadline conditions)
  16476. (not (re-search-forward org-deadline-time-regexp end t)))
  16477. (and (setq m (memq 'regexp conditions))
  16478. (stringp (setq r (nth 1 m)))
  16479. (re-search-forward (nth 1 m) end t))
  16480. (and (setq m (memq 'notregexp conditions))
  16481. (stringp (setq r (nth 1 m)))
  16482. (not (re-search-forward (nth 1 m) end t))))
  16483. end)))
  16484. (defun org-agenda-list-stuck-projects (&rest ignore)
  16485. "Create agenda view for projects that are stuck.
  16486. Stuck projects are project that have no next actions. For the definitions
  16487. of what a project is and how to check if it stuck, customize the variable
  16488. `org-stuck-projects'.
  16489. MATCH is being ignored."
  16490. (interactive)
  16491. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  16492. ;; FIXME: we could have used org-agenda-skip-if here.
  16493. (org-agenda-overriding-header "List of stuck projects: ")
  16494. (matcher (nth 0 org-stuck-projects))
  16495. (todo (nth 1 org-stuck-projects))
  16496. (todo-wds (if (member "*" todo)
  16497. (progn
  16498. (org-prepare-agenda-buffers (org-agenda-files))
  16499. (org-delete-all
  16500. org-done-keywords-for-agenda
  16501. (copy-sequence org-todo-keywords-for-agenda)))
  16502. todo))
  16503. (todo-re (concat "^\\*+[ \t]+\\("
  16504. (mapconcat 'identity todo-wds "\\|")
  16505. "\\)\\>"))
  16506. (tags (nth 2 org-stuck-projects))
  16507. (tags-re (if (member "*" tags)
  16508. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  16509. (concat "^\\*+ .*:\\("
  16510. (mapconcat 'identity tags "\\|")
  16511. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  16512. (gen-re (nth 3 org-stuck-projects))
  16513. (re-list
  16514. (delq nil
  16515. (list
  16516. (if todo todo-re)
  16517. (if tags tags-re)
  16518. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  16519. gen-re)))))
  16520. (setq org-agenda-skip-regexp
  16521. (if re-list
  16522. (mapconcat 'identity re-list "\\|")
  16523. (error "No information how to identify unstuck projects")))
  16524. (org-tags-view nil matcher)
  16525. (with-current-buffer org-agenda-buffer-name
  16526. (setq org-agenda-redo-command
  16527. '(org-agenda-list-stuck-projects
  16528. (or current-prefix-arg org-last-arg))))))
  16529. ;;; Diary integration
  16530. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  16531. (defun org-get-entries-from-diary (date)
  16532. "Get the (Emacs Calendar) diary entries for DATE."
  16533. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  16534. (diary-display-hook '(fancy-diary-display))
  16535. (pop-up-frames nil)
  16536. (list-diary-entries-hook
  16537. (cons 'org-diary-default-entry list-diary-entries-hook))
  16538. (diary-file-name-prefix-function nil) ; turn this feature off
  16539. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  16540. entries
  16541. (org-disable-agenda-to-diary t))
  16542. (save-excursion
  16543. (save-window-excursion
  16544. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  16545. (if (not (get-buffer fancy-diary-buffer))
  16546. (setq entries nil)
  16547. (with-current-buffer fancy-diary-buffer
  16548. (setq buffer-read-only nil)
  16549. (if (zerop (buffer-size))
  16550. ;; No entries
  16551. (setq entries nil)
  16552. ;; Omit the date and other unnecessary stuff
  16553. (org-agenda-cleanup-fancy-diary)
  16554. ;; Add prefix to each line and extend the text properties
  16555. (if (zerop (buffer-size))
  16556. (setq entries nil)
  16557. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  16558. (set-buffer-modified-p nil)
  16559. (kill-buffer fancy-diary-buffer)))
  16560. (when entries
  16561. (setq entries (org-split-string entries "\n"))
  16562. (setq entries
  16563. (mapcar
  16564. (lambda (x)
  16565. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  16566. ;; Extend the text properties to the beginning of the line
  16567. (org-add-props x (text-properties-at (1- (length x)) x)
  16568. 'type "diary" 'date date))
  16569. entries)))))
  16570. (defun org-agenda-cleanup-fancy-diary ()
  16571. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  16572. This gets rid of the date, the underline under the date, and
  16573. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  16574. date. It also removes lines that contain only whitespace."
  16575. (goto-char (point-min))
  16576. (if (looking-at ".*?:[ \t]*")
  16577. (progn
  16578. (replace-match "")
  16579. (re-search-forward "\n=+$" nil t)
  16580. (replace-match "")
  16581. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  16582. (re-search-forward "\n=+$" nil t)
  16583. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  16584. (goto-char (point-min))
  16585. (while (re-search-forward "^ +\n" nil t)
  16586. (replace-match ""))
  16587. (goto-char (point-min))
  16588. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  16589. (replace-match "")))
  16590. ;; Make sure entries from the diary have the right text properties.
  16591. (eval-after-load "diary-lib"
  16592. '(if (boundp 'diary-modify-entry-list-string-function)
  16593. ;; We can rely on the hook, nothing to do
  16594. nil
  16595. ;; Hook not avaiable, must use advice to make this work
  16596. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  16597. "Make the position visible."
  16598. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  16599. (stringp string)
  16600. buffer-file-name)
  16601. (setq string (org-modify-diary-entry-string string))))))
  16602. (defun org-modify-diary-entry-string (string)
  16603. "Add text properties to string, allowing org-mode to act on it."
  16604. (org-add-props string nil
  16605. 'mouse-face 'highlight
  16606. 'keymap org-agenda-keymap
  16607. 'help-echo (if buffer-file-name
  16608. (format "mouse-2 or RET jump to diary file %s"
  16609. (abbreviate-file-name buffer-file-name))
  16610. "")
  16611. 'org-agenda-diary-link t
  16612. 'org-marker (org-agenda-new-marker (point-at-bol))))
  16613. (defun org-diary-default-entry ()
  16614. "Add a dummy entry to the diary.
  16615. Needed to avoid empty dates which mess up holiday display."
  16616. ;; Catch the error if dealing with the new add-to-diary-alist
  16617. (when org-disable-agenda-to-diary
  16618. (condition-case nil
  16619. (add-to-diary-list original-date "Org-mode dummy" "")
  16620. (error
  16621. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  16622. ;;;###autoload
  16623. (defun org-diary (&rest args)
  16624. "Return diary information from org-files.
  16625. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  16626. It accesses org files and extracts information from those files to be
  16627. listed in the diary. The function accepts arguments specifying what
  16628. items should be listed. The following arguments are allowed:
  16629. :timestamp List the headlines of items containing a date stamp or
  16630. date range matching the selected date. Deadlines will
  16631. also be listed, on the expiration day.
  16632. :sexp FIXME
  16633. :deadline List any deadlines past due, or due within
  16634. `org-deadline-warning-days'. The listing occurs only
  16635. in the diary for *today*, not at any other date. If
  16636. an entry is marked DONE, it is no longer listed.
  16637. :scheduled List all items which are scheduled for the given date.
  16638. The diary for *today* also contains items which were
  16639. scheduled earlier and are not yet marked DONE.
  16640. :todo List all TODO items from the org-file. This may be a
  16641. long list - so this is not turned on by default.
  16642. Like deadlines, these entries only show up in the
  16643. diary for *today*, not at any other date.
  16644. The call in the diary file should look like this:
  16645. &%%(org-diary) ~/path/to/some/orgfile.org
  16646. Use a separate line for each org file to check. Or, if you omit the file name,
  16647. all files listed in `org-agenda-files' will be checked automatically:
  16648. &%%(org-diary)
  16649. If you don't give any arguments (as in the example above), the default
  16650. arguments (:deadline :scheduled :timestamp :sexp) are used.
  16651. So the example above may also be written as
  16652. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  16653. The function expects the lisp variables `entry' and `date' to be provided
  16654. by the caller, because this is how the calendar works. Don't use this
  16655. function from a program - use `org-agenda-get-day-entries' instead."
  16656. (org-agenda-maybe-reset-markers)
  16657. (org-compile-prefix-format 'agenda)
  16658. (org-set-sorting-strategy 'agenda)
  16659. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16660. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  16661. (list entry)
  16662. (org-agenda-files t)))
  16663. file rtn results)
  16664. (org-prepare-agenda-buffers files)
  16665. ;; If this is called during org-agenda, don't return any entries to
  16666. ;; the calendar. Org Agenda will list these entries itself.
  16667. (if org-disable-agenda-to-diary (setq files nil))
  16668. (while (setq file (pop files))
  16669. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  16670. (setq results (append results rtn)))
  16671. (if results
  16672. (concat (org-finalize-agenda-entries results) "\n"))))
  16673. ;;; Agenda entry finders
  16674. (defun org-agenda-get-day-entries (file date &rest args)
  16675. "Does the work for `org-diary' and `org-agenda'.
  16676. FILE is the path to a file to be checked for entries. DATE is date like
  16677. the one returned by `calendar-current-date'. ARGS are symbols indicating
  16678. which kind of entries should be extracted. For details about these, see
  16679. the documentation of `org-diary'."
  16680. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16681. (let* ((org-startup-folded nil)
  16682. (org-startup-align-all-tables nil)
  16683. (buffer (if (file-exists-p file)
  16684. (org-get-agenda-file-buffer file)
  16685. (error "No such file %s" file)))
  16686. arg results rtn)
  16687. (if (not buffer)
  16688. ;; If file does not exist, make sure an error message ends up in diary
  16689. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16690. (with-current-buffer buffer
  16691. (unless (org-mode-p)
  16692. (error "Agenda file %s is not in `org-mode'" file))
  16693. (setq org-category-table (org-get-category-table))
  16694. (let ((case-fold-search nil))
  16695. (save-excursion
  16696. (save-restriction
  16697. (if org-agenda-restrict
  16698. (narrow-to-region org-agenda-restrict-begin
  16699. org-agenda-restrict-end)
  16700. (widen))
  16701. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  16702. (while (setq arg (pop args))
  16703. (cond
  16704. ((and (eq arg :todo)
  16705. (equal date (calendar-current-date)))
  16706. (setq rtn (org-agenda-get-todos))
  16707. (setq results (append results rtn)))
  16708. ((eq arg :timestamp)
  16709. (setq rtn (org-agenda-get-blocks))
  16710. (setq results (append results rtn))
  16711. (setq rtn (org-agenda-get-timestamps))
  16712. (setq results (append results rtn)))
  16713. ((eq arg :sexp)
  16714. (setq rtn (org-agenda-get-sexps))
  16715. (setq results (append results rtn)))
  16716. ((eq arg :scheduled)
  16717. (setq rtn (org-agenda-get-scheduled))
  16718. (setq results (append results rtn)))
  16719. ((eq arg :closed)
  16720. (setq rtn (org-agenda-get-closed))
  16721. (setq results (append results rtn)))
  16722. ((eq arg :deadline)
  16723. (setq rtn (org-agenda-get-deadlines))
  16724. (setq results (append results rtn))))))))
  16725. results))))
  16726. ;; FIXME: this works only if the cursor is not at the
  16727. ;; beginning of the entry
  16728. (defun org-entry-is-done-p ()
  16729. "Is the current entry marked DONE?"
  16730. (save-excursion
  16731. (and (re-search-backward "[\r\n]\\*+ " nil t)
  16732. (looking-at org-nl-done-regexp))))
  16733. (defun org-at-date-range-p (&optional inactive-ok)
  16734. "Is the cursor inside a date range?"
  16735. (interactive)
  16736. (save-excursion
  16737. (catch 'exit
  16738. (let ((pos (point)))
  16739. (skip-chars-backward "^[<\r\n")
  16740. (skip-chars-backward "<[")
  16741. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16742. (>= (match-end 0) pos)
  16743. (throw 'exit t))
  16744. (skip-chars-backward "^<[\r\n")
  16745. (skip-chars-backward "<[")
  16746. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16747. (>= (match-end 0) pos)
  16748. (throw 'exit t)))
  16749. nil)))
  16750. (defun org-agenda-get-todos ()
  16751. "Return the TODO information for agenda display."
  16752. (let* ((props (list 'face nil
  16753. 'done-face 'org-done
  16754. 'org-not-done-regexp org-not-done-regexp
  16755. 'org-todo-regexp org-todo-regexp
  16756. 'mouse-face 'highlight
  16757. 'keymap org-agenda-keymap
  16758. 'help-echo
  16759. (format "mouse-2 or RET jump to org file %s"
  16760. (abbreviate-file-name buffer-file-name))))
  16761. ;; FIXME: get rid of the \n at some point but watch out
  16762. (regexp (concat "^\\*+[ \t]+\\("
  16763. (if org-select-this-todo-keyword
  16764. (if (equal org-select-this-todo-keyword "*")
  16765. org-todo-regexp
  16766. (concat "\\<\\("
  16767. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  16768. "\\)\\>"))
  16769. org-not-done-regexp)
  16770. "[^\n\r]*\\)"))
  16771. marker priority category tags
  16772. ee txt beg end)
  16773. (goto-char (point-min))
  16774. (while (re-search-forward regexp nil t)
  16775. (catch :skip
  16776. (save-match-data
  16777. (beginning-of-line)
  16778. (setq beg (point) end (progn (outline-next-heading) (point)))
  16779. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  16780. (re-search-forward org-scheduled-time-regexp end t))
  16781. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  16782. (re-search-forward org-deadline-time-regexp end t)
  16783. (org-deadline-close (match-string 1))))
  16784. (goto-char (1+ beg))
  16785. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  16786. (throw :skip nil)))
  16787. (goto-char beg)
  16788. (org-agenda-skip)
  16789. (goto-char (match-beginning 1))
  16790. (setq marker (org-agenda-new-marker (match-beginning 0))
  16791. category (org-get-category)
  16792. tags (org-get-tags-at (point))
  16793. txt (org-format-agenda-item "" (match-string 1) category tags)
  16794. priority (1+ (org-get-priority txt)))
  16795. (org-add-props txt props
  16796. 'org-marker marker 'org-hd-marker marker
  16797. 'priority priority 'org-category category
  16798. 'type "todo")
  16799. (push txt ee)
  16800. (if org-agenda-todo-list-sublevels
  16801. (goto-char (match-end 1))
  16802. (org-end-of-subtree 'invisible))))
  16803. (nreverse ee)))
  16804. (defconst org-agenda-no-heading-message
  16805. "No heading for this item in buffer or region.")
  16806. (defun org-agenda-get-timestamps ()
  16807. "Return the date stamp information for agenda display."
  16808. (let* ((props (list 'face nil
  16809. 'org-not-done-regexp org-not-done-regexp
  16810. 'org-todo-regexp org-todo-regexp
  16811. 'mouse-face 'highlight
  16812. 'keymap org-agenda-keymap
  16813. 'help-echo
  16814. (format "mouse-2 or RET jump to org file %s"
  16815. (abbreviate-file-name buffer-file-name))))
  16816. (d1 (calendar-absolute-from-gregorian date))
  16817. (regexp
  16818. (concat
  16819. (regexp-quote
  16820. (substring
  16821. (format-time-string
  16822. (car org-time-stamp-formats)
  16823. (apply 'encode-time ; DATE bound by calendar
  16824. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16825. 0 11))
  16826. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  16827. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  16828. marker hdmarker deadlinep scheduledp donep tmp priority category
  16829. ee txt timestr tags b0 b3 e3)
  16830. (goto-char (point-min))
  16831. (while (re-search-forward regexp nil t)
  16832. (setq b0 (match-beginning 0)
  16833. b3 (match-beginning 3) e3 (match-end 3))
  16834. (catch :skip
  16835. (and (org-at-date-range-p) (throw :skip nil))
  16836. (org-agenda-skip)
  16837. (if (and (match-end 1)
  16838. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  16839. (throw :skip nil))
  16840. (if (and e3
  16841. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  16842. (throw :skip nil))
  16843. (setq marker (org-agenda-new-marker b0)
  16844. category (org-get-category b0)
  16845. tmp (buffer-substring (max (point-min)
  16846. (- b0 org-ds-keyword-length))
  16847. b0)
  16848. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  16849. deadlinep (string-match org-deadline-regexp tmp)
  16850. scheduledp (string-match org-scheduled-regexp tmp)
  16851. donep (org-entry-is-done-p))
  16852. (if (or scheduledp deadlinep) (throw :skip t))
  16853. (if (string-match ">" timestr)
  16854. ;; substring should only run to end of time stamp
  16855. (setq timestr (substring timestr 0 (match-end 0))))
  16856. (save-excursion
  16857. (if (re-search-backward "^\\*+ " nil t)
  16858. (progn
  16859. (goto-char (match-beginning 0))
  16860. (setq hdmarker (org-agenda-new-marker)
  16861. tags (org-get-tags-at))
  16862. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16863. (setq txt (org-format-agenda-item
  16864. nil (match-string 1) category tags timestr)))
  16865. (setq txt org-agenda-no-heading-message))
  16866. (setq priority (org-get-priority txt))
  16867. (org-add-props txt props
  16868. 'org-marker marker 'org-hd-marker hdmarker)
  16869. (org-add-props txt nil 'priority priority
  16870. 'org-category category 'date date
  16871. 'type "timestamp")
  16872. (push txt ee))
  16873. (outline-next-heading)))
  16874. (nreverse ee)))
  16875. (defun org-agenda-get-sexps ()
  16876. "Return the sexp information for agenda display."
  16877. (require 'diary-lib)
  16878. (let* ((props (list 'face nil
  16879. 'mouse-face 'highlight
  16880. 'keymap org-agenda-keymap
  16881. 'help-echo
  16882. (format "mouse-2 or RET jump to org file %s"
  16883. (abbreviate-file-name buffer-file-name))))
  16884. (regexp "^&?%%(")
  16885. marker category ee txt tags entry result beg b sexp sexp-entry)
  16886. (goto-char (point-min))
  16887. (while (re-search-forward regexp nil t)
  16888. (catch :skip
  16889. (org-agenda-skip)
  16890. (setq beg (match-beginning 0))
  16891. (goto-char (1- (match-end 0)))
  16892. (setq b (point))
  16893. (forward-sexp 1)
  16894. (setq sexp (buffer-substring b (point)))
  16895. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  16896. (org-trim (match-string 1))
  16897. ""))
  16898. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  16899. (when result
  16900. (setq marker (org-agenda-new-marker beg)
  16901. category (org-get-category beg))
  16902. (if (string-match "\\S-" result)
  16903. (setq txt result)
  16904. (setq txt "SEXP entry returned empty string"))
  16905. (setq txt (org-format-agenda-item
  16906. "" txt category tags 'time))
  16907. (org-add-props txt props 'org-marker marker)
  16908. (org-add-props txt nil
  16909. 'org-category category 'date date
  16910. 'type "sexp")
  16911. (push txt ee))))
  16912. (nreverse ee)))
  16913. (defun org-agenda-get-closed ()
  16914. "Return the logged TODO entries for agenda display."
  16915. (let* ((props (list 'mouse-face 'highlight
  16916. 'org-not-done-regexp org-not-done-regexp
  16917. 'org-todo-regexp org-todo-regexp
  16918. 'keymap org-agenda-keymap
  16919. 'help-echo
  16920. (format "mouse-2 or RET jump to org file %s"
  16921. (abbreviate-file-name buffer-file-name))))
  16922. (regexp (concat
  16923. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  16924. (regexp-quote
  16925. (substring
  16926. (format-time-string
  16927. (car org-time-stamp-formats)
  16928. (apply 'encode-time ; DATE bound by calendar
  16929. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16930. 1 11))))
  16931. marker hdmarker priority category tags closedp
  16932. ee txt timestr)
  16933. (goto-char (point-min))
  16934. (while (re-search-forward regexp nil t)
  16935. (catch :skip
  16936. (org-agenda-skip)
  16937. (setq marker (org-agenda-new-marker (match-beginning 0))
  16938. closedp (equal (match-string 1) org-closed-string)
  16939. category (org-get-category (match-beginning 0))
  16940. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  16941. ;; donep (org-entry-is-done-p)
  16942. )
  16943. (if (string-match "\\]" timestr)
  16944. ;; substring should only run to end of time stamp
  16945. (setq timestr (substring timestr 0 (match-end 0))))
  16946. (save-excursion
  16947. (if (re-search-backward "^\\*+ " nil t)
  16948. (progn
  16949. (goto-char (match-beginning 0))
  16950. (setq hdmarker (org-agenda-new-marker)
  16951. tags (org-get-tags-at))
  16952. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16953. (setq txt (org-format-agenda-item
  16954. (if closedp "Closed: " "Clocked: ")
  16955. (match-string 1) category tags timestr)))
  16956. (setq txt org-agenda-no-heading-message))
  16957. (setq priority 100000)
  16958. (org-add-props txt props
  16959. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  16960. 'priority priority 'org-category category
  16961. 'type "closed" 'date date
  16962. 'undone-face 'org-warning 'done-face 'org-done)
  16963. (push txt ee))
  16964. (outline-next-heading)))
  16965. (nreverse ee)))
  16966. (defun org-agenda-get-deadlines ()
  16967. "Return the deadline information for agenda display."
  16968. (let* ((props (list 'mouse-face 'highlight
  16969. 'org-not-done-regexp org-not-done-regexp
  16970. 'org-todo-regexp org-todo-regexp
  16971. 'keymap org-agenda-keymap
  16972. 'help-echo
  16973. (format "mouse-2 or RET jump to org file %s"
  16974. (abbreviate-file-name buffer-file-name))))
  16975. (regexp org-deadline-time-regexp)
  16976. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  16977. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  16978. d2 diff dfrac wdays pos pos1 category tags
  16979. ee txt head face s upcomingp donep timestr)
  16980. (goto-char (point-min))
  16981. (while (re-search-forward regexp nil t)
  16982. (catch :skip
  16983. (org-agenda-skip)
  16984. (setq s (match-string 1)
  16985. pos (1- (match-beginning 1))
  16986. d2 (org-time-string-to-absolute (match-string 1) d1)
  16987. diff (- d2 d1))
  16988. (if (string-match "-\\([0-9]+\\)\\([dwmy]\\)\\'" s)
  16989. (setq wdays
  16990. (floor
  16991. (* (string-to-number (match-string 1 s))
  16992. (cdr (assoc (match-string 2 s)
  16993. '(("d" . 1) ("w" . 7)
  16994. ("m" . 30.4) ("y" . 365.25)))))))
  16995. (setq wdays org-deadline-warning-days))
  16996. (setq dfrac (/ (* 1.0 (- wdays diff)) wdays))
  16997. (setq upcomingp (and todayp (> diff 0)))
  16998. ;; When to show a deadline in the calendar:
  16999. ;; If the expiration is within wdays warning time.
  17000. ;; Past-due deadlines are only shown on the current date
  17001. (if (or (and (<= diff wdays) todayp)
  17002. (= diff 0))
  17003. (save-excursion
  17004. (setq category (org-get-category))
  17005. (if (re-search-backward "^\\*+[ \t]+" nil t)
  17006. (progn
  17007. (goto-char (match-end 0))
  17008. (setq pos1 (match-beginning 0))
  17009. (setq tags (org-get-tags-at pos1))
  17010. (setq head (buffer-substring-no-properties
  17011. (point)
  17012. (progn (skip-chars-forward "^\r\n")
  17013. (point))))
  17014. (setq donep (string-match org-looking-at-done-regexp head))
  17015. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  17016. (setq timestr
  17017. (concat (substring s (match-beginning 1)) " "))
  17018. (setq timestr 'time))
  17019. (if (and donep
  17020. (or org-agenda-skip-deadline-if-done
  17021. (not (= diff 0))))
  17022. (setq txt nil)
  17023. (setq txt (org-format-agenda-item
  17024. (if (= diff 0)
  17025. "Deadline: "
  17026. (format "In %3d d.: " diff))
  17027. head category tags timestr))))
  17028. (setq txt org-agenda-no-heading-message))
  17029. (when txt
  17030. (setq face (org-agenda-deadline-face dfrac))
  17031. (org-add-props txt props
  17032. 'org-marker (org-agenda-new-marker pos)
  17033. 'org-hd-marker (org-agenda-new-marker pos1)
  17034. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  17035. (org-get-priority txt))
  17036. 'org-category category
  17037. 'type (if upcomingp "upcoming-deadline" "deadline")
  17038. 'date (if upcomingp date d2)
  17039. 'face (if donep 'org-done face)
  17040. 'undone-face face 'done-face 'org-done)
  17041. (push txt ee))))))
  17042. ee))
  17043. (defun org-agenda-deadline-face (fraction)
  17044. "Return the face to displaying a deadline item.
  17045. FRACTION is what fraction of the head-warning time has passed."
  17046. (let ((faces org-agenda-deadline-faces) f)
  17047. (catch 'exit
  17048. (while (setq f (pop faces))
  17049. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  17050. (defun org-agenda-get-scheduled ()
  17051. "Return the scheduled information for agenda display."
  17052. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  17053. 'org-todo-regexp org-todo-regexp
  17054. 'done-face 'org-done
  17055. 'mouse-face 'highlight
  17056. 'keymap org-agenda-keymap
  17057. 'help-echo
  17058. (format "mouse-2 or RET jump to org file %s"
  17059. (abbreviate-file-name buffer-file-name))))
  17060. (regexp org-scheduled-time-regexp)
  17061. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  17062. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  17063. d2 diff pos pos1 category tags
  17064. ee txt head pastschedp donep face timestr s)
  17065. (goto-char (point-min))
  17066. (while (re-search-forward regexp nil t)
  17067. (catch :skip
  17068. (org-agenda-skip)
  17069. (setq s (match-string 1)
  17070. pos (1- (match-beginning 1))
  17071. d2 (org-time-string-to-absolute (match-string 1) d1)
  17072. diff (- d2 d1))
  17073. (setq pastschedp (and todayp (< diff 0)))
  17074. ;; When to show a scheduled item in the calendar:
  17075. ;; If it is on or past the date.
  17076. (if (or (and (< diff 0) todayp)
  17077. (= diff 0))
  17078. (save-excursion
  17079. (setq category (org-get-category))
  17080. (if (re-search-backward "^\\*+[ \t]+" nil t)
  17081. (progn
  17082. (goto-char (match-end 0))
  17083. (setq pos1 (match-beginning 0))
  17084. (setq tags (org-get-tags-at))
  17085. (setq head (buffer-substring-no-properties
  17086. (point)
  17087. (progn (skip-chars-forward "^\r\n") (point))))
  17088. (setq donep (string-match org-looking-at-done-regexp head))
  17089. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  17090. (setq timestr
  17091. (concat (substring s (match-beginning 1)) " "))
  17092. (setq timestr 'time))
  17093. (if (and donep
  17094. (or org-agenda-skip-scheduled-if-done
  17095. (not (= diff 0))))
  17096. (setq txt nil)
  17097. (setq txt (org-format-agenda-item
  17098. (if (= diff 0)
  17099. "Scheduled: "
  17100. (format "Sched.%2dx: " (- 1 diff)))
  17101. head category tags timestr))))
  17102. (setq txt org-agenda-no-heading-message))
  17103. (when txt
  17104. (setq face (if pastschedp
  17105. 'org-scheduled-previously
  17106. 'org-scheduled-today))
  17107. (org-add-props txt props
  17108. 'undone-face face
  17109. 'face (if donep 'org-done face)
  17110. 'org-marker (org-agenda-new-marker pos)
  17111. 'org-hd-marker (org-agenda-new-marker pos1)
  17112. 'type (if pastschedp "past-scheduled" "scheduled")
  17113. 'date (if pastschedp d2 date)
  17114. 'priority (+ (- 5 diff) (org-get-priority txt))
  17115. 'org-category category)
  17116. (push txt ee))))))
  17117. ee))
  17118. (defun org-agenda-get-blocks ()
  17119. "Return the date-range information for agenda display."
  17120. (let* ((props (list 'face nil
  17121. 'org-not-done-regexp org-not-done-regexp
  17122. 'org-todo-regexp org-todo-regexp
  17123. 'mouse-face 'highlight
  17124. 'keymap org-agenda-keymap
  17125. 'help-echo
  17126. (format "mouse-2 or RET jump to org file %s"
  17127. (abbreviate-file-name buffer-file-name))))
  17128. (regexp org-tr-regexp)
  17129. (d0 (calendar-absolute-from-gregorian date))
  17130. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  17131. (goto-char (point-min))
  17132. (while (re-search-forward regexp nil t)
  17133. (catch :skip
  17134. (org-agenda-skip)
  17135. (setq pos (point))
  17136. (setq timestr (match-string 0)
  17137. s1 (match-string 1)
  17138. s2 (match-string 2)
  17139. d1 (time-to-days (org-time-string-to-time s1))
  17140. d2 (time-to-days (org-time-string-to-time s2)))
  17141. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  17142. ;; Only allow days between the limits, because the normal
  17143. ;; date stamps will catch the limits.
  17144. (save-excursion
  17145. (setq marker (org-agenda-new-marker (point)))
  17146. (setq category (org-get-category))
  17147. (if (re-search-backward "^\\*+ " nil t)
  17148. (progn
  17149. (goto-char (match-beginning 0))
  17150. (setq hdmarker (org-agenda-new-marker (point)))
  17151. (setq tags (org-get-tags-at))
  17152. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17153. (setq txt (org-format-agenda-item
  17154. (format (if (= d1 d2) "" "(%d/%d): ")
  17155. (1+ (- d0 d1)) (1+ (- d2 d1)))
  17156. (match-string 1) category tags
  17157. (if (= d0 d1) timestr))))
  17158. (setq txt org-agenda-no-heading-message))
  17159. (org-add-props txt props
  17160. 'org-marker marker 'org-hd-marker hdmarker
  17161. 'type "block" 'date date
  17162. 'priority (org-get-priority txt) 'org-category category)
  17163. (push txt ee)))
  17164. (goto-char pos)))
  17165. ;; Sort the entries by expiration date.
  17166. (nreverse ee)))
  17167. ;;; Agenda presentation and sorting
  17168. (defconst org-plain-time-of-day-regexp
  17169. (concat
  17170. "\\(\\<[012]?[0-9]"
  17171. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  17172. "\\(--?"
  17173. "\\(\\<[012]?[0-9]"
  17174. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  17175. "\\)?")
  17176. "Regular expression to match a plain time or time range.
  17177. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  17178. groups carry important information:
  17179. 0 the full match
  17180. 1 the first time, range or not
  17181. 8 the second time, if it is a range.")
  17182. (defconst org-stamp-time-of-day-regexp
  17183. (concat
  17184. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  17185. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  17186. "\\(--?"
  17187. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  17188. "Regular expression to match a timestamp time or time range.
  17189. After a match, the following groups carry important information:
  17190. 0 the full match
  17191. 1 date plus weekday, for backreferencing to make sure both times on same day
  17192. 2 the first time, range or not
  17193. 4 the second time, if it is a range.")
  17194. (defvar org-prefix-has-time nil
  17195. "A flag, set by `org-compile-prefix-format'.
  17196. The flag is set if the currently compiled format contains a `%t'.")
  17197. (defvar org-prefix-has-tag nil
  17198. "A flag, set by `org-compile-prefix-format'.
  17199. The flag is set if the currently compiled format contains a `%T'.")
  17200. (defun org-format-agenda-item (extra txt &optional category tags dotime
  17201. noprefix)
  17202. "Format TXT to be inserted into the agenda buffer.
  17203. In particular, it adds the prefix and corresponding text properties. EXTRA
  17204. must be a string and replaces the `%s' specifier in the prefix format.
  17205. CATEGORY (string, symbol or nil) may be used to overrule the default
  17206. category taken from local variable or file name. It will replace the `%c'
  17207. specifier in the format. DOTIME, when non-nil, indicates that a
  17208. time-of-day should be extracted from TXT for sorting of this entry, and for
  17209. the `%t' specifier in the format. When DOTIME is a string, this string is
  17210. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  17211. only the correctly processes TXT should be returned - this is used by
  17212. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  17213. (save-match-data
  17214. ;; Diary entries sometimes have extra whitespace at the beginning
  17215. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  17216. (let* ((category (or category
  17217. org-category
  17218. (if buffer-file-name
  17219. (file-name-sans-extension
  17220. (file-name-nondirectory buffer-file-name))
  17221. "")))
  17222. (tag (if tags (nth (1- (length tags)) tags) ""))
  17223. time ; time and tag are needed for the eval of the prefix format
  17224. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  17225. (time-of-day (and dotime (org-get-time-of-day ts)))
  17226. stamp plain s0 s1 s2 rtn srp)
  17227. (when (and dotime time-of-day org-prefix-has-time)
  17228. ;; Extract starting and ending time and move them to prefix
  17229. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  17230. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  17231. (setq s0 (match-string 0 ts)
  17232. srp (and stamp (match-end 3))
  17233. s1 (match-string (if plain 1 2) ts)
  17234. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  17235. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  17236. ;; them, we might want to remove them there to avoid duplication.
  17237. ;; The user can turn this off with a variable.
  17238. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  17239. (string-match (concat (regexp-quote s0) " *") txt)
  17240. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  17241. (= (match-beginning 0) 0)
  17242. t))
  17243. (setq txt (replace-match "" nil nil txt))))
  17244. ;; Normalize the time(s) to 24 hour
  17245. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  17246. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  17247. (when (and s1 (not s2) org-agenda-default-appointment-duration
  17248. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  17249. (let ((m (+ (string-to-number (match-string 2 s1))
  17250. (* 60 (string-to-number (match-string 1 s1)))
  17251. org-agenda-default-appointment-duration))
  17252. h)
  17253. (setq h (/ m 60) m (- m (* h 60)))
  17254. (setq s2 (format "%02d:%02d" h m))))
  17255. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  17256. txt)
  17257. ;; Tags are in the string
  17258. (if (or (eq org-agenda-remove-tags t)
  17259. (and org-agenda-remove-tags
  17260. org-prefix-has-tag))
  17261. (setq txt (replace-match "" t t txt))
  17262. (setq txt (replace-match
  17263. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  17264. (match-string 2 txt))
  17265. t t txt))))
  17266. ;; Create the final string
  17267. (if noprefix
  17268. (setq rtn txt)
  17269. ;; Prepare the variables needed in the eval of the compiled format
  17270. (setq time (cond (s2 (concat s1 "-" s2))
  17271. (s1 (concat s1 "......"))
  17272. (t ""))
  17273. extra (or extra "")
  17274. category (if (symbolp category) (symbol-name category) category))
  17275. ;; Evaluate the compiled format
  17276. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  17277. ;; And finally add the text properties
  17278. (org-add-props rtn nil
  17279. 'org-category (downcase category) 'tags tags
  17280. 'prefix-length (- (length rtn) (length txt))
  17281. 'time-of-day time-of-day
  17282. 'txt txt
  17283. 'time time
  17284. 'extra extra
  17285. 'dotime dotime))))
  17286. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  17287. (defvar org-agenda-sorting-strategy-selected nil)
  17288. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  17289. (catch 'exit
  17290. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  17291. ((and todayp (member 'today (car org-agenda-time-grid))))
  17292. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  17293. ((member 'weekly (car org-agenda-time-grid)))
  17294. (t (throw 'exit list)))
  17295. (let* ((have (delq nil (mapcar
  17296. (lambda (x) (get-text-property 1 'time-of-day x))
  17297. list)))
  17298. (string (nth 1 org-agenda-time-grid))
  17299. (gridtimes (nth 2 org-agenda-time-grid))
  17300. (req (car org-agenda-time-grid))
  17301. (remove (member 'remove-match req))
  17302. new time)
  17303. (if (and (member 'require-timed req) (not have))
  17304. ;; don't show empty grid
  17305. (throw 'exit list))
  17306. (while (setq time (pop gridtimes))
  17307. (unless (and remove (member time have))
  17308. (setq time (int-to-string time))
  17309. (push (org-format-agenda-item
  17310. nil string "" nil
  17311. (concat (substring time 0 -2) ":" (substring time -2)))
  17312. new)
  17313. (put-text-property
  17314. 1 (length (car new)) 'face 'org-time-grid (car new))))
  17315. (if (member 'time-up org-agenda-sorting-strategy-selected)
  17316. (append new list)
  17317. (append list new)))))
  17318. (defun org-compile-prefix-format (key)
  17319. "Compile the prefix format into a Lisp form that can be evaluated.
  17320. The resulting form is returned and stored in the variable
  17321. `org-prefix-format-compiled'."
  17322. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  17323. (let ((s (cond
  17324. ((stringp org-agenda-prefix-format)
  17325. org-agenda-prefix-format)
  17326. ((assq key org-agenda-prefix-format)
  17327. (cdr (assq key org-agenda-prefix-format)))
  17328. (t " %-12:c%?-12t% s")))
  17329. (start 0)
  17330. varform vars var e c f opt)
  17331. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  17332. s start)
  17333. (setq var (cdr (assoc (match-string 4 s)
  17334. '(("c" . category) ("t" . time) ("s" . extra)
  17335. ("T" . tag))))
  17336. c (or (match-string 3 s) "")
  17337. opt (match-beginning 1)
  17338. start (1+ (match-beginning 0)))
  17339. (if (equal var 'time) (setq org-prefix-has-time t))
  17340. (if (equal var 'tag) (setq org-prefix-has-tag t))
  17341. (setq f (concat "%" (match-string 2 s) "s"))
  17342. (if opt
  17343. (setq varform
  17344. `(if (equal "" ,var)
  17345. ""
  17346. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  17347. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  17348. (setq s (replace-match "%s" t nil s))
  17349. (push varform vars))
  17350. (setq vars (nreverse vars))
  17351. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  17352. (defun org-set-sorting-strategy (key)
  17353. (if (symbolp (car org-agenda-sorting-strategy))
  17354. ;; the old format
  17355. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  17356. (setq org-agenda-sorting-strategy-selected
  17357. (or (cdr (assq key org-agenda-sorting-strategy))
  17358. (cdr (assq 'agenda org-agenda-sorting-strategy))
  17359. '(time-up category-keep priority-down)))))
  17360. (defun org-get-time-of-day (s &optional string mod24)
  17361. "Check string S for a time of day.
  17362. If found, return it as a military time number between 0 and 2400.
  17363. If not found, return nil.
  17364. The optional STRING argument forces conversion into a 5 character wide string
  17365. HH:MM."
  17366. (save-match-data
  17367. (when
  17368. (or
  17369. (string-match
  17370. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  17371. (string-match
  17372. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  17373. (let* ((h (string-to-number (match-string 1 s)))
  17374. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  17375. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  17376. (am-p (equal ampm "am"))
  17377. (h1 (cond ((not ampm) h)
  17378. ((= h 12) (if am-p 0 12))
  17379. (t (+ h (if am-p 0 12)))))
  17380. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  17381. (mod h1 24) h1))
  17382. (t0 (+ (* 100 h2) m))
  17383. (t1 (concat (if (>= h1 24) "+" " ")
  17384. (if (< t0 100) "0" "")
  17385. (if (< t0 10) "0" "")
  17386. (int-to-string t0))))
  17387. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  17388. (defun org-finalize-agenda-entries (list &optional nosort)
  17389. "Sort and concatenate the agenda items."
  17390. (setq list (mapcar 'org-agenda-highlight-todo list))
  17391. (if nosort
  17392. list
  17393. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  17394. (defun org-agenda-highlight-todo (x)
  17395. (let (re pl)
  17396. (if (eq x 'line)
  17397. (save-excursion
  17398. (beginning-of-line 1)
  17399. (setq re (get-text-property (point) 'org-todo-regexp))
  17400. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  17401. (and (looking-at (concat "[ \t]*\\.*" re))
  17402. (add-text-properties (match-beginning 0) (match-end 0)
  17403. (list 'face (org-get-todo-face 0)))))
  17404. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  17405. pl (get-text-property 0 'prefix-length x))
  17406. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  17407. (add-text-properties
  17408. (or (match-end 1) (match-end 0)) (match-end 0)
  17409. (list 'face (org-get-todo-face (match-string 2 x)))
  17410. x))
  17411. x)))
  17412. (defsubst org-cmp-priority (a b)
  17413. "Compare the priorities of string A and B."
  17414. (let ((pa (or (get-text-property 1 'priority a) 0))
  17415. (pb (or (get-text-property 1 'priority b) 0)))
  17416. (cond ((> pa pb) +1)
  17417. ((< pa pb) -1)
  17418. (t nil))))
  17419. (defsubst org-cmp-category (a b)
  17420. "Compare the string values of categories of strings A and B."
  17421. (let ((ca (or (get-text-property 1 'org-category a) ""))
  17422. (cb (or (get-text-property 1 'org-category b) "")))
  17423. (cond ((string-lessp ca cb) -1)
  17424. ((string-lessp cb ca) +1)
  17425. (t nil))))
  17426. (defsubst org-cmp-tag (a b)
  17427. "Compare the string values of categories of strings A and B."
  17428. (let ((ta (car (last (get-text-property 1 'tags a))))
  17429. (tb (car (last (get-text-property 1 'tags b)))))
  17430. (cond ((not ta) +1)
  17431. ((not tb) -1)
  17432. ((string-lessp ta tb) -1)
  17433. ((string-lessp tb ta) +1)
  17434. (t nil))))
  17435. (defsubst org-cmp-time (a b)
  17436. "Compare the time-of-day values of strings A and B."
  17437. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  17438. (ta (or (get-text-property 1 'time-of-day a) def))
  17439. (tb (or (get-text-property 1 'time-of-day b) def)))
  17440. (cond ((< ta tb) -1)
  17441. ((< tb ta) +1)
  17442. (t nil))))
  17443. (defun org-entries-lessp (a b)
  17444. "Predicate for sorting agenda entries."
  17445. ;; The following variables will be used when the form is evaluated.
  17446. ;; So even though the compiler complains, keep them.
  17447. (let* ((time-up (org-cmp-time a b))
  17448. (time-down (if time-up (- time-up) nil))
  17449. (priority-up (org-cmp-priority a b))
  17450. (priority-down (if priority-up (- priority-up) nil))
  17451. (category-up (org-cmp-category a b))
  17452. (category-down (if category-up (- category-up) nil))
  17453. (category-keep (if category-up +1 nil))
  17454. (tag-up (org-cmp-tag a b))
  17455. (tag-down (if tag-up (- tag-up) nil)))
  17456. (cdr (assoc
  17457. (eval (cons 'or org-agenda-sorting-strategy-selected))
  17458. '((-1 . t) (1 . nil) (nil . nil))))))
  17459. ;;; Agenda commands
  17460. (defun org-agenda-check-type (error &rest types)
  17461. "Check if agenda buffer is of allowed type.
  17462. If ERROR is non-nil, throw an error, otherwise just return nil."
  17463. (if (memq org-agenda-type types)
  17464. t
  17465. (if error
  17466. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  17467. nil)))
  17468. (defun org-agenda-quit ()
  17469. "Exit agenda by removing the window or the buffer."
  17470. (interactive)
  17471. (let ((buf (current-buffer)))
  17472. (if (not (one-window-p)) (delete-window))
  17473. (kill-buffer buf)
  17474. (org-agenda-maybe-reset-markers 'force)
  17475. (org-columns-remove-overlays))
  17476. ;; Maybe restore the pre-agenda window configuration.
  17477. (and org-agenda-restore-windows-after-quit
  17478. (not (eq org-agenda-window-setup 'other-frame))
  17479. org-pre-agenda-window-conf
  17480. (set-window-configuration org-pre-agenda-window-conf)))
  17481. (defun org-agenda-exit ()
  17482. "Exit agenda by removing the window or the buffer.
  17483. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  17484. Org-mode buffers visited directly by the user will not be touched."
  17485. (interactive)
  17486. (org-release-buffers org-agenda-new-buffers)
  17487. (setq org-agenda-new-buffers nil)
  17488. (org-agenda-quit))
  17489. (defun org-save-all-org-buffers ()
  17490. "Save all Org-mode buffers without user confirmation."
  17491. (interactive)
  17492. (message "Saving all Org-mode buffers...")
  17493. (save-some-buffers t 'org-mode-p)
  17494. (message "Saving all Org-mode buffers... done"))
  17495. (defun org-agenda-redo ()
  17496. "Rebuild Agenda.
  17497. When this is the global TODO list, a prefix argument will be interpreted."
  17498. (interactive)
  17499. (let* ((org-agenda-keep-modes t)
  17500. (line (org-current-line))
  17501. (window-line (- line (org-current-line (window-start))))
  17502. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  17503. (message "Rebuilding agenda buffer...")
  17504. (org-let lprops '(eval org-agenda-redo-command))
  17505. (setq org-agenda-undo-list nil
  17506. org-agenda-pending-undo-list nil)
  17507. (message "Rebuilding agenda buffer...done")
  17508. (goto-line line)
  17509. (recenter window-line)))
  17510. (defun org-agenda-goto-date (date)
  17511. "Jump to DATE in agenda."
  17512. (interactive (list (org-read-date)))
  17513. (org-agenda-list nil date))
  17514. (defun org-agenda-goto-today ()
  17515. "Go to today."
  17516. (interactive)
  17517. (org-agenda-check-type t 'timeline 'agenda)
  17518. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  17519. (cond
  17520. (tdpos (goto-char tdpos))
  17521. ((eq org-agenda-type 'agenda)
  17522. (let* ((sd (time-to-days (current-time)))
  17523. (comp (org-agenda-compute-time-span sd org-agenda-span))
  17524. (org-agenda-overriding-arguments org-agenda-last-arguments))
  17525. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  17526. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  17527. (org-agenda-redo)
  17528. (org-agenda-find-today-or-agenda)))
  17529. (t (error "Cannot find today")))))
  17530. (defun org-agenda-find-today-or-agenda ()
  17531. (goto-char
  17532. (or (text-property-any (point-min) (point-max) 'org-today t)
  17533. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  17534. (point-min))))
  17535. (defun org-agenda-later (arg)
  17536. "Go forward in time by thee current span.
  17537. With prefix ARG, go forward that many times the current span."
  17538. (interactive "p")
  17539. (org-agenda-check-type t 'agenda)
  17540. (let* ((span org-agenda-span)
  17541. (sd org-starting-day)
  17542. (greg (calendar-gregorian-from-absolute sd))
  17543. greg2 nd)
  17544. (cond
  17545. ((eq span 'day)
  17546. (setq sd (+ arg sd) nd 1))
  17547. ((eq span 'week)
  17548. (setq sd (+ (* 7 arg) sd) nd 7))
  17549. ((eq span 'month)
  17550. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  17551. sd (calendar-absolute-from-gregorian greg2))
  17552. (setcar greg2 (1+ (car greg2)))
  17553. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  17554. ((eq span 'year)
  17555. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  17556. sd (calendar-absolute-from-gregorian greg2))
  17557. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  17558. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  17559. (let ((org-agenda-overriding-arguments
  17560. (list (car org-agenda-last-arguments) sd nd t)))
  17561. (org-agenda-redo)
  17562. (org-agenda-find-today-or-agenda))))
  17563. (defun org-agenda-earlier (arg)
  17564. "Go backward in time by the current span.
  17565. With prefix ARG, go backward that many times the current span."
  17566. (interactive "p")
  17567. (org-agenda-later (- arg)))
  17568. (defun org-agenda-day-view ()
  17569. "Switch to daily view for agenda."
  17570. (interactive)
  17571. (setq org-agenda-ndays 1)
  17572. (org-agenda-change-time-span 'day))
  17573. (defun org-agenda-week-view ()
  17574. "Switch to daily view for agenda."
  17575. (interactive)
  17576. (setq org-agenda-ndays 7)
  17577. (org-agenda-change-time-span 'week))
  17578. (defun org-agenda-month-view ()
  17579. "Switch to daily view for agenda."
  17580. (interactive)
  17581. (org-agenda-change-time-span 'month))
  17582. (defun org-agenda-year-view ()
  17583. "Switch to daily view for agenda."
  17584. (interactive)
  17585. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  17586. (org-agenda-change-time-span 'year)
  17587. (error "Abort")))
  17588. (defun org-agenda-change-time-span (span)
  17589. "Change the agenda view to SPAN.
  17590. SPAN may be `day', `week', `month', `year'."
  17591. (org-agenda-check-type t 'agenda)
  17592. (if (equal org-agenda-span span)
  17593. (error "Viewing span is already \"%s\"" span))
  17594. (let* ((sd (or (get-text-property (point) 'day)
  17595. org-starting-day))
  17596. (computed (org-agenda-compute-time-span sd span))
  17597. (org-agenda-overriding-arguments
  17598. (list (car org-agenda-last-arguments)
  17599. (car computed) (cdr computed) t)))
  17600. (org-agenda-redo)
  17601. (org-agenda-find-today-or-agenda))
  17602. (org-agenda-set-mode-name)
  17603. (message "Switched to %s view" span))
  17604. (defun org-agenda-compute-time-span (sd span)
  17605. "Compute starting date and number of days for agenda.
  17606. SPAN may be `day', `week', `month', `year'. The return value
  17607. is a cons cell with the starting date and the number of days,
  17608. so that the date SD will be in that range."
  17609. (let* ((greg (calendar-gregorian-from-absolute sd))
  17610. nd)
  17611. (cond
  17612. ((eq span 'day)
  17613. (setq nd 1))
  17614. ((eq span 'week)
  17615. (let* ((nt (calendar-day-of-week
  17616. (calendar-gregorian-from-absolute sd)))
  17617. (d (if org-agenda-start-on-weekday
  17618. (- nt org-agenda-start-on-weekday)
  17619. 0)))
  17620. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  17621. (setq nd 7)))
  17622. ((eq span 'month)
  17623. (setq sd (calendar-absolute-from-gregorian
  17624. (list (car greg) 1 (nth 2 greg)))
  17625. nd (- (calendar-absolute-from-gregorian
  17626. (list (1+ (car greg)) 1 (nth 2 greg)))
  17627. sd)))
  17628. ((eq span 'year)
  17629. (setq sd (calendar-absolute-from-gregorian
  17630. (list 1 1 (nth 2 greg)))
  17631. nd (- (calendar-absolute-from-gregorian
  17632. (list 1 1 (1+ (nth 2 greg))))
  17633. sd))))
  17634. (cons sd nd)))
  17635. ;; FIXME: this no longer works if user make date format that starts with a blank
  17636. (defun org-agenda-next-date-line (&optional arg)
  17637. "Jump to the next line indicating a date in agenda buffer."
  17638. (interactive "p")
  17639. (org-agenda-check-type t 'agenda 'timeline)
  17640. (beginning-of-line 1)
  17641. (if (looking-at "^\\S-") (forward-char 1))
  17642. (if (not (re-search-forward "^\\S-" nil t arg))
  17643. (progn
  17644. (backward-char 1)
  17645. (error "No next date after this line in this buffer")))
  17646. (goto-char (match-beginning 0)))
  17647. (defun org-agenda-previous-date-line (&optional arg)
  17648. "Jump to the previous line indicating a date in agenda buffer."
  17649. (interactive "p")
  17650. (org-agenda-check-type t 'agenda 'timeline)
  17651. (beginning-of-line 1)
  17652. (if (not (re-search-backward "^\\S-" nil t arg))
  17653. (error "No previous date before this line in this buffer")))
  17654. ;; Initialize the highlight
  17655. (defvar org-hl (org-make-overlay 1 1))
  17656. (org-overlay-put org-hl 'face 'highlight)
  17657. (defun org-highlight (begin end &optional buffer)
  17658. "Highlight a region with overlay."
  17659. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  17660. org-hl begin end (or buffer (current-buffer))))
  17661. (defun org-unhighlight ()
  17662. "Detach overlay INDEX."
  17663. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  17664. ;; FIXME this is currently not used.
  17665. (defun org-highlight-until-next-command (beg end &optional buffer)
  17666. (org-highlight beg end buffer)
  17667. (add-hook 'pre-command-hook 'org-unhighlight-once))
  17668. (defun org-unhighlight-once ()
  17669. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  17670. (org-unhighlight))
  17671. (defun org-agenda-follow-mode ()
  17672. "Toggle follow mode in an agenda buffer."
  17673. (interactive)
  17674. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  17675. (org-agenda-set-mode-name)
  17676. (message "Follow mode is %s"
  17677. (if org-agenda-follow-mode "on" "off")))
  17678. (defun org-agenda-log-mode ()
  17679. "Toggle log mode in an agenda buffer."
  17680. (interactive)
  17681. (org-agenda-check-type t 'agenda 'timeline)
  17682. (setq org-agenda-show-log (not org-agenda-show-log))
  17683. (org-agenda-set-mode-name)
  17684. (org-agenda-redo)
  17685. (message "Log mode is %s"
  17686. (if org-agenda-show-log "on" "off")))
  17687. (defun org-agenda-toggle-diary ()
  17688. "Toggle diary inclusion in an agenda buffer."
  17689. (interactive)
  17690. (org-agenda-check-type t 'agenda)
  17691. (setq org-agenda-include-diary (not org-agenda-include-diary))
  17692. (org-agenda-redo)
  17693. (org-agenda-set-mode-name)
  17694. (message "Diary inclusion turned %s"
  17695. (if org-agenda-include-diary "on" "off")))
  17696. (defun org-agenda-toggle-time-grid ()
  17697. "Toggle time grid in an agenda buffer."
  17698. (interactive)
  17699. (org-agenda-check-type t 'agenda)
  17700. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  17701. (org-agenda-redo)
  17702. (org-agenda-set-mode-name)
  17703. (message "Time-grid turned %s"
  17704. (if org-agenda-use-time-grid "on" "off")))
  17705. (defun org-agenda-set-mode-name ()
  17706. "Set the mode name to indicate all the small mode settings."
  17707. (setq mode-name
  17708. (concat "Org-Agenda"
  17709. (if (equal org-agenda-ndays 1) " Day" "")
  17710. (if (equal org-agenda-ndays 7) " Week" "")
  17711. (if org-agenda-follow-mode " Follow" "")
  17712. (if org-agenda-include-diary " Diary" "")
  17713. (if org-agenda-use-time-grid " Grid" "")
  17714. (if org-agenda-show-log " Log" "")))
  17715. (force-mode-line-update))
  17716. (defun org-agenda-post-command-hook ()
  17717. (and (eolp) (not (bolp)) (backward-char 1))
  17718. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  17719. (if (and org-agenda-follow-mode
  17720. (get-text-property (point) 'org-marker))
  17721. (org-agenda-show)))
  17722. (defun org-agenda-show-priority ()
  17723. "Show the priority of the current item.
  17724. This priority is composed of the main priority given with the [#A] cookies,
  17725. and by additional input from the age of a schedules or deadline entry."
  17726. (interactive)
  17727. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  17728. (message "Priority is %d" (if pri pri -1000))))
  17729. (defun org-agenda-show-tags ()
  17730. "Show the tags applicable to the current item."
  17731. (interactive)
  17732. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  17733. (if tags
  17734. (message "Tags are :%s:"
  17735. (org-no-properties (mapconcat 'identity tags ":")))
  17736. (message "No tags associated with this line"))))
  17737. (defun org-agenda-goto (&optional highlight)
  17738. "Go to the Org-mode file which contains the item at point."
  17739. (interactive)
  17740. (let* ((marker (or (get-text-property (point) 'org-marker)
  17741. (org-agenda-error)))
  17742. (buffer (marker-buffer marker))
  17743. (pos (marker-position marker)))
  17744. (switch-to-buffer-other-window buffer)
  17745. (widen)
  17746. (goto-char pos)
  17747. (when (org-mode-p)
  17748. (org-show-context 'agenda)
  17749. (save-excursion
  17750. (and (outline-next-heading)
  17751. (org-flag-heading nil)))) ; show the next heading
  17752. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  17753. (defun org-agenda-kill ()
  17754. "Kill the entry or subtree belonging to the current agenda entry."
  17755. (interactive)
  17756. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17757. (let* ((marker (or (get-text-property (point) 'org-marker)
  17758. (org-agenda-error)))
  17759. (buffer (marker-buffer marker))
  17760. (pos (marker-position marker))
  17761. (type (get-text-property (point) 'type))
  17762. dbeg dend (n 0) conf)
  17763. (org-with-remote-undo buffer
  17764. (with-current-buffer buffer
  17765. (save-excursion
  17766. (goto-char pos)
  17767. (if (and (org-mode-p) (not (member type '("sexp"))))
  17768. (setq dbeg (progn (org-back-to-heading t) (point))
  17769. dend (org-end-of-subtree t t))
  17770. (setq dbeg (point-at-bol)
  17771. dend (min (point-max) (1+ (point-at-eol)))))
  17772. (goto-char dbeg)
  17773. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  17774. (setq conf (or (eq t org-agenda-confirm-kill)
  17775. (and (numberp org-agenda-confirm-kill)
  17776. (> n org-agenda-confirm-kill))))
  17777. (and conf
  17778. (not (y-or-n-p
  17779. (format "Delete entry with %d lines in buffer \"%s\"? "
  17780. n (buffer-name buffer))))
  17781. (error "Abort"))
  17782. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  17783. (with-current-buffer buffer (delete-region dbeg dend))
  17784. (message "Agenda item and source killed"))))
  17785. (defun org-agenda-archive ()
  17786. "Kill the entry or subtree belonging to the current agenda entry."
  17787. (interactive)
  17788. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17789. (let* ((marker (or (get-text-property (point) 'org-marker)
  17790. (org-agenda-error)))
  17791. (buffer (marker-buffer marker))
  17792. (pos (marker-position marker)))
  17793. (org-with-remote-undo buffer
  17794. (with-current-buffer buffer
  17795. (if (org-mode-p)
  17796. (save-excursion
  17797. (goto-char pos)
  17798. (org-remove-subtree-entries-from-agenda)
  17799. (org-back-to-heading t)
  17800. (org-archive-subtree))
  17801. (error "Archiving works only in Org-mode files"))))))
  17802. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  17803. "Remove all lines in the agenda that correspond to a given subtree.
  17804. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  17805. If this information is not given, the function uses the tree at point."
  17806. (let ((buf (or buf (current-buffer))) m p)
  17807. (save-excursion
  17808. (unless (and beg end)
  17809. (org-back-to-heading t)
  17810. (setq beg (point))
  17811. (org-end-of-subtree t)
  17812. (setq end (point)))
  17813. (set-buffer (get-buffer org-agenda-buffer-name))
  17814. (save-excursion
  17815. (goto-char (point-max))
  17816. (beginning-of-line 1)
  17817. (while (not (bobp))
  17818. (when (and (setq m (get-text-property (point) 'org-marker))
  17819. (equal buf (marker-buffer m))
  17820. (setq p (marker-position m))
  17821. (>= p beg)
  17822. (<= p end))
  17823. (let ((inhibit-read-only t))
  17824. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  17825. (beginning-of-line 0))))))
  17826. (defun org-agenda-open-link ()
  17827. "Follow the link in the current line, if any."
  17828. (interactive)
  17829. (let ((eol (point-at-eol)))
  17830. (save-excursion
  17831. (if (or (re-search-forward org-bracket-link-regexp eol t)
  17832. (re-search-forward org-angle-link-re eol t)
  17833. (re-search-forward org-plain-link-re eol t))
  17834. (call-interactively 'org-open-at-point)
  17835. (error "No link in current line")))))
  17836. (defun org-agenda-switch-to (&optional delete-other-windows)
  17837. "Go to the Org-mode file which contains the item at point."
  17838. (interactive)
  17839. (let* ((marker (or (get-text-property (point) 'org-marker)
  17840. (org-agenda-error)))
  17841. (buffer (marker-buffer marker))
  17842. (pos (marker-position marker)))
  17843. (switch-to-buffer buffer)
  17844. (and delete-other-windows (delete-other-windows))
  17845. (widen)
  17846. (goto-char pos)
  17847. (when (org-mode-p)
  17848. (org-show-context 'agenda)
  17849. (save-excursion
  17850. (and (outline-next-heading)
  17851. (org-flag-heading nil)))))) ; show the next heading
  17852. (defun org-agenda-goto-mouse (ev)
  17853. "Go to the Org-mode file which contains the item at the mouse click."
  17854. (interactive "e")
  17855. (mouse-set-point ev)
  17856. (org-agenda-goto))
  17857. (defun org-agenda-show ()
  17858. "Display the Org-mode file which contains the item at point."
  17859. (interactive)
  17860. (let ((win (selected-window)))
  17861. (org-agenda-goto t)
  17862. (select-window win)))
  17863. (defun org-agenda-recenter (arg)
  17864. "Display the Org-mode file which contains the item at point and recenter."
  17865. (interactive "P")
  17866. (let ((win (selected-window)))
  17867. (org-agenda-goto t)
  17868. (recenter arg)
  17869. (select-window win)))
  17870. (defun org-agenda-show-mouse (ev)
  17871. "Display the Org-mode file which contains the item at the mouse click."
  17872. (interactive "e")
  17873. (mouse-set-point ev)
  17874. (org-agenda-show))
  17875. (defun org-agenda-check-no-diary ()
  17876. "Check if the entry is a diary link and abort if yes."
  17877. (if (get-text-property (point) 'org-agenda-diary-link)
  17878. (org-agenda-error)))
  17879. (defun org-agenda-error ()
  17880. (error "Command not allowed in this line"))
  17881. (defun org-agenda-tree-to-indirect-buffer ()
  17882. "Show the subtree corresponding to the current entry in an indirect buffer.
  17883. This calls the command `org-tree-to-indirect-buffer' from the original
  17884. Org-mode buffer.
  17885. With numerical prefix arg ARG, go up to this level and then take that tree.
  17886. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  17887. dedicated frame)."
  17888. (interactive)
  17889. (org-agenda-check-no-diary)
  17890. (let* ((marker (or (get-text-property (point) 'org-marker)
  17891. (org-agenda-error)))
  17892. (buffer (marker-buffer marker))
  17893. (pos (marker-position marker)))
  17894. (with-current-buffer buffer
  17895. (save-excursion
  17896. (goto-char pos)
  17897. (call-interactively 'org-tree-to-indirect-buffer)))))
  17898. (defvar org-last-heading-marker (make-marker)
  17899. "Marker pointing to the headline that last changed its TODO state
  17900. by a remote command from the agenda.")
  17901. (defun org-agenda-todo-nextset ()
  17902. "Switch TODO entry to next sequence."
  17903. (interactive)
  17904. (org-agenda-todo 'nextset))
  17905. (defun org-agenda-todo-previousset ()
  17906. "Switch TODO entry to previous sequence."
  17907. (interactive)
  17908. (org-agenda-todo 'previousset))
  17909. (defun org-agenda-todo (&optional arg)
  17910. "Cycle TODO state of line at point, also in Org-mode file.
  17911. This changes the line at point, all other lines in the agenda referring to
  17912. the same tree node, and the headline of the tree node in the Org-mode file."
  17913. (interactive "P")
  17914. (org-agenda-check-no-diary)
  17915. (let* ((col (current-column))
  17916. (marker (or (get-text-property (point) 'org-marker)
  17917. (org-agenda-error)))
  17918. (buffer (marker-buffer marker))
  17919. (pos (marker-position marker))
  17920. (hdmarker (get-text-property (point) 'org-hd-marker))
  17921. (inhibit-read-only t)
  17922. newhead)
  17923. (org-with-remote-undo buffer
  17924. (with-current-buffer buffer
  17925. (widen)
  17926. (goto-char pos)
  17927. (org-show-context 'agenda)
  17928. (save-excursion
  17929. (and (outline-next-heading)
  17930. (org-flag-heading nil))) ; show the next heading
  17931. (org-todo arg)
  17932. (and (bolp) (forward-char 1))
  17933. (setq newhead (org-get-heading))
  17934. (save-excursion
  17935. (org-back-to-heading)
  17936. (move-marker org-last-heading-marker (point))))
  17937. (beginning-of-line 1)
  17938. (save-excursion
  17939. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  17940. (move-to-column col))))
  17941. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  17942. "Change all lines in the agenda buffer which match HDMARKER.
  17943. The new content of the line will be NEWHEAD (as modified by
  17944. `org-format-agenda-item'). HDMARKER is checked with
  17945. `equal' against all `org-hd-marker' text properties in the file.
  17946. If FIXFACE is non-nil, the face of each item is modified acording to
  17947. the new TODO state."
  17948. (let* ((inhibit-read-only t)
  17949. props m pl undone-face done-face finish new dotime cat tags)
  17950. (save-excursion
  17951. (goto-char (point-max))
  17952. (beginning-of-line 1)
  17953. (while (not finish)
  17954. (setq finish (bobp))
  17955. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  17956. (equal m hdmarker))
  17957. (setq props (text-properties-at (point))
  17958. dotime (get-text-property (point) 'dotime)
  17959. cat (get-text-property (point) 'org-category)
  17960. tags (get-text-property (point) 'tags)
  17961. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  17962. pl (get-text-property (point) 'prefix-length)
  17963. undone-face (get-text-property (point) 'undone-face)
  17964. done-face (get-text-property (point) 'done-face))
  17965. (move-to-column pl)
  17966. (cond
  17967. ((equal new "")
  17968. (beginning-of-line 1)
  17969. (and (looking-at ".*\n?") (replace-match "")))
  17970. ((looking-at ".*")
  17971. (replace-match new t t)
  17972. (beginning-of-line 1)
  17973. (add-text-properties (point-at-bol) (point-at-eol) props)
  17974. (when fixface
  17975. (add-text-properties
  17976. (point-at-bol) (point-at-eol)
  17977. (list 'face
  17978. (if org-last-todo-state-is-todo
  17979. undone-face done-face))))
  17980. (org-agenda-highlight-todo 'line)
  17981. (beginning-of-line 1))
  17982. (t (error "Line update did not work"))))
  17983. (beginning-of-line 0)))
  17984. (org-finalize-agenda)))
  17985. ;; FIXME: allow negative value for org-agenda-align-tags-to-column
  17986. ;; See the code in set-tags for the way to do this.
  17987. (defun org-agenda-align-tags (&optional line)
  17988. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  17989. (let ((inhibit-read-only t))
  17990. (save-excursion
  17991. (goto-char (if line (point-at-bol) (point-min)))
  17992. (while (re-search-forward (org-re "\\([ \t]+\\):[[:alnum:]_@:]+:[ \t]*$")
  17993. (if line (point-at-eol) nil) t)
  17994. (delete-region (match-beginning 1) (match-end 1))
  17995. (goto-char (match-beginning 1))
  17996. (insert (org-add-props
  17997. (make-string (max 1 (- org-agenda-align-tags-to-column
  17998. (current-column))) ?\ )
  17999. (text-properties-at (point))))))))
  18000. (defun org-agenda-priority-up ()
  18001. "Increase the priority of line at point, also in Org-mode file."
  18002. (interactive)
  18003. (org-agenda-priority 'up))
  18004. (defun org-agenda-priority-down ()
  18005. "Decrease the priority of line at point, also in Org-mode file."
  18006. (interactive)
  18007. (org-agenda-priority 'down))
  18008. (defun org-agenda-priority (&optional force-direction)
  18009. "Set the priority of line at point, also in Org-mode file.
  18010. This changes the line at point, all other lines in the agenda referring to
  18011. the same tree node, and the headline of the tree node in the Org-mode file."
  18012. (interactive)
  18013. (org-agenda-check-no-diary)
  18014. (let* ((marker (or (get-text-property (point) 'org-marker)
  18015. (org-agenda-error)))
  18016. (hdmarker (get-text-property (point) 'org-hd-marker))
  18017. (buffer (marker-buffer hdmarker))
  18018. (pos (marker-position hdmarker))
  18019. (inhibit-read-only t)
  18020. newhead)
  18021. (org-with-remote-undo buffer
  18022. (with-current-buffer buffer
  18023. (widen)
  18024. (goto-char pos)
  18025. (org-show-context 'agenda)
  18026. (save-excursion
  18027. (and (outline-next-heading)
  18028. (org-flag-heading nil))) ; show the next heading
  18029. (funcall 'org-priority force-direction)
  18030. (end-of-line 1)
  18031. (setq newhead (org-get-heading)))
  18032. (org-agenda-change-all-lines newhead hdmarker)
  18033. (beginning-of-line 1))))
  18034. (defun org-get-tags-at (&optional pos)
  18035. "Get a list of all headline tags applicable at POS.
  18036. POS defaults to point. If tags are inherited, the list contains
  18037. the targets in the same sequence as the headlines appear, i.e.
  18038. the tags of the current headline come last."
  18039. (interactive)
  18040. (let (tags lastpos)
  18041. (save-excursion
  18042. (save-restriction
  18043. (widen)
  18044. (goto-char (or pos (point)))
  18045. (save-match-data
  18046. (org-back-to-heading t)
  18047. (condition-case nil
  18048. (while (not (equal lastpos (point)))
  18049. (setq lastpos (point))
  18050. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  18051. (setq tags (append (org-split-string
  18052. (org-match-string-no-properties 1) ":")
  18053. tags)))
  18054. (or org-use-tag-inheritance (error ""))
  18055. (org-up-heading-all 1))
  18056. (error nil))))
  18057. tags)))
  18058. ;; FIXME: should fix the tags property of the agenda line.
  18059. (defun org-agenda-set-tags ()
  18060. "Set tags for the current headline."
  18061. (interactive)
  18062. (org-agenda-check-no-diary)
  18063. (if (and (org-region-active-p) (interactive-p))
  18064. (call-interactively 'org-change-tag-in-region)
  18065. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  18066. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  18067. (org-agenda-error)))
  18068. (buffer (marker-buffer hdmarker))
  18069. (pos (marker-position hdmarker))
  18070. (inhibit-read-only t)
  18071. newhead)
  18072. (org-with-remote-undo buffer
  18073. (with-current-buffer buffer
  18074. (widen)
  18075. (goto-char pos)
  18076. (save-excursion
  18077. (org-show-context 'agenda))
  18078. (save-excursion
  18079. (and (outline-next-heading)
  18080. (org-flag-heading nil))) ; show the next heading
  18081. (goto-char pos)
  18082. (call-interactively 'org-set-tags)
  18083. (end-of-line 1)
  18084. (setq newhead (org-get-heading)))
  18085. (org-agenda-change-all-lines newhead hdmarker)
  18086. (beginning-of-line 1)))))
  18087. (defun org-agenda-toggle-archive-tag ()
  18088. "Toggle the archive tag for the current entry."
  18089. (interactive)
  18090. (org-agenda-check-no-diary)
  18091. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  18092. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  18093. (org-agenda-error)))
  18094. (buffer (marker-buffer hdmarker))
  18095. (pos (marker-position hdmarker))
  18096. (inhibit-read-only t)
  18097. newhead)
  18098. (org-with-remote-undo buffer
  18099. (with-current-buffer buffer
  18100. (widen)
  18101. (goto-char pos)
  18102. (org-show-context 'agenda)
  18103. (save-excursion
  18104. (and (outline-next-heading)
  18105. (org-flag-heading nil))) ; show the next heading
  18106. (call-interactively 'org-toggle-archive-tag)
  18107. (end-of-line 1)
  18108. (setq newhead (org-get-heading)))
  18109. (org-agenda-change-all-lines newhead hdmarker)
  18110. (beginning-of-line 1))))
  18111. (defun org-agenda-date-later (arg &optional what)
  18112. "Change the date of this item to one day later."
  18113. (interactive "p")
  18114. (org-agenda-check-type t 'agenda 'timeline)
  18115. (org-agenda-check-no-diary)
  18116. (let* ((marker (or (get-text-property (point) 'org-marker)
  18117. (org-agenda-error)))
  18118. (buffer (marker-buffer marker))
  18119. (pos (marker-position marker)))
  18120. (org-with-remote-undo buffer
  18121. (with-current-buffer buffer
  18122. (widen)
  18123. (goto-char pos)
  18124. (if (not (org-at-timestamp-p))
  18125. (error "Cannot find time stamp"))
  18126. (org-timestamp-change arg (or what 'day)))
  18127. (org-agenda-show-new-time marker org-last-changed-timestamp))
  18128. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  18129. (defun org-agenda-date-earlier (arg &optional what)
  18130. "Change the date of this item to one day earlier."
  18131. (interactive "p")
  18132. (org-agenda-date-later (- arg) what))
  18133. (defun org-agenda-show-new-time (marker stamp)
  18134. "Show new date stamp via text properties."
  18135. ;; We use text properties to make this undoable
  18136. (let ((inhibit-read-only t))
  18137. (setq stamp (concat " => " stamp))
  18138. (save-excursion
  18139. (goto-char (point-max))
  18140. (while (not (bobp))
  18141. (when (equal marker (get-text-property (point) 'org-marker))
  18142. (move-to-column (- (window-width) (length stamp)) t)
  18143. (if (featurep 'xemacs)
  18144. ;; Use `duplicable' property to trigger undo recording
  18145. (let ((ex (make-extent nil nil))
  18146. (gl (make-glyph stamp)))
  18147. (set-glyph-face gl 'secondary-selection)
  18148. (set-extent-properties
  18149. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  18150. (insert-extent ex (1- (point)) (point-at-eol)))
  18151. (add-text-properties
  18152. (1- (point)) (point-at-eol)
  18153. (list 'display (org-add-props stamp nil
  18154. 'face 'secondary-selection))))
  18155. (beginning-of-line 1))
  18156. (beginning-of-line 0)))))
  18157. (defun org-agenda-date-prompt (arg)
  18158. "Change the date of this item. Date is prompted for, with default today.
  18159. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  18160. be used to request time specification in the time stamp."
  18161. (interactive "P")
  18162. (org-agenda-check-type t 'agenda 'timeline)
  18163. (org-agenda-check-no-diary)
  18164. (let* ((marker (or (get-text-property (point) 'org-marker)
  18165. (org-agenda-error)))
  18166. (buffer (marker-buffer marker))
  18167. (pos (marker-position marker)))
  18168. (org-with-remote-undo buffer
  18169. (with-current-buffer buffer
  18170. (widen)
  18171. (goto-char pos)
  18172. (if (not (org-at-timestamp-p))
  18173. (error "Cannot find time stamp"))
  18174. (org-time-stamp arg)
  18175. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  18176. (defun org-agenda-schedule (arg)
  18177. "Schedule the item at point."
  18178. (interactive "P")
  18179. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  18180. (org-agenda-check-no-diary)
  18181. (let* ((marker (or (get-text-property (point) 'org-marker)
  18182. (org-agenda-error)))
  18183. (buffer (marker-buffer marker))
  18184. (pos (marker-position marker))
  18185. (org-insert-labeled-timestamps-at-point nil)
  18186. ts)
  18187. (org-with-remote-undo buffer
  18188. (with-current-buffer buffer
  18189. (widen)
  18190. (goto-char pos)
  18191. (setq ts (org-schedule))
  18192. (message "Item scheduled for %s" ts)))))
  18193. (defun org-agenda-deadline (arg)
  18194. "Schedule the item at point."
  18195. (interactive "P")
  18196. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  18197. (org-agenda-check-no-diary)
  18198. (let* ((marker (or (get-text-property (point) 'org-marker)
  18199. (org-agenda-error)))
  18200. (buffer (marker-buffer marker))
  18201. (pos (marker-position marker))
  18202. (org-insert-labeled-timestamps-at-point nil)
  18203. ts)
  18204. (org-with-remote-undo buffer
  18205. (with-current-buffer buffer
  18206. (widen)
  18207. (goto-char pos)
  18208. (setq ts (org-deadline))
  18209. (message "Deadline for this item set to %s" ts)))))
  18210. (defun org-get-heading ()
  18211. "Return the heading of the current entry, without the stars."
  18212. (save-excursion
  18213. (org-back-to-heading t)
  18214. (if (looking-at "\\*+[ \t]+\\([^\r\n]*\\)") (match-string 1) "")))
  18215. (defun org-agenda-clock-in (&optional arg)
  18216. "Start the clock on the currently selected item."
  18217. (interactive "P")
  18218. (org-agenda-check-no-diary)
  18219. (let* ((marker (or (get-text-property (point) 'org-marker)
  18220. (org-agenda-error)))
  18221. (pos (marker-position marker)))
  18222. (org-with-remote-undo (marker-buffer marker)
  18223. (with-current-buffer (marker-buffer marker)
  18224. (widen)
  18225. (goto-char pos)
  18226. (org-clock-in)))))
  18227. (defun org-agenda-clock-out (&optional arg)
  18228. "Stop the currently running clock."
  18229. (interactive "P")
  18230. (unless (marker-buffer org-clock-marker)
  18231. (error "No running clock"))
  18232. (org-with-remote-undo (marker-buffer org-clock-marker)
  18233. (org-clock-out)))
  18234. (defun org-agenda-clock-cancel (&optional arg)
  18235. "Cancel the currently running clock."
  18236. (interactive "P")
  18237. (unless (marker-buffer org-clock-marker)
  18238. (error "No running clock"))
  18239. (org-with-remote-undo (marker-buffer org-clock-marker)
  18240. (org-clock-cancel)))
  18241. (defun org-agenda-diary-entry ()
  18242. "Make a diary entry, like the `i' command from the calendar.
  18243. All the standard commands work: block, weekly etc."
  18244. (interactive)
  18245. (org-agenda-check-type t 'agenda 'timeline)
  18246. (require 'diary-lib)
  18247. (let* ((char (progn
  18248. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  18249. (read-char-exclusive)))
  18250. (cmd (cdr (assoc char
  18251. '((?d . insert-diary-entry)
  18252. (?w . insert-weekly-diary-entry)
  18253. (?m . insert-monthly-diary-entry)
  18254. (?y . insert-yearly-diary-entry)
  18255. (?a . insert-anniversary-diary-entry)
  18256. (?b . insert-block-diary-entry)
  18257. (?c . insert-cyclic-diary-entry)))))
  18258. (oldf (symbol-function 'calendar-cursor-to-date))
  18259. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  18260. (point (point))
  18261. (mark (or (mark t) (point))))
  18262. (unless cmd
  18263. (error "No command associated with <%c>" char))
  18264. (unless (and (get-text-property point 'day)
  18265. (or (not (equal ?b char))
  18266. (get-text-property mark 'day)))
  18267. (error "Don't know which date to use for diary entry"))
  18268. ;; We implement this by hacking the `calendar-cursor-to-date' function
  18269. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  18270. (let ((calendar-mark-ring
  18271. (list (calendar-gregorian-from-absolute
  18272. (or (get-text-property mark 'day)
  18273. (get-text-property point 'day))))))
  18274. (unwind-protect
  18275. (progn
  18276. (fset 'calendar-cursor-to-date
  18277. (lambda (&optional error)
  18278. (calendar-gregorian-from-absolute
  18279. (get-text-property point 'day))))
  18280. (call-interactively cmd))
  18281. (fset 'calendar-cursor-to-date oldf)))))
  18282. (defun org-agenda-execute-calendar-command (cmd)
  18283. "Execute a calendar command from the agenda, with the date associated to
  18284. the cursor position."
  18285. (org-agenda-check-type t 'agenda 'timeline)
  18286. (require 'diary-lib)
  18287. (unless (get-text-property (point) 'day)
  18288. (error "Don't know which date to use for calendar command"))
  18289. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  18290. (point (point))
  18291. (date (calendar-gregorian-from-absolute
  18292. (get-text-property point 'day)))
  18293. ;; the following 3 vars are needed in the calendar
  18294. (displayed-day (extract-calendar-day date))
  18295. (displayed-month (extract-calendar-month date))
  18296. (displayed-year (extract-calendar-year date)))
  18297. (unwind-protect
  18298. (progn
  18299. (fset 'calendar-cursor-to-date
  18300. (lambda (&optional error)
  18301. (calendar-gregorian-from-absolute
  18302. (get-text-property point 'day))))
  18303. (call-interactively cmd))
  18304. (fset 'calendar-cursor-to-date oldf))))
  18305. (defun org-agenda-phases-of-moon ()
  18306. "Display the phases of the moon for the 3 months around the cursor date."
  18307. (interactive)
  18308. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  18309. (defun org-agenda-holidays ()
  18310. "Display the holidays for the 3 months around the cursor date."
  18311. (interactive)
  18312. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  18313. (defun org-agenda-sunrise-sunset (arg)
  18314. "Display sunrise and sunset for the cursor date.
  18315. Latitude and longitude can be specified with the variables
  18316. `calendar-latitude' and `calendar-longitude'. When called with prefix
  18317. argument, latitude and longitude will be prompted for."
  18318. (interactive "P")
  18319. (let ((calendar-longitude (if arg nil calendar-longitude))
  18320. (calendar-latitude (if arg nil calendar-latitude))
  18321. (calendar-location-name
  18322. (if arg "the given coordinates" calendar-location-name)))
  18323. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  18324. (defun org-agenda-goto-calendar ()
  18325. "Open the Emacs calendar with the date at the cursor."
  18326. (interactive)
  18327. (org-agenda-check-type t 'agenda 'timeline)
  18328. (let* ((day (or (get-text-property (point) 'day)
  18329. (error "Don't know which date to open in calendar")))
  18330. (date (calendar-gregorian-from-absolute day))
  18331. (calendar-move-hook nil)
  18332. (view-calendar-holidays-initially nil)
  18333. (view-diary-entries-initially nil))
  18334. (calendar)
  18335. (calendar-goto-date date)))
  18336. (defun org-calendar-goto-agenda ()
  18337. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  18338. This is a command that has to be installed in `calendar-mode-map'."
  18339. (interactive)
  18340. (org-agenda-list nil (calendar-absolute-from-gregorian
  18341. (calendar-cursor-to-date))
  18342. nil))
  18343. (defun org-agenda-convert-date ()
  18344. (interactive)
  18345. (org-agenda-check-type t 'agenda 'timeline)
  18346. (let ((day (get-text-property (point) 'day))
  18347. date s)
  18348. (unless day
  18349. (error "Don't know which date to convert"))
  18350. (setq date (calendar-gregorian-from-absolute day))
  18351. (setq s (concat
  18352. "Gregorian: " (calendar-date-string date) "\n"
  18353. "ISO: " (calendar-iso-date-string date) "\n"
  18354. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  18355. "Julian: " (calendar-julian-date-string date) "\n"
  18356. "Astron. JD: " (calendar-astro-date-string date)
  18357. " (Julian date number at noon UTC)\n"
  18358. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  18359. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  18360. "French: " (calendar-french-date-string date) "\n"
  18361. "Bahai: " (calendar-bahai-date-string date) " (until sunset)\n"
  18362. "Mayan: " (calendar-mayan-date-string date) "\n"
  18363. "Coptic: " (calendar-coptic-date-string date) "\n"
  18364. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  18365. "Persian: " (calendar-persian-date-string date) "\n"
  18366. "Chinese: " (calendar-chinese-date-string date) "\n"))
  18367. (with-output-to-temp-buffer "*Dates*"
  18368. (princ s))
  18369. (if (fboundp 'fit-window-to-buffer)
  18370. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  18371. ;;;; Embedded LaTeX
  18372. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  18373. "Keymap for the minor `org-cdlatex-mode'.")
  18374. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  18375. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  18376. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  18377. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  18378. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  18379. (defvar org-cdlatex-texmathp-advice-is-done nil
  18380. "Flag remembering if we have applied the advice to texmathp already.")
  18381. (define-minor-mode org-cdlatex-mode
  18382. "Toggle the minor `org-cdlatex-mode'.
  18383. This mode supports entering LaTeX environment and math in LaTeX fragments
  18384. in Org-mode.
  18385. \\{org-cdlatex-mode-map}"
  18386. nil " OCDL" nil
  18387. (when org-cdlatex-mode (require 'cdlatex))
  18388. (unless org-cdlatex-texmathp-advice-is-done
  18389. (setq org-cdlatex-texmathp-advice-is-done t)
  18390. (defadvice texmathp (around org-math-always-on activate)
  18391. "Always return t in org-mode buffers.
  18392. This is because we want to insert math symbols without dollars even outside
  18393. the LaTeX math segments. If Orgmode thinks that point is actually inside
  18394. en embedded LaTeX fragement, let texmathp do its job.
  18395. \\[org-cdlatex-mode-map]"
  18396. (interactive)
  18397. (let (p)
  18398. (cond
  18399. ((not (org-mode-p)) ad-do-it)
  18400. ((eq this-command 'cdlatex-math-symbol)
  18401. (setq ad-return-value t
  18402. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  18403. (t
  18404. (let ((p (org-inside-LaTeX-fragment-p)))
  18405. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  18406. (setq ad-return-value t
  18407. texmathp-why '("Org-mode embedded math" . 0))
  18408. (if p ad-do-it)))))))))
  18409. (defun turn-on-org-cdlatex ()
  18410. "Unconditionally turn on `org-cdlatex-mode'."
  18411. (org-cdlatex-mode 1))
  18412. (defun org-inside-LaTeX-fragment-p ()
  18413. "Test if point is inside a LaTeX fragment.
  18414. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  18415. sequence appearing also before point.
  18416. Even though the matchers for math are configurable, this function assumes
  18417. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  18418. delimiters are skipped when they have been removed by customization.
  18419. The return value is nil, or a cons cell with the delimiter and
  18420. and the position of this delimiter.
  18421. This function does a reasonably good job, but can locally be fooled by
  18422. for example currency specifications. For example it will assume being in
  18423. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  18424. fragments that are properly closed, but during editing, we have to live
  18425. with the uncertainty caused by missing closing delimiters. This function
  18426. looks only before point, not after."
  18427. (catch 'exit
  18428. (let ((pos (point))
  18429. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  18430. (lim (progn
  18431. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  18432. (point)))
  18433. dd-on str (start 0) m re)
  18434. (goto-char pos)
  18435. (when dodollar
  18436. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  18437. re (nth 1 (assoc "$" org-latex-regexps)))
  18438. (while (string-match re str start)
  18439. (cond
  18440. ((= (match-end 0) (length str))
  18441. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  18442. ((= (match-end 0) (- (length str) 5))
  18443. (throw 'exit nil))
  18444. (t (setq start (match-end 0))))))
  18445. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  18446. (goto-char pos)
  18447. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  18448. (and (match-beginning 2) (throw 'exit nil))
  18449. ;; count $$
  18450. (while (re-search-backward "\\$\\$" lim t)
  18451. (setq dd-on (not dd-on)))
  18452. (goto-char pos)
  18453. (if dd-on (cons "$$" m))))))
  18454. (defun org-try-cdlatex-tab ()
  18455. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  18456. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  18457. - inside a LaTeX fragment, or
  18458. - after the first word in a line, where an abbreviation expansion could
  18459. insert a LaTeX environment."
  18460. (when org-cdlatex-mode
  18461. (cond
  18462. ((save-excursion
  18463. (skip-chars-backward "a-zA-Z0-9*")
  18464. (skip-chars-backward " \t")
  18465. (bolp))
  18466. (cdlatex-tab) t)
  18467. ((org-inside-LaTeX-fragment-p)
  18468. (cdlatex-tab) t)
  18469. (t nil))))
  18470. (defun org-cdlatex-underscore-caret (&optional arg)
  18471. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  18472. Revert to the normal definition outside of these fragments."
  18473. (interactive "P")
  18474. (if (org-inside-LaTeX-fragment-p)
  18475. (call-interactively 'cdlatex-sub-superscript)
  18476. (let (org-cdlatex-mode)
  18477. (call-interactively (key-binding (vector last-input-event))))))
  18478. (defun org-cdlatex-math-modify (&optional arg)
  18479. "Execute `cdlatex-math-modify' in LaTeX fragments.
  18480. Revert to the normal definition outside of these fragments."
  18481. (interactive "P")
  18482. (if (org-inside-LaTeX-fragment-p)
  18483. (call-interactively 'cdlatex-math-modify)
  18484. (let (org-cdlatex-mode)
  18485. (call-interactively (key-binding (vector last-input-event))))))
  18486. (defvar org-latex-fragment-image-overlays nil
  18487. "List of overlays carrying the images of latex fragments.")
  18488. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  18489. (defun org-remove-latex-fragment-image-overlays ()
  18490. "Remove all overlays with LaTeX fragment images in current buffer."
  18491. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  18492. (setq org-latex-fragment-image-overlays nil))
  18493. (defun org-preview-latex-fragment (&optional subtree)
  18494. "Preview the LaTeX fragment at point, or all locally or globally.
  18495. If the cursor is in a LaTeX fragment, create the image and overlay
  18496. it over the source code. If there is no fragment at point, display
  18497. all fragments in the current text, from one headline to the next. With
  18498. prefix SUBTREE, display all fragments in the current subtree. With a
  18499. double prefix `C-u C-u', or when the cursor is before the first headline,
  18500. display all fragments in the buffer.
  18501. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  18502. (interactive "P")
  18503. (org-remove-latex-fragment-image-overlays)
  18504. (save-excursion
  18505. (save-restriction
  18506. (let (beg end at msg)
  18507. (cond
  18508. ((or (equal subtree '(16))
  18509. (not (save-excursion
  18510. (re-search-backward (concat "^" outline-regexp) nil t))))
  18511. (setq beg (point-min) end (point-max)
  18512. msg "Creating images for buffer...%s"))
  18513. ((equal subtree '(4))
  18514. (org-back-to-heading)
  18515. (setq beg (point) end (org-end-of-subtree t)
  18516. msg "Creating images for subtree...%s"))
  18517. (t
  18518. (if (setq at (org-inside-LaTeX-fragment-p))
  18519. (goto-char (max (point-min) (- (cdr at) 2)))
  18520. (org-back-to-heading))
  18521. (setq beg (point) end (progn (outline-next-heading) (point))
  18522. msg (if at "Creating image...%s"
  18523. "Creating images for entry...%s"))))
  18524. (message msg "")
  18525. (narrow-to-region beg end)
  18526. (goto-char beg)
  18527. (org-format-latex
  18528. (concat "ltxpng/" (file-name-sans-extension
  18529. (file-name-nondirectory
  18530. buffer-file-name)))
  18531. default-directory 'overlays msg at 'forbuffer)
  18532. (message msg "done. Use `C-c C-c' to remove images.")))))
  18533. (defvar org-latex-regexps
  18534. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  18535. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  18536. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  18537. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  18538. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  18539. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  18540. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  18541. "Regular expressions for matching embedded LaTeX.")
  18542. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  18543. "Replace LaTeX fragments with links to an image, and produce images."
  18544. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  18545. (let* ((prefixnodir (file-name-nondirectory prefix))
  18546. (absprefix (expand-file-name prefix dir))
  18547. (todir (file-name-directory absprefix))
  18548. (opt org-format-latex-options)
  18549. (matchers (plist-get opt :matchers))
  18550. (re-list org-latex-regexps)
  18551. (cnt 0) txt link beg end re e checkdir
  18552. m n block linkfile movefile ov)
  18553. ;; Check if there are old images files with this prefix, and remove them
  18554. (when (file-directory-p todir)
  18555. (mapc 'delete-file
  18556. (directory-files
  18557. todir 'full
  18558. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  18559. ;; Check the different regular expressions
  18560. (while (setq e (pop re-list))
  18561. (setq m (car e) re (nth 1 e) n (nth 2 e)
  18562. block (if (nth 3 e) "\n\n" ""))
  18563. (when (member m matchers)
  18564. (goto-char (point-min))
  18565. (while (re-search-forward re nil t)
  18566. (when (or (not at) (equal (cdr at) (match-beginning n)))
  18567. (setq txt (match-string n)
  18568. beg (match-beginning n) end (match-end n)
  18569. cnt (1+ cnt)
  18570. linkfile (format "%s_%04d.png" prefix cnt)
  18571. movefile (format "%s_%04d.png" absprefix cnt)
  18572. link (concat block "[[file:" linkfile "]]" block))
  18573. (if msg (message msg cnt))
  18574. (goto-char beg)
  18575. (unless checkdir ; make sure the directory exists
  18576. (setq checkdir t)
  18577. (or (file-directory-p todir) (make-directory todir)))
  18578. (org-create-formula-image
  18579. txt movefile opt forbuffer)
  18580. (if overlays
  18581. (progn
  18582. (setq ov (org-make-overlay beg end))
  18583. (if (featurep 'xemacs)
  18584. (progn
  18585. (org-overlay-put ov 'invisible t)
  18586. (org-overlay-put
  18587. ov 'end-glyph
  18588. (make-glyph (vector 'png :file movefile))))
  18589. (org-overlay-put
  18590. ov 'display
  18591. (list 'image :type 'png :file movefile :ascent 'center)))
  18592. (push ov org-latex-fragment-image-overlays)
  18593. (goto-char end))
  18594. (delete-region beg end)
  18595. (insert link))))))))
  18596. ;; This function borrows from Ganesh Swami's latex2png.el
  18597. (defun org-create-formula-image (string tofile options buffer)
  18598. (let* ((tmpdir (if (featurep 'xemacs)
  18599. (temp-directory)
  18600. temporary-file-directory))
  18601. (texfilebase (make-temp-name
  18602. (expand-file-name "orgtex" tmpdir)))
  18603. (texfile (concat texfilebase ".tex"))
  18604. (dvifile (concat texfilebase ".dvi"))
  18605. (pngfile (concat texfilebase ".png"))
  18606. (fnh (face-attribute 'default :height nil))
  18607. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  18608. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  18609. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  18610. "Black"))
  18611. (bg (or (plist-get options (if buffer :background :html-background))
  18612. "Transparent")))
  18613. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  18614. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  18615. (with-temp-file texfile
  18616. (insert org-format-latex-header
  18617. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  18618. (let ((dir default-directory))
  18619. (condition-case nil
  18620. (progn
  18621. (cd tmpdir)
  18622. (call-process "latex" nil nil nil texfile))
  18623. (error nil))
  18624. (cd dir))
  18625. (if (not (file-exists-p dvifile))
  18626. (progn (message "Failed to create dvi file from %s" texfile) nil)
  18627. (call-process "dvipng" nil nil nil
  18628. "-E" "-fg" fg "-bg" bg
  18629. "-D" dpi
  18630. ;;"-x" scale "-y" scale
  18631. "-T" "tight"
  18632. "-o" pngfile
  18633. dvifile)
  18634. (if (not (file-exists-p pngfile))
  18635. (progn (message "Failed to create png file from %s" texfile) nil)
  18636. ;; Use the requested file name and clean up
  18637. (copy-file pngfile tofile 'replace)
  18638. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  18639. (delete-file (concat texfilebase e)))
  18640. pngfile))))
  18641. (defun org-dvipng-color (attr)
  18642. "Return an rgb color specification for dvipng."
  18643. (apply 'format "rgb %s %s %s"
  18644. (mapcar 'org-normalize-color
  18645. (color-values (face-attribute 'default attr nil)))))
  18646. (defun org-normalize-color (value)
  18647. "Return string to be used as color value for an RGB component."
  18648. (format "%g" (/ value 65535.0)))
  18649. ;;;; Exporting
  18650. ;;; Variables, constants, and parameter plists
  18651. (defconst org-level-max 20)
  18652. (defvar org-export-html-preamble nil
  18653. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  18654. (defvar org-export-html-postamble nil
  18655. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  18656. (defvar org-export-html-auto-preamble t
  18657. "Should default preamble be inserted? Set by publishing functions.")
  18658. (defvar org-export-html-auto-postamble t
  18659. "Should default postamble be inserted? Set by publishing functions.")
  18660. (defvar org-current-export-file nil) ; dynamically scoped parameter
  18661. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  18662. (defconst org-export-plist-vars
  18663. '((:language . org-export-default-language)
  18664. (:customtime . org-display-custom-times)
  18665. (:headline-levels . org-export-headline-levels)
  18666. (:section-numbers . org-export-with-section-numbers)
  18667. (:table-of-contents . org-export-with-toc)
  18668. (:preserve-breaks . org-export-preserve-breaks)
  18669. (:archived-trees . org-export-with-archived-trees)
  18670. (:emphasize . org-export-with-emphasize)
  18671. (:sub-superscript . org-export-with-sub-superscripts)
  18672. (:footnotes . org-export-with-footnotes)
  18673. (:property-drawer . org-export-with-property-drawer)
  18674. (:TeX-macros . org-export-with-TeX-macros)
  18675. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  18676. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  18677. (:fixed-width . org-export-with-fixed-width)
  18678. (:timestamps . org-export-with-timestamps)
  18679. (:author-info . org-export-author-info)
  18680. (:time-stamp-file . org-export-time-stamp-file)
  18681. (:tables . org-export-with-tables)
  18682. (:table-auto-headline . org-export-highlight-first-table-line)
  18683. (:style . org-export-html-style)
  18684. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  18685. (:convert-org-links . org-export-html-link-org-files-as-html)
  18686. (:inline-images . org-export-html-inline-images)
  18687. (:expand-quoted-html . org-export-html-expand)
  18688. (:timestamp . org-export-html-with-timestamp)
  18689. (:publishing-directory . org-export-publishing-directory)
  18690. (:preamble . org-export-html-preamble)
  18691. (:postamble . org-export-html-postamble)
  18692. (:auto-preamble . org-export-html-auto-preamble)
  18693. (:auto-postamble . org-export-html-auto-postamble)
  18694. (:author . user-full-name)
  18695. (:email . user-mail-address)))
  18696. (defun org-default-export-plist ()
  18697. "Return the property list with default settings for the export variables."
  18698. (let ((l org-export-plist-vars) rtn e)
  18699. (while (setq e (pop l))
  18700. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  18701. rtn))
  18702. (defun org-infile-export-plist ()
  18703. "Return the property list with file-local settings for export."
  18704. (save-excursion
  18705. (goto-char 0)
  18706. (let ((re (org-make-options-regexp
  18707. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  18708. p key val text options)
  18709. (while (re-search-forward re nil t)
  18710. (setq key (org-match-string-no-properties 1)
  18711. val (org-match-string-no-properties 2))
  18712. (cond
  18713. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  18714. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  18715. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  18716. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  18717. ((string-equal key "TEXT")
  18718. (setq text (if text (concat text "\n" val) val)))
  18719. ((string-equal key "OPTIONS") (setq options val))))
  18720. (setq p (plist-put p :text text))
  18721. (when options
  18722. (let ((op '(("H" . :headline-levels)
  18723. ("num" . :section-numbers)
  18724. ("toc" . :table-of-contents)
  18725. ("\\n" . :preserve-breaks)
  18726. ("@" . :expand-quoted-html)
  18727. (":" . :fixed-width)
  18728. ("|" . :tables)
  18729. ("^" . :sub-superscript)
  18730. ("f" . :footnotes)
  18731. ("p" . :property-drawer)
  18732. ("*" . :emphasize)
  18733. ("TeX" . :TeX-macros)
  18734. ("LaTeX" . :LaTeX-fragments)
  18735. ("skip" . :skip-before-1st-heading)
  18736. ("author" . :author-info)
  18737. ("timestamp" . :time-stamp-file)))
  18738. o)
  18739. (while (setq o (pop op))
  18740. (if (string-match (concat (regexp-quote (car o))
  18741. ":\\([^ \t\n\r;,.]*\\)")
  18742. options)
  18743. (setq p (plist-put p (cdr o)
  18744. (car (read-from-string
  18745. (match-string 1 options)))))))))
  18746. p)))
  18747. (defun org-export-directory (type plist)
  18748. (let* ((val (plist-get plist :publishing-directory))
  18749. (dir (if (listp val)
  18750. (or (cdr (assoc type val)) ".")
  18751. val)))
  18752. dir))
  18753. (defun org-skip-comments (lines)
  18754. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  18755. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  18756. (re2 "^\\(\\*+\\)[ \t\n\r]")
  18757. (case-fold-search nil)
  18758. rtn line level)
  18759. (while (setq line (pop lines))
  18760. (cond
  18761. ((and (string-match re1 line)
  18762. (setq level (- (match-end 1) (match-beginning 1))))
  18763. ;; Beginning of a COMMENT subtree. Skip it.
  18764. (while (and (setq line (pop lines))
  18765. (or (not (string-match re2 line))
  18766. (> (- (match-end 1) (match-beginning 1)) level))))
  18767. (setq lines (cons line lines)))
  18768. ((string-match "^#" line)
  18769. ;; an ordinary comment line
  18770. )
  18771. ((and org-export-table-remove-special-lines
  18772. (string-match "^[ \t]*|" line)
  18773. (or (string-match "^[ \t]*| *[!_^] *|" line)
  18774. (and (string-match "| *<[0-9]+> *|" line)
  18775. (not (string-match "| *[^ <|]" line)))))
  18776. ;; a special table line that should be removed
  18777. )
  18778. (t (setq rtn (cons line rtn)))))
  18779. (nreverse rtn)))
  18780. (defun org-export (&optional arg)
  18781. (interactive)
  18782. (let ((help "[t] insert the export option template
  18783. \[v] limit export to visible part of outline tree
  18784. \[a] export as ASCII
  18785. \[h] export as HTML
  18786. \[H] export as HTML to temporary buffer
  18787. \[R] export region as HTML
  18788. \[b] export as HTML and browse immediately
  18789. \[x] export as XOXO
  18790. \[l] export as LaTeX
  18791. \[L] export as LaTeX to temporary buffer
  18792. \[i] export current file as iCalendar file
  18793. \[I] export all agenda files as iCalendar files
  18794. \[c] export agenda files into combined iCalendar file
  18795. \[F] publish current file
  18796. \[P] publish current project
  18797. \[X] publish... (project will be prompted for)
  18798. \[A] publish all projects")
  18799. (cmds
  18800. '((?t . org-insert-export-options-template)
  18801. (?v . org-export-visible)
  18802. (?a . org-export-as-ascii)
  18803. (?h . org-export-as-html)
  18804. (?b . org-export-as-html-and-open)
  18805. (?H . org-export-as-html-to-buffer)
  18806. (?R . org-export-region-as-html)
  18807. (?x . org-export-as-xoxo)
  18808. (?l . org-export-as-latex)
  18809. (?L . org-export-as-latex-to-buffer)
  18810. (?i . org-export-icalendar-this-file)
  18811. (?I . org-export-icalendar-all-agenda-files)
  18812. (?c . org-export-icalendar-combine-agenda-files)
  18813. (?F . org-publish-current-file)
  18814. (?P . org-publish-current-project)
  18815. (?X . org-publish)
  18816. (?A . org-publish-all)))
  18817. r1 r2 ass)
  18818. (save-window-excursion
  18819. (delete-other-windows)
  18820. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  18821. (princ help))
  18822. (message "Select command: ")
  18823. (setq r1 (read-char-exclusive)))
  18824. (setq r2 (if (< r1 27) (+ r1 96) r1))
  18825. (if (setq ass (assq r2 cmds))
  18826. (call-interactively (cdr ass))
  18827. (error "No command associated with key %c" r1))))
  18828. (defconst org-html-entities
  18829. '(("nbsp")
  18830. ("iexcl")
  18831. ("cent")
  18832. ("pound")
  18833. ("curren")
  18834. ("yen")
  18835. ("brvbar")
  18836. ("vert" . "&#124;")
  18837. ("sect")
  18838. ("uml")
  18839. ("copy")
  18840. ("ordf")
  18841. ("laquo")
  18842. ("not")
  18843. ("shy")
  18844. ("reg")
  18845. ("macr")
  18846. ("deg")
  18847. ("plusmn")
  18848. ("sup2")
  18849. ("sup3")
  18850. ("acute")
  18851. ("micro")
  18852. ("para")
  18853. ("middot")
  18854. ("odot"."o")
  18855. ("star"."*")
  18856. ("cedil")
  18857. ("sup1")
  18858. ("ordm")
  18859. ("raquo")
  18860. ("frac14")
  18861. ("frac12")
  18862. ("frac34")
  18863. ("iquest")
  18864. ("Agrave")
  18865. ("Aacute")
  18866. ("Acirc")
  18867. ("Atilde")
  18868. ("Auml")
  18869. ("Aring") ("AA"."&Aring;")
  18870. ("AElig")
  18871. ("Ccedil")
  18872. ("Egrave")
  18873. ("Eacute")
  18874. ("Ecirc")
  18875. ("Euml")
  18876. ("Igrave")
  18877. ("Iacute")
  18878. ("Icirc")
  18879. ("Iuml")
  18880. ("ETH")
  18881. ("Ntilde")
  18882. ("Ograve")
  18883. ("Oacute")
  18884. ("Ocirc")
  18885. ("Otilde")
  18886. ("Ouml")
  18887. ("times")
  18888. ("Oslash")
  18889. ("Ugrave")
  18890. ("Uacute")
  18891. ("Ucirc")
  18892. ("Uuml")
  18893. ("Yacute")
  18894. ("THORN")
  18895. ("szlig")
  18896. ("agrave")
  18897. ("aacute")
  18898. ("acirc")
  18899. ("atilde")
  18900. ("auml")
  18901. ("aring")
  18902. ("aelig")
  18903. ("ccedil")
  18904. ("egrave")
  18905. ("eacute")
  18906. ("ecirc")
  18907. ("euml")
  18908. ("igrave")
  18909. ("iacute")
  18910. ("icirc")
  18911. ("iuml")
  18912. ("eth")
  18913. ("ntilde")
  18914. ("ograve")
  18915. ("oacute")
  18916. ("ocirc")
  18917. ("otilde")
  18918. ("ouml")
  18919. ("divide")
  18920. ("oslash")
  18921. ("ugrave")
  18922. ("uacute")
  18923. ("ucirc")
  18924. ("uuml")
  18925. ("yacute")
  18926. ("thorn")
  18927. ("yuml")
  18928. ("fnof")
  18929. ("Alpha")
  18930. ("Beta")
  18931. ("Gamma")
  18932. ("Delta")
  18933. ("Epsilon")
  18934. ("Zeta")
  18935. ("Eta")
  18936. ("Theta")
  18937. ("Iota")
  18938. ("Kappa")
  18939. ("Lambda")
  18940. ("Mu")
  18941. ("Nu")
  18942. ("Xi")
  18943. ("Omicron")
  18944. ("Pi")
  18945. ("Rho")
  18946. ("Sigma")
  18947. ("Tau")
  18948. ("Upsilon")
  18949. ("Phi")
  18950. ("Chi")
  18951. ("Psi")
  18952. ("Omega")
  18953. ("alpha")
  18954. ("beta")
  18955. ("gamma")
  18956. ("delta")
  18957. ("epsilon")
  18958. ("varepsilon"."&epsilon;")
  18959. ("zeta")
  18960. ("eta")
  18961. ("theta")
  18962. ("iota")
  18963. ("kappa")
  18964. ("lambda")
  18965. ("mu")
  18966. ("nu")
  18967. ("xi")
  18968. ("omicron")
  18969. ("pi")
  18970. ("rho")
  18971. ("sigmaf") ("varsigma"."&sigmaf;")
  18972. ("sigma")
  18973. ("tau")
  18974. ("upsilon")
  18975. ("phi")
  18976. ("chi")
  18977. ("psi")
  18978. ("omega")
  18979. ("thetasym") ("vartheta"."&thetasym;")
  18980. ("upsih")
  18981. ("piv")
  18982. ("bull") ("bullet"."&bull;")
  18983. ("hellip") ("dots"."&hellip;")
  18984. ("prime")
  18985. ("Prime")
  18986. ("oline")
  18987. ("frasl")
  18988. ("weierp")
  18989. ("image")
  18990. ("real")
  18991. ("trade")
  18992. ("alefsym")
  18993. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  18994. ("uarr") ("uparrow"."&uarr;")
  18995. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  18996. ("darr")("downarrow"."&darr;")
  18997. ("harr") ("leftrightarrow"."&harr;")
  18998. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  18999. ("lArr") ("Leftarrow"."&lArr;")
  19000. ("uArr") ("Uparrow"."&uArr;")
  19001. ("rArr") ("Rightarrow"."&rArr;")
  19002. ("dArr") ("Downarrow"."&dArr;")
  19003. ("hArr") ("Leftrightarrow"."&hArr;")
  19004. ("forall")
  19005. ("part") ("partial"."&part;")
  19006. ("exist") ("exists"."&exist;")
  19007. ("empty") ("emptyset"."&empty;")
  19008. ("nabla")
  19009. ("isin") ("in"."&isin;")
  19010. ("notin")
  19011. ("ni")
  19012. ("prod")
  19013. ("sum")
  19014. ("minus")
  19015. ("lowast") ("ast"."&lowast;")
  19016. ("radic")
  19017. ("prop") ("proptp"."&prop;")
  19018. ("infin") ("infty"."&infin;")
  19019. ("ang") ("angle"."&ang;")
  19020. ("and") ("vee"."&and;")
  19021. ("or") ("wedge"."&or;")
  19022. ("cap")
  19023. ("cup")
  19024. ("int")
  19025. ("there4")
  19026. ("sim")
  19027. ("cong") ("simeq"."&cong;")
  19028. ("asymp")("approx"."&asymp;")
  19029. ("ne") ("neq"."&ne;")
  19030. ("equiv")
  19031. ("le")
  19032. ("ge")
  19033. ("sub") ("subset"."&sub;")
  19034. ("sup") ("supset"."&sup;")
  19035. ("nsub")
  19036. ("sube")
  19037. ("supe")
  19038. ("oplus")
  19039. ("otimes")
  19040. ("perp")
  19041. ("sdot") ("cdot"."&sdot;")
  19042. ("lceil")
  19043. ("rceil")
  19044. ("lfloor")
  19045. ("rfloor")
  19046. ("lang")
  19047. ("rang")
  19048. ("loz") ("Diamond"."&loz;")
  19049. ("spades") ("spadesuit"."&spades;")
  19050. ("clubs") ("clubsuit"."&clubs;")
  19051. ("hearts") ("diamondsuit"."&hearts;")
  19052. ("diams") ("diamondsuit"."&diams;")
  19053. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  19054. ("quot")
  19055. ("amp")
  19056. ("lt")
  19057. ("gt")
  19058. ("OElig")
  19059. ("oelig")
  19060. ("Scaron")
  19061. ("scaron")
  19062. ("Yuml")
  19063. ("circ")
  19064. ("tilde")
  19065. ("ensp")
  19066. ("emsp")
  19067. ("thinsp")
  19068. ("zwnj")
  19069. ("zwj")
  19070. ("lrm")
  19071. ("rlm")
  19072. ("ndash")
  19073. ("mdash")
  19074. ("lsquo")
  19075. ("rsquo")
  19076. ("sbquo")
  19077. ("ldquo")
  19078. ("rdquo")
  19079. ("bdquo")
  19080. ("dagger")
  19081. ("Dagger")
  19082. ("permil")
  19083. ("lsaquo")
  19084. ("rsaquo")
  19085. ("euro")
  19086. ("arccos"."arccos")
  19087. ("arcsin"."arcsin")
  19088. ("arctan"."arctan")
  19089. ("arg"."arg")
  19090. ("cos"."cos")
  19091. ("cosh"."cosh")
  19092. ("cot"."cot")
  19093. ("coth"."coth")
  19094. ("csc"."csc")
  19095. ("deg"."deg")
  19096. ("det"."det")
  19097. ("dim"."dim")
  19098. ("exp"."exp")
  19099. ("gcd"."gcd")
  19100. ("hom"."hom")
  19101. ("inf"."inf")
  19102. ("ker"."ker")
  19103. ("lg"."lg")
  19104. ("lim"."lim")
  19105. ("liminf"."liminf")
  19106. ("limsup"."limsup")
  19107. ("ln"."ln")
  19108. ("log"."log")
  19109. ("max"."max")
  19110. ("min"."min")
  19111. ("Pr"."Pr")
  19112. ("sec"."sec")
  19113. ("sin"."sin")
  19114. ("sinh"."sinh")
  19115. ("sup"."sup")
  19116. ("tan"."tan")
  19117. ("tanh"."tanh")
  19118. )
  19119. "Entities for TeX->HTML translation.
  19120. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  19121. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  19122. In that case, \"\\ent\" will be translated to \"&other;\".
  19123. The list contains HTML entities for Latin-1, Greek and other symbols.
  19124. It is supplemented by a number of commonly used TeX macros with appropriate
  19125. translations. There is currently no way for users to extend this.")
  19126. ;;; General functions for all backends
  19127. (defun org-cleaned-string-for-export (string &rest parameters)
  19128. "Cleanup a buffer STRING so that links can be created safely."
  19129. (interactive)
  19130. (let* ((re-radio (and org-target-link-regexp
  19131. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  19132. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  19133. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  19134. (re-archive (concat ":" org-archive-tag ":"))
  19135. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  19136. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  19137. (htmlp (plist-get parameters :for-html))
  19138. (asciip (plist-get parameters :for-ascii))
  19139. (latexp (plist-get parameters :for-LaTeX))
  19140. (commentsp (plist-get parameters :comments))
  19141. (archived-trees (plist-get parameters :archived-trees))
  19142. (inhibit-read-only t)
  19143. (outline-regexp "\\*+ ")
  19144. a b xx
  19145. rtn p)
  19146. (with-current-buffer (get-buffer-create " org-mode-tmp")
  19147. (erase-buffer)
  19148. (insert string)
  19149. ;; Remove license-to-kill stuff
  19150. (while (setq p (text-property-any (point-min) (point-max)
  19151. :org-license-to-kill t))
  19152. (delete-region p (next-single-property-change p :org-license-to-kill)))
  19153. (let ((org-inhibit-startup t)) (org-mode))
  19154. (untabify (point-min) (point-max))
  19155. ;; Get the correct stuff before the first headline
  19156. (when (plist-get parameters :skip-before-1st-heading)
  19157. (goto-char (point-min))
  19158. (when (re-search-forward "^\\*+[ \t]" nil t)
  19159. (delete-region (point-min) (match-beginning 0))
  19160. (goto-char (point-min))
  19161. (insert "\n")))
  19162. (when (plist-get parameters :add-text)
  19163. (goto-char (point-min))
  19164. (insert (plist-get parameters :add-text) "\n"))
  19165. ;; Get rid of archived trees
  19166. (when (not (eq archived-trees t))
  19167. (goto-char (point-min))
  19168. (while (re-search-forward re-archive nil t)
  19169. (if (not (org-on-heading-p t))
  19170. (org-end-of-subtree t)
  19171. (beginning-of-line 1)
  19172. (setq a (if archived-trees
  19173. (1+ (point-at-eol)) (point))
  19174. b (org-end-of-subtree t))
  19175. (if (> b a) (delete-region a b)))))
  19176. ;; Get rid of property drawers
  19177. (unless org-export-with-property-drawer
  19178. (goto-char (point-min))
  19179. (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t)
  19180. (replace-match "")))
  19181. ;; Find targets in comments and move them out of comments,
  19182. ;; but mark them as targets that should be invisible
  19183. (goto-char (point-min))
  19184. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  19185. (replace-match "\\1(INVISIBLE)"))
  19186. ;; Protect backend specific stuff, throw away the others.
  19187. (goto-char (point-min))
  19188. (let ((formatters
  19189. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  19190. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  19191. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  19192. fmt)
  19193. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  19194. (add-text-properties (match-beginning 0) (match-end 0)
  19195. '(org-protected t)))
  19196. (while formatters
  19197. (setq fmt (pop formatters))
  19198. (when (car fmt)
  19199. (goto-char (point-min))
  19200. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  19201. ":[ \t]*\\(.*\\)") nil t)
  19202. (replace-match "\\1" t)
  19203. (add-text-properties
  19204. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  19205. '(org-protected t))))
  19206. (goto-char (point-min))
  19207. (while (re-search-forward
  19208. (concat "^#\\+"
  19209. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  19210. (cadddr fmt) "\\>.*\n?") nil t)
  19211. (if (car fmt)
  19212. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  19213. '(org-protected t))
  19214. (delete-region (match-beginning 0) (match-end 0))))))
  19215. ;; Protect quoted subtrees
  19216. (goto-char (point-min))
  19217. (while (re-search-forward re-quote nil t)
  19218. (goto-char (match-beginning 0))
  19219. (end-of-line 1)
  19220. (add-text-properties (point) (org-end-of-subtree t)
  19221. '(org-protected t)))
  19222. ;; Remove subtrees that are commented
  19223. (goto-char (point-min))
  19224. (while (re-search-forward re-commented nil t)
  19225. (goto-char (match-beginning 0))
  19226. (delete-region (point) (org-end-of-subtree t)))
  19227. ;; Remove special table lines
  19228. (when org-export-table-remove-special-lines
  19229. (goto-char (point-min))
  19230. (while (re-search-forward "^[ \t]*|" nil t)
  19231. (beginning-of-line 1)
  19232. (if (or (looking-at "[ \t]*| *[!_^] *|")
  19233. (and (looking-at ".*?| *<[0-9]+> *|")
  19234. (not (looking-at ".*?| *[^ <|]"))))
  19235. (delete-region (max (point-min) (1- (point-at-bol)))
  19236. (point-at-eol))
  19237. (end-of-line 1))))
  19238. ;; Specific LaTeX stuff
  19239. (when latexp
  19240. (require 'org-export-latex nil t)
  19241. (org-export-latex-cleaned-string))
  19242. ;; Specific HTML stuff
  19243. (when htmlp
  19244. ;; Convert LaTeX fragments to images
  19245. (when (plist-get parameters :LaTeX-fragments)
  19246. (org-format-latex
  19247. (concat "ltxpng/" (file-name-sans-extension
  19248. (file-name-nondirectory
  19249. org-current-export-file)))
  19250. org-current-export-dir nil "Creating LaTeX image %s"))
  19251. (message "Exporting..."))
  19252. ;; Remove or replace comments
  19253. ;; FIXME: Does LaTeX export take care of its own comments?
  19254. ;; If :comments is set, use this char for commenting out comments and
  19255. ;; protect them. otherwise delete them
  19256. (goto-char (point-min))
  19257. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  19258. (if commentsp
  19259. (progn (add-text-properties
  19260. (match-beginning 0) (match-end 0) '(org-protected t))
  19261. (replace-match (format commentsp (match-string 1)) t t))
  19262. (replace-match "")))
  19263. ;; Find matches for radio targets and turn them into internal links
  19264. (goto-char (point-min))
  19265. (when re-radio
  19266. (while (re-search-forward re-radio nil t)
  19267. (org-if-unprotected
  19268. (replace-match "\\1[[\\2]]"))))
  19269. ;; Find all links that contain a newline and put them into a single line
  19270. (goto-char (point-min))
  19271. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  19272. (org-if-unprotected
  19273. (replace-match "\\1 \\3")
  19274. (goto-char (match-beginning 0))))
  19275. ;; Normalize links: Convert angle and plain links into bracket links
  19276. ;; Expand link abbreviations
  19277. (goto-char (point-min))
  19278. (while (re-search-forward re-plain-link nil t)
  19279. (goto-char (1- (match-end 0)))
  19280. (org-if-unprotected
  19281. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19282. ":" (match-string 3) "]]")))
  19283. ;; added 'org-link face to links
  19284. (put-text-property 0 (length s) 'face 'org-link s)
  19285. (replace-match s t t))))
  19286. (goto-char (point-min))
  19287. (while (re-search-forward re-angle-link nil t)
  19288. (goto-char (1- (match-end 0)))
  19289. (org-if-unprotected
  19290. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19291. ":" (match-string 3) "]]")))
  19292. (put-text-property 0 (length s) 'face 'org-link s)
  19293. (replace-match s t t))))
  19294. (goto-char (point-min))
  19295. (while (re-search-forward org-bracket-link-regexp nil t)
  19296. (org-if-unprotected
  19297. (let* ((s (concat "[[" (setq xx (save-match-data
  19298. (org-link-expand-abbrev (match-string 1))))
  19299. "]"
  19300. (if (match-end 3)
  19301. (match-string 2)
  19302. (concat "[" xx "]"))
  19303. "]")))
  19304. (put-text-property 0 (length s) 'face 'org-link s)
  19305. (replace-match s t t))))
  19306. ;; Find multiline emphasis and put them into single line
  19307. (when (plist-get parameters :emph-multiline)
  19308. (goto-char (point-min))
  19309. (while (re-search-forward org-emph-re nil t)
  19310. (if (not (= (char-after (match-beginning 3))
  19311. (char-after (match-beginning 4))))
  19312. (org-if-unprotected
  19313. (subst-char-in-region (match-beginning 0) (match-end 0)
  19314. ?\n ?\ t)
  19315. (goto-char (1- (match-end 0))))
  19316. (goto-char (1+ (match-beginning 0))))))
  19317. (setq rtn (buffer-string)))
  19318. (kill-buffer " org-mode-tmp")
  19319. rtn))
  19320. (defun org-export-grab-title-from-buffer ()
  19321. "Get a title for the current document, from looking at the buffer."
  19322. (let ((inhibit-read-only t))
  19323. (save-excursion
  19324. (goto-char (point-min))
  19325. (let ((end (save-excursion (outline-next-heading) (point))))
  19326. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  19327. ;; Mark the line so that it will not be exported as normal text.
  19328. (org-unmodified
  19329. (add-text-properties (match-beginning 0) (match-end 0)
  19330. (list :org-license-to-kill t)))
  19331. ;; Return the title string
  19332. (org-trim (match-string 0)))))))
  19333. (defun org-export-get-title-from-subtree ()
  19334. "Return subtree title and exclude it from export."
  19335. (let (title (m (mark)))
  19336. (save-excursion
  19337. (goto-char (region-beginning))
  19338. (when (and (org-at-heading-p)
  19339. (>= (org-end-of-subtree t t) (region-end)))
  19340. ;; This is a subtree, we take the title from the first heading
  19341. (goto-char (region-beginning))
  19342. (looking-at org-todo-line-regexp)
  19343. (setq title (match-string 3))
  19344. (org-unmodified
  19345. (add-text-properties (point) (1+ (point-at-eol))
  19346. (list :org-license-to-kill t)))))
  19347. title))
  19348. (defun org-solidify-link-text (s &optional alist)
  19349. "Take link text and make a safe target out of it."
  19350. (save-match-data
  19351. (let* ((rtn
  19352. (mapconcat
  19353. 'identity
  19354. (org-split-string s "[ \t\r\n]+") "--"))
  19355. (a (assoc rtn alist)))
  19356. (or (cdr a) rtn))))
  19357. ;; Variable holding the vector with section numbers
  19358. (defvar org-section-numbers (make-vector org-level-max 0))
  19359. (defun org-init-section-numbers ()
  19360. "Initialize the vector for the section numbers."
  19361. (let* ((level -1)
  19362. (numbers (nreverse (org-split-string "" "\\.")))
  19363. (depth (1- (length org-section-numbers)))
  19364. (i depth) number-string)
  19365. (while (>= i 0)
  19366. (if (> i level)
  19367. (aset org-section-numbers i 0)
  19368. (setq number-string (or (car numbers) "0"))
  19369. (if (string-match "\\`[A-Z]\\'" number-string)
  19370. (aset org-section-numbers i
  19371. (- (string-to-char number-string) ?A -1))
  19372. (aset org-section-numbers i (string-to-number number-string)))
  19373. (pop numbers))
  19374. (setq i (1- i)))))
  19375. (defun org-section-number (&optional level)
  19376. "Return a string with the current section number.
  19377. When LEVEL is non-nil, increase section numbers on that level."
  19378. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  19379. (when level
  19380. (when (> level -1)
  19381. (aset org-section-numbers
  19382. level (1+ (aref org-section-numbers level))))
  19383. (setq idx (1+ level))
  19384. (while (<= idx depth)
  19385. (if (not (= idx 1))
  19386. (aset org-section-numbers idx 0))
  19387. (setq idx (1+ idx))))
  19388. (setq idx 0)
  19389. (while (<= idx depth)
  19390. (setq n (aref org-section-numbers idx))
  19391. (setq string (concat string (if (not (string= string "")) "." "")
  19392. (int-to-string n)))
  19393. (setq idx (1+ idx)))
  19394. (save-match-data
  19395. (if (string-match "\\`\\([@0]\\.\\)+" string)
  19396. (setq string (replace-match "" t nil string)))
  19397. (if (string-match "\\(\\.0\\)+\\'" string)
  19398. (setq string (replace-match "" t nil string))))
  19399. string))
  19400. ;;; ASCII export
  19401. (defvar org-last-level nil) ; dynamically scoped variable
  19402. (defvar org-min-level nil) ; dynamically scoped variable
  19403. (defvar org-levels-open nil) ; dynamically scoped parameter
  19404. (defvar org-ascii-current-indentation nil) ; For communication
  19405. (defun org-export-as-ascii (arg)
  19406. "Export the outline as a pretty ASCII file.
  19407. If there is an active region, export only the region.
  19408. The prefix ARG specifies how many levels of the outline should become
  19409. underlined headlines. The default is 3."
  19410. (interactive "P")
  19411. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19412. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19413. (org-infile-export-plist)))
  19414. (region-p (org-region-active-p))
  19415. (subtree-p
  19416. (when region-p
  19417. (save-excursion
  19418. (goto-char (region-beginning))
  19419. (and (org-at-heading-p)
  19420. (>= (org-end-of-subtree t t) (region-end))))))
  19421. (custom-times org-display-custom-times)
  19422. (org-ascii-current-indentation '(0 . 0))
  19423. (level 0) line txt
  19424. (umax nil)
  19425. (umax-toc nil)
  19426. (case-fold-search nil)
  19427. (filename (concat (file-name-as-directory
  19428. (org-export-directory :ascii opt-plist))
  19429. (file-name-sans-extension
  19430. (or (and subtree-p
  19431. (org-entry-get (region-beginning)
  19432. "EXPORT_FILE_NAME" t))
  19433. (file-name-nondirectory buffer-file-name)))
  19434. ".txt"))
  19435. (filename (if (equal (file-truename filename)
  19436. (file-truename buffer-file-name))
  19437. (concat filename ".txt")
  19438. filename))
  19439. (buffer (find-file-noselect filename))
  19440. (org-levels-open (make-vector org-level-max nil))
  19441. (odd org-odd-levels-only)
  19442. (date (format-time-string "%Y/%m/%d" (current-time)))
  19443. (time (format-time-string "%X" (org-current-time)))
  19444. (author (plist-get opt-plist :author))
  19445. (title (or (and subtree-p (org-export-get-title-from-subtree))
  19446. (plist-get opt-plist :title)
  19447. (and (not
  19448. (plist-get opt-plist :skip-before-1st-heading))
  19449. (org-export-grab-title-from-buffer))
  19450. (file-name-sans-extension
  19451. (file-name-nondirectory buffer-file-name))))
  19452. (email (plist-get opt-plist :email))
  19453. (language (plist-get opt-plist :language))
  19454. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  19455. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  19456. (todo nil)
  19457. (lang-words nil)
  19458. (region
  19459. (buffer-substring
  19460. (if (org-region-active-p) (region-beginning) (point-min))
  19461. (if (org-region-active-p) (region-end) (point-max))))
  19462. (lines (org-split-string
  19463. (org-cleaned-string-for-export
  19464. region
  19465. :for-ascii t
  19466. :skip-before-1st-heading
  19467. (plist-get opt-plist :skip-before-1st-heading)
  19468. :archived-trees
  19469. (plist-get opt-plist :archived-trees)
  19470. :add-text (plist-get opt-plist :text))
  19471. "[\r\n]")) ;; FIXME: why \r here???/
  19472. thetoc have-headings first-heading-pos
  19473. table-open table-buffer)
  19474. (let ((inhibit-read-only t))
  19475. (org-unmodified
  19476. (remove-text-properties (point-min) (point-max)
  19477. '(:org-license-to-kill t))))
  19478. (setq org-min-level (org-get-min-level lines))
  19479. (setq org-last-level org-min-level)
  19480. (org-init-section-numbers)
  19481. (find-file-noselect filename)
  19482. (setq lang-words (or (assoc language org-export-language-setup)
  19483. (assoc "en" org-export-language-setup)))
  19484. (switch-to-buffer-other-window buffer)
  19485. (erase-buffer)
  19486. (fundamental-mode)
  19487. ;; create local variables for all options, to make sure all called
  19488. ;; functions get the correct information
  19489. (mapcar (lambda (x)
  19490. (set (make-local-variable (cdr x))
  19491. (plist-get opt-plist (car x))))
  19492. org-export-plist-vars)
  19493. (org-set-local 'org-odd-levels-only odd)
  19494. (setq umax (if arg (prefix-numeric-value arg)
  19495. org-export-headline-levels))
  19496. (setq umax-toc (if (integerp org-export-with-toc)
  19497. (min org-export-with-toc umax)
  19498. umax))
  19499. ;; File header
  19500. (if title (org-insert-centered title ?=))
  19501. (insert "\n")
  19502. (if (and (or author email)
  19503. org-export-author-info)
  19504. (insert (concat (nth 1 lang-words) ": " (or author "")
  19505. (if email (concat " <" email ">") "")
  19506. "\n")))
  19507. (if (and date time org-export-time-stamp-file)
  19508. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  19509. (insert "\n\n")
  19510. (if org-export-with-toc
  19511. (progn
  19512. (push (concat (nth 3 lang-words) "\n") thetoc)
  19513. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  19514. (mapcar '(lambda (line)
  19515. (if (string-match org-todo-line-regexp
  19516. line)
  19517. ;; This is a headline
  19518. (progn
  19519. (setq have-headings t)
  19520. (setq level (- (match-end 1) (match-beginning 1))
  19521. level (org-tr-level level)
  19522. txt (match-string 3 line)
  19523. todo
  19524. (or (and org-export-mark-todo-in-toc
  19525. (match-beginning 2)
  19526. (not (member (match-string 2 line)
  19527. org-done-keywords)))
  19528. ; TODO, not DONE
  19529. (and org-export-mark-todo-in-toc
  19530. (= level umax-toc)
  19531. (org-search-todo-below
  19532. line lines level))))
  19533. (setq txt (org-html-expand-for-ascii txt))
  19534. (if (and (memq org-export-with-tags '(not-in-toc nil))
  19535. (string-match
  19536. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  19537. txt))
  19538. (setq txt (replace-match "" t t txt)))
  19539. (if (string-match quote-re0 txt)
  19540. (setq txt (replace-match "" t t txt)))
  19541. (if org-export-with-section-numbers
  19542. (setq txt (concat (org-section-number level)
  19543. " " txt)))
  19544. (if (<= level umax-toc)
  19545. (progn
  19546. (push
  19547. (concat
  19548. (make-string
  19549. (* (max 0 (- level org-min-level)) 4) ?\ )
  19550. (format (if todo "%s (*)\n" "%s\n") txt))
  19551. thetoc)
  19552. (setq org-last-level level))
  19553. ))))
  19554. lines)
  19555. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  19556. (org-init-section-numbers)
  19557. (while (setq line (pop lines))
  19558. ;; Remove the quoted HTML tags.
  19559. (setq line (org-html-expand-for-ascii line))
  19560. ;; Remove targets
  19561. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  19562. (setq line (replace-match "" t t line)))
  19563. ;; Replace internal links
  19564. (while (string-match org-bracket-link-regexp line)
  19565. (setq line (replace-match
  19566. (if (match-end 3) "[\\3]" "[\\1]")
  19567. t nil line)))
  19568. (when custom-times
  19569. (setq line (org-translate-time line)))
  19570. (cond
  19571. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  19572. ;; a Headline
  19573. (setq first-heading-pos (or first-heading-pos (point)))
  19574. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  19575. txt (match-string 2 line))
  19576. (org-ascii-level-start level txt umax lines))
  19577. ((and org-export-with-tables
  19578. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  19579. (if (not table-open)
  19580. ;; New table starts
  19581. (setq table-open t table-buffer nil))
  19582. ;; Accumulate lines
  19583. (setq table-buffer (cons line table-buffer))
  19584. (when (or (not lines)
  19585. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  19586. (car lines))))
  19587. (setq table-open nil
  19588. table-buffer (nreverse table-buffer))
  19589. (insert (mapconcat
  19590. (lambda (x)
  19591. (org-fix-indentation x org-ascii-current-indentation))
  19592. (org-format-table-ascii table-buffer)
  19593. "\n") "\n")))
  19594. (t
  19595. (setq line (org-fix-indentation line org-ascii-current-indentation))
  19596. (if (and org-export-with-fixed-width
  19597. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  19598. (setq line (replace-match "\\1" nil nil line)))
  19599. (insert line "\n"))))
  19600. (normal-mode)
  19601. ;; insert the table of contents
  19602. (when thetoc
  19603. (goto-char (point-min))
  19604. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  19605. (progn
  19606. (goto-char (match-beginning 0))
  19607. (replace-match ""))
  19608. (goto-char first-heading-pos))
  19609. (mapc 'insert thetoc)
  19610. (or (looking-at "[ \t]*\n[ \t]*\n")
  19611. (insert "\n\n")))
  19612. (save-buffer)
  19613. ;; remove display and invisible chars
  19614. (let (beg end)
  19615. (goto-char (point-min))
  19616. (while (setq beg (next-single-property-change (point) 'display))
  19617. (setq end (next-single-property-change beg 'display))
  19618. (delete-region beg end)
  19619. (goto-char beg)
  19620. (insert "=>"))
  19621. (goto-char (point-min))
  19622. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  19623. (setq end (next-single-property-change beg 'org-cwidth))
  19624. (delete-region beg end)
  19625. (goto-char beg)))
  19626. (goto-char (point-min))))
  19627. (defun org-search-todo-below (line lines level)
  19628. "Search the subtree below LINE for any TODO entries."
  19629. (let ((rest (cdr (memq line lines)))
  19630. (re org-todo-line-regexp)
  19631. line lv todo)
  19632. (catch 'exit
  19633. (while (setq line (pop rest))
  19634. (if (string-match re line)
  19635. (progn
  19636. (setq lv (- (match-end 1) (match-beginning 1))
  19637. todo (and (match-beginning 2)
  19638. (not (member (match-string 2 line)
  19639. org-done-keywords))))
  19640. ; TODO, not DONE
  19641. (if (<= lv level) (throw 'exit nil))
  19642. (if todo (throw 'exit t))))))))
  19643. (defun org-html-expand-for-ascii (line)
  19644. "Handle quoted HTML for ASCII export."
  19645. (if org-export-html-expand
  19646. (while (string-match "@<[^<>\n]*>" line)
  19647. ;; We just remove the tags for now.
  19648. (setq line (replace-match "" nil nil line))))
  19649. line)
  19650. (defun org-insert-centered (s &optional underline)
  19651. "Insert the string S centered and underline it with character UNDERLINE."
  19652. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  19653. (insert (make-string ind ?\ ) s "\n")
  19654. (if underline
  19655. (insert (make-string ind ?\ )
  19656. (make-string (string-width s) underline)
  19657. "\n"))))
  19658. (defun org-ascii-level-start (level title umax &optional lines)
  19659. "Insert a new level in ASCII export."
  19660. (let (char (n (- level umax 1)) (ind 0))
  19661. (if (> level umax)
  19662. (progn
  19663. (insert (make-string (* 2 n) ?\ )
  19664. (char-to-string (nth (% n (length org-export-ascii-bullets))
  19665. org-export-ascii-bullets))
  19666. " " title "\n")
  19667. ;; find the indentation of the next non-empty line
  19668. (catch 'stop
  19669. (while lines
  19670. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  19671. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  19672. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  19673. (pop lines)))
  19674. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  19675. (if (or (not (equal (char-before) ?\n))
  19676. (not (equal (char-before (1- (point))) ?\n)))
  19677. (insert "\n"))
  19678. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  19679. (unless org-export-with-tags
  19680. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  19681. (setq title (replace-match "" t t title))))
  19682. (if org-export-with-section-numbers
  19683. (setq title (concat (org-section-number level) " " title)))
  19684. (insert title "\n" (make-string (string-width title) char) "\n")
  19685. (setq org-ascii-current-indentation '(0 . 0)))))
  19686. (defun org-export-visible (type arg)
  19687. "Create a copy of the visible part of the current buffer, and export it.
  19688. The copy is created in a temporary buffer and removed after use.
  19689. TYPE is the final key (as a string) that also select the export command in
  19690. the `C-c C-e' export dispatcher.
  19691. As a special case, if the you type SPC at the prompt, the temporary
  19692. org-mode file will not be removed but presented to you so that you can
  19693. continue to use it. The prefix arg ARG is passed through to the exporting
  19694. command."
  19695. (interactive
  19696. (list (progn
  19697. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  19698. (read-char-exclusive))
  19699. current-prefix-arg))
  19700. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  19701. (error "Invalid export key"))
  19702. (let* ((binding (cdr (assoc type
  19703. '((?a . org-export-as-ascii)
  19704. (?\C-a . org-export-as-ascii)
  19705. (?b . org-export-as-html-and-open)
  19706. (?\C-b . org-export-as-html-and-open)
  19707. (?h . org-export-as-html)
  19708. (?H . org-export-as-html-to-buffer)
  19709. (?R . org-export-region-as-html)
  19710. (?x . org-export-as-xoxo)))))
  19711. (keepp (equal type ?\ ))
  19712. (file buffer-file-name)
  19713. (buffer (get-buffer-create "*Org Export Visible*"))
  19714. s e)
  19715. ;; Need to hack the drawers here.
  19716. (save-excursion
  19717. (goto-char (point-min))
  19718. (while (re-search-forward org-drawer-regexp nil t)
  19719. (goto-char (match-beginning 1))
  19720. (or (org-invisible-p) (org-flag-drawer nil))))
  19721. (with-current-buffer buffer (erase-buffer))
  19722. (save-excursion
  19723. (setq s (goto-char (point-min)))
  19724. (while (not (= (point) (point-max)))
  19725. (goto-char (org-find-invisible))
  19726. (append-to-buffer buffer s (point))
  19727. (setq s (goto-char (org-find-visible))))
  19728. (org-cycle-hide-drawers 'all)
  19729. (goto-char (point-min))
  19730. (unless keepp
  19731. ;; Copy all comment lines to the end, to make sure #+ settings are
  19732. ;; still available for the second export step. Kind of a hack, but
  19733. ;; does do the trick.
  19734. (if (looking-at "#[^\r\n]*")
  19735. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  19736. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  19737. (append-to-buffer buffer (1+ (match-beginning 0))
  19738. (min (point-max) (1+ (match-end 0))))))
  19739. (set-buffer buffer)
  19740. (let ((buffer-file-name file)
  19741. (org-inhibit-startup t))
  19742. (org-mode)
  19743. (show-all)
  19744. (unless keepp (funcall binding arg))))
  19745. (if (not keepp)
  19746. (kill-buffer buffer)
  19747. (switch-to-buffer-other-window buffer)
  19748. (goto-char (point-min)))))
  19749. (defun org-find-visible ()
  19750. (let ((s (point)))
  19751. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19752. (get-char-property s 'invisible)))
  19753. s))
  19754. (defun org-find-invisible ()
  19755. (let ((s (point)))
  19756. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19757. (not (get-char-property s 'invisible))))
  19758. s))
  19759. ;;; HTML export
  19760. (defun org-get-current-options ()
  19761. "Return a string with current options as keyword options.
  19762. Does include HTML export options as well as TODO and CATEGORY stuff."
  19763. (format
  19764. "#+TITLE: %s
  19765. #+AUTHOR: %s
  19766. #+EMAIL: %s
  19767. #+LANGUAGE: %s
  19768. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  19769. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s
  19770. #+CATEGORY: %s
  19771. #+SEQ_TODO: %s
  19772. #+TYP_TODO: %s
  19773. #+PRIORITIES: %c %c %c
  19774. #+STARTUP: %s %s %s %s %s
  19775. #+TAGS: %s
  19776. #+ARCHIVE: %s
  19777. #+LINK: %s
  19778. "
  19779. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  19780. org-export-headline-levels
  19781. org-export-with-section-numbers
  19782. org-export-with-toc
  19783. org-export-preserve-breaks
  19784. org-export-html-expand
  19785. org-export-with-fixed-width
  19786. org-export-with-tables
  19787. org-export-with-sub-superscripts
  19788. org-export-with-footnotes
  19789. org-export-with-emphasize
  19790. org-export-with-TeX-macros
  19791. org-export-with-LaTeX-fragments
  19792. org-export-skip-text-before-1st-heading
  19793. org-export-with-property-drawer
  19794. (file-name-nondirectory buffer-file-name)
  19795. "TODO FEEDBACK VERIFY DONE"
  19796. "Me Jason Marie DONE"
  19797. org-highest-priority org-lowest-priority org-default-priority
  19798. (cdr (assoc org-startup-folded
  19799. '((nil . "showall") (t . "overview") (content . "content"))))
  19800. (if org-odd-levels-only "odd" "oddeven")
  19801. (if org-hide-leading-stars "hidestars" "showstars")
  19802. (if org-startup-align-all-tables "align" "noalign")
  19803. (cond ((eq t org-log-done) "logdone")
  19804. ((not org-log-done) "nologging")
  19805. ((listp org-log-done)
  19806. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  19807. org-log-done " ")))
  19808. (or (mapconcat (lambda (x)
  19809. (cond
  19810. ((equal '(:startgroup) x) "{")
  19811. ((equal '(:endgroup) x) "}")
  19812. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  19813. (t (car x))))
  19814. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  19815. org-archive-location
  19816. "org file:~/org/%s.org"
  19817. ))
  19818. (defun org-insert-export-options-template ()
  19819. "Insert into the buffer a template with information for exporting."
  19820. (interactive)
  19821. (if (not (bolp)) (newline))
  19822. (let ((s (org-get-current-options)))
  19823. (and (string-match "#\\+CATEGORY" s)
  19824. (setq s (substring s 0 (match-beginning 0))))
  19825. (insert s)))
  19826. (defun org-toggle-fixed-width-section (arg)
  19827. "Toggle the fixed-width export.
  19828. If there is no active region, the QUOTE keyword at the current headline is
  19829. inserted or removed. When present, it causes the text between this headline
  19830. and the next to be exported as fixed-width text, and unmodified.
  19831. If there is an active region, this command adds or removes a colon as the
  19832. first character of this line. If the first character of a line is a colon,
  19833. this line is also exported in fixed-width font."
  19834. (interactive "P")
  19835. (let* ((cc 0)
  19836. (regionp (org-region-active-p))
  19837. (beg (if regionp (region-beginning) (point)))
  19838. (end (if regionp (region-end)))
  19839. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19840. (re "[ \t]*\\(:\\)")
  19841. off)
  19842. (if regionp
  19843. (save-excursion
  19844. (goto-char beg)
  19845. (setq cc (current-column))
  19846. (beginning-of-line 1)
  19847. (setq off (looking-at re))
  19848. (while (> nlines 0)
  19849. (setq nlines (1- nlines))
  19850. (beginning-of-line 1)
  19851. (cond
  19852. (arg
  19853. (move-to-column cc t)
  19854. (insert ":\n")
  19855. (forward-line -1))
  19856. ((and off (looking-at re))
  19857. (replace-match "" t t nil 1))
  19858. ((not off) (move-to-column cc t) (insert ":")))
  19859. (forward-line 1)))
  19860. (save-excursion
  19861. (org-back-to-heading)
  19862. (if (looking-at (concat outline-regexp
  19863. "\\( *\\<" org-quote-string "\\>\\)"))
  19864. (replace-match "" t t nil 1)
  19865. (if (looking-at outline-regexp)
  19866. (progn
  19867. (goto-char (match-end 0))
  19868. (insert org-quote-string " "))))))))
  19869. (defun org-export-as-html-and-open (arg)
  19870. "Export the outline as HTML and immediately open it with a browser.
  19871. If there is an active region, export only the region.
  19872. The prefix ARG specifies how many levels of the outline should become
  19873. headlines. The default is 3. Lower levels will become bulleted lists."
  19874. (interactive "P")
  19875. (org-export-as-html arg 'hidden)
  19876. (org-open-file buffer-file-name))
  19877. (defun org-export-as-html-batch ()
  19878. "Call `org-export-as-html', may be used in batch processing as
  19879. emacs --batch
  19880. --load=$HOME/lib/emacs/org.el
  19881. --eval \"(setq org-export-headline-levels 2)\"
  19882. --visit=MyFile --funcall org-export-as-html-batch"
  19883. (org-export-as-html org-export-headline-levels 'hidden))
  19884. (defun org-export-as-html-to-buffer (arg)
  19885. "Call `org-exort-as-html` with output to a temporary buffer.
  19886. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  19887. (interactive "P")
  19888. (org-export-as-html arg nil nil "*Org HTML Export*")
  19889. (switch-to-buffer-other-window "*Org HTML Export*"))
  19890. (defun org-replace-region-by-html (beg end)
  19891. "Assume the current region has org-mode syntax, and convert it to HTML.
  19892. This can be used in any buffer. For example, you could write an
  19893. itemized list in org-mode syntax in an HTML buffer and then use this
  19894. command to convert it."
  19895. (interactive "r")
  19896. (let (reg html buf pop-up-frames)
  19897. (save-window-excursion
  19898. (if (org-mode-p)
  19899. (setq html (org-export-region-as-html
  19900. beg end t 'string))
  19901. (setq reg (buffer-substring beg end)
  19902. buf (get-buffer-create "*Org tmp*"))
  19903. (with-current-buffer buf
  19904. (erase-buffer)
  19905. (insert reg)
  19906. (org-mode)
  19907. (setq html (org-export-region-as-html
  19908. (point-min) (point-max) t 'string)))
  19909. (kill-buffer buf)))
  19910. (delete-region beg end)
  19911. (insert html)))
  19912. (defun org-export-region-as-html (beg end &optional body-only buffer)
  19913. "Convert region from BEG to END in org-mode buffer to HTML.
  19914. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  19915. contents, and only produce the region of converted text, useful for
  19916. cut-and-paste operations.
  19917. If BUFFER is a buffer or a string, use/create that buffer as a target
  19918. of the converted HTML. If BUFFER is the symbol `string', return the
  19919. produced HTML as a string and leave not buffer behind. For example,
  19920. a Lisp program could call this function in the following way:
  19921. (setq html (org-export-region-as-html beg end t 'string))
  19922. When called interactively, the output buffer is selected, and shown
  19923. in a window. A non-interactive call will only retunr the buffer."
  19924. (interactive "r\nP")
  19925. (when (interactive-p)
  19926. (setq buffer "*Org HTML Export*"))
  19927. (let ((transient-mark-mode t) (zmacs-regions t)
  19928. rtn)
  19929. (goto-char end)
  19930. (set-mark (point)) ;; to activate the region
  19931. (goto-char beg)
  19932. (setq rtn (org-export-as-html
  19933. nil nil nil
  19934. buffer body-only))
  19935. (if (fboundp 'deactivate-mark) (deactivate-mark))
  19936. (if (and (interactive-p) (bufferp rtn))
  19937. (switch-to-buffer-other-window rtn)
  19938. rtn)))
  19939. (defun org-export-as-html (arg &optional hidden ext-plist
  19940. to-buffer body-only)
  19941. "Export the outline as a pretty HTML file.
  19942. If there is an active region, export only the region. The prefix
  19943. ARG specifies how many levels of the outline should become
  19944. headlines. The default is 3. Lower levels will become bulleted
  19945. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  19946. EXT-PLIST is a property list with external parameters overriding
  19947. org-mode's default settings, but still inferior to file-local
  19948. settings. When TO-BUFFER is non-nil, create a buffer with that
  19949. name and export to that buffer. If TO-BUFFER is the symbol `string',
  19950. don't leave any buffer behind but just return the resulting HTML as
  19951. a string. When BODY-ONLY is set, don't produce the file header and footer,
  19952. simply return the content of <body>...</body>, without even
  19953. the body tags themselves."
  19954. (interactive "P")
  19955. ;; Make sure we have a file name when we need it.
  19956. (when (and (not (or to-buffer body-only))
  19957. (not buffer-file-name))
  19958. (if (buffer-base-buffer)
  19959. (org-set-local 'buffer-file-name
  19960. (with-current-buffer (buffer-base-buffer)
  19961. buffer-file-name))
  19962. (error "Need a file name to be able to export.")))
  19963. (message "Exporting...")
  19964. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19965. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  19966. (setq-default org-done-keywords org-done-keywords)
  19967. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  19968. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19969. ext-plist
  19970. (org-infile-export-plist)))
  19971. (style (plist-get opt-plist :style))
  19972. (link-validate (plist-get opt-plist :link-validation-function))
  19973. valid thetoc have-headings first-heading-pos
  19974. (odd org-odd-levels-only)
  19975. (region-p (org-region-active-p))
  19976. (subtree-p
  19977. (when region-p
  19978. (save-excursion
  19979. (goto-char (region-beginning))
  19980. (and (org-at-heading-p)
  19981. (>= (org-end-of-subtree t t) (region-end))))))
  19982. ;; The following two are dynamically scoped into other
  19983. ;; routines below.
  19984. (org-current-export-dir (org-export-directory :html opt-plist))
  19985. (org-current-export-file buffer-file-name)
  19986. (level 0) (line "") (origline "") txt todo
  19987. (umax nil)
  19988. (umax-toc nil)
  19989. (filename (if to-buffer nil
  19990. (concat (file-name-as-directory
  19991. (org-export-directory :html opt-plist))
  19992. (file-name-sans-extension
  19993. (or (and subtree-p
  19994. (org-entry-get (region-beginning)
  19995. "EXPORT_FILE_NAME" t))
  19996. (file-name-nondirectory buffer-file-name)))
  19997. ".html")))
  19998. (current-dir (if buffer-file-name
  19999. (file-name-directory buffer-file-name)
  20000. default-directory))
  20001. (buffer (if to-buffer
  20002. (cond
  20003. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  20004. (t (get-buffer-create to-buffer)))
  20005. (find-file-noselect filename)))
  20006. (org-levels-open (make-vector org-level-max nil))
  20007. (date (format-time-string "%Y/%m/%d" (current-time)))
  20008. (time (format-time-string "%X" (org-current-time)))
  20009. (author (plist-get opt-plist :author))
  20010. (title (or (and subtree-p (org-export-get-title-from-subtree))
  20011. (plist-get opt-plist :title)
  20012. (and (not
  20013. (plist-get opt-plist :skip-before-1st-heading))
  20014. (org-export-grab-title-from-buffer))
  20015. (and buffer-file-name
  20016. (file-name-sans-extension
  20017. (file-name-nondirectory buffer-file-name)))
  20018. "UNTITLED"))
  20019. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  20020. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  20021. (inquote nil)
  20022. (infixed nil)
  20023. (in-local-list nil)
  20024. (local-list-num nil)
  20025. (local-list-indent nil)
  20026. (llt org-plain-list-ordered-item-terminator)
  20027. (email (plist-get opt-plist :email))
  20028. (language (plist-get opt-plist :language))
  20029. (lang-words nil)
  20030. (target-alist nil) tg
  20031. (head-count 0) cnt
  20032. (start 0)
  20033. (coding-system (and (boundp 'buffer-file-coding-system)
  20034. buffer-file-coding-system))
  20035. (coding-system-for-write (or org-export-html-coding-system
  20036. coding-system))
  20037. (save-buffer-coding-system (or org-export-html-coding-system
  20038. coding-system))
  20039. (charset (and coding-system-for-write
  20040. (fboundp 'coding-system-get)
  20041. (coding-system-get coding-system-for-write
  20042. 'mime-charset)))
  20043. (region
  20044. (buffer-substring
  20045. (if region-p (region-beginning) (point-min))
  20046. (if region-p (region-end) (point-max))))
  20047. (lines
  20048. (org-split-string
  20049. (org-cleaned-string-for-export
  20050. region
  20051. :emph-multiline t
  20052. :for-html t
  20053. :skip-before-1st-heading
  20054. (plist-get opt-plist :skip-before-1st-heading)
  20055. :archived-trees
  20056. (plist-get opt-plist :archived-trees)
  20057. :add-text
  20058. (plist-get opt-plist :text)
  20059. :LaTeX-fragments
  20060. (plist-get opt-plist :LaTeX-fragments))
  20061. "[\r\n]"))
  20062. table-open type
  20063. table-buffer table-orig-buffer
  20064. ind start-is-num starter didclose
  20065. rpl path desc descp desc1 desc2 link
  20066. )
  20067. (let ((inhibit-read-only t))
  20068. (org-unmodified
  20069. (remove-text-properties (point-min) (point-max)
  20070. '(:org-license-to-kill t))))
  20071. (message "Exporting...")
  20072. (setq org-min-level (org-get-min-level lines))
  20073. (setq org-last-level org-min-level)
  20074. (org-init-section-numbers)
  20075. ;; Get the language-dependent settings
  20076. (setq lang-words (or (assoc language org-export-language-setup)
  20077. (assoc "en" org-export-language-setup)))
  20078. ;; Switch to the output buffer
  20079. (set-buffer buffer)
  20080. (erase-buffer)
  20081. (fundamental-mode)
  20082. (and (fboundp 'set-buffer-file-coding-system)
  20083. (set-buffer-file-coding-system coding-system-for-write))
  20084. (let ((case-fold-search nil)
  20085. (org-odd-levels-only odd))
  20086. ;; create local variables for all options, to make sure all called
  20087. ;; functions get the correct information
  20088. (mapcar (lambda (x)
  20089. (set (make-local-variable (cdr x))
  20090. (plist-get opt-plist (car x))))
  20091. org-export-plist-vars)
  20092. (setq umax (if arg (prefix-numeric-value arg)
  20093. org-export-headline-levels))
  20094. (setq umax-toc (if (integerp org-export-with-toc)
  20095. (min org-export-with-toc umax)
  20096. umax))
  20097. (unless body-only
  20098. ;; File header
  20099. (insert (format
  20100. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  20101. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  20102. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  20103. lang=\"%s\" xml:lang=\"%s\">
  20104. <head>
  20105. <title>%s</title>
  20106. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  20107. <meta name=\"generator\" content=\"Org-mode\"/>
  20108. <meta name=\"generated\" content=\"%s %s\"/>
  20109. <meta name=\"author\" content=\"%s\"/>
  20110. %s
  20111. </head><body>
  20112. "
  20113. language language (org-html-expand title)
  20114. (or charset "iso-8859-1") date time author style))
  20115. (insert (or (plist-get opt-plist :preamble) ""))
  20116. (when (plist-get opt-plist :auto-preamble)
  20117. (if title (insert (format org-export-html-title-format
  20118. (org-html-expand title))))))
  20119. (if (and org-export-with-toc (not body-only))
  20120. (progn
  20121. (push (format "<h%d>%s</h%d>\n"
  20122. org-export-html-toplevel-hlevel
  20123. (nth 3 lang-words)
  20124. org-export-html-toplevel-hlevel)
  20125. thetoc)
  20126. (push "<ul>\n<li>" thetoc)
  20127. (setq lines
  20128. (mapcar '(lambda (line)
  20129. (if (string-match org-todo-line-regexp line)
  20130. ;; This is a headline
  20131. (progn
  20132. (setq have-headings t)
  20133. (setq level (- (match-end 1) (match-beginning 1))
  20134. level (org-tr-level level)
  20135. txt (save-match-data
  20136. (org-html-expand
  20137. (org-export-cleanup-toc-line
  20138. (match-string 3 line))))
  20139. todo
  20140. (or (and org-export-mark-todo-in-toc
  20141. (match-beginning 2)
  20142. (not (member (match-string 2 line)
  20143. org-done-keywords)))
  20144. ; TODO, not DONE
  20145. (and org-export-mark-todo-in-toc
  20146. (= level umax-toc)
  20147. (org-search-todo-below
  20148. line lines level))))
  20149. (if (and (memq org-export-with-tags '(not-in-toc nil))
  20150. (string-match
  20151. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  20152. txt))
  20153. (setq txt (replace-match "" t t txt)))
  20154. (if (string-match quote-re0 txt)
  20155. (setq txt (replace-match "" t t txt)))
  20156. (if org-export-with-section-numbers
  20157. (setq txt (concat (org-section-number level)
  20158. " " txt)))
  20159. (if (<= level (max umax umax-toc))
  20160. (setq head-count (+ head-count 1)))
  20161. (if (<= level umax-toc)
  20162. (progn
  20163. (if (> level org-last-level)
  20164. (progn
  20165. (setq cnt (- level org-last-level))
  20166. (while (>= (setq cnt (1- cnt)) 0)
  20167. (push "\n<ul>\n<li>" thetoc))
  20168. (push "\n" thetoc)))
  20169. (if (< level org-last-level)
  20170. (progn
  20171. (setq cnt (- org-last-level level))
  20172. (while (>= (setq cnt (1- cnt)) 0)
  20173. (push "</li>\n</ul>" thetoc))
  20174. (push "\n" thetoc)))
  20175. ;; Check for targets
  20176. (while (string-match org-target-regexp line)
  20177. (setq tg (match-string 1 line)
  20178. line (replace-match
  20179. (concat "@<span class=\"target\">" tg "@</span> ")
  20180. t t line))
  20181. (push (cons (org-solidify-link-text tg)
  20182. (format "sec-%d" head-count))
  20183. target-alist))
  20184. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  20185. (setq txt (replace-match "" t t txt)))
  20186. (push
  20187. (format
  20188. (if todo
  20189. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  20190. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  20191. head-count txt) thetoc)
  20192. (setq org-last-level level))
  20193. )))
  20194. line)
  20195. lines))
  20196. (while (> org-last-level (1- org-min-level))
  20197. (setq org-last-level (1- org-last-level))
  20198. (push "</li>\n</ul>\n" thetoc))
  20199. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  20200. (setq head-count 0)
  20201. (org-init-section-numbers)
  20202. (while (setq line (pop lines) origline line)
  20203. (catch 'nextline
  20204. ;; end of quote section?
  20205. (when (and inquote (string-match "^\\*+ " line))
  20206. (insert "</pre>\n")
  20207. (setq inquote nil))
  20208. ;; inside a quote section?
  20209. (when inquote
  20210. (insert (org-html-protect line) "\n")
  20211. (throw 'nextline nil))
  20212. ;; verbatim lines
  20213. (when (and org-export-with-fixed-width
  20214. (string-match "^[ \t]*:\\(.*\\)" line))
  20215. (when (not infixed)
  20216. (setq infixed t)
  20217. (insert "<pre>\n"))
  20218. (insert (org-html-protect (match-string 1 line)) "\n")
  20219. (when (and lines
  20220. (not (string-match "^[ \t]*\\(:.*\\)"
  20221. (car lines))))
  20222. (setq infixed nil)
  20223. (insert "</pre>\n"))
  20224. (throw 'nextline nil))
  20225. ;; Protected HTML
  20226. (when (get-text-property 0 'org-protected line)
  20227. (let (par)
  20228. (when (re-search-backward
  20229. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  20230. (setq par (match-string 1))
  20231. (replace-match "\\2\n"))
  20232. (insert line "\n")
  20233. (while (and lines
  20234. (get-text-property 0 'org-protected (car lines)))
  20235. (insert (pop lines) "\n"))
  20236. (and par (insert "<p>\n")))
  20237. (throw 'nextline nil))
  20238. ;; Horizontal line
  20239. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  20240. (insert "\n<hr/>\n")
  20241. (throw 'nextline nil))
  20242. ;; make targets to anchors
  20243. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  20244. (cond
  20245. ((match-end 2)
  20246. (setq line (replace-match
  20247. (concat "@<a name=\""
  20248. (org-solidify-link-text (match-string 1 line))
  20249. "\">\\nbsp@</a>")
  20250. t t line)))
  20251. ((and org-export-with-toc (equal (string-to-char line) ?*))
  20252. (setq line (replace-match
  20253. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  20254. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  20255. t t line)))
  20256. (t
  20257. (setq line (replace-match
  20258. (concat "@<a name=\""
  20259. (org-solidify-link-text (match-string 1 line))
  20260. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  20261. t t line)))))
  20262. (setq line (org-html-handle-time-stamps line))
  20263. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  20264. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  20265. ;; Also handle sub_superscripts and checkboxes
  20266. (setq line (org-html-expand line))
  20267. ;; Format the links
  20268. (setq start 0)
  20269. (while (string-match org-bracket-link-analytic-regexp line start)
  20270. (setq start (match-beginning 0))
  20271. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  20272. (setq path (match-string 3 line))
  20273. (setq desc1 (if (match-end 5) (match-string 5 line))
  20274. desc2 (if (match-end 2) (concat type ":" path) path)
  20275. descp (and desc1 (not (equal desc1 desc2)))
  20276. desc (or desc1 desc2))
  20277. ;; Make an image out of the description if that is so wanted
  20278. (when (and descp (org-file-image-p desc))
  20279. (save-match-data
  20280. (if (string-match "^file:" desc)
  20281. (setq desc (substring desc (match-end 0)))))
  20282. (setq desc (concat "<img src=\"" desc "\"/>")))
  20283. ;; FIXME: do we need to unescape here somewhere?
  20284. (cond
  20285. ((equal type "internal")
  20286. (setq rpl
  20287. (concat
  20288. "<a href=\"#"
  20289. (org-solidify-link-text
  20290. (save-match-data (org-link-unescape path)) target-alist)
  20291. "\">" desc "</a>")))
  20292. ((member type '("http" "https")) ; FIXME: need to test this.
  20293. ;; standard URL, just check if we need to inline an image
  20294. (if (and (or (eq t org-export-html-inline-images)
  20295. (and org-export-html-inline-images (not descp)))
  20296. (org-file-image-p path))
  20297. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  20298. (setq link (concat type ":" path))
  20299. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  20300. ((member type '("ftp" "mailto" "news"))
  20301. ;; standard URL
  20302. (setq link (concat type ":" path))
  20303. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  20304. ((string= type "file")
  20305. ;; FILE link
  20306. (let* ((filename path)
  20307. (abs-p (file-name-absolute-p filename))
  20308. thefile file-is-image-p search)
  20309. (save-match-data
  20310. (if (string-match "::\\(.*\\)" filename)
  20311. (setq search (match-string 1 filename)
  20312. filename (replace-match "" t nil filename)))
  20313. (setq valid
  20314. (if (functionp link-validate)
  20315. (funcall link-validate filename current-dir)
  20316. t))
  20317. (setq file-is-image-p (org-file-image-p filename))
  20318. (setq thefile (if abs-p (expand-file-name filename) filename))
  20319. (when (and org-export-html-link-org-files-as-html
  20320. (string-match "\\.org$" thefile))
  20321. (setq thefile (concat (substring thefile 0
  20322. (match-beginning 0))
  20323. ".html"))
  20324. (if (and search
  20325. ;; make sure this is can be used as target search
  20326. (not (string-match "^[0-9]*$" search))
  20327. (not (string-match "^\\*" search))
  20328. (not (string-match "^/.*/$" search)))
  20329. (setq thefile (concat thefile "#"
  20330. (org-solidify-link-text
  20331. (org-link-unescape search)))))
  20332. (when (string-match "^file:" desc)
  20333. (setq desc (replace-match "" t t desc))
  20334. (if (string-match "\\.org$" desc)
  20335. (setq desc (replace-match "" t t desc))))))
  20336. (setq rpl (if (and file-is-image-p
  20337. (or (eq t org-export-html-inline-images)
  20338. (and org-export-html-inline-images
  20339. (not descp))))
  20340. (concat "<img src=\"" thefile "\"/>")
  20341. (concat "<a href=\"" thefile "\">" desc "</a>")))
  20342. (if (not valid) (setq rpl desc))))
  20343. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  20344. (setq rpl (concat "<i>&lt;" type ":"
  20345. (save-match-data (org-link-unescape path))
  20346. "&gt;</i>"))))
  20347. (setq line (replace-match rpl t t line)
  20348. start (+ start (length rpl))))
  20349. ;; TODO items
  20350. (if (and (string-match org-todo-line-regexp line)
  20351. (match-beginning 2))
  20352. (setq line
  20353. (concat (substring line 0 (match-beginning 2))
  20354. "<span class=\""
  20355. (if (member (match-string 2 line)
  20356. org-done-keywords)
  20357. "done" "todo")
  20358. "\">" (match-string 2 line)
  20359. "</span>" (substring line (match-end 2)))))
  20360. ;; Does this contain a reference to a footnote?
  20361. (when org-export-with-footnotes
  20362. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  20363. (let ((n (match-string 2 line)))
  20364. (setq line
  20365. (replace-match
  20366. (format
  20367. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  20368. (match-string 1 line) n n n)
  20369. t t line)))))
  20370. (cond
  20371. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  20372. ;; This is a headline
  20373. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  20374. txt (match-string 2 line))
  20375. (if (string-match quote-re0 txt)
  20376. (setq txt (replace-match "" t t txt)))
  20377. (if (<= level (max umax umax-toc))
  20378. (setq head-count (+ head-count 1)))
  20379. (when in-local-list
  20380. ;; Close any local lists before inserting a new header line
  20381. (while local-list-num
  20382. (org-close-li)
  20383. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20384. (pop local-list-num))
  20385. (setq local-list-indent nil
  20386. in-local-list nil))
  20387. (setq first-heading-pos (or first-heading-pos (point)))
  20388. (org-html-level-start level txt umax
  20389. (and org-export-with-toc (<= level umax))
  20390. head-count)
  20391. ;; QUOTES
  20392. (when (string-match quote-re line)
  20393. (insert "<pre>")
  20394. (setq inquote t)))
  20395. ((and org-export-with-tables
  20396. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  20397. (if (not table-open)
  20398. ;; New table starts
  20399. (setq table-open t table-buffer nil table-orig-buffer nil))
  20400. ;; Accumulate lines
  20401. (setq table-buffer (cons line table-buffer)
  20402. table-orig-buffer (cons origline table-orig-buffer))
  20403. (when (or (not lines)
  20404. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  20405. (car lines))))
  20406. (setq table-open nil
  20407. table-buffer (nreverse table-buffer)
  20408. table-orig-buffer (nreverse table-orig-buffer))
  20409. (org-close-par-maybe)
  20410. (insert (org-format-table-html table-buffer table-orig-buffer))))
  20411. (t
  20412. ;; Normal lines
  20413. (when (string-match
  20414. (cond
  20415. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20416. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20417. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20418. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  20419. line)
  20420. (setq ind (org-get-string-indentation line)
  20421. start-is-num (match-beginning 4)
  20422. starter (if (match-beginning 2)
  20423. (substring (match-string 2 line) 0 -1))
  20424. line (substring line (match-beginning 5)))
  20425. (unless (string-match "[^ \t]" line)
  20426. ;; empty line. Pretend indentation is large.
  20427. (setq ind (if org-empty-line-terminates-plain-lists
  20428. 0
  20429. (1+ (or (car local-list-indent) 1)))))
  20430. (setq didclose nil)
  20431. (while (and in-local-list
  20432. (or (and (= ind (car local-list-indent))
  20433. (not starter))
  20434. (< ind (car local-list-indent))))
  20435. (setq didclose t)
  20436. (org-close-li)
  20437. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20438. (pop local-list-num) (pop local-list-indent)
  20439. (setq in-local-list local-list-indent))
  20440. (cond
  20441. ((and starter
  20442. (or (not in-local-list)
  20443. (> ind (car local-list-indent))))
  20444. ;; Start new (level of) list
  20445. (org-close-par-maybe)
  20446. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  20447. (push start-is-num local-list-num)
  20448. (push ind local-list-indent)
  20449. (setq in-local-list t))
  20450. (starter
  20451. ;; continue current list
  20452. (org-close-li)
  20453. (insert "<li>\n"))
  20454. (didclose
  20455. ;; we did close a list, normal text follows: need <p>
  20456. (org-open-par)))
  20457. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  20458. (setq line
  20459. (replace-match
  20460. (if (equal (match-string 1 line) "X")
  20461. "<b>[X]</b>"
  20462. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  20463. t t line))))
  20464. ;; Empty lines start a new paragraph. If hand-formatted lists
  20465. ;; are not fully interpreted, lines starting with "-", "+", "*"
  20466. ;; also start a new paragraph.
  20467. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  20468. ;; Is this the start of a footnote?
  20469. (when org-export-with-footnotes
  20470. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  20471. (org-close-par-maybe)
  20472. (let ((n (match-string 1 line)))
  20473. (setq line (replace-match
  20474. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  20475. ;; Check if the line break needs to be conserved
  20476. (cond
  20477. ((string-match "\\\\\\\\[ \t]*$" line)
  20478. (setq line (replace-match "<br/>" t t line)))
  20479. (org-export-preserve-breaks
  20480. (setq line (concat line "<br/>"))))
  20481. (insert line "\n")))))
  20482. ;; Properly close all local lists and other lists
  20483. (when inquote (insert "</pre>\n"))
  20484. (when in-local-list
  20485. ;; Close any local lists before inserting a new header line
  20486. (while local-list-num
  20487. (org-close-li)
  20488. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  20489. (pop local-list-num))
  20490. (setq local-list-indent nil
  20491. in-local-list nil))
  20492. (org-html-level-start 1 nil umax
  20493. (and org-export-with-toc (<= level umax))
  20494. head-count)
  20495. (unless body-only
  20496. (when (plist-get opt-plist :auto-postamble)
  20497. (when (and org-export-author-info author)
  20498. (insert "<p class=\"author\"> "
  20499. (nth 1 lang-words) ": " author "\n")
  20500. (when email
  20501. (insert "<a href=\"mailto:" email "\">&lt;"
  20502. email "&gt;</a>\n"))
  20503. (insert "</p>\n"))
  20504. (when (and date time org-export-time-stamp-file)
  20505. (insert "<p class=\"date\"> "
  20506. (nth 2 lang-words) ": "
  20507. date " " time "</p>\n")))
  20508. (if org-export-html-with-timestamp
  20509. (insert org-export-html-html-helper-timestamp))
  20510. (insert (or (plist-get opt-plist :postamble) ""))
  20511. (insert "</body>\n</html>\n"))
  20512. (normal-mode)
  20513. (if (eq major-mode default-major-mode) (html-mode))
  20514. ;; insert the table of contents
  20515. (goto-char (point-min))
  20516. (when thetoc
  20517. (if (or (re-search-forward
  20518. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  20519. (re-search-forward
  20520. "\\[TABLE-OF-CONTENTS\\]" nil t))
  20521. (progn
  20522. (goto-char (match-beginning 0))
  20523. (replace-match ""))
  20524. (goto-char first-heading-pos)
  20525. (when (looking-at "\\s-*</p>")
  20526. (goto-char (match-end 0))
  20527. (insert "\n")))
  20528. (mapc 'insert thetoc))
  20529. ;; remove empty paragraphs and lists
  20530. (goto-char (point-min))
  20531. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  20532. (replace-match ""))
  20533. (goto-char (point-min))
  20534. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  20535. (replace-match ""))
  20536. (or to-buffer (save-buffer))
  20537. (goto-char (point-min))
  20538. (message "Exporting... done")
  20539. (if (eq to-buffer 'string)
  20540. (prog1 (buffer-substring (point-min) (point-max))
  20541. (kill-buffer (current-buffer)))
  20542. (current-buffer)))))
  20543. (defvar org-table-colgroup-info nil) ;; FIXME: mode to a better place
  20544. (defun org-format-table-ascii (lines)
  20545. "Format a table for ascii export."
  20546. (if (stringp lines)
  20547. (setq lines (org-split-string lines "\n")))
  20548. (if (not (string-match "^[ \t]*|" (car lines)))
  20549. ;; Table made by table.el - test for spanning
  20550. lines
  20551. ;; A normal org table
  20552. ;; Get rid of hlines at beginning and end
  20553. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20554. (setq lines (nreverse lines))
  20555. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20556. (setq lines (nreverse lines))
  20557. (when org-export-table-remove-special-lines
  20558. ;; Check if the table has a marking column. If yes remove the
  20559. ;; column and the special lines
  20560. (setq lines (org-table-clean-before-export lines)))
  20561. ;; Get rid of the vertical lines except for grouping
  20562. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  20563. rtn line vl1 start)
  20564. (while (setq line (pop lines))
  20565. (if (string-match org-table-hline-regexp line)
  20566. (and (string-match "|\\(.*\\)|" line)
  20567. (setq line (replace-match " \\1" t nil line)))
  20568. (setq start 0 vl1 vl)
  20569. (while (string-match "|" line start)
  20570. (setq start (match-end 0))
  20571. (or (pop vl1) (setq line (replace-match " " t t line)))))
  20572. (push line rtn))
  20573. (nreverse rtn))))
  20574. (defun org-colgroup-info-to-vline-list (info)
  20575. (let (vl new last)
  20576. (while info
  20577. (setq last new new (pop info))
  20578. (if (or (memq last '(:end :startend))
  20579. (memq new '(:start :startend)))
  20580. (push t vl)
  20581. (push nil vl)))
  20582. (setq vl (cons nil (nreverse vl)))))
  20583. (defun org-format-table-html (lines olines)
  20584. "Find out which HTML converter to use and return the HTML code."
  20585. (if (stringp lines)
  20586. (setq lines (org-split-string lines "\n")))
  20587. (if (string-match "^[ \t]*|" (car lines))
  20588. ;; A normal org table
  20589. (org-format-org-table-html lines)
  20590. ;; Table made by table.el - test for spanning
  20591. (let* ((hlines (delq nil (mapcar
  20592. (lambda (x)
  20593. (if (string-match "^[ \t]*\\+-" x) x
  20594. nil))
  20595. lines)))
  20596. (first (car hlines))
  20597. (ll (and (string-match "\\S-+" first)
  20598. (match-string 0 first)))
  20599. (re (concat "^[ \t]*" (regexp-quote ll)))
  20600. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  20601. hlines))))
  20602. (if (and (not spanning)
  20603. (not org-export-prefer-native-exporter-for-tables))
  20604. ;; We can use my own converter with HTML conversions
  20605. (org-format-table-table-html lines)
  20606. ;; Need to use the code generator in table.el, with the original text.
  20607. (org-format-table-table-html-using-table-generate-source olines)))))
  20608. (defun org-format-org-table-html (lines &optional splice)
  20609. "Format a table into HTML."
  20610. ;; Get rid of hlines at beginning and end
  20611. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20612. (setq lines (nreverse lines))
  20613. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20614. (setq lines (nreverse lines))
  20615. (when org-export-table-remove-special-lines
  20616. ;; Check if the table has a marking column. If yes remove the
  20617. ;; column and the special lines
  20618. (setq lines (org-table-clean-before-export lines)))
  20619. (let ((head (and org-export-highlight-first-table-line
  20620. (delq nil (mapcar
  20621. (lambda (x) (string-match "^[ \t]*|-" x))
  20622. (cdr lines)))))
  20623. (nlines 0) fnum i
  20624. tbopen line fields html gr colgropen)
  20625. (if splice (setq head nil))
  20626. (unless splice (push (if head "<thead>" "<tbody>") html))
  20627. (setq tbopen t)
  20628. (while (setq line (pop lines))
  20629. (catch 'next-line
  20630. (if (string-match "^[ \t]*|-" line)
  20631. (progn
  20632. (unless splice
  20633. (push (if head "</thead>" "</tbody>") html)
  20634. (if lines (push "<tbody>" html) (setq tbopen nil)))
  20635. (setq head nil) ;; head ends here, first time around
  20636. ;; ignore this line
  20637. (throw 'next-line t)))
  20638. ;; Break the line into fields
  20639. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  20640. (unless fnum (setq fnum (make-vector (length fields) 0)))
  20641. (setq nlines (1+ nlines) i -1)
  20642. (push (concat "<tr>"
  20643. (mapconcat
  20644. (lambda (x)
  20645. (setq i (1+ i))
  20646. (if (and (< i nlines)
  20647. (string-match org-table-number-regexp x))
  20648. (incf (aref fnum i)))
  20649. (if head
  20650. (concat (car org-export-table-header-tags) x
  20651. (cdr org-export-table-header-tags))
  20652. (concat (car org-export-table-data-tags) x
  20653. (cdr org-export-table-data-tags))))
  20654. fields "")
  20655. "</tr>")
  20656. html)))
  20657. (unless splice (if tbopen (push "</tbody>" html)))
  20658. (unless splice (push "</table>\n" html))
  20659. (setq html (nreverse html))
  20660. (unless splice
  20661. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  20662. (push (mapconcat
  20663. (lambda (x)
  20664. (setq gr (pop org-table-colgroup-info))
  20665. (format "%s<COL align=\"%s\"></COL>%s"
  20666. (if (memq gr '(:start :startend))
  20667. (prog1
  20668. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  20669. (setq colgropen t))
  20670. "")
  20671. (if (> (/ (float x) nlines) org-table-number-fraction)
  20672. "right" "left")
  20673. (if (memq gr '(:end :startend))
  20674. (progn (setq colgropen nil) "</colgroup>")
  20675. "")))
  20676. fnum "")
  20677. html)
  20678. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  20679. (push org-export-html-table-tag html))
  20680. (concat (mapconcat 'identity html "\n") "\n")))
  20681. (defun org-table-clean-before-export (lines)
  20682. "Check if the table has a marking column.
  20683. If yes remove the column and the special lines."
  20684. (setq org-table-colgroup-info nil)
  20685. (if (memq nil
  20686. (mapcar
  20687. (lambda (x) (or (string-match "^[ \t]*|-" x)
  20688. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  20689. lines))
  20690. (progn
  20691. (setq org-table-clean-did-remove-column nil)
  20692. (delq nil
  20693. (mapcar
  20694. (lambda (x)
  20695. (cond
  20696. ((string-match "^[ \t]*| */ *|" x)
  20697. (setq org-table-colgroup-info
  20698. (mapcar (lambda (x)
  20699. (cond ((member x '("<" "&lt;")) :start)
  20700. ((member x '(">" "&gt;")) :end)
  20701. ((member x '("<>" "&lt;&gt;")) :startend)
  20702. (t nil)))
  20703. (org-split-string x "[ \t]*|[ \t]*")))
  20704. nil)
  20705. (t x)))
  20706. lines)))
  20707. (setq org-table-clean-did-remove-column t)
  20708. (delq nil
  20709. (mapcar
  20710. (lambda (x)
  20711. (cond
  20712. ((string-match "^[ \t]*| */ *|" x)
  20713. (setq org-table-colgroup-info
  20714. (mapcar (lambda (x)
  20715. (cond ((member x '("<" "&lt;")) :start)
  20716. ((member x '(">" "&gt;")) :end)
  20717. ((member x '("<>" "&lt;&gt;")) :startend)
  20718. (t nil)))
  20719. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  20720. nil)
  20721. ((string-match "^[ \t]*| *[!_^/] *|" x)
  20722. nil) ; ignore this line
  20723. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  20724. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  20725. ;; remove the first column
  20726. (replace-match "\\1|" t nil x))
  20727. (t (error "This should not happen"))))
  20728. lines))))
  20729. (defun org-format-table-table-html (lines)
  20730. "Format a table generated by table.el into HTML.
  20731. This conversion does *not* use `table-generate-source' from table.el.
  20732. This has the advantage that Org-mode's HTML conversions can be used.
  20733. But it has the disadvantage, that no cell- or row-spanning is allowed."
  20734. (let (line field-buffer
  20735. (head org-export-highlight-first-table-line)
  20736. fields html empty)
  20737. (setq html (concat org-export-html-table-tag "\n"))
  20738. (while (setq line (pop lines))
  20739. (setq empty "&nbsp;")
  20740. (catch 'next-line
  20741. (if (string-match "^[ \t]*\\+-" line)
  20742. (progn
  20743. (if field-buffer
  20744. (progn
  20745. (setq
  20746. html
  20747. (concat
  20748. html
  20749. "<tr>"
  20750. (mapconcat
  20751. (lambda (x)
  20752. (if (equal x "") (setq x empty))
  20753. (if head
  20754. (concat (car org-export-table-header-tags) x
  20755. (cdr org-export-table-header-tags))
  20756. (concat (car org-export-table-data-tags) x
  20757. (cdr org-export-table-data-tags))))
  20758. field-buffer "\n")
  20759. "</tr>\n"))
  20760. (setq head nil)
  20761. (setq field-buffer nil)))
  20762. ;; Ignore this line
  20763. (throw 'next-line t)))
  20764. ;; Break the line into fields and store the fields
  20765. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  20766. (if field-buffer
  20767. (setq field-buffer (mapcar
  20768. (lambda (x)
  20769. (concat x "<br/>" (pop fields)))
  20770. field-buffer))
  20771. (setq field-buffer fields))))
  20772. (setq html (concat html "</table>\n"))
  20773. html))
  20774. (defun org-format-table-table-html-using-table-generate-source (lines)
  20775. "Format a table into html, using `table-generate-source' from table.el.
  20776. This has the advantage that cell- or row-spanning is allowed.
  20777. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  20778. (require 'table)
  20779. (with-current-buffer (get-buffer-create " org-tmp1 ")
  20780. (erase-buffer)
  20781. (insert (mapconcat 'identity lines "\n"))
  20782. (goto-char (point-min))
  20783. (if (not (re-search-forward "|[^+]" nil t))
  20784. (error "Error processing table"))
  20785. (table-recognize-table)
  20786. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  20787. (table-generate-source 'html " org-tmp2 ")
  20788. (set-buffer " org-tmp2 ")
  20789. (buffer-substring (point-min) (point-max))))
  20790. (defun org-html-handle-time-stamps (s)
  20791. "Format time stamps in string S, or remove them."
  20792. (catch 'exit
  20793. (let (r b)
  20794. (while (string-match org-maybe-keyword-time-regexp s)
  20795. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  20796. ;; never export CLOCK
  20797. (throw 'exit ""))
  20798. (or b (setq b (substring s 0 (match-beginning 0))))
  20799. (if (not org-export-with-timestamps)
  20800. (setq r (concat r (substring s 0 (match-beginning 0)))
  20801. s (substring s (match-end 0)))
  20802. (setq r (concat
  20803. r (substring s 0 (match-beginning 0))
  20804. (if (match-end 1)
  20805. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  20806. (match-string 1 s)))
  20807. (format " @<span class=\"timestamp\">%s@</span>"
  20808. (substring
  20809. (org-translate-time (match-string 3 s)) 1 -1)))
  20810. s (substring s (match-end 0)))))
  20811. ;; Line break if line started and ended with time stamp stuff
  20812. (if (not r)
  20813. s
  20814. (setq r (concat r s))
  20815. (unless (string-match "\\S-" (concat b s))
  20816. (setq r (concat r "@<br/>")))
  20817. r))))
  20818. (defun org-html-protect (s)
  20819. ;; convert & to &amp;, < to &lt; and > to &gt;
  20820. (let ((start 0))
  20821. (while (string-match "&" s start)
  20822. (setq s (replace-match "&amp;" t t s)
  20823. start (1+ (match-beginning 0))))
  20824. (while (string-match "<" s)
  20825. (setq s (replace-match "&lt;" t t s)))
  20826. (while (string-match ">" s)
  20827. (setq s (replace-match "&gt;" t t s))))
  20828. s)
  20829. (defun org-export-cleanup-toc-line (s)
  20830. "Remove tags and time staps from lines going into the toc."
  20831. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  20832. (setq s (replace-match "" t t s)))
  20833. (when org-export-remove-timestamps-from-toc
  20834. (while (string-match org-maybe-keyword-time-regexp s)
  20835. (setq s (replace-match "" t t s))))
  20836. (while (string-match org-bracket-link-regexp s)
  20837. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  20838. t t s)))
  20839. s)
  20840. (defun org-html-expand (string)
  20841. "Prepare STRING for HTML export. Applies all active conversions.
  20842. If there are links in the string, don't modify these."
  20843. (let* (m s l res)
  20844. (while (setq m (string-match org-bracket-link-regexp string))
  20845. (setq s (substring string 0 m)
  20846. l (match-string 0 string)
  20847. string (substring string (match-end 0)))
  20848. (push (org-html-do-expand s) res)
  20849. (push l res))
  20850. (push (org-html-do-expand string) res)
  20851. (apply 'concat (nreverse res))))
  20852. (defun org-html-do-expand (s)
  20853. "Apply all active conversions to translate special ASCII to HTML."
  20854. (setq s (org-html-protect s))
  20855. (if org-export-html-expand
  20856. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  20857. (setq s (replace-match "<\\1>" t nil s))))
  20858. (if org-export-with-emphasize
  20859. (setq s (org-export-html-convert-emphasize s)))
  20860. (if org-export-with-sub-superscripts
  20861. (setq s (org-export-html-convert-sub-super s)))
  20862. (if org-export-with-TeX-macros
  20863. (let ((start 0) wd ass)
  20864. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  20865. (setq wd (match-string 1 s))
  20866. (if (setq ass (assoc wd org-html-entities))
  20867. (setq s (replace-match (or (cdr ass)
  20868. (concat "&" (car ass) ";"))
  20869. t t s))
  20870. (setq start (+ start (length wd)))))))
  20871. s)
  20872. (defun org-create-multibrace-regexp (left right n)
  20873. "Create a regular expression which will match a balanced sexp.
  20874. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  20875. as single character strings.
  20876. The regexp returned will match the entire expression including the
  20877. delimiters. It will also define a single group which contains the
  20878. match except for the outermost delimiters. The maximum depth of
  20879. stacked delimiters is N. Escaping delimiters is not possible."
  20880. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  20881. (or "\\|")
  20882. (re nothing)
  20883. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  20884. (while (> n 1)
  20885. (setq n (1- n)
  20886. re (concat re or next)
  20887. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  20888. (concat left "\\(" re "\\)" right)))
  20889. (defvar org-match-substring-regexp
  20890. (concat
  20891. "\\([^\\]\\)\\([_^]\\)\\("
  20892. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  20893. "\\|"
  20894. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  20895. "\\|"
  20896. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  20897. "The regular expression matching a sub- or superscript.")
  20898. ;(let ((s "a\\_b"))
  20899. ; (and (string-match org-match-substring-regexp s)
  20900. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  20901. (defun org-export-html-convert-sub-super (string)
  20902. "Convert sub- and superscripts in STRING to HTML."
  20903. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  20904. (while (string-match org-match-substring-regexp string s)
  20905. (if (and requireb (match-end 8))
  20906. (setq s (match-end 2))
  20907. (setq s (match-end 1)
  20908. key (if (string= (match-string 2 string) "_") "sub" "sup")
  20909. c (or (match-string 8 string)
  20910. (match-string 6 string)
  20911. (match-string 5 string))
  20912. string (replace-match
  20913. (concat (match-string 1 string)
  20914. "<" key ">" c "</" key ">")
  20915. t t string))))
  20916. (while (string-match "\\\\\\([_^]\\)" string)
  20917. (setq string (replace-match (match-string 1 string) t t string)))
  20918. string))
  20919. (defun org-export-html-convert-emphasize (string)
  20920. "Apply emphasis."
  20921. (let ((s 0))
  20922. (while (string-match org-emph-re string s)
  20923. (if (not (equal
  20924. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  20925. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  20926. (setq string (replace-match
  20927. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  20928. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  20929. "\\5") t nil string))
  20930. (setq s (1+ s))))
  20931. string))
  20932. (defvar org-par-open nil)
  20933. (defun org-open-par ()
  20934. "Insert <p>, but first close previous paragraph if any."
  20935. (org-close-par-maybe)
  20936. (insert "\n<p>")
  20937. (setq org-par-open t))
  20938. (defun org-close-par-maybe ()
  20939. "Close paragraph if there is one open."
  20940. (when org-par-open
  20941. (insert "</p>")
  20942. (setq org-par-open nil)))
  20943. (defun org-close-li ()
  20944. "Close <li> if necessary."
  20945. (org-close-par-maybe)
  20946. (insert "</li>\n"))
  20947. (defvar body-only) ; dynamically scoped into this.
  20948. (defun org-html-level-start (level title umax with-toc head-count)
  20949. "Insert a new level in HTML export.
  20950. When TITLE is nil, just close all open levels."
  20951. (org-close-par-maybe)
  20952. (let ((l (1+ (max level umax))))
  20953. (while (<= l org-level-max)
  20954. (if (aref org-levels-open (1- l))
  20955. (progn
  20956. (org-html-level-close l)
  20957. (aset org-levels-open (1- l) nil)))
  20958. (setq l (1+ l)))
  20959. (when title
  20960. ;; If title is nil, this means this function is called to close
  20961. ;; all levels, so the rest is done only if title is given
  20962. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  20963. (setq title (replace-match
  20964. (if org-export-with-tags
  20965. (save-match-data
  20966. (concat
  20967. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  20968. (mapconcat 'identity (org-split-string
  20969. (match-string 1 title) ":")
  20970. "&nbsp;")
  20971. "</span>"))
  20972. "")
  20973. t t title)))
  20974. (if (> level umax)
  20975. (progn
  20976. (if (aref org-levels-open (1- level))
  20977. (progn
  20978. (org-close-li)
  20979. (insert "<li>" title "<br/>\n"))
  20980. (aset org-levels-open (1- level) t)
  20981. (org-close-par-maybe)
  20982. (insert "<ul>\n<li>" title "<br/>\n")))
  20983. (if (and org-export-with-section-numbers (not body-only))
  20984. (setq title (concat (org-section-number level) " " title)))
  20985. (setq level (+ level org-export-html-toplevel-hlevel -1))
  20986. (if with-toc
  20987. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  20988. level head-count title level))
  20989. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  20990. (org-open-par)))))
  20991. (defun org-html-level-close (&rest args)
  20992. "Terminate one level in HTML export."
  20993. (org-close-li)
  20994. (insert "</ul>\n"))
  20995. ;;; iCalendar export
  20996. ;;;###autoload
  20997. (defun org-export-icalendar-this-file ()
  20998. "Export current file as an iCalendar file.
  20999. The iCalendar file will be located in the same directory as the Org-mode
  21000. file, but with extension `.ics'."
  21001. (interactive)
  21002. (org-export-icalendar nil buffer-file-name))
  21003. ;;;###autoload
  21004. (defun org-export-icalendar-all-agenda-files ()
  21005. "Export all files in `org-agenda-files' to iCalendar .ics files.
  21006. Each iCalendar file will be located in the same directory as the Org-mode
  21007. file, but with extension `.ics'."
  21008. (interactive)
  21009. (apply 'org-export-icalendar nil (org-agenda-files t)))
  21010. ;;;###autoload
  21011. (defun org-export-icalendar-combine-agenda-files ()
  21012. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  21013. The file is stored under the name `org-combined-agenda-icalendar-file'."
  21014. (interactive)
  21015. (apply 'org-export-icalendar t (org-agenda-files t)))
  21016. (defun org-export-icalendar (combine &rest files)
  21017. "Create iCalendar files for all elements of FILES.
  21018. If COMBINE is non-nil, combine all calendar entries into a single large
  21019. file and store it under the name `org-combined-agenda-icalendar-file'."
  21020. (save-excursion
  21021. (org-prepare-agenda-buffers files)
  21022. (let* ((dir (org-export-directory
  21023. :ical (list :publishing-directory
  21024. org-export-publishing-directory)))
  21025. file ical-file ical-buffer category started org-agenda-new-buffers)
  21026. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  21027. (when combine
  21028. (setq ical-file
  21029. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  21030. org-combined-agenda-icalendar-file
  21031. (expand-file-name org-combined-agenda-icalendar-file dir))
  21032. ical-buffer (org-get-agenda-file-buffer ical-file))
  21033. (set-buffer ical-buffer) (erase-buffer))
  21034. (while (setq file (pop files))
  21035. (catch 'nextfile
  21036. (org-check-agenda-file file)
  21037. (set-buffer (org-get-agenda-file-buffer file))
  21038. (unless combine
  21039. (setq ical-file (concat (file-name-as-directory dir)
  21040. (file-name-sans-extension
  21041. (file-name-nondirectory buffer-file-name))
  21042. ".ics"))
  21043. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  21044. (with-current-buffer ical-buffer (erase-buffer)))
  21045. (setq category (or org-category
  21046. (file-name-sans-extension
  21047. (file-name-nondirectory buffer-file-name))))
  21048. (if (symbolp category) (setq category (symbol-name category)))
  21049. (let ((standard-output ical-buffer))
  21050. (if combine
  21051. (and (not started) (setq started t)
  21052. (org-start-icalendar-file org-icalendar-combined-name))
  21053. (org-start-icalendar-file category))
  21054. (org-print-icalendar-entries combine)
  21055. (when (or (and combine (not files)) (not combine))
  21056. (org-finish-icalendar-file)
  21057. (set-buffer ical-buffer)
  21058. (save-buffer)
  21059. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  21060. (org-release-buffers org-agenda-new-buffers))))
  21061. (defvar org-after-save-iCalendar-file-hook nil
  21062. "Hook run after an iCalendar file has been saved.
  21063. The iCalendar buffer is still current when this hook is run.
  21064. A good way to use this is to tell a desktop calenndar application to re-read
  21065. the iCalendar file.")
  21066. (defun org-print-icalendar-entries (&optional combine)
  21067. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  21068. When COMBINE is non nil, add the category to each line."
  21069. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  21070. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  21071. (org-category-table (org-get-category-table))
  21072. (dts (org-ical-ts-to-string
  21073. (format-time-string (cdr org-time-stamp-formats) (current-time))
  21074. "DTSTART"))
  21075. hd ts ts2 state status (inc t) pos b sexp rrule
  21076. scheduledp deadlinep tmp pri category
  21077. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  21078. (save-excursion
  21079. (goto-char (point-min))
  21080. (while (re-search-forward re1 nil t)
  21081. (catch :skip
  21082. (org-agenda-skip)
  21083. (setq pos (match-beginning 0)
  21084. ts (match-string 0)
  21085. inc t
  21086. hd (org-get-heading)
  21087. category (org-get-category))
  21088. (if (looking-at re2)
  21089. (progn
  21090. (goto-char (match-end 0))
  21091. (setq ts2 (match-string 1) inc nil))
  21092. (setq tmp (buffer-substring (max (point-min)
  21093. (- pos org-ds-keyword-length))
  21094. pos)
  21095. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  21096. (progn
  21097. (setq inc nil)
  21098. (replace-match "\\1" t nil ts))
  21099. ts)
  21100. deadlinep (string-match org-deadline-regexp tmp)
  21101. scheduledp (string-match org-scheduled-regexp tmp)
  21102. ;; donep (org-entry-is-done-p)
  21103. ))
  21104. (if (or (string-match org-tr-regexp hd)
  21105. (string-match org-ts-regexp hd))
  21106. (setq hd (replace-match "" t t hd)))
  21107. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  21108. (setq rrule
  21109. (concat "\nRRULE:FREQ="
  21110. (cdr (assoc
  21111. (match-string 2 ts)
  21112. '(("d" . "DAILY")("w" . "WEEKLY")
  21113. ("m" . "MONTHLY")("y" . "YEARLY"))))
  21114. ";INTERVAL=" (match-string 1 ts)))
  21115. (setq rrule ""))
  21116. (if (string-match org-bracket-link-regexp hd)
  21117. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  21118. (match-string 1 hd))
  21119. t t hd)))
  21120. (if deadlinep (setq hd (concat "DL: " hd)))
  21121. (if scheduledp (setq hd (concat "S: " hd)))
  21122. (if (string-match "\\`<%%" ts)
  21123. (with-current-buffer sexp-buffer
  21124. (insert (substring ts 1 -1) " " hd "\n"))
  21125. (princ (format "BEGIN:VEVENT
  21126. %s
  21127. %s%s
  21128. SUMMARY:%s
  21129. CATEGORIES:%s
  21130. END:VEVENT\n"
  21131. (org-ical-ts-to-string ts "DTSTART")
  21132. (org-ical-ts-to-string ts2 "DTEND" inc)
  21133. rrule hd category)))))
  21134. (when (and org-icalendar-include-sexps
  21135. (condition-case nil (require 'icalendar) (error nil))
  21136. (fboundp 'icalendar-export-region))
  21137. ;; Get all the literal sexps
  21138. (goto-char (point-min))
  21139. (while (re-search-forward "^&?%%(" nil t)
  21140. (catch :skip
  21141. (org-agenda-skip)
  21142. (setq b (match-beginning 0))
  21143. (goto-char (1- (match-end 0)))
  21144. (forward-sexp 1)
  21145. (end-of-line 1)
  21146. (setq sexp (buffer-substring b (point)))
  21147. (with-current-buffer sexp-buffer
  21148. (insert sexp "\n"))
  21149. (princ (org-diary-to-ical-string sexp-buffer)))))
  21150. (when org-icalendar-include-todo
  21151. (goto-char (point-min))
  21152. (while (re-search-forward org-todo-line-regexp nil t)
  21153. (catch :skip
  21154. (org-agenda-skip)
  21155. (setq state (match-string 2))
  21156. (setq status (if (member state org-done-keywords)
  21157. "COMPLETED" "NEEDS-ACTION"))
  21158. (when (and state
  21159. (or (not (member state org-done-keywords))
  21160. (eq org-icalendar-include-todo 'all))
  21161. (not (member org-archive-tag (org-get-tags-at)))
  21162. )
  21163. (setq hd (match-string 3))
  21164. (if (string-match org-bracket-link-regexp hd)
  21165. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  21166. (match-string 1 hd))
  21167. t t hd)))
  21168. (if (string-match org-priority-regexp hd)
  21169. (setq pri (string-to-char (match-string 2 hd))
  21170. hd (concat (substring hd 0 (match-beginning 1))
  21171. (substring hd (match-end 1))))
  21172. (setq pri org-default-priority))
  21173. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  21174. (- org-lowest-priority org-highest-priority))))))
  21175. (princ (format "BEGIN:VTODO
  21176. %s
  21177. SUMMARY:%s
  21178. CATEGORIES:%s
  21179. SEQUENCE:1
  21180. PRIORITY:%d
  21181. STATUS:%s
  21182. END:VTODO\n"
  21183. dts hd category pri status)))))))))
  21184. (defun org-start-icalendar-file (name)
  21185. "Start an iCalendar file by inserting the header."
  21186. (let ((user user-full-name)
  21187. (name (or name "unknown"))
  21188. (timezone (cadr (current-time-zone))))
  21189. (princ
  21190. (format "BEGIN:VCALENDAR
  21191. VERSION:2.0
  21192. X-WR-CALNAME:%s
  21193. PRODID:-//%s//Emacs with Org-mode//EN
  21194. X-WR-TIMEZONE:%s
  21195. CALSCALE:GREGORIAN\n" name user timezone))))
  21196. (defun org-finish-icalendar-file ()
  21197. "Finish an iCalendar file by inserting the END statement."
  21198. (princ "END:VCALENDAR\n"))
  21199. (defun org-ical-ts-to-string (s keyword &optional inc)
  21200. "Take a time string S and convert it to iCalendar format.
  21201. KEYWORD is added in front, to make a complete line like DTSTART....
  21202. When INC is non-nil, increase the hour by two (if time string contains
  21203. a time), or the day by one (if it does not contain a time)."
  21204. (let ((t1 (org-parse-time-string s 'nodefault))
  21205. t2 fmt have-time time)
  21206. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  21207. (setq t2 t1 have-time t)
  21208. (setq t2 (org-parse-time-string s)))
  21209. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  21210. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  21211. (when inc
  21212. (if have-time
  21213. (if org-agenda-default-appointment-duration
  21214. (setq mi (+ org-agenda-default-appointment-duration mi))
  21215. (setq h (+ 2 h)))
  21216. (setq d (1+ d))))
  21217. (setq time (encode-time s mi h d m y)))
  21218. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  21219. (concat keyword (format-time-string fmt time))))
  21220. ;;; XOXO export
  21221. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  21222. (with-current-buffer buffer
  21223. (apply 'insert output)))
  21224. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  21225. (defun org-export-as-xoxo (&optional buffer)
  21226. "Export the org buffer as XOXO.
  21227. The XOXO buffer is named *xoxo-<source buffer name>*"
  21228. (interactive (list (current-buffer)))
  21229. ;; A quickie abstraction
  21230. ;; Output everything as XOXO
  21231. (with-current-buffer (get-buffer buffer)
  21232. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  21233. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21234. (org-infile-export-plist)))
  21235. (filename (concat (file-name-as-directory
  21236. (org-export-directory :xoxo opt-plist))
  21237. (file-name-sans-extension
  21238. (file-name-nondirectory buffer-file-name))
  21239. ".html"))
  21240. (out (find-file-noselect filename))
  21241. (last-level 1)
  21242. (hanging-li nil))
  21243. ;; Check the output buffer is empty.
  21244. (with-current-buffer out (erase-buffer))
  21245. ;; Kick off the output
  21246. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  21247. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  21248. (let* ((hd (match-string-no-properties 1))
  21249. (level (length hd))
  21250. (text (concat
  21251. (match-string-no-properties 2)
  21252. (save-excursion
  21253. (goto-char (match-end 0))
  21254. (let ((str ""))
  21255. (catch 'loop
  21256. (while 't
  21257. (forward-line)
  21258. (if (looking-at "^[ \t]\\(.*\\)")
  21259. (setq str (concat str (match-string-no-properties 1)))
  21260. (throw 'loop str)))))))))
  21261. ;; Handle level rendering
  21262. (cond
  21263. ((> level last-level)
  21264. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  21265. ((< level last-level)
  21266. (dotimes (- (- last-level level) 1)
  21267. (if hanging-li
  21268. (org-export-as-xoxo-insert-into out "</li>\n"))
  21269. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21270. (when hanging-li
  21271. (org-export-as-xoxo-insert-into out "</li>\n")
  21272. (setq hanging-li nil)))
  21273. ((equal level last-level)
  21274. (if hanging-li
  21275. (org-export-as-xoxo-insert-into out "</li>\n")))
  21276. )
  21277. (setq last-level level)
  21278. ;; And output the new li
  21279. (setq hanging-li 't)
  21280. (if (equal ?+ (elt text 0))
  21281. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  21282. (org-export-as-xoxo-insert-into out "<li>" text))))
  21283. ;; Finally finish off the ol
  21284. (dotimes (- last-level 1)
  21285. (if hanging-li
  21286. (org-export-as-xoxo-insert-into out "</li>\n"))
  21287. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21288. ;; Finish the buffer off and clean it up.
  21289. (switch-to-buffer-other-window out)
  21290. (indent-region (point-min) (point-max) nil)
  21291. (save-buffer)
  21292. (goto-char (point-min))
  21293. )))
  21294. ;;;; Key bindings
  21295. ;; Make `C-c C-x' a prefix key
  21296. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  21297. ;; TAB key with modifiers
  21298. (org-defkey org-mode-map "\C-i" 'org-cycle)
  21299. (org-defkey org-mode-map [(tab)] 'org-cycle)
  21300. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  21301. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  21302. (org-defkey org-mode-map "\M-\t" 'org-complete)
  21303. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  21304. ;; The following line is necessary under Suse GNU/Linux
  21305. (unless (featurep 'xemacs)
  21306. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  21307. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  21308. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  21309. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  21310. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  21311. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  21312. ;; Cursor keys with modifiers
  21313. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  21314. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  21315. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  21316. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  21317. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  21318. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  21319. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  21320. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  21321. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  21322. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  21323. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  21324. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  21325. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  21326. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  21327. ;;; Extra keys for tty access.
  21328. ;; We only set them when really needed because otherwise the
  21329. ;; menus don't show the simple keys
  21330. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  21331. (not window-system))
  21332. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  21333. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  21334. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  21335. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  21336. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  21337. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  21338. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  21339. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  21340. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  21341. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  21342. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  21343. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  21344. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  21345. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  21346. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  21347. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  21348. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  21349. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  21350. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  21351. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  21352. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  21353. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  21354. ;; All the other keys
  21355. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  21356. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  21357. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  21358. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  21359. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  21360. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  21361. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  21362. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  21363. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  21364. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  21365. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  21366. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  21367. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  21368. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  21369. (org-defkey org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  21370. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  21371. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  21372. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  21373. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  21374. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  21375. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  21376. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  21377. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  21378. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  21379. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  21380. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  21381. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  21382. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  21383. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  21384. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  21385. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  21386. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  21387. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  21388. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  21389. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  21390. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  21391. (org-defkey org-mode-map "\C-c^" 'org-sort)
  21392. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  21393. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  21394. (org-defkey org-mode-map "\C-m" 'org-return)
  21395. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  21396. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  21397. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  21398. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  21399. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  21400. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  21401. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  21402. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  21403. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  21404. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  21405. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  21406. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  21407. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  21408. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  21409. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  21410. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  21411. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  21412. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  21413. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  21414. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  21415. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  21416. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  21417. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  21418. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  21419. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  21420. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  21421. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  21422. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  21423. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  21424. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  21425. (when (featurep 'xemacs)
  21426. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  21427. (defsubst org-table-p () (org-at-table-p))
  21428. (defun org-self-insert-command (N)
  21429. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  21430. If the cursor is in a table looking at whitespace, the whitespace is
  21431. overwritten, and the table is not marked as requiring realignment."
  21432. (interactive "p")
  21433. (if (and (org-table-p)
  21434. (progn
  21435. ;; check if we blank the field, and if that triggers align
  21436. (and org-table-auto-blank-field
  21437. (member last-command
  21438. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  21439. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  21440. ;; got extra space, this field does not determine column width
  21441. (let (org-table-may-need-update) (org-table-blank-field))
  21442. ;; no extra space, this field may determine column width
  21443. (org-table-blank-field)))
  21444. t)
  21445. (eq N 1)
  21446. (looking-at "[^|\n]* |"))
  21447. (let (org-table-may-need-update)
  21448. (goto-char (1- (match-end 0)))
  21449. (delete-backward-char 1)
  21450. (goto-char (match-beginning 0))
  21451. (self-insert-command N))
  21452. (setq org-table-may-need-update t)
  21453. (self-insert-command N)
  21454. (org-fix-tags-on-the-fly)))
  21455. (defun org-fix-tags-on-the-fly ()
  21456. (when (and (equal (char-after (point-at-bol)) ?*)
  21457. (org-on-heading-p))
  21458. (org-align-tags-here org-tags-column)))
  21459. (defun org-delete-backward-char (N)
  21460. "Like `delete-backward-char', insert whitespace at field end in tables.
  21461. When deleting backwards, in tables this function will insert whitespace in
  21462. front of the next \"|\" separator, to keep the table aligned. The table will
  21463. still be marked for re-alignment if the field did fill the entire column,
  21464. because, in this case the deletion might narrow the column."
  21465. (interactive "p")
  21466. (if (and (org-table-p)
  21467. (eq N 1)
  21468. (string-match "|" (buffer-substring (point-at-bol) (point)))
  21469. (looking-at ".*?|"))
  21470. (let ((pos (point))
  21471. (noalign (looking-at "[^|\n\r]* |"))
  21472. (c org-table-may-need-update))
  21473. (backward-delete-char N)
  21474. (skip-chars-forward "^|")
  21475. (insert " ")
  21476. (goto-char (1- pos))
  21477. ;; noalign: if there were two spaces at the end, this field
  21478. ;; does not determine the width of the column.
  21479. (if noalign (setq org-table-may-need-update c)))
  21480. (backward-delete-char N)
  21481. (org-fix-tags-on-the-fly)))
  21482. (defun org-delete-char (N)
  21483. "Like `delete-char', but insert whitespace at field end in tables.
  21484. When deleting characters, in tables this function will insert whitespace in
  21485. front of the next \"|\" separator, to keep the table aligned. The table will
  21486. still be marked for re-alignment if the field did fill the entire column,
  21487. because, in this case the deletion might narrow the column."
  21488. (interactive "p")
  21489. (if (and (org-table-p)
  21490. (not (bolp))
  21491. (not (= (char-after) ?|))
  21492. (eq N 1))
  21493. (if (looking-at ".*?|")
  21494. (let ((pos (point))
  21495. (noalign (looking-at "[^|\n\r]* |"))
  21496. (c org-table-may-need-update))
  21497. (replace-match (concat
  21498. (substring (match-string 0) 1 -1)
  21499. " |"))
  21500. (goto-char pos)
  21501. ;; noalign: if there were two spaces at the end, this field
  21502. ;; does not determine the width of the column.
  21503. (if noalign (setq org-table-may-need-update c)))
  21504. (delete-char N))
  21505. (delete-char N)
  21506. (org-fix-tags-on-the-fly)))
  21507. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  21508. (put 'org-self-insert-command 'delete-selection t)
  21509. (put 'orgtbl-self-insert-command 'delete-selection t)
  21510. (put 'org-delete-char 'delete-selection 'supersede)
  21511. (put 'org-delete-backward-char 'delete-selection 'supersede)
  21512. ;; Make `flyspell-mode' delay after some commands
  21513. (put 'org-self-insert-command 'flyspell-delayed t)
  21514. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  21515. (put 'org-delete-char 'flyspell-delayed t)
  21516. (put 'org-delete-backward-char 'flyspell-delayed t)
  21517. ;; How to do this: Measure non-white length of current string
  21518. ;; If equal to column width, we should realign.
  21519. (defun org-remap (map &rest commands)
  21520. "In MAP, remap the functions given in COMMANDS.
  21521. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  21522. (let (new old)
  21523. (while commands
  21524. (setq old (pop commands) new (pop commands))
  21525. (if (fboundp 'command-remapping)
  21526. (org-defkey map (vector 'remap old) new)
  21527. (substitute-key-definition old new map global-map)))))
  21528. (when (eq org-enable-table-editor 'optimized)
  21529. ;; If the user wants maximum table support, we need to hijack
  21530. ;; some standard editing functions
  21531. (org-remap org-mode-map
  21532. 'self-insert-command 'org-self-insert-command
  21533. 'delete-char 'org-delete-char
  21534. 'delete-backward-char 'org-delete-backward-char)
  21535. (org-defkey org-mode-map "|" 'org-force-self-insert))
  21536. (defun org-shiftcursor-error ()
  21537. "Throw an error because Shift-Cursor command was applied in wrong context."
  21538. (error "This command is active in special context like tables, headlines or timestamps"))
  21539. (defun org-shifttab (&optional arg)
  21540. "Global visibility cycling or move to previous table field.
  21541. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  21542. on context.
  21543. See the individual commands for more information."
  21544. (interactive "P")
  21545. (cond
  21546. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  21547. (arg (message "Content view to level: ")
  21548. (org-content (prefix-numeric-value arg))
  21549. (setq org-cycle-global-status 'overview))
  21550. (t (call-interactively 'org-global-cycle))))
  21551. (defun org-shiftmetaleft ()
  21552. "Promote subtree or delete table column.
  21553. Calls `org-promote-subtree', `org-outdent-item',
  21554. or `org-table-delete-column', depending on context.
  21555. See the individual commands for more information."
  21556. (interactive)
  21557. (cond
  21558. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  21559. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  21560. ((org-at-item-p) (call-interactively 'org-outdent-item))
  21561. (t (org-shiftcursor-error))))
  21562. (defun org-shiftmetaright ()
  21563. "Demote subtree or insert table column.
  21564. Calls `org-demote-subtree', `org-indent-item',
  21565. or `org-table-insert-column', depending on context.
  21566. See the individual commands for more information."
  21567. (interactive)
  21568. (cond
  21569. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  21570. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  21571. ((org-at-item-p) (call-interactively 'org-indent-item))
  21572. (t (org-shiftcursor-error))))
  21573. (defun org-shiftmetaup (&optional arg)
  21574. "Move subtree up or kill table row.
  21575. Calls `org-move-subtree-up' or `org-table-kill-row' or
  21576. `org-move-item-up' depending on context. See the individual commands
  21577. for more information."
  21578. (interactive "P")
  21579. (cond
  21580. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  21581. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  21582. ((org-at-item-p) (call-interactively 'org-move-item-up))
  21583. (t (org-shiftcursor-error))))
  21584. (defun org-shiftmetadown (&optional arg)
  21585. "Move subtree down or insert table row.
  21586. Calls `org-move-subtree-down' or `org-table-insert-row' or
  21587. `org-move-item-down', depending on context. See the individual
  21588. commands for more information."
  21589. (interactive "P")
  21590. (cond
  21591. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  21592. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  21593. ((org-at-item-p) (call-interactively 'org-move-item-down))
  21594. (t (org-shiftcursor-error))))
  21595. (defun org-metaleft (&optional arg)
  21596. "Promote heading or move table column to left.
  21597. Calls `org-do-promote' or `org-table-move-column', depending on context.
  21598. With no specific context, calls the Emacs default `backward-word'.
  21599. See the individual commands for more information."
  21600. (interactive "P")
  21601. (cond
  21602. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  21603. ((or (org-on-heading-p) (org-region-active-p))
  21604. (call-interactively 'org-do-promote))
  21605. ((org-at-item-p) (call-interactively 'org-outdent-item))
  21606. (t (call-interactively 'backward-word))))
  21607. (defun org-metaright (&optional arg)
  21608. "Demote subtree or move table column to right.
  21609. Calls `org-do-demote' or `org-table-move-column', depending on context.
  21610. With no specific context, calls the Emacs default `forward-word'.
  21611. See the individual commands for more information."
  21612. (interactive "P")
  21613. (cond
  21614. ((org-at-table-p) (call-interactively 'org-table-move-column))
  21615. ((or (org-on-heading-p) (org-region-active-p))
  21616. (call-interactively 'org-do-demote))
  21617. ((org-at-item-p) (call-interactively 'org-indent-item))
  21618. (t (call-interactively 'forward-word))))
  21619. (defun org-metaup (&optional arg)
  21620. "Move subtree up or move table row up.
  21621. Calls `org-move-subtree-up' or `org-table-move-row' or
  21622. `org-move-item-up', depending on context. See the individual commands
  21623. for more information."
  21624. (interactive "P")
  21625. (cond
  21626. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  21627. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  21628. ((org-at-item-p) (call-interactively 'org-move-item-up))
  21629. (t (org-shiftcursor-error))))
  21630. (defun org-metadown (&optional arg)
  21631. "Move subtree down or move table row down.
  21632. Calls `org-move-subtree-down' or `org-table-move-row' or
  21633. `org-move-item-down', depending on context. See the individual
  21634. commands for more information."
  21635. (interactive "P")
  21636. (cond
  21637. ((org-at-table-p) (call-interactively 'org-table-move-row))
  21638. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  21639. ((org-at-item-p) (call-interactively 'org-move-item-down))
  21640. (t (org-shiftcursor-error))))
  21641. (defun org-shiftup (&optional arg)
  21642. "Increase item in timestamp or increase priority of current headline.
  21643. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  21644. depending on context. See the individual commands for more information."
  21645. (interactive "P")
  21646. (cond
  21647. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  21648. ((org-on-heading-p) (call-interactively 'org-priority-up))
  21649. ((org-at-item-p) (call-interactively 'org-previous-item))
  21650. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  21651. (defun org-shiftdown (&optional arg)
  21652. "Decrease item in timestamp or decrease priority of current headline.
  21653. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  21654. depending on context. See the individual commands for more information."
  21655. (interactive "P")
  21656. (cond
  21657. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  21658. ((org-on-heading-p) (call-interactively 'org-priority-down))
  21659. (t (call-interactively 'org-next-item))))
  21660. (defun org-shiftright ()
  21661. "Next TODO keyword or timestamp one day later, depending on context."
  21662. (interactive)
  21663. (cond
  21664. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  21665. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  21666. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  21667. (t (org-shiftcursor-error))))
  21668. (defun org-shiftleft ()
  21669. "Previous TODO keyword or timestamp one day earlier, depending on context."
  21670. (interactive)
  21671. (cond
  21672. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  21673. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  21674. ((org-at-property-p)
  21675. (call-interactively 'org-property-previous-allowed-value))
  21676. (t (org-shiftcursor-error))))
  21677. (defun org-shiftcontrolright ()
  21678. "Switch to next TODO set."
  21679. (interactive)
  21680. (cond
  21681. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  21682. (t (org-shiftcursor-error))))
  21683. (defun org-shiftcontrolleft ()
  21684. "Switch to previous TODO set."
  21685. (interactive)
  21686. (cond
  21687. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  21688. (t (org-shiftcursor-error))))
  21689. (defun org-ctrl-c-ret ()
  21690. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  21691. (interactive)
  21692. (cond
  21693. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  21694. (t (call-interactively 'org-insert-heading))))
  21695. (defun org-copy-special ()
  21696. "Copy region in table or copy current subtree.
  21697. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  21698. See the individual commands for more information."
  21699. (interactive)
  21700. (call-interactively
  21701. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  21702. (defun org-cut-special ()
  21703. "Cut region in table or cut current subtree.
  21704. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  21705. See the individual commands for more information."
  21706. (interactive)
  21707. (call-interactively
  21708. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  21709. (defun org-paste-special (arg)
  21710. "Paste rectangular region into table, or past subtree relative to level.
  21711. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  21712. See the individual commands for more information."
  21713. (interactive "P")
  21714. (if (org-at-table-p)
  21715. (org-table-paste-rectangle)
  21716. (org-paste-subtree arg)))
  21717. (defun org-ctrl-c-ctrl-c (&optional arg)
  21718. "Set tags in headline, or update according to changed information at point.
  21719. This command does many different things, depending on context:
  21720. - If the cursor is in a headline, prompt for tags and insert them
  21721. into the current line, aligned to `org-tags-column'. When called
  21722. with prefix arg, realign all tags in the current buffer.
  21723. - If the cursor is in one of the special #+KEYWORD lines, this
  21724. triggers scanning the buffer for these lines and updating the
  21725. information.
  21726. - If the cursor is inside a table, realign the table. This command
  21727. works even if the automatic table editor has been turned off.
  21728. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  21729. the entire table.
  21730. - If the cursor is inside a table created by the table.el package,
  21731. activate that table.
  21732. - If the current buffer is a remember buffer, close note and file it.
  21733. with a prefix argument, file it without further interaction to the default
  21734. location.
  21735. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  21736. links in this buffer.
  21737. - If the cursor is on a numbered item in a plain list, renumber the
  21738. ordered list."
  21739. (interactive "P")
  21740. (let ((org-enable-table-editor t))
  21741. (cond
  21742. ((or org-clock-overlays
  21743. org-occur-highlights
  21744. org-latex-fragment-image-overlays)
  21745. (org-remove-clock-overlays)
  21746. (org-remove-occur-highlights)
  21747. (org-remove-latex-fragment-image-overlays)
  21748. (message "Temporary highlights/overlays removed from current buffer"))
  21749. ((and (local-variable-p 'org-finish-function (current-buffer))
  21750. (fboundp org-finish-function))
  21751. (funcall org-finish-function))
  21752. ((org-at-property-p)
  21753. (call-interactively 'org-property-action))
  21754. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  21755. ((org-on-heading-p) (call-interactively 'org-set-tags))
  21756. ((org-at-table.el-p)
  21757. (require 'table)
  21758. (beginning-of-line 1)
  21759. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  21760. (call-interactively 'table-recognize-table))
  21761. ((org-at-table-p)
  21762. (org-table-maybe-eval-formula)
  21763. (if arg
  21764. (call-interactively 'org-table-recalculate)
  21765. (org-table-maybe-recalculate-line))
  21766. (call-interactively 'org-table-align))
  21767. ((org-at-item-checkbox-p)
  21768. (call-interactively 'org-toggle-checkbox))
  21769. ((org-at-item-p)
  21770. (call-interactively 'org-maybe-renumber-ordered-list))
  21771. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  21772. (cond
  21773. ((equal (match-string 1) "TBLFM")
  21774. ;; Recalculate the table before this line
  21775. (save-excursion
  21776. (beginning-of-line 1)
  21777. (skip-chars-backward " \r\n\t")
  21778. (if (org-at-table-p)
  21779. (org-call-with-arg 'org-table-recalculate t))))
  21780. (t
  21781. (call-interactively 'org-mode-restart))))
  21782. (t (error "C-c C-c can do nothing useful at this location.")))))
  21783. (defun org-mode-restart ()
  21784. "Restart Org-mode, to scan again for special lines.
  21785. Also updates the keyword regular expressions."
  21786. (interactive)
  21787. (let ((org-inhibit-startup t)) (org-mode))
  21788. (message "Org-mode restarted to refresh keyword and special line setup"))
  21789. (defun org-return ()
  21790. "Goto next table row or insert a newline.
  21791. Calls `org-table-next-row' or `newline', depending on context.
  21792. See the individual commands for more information."
  21793. (interactive)
  21794. (cond
  21795. ((bobp) (newline))
  21796. ((org-at-table-p)
  21797. (org-table-justify-field-maybe)
  21798. (call-interactively 'org-table-next-row))
  21799. (t (newline))))
  21800. (defun org-ctrl-c-minus ()
  21801. "Insert separator line in table or modify bullet type in list.
  21802. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  21803. depending on context."
  21804. (interactive)
  21805. (cond
  21806. ((org-at-table-p)
  21807. (call-interactively 'org-table-insert-hline))
  21808. ((org-in-item-p)
  21809. (call-interactively 'org-cycle-list-bullet))
  21810. (t (error "`C-c -' does have no function here."))))
  21811. (defun org-meta-return (&optional arg)
  21812. "Insert a new heading or wrap a region in a table.
  21813. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  21814. See the individual commands for more information."
  21815. (interactive "P")
  21816. (cond
  21817. ((org-at-table-p)
  21818. (call-interactively 'org-table-wrap-region))
  21819. (t (call-interactively 'org-insert-heading))))
  21820. ;;; Menu entries
  21821. ;; Define the Org-mode menus
  21822. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  21823. '("Tbl"
  21824. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  21825. ["Next Field" org-cycle (org-at-table-p)]
  21826. ["Previous Field" org-shifttab (org-at-table-p)]
  21827. ["Next Row" org-return (org-at-table-p)]
  21828. "--"
  21829. ["Blank Field" org-table-blank-field (org-at-table-p)]
  21830. ["Edit Field" org-table-edit-field (org-at-table-p)]
  21831. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  21832. "--"
  21833. ("Column"
  21834. ["Move Column Left" org-metaleft (org-at-table-p)]
  21835. ["Move Column Right" org-metaright (org-at-table-p)]
  21836. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  21837. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  21838. ("Row"
  21839. ["Move Row Up" org-metaup (org-at-table-p)]
  21840. ["Move Row Down" org-metadown (org-at-table-p)]
  21841. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  21842. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  21843. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  21844. "--"
  21845. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  21846. ("Rectangle"
  21847. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  21848. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  21849. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  21850. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  21851. "--"
  21852. ("Calculate"
  21853. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  21854. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  21855. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  21856. "--"
  21857. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  21858. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  21859. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  21860. "--"
  21861. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  21862. "--"
  21863. ["Sum Column/Rectangle" org-table-sum
  21864. (or (org-at-table-p) (org-region-active-p))]
  21865. ["Which Column?" org-table-current-column (org-at-table-p)])
  21866. ["Debug Formulas"
  21867. org-table-toggle-formula-debugger
  21868. :style toggle :selected org-table-formula-debug]
  21869. ["Show Col/Row Numbers"
  21870. org-table-toggle-coordinate-overlays
  21871. :style toggle :selected org-table-overlay-coordinates]
  21872. "--"
  21873. ["Create" org-table-create (and (not (org-at-table-p))
  21874. org-enable-table-editor)]
  21875. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  21876. ["Import from File" org-table-import (not (org-at-table-p))]
  21877. ["Export to File" org-table-export (org-at-table-p)]
  21878. "--"
  21879. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  21880. (easy-menu-define org-org-menu org-mode-map "Org menu"
  21881. '("Org"
  21882. ("Show/Hide"
  21883. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  21884. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  21885. ["Sparse Tree" org-occur t]
  21886. ["Reveal Context" org-reveal t]
  21887. ["Show All" show-all t]
  21888. "--"
  21889. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  21890. "--"
  21891. ["New Heading" org-insert-heading t]
  21892. ("Navigate Headings"
  21893. ["Up" outline-up-heading t]
  21894. ["Next" outline-next-visible-heading t]
  21895. ["Previous" outline-previous-visible-heading t]
  21896. ["Next Same Level" outline-forward-same-level t]
  21897. ["Previous Same Level" outline-backward-same-level t]
  21898. "--"
  21899. ["Jump" org-goto t])
  21900. ("Edit Structure"
  21901. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  21902. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  21903. "--"
  21904. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  21905. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  21906. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  21907. "--"
  21908. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  21909. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  21910. ["Demote Heading" org-metaright (not (org-at-table-p))]
  21911. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  21912. "--"
  21913. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  21914. "--"
  21915. ["Convert to odd levels" org-convert-to-odd-levels t]
  21916. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  21917. ("Editing"
  21918. ["Emphasis..." org-emphasize t])
  21919. ("Archive"
  21920. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  21921. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  21922. ; :active t :keys "C-u C-c C-x C-a"]
  21923. ["Sparse trees open ARCHIVE trees"
  21924. (setq org-sparse-tree-open-archived-trees
  21925. (not org-sparse-tree-open-archived-trees))
  21926. :style toggle :selected org-sparse-tree-open-archived-trees]
  21927. ["Cycling opens ARCHIVE trees"
  21928. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  21929. :style toggle :selected org-cycle-open-archived-trees]
  21930. ["Agenda includes ARCHIVE trees"
  21931. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  21932. :style toggle :selected (not org-agenda-skip-archived-trees)]
  21933. "--"
  21934. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  21935. ; ["Check and Move Children" (org-archive-subtree '(4))
  21936. ; :active t :keys "C-u C-c C-x C-s"]
  21937. )
  21938. "--"
  21939. ("TODO Lists"
  21940. ["TODO/DONE/-" org-todo t]
  21941. ("Select keyword"
  21942. ["Next keyword" org-shiftright (org-on-heading-p)]
  21943. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  21944. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  21945. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  21946. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  21947. ["Show TODO Tree" org-show-todo-tree t]
  21948. ["Global TODO list" org-todo-list t]
  21949. "--"
  21950. ["Set Priority" org-priority t]
  21951. ["Priority Up" org-shiftup t]
  21952. ["Priority Down" org-shiftdown t])
  21953. ("TAGS and Properties"
  21954. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  21955. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)] ;FIXME
  21956. ["Column view of properties" org-columns t])
  21957. ("Dates and Scheduling"
  21958. ["Timestamp" org-time-stamp t]
  21959. ["Timestamp (inactive)" org-time-stamp-inactive t]
  21960. ("Change Date"
  21961. ["1 Day Later" org-shiftright t]
  21962. ["1 Day Earlier" org-shiftleft t]
  21963. ["1 ... Later" org-shiftup t]
  21964. ["1 ... Earlier" org-shiftdown t])
  21965. ["Compute Time Range" org-evaluate-time-range t]
  21966. ["Schedule Item" org-schedule t]
  21967. ["Deadline" org-deadline t]
  21968. "--"
  21969. ["Custom time format" org-toggle-time-stamp-overlays
  21970. :style radio :selected org-display-custom-times]
  21971. "--"
  21972. ["Goto Calendar" org-goto-calendar t]
  21973. ["Date from Calendar" org-date-from-calendar t])
  21974. ("Logging work"
  21975. ["Clock in" org-clock-in t]
  21976. ["Clock out" org-clock-out t]
  21977. ["Clock cancel" org-clock-cancel t]
  21978. ["Display times" org-clock-display t]
  21979. ["Create clock table" org-clock-report t]
  21980. "--"
  21981. ["Record DONE time"
  21982. (progn (setq org-log-done (not org-log-done))
  21983. (message "Switching to %s will %s record a timestamp"
  21984. (car org-done-keywords)
  21985. (if org-log-done "automatically" "not")))
  21986. :style toggle :selected org-log-done])
  21987. "--"
  21988. ["Agenda Command..." org-agenda t]
  21989. ("File List for Agenda")
  21990. ("Special views current file"
  21991. ["TODO Tree" org-show-todo-tree t]
  21992. ["Check Deadlines" org-check-deadlines t]
  21993. ["Timeline" org-timeline t]
  21994. ["Tags Tree" org-tags-sparse-tree t])
  21995. "--"
  21996. ("Hyperlinks"
  21997. ["Store Link (Global)" org-store-link t]
  21998. ["Insert Link" org-insert-link t]
  21999. ["Follow Link" org-open-at-point t]
  22000. "--"
  22001. ["Next link" org-next-link t]
  22002. ["Previous link" org-previous-link t]
  22003. "--"
  22004. ["Descriptive Links"
  22005. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  22006. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  22007. ["Literal Links"
  22008. (progn
  22009. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  22010. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  22011. "--"
  22012. ["Export/Publish..." org-export t]
  22013. ("LaTeX"
  22014. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  22015. :selected org-cdlatex-mode]
  22016. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  22017. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  22018. ["Modify math symbol" org-cdlatex-math-modify
  22019. (org-inside-LaTeX-fragment-p)]
  22020. ["Export LaTeX fragments as images"
  22021. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  22022. :style toggle :selected org-export-with-LaTeX-fragments])
  22023. "--"
  22024. ("Documentation"
  22025. ["Show Version" org-version t]
  22026. ["Info Documentation" org-info t])
  22027. ("Customize"
  22028. ["Browse Org Group" org-customize t]
  22029. "--"
  22030. ["Expand This Menu" org-create-customize-menu
  22031. (fboundp 'customize-menu-create)])
  22032. "--"
  22033. ["Refresh setup" org-mode-restart t]
  22034. ))
  22035. (defun org-info (&optional node)
  22036. "Read documentation for Org-mode in the info system.
  22037. With optional NODE, go directly to that node."
  22038. (interactive)
  22039. (require 'info)
  22040. (Info-goto-node (format "(org)%s" (or node ""))))
  22041. (defun org-install-agenda-files-menu ()
  22042. (let ((bl (buffer-list)))
  22043. (save-excursion
  22044. (while bl
  22045. (set-buffer (pop bl))
  22046. (if (org-mode-p) (setq bl nil)))
  22047. (when (org-mode-p)
  22048. (easy-menu-change
  22049. '("Org") "File List for Agenda"
  22050. (append
  22051. (list
  22052. ["Edit File List" (org-edit-agenda-file-list) t]
  22053. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  22054. ["Remove Current File from List" org-remove-file t]
  22055. ["Cycle through agenda files" org-cycle-agenda-files t]
  22056. "--")
  22057. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  22058. ;;;; Documentation
  22059. (defun org-customize ()
  22060. "Call the customize function with org as argument."
  22061. (interactive)
  22062. (customize-browse 'org))
  22063. (defun org-create-customize-menu ()
  22064. "Create a full customization menu for Org-mode, insert it into the menu."
  22065. (interactive)
  22066. (if (fboundp 'customize-menu-create)
  22067. (progn
  22068. (easy-menu-change
  22069. '("Org") "Customize"
  22070. `(["Browse Org group" org-customize t]
  22071. "--"
  22072. ,(customize-menu-create 'org)
  22073. ["Set" Custom-set t]
  22074. ["Save" Custom-save t]
  22075. ["Reset to Current" Custom-reset-current t]
  22076. ["Reset to Saved" Custom-reset-saved t]
  22077. ["Reset to Standard Settings" Custom-reset-standard t]))
  22078. (message "\"Org\"-menu now contains full customization menu"))
  22079. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  22080. ;;;; Miscellaneous stuff
  22081. ;;; Generally useful functions
  22082. (defun org-context ()
  22083. "Return a list of contexts of the current cursor position.
  22084. If several contexts apply, all are returned.
  22085. Each context entry is a list with a symbol naming the context, and
  22086. two positions indicating start and end of the context. Possible
  22087. contexts are:
  22088. :headline anywhere in a headline
  22089. :headline-stars on the leading stars in a headline
  22090. :todo-keyword on a TODO keyword (including DONE) in a headline
  22091. :tags on the TAGS in a headline
  22092. :priority on the priority cookie in a headline
  22093. :item on the first line of a plain list item
  22094. :item-bullet on the bullet/number of a plain list item
  22095. :checkbox on the checkbox in a plain list item
  22096. :table in an org-mode table
  22097. :table-special on a special filed in a table
  22098. :table-table in a table.el table
  22099. :link on a hyperlink
  22100. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  22101. :target on a <<target>>
  22102. :radio-target on a <<<radio-target>>>
  22103. :latex-fragment on a LaTeX fragment
  22104. :latex-preview on a LaTeX fragment with overlayed preview image
  22105. This function expects the position to be visible because it uses font-lock
  22106. faces as a help to recognize the following contexts: :table-special, :link,
  22107. and :keyword."
  22108. (let* ((f (get-text-property (point) 'face))
  22109. (faces (if (listp f) f (list f)))
  22110. (p (point)) clist o)
  22111. ;; First the large context
  22112. (cond
  22113. ((org-on-heading-p t)
  22114. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  22115. (when (progn
  22116. (beginning-of-line 1)
  22117. (looking-at org-todo-line-tags-regexp))
  22118. (push (org-point-in-group p 1 :headline-stars) clist)
  22119. (push (org-point-in-group p 2 :todo-keyword) clist)
  22120. (push (org-point-in-group p 4 :tags) clist))
  22121. (goto-char p)
  22122. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  22123. (if (looking-at "\\[#[A-Z0-9]\\]")
  22124. (push (org-point-in-group p 0 :priority) clist)))
  22125. ((org-at-item-p)
  22126. (push (org-point-in-group p 2 :item-bullet) clist)
  22127. (push (list :item (point-at-bol)
  22128. (save-excursion (org-end-of-item) (point)))
  22129. clist)
  22130. (and (org-at-item-checkbox-p)
  22131. (push (org-point-in-group p 0 :checkbox) clist)))
  22132. ((org-at-table-p)
  22133. (push (list :table (org-table-begin) (org-table-end)) clist)
  22134. (if (memq 'org-formula faces)
  22135. (push (list :table-special
  22136. (previous-single-property-change p 'face)
  22137. (next-single-property-change p 'face)) clist)))
  22138. ((org-at-table-p 'any)
  22139. (push (list :table-table) clist)))
  22140. (goto-char p)
  22141. ;; Now the small context
  22142. (cond
  22143. ((org-at-timestamp-p)
  22144. (push (org-point-in-group p 0 :timestamp) clist))
  22145. ((memq 'org-link faces)
  22146. (push (list :link
  22147. (previous-single-property-change p 'face)
  22148. (next-single-property-change p 'face)) clist))
  22149. ((memq 'org-special-keyword faces)
  22150. (push (list :keyword
  22151. (previous-single-property-change p 'face)
  22152. (next-single-property-change p 'face)) clist))
  22153. ((org-on-target-p)
  22154. (push (org-point-in-group p 0 :target) clist)
  22155. (goto-char (1- (match-beginning 0)))
  22156. (if (looking-at org-radio-target-regexp)
  22157. (push (org-point-in-group p 0 :radio-target) clist))
  22158. (goto-char p))
  22159. ((setq o (car (delq nil
  22160. (mapcar
  22161. (lambda (x)
  22162. (if (memq x org-latex-fragment-image-overlays) x))
  22163. (org-overlays-at (point))))))
  22164. (push (list :latex-fragment
  22165. (org-overlay-start o) (org-overlay-end o)) clist)
  22166. (push (list :latex-preview
  22167. (org-overlay-start o) (org-overlay-end o)) clist))
  22168. ((org-inside-LaTeX-fragment-p)
  22169. ;; FIXME: positions wrong.
  22170. (push (list :latex-fragment (point) (point)) clist)))
  22171. (setq clist (nreverse (delq nil clist)))
  22172. clist))
  22173. ;; FIXME Compare with at-regexp-p
  22174. (defun org-in-regexp (re &optional nlines visually)
  22175. "Check if point is inside a match of regexp.
  22176. Normally only the current line is checked, but you can include NLINES extra
  22177. lines both before and after point into the search.
  22178. If VISUALLY is set, require that the cursor is not after the match but
  22179. really on, so that the block visually is on the match."
  22180. (catch 'exit
  22181. (let ((pos (point))
  22182. (eol (point-at-eol (+ 1 (or nlines 0))))
  22183. (inc (if visually 1 0)))
  22184. (save-excursion
  22185. (beginning-of-line (- 1 (or nlines 0)))
  22186. (while (re-search-forward re eol t)
  22187. (if (and (<= (match-beginning 0) pos)
  22188. (>= (+ inc (match-end 0)) pos))
  22189. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  22190. (defun org-at-regexp-p (regexp)
  22191. "Is point inside a match of REGEXP in the current line?"
  22192. (catch 'exit
  22193. (save-excursion
  22194. (let ((pos (point)) (end (point-at-eol)))
  22195. (beginning-of-line 1)
  22196. (while (re-search-forward regexp end t)
  22197. (if (and (<= (match-beginning 0) pos)
  22198. (>= (match-end 0) pos))
  22199. (throw 'exit t)))
  22200. nil))))
  22201. (defun org-uniquify (list)
  22202. "Remove duplicate elements from LIST."
  22203. (let (res)
  22204. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  22205. res))
  22206. (defun org-delete-all (elts list)
  22207. "Remove all elements in ELTS from LIST."
  22208. (while elts
  22209. (setq list (delete (pop elts) list)))
  22210. list)
  22211. (defun org-point-in-group (point group &optional context)
  22212. "Check if POINT is in match-group GROUP.
  22213. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  22214. match. If the match group does ot exist or point is not inside it,
  22215. return nil."
  22216. (and (match-beginning group)
  22217. (>= point (match-beginning group))
  22218. (<= point (match-end group))
  22219. (if context
  22220. (list context (match-beginning group) (match-end group))
  22221. t)))
  22222. (defun org-switch-to-buffer-other-window (&rest args)
  22223. "Switch to buffer in a second window on the current frame.
  22224. In particular, do not allow pop-up frames."
  22225. (let (pop-up-frames special-display-buffer-names special-display-regexps
  22226. special-display-function)
  22227. (apply 'switch-to-buffer-other-window args)))
  22228. (defun org-combine-plists (&rest plists)
  22229. "Create a single property list from all plists in PLISTS.
  22230. The process starts by copying the first list, and then setting properties
  22231. from the other lists. Settings in the last list are the most significant
  22232. ones and overrule settings in the other lists."
  22233. (let ((rtn (copy-sequence (pop plists)))
  22234. p v ls)
  22235. (while plists
  22236. (setq ls (pop plists))
  22237. (while ls
  22238. (setq p (pop ls) v (pop ls))
  22239. (setq rtn (plist-put rtn p v))))
  22240. rtn))
  22241. (defun org-move-line-down (arg)
  22242. "Move the current line down. With prefix argument, move it past ARG lines."
  22243. (interactive "p")
  22244. (let ((col (current-column))
  22245. beg end pos)
  22246. (beginning-of-line 1) (setq beg (point))
  22247. (beginning-of-line 2) (setq end (point))
  22248. (beginning-of-line (+ 1 arg))
  22249. (setq pos (move-marker (make-marker) (point)))
  22250. (insert (delete-and-extract-region beg end))
  22251. (goto-char pos)
  22252. (move-to-column col)))
  22253. (defun org-move-line-up (arg)
  22254. "Move the current line up. With prefix argument, move it past ARG lines."
  22255. (interactive "p")
  22256. (let ((col (current-column))
  22257. beg end pos)
  22258. (beginning-of-line 1) (setq beg (point))
  22259. (beginning-of-line 2) (setq end (point))
  22260. (beginning-of-line (- arg))
  22261. (setq pos (move-marker (make-marker) (point)))
  22262. (insert (delete-and-extract-region beg end))
  22263. (goto-char pos)
  22264. (move-to-column col)))
  22265. (defun org-replace-escapes (string table)
  22266. "Replace %-escapes in STRING with values in TABLE.
  22267. TABLE is an association list with keys line \"%a\" and string values.
  22268. The sequences in STRING may contain normal field width and padding information,
  22269. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  22270. so values can contain further %-escapes if they are define later in TABLE."
  22271. (let ((case-fold-search nil)
  22272. e re rpl)
  22273. (while (setq e (pop table))
  22274. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  22275. (while (string-match re string)
  22276. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  22277. (cdr e)))
  22278. (setq string (replace-match rpl t t string))))
  22279. string))
  22280. (defun org-sublist (list start end)
  22281. "Return a section of LIST, from START to END.
  22282. Counting starts at 1."
  22283. (let (rtn (c start))
  22284. (setq list (nthcdr (1- start) list))
  22285. (while (and list (<= c end))
  22286. (push (pop list) rtn)
  22287. (setq c (1+ c)))
  22288. (nreverse rtn)))
  22289. (defun org-find-base-buffer-visiting (file)
  22290. "Like `find-buffer-visiting' but alway return the base buffer and
  22291. not an indirect buffer"
  22292. (let ((buf (find-buffer-visiting file)))
  22293. (or (buffer-base-buffer buf) buf)))
  22294. (defun org-image-file-name-regexp ()
  22295. "Return regexp matching the file names of images."
  22296. (if (fboundp 'image-file-name-regexp)
  22297. (image-file-name-regexp)
  22298. (let ((image-file-name-extensions
  22299. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  22300. "xbm" "xpm" "pbm" "pgm" "ppm")))
  22301. (concat "\\."
  22302. (regexp-opt (nconc (mapcar 'upcase
  22303. image-file-name-extensions)
  22304. image-file-name-extensions)
  22305. t)
  22306. "\\'"))))
  22307. (defun org-file-image-p (file)
  22308. "Return non-nil if FILE is an image."
  22309. (save-match-data
  22310. (string-match (org-image-file-name-regexp) file)))
  22311. ;;; Paragraph filling stuff.
  22312. ;; We want this to be just right, so use the full arsenal.
  22313. (defun org-indent-line-function ()
  22314. "Indent line like previous, but further if previous was headline or item."
  22315. (interactive)
  22316. (let* ((pos (point))
  22317. (itemp (org-at-item-p))
  22318. column bpos bcol tpos tcol bullet btype bullet-type)
  22319. ;; Find the previous relevant line
  22320. (beginning-of-line 1)
  22321. (cond
  22322. ((looking-at "#") (setq column 0))
  22323. ((looking-at "\\*+ ") (setq column 0))
  22324. (t
  22325. (beginning-of-line 0)
  22326. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  22327. (beginning-of-line 0))
  22328. (cond
  22329. ((looking-at "\\*+[ \t]+")
  22330. (goto-char (match-end 0))
  22331. (setq column (current-column)))
  22332. ((org-in-item-p)
  22333. (org-beginning-of-item)
  22334. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22335. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  22336. (setq bpos (match-beginning 1) tpos (match-end 0)
  22337. bcol (progn (goto-char bpos) (current-column))
  22338. tcol (progn (goto-char tpos) (current-column))
  22339. bullet (match-string 1)
  22340. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  22341. (if (not itemp)
  22342. (setq column tcol)
  22343. (goto-char pos)
  22344. (beginning-of-line 1)
  22345. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22346. (setq bullet (match-string 1)
  22347. btype (if (string-match "[0-9]" bullet) "n" bullet))
  22348. (setq column (if (equal btype bullet-type) bcol tcol))))
  22349. (t (setq column (org-get-indentation))))))
  22350. (goto-char pos)
  22351. (if (<= (current-column) (current-indentation))
  22352. (indent-line-to column)
  22353. (save-excursion (indent-line-to column)))
  22354. (setq column (current-column))
  22355. (beginning-of-line 1)
  22356. (if (looking-at
  22357. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  22358. (replace-match (concat "\\1" (format org-property-format
  22359. (match-string 2) (match-string 3)))
  22360. t nil))
  22361. (move-to-column column)))
  22362. (defun org-set-autofill-regexps ()
  22363. (interactive)
  22364. ;; In the paragraph separator we include headlines, because filling
  22365. ;; text in a line directly attached to a headline would otherwise
  22366. ;; fill the headline as well.
  22367. (org-set-local 'comment-start-skip "^#+[ \t]*")
  22368. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  22369. ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
  22370. ;; The paragraph starter includes hand-formatted lists.
  22371. (org-set-local 'paragraph-start
  22372. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  22373. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  22374. ;; But only if the user has not turned off tables or fixed-width regions
  22375. (org-set-local
  22376. 'auto-fill-inhibit-regexp
  22377. (concat "\\*+ \\|#\\+"
  22378. "\\|[ \t]*" org-keyword-time-regexp
  22379. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  22380. (concat
  22381. "\\|[ \t]*["
  22382. (if org-enable-table-editor "|" "")
  22383. (if org-enable-fixed-width-editor ":" "")
  22384. "]"))))
  22385. ;; We use our own fill-paragraph function, to make sure that tables
  22386. ;; and fixed-width regions are not wrapped. That function will pass
  22387. ;; through to `fill-paragraph' when appropriate.
  22388. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  22389. ; Adaptive filling: To get full control, first make sure that
  22390. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  22391. (org-set-local 'adaptive-fill-regexp "\000")
  22392. (org-set-local 'adaptive-fill-function
  22393. 'org-adaptive-fill-function))
  22394. (defun org-fill-paragraph (&optional justify)
  22395. "Re-align a table, pass through to fill-paragraph if no table."
  22396. (let ((table-p (org-at-table-p))
  22397. (table.el-p (org-at-table.el-p)))
  22398. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  22399. (table.el-p t) ; skip table.el tables
  22400. (table-p (org-table-align) t) ; align org-mode tables
  22401. (t nil)))) ; call paragraph-fill
  22402. ;; For reference, this is the default value of adaptive-fill-regexp
  22403. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  22404. (defun org-adaptive-fill-function ()
  22405. "Return a fill prefix for org-mode files.
  22406. In particular, this makes sure hanging paragraphs for hand-formatted lists
  22407. work correctly."
  22408. (cond ((looking-at "#[ \t]+")
  22409. (match-string 0))
  22410. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  22411. (save-excursion
  22412. (goto-char (match-end 0))
  22413. (make-string (current-column) ?\ )))
  22414. (t nil)))
  22415. ;;;; Functions extending outline functionality
  22416. (defun org-beginning-of-line (&optional arg)
  22417. "Go to the beginning of the current line. If that is invisible, continue
  22418. to a visible line beginning. This makes the function of C-a more intuitive.
  22419. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22420. first attempt, and only move to after the tags when the cursor is already
  22421. beyond the end of the headline."
  22422. (interactive "P")
  22423. (let ((pos (point)))
  22424. (beginning-of-line 1)
  22425. (if (bobp)
  22426. nil
  22427. (backward-char 1)
  22428. (if (org-invisible-p)
  22429. (while (and (not (bobp)) (org-invisible-p))
  22430. (backward-char 1)
  22431. (beginning-of-line 1))
  22432. (forward-char 1)))
  22433. (when org-special-ctrl-a/e
  22434. (cond
  22435. ((and (looking-at org-todo-line-regexp)
  22436. (= (char-after (match-end 1)) ?\ ))
  22437. (goto-char
  22438. (if (eq org-special-ctrl-a/e t)
  22439. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  22440. ((= pos (point)) (match-beginning 3))
  22441. (t (point)))
  22442. (cond ((> pos (point)) (point))
  22443. ((not (eq last-command this-command)) (point))
  22444. (t (match-beginning 3))))))
  22445. ((org-at-item-p)
  22446. (goto-char
  22447. (if (eq org-special-ctrl-a/e t)
  22448. (cond ((> pos (match-end 4)) (match-end 4))
  22449. ((= pos (point)) (match-end 4))
  22450. (t (point)))
  22451. (cond ((> pos (point)) (point))
  22452. ((not (eq last-command this-command)) (point))
  22453. (t (match-end 4))))))))))
  22454. (defun org-end-of-line (&optional arg)
  22455. "Go to the end of the line.
  22456. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22457. first attempt, and only move to after the tags when the cursor is already
  22458. beyond the end of the headline."
  22459. (interactive "P")
  22460. (if (or (not org-special-ctrl-a/e)
  22461. (not (org-on-heading-p)))
  22462. (end-of-line arg)
  22463. (let ((pos (point)))
  22464. (beginning-of-line 1)
  22465. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  22466. (if (eq org-special-ctrl-a/e t)
  22467. (if (or (< pos (match-beginning 1))
  22468. (= pos (match-end 0)))
  22469. (goto-char (match-beginning 1))
  22470. (goto-char (match-end 0)))
  22471. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  22472. (goto-char (match-end 0))
  22473. (goto-char (match-beginning 1))))
  22474. (end-of-line arg)))))
  22475. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22476. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22477. (defun org-invisible-p ()
  22478. "Check if point is at a character currently not visible."
  22479. ;; Early versions of noutline don't have `outline-invisible-p'.
  22480. (if (fboundp 'outline-invisible-p)
  22481. (outline-invisible-p)
  22482. (get-char-property (point) 'invisible)))
  22483. (defun org-invisible-p2 ()
  22484. "Check if point is at a character currently not visible."
  22485. (save-excursion
  22486. (if (and (eolp) (not (bobp))) (backward-char 1))
  22487. ;; Early versions of noutline don't have `outline-invisible-p'.
  22488. (if (fboundp 'outline-invisible-p)
  22489. (outline-invisible-p)
  22490. (get-char-property (point) 'invisible))))
  22491. (defalias 'org-back-to-heading 'outline-back-to-heading)
  22492. (defalias 'org-on-heading-p 'outline-on-heading-p)
  22493. (defalias 'org-at-heading-p 'outline-on-heading-p)
  22494. (defun org-at-heading-or-item-p ()
  22495. (or (org-on-heading-p) (org-at-item-p)))
  22496. (defun org-on-target-p ()
  22497. (or (org-in-regexp org-radio-target-regexp)
  22498. (org-in-regexp org-target-regexp)))
  22499. (defun org-up-heading-all (arg)
  22500. "Move to the heading line of which the present line is a subheading.
  22501. This function considers both visible and invisible heading lines.
  22502. With argument, move up ARG levels."
  22503. (if (fboundp 'outline-up-heading-all)
  22504. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22505. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22506. (defun org-goto-sibling (&optional previous)
  22507. "Goto the next sibling, even if it is invisible.
  22508. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22509. when a sibling was found. When none is found, return nil and don't
  22510. move point."
  22511. (let ((fun (if previous 're-search-backward 're-search-forward))
  22512. (pos (point))
  22513. (re (concat "^" outline-regexp))
  22514. level l)
  22515. (when (condition-case nil (org-back-to-heading t) (error nil))
  22516. (setq level (funcall outline-level))
  22517. (catch 'exit
  22518. (or previous (forward-char 1))
  22519. (while (funcall fun re nil t)
  22520. (setq l (funcall outline-level))
  22521. (when (< l level) (goto-char pos) (throw 'exit nil))
  22522. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22523. (goto-char pos)
  22524. nil))))
  22525. (defun org-show-siblings ()
  22526. "Show all siblings of the current headline."
  22527. (save-excursion
  22528. (while (org-goto-sibling) (org-flag-heading nil)))
  22529. (save-excursion
  22530. (while (org-goto-sibling 'previous)
  22531. (org-flag-heading nil))))
  22532. (defun org-show-hidden-entry ()
  22533. "Show an entry where even the heading is hidden."
  22534. (save-excursion
  22535. (org-show-entry)))
  22536. (defun org-flag-heading (flag &optional entry)
  22537. "Flag the current heading. FLAG non-nil means make invisible.
  22538. When ENTRY is non-nil, show the entire entry."
  22539. (save-excursion
  22540. (org-back-to-heading t)
  22541. ;; Check if we should show the entire entry
  22542. (if entry
  22543. (progn
  22544. (org-show-entry)
  22545. (save-excursion
  22546. (and (outline-next-heading)
  22547. (org-flag-heading nil))))
  22548. (outline-flag-region (max (point-min) (1- (point)))
  22549. (save-excursion (outline-end-of-heading) (point))
  22550. flag))))
  22551. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  22552. ;; This is an exact copy of the original function, but it uses
  22553. ;; `org-back-to-heading', to make it work also in invisible
  22554. ;; trees. And is uses an invisible-OK argument.
  22555. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22556. (org-back-to-heading invisible-OK)
  22557. (let ((first t)
  22558. (level (funcall outline-level)))
  22559. (while (and (not (eobp))
  22560. (or first (> (funcall outline-level) level)))
  22561. (setq first nil)
  22562. (outline-next-heading))
  22563. (unless to-heading
  22564. (if (memq (preceding-char) '(?\n ?\^M))
  22565. (progn
  22566. ;; Go to end of line before heading
  22567. (forward-char -1)
  22568. (if (memq (preceding-char) '(?\n ?\^M))
  22569. ;; leave blank line before heading
  22570. (forward-char -1))))))
  22571. (point))
  22572. (defun org-show-subtree ()
  22573. "Show everything after this heading at deeper levels."
  22574. (outline-flag-region
  22575. (point)
  22576. (save-excursion
  22577. (outline-end-of-subtree) (outline-next-heading) (point))
  22578. nil))
  22579. (defun org-show-entry ()
  22580. "Show the body directly following this heading.
  22581. Show the heading too, if it is currently invisible."
  22582. (interactive)
  22583. (save-excursion
  22584. (org-back-to-heading t)
  22585. (outline-flag-region
  22586. (max (point-min) (1- (point)))
  22587. (save-excursion
  22588. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  22589. (or (match-beginning 1) (point-max)))
  22590. nil)))
  22591. (defun org-make-options-regexp (kwds)
  22592. "Make a regular expression for keyword lines."
  22593. (concat
  22594. "^"
  22595. "#?[ \t]*\\+\\("
  22596. (mapconcat 'regexp-quote kwds "\\|")
  22597. "\\):[ \t]*"
  22598. "\\(.+\\)"))
  22599. ;; Make isearch reveal the necessary context
  22600. (defun org-isearch-end ()
  22601. "Reveal context after isearch exits."
  22602. (when isearch-success ; only if search was successful
  22603. (if (featurep 'xemacs)
  22604. ;; Under XEmacs, the hook is run in the correct place,
  22605. ;; we directly show the context.
  22606. (org-show-context 'isearch)
  22607. ;; In Emacs the hook runs *before* restoring the overlays.
  22608. ;; So we have to use a one-time post-command-hook to do this.
  22609. ;; (Emacs 22 has a special variable, see function `org-mode')
  22610. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22611. isearch-mode-end-hook-quit)
  22612. ;; Only when the isearch was not quitted.
  22613. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22614. 'append 'local)))))
  22615. (defun org-isearch-post-command ()
  22616. "Remove self from hook, and show context."
  22617. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22618. (org-show-context 'isearch))
  22619. ;;;; Address problems with some other packages
  22620. ;; Make flyspell not check words in links, to not mess up our keymap
  22621. (defun org-mode-flyspell-verify ()
  22622. "Don't let flyspell put overlays at active buttons."
  22623. (not (get-text-property (point) 'keymap)))
  22624. ;; Make `bookmark-jump' show the jump location if it was hidden.
  22625. (eval-after-load "bookmark"
  22626. '(if (boundp 'bookmark-after-jump-hook)
  22627. ;; We can use the hook
  22628. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22629. ;; Hook not available, use advice
  22630. (defadvice bookmark-jump (after org-make-visible activate)
  22631. "Make the position visible."
  22632. (org-bookmark-jump-unhide))))
  22633. (defun org-bookmark-jump-unhide ()
  22634. "Unhide the current position, to show the bookmark location."
  22635. (and (org-mode-p)
  22636. (or (org-invisible-p)
  22637. (save-excursion (goto-char (max (point-min) (1- (point))))
  22638. (org-invisible-p)))
  22639. (org-show-context 'bookmark-jump)))
  22640. ;; Make session.el ignore our circular variable
  22641. (eval-after-load "session"
  22642. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22643. ;;;; Experimental code
  22644. ;; Make appt aware of appointments from the agenda
  22645. (defun org-agenda-to-appt ()
  22646. "Activate appointments found in `org-agenda-files'."
  22647. (interactive)
  22648. (require 'org)
  22649. (let* ((today (org-date-to-gregorian
  22650. (time-to-days (current-time))))
  22651. (files org-agenda-files) entries file)
  22652. (while (setq file (pop files))
  22653. (setq entries (append entries (org-agenda-get-day-entries
  22654. file today :timestamp))))
  22655. (setq entries (delq nil entries))
  22656. (mapc (lambda(x)
  22657. (let* ((event (org-trim (get-text-property 1 'txt x)))
  22658. (time-of-day (get-text-property 1 'time-of-day x)) tod)
  22659. (when time-of-day
  22660. (setq tod (number-to-string time-of-day)
  22661. tod (when (string-match
  22662. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  22663. (concat (match-string 1 tod) ":"
  22664. (match-string 2 tod))))
  22665. (if tod (appt-add tod event))))) entries)))
  22666. (defun org-closed-in-range ()
  22667. "Sparse tree of items closed in a certain time range.
  22668. Still experimental, may disappear in the furture."
  22669. (interactive)
  22670. ;; Get the time interval from the user.
  22671. (let* ((time1 (time-to-seconds
  22672. (org-read-date nil 'to-time nil "Starting date: ")))
  22673. (time2 (time-to-seconds
  22674. (org-read-date nil 'to-time nil "End date:")))
  22675. ;; callback function
  22676. (callback (lambda ()
  22677. (let ((time
  22678. (time-to-seconds
  22679. (apply 'encode-time
  22680. (org-parse-time-string
  22681. (match-string 1))))))
  22682. ;; check if time in interval
  22683. (and (>= time time1) (<= time time2))))))
  22684. ;; make tree, check each match with the callback
  22685. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  22686. (defun org-fill-paragraph-experimental (&optional justify)
  22687. "Re-align a table, pass through to fill-paragraph if no table."
  22688. (let ((table-p (org-at-table-p))
  22689. (table.el-p (org-at-table.el-p)))
  22690. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  22691. (table.el-p t) ; skip table.el tables
  22692. (table-p (org-table-align) t) ; align org-mode tables
  22693. ((save-excursion
  22694. (let ((pos (1+ (point-at-eol))))
  22695. (backward-paragraph 1)
  22696. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  22697. (save-excursion
  22698. (save-restriction
  22699. (narrow-to-region (1+ (match-end 0)) (point-max))
  22700. (fill-paragraph nil)
  22701. t)))
  22702. (t nil)))) ; call paragraph-fill
  22703. (defun org-get-min-level (lines)
  22704. (let ((re "^\\(\\*+\\) ") l min)
  22705. (catch 'exit
  22706. (while (setq l (pop lines))
  22707. (if (string-match re l)
  22708. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22709. 1)))
  22710. ;;;; Finish up
  22711. (provide 'org)
  22712. (run-hooks 'org-load-hook)
  22713. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  22714. ;;; org.el ends here