org.el 898 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420
  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.06d
  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 2, 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.06d"
  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. and just in Org-mode (which will then use its own display table).
  212. Changing this requires executing `M-x org-mode' in a buffer to become
  213. effective."
  214. :group 'org-startup
  215. :type '(choice (const :tag "Default" nil)
  216. (string :tag "String" :value "...#")))
  217. (defvar org-display-table nil
  218. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  219. (defgroup org-keywords nil
  220. "Keywords in Org-mode."
  221. :tag "Org Keywords"
  222. :group 'org)
  223. (defcustom org-deadline-string "DEADLINE:"
  224. "String to mark deadline entries.
  225. A deadline is this string, followed by a time stamp. Should be a word,
  226. terminated by a colon. You can insert a schedule keyword and
  227. a timestamp with \\[org-deadline].
  228. Changes become only effective after restarting Emacs."
  229. :group 'org-keywords
  230. :type 'string)
  231. (defcustom org-scheduled-string "SCHEDULED:"
  232. "String to mark scheduled TODO entries.
  233. A schedule is this string, followed by a time stamp. Should be a word,
  234. terminated by a colon. You can insert a schedule keyword and
  235. a timestamp with \\[org-schedule].
  236. Changes become only effective after restarting Emacs."
  237. :group 'org-keywords
  238. :type 'string)
  239. (defcustom org-closed-string "CLOSED:"
  240. "String used as the prefix for timestamps logging closing a TODO entry."
  241. :group 'org-keywords
  242. :type 'string)
  243. (defcustom org-archived-string "ARCHIVED:"
  244. "String used as the prefix for timestamps logging archiving a TODO entry."
  245. :group 'org-keywords
  246. :type 'string)
  247. (defcustom org-clock-string "CLOCK:"
  248. "String used as prefix for timestamps clocking work hours on an item."
  249. :group 'org-keywords
  250. :type 'string)
  251. (defcustom org-comment-string "COMMENT"
  252. "Entries starting with this keyword will never be exported.
  253. An entry can be toggled between COMMENT and normal with
  254. \\[org-toggle-comment].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-quote-string "QUOTE"
  259. "Entries starting with this keyword will be exported in fixed-width font.
  260. Quoting applies only to the text in the entry following the headline, and does
  261. not extend beyond the next headline, even if that is lower level.
  262. An entry can be toggled between QUOTE and normal with
  263. \\[org-toggle-fixed-width-section]."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defconst org-repeat-re
  267. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  268. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  269. "Regular expression for specifying repeated events.
  270. After a match, group 1 contains the repeat expression.")
  271. (defgroup org-structure nil
  272. "Options concerning the general structure of Org-mode files."
  273. :tag "Org Structure"
  274. :group 'org)
  275. (defgroup org-reveal-location nil
  276. "Options about how to make context of a location visible."
  277. :tag "Org Reveal Location"
  278. :group 'org-structure)
  279. (defcustom org-show-hierarchy-above '((default . t))
  280. "Non-nil means, show full hierarchy when revealing a location.
  281. Org-mode often shows locations in an org-mode file which might have
  282. been invisible before. When this is set, the hierarchy of headings
  283. above the exposed location is shown.
  284. Turning this off for example for sparse trees makes them very compact.
  285. Instead of t, this can also be an alist specifying this option for different
  286. contexts. Valid contexts are
  287. agenda when exposing an entry from the agenda
  288. org-goto when using the command `org-goto' on key C-c C-j
  289. occur-tree when using the command `org-occur' on key C-c /
  290. tags-tree when constructing a sparse tree based on tags matches
  291. link-search when exposing search matches associated with a link
  292. mark-goto when exposing the jump goal of a mark
  293. bookmark-jump when exposing a bookmark location
  294. isearch when exiting from an incremental search
  295. default default for all contexts not set explicitly"
  296. :group 'org-reveal-location
  297. :type '(choice
  298. (const :tag "Always" t)
  299. (const :tag "Never" nil)
  300. (repeat :greedy t :tag "Individual contexts"
  301. (cons
  302. (choice :tag "Context"
  303. (const agenda)
  304. (const org-goto)
  305. (const occur-tree)
  306. (const tags-tree)
  307. (const link-search)
  308. (const mark-goto)
  309. (const bookmark-jump)
  310. (const isearch)
  311. (const default))
  312. (boolean)))))
  313. (defcustom org-show-following-heading '((default . nil))
  314. "Non-nil means, show following heading when revealing a location.
  315. Org-mode often shows locations in an org-mode file which might have
  316. been invisible before. When this is set, the heading following the
  317. match is shown.
  318. Turning this off for example for sparse trees makes them very compact,
  319. but makes it harder to edit the location of the match. In such a case,
  320. use the command \\[org-reveal] to show more context.
  321. Instead of t, this can also be an alist specifying this option for different
  322. contexts. See `org-show-hierarchy-above' for valid contexts."
  323. :group 'org-reveal-location
  324. :type '(choice
  325. (const :tag "Always" t)
  326. (const :tag "Never" nil)
  327. (repeat :greedy t :tag "Individual contexts"
  328. (cons
  329. (choice :tag "Context"
  330. (const agenda)
  331. (const org-goto)
  332. (const occur-tree)
  333. (const tags-tree)
  334. (const link-search)
  335. (const mark-goto)
  336. (const bookmark-jump)
  337. (const isearch)
  338. (const default))
  339. (boolean)))))
  340. (defcustom org-show-siblings '((default . nil) (isearch t))
  341. "Non-nil means, show all sibling heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the sibling of the current entry
  344. heading are all made visible. If `org-show-hierarchy-above' is t,
  345. the same happens on each level of the hierarchy above the current entry.
  346. By default this is on for the isearch context, off for all other contexts.
  347. Turning this off for example for sparse trees makes them very compact,
  348. but makes it harder to edit the location of the match. In such a case,
  349. use the command \\[org-reveal] to show more context.
  350. Instead of t, this can also be an alist specifying this option for different
  351. contexts. See `org-show-hierarchy-above' for valid contexts."
  352. :group 'org-reveal-location
  353. :type '(choice
  354. (const :tag "Always" t)
  355. (const :tag "Never" nil)
  356. (repeat :greedy t :tag "Individual contexts"
  357. (cons
  358. (choice :tag "Context"
  359. (const agenda)
  360. (const org-goto)
  361. (const occur-tree)
  362. (const tags-tree)
  363. (const link-search)
  364. (const mark-goto)
  365. (const bookmark-jump)
  366. (const isearch)
  367. (const default))
  368. (boolean)))))
  369. (defgroup org-cycle nil
  370. "Options concerning visibility cycling in Org-mode."
  371. :tag "Org Cycle"
  372. :group 'org-structure)
  373. (defcustom org-drawers '("PROPERTIES")
  374. "Names of drawers. Drawers are not opened by cycling on the headline above.
  375. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  376. this:
  377. :DRAWERNAME:
  378. .....
  379. :END:
  380. The drawer \"PROPERTIES\" is special for capturing properties through
  381. the property API."
  382. :group 'org-structure
  383. :type '(repeat (string :tag "Drawer Name")))
  384. (defcustom org-cycle-global-at-bob nil
  385. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  386. This makes it possible to do global cycling without having to use S-TAB or
  387. C-u TAB. For this special case to work, the first line of the buffer
  388. must not be a headline - it may be empty ot some other text. When used in
  389. this way, `org-cycle-hook' is disables temporarily, to make sure the
  390. cursor stays at the beginning of the buffer.
  391. When this option is nil, don't do anything special at the beginning
  392. of the buffer."
  393. :group 'org-cycle
  394. :type 'boolean)
  395. (defcustom org-cycle-emulate-tab t
  396. "Where should `org-cycle' emulate TAB.
  397. nil Never
  398. white Only in completely white lines
  399. whitestart Only at the beginning of lines, before the first non-white char.
  400. t Everywhere except in headlines
  401. exc-hl-bol Everywhere except at the start of a headline
  402. If TAB is used in a place where it does not emulate TAB, the current subtree
  403. visibility is cycled."
  404. :group 'org-cycle
  405. :type '(choice (const :tag "Never" nil)
  406. (const :tag "Only in completely white lines" white)
  407. (const :tag "Before first char in a line" whitestart)
  408. (const :tag "Everywhere except in headlines" t)
  409. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  410. ))
  411. (defcustom org-cycle-separator-lines 2
  412. "Number of empty lines needed to keep an empty line between collapsed trees.
  413. If you leave an empty line between the end of a subtree and the following
  414. headline, this empty line is hidden when the subtree is folded.
  415. Org-mode will leave (exactly) one empty line visible if the number of
  416. empty lines is equal or larger to the number given in this variable.
  417. So the default 2 means, at least 2 empty lines after the end of a subtree
  418. are needed to produce free space between a collapsed subtree and the
  419. following headline.
  420. Special case: when 0, never leave empty lines in collapsed view."
  421. :group 'org-cycle
  422. :type 'integer)
  423. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  424. org-cycle-hide-drawers
  425. org-cycle-show-empty-lines
  426. org-optimize-window-after-visibility-change)
  427. "Hook that is run after `org-cycle' has changed the buffer visibility.
  428. The function(s) in this hook must accept a single argument which indicates
  429. the new state that was set by the most recent `org-cycle' command. The
  430. argument is a symbol. After a global state change, it can have the values
  431. `overview', `content', or `all'. After a local state change, it can have
  432. the values `folded', `children', or `subtree'."
  433. :group 'org-cycle
  434. :type 'hook)
  435. (defgroup org-edit-structure nil
  436. "Options concerning structure editing in Org-mode."
  437. :tag "Org Edit Structure"
  438. :group 'org-structure)
  439. (defcustom org-special-ctrl-a/e nil
  440. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  441. When t, `C-a' will bring back the cursor to the beginning of the
  442. headline text, i.e. after the stars and after a possible TODO keyword.
  443. In an item, this will be the position after the bullet.
  444. When the cursor is already at that position, another `C-a' will bring
  445. it to the beginning of the line.
  446. `C-e' will jump to the end of the headline, ignoring the presence of tags
  447. in the headline. A second `C-e' will then jump to the true end of the
  448. line, after any tags.
  449. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  450. and only a directly following, identical keypress will bring the cursor
  451. to the special positions."
  452. :group 'org-edit-structure
  453. :type '(choice
  454. (const :tag "off" nil)
  455. (const :tag "after bullet first" t)
  456. (const :tag "border first" reversed)))
  457. (if (fboundp 'defvaralias)
  458. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  459. (defcustom org-odd-levels-only nil
  460. "Non-nil means, skip even levels and only use odd levels for the outline.
  461. This has the effect that two stars are being added/taken away in
  462. promotion/demotion commands. It also influences how levels are
  463. handled by the exporters.
  464. Changing it requires restart of `font-lock-mode' to become effective
  465. for fontification also in regions already fontified.
  466. You may also set this on a per-file basis by adding one of the following
  467. lines to the buffer:
  468. #+STARTUP: odd
  469. #+STARTUP: oddeven"
  470. :group 'org-edit-structure
  471. :group 'org-font-lock
  472. :type 'boolean)
  473. (defcustom org-adapt-indentation t
  474. "Non-nil means, adapt indentation when promoting and demoting.
  475. When this is set and the *entire* text in an entry is indented, the
  476. indentation is increased by one space in a demotion command, and
  477. decreased by one in a promotion command. If any line in the entry
  478. body starts at column 0, indentation is not changed at all."
  479. :group 'org-edit-structure
  480. :type 'boolean)
  481. (defcustom org-blank-before-new-entry '((heading . nil)
  482. (plain-list-item . nil))
  483. "Should `org-insert-heading' leave a blank line before new heading/item?
  484. The value is an alist, with `heading' and `plain-list-item' as car,
  485. and a boolean flag as cdr."
  486. :group 'org-edit-structure
  487. :type '(list
  488. (cons (const heading) (boolean))
  489. (cons (const plain-list-item) (boolean))))
  490. (defcustom org-insert-heading-hook nil
  491. "Hook being run after inserting a new heading."
  492. :group 'org-edit-structure
  493. :type 'boolean)
  494. (defcustom org-enable-fixed-width-editor t
  495. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  496. This currently only means, they are never auto-wrapped.
  497. When nil, such lines will be treated like ordinary lines.
  498. See also the QUOTE keyword."
  499. :group 'org-edit-structure
  500. :type 'boolean)
  501. (defgroup org-sparse-trees nil
  502. "Options concerning sparse trees in Org-mode."
  503. :tag "Org Sparse Trees"
  504. :group 'org-structure)
  505. (defcustom org-highlight-sparse-tree-matches t
  506. "Non-nil means, highlight all matches that define a sparse tree.
  507. The highlights will automatically disappear the next time the buffer is
  508. changed by an edit command."
  509. :group 'org-sparse-trees
  510. :type 'boolean)
  511. (defcustom org-remove-highlights-with-change t
  512. "Non-nil means, any change to the buffer will remove temporary highlights.
  513. Such highlights are created by `org-occur' and `org-clock-display'.
  514. When nil, `C-c C-c needs to be used to get rid of the highlights.
  515. The highlights created by `org-preview-latex-fragment' always need
  516. `C-c C-c' to be removed."
  517. :group 'org-sparse-trees
  518. :group 'org-time
  519. :type 'boolean)
  520. (defcustom org-occur-hook '(org-first-headline-recenter)
  521. "Hook that is run after `org-occur' has constructed a sparse tree.
  522. This can be used to recenter the window to show as much of the structure
  523. as possible."
  524. :group 'org-sparse-trees
  525. :type 'hook)
  526. (defgroup org-plain-lists nil
  527. "Options concerning plain lists in Org-mode."
  528. :tag "Org Plain lists"
  529. :group 'org-structure)
  530. (defcustom org-cycle-include-plain-lists nil
  531. "Non-nil means, include plain lists into visibility cycling.
  532. This means that during cycling, plain list items will *temporarily* be
  533. interpreted as outline headlines with a level given by 1000+i where i is the
  534. indentation of the bullet. In all other operations, plain list items are
  535. not seen as headlines. For example, you cannot assign a TODO keyword to
  536. such an item."
  537. :group 'org-plain-lists
  538. :type 'boolean)
  539. (defcustom org-plain-list-ordered-item-terminator t
  540. "The character that makes a line with leading number an ordered list item.
  541. Valid values are ?. and ?\). To get both terminators, use t. While
  542. ?. may look nicer, it creates the danger that a line with leading
  543. number may be incorrectly interpreted as an item. ?\) therefore is
  544. the safe choice."
  545. :group 'org-plain-lists
  546. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  547. (const :tag "paren like in \"2)\"" ?\))
  548. (const :tab "both" t)))
  549. (defcustom org-auto-renumber-ordered-lists t
  550. "Non-nil means, automatically renumber ordered plain lists.
  551. Renumbering happens when the sequence have been changed with
  552. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  553. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  554. :group 'org-plain-lists
  555. :type 'boolean)
  556. (defcustom org-provide-checkbox-statistics t
  557. "Non-nil means, update checkbox statistics after insert and toggle.
  558. When this is set, checkbox statistics is updated each time you either insert
  559. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  560. with \\[org-ctrl-c-ctrl-c\\]."
  561. :group 'org-plain-lists
  562. :type 'boolean)
  563. (defgroup org-archive nil
  564. "Options concerning archiving in Org-mode."
  565. :tag "Org Archive"
  566. :group 'org-structure)
  567. (defcustom org-archive-tag "ARCHIVE"
  568. "The tag that marks a subtree as archived.
  569. An archived subtree does not open during visibility cycling, and does
  570. not contribute to the agenda listings."
  571. :group 'org-archive
  572. :group 'org-keywords
  573. :type 'string)
  574. (defcustom org-agenda-skip-archived-trees t
  575. "Non-nil means, the agenda will skip any items located in archived trees.
  576. An archived tree is a tree marked with the tag ARCHIVE."
  577. :group 'org-archive
  578. :group 'org-agenda-skip
  579. :type 'boolean)
  580. (defcustom org-cycle-open-archived-trees nil
  581. "Non-nil means, `org-cycle' will open archived trees.
  582. An archived tree is a tree marked with the tag ARCHIVE.
  583. When nil, archived trees will stay folded. You can still open them with
  584. normal outline commands like `show-all', but not with the cycling commands."
  585. :group 'org-archive
  586. :group 'org-cycle
  587. :type 'boolean)
  588. (defcustom org-sparse-tree-open-archived-trees nil
  589. "Non-nil means sparse tree construction shows matches in archived trees.
  590. When nil, matches in these trees are highlighted, but the trees are kept in
  591. collapsed state."
  592. :group 'org-archive
  593. :group 'org-sparse-trees
  594. :type 'boolean)
  595. (defcustom org-archive-location "%s_archive::"
  596. "The location where subtrees should be archived.
  597. This string consists of two parts, separated by a double-colon.
  598. The first part is a file name - when omitted, archiving happens in the same
  599. file. %s will be replaced by the current file name (without directory part).
  600. Archiving to a different file is useful to keep archived entries from
  601. contributing to the Org-mode Agenda.
  602. The part after the double colon is a headline. The archived entries will be
  603. filed under that headline. When omitted, the subtrees are simply filed away
  604. at the end of the file, as top-level entries.
  605. Here are a few examples:
  606. \"%s_archive::\"
  607. If the current file is Projects.org, archive in file
  608. Projects.org_archive, as top-level trees. This is the default.
  609. \"::* Archived Tasks\"
  610. Archive in the current file, under the top-level headline
  611. \"* Archived Tasks\".
  612. \"~/org/archive.org::\"
  613. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  614. \"basement::** Finished Tasks\"
  615. Archive in file ./basement (relative path), as level 3 trees
  616. below the level 2 heading \"** Finished Tasks\".
  617. You may set this option on a per-file basis by adding to the buffer a
  618. line like
  619. #+ARCHIVE: basement::** Finished Tasks"
  620. :group 'org-archive
  621. :type 'string)
  622. (defcustom org-archive-mark-done t
  623. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  624. This can be a string to set the keyword to use. When t, Org-mode will
  625. use the first keyword in its list that means done."
  626. :group 'org-archive
  627. :type '(choice
  628. (const :tag "No" nil)
  629. (const :tag "Yes" t)
  630. (string :tag "Use this keyword")))
  631. (defcustom org-archive-stamp-time t
  632. "Non-nil means, add a time stamp to entries moved to an archive file."
  633. :group 'org-archive
  634. :type 'boolean)
  635. (defgroup org-table nil
  636. "Options concerning tables in Org-mode."
  637. :tag "Org Table"
  638. :group 'org)
  639. (defcustom org-enable-table-editor 'optimized
  640. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  641. When nil, such lines will be treated like ordinary lines.
  642. When equal to the symbol `optimized', the table editor will be optimized to
  643. do the following:
  644. - Automatic overwrite mode in front of whitespace in table fields.
  645. This makes the structure of the table stay in tact as long as the edited
  646. field does not exceed the column width.
  647. - Minimize the number of realigns. Normally, the table is aligned each time
  648. TAB or RET are pressed to move to another field. With optimization this
  649. happens only if changes to a field might have changed the column width.
  650. Optimization requires replacing the functions `self-insert-command',
  651. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  652. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  653. very good at guessing when a re-align will be necessary, but you can always
  654. force one with \\[org-ctrl-c-ctrl-c].
  655. If you would like to use the optimized version in Org-mode, but the
  656. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  657. This variable can be used to turn on and off the table editor during a session,
  658. but in order to toggle optimization, a restart is required.
  659. See also the variable `org-table-auto-blank-field'."
  660. :group 'org-table
  661. :type '(choice
  662. (const :tag "off" nil)
  663. (const :tag "on" t)
  664. (const :tag "on, optimized" optimized)))
  665. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  666. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  667. In the optimized version, the table editor takes over all simple keys that
  668. normally just insert a character. In tables, the characters are inserted
  669. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  670. for empty fields). Outside tables, the correct binding of the keys is
  671. restored.
  672. The default for this option is t if the optimized version is also used in
  673. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  674. this variable requires a restart of Emacs to become effective."
  675. :group 'org-table
  676. :type 'boolean)
  677. (defcustom orgtbl-radio-table-templates
  678. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  679. % END RECEIVE ORGTBL %n
  680. \\begin{comment}
  681. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  682. | | |
  683. \\end{comment}\n")
  684. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  685. @c END RECEIVE ORGTBL %n
  686. @ignore
  687. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  688. | | |
  689. @end ignore\n")
  690. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  691. <!-- END RECEIVE ORGTBL %n -->
  692. <!--
  693. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  694. | | |
  695. -->\n"))
  696. "Templates for radio tables in different major modes.
  697. All occurrences of %n in a template will be replaced with the name of the
  698. table, obtained by prompting the user."
  699. :group 'org-table
  700. :type '(repeat
  701. (list (symbol :tag "Major mode")
  702. (string :tag "Format"))))
  703. (defgroup org-table-settings nil
  704. "Settings for tables in Org-mode."
  705. :tag "Org Table Settings"
  706. :group 'org-table)
  707. (defcustom org-table-default-size "5x2"
  708. "The default size for newly created tables, Columns x Rows."
  709. :group 'org-table-settings
  710. :type 'string)
  711. (defcustom org-table-number-regexp
  712. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  713. "Regular expression for recognizing numbers in table columns.
  714. If a table column contains mostly numbers, it will be aligned to the
  715. right. If not, it will be aligned to the left.
  716. The default value of this option is a regular expression which allows
  717. anything which looks remotely like a number as used in scientific
  718. context. For example, all of the following will be considered a
  719. number:
  720. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  721. Other options offered by the customize interface are more restrictive."
  722. :group 'org-table-settings
  723. :type '(choice
  724. (const :tag "Positive Integers"
  725. "^[0-9]+$")
  726. (const :tag "Integers"
  727. "^[-+]?[0-9]+$")
  728. (const :tag "Floating Point Numbers"
  729. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  730. (const :tag "Floating Point Number or Integer"
  731. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  732. (const :tag "Exponential, Floating point, Integer"
  733. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  734. (const :tag "Very General Number-Like, including hex"
  735. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  736. (string :tag "Regexp:")))
  737. (defcustom org-table-number-fraction 0.5
  738. "Fraction of numbers in a column required to make the column align right.
  739. In a column all non-white fields are considered. If at least this
  740. fraction of fields is matched by `org-table-number-fraction',
  741. alignment to the right border applies."
  742. :group 'org-table-settings
  743. :type 'number)
  744. (defgroup org-table-editing nil
  745. "Bahavior of tables during editing in Org-mode."
  746. :tag "Org Table Editing"
  747. :group 'org-table)
  748. (defcustom org-table-automatic-realign t
  749. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  750. When nil, aligning is only done with \\[org-table-align], or after column
  751. removal/insertion."
  752. :group 'org-table-editing
  753. :type 'boolean)
  754. (defcustom org-table-auto-blank-field t
  755. "Non-nil means, automatically blank table field when starting to type into it.
  756. This only happens when typing immediately after a field motion
  757. command (TAB, S-TAB or RET).
  758. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  759. :group 'org-table-editing
  760. :type 'boolean)
  761. (defcustom org-table-tab-jumps-over-hlines t
  762. "Non-nil means, tab in the last column of a table with jump over a hline.
  763. If a horizontal separator line is following the current line,
  764. `org-table-next-field' can either create a new row before that line, or jump
  765. over the line. When this option is nil, a new line will be created before
  766. this line."
  767. :group 'org-table-editing
  768. :type 'boolean)
  769. (defcustom org-table-tab-recognizes-table.el t
  770. "Non-nil means, TAB will automatically notice a table.el table.
  771. When it sees such a table, it moves point into it and - if necessary -
  772. calls `table-recognize-table'."
  773. :group 'org-table-editing
  774. :type 'boolean)
  775. (defgroup org-table-calculation nil
  776. "Options concerning tables in Org-mode."
  777. :tag "Org Table Calculation"
  778. :group 'org-table)
  779. (defcustom org-table-use-standard-references t
  780. "Should org-mode work with table refrences like B3 instead of @3$2?
  781. Possible values are:
  782. nil never use them
  783. from accept as input, do not present for editing
  784. t: accept as input and present for editing"
  785. :group 'org-table-calculation
  786. :type '(choice
  787. (const :tag "Never, don't even check unser input for them" nil)
  788. (const :tag "Always, both as user input, and when editing" t)
  789. (const :tag "Convert user input, don't offer during editing" 'from)))
  790. (defcustom org-table-copy-increment t
  791. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  792. :group 'org-table-calculation
  793. :type 'boolean)
  794. (defcustom org-calc-default-modes
  795. '(calc-internal-prec 12
  796. calc-float-format (float 5)
  797. calc-angle-mode deg
  798. calc-prefer-frac nil
  799. calc-symbolic-mode nil
  800. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  801. calc-display-working-message t
  802. )
  803. "List with Calc mode settings for use in calc-eval for table formulas.
  804. The list must contain alternating symbols (Calc modes variables and values).
  805. Don't remove any of the default settings, just change the values. Org-mode
  806. relies on the variables to be present in the list."
  807. :group 'org-table-calculation
  808. :type 'plist)
  809. (defcustom org-table-formula-evaluate-inline t
  810. "Non-nil means, TAB and RET evaluate a formula in current table field.
  811. If the current field starts with an equal sign, it is assumed to be a formula
  812. which should be evaluated as described in the manual and in the documentation
  813. string of the command `org-table-eval-formula'. This feature requires the
  814. Emacs calc package.
  815. When this variable is nil, formula calculation is only available through
  816. the command \\[org-table-eval-formula]."
  817. :group 'org-table-calculation
  818. :type 'boolean)
  819. (defcustom org-table-formula-use-constants t
  820. "Non-nil means, interpret constants in formulas in tables.
  821. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  822. by the value given in `org-table-formula-constants', or by a value obtained
  823. from the `constants.el' package."
  824. :group 'org-table-calculation
  825. :type 'boolean)
  826. (defcustom org-table-formula-constants nil
  827. "Alist with constant names and values, for use in table formulas.
  828. The car of each element is a name of a constant, without the `$' before it.
  829. The cdr is the value as a string. For example, if you'd like to use the
  830. speed of light in a formula, you would configure
  831. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  832. and then use it in an equation like `$1*$c'.
  833. Constants can also be defined on a per-file basis using a line like
  834. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  835. :group 'org-table-calculation
  836. :type '(repeat
  837. (cons (string :tag "name")
  838. (string :tag "value"))))
  839. (defvar org-table-formula-constants-local nil
  840. "Local version of `org-table-formula-constants'.")
  841. (make-variable-buffer-local 'org-table-formula-constants-local)
  842. (defcustom org-table-allow-automatic-line-recalculation t
  843. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  844. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  845. :group 'org-table-calculation
  846. :type 'boolean)
  847. (defgroup org-link nil
  848. "Options concerning links in Org-mode."
  849. :tag "Org Link"
  850. :group 'org)
  851. (defvar org-link-abbrev-alist-local nil
  852. "Buffer-local version of `org-link-abbrev-alist', which see.
  853. The value of this is taken from the #+LINK lines.")
  854. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  855. (defcustom org-link-abbrev-alist nil
  856. "Alist of link abbreviations.
  857. The car of each element is a string, to be replaced at the start of a link.
  858. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  859. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  860. [[linkkey:tag][description]]
  861. If REPLACE is a string, the tag will simply be appended to create the link.
  862. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  863. also be a function that will be called with the tag as the only argument to
  864. create the link. See the manual for examples."
  865. :group 'org-link
  866. :type 'alist)
  867. (defcustom org-descriptive-links t
  868. "Non-nil means, hide link part and only show description of bracket links.
  869. Bracket links are like [[link][descritpion]]. This variable sets the initial
  870. state in new org-mode buffers. The setting can then be toggled on a
  871. per-buffer basis from the Org->Hyperlinks menu."
  872. :group 'org-link
  873. :type 'boolean)
  874. (defcustom org-link-file-path-type 'adaptive
  875. "How the path name in file links should be stored.
  876. Valid values are:
  877. relative relative to the current directory, i.e. the directory of the file
  878. into which the link is being inserted.
  879. absolute absolute path, if possible with ~ for home directory.
  880. noabbrev absolute path, no abbreviation of home directory.
  881. adaptive Use relative path for files in the current directory and sub-
  882. directories of it. For other files, use an absolute path."
  883. :group 'org-link
  884. :type '(choice
  885. (const relative)
  886. (const absolute)
  887. (const noabbrev)
  888. (const adaptive)))
  889. (defcustom org-activate-links '(bracket angle plain radio tag date)
  890. "Types of links that should be activated in Org-mode files.
  891. This is a list of symbols, each leading to the activation of a certain link
  892. type. In principle, it does not hurt to turn on most link types - there may
  893. be a small gain when turning off unused link types. The types are:
  894. bracket The recommended [[link][description]] or [[link]] links with hiding.
  895. angular Links in angular brackes that may contain whitespace like
  896. <bbdb:Carsten Dominik>.
  897. plain Plain links in normal text, no whitespace, like http://google.com.
  898. radio Text that is matched by a radio target, see manual for details.
  899. tag Tag settings in a headline (link to tag search).
  900. date Time stamps (link to calendar).
  901. Changing this variable requires a restart of Emacs to become effective."
  902. :group 'org-link
  903. :type '(set (const :tag "Double bracket links (new style)" bracket)
  904. (const :tag "Angular bracket links (old style)" angular)
  905. (const :tag "plain text links" plain)
  906. (const :tag "Radio target matches" radio)
  907. (const :tag "Tags" tag)
  908. (const :tag "Tags" target)
  909. (const :tag "Timestamps" date)))
  910. (defgroup org-link-store nil
  911. "Options concerning storing links in Org-mode"
  912. :tag "Org Store Link"
  913. :group 'org-link)
  914. (defcustom org-email-link-description-format "Email %c: %.30s"
  915. "Format of the description part of a link to an email or usenet message.
  916. The following %-excapes will be replaced by corresponding information:
  917. %F full \"From\" field
  918. %f name, taken from \"From\" field, address if no name
  919. %T full \"To\" field
  920. %t first name in \"To\" field, address if no name
  921. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  922. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  923. %s subject
  924. %m message-id.
  925. You may use normal field width specification between the % and the letter.
  926. This is for example useful to limit the length of the subject.
  927. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  928. :group 'org-link-store
  929. :type 'string)
  930. (defcustom org-from-is-user-regexp
  931. (let (r1 r2)
  932. (when (and user-mail-address (not (string= user-mail-address "")))
  933. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  934. (when (and user-full-name (not (string= user-full-name "")))
  935. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  936. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  937. "Regexp mached against the \"From:\" header of an email or usenet message.
  938. It should match if the message is from the user him/herself."
  939. :group 'org-link-store
  940. :type 'regexp)
  941. (defcustom org-context-in-file-links t
  942. "Non-nil means, file links from `org-store-link' contain context.
  943. A search string will be added to the file name with :: as separator and
  944. used to find the context when the link is activated by the command
  945. `org-open-at-point'.
  946. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  947. negates this setting for the duration of the command."
  948. :group 'org-link-store
  949. :type 'boolean)
  950. (defcustom org-keep-stored-link-after-insertion nil
  951. "Non-nil means, keep link in list for entire session.
  952. The command `org-store-link' adds a link pointing to the current
  953. location to an internal list. These links accumulate during a session.
  954. The command `org-insert-link' can be used to insert links into any
  955. Org-mode file (offering completion for all stored links). When this
  956. option is nil, every link which has been inserted once using \\[org-insert-link]
  957. will be removed from the list, to make completing the unused links
  958. more efficient."
  959. :group 'org-link-store
  960. :type 'boolean)
  961. (defcustom org-usenet-links-prefer-google nil
  962. "Non-nil means, `org-store-link' will create web links to Google groups.
  963. When nil, Gnus will be used for such links.
  964. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  965. negates this setting for the duration of the command."
  966. :group 'org-link-store
  967. :type 'boolean)
  968. (defgroup org-link-follow nil
  969. "Options concerning following links in Org-mode"
  970. :tag "Org Follow Link"
  971. :group 'org-link)
  972. (defcustom org-tab-follows-link nil
  973. "Non-nil means, on links TAB will follow the link.
  974. Needs to be set before org.el is loaded."
  975. :group 'org-link-follow
  976. :type 'boolean)
  977. (defcustom org-return-follows-link nil
  978. "Non-nil means, on links RET will follow the link.
  979. Needs to be set before org.el is loaded."
  980. :group 'org-link-follow
  981. :type 'boolean)
  982. (defcustom org-mouse-1-follows-link t
  983. "Non-nil means, mouse-1 on a link will follow the link.
  984. A longer mouse click will still set point. Does not wortk on XEmacs.
  985. Needs to be set before org.el is loaded."
  986. :group 'org-link-follow
  987. :type 'boolean)
  988. (defcustom org-mark-ring-length 4
  989. "Number of different positions to be recorded in the ring
  990. Changing this requires a restart of Emacs to work correctly."
  991. :group 'org-link-follow
  992. :type 'interger)
  993. (defcustom org-link-frame-setup
  994. '((vm . vm-visit-folder-other-frame)
  995. (gnus . gnus-other-frame)
  996. (file . find-file-other-window))
  997. "Setup the frame configuration for following links.
  998. When following a link with Emacs, it may often be useful to display
  999. this link in another window or frame. This variable can be used to
  1000. set this up for the different types of links.
  1001. For VM, use any of
  1002. `vm-visit-folder'
  1003. `vm-visit-folder-other-frame'
  1004. For Gnus, use any of
  1005. `gnus'
  1006. `gnus-other-frame'
  1007. For FILE, use any of
  1008. `find-file'
  1009. `find-file-other-window'
  1010. `find-file-other-frame'
  1011. For the calendar, use the variable `calendar-setup'.
  1012. For BBDB, it is currently only possible to display the matches in
  1013. another window."
  1014. :group 'org-link-follow
  1015. :type '(list
  1016. (cons (const vm)
  1017. (choice
  1018. (const vm-visit-folder)
  1019. (const vm-visit-folder-other-window)
  1020. (const vm-visit-folder-other-frame)))
  1021. (cons (const gnus)
  1022. (choice
  1023. (const gnus)
  1024. (const gnus-other-frame)))
  1025. (cons (const file)
  1026. (choice
  1027. (const find-file)
  1028. (const find-file-other-window)
  1029. (const find-file-other-frame)))))
  1030. (defcustom org-display-internal-link-with-indirect-buffer nil
  1031. "Non-nil means, use indirect buffer to display infile links.
  1032. Activating internal links (from one location in a file to another location
  1033. in the same file) normally just jumps to the location. When the link is
  1034. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1035. another window. When this option is set, the other window actually displays
  1036. an indirect buffer clone of the current buffer, to avoid any visibility
  1037. changes to the current buffer."
  1038. :group 'org-link-follow
  1039. :type 'boolean)
  1040. (defcustom org-open-non-existing-files nil
  1041. "Non-nil means, `org-open-file' will open non-existing files.
  1042. When nil, an error will be generated."
  1043. :group 'org-link-follow
  1044. :type 'boolean)
  1045. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1046. "Function and arguments to call for following mailto links.
  1047. This is a list with the first element being a lisp function, and the
  1048. remaining elements being arguments to the function. In string arguments,
  1049. %a will be replaced by the address, and %s will be replaced by the subject
  1050. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1051. :group 'org-link-follow
  1052. :type '(choice
  1053. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1054. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1055. (const :tag "message-mail" (message-mail "%a" "%s"))
  1056. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1057. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1058. "Non-nil means, ask for confirmation before executing shell links.
  1059. Shell links can be dangerous, just thing about a link
  1060. [[shell:rm -rf ~/*][Google Search]]
  1061. This link would show up in your Org-mode document as \"Google Search\"
  1062. but really it would remove your entire home directory.
  1063. Therefore I *definitely* advise against setting this variable to nil.
  1064. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1065. rather than having to type \"yes\"."
  1066. :group 'org-link-follow
  1067. :type '(choice
  1068. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1069. (const :tag "with y-or-n (faster)" y-or-n-p)
  1070. (const :tag "no confirmation (dangerous)" nil)))
  1071. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1072. "Non-nil means, ask for confirmation before executing elisp links.
  1073. Elisp links can be dangerous, just think about a link
  1074. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1075. This link would show up in your Org-mode document as \"Google Search\"
  1076. but really it would remove your entire home directory.
  1077. Therefore I *definitely* advise against setting this variable to nil.
  1078. Just change it to `y-or-n-p' of you want to confirm with a single key press
  1079. rather than having to type \"yes\"."
  1080. :group 'org-link-follow
  1081. :type '(choice
  1082. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1083. (const :tag "with y-or-n (faster)" y-or-n-p)
  1084. (const :tag "no confirmation (dangerous)" nil)))
  1085. (defconst org-file-apps-defaults-gnu
  1086. '((remote . emacs)
  1087. (t . mailcap))
  1088. "Default file applications on a UNIX or GNU/Linux system.
  1089. See `org-file-apps'.")
  1090. (defconst org-file-apps-defaults-macosx
  1091. '((remote . emacs)
  1092. (t . "open %s")
  1093. ("ps" . "gv %s")
  1094. ("ps.gz" . "gv %s")
  1095. ("eps" . "gv %s")
  1096. ("eps.gz" . "gv %s")
  1097. ("dvi" . "xdvi %s")
  1098. ("fig" . "xfig %s"))
  1099. "Default file applications on a MacOS X system.
  1100. The system \"open\" is known as a default, but we use X11 applications
  1101. for some files for which the OS does not have a good default.
  1102. See `org-file-apps'.")
  1103. (defconst org-file-apps-defaults-windowsnt
  1104. (list
  1105. '(remote . emacs)
  1106. (cons t
  1107. (list (if (featurep 'xemacs)
  1108. 'mswindows-shell-execute
  1109. 'w32-shell-execute)
  1110. "open" 'file)))
  1111. "Default file applications on a Windows NT system.
  1112. The system \"open\" is used for most files.
  1113. See `org-file-apps'.")
  1114. (defcustom org-file-apps
  1115. '(
  1116. ("txt" . emacs)
  1117. ("tex" . emacs)
  1118. ("ltx" . emacs)
  1119. ("org" . emacs)
  1120. ("el" . emacs)
  1121. ("bib" . emacs)
  1122. )
  1123. "External applications for opening `file:path' items in a document.
  1124. Org-mode uses system defaults for different file types, but
  1125. you can use this variable to set the application for a given file
  1126. extension. The entries in this list are cons cells where the car identifies
  1127. files and the cdr the corresponding command. Possible values for the
  1128. file identifier are
  1129. \"ext\" A string identifying an extension
  1130. `directory' Matches a directory
  1131. `remote' Matches a remote file, accessible through tramp or efs.
  1132. Remote files most likely should be visited through Emacs
  1133. because external applications cannot handle such paths.
  1134. t Default for all remaining files
  1135. Possible values for the command are:
  1136. `emacs' The file will be visited by the current Emacs process.
  1137. `default' Use the default application for this file type.
  1138. string A command to be executed by a shell; %s will be replaced
  1139. by the path to the file.
  1140. sexp A Lisp form which will be evaluated. The file path will
  1141. be available in the Lisp variable `file'.
  1142. For more examples, see the system specific constants
  1143. `org-file-apps-defaults-macosx'
  1144. `org-file-apps-defaults-windowsnt'
  1145. `org-file-apps-defaults-gnu'."
  1146. :group 'org-link-follow
  1147. :type '(repeat
  1148. (cons (choice :value ""
  1149. (string :tag "Extension")
  1150. (const :tag "Default for unrecognized files" t)
  1151. (const :tag "Remote file" remote)
  1152. (const :tag "Links to a directory" directory))
  1153. (choice :value ""
  1154. (const :tag "Visit with Emacs" emacs)
  1155. (const :tag "Use system default" default)
  1156. (string :tag "Command")
  1157. (sexp :tag "Lisp form")))))
  1158. (defcustom org-mhe-search-all-folders nil
  1159. "Non-nil means, that the search for the mh-message will be extended to
  1160. all folders if the message cannot be found in the folder given in the link.
  1161. Searching all folders is very efficient with one of the search engines
  1162. supported by MH-E, but will be slow with pick."
  1163. :group 'org-link-follow
  1164. :type 'boolean)
  1165. (defgroup org-remember nil
  1166. "Options concerning interaction with remember.el."
  1167. :tag "Org Remember"
  1168. :group 'org)
  1169. (defcustom org-directory "~/org"
  1170. "Directory with org files.
  1171. This directory will be used as default to prompt for org files.
  1172. Used by the hooks for remember.el."
  1173. :group 'org-remember
  1174. :type 'directory)
  1175. (defcustom org-default-notes-file "~/.notes"
  1176. "Default target for storing notes.
  1177. Used by the hooks for remember.el. This can be a string, or nil to mean
  1178. the value of `remember-data-file'.
  1179. You can set this on a per-template basis with the variable
  1180. `org-remember-templates'."
  1181. :group 'org-remember
  1182. :type '(choice
  1183. (const :tag "Default from remember-data-file" nil)
  1184. file))
  1185. (defcustom org-remember-store-without-prompt nil
  1186. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1187. In this case, you need `C-u C-c C-c' to get the prompts for
  1188. note file and headline.
  1189. When this variable is nil, `C-c C-c' give you the prompts, and
  1190. `C-u C-c C-c' trigger the fasttrack."
  1191. :group 'org-remember
  1192. :type 'boolean)
  1193. (defcustom org-remember-default-headline ""
  1194. "The headline that should be the default location in the notes file.
  1195. When filing remember notes, the cursor will start at that position.
  1196. You can set this on a per-template basis with the variable
  1197. `org-remember-templates'."
  1198. :group 'org-remember
  1199. :type 'string)
  1200. (defcustom org-remember-templates nil
  1201. "Templates for the creation of remember buffers.
  1202. When nil, just let remember make the buffer.
  1203. When not nil, this is a list of 4-element lists. In each entry, the first
  1204. element is a character, a unique key to select this template.
  1205. The second element is the template. The third element is optional and can
  1206. specify a destination file for remember items created with this template.
  1207. The default file is given by `org-default-notes-file'. An optional forth
  1208. element can specify the headline in that file that should be offered
  1209. first when the user is asked to file the entry. The default headline is
  1210. given in the variable `org-remember-default-headline'.
  1211. The template specifies the structure of the remember buffer. It should have
  1212. a first line starting with a star, to act as the org-mode headline.
  1213. Furthermore, the following %-escapes will be replaced with content:
  1214. %^{prompt} prompt the user for a string and replace this sequence with it.
  1215. %t time stamp, date only
  1216. %T time stamp with date and time
  1217. %u, %U like the above, but inactive time stamps
  1218. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1219. You may define a prompt like %^{Please specify birthday}t
  1220. %n user name (taken from `user-full-name')
  1221. %a annotation, normally the link created with org-store-link
  1222. %i initial content, the region when remember is called with C-u.
  1223. If %i is indented, the entire inserted text will be indented
  1224. as well.
  1225. %? After completing the template, position cursor here.
  1226. Apart from these general escapes, you can access information specific to the
  1227. link type that is created. For example, calling `remember' in emails or gnus
  1228. will record the author and the subject of the message, which you can access
  1229. with %:author and %:subject, respectively. Here is a complete list of what
  1230. is recorded for each link type.
  1231. Link type | Available information
  1232. -------------------+------------------------------------------------------
  1233. bbdb | %:type %:name %:company
  1234. vm, wl, mh, rmail | %:type %:subject %:message-id
  1235. | %:from %:fromname %:fromaddress
  1236. | %:to %:toname %:toaddress
  1237. | %:fromto (either \"to NAME\" or \"from NAME\")
  1238. gnus | %:group, for messages also all email fields
  1239. w3, w3m | %:type %:url
  1240. info | %:type %:file %:node
  1241. calendar | %:type %:date"
  1242. :group 'org-remember
  1243. :get (lambda (var) ; Make sure all entries have 4 elements
  1244. (mapcar (lambda (x)
  1245. (cond ((= (length x) 3) (append x '("")))
  1246. ((= (length x) 2) (append x '("" "")))
  1247. (t x)))
  1248. (default-value var)))
  1249. :type '(repeat
  1250. :tag "enabled"
  1251. (list :value (?a "\n" nil nil)
  1252. (character :tag "Selection Key")
  1253. (string :tag "Template")
  1254. (file :tag "Destination file (optional)")
  1255. (string :tag "Destination headline (optional)"))))
  1256. (defcustom org-reverse-note-order nil
  1257. "Non-nil means, store new notes at the beginning of a file or entry.
  1258. When nil, new notes will be filed to the end of a file or entry."
  1259. :group 'org-remember
  1260. :type '(choice
  1261. (const :tag "Reverse always" t)
  1262. (const :tag "Reverse never" nil)
  1263. (repeat :tag "By file name regexp"
  1264. (cons regexp boolean))))
  1265. (defgroup org-todo nil
  1266. "Options concerning TODO items in Org-mode."
  1267. :tag "Org TODO"
  1268. :group 'org)
  1269. (defgroup org-progress nil
  1270. "Options concerning Progress logging in Org-mode."
  1271. :tag "Org Progress"
  1272. :group 'org-time)
  1273. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1274. "List of TODO entry keyword sequences and their interpretation.
  1275. \\<org-mode-map>This is a list of sequences.
  1276. Each sequence starts with a symbol, either `sequence' or `type',
  1277. indicating if the keywords should be interpreted as a sequence of
  1278. action steps, or as different types of TODO items. The first
  1279. keywords are states requiring action - these states will select a headline
  1280. for inclusion into the global TODO list Org-mode produces. If one of
  1281. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1282. signify that no further action is necessary. If \"|\" is not found,
  1283. the last keyword is treated as the only DONE state of the sequence.
  1284. The command \\[org-todo] cycles an entry through these states, and one
  1285. additional state where no keyword is present. For details about this
  1286. cycling, see the manual.
  1287. TODO keywords and interpretation can also be set on a per-file basis with
  1288. the special #+SEQ_TODO and #+TYP_TODO lines.
  1289. For backward compatibility, this variable may also be just a list
  1290. of keywords - in this case the interptetation (sequence or type) will be
  1291. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1292. :group 'org-todo
  1293. :group 'org-keywords
  1294. :type '(choice
  1295. (repeat :tag "Old syntax, just keywords"
  1296. (string :tag "Keyword"))
  1297. (repeat :tag "New syntax"
  1298. (cons
  1299. (choice
  1300. :tag "Interpretation"
  1301. (const :tag "Sequence (cycling hits every state)" sequence)
  1302. (const :tag "Type (cycling directly to DONE)" type))
  1303. (repeat
  1304. (string :tag "Keyword"))))))
  1305. (defvar org-todo-keywords-1 nil)
  1306. (make-variable-buffer-local 'org-todo-keywords-1)
  1307. (defvar org-todo-keywords-for-agenda nil)
  1308. (defvar org-done-keywords-for-agenda nil)
  1309. (defvar org-not-done-keywords nil)
  1310. (make-variable-buffer-local 'org-not-done-keywords)
  1311. (defvar org-done-keywords nil)
  1312. (make-variable-buffer-local 'org-done-keywords)
  1313. (defvar org-todo-heads nil)
  1314. (make-variable-buffer-local 'org-todo-heads)
  1315. (defvar org-todo-sets nil)
  1316. (make-variable-buffer-local 'org-todo-sets)
  1317. (defvar org-todo-kwd-alist nil)
  1318. (make-variable-buffer-local 'org-todo-kwd-alist)
  1319. (defcustom org-todo-interpretation 'sequence
  1320. "Controls how TODO keywords are interpreted.
  1321. This variable is in principle obsolete and is only used for
  1322. backward compatibility, if the interpretation of todo keywords is
  1323. not given already in `org-todo-keywords'. See that variable for
  1324. more information."
  1325. :group 'org-todo
  1326. :group 'org-keywords
  1327. :type '(choice (const sequence)
  1328. (const type)))
  1329. (defcustom org-after-todo-state-change-hook nil
  1330. "Hook which is run after the state of a TODO item was changed.
  1331. The new state (a string with a TODO keyword, or nil) is available in the
  1332. Lisp variable `state'."
  1333. :group 'org-todo
  1334. :type 'hook)
  1335. (defcustom org-log-done nil
  1336. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1337. When the state of an entry is changed from nothing to TODO, remove a previous
  1338. closing date.
  1339. This can also be a list of symbols indicating under which conditions
  1340. the time stamp recording the action should be annotated with a short note.
  1341. Valid members of this list are
  1342. done Offer to record a note when marking entries done
  1343. state Offer to record a note whenever changing the TODO state
  1344. of an item. This is only relevant if TODO keywords are
  1345. interpreted as sequence, see variable `org-todo-interpretation'.
  1346. When `state' is set, this includes tracking `done'.
  1347. clock-out Offer to record a note when clocking out of an item.
  1348. A separate window will then pop up and allow you to type a note.
  1349. After finishing with C-c C-c, the note will be added directly after the
  1350. timestamp, as a plain list item. See also the variable
  1351. `org-log-note-headings'.
  1352. Logging can also be configured on a per-file basis by adding one of
  1353. the following lines anywhere in the buffer:
  1354. #+STARTUP: logdone
  1355. #+STARTUP: nologging
  1356. #+STARTUP: lognotedone
  1357. #+STARTUP: lognotestate
  1358. #+STARTUP: lognoteclock-out"
  1359. :group 'org-todo
  1360. :group 'org-progress
  1361. :type '(choice
  1362. (const :tag "off" nil)
  1363. (const :tag "on" t)
  1364. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1365. (const :tag "when item is marked DONE" done)
  1366. (const :tag "when TODO state changes" state)
  1367. (const :tag "when clocking out" clock-out))))
  1368. (defcustom org-log-done-with-time t
  1369. "Non-nil means, the CLOSED time stamp will contain date and time.
  1370. When nil, only the date will be recorded."
  1371. :group 'org-progress
  1372. :type 'boolean)
  1373. (defcustom org-log-note-headings
  1374. '((done . "CLOSING NOTE %t")
  1375. (state . "State %-12s %t")
  1376. (clock-out . ""))
  1377. "Headings for notes added when clocking out or closing TODO items.
  1378. The value is an alist, with the car being a symbol indicating the note
  1379. context, and the cdr is the heading to be used. The heading may also be the
  1380. empty string.
  1381. %t in the heading will be replaced by a time stamp.
  1382. %s will be replaced by the new TODO state, in double quotes.
  1383. %u will be replaced by the user name.
  1384. %U will be replaced by the full user name."
  1385. :group 'org-todo
  1386. :group 'org-progress
  1387. :type '(list :greedy t
  1388. (cons (const :tag "Heading when closing an item" done) string)
  1389. (cons (const :tag
  1390. "Heading when changing todo state (todo sequence only)"
  1391. state) string)
  1392. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1393. (defcustom org-log-states-order-reversed t
  1394. "Non-nil means, the latest state change note will be directly after heading.
  1395. When nil, the notes will be orderer according to time."
  1396. :group 'org-todo
  1397. :group 'org-progress
  1398. :type 'boolean)
  1399. (defcustom org-log-repeat t
  1400. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1401. When nil, no note will be taken."
  1402. :group 'org-todo
  1403. :group 'org-progress
  1404. :type 'boolean)
  1405. (defcustom org-clock-out-when-done t
  1406. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1407. Nil means, clock will keep running until stopped explicitly with
  1408. `C-c C-x C-o', or until the clock is started in a different item."
  1409. :group 'org-progress
  1410. :type 'boolean)
  1411. (defgroup org-priorities nil
  1412. "Priorities in Org-mode."
  1413. :tag "Org Priorities"
  1414. :group 'org-todo)
  1415. (defcustom org-highest-priority ?A
  1416. "The highest priority of TODO items. A character like ?A, ?B etc.
  1417. Must have a smaller ASCII number than `org-lowest-priority'."
  1418. :group 'org-priorities
  1419. :type 'character)
  1420. (defcustom org-lowest-priority ?C
  1421. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1422. Must have a larger ASCII number than `org-highest-priority'."
  1423. :group 'org-priorities
  1424. :type 'character)
  1425. (defcustom org-default-priority ?B
  1426. "The default priority of TODO items.
  1427. This is the priority an item get if no explicit priority is given."
  1428. :group 'org-priorities
  1429. :type 'character)
  1430. (defgroup org-time nil
  1431. "Options concerning time stamps and deadlines in Org-mode."
  1432. :tag "Org Time"
  1433. :group 'org)
  1434. (defcustom org-insert-labeled-timestamps-at-point nil
  1435. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1436. When nil, these labeled time stamps are forces into the second line of an
  1437. entry, just after the headline. When scheduling from the global TODO list,
  1438. the time stamp will always be forced into the second line."
  1439. :group 'org-time
  1440. :type 'boolean)
  1441. (defcustom org-insert-labeled-timestamps-before-properties-drawer t
  1442. "Non-nil means, always insert planning info before property drawer.
  1443. When this is nil and there is a property drawer *directly* after
  1444. the headline, move the planning info into the drawer. If the property
  1445. drawer separated from the headline by at least one line, this variable
  1446. has no effect."
  1447. :group 'org-time
  1448. :type 'boolean)
  1449. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1450. "Formats for `format-time-string' which are used for time stamps.
  1451. It is not recommended to change this constant.")
  1452. (defcustom org-time-stamp-rounding-minutes 0
  1453. "Number of minutes to round time stamps to upon insertion.
  1454. When zero, insert the time unmodified. Useful rounding numbers
  1455. should be factors of 60, so for example 5, 10, 15.
  1456. When this is not zero, you can still force an exact time-stamp by using
  1457. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1458. :group 'org-time
  1459. :type 'integer)
  1460. (defcustom org-display-custom-times nil
  1461. "Non-nil means, overlay custom formats over all time stamps.
  1462. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1463. To turn this on on a per-file basis, insert anywhere in the file:
  1464. #+STARTUP: customtime"
  1465. :group 'org-time
  1466. :set 'set-default
  1467. :type 'sexp)
  1468. (make-variable-buffer-local 'org-display-custom-times)
  1469. (defcustom org-time-stamp-custom-formats
  1470. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1471. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1472. These are overlayed over the default ISO format if the variable
  1473. `org-display-custom-times' is set. Time like %H:%M should be at the
  1474. end of the second format."
  1475. :group 'org-time
  1476. :type 'sexp)
  1477. (defun org-time-stamp-format (&optional long inactive)
  1478. "Get the right format for a time string."
  1479. (let ((f (if long (cdr org-time-stamp-formats)
  1480. (car org-time-stamp-formats))))
  1481. (if inactive
  1482. (concat "[" (substring f 1 -1) "]")
  1483. f)))
  1484. (defcustom org-deadline-warning-days 30
  1485. "No. of days before expiration during which a deadline becomes active.
  1486. This variable governs the display in sparse trees and in the agenda."
  1487. :group 'org-time
  1488. :type 'number)
  1489. (defcustom org-popup-calendar-for-date-prompt t
  1490. "Non-nil means, pop up a calendar when prompting for a date.
  1491. In the calendar, the date can be selected with mouse-1. However, the
  1492. minibuffer will also be active, and you can simply enter the date as well.
  1493. When nil, only the minibuffer will be available."
  1494. :group 'org-time
  1495. :type 'boolean)
  1496. (defcustom org-calendar-follow-timestamp-change t
  1497. "Non-nil means, make the calendar window follow timestamp changes.
  1498. When a timestamp is modified and the calendar window is visible, it will be
  1499. moved to the new date."
  1500. :group 'org-time
  1501. :type 'boolean)
  1502. (defgroup org-tags nil
  1503. "Options concerning tags in Org-mode."
  1504. :tag "Org Tags"
  1505. :group 'org)
  1506. (defcustom org-tag-alist nil
  1507. "List of tags allowed in Org-mode files.
  1508. When this list is nil, Org-mode will base TAG input on what is already in the
  1509. buffer.
  1510. The value of this variable is an alist, the car may be (and should) be a
  1511. character that is used to select that tag through the fast-tag-selection
  1512. interface. See the manual for details."
  1513. :group 'org-tags
  1514. :type '(repeat
  1515. (choice
  1516. (cons (string :tag "Tag name")
  1517. (character :tag "Access char"))
  1518. (const :tag "Start radio group" (:startgroup))
  1519. (const :tag "End radio group" (:endgroup)))))
  1520. (defcustom org-use-fast-tag-selection 'auto
  1521. "Non-nil means, use fast tag selection scheme.
  1522. This is a special interface to select and deselect tags with single keys.
  1523. When nil, fast selection is never used.
  1524. When the symbol `auto', fast selection is used if and only if selection
  1525. characters for tags have been configured, either through the variable
  1526. `org-tag-alist' or through a #+TAGS line in the buffer.
  1527. When t, fast selection is always used and selection keys are assigned
  1528. automatically if necessary."
  1529. :group 'org-tags
  1530. :type '(choice
  1531. (const :tag "Always" t)
  1532. (const :tag "Never" nil)
  1533. (const :tag "When selection characters are configured" 'auto)))
  1534. (defcustom org-fast-tag-selection-single-key nil
  1535. "Non-nil means, fast tag selection exits after first change.
  1536. When nil, you have to press RET to exit it.
  1537. During fast tag selection, you can toggle this flag with `C-c'.
  1538. This variable can also have the value `expert'. In this case, the window
  1539. displaying the tags menu is not even shown, until you press C-c again."
  1540. :group 'org-tags
  1541. :type '(choice
  1542. (const :tag "No" nil)
  1543. (const :tag "Yes" t)
  1544. (const :tag "Expert" expert)))
  1545. (defcustom org-tags-column 48
  1546. "The column to which tags should be indented in a headline.
  1547. If this number is positive, it specifies the column. If it is negative,
  1548. it means that the tags should be flushright to that column. For example,
  1549. -79 works well for a normal 80 character screen."
  1550. :group 'org-tags
  1551. :type 'integer)
  1552. (defcustom org-auto-align-tags t
  1553. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1554. These operations change the length of a headline and therefore shift
  1555. the tags around. With this options turned on, after each such operation
  1556. the tags are again aligned to `org-tags-column'."
  1557. :group 'org-tags
  1558. :type 'boolean)
  1559. (defcustom org-use-tag-inheritance t
  1560. "Non-nil means, tags in levels apply also for sublevels.
  1561. When nil, only the tags directly given in a specific line apply there.
  1562. If you turn off this option, you very likely want to turn on the
  1563. companion option `org-tags-match-list-sublevels'."
  1564. :group 'org-tags
  1565. :type 'boolean)
  1566. (defcustom org-tags-match-list-sublevels nil
  1567. "Non-nil means list also sublevels of headlines matching tag search.
  1568. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1569. the sublevels of a headline matching a tag search often also match
  1570. the same search. Listing all of them can create very long lists.
  1571. Setting this variable to nil causes subtrees of a match to be skipped.
  1572. This option is off by default, because inheritance in on. If you turn
  1573. inheritance off, you very likely want to turn this option on.
  1574. As a special case, if the tag search is restricted to TODO items, the
  1575. value of this variable is ignored and sublevels are always checked, to
  1576. make sure all corresponding TODO items find their way into the list."
  1577. :group 'org-tags
  1578. :type 'boolean)
  1579. (defvar org-tags-history nil
  1580. "History of minibuffer reads for tags.")
  1581. (defvar org-last-tags-completion-table nil
  1582. "The last used completion table for tags.")
  1583. (defgroup org-properties nil
  1584. "Options concerning properties in Org-mode."
  1585. :tag "Org Properties"
  1586. :group 'org)
  1587. (defcustom org-property-format "%-10s %s"
  1588. "How property key/value pairs should be formatted by `indent-line'.
  1589. When `indent-line' hits a property definition, it will format the line
  1590. according to this format, mainly to make sure that the values are
  1591. lined-up with respect to each other."
  1592. :group 'org-properties
  1593. :type 'string)
  1594. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1595. "The default column format, if no other format has been defined.
  1596. This variable can be set on the per-file basis by inserting a line
  1597. #+COLUMNS: %25ITEM ....."
  1598. :group 'org-properties
  1599. :type 'string)
  1600. (defcustom org-global-properties nil
  1601. "List of property/value pairs that can be inherited by any entry.
  1602. You can set buffer-local values for this by adding lines like
  1603. #+PROPERTY: NAME VALUE"
  1604. :group 'org-properties
  1605. :type '(repeat
  1606. (cons (string :tag "Property")
  1607. (string :tag "Value"))))
  1608. (defvar org-local-properties nil
  1609. "List of property/value pairs that can be inherited by any entry.
  1610. Valid for the current buffer.
  1611. This variable is populated from #+PROPERTY lines.")
  1612. (defgroup org-agenda nil
  1613. "Options concerning agenda views in Org-mode."
  1614. :tag "Org Agenda"
  1615. :group 'org)
  1616. (defvar org-category nil
  1617. "Variable used by org files to set a category for agenda display.
  1618. Such files should use a file variable to set it, for example
  1619. # -*- mode: org; org-category: \"ELisp\"
  1620. or contain a special line
  1621. #+CATEGORY: ELisp
  1622. If the file does not specify a category, then file's base name
  1623. is used instead.")
  1624. (make-variable-buffer-local 'org-category)
  1625. (defcustom org-agenda-files nil
  1626. "The files to be used for agenda display.
  1627. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1628. \\[org-remove-file]. You can also use customize to edit the list.
  1629. If the value of the variable is not a list but a single file name, then
  1630. the list of agenda files is actually stored and maintained in that file, one
  1631. agenda file per line."
  1632. :group 'org-agenda
  1633. :type '(choice
  1634. (repeat :tag "List of files" file)
  1635. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1636. (defcustom org-agenda-confirm-kill 1
  1637. "When set, remote killing from the agenda buffer needs confirmation.
  1638. When t, a confirmation is always needed. When a number N, confirmation is
  1639. only needed when the text to be killed contains more than N non-white lines."
  1640. :group 'org-agenda
  1641. :type '(choice
  1642. (const :tag "Never" nil)
  1643. (const :tag "Always" t)
  1644. (number :tag "When more than N lines")))
  1645. (defcustom org-calendar-to-agenda-key [?c]
  1646. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1647. The command `org-calendar-goto-agenda' will be bound to this key. The
  1648. default is the character `c' because then `c' can be used to switch back and
  1649. forth between agenda and calendar."
  1650. :group 'org-agenda
  1651. :type 'sexp)
  1652. (defgroup org-agenda-export nil
  1653. "Options concerning exporting agenda views in Org-mode."
  1654. :tag "Org Agenda Export"
  1655. :group 'org-agenda)
  1656. (defcustom org-agenda-with-colors t
  1657. "Non-nil means, use colors in agenda views."
  1658. :group 'org-agenda-export
  1659. :type 'boolean)
  1660. (defcustom org-agenda-exporter-settings nil
  1661. "Alist of variable/value pairs that should be active during agenda export.
  1662. This is a good place to set uptions for ps-print and for htmlize."
  1663. :group 'org-agenda-export
  1664. :type '(repeat
  1665. (list
  1666. (variable)
  1667. (sexp :tag "Value"))))
  1668. (defcustom org-agenda-export-html-style ""
  1669. "The style specification for exported HTML Agenda files.
  1670. If this variable contains a string, it will replace the default <style>
  1671. section as produced by `htmlize'.
  1672. Since there are different ways of setting style information, this variable
  1673. needs to contain the full HTML structure to provide a style, including the
  1674. surrounding HTML tags. The style specifications should include definitions
  1675. the fonts used by the agenda, here is an example:
  1676. <style type=\"text/css\">
  1677. p { font-weight: normal; color: gray; }
  1678. .org-agenda-structure {
  1679. font-size: 110%;
  1680. color: #003399;
  1681. font-weight: 600;
  1682. }
  1683. .org-todo {
  1684. color: #cc6666;Week-agenda:
  1685. font-weight: bold;
  1686. }
  1687. .org-done {
  1688. color: #339933;
  1689. }
  1690. .title { text-align: center; }
  1691. .todo, .deadline { color: red; }
  1692. .done { color: green; }
  1693. </style>
  1694. or, if you want to keep the style in a file,
  1695. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1696. As the value of this option simply gets inserted into the HTML <head> header,
  1697. you can \"misuse\" it to also add other text to the header. However,
  1698. <style>...</style> is required, if not present the variable will be ignored."
  1699. :group 'org-agenda-export
  1700. :group 'org-export-html
  1701. :type 'string)
  1702. (defgroup org-agenda-custom-commands nil
  1703. "Options concerning agenda views in Org-mode."
  1704. :tag "Org Agenda Custom Commands"
  1705. :group 'org-agenda)
  1706. (defcustom org-agenda-custom-commands nil
  1707. "Custom commands for the agenda.
  1708. These commands will be offered on the splash screen displayed by the
  1709. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1710. (key type match options files)
  1711. key The key (a single char as a string) to be associated with the command.
  1712. type The command type, any of the following symbols:
  1713. todo Entries with a specific TODO keyword, in all agenda files.
  1714. tags Tags match in all agenda files.
  1715. tags-todo Tags match in all agenda files, TODO entries only.
  1716. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1717. tags-tree Sparse tree with all tags matches in *current* file.
  1718. occur-tree Occur sparse tree for *current* file.
  1719. match What to search for:
  1720. - a single keyword for TODO keyword searches
  1721. - a tags match expression for tags searches
  1722. - a regular expression for occur searches
  1723. options A list of option settings, similar to that in a let form, so like
  1724. this: ((opt1 val1) (opt2 val2) ...)
  1725. files A list of files file to write the produced agenda buffer to
  1726. with the command `org-store-agenda-views'.
  1727. If a file name ends in \".html\", an HTML version of the buffer
  1728. is written out. If it ends in \".ps\", a postscript version is
  1729. produced. Otherwide, only the plain text is written to the file.
  1730. You can also define a set of commands, to create a composite agenda buffer.
  1731. In this case, an entry looks like this:
  1732. (key desc (cmd1 cmd2 ...) general-options file)
  1733. where
  1734. desc A description string to be displayed in the dispatcher menu.
  1735. cmd An agenda command, similar to the above. However, tree commands
  1736. are no allowed, but instead you can get agenda and global todo list.
  1737. So valid commands for a set are:
  1738. (agenda)
  1739. (alltodo)
  1740. (stuck)
  1741. (todo \"match\" options files)
  1742. (tags \"match\" options files)
  1743. (tags-todo \"match\" options files)
  1744. Each command can carry a list of options, and another set of options can be
  1745. given for the whole set of commands. Individual command options take
  1746. precedence over the general options."
  1747. :group 'org-agenda-custom-commands
  1748. :type '(repeat
  1749. (choice :value ("a" tags "" nil)
  1750. (list :tag "Single command"
  1751. (string :tag "Key")
  1752. (choice
  1753. (const :tag "Agenda" agenda)
  1754. (const :tag "TODO list" alltodo)
  1755. (const :tag "Stuck projects" stuck)
  1756. (const :tag "Tags search (all agenda files)" tags)
  1757. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1758. (const :tag "TODO keyword search (all agenda files)" todo)
  1759. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1760. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1761. (const :tag "Occur tree (current buffer)" occur-tree)
  1762. (symbol :tag "Other, user-defined function"))
  1763. (string :tag "Match")
  1764. (repeat :tag "Local options"
  1765. (list (variable :tag "Option") (sexp :tag "Value")))
  1766. (option (repeat :tag "Export" (file :tag "Export to"))))
  1767. (list :tag "Command series, all agenda files"
  1768. (string :tag "Key")
  1769. (string :tag "Description")
  1770. (repeat
  1771. (choice
  1772. (const :tag "Agenda" (agenda))
  1773. (const :tag "TODO list" (alltodo))
  1774. (const :tag "Stuck projects" (stuck))
  1775. (list :tag "Tags search"
  1776. (const :format "" tags)
  1777. (string :tag "Match")
  1778. (repeat :tag "Local options"
  1779. (list (variable :tag "Option")
  1780. (sexp :tag "Value"))))
  1781. (list :tag "Tags search, TODO entries only"
  1782. (const :format "" tags-todo)
  1783. (string :tag "Match")
  1784. (repeat :tag "Local options"
  1785. (list (variable :tag "Option")
  1786. (sexp :tag "Value"))))
  1787. (list :tag "TODO keyword search"
  1788. (const :format "" todo)
  1789. (string :tag "Match")
  1790. (repeat :tag "Local options"
  1791. (list (variable :tag "Option")
  1792. (sexp :tag "Value"))))
  1793. (list :tag "Other, user-defined function"
  1794. (symbol :tag "function")
  1795. (string :tag "Match")
  1796. (repeat :tag "Local options"
  1797. (list (variable :tag "Option")
  1798. (sexp :tag "Value"))))))
  1799. (repeat :tag "General options"
  1800. (list (variable :tag "Option")
  1801. (sexp :tag "Value")))
  1802. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1803. (defcustom org-stuck-projects
  1804. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1805. "How to identify stuck projects.
  1806. This is a list of four items:
  1807. 1. A tags/todo matcher string that is used to identify a project.
  1808. The entire tree below a headline matched by this is considered one project.
  1809. 2. A list of TODO keywords identifying non-stuck projects.
  1810. If the project subtree contains any headline with one of these todo
  1811. keywords, the project is considered to be not stuck. If you specify
  1812. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1813. 3. A list of tags identifying non-stuck projects.
  1814. If the project subtree contains any headline with one of these tags,
  1815. the project is considered to be not stuck. If you specify \"*\" as
  1816. a tag, any tag will mark the project unstuck.
  1817. 4. An arbitrary regular expression matching non-stuck projects.
  1818. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1819. or `C-c a #' to produce the list."
  1820. :group 'org-agenda-custom-commands
  1821. :type '(list
  1822. (string :tag "Tags/TODO match to identify a project")
  1823. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1824. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1825. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1826. (defgroup org-agenda-skip nil
  1827. "Options concerning skipping parts of agenda files."
  1828. :tag "Org Agenda Skip"
  1829. :group 'org-agenda)
  1830. (defcustom org-agenda-todo-list-sublevels t
  1831. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1832. When nil, the sublevels of a TODO entry are not checked, resulting in
  1833. potentially much shorter TODO lists."
  1834. :group 'org-agenda-skip
  1835. :group 'org-todo
  1836. :type 'boolean)
  1837. (defcustom org-agenda-todo-ignore-scheduled nil
  1838. "Non-nil means, don't show scheduled entries in the global todo list.
  1839. The idea behind this is that by scheduling it, you have already taken care
  1840. of this item."
  1841. :group 'org-agenda-skip
  1842. :group 'org-todo
  1843. :type 'boolean)
  1844. (defcustom org-agenda-todo-ignore-deadlines nil
  1845. "Non-nil means, don't show near deadline entries in the global todo list.
  1846. Near means closer than `org-deadline-warning-days' days.
  1847. The idea behind this is that such items will appear in the agenda anyway."
  1848. :group 'org-agenda-skip
  1849. :group 'org-todo
  1850. :type 'boolean)
  1851. (defcustom org-agenda-skip-scheduled-if-done nil
  1852. "Non-nil means don't show scheduled items in agenda when they are done.
  1853. This is relevant for the daily/weekly agenda, not for the TODO list. And
  1854. it applied only to the actualy date of the scheduling. Warnings about
  1855. an item with a past scheduling dates are always turned off when the item
  1856. is DONE."
  1857. :group 'org-agenda-skip
  1858. :type 'boolean)
  1859. (defcustom org-agenda-skip-deadline-if-done nil
  1860. "Non-nil means don't show deadines when the corresponding item is done.
  1861. When nil, the deadline is still shown and should give you a happy feeling.
  1862. This is relevant for the daily/weekly agenda. And it applied only to the
  1863. actualy date of the deadline. Warnings about approching and past-due
  1864. deadlines are always turned off when the item is DONE."
  1865. :group 'org-agenda-skip
  1866. :type 'boolean)
  1867. (defcustom org-timeline-show-empty-dates 3
  1868. "Non-nil means, `org-timeline' also shows dates without an entry.
  1869. When nil, only the days which actually have entries are shown.
  1870. When t, all days between the first and the last date are shown.
  1871. When an integer, show also empty dates, but if there is a gap of more than
  1872. N days, just insert a special line indicating the size of the gap."
  1873. :group 'org-agenda-skip
  1874. :type '(choice
  1875. (const :tag "None" nil)
  1876. (const :tag "All" t)
  1877. (number :tag "at most")))
  1878. (defgroup org-agenda-startup nil
  1879. "Options concerning initial settings in the Agenda in Org Mode."
  1880. :tag "Org Agenda Startup"
  1881. :group 'org-agenda)
  1882. (defcustom org-finalize-agenda-hook nil
  1883. "Hook run just before displaying an agenda buffer."
  1884. :group 'org-agenda-startup
  1885. :type 'hook)
  1886. (defcustom org-agenda-mouse-1-follows-link nil
  1887. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1888. A longer mouse click will still set point. Does not wortk on XEmacs.
  1889. Needs to be set before org.el is loaded."
  1890. :group 'org-agenda-startup
  1891. :type 'boolean)
  1892. (defcustom org-agenda-start-with-follow-mode nil
  1893. "The initial value of follow-mode in a newly created agenda window."
  1894. :group 'org-agenda-startup
  1895. :type 'boolean)
  1896. (defgroup org-agenda-windows nil
  1897. "Options concerning the windows used by the Agenda in Org Mode."
  1898. :tag "Org Agenda Windows"
  1899. :group 'org-agenda)
  1900. (defcustom org-agenda-window-setup 'reorganize-frame
  1901. "How the agenda buffer should be displayed.
  1902. Possible values for this option are:
  1903. current-window Show agenda in the current window, keeping all other windows.
  1904. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  1905. other-window Use `switch-to-buffer-other-window' to display agenda.
  1906. reorganize-frame Show only two windows on the current frame, the current
  1907. window and the agenda.
  1908. See also the variable `org-agenda-restore-windows-after-quit'."
  1909. :group 'org-agenda-windows
  1910. :type '(choice
  1911. (const current-window)
  1912. (const other-frame)
  1913. (const other-window)
  1914. (const reorganize-frame)))
  1915. (defcustom org-agenda-restore-windows-after-quit nil
  1916. "Non-nil means, restore window configuration open exiting agenda.
  1917. Before the window configuration is changed for displaying the agenda,
  1918. the current status is recorded. When the agenda is exited with
  1919. `q' or `x' and this option is set, the old state is restored. If
  1920. `org-agenda-window-setup' is `other-frame', the value of this
  1921. option will be ignored.."
  1922. :group 'org-agenda-windows
  1923. :type 'boolean)
  1924. (defcustom org-indirect-buffer-display 'other-window
  1925. "How should indirect tree buffers be displayed?
  1926. This applies to indirect buffers created with the commands
  1927. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1928. Valid values are:
  1929. current-window Display in the current window
  1930. other-window Just display in another window.
  1931. dedicated-frame Create one new frame, and re-use it each time.
  1932. new-frame Make a new frame each time."
  1933. :group 'org-structure
  1934. :group 'org-agenda-windows
  1935. :type '(choice
  1936. (const :tag "In current window" current-window)
  1937. (const :tag "In current frame, other window" other-window)
  1938. (const :tag "Each time a new frame" new-frame)
  1939. (const :tag "One dedicated frame" dedicated-frame)))
  1940. (defgroup org-agenda-daily/weekly nil
  1941. "Options concerning the daily/weekly agenda."
  1942. :tag "Org Agenda Daily/Weekly"
  1943. :group 'org-agenda)
  1944. (defcustom org-agenda-ndays 7
  1945. "Number of days to include in overview display.
  1946. Should be 1 or 7."
  1947. :group 'org-agenda-daily/weekly
  1948. :type 'number)
  1949. (defcustom org-agenda-start-on-weekday 1
  1950. "Non-nil means, start the overview always on the specified weekday.
  1951. 0 denotes Sunday, 1 denotes Monday etc.
  1952. When nil, always start on the current day."
  1953. :group 'org-agenda-daily/weekly
  1954. :type '(choice (const :tag "Today" nil)
  1955. (number :tag "Weekday No.")))
  1956. (defcustom org-agenda-show-all-dates t
  1957. "Non-nil means, `org-agenda' shows every day in the selected range.
  1958. When nil, only the days which actually have entries are shown."
  1959. :group 'org-agenda-daily/weekly
  1960. :type 'boolean)
  1961. (defcustom org-agenda-date-format "%A %d %B %Y"
  1962. "Format string for displaying dates in the agenda.
  1963. Used by the daily/weekly agenda and by the timeline. This should be
  1964. a format string understood by `format-time-string'.
  1965. FIXME: Not used currently, because of timezone problem."
  1966. :group 'org-agenda-daily/weekly
  1967. :type 'string)
  1968. (defcustom org-agenda-include-diary nil
  1969. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1970. :group 'org-agenda-daily/weekly
  1971. :type 'boolean)
  1972. (defcustom org-agenda-include-all-todo nil
  1973. "Set means weekly/daily agenda will always contain all TODO entries.
  1974. The TODO entries will be listed at the top of the agenda, before
  1975. the entries for specific days."
  1976. :group 'org-agenda-daily/weekly
  1977. :type 'boolean)
  1978. (defcustom org-agenda-repeating-timestamp-show-all t
  1979. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  1980. When nil, only one occurence is shown, either today or the
  1981. nearest into the future."
  1982. :group 'org-agenda-daily/weekly
  1983. :type 'boolean)
  1984. (defgroup org-agenda-time-grid nil
  1985. "Options concerning the time grid in the Org-mode Agenda."
  1986. :tag "Org Agenda Time Grid"
  1987. :group 'org-agenda)
  1988. (defcustom org-agenda-use-time-grid t
  1989. "Non-nil means, show a time grid in the agenda schedule.
  1990. A time grid is a set of lines for specific times (like every two hours between
  1991. 8:00 and 20:00). The items scheduled for a day at specific times are
  1992. sorted in between these lines.
  1993. For details about when the grid will be shown, and what it will look like, see
  1994. the variable `org-agenda-time-grid'."
  1995. :group 'org-agenda-time-grid
  1996. :type 'boolean)
  1997. (defcustom org-agenda-time-grid
  1998. '((daily today require-timed)
  1999. "----------------"
  2000. (800 1000 1200 1400 1600 1800 2000))
  2001. "The settings for time grid for agenda display.
  2002. This is a list of three items. The first item is again a list. It contains
  2003. symbols specifying conditions when the grid should be displayed:
  2004. daily if the agenda shows a single day
  2005. weekly if the agenda shows an entire week
  2006. today show grid on current date, independent of daily/weekly display
  2007. require-timed show grid only if at least one item has a time specification
  2008. The second item is a string which will be places behing the grid time.
  2009. The third item is a list of integers, indicating the times that should have
  2010. a grid line."
  2011. :group 'org-agenda-time-grid
  2012. :type
  2013. '(list
  2014. (set :greedy t :tag "Grid Display Options"
  2015. (const :tag "Show grid in single day agenda display" daily)
  2016. (const :tag "Show grid in weekly agenda display" weekly)
  2017. (const :tag "Always show grid for today" today)
  2018. (const :tag "Show grid only if any timed entries are present"
  2019. require-timed)
  2020. (const :tag "Skip grid times already present in an entry"
  2021. remove-match))
  2022. (string :tag "Grid String")
  2023. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2024. (defgroup org-agenda-sorting nil
  2025. "Options concerning sorting in the Org-mode Agenda."
  2026. :tag "Org Agenda Sorting"
  2027. :group 'org-agenda)
  2028. (let ((sorting-choice
  2029. '(choice
  2030. (const time-up) (const time-down)
  2031. (const category-keep) (const category-up) (const category-down)
  2032. (const tag-down) (const tag-up)
  2033. (const priority-up) (const priority-down))))
  2034. (defcustom org-agenda-sorting-strategy
  2035. '((agenda time-up category-keep priority-down)
  2036. (todo category-keep priority-down)
  2037. (tags category-keep priority-down))
  2038. "Sorting structure for the agenda items of a single day.
  2039. This is a list of symbols which will be used in sequence to determine
  2040. if an entry should be listed before another entry. The following
  2041. symbols are recognized:
  2042. time-up Put entries with time-of-day indications first, early first
  2043. time-down Put entries with time-of-day indications first, late first
  2044. category-keep Keep the default order of categories, corresponding to the
  2045. sequence in `org-agenda-files'.
  2046. category-up Sort alphabetically by category, A-Z.
  2047. category-down Sort alphabetically by category, Z-A.
  2048. tag-up Sort alphabetically by last tag, A-Z.
  2049. tag-down Sort alphabetically by last tag, Z-A.
  2050. priority-up Sort numerically by priority, high priority last.
  2051. priority-down Sort numerically by priority, high priority first.
  2052. The different possibilities will be tried in sequence, and testing stops
  2053. if one comparison returns a \"not-equal\". For example, the default
  2054. '(time-up category-keep priority-down)
  2055. means: Pull out all entries having a specified time of day and sort them,
  2056. in order to make a time schedule for the current day the first thing in the
  2057. agenda listing for the day. Of the entries without a time indication, keep
  2058. the grouped in categories, don't sort the categories, but keep them in
  2059. the sequence given in `org-agenda-files'. Within each category sort by
  2060. priority.
  2061. Leaving out `category-keep' would mean that items will be sorted across
  2062. categories by priority."
  2063. :group 'org-agenda-sorting
  2064. :type `(choice
  2065. (repeat :tag "General" ,sorting-choice)
  2066. (list :tag "Individually"
  2067. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2068. (repeat ,sorting-choice))
  2069. (cons (const :tag "Strategy for TODO lists" todo)
  2070. (repeat ,sorting-choice))
  2071. (cons (const :tag "Strategy for Tags matches" tags)
  2072. (repeat ,sorting-choice))))))
  2073. (defcustom org-sort-agenda-notime-is-late t
  2074. "Non-nil means, items without time are considered late.
  2075. This is only relevant for sorting. When t, items which have no explicit
  2076. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2077. do have a time. When nil, the default time is before 0:00. You can use this
  2078. option to decide if the schedule for today should come before or after timeless
  2079. agenda entries."
  2080. :group 'org-agenda-sorting
  2081. :type 'boolean)
  2082. (defgroup org-agenda-prefix nil
  2083. "Options concerning the entry prefix in the Org-mode agenda display."
  2084. :tag "Org Agenda Prefix"
  2085. :group 'org-agenda)
  2086. (defcustom org-agenda-prefix-format
  2087. '((agenda . " %-12:c%?-12t% s")
  2088. (timeline . " % s")
  2089. (todo . " %-12:c")
  2090. (tags . " %-12:c"))
  2091. "Format specifications for the prefix of items in the agenda views.
  2092. An alist with four entries, for the different agenda types. The keys to the
  2093. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2094. are format strings.
  2095. This format works similar to a printf format, with the following meaning:
  2096. %c the category of the item, \"Diary\" for entries from the diary, or
  2097. as given by the CATEGORY keyword or derived from the file name.
  2098. %T the *last* tag of the item. Last because inherited tags come
  2099. first in the list.
  2100. %t the time-of-day specification if one applies to the entry, in the
  2101. format HH:MM
  2102. %s Scheduling/Deadline information, a short string
  2103. All specifiers work basically like the standard `%s' of printf, but may
  2104. contain two additional characters: A question mark just after the `%' and
  2105. a whitespace/punctuation character just before the final letter.
  2106. If the first character after `%' is a question mark, the entire field
  2107. will only be included if the corresponding value applies to the
  2108. current entry. This is useful for fields which should have fixed
  2109. width when present, but zero width when absent. For example,
  2110. \"%?-12t\" will result in a 12 character time field if a time of the
  2111. day is specified, but will completely disappear in entries which do
  2112. not contain a time.
  2113. If there is punctuation or whitespace character just before the final
  2114. format letter, this character will be appended to the field value if
  2115. the value is not empty. For example, the format \"%-12:c\" leads to
  2116. \"Diary: \" if the category is \"Diary\". If the category were be
  2117. empty, no additional colon would be interted.
  2118. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2119. - Indent the line with two space characters
  2120. - Give the category in a 12 chars wide field, padded with whitespace on
  2121. the right (because of `-'). Append a colon if there is a category
  2122. (because of `:').
  2123. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2124. time, don't put in an empty field, just skip it (because of '?').
  2125. - Finally, put the scheduling information and append a whitespace.
  2126. As another example, if you don't want the time-of-day of entries in
  2127. the prefix, you could use:
  2128. (setq org-agenda-prefix-format \" %-11:c% s\")
  2129. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2130. `org-agenda-remove-tags'."
  2131. :type '(choice
  2132. (string :tag "General format")
  2133. (list :greedy t :tag "View dependent"
  2134. (cons (const agenda) (string :tag "Format"))
  2135. (cons (const timeline) (string :tag "Format"))
  2136. (cons (const todo) (string :tag "Format"))
  2137. (cons (const tags) (string :tag "Format"))))
  2138. :group 'org-agenda-prefix)
  2139. (defvar org-prefix-format-compiled nil
  2140. "The compiled version of the most recently used prefix format.
  2141. See the variable `org-agenda-prefix-format'.")
  2142. (defcustom org-agenda-remove-times-when-in-prefix t
  2143. "Non-nil means, remove duplicate time specifications in agenda items.
  2144. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2145. time-of-day specification in a headline or diary entry is extracted and
  2146. placed into the prefix. If this option is non-nil, the original specification
  2147. \(a timestamp or -range, or just a plain time(range) specification like
  2148. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2149. cluttered.
  2150. The option can be t or nil. It may also be the symbol `beg', indicating
  2151. that the time should only be removed what it is located at the beginning of
  2152. the headline/diary entry."
  2153. :group 'org-agenda-prefix
  2154. :type '(choice
  2155. (const :tag "Always" t)
  2156. (const :tag "Never" nil)
  2157. (const :tag "When at beginning of entry" beg)))
  2158. (defcustom org-agenda-default-appointment-duration nil
  2159. "Default duration for appointments that only have a starting time.
  2160. When nil, no duration is specified in such cases.
  2161. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2162. :group 'org-agenda-prefix
  2163. :type '(choice
  2164. (integer :tag "Minutes")
  2165. (const :tag "No default duration")))
  2166. (defcustom org-agenda-remove-tags nil
  2167. "Non-nil means, remove the tags from the headline copy in the agenda.
  2168. When this is the symbol `prefix', only remove tags when
  2169. `org-agenda-prefix-format' contains a `%T' specifier."
  2170. :group 'org-agenda-prefix
  2171. :type '(choice
  2172. (const :tag "Always" t)
  2173. (const :tag "Never" nil)
  2174. (const :tag "When prefix format contains %T" prefix)))
  2175. (if (fboundp 'defvaralias)
  2176. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2177. 'org-agenda-remove-tags))
  2178. (defcustom org-agenda-align-tags-to-column 65
  2179. "Shift tags in agenda items to this column."
  2180. :group 'org-agenda-prefix
  2181. :type 'integer)
  2182. (defgroup org-latex nil
  2183. "Options for embedding LaTeX code into Org-mode"
  2184. :tag "Org LaTeX"
  2185. :group 'org)
  2186. (defcustom org-format-latex-options
  2187. '(:foreground default :background default :scale 1.0
  2188. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2189. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2190. "Options for creating images from LaTeX fragments.
  2191. This is a property list with the following properties:
  2192. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2193. `default' means use the forground of the default face.
  2194. :background the background color, or \"Transparent\".
  2195. `default' means use the background of the default face.
  2196. :scale a scaling factor for the size of the images
  2197. :html-foreground, :html-background, :html-scale
  2198. The same numbers for HTML export.
  2199. :matchers a list indicating which matchers should be used to
  2200. find LaTeX fragments. Valid members of this list are:
  2201. \"begin\" find environments
  2202. \"$\" find math expressions surrounded by $...$
  2203. \"$$\" find math expressions surrounded by $$....$$
  2204. \"\\(\" find math expressions surrounded by \\(...\\)
  2205. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2206. :group 'org-export-latex
  2207. :type 'plist)
  2208. (defcustom org-format-latex-header "\\documentclass{article}
  2209. \\usepackage{fullpage} % do not remove
  2210. \\usepackage{amssymb}
  2211. \\usepackage[usenames]{color}
  2212. \\usepackage{amsmath}
  2213. \\usepackage{latexsym}
  2214. \\usepackage[mathscr]{eucal}
  2215. \\pagestyle{empty} % do not remove"
  2216. "The document header used for processing LaTeX fragments."
  2217. :group 'org-export-latex
  2218. :type 'string)
  2219. (defgroup org-export nil
  2220. "Options for exporting org-listings."
  2221. :tag "Org Export"
  2222. :group 'org)
  2223. (defgroup org-export-general nil
  2224. "General options for exporting Org-mode files."
  2225. :tag "Org Export General"
  2226. :group 'org-export)
  2227. (defcustom org-export-publishing-directory "."
  2228. "Path to the location where exported files should be located.
  2229. This path may be relative to the directory where the Org-mode file lives.
  2230. The default is to put them into the same directory as the Org-mode file.
  2231. The variable may also be an alist with export types `:html', `:ascii',
  2232. `:ical', or `:xoxo' and the corresponding directories. If a directory path
  2233. is relative, it is interpreted relative to the directory where the exported
  2234. Org-mode files lives."
  2235. :group 'org-export-general
  2236. :type '(choice
  2237. (directory)
  2238. (repeat
  2239. (cons
  2240. (choice :tag "Type"
  2241. (const :html) (const :ascii) (const :ical) (const :xoxo))
  2242. (directory)))))
  2243. (defcustom org-export-language-setup
  2244. '(("en" "Author" "Date" "Table of Contents")
  2245. ("cs" "Autor" "Datum" "Obsah")
  2246. ("da" "Ophavsmand" "Dato" "Indhold")
  2247. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2248. ("es" "Autor" "Fecha" "\xccndice")
  2249. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  2250. ("it" "Autore" "Data" "Indice")
  2251. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2252. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2253. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2254. "Terms used in export text, translated to different languages.
  2255. Use the variable `org-export-default-language' to set the language,
  2256. or use the +OPTION lines for a per-file setting."
  2257. :group 'org-export-general
  2258. :type '(repeat
  2259. (list
  2260. (string :tag "HTML language tag")
  2261. (string :tag "Author")
  2262. (string :tag "Date")
  2263. (string :tag "Table of Contents"))))
  2264. (defcustom org-export-default-language "en"
  2265. "The default language of HTML export, as a string.
  2266. This should have an association in `org-export-language-setup'."
  2267. :group 'org-export-general
  2268. :type 'string)
  2269. (defcustom org-export-skip-text-before-1st-heading t
  2270. "Non-nil means, skip all text before the first headline when exporting.
  2271. When nil, that text is exported as well."
  2272. :group 'org-export-general
  2273. :type 'boolean)
  2274. (defcustom org-export-headline-levels 3
  2275. "The last level which is still exported as a headline.
  2276. Inferior levels will produce itemize lists when exported.
  2277. Note that a numeric prefix argument to an exporter function overrides
  2278. this setting.
  2279. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2280. :group 'org-export-general
  2281. :type 'number)
  2282. (defcustom org-export-with-section-numbers t
  2283. "Non-nil means, add section numbers to headlines when exporting.
  2284. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2285. :group 'org-export-general
  2286. :type 'boolean)
  2287. (defcustom org-export-with-toc t
  2288. "Non-nil means, create a table of contents in exported files.
  2289. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2290. When an integer, include levels up to N in the toc, this may then be
  2291. different from `org-export-headline-levels', but it will not be allowed
  2292. to be larger than the number of headline levels.
  2293. When nil, no table of contents is made.
  2294. Headlines which contain any TODO items will be marked with \"(*)\" in
  2295. ASCII export, and with red color in HTML output, if the option
  2296. `org-export-mark-todo-in-toc' is set.
  2297. In HTML output, the TOC will be clickable.
  2298. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2299. or \"toc:3\"."
  2300. :group 'org-export-general
  2301. :type '(choice
  2302. (const :tag "No Table of Contents" nil)
  2303. (const :tag "Full Table of Contents" t)
  2304. (integer :tag "TOC to level")))
  2305. (defcustom org-export-mark-todo-in-toc nil
  2306. "Non-nil means, mark TOC lines that contain any open TODO items."
  2307. :group 'org-export-general
  2308. :type 'boolean)
  2309. (defcustom org-export-preserve-breaks nil
  2310. "Non-nil means, preserve all line breaks when exporting.
  2311. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2312. export, line breaks will always be preserved, regardless of this variable.
  2313. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2314. :group 'org-export-general
  2315. :type 'boolean)
  2316. (defcustom org-export-with-archived-trees 'headline
  2317. "Whether subtrees with the ARCHIVE tag should be exported.
  2318. This can have three different values
  2319. nil Do not export, pretend this tree is not present
  2320. t Do export the entire tree
  2321. headline Only export the headline, but skip the tree below it."
  2322. :group 'org-export-general
  2323. :group 'org-archive
  2324. :type '(choice
  2325. (const :tag "not at all" nil)
  2326. (const :tag "headline only" 'headline)
  2327. (const :tag "entirely" t)))
  2328. (defcustom org-export-author-info t
  2329. "Non-nil means, insert author name and email into the exported file.
  2330. This option can also be set with the +OPTIONS line,
  2331. e.g. \"author-info:nil\"."
  2332. :group 'org-export-general
  2333. :type 'boolean)
  2334. (defcustom org-export-time-stamp-file t
  2335. "Non-nil means, insert a time stamp into the exported file.
  2336. The time stamp shows when the file was created.
  2337. This option can also be set with the +OPTIONS line,
  2338. e.g. \"timestamp:nil\"."
  2339. :group 'org-export-general
  2340. :type 'boolean)
  2341. (defcustom org-export-with-timestamps t
  2342. "If nil, do not export time stamps and associated keywords."
  2343. :group 'org-export-general
  2344. :type 'boolean)
  2345. (defcustom org-export-remove-timestamps-from-toc t
  2346. "If nil, remove timestamps from the table of contents entries."
  2347. :group 'org-export-general
  2348. :type 'boolean)
  2349. (defcustom org-export-with-tags 'not-in-toc
  2350. "If nil, do not export tags, just remove them from headlines.
  2351. If this is the symbol `not-in-toc', tags will be removed from table of
  2352. contents entries, but still be shown in the headlines of the document."
  2353. :group 'org-export-general
  2354. :type '(choice
  2355. (const :tag "Off" nil)
  2356. (const :tag "Not in TOC" not-in-toc)
  2357. (const :tag "On" t)))
  2358. (defcustom org-export-with-property-drawer nil
  2359. "Non-nil means, export property drawers.
  2360. When nil, these drawers are removed before export.
  2361. This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
  2362. :group 'org-export-general
  2363. :type 'boolean)
  2364. (defgroup org-export-translation nil
  2365. "Options for translating special ascii sequences for the export backends."
  2366. :tag "Org Export Translation"
  2367. :group 'org-export)
  2368. (defcustom org-export-with-emphasize t
  2369. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2370. If the export target supports emphasizing text, the word will be
  2371. typeset in bold, italic, or underlined, respectively. Works only for
  2372. single words, but you can say: I *really* *mean* *this*.
  2373. Not all export backends support this.
  2374. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2375. :group 'org-export-translation
  2376. :type 'boolean)
  2377. (defcustom org-export-with-footnotes t
  2378. "If nil, export [1] as a footnote marker.
  2379. Lines starting with [1] will be formatted as footnotes.
  2380. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2381. :group 'org-export-translation
  2382. :type 'boolean)
  2383. (defcustom org-export-with-sub-superscripts t
  2384. "Non-nil means, interpret \"_\" and \"^\" for export.
  2385. When this option is turned on, you can use TeX-like syntax for sub- and
  2386. superscripts. Several characters after \"_\" or \"^\" will be
  2387. considered as a single item - so grouping with {} is normally not
  2388. needed. For example, the following things will be parsed as single
  2389. sub- or superscripts.
  2390. 10^24 or 10^tau several digits will be considered 1 item.
  2391. 10^-12 or 10^-tau a leading sign with digits or a word
  2392. x^2-y^3 will be read as x^2 - y^3, because items are
  2393. terminated by almost any nonword/nondigit char.
  2394. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2395. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2396. sub/superscript. If you set this variable to the symbol `{}',
  2397. the braces are *required* in order to trigger interpretations as
  2398. sub/superscript. This can be helpful in documents that need \"_\"
  2399. frequently in plain text.
  2400. Not all export backends support this, but HTML does.
  2401. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2402. :group 'org-export-translation
  2403. :type '(choice
  2404. (const :tag "Always interpret" t)
  2405. (const :tag "Only with braces" {})
  2406. (const :tag "Never interpret" nil)))
  2407. (defcustom org-export-with-TeX-macros t
  2408. "Non-nil means, interpret simple TeX-like macros when exporting.
  2409. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2410. No only real TeX macros will work here, but the standard HTML entities
  2411. for math can be used as macro names as well. For a list of supported
  2412. names in HTML export, see the constant `org-html-entities'.
  2413. Not all export backends support this.
  2414. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2415. :group 'org-export-translation
  2416. :group 'org-export-latex
  2417. :type 'boolean)
  2418. (defcustom org-export-with-LaTeX-fragments nil
  2419. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2420. When set, the exporter will find LaTeX environments if the \\begin line is
  2421. the first non-white thing on a line. It will also find the math delimiters
  2422. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2423. display math.
  2424. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2425. :group 'org-export-translation
  2426. :group 'org-export-latex
  2427. :type 'boolean)
  2428. (defcustom org-export-with-fixed-width t
  2429. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2430. This can be used to have pre-formatted text, fragments of code etc. For
  2431. example:
  2432. : ;; Some Lisp examples
  2433. : (while (defc cnt)
  2434. : (ding))
  2435. will be looking just like this in also HTML. See also the QUOTE keyword.
  2436. Not all export backends support this.
  2437. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2438. :group 'org-export-translation
  2439. :type 'boolean)
  2440. (defcustom org-match-sexp-depth 3
  2441. "Number of stacked braces for sub/superscript matching.
  2442. This has to be set before loading org.el to be effective."
  2443. :group 'org-export-translation
  2444. :type 'integer)
  2445. (defgroup org-export-tables nil
  2446. "Options for exporting tables in Org-mode."
  2447. :tag "Org Export Tables"
  2448. :group 'org-export)
  2449. (defcustom org-export-with-tables t
  2450. "If non-nil, lines starting with \"|\" define a table.
  2451. For example:
  2452. | Name | Address | Birthday |
  2453. |-------------+----------+-----------|
  2454. | Arthur Dent | England | 29.2.2100 |
  2455. Not all export backends support this.
  2456. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2457. :group 'org-export-tables
  2458. :type 'boolean)
  2459. (defcustom org-export-highlight-first-table-line t
  2460. "Non-nil means, highlight the first table line.
  2461. In HTML export, this means use <th> instead of <td>.
  2462. In tables created with table.el, this applies to the first table line.
  2463. In Org-mode tables, all lines before the first horizontal separator
  2464. line will be formatted with <th> tags."
  2465. :group 'org-export-tables
  2466. :type 'boolean)
  2467. (defcustom org-export-table-remove-special-lines t
  2468. "Remove special lines and marking characters in calculating tables.
  2469. This removes the special marking character column from tables that are set
  2470. up for spreadsheet calculations. It also removes the entire lines
  2471. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2472. the values of constants may be useful to have."
  2473. :group 'org-export-tables
  2474. :type 'boolean)
  2475. (defcustom org-export-prefer-native-exporter-for-tables nil
  2476. "Non-nil means, always export tables created with table.el natively.
  2477. Natively means, use the HTML code generator in table.el.
  2478. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2479. the table does not use row- or column-spanning). This has the
  2480. advantage, that the automatic HTML conversions for math symbols and
  2481. sub/superscripts can be applied. Org-mode's HTML generator is also
  2482. much faster."
  2483. :group 'org-export-tables
  2484. :type 'boolean)
  2485. (defgroup org-export-ascii nil
  2486. "Options specific for ASCII export of Org-mode files."
  2487. :tag "Org Export ASCII"
  2488. :group 'org-export)
  2489. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2490. "Characters for underlining headings in ASCII export.
  2491. In the given sequence, these characters will be used for level 1, 2, ..."
  2492. :group 'org-export-ascii
  2493. :type '(repeat character))
  2494. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2495. "Bullet characters for headlines converted to lists in ASCII export.
  2496. The first character is is used for the first lest level generated in this
  2497. way, and so on. If there are more levels than characters given here,
  2498. the list will be repeated.
  2499. Note that plain lists will keep the same bullets as the have in the
  2500. Org-mode file."
  2501. :group 'org-export-ascii
  2502. :type '(repeat character))
  2503. (defgroup org-export-xml nil
  2504. "Options specific for XML export of Org-mode files."
  2505. :tag "Org Export XML"
  2506. :group 'org-export)
  2507. (defgroup org-export-html nil
  2508. "Options specific for HTML export of Org-mode files."
  2509. :tag "Org Export HTML"
  2510. :group 'org-export)
  2511. (defcustom org-export-html-coding-system nil
  2512. ""
  2513. :group 'org-export-html
  2514. :type 'coding-system)
  2515. (defcustom org-export-html-style
  2516. "<style type=\"text/css\">
  2517. html {
  2518. font-family: Times, serif;
  2519. font-size: 12pt;
  2520. }
  2521. .title { text-align: center; }
  2522. .todo { color: red; }
  2523. .done { color: green; }
  2524. .timestamp { color: grey }
  2525. .timestamp-kwd { color: CadetBlue }
  2526. .tag { background-color:lightblue; font-weight:normal }
  2527. .target { background-color: lavender; }
  2528. pre {
  2529. border: 1pt solid #AEBDCC;
  2530. background-color: #F3F5F7;
  2531. padding: 5pt;
  2532. font-family: courier, monospace;
  2533. }
  2534. table { border-collapse: collapse; }
  2535. td, th {
  2536. vertical-align: top;
  2537. <!--border: 1pt solid #ADB9CC;-->
  2538. }
  2539. </style>"
  2540. "The default style specification for exported HTML files.
  2541. Since there are different ways of setting style information, this variable
  2542. needs to contain the full HTML structure to provide a style, including the
  2543. surrounding HTML tags. The style specifications should include definitions
  2544. for new classes todo, done, title, and deadline. For example, legal values
  2545. would be:
  2546. <style type=\"text/css\">
  2547. p { font-weight: normal; color: gray; }
  2548. h1 { color: black; }
  2549. .title { text-align: center; }
  2550. .todo, .deadline { color: red; }
  2551. .done { color: green; }
  2552. </style>
  2553. or, if you want to keep the style in a file,
  2554. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2555. As the value of this option simply gets inserted into the HTML <head> header,
  2556. you can \"misuse\" it to add arbitrary text to the header."
  2557. :group 'org-export-html
  2558. :type 'string)
  2559. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2560. "Format for typesetting the document title in HTML export."
  2561. :group 'org-export-html
  2562. :type 'string)
  2563. (defcustom org-export-html-toplevel-hlevel 2
  2564. "The <H> level for level 1 headings in HTML export."
  2565. :group 'org-export-html
  2566. :type 'string)
  2567. (defcustom org-export-html-link-org-files-as-html t
  2568. "Non-nil means, make file links to `file.org' point to `file.html'.
  2569. When org-mode is exporting an org-mode file to HTML, links to
  2570. non-html files are directly put into a href tag in HTML.
  2571. However, links to other Org-mode files (recognized by the
  2572. extension `.org.) should become links to the corresponding html
  2573. file, assuming that the linked org-mode file will also be
  2574. converted to HTML.
  2575. When nil, the links still point to the plain `.org' file."
  2576. :group 'org-export-html
  2577. :type 'boolean)
  2578. (defcustom org-export-html-inline-images 'maybe
  2579. "Non-nil means, inline images into exported HTML pages.
  2580. This is done using an <img> tag. When nil, an anchor with href is used to
  2581. link to the image. If this option is `maybe', then images in links with
  2582. an empty description will be inlined, while images with a description will
  2583. be linked only."
  2584. :group 'org-export-html
  2585. :type '(choice (const :tag "Never" nil)
  2586. (const :tag "Always" t)
  2587. (const :tag "When there is no description" maybe)))
  2588. ;; FIXME: rename
  2589. (defcustom org-export-html-expand t
  2590. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2591. When nil, these tags will be exported as plain text and therefore
  2592. not be interpreted by a browser.
  2593. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2594. :group 'org-export-html
  2595. :type 'boolean)
  2596. (defcustom org-export-html-table-tag
  2597. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2598. "The HTML tag that is used to start a table.
  2599. This must be a <table> tag, but you may change the options like
  2600. borders and spacing."
  2601. :group 'org-export-html
  2602. :type 'string)
  2603. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2604. "The opening tag for table header fields.
  2605. This is customizable so that alignment options can be specified."
  2606. :group 'org-export-tables
  2607. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2608. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2609. "The opening tag for table data fields.
  2610. This is customizable so that alignment options can be specified."
  2611. :group 'org-export-tables
  2612. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2613. (defcustom org-export-html-with-timestamp nil
  2614. "If non-nil, write `org-export-html-html-helper-timestamp'
  2615. into the exported HTML text. Otherwise, the buffer will just be saved
  2616. to a file."
  2617. :group 'org-export-html
  2618. :type 'boolean)
  2619. (defcustom org-export-html-html-helper-timestamp
  2620. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2621. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2622. :group 'org-export-html
  2623. :type 'string)
  2624. (defgroup org-export-icalendar nil
  2625. "Options specific for iCalendar export of Org-mode files."
  2626. :tag "Org Export iCalendar"
  2627. :group 'org-export)
  2628. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2629. "The file name for the iCalendar file covering all agenda files.
  2630. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2631. The file name should be absolute."
  2632. :group 'org-export-icalendar
  2633. :type 'file)
  2634. (defcustom org-icalendar-include-todo nil
  2635. "Non-nil means, export to iCalendar files should also cover TODO items."
  2636. :group 'org-export-icalendar
  2637. :type '(choice
  2638. (const :tag "None" nil)
  2639. (const :tag "Unfinished" t)
  2640. (const :tag "All" all)))
  2641. (defcustom org-icalendar-include-sexps t
  2642. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2643. These are entries like in the diary, but directly in an Org-mode file."
  2644. :group 'org-export-icalendar
  2645. :type 'boolean)
  2646. (defcustom org-icalendar-combined-name "OrgMode"
  2647. "Calendar name for the combined iCalendar representing all agenda files."
  2648. :group 'org-export-icalendar
  2649. :type 'string)
  2650. (defgroup org-font-lock nil
  2651. "Font-lock settings for highlighting in Org-mode."
  2652. :tag "Org Font Lock"
  2653. :group 'org)
  2654. (defcustom org-level-color-stars-only nil
  2655. "Non-nil means fontify only the stars in each headline.
  2656. When nil, the entire headline is fontified.
  2657. Changing it requires restart of `font-lock-mode' to become effective
  2658. also in regions already fontified."
  2659. :group 'org-font-lock
  2660. :type 'boolean)
  2661. (defcustom org-hide-leading-stars nil
  2662. "Non-nil means, hide the first N-1 stars in a headline.
  2663. This works by using the face `org-hide' for these stars. This
  2664. face is white for a light background, and black for a dark
  2665. background. You may have to customize the face `org-hide' to
  2666. make this work.
  2667. Changing it requires restart of `font-lock-mode' to become effective
  2668. also in regions already fontified.
  2669. You may also set this on a per-file basis by adding one of the following
  2670. lines to the buffer:
  2671. #+STARTUP: hidestars
  2672. #+STARTUP: showstars"
  2673. :group 'org-font-lock
  2674. :type 'boolean)
  2675. (defcustom org-fontify-done-headline nil
  2676. "Non-nil means, change the face of a headline if it is marked DONE.
  2677. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2678. When this is non-nil, the headline after the keyword is set to the
  2679. `org-headline-done' as an additional indication."
  2680. :group 'org-font-lock
  2681. :type 'boolean)
  2682. (defcustom org-fontify-emphasized-text t
  2683. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2684. Changing this variable requires a restart of Emacs to take effect."
  2685. :group 'org-font-lock
  2686. :type 'boolean)
  2687. (defvar org-emph-re nil
  2688. "Regular expression for matching emphasis.")
  2689. (defvar org-emphasis-regexp-components) ; defined just below
  2690. (defvar org-emphasis-alist) ; defined just below
  2691. (defun org-set-emph-re (var val)
  2692. "Set variable and compute the emphasis regular expression."
  2693. (set var val)
  2694. (when (and (boundp 'org-emphasis-alist)
  2695. (boundp 'org-emphasis-regexp-components)
  2696. org-emphasis-alist org-emphasis-regexp-components)
  2697. (let* ((e org-emphasis-regexp-components)
  2698. (pre (car e))
  2699. (post (nth 1 e))
  2700. (border (nth 2 e))
  2701. (body (nth 3 e))
  2702. (nl (nth 4 e))
  2703. (stacked (nth 5 e))
  2704. (body1 (concat body "*?"))
  2705. (markers (mapconcat 'car org-emphasis-alist "")))
  2706. ;; make sure special characters appear at the right position in the class
  2707. (if (string-match "\\^" markers)
  2708. (setq markers (concat (replace-match "" t t markers) "^")))
  2709. (if (string-match "-" markers)
  2710. (setq markers (concat (replace-match "" t t markers) "-")))
  2711. (if (> nl 0)
  2712. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2713. (int-to-string nl) "\\}")))
  2714. ;; Make the regexp
  2715. (setq org-emph-re
  2716. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2717. "\\("
  2718. "\\([" markers "]\\)"
  2719. "\\("
  2720. "[^" border (if (and nil stacked) markers) "]"
  2721. body1
  2722. "[^" border (if (and nil stacked) markers) "]"
  2723. "\\)"
  2724. "\\3\\)"
  2725. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2726. (defcustom org-emphasis-regexp-components
  2727. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2728. "Components used to build the reqular expression for emphasis.
  2729. This is a list with 6 entries. Terminology: In an emphasis string
  2730. like \" *strong word* \", we call the initial space PREMATCH, the final
  2731. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2732. and \"trong wor\" is the body. The different components in this variable
  2733. specify what is allowed/forbidden in each part:
  2734. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2735. post Chars allowed as postmatch. End of line will be allowed too.
  2736. border The chars *forbidden* as border characters.
  2737. body-regexp A regexp like \".\" to match a body character. Don't use
  2738. non-shy groups here, and don't allow newline here.
  2739. newline The maximum number of newlines allowed in an emphasis exp.
  2740. stacked Non-nil means, allow stacked styles. This works only in HTML
  2741. export. When this is set, all marker characters (as given in
  2742. `org-emphasis-alist') will be allowed as pre/post, aiding
  2743. inside-out matching.
  2744. Use customize to modify this, or restart Emacs after changing it."
  2745. :group 'org-font-lock
  2746. :set 'org-set-emph-re
  2747. :type '(list
  2748. (sexp :tag "Allowed chars in pre ")
  2749. (sexp :tag "Allowed chars in post ")
  2750. (sexp :tag "Forbidden chars in border ")
  2751. (sexp :tag "Regexp for body ")
  2752. (integer :tag "number of newlines allowed")
  2753. (boolean :tag "Stacking allowed ")))
  2754. (defcustom org-emphasis-alist
  2755. '(("*" bold "<b>" "</b>")
  2756. ("/" italic "<i>" "</i>")
  2757. ("_" underline "<u>" "</u>")
  2758. ("=" org-code "<code>" "</code>")
  2759. ("+" (:strike-through t) "<del>" "</del>")
  2760. )
  2761. "Special syntax for emphasized text.
  2762. Text starting and ending with a special character will be emphasized, for
  2763. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2764. characters, the face to be used by font-lock for highlighting in Org-mode
  2765. Emacs buffers, and the HTML tags to be used for this.
  2766. Use customize to modify this, or restart Emacs after changing it."
  2767. :group 'org-font-lock
  2768. :set 'org-set-emph-re
  2769. :type '(repeat
  2770. (list
  2771. (string :tag "Marker character")
  2772. (choice
  2773. (face :tag "Font-lock-face")
  2774. (plist :tag "Face property list"))
  2775. (string :tag "HTML start tag")
  2776. (string :tag "HTML end tag"))))
  2777. ;;; The faces
  2778. (defgroup org-faces nil
  2779. "Faces in Org-mode."
  2780. :tag "Org Faces"
  2781. :group 'org-font-lock)
  2782. ;; FIXME: convert that into a macro? Not critical, because this
  2783. ;; is only executed a few times at load time.
  2784. (defun org-compatible-face (specs)
  2785. "Make a compatible face specification.
  2786. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2787. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2788. to the top of the list. The `min-colors' attribute will be removed from
  2789. any other entries, and any resulting duplicates will be removed entirely."
  2790. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2791. (let (r e a)
  2792. (while (setq e (pop specs))
  2793. (cond
  2794. ((memq (car e) '(t default)) (push e r))
  2795. ((setq a (member '(min-colors 8) (car e)))
  2796. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2797. (cdr e)))))
  2798. ((setq a (assq 'min-colors (car e)))
  2799. (setq e (cons (delq a (car e)) (cdr e)))
  2800. (or (assoc (car e) r) (push e r)))
  2801. (t (or (assoc (car e) r) (push e r)))))
  2802. (nreverse r))
  2803. specs))
  2804. (defface org-hide
  2805. '((((background light)) (:foreground "white"))
  2806. (((background dark)) (:foreground "black")))
  2807. "Face used to hide leading stars in headlines.
  2808. The forground color of this face should be equal to the background
  2809. color of the frame."
  2810. :group 'org-faces)
  2811. (defface org-level-1 ;; font-lock-function-name-face
  2812. (org-compatible-face
  2813. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2814. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2815. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2816. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2817. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2818. (t (:bold t))))
  2819. "Face used for level 1 headlines."
  2820. :group 'org-faces)
  2821. (defface org-level-2 ;; font-lock-variable-name-face
  2822. (org-compatible-face
  2823. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2824. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2825. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2826. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2827. (t (:bold t))))
  2828. "Face used for level 2 headlines."
  2829. :group 'org-faces)
  2830. (defface org-level-3 ;; font-lock-keyword-face
  2831. (org-compatible-face
  2832. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2833. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2834. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2835. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2836. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2837. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2838. (t (:bold t))))
  2839. "Face used for level 3 headlines."
  2840. :group 'org-faces)
  2841. (defface org-level-4 ;; font-lock-comment-face
  2842. (org-compatible-face
  2843. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2844. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2845. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2846. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2847. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2848. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2849. (t (:bold t))))
  2850. "Face used for level 4 headlines."
  2851. :group 'org-faces)
  2852. (defface org-level-5 ;; font-lock-type-face
  2853. (org-compatible-face
  2854. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2855. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2856. (((class color) (min-colors 8)) (:foreground "green"))))
  2857. "Face used for level 5 headlines."
  2858. :group 'org-faces)
  2859. (defface org-level-6 ;; font-lock-constant-face
  2860. (org-compatible-face
  2861. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2862. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2863. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2864. "Face used for level 6 headlines."
  2865. :group 'org-faces)
  2866. (defface org-level-7 ;; font-lock-builtin-face
  2867. (org-compatible-face
  2868. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2869. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2870. (((class color) (min-colors 8)) (:foreground "blue"))))
  2871. "Face used for level 7 headlines."
  2872. :group 'org-faces)
  2873. (defface org-level-8 ;; font-lock-string-face
  2874. (org-compatible-face
  2875. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2876. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2877. (((class color) (min-colors 8)) (:foreground "green"))))
  2878. "Face used for level 8 headlines."
  2879. :group 'org-faces)
  2880. (defface org-special-keyword ;; font-lock-string-face
  2881. (org-compatible-face
  2882. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2883. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2884. (t (:italic t))))
  2885. "Face used for special keywords."
  2886. :group 'org-faces)
  2887. (defface org-drawer ;; font-lock-function-name-face
  2888. (org-compatible-face
  2889. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2890. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2891. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2892. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2893. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2894. (t (:bold t))))
  2895. "Face used for drawers."
  2896. :group 'org-faces)
  2897. (defface org-property-value nil
  2898. "Face used for the value of a property."
  2899. :group 'org-faces)
  2900. (defface org-column
  2901. (org-compatible-face
  2902. '((((class color) (min-colors 16) (background light))
  2903. (:background "grey90"))
  2904. (((class color) (min-colors 16) (background dark))
  2905. (:background "grey30"))
  2906. (((class color) (min-colors 8))
  2907. (:background "cyan" :foreground "black"))
  2908. (t (:inverse-video t))))
  2909. "Face for column display of entry properties."
  2910. :group 'org-faces)
  2911. (when (fboundp 'set-face-attribute)
  2912. ;; Make sure that a fixed-width face is used when we have a column table.
  2913. (set-face-attribute 'org-column nil
  2914. :height (face-attribute 'default :height)
  2915. :family (face-attribute 'default :family)))
  2916. (defface org-warning ;; font-lock-warning-face
  2917. (org-compatible-face
  2918. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2919. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2920. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2921. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2922. (t (:bold t))))
  2923. "Face for deadlines and TODO keywords."
  2924. :group 'org-faces)
  2925. (defface org-archived ; similar to shadow
  2926. (org-compatible-face
  2927. '((((class color grayscale) (min-colors 88) (background light))
  2928. (:foreground "grey50"))
  2929. (((class color grayscale) (min-colors 88) (background dark))
  2930. (:foreground "grey70"))
  2931. (((class color) (min-colors 8) (background light))
  2932. (:foreground "green"))
  2933. (((class color) (min-colors 8) (background dark))
  2934. (:foreground "yellow"))))
  2935. "Face for headline with the ARCHIVE tag."
  2936. :group 'org-faces)
  2937. (defface org-link
  2938. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2939. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2940. (t (:underline t)))
  2941. "Face for links."
  2942. :group 'org-faces)
  2943. (defface org-target
  2944. '((((class color) (background light)) (:underline t))
  2945. (((class color) (background dark)) (:underline t))
  2946. (t (:underline t)))
  2947. "Face for links."
  2948. :group 'org-faces)
  2949. (defface org-date
  2950. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2951. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2952. (t (:underline t)))
  2953. "Face for links."
  2954. :group 'org-faces)
  2955. (defface org-sexp-date
  2956. '((((class color) (background light)) (:foreground "Purple"))
  2957. (((class color) (background dark)) (:foreground "Cyan"))
  2958. (t (:underline t)))
  2959. "Face for links."
  2960. :group 'org-faces)
  2961. (defface org-tag
  2962. '((t (:bold t)))
  2963. "Face for tags."
  2964. :group 'org-faces)
  2965. (defface org-todo ;; font-lock-warning-face
  2966. (org-compatible-face
  2967. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2968. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2969. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2970. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2971. (t (:inverse-video t :bold t))))
  2972. "Face for TODO keywords."
  2973. :group 'org-faces)
  2974. (defface org-done ;; font-lock-type-face
  2975. (org-compatible-face
  2976. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2977. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2978. (((class color) (min-colors 8)) (:foreground "green"))
  2979. (t (:bold t))))
  2980. "Face used for todo keywords that indicate DONE items."
  2981. :group 'org-faces)
  2982. (defface org-headline-done ;; font-lock-string-face
  2983. (org-compatible-face
  2984. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2985. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2986. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2987. "Face used to indicate that a headline is DONE.
  2988. This face is only used if `org-fontify-done-headline' is set. If applies
  2989. to the part of the headline after the DONE keyword."
  2990. :group 'org-faces)
  2991. (defface org-table ;; font-lock-function-name-face
  2992. (org-compatible-face
  2993. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2994. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2995. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2996. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2997. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2998. (((class color) (min-colors 8) (background dark)))))
  2999. "Face used for tables."
  3000. :group 'org-faces)
  3001. (defface org-formula
  3002. (org-compatible-face
  3003. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3004. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3005. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3006. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3007. (t (:bold t :italic t))))
  3008. "Face for formulas."
  3009. :group 'org-faces)
  3010. (defface org-code
  3011. (org-compatible-face
  3012. '((((class color grayscale) (min-colors 88) (background light))
  3013. :foreground "grey50")
  3014. (((class color grayscale) (min-colors 88) (background dark))
  3015. :foreground "grey70")
  3016. (((class color) (min-colors 8) (background light))
  3017. :foreground "green")
  3018. (((class color) (min-colors 8) (background dark))
  3019. :foreground "yellow")))
  3020. "Face for fixed-with text like code snippets."
  3021. :group 'org-faces
  3022. :version "22.1")
  3023. (defface org-agenda-structure ;; font-lock-function-name-face
  3024. (org-compatible-face
  3025. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3026. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3027. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3028. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3029. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3030. (t (:bold t))))
  3031. "Face used in agenda for captions and dates."
  3032. :group 'org-faces)
  3033. (defface org-scheduled-today
  3034. (org-compatible-face
  3035. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3036. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3037. (((class color) (min-colors 8)) (:foreground "green"))
  3038. (t (:bold t :italic t))))
  3039. "Face for items scheduled for a certain day."
  3040. :group 'org-faces)
  3041. (defface org-scheduled-previously
  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" :bold t))
  3047. (t (:bold t))))
  3048. "Face for items scheduled previously, and not yet done."
  3049. :group 'org-faces)
  3050. (defface org-upcoming-deadline
  3051. (org-compatible-face
  3052. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3053. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3054. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3055. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3056. (t (:bold t))))
  3057. "Face for items scheduled previously, and not yet done."
  3058. :group 'org-faces)
  3059. (defcustom org-agenda-deadline-faces
  3060. '((1.0 . org-warning)
  3061. (0.5 . org-upcoming-deadline)
  3062. (0.0 . default))
  3063. "Faces for showing deadlines in the agenda.
  3064. This is a list of cons cells. The cdr of each cess is a face to be used,
  3065. and it can also just be a like like '(:foreground \"yellow\").
  3066. Each car is a fraction of the head-warning time that must have passed for
  3067. this the face in the cdr to be used for display. The numbers must be
  3068. given in descending order. The head-warning time is normally taken
  3069. from `org-deadline-warning-days', but can also be specified in the deadline
  3070. timestamp itself, like this:
  3071. DEADLINE: <2007-08-13 Mon -8d>
  3072. You may use d for days, w for weeks, m for months and y for years. Months
  3073. and years will only be treated in an approximate fashion (30.4 days for a
  3074. month and 365.24 days for a year)."
  3075. :group 'org-faces
  3076. :group 'org-agenda-daily/weekly
  3077. :type '(repeat
  3078. (cons
  3079. (number :tag "Fraction of head-warning time passed")
  3080. (sexp :tag "Face"))))
  3081. (defface org-time-grid ;; font-lock-variable-name-face
  3082. (org-compatible-face
  3083. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3084. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3085. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3086. "Face used for time grids."
  3087. :group 'org-faces)
  3088. (defconst org-level-faces
  3089. '(org-level-1 org-level-2 org-level-3 org-level-4
  3090. org-level-5 org-level-6 org-level-7 org-level-8
  3091. ))
  3092. (defcustom org-n-level-faces (length org-level-faces)
  3093. "The number different faces to be used for headlines.
  3094. Org-mode defines 8 different headline faces, so this can be at most 8.
  3095. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3096. :type 'number
  3097. :group 'org-faces)
  3098. ;;; Variables for pre-computed regular expressions, all buffer local
  3099. (defvar org-drawer-regexp nil
  3100. "Matches first line of a hidden block.")
  3101. (make-variable-buffer-local 'org-drawer-regexp)
  3102. (defvar org-todo-regexp nil
  3103. "Matches any of the TODO state keywords.")
  3104. (make-variable-buffer-local 'org-todo-regexp)
  3105. (defvar org-not-done-regexp nil
  3106. "Matches any of the TODO state keywords except the last one.")
  3107. (make-variable-buffer-local 'org-not-done-regexp)
  3108. (defvar org-todo-line-regexp nil
  3109. "Matches a headline and puts TODO state into group 2 if present.")
  3110. (make-variable-buffer-local 'org-todo-line-regexp)
  3111. (defvar org-todo-line-tags-regexp nil
  3112. "Matches a headline and puts TODO state into group 2 if present.
  3113. Also put tags into group 4 if tags are present.")
  3114. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3115. (defvar org-nl-done-regexp nil
  3116. "Matches newline followed by a headline with the DONE keyword.")
  3117. (make-variable-buffer-local 'org-nl-done-regexp)
  3118. (defvar org-looking-at-done-regexp nil
  3119. "Matches the DONE keyword a point.")
  3120. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3121. (defvar org-ds-keyword-length 12
  3122. "Maximum length of the Deadline and SCHEDULED keywords.")
  3123. (make-variable-buffer-local 'org-ds-keyword-length)
  3124. (defvar org-deadline-regexp nil
  3125. "Matches the DEADLINE keyword.")
  3126. (make-variable-buffer-local 'org-deadline-regexp)
  3127. (defvar org-deadline-time-regexp nil
  3128. "Matches the DEADLINE keyword together with a time stamp.")
  3129. (make-variable-buffer-local 'org-deadline-time-regexp)
  3130. (defvar org-deadline-line-regexp nil
  3131. "Matches the DEADLINE keyword and the rest of the line.")
  3132. (make-variable-buffer-local 'org-deadline-line-regexp)
  3133. (defvar org-scheduled-regexp nil
  3134. "Matches the SCHEDULED keyword.")
  3135. (make-variable-buffer-local 'org-scheduled-regexp)
  3136. (defvar org-scheduled-time-regexp nil
  3137. "Matches the SCHEDULED keyword together with a time stamp.")
  3138. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3139. (defvar org-closed-time-regexp nil
  3140. "Matches the CLOSED keyword together with a time stamp.")
  3141. (make-variable-buffer-local 'org-closed-time-regexp)
  3142. (defvar org-keyword-time-regexp nil
  3143. "Matches any of the 4 keywords, together with the time stamp.")
  3144. (make-variable-buffer-local 'org-keyword-time-regexp)
  3145. (defvar org-keyword-time-not-clock-regexp nil
  3146. "Matches any of the 3 keywords, together with the time stamp.")
  3147. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3148. (defvar org-maybe-keyword-time-regexp nil
  3149. "Matches a timestamp, possibly preceeded by a keyword.")
  3150. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3151. (defvar org-planning-or-clock-line-re nil
  3152. "Matches a line with planning or clock info.")
  3153. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3154. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3155. rear-nonsticky t mouse-map t fontified t)
  3156. "Properties to remove when a string without properties is wanted.")
  3157. (defsubst org-match-string-no-properties (num &optional string)
  3158. (if (featurep 'xemacs)
  3159. (let ((s (match-string num string)))
  3160. (remove-text-properties 0 (length s) org-rm-props s)
  3161. s)
  3162. (match-string-no-properties num string)))
  3163. (defsubst org-no-properties (s)
  3164. (if (fboundp 'set-text-properties)
  3165. (set-text-properties 0 (length s) nil s)
  3166. (remove-text-properties 0 (length s) org-rm-props s))
  3167. s)
  3168. (defsubst org-get-alist-option (option key)
  3169. (cond ((eq key t) t)
  3170. ((eq option t) t)
  3171. ((assoc key option) (cdr (assoc key option)))
  3172. (t (cdr (assq 'default option)))))
  3173. (defsubst org-inhibit-invisibility ()
  3174. "Modified `buffer-invisibility-spec' for Emacs 21.
  3175. Some ops with invisible text do not work correctly on Emacs 21. For these
  3176. we turn off invisibility temporarily. Use this in a `let' form."
  3177. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3178. (defsubst org-set-local (var value)
  3179. "Make VAR local in current buffer and set it to VALUE."
  3180. (set (make-variable-buffer-local var) value))
  3181. (defsubst org-mode-p ()
  3182. "Check if the current buffer is in Org-mode."
  3183. (eq major-mode 'org-mode))
  3184. (defsubst org-last (list)
  3185. "Return the last element of LIST."
  3186. (car (last list)))
  3187. (defun org-let (list &rest body)
  3188. (eval (cons 'let (cons list body))))
  3189. (put 'org-let 'lisp-indent-function 1)
  3190. (defun org-let2 (list1 list2 &rest body)
  3191. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3192. (put 'org-let2 'lisp-indent-function 2)
  3193. (defconst org-startup-options
  3194. '(("fold" org-startup-folded t)
  3195. ("overview" org-startup-folded t)
  3196. ("nofold" org-startup-folded nil)
  3197. ("showall" org-startup-folded nil)
  3198. ("content" org-startup-folded content)
  3199. ("hidestars" org-hide-leading-stars t)
  3200. ("showstars" org-hide-leading-stars nil)
  3201. ("odd" org-odd-levels-only t)
  3202. ("oddeven" org-odd-levels-only nil)
  3203. ("align" org-startup-align-all-tables t)
  3204. ("noalign" org-startup-align-all-tables nil)
  3205. ("customtime" org-display-custom-times t)
  3206. ("logging" org-log-done t)
  3207. ("logdone" org-log-done t)
  3208. ("nologging" org-log-done nil)
  3209. ("lognotedone" org-log-done done push)
  3210. ("lognotestate" org-log-done state push)
  3211. ("lognoteclock-out" org-log-done clock-out push)
  3212. ("logrepeat" org-log-repeat t)
  3213. ("nologrepeat" org-log-repeat nil)
  3214. ("constcgs" constants-unit-system cgs)
  3215. ("constSI" constants-unit-system SI))
  3216. "Variable associated with STARTUP options for org-mode.
  3217. Each element is a list of three items: The startup options as written
  3218. in the #+STARTUP line, the corresponding variable, and the value to
  3219. set this variable to if the option is found. An optional forth element PUSH
  3220. means to push this value onto the list in the variable.")
  3221. (defun org-set-regexps-and-options ()
  3222. "Precompute regular expressions for current buffer."
  3223. (when (org-mode-p)
  3224. (org-set-local 'org-todo-kwd-alist nil)
  3225. (org-set-local 'org-todo-keywords-1 nil)
  3226. (org-set-local 'org-done-keywords nil)
  3227. (org-set-local 'org-todo-heads nil)
  3228. (org-set-local 'org-todo-sets nil)
  3229. (let ((re (org-make-options-regexp
  3230. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO" "COLUMNS"
  3231. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3232. "CONSTANTS" "PROPERTY")))
  3233. (splitre "[ \t]+")
  3234. kwds key value cat arch tags const links hw dws tail sep kws1 prio
  3235. props)
  3236. (save-excursion
  3237. (save-restriction
  3238. (widen)
  3239. (goto-char (point-min))
  3240. (while (re-search-forward re nil t)
  3241. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3242. (cond
  3243. ((equal key "CATEGORY")
  3244. (if (string-match "[ \t]+$" value)
  3245. (setq value (replace-match "" t t value)))
  3246. (setq cat (intern value)))
  3247. ((equal key "SEQ_TODO")
  3248. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3249. ((equal key "TYP_TODO")
  3250. (push (cons 'type (org-split-string value splitre)) kwds))
  3251. ((equal key "TAGS")
  3252. (setq tags (append tags (org-split-string value splitre))))
  3253. ((equal key "COLUMNS")
  3254. (org-set-local 'org-columns-default-format value))
  3255. ((equal key "LINK")
  3256. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3257. (push (cons (match-string 1 value)
  3258. (org-trim (match-string 2 value)))
  3259. links)))
  3260. ((equal key "PRIORITIES")
  3261. (setq prio (org-split-string value " +")))
  3262. ((equal key "PROPERTY")
  3263. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3264. (push (cons (match-string 1 value) (match-string 2 value))
  3265. props)))
  3266. ((equal key "CONSTANTS")
  3267. (setq const (append const (org-split-string value splitre))))
  3268. ((equal key "STARTUP")
  3269. (let ((opts (org-split-string value splitre))
  3270. l var val)
  3271. (while (setq l (pop opts))
  3272. (when (setq l (assoc l org-startup-options))
  3273. (setq var (nth 1 l) val (nth 2 l))
  3274. (if (not (nth 3 l))
  3275. (set (make-local-variable var) val)
  3276. (if (not (listp (symbol-value var)))
  3277. (set (make-local-variable var) nil))
  3278. (set (make-local-variable var) (symbol-value var))
  3279. (add-to-list var val))))))
  3280. ((equal key "ARCHIVE")
  3281. (string-match " *$" value)
  3282. (setq arch (replace-match "" t t value))
  3283. (remove-text-properties 0 (length arch)
  3284. '(face t fontified t) arch)))
  3285. )))
  3286. (and cat (org-set-local 'org-category cat))
  3287. (when prio
  3288. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3289. (setq prio (mapcar 'string-to-char prio))
  3290. (org-set-local 'org-highest-priority (nth 0 prio))
  3291. (org-set-local 'org-lowest-priority (nth 1 prio))
  3292. (org-set-local 'org-default-priority (nth 2 prio)))
  3293. (and props (org-set-local 'org-local-properties (nreverse props)))
  3294. (and arch (org-set-local 'org-archive-location arch))
  3295. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3296. ;; Process the TODO keywords
  3297. (unless kwds
  3298. ;; Use the global values as if they had been given locally.
  3299. (setq kwds (default-value 'org-todo-keywords))
  3300. (if (stringp (car kwds))
  3301. (setq kwds (list (cons org-todo-interpretation
  3302. (default-value 'org-todo-keywords)))))
  3303. (setq kwds (reverse kwds)))
  3304. (setq kwds (nreverse kwds))
  3305. (let (inter kws)
  3306. (while (setq kws (pop kwds))
  3307. (setq inter (pop kws) sep (member "|" kws)
  3308. kws1 (delete "|" (copy-sequence kws))
  3309. hw (car kws1)
  3310. dws (if sep (cdr sep) (last kws1))
  3311. tail (list inter hw (car dws) (org-last dws)))
  3312. (add-to-list 'org-todo-heads hw 'append)
  3313. (push kws1 org-todo-sets)
  3314. (setq org-done-keywords (append org-done-keywords dws nil))
  3315. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3316. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3317. (setq org-todo-sets (nreverse org-todo-sets)
  3318. org-todo-kwd-alist (nreverse org-todo-kwd-alist)))
  3319. ;; Process the constants
  3320. (when const
  3321. (let (e cst)
  3322. (while (setq e (pop const))
  3323. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3324. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3325. (setq org-table-formula-constants-local cst)))
  3326. ;; Process the tags.
  3327. (when tags
  3328. (let (e tgs)
  3329. (while (setq e (pop tags))
  3330. (cond
  3331. ((equal e "{") (push '(:startgroup) tgs))
  3332. ((equal e "}") (push '(:endgroup) tgs))
  3333. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3334. (push (cons (match-string 1 e)
  3335. (string-to-char (match-string 2 e)))
  3336. tgs))
  3337. (t (push (list e) tgs))))
  3338. (org-set-local 'org-tag-alist nil)
  3339. (while (setq e (pop tgs))
  3340. (or (and (stringp (car e))
  3341. (assoc (car e) org-tag-alist))
  3342. (push e org-tag-alist))))))
  3343. ;; Compute the regular expressions and other local variables
  3344. (if (not org-done-keywords)
  3345. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3346. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3347. (length org-scheduled-string)))
  3348. org-drawer-regexp
  3349. (concat "^[ \t]*:\\("
  3350. (mapconcat 'regexp-quote org-drawers "\\|")
  3351. "\\):[ \t]*$")
  3352. org-not-done-keywords
  3353. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3354. org-todo-regexp
  3355. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3356. "\\|") "\\)\\>")
  3357. org-not-done-regexp
  3358. (concat "\\<\\("
  3359. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3360. "\\)\\>")
  3361. org-todo-line-regexp
  3362. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3363. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3364. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3365. org-nl-done-regexp
  3366. (concat "\n\\*+[ \t]+"
  3367. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3368. "\\)" "\\>")
  3369. org-todo-line-tags-regexp
  3370. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3371. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3372. (org-re
  3373. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3374. org-looking-at-done-regexp
  3375. (concat "^" "\\(?:"
  3376. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3377. "\\>")
  3378. org-deadline-regexp (concat "\\<" org-deadline-string)
  3379. org-deadline-time-regexp
  3380. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3381. org-deadline-line-regexp
  3382. (concat "\\<\\(" org-deadline-string "\\).*")
  3383. org-scheduled-regexp
  3384. (concat "\\<" org-scheduled-string)
  3385. org-scheduled-time-regexp
  3386. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3387. org-closed-time-regexp
  3388. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3389. org-keyword-time-regexp
  3390. (concat "\\<\\(" org-scheduled-string
  3391. "\\|" org-deadline-string
  3392. "\\|" org-closed-string
  3393. "\\|" org-archived-string
  3394. "\\|" org-clock-string "\\)"
  3395. " *[[<]\\([^]>]+\\)[]>]")
  3396. org-keyword-time-not-clock-regexp
  3397. (concat "\\<\\(" org-scheduled-string
  3398. "\\|" org-deadline-string
  3399. "\\|" org-closed-string
  3400. "\\|" org-archived-string
  3401. "\\)"
  3402. " *[[<]\\([^]>]+\\)[]>]")
  3403. org-maybe-keyword-time-regexp
  3404. (concat "\\(\\<\\(" org-scheduled-string
  3405. "\\|" org-deadline-string
  3406. "\\|" org-closed-string
  3407. "\\|" org-archived-string
  3408. "\\|" org-clock-string "\\)\\)?"
  3409. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3410. org-planning-or-clock-line-re
  3411. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3412. "\\|" org-deadline-string
  3413. "\\|" org-closed-string "\\|" org-clock-string
  3414. "\\|" org-archived-string "\\)\\>\\)")
  3415. )
  3416. (org-set-font-lock-defaults)))
  3417. ;;; Some variables ujsed in various places
  3418. (defvar org-window-configuration nil
  3419. "Used in various places to store a window configuration.")
  3420. (defvar org-finish-function nil
  3421. "Function to be called when `C-c C-c' is used.
  3422. This is for getting out of special buffers like remember.")
  3423. ;;; Foreign variables, to inform the compiler
  3424. ;; XEmacs only
  3425. (defvar outline-mode-menu-heading)
  3426. (defvar outline-mode-menu-show)
  3427. (defvar outline-mode-menu-hide)
  3428. (defvar zmacs-regions) ; XEmacs regions
  3429. ;; Emacs only
  3430. (defvar mark-active)
  3431. ;; Packages that org-mode interacts with
  3432. (defvar calc-embedded-close-formula)
  3433. (defvar calc-embedded-open-formula)
  3434. (defvar font-lock-unfontify-region-function)
  3435. (defvar org-goto-start-pos)
  3436. (defvar vm-message-pointer)
  3437. (defvar vm-folder-directory)
  3438. (defvar wl-summary-buffer-elmo-folder)
  3439. (defvar wl-summary-buffer-folder-name)
  3440. (defvar gnus-other-frame-object)
  3441. (defvar gnus-group-name)
  3442. (defvar gnus-article-current)
  3443. (defvar w3m-current-url)
  3444. (defvar w3m-current-title)
  3445. (defvar mh-progs)
  3446. (defvar mh-current-folder)
  3447. (defvar mh-show-folder-buffer)
  3448. (defvar mh-index-folder)
  3449. (defvar mh-searcher)
  3450. (defvar calendar-mode-map)
  3451. (defvar Info-current-file)
  3452. (defvar Info-current-node)
  3453. (defvar texmathp-why)
  3454. (defvar remember-save-after-remembering)
  3455. (defvar remember-data-file)
  3456. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3457. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3458. (defvar org-latex-regexps)
  3459. (defvar constants-unit-system)
  3460. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3461. ;; FIXME: Occasionally check by commenting these, to make sure
  3462. ;; no other functions uses these, forgetting to let-bind them.
  3463. (defvar entry)
  3464. (defvar state)
  3465. (defvar last-state)
  3466. (defvar date)
  3467. (defvar description)
  3468. ;; Defined somewhere in this file, but used before definition.
  3469. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3470. (defvar org-agenda-buffer-name)
  3471. (defvar org-agenda-undo-list)
  3472. (defvar org-agenda-pending-undo-list)
  3473. (defvar org-agenda-overriding-header)
  3474. (defvar orgtbl-mode)
  3475. (defvar org-html-entities)
  3476. (defvar org-struct-menu)
  3477. (defvar org-org-menu)
  3478. (defvar org-tbl-menu)
  3479. (defvar org-agenda-keymap)
  3480. (defvar org-category-table)
  3481. ;;;; Emacs/XEmacs compatibility
  3482. ;; Overlay compatibility functions
  3483. (defun org-make-overlay (beg end &optional buffer)
  3484. (if (featurep 'xemacs)
  3485. (make-extent beg end buffer)
  3486. (make-overlay beg end buffer)))
  3487. (defun org-delete-overlay (ovl)
  3488. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3489. (defun org-detach-overlay (ovl)
  3490. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3491. (defun org-move-overlay (ovl beg end &optional buffer)
  3492. (if (featurep 'xemacs)
  3493. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3494. (move-overlay ovl beg end buffer)))
  3495. (defun org-overlay-put (ovl prop value)
  3496. (if (featurep 'xemacs)
  3497. (set-extent-property ovl prop value)
  3498. (overlay-put ovl prop value)))
  3499. (defun org-overlay-display (ovl text &optional face evap)
  3500. "Make overlay OVL display TEXT with face FACE."
  3501. (if (featurep 'xemacs)
  3502. (let ((gl (make-glyph text)))
  3503. (and face (set-glyph-face gl face))
  3504. (set-extent-property ovl 'invisible t)
  3505. (set-extent-property ovl 'end-glyph gl))
  3506. (overlay-put ovl 'display text)
  3507. (if face (overlay-put ovl 'face face))
  3508. (if evap (overlay-put ovl 'evaporate t))))
  3509. (defun org-overlay-before-string (ovl text &optional face evap)
  3510. "Make overlay OVL display TEXT with face FACE."
  3511. (if (featurep 'xemacs)
  3512. (let ((gl (make-glyph text)))
  3513. (and face (set-glyph-face gl face))
  3514. (set-extent-property ovl 'begin-glyph gl))
  3515. (if face (org-add-props text nil 'face face))
  3516. (overlay-put ovl 'before-string text)
  3517. (if evap (overlay-put ovl 'evaporate t))))
  3518. (defun org-overlay-get (ovl prop)
  3519. (if (featurep 'xemacs)
  3520. (extent-property ovl prop)
  3521. (overlay-get ovl prop)))
  3522. (defun org-overlays-at (pos)
  3523. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3524. ;; FIXME: this is currently not used
  3525. (defun org-overlays-in (&optional start end)
  3526. (if (featurep 'xemacs)
  3527. (extent-list nil start end)
  3528. (overlays-in start end)))
  3529. (defun org-overlay-start (o)
  3530. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3531. (defun org-overlay-end (o)
  3532. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3533. ;; FIXME: this is currently not used
  3534. (defun org-find-overlays (prop &optional pos delete)
  3535. "Find all overlays specifying PROP at POS or point.
  3536. If DELETE is non-nil, delete all those overlays."
  3537. (let ((overlays (org-overlays-at (or pos (point))))
  3538. ov found)
  3539. (while (setq ov (pop overlays))
  3540. (if (org-overlay-get ov prop)
  3541. (if delete (org-delete-overlay ov) (push ov found))))
  3542. found))
  3543. ;; Region compatibility
  3544. (defun org-add-hook (hook function &optional append local)
  3545. "Add-hook, compatible with both Emacsen."
  3546. (if (and local (featurep 'xemacs))
  3547. (add-local-hook hook function append)
  3548. (add-hook hook function append local)))
  3549. (defvar org-ignore-region nil
  3550. "To temporarily disable the active region.")
  3551. (defun org-region-active-p ()
  3552. "Is `transient-mark-mode' on and the region active?
  3553. Works on both Emacs and XEmacs."
  3554. (if org-ignore-region
  3555. nil
  3556. (if (featurep 'xemacs)
  3557. (and zmacs-regions (region-active-p))
  3558. (and transient-mark-mode mark-active))))
  3559. ;; Invisibility compatibility
  3560. (defun org-add-to-invisibility-spec (arg)
  3561. "Add elements to `buffer-invisibility-spec'.
  3562. See documentation for `buffer-invisibility-spec' for the kind of elements
  3563. that can be added."
  3564. (cond
  3565. ((fboundp 'add-to-invisibility-spec)
  3566. (add-to-invisibility-spec arg))
  3567. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3568. (setq buffer-invisibility-spec (list arg)))
  3569. (t
  3570. (setq buffer-invisibility-spec
  3571. (cons arg buffer-invisibility-spec)))))
  3572. (defun org-remove-from-invisibility-spec (arg)
  3573. "Remove elements from `buffer-invisibility-spec'."
  3574. (if (fboundp 'remove-from-invisibility-spec)
  3575. (remove-from-invisibility-spec arg)
  3576. (if (consp buffer-invisibility-spec)
  3577. (setq buffer-invisibility-spec
  3578. (delete arg buffer-invisibility-spec)))))
  3579. ;; FIXME: this is currently not used
  3580. (defun org-in-invisibility-spec-p (arg)
  3581. "Is ARG a member of `buffer-invisibility-spec'?"
  3582. (if (consp buffer-invisibility-spec)
  3583. (member arg buffer-invisibility-spec)
  3584. nil))
  3585. ;;;; Define the Org-mode
  3586. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3587. (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."))
  3588. ;; We use a before-change function to check if a table might need
  3589. ;; an update.
  3590. (defvar org-table-may-need-update t
  3591. "Indicates that a table might need an update.
  3592. This variable is set by `org-before-change-function'.
  3593. `org-table-align' sets it back to nil.")
  3594. (defvar org-mode-map)
  3595. (defvar org-mode-hook nil)
  3596. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3597. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3598. (defvar org-table-buffer-is-an nil)
  3599. ;;;###autoload
  3600. (define-derived-mode org-mode outline-mode "Org"
  3601. "Outline-based notes management and organizer, alias
  3602. \"Carsten's outline-mode for keeping track of everything.\"
  3603. Org-mode develops organizational tasks around a NOTES file which
  3604. contains information about projects as plain text. Org-mode is
  3605. implemented on top of outline-mode, which is ideal to keep the content
  3606. of large files well structured. It supports ToDo items, deadlines and
  3607. time stamps, which magically appear in the diary listing of the Emacs
  3608. calendar. Tables are easily created with a built-in table editor.
  3609. Plain text URL-like links connect to websites, emails (VM), Usenet
  3610. messages (Gnus), BBDB entries, and any files related to the project.
  3611. For printing and sharing of notes, an Org-mode file (or a part of it)
  3612. can be exported as a structured ASCII or HTML file.
  3613. The following commands are available:
  3614. \\{org-mode-map}"
  3615. ;; Get rid of Outline menus, they are not needed
  3616. ;; Need to do this here because define-derived-mode sets up
  3617. ;; the keymap so late. Still, it is a waste to call this each time
  3618. ;; we switch another buffer into org-mode.
  3619. (if (featurep 'xemacs)
  3620. (when (boundp 'outline-mode-menu-heading)
  3621. ;; Assume this is Greg's port, it used easymenu
  3622. (easy-menu-remove outline-mode-menu-heading)
  3623. (easy-menu-remove outline-mode-menu-show)
  3624. (easy-menu-remove outline-mode-menu-hide))
  3625. (define-key org-mode-map [menu-bar headings] 'undefined)
  3626. (define-key org-mode-map [menu-bar hide] 'undefined)
  3627. (define-key org-mode-map [menu-bar show] 'undefined))
  3628. (easy-menu-add org-org-menu)
  3629. (easy-menu-add org-tbl-menu)
  3630. (org-install-agenda-files-menu)
  3631. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3632. (org-add-to-invisibility-spec '(org-cwidth))
  3633. (when (featurep 'xemacs)
  3634. (org-set-local 'line-move-ignore-invisible t))
  3635. (org-set-local 'outline-regexp "\\*+ ")
  3636. (setq outline-level 'org-outline-level)
  3637. (when (and org-ellipsis (stringp org-ellipsis)
  3638. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table))
  3639. (unless org-display-table
  3640. (setq org-display-table (make-display-table)))
  3641. (set-display-table-slot org-display-table
  3642. 4 (string-to-vector org-ellipsis))
  3643. (setq buffer-display-table org-display-table))
  3644. (org-set-regexps-and-options)
  3645. ;; Calc embedded
  3646. (org-set-local 'calc-embedded-open-mode "# ")
  3647. (modify-syntax-entry ?# "<")
  3648. (modify-syntax-entry ?@ "w")
  3649. (if org-startup-truncated (setq truncate-lines t))
  3650. (org-set-local 'font-lock-unfontify-region-function
  3651. 'org-unfontify-region)
  3652. ;; Activate before-change-function
  3653. (org-set-local 'org-table-may-need-update t)
  3654. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3655. 'local)
  3656. ;; Check for running clock before killing a buffer
  3657. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3658. ;; Paragraphs and auto-filling
  3659. (org-set-autofill-regexps)
  3660. (setq indent-line-function 'org-indent-line-function)
  3661. (org-update-radio-target-regexp)
  3662. ;; Comment characters
  3663. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3664. (org-set-local 'comment-padding " ")
  3665. ;; Make isearch reveal context
  3666. (if (or (featurep 'xemacs)
  3667. (not (boundp 'outline-isearch-open-invisible-function)))
  3668. ;; Emacs 21 and XEmacs make use of the hook
  3669. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3670. ;; Emacs 22 deals with this through a special variable
  3671. (org-set-local 'outline-isearch-open-invisible-function
  3672. (lambda (&rest ignore) (org-show-context 'isearch))))
  3673. ;; If empty file that did not turn on org-mode automatically, make it to.
  3674. (if (and org-insert-mode-line-in-empty-file
  3675. (interactive-p)
  3676. (= (point-min) (point-max)))
  3677. (insert "# -*- mode: org -*-\n\n"))
  3678. (unless org-inhibit-startup
  3679. (when org-startup-align-all-tables
  3680. (let ((bmp (buffer-modified-p)))
  3681. (org-table-map-tables 'org-table-align)
  3682. (set-buffer-modified-p bmp)))
  3683. (org-cycle-hide-drawers 'all)
  3684. (cond
  3685. ((eq org-startup-folded t)
  3686. (org-cycle '(4)))
  3687. ((eq org-startup-folded 'content)
  3688. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3689. (org-cycle '(4)) (org-cycle '(4)))))))
  3690. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3691. (defsubst org-call-with-arg (command arg)
  3692. "Call COMMAND interactively, but pretend prefix are was ARG."
  3693. (let ((current-prefix-arg arg)) (call-interactively command)))
  3694. (defsubst org-current-line (&optional pos)
  3695. (save-excursion
  3696. (and pos (goto-char pos))
  3697. ;; works also in narrowed buffer, because we start at 1, not point-min
  3698. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3699. (defun org-current-time ()
  3700. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3701. (if (> org-time-stamp-rounding-minutes 0)
  3702. (let ((r org-time-stamp-rounding-minutes)
  3703. (time (decode-time)))
  3704. (apply 'encode-time
  3705. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3706. (nthcdr 2 time))))
  3707. (current-time)))
  3708. (defun org-add-props (string plist &rest props)
  3709. "Add text properties to entire string, from beginning to end.
  3710. PLIST may be a list of properties, PROPS are individual properties and values
  3711. that will be added to PLIST. Returns the string that was modified."
  3712. (add-text-properties
  3713. 0 (length string) (if props (append plist props) plist) string)
  3714. string)
  3715. (put 'org-add-props 'lisp-indent-function 2)
  3716. ;;;; Font-Lock stuff, including the activators
  3717. (defvar org-mouse-map (make-sparse-keymap))
  3718. (org-defkey org-mouse-map
  3719. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3720. (org-defkey org-mouse-map
  3721. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3722. (when org-mouse-1-follows-link
  3723. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3724. (when org-tab-follows-link
  3725. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3726. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3727. (when org-return-follows-link
  3728. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3729. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3730. (require 'font-lock)
  3731. (defconst org-non-link-chars "]\t\n\r<>")
  3732. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3733. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3734. (defvar org-link-re-with-space nil
  3735. "Matches a link with spaces, optional angular brackets around it.")
  3736. (defvar org-link-re-with-space2 nil
  3737. "Matches a link with spaces, optional angular brackets around it.")
  3738. (defvar org-angle-link-re nil
  3739. "Matches link with angular brackets, spaces are allowed.")
  3740. (defvar org-plain-link-re nil
  3741. "Matches plain link, without spaces.")
  3742. (defvar org-bracket-link-regexp nil
  3743. "Matches a link in double brackets.")
  3744. (defvar org-bracket-link-analytic-regexp nil
  3745. "Regular expression used to analyze links.
  3746. Here is what the match groups contain after a match:
  3747. 1: http:
  3748. 2: http
  3749. 3: path
  3750. 4: [desc]
  3751. 5: desc")
  3752. (defvar org-any-link-re nil
  3753. "Regular expression matching any link.")
  3754. (defun org-make-link-regexps ()
  3755. "Update the link regular expressions.
  3756. This should be called after the variable `org-link-types' has changed."
  3757. (setq org-link-re-with-space
  3758. (concat
  3759. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3760. "\\([^" org-non-link-chars " ]"
  3761. "[^" org-non-link-chars "]*"
  3762. "[^" org-non-link-chars " ]\\)>?")
  3763. org-link-re-with-space2
  3764. (concat
  3765. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3766. "\\([^" org-non-link-chars " ]"
  3767. "[^]\t\n\r]*"
  3768. "[^" org-non-link-chars " ]\\)>?")
  3769. org-angle-link-re
  3770. (concat
  3771. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3772. "\\([^" org-non-link-chars " ]"
  3773. "[^" org-non-link-chars "]*"
  3774. "\\)>")
  3775. org-plain-link-re
  3776. (concat
  3777. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3778. "\\([^]\t\n\r<>,;() ]+\\)")
  3779. org-bracket-link-regexp
  3780. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3781. org-bracket-link-analytic-regexp
  3782. (concat
  3783. "\\[\\["
  3784. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3785. "\\([^]]+\\)"
  3786. "\\]"
  3787. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3788. "\\]")
  3789. org-any-link-re
  3790. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3791. org-angle-link-re "\\)\\|\\("
  3792. org-plain-link-re "\\)")))
  3793. (org-make-link-regexps)
  3794. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3795. "Regular expression for fast time stamp matching.")
  3796. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3797. "Regular expression for fast time stamp matching.")
  3798. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3799. "Regular expression matching time strings for analysis.
  3800. This one does not require the space after the date.")
  3801. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3802. "Regular expression matching time strings for analysis.")
  3803. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
  3804. "Regular expression matching time stamps, with groups.")
  3805. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
  3806. "Regular expression matching time stamps (also [..]), with groups.")
  3807. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3808. "Regular expression matching a time stamp range.")
  3809. (defconst org-tr-regexp-both
  3810. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3811. "Regular expression matching a time stamp range.")
  3812. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3813. org-ts-regexp "\\)?")
  3814. "Regular expression matching a time stamp or time stamp range.")
  3815. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3816. org-ts-regexp-both "\\)?")
  3817. "Regular expression matching a time stamp or time stamp range.
  3818. The time stamps may be either active or inactive.")
  3819. (defvar org-emph-face nil)
  3820. (defun org-do-emphasis-faces (limit)
  3821. "Run through the buffer and add overlays to links."
  3822. (let (rtn)
  3823. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3824. (if (not (= (char-after (match-beginning 3))
  3825. (char-after (match-beginning 4))))
  3826. (progn
  3827. (setq rtn t)
  3828. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3829. 'face
  3830. (nth 1 (assoc (match-string 3)
  3831. org-emphasis-alist)))
  3832. (add-text-properties (match-beginning 2) (match-end 2)
  3833. '(font-lock-multiline t))
  3834. (backward-char 1))))
  3835. rtn))
  3836. (defun org-emphasize (&optional char)
  3837. "Insert or change an emphasis, i.e. a font like bold or italic.
  3838. If there is an active region, change that region to a new emphasis.
  3839. If there is no region, just insert the marker characters and position
  3840. the cursor between them.
  3841. CHAR should be either the marker character, or the first character of the
  3842. HTML tag associated with that emphasis. If CHAR is a space, the means
  3843. to remove the emphasis of the selected region.
  3844. If char is not given (for example in an interactive call) it
  3845. will be prompted for."
  3846. (interactive)
  3847. (let ((eal org-emphasis-alist) e det
  3848. (erc org-emphasis-regexp-components)
  3849. (prompt "")
  3850. (string "") beg end move tag c s)
  3851. (if (org-region-active-p)
  3852. (setq beg (region-beginning) end (region-end)
  3853. string (buffer-substring beg end))
  3854. (setq move t))
  3855. (while (setq e (pop eal))
  3856. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3857. c (aref tag 0))
  3858. (push (cons c (string-to-char (car e))) det)
  3859. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3860. (substring tag 1)))))
  3861. (unless char
  3862. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3863. (setq char (read-char-exclusive)))
  3864. (setq char (or (cdr (assoc char det)) char))
  3865. (if (equal char ?\ )
  3866. (setq s "" move nil)
  3867. (unless (assoc (char-to-string char) org-emphasis-alist)
  3868. (error "No such emphasis marker: \"%c\"" char))
  3869. (setq s (char-to-string char)))
  3870. (while (and (> (length string) 1)
  3871. (equal (substring string 0 1) (substring string -1))
  3872. (assoc (substring string 0 1) org-emphasis-alist))
  3873. (setq string (substring string 1 -1)))
  3874. (setq string (concat s string s))
  3875. (if beg (delete-region beg end))
  3876. (unless (or (bolp)
  3877. (string-match (concat "[" (nth 0 erc) "\n]")
  3878. (char-to-string (char-before (point)))))
  3879. (insert " "))
  3880. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3881. (char-to-string (char-after (point))))
  3882. (insert " ") (backward-char 1))
  3883. (insert string)
  3884. (and move (backward-char 1))))
  3885. (defun org-activate-plain-links (limit)
  3886. "Run through the buffer and add overlays to links."
  3887. (catch 'exit
  3888. (let (f)
  3889. (while (re-search-forward org-plain-link-re limit t)
  3890. (setq f (get-text-property (match-beginning 0) 'face))
  3891. (if (or (eq f 'org-tag)
  3892. (and (listp f) (memq 'org-tag f)))
  3893. nil
  3894. (add-text-properties (match-beginning 0) (match-end 0)
  3895. (list 'mouse-face 'highlight
  3896. 'rear-nonsticky t
  3897. 'keymap org-mouse-map
  3898. ))
  3899. (throw 'exit t))))))
  3900. (defun org-activate-angle-links (limit)
  3901. "Run through the buffer and add overlays to links."
  3902. (if (re-search-forward org-angle-link-re limit t)
  3903. (progn
  3904. (add-text-properties (match-beginning 0) (match-end 0)
  3905. (list 'mouse-face 'highlight
  3906. 'rear-nonsticky t
  3907. 'keymap org-mouse-map
  3908. ))
  3909. t)))
  3910. (defmacro org-maybe-intangible (props)
  3911. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  3912. In emacs 21, invisible text is not avoided by the command loop, so the
  3913. intangible property is needed to make sure point skips this text.
  3914. In Emacs 22, this is not necessary. The intangible text property has
  3915. led to problems with flyspell. These problems are fixed in flyspell.el,
  3916. but we still avoid setting the property in Emacs 22 and later.
  3917. We use a macro so that the test can happen at compilation time."
  3918. (if (< emacs-major-version 22)
  3919. `(append '(intangible t) ,props)
  3920. props))
  3921. (defun org-activate-bracket-links (limit)
  3922. "Run through the buffer and add overlays to bracketed links."
  3923. (if (re-search-forward org-bracket-link-regexp limit t)
  3924. (let* ((help (concat "LINK: "
  3925. (org-match-string-no-properties 1)))
  3926. ;; FIXME: above we should remove the escapes.
  3927. ;; but that requires another match, protecting match data,
  3928. ;; a lot of overhead for font-lock.
  3929. (ip (org-maybe-intangible
  3930. (list 'invisible 'org-link 'rear-nonsticky t
  3931. 'keymap org-mouse-map 'mouse-face 'highlight
  3932. 'help-echo help)))
  3933. (vp (list 'rear-nonsticky t
  3934. 'keymap org-mouse-map 'mouse-face 'highlight
  3935. 'help-echo help)))
  3936. ;; We need to remove the invisible property here. Table narrowing
  3937. ;; may have made some of this invisible.
  3938. (remove-text-properties (match-beginning 0) (match-end 0)
  3939. '(invisible nil))
  3940. (if (match-end 3)
  3941. (progn
  3942. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3943. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3944. (add-text-properties (match-end 3) (match-end 0) ip))
  3945. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3946. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3947. (add-text-properties (match-end 1) (match-end 0) ip))
  3948. t)))
  3949. (defun org-activate-dates (limit)
  3950. "Run through the buffer and add overlays to dates."
  3951. (if (re-search-forward org-tsr-regexp-both limit t)
  3952. (progn
  3953. (add-text-properties (match-beginning 0) (match-end 0)
  3954. (list 'mouse-face 'highlight
  3955. 'rear-nonsticky t
  3956. 'keymap org-mouse-map))
  3957. (when org-display-custom-times
  3958. (if (match-end 3)
  3959. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3960. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3961. t)))
  3962. (defvar org-target-link-regexp nil
  3963. "Regular expression matching radio targets in plain text.")
  3964. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3965. "Regular expression matching a link target.")
  3966. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3967. "Regular expression matching a radio target.")
  3968. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3969. "Regular expression matching any target.")
  3970. (defun org-activate-target-links (limit)
  3971. "Run through the buffer and add overlays to target matches."
  3972. (when org-target-link-regexp
  3973. (let ((case-fold-search t))
  3974. (if (re-search-forward org-target-link-regexp limit t)
  3975. (progn
  3976. (add-text-properties (match-beginning 0) (match-end 0)
  3977. (list 'mouse-face 'highlight
  3978. 'rear-nonsticky t
  3979. 'keymap org-mouse-map
  3980. 'help-echo "Radio target link"
  3981. 'org-linked-text t))
  3982. t)))))
  3983. (defun org-update-radio-target-regexp ()
  3984. "Find all radio targets in this file and update the regular expression."
  3985. (interactive)
  3986. (when (memq 'radio org-activate-links)
  3987. (setq org-target-link-regexp
  3988. (org-make-target-link-regexp (org-all-targets 'radio)))
  3989. (org-restart-font-lock)))
  3990. (defun org-hide-wide-columns (limit)
  3991. (let (s e)
  3992. (setq s (text-property-any (point) (or limit (point-max))
  3993. 'org-cwidth t))
  3994. (when s
  3995. (setq e (next-single-property-change s 'org-cwidth))
  3996. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3997. (goto-char e)
  3998. t)))
  3999. (defun org-restart-font-lock ()
  4000. "Restart font-lock-mode, to force refontification."
  4001. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4002. ;; FIXME: Could font-lock-fontify-buffer be enough???
  4003. (font-lock-mode -1)
  4004. (font-lock-mode 1)))
  4005. (defun org-all-targets (&optional radio)
  4006. "Return a list of all targets in this file.
  4007. With optional argument RADIO, only find radio targets."
  4008. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4009. rtn)
  4010. (save-excursion
  4011. (goto-char (point-min))
  4012. (while (re-search-forward re nil t)
  4013. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4014. rtn)))
  4015. (defun org-make-target-link-regexp (targets)
  4016. "Make regular expression matching all strings in TARGETS.
  4017. The regular expression finds the targets also if there is a line break
  4018. between words."
  4019. (and targets
  4020. (concat
  4021. "\\<\\("
  4022. (mapconcat
  4023. (lambda (x)
  4024. (while (string-match " +" x)
  4025. (setq x (replace-match "\\s-+" t t x)))
  4026. x)
  4027. targets
  4028. "\\|")
  4029. "\\)\\>")))
  4030. (defun org-activate-tags (limit)
  4031. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4032. (progn
  4033. (add-text-properties (match-beginning 1) (match-end 1)
  4034. (list 'mouse-face 'highlight
  4035. 'rear-nonsticky t
  4036. 'keymap org-mouse-map))
  4037. t)))
  4038. (defun org-outline-level ()
  4039. (save-excursion
  4040. (looking-at outline-regexp)
  4041. (if (match-beginning 1)
  4042. (+ (org-get-string-indentation (match-string 1)) 1000)
  4043. (1- (- (match-end 0) (match-beginning 0))))))
  4044. (defvar org-font-lock-keywords nil)
  4045. (defconst org-property-re "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(\\S-.*\\)"
  4046. "Regular expression matching a property line.")
  4047. (defun org-set-font-lock-defaults ()
  4048. (let* ((em org-fontify-emphasized-text)
  4049. (lk org-activate-links)
  4050. (org-font-lock-extra-keywords
  4051. ;; Headlines
  4052. (list
  4053. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4054. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4055. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4056. (1 'org-table))
  4057. ;; Links
  4058. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4059. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4060. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4061. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4062. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4063. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4064. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4065. '(org-hide-wide-columns (0 nil append))
  4066. ;; TODO lines
  4067. (list (concat "^\\*+[ \t]+" org-not-done-regexp)
  4068. '(1 'org-todo t))
  4069. ;; Priorities
  4070. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4071. ;; Special keywords
  4072. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4073. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4074. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4075. (list (concat "\\<" org-archived-string) '(0 'org-special-keyword t))
  4076. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4077. ;; Emphasis
  4078. (if em
  4079. (if (featurep 'xemacs)
  4080. '(org-do-emphasis-faces (0 nil append))
  4081. '(org-do-emphasis-faces)))
  4082. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  4083. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4084. 2 'bold prepend)
  4085. (if org-provide-checkbox-statistics
  4086. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4087. (0 (org-get-checkbox-statistics-face) t)))
  4088. ;; COMMENT
  4089. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4090. "\\|" org-quote-string "\\)\\>")
  4091. '(1 'org-special-keyword t))
  4092. '("^#.*" (0 'font-lock-comment-face t))
  4093. ;; DONE
  4094. (if org-fontify-done-headline
  4095. (list (concat "^[*]+ +\\<\\("
  4096. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4097. "\\)\\(.*\\)")
  4098. '(1 'org-done t) '(2 'org-headline-done t))
  4099. (list (concat "^[*]+ +\\<\\("
  4100. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4101. "\\)\\>")
  4102. '(1 'org-done t)))
  4103. ;; Code
  4104. '("^[ \t]*\\(:.*\\)" (1 'org-code t))
  4105. ;; Table
  4106. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4107. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4108. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4109. ;; Drawers
  4110. (list org-drawer-regexp '(0 'org-special-keyword t))
  4111. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4112. ;; Properties
  4113. (list org-property-re
  4114. '(1 'org-special-keyword t)
  4115. '(3 'org-property-value t))
  4116. (if org-format-transports-properties-p
  4117. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4118. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  4119. )))
  4120. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4121. ;; Now set the full font-lock-keywords
  4122. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4123. (org-set-local 'font-lock-defaults
  4124. '(org-font-lock-keywords t nil nil backward-paragraph))
  4125. (kill-local-variable 'font-lock-keywords) nil))
  4126. (defvar org-m nil)
  4127. (defvar org-l nil)
  4128. (defvar org-f nil)
  4129. (defun org-get-level-face (n)
  4130. "Get the right face for match N in font-lock matching of healdines."
  4131. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4132. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4133. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4134. (cond
  4135. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4136. ((eq n 2) org-f)
  4137. (t (if org-level-color-stars-only nil org-f))))
  4138. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4139. "Remove fontification and activation overlays from links."
  4140. (font-lock-default-unfontify-region beg end)
  4141. (let* ((buffer-undo-list t)
  4142. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4143. (inhibit-modification-hooks t)
  4144. deactivate-mark buffer-file-name buffer-file-truename)
  4145. (remove-text-properties beg end
  4146. '(mouse-face t keymap t org-linked-text t
  4147. rear-nonsticky t
  4148. invisible t intangible t))))
  4149. ;;;; Visibility cycling, including org-goto and indirect buffer
  4150. ;;; Cycling
  4151. (defvar org-cycle-global-status nil)
  4152. (make-variable-buffer-local 'org-cycle-global-status)
  4153. (defvar org-cycle-subtree-status nil)
  4154. (make-variable-buffer-local 'org-cycle-subtree-status)
  4155. ;;;###autoload
  4156. (defun org-cycle (&optional arg)
  4157. "Visibility cycling for Org-mode.
  4158. - When this function is called with a prefix argument, rotate the entire
  4159. buffer through 3 states (global cycling)
  4160. 1. OVERVIEW: Show only top-level headlines.
  4161. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4162. 3. SHOW ALL: Show everything.
  4163. - When point is at the beginning of a headline, rotate the subtree started
  4164. by this line through 3 different states (local cycling)
  4165. 1. FOLDED: Only the main headline is shown.
  4166. 2. CHILDREN: The main headline and the direct children are shown.
  4167. From this state, you can move to one of the children
  4168. and zoom in further.
  4169. 3. SUBTREE: Show the entire subtree, including body text.
  4170. - When there is a numeric prefix, go up to a heading with level ARG, do
  4171. a `show-subtree' and return to the previous cursor position. If ARG
  4172. is negative, go up that many levels.
  4173. - When point is not at the beginning of a headline, execute
  4174. `indent-relative', like TAB normally does. See the option
  4175. `org-cycle-emulate-tab' for details.
  4176. - Special case: if point is at the beginning of the buffer and there is
  4177. no headline in line 1, this function will act as if called with prefix arg."
  4178. (interactive "P")
  4179. (let* ((outline-regexp
  4180. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4181. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4182. outline-regexp))
  4183. (bob-special (and org-cycle-global-at-bob (bobp)
  4184. (not (looking-at outline-regexp))))
  4185. (org-cycle-hook
  4186. (if bob-special
  4187. (delq 'org-optimize-window-after-visibility-change
  4188. (copy-sequence org-cycle-hook))
  4189. org-cycle-hook))
  4190. (pos (point)))
  4191. (if (or bob-special (equal arg '(4)))
  4192. ;; special case: use global cycling
  4193. (setq arg t))
  4194. (cond
  4195. ((org-at-table-p 'any)
  4196. ;; Enter the table or move to the next field in the table
  4197. (or (org-table-recognize-table.el)
  4198. (progn
  4199. (if arg (org-table-edit-field t)
  4200. (org-table-justify-field-maybe)
  4201. (call-interactively 'org-table-next-field)))))
  4202. ((eq arg t) ;; Global cycling
  4203. (cond
  4204. ((and (eq last-command this-command)
  4205. (eq org-cycle-global-status 'overview))
  4206. ;; We just created the overview - now do table of contents
  4207. ;; This can be slow in very large buffers, so indicate action
  4208. (message "CONTENTS...")
  4209. (org-content)
  4210. (message "CONTENTS...done")
  4211. (setq org-cycle-global-status 'contents)
  4212. (run-hook-with-args 'org-cycle-hook 'contents))
  4213. ((and (eq last-command this-command)
  4214. (eq org-cycle-global-status 'contents))
  4215. ;; We just showed the table of contents - now show everything
  4216. (show-all)
  4217. (message "SHOW ALL")
  4218. (setq org-cycle-global-status 'all)
  4219. (run-hook-with-args 'org-cycle-hook 'all))
  4220. (t
  4221. ;; Default action: go to overview
  4222. (org-overview)
  4223. (message "OVERVIEW")
  4224. (setq org-cycle-global-status 'overview)
  4225. (run-hook-with-args 'org-cycle-hook 'overview))))
  4226. ((and org-drawers org-drawer-regexp
  4227. (save-excursion
  4228. (beginning-of-line 1)
  4229. (looking-at org-drawer-regexp)))
  4230. ;; Toggle block visibility
  4231. (org-flag-drawer
  4232. (not (get-char-property (match-end 0) 'invisible))))
  4233. ((integerp arg)
  4234. ;; Show-subtree, ARG levels up from here.
  4235. (save-excursion
  4236. (org-back-to-heading)
  4237. (outline-up-heading (if (< arg 0) (- arg)
  4238. (- (funcall outline-level) arg)))
  4239. (org-show-subtree)))
  4240. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4241. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4242. ;; At a heading: rotate between three different views
  4243. (org-back-to-heading)
  4244. (let ((goal-column 0) eoh eol eos)
  4245. ;; First, some boundaries
  4246. (save-excursion
  4247. (org-back-to-heading)
  4248. (save-excursion
  4249. (beginning-of-line 2)
  4250. (while (and (not (eobp)) ;; this is like `next-line'
  4251. (get-char-property (1- (point)) 'invisible))
  4252. (beginning-of-line 2)) (setq eol (point)))
  4253. (outline-end-of-heading) (setq eoh (point))
  4254. (org-end-of-subtree t)
  4255. (unless (eobp)
  4256. (skip-chars-forward " \t\n")
  4257. (beginning-of-line 1) ; in case this is an item
  4258. )
  4259. (setq eos (1- (point))))
  4260. ;; Find out what to do next and set `this-command'
  4261. (cond
  4262. ((= eos eoh)
  4263. ;; Nothing is hidden behind this heading
  4264. (message "EMPTY ENTRY")
  4265. (setq org-cycle-subtree-status nil)
  4266. (save-excursion
  4267. (goto-char eos)
  4268. (outline-next-heading)
  4269. (if (org-invisible-p) (org-flag-heading nil))))
  4270. ((or (>= eol eos)
  4271. (not (string-match "\\S-" (buffer-substring eol eos))))
  4272. ;; Entire subtree is hidden in one line: open it
  4273. (org-show-entry)
  4274. (show-children)
  4275. (message "CHILDREN")
  4276. (save-excursion
  4277. (goto-char eos)
  4278. (outline-next-heading)
  4279. (if (org-invisible-p) (org-flag-heading nil)))
  4280. (setq org-cycle-subtree-status 'children)
  4281. (run-hook-with-args 'org-cycle-hook 'children))
  4282. ((and (eq last-command this-command)
  4283. (eq org-cycle-subtree-status 'children))
  4284. ;; We just showed the children, now show everything.
  4285. (org-show-subtree)
  4286. (message "SUBTREE")
  4287. (setq org-cycle-subtree-status 'subtree)
  4288. (run-hook-with-args 'org-cycle-hook 'subtree))
  4289. (t
  4290. ;; Default action: hide the subtree.
  4291. (hide-subtree)
  4292. (message "FOLDED")
  4293. (setq org-cycle-subtree-status 'folded)
  4294. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4295. ;; TAB emulation
  4296. (buffer-read-only (org-back-to-heading))
  4297. ((org-try-cdlatex-tab))
  4298. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4299. (or (not (bolp))
  4300. (not (looking-at outline-regexp))))
  4301. (call-interactively (global-key-binding "\t")))
  4302. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4303. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4304. (or (and (eq org-cycle-emulate-tab 'white)
  4305. (= (match-end 0) (point-at-eol)))
  4306. (and (eq org-cycle-emulate-tab 'whitestart)
  4307. (>= (match-end 0) pos))))
  4308. t
  4309. (eq org-cycle-emulate-tab t))
  4310. (if (and (looking-at "[ \n\r\t]")
  4311. (string-match "^[ \t]*$" (buffer-substring
  4312. (point-at-bol) (point))))
  4313. (progn
  4314. (beginning-of-line 1)
  4315. (and (looking-at "[ \t]+") (replace-match ""))))
  4316. (call-interactively (global-key-binding "\t")))
  4317. (t (save-excursion
  4318. (org-back-to-heading)
  4319. (org-cycle))))))
  4320. ;;;###autoload
  4321. (defun org-global-cycle (&optional arg)
  4322. "Cycle the global visibility. For details see `org-cycle'."
  4323. (interactive "P")
  4324. (let ((org-cycle-include-plain-lists
  4325. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4326. (if (integerp arg)
  4327. (progn
  4328. (show-all)
  4329. (hide-sublevels arg)
  4330. (setq org-cycle-global-status 'contents))
  4331. (org-cycle '(4)))))
  4332. (defun org-overview ()
  4333. "Switch to overview mode, shoing only top-level headlines.
  4334. Really, this shows all headlines with level equal or greater than the level
  4335. of the first headline in the buffer. This is important, because if the
  4336. first headline is not level one, then (hide-sublevels 1) gives confusing
  4337. results."
  4338. (interactive)
  4339. (let ((level (save-excursion
  4340. (goto-char (point-min))
  4341. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4342. (progn
  4343. (goto-char (match-beginning 0))
  4344. (funcall outline-level))))))
  4345. (and level (hide-sublevels level))))
  4346. (defun org-content (&optional arg)
  4347. "Show all headlines in the buffer, like a table of contents.
  4348. With numerical argument N, show content up to level N."
  4349. (interactive "P")
  4350. (save-excursion
  4351. ;; Visit all headings and show their offspring
  4352. (and (integerp arg) (org-overview))
  4353. (goto-char (point-max))
  4354. (catch 'exit
  4355. (while (and (progn (condition-case nil
  4356. (outline-previous-visible-heading 1)
  4357. (error (goto-char (point-min))))
  4358. t)
  4359. (looking-at outline-regexp))
  4360. (if (integerp arg)
  4361. (show-children (1- arg))
  4362. (show-branches))
  4363. (if (bobp) (throw 'exit nil))))))
  4364. (defun org-optimize-window-after-visibility-change (state)
  4365. "Adjust the window after a change in outline visibility.
  4366. This function is the default value of the hook `org-cycle-hook'."
  4367. (when (get-buffer-window (current-buffer))
  4368. (cond
  4369. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4370. ; ((eq state 'overview) (org-beginning-of-line))
  4371. ((eq state 'content) nil)
  4372. ((eq state 'all) nil)
  4373. ((eq state 'folded) nil)
  4374. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4375. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4376. (defun org-cycle-show-empty-lines (state)
  4377. "Show empty lines above all visible headlines.
  4378. The region to be covered depends on STATE when called through
  4379. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4380. entire buffer covered. Note that an empty line is only shown if there
  4381. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4382. (when (> org-cycle-separator-lines 0)
  4383. (save-excursion
  4384. (let* ((n org-cycle-separator-lines)
  4385. (re (cond
  4386. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4387. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4388. (t (let ((ns (number-to-string (- n 2))))
  4389. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4390. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4391. beg end)
  4392. (cond
  4393. ((memq state '(overview contents t))
  4394. (setq beg (point-min) end (point-max)))
  4395. ((memq state '(children folded))
  4396. (setq beg (point) end (progn (org-end-of-subtree t t)
  4397. (beginning-of-line 2)
  4398. (point)))))
  4399. (when beg
  4400. (goto-char beg)
  4401. (while (re-search-forward re end t)
  4402. (if (not (get-char-property (match-end 1) 'invisible))
  4403. (outline-flag-region
  4404. (match-beginning 1) (match-end 1) nil)))))))
  4405. ;; Never hide empty lines at the end of the file.
  4406. (save-excursion
  4407. (goto-char (point-max))
  4408. (outline-previous-heading)
  4409. (outline-end-of-heading)
  4410. (if (and (looking-at "[ \t\n]+")
  4411. (= (match-end 0) (point-max)))
  4412. (outline-flag-region (point) (match-end 0) nil))))
  4413. (defun org-subtree-end-visible-p ()
  4414. "Is the end of the current subtree visible?"
  4415. (pos-visible-in-window-p
  4416. (save-excursion (org-end-of-subtree t) (point))))
  4417. (defun org-first-headline-recenter (&optional N)
  4418. "Move cursor to the first headline and recenter the headline.
  4419. Optional argument N means, put the headline into the Nth line of the window."
  4420. (goto-char (point-min))
  4421. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4422. (beginning-of-line)
  4423. (recenter (prefix-numeric-value N))))
  4424. ;;; Org-goto
  4425. (defvar org-goto-window-configuration nil)
  4426. (defvar org-goto-marker nil)
  4427. (defvar org-goto-map
  4428. (let ((map (make-sparse-keymap)))
  4429. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  4430. (while (setq cmd (pop cmds))
  4431. (substitute-key-definition cmd cmd map global-map)))
  4432. (org-defkey map "\C-m" 'org-goto-ret)
  4433. (org-defkey map [(left)] 'org-goto-left)
  4434. (org-defkey map [(right)] 'org-goto-right)
  4435. (org-defkey map [(?q)] 'org-goto-quit)
  4436. (org-defkey map [(control ?g)] 'org-goto-quit)
  4437. (org-defkey map "\C-i" 'org-cycle)
  4438. (org-defkey map [(tab)] 'org-cycle)
  4439. (org-defkey map [(down)] 'outline-next-visible-heading)
  4440. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4441. (org-defkey map "n" 'outline-next-visible-heading)
  4442. (org-defkey map "p" 'outline-previous-visible-heading)
  4443. (org-defkey map "f" 'outline-forward-same-level)
  4444. (org-defkey map "b" 'outline-backward-same-level)
  4445. (org-defkey map "u" 'outline-up-heading)
  4446. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4447. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4448. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4449. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4450. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4451. ;; FIXME: Could we use suppress-keymap?
  4452. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4453. (while l (org-defkey map (int-to-string (pop l)) 'digit-argument)))
  4454. map))
  4455. (defconst org-goto-help
  4456. "Select a location to jump to, press RET
  4457. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  4458. (defun org-goto ()
  4459. "Go to a different location of the document, keeping current visibility.
  4460. When you want to go to a different location in a document, the fastest way
  4461. is often to fold the entire buffer and then dive into the tree. This
  4462. method has the disadvantage, that the previous location will be folded,
  4463. which may not be what you want.
  4464. This command works around this by showing a copy of the current buffer in
  4465. overview mode. You can dive into the tree in that copy, to find the
  4466. location you want to reach. When pressing RET, the command returns to the
  4467. original buffer in which the visibility is still unchanged. It then jumps
  4468. to the new location, making it and the headline hierarchy above it visible."
  4469. (interactive)
  4470. (let* ((org-goto-start-pos (point))
  4471. (selected-point
  4472. (org-get-location (current-buffer) org-goto-help)))
  4473. (if selected-point
  4474. (progn
  4475. (org-mark-ring-push org-goto-start-pos)
  4476. (goto-char selected-point)
  4477. (if (or (org-invisible-p) (org-invisible-p2))
  4478. (org-show-context 'org-goto)))
  4479. (error "Quit"))))
  4480. (defvar org-selected-point nil) ; dynamically scoped parameter
  4481. (defun org-get-location (buf help)
  4482. "Let the user select a location in the Org-mode buffer BUF.
  4483. This function uses a recursive edit. It returns the selected position
  4484. or nil."
  4485. (let (org-selected-point)
  4486. (save-excursion
  4487. (save-window-excursion
  4488. (delete-other-windows)
  4489. (switch-to-buffer (get-buffer-create "*org-goto*"))
  4490. (with-output-to-temp-buffer "*Help*"
  4491. (princ help))
  4492. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4493. (setq buffer-read-only nil)
  4494. (erase-buffer)
  4495. (insert-buffer-substring buf)
  4496. (let ((org-startup-truncated t)
  4497. (org-startup-folded t)
  4498. (org-startup-align-all-tables nil))
  4499. (org-mode))
  4500. (setq buffer-read-only t)
  4501. (if (and (boundp 'org-goto-start-pos)
  4502. (integer-or-marker-p org-goto-start-pos))
  4503. (let ((org-show-hierarchy-above t)
  4504. (org-show-siblings t)
  4505. (org-show-following-heading t))
  4506. (goto-char org-goto-start-pos)
  4507. (and (org-invisible-p) (org-show-context)))
  4508. (goto-char (point-min)))
  4509. (org-beginning-of-line)
  4510. (message "Select location and press RET")
  4511. ;; now we make sure that during selection, ony very few keys work
  4512. ;; and that it is impossible to switch to another window.
  4513. (let ((gm (current-global-map))
  4514. (overriding-local-map org-goto-map))
  4515. (unwind-protect
  4516. (progn
  4517. (use-global-map org-goto-map)
  4518. (recursive-edit))
  4519. (use-global-map gm)))))
  4520. (kill-buffer "*org-goto*")
  4521. org-selected-point))
  4522. (defun org-goto-ret (&optional arg)
  4523. "Finish `org-goto' by going to the new location."
  4524. (interactive "P")
  4525. (setq org-selected-point (point)
  4526. current-prefix-arg arg)
  4527. (throw 'exit nil))
  4528. (defun org-goto-left ()
  4529. "Finish `org-goto' by going to the new location."
  4530. (interactive)
  4531. (if (org-on-heading-p)
  4532. (progn
  4533. (beginning-of-line 1)
  4534. (setq org-selected-point (point)
  4535. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4536. (throw 'exit nil))
  4537. (error "Not on a heading")))
  4538. (defun org-goto-right ()
  4539. "Finish `org-goto' by going to the new location."
  4540. (interactive)
  4541. (if (org-on-heading-p)
  4542. (progn
  4543. (outline-end-of-subtree)
  4544. (or (eobp) (forward-char 1))
  4545. (setq org-selected-point (point)
  4546. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  4547. (throw 'exit nil))
  4548. (error "Not on a heading")))
  4549. (defun org-goto-quit ()
  4550. "Finish `org-goto' without cursor motion."
  4551. (interactive)
  4552. (setq org-selected-point nil)
  4553. (throw 'exit nil))
  4554. ;;; Indirect buffer display of subtrees
  4555. (defvar org-indirect-dedicated-frame nil
  4556. "This is the frame being used for indirect tree display.")
  4557. (defvar org-last-indirect-buffer nil)
  4558. (defun org-tree-to-indirect-buffer (&optional arg)
  4559. "Create indirect buffer and narrow it to current subtree.
  4560. With numerical prefix ARG, go up to this level and then take that tree.
  4561. If ARG is negative, go up that many levels.
  4562. Normally this command removes the indirect buffer previously made
  4563. with this command. However, when called with a C-u prefix, the last buffer
  4564. is kept so that you can work with several indirect buffers at the same time.
  4565. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4566. requests that a new frame be made for the new buffer, so that the dedicated
  4567. frame is not changed."
  4568. (interactive "P")
  4569. (let ((cbuf (current-buffer))
  4570. (cwin (selected-window))
  4571. (pos (point))
  4572. beg end level heading ibuf)
  4573. (save-excursion
  4574. (org-back-to-heading t)
  4575. (when (numberp arg)
  4576. (setq level (org-outline-level))
  4577. (if (< arg 0) (setq arg (+ level arg)))
  4578. (while (> (setq level (org-outline-level)) arg)
  4579. (outline-up-heading 1 t)))
  4580. (setq beg (point)
  4581. heading (org-get-heading))
  4582. (org-end-of-subtree t) (setq end (point)))
  4583. (if (and (not arg)
  4584. (buffer-live-p org-last-indirect-buffer))
  4585. (kill-buffer org-last-indirect-buffer))
  4586. (setq ibuf (org-get-indirect-buffer cbuf)
  4587. org-last-indirect-buffer ibuf)
  4588. (cond
  4589. ((or (eq org-indirect-buffer-display 'new-frame)
  4590. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4591. (select-frame (make-frame))
  4592. (delete-other-windows)
  4593. (switch-to-buffer ibuf)
  4594. (org-set-frame-title heading))
  4595. ((eq org-indirect-buffer-display 'dedicated-frame)
  4596. (raise-frame
  4597. (select-frame (or (and org-indirect-dedicated-frame
  4598. (frame-live-p org-indirect-dedicated-frame)
  4599. org-indirect-dedicated-frame)
  4600. (setq org-indirect-dedicated-frame (make-frame)))))
  4601. (delete-other-windows)
  4602. (switch-to-buffer ibuf)
  4603. (org-set-frame-title (concat "Indirect: " heading)))
  4604. ((eq org-indirect-buffer-display 'current-window)
  4605. (switch-to-buffer ibuf))
  4606. ((eq org-indirect-buffer-display 'other-window)
  4607. (pop-to-buffer ibuf))
  4608. (t (error "Invalid value.")))
  4609. (if (featurep 'xemacs)
  4610. (save-excursion (org-mode) (turn-on-font-lock)))
  4611. (narrow-to-region beg end)
  4612. (show-all)
  4613. (goto-char pos)
  4614. (and (window-live-p cwin) (select-window cwin))))
  4615. (defun org-get-indirect-buffer (&optional buffer)
  4616. (setq buffer (or buffer (current-buffer)))
  4617. (let ((n 1) (base (buffer-name buffer)) bname)
  4618. (while (buffer-live-p
  4619. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4620. (setq n (1+ n)))
  4621. (condition-case nil
  4622. (make-indirect-buffer buffer bname 'clone)
  4623. (error (make-indirect-buffer buffer bname)))))
  4624. (defun org-set-frame-title (title)
  4625. "Set the title of the current frame to the string TITLE."
  4626. ;; FIXME: how to name a single frame in XEmacs???
  4627. (unless (featurep 'xemacs)
  4628. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4629. ;;;; Structure editing
  4630. ;;; Inserting headlines
  4631. (defun org-insert-heading (&optional force-heading)
  4632. "Insert a new heading or item with same depth at point.
  4633. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4634. If point is at the beginning of a headline, insert a sibling before the
  4635. current headline. If point is in the middle of a headline, split the headline
  4636. at that position and make the rest of the headline part of the sibling below
  4637. the current headline."
  4638. (interactive "P")
  4639. (if (= (buffer-size) 0)
  4640. (insert "\n* ")
  4641. (when (or force-heading (not (org-insert-item)))
  4642. (let* ((head (save-excursion
  4643. (condition-case nil
  4644. (progn
  4645. (org-back-to-heading)
  4646. (match-string 0))
  4647. (error "*"))))
  4648. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4649. pos)
  4650. (cond
  4651. ((and (org-on-heading-p) (bolp)
  4652. (or (bobp)
  4653. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4654. (open-line (if blank 2 1)))
  4655. ((and (bolp)
  4656. (or (bobp)
  4657. (save-excursion
  4658. (backward-char 1) (not (org-invisible-p)))))
  4659. nil)
  4660. (t (newline (if blank 2 1))))
  4661. (insert head) (just-one-space)
  4662. (setq pos (point))
  4663. (end-of-line 1)
  4664. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4665. (run-hooks 'org-insert-heading-hook)))))
  4666. (defun org-insert-todo-heading (arg)
  4667. "Insert a new heading with the same level and TODO state as current heading.
  4668. If the heading has no TODO state, or if the state is DONE, use the first
  4669. state (TODO by default). Also with prefix arg, force first state."
  4670. (interactive "P")
  4671. (when (not (org-insert-item 'checkbox))
  4672. (org-insert-heading)
  4673. (save-excursion
  4674. (org-back-to-heading)
  4675. (outline-previous-heading)
  4676. (looking-at org-todo-line-regexp))
  4677. (if (or arg
  4678. (not (match-beginning 2))
  4679. (member (match-string 2) org-done-keywords))
  4680. (insert (car org-todo-keywords-1) " ")
  4681. (insert (match-string 2) " "))))
  4682. (defun org-insert-subheading (arg)
  4683. "Insert a new subheading and demote it.
  4684. Works for outline headings and for plain lists alike."
  4685. (interactive "P")
  4686. (org-insert-heading arg)
  4687. (cond
  4688. ((org-on-heading-p) (org-do-demote))
  4689. ((org-at-item-p) (org-indent-item 1))))
  4690. (defun org-insert-todo-subheading (arg)
  4691. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4692. Works for outline headings and for plain lists alike."
  4693. (interactive "P")
  4694. (org-insert-todo-heading arg)
  4695. (cond
  4696. ((org-on-heading-p) (org-do-demote))
  4697. ((org-at-item-p) (org-indent-item 1))))
  4698. ;;; Promotion and Demotion
  4699. (defun org-promote-subtree ()
  4700. "Promote the entire subtree.
  4701. See also `org-promote'."
  4702. (interactive)
  4703. (save-excursion
  4704. (org-map-tree 'org-promote))
  4705. (org-fix-position-after-promote))
  4706. (defun org-demote-subtree ()
  4707. "Demote the entire subtree. See `org-demote'.
  4708. See also `org-promote'."
  4709. (interactive)
  4710. (save-excursion
  4711. (org-map-tree 'org-demote))
  4712. (org-fix-position-after-promote))
  4713. (defun org-do-promote ()
  4714. "Promote the current heading higher up the tree.
  4715. If the region is active in `transient-mark-mode', promote all headings
  4716. in the region."
  4717. (interactive)
  4718. (save-excursion
  4719. (if (org-region-active-p)
  4720. (org-map-region 'org-promote (region-beginning) (region-end))
  4721. (org-promote)))
  4722. (org-fix-position-after-promote))
  4723. (defun org-do-demote ()
  4724. "Demote the current heading lower down the tree.
  4725. If the region is active in `transient-mark-mode', demote all headings
  4726. in the region."
  4727. (interactive)
  4728. (save-excursion
  4729. (if (org-region-active-p)
  4730. (org-map-region 'org-demote (region-beginning) (region-end))
  4731. (org-demote)))
  4732. (org-fix-position-after-promote))
  4733. (defun org-fix-position-after-promote ()
  4734. "Make sure that after pro/demotion cursor position is right."
  4735. (let ((pos (point)))
  4736. (when (save-excursion
  4737. (beginning-of-line 1)
  4738. (looking-at org-todo-line-regexp)
  4739. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4740. (cond ((eobp) (insert " "))
  4741. ((eolp) (insert " "))
  4742. ((equal (char-after) ?\ ) (forward-char 1))))))
  4743. (defun org-reduced-level (l)
  4744. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4745. (defun org-get-legal-level (level &optional change)
  4746. "Rectify a level change under the influence of `org-odd-levels-only'
  4747. LEVEL is a current level, CHANGE is by how much the level should be
  4748. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4749. even level numbers will become the next higher odd number."
  4750. (if org-odd-levels-only
  4751. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4752. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4753. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4754. (max 1 (+ level change))))
  4755. (defun org-promote ()
  4756. "Promote the current heading higher up the tree.
  4757. If the region is active in `transient-mark-mode', promote all headings
  4758. in the region."
  4759. (org-back-to-heading t)
  4760. (let* ((level (save-match-data (funcall outline-level)))
  4761. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  4762. (diff (abs (- level (length up-head) -1))))
  4763. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4764. (replace-match up-head nil t)
  4765. ;; Fixup tag positioning
  4766. (and org-auto-align-tags (org-set-tags nil t))
  4767. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4768. (defun org-demote ()
  4769. "Demote the current heading lower down the tree.
  4770. If the region is active in `transient-mark-mode', demote all headings
  4771. in the region."
  4772. (org-back-to-heading t)
  4773. (let* ((level (save-match-data (funcall outline-level)))
  4774. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  4775. (diff (abs (- level (length down-head) -1))))
  4776. (replace-match down-head nil t)
  4777. ;; Fixup tag positioning
  4778. (and org-auto-align-tags (org-set-tags nil t))
  4779. (if org-adapt-indentation (org-fixup-indentation diff))))
  4780. (defun org-map-tree (fun)
  4781. "Call FUN for every heading underneath the current one."
  4782. (org-back-to-heading)
  4783. (let ((level (funcall outline-level)))
  4784. (save-excursion
  4785. (funcall fun)
  4786. (while (and (progn
  4787. (outline-next-heading)
  4788. (> (funcall outline-level) level))
  4789. (not (eobp)))
  4790. (funcall fun)))))
  4791. (defun org-map-region (fun beg end)
  4792. "Call FUN for every heading between BEG and END."
  4793. (let ((org-ignore-region t))
  4794. (save-excursion
  4795. (setq end (copy-marker end))
  4796. (goto-char beg)
  4797. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4798. (< (point) end))
  4799. (funcall fun))
  4800. (while (and (progn
  4801. (outline-next-heading)
  4802. (< (point) end))
  4803. (not (eobp)))
  4804. (funcall fun)))))
  4805. (defun org-fixup-indentation (diff)
  4806. "Change the indentation in the current entry by DIFF
  4807. However, if any line in the current entry has no indentation, or if it
  4808. would end up with no indentation after the change, nothing at all is done."
  4809. (save-excursion
  4810. (let ((end (save-excursion (outline-next-heading)
  4811. (point-marker)))
  4812. (prohibit (if (> diff 0)
  4813. "^\\S-"
  4814. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4815. col)
  4816. (unless (save-excursion (end-of-line 1)
  4817. (re-search-forward prohibit end t))
  4818. (while (re-search-forward "^[ \t]+" end t)
  4819. (goto-char (match-end 0))
  4820. (setq col (current-column))
  4821. (if (< diff 0) (replace-match ""))
  4822. (indent-to (+ diff col))))
  4823. (move-marker end nil))))
  4824. (defun org-convert-to-odd-levels ()
  4825. "Convert an org-mode file with all levels allowed to one with odd levels.
  4826. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4827. level 5 etc."
  4828. (interactive)
  4829. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4830. (let ((org-odd-levels-only nil) n)
  4831. (save-excursion
  4832. (goto-char (point-min))
  4833. (while (re-search-forward "^\\*\\*+ " nil t)
  4834. (setq n (- (length (match-string 0)) 2))
  4835. (while (>= (setq n (1- n)) 0)
  4836. (org-demote))
  4837. (end-of-line 1))))))
  4838. (defun org-convert-to-oddeven-levels ()
  4839. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4840. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4841. section with an even level, conversion would destroy the structure of the file. An error
  4842. is signaled in this case."
  4843. (interactive)
  4844. (goto-char (point-min))
  4845. ;; First check if there are no even levels
  4846. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4847. (org-show-context t)
  4848. (error "Not all levels are odd in this file. Conversion not possible."))
  4849. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4850. (let ((org-odd-levels-only nil) n)
  4851. (save-excursion
  4852. (goto-char (point-min))
  4853. (while (re-search-forward "^\\*\\*+ " nil t)
  4854. (setq n (/ (length (1- (match-string 0))) 2))
  4855. (while (>= (setq n (1- n)) 0)
  4856. (org-promote))
  4857. (end-of-line 1))))))
  4858. (defun org-tr-level (n)
  4859. "Make N odd if required."
  4860. (if org-odd-levels-only (1+ (/ n 2)) n))
  4861. ;;; Vertical tree motion, cutting and pasting of subtrees
  4862. (defun org-move-subtree-up (&optional arg)
  4863. "Move the current subtree up past ARG headlines of the same level."
  4864. (interactive "p")
  4865. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4866. (defun org-move-subtree-down (&optional arg)
  4867. "Move the current subtree down past ARG headlines of the same level."
  4868. (interactive "p")
  4869. (setq arg (prefix-numeric-value arg))
  4870. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4871. 'outline-get-last-sibling))
  4872. (ins-point (make-marker))
  4873. (cnt (abs arg))
  4874. beg end txt folded)
  4875. ;; Select the tree
  4876. (org-back-to-heading)
  4877. (setq beg (point))
  4878. (save-match-data
  4879. (save-excursion (outline-end-of-heading)
  4880. (setq folded (org-invisible-p)))
  4881. (outline-end-of-subtree))
  4882. (outline-next-heading)
  4883. (setq end (point))
  4884. ;; Find insertion point, with error handling
  4885. (goto-char beg)
  4886. (while (> cnt 0)
  4887. (or (and (funcall movfunc) (looking-at outline-regexp))
  4888. (progn (goto-char beg)
  4889. (error "Cannot move past superior level or buffer limit")))
  4890. (setq cnt (1- cnt)))
  4891. (if (> arg 0)
  4892. ;; Moving forward - still need to move over subtree
  4893. (progn (outline-end-of-subtree)
  4894. (outline-next-heading)
  4895. (if (not (or (looking-at (concat "^" outline-regexp))
  4896. (bolp)))
  4897. (newline))))
  4898. (move-marker ins-point (point))
  4899. (setq txt (buffer-substring beg end))
  4900. (delete-region beg end)
  4901. (insert txt)
  4902. (or (bolp) (insert "\n"))
  4903. (goto-char ins-point)
  4904. (if folded (hide-subtree))
  4905. (move-marker ins-point nil)))
  4906. (defvar org-subtree-clip ""
  4907. "Clipboard for cut and paste of subtrees.
  4908. This is actually only a copy of the kill, because we use the normal kill
  4909. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4910. (defvar org-subtree-clip-folded nil
  4911. "Was the last copied subtree folded?
  4912. This is used to fold the tree back after pasting.")
  4913. (defun org-cut-subtree ()
  4914. "Cut the current subtree into the clipboard.
  4915. This is a short-hand for marking the subtree and then cutting it."
  4916. (interactive)
  4917. (org-copy-subtree 'cut))
  4918. (defun org-copy-subtree (&optional cut)
  4919. "Cut the current subtree into the clipboard.
  4920. This is a short-hand for marking the subtree and then copying it.
  4921. If CUT is non-nil, actually cut the subtree."
  4922. (interactive)
  4923. (let (beg end folded)
  4924. (if (interactive-p)
  4925. (org-back-to-heading nil) ; take what looks like a subtree
  4926. (org-back-to-heading t)) ; take what is really there
  4927. (setq beg (point))
  4928. (save-match-data
  4929. (save-excursion (outline-end-of-heading)
  4930. (setq folded (org-invisible-p)))
  4931. (outline-end-of-subtree))
  4932. (if (equal (char-after) ?\n) (forward-char 1))
  4933. (setq end (point))
  4934. (goto-char beg)
  4935. (when (> end beg)
  4936. (setq org-subtree-clip-folded folded)
  4937. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4938. (setq org-subtree-clip (current-kill 0))
  4939. (message "%s: Subtree with %d characters"
  4940. (if cut "Cut" "Copied")
  4941. (length org-subtree-clip)))))
  4942. (defun org-paste-subtree (&optional level tree)
  4943. "Paste the clipboard as a subtree, with modification of headline level.
  4944. The entire subtree is promoted or demoted in order to match a new headline
  4945. level. By default, the new level is derived from the visible headings
  4946. before and after the insertion point, and taken to be the inferior headline
  4947. level of the two. So if the previous visible heading is level 3 and the
  4948. next is level 4 (or vice versa), level 4 will be used for insertion.
  4949. This makes sure that the subtree remains an independent subtree and does
  4950. not swallow low level entries.
  4951. You can also force a different level, either by using a numeric prefix
  4952. argument, or by inserting the heading marker by hand. For example, if the
  4953. cursor is after \"*****\", then the tree will be shifted to level 5.
  4954. If you want to insert the tree as is, just use \\[yank].
  4955. If optional TREE is given, use this text instead of the kill ring."
  4956. (interactive "P")
  4957. (unless (org-kill-is-subtree-p tree)
  4958. (error
  4959. (substitute-command-keys
  4960. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4961. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4962. (^re (concat "^\\(" outline-regexp "\\)"))
  4963. (re (concat "\\(" outline-regexp "\\)"))
  4964. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  4965. (old-level (if (string-match ^re txt)
  4966. (- (match-end 0) (match-beginning 0) 1)
  4967. -1))
  4968. (force-level (cond (level (prefix-numeric-value level))
  4969. ((string-match
  4970. ^re_ (buffer-substring (point-at-bol) (point)))
  4971. (- (match-end 0) (match-beginning 0)))
  4972. (t nil)))
  4973. (previous-level (save-excursion
  4974. (condition-case nil
  4975. (progn
  4976. (outline-previous-visible-heading 1)
  4977. (if (looking-at re)
  4978. (- (match-end 0) (match-beginning 0))
  4979. 1))
  4980. (error 1))))
  4981. (next-level (save-excursion
  4982. (condition-case nil
  4983. (progn
  4984. (outline-next-visible-heading 1)
  4985. (if (looking-at re)
  4986. (- (match-end 0) (match-beginning 0))
  4987. 1))
  4988. (error 1))))
  4989. (new-level (or force-level (max previous-level next-level)))
  4990. (shift (if (or (= old-level -1)
  4991. (= new-level -1)
  4992. (= old-level new-level))
  4993. 0
  4994. (- new-level old-level)))
  4995. (shift1 shift)
  4996. (delta (if (> shift 0) -1 1))
  4997. (func (if (> shift 0) 'org-demote 'org-promote))
  4998. (org-odd-levels-only nil)
  4999. beg end)
  5000. ;; Remove the forces level indicator
  5001. (if force-level
  5002. (delete-region (point-at-bol) (point)))
  5003. ;; Make sure we start at the beginning of an empty line
  5004. (if (not (bolp)) (insert "\n"))
  5005. (if (not (looking-at "[ \t]*$"))
  5006. (progn (insert "\n") (backward-char 1)))
  5007. ;; Paste
  5008. (setq beg (point))
  5009. (if (string-match "[ \t\r\n]+\\'" txt)
  5010. (setq txt (replace-match "\n" t t txt)))
  5011. (insert txt)
  5012. (setq end (point))
  5013. (if (looking-at "[ \t\r\n]+")
  5014. (replace-match "\n"))
  5015. (goto-char beg)
  5016. ;; Shift if necessary
  5017. (if (= shift 0)
  5018. (message "Pasted at level %d, without shift" new-level)
  5019. (save-restriction
  5020. (narrow-to-region beg end)
  5021. (while (not (= shift 0))
  5022. (org-map-region func (point-min) (point-max))
  5023. (setq shift (+ delta shift)))
  5024. (goto-char (point-min))
  5025. (message "Pasted at level %d, with shift by %d levels"
  5026. new-level shift1)))
  5027. (if (and kill-ring
  5028. (eq org-subtree-clip (current-kill 0))
  5029. org-subtree-clip-folded)
  5030. ;; The tree was folded before it was killed/copied
  5031. (hide-subtree))))
  5032. (defun org-kill-is-subtree-p (&optional txt)
  5033. "Check if the current kill is an outline subtree, or a set of trees.
  5034. Returns nil if kill does not start with a headline, or if the first
  5035. headline level is not the largest headline level in the tree.
  5036. So this will actually accept several entries of equal levels as well,
  5037. which is OK for `org-paste-subtree'.
  5038. If optional TXT is given, check this string instead of the current kill."
  5039. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5040. (start-level (and kill
  5041. (string-match (concat "\\`" outline-regexp) kill)
  5042. (- (match-end 0) (match-beginning 0))))
  5043. (re (concat "^" outline-regexp))
  5044. (start 1))
  5045. (if (not start-level)
  5046. nil ;; does not even start with a heading
  5047. (catch 'exit
  5048. (while (setq start (string-match re kill (1+ start)))
  5049. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  5050. (throw 'exit nil)))
  5051. t))))
  5052. (defun org-narrow-to-subtree ()
  5053. "Narrow buffer to the current subtree."
  5054. (interactive)
  5055. (save-excursion
  5056. (narrow-to-region
  5057. (progn (org-back-to-heading) (point))
  5058. (progn (org-end-of-subtree t t) (point)))))
  5059. ;;; Outline Sorting
  5060. (defun org-sort (with-case)
  5061. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  5062. (interactive "P")
  5063. (if (org-at-table-p)
  5064. (org-call-with-arg 'org-table-sort-lines with-case)
  5065. (org-call-with-arg 'org-sort-entries with-case)))
  5066. (defun org-sort-entries (&optional with-case sorting-type)
  5067. "Sort entries on a certain level of an outline tree.
  5068. If there is an active region, the entries in the region are sorted.
  5069. Else, if the cursor is before the first entry, sort the top-level items.
  5070. Else, the children of the entry at point are sorted.
  5071. Sorting can be alphabetically, numerically, and by date/time as given by
  5072. the first time stamp in the entry. The command prompts for the sorting
  5073. type unless it has been given to the function through the SORTING-TYPE
  5074. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  5075. Comparing entries ignores case by default. However, with an optional argument
  5076. WITH-CASE, the sorting considers case as well. With two prefix arguments
  5077. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  5078. (interactive "P")
  5079. (let ((unique (equal with-case '(16)))
  5080. start beg end entries stars re re2 p nentries (nremoved 0)
  5081. last txt what)
  5082. ;; Find beginning and end of region to sort
  5083. (cond
  5084. ((org-region-active-p)
  5085. ;; we will sort the region
  5086. (setq end (region-end)
  5087. what "region")
  5088. (goto-char (region-beginning))
  5089. (if (not (org-on-heading-p)) (outline-next-heading))
  5090. (setq start (point)))
  5091. ((or (org-on-heading-p)
  5092. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5093. ;; we will sort the children of the current headline
  5094. (org-back-to-heading)
  5095. (setq start (point) end (org-end-of-subtree) what "children")
  5096. (goto-char start)
  5097. (show-subtree)
  5098. (outline-next-heading))
  5099. (t
  5100. ;; we will sort the top-level entries in this file
  5101. (goto-char (point-min))
  5102. (or (org-on-heading-p) (outline-next-heading))
  5103. (setq start (point) end (point-max) what "top-level")
  5104. (goto-char start)
  5105. (show-all)))
  5106. (setq beg (point))
  5107. (if (>= (point) end) (error "Nothing to sort"))
  5108. (looking-at "\\(\\*+\\)")
  5109. (setq stars (match-string 1)
  5110. re (concat "^" (regexp-quote stars) " +")
  5111. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5112. txt (buffer-substring beg end))
  5113. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5114. (if (and (not (equal stars "*")) (string-match re2 txt))
  5115. (error "Region to sort contains a level above the first entry"))
  5116. ;; Make a list that can be sorted.
  5117. ;; The car is the string for comparison, the cdr is the subtree
  5118. (message "Sorting entries...")
  5119. (setq entries
  5120. (mapcar
  5121. (lambda (x)
  5122. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  5123. (cons (match-string 0 x) x))
  5124. (org-split-string txt re)))
  5125. ;; Sort the list
  5126. (save-excursion
  5127. (goto-char start)
  5128. (setq entries (org-do-sort entries what with-case sorting-type)))
  5129. ;; Delete the old stuff
  5130. (goto-char beg)
  5131. (kill-region beg end)
  5132. (setq nentries (length entries))
  5133. ;; Insert the sorted entries, and remove duplicates if this is required
  5134. (while (setq p (pop entries))
  5135. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  5136. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  5137. (insert stars " " (cdr p))))
  5138. (goto-char start)
  5139. (message "Sorting entries...done (%d entries%s)"
  5140. nentries
  5141. (if unique (format ", %d duplicates removed" nremoved) ""))))
  5142. (defun org-do-sort (table what &optional with-case sorting-type)
  5143. "Sort TABLE of WHAT according to SORTING-TYPE.
  5144. The user will be prompted for the SORTING-TYPE if the call to this
  5145. function does not specify it. WHAT is only for the prompt, to indicate
  5146. what is being sorted. The sorting key will be extracted from
  5147. the car of the elements of the table.
  5148. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5149. (unless sorting-type
  5150. (message
  5151. "Sort %s: [a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
  5152. what)
  5153. (setq sorting-type (read-char-exclusive)))
  5154. (let ((dcst (downcase sorting-type))
  5155. extractfun comparefun)
  5156. ;; Define the appropriate functions
  5157. (cond
  5158. ((= dcst ?n)
  5159. (setq extractfun 'string-to-number
  5160. comparefun (if (= dcst sorting-type) '< '>)))
  5161. ((= dcst ?a)
  5162. (setq extractfun (if with-case 'identity 'downcase)
  5163. comparefun (if (= dcst sorting-type)
  5164. 'string<
  5165. (lambda (a b) (and (not (string< a b))
  5166. (not (string= a b)))))))
  5167. ((= dcst ?t)
  5168. (setq extractfun
  5169. (lambda (x)
  5170. (if (string-match org-ts-regexp x)
  5171. (time-to-seconds
  5172. (org-time-string-to-time (match-string 0 x)))
  5173. 0))
  5174. comparefun (if (= dcst sorting-type) '< '>)))
  5175. (t (error "Invalid sorting type `%c'" sorting-type)))
  5176. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5177. table)
  5178. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5179. ;;;; Plain list items, including checkboxes
  5180. ;;; Plain list items
  5181. (defun org-at-item-p ()
  5182. "Is point in a line starting a hand-formatted item?"
  5183. (let ((llt org-plain-list-ordered-item-terminator))
  5184. (save-excursion
  5185. (goto-char (point-at-bol))
  5186. (looking-at
  5187. (cond
  5188. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5189. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5190. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5191. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5192. (defun org-in-item-p ()
  5193. "It the cursor inside a plain list item.
  5194. Does not have to be the first line."
  5195. (save-excursion
  5196. (condition-case nil
  5197. (progn
  5198. (org-beginning-of-item)
  5199. (org-at-item-p)
  5200. t)
  5201. (error nil))))
  5202. (defun org-insert-item (&optional checkbox)
  5203. "Insert a new item at the current level.
  5204. Return t when things worked, nil when we are not in an item."
  5205. (when (save-excursion
  5206. (condition-case nil
  5207. (progn
  5208. (org-beginning-of-item)
  5209. (org-at-item-p)
  5210. (if (org-invisible-p) (error "Invisible item"))
  5211. t)
  5212. (error nil)))
  5213. (let* ((bul (match-string 0))
  5214. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5215. (match-end 0)))
  5216. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5217. pos)
  5218. (cond
  5219. ((and (org-at-item-p) (<= (point) eow))
  5220. ;; before the bullet
  5221. (beginning-of-line 1)
  5222. (open-line (if blank 2 1)))
  5223. ((<= (point) eow)
  5224. (beginning-of-line 1))
  5225. (t (newline (if blank 2 1))))
  5226. (insert bul (if checkbox "[ ]" ""))
  5227. (just-one-space)
  5228. (setq pos (point))
  5229. (end-of-line 1)
  5230. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5231. (org-maybe-renumber-ordered-list)
  5232. (and checkbox (org-update-checkbox-count-maybe))
  5233. t))
  5234. ;;; Checkboxes
  5235. (defun org-at-item-checkbox-p ()
  5236. "Is point at a line starting a plain-list item with a checklet?"
  5237. (and (org-at-item-p)
  5238. (save-excursion
  5239. (goto-char (match-end 0))
  5240. (skip-chars-forward " \t")
  5241. (looking-at "\\[[- X]\\]"))))
  5242. (defun org-toggle-checkbox (&optional arg)
  5243. "Toggle the checkbox in the current line."
  5244. (interactive "P")
  5245. (catch 'exit
  5246. (let (beg end status (firstnew 'unknown))
  5247. (cond
  5248. ((org-region-active-p)
  5249. (setq beg (region-beginning) end (region-end)))
  5250. ((org-on-heading-p)
  5251. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5252. ((org-at-item-checkbox-p)
  5253. (save-excursion
  5254. (replace-match
  5255. (cond (arg "[-]")
  5256. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5257. (t "[ ]"))
  5258. t t))
  5259. (throw 'exit t))
  5260. (t (error "Not at a checkbox or heading, and no active region")))
  5261. (save-excursion
  5262. (goto-char beg)
  5263. (while (< (point) end)
  5264. (when (org-at-item-checkbox-p)
  5265. (setq status (equal (match-string 0) "[X]"))
  5266. (when (eq firstnew 'unknown)
  5267. (setq firstnew (not status)))
  5268. (replace-match
  5269. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5270. (beginning-of-line 2)))))
  5271. (org-update-checkbox-count-maybe))
  5272. (defun org-update-checkbox-count-maybe ()
  5273. "Update checkbox statistics unless turned off by user."
  5274. (when org-provide-checkbox-statistics
  5275. (org-update-checkbox-count)))
  5276. (defun org-update-checkbox-count (&optional all)
  5277. "Update the checkbox statistics in the current section.
  5278. This will find all statistic cookies like [57%] and [6/12] and update them
  5279. with the current numbers. With optional prefix argument ALL, do this for
  5280. the whole buffer."
  5281. (interactive "P")
  5282. (save-excursion
  5283. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5284. (beg (condition-case nil
  5285. (progn (outline-back-to-heading) (point))
  5286. (error (point-min))))
  5287. (end (move-marker (make-marker)
  5288. (progn (outline-next-heading) (point))))
  5289. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5290. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5291. b1 e1 f1 c-on c-off lim (cstat 0))
  5292. (when all
  5293. (goto-char (point-min))
  5294. (outline-next-heading)
  5295. (setq beg (point) end (point-max)))
  5296. (goto-char beg)
  5297. (while (re-search-forward re end t)
  5298. (setq cstat (1+ cstat)
  5299. b1 (match-beginning 0)
  5300. e1 (match-end 0)
  5301. f1 (match-beginning 1)
  5302. lim (cond
  5303. ((org-on-heading-p) (outline-next-heading) (point))
  5304. ((org-at-item-p) (org-end-of-item) (point))
  5305. (t nil))
  5306. c-on 0 c-off 0)
  5307. (goto-char e1)
  5308. (when lim
  5309. (while (re-search-forward re-box lim t)
  5310. (if (member (match-string 2) '("[ ]" "[-]"))
  5311. (setq c-off (1+ c-off))
  5312. (setq c-on (1+ c-on))))
  5313. ; (delete-region b1 e1)
  5314. (goto-char b1)
  5315. (insert (if f1
  5316. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5317. (format "[%d/%d]" c-on (+ c-on c-off))))
  5318. (and (looking-at "\\[.*?\\]")
  5319. (replace-match ""))))
  5320. (when (interactive-p)
  5321. (message "Checkbox satistics updated %s (%d places)"
  5322. (if all "in entire file" "in current outline entry") cstat)))))
  5323. (defun org-get-checkbox-statistics-face ()
  5324. "Select the face for checkbox statistics.
  5325. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5326. it will be `org-todo'."
  5327. (if (match-end 1)
  5328. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5329. (if (and (> (match-end 2) (match-beginning 2))
  5330. (equal (match-string 2) (match-string 3)))
  5331. 'org-done
  5332. 'org-todo)))
  5333. (defun org-get-indentation (&optional line)
  5334. "Get the indentation of the current line, interpreting tabs.
  5335. When LINE is given, assume it represents a line and compute its indentation."
  5336. (if line
  5337. (if (string-match "^ *" (org-remove-tabs line))
  5338. (match-end 0))
  5339. (save-excursion
  5340. (beginning-of-line 1)
  5341. (skip-chars-forward " \t")
  5342. (current-column))))
  5343. (defun org-remove-tabs (s &optional width)
  5344. "Replace tabulators in S with spaces.
  5345. Assumes that s is a single line, starting in column 0."
  5346. (setq width (or width tab-width))
  5347. (while (string-match "\t" s)
  5348. (setq s (replace-match
  5349. (make-string
  5350. (- (* width (/ (+ (match-beginning 0) width) width))
  5351. (match-beginning 0)) ?\ )
  5352. t t s)))
  5353. s)
  5354. (defun org-fix-indentation (line ind)
  5355. "Fix indentation in LINE.
  5356. IND is a cons cell with target and minimum indentation.
  5357. If the current indenation in LINE is smaller than the minimum,
  5358. leave it alone. If it is larger than ind, set it to the target."
  5359. (let* ((l (org-remove-tabs line))
  5360. (i (org-get-indentation l))
  5361. (i1 (car ind)) (i2 (cdr ind)))
  5362. (if (>= i i2) (setq l (substring line i2)))
  5363. (if (> i1 0)
  5364. (concat (make-string i1 ?\ ) l)
  5365. l)))
  5366. (defcustom org-empty-line-terminates-plain-lists nil
  5367. "Non-nil means, an empty line ends all plain list levels.
  5368. When nil, empty lines are part of the preceeding item."
  5369. :group 'org-plain-lists
  5370. :type 'boolean)
  5371. (defun org-beginning-of-item ()
  5372. "Go to the beginning of the current hand-formatted item.
  5373. If the cursor is not in an item, throw an error."
  5374. (interactive)
  5375. (let ((pos (point))
  5376. (limit (save-excursion
  5377. (condition-case nil
  5378. (progn
  5379. (org-back-to-heading)
  5380. (beginning-of-line 2) (point))
  5381. (error (point-min)))))
  5382. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5383. ind ind1)
  5384. (if (org-at-item-p)
  5385. (beginning-of-line 1)
  5386. (beginning-of-line 1)
  5387. (skip-chars-forward " \t")
  5388. (setq ind (current-column))
  5389. (if (catch 'exit
  5390. (while t
  5391. (beginning-of-line 0)
  5392. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5393. (if (looking-at "[ \t]*$")
  5394. (setq ind1 ind-empty)
  5395. (skip-chars-forward " \t")
  5396. (setq ind1 (current-column)))
  5397. (if (< ind1 ind)
  5398. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5399. nil
  5400. (goto-char pos)
  5401. (error "Not in an item")))))
  5402. (defun org-end-of-item ()
  5403. "Go to the end of the current hand-formatted item.
  5404. If the cursor is not in an item, throw an error."
  5405. (interactive)
  5406. (let* ((pos (point))
  5407. ind1
  5408. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5409. (limit (save-excursion (outline-next-heading) (point)))
  5410. (ind (save-excursion
  5411. (org-beginning-of-item)
  5412. (skip-chars-forward " \t")
  5413. (current-column)))
  5414. (end (catch 'exit
  5415. (while t
  5416. (beginning-of-line 2)
  5417. (if (eobp) (throw 'exit (point)))
  5418. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5419. (if (looking-at "[ \t]*$")
  5420. (setq ind1 ind-empty)
  5421. (skip-chars-forward " \t")
  5422. (setq ind1 (current-column)))
  5423. (if (<= ind1 ind)
  5424. (throw 'exit (point-at-bol)))))))
  5425. (if end
  5426. (goto-char end)
  5427. (goto-char pos)
  5428. (error "Not in an item"))))
  5429. (defun org-next-item ()
  5430. "Move to the beginning of the next item in the current plain list.
  5431. Error if not at a plain list, or if this is the last item in the list."
  5432. (interactive)
  5433. (let (ind ind1 (pos (point)))
  5434. (org-beginning-of-item)
  5435. (setq ind (org-get-indentation))
  5436. (org-end-of-item)
  5437. (setq ind1 (org-get-indentation))
  5438. (unless (and (org-at-item-p) (= ind ind1))
  5439. (goto-char pos)
  5440. (error "On last item"))))
  5441. (defun org-previous-item ()
  5442. "Move to the beginning of the previous item in the current plain list.
  5443. Error if not at a plain list, or if this is the first item in the list."
  5444. (interactive)
  5445. (let (beg ind ind1 (pos (point)))
  5446. (org-beginning-of-item)
  5447. (setq beg (point))
  5448. (setq ind (org-get-indentation))
  5449. (goto-char beg)
  5450. (catch 'exit
  5451. (while t
  5452. (beginning-of-line 0)
  5453. (if (looking-at "[ \t]*$")
  5454. nil
  5455. (if (<= (setq ind1 (org-get-indentation)) ind)
  5456. (throw 'exit t)))))
  5457. (condition-case nil
  5458. (if (or (not (org-at-item-p))
  5459. (< ind1 (1- ind)))
  5460. (error "")
  5461. (org-beginning-of-item))
  5462. (error (goto-char pos)
  5463. (error "On first item")))))
  5464. (defun org-move-item-down ()
  5465. "Move the plain list item at point down, i.e. swap with following item.
  5466. Subitems (items with larger indentation) are considered part of the item,
  5467. so this really moves item trees."
  5468. (interactive)
  5469. (let (beg end ind ind1 (pos (point)) txt)
  5470. (org-beginning-of-item)
  5471. (setq beg (point))
  5472. (setq ind (org-get-indentation))
  5473. (org-end-of-item)
  5474. (setq end (point))
  5475. (setq ind1 (org-get-indentation))
  5476. (if (and (org-at-item-p) (= ind ind1))
  5477. (progn
  5478. (org-end-of-item)
  5479. (setq txt (buffer-substring beg end))
  5480. (save-excursion
  5481. (delete-region beg end))
  5482. (setq pos (point))
  5483. (insert txt)
  5484. (goto-char pos)
  5485. (org-maybe-renumber-ordered-list))
  5486. (goto-char pos)
  5487. (error "Cannot move this item further down"))))
  5488. (defun org-move-item-up (arg)
  5489. "Move the plain list item at point up, i.e. swap with previous item.
  5490. Subitems (items with larger indentation) are considered part of the item,
  5491. so this really moves item trees."
  5492. (interactive "p")
  5493. (let (beg end ind ind1 (pos (point)) txt)
  5494. (org-beginning-of-item)
  5495. (setq beg (point))
  5496. (setq ind (org-get-indentation))
  5497. (org-end-of-item)
  5498. (setq end (point))
  5499. (goto-char beg)
  5500. (catch 'exit
  5501. (while t
  5502. (beginning-of-line 0)
  5503. (if (looking-at "[ \t]*$")
  5504. (if org-empty-line-terminates-plain-lists
  5505. (progn
  5506. (goto-char pos)
  5507. (error "Cannot move this item further up"))
  5508. nil)
  5509. (if (<= (setq ind1 (org-get-indentation)) ind)
  5510. (throw 'exit t)))))
  5511. (condition-case nil
  5512. (org-beginning-of-item)
  5513. (error (goto-char beg)
  5514. (error "Cannot move this item further up")))
  5515. (setq ind1 (org-get-indentation))
  5516. (if (and (org-at-item-p) (= ind ind1))
  5517. (progn
  5518. (setq txt (buffer-substring beg end))
  5519. (save-excursion
  5520. (delete-region beg end))
  5521. (setq pos (point))
  5522. (insert txt)
  5523. (goto-char pos)
  5524. (org-maybe-renumber-ordered-list))
  5525. (goto-char pos)
  5526. (error "Cannot move this item further up"))))
  5527. (defun org-maybe-renumber-ordered-list ()
  5528. "Renumber the ordered list at point if setup allows it.
  5529. This tests the user option `org-auto-renumber-ordered-lists' before
  5530. doing the renumbering."
  5531. (interactive)
  5532. (when (and org-auto-renumber-ordered-lists
  5533. (org-at-item-p))
  5534. (if (match-beginning 3)
  5535. (org-renumber-ordered-list 1)
  5536. (org-fix-bullet-type 1))))
  5537. (defun org-maybe-renumber-ordered-list-safe ()
  5538. (condition-case nil
  5539. (save-excursion
  5540. (org-maybe-renumber-ordered-list))
  5541. (error nil)))
  5542. (defun org-cycle-list-bullet (&optional which)
  5543. "Cycle through the different itemize/enumerate bullets.
  5544. This cycle the entire list level through the sequence:
  5545. `-' -> `+' -> `*' -> `1.' -> `1)'
  5546. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5547. 0 meand `-', 1 means `+' etc."
  5548. (interactive "P")
  5549. (org-preserve-lc
  5550. (org-beginning-of-item-list)
  5551. (org-at-item-p)
  5552. (beginning-of-line 1)
  5553. (let ((current (match-string 0)) new)
  5554. (setq new (cond
  5555. ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5556. ((string-match "-" current) "+")
  5557. ((string-match "\\+" current)
  5558. (if (looking-at "\\S-") "1." "*"))
  5559. ((string-match "\\*" current) "1.")
  5560. ((string-match "\\." current) "1)")
  5561. ((string-match ")" current) "-")
  5562. (t (error "This should not happen"))))
  5563. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5564. (org-fix-bullet-type 1)
  5565. (org-maybe-renumber-ordered-list))))
  5566. (defun org-get-string-indentation (s)
  5567. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5568. (let ((n -1) (i 0) (w tab-width) c)
  5569. (catch 'exit
  5570. (while (< (setq n (1+ n)) (length s))
  5571. (setq c (aref s n))
  5572. (cond ((= c ?\ ) (setq i (1+ i)))
  5573. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5574. (t (throw 'exit t)))))
  5575. i))
  5576. (defun org-renumber-ordered-list (arg)
  5577. "Renumber an ordered plain list.
  5578. Cursor needs to be in the first line of an item, the line that starts
  5579. with something like \"1.\" or \"2)\"."
  5580. (interactive "p")
  5581. (unless (and (org-at-item-p)
  5582. (match-beginning 3))
  5583. (error "This is not an ordered list"))
  5584. (let ((line (org-current-line))
  5585. (col (current-column))
  5586. (ind (org-get-string-indentation
  5587. (buffer-substring (point-at-bol) (match-beginning 3))))
  5588. ;; (term (substring (match-string 3) -1))
  5589. ind1 (n (1- arg))
  5590. fmt)
  5591. ;; find where this list begins
  5592. (org-beginning-of-item-list)
  5593. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5594. (setq fmt (concat "%d" (match-string 1)))
  5595. (beginning-of-line 0)
  5596. ;; walk forward and replace these numbers
  5597. (catch 'exit
  5598. (while t
  5599. (catch 'next
  5600. (beginning-of-line 2)
  5601. (if (eobp) (throw 'exit nil))
  5602. (if (looking-at "[ \t]*$") (throw 'next nil))
  5603. (skip-chars-forward " \t") (setq ind1 (current-column))
  5604. (if (> ind1 ind) (throw 'next t))
  5605. (if (< ind1 ind) (throw 'exit t))
  5606. (if (not (org-at-item-p)) (throw 'exit nil))
  5607. (delete-region (match-beginning 2) (match-end 2))
  5608. (goto-char (match-beginning 2))
  5609. (insert (format fmt (setq n (1+ n)))))))
  5610. (goto-line line)
  5611. (move-to-column col)))
  5612. (defun org-fix-bullet-type (arg)
  5613. "Make sure all items in this list have the same bullet."
  5614. (interactive "p")
  5615. (unless (org-at-item-p) (error "This is not a list"))
  5616. (let ((line (org-current-line))
  5617. (col (current-column))
  5618. (ind (current-indentation))
  5619. ind1 bullet)
  5620. ;; find where this list begins
  5621. (org-beginning-of-item-list)
  5622. (beginning-of-line 1)
  5623. ;; find out what the bullet type is
  5624. (looking-at "[ \t]*\\(\\S-+\\)")
  5625. (setq bullet (match-string 1))
  5626. ;; walk forward and replace these numbers
  5627. (beginning-of-line 0)
  5628. (catch 'exit
  5629. (while t
  5630. (catch 'next
  5631. (beginning-of-line 2)
  5632. (if (eobp) (throw 'exit nil))
  5633. (if (looking-at "[ \t]*$") (throw 'next nil))
  5634. (skip-chars-forward " \t") (setq ind1 (current-column))
  5635. (if (> ind1 ind) (throw 'next t))
  5636. (if (< ind1 ind) (throw 'exit t))
  5637. (if (not (org-at-item-p)) (throw 'exit nil))
  5638. (skip-chars-forward " \t")
  5639. (looking-at "\\S-+")
  5640. (replace-match bullet))))
  5641. (goto-line line)
  5642. (move-to-column col)
  5643. (if (string-match "[0-9]" bullet)
  5644. (org-renumber-ordered-list 1))))
  5645. (defun org-beginning-of-item-list ()
  5646. "Go to the beginning of the current item list.
  5647. I.e. to the first item in this list."
  5648. (interactive)
  5649. (org-beginning-of-item)
  5650. (let ((pos (point-at-bol))
  5651. (ind (org-get-indentation))
  5652. ind1)
  5653. ;; find where this list begins
  5654. (catch 'exit
  5655. (while t
  5656. (catch 'next
  5657. (beginning-of-line 0)
  5658. (if (looking-at "[ \t]*$")
  5659. (throw (if (bobp) 'exit 'next) t))
  5660. (skip-chars-forward " \t") (setq ind1 (current-column))
  5661. (if (or (< ind1 ind)
  5662. (and (= ind1 ind)
  5663. (not (org-at-item-p)))
  5664. (bobp))
  5665. (throw 'exit t)
  5666. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5667. (goto-char pos)))
  5668. (defvar org-last-indent-begin-marker (make-marker))
  5669. (defvar org-last-indent-end-marker (make-marker))
  5670. (defun org-outdent-item (arg)
  5671. "Outdent a local list item."
  5672. (interactive "p")
  5673. (org-indent-item (- arg)))
  5674. (defun org-indent-item (arg)
  5675. "Indent a local list item."
  5676. (interactive "p")
  5677. (unless (org-at-item-p)
  5678. (error "Not on an item"))
  5679. (save-excursion
  5680. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5681. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5682. (setq beg org-last-indent-begin-marker
  5683. end org-last-indent-end-marker)
  5684. (org-beginning-of-item)
  5685. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5686. (org-end-of-item)
  5687. (setq end (move-marker org-last-indent-end-marker (point))))
  5688. (goto-char beg)
  5689. (setq tmp (org-item-indent-positions)
  5690. ind (car tmp)
  5691. ind-down (nth 2 tmp)
  5692. ind-up (nth 1 tmp)
  5693. delta (if (> arg 0)
  5694. (if ind-down (- ind-down ind) 2)
  5695. (if ind-up (- ind-up ind) -2)))
  5696. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5697. (while (< (point) end)
  5698. (beginning-of-line 1)
  5699. (skip-chars-forward " \t") (setq ind1 (current-column))
  5700. (delete-region (point-at-bol) (point))
  5701. (or (eolp) (indent-to-column (+ ind1 delta)))
  5702. (beginning-of-line 2))))
  5703. (org-maybe-renumber-ordered-list-safe)
  5704. (save-excursion
  5705. (beginning-of-line 0)
  5706. (condition-case nil (org-beginning-of-item) (error nil))
  5707. (org-maybe-renumber-ordered-list-safe)))
  5708. (defun org-item-indent-positions ()
  5709. "Assumes cursor in item line. FIXME"
  5710. (let* ((bolpos (point-at-bol))
  5711. (ind (org-get-indentation))
  5712. ind-down ind-up pos)
  5713. (save-excursion
  5714. (org-beginning-of-item-list)
  5715. (skip-chars-backward "\n\r \t")
  5716. (when (org-in-item-p)
  5717. (org-beginning-of-item)
  5718. (setq ind-up (org-get-indentation))))
  5719. (setq pos (point))
  5720. (save-excursion
  5721. (cond
  5722. ((and (condition-case nil (progn (org-previous-item) t)
  5723. (error nil))
  5724. (or (forward-char 1) t)
  5725. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5726. (setq ind-down (org-get-indentation)))
  5727. ((and (goto-char pos)
  5728. (org-at-item-p))
  5729. (goto-char (match-end 0))
  5730. (skip-chars-forward " \t")
  5731. (setq ind-down (current-column)))))
  5732. (list ind ind-up ind-down)))
  5733. ;;; The orgstruct minor mode
  5734. ;; Define a minor mode which can be used in other modes in order to
  5735. ;; integrate the org-mode structure editing commands.
  5736. ;; This is really a hack, because the org-mode structure commands use
  5737. ;; keys which normally belong to the major mode. Here is how it
  5738. ;; works: The minor mode defines all the keys necessary to operate the
  5739. ;; structure commands, but wraps the commands into a function which
  5740. ;; tests if the cursor is currently at a headline or a plain list
  5741. ;; item. If that is the case, the structure command is used,
  5742. ;; temporarily setting many Org-mode variables like regular
  5743. ;; expressions for filling etc. However, when any of those keys is
  5744. ;; used at a different location, function uses `key-binding' to look
  5745. ;; up if the key has an associated command in another currently active
  5746. ;; keymap (minor modes, major mode, global), and executes that
  5747. ;; command. There might be problems if any of the keys is otherwise
  5748. ;; used as a prefix key.
  5749. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5750. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5751. ;; addresses this by checking explicitly for both bindings.
  5752. (defvar orgstruct-mode-map (make-sparse-keymap)
  5753. "Keymap for the minor `orgstruct-mode'.")
  5754. ;;;###autoload
  5755. (define-minor-mode orgstruct-mode
  5756. "Toggle the minor more `orgstruct-mode'.
  5757. This mode is for using Org-mode structure commands in other modes.
  5758. The following key behave as if Org-mode was active, if the cursor
  5759. is on a headline, or on a plain list item (both in the definition
  5760. of Org-mode).
  5761. M-up Move entry/item up
  5762. M-down Move entry/item down
  5763. M-left Promote
  5764. M-right Demote
  5765. M-S-up Move entry/item up
  5766. M-S-down Move entry/item down
  5767. M-S-left Promote subtree
  5768. M-S-right Demote subtree
  5769. M-q Fill paragraph and items like in Org-mode
  5770. C-c ^ Sort entries
  5771. C-c - Cycle list bullet
  5772. TAB Cycle item visibility
  5773. M-RET Insert new heading/item
  5774. S-M-RET Insert new TODO heading / Chekbox item
  5775. C-c C-c Set tags / toggle checkbox"
  5776. nil " OrgStruct" nil
  5777. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5778. ;;;###autoload
  5779. (defun turn-on-orgstruct ()
  5780. "Unconditionally turn on `orgstruct-mode'."
  5781. (orgstruct-mode 1))
  5782. (defun orgstruct-error ()
  5783. "Error when there is no default binding for a structure key."
  5784. (interactive)
  5785. (error "This key is has no function outside structure elements"))
  5786. (defvar org-local-vars nil
  5787. "List of local variables, for use by `orgstruct-mode'")
  5788. (defun orgstruct-setup ()
  5789. "Setup orgstruct keymaps."
  5790. (let ((nfunc 0)
  5791. (bindings
  5792. (list
  5793. '([(meta up)] org-metaup)
  5794. '([(meta down)] org-metadown)
  5795. '([(meta left)] org-metaleft)
  5796. '([(meta right)] org-metaright)
  5797. '([(meta shift up)] org-shiftmetaup)
  5798. '([(meta shift down)] org-shiftmetadown)
  5799. '([(meta shift left)] org-shiftmetaleft)
  5800. '([(meta shift right)] org-shiftmetaright)
  5801. '([(shift up)] org-shiftup)
  5802. '([(shift down)] org-shiftdown)
  5803. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5804. '("\M-q" fill-paragraph)
  5805. '("\C-c^" org-sort)
  5806. '("\C-c-" org-cycle-list-bullet)))
  5807. elt key fun cmd)
  5808. (while (setq elt (pop bindings))
  5809. (setq nfunc (1+ nfunc))
  5810. (setq key (org-key (car elt))
  5811. fun (nth 1 elt)
  5812. cmd (orgstruct-make-binding fun nfunc key))
  5813. (org-defkey orgstruct-mode-map key cmd))
  5814. ;; Special treatment needed for TAB and RET
  5815. (org-defkey orgstruct-mode-map [(tab)]
  5816. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5817. (org-defkey orgstruct-mode-map "\C-i"
  5818. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5819. (org-defkey orgstruct-mode-map "\M-\C-m"
  5820. (orgstruct-make-binding 'org-insert-heading 105
  5821. "\M-\C-m" [(meta return)]))
  5822. (org-defkey orgstruct-mode-map [(meta return)]
  5823. (orgstruct-make-binding 'org-insert-heading 106
  5824. [(meta return)] "\M-\C-m"))
  5825. (org-defkey orgstruct-mode-map [(shift meta return)]
  5826. (orgstruct-make-binding 'org-insert-todo-heading 107
  5827. [(meta return)] "\M-\C-m"))
  5828. (unless org-local-vars
  5829. (setq org-local-vars (org-get-local-variables)))
  5830. t))
  5831. (defun orgstruct-make-binding (fun n &rest keys)
  5832. "Create a function for binding in the structure minor mode.
  5833. FUN is the command to call inside a table. N is used to create a unique
  5834. command name. KEYS are keys that should be checked in for a command
  5835. to execute outside of tables."
  5836. (eval
  5837. (list 'defun
  5838. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5839. '(arg)
  5840. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5841. "Outside of structure, run the binding of `"
  5842. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5843. "'.")
  5844. '(interactive "p")
  5845. (list 'if
  5846. '(org-context-p 'headline 'item)
  5847. (list 'org-run-like-in-org-mode (list 'quote fun))
  5848. (list 'let '(orgstruct-mode)
  5849. (list 'call-interactively
  5850. (append '(or)
  5851. (mapcar (lambda (k)
  5852. (list 'key-binding k))
  5853. keys)
  5854. '('orgstruct-error))))))))
  5855. (defun org-context-p (&rest contexts)
  5856. "FIXME:"
  5857. (let ((pos (point)))
  5858. (goto-char (point-at-bol))
  5859. (prog1 (or (and (memq 'table contexts)
  5860. (looking-at "[ \t]*|"))
  5861. (and (memq 'headline contexts)
  5862. (looking-at "\\*+"))
  5863. (and (memq 'item contexts)
  5864. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5865. (goto-char pos))))
  5866. (defun org-get-local-variables ()
  5867. "Return a list of all local variables in an org-mode buffer."
  5868. (let (varlist)
  5869. (with-current-buffer (get-buffer-create "*Org tmp*")
  5870. (erase-buffer)
  5871. (org-mode)
  5872. (setq varlist (buffer-local-variables)))
  5873. (kill-buffer "*Org tmp*")
  5874. (delq nil
  5875. (mapcar
  5876. (lambda (x)
  5877. (setq x
  5878. (if (symbolp x)
  5879. (list x)
  5880. (list (car x) (list 'quote (cdr x)))))
  5881. (if (string-match
  5882. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5883. (symbol-name (car x)))
  5884. x nil))
  5885. varlist))))
  5886. ;;;###autoload
  5887. (defun org-run-like-in-org-mode (cmd)
  5888. (unless org-local-vars
  5889. (setq org-local-vars (org-get-local-variables)))
  5890. (eval (list 'let org-local-vars
  5891. (list 'call-interactively (list 'quote cmd)))))
  5892. ;;;; Archiving
  5893. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5894. (defun org-archive-subtree (&optional find-done)
  5895. "Move the current subtree to the archive.
  5896. The archive can be a certain top-level heading in the current file, or in
  5897. a different file. The tree will be moved to that location, the subtree
  5898. heading be marked DONE, and the current time will be added.
  5899. When called with prefix argument FIND-DONE, find whole trees without any
  5900. open TODO items and archive them (after getting confirmation from the user).
  5901. If the cursor is not at a headline when this comand is called, try all level
  5902. 1 trees. If the cursor is on a headline, only try the direct children of
  5903. this heading."
  5904. (interactive "P")
  5905. (if find-done
  5906. (org-archive-all-done)
  5907. ;; Save all relevant TODO keyword-relatex variables
  5908. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5909. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5910. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5911. (tr-org-done-keywords org-done-keywords)
  5912. (tr-org-todo-regexp org-todo-regexp)
  5913. (tr-org-todo-line-regexp org-todo-line-regexp)
  5914. (tr-org-odd-levels-only org-odd-levels-only)
  5915. (this-buffer (current-buffer))
  5916. (org-archive-location org-archive-location)
  5917. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5918. file heading buffer level newfile-p)
  5919. ;; Try to find a local archive location
  5920. (save-excursion
  5921. (save-restriction
  5922. (widen)
  5923. (if (or (re-search-backward re nil t) (re-search-forward re nil t))
  5924. (setq org-archive-location (match-string 1)))))
  5925. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5926. (progn
  5927. (setq file (format (match-string 1 org-archive-location)
  5928. (file-name-nondirectory buffer-file-name))
  5929. heading (match-string 2 org-archive-location)))
  5930. (error "Invalid `org-archive-location'"))
  5931. (if (> (length file) 0)
  5932. (setq newfile-p (not (file-exists-p file))
  5933. buffer (find-file-noselect file))
  5934. (setq buffer (current-buffer)))
  5935. (unless buffer
  5936. (error "Cannot access file \"%s\"" file))
  5937. (if (and (> (length heading) 0)
  5938. (string-match "^\\*+" heading))
  5939. (setq level (match-end 0))
  5940. (setq heading nil level 0))
  5941. (save-excursion
  5942. ;; We first only copy, in case something goes wrong
  5943. ;; we need to protect this-command, to avoid kill-region sets it,
  5944. ;; which would lead to duplication of subtrees
  5945. (let (this-command) (org-copy-subtree))
  5946. (set-buffer buffer)
  5947. ;; Enforce org-mode for the archive buffer
  5948. (if (not (org-mode-p))
  5949. ;; Force the mode for future visits.
  5950. (let ((org-insert-mode-line-in-empty-file t)
  5951. (org-inhibit-startup t))
  5952. (call-interactively 'org-mode)))
  5953. (when newfile-p
  5954. (goto-char (point-max))
  5955. (insert (format "\nArchived entries from file %s\n\n"
  5956. (buffer-file-name this-buffer))))
  5957. ;; Force the TODO keywords of the original buffer
  5958. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5959. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5960. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5961. (org-done-keywords tr-org-done-keywords)
  5962. (org-todo-regexp tr-org-todo-regexp)
  5963. (org-todo-line-regexp tr-org-todo-line-regexp)
  5964. (org-odd-levels-only
  5965. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5966. org-odd-levels-only
  5967. tr-org-odd-levels-only)))
  5968. (goto-char (point-min))
  5969. (if heading
  5970. (progn
  5971. (if (re-search-forward
  5972. (concat "^" (regexp-quote heading)
  5973. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5974. nil t)
  5975. (goto-char (match-end 0))
  5976. ;; Heading not found, just insert it at the end
  5977. (goto-char (point-max))
  5978. (or (bolp) (insert "\n"))
  5979. (insert "\n" heading "\n")
  5980. (end-of-line 0))
  5981. ;; Make the subtree visible
  5982. (show-subtree)
  5983. (org-end-of-subtree t)
  5984. (skip-chars-backward " \t\r\n")
  5985. (and (looking-at "[ \t\r\n]*")
  5986. (replace-match "\n\n")))
  5987. ;; No specific heading, just go to end of file.
  5988. (goto-char (point-max)) (insert "\n"))
  5989. ;; Paste
  5990. (org-paste-subtree (org-get-legal-level level 1))
  5991. ;; Mark the entry as done
  5992. (when (and org-archive-mark-done
  5993. (looking-at org-todo-line-regexp)
  5994. (or (not (match-end 2))
  5995. (not (member (match-string 2) org-done-keywords))))
  5996. (let (org-log-done)
  5997. (org-todo
  5998. (car (or (member org-archive-mark-done org-done-keywords)
  5999. org-done-keywords)))))
  6000. ;; Move cursor to right after the TODO keyword
  6001. (when org-archive-stamp-time
  6002. (org-add-planning-info 'archived (org-current-time)))
  6003. ;; Save the buffer, if it is not the same buffer.
  6004. (if (not (eq this-buffer buffer)) (save-buffer))))
  6005. ;; Here we are back in the original buffer. Everything seems to have
  6006. ;; worked. So now cut the tree and finish up.
  6007. (let (this-command) (org-cut-subtree))
  6008. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  6009. (message "Subtree archived %s"
  6010. (if (eq this-buffer buffer)
  6011. (concat "under heading: " heading)
  6012. (concat "in file: " (abbreviate-file-name file)))))))
  6013. (defun org-archive-all-done (&optional tag)
  6014. "Archive sublevels of the current tree without open TODO items.
  6015. If the cursor is not on a headline, try all level 1 trees. If
  6016. it is on a headline, try all direct children.
  6017. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  6018. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  6019. (rea (concat ".*:" org-archive-tag ":"))
  6020. (begm (make-marker))
  6021. (endm (make-marker))
  6022. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  6023. "Move subtree to archive (no open TODO items)? "))
  6024. beg end (cntarch 0))
  6025. (if (org-on-heading-p)
  6026. (progn
  6027. (setq re1 (concat "^" (regexp-quote
  6028. (make-string
  6029. (1+ (- (match-end 0) (match-beginning 0)))
  6030. ?*))
  6031. " "))
  6032. (move-marker begm (point))
  6033. (move-marker endm (org-end-of-subtree t)))
  6034. (setq re1 "^* ")
  6035. (move-marker begm (point-min))
  6036. (move-marker endm (point-max)))
  6037. (save-excursion
  6038. (goto-char begm)
  6039. (while (re-search-forward re1 endm t)
  6040. (setq beg (match-beginning 0)
  6041. end (save-excursion (org-end-of-subtree t) (point)))
  6042. (goto-char beg)
  6043. (if (re-search-forward re end t)
  6044. (goto-char end)
  6045. (goto-char beg)
  6046. (if (and (or (not tag) (not (looking-at rea)))
  6047. (y-or-n-p question))
  6048. (progn
  6049. (if tag
  6050. (org-toggle-tag org-archive-tag 'on)
  6051. (org-archive-subtree))
  6052. (setq cntarch (1+ cntarch)))
  6053. (goto-char end)))))
  6054. (message "%d trees archived" cntarch)))
  6055. (defun org-cycle-hide-drawers (state)
  6056. "Re-hide all drawers after a visibility state change."
  6057. (when (and (org-mode-p)
  6058. (not (memq state '(overview folded))))
  6059. (save-excursion
  6060. (let* ((globalp (memq state '(contents all)))
  6061. (beg (if globalp (point-min) (point)))
  6062. (end (if globalp (point-max) (org-end-of-subtree t))))
  6063. (goto-char beg)
  6064. (while (re-search-forward org-drawer-regexp end t)
  6065. (org-flag-drawer t))))))
  6066. (defun org-flag-drawer (flag)
  6067. (save-excursion
  6068. (beginning-of-line 1)
  6069. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6070. (let ((b (match-end 0)))
  6071. (if (re-search-forward
  6072. "^[ \t]*:END:"
  6073. (save-excursion (outline-next-heading) (point)) t)
  6074. (outline-flag-region b (point-at-eol) flag)
  6075. (error ":END: line missing"))))))
  6076. (defun org-cycle-hide-archived-subtrees (state)
  6077. "Re-hide all archived subtrees after a visibility state change."
  6078. (when (and (not org-cycle-open-archived-trees)
  6079. (not (memq state '(overview folded))))
  6080. (save-excursion
  6081. (let* ((globalp (memq state '(contents all)))
  6082. (beg (if globalp (point-min) (point)))
  6083. (end (if globalp (point-max) (org-end-of-subtree t))))
  6084. (org-hide-archived-subtrees beg end)
  6085. (goto-char beg)
  6086. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6087. (message (substitute-command-keys
  6088. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6089. (defun org-force-cycle-archived ()
  6090. "Cycle subtree even if it is archived."
  6091. (interactive)
  6092. (setq this-command 'org-cycle)
  6093. (let ((org-cycle-open-archived-trees t))
  6094. (call-interactively 'org-cycle)))
  6095. (defun org-hide-archived-subtrees (beg end)
  6096. "Re-hide all archived subtrees after a visibility state change."
  6097. (save-excursion
  6098. (let* ((re (concat ":" org-archive-tag ":")))
  6099. (goto-char beg)
  6100. (while (re-search-forward re end t)
  6101. (and (org-on-heading-p) (hide-subtree))
  6102. (org-end-of-subtree t)))))
  6103. (defun org-toggle-tag (tag &optional onoff)
  6104. "Toggle the tag TAG for the current line.
  6105. If ONOFF is `on' or `off', don't toggle but set to this state."
  6106. (unless (org-on-heading-p t) (error "Not on headling"))
  6107. (let (res current)
  6108. (save-excursion
  6109. (beginning-of-line)
  6110. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6111. (point-at-eol) t)
  6112. (progn
  6113. (setq current (match-string 1))
  6114. (replace-match ""))
  6115. (setq current ""))
  6116. (setq current (nreverse (org-split-string current ":")))
  6117. (cond
  6118. ((eq onoff 'on)
  6119. (setq res t)
  6120. (or (member tag current) (push tag current)))
  6121. ((eq onoff 'off)
  6122. (or (not (member tag current)) (setq current (delete tag current))))
  6123. (t (if (member tag current)
  6124. (setq current (delete tag current))
  6125. (setq res t)
  6126. (push tag current))))
  6127. (end-of-line 1)
  6128. (when current
  6129. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  6130. (org-set-tags nil t))
  6131. res))
  6132. (defun org-toggle-archive-tag (&optional arg)
  6133. "Toggle the archive tag for the current headline.
  6134. With prefix ARG, check all children of current headline and offer tagging
  6135. the children that do not contain any open TODO items."
  6136. (interactive "P")
  6137. (if arg
  6138. (org-archive-all-done 'tag)
  6139. (let (set)
  6140. (save-excursion
  6141. (org-back-to-heading t)
  6142. (setq set (org-toggle-tag org-archive-tag))
  6143. (when set (hide-subtree)))
  6144. (and set (beginning-of-line 1))
  6145. (message "Subtree %s" (if set "archived" "unarchived")))))
  6146. ;;;; Tables
  6147. ;;; The table editor
  6148. ;; Watch out: Here we are talking about two different kind of tables.
  6149. ;; Most of the code is for the tables created with the Org-mode table editor.
  6150. ;; Sometimes, we talk about tables created and edited with the table.el
  6151. ;; Emacs package. We call the former org-type tables, and the latter
  6152. ;; table.el-type tables.
  6153. (defun org-before-change-function (beg end)
  6154. "Every change indicates that a table might need an update."
  6155. (setq org-table-may-need-update t))
  6156. (defconst org-table-line-regexp "^[ \t]*|"
  6157. "Detects an org-type table line.")
  6158. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  6159. "Detects an org-type table line.")
  6160. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  6161. "Detects a table line marked for automatic recalculation.")
  6162. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  6163. "Detects a table line marked for automatic recalculation.")
  6164. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  6165. "Detects a table line marked for automatic recalculation.")
  6166. (defconst org-table-hline-regexp "^[ \t]*|-"
  6167. "Detects an org-type table hline.")
  6168. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  6169. "Detects a table-type table hline.")
  6170. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  6171. "Detects an org-type or table-type table.")
  6172. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  6173. "Searching from within a table (any type) this finds the first line
  6174. outside the table.")
  6175. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  6176. "Searching from within a table (any type) this finds the first line
  6177. outside the table.")
  6178. (defvar org-table-last-highlighted-reference nil)
  6179. (defvar org-table-formula-history nil)
  6180. (defvar org-table-column-names nil
  6181. "Alist with column names, derived from the `!' line.")
  6182. (defvar org-table-column-name-regexp nil
  6183. "Regular expression matching the current column names.")
  6184. (defvar org-table-local-parameters nil
  6185. "Alist with parameter names, derived from the `$' line.")
  6186. (defvar org-table-named-field-locations nil
  6187. "Alist with locations of named fields.")
  6188. (defvar org-table-current-line-types nil
  6189. "Table row types, non-nil only for the duration of a comand.")
  6190. (defvar org-table-current-begin-line nil
  6191. "Table begin line, non-nil only for the duration of a comand.")
  6192. (defvar org-table-current-begin-pos nil
  6193. "Table begin position, non-nil only for the duration of a comand.")
  6194. (defvar org-table-dlines nil
  6195. "Vector of data line line numbers in the current table.")
  6196. (defvar org-table-hlines nil
  6197. "Vector of hline line numbers in the current table.")
  6198. (defconst org-table-range-regexp
  6199. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6200. ;; 1 2 3 4 5
  6201. "Regular expression for matching ranges in formulas.")
  6202. (defconst org-table-range-regexp2
  6203. (concat
  6204. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6205. "\\.\\."
  6206. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6207. "Match a range for reference display.")
  6208. (defconst org-table-translate-regexp
  6209. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6210. "Match a reference that needs translation, for reference display.")
  6211. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6212. (defun org-table-create-with-table.el ()
  6213. "Use the table.el package to insert a new table.
  6214. If there is already a table at point, convert between Org-mode tables
  6215. and table.el tables."
  6216. (interactive)
  6217. (require 'table)
  6218. (cond
  6219. ((org-at-table.el-p)
  6220. (if (y-or-n-p "Convert table to Org-mode table? ")
  6221. (org-table-convert)))
  6222. ((org-at-table-p)
  6223. (if (y-or-n-p "Convert table to table.el table? ")
  6224. (org-table-convert)))
  6225. (t (call-interactively 'table-insert))))
  6226. (defun org-table-create-or-convert-from-region (arg)
  6227. "Convert region to table, or create an empty table.
  6228. If there is an active region, convert it to a table, using the function
  6229. `org-table-convert-region'.
  6230. If there is no such region, create an empty table with `org-table-create'."
  6231. (interactive "P")
  6232. (if (org-region-active-p)
  6233. (org-table-convert-region (region-beginning) (region-end) arg)
  6234. (org-table-create arg)))
  6235. (defun org-table-create (&optional size)
  6236. "Query for a size and insert a table skeleton.
  6237. SIZE is a string Columns x Rows like for example \"3x2\"."
  6238. (interactive "P")
  6239. (unless size
  6240. (setq size (read-string
  6241. (concat "Table size Columns x Rows [e.g. "
  6242. org-table-default-size "]: ")
  6243. "" nil org-table-default-size)))
  6244. (let* ((pos (point))
  6245. (indent (make-string (current-column) ?\ ))
  6246. (split (org-split-string size " *x *"))
  6247. (rows (string-to-number (nth 1 split)))
  6248. (columns (string-to-number (car split)))
  6249. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6250. "\n")))
  6251. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6252. (point-at-bol) (point)))
  6253. (beginning-of-line 1)
  6254. (newline))
  6255. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6256. (dotimes (i rows) (insert line))
  6257. (goto-char pos)
  6258. (if (> rows 1)
  6259. ;; Insert a hline after the first row.
  6260. (progn
  6261. (end-of-line 1)
  6262. (insert "\n|-")
  6263. (goto-char pos)))
  6264. (org-table-align)))
  6265. (defun org-table-convert-region (beg0 end0 &optional nspace)
  6266. "Convert region to a table.
  6267. The region goes from BEG0 to END0, but these borders will be moved
  6268. slightly, to make sure a beginning of line in the first line is included.
  6269. When NSPACE is non-nil, it indicates the minimum number of spaces that
  6270. separate columns. By default, the function first checks if every line
  6271. contains at lease one TAB. If yes, it assumes that the material is TAB
  6272. separated. If not, it assumes a single space as separator."
  6273. (interactive "rP")
  6274. (let* ((beg (min beg0 end0))
  6275. (end (max beg0 end0))
  6276. (tabsep t)
  6277. re)
  6278. (goto-char beg)
  6279. (beginning-of-line 1)
  6280. (setq beg (move-marker (make-marker) (point)))
  6281. (goto-char end)
  6282. (if (bolp) (backward-char 1) (end-of-line 1))
  6283. (setq end (move-marker (make-marker) (point)))
  6284. ;; Lets see if this is tab-separated material. If every nonempty line
  6285. ;; contains a tab, we will assume that it is tab-separated material
  6286. (if nspace
  6287. (setq tabsep nil)
  6288. (goto-char beg)
  6289. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  6290. (if nspace (setq tabsep nil))
  6291. (if tabsep
  6292. (setq re "^\\|\t")
  6293. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  6294. (max 1 (prefix-numeric-value nspace)))))
  6295. (goto-char beg)
  6296. (while (re-search-forward re end t)
  6297. (replace-match "| " t t))
  6298. (goto-char beg)
  6299. (insert " ")
  6300. (org-table-align)))
  6301. (defun org-table-import (file arg)
  6302. "Import FILE as a table.
  6303. The file is assumed to be tab-separated. Such files can be produced by most
  6304. spreadsheet and database applications. If no tabs (at least one per line)
  6305. are found, lines will be split on whitespace into fields."
  6306. (interactive "f\nP")
  6307. (or (bolp) (newline))
  6308. (let ((beg (point))
  6309. (pm (point-max)))
  6310. (insert-file-contents file)
  6311. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6312. (defun org-table-export ()
  6313. "Export table as a tab-separated file.
  6314. Such a file can be imported into a spreadsheet program like Excel."
  6315. (interactive)
  6316. (let* ((beg (org-table-begin))
  6317. (end (org-table-end))
  6318. (table (buffer-substring beg end))
  6319. (file (read-file-name "Export table to: "))
  6320. buf)
  6321. (unless (or (not (file-exists-p file))
  6322. (y-or-n-p (format "Overwrite file %s? " file)))
  6323. (error "Abort"))
  6324. (with-current-buffer (find-file-noselect file)
  6325. (setq buf (current-buffer))
  6326. (erase-buffer)
  6327. (fundamental-mode)
  6328. (insert table)
  6329. (goto-char (point-min))
  6330. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6331. (replace-match "" t t)
  6332. (end-of-line 1))
  6333. (goto-char (point-min))
  6334. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6335. (replace-match "" t t)
  6336. (goto-char (min (1+ (point)) (point-max))))
  6337. (goto-char (point-min))
  6338. (while (re-search-forward "^-[-+]*$" nil t)
  6339. (replace-match "")
  6340. (if (looking-at "\n")
  6341. (delete-char 1)))
  6342. (goto-char (point-min))
  6343. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6344. (replace-match "\t" t t))
  6345. (save-buffer))
  6346. (kill-buffer buf)))
  6347. (defvar org-table-aligned-begin-marker (make-marker)
  6348. "Marker at the beginning of the table last aligned.
  6349. Used to check if cursor still is in that table, to minimize realignment.")
  6350. (defvar org-table-aligned-end-marker (make-marker)
  6351. "Marker at the end of the table last aligned.
  6352. Used to check if cursor still is in that table, to minimize realignment.")
  6353. (defvar org-table-last-alignment nil
  6354. "List of flags for flushright alignment, from the last re-alignment.
  6355. This is being used to correctly align a single field after TAB or RET.")
  6356. (defvar org-table-last-column-widths nil
  6357. "List of max width of fields in each column.
  6358. This is being used to correctly align a single field after TAB or RET.")
  6359. (defvar org-table-overlay-coordinates nil
  6360. "Overlay coordinates after each align of a table.")
  6361. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6362. (defvar org-last-recalc-line nil)
  6363. (defconst org-narrow-column-arrow "=>"
  6364. "Used as display property in narrowed table columns.")
  6365. (defun org-table-align ()
  6366. "Align the table at point by aligning all vertical bars."
  6367. (interactive)
  6368. (let* (
  6369. ;; Limits of table
  6370. (beg (org-table-begin))
  6371. (end (org-table-end))
  6372. ;; Current cursor position
  6373. (linepos (org-current-line))
  6374. (colpos (org-table-current-column))
  6375. (winstart (window-start))
  6376. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6377. lines (new "") lengths l typenums ty fields maxfields i
  6378. column
  6379. (indent "") cnt frac
  6380. rfmt hfmt
  6381. (spaces '(1 . 1))
  6382. (sp1 (car spaces))
  6383. (sp2 (cdr spaces))
  6384. (rfmt1 (concat
  6385. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6386. (hfmt1 (concat
  6387. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6388. emptystrings links dates narrow fmax f1 len c e)
  6389. (untabify beg end)
  6390. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6391. ;; Check if we have links or dates
  6392. (goto-char beg)
  6393. (setq links (re-search-forward org-bracket-link-regexp end t))
  6394. (goto-char beg)
  6395. (setq dates (and org-display-custom-times
  6396. (re-search-forward org-ts-regexp-both end t)))
  6397. ;; Make sure the link properties are right
  6398. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6399. ;; Make sure the date properties are right
  6400. (when dates (goto-char beg) (while (org-activate-dates end)))
  6401. ;; Check if we are narrowing any columns
  6402. (goto-char beg)
  6403. (setq narrow (and org-format-transports-properties-p
  6404. (re-search-forward "<[0-9]+>" end t)))
  6405. ;; Get the rows
  6406. (setq lines (org-split-string
  6407. (buffer-substring beg end) "\n"))
  6408. ;; Store the indentation of the first line
  6409. (if (string-match "^ *" (car lines))
  6410. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6411. ;; Mark the hlines by setting the corresponding element to nil
  6412. ;; At the same time, we remove trailing space.
  6413. (setq lines (mapcar (lambda (l)
  6414. (if (string-match "^ *|-" l)
  6415. nil
  6416. (if (string-match "[ \t]+$" l)
  6417. (substring l 0 (match-beginning 0))
  6418. l)))
  6419. lines))
  6420. ;; Get the data fields by splitting the lines.
  6421. (setq fields (mapcar
  6422. (lambda (l)
  6423. (org-split-string l " *| *"))
  6424. (delq nil (copy-sequence lines))))
  6425. ;; How many fields in the longest line?
  6426. (condition-case nil
  6427. (setq maxfields (apply 'max (mapcar 'length fields)))
  6428. (error
  6429. (kill-region beg end)
  6430. (org-table-create org-table-default-size)
  6431. (error "Empty table - created default table")))
  6432. ;; A list of empty strings to fill any short rows on output
  6433. (setq emptystrings (make-list maxfields ""))
  6434. ;; Check for special formatting.
  6435. (setq i -1)
  6436. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  6437. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  6438. ;; Check if there is an explicit width specified
  6439. (when narrow
  6440. (setq c column fmax nil)
  6441. (while c
  6442. (setq e (pop c))
  6443. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  6444. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  6445. ;; Find fields that are wider than fmax, and shorten them
  6446. (when fmax
  6447. (loop for xx in column do
  6448. (when (and (stringp xx)
  6449. (> (org-string-width xx) fmax))
  6450. (org-add-props xx nil
  6451. 'help-echo
  6452. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  6453. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  6454. (unless (> f1 1)
  6455. (error "Cannot narrow field starting with wide link \"%s\""
  6456. (match-string 0 xx)))
  6457. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  6458. (add-text-properties (- f1 2) f1
  6459. (list 'display org-narrow-column-arrow)
  6460. xx)))))
  6461. ;; Get the maximum width for each column
  6462. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  6463. ;; Get the fraction of numbers, to decide about alignment of the column
  6464. (setq cnt 0 frac 0.0)
  6465. (loop for x in column do
  6466. (if (equal x "")
  6467. nil
  6468. (setq frac ( / (+ (* frac cnt)
  6469. (if (string-match org-table-number-regexp x) 1 0))
  6470. (setq cnt (1+ cnt))))))
  6471. (push (>= frac org-table-number-fraction) typenums))
  6472. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  6473. ;; Store the alignment of this table, for later editing of single fields
  6474. (setq org-table-last-alignment typenums
  6475. org-table-last-column-widths lengths)
  6476. ;; With invisible characters, `format' does not get the field width right
  6477. ;; So we need to make these fields wide by hand.
  6478. (when links
  6479. (loop for i from 0 upto (1- maxfields) do
  6480. (setq len (nth i lengths))
  6481. (loop for j from 0 upto (1- (length fields)) do
  6482. (setq c (nthcdr i (car (nthcdr j fields))))
  6483. (if (and (stringp (car c))
  6484. (string-match org-bracket-link-regexp (car c))
  6485. (< (org-string-width (car c)) len))
  6486. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  6487. ;; Compute the formats needed for output of the table
  6488. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  6489. (while (setq l (pop lengths))
  6490. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  6491. (setq rfmt (concat rfmt (format rfmt1 ty l))
  6492. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  6493. (setq rfmt (concat rfmt "\n")
  6494. hfmt (concat (substring hfmt 0 -1) "|\n"))
  6495. (setq new (mapconcat
  6496. (lambda (l)
  6497. (if l (apply 'format rfmt
  6498. (append (pop fields) emptystrings))
  6499. hfmt))
  6500. lines ""))
  6501. ;; Replace the old one
  6502. (delete-region beg end)
  6503. (move-marker end nil)
  6504. (move-marker org-table-aligned-begin-marker (point))
  6505. (insert new)
  6506. (move-marker org-table-aligned-end-marker (point))
  6507. (when (and orgtbl-mode (not (org-mode-p)))
  6508. (goto-char org-table-aligned-begin-marker)
  6509. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  6510. ;; Try to move to the old location
  6511. (goto-line winstartline)
  6512. (setq winstart (point-at-bol))
  6513. (goto-line linepos)
  6514. (set-window-start (selected-window) winstart 'noforce)
  6515. (org-table-goto-column colpos)
  6516. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  6517. (setq org-table-may-need-update nil)
  6518. ))
  6519. (defun org-string-width (s)
  6520. "Compute width of string, ignoring invisible characters.
  6521. This ignores character with invisibility property `org-link', and also
  6522. characters with property `org-cwidth', because these will become invisible
  6523. upon the next fontification round."
  6524. (let (b l)
  6525. (when (or (eq t buffer-invisibility-spec)
  6526. (assq 'org-link buffer-invisibility-spec))
  6527. (while (setq b (text-property-any 0 (length s)
  6528. 'invisible 'org-link s))
  6529. (setq s (concat (substring s 0 b)
  6530. (substring s (or (next-single-property-change
  6531. b 'invisible s) (length s)))))))
  6532. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6533. (setq s (concat (substring s 0 b)
  6534. (substring s (or (next-single-property-change
  6535. b 'org-cwidth s) (length s))))))
  6536. (setq l (string-width s) b -1)
  6537. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6538. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6539. l))
  6540. (defun org-table-begin (&optional table-type)
  6541. "Find the beginning of the table and return its position.
  6542. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6543. (save-excursion
  6544. (if (not (re-search-backward
  6545. (if table-type org-table-any-border-regexp
  6546. org-table-border-regexp)
  6547. nil t))
  6548. (progn (goto-char (point-min)) (point))
  6549. (goto-char (match-beginning 0))
  6550. (beginning-of-line 2)
  6551. (point))))
  6552. (defun org-table-end (&optional table-type)
  6553. "Find the end of the table and return its position.
  6554. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6555. (save-excursion
  6556. (if (not (re-search-forward
  6557. (if table-type org-table-any-border-regexp
  6558. org-table-border-regexp)
  6559. nil t))
  6560. (goto-char (point-max))
  6561. (goto-char (match-beginning 0)))
  6562. (point-marker)))
  6563. (defun org-table-justify-field-maybe (&optional new)
  6564. "Justify the current field, text to left, number to right.
  6565. Optional argument NEW may specify text to replace the current field content."
  6566. (cond
  6567. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6568. ((org-at-table-hline-p))
  6569. ((and (not new)
  6570. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6571. (current-buffer)))
  6572. (< (point) org-table-aligned-begin-marker)
  6573. (>= (point) org-table-aligned-end-marker)))
  6574. ;; This is not the same table, force a full re-align
  6575. (setq org-table-may-need-update t))
  6576. (t ;; realign the current field, based on previous full realign
  6577. (let* ((pos (point)) s
  6578. (col (org-table-current-column))
  6579. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6580. l f n o e)
  6581. (when (> col 0)
  6582. (skip-chars-backward "^|\n")
  6583. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6584. (progn
  6585. (setq s (match-string 1)
  6586. o (match-string 0)
  6587. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6588. e (not (= (match-beginning 2) (match-end 2))))
  6589. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6590. l (if e "|" (setq org-table-may-need-update t) ""))
  6591. n (format f s))
  6592. (if new
  6593. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6594. (setq n (format f new))
  6595. (setq n (concat new "|") org-table-may-need-update t)))
  6596. (or (equal n o)
  6597. (let (org-table-may-need-update)
  6598. (replace-match n t t))))
  6599. (setq org-table-may-need-update t))
  6600. (goto-char pos))))))
  6601. (defun org-table-next-field ()
  6602. "Go to the next field in the current table, creating new lines as needed.
  6603. Before doing so, re-align the table if necessary."
  6604. (interactive)
  6605. (org-table-maybe-eval-formula)
  6606. (org-table-maybe-recalculate-line)
  6607. (if (and org-table-automatic-realign
  6608. org-table-may-need-update)
  6609. (org-table-align))
  6610. (let ((end (org-table-end)))
  6611. (if (org-at-table-hline-p)
  6612. (end-of-line 1))
  6613. (condition-case nil
  6614. (progn
  6615. (re-search-forward "|" end)
  6616. (if (looking-at "[ \t]*$")
  6617. (re-search-forward "|" end))
  6618. (if (and (looking-at "-")
  6619. org-table-tab-jumps-over-hlines
  6620. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  6621. (goto-char (match-beginning 1)))
  6622. (if (looking-at "-")
  6623. (progn
  6624. (beginning-of-line 0)
  6625. (org-table-insert-row 'below))
  6626. (if (looking-at " ") (forward-char 1))))
  6627. (error
  6628. (org-table-insert-row 'below)))))
  6629. (defun org-table-previous-field ()
  6630. "Go to the previous field in the table.
  6631. Before doing so, re-align the table if necessary."
  6632. (interactive)
  6633. (org-table-justify-field-maybe)
  6634. (org-table-maybe-recalculate-line)
  6635. (if (and org-table-automatic-realign
  6636. org-table-may-need-update)
  6637. (org-table-align))
  6638. (if (org-at-table-hline-p)
  6639. (end-of-line 1))
  6640. (re-search-backward "|" (org-table-begin))
  6641. (re-search-backward "|" (org-table-begin))
  6642. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  6643. (re-search-backward "|" (org-table-begin)))
  6644. (if (looking-at "| ?")
  6645. (goto-char (match-end 0))))
  6646. (defun org-table-next-row ()
  6647. "Go to the next row (same column) in the current table.
  6648. Before doing so, re-align the table if necessary."
  6649. (interactive)
  6650. (org-table-maybe-eval-formula)
  6651. (org-table-maybe-recalculate-line)
  6652. (if (or (looking-at "[ \t]*$")
  6653. (save-excursion (skip-chars-backward " \t") (bolp)))
  6654. (newline)
  6655. (if (and org-table-automatic-realign
  6656. org-table-may-need-update)
  6657. (org-table-align))
  6658. (let ((col (org-table-current-column)))
  6659. (beginning-of-line 2)
  6660. (if (or (not (org-at-table-p))
  6661. (org-at-table-hline-p))
  6662. (progn
  6663. (beginning-of-line 0)
  6664. (org-table-insert-row 'below)))
  6665. (org-table-goto-column col)
  6666. (skip-chars-backward "^|\n\r")
  6667. (if (looking-at " ") (forward-char 1)))))
  6668. (defun org-table-copy-down (n)
  6669. "Copy a field down in the current column.
  6670. If the field at the cursor is empty, copy into it the content of the nearest
  6671. non-empty field above. With argument N, use the Nth non-empty field.
  6672. If the current field is not empty, it is copied down to the next row, and
  6673. the cursor is moved with it. Therefore, repeating this command causes the
  6674. column to be filled row-by-row.
  6675. If the variable `org-table-copy-increment' is non-nil and the field is an
  6676. integer or a timestamp, it will be incremented while copying. In the case of
  6677. a timestamp, if the cursor is on the year, change the year. If it is on the
  6678. month or the day, change that. Point will stay on the current date field
  6679. in order to easily repeat the interval."
  6680. (interactive "p")
  6681. (let* ((colpos (org-table-current-column))
  6682. (col (current-column))
  6683. (field (org-table-get-field))
  6684. (non-empty (string-match "[^ \t]" field))
  6685. (beg (org-table-begin))
  6686. txt)
  6687. (org-table-check-inside-data-field)
  6688. (if non-empty
  6689. (progn
  6690. (setq txt (org-trim field))
  6691. (org-table-next-row)
  6692. (org-table-blank-field))
  6693. (save-excursion
  6694. (setq txt
  6695. (catch 'exit
  6696. (while (progn (beginning-of-line 1)
  6697. (re-search-backward org-table-dataline-regexp
  6698. beg t))
  6699. (org-table-goto-column colpos t)
  6700. (if (and (looking-at
  6701. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  6702. (= (setq n (1- n)) 0))
  6703. (throw 'exit (match-string 1))))))))
  6704. (if txt
  6705. (progn
  6706. (if (and org-table-copy-increment
  6707. (string-match "^[0-9]+$" txt))
  6708. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  6709. (insert txt)
  6710. (move-to-column col)
  6711. (if (and org-table-copy-increment (org-at-timestamp-p t))
  6712. (org-timestamp-up 1)
  6713. (org-table-maybe-recalculate-line))
  6714. (org-table-align)
  6715. (move-to-column col))
  6716. (error "No non-empty field found"))))
  6717. (defun org-table-check-inside-data-field ()
  6718. "Is point inside a table data field?
  6719. I.e. not on a hline or before the first or after the last column?
  6720. This actually throws an error, so it aborts the current command."
  6721. (if (or (not (org-at-table-p))
  6722. (= (org-table-current-column) 0)
  6723. (org-at-table-hline-p)
  6724. (looking-at "[ \t]*$"))
  6725. (error "Not in table data field")))
  6726. (defvar org-table-clip nil
  6727. "Clipboard for table regions.")
  6728. (defun org-table-blank-field ()
  6729. "Blank the current table field or active region."
  6730. (interactive)
  6731. (org-table-check-inside-data-field)
  6732. (if (and (interactive-p) (org-region-active-p))
  6733. (let (org-table-clip)
  6734. (org-table-cut-region (region-beginning) (region-end)))
  6735. (skip-chars-backward "^|")
  6736. (backward-char 1)
  6737. (if (looking-at "|[^|\n]+")
  6738. (let* ((pos (match-beginning 0))
  6739. (match (match-string 0))
  6740. (len (org-string-width match)))
  6741. (replace-match (concat "|" (make-string (1- len) ?\ )))
  6742. (goto-char (+ 2 pos))
  6743. (substring match 1)))))
  6744. (defun org-table-get-field (&optional n replace)
  6745. "Return the value of the field in column N of current row.
  6746. N defaults to current field.
  6747. If REPLACE is a string, replace field with this value. The return value
  6748. is always the old value."
  6749. (and n (org-table-goto-column n))
  6750. (skip-chars-backward "^|\n")
  6751. (backward-char 1)
  6752. (if (looking-at "|[^|\r\n]*")
  6753. (let* ((pos (match-beginning 0))
  6754. (val (buffer-substring (1+ pos) (match-end 0))))
  6755. (if replace
  6756. (replace-match (concat "|" replace) t t))
  6757. (goto-char (min (point-at-eol) (+ 2 pos)))
  6758. val)
  6759. (forward-char 1) ""))
  6760. (defun org-table-field-info (arg)
  6761. "Show info about the current field, and highlight any reference at point."
  6762. (interactive "P")
  6763. (org-table-get-specials)
  6764. (save-excursion
  6765. (let* ((pos (point))
  6766. (col (org-table-current-column))
  6767. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  6768. (name (car (rassoc (list (org-current-line) col)
  6769. org-table-named-field-locations)))
  6770. (eql (org-table-get-stored-formulas))
  6771. (dline (org-table-current-dline))
  6772. (ref (format "@%d$%d" dline col))
  6773. (ref1 (org-table-convert-refs-to-an ref))
  6774. (fequation (or (assoc name eql) (assoc ref eql)))
  6775. (cequation (assoc (int-to-string col) eql))
  6776. (eqn (or fequation cequation)))
  6777. (goto-char pos)
  6778. (condition-case nil
  6779. (org-table-show-reference 'local)
  6780. (error nil))
  6781. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  6782. dline col
  6783. (if cname (concat " or $" cname) "")
  6784. dline col ref1
  6785. (if name (concat " or $" name) "")
  6786. ;; FIXME: formula info not correct if special table line
  6787. (if eqn
  6788. (concat ", formula: "
  6789. (org-table-formula-to-user
  6790. (concat
  6791. (if (string-match "^[$@]"(car eqn)) "" "$")
  6792. (car eqn) "=" (cdr eqn))))
  6793. "")))))
  6794. (defun org-table-current-column ()
  6795. "Find out which column we are in.
  6796. When called interactively, column is also displayed in echo area."
  6797. (interactive)
  6798. (if (interactive-p) (org-table-check-inside-data-field))
  6799. (save-excursion
  6800. (let ((cnt 0) (pos (point)))
  6801. (beginning-of-line 1)
  6802. (while (search-forward "|" pos t)
  6803. (setq cnt (1+ cnt)))
  6804. (if (interactive-p) (message "This is table column %d" cnt))
  6805. cnt)))
  6806. (defun org-table-current-dline ()
  6807. "Find out what table data line we are in.
  6808. Only datalins count for this."
  6809. (interactive)
  6810. (if (interactive-p) (org-table-check-inside-data-field))
  6811. (save-excursion
  6812. (let ((cnt 0) (pos (point)))
  6813. (goto-char (org-table-begin))
  6814. (while (<= (point) pos)
  6815. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  6816. (beginning-of-line 2))
  6817. (if (interactive-p) (message "This is table line %d" cnt))
  6818. cnt)))
  6819. (defun org-table-goto-column (n &optional on-delim force)
  6820. "Move the cursor to the Nth column in the current table line.
  6821. With optional argument ON-DELIM, stop with point before the left delimiter
  6822. of the field.
  6823. If there are less than N fields, just go to after the last delimiter.
  6824. However, when FORCE is non-nil, create new columns if necessary."
  6825. (interactive "p")
  6826. (let ((pos (point-at-eol)))
  6827. (beginning-of-line 1)
  6828. (when (> n 0)
  6829. (while (and (> (setq n (1- n)) -1)
  6830. (or (search-forward "|" pos t)
  6831. (and force
  6832. (progn (end-of-line 1)
  6833. (skip-chars-backward "^|")
  6834. (insert " | "))))))
  6835. ; (backward-char 2) t)))))
  6836. (when (and force (not (looking-at ".*|")))
  6837. (save-excursion (end-of-line 1) (insert " | ")))
  6838. (if on-delim
  6839. (backward-char 1)
  6840. (if (looking-at " ") (forward-char 1))))))
  6841. (defun org-at-table-p (&optional table-type)
  6842. "Return t if the cursor is inside an org-type table.
  6843. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  6844. (if org-enable-table-editor
  6845. (save-excursion
  6846. (beginning-of-line 1)
  6847. (looking-at (if table-type org-table-any-line-regexp
  6848. org-table-line-regexp)))
  6849. nil))
  6850. (defun org-at-table.el-p ()
  6851. "Return t if and only if we are at a table.el table."
  6852. (and (org-at-table-p 'any)
  6853. (save-excursion
  6854. (goto-char (org-table-begin 'any))
  6855. (looking-at org-table1-hline-regexp))))
  6856. (defun org-table-recognize-table.el ()
  6857. "If there is a table.el table nearby, recognize it and move into it."
  6858. (if org-table-tab-recognizes-table.el
  6859. (if (org-at-table.el-p)
  6860. (progn
  6861. (beginning-of-line 1)
  6862. (if (looking-at org-table-dataline-regexp)
  6863. nil
  6864. (if (looking-at org-table1-hline-regexp)
  6865. (progn
  6866. (beginning-of-line 2)
  6867. (if (looking-at org-table-any-border-regexp)
  6868. (beginning-of-line -1)))))
  6869. (if (re-search-forward "|" (org-table-end t) t)
  6870. (progn
  6871. (require 'table)
  6872. (if (table--at-cell-p (point))
  6873. t
  6874. (message "recognizing table.el table...")
  6875. (table-recognize-table)
  6876. (message "recognizing table.el table...done")))
  6877. (error "This should not happen..."))
  6878. t)
  6879. nil)
  6880. nil))
  6881. (defun org-at-table-hline-p ()
  6882. "Return t if the cursor is inside a hline in a table."
  6883. (if org-enable-table-editor
  6884. (save-excursion
  6885. (beginning-of-line 1)
  6886. (looking-at org-table-hline-regexp))
  6887. nil))
  6888. (defun org-table-insert-column ()
  6889. "Insert a new column into the table."
  6890. (interactive)
  6891. (if (not (org-at-table-p))
  6892. (error "Not at a table"))
  6893. (org-table-find-dataline)
  6894. (let* ((col (max 1 (org-table-current-column)))
  6895. (beg (org-table-begin))
  6896. (end (org-table-end))
  6897. ;; Current cursor position
  6898. (linepos (org-current-line))
  6899. (colpos col))
  6900. (goto-char beg)
  6901. (while (< (point) end)
  6902. (if (org-at-table-hline-p)
  6903. nil
  6904. (org-table-goto-column col t)
  6905. (insert "| "))
  6906. (beginning-of-line 2))
  6907. (move-marker end nil)
  6908. (goto-line linepos)
  6909. (org-table-goto-column colpos)
  6910. (org-table-align)
  6911. (org-table-fix-formulas "$" nil (1- col) 1)))
  6912. (defun org-table-find-dataline ()
  6913. "Find a dataline in the current table, which is needed for column commands."
  6914. (if (and (org-at-table-p)
  6915. (not (org-at-table-hline-p)))
  6916. t
  6917. (let ((col (current-column))
  6918. (end (org-table-end)))
  6919. (move-to-column col)
  6920. (while (and (< (point) end)
  6921. (or (not (= (current-column) col))
  6922. (org-at-table-hline-p)))
  6923. (beginning-of-line 2)
  6924. (move-to-column col))
  6925. (if (and (org-at-table-p)
  6926. (not (org-at-table-hline-p)))
  6927. t
  6928. (error
  6929. "Please position cursor in a data line for column operations")))))
  6930. (defun org-table-delete-column ()
  6931. "Delete a column from the table."
  6932. (interactive)
  6933. (if (not (org-at-table-p))
  6934. (error "Not at a table"))
  6935. (org-table-find-dataline)
  6936. (org-table-check-inside-data-field)
  6937. (let* ((col (org-table-current-column))
  6938. (beg (org-table-begin))
  6939. (end (org-table-end))
  6940. ;; Current cursor position
  6941. (linepos (org-current-line))
  6942. (colpos col))
  6943. (goto-char beg)
  6944. (while (< (point) end)
  6945. (if (org-at-table-hline-p)
  6946. nil
  6947. (org-table-goto-column col t)
  6948. (and (looking-at "|[^|\n]+|")
  6949. (replace-match "|")))
  6950. (beginning-of-line 2))
  6951. (move-marker end nil)
  6952. (goto-line linepos)
  6953. (org-table-goto-column colpos)
  6954. (org-table-align)
  6955. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  6956. col -1 col)))
  6957. (defun org-table-move-column-right ()
  6958. "Move column to the right."
  6959. (interactive)
  6960. (org-table-move-column nil))
  6961. (defun org-table-move-column-left ()
  6962. "Move column to the left."
  6963. (interactive)
  6964. (org-table-move-column 'left))
  6965. (defun org-table-move-column (&optional left)
  6966. "Move the current column to the right. With arg LEFT, move to the left."
  6967. (interactive "P")
  6968. (if (not (org-at-table-p))
  6969. (error "Not at a table"))
  6970. (org-table-find-dataline)
  6971. (org-table-check-inside-data-field)
  6972. (let* ((col (org-table-current-column))
  6973. (col1 (if left (1- col) col))
  6974. (beg (org-table-begin))
  6975. (end (org-table-end))
  6976. ;; Current cursor position
  6977. (linepos (org-current-line))
  6978. (colpos (if left (1- col) (1+ col))))
  6979. (if (and left (= col 1))
  6980. (error "Cannot move column further left"))
  6981. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  6982. (error "Cannot move column further right"))
  6983. (goto-char beg)
  6984. (while (< (point) end)
  6985. (if (org-at-table-hline-p)
  6986. nil
  6987. (org-table-goto-column col1 t)
  6988. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  6989. (replace-match "|\\2|\\1|")))
  6990. (beginning-of-line 2))
  6991. (move-marker end nil)
  6992. (goto-line linepos)
  6993. (org-table-goto-column colpos)
  6994. (org-table-align)
  6995. (org-table-fix-formulas
  6996. "$" (list (cons (number-to-string col) (number-to-string colpos))
  6997. (cons (number-to-string colpos) (number-to-string col))))))
  6998. (defun org-table-move-row-down ()
  6999. "Move table row down."
  7000. (interactive)
  7001. (org-table-move-row nil))
  7002. (defun org-table-move-row-up ()
  7003. "Move table row up."
  7004. (interactive)
  7005. (org-table-move-row 'up))
  7006. (defun org-table-move-row (&optional up)
  7007. "Move the current table line down. With arg UP, move it up."
  7008. (interactive "P")
  7009. (let* ((col (current-column))
  7010. (pos (point))
  7011. (hline1p (save-excursion (beginning-of-line 1)
  7012. (looking-at org-table-hline-regexp)))
  7013. (dline1 (org-table-current-dline))
  7014. (dline2 (+ dline1 (if up -1 1)))
  7015. (tonew (if up 0 2))
  7016. txt hline2p)
  7017. (beginning-of-line tonew)
  7018. (unless (org-at-table-p)
  7019. (goto-char pos)
  7020. (error "Cannot move row further"))
  7021. (setq hline2p (looking-at org-table-hline-regexp))
  7022. (goto-char pos)
  7023. (beginning-of-line 1)
  7024. (setq pos (point))
  7025. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  7026. (delete-region (point) (1+ (point-at-eol)))
  7027. (beginning-of-line tonew)
  7028. (insert txt)
  7029. (beginning-of-line 0)
  7030. (move-to-column col)
  7031. (unless (or hline1p hline2p)
  7032. (org-table-fix-formulas
  7033. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  7034. (cons (number-to-string dline2) (number-to-string dline1)))))))
  7035. (defun org-table-insert-row (&optional arg)
  7036. "Insert a new row above the current line into the table.
  7037. With prefix ARG, insert below the current line."
  7038. (interactive "P")
  7039. (if (not (org-at-table-p))
  7040. (error "Not at a table"))
  7041. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  7042. (new (org-table-clean-line line)))
  7043. ;; Fix the first field if necessary
  7044. (if (string-match "^[ \t]*| *[#$] *|" line)
  7045. (setq new (replace-match (match-string 0 line) t t new)))
  7046. (beginning-of-line (if arg 2 1))
  7047. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  7048. (beginning-of-line 0)
  7049. (re-search-forward "| ?" (point-at-eol) t)
  7050. (and (or org-table-may-need-update org-table-overlay-coordinates)
  7051. (org-table-align))
  7052. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  7053. (defun org-table-insert-hline (&optional above)
  7054. "Insert a horizontal-line below the current line into the table.
  7055. With prefix ABOVE, insert above the current line."
  7056. (interactive "P")
  7057. (if (not (org-at-table-p))
  7058. (error "Not at a table"))
  7059. (let ((line (org-table-clean-line
  7060. (buffer-substring (point-at-bol) (point-at-eol))))
  7061. (col (current-column)))
  7062. (while (string-match "|\\( +\\)|" line)
  7063. (setq line (replace-match
  7064. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  7065. ?-) "|") t t line)))
  7066. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  7067. (beginning-of-line (if above 1 2))
  7068. (insert line "\n")
  7069. (beginning-of-line (if above 1 -1))
  7070. (move-to-column col)
  7071. (and org-table-overlay-coordinates (org-table-align))))
  7072. (defun org-table-hline-and-move (&optional same-column)
  7073. "Insert a hline and move to the row below that line."
  7074. (interactive "P")
  7075. (let ((col (org-table-current-column)))
  7076. (org-table-maybe-eval-formula)
  7077. (org-table-maybe-recalculate-line)
  7078. (org-table-insert-hline)
  7079. (end-of-line 2)
  7080. (if (looking-at "\n[ \t]*|-")
  7081. (progn (insert "\n|") (org-table-align))
  7082. (org-table-next-field))
  7083. (if same-column (org-table-goto-column col))))
  7084. (defun org-table-clean-line (s)
  7085. "Convert a table line S into a string with only \"|\" and space.
  7086. In particular, this does handle wide and invisible characters."
  7087. (if (string-match "^[ \t]*|-" s)
  7088. ;; It's a hline, just map the characters
  7089. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  7090. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  7091. (setq s (replace-match
  7092. (concat "|" (make-string (org-string-width (match-string 1 s))
  7093. ?\ ) "|")
  7094. t t s)))
  7095. s))
  7096. (defun org-table-kill-row ()
  7097. "Delete the current row or horizontal line from the table."
  7098. (interactive)
  7099. (if (not (org-at-table-p))
  7100. (error "Not at a table"))
  7101. (let ((col (current-column))
  7102. (dline (org-table-current-dline)))
  7103. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  7104. (if (not (org-at-table-p)) (beginning-of-line 0))
  7105. (move-to-column col)
  7106. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  7107. dline -1 dline)))
  7108. (defun org-table-sort-lines (with-case &optional sorting-type)
  7109. "Sort table lines according to the column at point.
  7110. The position of point indicates the column to be used for
  7111. sorting, and the range of lines is the range between the nearest
  7112. horizontal separator lines, or the entire table of no such lines
  7113. exist. If point is before the first column, you will be prompted
  7114. for the sorting column. If there is an active region, the mark
  7115. specifies the first line and the sorting column, while point
  7116. should be in the last line to be included into the sorting.
  7117. The command then prompts for the sorting type which can be
  7118. alphabetically, numerically, or by time (as given in a time stamp
  7119. in the field). Sorting in reverse order is also possible.
  7120. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  7121. If SORTING-TYPE is specified when this function is called from a Lisp
  7122. program, no prompting will take place. SORTING-TYPE must be a character,
  7123. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  7124. should be done in reverse order."
  7125. (interactive "P")
  7126. (let* ((thisline (org-current-line))
  7127. (thiscol (org-table-current-column))
  7128. beg end bcol ecol tend tbeg column lns pos)
  7129. (when (equal thiscol 0)
  7130. (if (interactive-p)
  7131. (setq thiscol
  7132. (string-to-number
  7133. (read-string "Use column N for sorting: ")))
  7134. (setq thiscol 1))
  7135. (org-table-goto-column thiscol))
  7136. (org-table-check-inside-data-field)
  7137. (if (org-region-active-p)
  7138. (progn
  7139. (setq beg (region-beginning) end (region-end))
  7140. (goto-char beg)
  7141. (setq column (org-table-current-column)
  7142. beg (point-at-bol))
  7143. (goto-char end)
  7144. (setq end (point-at-bol 2)))
  7145. (setq column (org-table-current-column)
  7146. pos (point)
  7147. tbeg (org-table-begin)
  7148. tend (org-table-end))
  7149. (if (re-search-backward org-table-hline-regexp tbeg t)
  7150. (setq beg (point-at-bol 2))
  7151. (goto-char tbeg)
  7152. (setq beg (point-at-bol 1)))
  7153. (goto-char pos)
  7154. (if (re-search-forward org-table-hline-regexp tend t)
  7155. (setq end (point-at-bol 1))
  7156. (goto-char tend)
  7157. (setq end (point-at-bol))))
  7158. (setq beg (move-marker (make-marker) beg)
  7159. end (move-marker (make-marker) end))
  7160. (untabify beg end)
  7161. (goto-char beg)
  7162. (org-table-goto-column column)
  7163. (skip-chars-backward "^|")
  7164. (setq bcol (current-column))
  7165. (org-table-goto-column (1+ column))
  7166. (skip-chars-backward "^|")
  7167. (setq ecol (1- (current-column)))
  7168. (org-table-goto-column column)
  7169. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  7170. (org-split-string (buffer-substring beg end) "\n")))
  7171. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  7172. (delete-region beg end)
  7173. (move-marker beg nil)
  7174. (move-marker end nil)
  7175. (insert (mapconcat 'cdr lns "\n") "\n")
  7176. (goto-line thisline)
  7177. (org-table-goto-column thiscol)
  7178. (message "%d lines sorted, based on column %d" (length lns) column)))
  7179. (defun org-table-cut-region (beg end)
  7180. "Copy region in table to the clipboard and blank all relevant fields."
  7181. (interactive "r")
  7182. (org-table-copy-region beg end 'cut))
  7183. (defun org-table-copy-region (beg end &optional cut)
  7184. "Copy rectangular region in table to clipboard.
  7185. A special clipboard is used which can only be accessed
  7186. with `org-table-paste-rectangle'."
  7187. (interactive "rP")
  7188. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7189. region cols
  7190. (rpl (if cut " " nil)))
  7191. (goto-char beg)
  7192. (org-table-check-inside-data-field)
  7193. (setq l01 (org-current-line)
  7194. c01 (org-table-current-column))
  7195. (goto-char end)
  7196. (org-table-check-inside-data-field)
  7197. (setq l02 (org-current-line)
  7198. c02 (org-table-current-column))
  7199. (setq l1 (min l01 l02) l2 (max l01 l02)
  7200. c1 (min c01 c02) c2 (max c01 c02))
  7201. (catch 'exit
  7202. (while t
  7203. (catch 'nextline
  7204. (if (> l1 l2) (throw 'exit t))
  7205. (goto-line l1)
  7206. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7207. (setq cols nil ic1 c1 ic2 c2)
  7208. (while (< ic1 (1+ ic2))
  7209. (push (org-table-get-field ic1 rpl) cols)
  7210. (setq ic1 (1+ ic1)))
  7211. (push (nreverse cols) region)
  7212. (setq l1 (1+ l1)))))
  7213. (setq org-table-clip (nreverse region))
  7214. (if cut (org-table-align))
  7215. org-table-clip))
  7216. (defun org-table-paste-rectangle ()
  7217. "Paste a rectangular region into a table.
  7218. The upper right corner ends up in the current field. All involved fields
  7219. will be overwritten. If the rectangle does not fit into the present table,
  7220. the table is enlarged as needed. The process ignores horizontal separator
  7221. lines."
  7222. (interactive)
  7223. (unless (and org-table-clip (listp org-table-clip))
  7224. (error "First cut/copy a region to paste!"))
  7225. (org-table-check-inside-data-field)
  7226. (let* ((clip org-table-clip)
  7227. (line (org-current-line))
  7228. (col (org-table-current-column))
  7229. (org-enable-table-editor t)
  7230. (org-table-automatic-realign nil)
  7231. c cols field)
  7232. (while (setq cols (pop clip))
  7233. (while (org-at-table-hline-p) (beginning-of-line 2))
  7234. (if (not (org-at-table-p))
  7235. (progn (end-of-line 0) (org-table-next-field)))
  7236. (setq c col)
  7237. (while (setq field (pop cols))
  7238. (org-table-goto-column c nil 'force)
  7239. (org-table-get-field nil field)
  7240. (setq c (1+ c)))
  7241. (beginning-of-line 2))
  7242. (goto-line line)
  7243. (org-table-goto-column col)
  7244. (org-table-align)))
  7245. (defun org-table-convert ()
  7246. "Convert from `org-mode' table to table.el and back.
  7247. Obviously, this only works within limits. When an Org-mode table is
  7248. converted to table.el, all horizontal separator lines get lost, because
  7249. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7250. A table.el table can be converted to an Org-mode table only if it does not
  7251. do row or column spanning. Multiline cells will become multiple cells.
  7252. Beware, Org-mode does not test if the table can be successfully converted - it
  7253. blindly applies a recipe that works for simple tables."
  7254. (interactive)
  7255. (require 'table)
  7256. (if (org-at-table.el-p)
  7257. ;; convert to Org-mode table
  7258. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7259. (end (move-marker (make-marker) (org-table-end t))))
  7260. (table-unrecognize-region beg end)
  7261. (goto-char beg)
  7262. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7263. (replace-match ""))
  7264. (goto-char beg))
  7265. (if (org-at-table-p)
  7266. ;; convert to table.el table
  7267. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7268. (end (move-marker (make-marker) (org-table-end))))
  7269. ;; first, get rid of all horizontal lines
  7270. (goto-char beg)
  7271. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7272. (replace-match ""))
  7273. ;; insert a hline before first
  7274. (goto-char beg)
  7275. (org-table-insert-hline 'above)
  7276. (beginning-of-line -1)
  7277. ;; insert a hline after each line
  7278. (while (progn (beginning-of-line 3) (< (point) end))
  7279. (org-table-insert-hline))
  7280. (goto-char beg)
  7281. (setq end (move-marker end (org-table-end)))
  7282. ;; replace "+" at beginning and ending of hlines
  7283. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7284. (replace-match "\\1+-"))
  7285. (goto-char beg)
  7286. (while (re-search-forward "-|[ \t]*$" end t)
  7287. (replace-match "-+"))
  7288. (goto-char beg)))))
  7289. (defun org-table-wrap-region (arg)
  7290. "Wrap several fields in a column like a paragraph.
  7291. This is useful if you'd like to spread the contents of a field over several
  7292. lines, in order to keep the table compact.
  7293. If there is an active region, and both point and mark are in the same column,
  7294. the text in the column is wrapped to minimum width for the given number of
  7295. lines. Generally, this makes the table more compact. A prefix ARG may be
  7296. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7297. formats the selected text to two lines. If the region was longer than two
  7298. lines, the remaining lines remain empty. A negative prefix argument reduces
  7299. the current number of lines by that amount. The wrapped text is pasted back
  7300. into the table. If you formatted it to more lines than it was before, fields
  7301. further down in the table get overwritten - so you might need to make space in
  7302. the table first.
  7303. If there is no region, the current field is split at the cursor position and
  7304. the text fragment to the right of the cursor is prepended to the field one
  7305. line down.
  7306. If there is no region, but you specify a prefix ARG, the current field gets
  7307. blank, and the content is appended to the field above."
  7308. (interactive "P")
  7309. (org-table-check-inside-data-field)
  7310. (if (org-region-active-p)
  7311. ;; There is a region: fill as a paragraph
  7312. (let* ((beg (region-beginning))
  7313. (cline (save-excursion (goto-char beg) (org-current-line)))
  7314. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7315. nlines)
  7316. (org-table-cut-region (region-beginning) (region-end))
  7317. (if (> (length (car org-table-clip)) 1)
  7318. (error "Region must be limited to single column"))
  7319. (setq nlines (if arg
  7320. (if (< arg 1)
  7321. (+ (length org-table-clip) arg)
  7322. arg)
  7323. (length org-table-clip)))
  7324. (setq org-table-clip
  7325. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7326. nil nlines)))
  7327. (goto-line cline)
  7328. (org-table-goto-column ccol)
  7329. (org-table-paste-rectangle))
  7330. ;; No region, split the current field at point
  7331. (if arg
  7332. ;; combine with field above
  7333. (let ((s (org-table-blank-field))
  7334. (col (org-table-current-column)))
  7335. (beginning-of-line 0)
  7336. (while (org-at-table-hline-p) (beginning-of-line 0))
  7337. (org-table-goto-column col)
  7338. (skip-chars-forward "^|")
  7339. (skip-chars-backward " ")
  7340. (insert " " (org-trim s))
  7341. (org-table-align))
  7342. ;; split field
  7343. (when (looking-at "\\([^|]+\\)+|")
  7344. (let ((s (match-string 1)))
  7345. (replace-match " |")
  7346. (goto-char (match-beginning 0))
  7347. (org-table-next-row)
  7348. (insert (org-trim s) " ")
  7349. (org-table-align))))))
  7350. (defvar org-field-marker nil)
  7351. (defun org-table-edit-field (arg)
  7352. "Edit table field in a different window.
  7353. This is mainly useful for fields that contain hidden parts.
  7354. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7355. it can be edited in place."
  7356. (interactive "P")
  7357. (if arg
  7358. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7359. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7360. (remove-text-properties b e '(org-cwidth t invisible t
  7361. display t intangible t))
  7362. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7363. (font-lock-fontify-block)))
  7364. (let ((pos (move-marker (make-marker) (point)))
  7365. (field (org-table-get-field))
  7366. (cw (current-window-configuration))
  7367. p)
  7368. (switch-to-buffer-other-window "*Org tmp*")
  7369. (erase-buffer)
  7370. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7371. (let ((org-inhibit-startup t)) (org-mode))
  7372. (goto-char (setq p (point-max)))
  7373. (insert (org-trim field))
  7374. (remove-text-properties p (point-max)
  7375. '(invisible t org-cwidth t display t
  7376. intangible t))
  7377. (goto-char p)
  7378. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7379. (org-set-local 'org-window-configuration cw)
  7380. (org-set-local 'org-field-marker pos)
  7381. (message "Edit and finish with C-c C-c"))))
  7382. (defun org-table-finish-edit-field ()
  7383. "Finish editing a table data field.
  7384. Remove all newline characters, insert the result into the table, realign
  7385. the table and kill the editing buffer."
  7386. (let ((pos org-field-marker)
  7387. (cw org-window-configuration)
  7388. (cb (current-buffer))
  7389. text)
  7390. (goto-char (point-min))
  7391. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7392. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7393. (replace-match " "))
  7394. (setq text (org-trim (buffer-string)))
  7395. (set-window-configuration cw)
  7396. (kill-buffer cb)
  7397. (select-window (get-buffer-window (marker-buffer pos)))
  7398. (goto-char pos)
  7399. (move-marker pos nil)
  7400. (org-table-check-inside-data-field)
  7401. (org-table-get-field nil text)
  7402. (org-table-align)
  7403. (message "New field value inserted")))
  7404. (defun org-trim (s)
  7405. "Remove whitespace at beginning and end of string."
  7406. (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7407. (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
  7408. s)
  7409. (defun org-wrap (string &optional width lines)
  7410. "Wrap string to either a number of lines, or a width in characters.
  7411. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7412. that costs. If there is a word longer than WIDTH, the text is actually
  7413. wrapped to the length of that word.
  7414. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7415. many lines, whatever width that takes.
  7416. The return value is a list of lines, without newlines at the end."
  7417. (let* ((words (org-split-string string "[ \t\n]+"))
  7418. (maxword (apply 'max (mapcar 'org-string-width words)))
  7419. w ll)
  7420. (cond (width
  7421. (org-do-wrap words (max maxword width)))
  7422. (lines
  7423. (setq w maxword)
  7424. (setq ll (org-do-wrap words maxword))
  7425. (if (<= (length ll) lines)
  7426. ll
  7427. (setq ll words)
  7428. (while (> (length ll) lines)
  7429. (setq w (1+ w))
  7430. (setq ll (org-do-wrap words w)))
  7431. ll))
  7432. (t (error "Cannot wrap this")))))
  7433. (defun org-do-wrap (words width)
  7434. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  7435. (let (lines line)
  7436. (while words
  7437. (setq line (pop words))
  7438. (while (and words (< (+ (length line) (length (car words))) width))
  7439. (setq line (concat line " " (pop words))))
  7440. (setq lines (push line lines)))
  7441. (nreverse lines)))
  7442. (defun org-split-string (string &optional separators)
  7443. "Splits STRING into substrings at SEPARATORS.
  7444. No empty strings are returned if there are matches at the beginning
  7445. and end of string."
  7446. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  7447. (start 0)
  7448. notfirst
  7449. (list nil))
  7450. (while (and (string-match rexp string
  7451. (if (and notfirst
  7452. (= start (match-beginning 0))
  7453. (< start (length string)))
  7454. (1+ start) start))
  7455. (< (match-beginning 0) (length string)))
  7456. (setq notfirst t)
  7457. (or (eq (match-beginning 0) 0)
  7458. (and (eq (match-beginning 0) (match-end 0))
  7459. (eq (match-beginning 0) start))
  7460. (setq list
  7461. (cons (substring string start (match-beginning 0))
  7462. list)))
  7463. (setq start (match-end 0)))
  7464. (or (eq start (length string))
  7465. (setq list
  7466. (cons (substring string start)
  7467. list)))
  7468. (nreverse list)))
  7469. (defun org-table-map-tables (function)
  7470. "Apply FUNCTION to the start of all tables in the buffer."
  7471. (save-excursion
  7472. (save-restriction
  7473. (widen)
  7474. (goto-char (point-min))
  7475. (while (re-search-forward org-table-any-line-regexp nil t)
  7476. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  7477. (beginning-of-line 1)
  7478. (if (looking-at org-table-line-regexp)
  7479. (save-excursion (funcall function)))
  7480. (re-search-forward org-table-any-border-regexp nil 1))))
  7481. (message "Mapping tables: done"))
  7482. (defvar org-timecnt) ; dynamically scoped parameter
  7483. (defun org-table-sum (&optional beg end nlast)
  7484. "Sum numbers in region of current table column.
  7485. The result will be displayed in the echo area, and will be available
  7486. as kill to be inserted with \\[yank].
  7487. If there is an active region, it is interpreted as a rectangle and all
  7488. numbers in that rectangle will be summed. If there is no active
  7489. region and point is located in a table column, sum all numbers in that
  7490. column.
  7491. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  7492. numbers are assumed to be times as well (in decimal hours) and the
  7493. numbers are added as such.
  7494. If NLAST is a number, only the NLAST fields will actually be summed."
  7495. (interactive)
  7496. (save-excursion
  7497. (let (col (org-timecnt 0) diff h m s org-table-clip)
  7498. (cond
  7499. ((and beg end)) ; beg and end given explicitly
  7500. ((org-region-active-p)
  7501. (setq beg (region-beginning) end (region-end)))
  7502. (t
  7503. (setq col (org-table-current-column))
  7504. (goto-char (org-table-begin))
  7505. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  7506. (error "No table data"))
  7507. (org-table-goto-column col)
  7508. (setq beg (point))
  7509. (goto-char (org-table-end))
  7510. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  7511. (error "No table data"))
  7512. (org-table-goto-column col)
  7513. (setq end (point))))
  7514. (let* ((items (apply 'append (org-table-copy-region beg end)))
  7515. (items1 (cond ((not nlast) items)
  7516. ((>= nlast (length items)) items)
  7517. (t (setq items (reverse items))
  7518. (setcdr (nthcdr (1- nlast) items) nil)
  7519. (nreverse items))))
  7520. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  7521. items1)))
  7522. (res (apply '+ numbers))
  7523. (sres (if (= org-timecnt 0)
  7524. (format "%g" res)
  7525. (setq diff (* 3600 res)
  7526. h (floor (/ diff 3600)) diff (mod diff 3600)
  7527. m (floor (/ diff 60)) diff (mod diff 60)
  7528. s diff)
  7529. (format "%d:%02d:%02d" h m s))))
  7530. (kill-new sres)
  7531. (if (interactive-p)
  7532. (message "%s"
  7533. (substitute-command-keys
  7534. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7535. (length numbers) sres))))
  7536. sres))))
  7537. (defun org-table-get-number-for-summing (s)
  7538. (let (n)
  7539. (if (string-match "^ *|? *" s)
  7540. (setq s (replace-match "" nil nil s)))
  7541. (if (string-match " *|? *$" s)
  7542. (setq s (replace-match "" nil nil s)))
  7543. (setq n (string-to-number s))
  7544. (cond
  7545. ((and (string-match "0" s)
  7546. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7547. ((string-match "\\`[ \t]+\\'" s) nil)
  7548. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7549. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7550. (m (string-to-number (or (match-string 2 s) "0")))
  7551. (s (string-to-number (or (match-string 4 s) "0"))))
  7552. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7553. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7554. ((equal n 0) nil)
  7555. (t n))))
  7556. (defun org-table-current-field-formula (&optional key noerror)
  7557. "Return the formula active for the current field.
  7558. Assumes that specials are in place.
  7559. If KEY is given, return the key to this formula.
  7560. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7561. (let* ((name (car (rassoc (list (org-current-line)
  7562. (org-table-current-column))
  7563. org-table-named-field-locations)))
  7564. (col (org-table-current-column))
  7565. (scol (int-to-string col))
  7566. (ref (format "@%d$%d" (org-table-current-dline) col))
  7567. (stored-list (org-table-get-stored-formulas noerror))
  7568. (ass (or (assoc name stored-list)
  7569. (assoc ref stored-list)
  7570. (assoc scol stored-list))))
  7571. (if key
  7572. (car ass)
  7573. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7574. (cdr ass))))))
  7575. (defun org-table-get-formula (&optional equation named)
  7576. "Read a formula from the minibuffer, offer stored formula as default.
  7577. When NAMED is non-nil, look for a named equation."
  7578. (let* ((stored-list (org-table-get-stored-formulas))
  7579. (name (car (rassoc (list (org-current-line)
  7580. (org-table-current-column))
  7581. org-table-named-field-locations)))
  7582. (ref (format "@%d$%d" (org-table-current-dline)
  7583. (org-table-current-column)))
  7584. (refass (assoc ref stored-list))
  7585. (scol (if named
  7586. (if name name ref)
  7587. (int-to-string (org-table-current-column))))
  7588. (dummy (and (or name refass) (not named)
  7589. (not (y-or-n-p "Replace field formula with column formula? " ))
  7590. (error "Abort")))
  7591. (name (or name ref))
  7592. (org-table-may-need-update nil)
  7593. (stored (cdr (assoc scol stored-list)))
  7594. (eq (cond
  7595. ((and stored equation (string-match "^ *=? *$" equation))
  7596. stored)
  7597. ((stringp equation)
  7598. equation)
  7599. (t (org-table-formula-from-user
  7600. (read-string
  7601. (org-table-formula-to-user
  7602. (format "%s formula %s%s="
  7603. (if named "Field" "Column")
  7604. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7605. scol))
  7606. (if stored (org-table-formula-to-user stored) "")
  7607. 'org-table-formula-history
  7608. )))))
  7609. mustsave)
  7610. (when (not (string-match "\\S-" eq))
  7611. ;; remove formula
  7612. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7613. (org-table-store-formulas stored-list)
  7614. (error "Formula removed"))
  7615. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7616. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7617. (if (and name (not named))
  7618. ;; We set the column equation, delete the named one.
  7619. (setq stored-list (delq (assoc name stored-list) stored-list)
  7620. mustsave t))
  7621. (if stored
  7622. (setcdr (assoc scol stored-list) eq)
  7623. (setq stored-list (cons (cons scol eq) stored-list)))
  7624. (if (or mustsave (not (equal stored eq)))
  7625. (org-table-store-formulas stored-list))
  7626. eq))
  7627. (defun org-table-store-formulas (alist)
  7628. "Store the list of formulas below the current table."
  7629. (setq alist (sort alist 'org-table-formula-less-p))
  7630. (save-excursion
  7631. (goto-char (org-table-end))
  7632. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  7633. (progn
  7634. ;; don't overwrite TBLFM, we might use text properties to store stuff
  7635. (goto-char (match-beginning 2))
  7636. (delete-region (match-beginning 2) (match-end 0)))
  7637. (insert "#+TBLFM:"))
  7638. (insert " "
  7639. (mapconcat (lambda (x)
  7640. (concat
  7641. (if (equal (string-to-char (car x)) ?@) "" "$")
  7642. (car x) "=" (cdr x)))
  7643. alist "::")
  7644. "\n")))
  7645. (defsubst org-table-formula-make-cmp-string (a)
  7646. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  7647. (concat
  7648. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  7649. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  7650. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  7651. (defun org-table-formula-less-p (a b)
  7652. "Compare two formulas for sorting."
  7653. (let ((as (org-table-formula-make-cmp-string (car a)))
  7654. (bs (org-table-formula-make-cmp-string (car b))))
  7655. (and as bs (string< as bs))))
  7656. (defun org-table-get-stored-formulas (&optional noerror)
  7657. "Return an alist with the stored formulas directly after current table."
  7658. (interactive)
  7659. (let (scol eq eq-alist strings string seen)
  7660. (save-excursion
  7661. (goto-char (org-table-end))
  7662. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  7663. (setq strings (org-split-string (match-string 2) " *:: *"))
  7664. (while (setq string (pop strings))
  7665. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  7666. (setq scol (if (match-end 2)
  7667. (match-string 2 string)
  7668. (match-string 1 string))
  7669. eq (match-string 3 string)
  7670. eq-alist (cons (cons scol eq) eq-alist))
  7671. (if (member scol seen)
  7672. (if noerror
  7673. (progn
  7674. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  7675. (ding)
  7676. (sit-for 2))
  7677. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  7678. (push scol seen))))))
  7679. (nreverse eq-alist)))
  7680. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  7681. "Modify the equations after the table structure has been edited.
  7682. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  7683. For all numbers larger than LIMIT, shift them by DELTA."
  7684. (save-excursion
  7685. (goto-char (org-table-end))
  7686. (when (looking-at "#\\+TBLFM:")
  7687. (let ((re (concat key "\\([0-9]+\\)"))
  7688. (re2
  7689. (when remove
  7690. (if (equal key "$")
  7691. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  7692. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  7693. s n a)
  7694. (when remove
  7695. (while (re-search-forward re2 (point-at-eol) t)
  7696. (replace-match "")))
  7697. (while (re-search-forward re (point-at-eol) t)
  7698. (setq s (match-string 1) n (string-to-number s))
  7699. (cond
  7700. ((setq a (assoc s replace))
  7701. (replace-match (concat key (cdr a)) t t))
  7702. ((and limit (> n limit))
  7703. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  7704. (defun org-table-get-specials ()
  7705. "Get the column names and local parameters for this table."
  7706. (save-excursion
  7707. (let ((beg (org-table-begin)) (end (org-table-end))
  7708. names name fields fields1 field cnt
  7709. c v l line col types dlines hlines)
  7710. (setq org-table-column-names nil
  7711. org-table-local-parameters nil
  7712. org-table-named-field-locations nil
  7713. org-table-current-begin-line nil
  7714. org-table-current-begin-pos nil
  7715. org-table-current-line-types nil)
  7716. (goto-char beg)
  7717. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  7718. (setq names (org-split-string (match-string 1) " *| *")
  7719. cnt 1)
  7720. (while (setq name (pop names))
  7721. (setq cnt (1+ cnt))
  7722. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  7723. (push (cons name (int-to-string cnt)) org-table-column-names))))
  7724. (setq org-table-column-names (nreverse org-table-column-names))
  7725. (setq org-table-column-name-regexp
  7726. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  7727. (goto-char beg)
  7728. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  7729. (setq fields (org-split-string (match-string 1) " *| *"))
  7730. (while (setq field (pop fields))
  7731. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  7732. (push (cons (match-string 1 field) (match-string 2 field))
  7733. org-table-local-parameters))))
  7734. (goto-char beg)
  7735. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  7736. (setq c (match-string 1)
  7737. fields (org-split-string (match-string 2) " *| *"))
  7738. (save-excursion
  7739. (beginning-of-line (if (equal c "_") 2 0))
  7740. (setq line (org-current-line) col 1)
  7741. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  7742. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  7743. (while (and fields1 (setq field (pop fields)))
  7744. (setq v (pop fields1) col (1+ col))
  7745. (when (and (stringp field) (stringp v)
  7746. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  7747. (push (cons field v) org-table-local-parameters)
  7748. (push (list field line col) org-table-named-field-locations))))
  7749. ;; Analyse the line types
  7750. (goto-char beg)
  7751. (setq org-table-current-begin-line (org-current-line)
  7752. org-table-current-begin-pos (point)
  7753. l org-table-current-begin-line)
  7754. (while (looking-at "[ \t]*|\\(-\\)?")
  7755. (push (if (match-end 1) 'hline 'dline) types)
  7756. (if (match-end 1) (push l hlines) (push l dlines))
  7757. (beginning-of-line 2)
  7758. (setq l (1+ l)))
  7759. (setq org-table-current-line-types (apply 'vector (nreverse types))
  7760. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  7761. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  7762. (defun org-table-maybe-eval-formula ()
  7763. "Check if the current field starts with \"=\" or \":=\".
  7764. If yes, store the formula and apply it."
  7765. ;; We already know we are in a table. Get field will only return a formula
  7766. ;; when appropriate. It might return a separator line, but no problem.
  7767. (when org-table-formula-evaluate-inline
  7768. (let* ((field (org-trim (or (org-table-get-field) "")))
  7769. named eq)
  7770. (when (string-match "^:?=\\(.*\\)" field)
  7771. (setq named (equal (string-to-char field) ?:)
  7772. eq (match-string 1 field))
  7773. (if (or (fboundp 'calc-eval)
  7774. (equal (substring eq 0 (min 2 (length eq))) "'("))
  7775. (org-table-eval-formula (if named '(4) nil)
  7776. (org-table-formula-from-user eq))
  7777. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  7778. (defvar org-recalc-commands nil
  7779. "List of commands triggering the recalculation of a line.
  7780. Will be filled automatically during use.")
  7781. (defvar org-recalc-marks
  7782. '((" " . "Unmarked: no special line, no automatic recalculation")
  7783. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  7784. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  7785. ("!" . "Column name definition line. Reference in formula as $name.")
  7786. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  7787. ("_" . "Names for values in row below this one.")
  7788. ("^" . "Names for values in row above this one.")))
  7789. (defun org-table-rotate-recalc-marks (&optional newchar)
  7790. "Rotate the recalculation mark in the first column.
  7791. If in any row, the first field is not consistent with a mark,
  7792. insert a new column for the markers.
  7793. When there is an active region, change all the lines in the region,
  7794. after prompting for the marking character.
  7795. After each change, a message will be displayed indicating the meaning
  7796. of the new mark."
  7797. (interactive)
  7798. (unless (org-at-table-p) (error "Not at a table"))
  7799. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  7800. (beg (org-table-begin))
  7801. (end (org-table-end))
  7802. (l (org-current-line))
  7803. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  7804. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  7805. (have-col
  7806. (save-excursion
  7807. (goto-char beg)
  7808. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  7809. (col (org-table-current-column))
  7810. (forcenew (car (assoc newchar org-recalc-marks)))
  7811. epos new)
  7812. (when l1
  7813. (message "Change region to what mark? Type # * ! $ or SPC: ")
  7814. (setq newchar (char-to-string (read-char-exclusive))
  7815. forcenew (car (assoc newchar org-recalc-marks))))
  7816. (if (and newchar (not forcenew))
  7817. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  7818. newchar))
  7819. (if l1 (goto-line l1))
  7820. (save-excursion
  7821. (beginning-of-line 1)
  7822. (unless (looking-at org-table-dataline-regexp)
  7823. (error "Not at a table data line")))
  7824. (unless have-col
  7825. (org-table-goto-column 1)
  7826. (org-table-insert-column)
  7827. (org-table-goto-column (1+ col)))
  7828. (setq epos (point-at-eol))
  7829. (save-excursion
  7830. (beginning-of-line 1)
  7831. (org-table-get-field
  7832. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  7833. (concat " "
  7834. (setq new (or forcenew
  7835. (cadr (member (match-string 1) marks))))
  7836. " ")
  7837. " # ")))
  7838. (if (and l1 l2)
  7839. (progn
  7840. (goto-line l1)
  7841. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  7842. (and (looking-at org-table-dataline-regexp)
  7843. (org-table-get-field 1 (concat " " new " "))))
  7844. (goto-line l1)))
  7845. (if (not (= epos (point-at-eol))) (org-table-align))
  7846. (goto-line l)
  7847. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  7848. (defun org-table-maybe-recalculate-line ()
  7849. "Recompute the current line if marked for it, and if we haven't just done it."
  7850. (interactive)
  7851. (and org-table-allow-automatic-line-recalculation
  7852. (not (and (memq last-command org-recalc-commands)
  7853. (equal org-last-recalc-line (org-current-line))))
  7854. (save-excursion (beginning-of-line 1)
  7855. (looking-at org-table-auto-recalculate-regexp))
  7856. (org-table-recalculate) t))
  7857. (defvar org-table-formula-debug nil
  7858. "Non-nil means, debug table formulas.
  7859. When nil, simply write \"#ERROR\" in corrupted fields.")
  7860. (make-variable-buffer-local 'org-table-formula-debug)
  7861. (defvar modes)
  7862. (defsubst org-set-calc-mode (var &optional value)
  7863. (if (stringp var)
  7864. (setq var (assoc var '(("D" calc-angle-mode deg)
  7865. ("R" calc-angle-mode rad)
  7866. ("F" calc-prefer-frac t)
  7867. ("S" calc-symbolic-mode t)))
  7868. value (nth 2 var) var (nth 1 var)))
  7869. (if (memq var modes)
  7870. (setcar (cdr (memq var modes)) value)
  7871. (cons var (cons value modes)))
  7872. modes)
  7873. (defun org-table-eval-formula (&optional arg equation
  7874. suppress-align suppress-const
  7875. suppress-store suppress-analysis)
  7876. "Replace the table field value at the cursor by the result of a calculation.
  7877. This function makes use of Dave Gillespie's Calc package, in my view the
  7878. most exciting program ever written for GNU Emacs. So you need to have Calc
  7879. installed in order to use this function.
  7880. In a table, this command replaces the value in the current field with the
  7881. result of a formula. It also installs the formula as the \"current\" column
  7882. formula, by storing it in a special line below the table. When called
  7883. with a `C-u' prefix, the current field must ba a named field, and the
  7884. formula is installed as valid in only this specific field.
  7885. When called with two `C-u' prefixes, insert the active equation
  7886. for the field back into the current field, so that it can be
  7887. edited there. This is useful in order to use \\[org-table-show-reference]
  7888. to check the referenced fields.
  7889. When called, the command first prompts for a formula, which is read in
  7890. the minibuffer. Previously entered formulas are available through the
  7891. history list, and the last used formula is offered as a default.
  7892. These stored formulas are adapted correctly when moving, inserting, or
  7893. deleting columns with the corresponding commands.
  7894. The formula can be any algebraic expression understood by the Calc package.
  7895. For details, see the Org-mode manual.
  7896. This function can also be called from Lisp programs and offers
  7897. additional arguments: EQUATION can be the formula to apply. If this
  7898. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  7899. used to speed-up recursive calls by by-passing unnecessary aligns.
  7900. SUPPRESS-CONST suppresses the interpretation of constants in the
  7901. formula, assuming that this has been done already outside the function.
  7902. SUPPRESS-STORE means the formula should not be stored, either because
  7903. it is already stored, or because it is a modified equation that should
  7904. not overwrite the stored one."
  7905. (interactive "P")
  7906. (org-table-check-inside-data-field)
  7907. (or suppress-analysis (org-table-get-specials))
  7908. (if (equal arg '(16))
  7909. (let ((eq (org-table-current-field-formula)))
  7910. (or eq (error "No equation active for current field"))
  7911. (org-table-get-field nil eq)
  7912. (org-table-align)
  7913. (setq org-table-may-need-update t))
  7914. (let* (fields
  7915. (ndown (if (integerp arg) arg 1))
  7916. (org-table-automatic-realign nil)
  7917. (case-fold-search nil)
  7918. (down (> ndown 1))
  7919. (formula (if (and equation suppress-store)
  7920. equation
  7921. (org-table-get-formula equation (equal arg '(4)))))
  7922. (n0 (org-table-current-column))
  7923. (modes (copy-sequence org-calc-default-modes))
  7924. (numbers nil) ; was a variable, now fixed default
  7925. (keep-empty nil)
  7926. n form form0 bw fmt x ev orig c lispp literal)
  7927. ;; Parse the format string. Since we have a lot of modes, this is
  7928. ;; a lot of work. However, I think calc still uses most of the time.
  7929. (if (string-match ";" formula)
  7930. (let ((tmp (org-split-string formula ";")))
  7931. (setq formula (car tmp)
  7932. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  7933. (nth 1 tmp)))
  7934. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  7935. (setq c (string-to-char (match-string 1 fmt))
  7936. n (string-to-number (match-string 2 fmt)))
  7937. (if (= c ?p)
  7938. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  7939. (setq modes (org-set-calc-mode
  7940. 'calc-float-format
  7941. (list (cdr (assoc c '((?n . float) (?f . fix)
  7942. (?s . sci) (?e . eng))))
  7943. n))))
  7944. (setq fmt (replace-match "" t t fmt)))
  7945. (if (string-match "[NT]" fmt)
  7946. (setq numbers (equal (match-string 0 fmt) "N")
  7947. fmt (replace-match "" t t fmt)))
  7948. (if (string-match "L" fmt)
  7949. (setq literal t
  7950. fmt (replace-match "" t t fmt)))
  7951. (if (string-match "E" fmt)
  7952. (setq keep-empty t
  7953. fmt (replace-match "" t t fmt)))
  7954. (while (string-match "[DRFS]" fmt)
  7955. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  7956. (setq fmt (replace-match "" t t fmt)))
  7957. (unless (string-match "\\S-" fmt)
  7958. (setq fmt nil))))
  7959. (if (and (not suppress-const) org-table-formula-use-constants)
  7960. (setq formula (org-table-formula-substitute-names formula)))
  7961. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  7962. (while (> ndown 0)
  7963. (setq fields (org-split-string
  7964. (org-no-properties
  7965. (buffer-substring (point-at-bol) (point-at-eol)))
  7966. " *| *"))
  7967. (if (eq numbers t)
  7968. (setq fields (mapcar
  7969. (lambda (x) (number-to-string (string-to-number x)))
  7970. fields)))
  7971. (setq ndown (1- ndown))
  7972. (setq form (copy-sequence formula)
  7973. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  7974. (if (and lispp literal) (setq lispp 'literal))
  7975. ;; Check for old vertical references
  7976. (setq form (org-rewrite-old-row-references form))
  7977. ;; Insert complex ranges
  7978. (while (string-match org-table-range-regexp form)
  7979. (setq form
  7980. (replace-match
  7981. (save-match-data
  7982. (org-table-make-reference
  7983. (org-table-get-range (match-string 0 form) nil n0)
  7984. keep-empty numbers lispp))
  7985. t t form)))
  7986. ;; Insert simple ranges
  7987. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  7988. (setq form
  7989. (replace-match
  7990. (save-match-data
  7991. (org-table-make-reference
  7992. (org-sublist
  7993. fields (string-to-number (match-string 1 form))
  7994. (string-to-number (match-string 2 form)))
  7995. keep-empty numbers lispp))
  7996. t t form)))
  7997. (setq form0 form)
  7998. ;; Insert the references to fields in same row
  7999. (while (string-match "\\$\\([0-9]+\\)" form)
  8000. (setq n (string-to-number (match-string 1 form))
  8001. x (nth (1- (if (= n 0) n0 n)) fields))
  8002. (unless x (error "Invalid field specifier \"%s\""
  8003. (match-string 0 form)))
  8004. (setq form (replace-match
  8005. (save-match-data
  8006. (org-table-make-reference x nil numbers lispp))
  8007. t t form)))
  8008. (if lispp
  8009. (setq ev (condition-case nil
  8010. (eval (eval (read form)))
  8011. (error "#ERROR"))
  8012. ev (if (numberp ev) (number-to-string ev) ev))
  8013. (or (fboundp 'calc-eval)
  8014. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  8015. (setq ev (calc-eval (cons form modes)
  8016. (if numbers 'num))))
  8017. (when org-table-formula-debug
  8018. (with-output-to-temp-buffer "*Substitution History*"
  8019. (princ (format "Substitution history of formula
  8020. Orig: %s
  8021. $xyz-> %s
  8022. @r$c-> %s
  8023. $1-> %s\n" orig formula form0 form))
  8024. (if (listp ev)
  8025. (princ (format " %s^\nError: %s"
  8026. (make-string (car ev) ?\-) (nth 1 ev)))
  8027. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  8028. ev (or fmt "NONE")
  8029. (if fmt (format fmt (string-to-number ev)) ev)))))
  8030. (setq bw (get-buffer-window "*Substitution History*"))
  8031. (shrink-window-if-larger-than-buffer bw)
  8032. (unless (and (interactive-p) (not ndown))
  8033. (unless (let (inhibit-redisplay)
  8034. (y-or-n-p "Debugging Formula. Continue to next? "))
  8035. (org-table-align)
  8036. (error "Abort"))
  8037. (delete-window bw)
  8038. (message "")))
  8039. (if (listp ev) (setq fmt nil ev "#ERROR"))
  8040. (org-table-justify-field-maybe
  8041. (if fmt (format fmt (string-to-number ev)) ev))
  8042. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  8043. (call-interactively 'org-return)
  8044. (setq ndown 0)))
  8045. (and down (org-table-maybe-recalculate-line))
  8046. (or suppress-align (and org-table-may-need-update
  8047. (org-table-align))))))
  8048. (defun org-table-put-field-property (prop value)
  8049. (save-excursion
  8050. (put-text-property (progn (skip-chars-backward "^|") (point))
  8051. (progn (skip-chars-forward "^|") (point))
  8052. prop value)))
  8053. (defun org-table-get-range (desc &optional tbeg col highlight)
  8054. "Get a calc vector from a column, accorting to descriptor DESC.
  8055. Optional arguments TBEG and COL can give the beginning of the table and
  8056. the current column, to avoid unnecessary parsing.
  8057. HIGHLIGHT means, just highlight the range."
  8058. (if (not (equal (string-to-char desc) ?@))
  8059. (setq desc (concat "@" desc)))
  8060. (save-excursion
  8061. (or tbeg (setq tbeg (org-table-begin)))
  8062. (or col (setq col (org-table-current-column)))
  8063. (let ((thisline (org-current-line))
  8064. beg end c1 c2 r1 r2 rangep tmp)
  8065. (unless (string-match org-table-range-regexp desc)
  8066. (error "Invalid table range specifier `%s'" desc))
  8067. (setq rangep (match-end 3)
  8068. r1 (and (match-end 1) (match-string 1 desc))
  8069. r2 (and (match-end 4) (match-string 4 desc))
  8070. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  8071. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  8072. (and c1 (setq c1 (+ (string-to-number c1)
  8073. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  8074. (and c2 (setq c2 (+ (string-to-number c2)
  8075. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  8076. (if (equal r1 "") (setq r1 nil))
  8077. (if (equal r2 "") (setq r2 nil))
  8078. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  8079. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  8080. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  8081. (if (not r1) (setq r1 thisline))
  8082. (if (not r2) (setq r2 thisline))
  8083. (if (not c1) (setq c1 col))
  8084. (if (not c2) (setq c2 col))
  8085. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  8086. ;; just one field
  8087. (progn
  8088. (goto-line r1)
  8089. (while (not (looking-at org-table-dataline-regexp))
  8090. (beginning-of-line 2))
  8091. (prog1 (org-trim (org-table-get-field c1))
  8092. (if highlight (org-table-highlight-rectangle (point) (point)))))
  8093. ;; A range, return a vector
  8094. ;; First sort the numbers to get a regular ractangle
  8095. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  8096. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  8097. (goto-line r1)
  8098. (while (not (looking-at org-table-dataline-regexp))
  8099. (beginning-of-line 2))
  8100. (org-table-goto-column c1)
  8101. (setq beg (point))
  8102. (goto-line r2)
  8103. (while (not (looking-at org-table-dataline-regexp))
  8104. (beginning-of-line 0))
  8105. (org-table-goto-column c2)
  8106. (setq end (point))
  8107. (if highlight
  8108. (org-table-highlight-rectangle
  8109. beg (progn (skip-chars-forward "^|\n") (point))))
  8110. ;; return string representation of calc vector
  8111. (mapcar 'org-trim
  8112. (apply 'append (org-table-copy-region beg end)))))))
  8113. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  8114. "Analyze descriptor DESC and retrieve the corresponding line number.
  8115. The cursor is currently in line CLINE, the table begins in line BLINE,
  8116. and TABLE is a vector with line types."
  8117. (if (string-match "^[0-9]+$" desc)
  8118. (aref org-table-dlines (string-to-number desc))
  8119. (setq cline (or cline (org-current-line))
  8120. bline (or bline org-table-current-begin-line)
  8121. table (or table org-table-current-line-types))
  8122. (if (or
  8123. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  8124. ;; 1 2 3 4 5 6
  8125. (and (not (match-end 3)) (not (match-end 6)))
  8126. (and (match-end 3) (match-end 6) (not (match-end 5))))
  8127. (error "invalid row descriptor `%s'" desc))
  8128. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  8129. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  8130. (odir (and (match-end 5) (match-string 5 desc)))
  8131. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  8132. (i (- cline bline))
  8133. (rel (and (match-end 6)
  8134. (or (and (match-end 1) (not (match-end 3)))
  8135. (match-end 5)))))
  8136. (if (and hn (not hdir))
  8137. (progn
  8138. (setq i 0 hdir "+")
  8139. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  8140. (if (and (not hn) on (not odir))
  8141. (error "should never happen");;(aref org-table-dlines on)
  8142. (if (and hn (> hn 0))
  8143. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  8144. (if on
  8145. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  8146. (+ bline i)))))
  8147. (defun org-find-row-type (table i type backwards relative n)
  8148. (let ((l (length table)))
  8149. (while (> n 0)
  8150. (while (and (setq i (+ i (if backwards -1 1)))
  8151. (>= i 0) (< i l)
  8152. (not (eq (aref table i) type))
  8153. (if (and relative (eq (aref table i) 'hline))
  8154. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  8155. t)))
  8156. (setq n (1- n)))
  8157. (if (or (< i 0) (>= i l))
  8158. (error "Row descriptior leads outside table")
  8159. i)))
  8160. (defun org-rewrite-old-row-references (s)
  8161. (if (string-match "&[-+0-9I]" s)
  8162. (error "Formula contains old &row reference, please rewrite using @-syntax")
  8163. s))
  8164. (defun org-table-make-reference (elements keep-empty numbers lispp)
  8165. "Convert list ELEMENTS to something appropriate to insert into formula.
  8166. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  8167. NUMBERS indicates that everything should be converted to numbers.
  8168. LISPP means to return something appropriate for a Lisp list."
  8169. (if (stringp elements) ; just a single val
  8170. (if lispp
  8171. (if (eq lispp 'literal)
  8172. elements
  8173. (prin1-to-string (if numbers (string-to-number elements) elements)))
  8174. (if (equal elements "") (setq elements "0"))
  8175. (if numbers (number-to-string (string-to-number elements)) elements))
  8176. (unless keep-empty
  8177. (setq elements
  8178. (delq nil
  8179. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8180. elements))))
  8181. (setq elements (or elements '("0")))
  8182. (if lispp
  8183. (mapconcat
  8184. (lambda (x)
  8185. (if (eq lispp 'literal)
  8186. x
  8187. (prin1-to-string (if numbers (string-to-number x) x))))
  8188. elements " ")
  8189. (concat "[" (mapconcat
  8190. (lambda (x)
  8191. (if numbers (number-to-string (string-to-number x)) x))
  8192. elements
  8193. ",") "]"))))
  8194. (defun org-table-recalculate (&optional all noalign)
  8195. "Recalculate the current table line by applying all stored formulas.
  8196. With prefix arg ALL, do this for all lines in the table."
  8197. (interactive "P")
  8198. (or (memq this-command org-recalc-commands)
  8199. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8200. (unless (org-at-table-p) (error "Not at a table"))
  8201. (if (equal all '(16))
  8202. (org-table-iterate)
  8203. (org-table-get-specials)
  8204. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8205. (lambda (a b) (string< (car a) (car b)))))
  8206. (inhibit-redisplay (not debug-on-error))
  8207. (line-re org-table-dataline-regexp)
  8208. (thisline (org-current-line))
  8209. (thiscol (org-table-current-column))
  8210. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8211. ;; Insert constants in all formulas
  8212. (setq eqlist
  8213. (mapcar (lambda (x)
  8214. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8215. x)
  8216. eqlist))
  8217. ;; Split the equation list
  8218. (while (setq eq (pop eqlist))
  8219. (if (<= (string-to-char (car eq)) ?9)
  8220. (push eq eqlnum)
  8221. (push eq eqlname)))
  8222. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8223. (if all
  8224. (progn
  8225. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8226. (goto-char (setq beg (org-table-begin)))
  8227. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8228. ;; This is a table with marked lines, compute selected lines
  8229. (setq line-re org-table-recalculate-regexp)
  8230. ;; Move forward to the first non-header line
  8231. (if (and (re-search-forward org-table-dataline-regexp end t)
  8232. (re-search-forward org-table-hline-regexp end t)
  8233. (re-search-forward org-table-dataline-regexp end t))
  8234. (setq beg (match-beginning 0))
  8235. nil))) ;; just leave beg where it is
  8236. (setq beg (point-at-bol)
  8237. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8238. (goto-char beg)
  8239. (and all (message "Re-applying formulas to full table..."))
  8240. ;; First find the named fields, and mark them untouchanble
  8241. (remove-text-properties beg end '(org-untouchable t))
  8242. (while (setq eq (pop eqlname))
  8243. (setq name (car eq)
  8244. a (assoc name org-table-named-field-locations))
  8245. (and (not a)
  8246. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8247. (setq a (list name
  8248. (aref org-table-dlines
  8249. (string-to-number (match-string 1 name)))
  8250. (string-to-number (match-string 2 name)))))
  8251. (when (and a (or all (equal (nth 1 a) thisline)))
  8252. (message "Re-applying formula to field: %s" name)
  8253. (goto-line (nth 1 a))
  8254. (org-table-goto-column (nth 2 a))
  8255. (push (append a (list (cdr eq))) eqlname1)
  8256. ;; FIXME (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
  8257. ;; FIXME 'nostore 'noanalysis)
  8258. (org-table-put-field-property :org-untouchable t)))
  8259. ;; Now evauluate the column formulas, but skip fields covered by
  8260. ;; field formulas
  8261. (goto-char beg)
  8262. (while (re-search-forward line-re end t)
  8263. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8264. ;; Unprotected line, recalculate
  8265. (and all (message "Re-applying formulas to full table...(line %d)"
  8266. (setq cnt (1+ cnt))))
  8267. (setq org-last-recalc-line (org-current-line))
  8268. (setq eql eqlnum)
  8269. (while (setq entry (pop eql))
  8270. (goto-line org-last-recalc-line)
  8271. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8272. (unless (get-text-property (point) :org-untouchable)
  8273. (org-table-eval-formula nil (cdr entry)
  8274. 'noalign 'nocst 'nostore 'noanalysis)))))
  8275. ;; Now evaluate the field formulas
  8276. (while (setq eq (pop eqlname1))
  8277. (message "Re-applying formula to field: %s" (car eq))
  8278. (goto-line (nth 1 eq))
  8279. (org-table-goto-column (nth 2 eq))
  8280. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8281. 'nostore 'noanalysis))
  8282. (goto-line thisline)
  8283. (org-table-goto-column thiscol)
  8284. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8285. (or noalign (and org-table-may-need-update (org-table-align))
  8286. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8287. ;; back to initial position
  8288. (message "Re-applying formulas...done")
  8289. (goto-line thisline)
  8290. (org-table-goto-column thiscol)
  8291. (or noalign (and org-table-may-need-update (org-table-align))
  8292. (and all (message "Re-applying formulas...done"))))))
  8293. (defun org-table-iterate (&optional arg)
  8294. "Recalculate the table until it does not change anymore."
  8295. (interactive "P")
  8296. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8297. (i 0)
  8298. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8299. thistbl)
  8300. (catch 'exit
  8301. (while (< i imax)
  8302. (setq i (1+ i))
  8303. (org-table-recalculate 'all)
  8304. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8305. (if (not (string= lasttbl thistbl))
  8306. (setq lasttbl thistbl)
  8307. (if (> i 1)
  8308. (message "Convergence after %d iterations" i)
  8309. (message "Table was already stable"))
  8310. (throw 'exit t)))
  8311. (error "No convergence after %d iterations" i))))
  8312. (defun org-table-formula-substitute-names (f)
  8313. "Replace $const with values in string F."
  8314. (let ((start 0) a (f1 f))
  8315. ;; First, check for column names
  8316. (while (setq start (string-match org-table-column-name-regexp f start))
  8317. (setq start (1+ start))
  8318. (setq a (assoc (match-string 1 f) org-table-column-names))
  8319. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8320. ;; Parameters and constants
  8321. (setq start 0)
  8322. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8323. (setq start (1+ start))
  8324. (if (setq a (save-match-data
  8325. (org-table-get-constant (match-string 1 f))))
  8326. (setq f (replace-match (concat "(" a ")") t t f))))
  8327. (if org-table-formula-debug
  8328. (put-text-property 0 (length f) :orig-formula f1 f))
  8329. f))
  8330. (defun org-table-get-constant (const)
  8331. "Find the value for a parameter or constant in a formula.
  8332. Parameters get priority."
  8333. (or (cdr (assoc const org-table-local-parameters))
  8334. (cdr (assoc const org-table-formula-constants-local))
  8335. (cdr (assoc const org-table-formula-constants))
  8336. (and (fboundp 'constants-get) (constants-get const))
  8337. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8338. (org-entry-get nil (substring const 5) 'inherit))
  8339. "#UNDEFINED_NAME"))
  8340. (defvar org-table-fedit-map
  8341. (let ((map (make-sparse-keymap)))
  8342. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  8343. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  8344. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  8345. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  8346. (org-defkey map "\C-c?" 'org-table-show-reference)
  8347. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  8348. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  8349. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  8350. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  8351. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  8352. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  8353. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  8354. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  8355. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  8356. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  8357. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  8358. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  8359. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8360. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8361. map))
  8362. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8363. '("Edit-Formulas"
  8364. ["Finish and Install" org-table-fedit-finish t]
  8365. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8366. ["Abort" org-table-fedit-abort t]
  8367. "--"
  8368. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8369. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8370. "--"
  8371. "Shift Reference at Point"
  8372. ["Up" org-table-fedit-ref-up t]
  8373. ["Down" org-table-fedit-ref-down t]
  8374. ["Left" org-table-fedit-ref-left t]
  8375. ["Right" org-table-fedit-ref-right t]
  8376. "-"
  8377. "Change Test Row for Column Formulas"
  8378. ["Up" org-table-fedit-line-up t]
  8379. ["Down" org-table-fedit-line-down t]
  8380. "--"
  8381. ["Scroll Table Window" org-table-fedit-scroll t]
  8382. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8383. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8384. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8385. org-table-overlay-coordinates)]
  8386. "--"
  8387. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8388. :style toggle :selected org-table-buffer-is-an]))
  8389. (defvar org-pos)
  8390. (defun org-table-edit-formulas ()
  8391. "Edit the formulas of the current table in a separate buffer."
  8392. (interactive)
  8393. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8394. (beginning-of-line 0))
  8395. (unless (org-at-table-p) (error "Not at a table"))
  8396. (org-table-get-specials)
  8397. (let ((key (org-table-current-field-formula 'key 'noerror))
  8398. (eql (sort (org-table-get-stored-formulas 'noerror)
  8399. 'org-table-formula-less-p))
  8400. (pos (move-marker (make-marker) (point)))
  8401. (startline 1)
  8402. (wc (current-window-configuration))
  8403. (titles '((column . "# Column Formulas\n")
  8404. (field . "# Field Formulas\n")
  8405. (named . "# Named Field Formulas\n")))
  8406. entry s type title)
  8407. (switch-to-buffer-other-window "*Edit Formulas*")
  8408. (erase-buffer)
  8409. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8410. (let ((font-lock-global-modes '(not fundamental-mode)))
  8411. (fundamental-mode))
  8412. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8413. (org-set-local 'org-pos pos)
  8414. (org-set-local 'org-window-configuration wc)
  8415. (use-local-map org-table-fedit-map)
  8416. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8417. (easy-menu-add org-table-fedit-menu)
  8418. (setq startline (org-current-line))
  8419. (while (setq entry (pop eql))
  8420. (setq type (cond
  8421. ((equal (string-to-char (car entry)) ?@) 'field)
  8422. ((string-match "^[0-9]" (car entry)) 'column)
  8423. (t 'named)))
  8424. (when (setq title (assq type titles))
  8425. (or (bobp) (insert "\n"))
  8426. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  8427. (setq titles (delq title titles)))
  8428. (if (equal key (car entry)) (setq startline (org-current-line)))
  8429. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  8430. (car entry) " = " (cdr entry) "\n"))
  8431. (remove-text-properties 0 (length s) '(face nil) s)
  8432. (insert s))
  8433. (if (eq org-table-use-standard-references t)
  8434. (org-table-fedit-toggle-ref-type))
  8435. (goto-line startline)
  8436. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  8437. (defun org-table-fedit-post-command ()
  8438. (when (not (memq this-command '(lisp-complete-symbol)))
  8439. (let ((win (selected-window)))
  8440. (save-excursion
  8441. (condition-case nil
  8442. (org-table-show-reference)
  8443. (error nil))
  8444. (select-window win)))))
  8445. (defun org-table-formula-to-user (s)
  8446. "Convert a formula from internal to user representation."
  8447. (if (eq org-table-use-standard-references t)
  8448. (org-table-convert-refs-to-an s)
  8449. s))
  8450. (defun org-table-formula-from-user (s)
  8451. "Convert a formula from user to internal representation."
  8452. (if org-table-use-standard-references
  8453. (org-table-convert-refs-to-rc s)
  8454. s))
  8455. (defun org-table-convert-refs-to-rc (s)
  8456. "Convert spreadsheet references from AB7 to @7$28.
  8457. Works for single references, but also for entire formulas and even the
  8458. full TBLFM line."
  8459. (let ((start 0))
  8460. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  8461. (cond
  8462. ((match-end 3)
  8463. ;; format match, just advance
  8464. (setq start (match-end 0)))
  8465. ((and (> (match-beginning 0) 0)
  8466. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  8467. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  8468. ;; 3.e5 or something like this. FIXME: is this ok????
  8469. (setq start (match-end 0)))
  8470. (t
  8471. (setq start (match-beginning 0)
  8472. s (replace-match
  8473. (if (equal (match-string 2 s) "&")
  8474. (format "$%d" (org-letters-to-number (match-string 1 s)))
  8475. (format "@%d$%d"
  8476. (string-to-number (match-string 2 s))
  8477. (org-letters-to-number (match-string 1 s))))
  8478. t t s)))))
  8479. s))
  8480. (defun org-table-convert-refs-to-an (s)
  8481. "Convert spreadsheet references from to @7$28 to AB7.
  8482. Works for single references, but also for entire formulas and even the
  8483. full TBLFM line."
  8484. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  8485. (setq s (replace-match
  8486. (format "%s%d"
  8487. (org-number-to-letters
  8488. (string-to-number (match-string 2 s)))
  8489. (string-to-number (match-string 1 s)))
  8490. t t s)))
  8491. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  8492. (setq s (replace-match (concat "\\1"
  8493. (org-number-to-letters
  8494. (string-to-number (match-string 2 s))) "&")
  8495. t nil s)))
  8496. s)
  8497. (defun org-letters-to-number (s)
  8498. "Convert a base 26 number represented by letters into an integer.
  8499. For example: AB -> 28."
  8500. (let ((n 0))
  8501. (setq s (upcase s))
  8502. (while (> (length s) 0)
  8503. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  8504. s (substring s 1)))
  8505. n))
  8506. (defun org-number-to-letters (n)
  8507. "Convert an integer into a base 26 number represented by letters.
  8508. For example: 28 -> AB."
  8509. (let ((s ""))
  8510. (while (> n 0)
  8511. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  8512. n (/ (1- n) 26)))
  8513. s))
  8514. (defun org-table-fedit-convert-buffer (function)
  8515. "Convert all references in this buffer, using FUNTION."
  8516. (let ((line (org-current-line)))
  8517. (goto-char (point-min))
  8518. (while (not (eobp))
  8519. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  8520. (delete-region (point) (point-at-eol))
  8521. (or (eobp) (forward-char 1)))
  8522. (goto-line line)))
  8523. (defun org-table-fedit-toggle-ref-type ()
  8524. "Convert all references in the buffer from B3 to @3$2 and back."
  8525. (interactive)
  8526. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  8527. (org-table-fedit-convert-buffer
  8528. (if org-table-buffer-is-an
  8529. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  8530. (message "Reference type switched to %s"
  8531. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  8532. (defun org-table-fedit-ref-up ()
  8533. "Shift the reference at point one row/hline up."
  8534. (interactive)
  8535. (org-table-fedit-shift-reference 'up))
  8536. (defun org-table-fedit-ref-down ()
  8537. "Shift the reference at point one row/hline down."
  8538. (interactive)
  8539. (org-table-fedit-shift-reference 'down))
  8540. (defun org-table-fedit-ref-left ()
  8541. "Shift the reference at point one field to the left."
  8542. (interactive)
  8543. (org-table-fedit-shift-reference 'left))
  8544. (defun org-table-fedit-ref-right ()
  8545. "Shift the reference at point one field to the right."
  8546. (interactive)
  8547. (org-table-fedit-shift-reference 'right))
  8548. (defun org-table-fedit-shift-reference (dir)
  8549. (cond
  8550. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  8551. (if (memq dir '(left right))
  8552. (org-rematch-and-replace 1 (eq dir 'left))
  8553. (error "Cannot shift reference in this direction")))
  8554. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8555. ;; A B3-like reference
  8556. (if (memq dir '(up down))
  8557. (org-rematch-and-replace 2 (eq dir 'up))
  8558. (org-rematch-and-replace 1 (eq dir 'left))))
  8559. ((org-at-regexp-p
  8560. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8561. ;; An internal reference
  8562. (if (memq dir '(up down))
  8563. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8564. (org-rematch-and-replace 5 (eq dir 'left))))))
  8565. (defun org-rematch-and-replace (n &optional decr hline)
  8566. "Re-match the group N, and replace it with the shifted refrence."
  8567. (or (match-end n) (error "Cannot shift reference in this direction"))
  8568. (goto-char (match-beginning n))
  8569. (and (looking-at (regexp-quote (match-string n)))
  8570. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8571. t t)))
  8572. (defun org-shift-refpart (ref &optional decr hline)
  8573. "Shift a refrence part REF.
  8574. If DECR is set, decrease the references row/column, else increase.
  8575. If HLINE is set, this may be a hline reference, it certainly is not
  8576. a translation reference."
  8577. (save-match-data
  8578. (let* ((sign (string-match "^[-+]" ref)) n)
  8579. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8580. (cond
  8581. ((and hline (string-match "^I+" ref))
  8582. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8583. (setq n (+ n (if decr -1 1)))
  8584. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8585. (if sign
  8586. (setq sign (if (< n 0) "-" "+") n (abs n))
  8587. (setq n (max 1 n)))
  8588. (concat sign (make-string n ?I)))
  8589. ((string-match "^[0-9]+" ref)
  8590. (setq n (string-to-number (concat sign ref)))
  8591. (setq n (+ n (if decr -1 1)))
  8592. (if sign
  8593. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8594. (number-to-string (max 1 n))))
  8595. ((string-match "^[a-zA-Z]+" ref)
  8596. (org-number-to-letters
  8597. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8598. (t (error "Cannot shift reference"))))))
  8599. (defun org-table-fedit-toggle-coordinates ()
  8600. "Toggle the display of coordinates in the refrenced table."
  8601. (interactive)
  8602. (let ((pos (marker-position org-pos)))
  8603. (with-current-buffer (marker-buffer org-pos)
  8604. (save-excursion
  8605. (goto-char pos)
  8606. (org-table-toggle-coordinate-overlays)))))
  8607. (defun org-table-fedit-finish (&optional arg)
  8608. "Parse the buffer for formula definitions and install them.
  8609. With prefix ARG, apply the new formulas to the table."
  8610. (interactive "P")
  8611. (org-table-remove-rectangle-highlight)
  8612. (if org-table-use-standard-references
  8613. (progn
  8614. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8615. (setq org-table-buffer-is-an nil)))
  8616. (let ((pos org-pos) eql var form)
  8617. (goto-char (point-min))
  8618. (while (re-search-forward
  8619. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  8620. nil t)
  8621. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  8622. form (match-string 3))
  8623. (setq form (org-trim form))
  8624. (when (not (equal form ""))
  8625. (while (string-match "[ \t]*\n[ \t]*" form)
  8626. (setq form (replace-match " " t t form)))
  8627. (when (assoc var eql)
  8628. (error "Double formulas for %s" var))
  8629. (push (cons var form) eql)))
  8630. (setq org-pos nil)
  8631. (set-window-configuration org-window-configuration)
  8632. (select-window (get-buffer-window (marker-buffer pos)))
  8633. (goto-char pos)
  8634. (unless (org-at-table-p)
  8635. (error "Lost table position - cannot install formulae"))
  8636. (org-table-store-formulas eql)
  8637. (move-marker pos nil)
  8638. (kill-buffer "*Edit Formulas*")
  8639. (if arg
  8640. (org-table-recalculate 'all)
  8641. (message "New formulas installed - press C-u C-c C-c to apply."))))
  8642. (defun org-table-fedit-abort ()
  8643. "Abort editing formulas, without installing the changes."
  8644. (interactive)
  8645. (org-table-remove-rectangle-highlight)
  8646. (let ((pos org-pos))
  8647. (set-window-configuration org-window-configuration)
  8648. (select-window (get-buffer-window (marker-buffer pos)))
  8649. (goto-char pos)
  8650. (move-marker pos nil)
  8651. (message "Formula editing aborted without installing changes")))
  8652. (defun org-table-fedit-lisp-indent ()
  8653. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  8654. (interactive)
  8655. (let ((pos (point)) beg end ind)
  8656. (beginning-of-line 1)
  8657. (cond
  8658. ((looking-at "[ \t]")
  8659. (goto-char pos)
  8660. (call-interactively 'lisp-indent-line))
  8661. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  8662. ((not (fboundp 'pp-buffer))
  8663. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  8664. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  8665. (goto-char (- (match-end 0) 2))
  8666. (setq beg (point))
  8667. (setq ind (make-string (current-column) ?\ ))
  8668. (condition-case nil (forward-sexp 1)
  8669. (error
  8670. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  8671. (setq end (point))
  8672. (save-restriction
  8673. (narrow-to-region beg end)
  8674. (if (eq last-command this-command)
  8675. (progn
  8676. (goto-char (point-min))
  8677. (setq this-command nil)
  8678. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8679. (replace-match " ")))
  8680. (pp-buffer)
  8681. (untabify (point-min) (point-max))
  8682. (goto-char (1+ (point-min)))
  8683. (while (re-search-forward "^." nil t)
  8684. (beginning-of-line 1)
  8685. (insert ind))
  8686. (goto-char (point-max))
  8687. (backward-delete-char 1)))
  8688. (goto-char beg))
  8689. (t nil))))
  8690. (defvar org-show-positions nil)
  8691. (defun org-table-show-reference (&optional local)
  8692. "Show the location/value of the $ expression at point."
  8693. (interactive)
  8694. (org-table-remove-rectangle-highlight)
  8695. (catch 'exit
  8696. (let ((pos (if local (point) org-pos))
  8697. (face2 'highlight)
  8698. (org-inhibit-highlight-removal t)
  8699. (win (selected-window))
  8700. (org-show-positions nil)
  8701. var name e what match dest)
  8702. (if local (org-table-get-specials))
  8703. (setq what (cond
  8704. ((or (org-at-regexp-p org-table-range-regexp2)
  8705. (org-at-regexp-p org-table-translate-regexp)
  8706. (org-at-regexp-p org-table-range-regexp))
  8707. (setq match
  8708. (save-match-data
  8709. (org-table-convert-refs-to-rc (match-string 0))))
  8710. 'range)
  8711. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  8712. ((org-at-regexp-p "\\$[0-9]+") 'column)
  8713. ((not local) nil)
  8714. (t (error "No reference at point")))
  8715. match (and what (or match (match-string 0))))
  8716. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  8717. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  8718. 'secondary-selection))
  8719. (org-add-hook 'before-change-functions
  8720. 'org-table-remove-rectangle-highlight)
  8721. (if (eq what 'name) (setq var (substring match 1)))
  8722. (when (eq what 'range)
  8723. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  8724. (setq match (org-table-formula-substitute-names match)))
  8725. (unless local
  8726. (save-excursion
  8727. (end-of-line 1)
  8728. (re-search-backward "^\\S-" nil t)
  8729. (beginning-of-line 1)
  8730. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  8731. (setq dest
  8732. (save-match-data
  8733. (org-table-convert-refs-to-rc (match-string 1))))
  8734. (org-table-add-rectangle-overlay
  8735. (match-beginning 1) (match-end 1) face2))))
  8736. (if (and (markerp pos) (marker-buffer pos))
  8737. (if (get-buffer-window (marker-buffer pos))
  8738. (select-window (get-buffer-window (marker-buffer pos)))
  8739. (switch-to-buffer-other-window (get-buffer-window
  8740. (marker-buffer pos)))))
  8741. (goto-char pos)
  8742. (org-table-force-dataline)
  8743. (when dest
  8744. (setq name (substring dest 1))
  8745. (cond
  8746. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  8747. (setq e (assoc name org-table-named-field-locations))
  8748. (goto-line (nth 1 e))
  8749. (org-table-goto-column (nth 2 e)))
  8750. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  8751. (let ((l (string-to-number (match-string 1 dest)))
  8752. (c (string-to-number (match-string 2 dest))))
  8753. (goto-line (aref org-table-dlines l))
  8754. (org-table-goto-column c)))
  8755. (t (org-table-goto-column (string-to-number name))))
  8756. (move-marker pos (point))
  8757. (org-table-highlight-rectangle nil nil face2))
  8758. (cond
  8759. ((equal dest match))
  8760. ((not match))
  8761. ((eq what 'range)
  8762. (condition-case nil
  8763. (save-excursion
  8764. (org-table-get-range match nil nil 'highlight))
  8765. (error nil)))
  8766. ((setq e (assoc var org-table-named-field-locations))
  8767. (goto-line (nth 1 e))
  8768. (org-table-goto-column (nth 2 e))
  8769. (org-table-highlight-rectangle (point) (point))
  8770. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  8771. ((setq e (assoc var org-table-column-names))
  8772. (org-table-goto-column (string-to-number (cdr e)))
  8773. (org-table-highlight-rectangle (point) (point))
  8774. (goto-char (org-table-begin))
  8775. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  8776. (org-table-end) t)
  8777. (progn
  8778. (goto-char (match-beginning 1))
  8779. (org-table-highlight-rectangle)
  8780. (message "Named column (column %s)" (cdr e)))
  8781. (error "Column name not found")))
  8782. ((eq what 'column)
  8783. ;; column number
  8784. (org-table-goto-column (string-to-number (substring match 1)))
  8785. (org-table-highlight-rectangle (point) (point))
  8786. (message "Column %s" (substring match 1)))
  8787. ((setq e (assoc var org-table-local-parameters))
  8788. (goto-char (org-table-begin))
  8789. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  8790. (progn
  8791. (goto-char (match-beginning 1))
  8792. (org-table-highlight-rectangle)
  8793. (message "Local parameter."))
  8794. (error "Parameter not found")))
  8795. (t
  8796. (cond
  8797. ((not var) (error "No reference at point"))
  8798. ((setq e (assoc var org-table-formula-constants-local))
  8799. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  8800. var (cdr e)))
  8801. ((setq e (assoc var org-table-formula-constants))
  8802. (message "Constant: $%s=%s in `org-table-formula-constants'."
  8803. var (cdr e)))
  8804. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  8805. (message "Constant: $%s=%s, from `constants.el'%s."
  8806. var e (format " (%s units)" constants-unit-system)))
  8807. (t (error "Undefined name $%s" var)))))
  8808. (goto-char pos)
  8809. (when (and org-show-positions
  8810. (not (memq this-command '(org-table-fedit-scroll
  8811. org-table-fedit-scroll-down))))
  8812. (push pos org-show-positions)
  8813. (push org-table-current-begin-pos org-show-positions)
  8814. (let ((min (apply 'min org-show-positions))
  8815. (max (apply 'max org-show-positions)))
  8816. (goto-char min) (recenter 0)
  8817. (goto-char max)
  8818. (or (pos-visible-in-window-p max) (recenter -1))))
  8819. (select-window win))))
  8820. (defun org-table-force-dataline ()
  8821. "Make sure the cursor is in a dataline in a table."
  8822. (unless (save-excursion
  8823. (beginning-of-line 1)
  8824. (looking-at org-table-dataline-regexp))
  8825. (let* ((re org-table-dataline-regexp)
  8826. (p1 (save-excursion (re-search-forward re nil 'move)))
  8827. (p2 (save-excursion (re-search-backward re nil 'move))))
  8828. (cond ((and p1 p2)
  8829. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  8830. p1 p2)))
  8831. ((or p1 p2) (goto-char (or p1 p2)))
  8832. (t (error "No table dataline around here"))))))
  8833. (defun org-table-fedit-line-up ()
  8834. "Move cursor one line up in the window showing the table."
  8835. (interactive)
  8836. (org-table-fedit-move 'previous-line))
  8837. (defun org-table-fedit-line-down ()
  8838. "Move cursor one line down in the window showing the table."
  8839. (interactive)
  8840. (org-table-fedit-move 'next-line))
  8841. (defun org-table-fedit-move (command)
  8842. "Move the cursor in the window shoinw the table.
  8843. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  8844. (let ((org-table-allow-automatic-line-recalculation nil)
  8845. (pos org-pos) (win (selected-window)) p)
  8846. (select-window (get-buffer-window (marker-buffer org-pos)))
  8847. (setq p (point))
  8848. (call-interactively command)
  8849. (while (and (org-at-table-p)
  8850. (org-at-table-hline-p))
  8851. (call-interactively command))
  8852. (or (org-at-table-p) (goto-char p))
  8853. (move-marker pos (point))
  8854. (select-window win)))
  8855. (defun org-table-fedit-scroll (N)
  8856. (interactive "p")
  8857. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  8858. (scroll-other-window N)))
  8859. (defun org-table-fedit-scroll-down (N)
  8860. (interactive "p")
  8861. (org-table-fedit-scroll (- N)))
  8862. (defvar org-table-rectangle-overlays nil)
  8863. (defun org-table-add-rectangle-overlay (beg end &optional face)
  8864. "Add a new overlay."
  8865. (let ((ov (org-make-overlay beg end)))
  8866. (org-overlay-put ov 'face (or face 'secondary-selection))
  8867. (push ov org-table-rectangle-overlays)))
  8868. (defun org-table-highlight-rectangle (&optional beg end face)
  8869. "Highlight rectangular region in a table."
  8870. (setq beg (or beg (point)) end (or end (point)))
  8871. (let ((b (min beg end))
  8872. (e (max beg end))
  8873. l1 c1 l2 c2 tmp)
  8874. (and (boundp 'org-show-positions)
  8875. (setq org-show-positions (cons b (cons e org-show-positions))))
  8876. (goto-char (min beg end))
  8877. (setq l1 (org-current-line)
  8878. c1 (org-table-current-column))
  8879. (goto-char (max beg end))
  8880. (setq l2 (org-current-line)
  8881. c2 (org-table-current-column))
  8882. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  8883. (goto-line l1)
  8884. (beginning-of-line 1)
  8885. (loop for line from l1 to l2 do
  8886. (when (looking-at org-table-dataline-regexp)
  8887. (org-table-goto-column c1)
  8888. (skip-chars-backward "^|\n") (setq beg (point))
  8889. (org-table-goto-column c2)
  8890. (skip-chars-forward "^|\n") (setq end (point))
  8891. (org-table-add-rectangle-overlay beg end face))
  8892. (beginning-of-line 2))
  8893. (goto-char b))
  8894. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  8895. (defun org-table-remove-rectangle-highlight (&rest ignore)
  8896. "Remove the rectangle overlays."
  8897. (unless org-inhibit-highlight-removal
  8898. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  8899. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  8900. (setq org-table-rectangle-overlays nil)))
  8901. (defvar org-table-coordinate-overlays nil
  8902. "Collects the cooordinate grid overlays, so that they can be removed.")
  8903. (make-variable-buffer-local 'org-table-coordinate-overlays)
  8904. (defun org-table-overlay-coordinates ()
  8905. "Add overlays to the table at point, to show row/column coordinates."
  8906. (interactive)
  8907. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8908. (setq org-table-coordinate-overlays nil)
  8909. (save-excursion
  8910. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  8911. (goto-char (org-table-begin))
  8912. (while (org-at-table-p)
  8913. (setq eol (point-at-eol))
  8914. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  8915. (push ov org-table-coordinate-overlays)
  8916. (setq hline (looking-at org-table-hline-regexp))
  8917. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  8918. (format "%4d" (setq id (1+ id)))))
  8919. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  8920. (when hline
  8921. (setq ic 0)
  8922. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  8923. (setq beg (1+ (match-beginning 0))
  8924. ic (1+ ic)
  8925. s1 (concat "$" (int-to-string ic))
  8926. s2 (org-number-to-letters ic)
  8927. str (if (eq org-table-use-standard-references t) s2 s1))
  8928. (setq ov (org-make-overlay beg (+ beg (length str))))
  8929. (push ov org-table-coordinate-overlays)
  8930. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  8931. (beginning-of-line 2)))))
  8932. (defun org-table-toggle-coordinate-overlays ()
  8933. "Toggle the display of Row/Column numbers in tables."
  8934. (interactive)
  8935. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  8936. (message "Row/Column number display turned %s"
  8937. (if org-table-overlay-coordinates "on" "off"))
  8938. (if (and (org-at-table-p) org-table-overlay-coordinates)
  8939. (org-table-align))
  8940. (unless org-table-overlay-coordinates
  8941. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  8942. (setq org-table-coordinate-overlays nil)))
  8943. (defun org-table-toggle-formula-debugger ()
  8944. "Toggle the formula debugger in tables."
  8945. (interactive)
  8946. (setq org-table-formula-debug (not org-table-formula-debug))
  8947. (message "Formula debugging has been turned %s"
  8948. (if org-table-formula-debug "on" "off")))
  8949. ;;; The orgtbl minor mode
  8950. ;; Define a minor mode which can be used in other modes in order to
  8951. ;; integrate the org-mode table editor.
  8952. ;; This is really a hack, because the org-mode table editor uses several
  8953. ;; keys which normally belong to the major mode, for example the TAB and
  8954. ;; RET keys. Here is how it works: The minor mode defines all the keys
  8955. ;; necessary to operate the table editor, but wraps the commands into a
  8956. ;; function which tests if the cursor is currently inside a table. If that
  8957. ;; is the case, the table editor command is executed. However, when any of
  8958. ;; those keys is used outside a table, the function uses `key-binding' to
  8959. ;; look up if the key has an associated command in another currently active
  8960. ;; keymap (minor modes, major mode, global), and executes that command.
  8961. ;; There might be problems if any of the keys used by the table editor is
  8962. ;; otherwise used as a prefix key.
  8963. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  8964. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  8965. ;; addresses this by checking explicitly for both bindings.
  8966. ;; The optimized version (see variable `orgtbl-optimized') takes over
  8967. ;; all keys which are bound to `self-insert-command' in the *global map*.
  8968. ;; Some modes bind other commands to simple characters, for example
  8969. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  8970. ;; active, this binding is ignored inside tables and replaced with a
  8971. ;; modified self-insert.
  8972. (defvar orgtbl-mode nil
  8973. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  8974. table editor in arbitrary modes.")
  8975. (make-variable-buffer-local 'orgtbl-mode)
  8976. (defvar orgtbl-mode-map (make-keymap)
  8977. "Keymap for `orgtbl-mode'.")
  8978. ;;;###autoload
  8979. (defun turn-on-orgtbl ()
  8980. "Unconditionally turn on `orgtbl-mode'."
  8981. (orgtbl-mode 1))
  8982. (defvar org-old-auto-fill-inhibit-regexp nil
  8983. "Local variable used by `orgtbl-mode'")
  8984. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  8985. "Matches a line belonging to an orgtbl.")
  8986. (defconst orgtbl-extra-font-lock-keywords
  8987. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  8988. 0 (quote 'org-table) 'prepend))
  8989. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  8990. ;;;###autoload
  8991. (defun orgtbl-mode (&optional arg)
  8992. "The `org-mode' table editor as a minor mode for use in other modes."
  8993. (interactive)
  8994. (if (org-mode-p)
  8995. ;; Exit without error, in case some hook functions calls this
  8996. ;; by accident in org-mode.
  8997. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  8998. (setq orgtbl-mode
  8999. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9000. (if orgtbl-mode
  9001. (progn
  9002. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9003. ;; Make sure we are first in minor-mode-map-alist
  9004. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9005. (and c (setq minor-mode-map-alist
  9006. (cons c (delq c minor-mode-map-alist)))))
  9007. (org-set-local (quote org-table-may-need-update) t)
  9008. (org-add-hook 'before-change-functions 'org-before-change-function
  9009. nil 'local)
  9010. (org-set-local 'org-old-auto-fill-inhibit-regexp
  9011. auto-fill-inhibit-regexp)
  9012. (org-set-local 'auto-fill-inhibit-regexp
  9013. (if auto-fill-inhibit-regexp
  9014. (concat orgtbl-line-start-regexp "\\|"
  9015. auto-fill-inhibit-regexp)
  9016. orgtbl-line-start-regexp))
  9017. (org-add-to-invisibility-spec '(org-cwidth))
  9018. (when (fboundp 'font-lock-add-keywords)
  9019. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  9020. (org-restart-font-lock))
  9021. (easy-menu-add orgtbl-mode-menu)
  9022. (run-hooks 'orgtbl-mode-hook))
  9023. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9024. (org-cleanup-narrow-column-properties)
  9025. (org-remove-from-invisibility-spec '(org-cwidth))
  9026. (remove-hook 'before-change-functions 'org-before-change-function t)
  9027. (when (fboundp 'font-lock-remove-keywords)
  9028. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  9029. (org-restart-font-lock))
  9030. (easy-menu-remove orgtbl-mode-menu)
  9031. (force-mode-line-update 'all))))
  9032. (defun org-cleanup-narrow-column-properties ()
  9033. "Remove all properties related to narrow-column invisibility."
  9034. (let ((s 1))
  9035. (while (setq s (text-property-any s (point-max)
  9036. 'display org-narrow-column-arrow))
  9037. (remove-text-properties s (1+ s) '(display t)))
  9038. (setq s 1)
  9039. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9040. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9041. (setq s 1)
  9042. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9043. (remove-text-properties s (1+ s) '(invisible t)))))
  9044. ;; Install it as a minor mode.
  9045. (put 'orgtbl-mode :included t)
  9046. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9047. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9048. (defun orgtbl-make-binding (fun n &rest keys)
  9049. "Create a function for binding in the table minor mode.
  9050. FUN is the command to call inside a table. N is used to create a unique
  9051. command name. KEYS are keys that should be checked in for a command
  9052. to execute outside of tables."
  9053. (eval
  9054. (list 'defun
  9055. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9056. '(arg)
  9057. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9058. "Outside of tables, run the binding of `"
  9059. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9060. "'.")
  9061. '(interactive "p")
  9062. (list 'if
  9063. '(org-at-table-p)
  9064. (list 'call-interactively (list 'quote fun))
  9065. (list 'let '(orgtbl-mode)
  9066. (list 'call-interactively
  9067. (append '(or)
  9068. (mapcar (lambda (k)
  9069. (list 'key-binding k))
  9070. keys)
  9071. '('orgtbl-error))))))))
  9072. (defun orgtbl-error ()
  9073. "Error when there is no default binding for a table key."
  9074. (interactive)
  9075. (error "This key is has no function outside tables"))
  9076. (defun orgtbl-setup ()
  9077. "Setup orgtbl keymaps."
  9078. (let ((nfunc 0)
  9079. (bindings
  9080. (list
  9081. '([(meta shift left)] org-table-delete-column)
  9082. '([(meta left)] org-table-move-column-left)
  9083. '([(meta right)] org-table-move-column-right)
  9084. '([(meta shift right)] org-table-insert-column)
  9085. '([(meta shift up)] org-table-kill-row)
  9086. '([(meta shift down)] org-table-insert-row)
  9087. '([(meta up)] org-table-move-row-up)
  9088. '([(meta down)] org-table-move-row-down)
  9089. '("\C-c\C-w" org-table-cut-region)
  9090. '("\C-c\M-w" org-table-copy-region)
  9091. '("\C-c\C-y" org-table-paste-rectangle)
  9092. '("\C-c-" org-table-insert-hline)
  9093. '("\C-c}" org-table-toggle-coordinate-overlays)
  9094. '("\C-c{" org-table-toggle-formula-debugger)
  9095. '("\C-m" org-table-next-row)
  9096. '([(shift return)] org-table-copy-down)
  9097. '("\C-c\C-q" org-table-wrap-region)
  9098. '("\C-c?" org-table-field-info)
  9099. '("\C-c " org-table-blank-field)
  9100. '("\C-c+" org-table-sum)
  9101. '("\C-c=" org-table-eval-formula)
  9102. '("\C-c'" org-table-edit-formulas)
  9103. '("\C-c`" org-table-edit-field)
  9104. '("\C-c*" org-table-recalculate)
  9105. '("\C-c|" org-table-create-or-convert-from-region)
  9106. '("\C-c^" org-table-sort-lines)
  9107. '([(control ?#)] org-table-rotate-recalc-marks)))
  9108. elt key fun cmd)
  9109. (while (setq elt (pop bindings))
  9110. (setq nfunc (1+ nfunc))
  9111. (setq key (org-key (car elt))
  9112. fun (nth 1 elt)
  9113. cmd (orgtbl-make-binding fun nfunc key))
  9114. (org-defkey orgtbl-mode-map key cmd))
  9115. ;; Special treatment needed for TAB and RET
  9116. (org-defkey orgtbl-mode-map [(return)]
  9117. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9118. (org-defkey orgtbl-mode-map "\C-m"
  9119. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9120. (org-defkey orgtbl-mode-map [(tab)]
  9121. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9122. (org-defkey orgtbl-mode-map "\C-i"
  9123. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  9124. (org-defkey orgtbl-mode-map [(shift tab)]
  9125. (orgtbl-make-binding 'org-table-previous-field 104
  9126. [(shift tab)] [(tab)] "\C-i"))
  9127. (org-defkey orgtbl-mode-map "\M-\C-m"
  9128. (orgtbl-make-binding 'org-table-wrap-region 105
  9129. "\M-\C-m" [(meta return)]))
  9130. (org-defkey orgtbl-mode-map [(meta return)]
  9131. (orgtbl-make-binding 'org-table-wrap-region 106
  9132. [(meta return)] "\M-\C-m"))
  9133. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  9134. (when orgtbl-optimized
  9135. ;; If the user wants maximum table support, we need to hijack
  9136. ;; some standard editing functions
  9137. (org-remap orgtbl-mode-map
  9138. 'self-insert-command 'orgtbl-self-insert-command
  9139. 'delete-char 'org-delete-char
  9140. 'delete-backward-char 'org-delete-backward-char)
  9141. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  9142. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9143. '("OrgTbl"
  9144. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9145. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9146. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9147. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9148. "--"
  9149. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9150. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9151. ["Copy Field from Above"
  9152. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9153. "--"
  9154. ("Column"
  9155. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9156. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9157. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9158. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9159. ("Row"
  9160. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9161. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9162. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9163. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9164. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9165. "--"
  9166. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9167. ("Rectangle"
  9168. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9169. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9170. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9171. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9172. "--"
  9173. ("Radio tables"
  9174. ["Insert table template" orgtbl-insert-radio-table
  9175. (assq major-mode orgtbl-radio-table-templates)]
  9176. ["Comment/uncomment table" orgtbl-toggle-comment t])
  9177. "--"
  9178. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9179. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9180. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9181. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9182. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9183. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9184. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9185. ["Sum Column/Rectangle" org-table-sum
  9186. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9187. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9188. ["Debug Formulas"
  9189. org-table-toggle-formula-debugger :active (org-at-table-p)
  9190. :keys "C-c {"
  9191. :style toggle :selected org-table-formula-debug]
  9192. ["Show Col/Row Numbers"
  9193. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9194. :keys "C-c }"
  9195. :style toggle :selected org-table-overlay-coordinates]
  9196. ))
  9197. t))
  9198. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9199. "If the cursor is inside a table, realign the table.
  9200. It it is a table to be sent away to a receiver, do it.
  9201. With prefix arg, also recompute table."
  9202. (interactive "P")
  9203. (let ((pos (point)) action)
  9204. (save-excursion
  9205. (beginning-of-line 1)
  9206. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9207. ((looking-at "[ \t]*|") pos)
  9208. ((looking-at "#\\+TBLFM:") 'recalc))))
  9209. (cond
  9210. ((integerp action)
  9211. (goto-char action)
  9212. (org-table-maybe-eval-formula)
  9213. (if arg
  9214. (call-interactively 'org-table-recalculate)
  9215. (org-table-maybe-recalculate-line))
  9216. (call-interactively 'org-table-align)
  9217. (orgtbl-send-table 'maybe))
  9218. ((eq action 'recalc)
  9219. (save-excursion
  9220. (beginning-of-line 1)
  9221. (skip-chars-backward " \r\n\t")
  9222. (if (org-at-table-p)
  9223. (org-call-with-arg 'org-table-recalculate t))))
  9224. (t (let (orgtbl-mode)
  9225. (call-interactively (key-binding "\C-c\C-c")))))))
  9226. (defun orgtbl-tab (arg)
  9227. "Justification and field motion for `orgtbl-mode'."
  9228. (interactive "P")
  9229. (if arg (org-table-edit-field t)
  9230. (org-table-justify-field-maybe)
  9231. (org-table-next-field)))
  9232. (defun orgtbl-ret ()
  9233. "Justification and field motion for `orgtbl-mode'."
  9234. (interactive)
  9235. (org-table-justify-field-maybe)
  9236. (org-table-next-row))
  9237. (defun orgtbl-self-insert-command (N)
  9238. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9239. If the cursor is in a table looking at whitespace, the whitespace is
  9240. overwritten, and the table is not marked as requiring realignment."
  9241. (interactive "p")
  9242. (if (and (org-at-table-p)
  9243. (or
  9244. (and org-table-auto-blank-field
  9245. (member last-command
  9246. '(orgtbl-hijacker-command-100
  9247. orgtbl-hijacker-command-101
  9248. orgtbl-hijacker-command-102
  9249. orgtbl-hijacker-command-103
  9250. orgtbl-hijacker-command-104
  9251. orgtbl-hijacker-command-105))
  9252. (org-table-blank-field))
  9253. t)
  9254. (eq N 1)
  9255. (looking-at "[^|\n]* +|"))
  9256. (let (org-table-may-need-update)
  9257. (goto-char (1- (match-end 0)))
  9258. (delete-backward-char 1)
  9259. (goto-char (match-beginning 0))
  9260. (self-insert-command N))
  9261. (setq org-table-may-need-update t)
  9262. (let (orgtbl-mode)
  9263. (call-interactively (key-binding (vector last-input-event))))))
  9264. (defun org-force-self-insert (N)
  9265. "Needed to enforce self-insert under remapping."
  9266. (interactive "p")
  9267. (self-insert-command N))
  9268. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9269. "Regula expression matching exponentials as produced by calc.")
  9270. (defvar org-table-clean-did-remove-column nil)
  9271. (defun orgtbl-export (table target)
  9272. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9273. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9274. org-table-last-alignment org-table-last-column-widths
  9275. maxcol column)
  9276. (if (not (fboundp func))
  9277. (error "Cannot export orgtbl table to %s" target))
  9278. (setq lines (org-table-clean-before-export lines))
  9279. (setq table
  9280. (mapcar
  9281. (lambda (x)
  9282. (if (string-match org-table-hline-regexp x)
  9283. 'hline
  9284. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9285. lines))
  9286. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9287. table)))
  9288. (loop for i from (1- maxcol) downto 0 do
  9289. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9290. (setq column (delq nil column))
  9291. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9292. (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))
  9293. (funcall func table nil)))
  9294. (defun orgtbl-send-table (&optional maybe)
  9295. "Send a tranformed version of this table to the receiver position.
  9296. With argument MAYBE, fail quietly if no transformation is defined for
  9297. this table."
  9298. (interactive)
  9299. (catch 'exit
  9300. (unless (org-at-table-p) (error "Not at a table"))
  9301. ;; when non-interactive, we assume align has just happened.
  9302. (when (interactive-p) (org-table-align))
  9303. (save-excursion
  9304. (goto-char (org-table-begin))
  9305. (beginning-of-line 0)
  9306. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9307. (if maybe
  9308. (throw 'exit nil)
  9309. (error "Don't know how to transform this table."))))
  9310. (let* ((name (match-string 1))
  9311. beg
  9312. (transform (intern (match-string 2)))
  9313. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9314. (skip (plist-get params :skip))
  9315. (skipcols (plist-get params :skipcols))
  9316. (txt (buffer-substring-no-properties
  9317. (org-table-begin) (org-table-end)))
  9318. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9319. (lines (org-table-clean-before-export lines))
  9320. (i0 (if org-table-clean-did-remove-column 2 1))
  9321. (table (mapcar
  9322. (lambda (x)
  9323. (if (string-match org-table-hline-regexp x)
  9324. 'hline
  9325. (org-remove-by-index
  9326. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9327. skipcols i0)))
  9328. lines))
  9329. (fun (if (= i0 2) 'cdr 'identity))
  9330. (org-table-last-alignment
  9331. (org-remove-by-index (funcall fun org-table-last-alignment)
  9332. skipcols i0))
  9333. (org-table-last-column-widths
  9334. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9335. skipcols i0)))
  9336. (unless (fboundp transform)
  9337. (error "No such transformation function %s" transform))
  9338. (setq txt (funcall transform table params))
  9339. ;; Find the insertion place
  9340. (save-excursion
  9341. (goto-char (point-min))
  9342. (unless (re-search-forward
  9343. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9344. (error "Don't know where to insert translated table"))
  9345. (goto-char (match-beginning 0))
  9346. (beginning-of-line 2)
  9347. (setq beg (point))
  9348. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9349. (error "Cannot find end of insertion region"))
  9350. (beginning-of-line 1)
  9351. (delete-region beg (point))
  9352. (goto-char beg)
  9353. (insert txt "\n"))
  9354. (message "Table converted and installed at receiver location"))))
  9355. (defun org-remove-by-index (list indices &optional i0)
  9356. "Remove the elements in LIST with indices in INDICES.
  9357. First element has index 0, or I0 if given."
  9358. (if (not indices)
  9359. list
  9360. (if (integerp indices) (setq indices (list indices)))
  9361. (setq i0 (1- (or i0 0)))
  9362. (delq :rm (mapcar (lambda (x)
  9363. (setq i0 (1+ i0))
  9364. (if (memq i0 indices) :rm x))
  9365. list))))
  9366. (defun orgtbl-toggle-comment ()
  9367. "Comment or uncomment the orgtbl at point."
  9368. (interactive)
  9369. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9370. (re2 (concat "^" orgtbl-line-start-regexp))
  9371. (commented (save-excursion (beginning-of-line 1)
  9372. (cond ((looking-at re1) t)
  9373. ((looking-at re2) nil)
  9374. (t (error "Not at an org table")))))
  9375. (re (if commented re1 re2))
  9376. beg end)
  9377. (save-excursion
  9378. (beginning-of-line 1)
  9379. (while (looking-at re) (beginning-of-line 0))
  9380. (beginning-of-line 2)
  9381. (setq beg (point))
  9382. (while (looking-at re) (beginning-of-line 2))
  9383. (setq end (point)))
  9384. (comment-region beg end (if commented '(4) nil))))
  9385. (defun orgtbl-insert-radio-table ()
  9386. "Insert a radio table template appropriate for this major mode."
  9387. (interactive)
  9388. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9389. (txt (nth 1 e))
  9390. name pos)
  9391. (unless e (error "No radio table setup defined for %s" major-mode))
  9392. (setq name (read-string "Table name: "))
  9393. (while (string-match "%n" txt)
  9394. (setq txt (replace-match name t t txt)))
  9395. (or (bolp) (insert "\n"))
  9396. (setq pos (point))
  9397. (insert txt)
  9398. (goto-char pos)))
  9399. (defun org-get-param (params header i sym &optional hsym)
  9400. "Get parameter value for symbol SYM.
  9401. If this is a header line, actually get the value for the symbol with an
  9402. additional \"h\" inserted after the colon.
  9403. If the value is a protperty list, get the element for the current column.
  9404. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9405. (let ((val (plist-get params sym)))
  9406. (and hsym header (setq val (or (plist-get params hsym) val)))
  9407. (if (consp val) (plist-get val i) val)))
  9408. (defun orgtbl-to-generic (table params)
  9409. "Convert the orgtbl-mode TABLE to some other format.
  9410. This generic routine can be used for many standard cases.
  9411. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9412. separator line, or a list of fields for that line.
  9413. PARAMS is a property list of parameters that can influence the conversion.
  9414. For the generic converter, some parameters are obligatory: You need to
  9415. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9416. :splice, you must have :tstart and :tend.
  9417. Valid parameters are
  9418. :tstart String to start the table. Ignored when :splice is t.
  9419. :tend String to end the table. Ignored when :splice is t.
  9420. :splice When set to t, return only table body lines, don't wrap
  9421. them into :tstart and :tend. Default is nil.
  9422. :hline String to be inserted on horizontal separation lines.
  9423. May be nil to ignore hlines.
  9424. :lstart String to start a new table line.
  9425. :lend String to end a table line
  9426. :sep Separator between two fields
  9427. :lfmt Format for entire line, with enough %s to capture all fields.
  9428. If this is present, :lstart, :lend, and :sep are ignored.
  9429. :fmt A format to be used to wrap the field, should contain
  9430. %s for the original field value. For example, to wrap
  9431. everything in dollars, you could use :fmt \"$%s$\".
  9432. This may also be a property list with column numbers and
  9433. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9434. :hlstart :hlend :hlsep :hlfmt :hfmt
  9435. Same as above, specific for the header lines in the table.
  9436. All lines before the first hline are treated as header.
  9437. If any of these is not present, the data line value is used.
  9438. :efmt Use this format to print numbers with exponentials.
  9439. The format should have %s twice for inserting mantissa
  9440. and exponent, for example \"%s\\\\times10^{%s}\". This
  9441. may also be a property list with column numbers and
  9442. formats. :fmt will still be applied after :efmt.
  9443. In addition to this, the parameters :skip and :skipcols are always handled
  9444. directly by `orgtbl-send-table'. See manual."
  9445. (interactive)
  9446. (let* ((p params)
  9447. (splicep (plist-get p :splice))
  9448. (hline (plist-get p :hline))
  9449. rtn line i fm efm lfmt h)
  9450. ;; Do we have a header?
  9451. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  9452. (setq h t))
  9453. ;; Put header
  9454. (unless splicep
  9455. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  9456. ;; Now loop over all lines
  9457. (while (setq line (pop table))
  9458. (if (eq line 'hline)
  9459. ;; A horizontal separator line
  9460. (progn (if hline (push hline rtn))
  9461. (setq h nil)) ; no longer in header
  9462. ;; A normal line. Convert the fields, push line onto the result list
  9463. (setq i 0)
  9464. (setq line
  9465. (mapcar
  9466. (lambda (f)
  9467. (setq i (1+ i)
  9468. fm (org-get-param p h i :fmt :hfmt)
  9469. efm (org-get-param p h i :efmt))
  9470. (if (and efm (string-match orgtbl-exp-regexp f))
  9471. (setq f (format
  9472. efm (match-string 1 f) (match-string 2 f))))
  9473. (if fm (setq f (format fm f)))
  9474. f)
  9475. line))
  9476. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  9477. (push (apply 'format lfmt line) rtn)
  9478. (push (concat
  9479. (org-get-param p h i :lstart :hlstart)
  9480. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  9481. (org-get-param p h i :lend :hlend))
  9482. rtn))))
  9483. (unless splicep
  9484. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  9485. (mapconcat 'identity (nreverse rtn) "\n")))
  9486. (defun orgtbl-to-latex (table params)
  9487. "Convert the orgtbl-mode TABLE to LaTeX.
  9488. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9489. separator line, or a list of fields for that line.
  9490. PARAMS is a property list of parameters that can influence the conversion.
  9491. Supports all parameters from `orgtbl-to-generic'. Most important for
  9492. LaTeX are:
  9493. :splice When set to t, return only table body lines, don't wrap
  9494. them into a tabular environment. Default is nil.
  9495. :fmt A format to be used to wrap the field, should contain %s for the
  9496. original field value. For example, to wrap everything in dollars,
  9497. use :fmt \"$%s$\". This may also be a property list with column
  9498. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9499. :efmt Format for transforming numbers with exponentials. The format
  9500. should have %s twice for inserting mantissa and exponent, for
  9501. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  9502. This may also be a property list with column numbers and formats.
  9503. The general parameters :skip and :skipcols have already been applied when
  9504. this function is called."
  9505. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  9506. org-table-last-alignment ""))
  9507. (params2
  9508. (list
  9509. :tstart (concat "\\begin{tabular}{" alignment "}")
  9510. :tend "\\end{tabular}"
  9511. :lstart "" :lend " \\\\" :sep " & "
  9512. :efmt "%s\\,(%s)" :hline "\\hline")))
  9513. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9514. (defun orgtbl-to-html (table params)
  9515. "Convert the orgtbl-mode TABLE to LaTeX.
  9516. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9517. separator line, or a list of fields for that line.
  9518. PARAMS is a property list of parameters that can influence the conversion.
  9519. Currently this function recognizes the following parameters:
  9520. :splice When set to t, return only table body lines, don't wrap
  9521. them into a <table> environment. Default is nil.
  9522. The general parameters :skip and :skipcols have already been applied when
  9523. this function is called. The function does *not* use `orgtbl-to-generic',
  9524. so you cannot specify parameters for it."
  9525. (let* ((splicep (plist-get params :splice))
  9526. html)
  9527. ;; Just call the formatter we already have
  9528. ;; We need to make text lines for it, so put the fields back together.
  9529. (setq html (org-format-org-table-html
  9530. (mapcar
  9531. (lambda (x)
  9532. (if (eq x 'hline)
  9533. "|----+----|"
  9534. (concat "| " (mapconcat 'identity x " | ") " |")))
  9535. table)
  9536. splicep))
  9537. (if (string-match "\n+\\'" html)
  9538. (setq html (replace-match "" t t html)))
  9539. html))
  9540. (defun orgtbl-to-texinfo (table params)
  9541. "Convert the orgtbl-mode TABLE to TeXInfo.
  9542. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9543. separator line, or a list of fields for that line.
  9544. PARAMS is a property list of parameters that can influence the conversion.
  9545. Supports all parameters from `orgtbl-to-generic'. Most important for
  9546. TeXInfo are:
  9547. :splice nil/t When set to t, return only table body lines, don't wrap
  9548. them into a multitable environment. Default is nil.
  9549. :fmt fmt A format to be used to wrap the field, should contain
  9550. %s for the original field value. For example, to wrap
  9551. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  9552. This may also be a property list with column numbers and
  9553. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  9554. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  9555. are computed automatically from the width of the columns
  9556. under org-mode.
  9557. The general parameters :skip and :skipcols have already been applied when
  9558. this function is called."
  9559. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9560. (colfrac (or (plist-get params :cf)
  9561. (mapconcat
  9562. (lambda (x) (format "%.3f" (/ (float x) total)))
  9563. org-table-last-column-widths " ")))
  9564. (params2
  9565. (list
  9566. :tstart (concat "@multitable @columnfractions " colfrac)
  9567. :tend "@end multitable"
  9568. :lstart "@item " :lend "" :sep " @tab "
  9569. :hlstart "@headitem ")))
  9570. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9571. ;;;; Link Stuff
  9572. ;;; Link abbreviations
  9573. (defun org-link-expand-abbrev (link)
  9574. "Apply replacements as defined in `org-link-abbrev-alist."
  9575. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  9576. (let* ((key (match-string 1 link))
  9577. (as (or (assoc key org-link-abbrev-alist-local)
  9578. (assoc key org-link-abbrev-alist)))
  9579. (tag (and (match-end 2) (match-string 3 link)))
  9580. rpl)
  9581. (if (not as)
  9582. link
  9583. (setq rpl (cdr as))
  9584. (cond
  9585. ((symbolp rpl) (funcall rpl tag))
  9586. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9587. (t (concat rpl tag)))))
  9588. link))
  9589. ;;; Storing and inserting links
  9590. (defvar org-insert-link-history nil
  9591. "Minibuffer history for links inserted with `org-insert-link'.")
  9592. (defvar org-stored-links nil
  9593. "Contains the links stored with `org-store-link'.")
  9594. (defvar org-store-link-plist nil
  9595. "Plist with info about the most recently link created with `org-store-link'.")
  9596. (defvar org-link-protocols nil
  9597. "Link protocols added to Org-mode using `org-add-link-type'.")
  9598. (defvar org-store-link-functions nil
  9599. "List of functions that are called to create and store a link.
  9600. Each function will be called in turn until one returns a non-nil
  9601. value. Each function should check if it is responsible for creating
  9602. this link (for example by looking at the major mode).
  9603. If not, it must exit and return nil.
  9604. If yes, it should return a non-nil value after a calling
  9605. `org-store-link-properties' with a list of properties and values.
  9606. Special properties are:
  9607. :type The link prefix. like \"http\". This must be given.
  9608. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  9609. This is obligatory as well.
  9610. :description Optional default description for the second pair
  9611. of brackets in an Org-mode link. The user can still change
  9612. this when inserting this link into an Org-mode buffer.
  9613. In addition to these, any additional properties can be specified
  9614. and then used in remember templates.")
  9615. (defun org-add-link-type (type &optional follow publish)
  9616. "Add TYPE to the list of `org-link-types'.
  9617. Re-compute all regular expressions depending on `org-link-types'
  9618. FOLLOW and PUBLISH are two functions. Both take the link path as
  9619. an argument.
  9620. FOLLOW should do whatever is necessary to follow the link, for example
  9621. to find a file or display a mail message.
  9622. PUBLISH takes the path and retuns the string that should be used when
  9623. this document is published."
  9624. (add-to-list 'org-link-types type t)
  9625. (org-make-link-regexps)
  9626. (add-to-list 'org-link-protocols
  9627. (list type follow publish)))
  9628. (defun org-add-agenda-custom-command (entry)
  9629. "Replace or add a command in `org-agenda-custom-commands'.
  9630. This is mostly for hacking and trying a new command - once the command
  9631. works you probably want to add it to `org-agenda-custom-commands' for good."
  9632. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  9633. (if ass
  9634. (setcdr ass (cdr entry))
  9635. (push entry org-agenda-custom-commands))))
  9636. ;;;###autoload
  9637. (defun org-store-link (arg)
  9638. "\\<org-mode-map>Store an org-link to the current location.
  9639. This link can later be inserted into an org-buffer with
  9640. \\[org-insert-link].
  9641. For some link types, a prefix arg is interpreted:
  9642. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9643. For file links, arg negates `org-context-in-file-links'."
  9644. (interactive "P")
  9645. (setq org-store-link-plist nil) ; reset
  9646. (let (link cpltxt desc description search txt)
  9647. (cond
  9648. ((run-hook-with-args-until-success 'org-store-link-functions)
  9649. (setq link (plist-get org-store-link-plist :link)
  9650. desc (or (plist-get org-store-link-plist :description) link)))
  9651. ((eq major-mode 'bbdb-mode)
  9652. (let ((name (bbdb-record-name (bbdb-current-record)))
  9653. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  9654. (setq cpltxt (concat "bbdb:" (or name company))
  9655. link (org-make-link cpltxt))
  9656. (org-store-link-props :type "bbdb" :name name :company company)))
  9657. ((eq major-mode 'Info-mode)
  9658. (setq link (org-make-link "info:"
  9659. (file-name-nondirectory Info-current-file)
  9660. ":" Info-current-node))
  9661. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9662. ":" Info-current-node))
  9663. (org-store-link-props :type "info" :file Info-current-file
  9664. :node Info-current-node))
  9665. ((eq major-mode 'calendar-mode)
  9666. (let ((cd (calendar-cursor-to-date)))
  9667. (setq link
  9668. (format-time-string
  9669. (car org-time-stamp-formats)
  9670. (apply 'encode-time
  9671. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9672. nil nil nil))))
  9673. (org-store-link-props :type "calendar" :date cd)))
  9674. ((or (eq major-mode 'vm-summary-mode)
  9675. (eq major-mode 'vm-presentation-mode))
  9676. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9677. (vm-follow-summary-cursor)
  9678. (save-excursion
  9679. (vm-select-folder-buffer)
  9680. (let* ((message (car vm-message-pointer))
  9681. (folder buffer-file-name)
  9682. (subject (vm-su-subject message))
  9683. (to (vm-get-header-contents message "To"))
  9684. (from (vm-get-header-contents message "From"))
  9685. (message-id (vm-su-message-id message)))
  9686. (org-store-link-props :type "vm" :from from :to to :subject subject
  9687. :message-id message-id)
  9688. (setq message-id (org-remove-angle-brackets message-id))
  9689. (setq folder (abbreviate-file-name folder))
  9690. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9691. folder)
  9692. (setq folder (replace-match "" t t folder)))
  9693. (setq cpltxt (org-email-link-description))
  9694. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9695. ((eq major-mode 'wl-summary-mode)
  9696. (let* ((msgnum (wl-summary-message-number))
  9697. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9698. msgnum 'message-id))
  9699. (wl-message-entity
  9700. (if (fboundp 'elmo-message-entity)
  9701. (elmo-message-entity
  9702. wl-summary-buffer-elmo-folder msgnum)
  9703. (elmo-msgdb-overview-get-entity
  9704. msgnum (wl-summary-buffer-msgdb))))
  9705. (from (wl-summary-line-from))
  9706. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  9707. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  9708. (wl-summary-line-subject))))
  9709. (org-store-link-props :type "wl" :from from :to to
  9710. :subject subject :message-id message-id)
  9711. (setq message-id (org-remove-angle-brackets message-id))
  9712. (setq cpltxt (org-email-link-description))
  9713. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9714. "#" message-id))))
  9715. ((or (equal major-mode 'mh-folder-mode)
  9716. (equal major-mode 'mh-show-mode))
  9717. (let ((from (org-mhe-get-header "From:"))
  9718. (to (org-mhe-get-header "To:"))
  9719. (message-id (org-mhe-get-header "Message-Id:"))
  9720. (subject (org-mhe-get-header "Subject:")))
  9721. (org-store-link-props :type "mh" :from from :to to
  9722. :subject subject :message-id message-id)
  9723. (setq cpltxt (org-email-link-description))
  9724. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9725. (org-remove-angle-brackets message-id)))))
  9726. ((eq major-mode 'rmail-mode)
  9727. (save-excursion
  9728. (save-restriction
  9729. (rmail-narrow-to-non-pruned-header)
  9730. (let ((folder buffer-file-name)
  9731. (message-id (mail-fetch-field "message-id"))
  9732. (from (mail-fetch-field "from"))
  9733. (to (mail-fetch-field "to"))
  9734. (subject (mail-fetch-field "subject")))
  9735. (org-store-link-props
  9736. :type "rmail" :from from :to to
  9737. :subject subject :message-id message-id)
  9738. (setq message-id (org-remove-angle-brackets message-id))
  9739. (setq cpltxt (org-email-link-description))
  9740. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9741. ((eq major-mode 'gnus-group-mode)
  9742. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9743. (gnus-group-group-name)) ; version
  9744. ((fboundp 'gnus-group-name)
  9745. (gnus-group-name))
  9746. (t "???"))))
  9747. (unless group (error "Not on a group"))
  9748. (org-store-link-props :type "gnus" :group group)
  9749. (setq cpltxt (concat
  9750. (if (org-xor arg org-usenet-links-prefer-google)
  9751. "http://groups.google.com/groups?group="
  9752. "gnus:")
  9753. group)
  9754. link (org-make-link cpltxt))))
  9755. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9756. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9757. (let* ((group gnus-newsgroup-name)
  9758. (article (gnus-summary-article-number))
  9759. (header (gnus-summary-article-header article))
  9760. (from (mail-header-from header))
  9761. (message-id (mail-header-id header))
  9762. (date (mail-header-date header))
  9763. (subject (gnus-summary-subject-string)))
  9764. (org-store-link-props :type "gnus" :from from :subject subject
  9765. :message-id message-id :group group)
  9766. (setq cpltxt (org-email-link-description))
  9767. (if (org-xor arg org-usenet-links-prefer-google)
  9768. (setq link
  9769. (concat
  9770. cpltxt "\n "
  9771. (format "http://groups.google.com/groups?as_umsgid=%s"
  9772. (org-fixup-message-id-for-http message-id))))
  9773. (setq link (org-make-link "gnus:" group
  9774. "#" (number-to-string article))))))
  9775. ((eq major-mode 'w3-mode)
  9776. (setq cpltxt (url-view-url t)
  9777. link (org-make-link cpltxt))
  9778. (org-store-link-props :type "w3" :url (url-view-url t)))
  9779. ((eq major-mode 'w3m-mode)
  9780. (setq cpltxt (or w3m-current-title w3m-current-url)
  9781. link (org-make-link w3m-current-url))
  9782. (org-store-link-props :type "w3m" :url (url-view-url t)))
  9783. ((setq search (run-hook-with-args-until-success
  9784. 'org-create-file-search-functions))
  9785. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9786. "::" search))
  9787. (setq cpltxt (or description link)))
  9788. ((eq major-mode 'image-mode)
  9789. (setq cpltxt (concat "file:"
  9790. (abbreviate-file-name buffer-file-name))
  9791. link (org-make-link cpltxt))
  9792. (org-store-link-props :type "image" :file buffer-file-name))
  9793. ((eq major-mode 'dired-mode)
  9794. ;; link to the file in the current line
  9795. (setq cpltxt (concat "file:"
  9796. (abbreviate-file-name
  9797. (expand-file-name
  9798. (dired-get-filename nil t))))
  9799. link (org-make-link cpltxt)))
  9800. ((and buffer-file-name (org-mode-p))
  9801. ;; Just link to current headline
  9802. (setq cpltxt (concat "file:"
  9803. (abbreviate-file-name buffer-file-name)))
  9804. ;; Add a context search string
  9805. (when (org-xor org-context-in-file-links arg)
  9806. ;; Check if we are on a target
  9807. (if (org-in-regexp "<<\\(.*?\\)>>")
  9808. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9809. (setq txt (cond
  9810. ((org-on-heading-p) nil)
  9811. ((org-region-active-p)
  9812. (buffer-substring (region-beginning) (region-end)))
  9813. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9814. (when (or (null txt) (string-match "\\S-" txt))
  9815. (setq cpltxt
  9816. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9817. desc "NONE"))))
  9818. (if (string-match "::\\'" cpltxt)
  9819. (setq cpltxt (substring cpltxt 0 -2)))
  9820. (setq link (org-make-link cpltxt)))
  9821. (buffer-file-name
  9822. ;; Just link to this file here.
  9823. (setq cpltxt (concat "file:"
  9824. (abbreviate-file-name buffer-file-name)))
  9825. ;; Add a context string
  9826. (when (org-xor org-context-in-file-links arg)
  9827. (setq txt (if (org-region-active-p)
  9828. (buffer-substring (region-beginning) (region-end))
  9829. (buffer-substring (point-at-bol) (point-at-eol))))
  9830. ;; Only use search option if there is some text.
  9831. (when (string-match "\\S-" txt)
  9832. (setq cpltxt
  9833. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  9834. desc "NONE")))
  9835. (setq link (org-make-link cpltxt)))
  9836. ((interactive-p)
  9837. (error "Cannot link to a buffer which is not visiting a file"))
  9838. (t (setq link nil)))
  9839. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9840. (setq link (or link cpltxt)
  9841. desc (or desc cpltxt))
  9842. (if (equal desc "NONE") (setq desc nil))
  9843. (if (and (interactive-p) link)
  9844. (progn
  9845. (setq org-stored-links
  9846. (cons (list cpltxt link desc) org-stored-links))
  9847. (message "Stored: %s" (or cpltxt link)))
  9848. (org-make-link-string link desc))))
  9849. (defun org-store-link-props (&rest plist)
  9850. "Store link properties, extract names and addresses."
  9851. (let (x adr)
  9852. (when (setq x (plist-get plist :from))
  9853. (setq adr (mail-extract-address-components x))
  9854. (plist-put plist :fromname (car adr))
  9855. (plist-put plist :fromaddress (nth 1 adr)))
  9856. (when (setq x (plist-get plist :to))
  9857. (setq adr (mail-extract-address-components x))
  9858. (plist-put plist :toname (car adr))
  9859. (plist-put plist :toaddress (nth 1 adr))))
  9860. (let ((from (plist-get plist :from))
  9861. (to (plist-get plist :to)))
  9862. (when (and from to org-from-is-user-regexp)
  9863. (plist-put plist :fromto
  9864. (if (string-match org-from-is-user-regexp from)
  9865. (concat "to %t")
  9866. (concat "from %f")))))
  9867. (setq org-store-link-plist plist))
  9868. (defun org-email-link-description (&optional fmt)
  9869. "Return the description part of an email link.
  9870. This takes information from `org-store-link-plist' and formats it
  9871. according to FMT (default from `org-email-link-description-format')."
  9872. (setq fmt (or fmt org-email-link-description-format))
  9873. (let* ((p org-store-link-plist)
  9874. (to (plist-get p :toaddress))
  9875. (from (plist-get p :fromaddress))
  9876. (table
  9877. (list
  9878. (cons "%c" (plist-get p :fromto))
  9879. (cons "%F" (plist-get p :from))
  9880. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9881. (cons "%T" (plist-get p :to))
  9882. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9883. (cons "%s" (plist-get p :subject))
  9884. (cons "%m" (plist-get p :message-id)))))
  9885. (when (string-match "%c" fmt)
  9886. ;; Check if the user wrote this message
  9887. (if (and org-from-is-user-regexp from to
  9888. (save-match-data (string-match org-from-is-user-regexp from)))
  9889. (setq fmt (replace-match "to %t" t t fmt))
  9890. (setq fmt (replace-match "from %f" t t fmt))))
  9891. (org-replace-escapes fmt table)))
  9892. (defun org-make-org-heading-search-string (&optional string heading)
  9893. "Make search string for STRING or current headline."
  9894. (interactive)
  9895. (let ((s (or string (org-get-heading))))
  9896. (unless (and string (not heading))
  9897. ;; We are using a headline, clean up garbage in there.
  9898. (if (string-match org-todo-regexp s)
  9899. (setq s (replace-match "" t t s)))
  9900. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  9901. (setq s (replace-match "" t t s)))
  9902. (setq s (org-trim s))
  9903. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9904. org-comment-string "\\)") s)
  9905. (setq s (replace-match "" t t s)))
  9906. (while (string-match org-ts-regexp s)
  9907. (setq s (replace-match "" t t s))))
  9908. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9909. (setq s (replace-match " " t t s)))
  9910. (or string (setq s (concat "*" s))) ; Add * for headlines
  9911. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9912. (defun org-make-link (&rest strings)
  9913. "Concatenate STRINGS."
  9914. (apply 'concat strings))
  9915. (defun org-make-link-string (link &optional description)
  9916. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9917. (unless (string-match "\\S-" link)
  9918. (error "Empty link"))
  9919. (when (stringp description)
  9920. ;; Remove brackets from the description, they are fatal.
  9921. (while (string-match "\\[\\|\\]" description)
  9922. (setq description (replace-match "" t t description))))
  9923. (when (equal (org-link-escape link) description)
  9924. ;; No description needed, it is identical
  9925. (setq description nil))
  9926. (when (and (not description)
  9927. (not (equal link (org-link-escape link))))
  9928. (setq description link))
  9929. (concat "[[" (org-link-escape link) "]"
  9930. (if description (concat "[" description "]") "")
  9931. "]"))
  9932. (defconst org-link-escape-chars
  9933. '((" " . "%20") ("\340" . "%E0")
  9934. ("\342" . "%E2") ("\347" . "%E7")
  9935. ("\350" . "%E8") ("\351" . "%E9")
  9936. ("\352" . "%EA") ("\356" . "%EE")
  9937. ("\364" . "%F4") ("\371" . "%F9")
  9938. ("\373" . "%FB") (";" . "%3B")
  9939. ("?" . "%3F") ("=" . "%3D")
  9940. ("+" . "%2B"))
  9941. "Association list of escapes for some characters problematic in links.")
  9942. (defun org-link-escape (text)
  9943. "Escape charaters in TEXT that are problematic for links."
  9944. (when text
  9945. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9946. org-link-escape-chars "\\|")))
  9947. (while (string-match re text)
  9948. (setq text
  9949. (replace-match
  9950. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9951. t t text)))
  9952. text)))
  9953. (defun org-link-unescape (text)
  9954. "Reverse the action of `org-link-escape'."
  9955. (when text
  9956. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9957. org-link-escape-chars "\\|")))
  9958. (while (string-match re text)
  9959. (setq text
  9960. (replace-match
  9961. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9962. t t text)))
  9963. text)))
  9964. (defun org-xor (a b)
  9965. "Exclusive or."
  9966. (if a (not b) b))
  9967. (defun org-get-header (header)
  9968. "Find a header field in the current buffer."
  9969. (save-excursion
  9970. (goto-char (point-min))
  9971. (let ((case-fold-search t) s)
  9972. (cond
  9973. ((eq header 'from)
  9974. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9975. (setq s (match-string 1)))
  9976. (while (string-match "\"" s)
  9977. (setq s (replace-match "" t t s)))
  9978. (if (string-match "[<(].*" s)
  9979. (setq s (replace-match "" t t s))))
  9980. ((eq header 'message-id)
  9981. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9982. (setq s (match-string 1))))
  9983. ((eq header 'subject)
  9984. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9985. (setq s (match-string 1)))))
  9986. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9987. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9988. s)))
  9989. (defun org-fixup-message-id-for-http (s)
  9990. "Replace special characters in a message id, so it can be used in an http query."
  9991. (while (string-match "<" s)
  9992. (setq s (replace-match "%3C" t t s)))
  9993. (while (string-match ">" s)
  9994. (setq s (replace-match "%3E" t t s)))
  9995. (while (string-match "@" s)
  9996. (setq s (replace-match "%40" t t s)))
  9997. s)
  9998. ;;;###autoload
  9999. (defun org-insert-link-global ()
  10000. "Insert a link like Org-mode does.
  10001. This command can be called in any mode to insert a link in Org-mode syntax."
  10002. (interactive)
  10003. (org-run-like-in-org-mode 'org-insert-link))
  10004. (defun org-insert-link (&optional complete-file)
  10005. "Insert a link. At the prompt, enter the link.
  10006. Completion can be used to select a link previously stored with
  10007. `org-store-link'. When the empty string is entered (i.e. if you just
  10008. press RET at the prompt), the link defaults to the most recently
  10009. stored link. As SPC triggers completion in the minibuffer, you need to
  10010. use M-SPC or C-q SPC to force the insertion of a space character.
  10011. You will also be prompted for a description, and if one is given, it will
  10012. be displayed in the buffer instead of the link.
  10013. If there is already a link at point, this command will allow you to edit link
  10014. and description parts.
  10015. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10016. selected using completion. The path to the file will be relative to
  10017. the current directory if the file is in the current directory or a
  10018. subdirectory. Otherwise, the link will be the absolute path as
  10019. completed in the minibuffer (i.e. normally ~/path/to/file).
  10020. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10021. is in the current directory or below.
  10022. With three \\[universal-argument] prefixes, negate the meaning of
  10023. `org-keep-stored-link-after-insertion'."
  10024. (interactive "P")
  10025. (let ((wcf (current-window-configuration))
  10026. (region (if (org-region-active-p)
  10027. (prog1 (buffer-substring (region-beginning) (region-end))
  10028. (delete-region (region-beginning) (region-end)))))
  10029. tmphist ; byte-compile incorrectly complains about this
  10030. link desc entry remove file)
  10031. (cond
  10032. ((org-in-regexp org-bracket-link-regexp 1)
  10033. ;; We do have a link at point, and we are going to edit it.
  10034. (setq remove (list (match-beginning 0) (match-end 0)))
  10035. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10036. (setq link (read-string "Link: "
  10037. (org-link-unescape
  10038. (org-match-string-no-properties 1)))))
  10039. ((or (org-in-regexp org-angle-link-re)
  10040. (org-in-regexp org-plain-link-re))
  10041. ;; Convert to bracket link
  10042. (setq remove (list (match-beginning 0) (match-end 0))
  10043. link (read-string "Link: "
  10044. (org-remove-angle-brackets (match-string 0)))))
  10045. ((equal complete-file '(4))
  10046. ;; Completing read for file names.
  10047. (setq file (read-file-name "File: "))
  10048. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10049. (pwd1 (file-name-as-directory (abbreviate-file-name
  10050. (expand-file-name ".")))))
  10051. (cond
  10052. ((equal complete-file '(16))
  10053. (setq link (org-make-link
  10054. "file:"
  10055. (abbreviate-file-name (expand-file-name file)))))
  10056. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10057. (setq link (org-make-link "file:" (match-string 1 file))))
  10058. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10059. (expand-file-name file))
  10060. (setq link (org-make-link
  10061. "file:" (match-string 1 (expand-file-name file)))))
  10062. (t (setq link (org-make-link "file:" file))))))
  10063. (t
  10064. ;; Read link, with completion for stored links.
  10065. (with-output-to-temp-buffer "*Org Links*"
  10066. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  10067. (when org-stored-links
  10068. (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
  10069. (princ (mapconcat 'car (reverse org-stored-links) "\n"))))
  10070. (let ((cw (selected-window)))
  10071. (select-window (get-buffer-window "*Org Links*"))
  10072. (shrink-window-if-larger-than-buffer)
  10073. (setq truncate-lines t)
  10074. (select-window cw))
  10075. ;; Fake a link history, containing the stored links.
  10076. (setq tmphist (append (mapcar 'car org-stored-links)
  10077. org-insert-link-history))
  10078. (unwind-protect
  10079. (setq link (org-completing-read
  10080. "Link: "
  10081. (append
  10082. (mapcar (lambda (x) (list (concat (car x) ":")))
  10083. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  10084. (mapcar (lambda (x) (list (concat x ":")))
  10085. org-link-types))
  10086. nil nil nil
  10087. 'tmphist
  10088. (or (car (car org-stored-links)))))
  10089. (set-window-configuration wcf)
  10090. (kill-buffer "*Org Links*"))
  10091. (setq entry (assoc link org-stored-links))
  10092. (or entry (push link org-insert-link-history))
  10093. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10094. (not org-keep-stored-link-after-insertion))
  10095. (setq org-stored-links (delq (assoc link org-stored-links)
  10096. org-stored-links)))
  10097. (setq link (if entry (nth 1 entry) link)
  10098. desc (or region desc (nth 2 entry)))))
  10099. (if (string-match org-plain-link-re link)
  10100. ;; URL-like link, normalize the use of angular brackets.
  10101. (setq link (org-make-link (org-remove-angle-brackets link))))
  10102. ;; Check if we are linking to the current file with a search option
  10103. ;; If yes, simplify the link by using only the search option.
  10104. (when (and buffer-file-name
  10105. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10106. (let* ((path (match-string 1 link))
  10107. (case-fold-search nil)
  10108. (search (match-string 2 link)))
  10109. (save-match-data
  10110. (if (equal (file-truename buffer-file-name) (file-truename path))
  10111. ;; We are linking to this same file, with a search option
  10112. (setq link search)))))
  10113. ;; Check if we can/should use a relative path. If yes, simplify the link
  10114. (when (string-match "\\<file:\\(.*\\)" link)
  10115. (let* ((path (match-string 1 link))
  10116. (case-fold-search nil))
  10117. (cond
  10118. ((eq org-link-file-path-type 'absolute)
  10119. (setq path (abbreviate-file-name (expand-file-name path))))
  10120. ((eq org-link-file-path-type 'noabbrev)
  10121. (setq path (expand-file-name path)))
  10122. ((eq org-link-file-path-type 'relative)
  10123. (setq path (file-relative-name path)))
  10124. (t
  10125. (save-match-data
  10126. (if (string-match (concat "^" (regexp-quote
  10127. (file-name-as-directory
  10128. (expand-file-name "."))))
  10129. (expand-file-name path))
  10130. ;; We are linking a file with relative path name.
  10131. (setq path (substring (expand-file-name path)
  10132. (match-end 0)))))))
  10133. (setq link (concat "file:" path))))
  10134. (setq desc (read-string "Description: " desc))
  10135. (unless (string-match "\\S-" desc) (setq desc nil))
  10136. (if remove (apply 'delete-region remove))
  10137. (insert (org-make-link-string link desc))))
  10138. (defun org-completing-read (&rest args)
  10139. (let ((minibuffer-local-completion-map
  10140. (copy-keymap minibuffer-local-completion-map)))
  10141. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  10142. (apply 'completing-read args)))
  10143. ;;; Opening/following a link
  10144. (defvar org-link-search-failed nil)
  10145. (defun org-next-link ()
  10146. "Move forward to the next link.
  10147. If the link is in hidden text, expose it."
  10148. (interactive)
  10149. (when (and org-link-search-failed (eq this-command last-command))
  10150. (goto-char (point-min))
  10151. (message "Link search wrapped back to beginning of buffer"))
  10152. (setq org-link-search-failed nil)
  10153. (let* ((pos (point))
  10154. (ct (org-context))
  10155. (a (assoc :link ct)))
  10156. (if a (goto-char (nth 2 a)))
  10157. (if (re-search-forward org-any-link-re nil t)
  10158. (progn
  10159. (goto-char (match-beginning 0))
  10160. (if (org-invisible-p) (org-show-context)))
  10161. (goto-char pos)
  10162. (setq org-link-search-failed t)
  10163. (error "No further link found"))))
  10164. (defun org-previous-link ()
  10165. "Move backward to the previous link.
  10166. If the link is in hidden text, expose it."
  10167. (interactive)
  10168. (when (and org-link-search-failed (eq this-command last-command))
  10169. (goto-char (point-max))
  10170. (message "Link search wrapped back to end of buffer"))
  10171. (setq org-link-search-failed nil)
  10172. (let* ((pos (point))
  10173. (ct (org-context))
  10174. (a (assoc :link ct)))
  10175. (if a (goto-char (nth 1 a)))
  10176. (if (re-search-backward org-any-link-re nil t)
  10177. (progn
  10178. (goto-char (match-beginning 0))
  10179. (if (org-invisible-p) (org-show-context)))
  10180. (goto-char pos)
  10181. (setq org-link-search-failed t)
  10182. (error "No further link found"))))
  10183. (defun org-find-file-at-mouse (ev)
  10184. "Open file link or URL at mouse."
  10185. (interactive "e")
  10186. (mouse-set-point ev)
  10187. (org-open-at-point 'in-emacs))
  10188. (defun org-open-at-mouse (ev)
  10189. "Open file link or URL at mouse."
  10190. (interactive "e")
  10191. (mouse-set-point ev)
  10192. (org-open-at-point))
  10193. (defvar org-window-config-before-follow-link nil
  10194. "The window configuration before following a link.
  10195. This is saved in case the need arises to restore it.")
  10196. (defvar org-open-link-marker (make-marker)
  10197. "Marker pointing to the location where `org-open-at-point; was called.")
  10198. ;;;###autoload
  10199. (defun org-open-at-point-global ()
  10200. "Follow a link like Org-mode does.
  10201. This command can be called in any mode to follow a link that has
  10202. Org-mode syntax."
  10203. (interactive)
  10204. (org-run-like-in-org-mode 'org-open-at-point))
  10205. (defun org-open-at-point (&optional in-emacs)
  10206. "Open link at or after point.
  10207. If there is no link at point, this function will search forward up to
  10208. the end of the current subtree.
  10209. Normally, files will be opened by an appropriate application. If the
  10210. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  10211. (interactive "P")
  10212. (move-marker org-open-link-marker (point))
  10213. (setq org-window-config-before-follow-link (current-window-configuration))
  10214. (org-remove-occur-highlights nil nil t)
  10215. (if (org-at-timestamp-p t)
  10216. (org-follow-timestamp-link)
  10217. (let (type path link line search (pos (point)))
  10218. (catch 'match
  10219. (save-excursion
  10220. (skip-chars-forward "^]\n\r")
  10221. (when (org-in-regexp org-bracket-link-regexp)
  10222. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  10223. (while (string-match " *\n *" link)
  10224. (setq link (replace-match " " t t link)))
  10225. (setq link (org-link-expand-abbrev link))
  10226. (if (string-match org-link-re-with-space2 link)
  10227. (setq type (match-string 1 link) path (match-string 2 link))
  10228. (setq type "thisfile" path link))
  10229. (throw 'match t)))
  10230. (when (get-text-property (point) 'org-linked-text)
  10231. (setq type "thisfile"
  10232. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  10233. (1+ (point)) (point))
  10234. path (buffer-substring
  10235. (previous-single-property-change pos 'org-linked-text)
  10236. (next-single-property-change pos 'org-linked-text)))
  10237. (throw 'match t))
  10238. (save-excursion
  10239. (when (or (org-in-regexp org-angle-link-re)
  10240. (org-in-regexp org-plain-link-re))
  10241. (setq type (match-string 1) path (match-string 2))
  10242. (throw 'match t)))
  10243. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  10244. (setq type "tree-match"
  10245. path (match-string 1))
  10246. (throw 'match t))
  10247. (save-excursion
  10248. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  10249. (setq type "tags"
  10250. path (match-string 1))
  10251. (while (string-match ":" path)
  10252. (setq path (replace-match "+" t t path)))
  10253. (throw 'match t))))
  10254. (unless path
  10255. (error "No link found"))
  10256. ;; Remove any trailing spaces in path
  10257. (if (string-match " +\\'" path)
  10258. (setq path (replace-match "" t t path)))
  10259. (cond
  10260. ((assoc type org-link-protocols)
  10261. (funcall (nth 1 (assoc type org-link-protocols)) path))
  10262. ((equal type "mailto")
  10263. (let ((cmd (car org-link-mailto-program))
  10264. (args (cdr org-link-mailto-program)) args1
  10265. (address path) (subject "") a)
  10266. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10267. (setq address (match-string 1 path)
  10268. subject (org-link-escape (match-string 2 path))))
  10269. (while args
  10270. (cond
  10271. ((not (stringp (car args))) (push (pop args) args1))
  10272. (t (setq a (pop args))
  10273. (if (string-match "%a" a)
  10274. (setq a (replace-match address t t a)))
  10275. (if (string-match "%s" a)
  10276. (setq a (replace-match subject t t a)))
  10277. (push a args1))))
  10278. (apply cmd (nreverse args1))))
  10279. ((member type '("http" "https" "ftp" "news"))
  10280. (browse-url (concat type ":" path)))
  10281. ((string= type "tags")
  10282. (org-tags-view in-emacs path))
  10283. ((string= type "thisfile")
  10284. (if in-emacs
  10285. (switch-to-buffer-other-window
  10286. (org-get-buffer-for-internal-link (current-buffer)))
  10287. (org-mark-ring-push))
  10288. (let ((cmd `(org-link-search
  10289. ,path
  10290. ,(cond ((equal in-emacs '(4)) 'occur)
  10291. ((equal in-emacs '(16)) 'org-occur)
  10292. (t nil))
  10293. ,pos)))
  10294. (condition-case nil (eval cmd)
  10295. (error (progn (widen) (eval cmd))))))
  10296. ((string= type "tree-match")
  10297. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10298. ((string= type "file")
  10299. (if (string-match "::\\([0-9]+\\)\\'" path)
  10300. (setq line (string-to-number (match-string 1 path))
  10301. path (substring path 0 (match-beginning 0)))
  10302. (if (string-match "::\\(.+\\)\\'" path)
  10303. (setq search (match-string 1 path)
  10304. path (substring path 0 (match-beginning 0)))))
  10305. (org-open-file path in-emacs line search))
  10306. ((string= type "news")
  10307. (org-follow-gnus-link path))
  10308. ((string= type "bbdb")
  10309. (org-follow-bbdb-link path))
  10310. ((string= type "info")
  10311. (org-follow-info-link path))
  10312. ((string= type "gnus")
  10313. (let (group article)
  10314. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10315. (error "Error in Gnus link"))
  10316. (setq group (match-string 1 path)
  10317. article (match-string 3 path))
  10318. (org-follow-gnus-link group article)))
  10319. ((string= type "vm")
  10320. (let (folder article)
  10321. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10322. (error "Error in VM link"))
  10323. (setq folder (match-string 1 path)
  10324. article (match-string 3 path))
  10325. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10326. (org-follow-vm-link folder article in-emacs)))
  10327. ((string= type "wl")
  10328. (let (folder article)
  10329. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10330. (error "Error in Wanderlust link"))
  10331. (setq folder (match-string 1 path)
  10332. article (match-string 3 path))
  10333. (org-follow-wl-link folder article)))
  10334. ((string= type "mhe")
  10335. (let (folder article)
  10336. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10337. (error "Error in MHE link"))
  10338. (setq folder (match-string 1 path)
  10339. article (match-string 3 path))
  10340. (org-follow-mhe-link folder article)))
  10341. ((string= type "rmail")
  10342. (let (folder article)
  10343. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10344. (error "Error in RMAIL link"))
  10345. (setq folder (match-string 1 path)
  10346. article (match-string 3 path))
  10347. (org-follow-rmail-link folder article)))
  10348. ((string= type "shell")
  10349. (let ((cmd path))
  10350. ;; FIXME: the following is only for backward compatibility
  10351. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10352. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10353. (if (or (not org-confirm-shell-link-function)
  10354. (funcall org-confirm-shell-link-function
  10355. (format "Execute \"%s\" in shell? "
  10356. (org-add-props cmd nil
  10357. 'face 'org-warning))))
  10358. (progn
  10359. (message "Executing %s" cmd)
  10360. (shell-command cmd))
  10361. (error "Abort"))))
  10362. ((string= type "elisp")
  10363. (let ((cmd path))
  10364. (if (or (not org-confirm-elisp-link-function)
  10365. (funcall org-confirm-elisp-link-function
  10366. (format "Execute \"%s\" as elisp? "
  10367. (org-add-props cmd nil
  10368. 'face 'org-warning))))
  10369. (message "%s => %s" cmd (eval (read cmd)))
  10370. (error "Abort"))))
  10371. (t
  10372. (browse-url-at-point)))))
  10373. (move-marker org-open-link-marker nil))
  10374. ;;; File search
  10375. (defvar org-create-file-search-functions nil
  10376. "List of functions to construct the right search string for a file link.
  10377. These functions are called in turn with point at the location to
  10378. which the link should point.
  10379. A function in the hook should first test if it would like to
  10380. handle this file type, for example by checking the major-mode or
  10381. the file extension. If it decides not to handle this file, it
  10382. should just return nil to give other functions a chance. If it
  10383. does handle the file, it must return the search string to be used
  10384. when following the link. The search string will be part of the
  10385. file link, given after a double colon, and `org-open-at-point'
  10386. will automatically search for it. If special measures must be
  10387. taken to make the search successful, another function should be
  10388. added to the companion hook `org-execute-file-search-functions',
  10389. which see.
  10390. A function in this hook may also use `setq' to set the variable
  10391. `description' to provide a suggestion for the descriptive text to
  10392. be used for this link when it gets inserted into an Org-mode
  10393. buffer with \\[org-insert-link].")
  10394. (defvar org-execute-file-search-functions nil
  10395. "List of functions to execute a file search triggered by a link.
  10396. Functions added to this hook must accept a single argument, the
  10397. search string that was part of the file link, the part after the
  10398. double colon. The function must first check if it would like to
  10399. handle this search, for example by checking the major-mode or the
  10400. file extension. If it decides not to handle this search, it
  10401. should just return nil to give other functions a chance. If it
  10402. does handle the search, it must return a non-nil value to keep
  10403. other functions from trying.
  10404. Each function can access the current prefix argument through the
  10405. variable `current-prefix-argument'. Note that a single prefix is
  10406. used to force opening a link in Emacs, so it may be good to only
  10407. use a numeric or double prefix to guide the search function.
  10408. In case this is needed, a function in this hook can also restore
  10409. the window configuration before `org-open-at-point' was called using:
  10410. (set-window-configuration org-window-config-before-follow-link)")
  10411. (defun org-link-search (s &optional type avoid-pos)
  10412. "Search for a link search option.
  10413. If S is surrounded by forward slashes, it is interpreted as a
  10414. regular expression. In org-mode files, this will create an `org-occur'
  10415. sparse tree. In ordinary files, `occur' will be used to list matches.
  10416. If the current buffer is in `dired-mode', grep will be used to search
  10417. in all files. If AVOID-POS is given, ignore matches near that position."
  10418. (let ((case-fold-search t)
  10419. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  10420. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  10421. (append '(("") (" ") ("\t") ("\n"))
  10422. org-emphasis-alist)
  10423. "\\|") "\\)"))
  10424. (pos (point))
  10425. (pre "") (post "")
  10426. words re0 re1 re2 re3 re4 re5 re2a reall)
  10427. (cond
  10428. ;; First check if there are any special
  10429. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  10430. ;; Now try the builtin stuff
  10431. ((save-excursion
  10432. (goto-char (point-min))
  10433. (and
  10434. (re-search-forward
  10435. (concat "<<" (regexp-quote s0) ">>") nil t)
  10436. (setq pos (match-beginning 0))))
  10437. ;; There is an exact target for this
  10438. (goto-char pos))
  10439. ((string-match "^/\\(.*\\)/$" s)
  10440. ;; A regular expression
  10441. (cond
  10442. ((org-mode-p)
  10443. (org-occur (match-string 1 s)))
  10444. ;;((eq major-mode 'dired-mode)
  10445. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  10446. (t (org-do-occur (match-string 1 s)))))
  10447. (t
  10448. ;; A normal search strings
  10449. (when (equal (string-to-char s) ?*)
  10450. ;; Anchor on headlines, post may include tags.
  10451. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10452. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  10453. s (substring s 1)))
  10454. (remove-text-properties
  10455. 0 (length s)
  10456. '(face nil mouse-face nil keymap nil fontified nil) s)
  10457. ;; Make a series of regular expressions to find a match
  10458. (setq words (org-split-string s "[ \n\r\t]+")
  10459. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10460. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10461. "\\)" markers)
  10462. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  10463. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10464. re1 (concat pre re2 post)
  10465. re3 (concat pre re4 post)
  10466. re5 (concat pre ".*" re4)
  10467. re2 (concat pre re2)
  10468. re2a (concat pre re2a)
  10469. re4 (concat pre re4)
  10470. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10471. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10472. re5 "\\)"
  10473. ))
  10474. (cond
  10475. ((eq type 'org-occur) (org-occur reall))
  10476. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10477. (t (goto-char (point-min))
  10478. (if (or (org-search-not-self 1 re0 nil t)
  10479. (org-search-not-self 1 re1 nil t)
  10480. (org-search-not-self 1 re2 nil t)
  10481. (org-search-not-self 1 re2a nil t)
  10482. (org-search-not-self 1 re3 nil t)
  10483. (org-search-not-self 1 re4 nil t)
  10484. (org-search-not-self 1 re5 nil t)
  10485. )
  10486. (goto-char (match-beginning 1))
  10487. (goto-char pos)
  10488. (error "No match")))))
  10489. (t
  10490. ;; Normal string-search
  10491. (goto-char (point-min))
  10492. (if (search-forward s nil t)
  10493. (goto-char (match-beginning 0))
  10494. (error "No match"))))
  10495. (and (org-mode-p) (org-show-context 'link-search))))
  10496. (defun org-search-not-self (group &rest args)
  10497. "Execute `re-search-forward', but only accept matches that do not
  10498. enclose the position of `org-open-link-marker'."
  10499. (let ((m org-open-link-marker))
  10500. (catch 'exit
  10501. (while (apply 're-search-forward args)
  10502. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10503. (goto-char (match-end group))
  10504. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10505. (> (match-beginning 0) (marker-position m))
  10506. (< (match-end 0) (marker-position m)))
  10507. (save-match-data
  10508. (or (not (org-in-regexp
  10509. org-bracket-link-analytic-regexp 1))
  10510. (not (match-end 4)) ; no description
  10511. (and (<= (match-beginning 4) (point))
  10512. (>= (match-end 4) (point))))))
  10513. (throw 'exit (point))))))))
  10514. (defun org-get-buffer-for-internal-link (buffer)
  10515. "Return a buffer to be used for displaying the link target of internal links."
  10516. (cond
  10517. ((not org-display-internal-link-with-indirect-buffer)
  10518. buffer)
  10519. ((string-match "(Clone)$" (buffer-name buffer))
  10520. (message "Buffer is already a clone, not making another one")
  10521. ;; we also do not modify visibility in this case
  10522. buffer)
  10523. (t ; make a new indirect buffer for displaying the link
  10524. (let* ((bn (buffer-name buffer))
  10525. (ibn (concat bn "(Clone)"))
  10526. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10527. (with-current-buffer ib (org-overview))
  10528. ib))))
  10529. (defun org-do-occur (regexp &optional cleanup)
  10530. "Call the Emacs command `occur'.
  10531. If CLEANUP is non-nil, remove the printout of the regular expression
  10532. in the *Occur* buffer. This is useful if the regex is long and not useful
  10533. to read."
  10534. (occur regexp)
  10535. (when cleanup
  10536. (let ((cwin (selected-window)) win beg end)
  10537. (when (setq win (get-buffer-window "*Occur*"))
  10538. (select-window win))
  10539. (goto-char (point-min))
  10540. (when (re-search-forward "match[a-z]+" nil t)
  10541. (setq beg (match-end 0))
  10542. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10543. (setq end (1- (match-beginning 0)))))
  10544. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10545. (goto-char (point-min))
  10546. (select-window cwin))))
  10547. ;;; The mark ring for links jumps
  10548. (defvar org-mark-ring nil
  10549. "Mark ring for positions before jumps in Org-mode.")
  10550. (defvar org-mark-ring-last-goto nil
  10551. "Last position in the mark ring used to go back.")
  10552. ;; Fill and close the ring
  10553. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10554. (loop for i from 1 to org-mark-ring-length do
  10555. (push (make-marker) org-mark-ring))
  10556. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10557. org-mark-ring)
  10558. (defun org-mark-ring-push (&optional pos buffer)
  10559. "Put the current position or POS into the mark ring and rotate it."
  10560. (interactive)
  10561. (setq pos (or pos (point)))
  10562. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10563. (move-marker (car org-mark-ring)
  10564. (or pos (point))
  10565. (or buffer (current-buffer)))
  10566. (message
  10567. (substitute-command-keys
  10568. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10569. (defun org-mark-ring-goto (&optional n)
  10570. "Jump to the previous position in the mark ring.
  10571. With prefix arg N, jump back that many stored positions. When
  10572. called several times in succession, walk through the entire ring.
  10573. Org-mode commands jumping to a different position in the current file,
  10574. or to another Org-mode file, automatically push the old position
  10575. onto the ring."
  10576. (interactive "p")
  10577. (let (p m)
  10578. (if (eq last-command this-command)
  10579. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10580. (setq p org-mark-ring))
  10581. (setq org-mark-ring-last-goto p)
  10582. (setq m (car p))
  10583. (switch-to-buffer (marker-buffer m))
  10584. (goto-char m)
  10585. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10586. (defun org-remove-angle-brackets (s)
  10587. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10588. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10589. s)
  10590. (defun org-add-angle-brackets (s)
  10591. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10592. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10593. s)
  10594. ;;; Following specific links
  10595. (defun org-follow-timestamp-link ()
  10596. (cond
  10597. ((org-at-date-range-p t)
  10598. (let ((org-agenda-start-on-weekday)
  10599. (t1 (match-string 1))
  10600. (t2 (match-string 2)))
  10601. (setq t1 (time-to-days (org-time-string-to-time t1))
  10602. t2 (time-to-days (org-time-string-to-time t2)))
  10603. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  10604. ((org-at-timestamp-p t)
  10605. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10606. (substring (match-string 1) 0 10)))
  10607. 1))
  10608. (t (error "This should not happen"))))
  10609. (defun org-follow-bbdb-link (name)
  10610. "Follow a BBDB link to NAME."
  10611. (require 'bbdb)
  10612. (let ((inhibit-redisplay (not debug-on-error))
  10613. (bbdb-electric-p nil))
  10614. (catch 'exit
  10615. ;; Exact match on name
  10616. (bbdb-name (concat "\\`" name "\\'") nil)
  10617. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10618. ;; Exact match on name
  10619. (bbdb-company (concat "\\`" name "\\'") nil)
  10620. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10621. ;; Partial match on name
  10622. (bbdb-name name nil)
  10623. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10624. ;; Partial match on company
  10625. (bbdb-company name nil)
  10626. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  10627. ;; General match including network address and notes
  10628. (bbdb name nil)
  10629. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  10630. (delete-window (get-buffer-window "*BBDB*"))
  10631. (error "No matching BBDB record")))))
  10632. (defun org-follow-info-link (name)
  10633. "Follow an info file & node link to NAME."
  10634. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  10635. (string-match "\\(.*\\)" name))
  10636. (progn
  10637. (require 'info)
  10638. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  10639. (Info-find-node (match-string 1 name) (match-string 2 name))
  10640. (Info-find-node (match-string 1 name) "Top")))
  10641. (message (concat "Could not open: " name))))
  10642. (defun org-follow-gnus-link (&optional group article)
  10643. "Follow a Gnus link to GROUP and ARTICLE."
  10644. (require 'gnus)
  10645. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  10646. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  10647. (cond ((and group article)
  10648. (gnus-group-read-group 1 nil group)
  10649. (gnus-summary-goto-article (string-to-number article) nil t))
  10650. (group (gnus-group-jump-to-group group))))
  10651. (defun org-follow-vm-link (&optional folder article readonly)
  10652. "Follow a VM link to FOLDER and ARTICLE."
  10653. (require 'vm)
  10654. (setq article (org-add-angle-brackets article))
  10655. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  10656. ;; ange-ftp or efs or tramp access
  10657. (let ((user (or (match-string 1 folder) (user-login-name)))
  10658. (host (match-string 2 folder))
  10659. (file (match-string 3 folder)))
  10660. (cond
  10661. ((featurep 'tramp)
  10662. ;; use tramp to access the file
  10663. (if (featurep 'xemacs)
  10664. (setq folder (format "[%s@%s]%s" user host file))
  10665. (setq folder (format "/%s@%s:%s" user host file))))
  10666. (t
  10667. ;; use ange-ftp or efs
  10668. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  10669. (setq folder (format "/%s@%s:%s" user host file))))))
  10670. (when folder
  10671. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  10672. (sit-for 0.1)
  10673. (when article
  10674. (vm-select-folder-buffer)
  10675. (widen)
  10676. (let ((case-fold-search t))
  10677. (goto-char (point-min))
  10678. (if (not (re-search-forward
  10679. (concat "^" "message-id: *" (regexp-quote article))))
  10680. (error "Could not find the specified message in this folder"))
  10681. (vm-isearch-update)
  10682. (vm-isearch-narrow)
  10683. (vm-beginning-of-message)
  10684. (vm-summarize)))))
  10685. (defun org-follow-wl-link (folder article)
  10686. "Follow a Wanderlust link to FOLDER and ARTICLE."
  10687. (if (and (string= folder "%")
  10688. article
  10689. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  10690. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  10691. ;; Thus, we recompose folder and article ids.
  10692. (setq folder (format "%s#%s" folder (match-string 1 article))
  10693. article (match-string 3 article)))
  10694. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  10695. (error "No such folder: %s" folder))
  10696. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  10697. (and article
  10698. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  10699. (wl-summary-redisplay)))
  10700. (defun org-follow-rmail-link (folder article)
  10701. "Follow an RMAIL link to FOLDER and ARTICLE."
  10702. (setq article (org-add-angle-brackets article))
  10703. (let (message-number)
  10704. (save-excursion
  10705. (save-window-excursion
  10706. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10707. (setq message-number
  10708. (save-restriction
  10709. (widen)
  10710. (goto-char (point-max))
  10711. (if (re-search-backward
  10712. (concat "^Message-ID:\\s-+" (regexp-quote
  10713. (or article "")))
  10714. nil t)
  10715. (rmail-what-message))))))
  10716. (if message-number
  10717. (progn
  10718. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  10719. (rmail-show-message message-number)
  10720. message-number)
  10721. (error "Message not found"))))
  10722. ;;; mh-e integration based on planner-mode
  10723. (defun org-mhe-get-message-real-folder ()
  10724. "Return the name of the current message real folder, so if you use
  10725. sequences, it will now work."
  10726. (save-excursion
  10727. (let* ((folder
  10728. (if (equal major-mode 'mh-folder-mode)
  10729. mh-current-folder
  10730. ;; Refer to the show buffer
  10731. mh-show-folder-buffer))
  10732. (end-index
  10733. (if (boundp 'mh-index-folder)
  10734. (min (length mh-index-folder) (length folder))))
  10735. )
  10736. ;; a simple test on mh-index-data does not work, because
  10737. ;; mh-index-data is always nil in a show buffer.
  10738. (if (and (boundp 'mh-index-folder)
  10739. (string= mh-index-folder (substring folder 0 end-index)))
  10740. (if (equal major-mode 'mh-show-mode)
  10741. (save-window-excursion
  10742. (when (buffer-live-p (get-buffer folder))
  10743. (progn
  10744. (pop-to-buffer folder)
  10745. (org-mhe-get-message-folder-from-index)
  10746. )
  10747. ))
  10748. (org-mhe-get-message-folder-from-index)
  10749. )
  10750. folder
  10751. )
  10752. )))
  10753. (defun org-mhe-get-message-folder-from-index ()
  10754. "Returns the name of the message folder in a index folder buffer."
  10755. (save-excursion
  10756. (mh-index-previous-folder)
  10757. (re-search-forward "^\\(+.*\\)$" nil t)
  10758. (message (match-string 1))))
  10759. (defun org-mhe-get-message-folder ()
  10760. "Return the name of the current message folder. Be careful if you
  10761. use sequences."
  10762. (save-excursion
  10763. (if (equal major-mode 'mh-folder-mode)
  10764. mh-current-folder
  10765. ;; Refer to the show buffer
  10766. mh-show-folder-buffer)))
  10767. (defun org-mhe-get-message-num ()
  10768. "Return the number of the current message. Be careful if you
  10769. use sequences."
  10770. (save-excursion
  10771. (if (equal major-mode 'mh-folder-mode)
  10772. (mh-get-msg-num nil)
  10773. ;; Refer to the show buffer
  10774. (mh-show-buffer-message-number))))
  10775. (defun org-mhe-get-header (header)
  10776. "Return a header of the message in folder mode. This will create a
  10777. show buffer for the corresponding message. If you have a more clever
  10778. idea..."
  10779. (let* ((folder (org-mhe-get-message-folder))
  10780. (num (org-mhe-get-message-num))
  10781. (buffer (get-buffer-create (concat "show-" folder)))
  10782. (header-field))
  10783. (with-current-buffer buffer
  10784. (mh-display-msg num folder)
  10785. (if (equal major-mode 'mh-folder-mode)
  10786. (mh-header-display)
  10787. (mh-show-header-display))
  10788. (set-buffer buffer)
  10789. (setq header-field (mh-get-header-field header))
  10790. (if (equal major-mode 'mh-folder-mode)
  10791. (mh-show)
  10792. (mh-show-show))
  10793. header-field)))
  10794. (defun org-follow-mhe-link (folder article)
  10795. "Follow an MHE link to FOLDER and ARTICLE.
  10796. If ARTICLE is nil FOLDER is shown. If the configuration variable
  10797. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  10798. ARTICLE is searched in all folders. Indexed searches (swish++,
  10799. namazu, and others supported by MH-E) will always search in all
  10800. folders."
  10801. (require 'mh-e)
  10802. (require 'mh-search)
  10803. (require 'mh-utils)
  10804. (mh-find-path)
  10805. (if (not article)
  10806. (mh-visit-folder (mh-normalize-folder-name folder))
  10807. (setq article (org-add-angle-brackets article))
  10808. (mh-search-choose)
  10809. (if (equal mh-searcher 'pick)
  10810. (progn
  10811. (mh-search folder (list "--message-id" article))
  10812. (when (and org-mhe-search-all-folders
  10813. (not (org-mhe-get-message-real-folder)))
  10814. (kill-this-buffer)
  10815. (mh-search "+" (list "--message-id" article))))
  10816. (mh-search "+" article))
  10817. (if (org-mhe-get-message-real-folder)
  10818. (mh-show-msg 1)
  10819. (kill-this-buffer)
  10820. (error "Message not found"))))
  10821. ;;; BibTeX links
  10822. ;; Use the custom search meachnism to construct and use search strings for
  10823. ;; file links to BibTeX database entries.
  10824. (defun org-create-file-search-in-bibtex ()
  10825. "Create the search string and description for a BibTeX database entry."
  10826. (when (eq major-mode 'bibtex-mode)
  10827. ;; yes, we want to construct this search string.
  10828. ;; Make a good description for this entry, using names, year and the title
  10829. ;; Put it into the `description' variable which is dynamically scoped.
  10830. (let ((bibtex-autokey-names 1)
  10831. (bibtex-autokey-names-stretch 1)
  10832. (bibtex-autokey-name-case-convert-function 'identity)
  10833. (bibtex-autokey-name-separator " & ")
  10834. (bibtex-autokey-additional-names " et al.")
  10835. (bibtex-autokey-year-length 4)
  10836. (bibtex-autokey-name-year-separator " ")
  10837. (bibtex-autokey-titlewords 3)
  10838. (bibtex-autokey-titleword-separator " ")
  10839. (bibtex-autokey-titleword-case-convert-function 'identity)
  10840. (bibtex-autokey-titleword-length 'infty)
  10841. (bibtex-autokey-year-title-separator ": "))
  10842. (setq description (bibtex-generate-autokey)))
  10843. ;; Now parse the entry, get the key and return it.
  10844. (save-excursion
  10845. (bibtex-beginning-of-entry)
  10846. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  10847. (defun org-execute-file-search-in-bibtex (s)
  10848. "Find the link search string S as a key for a database entry."
  10849. (when (eq major-mode 'bibtex-mode)
  10850. ;; Yes, we want to do the search in this file.
  10851. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  10852. (goto-char (point-min))
  10853. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  10854. (regexp-quote s) "[ \t\n]*,") nil t)
  10855. (goto-char (match-beginning 0)))
  10856. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  10857. ;; Use double prefix to indicate that any web link should be browsed
  10858. (let ((b (current-buffer)) (p (point)))
  10859. ;; Restore the window configuration because we just use the web link
  10860. (set-window-configuration org-window-config-before-follow-link)
  10861. (save-excursion (set-buffer b) (goto-char p)
  10862. (bibtex-url)))
  10863. (recenter 0)) ; Move entry start to beginning of window
  10864. ;; return t to indicate that the search is done.
  10865. t))
  10866. ;; Finally add the functions to the right hooks.
  10867. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  10868. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  10869. ;; end of Bibtex link setup
  10870. ;;; Following file links
  10871. (defun org-open-file (path &optional in-emacs line search)
  10872. "Open the file at PATH.
  10873. First, this expands any special file name abbreviations. Then the
  10874. configuration variable `org-file-apps' is checked if it contains an
  10875. entry for this file type, and if yes, the corresponding command is launched.
  10876. If no application is found, Emacs simply visits the file.
  10877. With optional argument IN-EMACS, Emacs will visit the file.
  10878. Optional LINE specifies a line to go to, optional SEARCH a string to
  10879. search for. If LINE or SEARCH is given, the file will always be
  10880. opened in Emacs.
  10881. If the file does not exist, an error is thrown."
  10882. (setq in-emacs (or in-emacs line search))
  10883. (let* ((file (if (equal path "")
  10884. buffer-file-name
  10885. (substitute-in-file-name (expand-file-name path))))
  10886. (apps (append org-file-apps (org-default-apps)))
  10887. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10888. (dirp (if remp nil (file-directory-p file)))
  10889. (dfile (downcase file))
  10890. (old-buffer (current-buffer))
  10891. (old-pos (point))
  10892. (old-mode major-mode)
  10893. ext cmd)
  10894. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10895. (setq ext (match-string 1 dfile))
  10896. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10897. (setq ext (match-string 1 dfile))))
  10898. (if in-emacs
  10899. (setq cmd 'emacs)
  10900. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10901. (and dirp (cdr (assoc 'directory apps)))
  10902. (cdr (assoc ext apps))
  10903. (cdr (assoc t apps)))))
  10904. (when (eq cmd 'mailcap)
  10905. (require 'mailcap)
  10906. (mailcap-parse-mailcaps)
  10907. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10908. (command (mailcap-mime-info mime-type)))
  10909. (if (stringp command)
  10910. (setq cmd command)
  10911. (setq cmd 'emacs))))
  10912. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10913. (not (file-exists-p file))
  10914. (not org-open-non-existing-files))
  10915. (error "No such file: %s" file))
  10916. (cond
  10917. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10918. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10919. (if (string-match "['\"]%s['\"]" cmd)
  10920. (setq cmd (replace-match "%s" t t cmd)))
  10921. (setq cmd (format cmd (shell-quote-argument file)))
  10922. (save-window-excursion
  10923. (shell-command (concat cmd " &"))))
  10924. ((or (stringp cmd)
  10925. (eq cmd 'emacs))
  10926. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10927. (widen)
  10928. (if line (goto-line line)
  10929. (if search (org-link-search search))))
  10930. ((consp cmd)
  10931. (eval cmd))
  10932. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10933. (and (org-mode-p) (eq old-mode 'org-mode)
  10934. (or (not (equal old-buffer (current-buffer)))
  10935. (not (equal old-pos (point))))
  10936. (org-mark-ring-push old-pos old-buffer))))
  10937. (defun org-default-apps ()
  10938. "Return the default applications for this operating system."
  10939. (cond
  10940. ((eq system-type 'darwin)
  10941. org-file-apps-defaults-macosx)
  10942. ((eq system-type 'windows-nt)
  10943. org-file-apps-defaults-windowsnt)
  10944. (t org-file-apps-defaults-gnu)))
  10945. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10946. (defun org-file-remote-p (file)
  10947. "Test whether FILE specifies a location on a remote system.
  10948. Return non-nil if the location is indeed remote.
  10949. For example, the filename \"/user@host:/foo\" specifies a location
  10950. on the system \"/user@host:\"."
  10951. (cond ((fboundp 'file-remote-p)
  10952. (file-remote-p file))
  10953. ((fboundp 'tramp-handle-file-remote-p)
  10954. (tramp-handle-file-remote-p file))
  10955. ((and (boundp 'ange-ftp-name-format)
  10956. (string-match (car ange-ftp-name-format) file))
  10957. t)
  10958. (t nil)))
  10959. ;;;; Hooks for remember.el
  10960. ;;;###autoload
  10961. (defun org-remember-annotation ()
  10962. "Return a link to the current location as an annotation for remember.el.
  10963. If you are using Org-mode files as target for data storage with
  10964. remember.el, then the annotations should include a link compatible with the
  10965. conventions in Org-mode. This function returns such a link."
  10966. (org-store-link nil))
  10967. (defconst org-remember-help
  10968. "Select a destination location for the note.
  10969. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  10970. RET at beg-of-buf -> Append to file as level 2 headline
  10971. RET on headline -> Store as sublevel entry to current headline
  10972. <left>/<right> -> before/after current headline, same headings level")
  10973. ;;;###autoload
  10974. (defun org-remember-apply-template (&optional use-char skip-interactive)
  10975. "Initialize *remember* buffer with template, invoke `org-mode'.
  10976. This function should be placed into `remember-mode-hook' and in fact requires
  10977. to be run from that hook to fucntion properly."
  10978. (if org-remember-templates
  10979. (let* ((char (or use-char
  10980. (if (= (length org-remember-templates) 1)
  10981. (caar org-remember-templates)
  10982. (message "Select template: %s"
  10983. (mapconcat
  10984. (lambda (x) (char-to-string (car x)))
  10985. org-remember-templates " "))
  10986. (read-char-exclusive))))
  10987. (entry (cdr (assoc char org-remember-templates)))
  10988. (tpl (car entry))
  10989. (plist-p (if org-store-link-plist t nil))
  10990. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  10991. (string-match "\\S-" (nth 1 entry)))
  10992. (nth 1 entry)
  10993. org-default-notes-file))
  10994. (headline (nth 2 entry))
  10995. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  10996. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  10997. (v-u (concat "[" (substring v-t 1 -1) "]"))
  10998. (v-U (concat "[" (substring v-T 1 -1) "]"))
  10999. (v-i initial) ; defined in `remember-mode'
  11000. (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
  11001. (v-n user-full-name)
  11002. (org-startup-folded nil)
  11003. org-time-was-given org-end-time-was-given x prompt char time)
  11004. (setq org-store-link-plist
  11005. (append (list :annotation v-a :initial v-i)
  11006. org-store-link-plist))
  11007. (unless tpl (setq tpl "") (message "No template") (ding))
  11008. (erase-buffer)
  11009. (insert (substitute-command-keys
  11010. (format
  11011. "## `C-c C-c' to file interactively, `C-u C-c C-c' to file directly.
  11012. ## Target file \"%s\", headline \"%s\"
  11013. ## To switch templates, use `\\[org-remember]'.\n\n"
  11014. (abbreviate-file-name (or file org-default-notes-file))
  11015. (or headline ""))))
  11016. (insert tpl) (goto-char (point-min))
  11017. ;; Simple %-escapes
  11018. (while (re-search-forward "%\\([tTuUai]\\)" nil t)
  11019. (when (and initial (equal (match-string 0) "%i"))
  11020. (save-match-data
  11021. (let* ((lead (buffer-substring
  11022. (point-at-bol) (match-beginning 0))))
  11023. (setq v-i (mapconcat 'identity
  11024. (org-split-string initial "\n")
  11025. (concat "\n" lead))))))
  11026. (replace-match
  11027. (or (eval (intern (concat "v-" (match-string 1)))) "")
  11028. t t))
  11029. ;; From the property list
  11030. (when plist-p
  11031. (goto-char (point-min))
  11032. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  11033. (and (setq x (plist-get org-store-link-plist
  11034. (intern (match-string 1))))
  11035. (replace-match x t t))))
  11036. ;; Turn on org-mode in the remember buffer, set local variables
  11037. (org-mode)
  11038. (org-set-local 'org-finish-function 'remember-buffer)
  11039. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  11040. (org-set-local 'org-default-notes-file file))
  11041. (if (and headline (stringp headline) (string-match "\\S-" headline))
  11042. (org-set-local 'org-remember-default-headline headline))
  11043. ;; Interactive template entries
  11044. (goto-char (point-min))
  11045. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  11046. (setq char (if (match-end 3) (match-string 3))
  11047. prompt (if (match-end 2) (match-string 2)))
  11048. (goto-char (match-beginning 0))
  11049. (replace-match "")
  11050. (cond
  11051. ((member char '("G" "g"))
  11052. (let* ((org-last-tags-completion-table
  11053. (org-global-tags-completion-table
  11054. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  11055. (org-add-colon-after-tag-completion t)
  11056. (ins (completing-read
  11057. (if prompt (concat prompt ": ") "Tags: ")
  11058. 'org-tags-completion-function nil nil nil
  11059. 'org-tags-history)))
  11060. (setq ins (mapconcat 'identity
  11061. (org-split-string ins (org-re "[^[:alnum:]]+"))
  11062. ":"))
  11063. (when (string-match "\\S-" ins)
  11064. (or (equal (char-before) ?:) (insert ":"))
  11065. (insert ins)
  11066. (or (equal (char-after) ?:) (insert ":")))))
  11067. (char
  11068. (setq org-time-was-given (equal (upcase char) char))
  11069. (setq time (org-read-date (equal (upcase char) "U") t nil
  11070. prompt))
  11071. (org-insert-time-stamp time org-time-was-given
  11072. (member char '("u" "U"))
  11073. nil nil (list org-end-time-was-given)))
  11074. (t
  11075. (insert (read-string
  11076. (if prompt (concat prompt ": ") "Enter string"))))))
  11077. (goto-char (point-min))
  11078. (if (re-search-forward "%\\?" nil t)
  11079. (replace-match "")
  11080. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  11081. (org-mode)
  11082. (org-set-local 'org-finish-function 'remember-buffer)))
  11083. ;;;###autoload
  11084. (defun org-remember ()
  11085. "Call `remember'. If this is already a remember buffer, re-apply template.
  11086. If there is an active region, make sure remember uses it as initial content
  11087. of the remember buffer."
  11088. (interactive)
  11089. (if (eq org-finish-function 'remember-buffer)
  11090. (progn
  11091. (when (< (length org-remember-templates) 2)
  11092. (error "No other template available"))
  11093. (erase-buffer)
  11094. (let ((annotation (plist-get org-store-link-plist :annotation))
  11095. (initial (plist-get org-store-link-plist :initial)))
  11096. (org-remember-apply-template))
  11097. (message "Press C-c C-c to remember data"))
  11098. (if (org-region-active-p)
  11099. (remember (buffer-substring (point) (mark)))
  11100. (call-interactively 'remember))))
  11101. ;;;###autoload
  11102. (defun org-remember-handler ()
  11103. "Store stuff from remember.el into an org file.
  11104. First prompts for an org file. If the user just presses return, the value
  11105. of `org-default-notes-file' is used.
  11106. Then the command offers the headings tree of the selected file in order to
  11107. file the text at a specific location.
  11108. You can either immediately press RET to get the note appended to the
  11109. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  11110. find a better place. Then press RET or <left> or <right> in insert the note.
  11111. Key Cursor position Note gets inserted
  11112. -----------------------------------------------------------------------------
  11113. RET buffer-start as level 2 heading at end of file
  11114. RET on headline as sublevel of the heading at cursor
  11115. RET no heading at cursor position, level taken from context.
  11116. Or use prefix arg to specify level manually.
  11117. <left> on headline as same level, before current heading
  11118. <right> on headline as same level, after current heading
  11119. So the fastest way to store the note is to press RET RET to append it to
  11120. the default file. This way your current train of thought is not
  11121. interrupted, in accordance with the principles of remember.el.
  11122. You can also get the fast execution without prompting by using
  11123. C-u C-c C-c to exit the remember buffer. See also the variable
  11124. `org-remember-store-without-prompt'.
  11125. Before being stored away, the function ensures that the text has a
  11126. headline, i.e. a first line that starts with a \"*\". If not, a headline
  11127. is constructed from the current date and some additional data.
  11128. If the variable `org-adapt-indentation' is non-nil, the entire text is
  11129. also indented so that it starts in the same column as the headline
  11130. \(i.e. after the stars).
  11131. See also the variable `org-reverse-note-order'."
  11132. (goto-char (point-min))
  11133. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  11134. (replace-match ""))
  11135. (catch 'quit
  11136. (let* ((txt (buffer-substring (point-min) (point-max)))
  11137. (fastp (org-xor (equal current-prefix-arg '(4))
  11138. org-remember-store-without-prompt))
  11139. (file (if fastp org-default-notes-file (org-get-org-file)))
  11140. (heading org-remember-default-headline)
  11141. (visiting (org-find-base-buffer-visiting file))
  11142. (org-startup-folded nil)
  11143. (org-startup-align-all-tables nil)
  11144. (org-goto-start-pos 1)
  11145. spos level indent reversed)
  11146. (setq current-prefix-arg nil)
  11147. ;; Modify text so that it becomes a nice subtree which can be inserted
  11148. ;; into an org tree.
  11149. (let* ((lines (split-string txt "\n"))
  11150. first)
  11151. (setq first (car lines) lines (cdr lines))
  11152. (if (string-match "^\\*+ " first)
  11153. ;; Is already a headline
  11154. (setq indent nil)
  11155. ;; We need to add a headline: Use time and first buffer line
  11156. (setq lines (cons first lines)
  11157. first (concat "* " (current-time-string)
  11158. " (" (remember-buffer-desc) ")")
  11159. indent " "))
  11160. (if (and org-adapt-indentation indent)
  11161. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  11162. (setq txt (concat first "\n"
  11163. (mapconcat 'identity lines "\n"))))
  11164. ;; Find the file
  11165. (if (not visiting) (find-file-noselect file))
  11166. (with-current-buffer (or visiting (get-file-buffer file))
  11167. (save-excursion
  11168. (save-restriction
  11169. (widen)
  11170. (and (goto-char (point-min))
  11171. (not (re-search-forward "^\\* " nil t))
  11172. (insert "\n* Notes\n"))
  11173. (setq reversed (org-notes-order-reversed-p))
  11174. ;; Find the default location
  11175. (when (and heading (stringp heading) (string-match "\\S-" heading))
  11176. (goto-char (point-min))
  11177. (if (re-search-forward
  11178. (concat "^\\*+[ \t]+" (regexp-quote heading)
  11179. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  11180. nil t)
  11181. (setq org-goto-start-pos (match-beginning 0))))
  11182. ;; Ask the User for a location
  11183. (setq spos (if fastp
  11184. org-goto-start-pos
  11185. (org-get-location (current-buffer) org-remember-help)))
  11186. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  11187. ; not handle this note
  11188. (goto-char spos)
  11189. (cond ((and (bobp) (not reversed))
  11190. ;; Put it at the end, one level below level 1
  11191. (save-restriction
  11192. (widen)
  11193. (goto-char (point-max))
  11194. (if (not (bolp)) (newline))
  11195. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  11196. ((and (bobp) reversed)
  11197. ;; Put it at the start, as level 1
  11198. (save-restriction
  11199. (widen)
  11200. (goto-char (point-min))
  11201. (re-search-forward "^\\*+ " nil t)
  11202. (beginning-of-line 1)
  11203. (org-paste-subtree 1 txt)))
  11204. ((and (org-on-heading-p t) (not current-prefix-arg))
  11205. ;; Put it below this entry, at the beg/end of the subtree
  11206. (org-back-to-heading t)
  11207. (setq level (funcall outline-level))
  11208. (if reversed
  11209. (outline-next-heading)
  11210. (org-end-of-subtree t))
  11211. (if (not (bolp)) (newline))
  11212. (beginning-of-line 1)
  11213. (org-paste-subtree (org-get-legal-level level 1) txt))
  11214. (t
  11215. ;; Put it right there, with automatic level determined by
  11216. ;; org-paste-subtree or from prefix arg
  11217. (org-paste-subtree
  11218. (if (numberp current-prefix-arg) current-prefix-arg)
  11219. txt)))
  11220. (when remember-save-after-remembering
  11221. (save-buffer)
  11222. (if (not visiting) (kill-buffer (current-buffer)))))))))
  11223. t) ;; return t to indicate that we took care of this note.
  11224. (defun org-get-org-file ()
  11225. "Read a filename, with default directory `org-directory'."
  11226. (let ((default (or org-default-notes-file remember-data-file)))
  11227. (read-file-name (format "File name [%s]: " default)
  11228. (file-name-as-directory org-directory)
  11229. default)))
  11230. (defun org-notes-order-reversed-p ()
  11231. "Check if the current file should receive notes in reversed order."
  11232. (cond
  11233. ((not org-reverse-note-order) nil)
  11234. ((eq t org-reverse-note-order) t)
  11235. ((not (listp org-reverse-note-order)) nil)
  11236. (t (catch 'exit
  11237. (let ((all org-reverse-note-order)
  11238. entry)
  11239. (while (setq entry (pop all))
  11240. (if (string-match (car entry) buffer-file-name)
  11241. (throw 'exit (cdr entry))))
  11242. nil)))))
  11243. ;;;; Dynamic blocks
  11244. (defun org-find-dblock (name)
  11245. "Find the first dynamic block with name NAME in the buffer.
  11246. If not found, stay at current position and return nil."
  11247. (let (pos)
  11248. (save-excursion
  11249. (goto-char (point-min))
  11250. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  11251. nil t)
  11252. (match-beginning 0))))
  11253. (if pos (goto-char pos))
  11254. pos))
  11255. (defconst org-dblock-start-re
  11256. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  11257. "Matches the startline of a dynamic block, with parameters.")
  11258. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  11259. "Matches the end of a dyhamic block.")
  11260. (defun org-create-dblock (plist)
  11261. "Create a dynamic block section, with parameters taken from PLIST.
  11262. PLIST must containe a :name entry which is used as name of the block."
  11263. (unless (bolp) (newline))
  11264. (let ((name (plist-get plist :name)))
  11265. (insert "#+BEGIN: " name)
  11266. (while plist
  11267. (if (eq (car plist) :name)
  11268. (setq plist (cddr plist))
  11269. (insert " " (prin1-to-string (pop plist)))))
  11270. (insert "\n\n#+END:\n")
  11271. (beginning-of-line -2)))
  11272. (defun org-prepare-dblock ()
  11273. "Prepare dynamic block for refresh.
  11274. This empties the block, puts the cursor at the insert position and returns
  11275. the property list including an extra property :name with the block name."
  11276. (unless (looking-at org-dblock-start-re)
  11277. (error "Not at a dynamic block"))
  11278. (let* ((begdel (1+ (match-end 0)))
  11279. (name (match-string 1))
  11280. (params (append (list :name name)
  11281. (read (concat "(" (match-string 3) ")")))))
  11282. (unless (re-search-forward org-dblock-end-re nil t)
  11283. (error "Dynamic block not terminated"))
  11284. (delete-region begdel (match-beginning 0))
  11285. (goto-char begdel)
  11286. (open-line 1)
  11287. params))
  11288. (defun org-map-dblocks (&optional command)
  11289. "Apply COMMAND to all dynamic blocks in the current buffer.
  11290. If COMMAND is not given, use `org-update-dblock'."
  11291. (let ((cmd (or command 'org-update-dblock))
  11292. pos)
  11293. (save-excursion
  11294. (goto-char (point-min))
  11295. (while (re-search-forward org-dblock-start-re nil t)
  11296. (goto-char (setq pos (match-beginning 0)))
  11297. (condition-case nil
  11298. (funcall cmd)
  11299. (error (message "Error during update of dynamic block")))
  11300. (goto-char pos)
  11301. (unless (re-search-forward org-dblock-end-re nil t)
  11302. (error "Dynamic block not terminated"))))))
  11303. (defun org-dblock-update (&optional arg)
  11304. "User command for updating dynamic blocks.
  11305. Update the dynamic block at point. With prefix ARG, update all dynamic
  11306. blocks in the buffer."
  11307. (interactive "P")
  11308. (if arg
  11309. (org-update-all-dblocks)
  11310. (or (looking-at org-dblock-start-re)
  11311. (org-beginning-of-dblock))
  11312. (org-update-dblock)))
  11313. (defun org-update-dblock ()
  11314. "Update the dynamic block at point
  11315. This means to empty the block, parse for parameters and then call
  11316. the correct writing function."
  11317. (let* ((pos (point))
  11318. (params (org-prepare-dblock))
  11319. (name (plist-get params :name))
  11320. (cmd (intern (concat "org-dblock-write:" name))))
  11321. (funcall cmd params)
  11322. (goto-char pos)))
  11323. (defun org-beginning-of-dblock ()
  11324. "Find the beginning of the dynamic block at point.
  11325. Error if there is no scuh block at point."
  11326. (let ((pos (point))
  11327. beg)
  11328. (end-of-line 1)
  11329. (if (and (re-search-backward org-dblock-start-re nil t)
  11330. (setq beg (match-beginning 0))
  11331. (re-search-forward org-dblock-end-re nil t)
  11332. (> (match-end 0) pos))
  11333. (goto-char beg)
  11334. (goto-char pos)
  11335. (error "Not in a dynamic block"))))
  11336. (defun org-update-all-dblocks ()
  11337. "Update all dynamic blocks in the buffer.
  11338. This function can be used in a hook."
  11339. (when (org-mode-p)
  11340. (org-map-dblocks 'org-update-dblock)))
  11341. ;;;; Completion
  11342. (defun org-complete (&optional arg)
  11343. "Perform completion on word at point.
  11344. At the beginning of a headline, this completes TODO keywords as given in
  11345. `org-todo-keywords'.
  11346. If the current word is preceded by a backslash, completes the TeX symbols
  11347. that are supported for HTML support.
  11348. If the current word is preceded by \"#+\", completes special words for
  11349. setting file options.
  11350. In the line after \"#+STARTUP:, complete valid keywords.\"
  11351. At all other locations, this simply calls `ispell-complete-word'."
  11352. (interactive "P")
  11353. (catch 'exit
  11354. (let* ((end (point))
  11355. (beg1 (save-excursion
  11356. (skip-chars-backward (org-re "[:alnum:]_@"))
  11357. (point)))
  11358. (beg (save-excursion
  11359. (skip-chars-backward "a-zA-Z0-9_:$")
  11360. (point)))
  11361. (confirm (lambda (x) (stringp (car x))))
  11362. (searchhead (equal (char-before beg) ?*))
  11363. (tag (and (equal (char-before beg1) ?:)
  11364. (equal (char-after (point-at-bol)) ?*)))
  11365. (prop (and (equal (char-before beg1) ?:)
  11366. (not (equal (char-after (point-at-bol)) ?*))))
  11367. (texp (equal (char-before beg) ?\\))
  11368. (link (equal (char-before beg) ?\[))
  11369. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  11370. beg)
  11371. "#+"))
  11372. (startup (string-match "^#\\+STARTUP:.*"
  11373. (buffer-substring (point-at-bol) (point))))
  11374. (completion-ignore-case opt)
  11375. (type nil)
  11376. (tbl nil)
  11377. (table (cond
  11378. (opt
  11379. (setq type :opt)
  11380. (mapcar (lambda (x)
  11381. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  11382. (cons (match-string 2 x) (match-string 1 x)))
  11383. (org-split-string (org-get-current-options) "\n")))
  11384. (startup
  11385. (setq type :startup)
  11386. org-startup-options)
  11387. (link (append org-link-abbrev-alist-local
  11388. org-link-abbrev-alist))
  11389. (texp
  11390. (setq type :tex)
  11391. org-html-entities)
  11392. ((string-match "\\`\\*+[ \t]+\\'"
  11393. (buffer-substring (point-at-bol) beg))
  11394. (setq type :todo)
  11395. (mapcar 'list org-todo-keywords-1))
  11396. (searchhead
  11397. (setq type :searchhead)
  11398. (save-excursion
  11399. (goto-char (point-min))
  11400. (while (re-search-forward org-todo-line-regexp nil t)
  11401. (push (list
  11402. (org-make-org-heading-search-string
  11403. (match-string 3) t))
  11404. tbl)))
  11405. tbl)
  11406. (tag (setq type :tag beg beg1)
  11407. (or org-tag-alist (org-get-buffer-tags)))
  11408. (prop (setq type :prop beg beg1)
  11409. (mapcar 'list (org-buffer-property-keys)))
  11410. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  11411. (pattern (buffer-substring-no-properties beg end))
  11412. (completion (try-completion pattern table confirm)))
  11413. (cond ((eq completion t)
  11414. (if (equal type :opt)
  11415. (insert (substring (cdr (assoc (upcase pattern) table))
  11416. (length pattern)))
  11417. (if (memq type '(:tag :prop)) (insert ":"))))
  11418. ((null completion)
  11419. (message "Can't find completion for \"%s\"" pattern)
  11420. (ding))
  11421. ((not (string= pattern completion))
  11422. (delete-region beg end)
  11423. (if (string-match " +$" completion)
  11424. (setq completion (replace-match "" t t completion)))
  11425. (insert completion)
  11426. (if (get-buffer-window "*Completions*")
  11427. (delete-window (get-buffer-window "*Completions*")))
  11428. (if (assoc completion table)
  11429. (if (eq type :todo) (insert " ")
  11430. (if (memq type '(:tag :prop)) (insert ":"))))
  11431. (if (and (equal type :opt) (assoc completion table))
  11432. (message "%s" (substitute-command-keys
  11433. "Press \\[org-complete] again to insert example settings"))))
  11434. (t
  11435. (message "Making completion list...")
  11436. (let ((list (sort (all-completions pattern table confirm)
  11437. 'string<)))
  11438. (with-output-to-temp-buffer "*Completions*"
  11439. (condition-case nil
  11440. ;; Protection needed for XEmacs and emacs 21
  11441. (display-completion-list list pattern)
  11442. (error (display-completion-list list)))))
  11443. (message "Making completion list...%s" "done"))))))
  11444. ;;;; TODO, DEADLINE, Comments
  11445. (defun org-toggle-comment ()
  11446. "Change the COMMENT state of an entry."
  11447. (interactive)
  11448. (save-excursion
  11449. (org-back-to-heading)
  11450. (if (looking-at (concat outline-regexp
  11451. "\\( *\\<" org-comment-string "\\>\\)"))
  11452. (replace-match "" t t nil 1)
  11453. (if (looking-at outline-regexp)
  11454. (progn
  11455. (goto-char (match-end 0))
  11456. (insert org-comment-string " "))))))
  11457. (defvar org-last-todo-state-is-todo nil
  11458. "This is non-nil when the last TODO state change led to a TODO state.
  11459. If the last change removed the TODO tag or switched to DONE, then
  11460. this is nil.")
  11461. (defun org-todo (&optional arg)
  11462. "Change the TODO state of an item.
  11463. The state of an item is given by a keyword at the start of the heading,
  11464. like
  11465. *** TODO Write paper
  11466. *** DONE Call mom
  11467. The different keywords are specified in the variable `org-todo-keywords'.
  11468. By default the available states are \"TODO\" and \"DONE\".
  11469. So for this example: when the item starts with TODO, it is changed to DONE.
  11470. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11471. DONE are present, add TODO at the beginning of the heading.
  11472. With C-u prefix arg, use completion to determine the new state.
  11473. With numeric prefix arg, switch to that state.
  11474. For calling through lisp, arg is also interpreted in the following way:
  11475. 'none -> empty state
  11476. \"\"(empty string) -> switch to empty state
  11477. 'done -> switch to DONE
  11478. 'nextset -> switch to the next set of keywords
  11479. 'previousset -> switch to the previous set of keywords
  11480. \"WAITING\" -> switch to the specified keyword, but only if it
  11481. really is a member of `org-todo-keywords'."
  11482. (interactive "P")
  11483. (save-excursion
  11484. (org-back-to-heading)
  11485. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  11486. (or (looking-at (concat " +" org-todo-regexp " *"))
  11487. (looking-at " *"))
  11488. (let* ((this (match-string 1))
  11489. (head (org-get-todo-sequence-head this))
  11490. (ass (assoc head org-todo-kwd-alist))
  11491. (interpret (nth 1 ass))
  11492. (done-word (nth 3 ass))
  11493. (final-done-word (nth 4 ass))
  11494. (last-state (or this ""))
  11495. (completion-ignore-case t)
  11496. (member (member this org-todo-keywords-1))
  11497. (tail (cdr member))
  11498. (state (cond
  11499. ((equal arg '(4))
  11500. ;; Read a state with completion
  11501. (completing-read "State: " (mapcar (lambda(x) (list x))
  11502. org-todo-keywords-1)
  11503. nil t))
  11504. ((eq arg 'right)
  11505. (if this
  11506. (if tail (car tail) nil)
  11507. (car org-todo-keywords-1)))
  11508. ((eq arg 'left)
  11509. (if (equal member org-todo-keywords-1)
  11510. nil
  11511. (if this
  11512. (nth (- (length org-todo-keywords-1) (length tail) 2)
  11513. org-todo-keywords-1)
  11514. (org-last org-todo-keywords-1))))
  11515. (arg
  11516. ;; user or caller requests a specific state
  11517. (cond
  11518. ((equal arg "") nil)
  11519. ((eq arg 'none) nil)
  11520. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11521. ((eq arg 'nextset)
  11522. (or (car (cdr (member head org-todo-heads)))
  11523. (car org-todo-heads)))
  11524. ((eq arg 'previousset)
  11525. (let ((org-todo-heads (reverse org-todo-heads)))
  11526. (or (car (cdr (member head org-todo-heads)))
  11527. (car org-todo-heads))))
  11528. ((car (member arg org-todo-keywords-1)))
  11529. ((nth (1- (prefix-numeric-value arg))
  11530. org-todo-keywords-1))))
  11531. ((null member) (or head (car org-todo-keywords-1)))
  11532. ((equal this final-done-word) nil) ;; -> make empty
  11533. ((null tail) nil) ;; -> first entry
  11534. ((eq interpret 'sequence)
  11535. (car tail))
  11536. ((memq interpret '(type priority))
  11537. (if (eq this-command last-command)
  11538. (car tail)
  11539. (if (> (length tail) 0)
  11540. (or done-word (car org-done-keywords))
  11541. nil)))
  11542. (t nil)))
  11543. (next (if state (concat " " state " ") " "))
  11544. dostates)
  11545. (replace-match next t t)
  11546. (unless head
  11547. (setq head (org-get-todo-sequence-head state)
  11548. ass (assoc head org-todo-kwd-alist)
  11549. interpret (nth 1 ass)
  11550. done-word (nth 3 ass)
  11551. final-done-word (nth 4 ass)))
  11552. (when (memq arg '(nextset previousset))
  11553. (message "Keyword-Set %d/%d: %s"
  11554. (- (length org-todo-sets) -1
  11555. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  11556. (length org-todo-sets)
  11557. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  11558. (setq org-last-todo-state-is-todo
  11559. (not (member state org-done-keywords)))
  11560. (when (and org-log-done (not (memq arg '(nextset previousset))))
  11561. (setq dostates (and (eq interpret 'sequence)
  11562. (listp org-log-done) (memq 'state org-log-done)))
  11563. (cond
  11564. ((and state (not this))
  11565. ;; FIXME: should we remove CLOSED already then state is nil?
  11566. (org-add-planning-info nil nil 'closed)
  11567. (and dostates (org-add-log-maybe 'state state 'findpos)))
  11568. ((and state dostates)
  11569. (org-add-log-maybe 'state state 'findpos))
  11570. ((member state org-done-keywords)
  11571. ;; Planning info calls the note-setting command.
  11572. (org-add-planning-info 'closed (org-current-time)
  11573. (if (org-get-repeat) nil 'scheduled))
  11574. (org-add-log-maybe 'done state 'findpos))))
  11575. ;; Fixup tag positioning
  11576. (and org-auto-align-tags (org-set-tags nil t))
  11577. (run-hooks 'org-after-todo-state-change-hook)
  11578. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  11579. (if (and arg (not (member state org-done-keywords)))
  11580. (setq head (org-get-todo-sequence-head state)))
  11581. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  11582. ;; Fixup cursor location if close to the keyword
  11583. (if (and (outline-on-heading-p)
  11584. (not (bolp))
  11585. (save-excursion (beginning-of-line 1)
  11586. (looking-at org-todo-line-regexp))
  11587. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11588. (progn
  11589. (goto-char (or (match-end 2) (match-end 1)))
  11590. (just-one-space))))
  11591. (defun org-get-todo-sequence-head (kwd)
  11592. "Return the head of the TODO sequence to which KWD belongs.
  11593. If KWD is not set, check if there is a text property remembering the
  11594. right sequence."
  11595. (let (p)
  11596. (cond
  11597. ((not kwd)
  11598. (or (get-text-property (point-at-bol) 'org-todo-head)
  11599. (progn
  11600. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11601. nil (point-at-eol)))
  11602. (get-text-property p 'org-todo-head))))
  11603. ((not (member kwd org-todo-keywords-1))
  11604. (car org-todo-keywords-1))
  11605. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11606. (defun org-get-repeat ()
  11607. "Check if tere is a deadline/schedule with repeater in this entry."
  11608. (save-match-data
  11609. (save-excursion
  11610. (org-back-to-heading t)
  11611. (if (re-search-forward
  11612. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  11613. (match-string 1)))))
  11614. (defvar org-last-changed-timestamp)
  11615. (defvar org-log-post-message)
  11616. (defun org-auto-repeat-maybe ()
  11617. "Check if the current headline contains a repeated deadline/schedule.
  11618. If yes, set TODO state back to what it was and change the base date
  11619. of repeating deadline/scheduled time stamps to new date.
  11620. This function should be run in the `org-after-todo-state-change-hook'."
  11621. ;; last-state is dynamically scoped into this function
  11622. (let* ((repeat (org-get-repeat))
  11623. (aa (assoc last-state org-todo-kwd-alist))
  11624. (interpret (nth 1 aa))
  11625. (head (nth 2 aa))
  11626. (done-word (nth 3 aa))
  11627. (whata '(("d" . day) ("m" . month) ("y" . year)))
  11628. (msg "Entry repeats: ")
  11629. (org-log-done)
  11630. re type n what ts)
  11631. (when repeat
  11632. (org-todo (if (eq interpret 'type) last-state head))
  11633. (when (and org-log-repeat
  11634. (not (memq 'org-add-log-note
  11635. (default-value 'post-command-hook))))
  11636. ;; Make sure a note is taken
  11637. (let ((org-log-done '(done)))
  11638. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  11639. 'findpos)))
  11640. (org-back-to-heading t)
  11641. (org-add-planning-info nil nil 'closed)
  11642. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11643. org-deadline-time-regexp "\\)"))
  11644. (while (re-search-forward
  11645. re (save-excursion (outline-next-heading) (point)) t)
  11646. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  11647. ts (match-string (if (match-end 2) 2 4)))
  11648. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  11649. (setq n (string-to-number (match-string 1 ts))
  11650. what (match-string 2 ts))
  11651. (if (equal what "w") (setq n (* n 7) what "d"))
  11652. (org-timestamp-change n (cdr (assoc what whata))))
  11653. (setq msg (concat msg type org-last-changed-timestamp " ")))
  11654. (setq org-log-post-message msg)
  11655. (message msg))))
  11656. (defun org-show-todo-tree (arg)
  11657. "Make a compact tree which shows all headlines marked with TODO.
  11658. The tree will show the lines where the regexp matches, and all higher
  11659. headlines above the match.
  11660. With \\[universal-argument] prefix, also show the DONE entries.
  11661. With a numeric prefix N, construct a sparse tree for the Nth element
  11662. of `org-todo-keywords-1'."
  11663. (interactive "P")
  11664. (let ((case-fold-search nil)
  11665. (kwd-re
  11666. (cond ((null arg) org-not-done-regexp)
  11667. ((equal arg '(4))
  11668. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  11669. (mapcar 'list org-todo-keywords-1))))
  11670. (concat "\\("
  11671. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11672. "\\)\\>")))
  11673. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11674. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11675. org-todo-keywords-1)))
  11676. (t (error "Invalid prefix argument: %s" arg)))))
  11677. (message "%d TODO entries found"
  11678. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  11679. (defun org-deadline ()
  11680. "Insert the DEADLINE: string to make a deadline.
  11681. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11682. to modify it to the correct date."
  11683. (interactive)
  11684. (org-add-planning-info 'deadline nil 'closed))
  11685. (defun org-schedule ()
  11686. "Insert the SCHEDULED: string to schedule a TODO item.
  11687. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  11688. to modify it to the correct date."
  11689. (interactive)
  11690. (org-add-planning-info 'scheduled nil 'closed))
  11691. (defun org-add-planning-info (what &optional time &rest remove)
  11692. "Insert new timestamp with keyword in the line directly after the headline.
  11693. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  11694. If non is given, the user is prompted for a date.
  11695. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11696. be removed."
  11697. (interactive)
  11698. (let (org-time-was-given org-end-time-was-given)
  11699. (when what (setq time (or time (org-read-date nil 'to-time))))
  11700. (when (and org-insert-labeled-timestamps-at-point
  11701. (member what '(scheduled deadline)))
  11702. (insert
  11703. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11704. (org-insert-time-stamp time org-time-was-given
  11705. nil nil nil (list org-end-time-was-given))
  11706. (setq what nil))
  11707. (save-excursion
  11708. (save-restriction
  11709. (let (col list elt ts buffer-invisibility-spec)
  11710. (org-back-to-heading t)
  11711. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  11712. (goto-char (match-end 1))
  11713. (setq col (current-column))
  11714. (goto-char (match-end 0))
  11715. (if (eobp) (insert "\n"))
  11716. (forward-char 1)
  11717. (when (and (not org-insert-labeled-timestamps-before-properties-drawer)
  11718. (looking-at "[ \t]*:PROPERTIES:[ \t]*$"))
  11719. (goto-char (match-end 0))
  11720. (if (eobp) (insert "\n"))
  11721. (forward-char 1))
  11722. (if (and (not (looking-at outline-regexp))
  11723. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11724. "[^\r\n]*"))
  11725. (not (equal (match-string 1) org-clock-string)))
  11726. (narrow-to-region (match-beginning 0) (match-end 0))
  11727. (insert-before-markers "\n")
  11728. (backward-char 1)
  11729. (narrow-to-region (point) (point))
  11730. (indent-to-column col))
  11731. ;; Check if we have to remove something.
  11732. (setq list (cons what remove))
  11733. (while list
  11734. (setq elt (pop list))
  11735. (goto-char (point-min))
  11736. (when (or (and (eq elt 'scheduled)
  11737. (re-search-forward org-scheduled-time-regexp nil t))
  11738. (and (eq elt 'deadline)
  11739. (re-search-forward org-deadline-time-regexp nil t))
  11740. (and (eq elt 'closed)
  11741. (re-search-forward org-closed-time-regexp nil t)))
  11742. (replace-match "")
  11743. (if (looking-at "--+<[^>]+>") (replace-match ""))
  11744. (if (looking-at " +") (replace-match ""))))
  11745. (goto-char (point-max))
  11746. (when what
  11747. (insert
  11748. (if (not (equal (char-before) ?\ )) " " "")
  11749. (cond ((eq what 'scheduled) org-scheduled-string)
  11750. ((eq what 'deadline) org-deadline-string)
  11751. ((eq what 'closed) org-closed-string)
  11752. ((eq what 'archived) org-archived-string))
  11753. " ")
  11754. (org-insert-time-stamp
  11755. time
  11756. (or org-time-was-given
  11757. (and (eq what 'closed) org-log-done-with-time))
  11758. (eq what 'closed)
  11759. nil nil (list org-end-time-was-given))
  11760. (end-of-line 1))
  11761. (goto-char (point-min))
  11762. (widen)
  11763. (if (looking-at "[ \t]+\r?\n")
  11764. (replace-match ""))
  11765. ts)))))
  11766. (defvar org-log-note-marker (make-marker))
  11767. (defvar org-log-note-purpose nil)
  11768. (defvar org-log-note-state nil)
  11769. (defvar org-log-note-window-configuration nil)
  11770. (defvar org-log-note-return-to (make-marker))
  11771. (defvar org-log-post-message nil
  11772. "Message to be displayed after a log note has been stored.
  11773. The auto-repeater uses this.")
  11774. (defun org-add-log-maybe (&optional purpose state findpos)
  11775. "Set up the post command hook to take a note."
  11776. (save-excursion
  11777. (when (and (listp org-log-done)
  11778. (memq purpose org-log-done))
  11779. (when findpos
  11780. (org-back-to-heading t)
  11781. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  11782. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11783. "[^\r\n]*\\)?"))
  11784. (goto-char (match-end 0))
  11785. (unless org-log-states-order-reversed
  11786. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11787. (while (looking-at "[ \t]*- State")
  11788. (condition-case nil
  11789. (org-next-item)
  11790. (error (org-end-of-item))))
  11791. (skip-chars-backward " \t\n\r")))
  11792. (move-marker org-log-note-marker (point))
  11793. (setq org-log-note-purpose purpose)
  11794. (setq org-log-note-state state)
  11795. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  11796. (defun org-add-log-note (&optional purpose)
  11797. "Pop up a window for taking a note, and add this note later at point."
  11798. (remove-hook 'post-command-hook 'org-add-log-note)
  11799. (setq org-log-note-window-configuration (current-window-configuration))
  11800. (delete-other-windows)
  11801. (move-marker org-log-note-return-to (point))
  11802. (switch-to-buffer (marker-buffer org-log-note-marker))
  11803. (goto-char org-log-note-marker)
  11804. (switch-to-buffer-other-window "*Org Note*")
  11805. (erase-buffer)
  11806. (let ((org-inhibit-startup t)) (org-mode))
  11807. (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
  11808. (cond
  11809. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11810. ((eq org-log-note-purpose 'done) "closed todo item")
  11811. ((eq org-log-note-purpose 'state) "state change")
  11812. (t (error "This should not happen")))))
  11813. (org-set-local 'org-finish-function 'org-store-log-note))
  11814. (defun org-store-log-note ()
  11815. "Finish taking a log note, and insert it to where it belongs."
  11816. (let ((txt (buffer-string))
  11817. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11818. lines ind)
  11819. (kill-buffer (current-buffer))
  11820. (if (string-match "^#.*\n[ \t\n]*" txt)
  11821. (setq txt (replace-match "" t t txt)))
  11822. (if (string-match "\\s-+\\'" txt)
  11823. (setq txt (replace-match "" t t txt)))
  11824. (setq lines (org-split-string txt "\n"))
  11825. (when (and note (string-match "\\S-" note))
  11826. (setq note
  11827. (org-replace-escapes
  11828. note
  11829. (list (cons "%u" (user-login-name))
  11830. (cons "%U" user-full-name)
  11831. (cons "%t" (format-time-string
  11832. (org-time-stamp-format 'long 'inactive)
  11833. (current-time)))
  11834. (cons "%s" (if org-log-note-state
  11835. (concat "\"" org-log-note-state "\"")
  11836. "")))))
  11837. (if lines (setq note (concat note " \\\\")))
  11838. (push note lines))
  11839. (when lines
  11840. (save-excursion
  11841. (set-buffer (marker-buffer org-log-note-marker))
  11842. (save-excursion
  11843. (goto-char org-log-note-marker)
  11844. (move-marker org-log-note-marker nil)
  11845. (end-of-line 1)
  11846. (if (not (bolp)) (insert "\n")) (indent-relative nil)
  11847. (insert " - " (pop lines))
  11848. (org-indent-line-function)
  11849. (beginning-of-line 1)
  11850. (looking-at "[ \t]*")
  11851. (setq ind (concat (match-string 0) " "))
  11852. (end-of-line 1)
  11853. (while lines (insert "\n" ind (pop lines)))))))
  11854. (set-window-configuration org-log-note-window-configuration)
  11855. (with-current-buffer (marker-buffer org-log-note-return-to)
  11856. (goto-char org-log-note-return-to))
  11857. (move-marker org-log-note-return-to nil)
  11858. (and org-log-post-message (message org-log-post-message)))
  11859. (defvar org-occur-highlights nil)
  11860. (make-variable-buffer-local 'org-occur-highlights)
  11861. (defun org-occur (regexp &optional keep-previous callback)
  11862. "Make a compact tree which shows all matches of REGEXP.
  11863. The tree will show the lines where the regexp matches, and all higher
  11864. headlines above the match. It will also show the heading after the match,
  11865. to make sure editing the matching entry is easy.
  11866. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11867. call to `org-occur' will be kept, to allow stacking of calls to this
  11868. command.
  11869. If CALLBACK is non-nil, it is a function which is called to confirm
  11870. that the match should indeed be shown."
  11871. (interactive "sRegexp: \nP")
  11872. (or keep-previous (org-remove-occur-highlights nil nil t))
  11873. (let ((cnt 0))
  11874. (save-excursion
  11875. (goto-char (point-min))
  11876. (if (or (not keep-previous) ; do not want to keep
  11877. (not org-occur-highlights)) ; no previous matches
  11878. ;; hide everything
  11879. (org-overview))
  11880. (while (re-search-forward regexp nil t)
  11881. (when (or (not callback)
  11882. (save-match-data (funcall callback)))
  11883. (setq cnt (1+ cnt))
  11884. (when org-highlight-sparse-tree-matches
  11885. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11886. (org-show-context 'occur-tree))))
  11887. (when org-remove-highlights-with-change
  11888. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11889. nil 'local))
  11890. (unless org-sparse-tree-open-archived-trees
  11891. (org-hide-archived-subtrees (point-min) (point-max)))
  11892. (run-hooks 'org-occur-hook)
  11893. (if (interactive-p)
  11894. (message "%d match(es) for regexp %s" cnt regexp))
  11895. cnt))
  11896. (defun org-show-context (&optional key)
  11897. "Make sure point and context and visible.
  11898. How much context is shown depends upon the variables
  11899. `org-show-hierarchy-above', `org-show-following-heading'. and
  11900. `org-show-siblings'."
  11901. (let ((heading-p (org-on-heading-p t))
  11902. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11903. (following-p (org-get-alist-option org-show-following-heading key))
  11904. (siblings-p (org-get-alist-option org-show-siblings key)))
  11905. (catch 'exit
  11906. ;; Show heading or entry text
  11907. (if heading-p
  11908. (org-flag-heading nil) ; only show the heading
  11909. (and (or (org-invisible-p) (org-invisible-p2))
  11910. (org-show-hidden-entry))) ; show entire entry
  11911. (when following-p
  11912. ;; Show next sibling, or heading below text
  11913. (save-excursion
  11914. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11915. (org-flag-heading nil))))
  11916. (when siblings-p (org-show-siblings))
  11917. (when hierarchy-p
  11918. ;; show all higher headings, possibly with siblings
  11919. (save-excursion
  11920. (while (and (condition-case nil
  11921. (progn (org-up-heading-all 1) t)
  11922. (error nil))
  11923. (not (bobp)))
  11924. (org-flag-heading nil)
  11925. (when siblings-p (org-show-siblings))))))))
  11926. (defun org-reveal (&optional siblings)
  11927. "Show current entry, hierarchy above it, and the following headline.
  11928. This can be used to show a consistent set of context around locations
  11929. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11930. not t for the search context.
  11931. With optional argument SIBLINGS, on each level of the hierarchy all
  11932. siblings are shown. This repairs the tree structure to what it would
  11933. look like when opened with hierarchical calls to `org-cycle'."
  11934. (interactive "P")
  11935. (let ((org-show-hierarchy-above t)
  11936. (org-show-following-heading t)
  11937. (org-show-siblings (if siblings t org-show-siblings)))
  11938. (org-show-context nil)))
  11939. (defun org-highlight-new-match (beg end)
  11940. "Highlight from BEG to END and mark the highlight is an occur headline."
  11941. (let ((ov (org-make-overlay beg end)))
  11942. (org-overlay-put ov 'face 'secondary-selection)
  11943. (push ov org-occur-highlights)))
  11944. (defun org-remove-occur-highlights (&optional beg end noremove)
  11945. "Remove the occur highlights from the buffer.
  11946. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11947. from the `before-change-functions' in the current buffer."
  11948. (interactive)
  11949. (unless org-inhibit-highlight-removal
  11950. (mapc 'org-delete-overlay org-occur-highlights)
  11951. (setq org-occur-highlights nil)
  11952. (unless noremove
  11953. (remove-hook 'before-change-functions
  11954. 'org-remove-occur-highlights 'local))))
  11955. ;;;; Priorities
  11956. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11957. "Regular expression matching the priority indicator.")
  11958. (defvar org-remove-priority-next-time nil)
  11959. (defun org-priority-up ()
  11960. "Increase the priority of the current item."
  11961. (interactive)
  11962. (org-priority 'up))
  11963. (defun org-priority-down ()
  11964. "Decrease the priority of the current item."
  11965. (interactive)
  11966. (org-priority 'down))
  11967. (defun org-priority (&optional action)
  11968. "Change the priority of an item by ARG.
  11969. ACTION can be `set', `up', `down', or a character."
  11970. (interactive)
  11971. (setq action (or action 'set))
  11972. (let (current new news have remove)
  11973. (save-excursion
  11974. (org-back-to-heading)
  11975. (if (looking-at org-priority-regexp)
  11976. (setq current (string-to-char (match-string 2))
  11977. have t)
  11978. (setq current org-default-priority))
  11979. (cond
  11980. ((or (eq action 'set) (integerp action))
  11981. (if (integerp action)
  11982. (setq new action)
  11983. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  11984. (setq new (read-char-exclusive)))
  11985. (cond ((equal new ?\ ) (setq remove t))
  11986. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11987. (error "Priority must be between `%c' and `%c'"
  11988. org-highest-priority org-lowest-priority))))
  11989. ((eq action 'up)
  11990. (setq new (1- current)))
  11991. ((eq action 'down)
  11992. (setq new (1+ current)))
  11993. (t (error "Invalid action")))
  11994. (setq new (min (max org-highest-priority (upcase new)) org-lowest-priority))
  11995. (setq news (format "%c" new))
  11996. (if have
  11997. (if remove
  11998. (replace-match "" t t nil 1)
  11999. (replace-match news t t nil 2))
  12000. (if remove
  12001. (error "No priority cookie found in line")
  12002. (looking-at org-todo-line-regexp)
  12003. (if (match-end 2)
  12004. (progn
  12005. (goto-char (match-end 2))
  12006. (insert " [#" news "]"))
  12007. (goto-char (match-beginning 3))
  12008. (insert "[#" news "] ")))))
  12009. (org-preserve-lc (org-set-tags nil 'align))
  12010. (if remove
  12011. (message "Priority removed")
  12012. (message "Priority of current item set to %s" news))))
  12013. (defun org-get-priority (s)
  12014. "Find priority cookie and return priority."
  12015. (save-match-data
  12016. (if (not (string-match org-priority-regexp s))
  12017. (* 1000 (- org-lowest-priority org-default-priority))
  12018. (* 1000 (- org-lowest-priority
  12019. (string-to-char (match-string 2 s)))))))
  12020. ;;;; Tags
  12021. (defun org-scan-tags (action matcher &optional todo-only)
  12022. "Scan headline tags with inheritance and produce output ACTION.
  12023. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  12024. evaluated, testing if a given set of tags qualifies a headline for
  12025. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  12026. are included in the output."
  12027. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  12028. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12029. (org-re
  12030. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  12031. (props (list 'face nil
  12032. 'done-face 'org-done
  12033. 'undone-face nil
  12034. 'mouse-face 'highlight
  12035. 'org-not-done-regexp org-not-done-regexp
  12036. 'org-todo-regexp org-todo-regexp
  12037. 'keymap org-agenda-keymap
  12038. 'help-echo
  12039. (format "mouse-2 or RET jump to org file %s"
  12040. (abbreviate-file-name buffer-file-name))))
  12041. (case-fold-search nil)
  12042. lspos
  12043. tags tags-list tags-alist (llast 0) rtn level category i txt
  12044. todo marker entry priority)
  12045. (save-excursion
  12046. (goto-char (point-min))
  12047. (when (eq action 'sparse-tree) (org-overview))
  12048. (while (re-search-forward re nil t)
  12049. (catch :skip
  12050. (setq todo (if (match-end 1) (match-string 2))
  12051. tags (if (match-end 4) (match-string 4)))
  12052. (goto-char (setq lspos (1+ (match-beginning 0))))
  12053. (setq level (org-reduced-level (funcall outline-level))
  12054. category (org-get-category))
  12055. (setq i llast llast level)
  12056. ;; remove tag lists from same and sublevels
  12057. (while (>= i level)
  12058. (when (setq entry (assoc i tags-alist))
  12059. (setq tags-alist (delete entry tags-alist)))
  12060. (setq i (1- i)))
  12061. ;; add the nex tags
  12062. (when tags
  12063. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  12064. tags-alist
  12065. (cons (cons level tags) tags-alist)))
  12066. ;; compile tags for current headline
  12067. (setq tags-list
  12068. (if org-use-tag-inheritance
  12069. (apply 'append (mapcar 'cdr tags-alist))
  12070. tags))
  12071. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  12072. (eval matcher)
  12073. (or (not org-agenda-skip-archived-trees)
  12074. (not (member org-archive-tag tags-list))))
  12075. (and (eq action 'agenda) (org-agenda-skip))
  12076. ;; list this headline
  12077. (if (eq action 'sparse-tree)
  12078. (progn
  12079. (org-show-context 'tags-tree))
  12080. (setq txt (org-format-agenda-item
  12081. ""
  12082. (concat
  12083. (if org-tags-match-list-sublevels
  12084. (make-string (1- level) ?.) "")
  12085. (org-get-heading))
  12086. category tags-list)
  12087. priority (org-get-priority txt))
  12088. (goto-char lspos)
  12089. (setq marker (org-agenda-new-marker))
  12090. (org-add-props txt props
  12091. 'org-marker marker 'org-hd-marker marker 'org-category category
  12092. 'priority priority 'type "tagsmatch")
  12093. (push txt rtn))
  12094. ;; if we are to skip sublevels, jump to end of subtree
  12095. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  12096. (when (and (eq action 'sparse-tree)
  12097. (not org-sparse-tree-open-archived-trees))
  12098. (org-hide-archived-subtrees (point-min) (point-max)))
  12099. (nreverse rtn)))
  12100. (defvar todo-only) ;; dynamically scoped
  12101. (defun org-tags-sparse-tree (&optional todo-only match)
  12102. "Create a sparse tree according to tags string MATCH.
  12103. MATCH can contain positive and negative selection of tags, like
  12104. \"+WORK+URGENT-WITHBOSS\".
  12105. If optional argument TODO_ONLY is non-nil, only select lines that are
  12106. also TODO lines."
  12107. (interactive "P")
  12108. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12109. (defvar org-cached-props nil)
  12110. (defun org-cached-entry-get (pom property)
  12111. (cdr (assoc property (or org-cached-props
  12112. (setq org-cached-props
  12113. (org-entry-properties pom))))))
  12114. (defun org-global-tags-completion-table (&optional files)
  12115. "Return the list of all tags in all agenda buffer/files."
  12116. (save-excursion
  12117. (org-uniquify
  12118. (apply 'append
  12119. (mapcar
  12120. (lambda (file)
  12121. (set-buffer (find-file-noselect file))
  12122. (org-get-buffer-tags))
  12123. (if (and files (car files))
  12124. files
  12125. (org-agenda-files)))))))
  12126. (defun org-make-tags-matcher (match)
  12127. "Create the TAGS//TODO matcher form for the selection string MATCH."
  12128. ;; todo-only is scoped dynamically into this function, and the function
  12129. ;; may change it it the matcher asksk for it.
  12130. (unless match
  12131. ;; Get a new match request, with completion
  12132. (let ((org-last-tags-completion-table
  12133. (org-global-tags-completion-table)))
  12134. (setq match (completing-read
  12135. "Match: " 'org-tags-completion-function nil nil nil
  12136. 'org-tags-history))))
  12137. ;; Parse the string and create a lisp form
  12138. (let ((match0 match)
  12139. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  12140. minus tag mm
  12141. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12142. orterms term orlist re-p level-p prop-p pn pv)
  12143. (if (string-match "/+" match)
  12144. ;; match contains also a todo-matching request
  12145. (progn
  12146. (setq tagsmatch (substring match 0 (match-beginning 0))
  12147. todomatch (substring match (match-end 0)))
  12148. (if (string-match "^!" todomatch)
  12149. (setq todo-only t todomatch (substring todomatch 1)))
  12150. (if (string-match "^\\s-*$" todomatch)
  12151. (setq todomatch nil)))
  12152. ;; only matching tags
  12153. (setq tagsmatch match todomatch nil))
  12154. ;; Make the tags matcher
  12155. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12156. (setq tagsmatcher t)
  12157. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12158. (while (setq term (pop orterms))
  12159. (while (and (equal (substring term -1) "\\") orterms)
  12160. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12161. (while (string-match re term)
  12162. (setq minus (and (match-end 1)
  12163. (equal (match-string 1 term) "-"))
  12164. tag (match-string 2 term)
  12165. re-p (equal (string-to-char tag) ?{)
  12166. level-p (match-end 3)
  12167. prop-p (match-end 4)
  12168. mm (cond
  12169. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12170. (level-p `(= level ,(string-to-number
  12171. (match-string 3 term))))
  12172. (prop-p
  12173. (setq pn (match-string 4 term)
  12174. pv (match-string 5 term)
  12175. re-p (equal (string-to-char pv) ?{)
  12176. pv (substring pv 1 -1))
  12177. (if re-p
  12178. `(string-match ,pv (org-cached-entry-get nil ,pn))
  12179. `(equal ,pv (org-cached-entry-get nil ,pn))))
  12180. (t `(member ,(downcase tag) tags-list)))
  12181. mm (if minus (list 'not mm) mm)
  12182. term (substring term (match-end 0)))
  12183. (push mm tagsmatcher))
  12184. (push (if (> (length tagsmatcher) 1)
  12185. (cons 'and tagsmatcher)
  12186. (car tagsmatcher))
  12187. orlist)
  12188. (setq tagsmatcher nil))
  12189. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12190. (setq tagsmatcher
  12191. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12192. ;; Make the todo matcher
  12193. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12194. (setq todomatcher t)
  12195. (setq orterms (org-split-string todomatch "|") orlist nil)
  12196. (while (setq term (pop orterms))
  12197. (while (string-match re term)
  12198. (setq minus (and (match-end 1)
  12199. (equal (match-string 1 term) "-"))
  12200. kwd (match-string 2 term)
  12201. re-p (equal (string-to-char kwd) ?{)
  12202. term (substring term (match-end 0))
  12203. mm (if re-p
  12204. `(string-match ,(substring kwd 1 -1) todo)
  12205. (list 'equal 'todo kwd))
  12206. mm (if minus (list 'not mm) mm))
  12207. (push mm todomatcher))
  12208. (push (if (> (length todomatcher) 1)
  12209. (cons 'and todomatcher)
  12210. (car todomatcher))
  12211. orlist)
  12212. (setq todomatcher nil))
  12213. (setq todomatcher (if (> (length orlist) 1)
  12214. (cons 'or orlist) (car orlist))))
  12215. ;; Return the string and lisp forms of the matcher
  12216. (setq matcher (if todomatcher
  12217. (list 'and tagsmatcher todomatcher)
  12218. tagsmatcher))
  12219. (cons match0 matcher)))
  12220. (defun org-match-any-p (re list)
  12221. "Does re match any element of list?"
  12222. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12223. (delq nil list))
  12224. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  12225. (defvar org-tags-overlay (org-make-overlay 1 1))
  12226. (org-detach-overlay org-tags-overlay)
  12227. (defun org-align-tags-here (to-col)
  12228. ;; Assumes that this is a headline
  12229. (let ((pos (point)) (col (current-column)) tags)
  12230. (beginning-of-line 1)
  12231. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12232. (< pos (match-beginning 2)))
  12233. (progn
  12234. (setq tags (match-string 2))
  12235. (goto-char (match-beginning 1))
  12236. (insert " ")
  12237. (delete-region (point) (1+ (match-end 0)))
  12238. (backward-char 1)
  12239. (move-to-column
  12240. (max (1+ (current-column))
  12241. (1+ col)
  12242. (if (> to-col 0)
  12243. to-col
  12244. (- (abs to-col) (length tags))))
  12245. t)
  12246. (insert tags)
  12247. (move-to-column (min (current-column) col) t))
  12248. (goto-char pos))))
  12249. (defun org-set-tags (&optional arg just-align)
  12250. "Set the tags for the current headline.
  12251. With prefix ARG, realign all tags in headings in the current buffer."
  12252. (interactive "P")
  12253. (let* ((re (concat "^" outline-regexp))
  12254. (current (org-get-tags))
  12255. table current-tags inherited-tags ; computed below when needed
  12256. tags p0 c0 c1 rpl)
  12257. (if arg
  12258. (save-excursion
  12259. (goto-char (point-min))
  12260. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12261. (while (re-search-forward re nil t)
  12262. (org-set-tags nil t)
  12263. (end-of-line 1)))
  12264. (message "All tags realigned to column %d" org-tags-column))
  12265. (if just-align
  12266. (setq tags current)
  12267. ;; Get a new set of tags from the user
  12268. (save-excursion
  12269. (setq table (or org-tag-alist (org-get-buffer-tags))
  12270. org-last-tags-completion-table table
  12271. current-tags (org-split-string current ":")
  12272. inherited-tags (nreverse
  12273. (nthcdr (length current-tags)
  12274. (nreverse (org-get-tags-at))))
  12275. tags
  12276. (if (or (eq t org-use-fast-tag-selection)
  12277. (and org-use-fast-tag-selection
  12278. (delq nil (mapcar 'cdr table))))
  12279. (org-fast-tag-selection current-tags inherited-tags table)
  12280. (let ((org-add-colon-after-tag-completion t))
  12281. (org-trim
  12282. (completing-read "Tags: " 'org-tags-completion-function
  12283. nil nil current 'org-tags-history))))))
  12284. (while (string-match "[-+&]+" tags)
  12285. ;; No boolean logic, just a list
  12286. (setq tags (replace-match ":" t t tags))))
  12287. (if (string-match "\\`[\t ]*\\'" tags)
  12288. (setq tags "")
  12289. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12290. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12291. ;; Insert new tags at the correct column
  12292. (beginning-of-line 1)
  12293. (cond
  12294. ((and (equal current "") (equal tags "")))
  12295. ((re-search-forward
  12296. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12297. (point-at-eol) t)
  12298. (if (equal tags "")
  12299. (setq rpl "")
  12300. (goto-char (match-beginning 0))
  12301. (setq c0 (current-column) p0 (point)
  12302. c1 (max (1+ c0) (if (> org-tags-column 0)
  12303. org-tags-column
  12304. (- (- org-tags-column) (length tags))))
  12305. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12306. (replace-match rpl t t)
  12307. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  12308. tags)
  12309. (t (error "Tags alignment failed"))))))
  12310. (defun org-change-tag-in-region (beg end tag off)
  12311. "Add or remove TAG for each entry in the region.
  12312. This works in the agenda, and also in an org-mode buffer."
  12313. (interactive
  12314. (list (region-beginning) (region-end)
  12315. (let ((org-last-tags-completion-table
  12316. (if (org-mode-p)
  12317. (org-get-buffer-tags)
  12318. (org-global-tags-completion-table))))
  12319. (completing-read
  12320. "Tag: " 'org-tags-completion-function nil nil nil
  12321. 'org-tags-history))
  12322. (progn
  12323. (message "[s]et or [r]emove? ")
  12324. (equal (read-char-exclusive) ?r))))
  12325. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12326. (let ((agendap (equal major-mode 'org-agenda-mode))
  12327. l1 l2 m buf pos newhead (cnt 0))
  12328. (goto-char end)
  12329. (setq l2 (1- (org-current-line)))
  12330. (goto-char beg)
  12331. (setq l1 (org-current-line))
  12332. (loop for l from l1 to l2 do
  12333. (goto-line l)
  12334. (setq m (get-text-property (point) 'org-hd-marker))
  12335. (when (or (and (org-mode-p) (org-on-heading-p))
  12336. (and agendap m))
  12337. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12338. pos (if agendap m (point)))
  12339. (with-current-buffer buf
  12340. (save-excursion
  12341. (save-restriction
  12342. (goto-char pos)
  12343. (setq cnt (1+ cnt))
  12344. (org-toggle-tag tag (if off 'off 'on))
  12345. (setq newhead (org-get-heading)))))
  12346. (and agendap (org-agenda-change-all-lines newhead m))))
  12347. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12348. (defun org-tags-completion-function (string predicate &optional flag)
  12349. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12350. (confirm (lambda (x) (stringp (car x)))))
  12351. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  12352. (setq s1 (match-string 1 string)
  12353. s2 (match-string 2 string))
  12354. (setq s1 "" s2 string))
  12355. (cond
  12356. ((eq flag nil)
  12357. ;; try completion
  12358. (setq rtn (try-completion s2 ctable confirm))
  12359. (if (stringp rtn)
  12360. (setq rtn
  12361. (concat s1 s2 (substring rtn (length s2))
  12362. (if (and org-add-colon-after-tag-completion
  12363. (assoc rtn ctable))
  12364. ":" ""))))
  12365. rtn)
  12366. ((eq flag t)
  12367. ;; all-completions
  12368. (all-completions s2 ctable confirm)
  12369. )
  12370. ((eq flag 'lambda)
  12371. ;; exact match?
  12372. (assoc s2 ctable)))
  12373. ))
  12374. (defun org-fast-tag-insert (kwd tags face &optional end)
  12375. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  12376. (insert (format "%-12s" (concat kwd ":"))
  12377. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12378. (or end "")))
  12379. (defun org-fast-tag-show-exit (flag)
  12380. (save-excursion
  12381. (goto-line 3)
  12382. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12383. (replace-match ""))
  12384. (when flag
  12385. (end-of-line 1)
  12386. (move-to-column (- (window-width) 19) t)
  12387. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12388. (defun org-set-current-tags-overlay (current prefix)
  12389. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12390. (if (featurep 'xemacs)
  12391. (org-overlay-display org-tags-overlay (concat prefix s)
  12392. 'secondary-selection)
  12393. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12394. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12395. (defun org-fast-tag-selection (current inherited table)
  12396. "Fast tag selection with single keys.
  12397. CURRENT is the current list of tags in the headline, INHERITED is the
  12398. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12399. possibly with grouping information.
  12400. If the keys are nil, a-z are automatically assigned.
  12401. Returns the new tags string, or nil to not change the current settings."
  12402. (let* ((maxlen (apply 'max (mapcar
  12403. (lambda (x)
  12404. (if (stringp (car x)) (string-width (car x)) 0))
  12405. table)))
  12406. (buf (current-buffer))
  12407. (expert (eq org-fast-tag-selection-single-key 'expert))
  12408. (buffer-tags nil)
  12409. (fwidth (+ maxlen 3 1 3))
  12410. (ncol (/ (- (window-width) 4) fwidth))
  12411. (i-face 'org-done)
  12412. (c-face 'org-todo)
  12413. tg cnt e c char c1 c2 ntable tbl rtn
  12414. ov-start ov-end ov-prefix
  12415. (exit-after-next org-fast-tag-selection-single-key)
  12416. groups ingroup)
  12417. (save-excursion
  12418. (beginning-of-line 1)
  12419. (if (looking-at
  12420. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12421. (setq ov-start (match-beginning 1)
  12422. ov-end (match-end 1)
  12423. ov-prefix "")
  12424. (setq ov-start (1- (point-at-eol))
  12425. ov-end (1+ ov-start))
  12426. (skip-chars-forward "^\n\r")
  12427. (setq ov-prefix
  12428. (concat
  12429. (buffer-substring (1- (point)) (point))
  12430. (if (> (current-column) org-tags-column)
  12431. " "
  12432. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12433. (org-move-overlay org-tags-overlay ov-start ov-end)
  12434. (save-window-excursion
  12435. (if expert
  12436. (set-buffer (get-buffer-create " *Org tags*"))
  12437. (delete-other-windows)
  12438. (split-window-vertically)
  12439. (switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12440. (erase-buffer)
  12441. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12442. (org-fast-tag-insert "Current" current c-face "\n\n")
  12443. (org-fast-tag-show-exit exit-after-next)
  12444. (org-set-current-tags-overlay current ov-prefix)
  12445. (setq tbl table char ?a cnt 0)
  12446. (while (setq e (pop tbl))
  12447. (cond
  12448. ((equal e '(:startgroup))
  12449. (push '() groups) (setq ingroup t)
  12450. (when (not (= cnt 0))
  12451. (setq cnt 0)
  12452. (insert "\n"))
  12453. (insert "{ "))
  12454. ((equal e '(:endgroup))
  12455. (setq ingroup nil cnt 0)
  12456. (insert "}\n"))
  12457. (t
  12458. (setq tg (car e) c2 nil)
  12459. (if (cdr e)
  12460. (setq c (cdr e))
  12461. ;; automatically assign a character.
  12462. (setq c1 (string-to-char
  12463. (downcase (substring
  12464. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12465. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12466. (while (or (rassoc char ntable) (rassoc char table))
  12467. (setq char (1+ char)))
  12468. (setq c2 c1))
  12469. (setq c (or c2 char)))
  12470. (if ingroup (push tg (car groups)))
  12471. (setq tg (org-add-props tg nil 'face
  12472. (cond
  12473. ((member tg current) c-face)
  12474. ((member tg inherited) i-face)
  12475. (t nil))))
  12476. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12477. (insert "[" c "] " tg (make-string
  12478. (- fwidth 4 (length tg)) ?\ ))
  12479. (push (cons tg c) ntable)
  12480. (when (= (setq cnt (1+ cnt)) ncol)
  12481. (insert "\n")
  12482. (if ingroup (insert " "))
  12483. (setq cnt 0)))))
  12484. (setq ntable (nreverse ntable))
  12485. (insert "\n")
  12486. (goto-char (point-min))
  12487. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12488. (fit-window-to-buffer))
  12489. (setq rtn
  12490. (catch 'exit
  12491. (while t
  12492. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  12493. (if groups " [!] no groups" " [!]groups")
  12494. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12495. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12496. (cond
  12497. ((= c ?\r) (throw 'exit t))
  12498. ((= c ?!)
  12499. (setq groups (not groups))
  12500. (goto-char (point-min))
  12501. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12502. ((= c ?\C-c)
  12503. (if (not expert)
  12504. (org-fast-tag-show-exit
  12505. (setq exit-after-next (not exit-after-next)))
  12506. (setq expert nil)
  12507. (delete-other-windows)
  12508. (split-window-vertically)
  12509. (switch-to-buffer-other-window " *Org tags*")
  12510. (and (fboundp 'fit-window-to-buffer)
  12511. (fit-window-to-buffer))))
  12512. ((or (= c ?\C-g)
  12513. (and (= c ?q) (not (rassoc c ntable))))
  12514. (org-detach-overlay org-tags-overlay)
  12515. (setq quit-flag t))
  12516. ((= c ?\ )
  12517. (setq current nil)
  12518. (if exit-after-next (setq exit-after-next 'now)))
  12519. ((= c ?\t)
  12520. (condition-case nil
  12521. (setq tg (completing-read
  12522. "Tag: "
  12523. (or buffer-tags
  12524. (with-current-buffer buf
  12525. (org-get-buffer-tags)))))
  12526. (quit (setq tg "")))
  12527. (when (string-match "\\S-" tg)
  12528. (add-to-list 'buffer-tags (list tg))
  12529. (if (member tg current)
  12530. (setq current (delete tg current))
  12531. (push tg current)))
  12532. (if exit-after-next (setq exit-after-next 'now)))
  12533. ((setq e (rassoc c ntable) tg (car e))
  12534. (if (member tg current)
  12535. (setq current (delete tg current))
  12536. (loop for g in groups do
  12537. (if (member tg g)
  12538. (mapcar (lambda (x)
  12539. (setq current (delete x current)))
  12540. g)))
  12541. (push tg current))
  12542. (if exit-after-next (setq exit-after-next 'now))))
  12543. ;; Create a sorted list
  12544. (setq current
  12545. (sort current
  12546. (lambda (a b)
  12547. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12548. (if (eq exit-after-next 'now) (throw 'exit t))
  12549. (goto-char (point-min))
  12550. (beginning-of-line 2)
  12551. (delete-region (point) (point-at-eol))
  12552. (org-fast-tag-insert "Current" current c-face)
  12553. (org-set-current-tags-overlay current ov-prefix)
  12554. (while (re-search-forward
  12555. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  12556. (setq tg (match-string 1))
  12557. (add-text-properties (match-beginning 1) (match-end 1)
  12558. (list 'face
  12559. (cond
  12560. ((member tg current) c-face)
  12561. ((member tg inherited) i-face)
  12562. (t nil)))))
  12563. (goto-char (point-min)))))
  12564. (org-detach-overlay org-tags-overlay)
  12565. (if rtn
  12566. (mapconcat 'identity current ":")
  12567. nil))))
  12568. (defun org-get-tags ()
  12569. "Get the TAGS string in the current headline."
  12570. (unless (org-on-heading-p t)
  12571. (error "Not on a heading"))
  12572. (save-excursion
  12573. (beginning-of-line 1)
  12574. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12575. (org-match-string-no-properties 1)
  12576. "")))
  12577. (defun org-get-buffer-tags ()
  12578. "Get a table of all tags used in the buffer, for completion."
  12579. (let (tags)
  12580. (save-excursion
  12581. (goto-char (point-min))
  12582. (while (re-search-forward
  12583. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  12584. (when (equal (char-after (point-at-bol 0)) ?*)
  12585. (mapc (lambda (x) (add-to-list 'tags x))
  12586. (org-split-string (org-match-string-no-properties 1) ":")))))
  12587. (mapcar 'list tags)))
  12588. ;;;; Properties
  12589. ;;; Setting and retrieving properties
  12590. (defconst org-special-properties
  12591. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED"
  12592. "CLOCK" "PRIORITY")
  12593. "The special properties valid in Org-mode.
  12594. These are properties that are not defined in the property drawer,
  12595. but in some other way.")
  12596. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12597. "Regular expression matching the first line of a property drawer.")
  12598. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12599. "Regular expression matching the first line of a property drawer.")
  12600. (defun org-property-action ()
  12601. "Do an action on properties."
  12602. (interactive)
  12603. (let (c prop)
  12604. (org-at-property-p)
  12605. (setq prop (match-string 2))
  12606. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12607. (setq c (read-char-exclusive))
  12608. (cond
  12609. ((equal c ?s)
  12610. (call-interactively 'org-set-property))
  12611. ((equal c ?d)
  12612. (call-interactively 'org-delete-property))
  12613. ((equal c ?D)
  12614. (call-interactively 'org-delete-property-globally))
  12615. ((equal c ?c)
  12616. (call-interactively 'org-compute-property-at-point))
  12617. (t (error "No such property action %c" c)))))
  12618. (defun org-at-property-p ()
  12619. "Is the cursor in a property line?"
  12620. ;; FIXME: Does not check if we are actually in the drawer.
  12621. ;; FIXME: also returns true on any drawers.....
  12622. ;; This is used by C-c C-c for property action.
  12623. (save-excursion
  12624. (beginning-of-line 1)
  12625. (looking-at "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(.*\\)")))
  12626. (defmacro org-with-point-at (pom &rest body)
  12627. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  12628. (declare (indent 1) (debug t))
  12629. `(save-excursion
  12630. (if (markerp pom) (set-buffer (marker-buffer pom)))
  12631. (save-excursion
  12632. (goto-char (or pom (point)))
  12633. ,@body)))
  12634. (defun org-get-property-block (&optional beg end force)
  12635. "Return the (beg . end) range of the body of the property drawer.
  12636. BEG and END can be beginning and end of subtree, if not given
  12637. they will be found.
  12638. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12639. (catch 'exit
  12640. (save-excursion
  12641. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12642. (end (or end (progn (outline-next-heading) (point)))))
  12643. (goto-char beg)
  12644. (if (re-search-forward org-property-start-re end t)
  12645. (setq beg (1+ (match-end 0)))
  12646. (if force
  12647. (save-excursion
  12648. (org-insert-property-drawer)
  12649. (setq end (progn (outline-next-heading) (point))))
  12650. (throw 'exit nil))
  12651. (goto-char beg)
  12652. (if (re-search-forward org-property-start-re end t)
  12653. (setq beg (1+ (match-end 0)))))
  12654. (if (re-search-forward org-property-end-re end t)
  12655. (setq end (match-beginning 0))
  12656. (or force (throw 'exit nil))
  12657. (goto-char beg)
  12658. (setq end beg)
  12659. (org-indent-line-function)
  12660. (insert ":END:\n"))
  12661. (cons beg end)))))
  12662. (defun org-entry-properties (&optional pom which)
  12663. "Get all properties of the entry at point-or-marker POM.
  12664. This includes the TODO keyword, the tags, time strings for deadline,
  12665. scheduled, and clocking, and any additional properties defined in the
  12666. entry. The return value is an alist, keys may occur multiple times
  12667. if the property key was used several times.
  12668. POM may also be nil, in which case the current entry is used.
  12669. If WHICH is nil or `all', get all properties. If WHICH is
  12670. `special' or `standard', only get that subclass."
  12671. (setq which (or which 'all))
  12672. (org-with-point-at pom
  12673. (let ((clockstr (substring org-clock-string 0 -1))
  12674. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  12675. beg end range props sum-props key value)
  12676. (save-excursion
  12677. (when (condition-case nil (org-back-to-heading t) (error nil))
  12678. (setq beg (point))
  12679. (setq sum-props (get-text-property (point) 'org-summaries))
  12680. (outline-next-heading)
  12681. (setq end (point))
  12682. (when (memq which '(all special))
  12683. ;; Get the special properties, like TODO and tags
  12684. (goto-char beg)
  12685. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  12686. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12687. (when (looking-at org-priority-regexp)
  12688. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12689. (when (and (setq value (org-get-tags)) (string-match "\\S-" value))
  12690. (push (cons "TAGS" value) props))
  12691. (when (setq value (org-get-tags-at))
  12692. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  12693. props))
  12694. (while (re-search-forward org-keyword-time-regexp end t)
  12695. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  12696. (unless (member key excluded) (push key excluded))
  12697. (push (cons key
  12698. (if (equal key clockstr)
  12699. (org-no-properties
  12700. (org-trim
  12701. (buffer-substring
  12702. (match-beginning 2) (point-at-eol))))
  12703. (org-match-string-no-properties 2)))
  12704. props)))
  12705. (when (memq which '(all standard))
  12706. ;; Get the standard properties, like :PORP: ...
  12707. (setq range (org-get-property-block beg end))
  12708. (when range
  12709. (goto-char (car range))
  12710. (while (re-search-forward
  12711. "^[ \t]*:\\([a-zA-Z][a-zA-Z_0-9]*\\):[ \t]*\\(\\S-.*\\)?"
  12712. (cdr range) t)
  12713. (setq key (org-match-string-no-properties 1)
  12714. value (org-trim (or (org-match-string-no-properties 2) "")))
  12715. (unless (member key excluded)
  12716. (push (cons key (or value "")) props)))))
  12717. (append sum-props (nreverse props)))))))
  12718. (defun org-entry-get (pom property &optional inherit)
  12719. "Get value of PROPERTY for entry at point-or-marker POM.
  12720. If INHERIT is non-nil and the entry does not have the property,
  12721. then also check higher levels of the hierarchy.
  12722. If the property is present but empty, the return value is the empty string.
  12723. If the property is not present at all, nil is returned."
  12724. (org-with-point-at pom
  12725. (if inherit
  12726. (org-entry-get-with-inheritance property)
  12727. (if (member property org-special-properties)
  12728. ;; We need a special property. Use brute force, get all properties.
  12729. (cdr (assoc property (org-entry-properties nil 'special)))
  12730. (let ((range (org-get-property-block)))
  12731. (if (and range
  12732. (goto-char (car range))
  12733. (re-search-forward
  12734. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  12735. (cdr range) t))
  12736. ;; Found the property, return it.
  12737. (if (match-end 1)
  12738. (org-match-string-no-properties 1)
  12739. "")))))))
  12740. (defun org-entry-delete (pom property)
  12741. "Delete the property PROPERTY from entry at point-or-marker POM."
  12742. (org-with-point-at pom
  12743. (if (member property org-special-properties)
  12744. nil ; cannot delete these properties.
  12745. (let ((range (org-get-property-block)))
  12746. (if (and range
  12747. (goto-char (car range))
  12748. (re-search-forward
  12749. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  12750. (cdr range) t))
  12751. (progn
  12752. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12753. t)
  12754. nil)))))
  12755. (defvar org-entry-property-inherited-from (make-marker))
  12756. (defun org-entry-get-with-inheritance (property)
  12757. "Get entry property, and search higher levels if not present."
  12758. (let (tmp)
  12759. (save-excursion
  12760. (catch 'ex
  12761. (while t
  12762. (when (setq tmp (org-entry-get nil property))
  12763. (org-back-to-heading t)
  12764. (move-marker org-entry-property-inherited-from (point))
  12765. (throw 'ex tmp))
  12766. (condition-case nil
  12767. (org-up-heading-all 1)
  12768. (error (throw 'ex nil))))))
  12769. (or tmp (cdr (assoc property org-local-properties))
  12770. (cdr (assoc property org-global-properties)))))
  12771. (defun org-entry-put (pom property value)
  12772. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12773. (org-with-point-at pom
  12774. (org-back-to-heading t)
  12775. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12776. range)
  12777. (cond
  12778. ((equal property "TODO")
  12779. (when (and (stringp value) (string-match "\\S-" value)
  12780. (not (member value org-todo-keywords-1)))
  12781. (error "\"%s\" is not a valid TODO state" value))
  12782. (if (or (not value)
  12783. (not (string-match "\\S-" value)))
  12784. (setq value 'none))
  12785. (org-todo value)
  12786. (org-set-tags nil 'align))
  12787. ((equal property "PRIORITY")
  12788. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12789. (string-to-char value) ?\ ))
  12790. (org-set-tags nil 'align))
  12791. ((equal property "SCHEDULED")
  12792. (if (re-search-forward org-scheduled-time-regexp end t)
  12793. (cond
  12794. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12795. ((eq value 'later) (org-timestamp-change 1 'day))
  12796. (t (call-interactively 'org-schedule)))
  12797. (call-interactively 'org-schedule)))
  12798. ((equal property "DEADLINE")
  12799. (if (re-search-forward org-deadline-time-regexp end t)
  12800. (cond
  12801. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12802. ((eq value 'later) (org-timestamp-change 1 'day))
  12803. (t (call-interactively 'org-deadline)))
  12804. (call-interactively 'org-deadline)))
  12805. ((member property org-special-properties)
  12806. (error "The %s property can not yet be set with `org-entry-put'"
  12807. property))
  12808. (t ; a non-special property
  12809. (setq range (org-get-property-block beg end 'force))
  12810. (goto-char (car range))
  12811. (if (re-search-forward
  12812. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12813. (progn
  12814. (delete-region (match-beginning 1) (match-end 1))
  12815. (goto-char (match-beginning 1)))
  12816. (goto-char (cdr range))
  12817. (insert "\n")
  12818. (backward-char 1)
  12819. (org-indent-line-function)
  12820. (insert ":" property ":"))
  12821. (and value (insert " " value))
  12822. (org-indent-line-function))))))
  12823. (defun org-buffer-property-keys (&optional include-specials)
  12824. "Get all property keys in the current buffer."
  12825. (let (rtn range)
  12826. (save-excursion
  12827. (save-restriction
  12828. (widen)
  12829. (goto-char (point-min))
  12830. (while (re-search-forward org-property-start-re nil t)
  12831. (setq range (org-get-property-block))
  12832. (goto-char (car range))
  12833. (while (re-search-forward "^[ \t]*:\\([a-zA-Z0-9]+\\):" (cdr range) t)
  12834. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12835. (outline-next-heading))))
  12836. (when include-specials
  12837. (setq rtn (append org-special-properties rtn)))
  12838. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12839. (defun org-insert-property-drawer ()
  12840. "Insert a property drawer into the current entry."
  12841. (interactive)
  12842. (org-back-to-heading t)
  12843. (let ((beg (point))
  12844. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12845. end hiddenp)
  12846. (outline-next-heading)
  12847. (setq end (point))
  12848. (goto-char beg)
  12849. (while (re-search-forward re end t))
  12850. (setq hiddenp (org-invisible-p))
  12851. (end-of-line 1)
  12852. (insert "\n:PROPERTIES:\n:END:")
  12853. (beginning-of-line 0)
  12854. (org-indent-line-function)
  12855. (beginning-of-line 2)
  12856. (org-indent-line-function)
  12857. (beginning-of-line 0)
  12858. (if hiddenp
  12859. (save-excursion
  12860. (org-back-to-heading t)
  12861. (hide-entry))
  12862. (org-flag-drawer t))))
  12863. (defun org-set-property (property value)
  12864. "In the current entry, set PROPERTY to VALUE."
  12865. (interactive
  12866. (let* ((prop (completing-read "Property: "
  12867. (mapcar 'list (org-buffer-property-keys))))
  12868. (cur (org-entry-get nil prop))
  12869. (allowed (org-property-get-allowed-values nil prop 'table))
  12870. (val (if allowed
  12871. (completing-read "Value: " allowed nil 'req-match)
  12872. (read-string
  12873. (concat "Value" (if (and cur (string-match "\\S-" cur))
  12874. (concat "[" cur "]") "")
  12875. ": ")
  12876. "" cur))))
  12877. (list prop (if (equal val "") cur val))))
  12878. (unless (equal (org-entry-get nil property) value)
  12879. (org-entry-put nil property value)))
  12880. (defun org-delete-property (property)
  12881. "In the current entry, delete PROPERTY."
  12882. (interactive
  12883. (let* ((prop (completing-read
  12884. "Property: " (org-entry-properties nil 'standard))))
  12885. (list prop)))
  12886. (message (concat "Property " property
  12887. (if (org-entry-delete nil property)
  12888. " deleted"
  12889. " was not present in the entry"))))
  12890. (defun org-delete-property-globally (property)
  12891. "Remove PROPERTY globally, from all entries."
  12892. (interactive
  12893. (let* ((prop (completing-read
  12894. "Globally remove property: "
  12895. (mapcar 'list (org-buffer-property-keys)))))
  12896. (list prop)))
  12897. (save-excursion
  12898. (save-restriction
  12899. (widen)
  12900. (goto-char (point-min))
  12901. (let ((cnt 0))
  12902. (while (re-search-forward
  12903. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12904. nil t)
  12905. (setq cnt (1+ cnt))
  12906. (replace-match ""))
  12907. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12908. (defvar org-columns-current-fmt-compiled) ; defined below
  12909. (defun org-compute-property-at-point ()
  12910. "FIXME:"
  12911. (interactive)
  12912. (unless (org-at-property-p)
  12913. (error "Not at a property"))
  12914. (let ((prop (org-match-string-no-properties 2)))
  12915. (org-columns-get-format-and-top-level)
  12916. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12917. (error "No operator defined for property %s" prop))
  12918. (org-columns-compute prop)))
  12919. (defun org-property-get-allowed-values (pom property &optional table)
  12920. "Get allowed values for the property PROPERTY.
  12921. When TABLE is non-nil, return an alist that can directly be used for
  12922. completion."
  12923. (let (vals)
  12924. (cond
  12925. ((equal property "TODO")
  12926. (setq vals (org-with-point-at pom
  12927. (append org-todo-keywords-1 '("")))))
  12928. ((equal property "PRIORITY")
  12929. (let ((n org-lowest-priority))
  12930. (while (>= n org-highest-priority)
  12931. (push (char-to-string n) vals)
  12932. (setq n (1- n)))))
  12933. ((member property org-special-properties))
  12934. (t
  12935. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12936. (when (and vals (string-match "\\S-" vals))
  12937. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12938. (setq vals (mapcar (lambda (x)
  12939. (cond ((stringp x) x)
  12940. ((numberp x) (number-to-string x))
  12941. ((symbolp x) (symbol-name x))
  12942. (t "???")))
  12943. vals)))))
  12944. (if table (mapcar 'list vals) vals)))
  12945. (defun org-property-previous-allowed-value (&optional previous)
  12946. "Switch to the next allowed value for this property."
  12947. (interactive)
  12948. (org-property-next-allowed-value t))
  12949. (defun org-property-next-allowed-value (&optional previous)
  12950. "Switch to the next allowed value for this property."
  12951. (interactive)
  12952. (unless (org-at-property-p)
  12953. (error "Not at a property"))
  12954. (let* ((key (match-string 2))
  12955. (value (match-string 3))
  12956. (allowed (or (org-property-get-allowed-values (point) key)
  12957. (and (member value '("[ ]" "[-]" "[X]"))
  12958. '("[ ]" "[X]"))))
  12959. nval)
  12960. (unless allowed
  12961. (error "Allowed values for this property have not been defined"))
  12962. (if previous (setq allowed (reverse allowed)))
  12963. (if (member value allowed)
  12964. (setq nval (car (cdr (member value allowed)))))
  12965. (setq nval (or nval (car allowed)))
  12966. (if (equal nval value)
  12967. (error "Only one allowed value for this property"))
  12968. (org-at-property-p)
  12969. (replace-match (concat " :" key ": " nval) t t)
  12970. (org-indent-line-function)
  12971. (beginning-of-line 1)
  12972. (skip-chars-forward " \t")))
  12973. ;;; Column View
  12974. (defvar org-columns-overlays nil
  12975. "Holds the list of current column overlays.")
  12976. (defvar org-columns-current-fmt nil
  12977. "Local variable, holds the currently active column format.")
  12978. (defvar org-columns-current-fmt-compiled nil
  12979. "Local variable, holds the currently active column format.
  12980. This is the compiled version of the format.")
  12981. (defvar org-columns-current-maxwidths nil
  12982. "Loval variable, holds the currently active maximum column widths.")
  12983. (defvar org-columns-begin-marker (make-marker)
  12984. "Points to the position where last a column creation command was called.")
  12985. (defvar org-columns-top-level-marker (make-marker)
  12986. "Points to the position where current columns region starts.")
  12987. (defvar org-columns-map (make-sparse-keymap)
  12988. "The keymap valid in column display.")
  12989. (defun org-columns-content ()
  12990. "Switch to contents view while in columns view."
  12991. (interactive)
  12992. (org-overview)
  12993. (org-content))
  12994. (org-defkey org-columns-map "c" 'org-columns-content)
  12995. (org-defkey org-columns-map "o" 'org-overview)
  12996. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  12997. (org-defkey org-columns-map "v" 'org-columns-show-value)
  12998. (org-defkey org-columns-map "q" 'org-columns-quit)
  12999. (org-defkey org-columns-map "r" 'org-columns-redo)
  13000. (org-defkey org-columns-map [left] 'backward-char)
  13001. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  13002. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  13003. (org-defkey org-columns-map [right] 'forward-char)
  13004. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  13005. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  13006. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-next-allowed-value)
  13007. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  13008. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  13009. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  13010. (org-defkey org-columns-map "<" 'org-columns-narrow)
  13011. (org-defkey org-columns-map ">" 'org-columns-widen)
  13012. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  13013. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  13014. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  13015. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  13016. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  13017. '("Column"
  13018. ["Edit property" org-columns-edit-value t]
  13019. ["Next allowed value" org-columns-next-allowed-value t]
  13020. ["Previous allowed value" org-columns-previous-allowed-value t]
  13021. ["Show full value" org-columns-show-value t]
  13022. ["Edit allowed" org-columns-edit-allowed t]
  13023. "--"
  13024. ["Edit column attributes" org-columns-edit-attributes t]
  13025. ["Increase column width" org-columns-widen t]
  13026. ["Decrease column width" org-columns-narrow t]
  13027. "--"
  13028. ["Move column right" org-columns-move-right t]
  13029. ["Move column left" org-columns-move-left t]
  13030. ["Add column" org-columns-new t]
  13031. ["Delete column" org-columns-delete t]
  13032. "--"
  13033. ["CONTENTS" org-columns-content t]
  13034. ["OVERVIEW" org-overview t]
  13035. ["Refresh columns display" org-columns-redo t]
  13036. "--"
  13037. ["Quit" org-columns-quit t]))
  13038. (defun org-columns-new-overlay (beg end &optional string face)
  13039. "Create a new column overlay and add it to the list."
  13040. (let ((ov (org-make-overlay beg end)))
  13041. (org-overlay-put ov 'face (or face 'secondary-selection))
  13042. (org-overlay-display ov string face)
  13043. (push ov org-columns-overlays)
  13044. ov))
  13045. (defun org-columns-display-here (&optional props)
  13046. "Overlay the current line with column display."
  13047. (interactive)
  13048. (let* ((fmt org-columns-current-fmt-compiled)
  13049. (beg (point-at-bol))
  13050. (level-face (save-excursion
  13051. (beginning-of-line 1)
  13052. (looking-at "\\(\\**\\)\\(\\* \\)")
  13053. (org-get-level-face 2)))
  13054. (color (list :foreground
  13055. (face-attribute (or level-face 'default) :foreground)))
  13056. props pom property ass width f string ov column)
  13057. ;; Check if the entry is in another buffer.
  13058. (unless props
  13059. (if (eq major-mode 'org-agenda-mode)
  13060. (setq pom (or (get-text-property (point) 'org-hd-marker)
  13061. (get-text-property (point) 'org-marker))
  13062. props (if pom (org-entry-properties pom) nil))
  13063. (setq props (org-entry-properties nil))))
  13064. ;; Walk the format
  13065. (while (setq column (pop fmt))
  13066. (setq property (car column)
  13067. ass (if (equal property "ITEM")
  13068. (cons "ITEM"
  13069. (save-match-data
  13070. (org-no-properties
  13071. (org-remove-tabs
  13072. (buffer-substring-no-properties
  13073. (point-at-bol) (point-at-eol))))))
  13074. (assoc property props))
  13075. width (or (cdr (assoc property org-columns-current-maxwidths))
  13076. (nth 2 column))
  13077. f (format "%%-%d.%ds | " width width)
  13078. string (format f (or (cdr ass) "")))
  13079. ;; Create the overlay
  13080. (org-unmodified
  13081. (setq ov (org-columns-new-overlay
  13082. beg (setq beg (1+ beg)) string
  13083. (list color 'org-column)))
  13084. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  13085. (org-overlay-put ov 'keymap org-columns-map)
  13086. (org-overlay-put ov 'org-columns-key property)
  13087. (org-overlay-put ov 'org-columns-value (cdr ass))
  13088. (org-overlay-put ov 'org-columns-pom pom)
  13089. (org-overlay-put ov 'org-columns-format f))
  13090. (if (or (not (char-after beg))
  13091. (equal (char-after beg) ?\n))
  13092. (let ((inhibit-read-only t))
  13093. (save-excursion
  13094. (goto-char beg)
  13095. (insert " ")))))
  13096. ;; Make the rest of the line disappear.
  13097. (org-unmodified
  13098. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  13099. (org-overlay-put ov 'invisible t)
  13100. (org-overlay-put ov 'keymap org-columns-map)
  13101. (org-overlay-put ov 'intangible t)
  13102. (push ov org-columns-overlays)
  13103. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  13104. (org-overlay-put ov 'keymap org-columns-map)
  13105. (push ov org-columns-overlays)
  13106. (let ((inhibit-read-only t))
  13107. (put-text-property (max (point-min) (1- (point-at-bol)))
  13108. (min (point-max) (1+ (point-at-eol)))
  13109. 'read-only "Type `e' to edit property")))))
  13110. (defvar org-previous-header-line-format nil
  13111. "The header line format before column view was turned on.")
  13112. (defvar org-columns-inhibit-recalculation nil
  13113. "Inhibit recomputing of columns on column view startup.")
  13114. (defvar header-line-format)
  13115. (defun org-columns-display-here-title ()
  13116. "Overlay the newline before the current line with the table title."
  13117. (interactive)
  13118. (let ((fmt org-columns-current-fmt-compiled)
  13119. string (title "")
  13120. property width f column str)
  13121. (while (setq column (pop fmt))
  13122. (setq property (car column)
  13123. str (or (nth 1 column) property)
  13124. width (or (cdr (assoc property org-columns-current-maxwidths))
  13125. (nth 2 column))
  13126. f (format "%%-%d.%ds | " width width)
  13127. string (format f str)
  13128. title (concat title string)))
  13129. (setq title (concat
  13130. (org-add-props " " nil 'display '(space :align-to 0))
  13131. (org-add-props title nil 'face '(:weight bold :underline t))))
  13132. (org-set-local 'org-previous-header-line-format header-line-format)
  13133. (setq header-line-format title)))
  13134. (defun org-columns-remove-overlays ()
  13135. "Remove all currently active column overlays."
  13136. (interactive)
  13137. (when (marker-buffer org-columns-begin-marker)
  13138. (with-current-buffer (marker-buffer org-columns-begin-marker)
  13139. (when (local-variable-p 'org-previous-header-line-format)
  13140. (setq header-line-format org-previous-header-line-format)
  13141. (kill-local-variable 'org-previous-header-line-format))
  13142. (move-marker org-columns-begin-marker nil)
  13143. (move-marker org-columns-top-level-marker nil)
  13144. (org-unmodified
  13145. (mapc 'org-delete-overlay org-columns-overlays)
  13146. (setq org-columns-overlays nil)
  13147. (let ((inhibit-read-only t))
  13148. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  13149. (defun org-columns-show-value ()
  13150. "Show the full value of the property."
  13151. (interactive)
  13152. (let ((value (get-char-property (point) 'org-columns-value)))
  13153. (message "Value is: %s" (or value ""))))
  13154. (defun org-columns-quit ()
  13155. "Remove the column overlays and in this way exit column editing."
  13156. (interactive)
  13157. (org-unmodified
  13158. (org-columns-remove-overlays)
  13159. (let ((inhibit-read-only t))
  13160. ;; FIXME: is this safe???
  13161. ;; or are there other reasons why there may be a read-only property????
  13162. (remove-text-properties (point-min) (point-max) '(read-only t))))
  13163. (when (eq major-mode 'org-agenda-mode)
  13164. (message "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  13165. (defun org-columns-edit-value ()
  13166. "Edit the value of the property at point in column view.
  13167. Where possible, use the standard interface for changing this line."
  13168. (interactive)
  13169. (let* ((col (current-column))
  13170. (key (get-char-property (point) 'org-columns-key))
  13171. (value (get-char-property (point) 'org-columns-value))
  13172. (bol (point-at-bol)) (eol (point-at-eol))
  13173. (pom (or (get-text-property bol 'org-hd-marker)
  13174. (point))) ; keep despite of compiler waring
  13175. (line-overlays
  13176. (delq nil (mapcar (lambda (x)
  13177. (and (eq (overlay-buffer x) (current-buffer))
  13178. (>= (overlay-start x) bol)
  13179. (<= (overlay-start x) eol)
  13180. x))
  13181. org-columns-overlays)))
  13182. nval eval allowed)
  13183. (when (equal key "ITEM")
  13184. (error "Cannot edit item headline from here"))
  13185. (cond
  13186. ((equal key "TODO")
  13187. (setq eval '(org-with-point-at pom
  13188. (let ((current-prefix-arg '(4))) (org-todo '(4))))))
  13189. ((equal key "PRIORITY")
  13190. (setq eval '(org-with-point-at pom
  13191. (call-interactively 'org-priority))))
  13192. ((equal key "TAGS")
  13193. (setq eval '(org-with-point-at pom
  13194. (let ((org-fast-tag-selection-single-key
  13195. (if (eq org-fast-tag-selection-single-key 'expert)
  13196. t org-fast-tag-selection-single-key)))
  13197. (call-interactively 'org-set-tags)))))
  13198. ((equal key "DEADLINE")
  13199. (setq eval '(org-with-point-at pom
  13200. (call-interactively 'org-deadline))))
  13201. ((equal key "SCHEDULED")
  13202. (setq eval '(org-with-point-at pom
  13203. (call-interactively 'org-schedule))))
  13204. (t
  13205. (setq allowed (org-property-get-allowed-values pom key 'table))
  13206. (if allowed
  13207. (setq nval (completing-read "Value: " allowed nil t))
  13208. (setq nval (read-string "Edit: " value)))
  13209. (setq nval (org-trim nval))
  13210. (when (not (equal nval value))
  13211. (setq eval '(org-entry-put pom key nval)))))
  13212. (when eval
  13213. (let ((inhibit-read-only t))
  13214. (remove-text-properties (1- bol) eol '(read-only t))
  13215. (unwind-protect
  13216. (progn
  13217. (setq org-columns-overlays
  13218. (org-delete-all line-overlays org-columns-overlays))
  13219. (mapc 'org-delete-overlay line-overlays)
  13220. (org-columns-eval eval))
  13221. (org-columns-display-here))))
  13222. (move-to-column col)
  13223. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13224. (org-columns-update key))))
  13225. (defun org-columns-edit-allowed ()
  13226. "Edit the list of allowed values for the current property."
  13227. (interactive)
  13228. (let* ((col (current-column))
  13229. (key (get-char-property (point) 'org-columns-key))
  13230. (key1 (concat key "_ALL"))
  13231. (value (get-char-property (point) 'org-columns-value))
  13232. (allowed (org-entry-get (point) key1 t))
  13233. nval)
  13234. (setq nval (read-string "Allowed: " allowed))
  13235. (org-entry-put
  13236. (cond ((marker-position org-entry-property-inherited-from)
  13237. org-entry-property-inherited-from)
  13238. ((marker-position org-columns-top-level-marker)
  13239. org-columns-top-level-marker))
  13240. key1 nval)))
  13241. (defun org-columns-eval (form)
  13242. (let (hidep)
  13243. (save-excursion
  13244. (beginning-of-line 1)
  13245. (next-line 1)
  13246. (setq hidep (org-on-heading-p 1)))
  13247. (eval form)
  13248. (and hidep (hide-entry))))
  13249. (defun org-columns-previous-allowed-value ()
  13250. "Switch to the previous allowed value for this column."
  13251. (interactive)
  13252. (org-columns-next-allowed-value t))
  13253. (defun org-columns-next-allowed-value (&optional previous)
  13254. "Switch to the next allowed value for this column."
  13255. (interactive)
  13256. (let* ((col (current-column))
  13257. (key (get-char-property (point) 'org-columns-key))
  13258. (value (get-char-property (point) 'org-columns-value))
  13259. (bol (point-at-bol)) (eol (point-at-eol))
  13260. (pom (or (get-text-property bol 'org-hd-marker)
  13261. (point))) ; keep despite of compiler waring
  13262. (line-overlays
  13263. (delq nil (mapcar (lambda (x)
  13264. (and (eq (overlay-buffer x) (current-buffer))
  13265. (>= (overlay-start x) bol)
  13266. (<= (overlay-start x) eol)
  13267. x))
  13268. org-columns-overlays)))
  13269. (allowed (or (org-property-get-allowed-values pom key)
  13270. (and (equal
  13271. (nth 4 (assoc key org-columns-current-fmt-compiled))
  13272. 'checkbox) '("[ ]" "[X]"))))
  13273. nval)
  13274. (when (equal key "ITEM")
  13275. (error "Cannot edit item headline from here"))
  13276. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  13277. (error "Allowed values for this property have not been defined"))
  13278. (if (member key '("SCHEDULED" "DEADLINE"))
  13279. (setq nval (if previous 'earlier 'later))
  13280. (if previous (setq allowed (reverse allowed)))
  13281. (if (member value allowed)
  13282. (setq nval (car (cdr (member value allowed)))))
  13283. (setq nval (or nval (car allowed)))
  13284. (if (equal nval value)
  13285. (error "Only one allowed value for this property")))
  13286. (let ((inhibit-read-only t))
  13287. (remove-text-properties (1- bol) eol '(read-only t))
  13288. (unwind-protect
  13289. (progn
  13290. (setq org-columns-overlays
  13291. (org-delete-all line-overlays org-columns-overlays))
  13292. (mapc 'org-delete-overlay line-overlays)
  13293. (org-columns-eval '(org-entry-put pom key nval)))
  13294. (org-columns-display-here)))
  13295. (move-to-column col)
  13296. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13297. (org-columns-update key))))
  13298. (defun org-verify-version (task)
  13299. (cond
  13300. ((eq task 'columns)
  13301. (if (or (featurep 'xemacs)
  13302. (< emacs-major-version 22))
  13303. (error "Emacs 22 is required for the columns feature")))))
  13304. (defun org-columns-get-format-and-top-level ()
  13305. (let (fmt)
  13306. (when (condition-case nil (org-back-to-heading) (error nil))
  13307. (move-marker org-entry-property-inherited-from nil)
  13308. (setq fmt (org-entry-get nil "COLUMNS" t)))
  13309. (setq fmt (or fmt org-columns-default-format))
  13310. (org-set-local 'org-columns-current-fmt fmt)
  13311. (org-columns-compile-format fmt)
  13312. (if (marker-position org-entry-property-inherited-from)
  13313. (move-marker org-columns-top-level-marker
  13314. org-entry-property-inherited-from)
  13315. (move-marker org-columns-top-level-marker (point)))
  13316. fmt))
  13317. (defun org-columns ()
  13318. "Turn on column view on an org-mode file."
  13319. (interactive)
  13320. (org-verify-version 'columns)
  13321. (org-columns-remove-overlays)
  13322. (move-marker org-columns-begin-marker (point))
  13323. (let (beg end fmt cache maxwidths)
  13324. (setq fmt (org-columns-get-format-and-top-level))
  13325. (save-excursion
  13326. (goto-char org-columns-top-level-marker)
  13327. (setq beg (point))
  13328. (unless org-columns-inhibit-recalculation
  13329. (org-columns-compute-all))
  13330. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  13331. (point-max)))
  13332. (goto-char beg)
  13333. ;; Get and cache the properties
  13334. (while (re-search-forward (concat "^" outline-regexp) end t)
  13335. (push (cons (org-current-line) (org-entry-properties)) cache))
  13336. (when cache
  13337. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13338. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13339. (org-columns-display-here-title)
  13340. (mapc (lambda (x)
  13341. (goto-line (car x))
  13342. (org-columns-display-here (cdr x)))
  13343. cache)))))
  13344. (defun org-columns-new (&optional prop title width op fmt)
  13345. "Insert a new column, to the leeft o the current column."
  13346. (interactive)
  13347. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  13348. cell)
  13349. (setq prop (completing-read
  13350. "Property: " (mapcar 'list (org-buffer-property-keys t))
  13351. nil nil prop))
  13352. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  13353. (setq width (read-string "Column width: " (if width (number-to-string width))))
  13354. (if (string-match "\\S-" width)
  13355. (setq width (string-to-number width))
  13356. (setq width nil))
  13357. (setq fmt (completing-read "Summary [none]: "
  13358. '(("none") ("add_numbers") ("add_times") ("checkbox"))
  13359. nil t))
  13360. (if (string-match "\\S-" fmt)
  13361. (setq fmt (intern fmt))
  13362. (setq fmt nil))
  13363. (if (eq fmt 'none) (setq fmt nil))
  13364. (if editp
  13365. (progn
  13366. (setcar editp prop)
  13367. (setcdr editp (list title width nil fmt)))
  13368. (setq cell (nthcdr (1- (current-column))
  13369. org-columns-current-fmt-compiled))
  13370. (setcdr cell (cons (list prop title width nil fmt)
  13371. (cdr cell))))
  13372. (org-columns-store-format)
  13373. (org-columns-redo)))
  13374. (defun org-columns-delete ()
  13375. "Delete the column at point from columns view."
  13376. (interactive)
  13377. (let* ((n (current-column))
  13378. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  13379. (when (y-or-n-p
  13380. (format "Are you sure you want to remove column \"%s\"? " title))
  13381. (setq org-columns-current-fmt-compiled
  13382. (delq (nth n org-columns-current-fmt-compiled)
  13383. org-columns-current-fmt-compiled))
  13384. (org-columns-store-format)
  13385. (org-columns-redo)
  13386. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  13387. (backward-char 1)))))
  13388. (defun org-columns-edit-attributes ()
  13389. "Edit the attributes of the current column."
  13390. (interactive)
  13391. (let* ((n (current-column))
  13392. (info (nth n org-columns-current-fmt-compiled)))
  13393. (apply 'org-columns-new info)))
  13394. (defun org-columns-widen (arg)
  13395. "Make the column wider by ARG characters."
  13396. (interactive "p")
  13397. (let* ((n (current-column))
  13398. (entry (nth n org-columns-current-fmt-compiled))
  13399. (width (or (nth 2 entry)
  13400. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  13401. (setq width (max 1 (+ width arg)))
  13402. (setcar (nthcdr 2 entry) width)
  13403. (org-columns-store-format)
  13404. (org-columns-redo)))
  13405. (defun org-columns-narrow (arg)
  13406. "Make the column nrrower by ARG characters."
  13407. (interactive "p")
  13408. (org-columns-widen (- arg)))
  13409. (defun org-columns-move-right ()
  13410. "Swap this column with the one to the right."
  13411. (interactive)
  13412. (let* ((n (current-column))
  13413. (cell (nthcdr n org-columns-current-fmt-compiled))
  13414. e)
  13415. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  13416. (error "Cannot shift this column further to the right"))
  13417. (setq e (car cell))
  13418. (setcar cell (car (cdr cell)))
  13419. (setcdr cell (cons e (cdr (cdr cell))))
  13420. (org-columns-store-format)
  13421. (org-columns-redo)
  13422. (forward-char 1)))
  13423. (defun org-columns-move-left ()
  13424. "Swap this column with the one to the left."
  13425. (interactive)
  13426. (let* ((n (current-column)))
  13427. (when (= n 0)
  13428. (error "Cannot shift this column further to the left"))
  13429. (backward-char 1)
  13430. (org-columns-move-right)
  13431. (backward-char 1)))
  13432. (defun org-columns-store-format ()
  13433. "Store the text version of the current columns format in appropriate place.
  13434. This is either in the COLUMNS property of the node starting the current column
  13435. display, or in the #+COLUMNS line of the current buffer."
  13436. (let (fmt)
  13437. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  13438. (if (marker-position org-columns-top-level-marker)
  13439. (save-excursion
  13440. (goto-char org-columns-top-level-marker)
  13441. (if (org-entry-get nil "COLUMNS")
  13442. (org-entry-put nil "COLUMNS" fmt)
  13443. (goto-char (point-min))
  13444. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  13445. (replace-match (concat "#+COLUMNS: " fmt t t)))))
  13446. (setq org-columns-current-fmt fmt))))
  13447. (defvar org-overriding-columns-format nil
  13448. "When set, overrides any other definition.")
  13449. (defvar org-agenda-view-columns-initially nil
  13450. "When set, switch to columns view immediately after creating the agenda.")
  13451. (defun org-agenda-columns ()
  13452. "Turn on column view in the agenda."
  13453. (interactive)
  13454. (org-verify-version 'columns)
  13455. (org-columns-remove-overlays)
  13456. (move-marker org-columns-begin-marker (point))
  13457. (let (fmt cache maxwidths m)
  13458. (cond
  13459. ((and (local-variable-p 'org-overriding-columns-format)
  13460. org-overriding-columns-format)
  13461. (setq fmt org-overriding-columns-format))
  13462. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  13463. (setq fmt (org-entry-get m "COLUMNS" t)))
  13464. ((and (boundp 'org-columns-current-fmt)
  13465. (local-variable-p 'org-columns-current-fmt)
  13466. org-columns-current-fmt)
  13467. (setq fmt org-columns-current-fmt))
  13468. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  13469. (setq m (get-text-property m 'org-hd-marker))
  13470. (setq fmt (org-entry-get m "COLUMNS" t))))
  13471. (setq fmt (or fmt org-columns-default-format))
  13472. (org-set-local 'org-columns-current-fmt fmt)
  13473. (org-columns-compile-format fmt)
  13474. (save-excursion
  13475. ;; Get and cache the properties
  13476. (goto-char (point-min))
  13477. (while (not (eobp))
  13478. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  13479. (get-text-property (point) 'org-marker)))
  13480. (push (cons (org-current-line) (org-entry-properties m)) cache))
  13481. (beginning-of-line 2))
  13482. (when cache
  13483. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  13484. (org-set-local 'org-columns-current-maxwidths maxwidths)
  13485. (org-columns-display-here-title)
  13486. (mapc (lambda (x)
  13487. (goto-line (car x))
  13488. (org-columns-display-here (cdr x)))
  13489. cache)))))
  13490. (defun org-columns-get-autowidth-alist (s cache)
  13491. "Derive the maximum column widths from the format and the cache."
  13492. (let ((start 0) rtn)
  13493. (while (string-match "%\\([a-zA-Z]\\S-*\\)" s start)
  13494. (push (cons (match-string 1 s) 1) rtn)
  13495. (setq start (match-end 0)))
  13496. (mapc (lambda (x)
  13497. (setcdr x (apply 'max
  13498. (mapcar
  13499. (lambda (y)
  13500. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  13501. cache))))
  13502. rtn)
  13503. rtn))
  13504. (defun org-columns-compute-all ()
  13505. "Compute all columns that have operators defined."
  13506. (org-unmodified
  13507. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  13508. (let ((columns org-columns-current-fmt-compiled) col)
  13509. (while (setq col (pop columns))
  13510. (when (nth 3 col)
  13511. (save-excursion
  13512. (org-columns-compute (car col)))))))
  13513. (defun org-columns-update (property)
  13514. "Recompute PROPERTY, and update the columns display for it."
  13515. (org-columns-compute property)
  13516. (let (fmt val pos)
  13517. (save-excursion
  13518. (mapc (lambda (ov)
  13519. (when (equal (org-overlay-get ov 'org-columns-key) property)
  13520. (setq pos (org-overlay-start ov))
  13521. (goto-char pos)
  13522. (when (setq val (cdr (assoc property
  13523. (get-text-property (point-at-bol) 'org-summaries))))
  13524. (setq fmt (org-overlay-get ov 'org-columns-format))
  13525. (org-overlay-put ov 'display (format fmt val)))))
  13526. org-columns-overlays))))
  13527. (defun org-columns-compute (property)
  13528. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  13529. (interactive)
  13530. (let* ((re (concat "^" outline-regexp))
  13531. (lmax 30) ; Does anyone use deeper levels???
  13532. (lsum (make-vector lmax 0))
  13533. (level 0)
  13534. (ass (assoc property org-columns-current-fmt-compiled))
  13535. (format (nth 4 ass))
  13536. (beg org-columns-top-level-marker)
  13537. last-level val end sumpos sum-alist sum str)
  13538. (save-excursion
  13539. ;; Find the region to compute
  13540. (goto-char beg)
  13541. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  13542. (goto-char end)
  13543. ;; Walk the tree from the back and do the computations
  13544. (while (re-search-backward re beg t)
  13545. (setq sumpos (match-beginning 0)
  13546. last-level level
  13547. level (org-outline-level)
  13548. val (org-entry-get nil property))
  13549. (cond
  13550. ((< level last-level)
  13551. ;; put the sum of lower levels here as a property
  13552. (setq sum (aref lsum last-level)
  13553. str (org-column-number-to-string sum format)
  13554. sum-alist (get-text-property sumpos 'org-summaries))
  13555. (if (assoc property sum-alist)
  13556. (setcdr (assoc property sum-alist) str)
  13557. (push (cons property str) sum-alist)
  13558. (org-unmodified
  13559. (add-text-properties sumpos (1+ sumpos)
  13560. (list 'org-summaries sum-alist))))
  13561. (when val ;?????????????????????????????????? and force?????
  13562. (org-entry-put nil property str))
  13563. ;; add current to current level accumulator
  13564. (aset lsum level (+ (aref lsum level) sum))
  13565. ;; clear accumulators for deeper levels
  13566. (loop for l from (1+ level) to (1- lmax) do (aset lsum l 0)))
  13567. ((>= level last-level)
  13568. ;; add what we have here to the accumulator for this level
  13569. (aset lsum level (+ (aref lsum level)
  13570. (org-column-string-to-number (or val "0") format))))
  13571. (t (error "This should not happen")))))))
  13572. (defun org-columns-redo ()
  13573. "Construct the column display again."
  13574. (interactive)
  13575. (message "Recomputing columns...")
  13576. (save-excursion
  13577. (if (marker-position org-columns-begin-marker)
  13578. (goto-char org-columns-begin-marker))
  13579. (org-columns-remove-overlays)
  13580. (if (org-mode-p)
  13581. (call-interactively 'org-columns)
  13582. (call-interactively 'org-agenda-columns)))
  13583. (message "Recomputing columns...done"))
  13584. (defun org-columns-not-in-agenda ()
  13585. (if (eq major-mode 'org-agenda-mode)
  13586. (error "This command is only allowed in Org-mode buffers")))
  13587. (defun org-string-to-number (s)
  13588. "Convert string to number, and interpret hh:mm:ss."
  13589. (if (not (string-match ":" s))
  13590. (string-to-number s)
  13591. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13592. (while l
  13593. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13594. sum)))
  13595. (defun org-column-number-to-string (n fmt)
  13596. "Convert a computed column number to a string value, according to FMT."
  13597. (cond
  13598. ((eq fmt 'add_times)
  13599. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  13600. (format "%d:%02d" h m)))
  13601. ((eq fmt 'checkbox)
  13602. (cond ((= n (floor n)) "[X]")
  13603. ((> n 1.) "[-]")
  13604. (t "[ ]")))
  13605. (t (number-to-string n))))
  13606. (defun org-column-string-to-number (s fmt)
  13607. "Convert a column value to a number that can be used for column computing."
  13608. (cond
  13609. ((string-match ":" s)
  13610. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  13611. (while l
  13612. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  13613. sum))
  13614. ((eq fmt 'checkbox)
  13615. (if (equal s "[X]") 1. 0.000001))
  13616. (t (string-to-number s))))
  13617. (defun org-columns-uncompile-format (cfmt)
  13618. "Turn the compiled columns format back into a string representation."
  13619. (let ((rtn "") e s prop title op width fmt)
  13620. (while (setq e (pop cfmt))
  13621. (setq prop (car e)
  13622. title (nth 1 e)
  13623. width (nth 2 e)
  13624. op (nth 3 e)
  13625. fmt (nth 4 e))
  13626. (cond
  13627. ((eq fmt 'add_times) (setq op ":"))
  13628. ((eq fmt 'checkbox) (setq op "X"))
  13629. ((eq fmt 'add_numbers) (setq op "+")))
  13630. (if (equal title prop) (setq title nil))
  13631. (setq s (concat "%" (if width (number-to-string width))
  13632. prop
  13633. (if title (concat "(" title ")"))
  13634. (if op (concat "{" op "}"))))
  13635. (setq rtn (concat rtn " " s)))
  13636. (org-trim rtn)))
  13637. (defun org-columns-compile-format (fmt)
  13638. "FIXME"
  13639. (let ((start 0) width prop title op f)
  13640. (setq org-columns-current-fmt-compiled nil)
  13641. (while (string-match "%\\([0-9]+\\)?\\([a-zA-Z_0-9]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*"
  13642. fmt start)
  13643. (setq start (match-end 0)
  13644. width (match-string 1 fmt)
  13645. prop (match-string 2 fmt)
  13646. title (or (match-string 3 fmt) prop)
  13647. op (match-string 4 fmt)
  13648. f nil)
  13649. (if width (setq width (string-to-number width)))
  13650. (cond
  13651. ((equal op "+") (setq f 'add_numbers))
  13652. ((equal op ":") (setq f 'add_times))
  13653. ((equal op "X") (setq f 'checkbox)))
  13654. (push (list prop title width op f) org-columns-current-fmt-compiled))
  13655. (setq org-columns-current-fmt-compiled
  13656. (nreverse org-columns-current-fmt-compiled))))
  13657. ;;;; Timestamps
  13658. (defvar org-last-changed-timestamp nil)
  13659. (defvar org-time-was-given) ; dynamically scoped parameter
  13660. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13661. (defvar org-ts-what) ; dynamically scoped parameter
  13662. (defun org-time-stamp (arg)
  13663. "Prompt for a date/time and insert a time stamp.
  13664. If the user specifies a time like HH:MM, or if this command is called
  13665. with a prefix argument, the time stamp will contain date and time.
  13666. Otherwise, only the date will be included. All parts of a date not
  13667. specified by the user will be filled in from the current date/time.
  13668. So if you press just return without typing anything, the time stamp
  13669. will represent the current date/time. If there is already a timestamp
  13670. at the cursor, it will be modified."
  13671. (interactive "P")
  13672. (let (org-time-was-given org-end-time-was-given time)
  13673. (cond
  13674. ((and (org-at-timestamp-p)
  13675. (eq last-command 'org-time-stamp)
  13676. (eq this-command 'org-time-stamp))
  13677. (insert "--")
  13678. (setq time (let ((this-command this-command))
  13679. (org-read-date arg 'totime)))
  13680. (org-insert-time-stamp time (or org-time-was-given arg)))
  13681. ((org-at-timestamp-p)
  13682. (setq time (let ((this-command this-command))
  13683. (org-read-date arg 'totime)))
  13684. (when (org-at-timestamp-p) ; just to get the match data
  13685. (replace-match "")
  13686. (setq org-last-changed-timestamp
  13687. (org-insert-time-stamp
  13688. time (or org-time-was-given arg)
  13689. nil nil nil (list org-end-time-was-given))))
  13690. (message "Timestamp updated"))
  13691. (t
  13692. (setq time (let ((this-command this-command))
  13693. (org-read-date arg 'totime)))
  13694. (org-insert-time-stamp time (or org-time-was-given arg)
  13695. nil nil nil (list org-end-time-was-given))))))
  13696. (defun org-time-stamp-inactive (&optional arg)
  13697. "Insert an inactive time stamp.
  13698. An inactive time stamp is enclosed in square brackets instead of angle
  13699. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13700. does not link to the calendar and cannot be changed with the S-cursor keys.
  13701. So these are more for recording a certain time/date."
  13702. (interactive "P")
  13703. (let (org-time-was-given org-end-time-was-given time)
  13704. (setq time (org-read-date arg 'totime))
  13705. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  13706. nil nil (list org-end-time-was-given))))
  13707. (defvar org-date-ovl (org-make-overlay 1 1))
  13708. (org-overlay-put org-date-ovl 'face 'org-warning)
  13709. (org-detach-overlay org-date-ovl)
  13710. (defvar org-ans1) ; dynamically scoped parameter
  13711. (defvar org-ans2) ; dynamically scoped parameter
  13712. (defvar org-plain-time-of-day-regexp) ; defined below
  13713. (defun org-read-date (&optional with-time to-time from-string prompt)
  13714. "Read a date and make things smooth for the user.
  13715. The prompt will suggest to enter an ISO date, but you can also enter anything
  13716. which will at least partially be understood by `parse-time-string'.
  13717. Unrecognized parts of the date will default to the current day, month, year,
  13718. hour and minute. For example,
  13719. 3-2-5 --> 2003-02-05
  13720. feb 15 --> currentyear-02-15
  13721. sep 12 9 --> 2009-09-12
  13722. 12:45 --> today 12:45
  13723. 22 sept 0:34 --> currentyear-09-22 0:34
  13724. 12 --> currentyear-currentmonth-12
  13725. Fri --> nearest Friday (today or later)
  13726. +4 --> four days from today (only if +N is the only thing given)
  13727. etc.
  13728. The function understands only English month and weekday abbreviations,
  13729. but this can be configured with the variables `parse-time-months' and
  13730. `parse-time-weekdays'.
  13731. While prompting, a calendar is popped up - you can also select the
  13732. date with the mouse (button 1). The calendar shows a period of three
  13733. months. To scroll it to other months, use the keys `>' and `<'.
  13734. If you don't like the calendar, turn it off with
  13735. \(setq org-popup-calendar-for-date-prompt nil)
  13736. With optional argument TO-TIME, the date will immediately be converted
  13737. to an internal time.
  13738. With an optional argument WITH-TIME, the prompt will suggest to also
  13739. insert a time. Note that when WITH-TIME is not set, you can still
  13740. enter a time, and this function will inform the calling routine about
  13741. this change. The calling routine may then choose to change the format
  13742. used to insert the time stamp into the buffer to include the time."
  13743. (require 'parse-time)
  13744. (let* ((org-time-stamp-rounding-minutes
  13745. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  13746. (ct (org-current-time))
  13747. (default-time
  13748. ;; Default time is either today, or, when entering a range,
  13749. ;; the range start.
  13750. (if (save-excursion
  13751. (re-search-backward
  13752. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13753. (- (point) 20) t))
  13754. (apply
  13755. 'encode-time
  13756. (mapcar (lambda(x) (or x 0))
  13757. (parse-time-string (match-string 1))))
  13758. ct))
  13759. (calendar-move-hook nil)
  13760. (view-diary-entries-initially nil)
  13761. (view-calendar-holidays-initially nil)
  13762. (timestr (format-time-string
  13763. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  13764. (prompt (concat (if prompt (concat prompt " ") "")
  13765. (format "Date and/or time (default [%s]): " timestr)))
  13766. ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
  13767. second minute hour day month year tl wday wday1 pm)
  13768. (cond
  13769. (from-string (setq ans from-string))
  13770. (org-popup-calendar-for-date-prompt
  13771. (save-excursion
  13772. (save-window-excursion
  13773. (calendar)
  13774. (calendar-forward-day (- (time-to-days default-time)
  13775. (calendar-absolute-from-gregorian
  13776. (calendar-current-date))))
  13777. (org-eval-in-calendar nil t)
  13778. (let* ((old-map (current-local-map))
  13779. (map (copy-keymap calendar-mode-map))
  13780. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13781. (org-defkey map (kbd "RET") 'org-calendar-select)
  13782. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  13783. 'org-calendar-select-mouse)
  13784. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  13785. 'org-calendar-select-mouse)
  13786. (org-defkey minibuffer-local-map [(meta shift left)]
  13787. (lambda () (interactive)
  13788. (org-eval-in-calendar '(calendar-backward-month 1))))
  13789. (org-defkey minibuffer-local-map [(meta shift right)]
  13790. (lambda () (interactive)
  13791. (org-eval-in-calendar '(calendar-forward-month 1))))
  13792. (org-defkey minibuffer-local-map [(shift up)]
  13793. (lambda () (interactive)
  13794. (org-eval-in-calendar '(calendar-backward-week 1))))
  13795. (org-defkey minibuffer-local-map [(shift down)]
  13796. (lambda () (interactive)
  13797. (org-eval-in-calendar '(calendar-forward-week 1))))
  13798. (org-defkey minibuffer-local-map [(shift left)]
  13799. (lambda () (interactive)
  13800. (org-eval-in-calendar '(calendar-backward-day 1))))
  13801. (org-defkey minibuffer-local-map [(shift right)]
  13802. (lambda () (interactive)
  13803. (org-eval-in-calendar '(calendar-forward-day 1))))
  13804. (org-defkey minibuffer-local-map ">"
  13805. (lambda () (interactive)
  13806. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13807. (org-defkey minibuffer-local-map "<"
  13808. (lambda () (interactive)
  13809. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13810. (unwind-protect
  13811. (progn
  13812. (use-local-map map)
  13813. (setq org-ans0 (read-string prompt "" nil nil))
  13814. ;; org-ans0: from prompt
  13815. ;; org-ans1: from mouse click
  13816. ;; org-ans2: from calendar motion
  13817. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13818. (use-local-map old-map))))))
  13819. (t ; Naked prompt only
  13820. (setq ans (read-string prompt "" nil timestr))))
  13821. (org-detach-overlay org-date-ovl)
  13822. (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0)
  13823. (setq deltadays (string-to-number ans) ans ""))
  13824. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  13825. (when (string-match
  13826. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13827. (setq year (if (match-end 2)
  13828. (string-to-number (match-string 2 ans))
  13829. (string-to-number (format-time-string "%Y")))
  13830. month (string-to-number (match-string 3 ans))
  13831. day (string-to-number (match-string 4 ans)))
  13832. (if (< year 100) (setq year (+ 2000 year)))
  13833. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13834. t nil ans)))
  13835. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13836. ;; If there is a time with am/pm, and *no* time without it, we convert
  13837. ;; so that matching will be successful.
  13838. ;; FIXME: make this replace twice, so that we catch the end time.
  13839. (when (and (not (string-match "[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13840. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13841. (setq hour (string-to-number (match-string 1 ans))
  13842. minute (if (match-end 3) (string-to-number (match-string 3 ans)) 0)
  13843. pm (equal ?p (string-to-char (downcase (match-string 4 ans)))))
  13844. (if (and (= hour 12) (not pm))
  13845. (setq hour 0)
  13846. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13847. (setq ans (replace-match (format "%02d:%02d" hour minute) t t ans)))
  13848. ;; Check if there is a time range
  13849. (when (and (boundp 'org-end-time-was-given)
  13850. (string-match org-plain-time-of-day-regexp ans)
  13851. (match-end 8))
  13852. (setq org-end-time-was-given (match-string 8 ans))
  13853. (setq ans (concat (substring ans 0 (match-beginning 7))
  13854. (substring ans (match-end 7)))))
  13855. (setq tl (parse-time-string ans)
  13856. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  13857. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  13858. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  13859. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  13860. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  13861. second (or (nth 0 tl) 0)
  13862. wday (nth 6 tl))
  13863. (setq day (+ day deltadays))
  13864. (when (and wday (not (nth 3 tl)))
  13865. ;; Weekday was given, but no day, so pick that day in the week
  13866. ;; on or after the derived date.
  13867. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13868. (unless (equal wday wday1)
  13869. (setq day (+ day (% (- wday wday1 -7) 7)))))
  13870. (if (and (boundp 'org-time-was-given)
  13871. (nth 2 tl))
  13872. (setq org-time-was-given t))
  13873. (if (< year 100) (setq year (+ 2000 year)))
  13874. (if to-time
  13875. (encode-time second minute hour day month year)
  13876. (if (or (nth 1 tl) (nth 2 tl))
  13877. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  13878. (format "%04d-%02d-%02d" year month day)))))
  13879. (defun org-eval-in-calendar (form &optional keepdate)
  13880. "Eval FORM in the calendar window and return to current window.
  13881. Also, store the cursor date in variable org-ans2."
  13882. (let ((sw (selected-window)))
  13883. (select-window (get-buffer-window "*Calendar*"))
  13884. (eval form)
  13885. (when (and (not keepdate) (calendar-cursor-to-date))
  13886. (let* ((date (calendar-cursor-to-date))
  13887. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13888. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13889. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13890. (select-window sw)
  13891. ;; Update the prompt to show new default date
  13892. (save-excursion
  13893. (goto-char (point-min))
  13894. (when (and org-ans2
  13895. (re-search-forward "\\[[-0-9]+\\]" nil t)
  13896. (get-text-property (match-end 0) 'field))
  13897. (let ((inhibit-read-only t))
  13898. (replace-match (concat "[" org-ans2 "]") t t)
  13899. (add-text-properties (point-min) (1+ (match-end 0))
  13900. (text-properties-at (1+ (point-min)))))))))
  13901. (defun org-calendar-select ()
  13902. "Return to `org-read-date' with the date currently selected.
  13903. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13904. (interactive)
  13905. (when (calendar-cursor-to-date)
  13906. (let* ((date (calendar-cursor-to-date))
  13907. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13908. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13909. (if (active-minibuffer-window) (exit-minibuffer))))
  13910. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13911. "Insert a date stamp for the date given by the internal TIME.
  13912. WITH-HM means, use the stamp format that includes the time of the day.
  13913. INACTIVE means use square brackets instead of angular ones, so that the
  13914. stamp will not contribute to the agenda.
  13915. PRE and POST are optional strings to be inserted before and after the
  13916. stamp.
  13917. The command returns the inserted time stamp."
  13918. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13919. stamp)
  13920. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13921. (insert (or pre ""))
  13922. (insert (setq stamp (format-time-string fmt time)))
  13923. (when (listp extra)
  13924. (setq extra (car extra))
  13925. (if (and (stringp extra)
  13926. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13927. (setq extra (format "-%02d:%02d"
  13928. (string-to-number (match-string 1 extra))
  13929. (string-to-number (match-string 2 extra))))
  13930. (setq extra nil)))
  13931. (when extra
  13932. (backward-char 1)
  13933. (insert extra)
  13934. (forward-char 1))
  13935. (insert (or post ""))
  13936. stamp))
  13937. (defun org-toggle-time-stamp-overlays ()
  13938. "Toggle the use of custom time stamp formats."
  13939. (interactive)
  13940. (setq org-display-custom-times (not org-display-custom-times))
  13941. (unless org-display-custom-times
  13942. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13943. (while (setq p (next-single-property-change p 'display))
  13944. (if (and (get-text-property p 'display)
  13945. (eq (get-text-property p 'face) 'org-date))
  13946. (remove-text-properties
  13947. p (setq p (next-single-property-change p 'display))
  13948. '(display t))))
  13949. (set-buffer-modified-p bmp)))
  13950. (if (featurep 'xemacs)
  13951. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13952. (org-restart-font-lock)
  13953. (setq org-table-may-need-update t)
  13954. (if org-display-custom-times
  13955. (message "Time stamps are overlayed with custom format")
  13956. (message "Time stamp overlays removed")))
  13957. (defun org-display-custom-time (beg end)
  13958. "Overlay modified time stamp format over timestamp between BED and END."
  13959. (let* ((ts (buffer-substring beg end))
  13960. t1 w1 with-hm tf time str w2 (off 0))
  13961. (save-match-data
  13962. (setq t1 (org-parse-time-string ts t))
  13963. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  13964. (setq off (- (match-end 0) (match-beginning 0)))))
  13965. (setq end (- end off))
  13966. (setq w1 (- end beg)
  13967. with-hm (and (nth 1 t1) (nth 2 t1))
  13968. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13969. time (org-fix-decoded-time t1)
  13970. str (org-add-props
  13971. (format-time-string
  13972. (substring tf 1 -1) (apply 'encode-time time))
  13973. nil 'mouse-face 'highlight)
  13974. w2 (length str))
  13975. (if (not (= w2 w1))
  13976. (add-text-properties (1+ beg) (+ 2 beg)
  13977. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13978. (if (featurep 'xemacs)
  13979. (progn
  13980. (put-text-property beg end 'invisible t)
  13981. (put-text-property beg end 'end-glyph (make-glyph str)))
  13982. (put-text-property beg end 'display str))))
  13983. (defun org-translate-time (string)
  13984. "Translate all timestamps in STRING to custom format.
  13985. But do this only if the variable `org-display-custom-times' is set."
  13986. (when org-display-custom-times
  13987. (save-match-data
  13988. (let* ((start 0)
  13989. (re org-ts-regexp-both)
  13990. t1 with-hm inactive tf time str beg end)
  13991. (while (setq start (string-match re string start))
  13992. (setq beg (match-beginning 0)
  13993. end (match-end 0)
  13994. t1 (save-match-data
  13995. (org-parse-time-string (substring string beg end) t))
  13996. with-hm (and (nth 1 t1) (nth 2 t1))
  13997. inactive (equal (substring string beg (1+ beg)) "[")
  13998. tf (funcall (if with-hm 'cdr 'car)
  13999. org-time-stamp-custom-formats)
  14000. time (org-fix-decoded-time t1)
  14001. str (format-time-string
  14002. (concat
  14003. (if inactive "[" "<") (substring tf 1 -1)
  14004. (if inactive "]" ">"))
  14005. (apply 'encode-time time))
  14006. string (replace-match str t t string)
  14007. start (+ start (length str)))))))
  14008. string)
  14009. (defun org-fix-decoded-time (time)
  14010. "Set 0 instead of nil for the first 6 elements of time.
  14011. Don't touch the rest."
  14012. (let ((n 0))
  14013. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14014. (defun org-days-to-time (timestamp-string)
  14015. "Difference between TIMESTAMP-STRING and now in days."
  14016. (- (time-to-days (org-time-string-to-time timestamp-string))
  14017. (time-to-days (current-time))))
  14018. (defun org-deadline-close (timestamp-string &optional ndays)
  14019. "Is the time in TIMESTAMP-STRING close to the current date?"
  14020. (and (< (org-days-to-time timestamp-string)
  14021. (or ndays org-deadline-warning-days))
  14022. (not (org-entry-is-done-p))))
  14023. (defun org-calendar-select-mouse (ev)
  14024. "Return to `org-read-date' with the date currently selected.
  14025. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14026. (interactive "e")
  14027. (mouse-set-point ev)
  14028. (when (calendar-cursor-to-date)
  14029. (let* ((date (calendar-cursor-to-date))
  14030. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14031. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14032. (if (active-minibuffer-window) (exit-minibuffer))))
  14033. (defun org-check-deadlines (ndays)
  14034. "Check if there are any deadlines due or past due.
  14035. A deadline is considered due if it happens within `org-deadline-warning-days'
  14036. days from today's date. If the deadline appears in an entry marked DONE,
  14037. it is not shown. The prefix arg NDAYS can be used to test that many
  14038. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14039. (interactive "P")
  14040. (let* ((org-warn-days
  14041. (cond
  14042. ((equal ndays '(4)) 100000)
  14043. (ndays (prefix-numeric-value ndays))
  14044. (t org-deadline-warning-days)))
  14045. (case-fold-search nil)
  14046. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14047. (callback
  14048. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14049. (message "%d deadlines past-due or due within %d days"
  14050. (org-occur regexp nil callback)
  14051. org-warn-days)))
  14052. (defun org-evaluate-time-range (&optional to-buffer)
  14053. "Evaluate a time range by computing the difference between start and end.
  14054. Normally the result is just printed in the echo area, but with prefix arg
  14055. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14056. If the time range is actually in a table, the result is inserted into the
  14057. next column.
  14058. For time difference computation, a year is assumed to be exactly 365
  14059. days in order to avoid rounding problems."
  14060. (interactive "P")
  14061. (or
  14062. (org-clock-update-time-maybe)
  14063. (save-excursion
  14064. (unless (org-at-date-range-p)
  14065. (goto-char (point-at-bol))
  14066. (re-search-forward org-tr-regexp (point-at-eol) t))
  14067. (if (not (org-at-date-range-p))
  14068. (error "Not at a time-stamp range, and none found in current line")))
  14069. (let* ((ts1 (match-string 1))
  14070. (ts2 (match-string 2))
  14071. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14072. (match-end (match-end 0))
  14073. (time1 (org-time-string-to-time ts1))
  14074. (time2 (org-time-string-to-time ts2))
  14075. (t1 (time-to-seconds time1))
  14076. (t2 (time-to-seconds time2))
  14077. (diff (abs (- t2 t1)))
  14078. (negative (< (- t2 t1) 0))
  14079. ;; (ys (floor (* 365 24 60 60)))
  14080. (ds (* 24 60 60))
  14081. (hs (* 60 60))
  14082. (fy "%dy %dd %02d:%02d")
  14083. (fy1 "%dy %dd")
  14084. (fd "%dd %02d:%02d")
  14085. (fd1 "%dd")
  14086. (fh "%02d:%02d")
  14087. y d h m align)
  14088. (if havetime
  14089. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14090. y 0
  14091. d (floor (/ diff ds)) diff (mod diff ds)
  14092. h (floor (/ diff hs)) diff (mod diff hs)
  14093. m (floor (/ diff 60)))
  14094. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14095. y 0
  14096. d (floor (+ (/ diff ds) 0.5))
  14097. h 0 m 0))
  14098. (if (not to-buffer)
  14099. (message (org-make-tdiff-string y d h m))
  14100. (when (org-at-table-p)
  14101. (goto-char match-end)
  14102. (setq align t)
  14103. (and (looking-at " *|") (goto-char (match-end 0))))
  14104. (if (looking-at
  14105. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14106. (replace-match ""))
  14107. (if negative (insert " -"))
  14108. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14109. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14110. (insert " " (format fh h m))))
  14111. (if align (org-table-align))
  14112. (message "Time difference inserted")))))
  14113. (defun org-make-tdiff-string (y d h m)
  14114. (let ((fmt "")
  14115. (l nil))
  14116. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14117. l (push y l)))
  14118. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14119. l (push d l)))
  14120. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14121. l (push h l)))
  14122. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14123. l (push m l)))
  14124. (apply 'format fmt (nreverse l))))
  14125. (defun org-time-string-to-time (s)
  14126. (apply 'encode-time (org-parse-time-string s)))
  14127. (defun org-time-string-to-absolute (s &optional daynr)
  14128. "Convert a time stamp to an absolute day number.
  14129. If there is a specifyer for a cyclic time stamp, get the closest date to
  14130. DAYNR."
  14131. (cond
  14132. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14133. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14134. daynr
  14135. (+ daynr 1000)))
  14136. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14137. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14138. (time-to-days (current-time))) (match-string 0 s)))
  14139. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  14140. (defun org-calendar-holiday ()
  14141. "List of holidays, for Diary display in Org-mode."
  14142. (let ((hl (check-calendar-holidays date)))
  14143. (if hl (mapconcat 'identity hl "; "))))
  14144. (defun org-diary-sexp-entry (sexp entry date)
  14145. "Process a SEXP diary ENTRY for DATE."
  14146. (require 'diary-lib)
  14147. (let ((result (if calendar-debug-sexp
  14148. (let ((stack-trace-on-error t))
  14149. (eval (car (read-from-string sexp))))
  14150. (condition-case nil
  14151. (eval (car (read-from-string sexp)))
  14152. (error
  14153. (beep)
  14154. (message "Bad sexp at line %d in %s: %s"
  14155. (org-current-line)
  14156. (buffer-file-name) sexp)
  14157. (sleep-for 2))))))
  14158. (cond ((stringp result) result)
  14159. ((and (consp result)
  14160. (stringp (cdr result))) (cdr result))
  14161. (result entry)
  14162. (t nil))))
  14163. (defun org-diary-to-ical-string (frombuf)
  14164. "FIXME"
  14165. (let* ((tmpdir (if (featurep 'xemacs)
  14166. (temp-directory)
  14167. temporary-file-directory))
  14168. (tmpfile (make-temp-name
  14169. (expand-file-name "orgics" tmpdir)))
  14170. buf rtn b e)
  14171. (save-excursion
  14172. (set-buffer frombuf)
  14173. (icalendar-export-region (point-min) (point-max) tmpfile)
  14174. (setq buf (find-buffer-visiting tmpfile))
  14175. (set-buffer buf)
  14176. (goto-char (point-min))
  14177. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14178. (setq b (match-beginning 0)))
  14179. (goto-char (point-max))
  14180. (if (re-search-backward "^END:VEVENT" nil t)
  14181. (setq e (match-end 0)))
  14182. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14183. (kill-buffer buf)
  14184. (kill-buffer frombuf)
  14185. (delete-file tmpfile)
  14186. rtn))
  14187. (defun org-closest-date (start current change)
  14188. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  14189. ;; Make the proper lists from the dates
  14190. (catch 'exit
  14191. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14192. dn dw sday cday n1 n2
  14193. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14194. (setq start (org-date-to-gregorian start)
  14195. current (org-date-to-gregorian
  14196. (if org-agenda-repeating-timestamp-show-all
  14197. current
  14198. (time-to-days (current-time))))
  14199. sday (calendar-absolute-from-gregorian start)
  14200. cday (calendar-absolute-from-gregorian current))
  14201. (if (<= cday sday) (throw 'exit sday))
  14202. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14203. (setq dn (string-to-number (match-string 1 change))
  14204. dw (cdr (assoc (match-string 2 change) a1)))
  14205. (error "Invalid change specifyer: %s" change))
  14206. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14207. (cond
  14208. ((eq dw 'day)
  14209. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14210. n2 (+ n1 dn)))
  14211. ((eq dw 'year)
  14212. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14213. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14214. (setq date1 (list m d y1)
  14215. n1 (calendar-absolute-from-gregorian date1)
  14216. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14217. n2 (calendar-absolute-from-gregorian date2)))
  14218. ((eq dw 'month)
  14219. ;; approx number of month between the tow dates
  14220. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14221. ;; How often does dn fit in there?
  14222. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14223. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14224. m (+ m nm)
  14225. ny (floor (/ m 12))
  14226. y (+ y ny)
  14227. m (- m (* ny 12)))
  14228. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14229. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14230. (setq m2 (+ m dn) y2 y)
  14231. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14232. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14233. (while (< n2 cday)
  14234. (setq n1 n2 m m2 y y2)
  14235. (setq m2 (+ m dn) y2 y)
  14236. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14237. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14238. (if org-agenda-repeating-timestamp-show-all
  14239. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  14240. (if (= cday n1) n1 n2)))))
  14241. (defun org-date-to-gregorian (date)
  14242. "Turn any specification of DATE into a gregorian date for the calendar."
  14243. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14244. ((and (listp date) (= (length date) 3)) date)
  14245. ((stringp date)
  14246. (setq date (org-parse-time-string date))
  14247. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14248. ((listp date)
  14249. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14250. (defun org-parse-time-string (s &optional nodefault)
  14251. "Parse the standard Org-mode time string.
  14252. This should be a lot faster than the normal `parse-time-string'.
  14253. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14254. hour and minute fields will be nil if not given."
  14255. (if (string-match org-ts-regexp0 s)
  14256. (list 0
  14257. (if (or (match-beginning 8) (not nodefault))
  14258. (string-to-number (or (match-string 8 s) "0")))
  14259. (if (or (match-beginning 7) (not nodefault))
  14260. (string-to-number (or (match-string 7 s) "0")))
  14261. (string-to-number (match-string 4 s))
  14262. (string-to-number (match-string 3 s))
  14263. (string-to-number (match-string 2 s))
  14264. nil nil nil)
  14265. (make-list 9 0)))
  14266. (defun org-timestamp-up (&optional arg)
  14267. "Increase the date item at the cursor by one.
  14268. If the cursor is on the year, change the year. If it is on the month or
  14269. the day, change that.
  14270. With prefix ARG, change by that many units."
  14271. (interactive "p")
  14272. (org-timestamp-change (prefix-numeric-value arg)))
  14273. (defun org-timestamp-down (&optional arg)
  14274. "Decrease the date item at the cursor by one.
  14275. If the cursor is on the year, change the year. If it is on the month or
  14276. the day, change that.
  14277. With prefix ARG, change by that many units."
  14278. (interactive "p")
  14279. (org-timestamp-change (- (prefix-numeric-value arg))))
  14280. (defun org-timestamp-up-day (&optional arg)
  14281. "Increase the date in the time stamp by one day.
  14282. With prefix ARG, change that many days."
  14283. (interactive "p")
  14284. (if (and (not (org-at-timestamp-p t))
  14285. (org-on-heading-p))
  14286. (org-todo 'up)
  14287. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  14288. (defun org-timestamp-down-day (&optional arg)
  14289. "Decrease the date in the time stamp by one day.
  14290. With prefix ARG, change that many days."
  14291. (interactive "p")
  14292. (if (and (not (org-at-timestamp-p t))
  14293. (org-on-heading-p))
  14294. (org-todo 'down)
  14295. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  14296. (defsubst org-pos-in-match-range (pos n)
  14297. (and (match-beginning n)
  14298. (<= (match-beginning n) pos)
  14299. (>= (match-end n) pos)))
  14300. (defun org-at-timestamp-p (&optional inactive-ok)
  14301. "Determine if the cursor is in or at a timestamp."
  14302. (interactive)
  14303. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14304. (pos (point))
  14305. (ans (or (looking-at tsr)
  14306. (save-excursion
  14307. (skip-chars-backward "^[<\n\r\t")
  14308. (if (> (point) 1) (backward-char 1))
  14309. (and (looking-at tsr)
  14310. (> (- (match-end 0) pos) -1))))))
  14311. (and (boundp 'org-ts-what)
  14312. (setq org-ts-what
  14313. (cond
  14314. ((org-pos-in-match-range pos 2) 'year)
  14315. ((org-pos-in-match-range pos 3) 'month)
  14316. ((org-pos-in-match-range pos 7) 'hour)
  14317. ((org-pos-in-match-range pos 8) 'minute)
  14318. ((or (org-pos-in-match-range pos 4)
  14319. (org-pos-in-match-range pos 5)) 'day)
  14320. ((and (> pos (or (match-end 8) (match-end 5)))
  14321. (< pos (match-end 0)))
  14322. (- pos (or (match-end 8) (match-end 5))))
  14323. (t 'day))))
  14324. ans))
  14325. (defun org-timestamp-change (n &optional what)
  14326. "Change the date in the time stamp at point.
  14327. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14328. `year', `minute', `second'. If WHAT is not given, the cursor position
  14329. in the timestamp determines what will be changed."
  14330. (let ((pos (point))
  14331. with-hm inactive
  14332. org-ts-what
  14333. extra
  14334. ts time time0)
  14335. (if (not (org-at-timestamp-p t))
  14336. (error "Not at a timestamp"))
  14337. (if (and (not what) (not (eq org-ts-what 'day))
  14338. org-display-custom-times
  14339. (get-text-property (point) 'display)
  14340. (not (get-text-property (1- (point)) 'display)))
  14341. (setq org-ts-what 'day))
  14342. (setq org-ts-what (or what org-ts-what)
  14343. inactive (= (char-after (match-beginning 0)) ?\[)
  14344. ts (match-string 0))
  14345. (replace-match "")
  14346. (if (string-match
  14347. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  14348. ts)
  14349. (setq extra (match-string 1 ts)))
  14350. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14351. (setq with-hm t))
  14352. (setq time0 (org-parse-time-string ts))
  14353. (setq time
  14354. (apply 'encode-time
  14355. (append
  14356. (list (or (car time0) 0))
  14357. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  14358. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  14359. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  14360. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  14361. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  14362. (nthcdr 6 time0))))
  14363. (when (integerp org-ts-what)
  14364. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  14365. (if (eq what 'calendar)
  14366. (let ((cal-date
  14367. (save-excursion
  14368. (save-match-data
  14369. (set-buffer "*Calendar*")
  14370. (calendar-cursor-to-date)))))
  14371. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14372. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14373. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14374. (setcar time0 (or (car time0) 0))
  14375. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14376. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  14377. (setq time (apply 'encode-time time0))))
  14378. (setq org-last-changed-timestamp
  14379. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14380. (org-clock-update-time-maybe)
  14381. (goto-char pos)
  14382. ;; Try to recenter the calendar window, if any
  14383. (if (and org-calendar-follow-timestamp-change
  14384. (get-buffer-window "*Calendar*" t)
  14385. (memq org-ts-what '(day month year)))
  14386. (org-recenter-calendar (time-to-days time)))))
  14387. (defun org-modify-ts-extra (s pos n)
  14388. "FIXME"
  14389. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14390. ng h m new)
  14391. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14392. (cond
  14393. ((or (org-pos-in-match-range pos 2)
  14394. (org-pos-in-match-range pos 3))
  14395. (setq m (string-to-number (match-string 3 s))
  14396. h (string-to-number (match-string 2 s)))
  14397. (if (org-pos-in-match-range pos 2)
  14398. (setq h (+ h n))
  14399. (setq m (+ m n)))
  14400. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14401. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14402. (setq h (min 24 (max 0 h)))
  14403. (setq ng 1 new (format "-%02d:%02d" h m)))
  14404. ((org-pos-in-match-range pos 6)
  14405. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14406. ((org-pos-in-match-range pos 5)
  14407. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  14408. (when ng
  14409. (setq s (concat
  14410. (substring s 0 (match-beginning ng))
  14411. new
  14412. (substring s (match-end ng))))))
  14413. s))
  14414. (defun org-recenter-calendar (date)
  14415. "If the calendar is visible, recenter it to DATE."
  14416. (let* ((win (selected-window))
  14417. (cwin (get-buffer-window "*Calendar*" t))
  14418. (calendar-move-hook nil))
  14419. (when cwin
  14420. (select-window cwin)
  14421. (calendar-goto-date (if (listp date) date
  14422. (calendar-gregorian-from-absolute date)))
  14423. (select-window win))))
  14424. (defun org-goto-calendar (&optional arg)
  14425. "Go to the Emacs calendar at the current date.
  14426. If there is a time stamp in the current line, go to that date.
  14427. A prefix ARG can be used to force the current date."
  14428. (interactive "P")
  14429. (let ((tsr org-ts-regexp) diff
  14430. (calendar-move-hook nil)
  14431. (view-calendar-holidays-initially nil)
  14432. (view-diary-entries-initially nil))
  14433. (if (or (org-at-timestamp-p)
  14434. (save-excursion
  14435. (beginning-of-line 1)
  14436. (looking-at (concat ".*" tsr))))
  14437. (let ((d1 (time-to-days (current-time)))
  14438. (d2 (time-to-days
  14439. (org-time-string-to-time (match-string 1)))))
  14440. (setq diff (- d2 d1))))
  14441. (calendar)
  14442. (calendar-goto-today)
  14443. (if (and diff (not arg)) (calendar-forward-day diff))))
  14444. (defun org-date-from-calendar ()
  14445. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14446. If there is already a time stamp at the cursor position, update it."
  14447. (interactive)
  14448. (org-timestamp-change 0 'calendar))
  14449. ;;; The clock for measuring work time.
  14450. (defvar org-mode-line-string "")
  14451. (put 'org-mode-line-string 'risky-local-variable t)
  14452. (defvar org-mode-line-timer nil)
  14453. (defvar org-clock-heading "")
  14454. (defvar org-clock-start-time "")
  14455. (defun org-update-mode-line ()
  14456. (let* ((delta (- (time-to-seconds (current-time))
  14457. (time-to-seconds org-clock-start-time)))
  14458. (h (floor delta 3600))
  14459. (m (floor (- delta (* 3600 h)) 60)))
  14460. (setq org-mode-line-string
  14461. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  14462. 'help-echo "Org-mode clock is running"))
  14463. (force-mode-line-update)))
  14464. (defvar org-clock-marker (make-marker)
  14465. "Marker recording the last clock-in.")
  14466. (defvar org-clock-mode-line-entry nil
  14467. "Information for the modeline about the running clock.")
  14468. (defun org-clock-in ()
  14469. "Start the clock on the current item.
  14470. If necessary, clock-out of the currently active clock."
  14471. (interactive)
  14472. (org-clock-out t)
  14473. (let (ts)
  14474. (save-excursion
  14475. (org-back-to-heading t)
  14476. (if (looking-at org-todo-line-regexp)
  14477. (setq org-clock-heading (match-string 3))
  14478. (setq org-clock-heading "???"))
  14479. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  14480. (beginning-of-line 2)
  14481. (while
  14482. (or (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14483. (not (equal (match-string 1) org-clock-string)))
  14484. (and (looking-at "[ \t]*:PROPERTIES:")
  14485. (not org-insert-labeled-timestamps-before-properties-drawer)))
  14486. ;; Scheduling info, or properties drawer, move one line further
  14487. (beginning-of-line 2)
  14488. (or (bolp) (newline)))
  14489. (insert "\n") (backward-char 1)
  14490. (indent-relative)
  14491. (insert org-clock-string " ")
  14492. (setq org-clock-start-time (current-time))
  14493. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14494. (move-marker org-clock-marker (point) (buffer-base-buffer))
  14495. (or global-mode-string (setq global-mode-string '("")))
  14496. (or (memq 'org-mode-line-string global-mode-string)
  14497. (setq global-mode-string
  14498. (append global-mode-string '(org-mode-line-string))))
  14499. (org-update-mode-line)
  14500. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  14501. (message "Clock started at %s" ts))))
  14502. (defun org-clock-out (&optional fail-quietly)
  14503. "Stop the currently running clock.
  14504. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  14505. (interactive)
  14506. (catch 'exit
  14507. (if (not (marker-buffer org-clock-marker))
  14508. (if fail-quietly (throw 'exit t) (error "No active clock")))
  14509. (let (ts te s h m)
  14510. (save-excursion
  14511. (set-buffer (marker-buffer org-clock-marker))
  14512. (goto-char org-clock-marker)
  14513. (beginning-of-line 1)
  14514. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  14515. (equal (match-string 1) org-clock-string))
  14516. (setq ts (match-string 2))
  14517. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  14518. (goto-char (match-end 0))
  14519. (delete-region (point) (point-at-eol))
  14520. (insert "--")
  14521. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  14522. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  14523. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  14524. h (floor (/ s 3600))
  14525. s (- s (* 3600 h))
  14526. m (floor (/ s 60))
  14527. s (- s (* 60 s)))
  14528. (insert " => " (format "%2d:%02d" h m))
  14529. (move-marker org-clock-marker nil)
  14530. (org-add-log-maybe 'clock-out)
  14531. (when org-mode-line-timer
  14532. (cancel-timer org-mode-line-timer)
  14533. (setq org-mode-line-timer nil))
  14534. (setq global-mode-string
  14535. (delq 'org-mode-line-string global-mode-string))
  14536. (force-mode-line-update)
  14537. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  14538. (defun org-clock-cancel ()
  14539. "Cancel the running clock be removing the start timestamp."
  14540. (interactive)
  14541. (if (not (marker-buffer org-clock-marker))
  14542. (error "No active clock"))
  14543. (save-excursion
  14544. (set-buffer (marker-buffer org-clock-marker))
  14545. (goto-char org-clock-marker)
  14546. (delete-region (1- (point-at-bol)) (point-at-eol)))
  14547. (message "Clock canceled"))
  14548. (defvar org-clock-file-total-minutes nil
  14549. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  14550. (make-variable-buffer-local 'org-clock-file-total-minutes)
  14551. (defun org-clock-sum (&optional tstart tend)
  14552. "Sum the times for each subtree.
  14553. Puts the resulting times in minutes as a text property on each headline."
  14554. (interactive)
  14555. (let* ((bmp (buffer-modified-p))
  14556. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  14557. org-clock-string
  14558. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  14559. (lmax 30)
  14560. (ltimes (make-vector lmax 0))
  14561. (t1 0)
  14562. (level 0)
  14563. ts te dt
  14564. time)
  14565. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  14566. (save-excursion
  14567. (goto-char (point-max))
  14568. (while (re-search-backward re nil t)
  14569. (cond
  14570. ((match-end 2)
  14571. ;; Two time stamps
  14572. (setq ts (match-string 2)
  14573. te (match-string 3)
  14574. ts (time-to-seconds
  14575. (apply 'encode-time (org-parse-time-string ts)))
  14576. te (time-to-seconds
  14577. (apply 'encode-time (org-parse-time-string te)))
  14578. ts (if tstart (max ts tstart) ts)
  14579. te (if tend (min te tend) te)
  14580. dt (- te ts)
  14581. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  14582. ((match-end 4)
  14583. ;; A naket time
  14584. (setq t1 (+ t1 (string-to-number (match-string 5))
  14585. (* 60 (string-to-number (match-string 4))))))
  14586. (t ;; A headline
  14587. (setq level (- (match-end 1) (match-beginning 1)))
  14588. (when (or (> t1 0) (> (aref ltimes level) 0))
  14589. (loop for l from 0 to level do
  14590. (aset ltimes l (+ (aref ltimes l) t1)))
  14591. (setq t1 0 time (aref ltimes level))
  14592. (loop for l from level to (1- lmax) do
  14593. (aset ltimes l 0))
  14594. (goto-char (match-beginning 0))
  14595. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  14596. (setq org-clock-file-total-minutes (aref ltimes 0)))
  14597. (set-buffer-modified-p bmp)))
  14598. (defun org-clock-display (&optional total-only)
  14599. "Show subtree times in the entire buffer.
  14600. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  14601. in the echo area."
  14602. (interactive)
  14603. (org-remove-clock-overlays)
  14604. (let (time h m p)
  14605. (org-clock-sum)
  14606. (unless total-only
  14607. (save-excursion
  14608. (goto-char (point-min))
  14609. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14610. (goto-char p)
  14611. (when (setq time (get-text-property p :org-clock-minutes))
  14612. (org-put-clock-overlay time (funcall outline-level))))
  14613. (setq h (/ org-clock-file-total-minutes 60)
  14614. m (- org-clock-file-total-minutes (* 60 h)))
  14615. ;; Arrange to remove the overlays upon next change.
  14616. (when org-remove-highlights-with-change
  14617. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  14618. nil 'local))))
  14619. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  14620. (defvar org-clock-overlays nil)
  14621. (make-variable-buffer-local 'org-clock-overlays)
  14622. (defun org-put-clock-overlay (time &optional level)
  14623. "Put an overlays on the current line, displaying TIME.
  14624. If LEVEL is given, prefix time with a corresponding number of stars.
  14625. This creates a new overlay and stores it in `org-clock-overlays', so that it
  14626. will be easy to remove."
  14627. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  14628. (l (if level (org-get-legal-level level 0) 0))
  14629. (off 0)
  14630. ov tx)
  14631. (move-to-column c)
  14632. (unless (eolp) (skip-chars-backward "^ \t"))
  14633. (skip-chars-backward " \t")
  14634. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  14635. tx (concat (buffer-substring (1- (point)) (point))
  14636. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  14637. (org-add-props (format "%s %2d:%02d%s"
  14638. (make-string l ?*) h m
  14639. (make-string (- 10 l) ?\ ))
  14640. '(face secondary-selection))
  14641. ""))
  14642. (if (not (featurep 'xemacs))
  14643. (org-overlay-put ov 'display tx)
  14644. (org-overlay-put ov 'invisible t)
  14645. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  14646. (push ov org-clock-overlays)))
  14647. (defun org-remove-clock-overlays (&optional beg end noremove)
  14648. "Remove the occur highlights from the buffer.
  14649. BEG and END are ignored. If NOREMOVE is nil, remove this function
  14650. from the `before-change-functions' in the current buffer."
  14651. (interactive)
  14652. (unless org-inhibit-highlight-removal
  14653. (mapc 'org-delete-overlay org-clock-overlays)
  14654. (setq org-clock-overlays nil)
  14655. (unless noremove
  14656. (remove-hook 'before-change-functions
  14657. 'org-remove-clock-overlays 'local))))
  14658. (defun org-clock-out-if-current ()
  14659. "Clock out if the current entry contains the running clock.
  14660. This is used to stop the clock after a TODO entry is marked DONE,
  14661. and is only done if the variable `org-clock-out-when-done' is not nil."
  14662. (when (and org-clock-out-when-done
  14663. (member state org-done-keywords)
  14664. (equal (marker-buffer org-clock-marker) (current-buffer))
  14665. (< (point) org-clock-marker)
  14666. (> (save-excursion (outline-next-heading) (point))
  14667. org-clock-marker))
  14668. ;; Clock out, but don't accept a logging message for this.
  14669. (let ((org-log-done (if (and (listp org-log-done)
  14670. (member 'clock-out org-log-done))
  14671. '(done)
  14672. org-log-done)))
  14673. (org-clock-out))))
  14674. (add-hook 'org-after-todo-state-change-hook
  14675. 'org-clock-out-if-current)
  14676. (defun org-check-running-clock ()
  14677. "Check if the current buffer contains the running clock.
  14678. If yes, offer to stop it and to save the buffer with the changes."
  14679. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  14680. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  14681. (buffer-name))))
  14682. (org-clock-out)
  14683. (when (y-or-n-p "Save changed buffer?")
  14684. (save-buffer))))
  14685. (defun org-clock-report ()
  14686. "Create a table containing a report about clocked time.
  14687. If the buffer contains lines
  14688. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  14689. #+END: clocktable
  14690. then the table will be inserted between these lines, replacing whatever
  14691. is was there before. If these lines are not in the buffer, the table
  14692. is inserted at point, surrounded by the special lines.
  14693. The BEGIN line can contain parameters. Allowed are:
  14694. :maxlevel The maximum level to be included in the table. Default is 3.
  14695. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  14696. (interactive)
  14697. (org-remove-clock-overlays)
  14698. (unless (org-find-dblock "clocktable")
  14699. (org-create-dblock (list :name "clocktable"
  14700. :maxlevel 2 :emphasize nil)))
  14701. (org-update-dblock))
  14702. (defun org-clock-update-time-maybe ()
  14703. "If this is a CLOCK line, update it and return t.
  14704. Otherwise, return nil."
  14705. (interactive)
  14706. (save-excursion
  14707. (beginning-of-line 1)
  14708. (skip-chars-forward " \t")
  14709. (when (looking-at org-clock-string)
  14710. (let ((re (concat "[ \t]*" org-clock-string
  14711. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  14712. "\\([ \t]*=>.*\\)?"))
  14713. ts te h m s)
  14714. (if (not (looking-at re))
  14715. nil
  14716. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  14717. (end-of-line 1)
  14718. (setq ts (match-string 1)
  14719. te (match-string 2))
  14720. (setq s (- (time-to-seconds
  14721. (apply 'encode-time (org-parse-time-string te)))
  14722. (time-to-seconds
  14723. (apply 'encode-time (org-parse-time-string ts))))
  14724. h (floor (/ s 3600))
  14725. s (- s (* 3600 h))
  14726. m (floor (/ s 60))
  14727. s (- s (* 60 s)))
  14728. (insert " => " (format "%2d:%02d" h m))
  14729. t)))))
  14730. (defun org-clock-special-range (key &optional time as-strings)
  14731. "Return two times bordering a special time range.
  14732. Key is a symbol specifying the range and can be one of `today', `yesterday',
  14733. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  14734. A week starts Monday 0:00 and ends Sunday 24:00.
  14735. The range is determined relative to TIME. TIME defaults to the current time.
  14736. The return value is a cons cell with two internal times like the ones
  14737. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  14738. the returned times will be formatted strings."
  14739. (let* ((tm (decode-time (or time (current-time))))
  14740. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  14741. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  14742. (dow (nth 6 tm))
  14743. s1 m1 h1 d1 month1 y1 diff ts te fm)
  14744. (cond
  14745. ((eq key 'today)
  14746. (setq h 0 m 0 h1 24 m1 0))
  14747. ((eq key 'yesterday)
  14748. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  14749. ((eq key 'thisweek)
  14750. (setq diff (if (= dow 0) 6 (1- dow))
  14751. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14752. ((eq key 'lastweek)
  14753. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  14754. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  14755. ((eq key 'thismonth)
  14756. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  14757. ((eq key 'lastmonth)
  14758. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  14759. ((eq key 'thisyear)
  14760. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  14761. ((eq key 'lastyear)
  14762. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  14763. (t (error "No such time block %s" key)))
  14764. (setq ts (encode-time s m h d month y)
  14765. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  14766. (or d1 d) (or month1 month) (or y1 y)))
  14767. (setq fm (cdr org-time-stamp-formats))
  14768. (if as-strings
  14769. (cons (format-time-string fm ts) (format-time-string fm te))
  14770. (cons ts te))))
  14771. (defun org-dblock-write:clocktable (params)
  14772. "Write the standard clocktable."
  14773. (let ((hlchars '((1 . "*") (2 . ?/)))
  14774. (emph nil)
  14775. (ins (make-marker))
  14776. ipos time h m p level hlc hdl maxlevel
  14777. ts te cc block)
  14778. (setq maxlevel (or (plist-get params :maxlevel) 3)
  14779. emph (plist-get params :emphasize)
  14780. ts (plist-get params :tstart)
  14781. te (plist-get params :tend)
  14782. block (plist-get params :block))
  14783. (when block
  14784. (setq cc (org-clock-special-range block nil t)
  14785. ts (car cc) te (cdr cc)))
  14786. (if ts (setq ts (time-to-seconds
  14787. (apply 'encode-time (org-parse-time-string ts)))))
  14788. (if te (setq te (time-to-seconds
  14789. (apply 'encode-time (org-parse-time-string te)))))
  14790. (move-marker ins (point))
  14791. (setq ipos (point))
  14792. (insert-before-markers "Clock summary at ["
  14793. (substring
  14794. (format-time-string (cdr org-time-stamp-formats))
  14795. 1 -1)
  14796. "]."
  14797. (if block
  14798. (format " Considered range is /%s/." block)
  14799. "")
  14800. "\n\n|L|Headline|Time|\n")
  14801. (org-clock-sum ts te)
  14802. (setq h (/ org-clock-file-total-minutes 60)
  14803. m (- org-clock-file-total-minutes (* 60 h)))
  14804. (insert-before-markers "|-\n|0|" "*Total file time*| "
  14805. (format "*%d:%02d*" h m)
  14806. "|\n")
  14807. (goto-char (point-min))
  14808. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  14809. (goto-char p)
  14810. (when (setq time (get-text-property p :org-clock-minutes))
  14811. (save-excursion
  14812. (beginning-of-line 1)
  14813. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  14814. (setq level (- (match-end 1) (match-beginning 1)))
  14815. (<= level maxlevel))
  14816. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  14817. hdl (match-string 2)
  14818. h (/ time 60)
  14819. m (- time (* 60 h)))
  14820. (goto-char ins)
  14821. (if (= level 1) (insert-before-markers "|-\n"))
  14822. (insert-before-markers
  14823. "| " (int-to-string level) "|" hlc hdl hlc " |"
  14824. (make-string (1- level) ?|)
  14825. hlc
  14826. (format "%d:%02d" h m)
  14827. hlc
  14828. " |\n")))))
  14829. (goto-char ins)
  14830. (backward-delete-char 1)
  14831. (goto-char ipos)
  14832. (skip-chars-forward "^|")
  14833. (org-table-align)))
  14834. ;; FIXME: I don't think anybody uses this, ask David
  14835. (defun org-collect-clock-time-entries ()
  14836. "Return an internal list with clocking information.
  14837. This list has one entry for each CLOCK interval.
  14838. FIXME: describe the elements."
  14839. (interactive)
  14840. (let ((re (concat "^[ \t]*" org-clock-string
  14841. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  14842. rtn beg end next cont level title total closedp leafp
  14843. clockpos titlepos h m donep)
  14844. (save-excursion
  14845. (org-clock-sum)
  14846. (goto-char (point-min))
  14847. (while (re-search-forward re nil t)
  14848. (setq clockpos (match-beginning 0)
  14849. beg (match-string 1) end (match-string 2)
  14850. cont (match-end 0))
  14851. (setq beg (apply 'encode-time (org-parse-time-string beg))
  14852. end (apply 'encode-time (org-parse-time-string end)))
  14853. (org-back-to-heading t)
  14854. (setq donep (org-entry-is-done-p))
  14855. (setq titlepos (point)
  14856. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  14857. h (/ total 60) m (- total (* 60 h))
  14858. total (cons h m))
  14859. (looking-at "\\(\\*+\\) +\\(.*\\)")
  14860. (setq level (- (match-end 1) (match-beginning 1))
  14861. title (org-match-string-no-properties 2))
  14862. (save-excursion (outline-next-heading) (setq next (point)))
  14863. (setq closedp (re-search-forward org-closed-time-regexp next t))
  14864. (goto-char next)
  14865. (setq leafp (and (looking-at "^\\*+ ")
  14866. (<= (- (match-end 0) (point)) level)))
  14867. (push (list beg end clockpos closedp donep
  14868. total title titlepos level leafp)
  14869. rtn)
  14870. (goto-char cont)))
  14871. (nreverse rtn)))
  14872. ;;;; Agenda, and Diary Integration
  14873. ;;; Define the Org-agenda-mode
  14874. (defvar org-agenda-mode-map (make-sparse-keymap)
  14875. "Keymap for `org-agenda-mode'.")
  14876. (defvar org-agenda-menu) ; defined later in this file.
  14877. (defvar org-agenda-follow-mode nil)
  14878. (defvar org-agenda-show-log nil)
  14879. (defvar org-agenda-redo-command nil)
  14880. (defvar org-agenda-mode-hook nil)
  14881. (defvar org-agenda-type nil)
  14882. (defvar org-agenda-force-single-file nil)
  14883. (defun org-agenda-mode ()
  14884. "Mode for time-sorted view on action items in Org-mode files.
  14885. The following commands are available:
  14886. \\{org-agenda-mode-map}"
  14887. (interactive)
  14888. (kill-all-local-variables)
  14889. (setq org-agenda-undo-list nil
  14890. org-agenda-pending-undo-list nil)
  14891. (setq major-mode 'org-agenda-mode)
  14892. ;; Keep global-font-lock-mode from turning on font-lock-mode
  14893. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  14894. (setq mode-name "Org-Agenda")
  14895. (use-local-map org-agenda-mode-map)
  14896. (easy-menu-add org-agenda-menu)
  14897. (if org-startup-truncated (setq truncate-lines t))
  14898. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  14899. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  14900. ;; Make sure properties are removed when copying text
  14901. (when (boundp 'buffer-substring-filters)
  14902. (org-set-local 'buffer-substring-filters
  14903. (cons (lambda (x)
  14904. (set-text-properties 0 (length x) nil x) x)
  14905. buffer-substring-filters)))
  14906. (unless org-agenda-keep-modes
  14907. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  14908. org-agenda-show-log nil))
  14909. (easy-menu-change
  14910. '("Agenda") "Agenda Files"
  14911. (append
  14912. (list
  14913. (vector
  14914. (if (get 'org-agenda-files 'org-restrict)
  14915. "Restricted to single file"
  14916. "Edit File List")
  14917. '(org-edit-agenda-file-list)
  14918. (not (get 'org-agenda-files 'org-restrict)))
  14919. "--")
  14920. (mapcar 'org-file-menu-entry (org-agenda-files))))
  14921. (org-agenda-set-mode-name)
  14922. (apply
  14923. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  14924. (list 'org-agenda-mode-hook)))
  14925. (substitute-key-definition 'undo 'org-agenda-undo
  14926. org-agenda-mode-map global-map)
  14927. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  14928. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  14929. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  14930. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  14931. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  14932. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  14933. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  14934. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  14935. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  14936. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  14937. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  14938. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  14939. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  14940. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  14941. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  14942. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  14943. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  14944. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  14945. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  14946. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  14947. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  14948. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  14949. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  14950. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  14951. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  14952. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  14953. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  14954. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  14955. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  14956. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  14957. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  14958. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  14959. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  14960. (while l (org-defkey org-agenda-mode-map
  14961. (int-to-string (pop l)) 'digit-argument)))
  14962. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  14963. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  14964. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  14965. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  14966. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  14967. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  14968. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  14969. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  14970. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  14971. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  14972. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  14973. (org-defkey org-agenda-mode-map "n" 'next-line)
  14974. (org-defkey org-agenda-mode-map "p" 'previous-line)
  14975. (org-defkey org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  14976. (org-defkey org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  14977. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  14978. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  14979. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  14980. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  14981. (eval-after-load "calendar"
  14982. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  14983. 'org-calendar-goto-agenda))
  14984. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  14985. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  14986. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  14987. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  14988. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  14989. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  14990. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  14991. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  14992. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  14993. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  14994. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  14995. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  14996. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  14997. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  14998. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  14999. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  15000. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  15001. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  15002. "Local keymap for agenda entries from Org-mode.")
  15003. (org-defkey org-agenda-keymap
  15004. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  15005. (org-defkey org-agenda-keymap
  15006. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  15007. (when org-agenda-mouse-1-follows-link
  15008. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  15009. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  15010. '("Agenda"
  15011. ("Agenda Files")
  15012. "--"
  15013. ["Show" org-agenda-show t]
  15014. ["Go To (other window)" org-agenda-goto t]
  15015. ["Go To (this window)" org-agenda-switch-to t]
  15016. ["Follow Mode" org-agenda-follow-mode
  15017. :style toggle :selected org-agenda-follow-mode :active t]
  15018. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  15019. "--"
  15020. ["Cycle TODO" org-agenda-todo t]
  15021. ["Archive subtree" org-agenda-archive t]
  15022. ["Delete subtree" org-agenda-kill t]
  15023. "--"
  15024. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  15025. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  15026. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  15027. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  15028. "--"
  15029. ("Tags and Properties"
  15030. ["Show all Tags" org-agenda-show-tags t]
  15031. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  15032. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  15033. "--"
  15034. ["Column View" org-columns t])
  15035. ("Date/Schedule"
  15036. ["Schedule" org-agenda-schedule t]
  15037. ["Set Deadline" org-agenda-deadline t]
  15038. "--"
  15039. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  15040. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  15041. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  15042. ("Priority"
  15043. ["Set Priority" org-agenda-priority t]
  15044. ["Increase Priority" org-agenda-priority-up t]
  15045. ["Decrease Priority" org-agenda-priority-down t]
  15046. ["Show Priority" org-agenda-show-priority t])
  15047. ("Calendar/Diary"
  15048. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  15049. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  15050. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  15051. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  15052. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  15053. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  15054. "--"
  15055. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  15056. "--"
  15057. ("View"
  15058. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  15059. :style radio :selected (equal org-agenda-ndays 1)]
  15060. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  15061. :style radio :selected (equal org-agenda-ndays 7)]
  15062. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  15063. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  15064. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  15065. :style radio :selected (member org-agenda-ndays '(365 366))]
  15066. "--"
  15067. ["Show Logbook entries" org-agenda-log-mode
  15068. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  15069. ["Include Diary" org-agenda-toggle-diary
  15070. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  15071. ["Use Time Grid" org-agenda-toggle-time-grid
  15072. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  15073. ["Write view to file" org-write-agenda t]
  15074. ["Rebuild buffer" org-agenda-redo t]
  15075. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  15076. "--"
  15077. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  15078. "--"
  15079. ["Quit" org-agenda-quit t]
  15080. ["Exit and Release Buffers" org-agenda-exit t]
  15081. ))
  15082. ;;; Agenda undo
  15083. (defvar org-agenda-allow-remote-undo t
  15084. "Non-nil means, allow remote undo from the agenda buffer.")
  15085. (defvar org-agenda-undo-list nil
  15086. "List of undoable operations in the agenda since last refresh.")
  15087. (defvar org-agenda-undo-has-started-in nil
  15088. "Buffers that have already seen `undo-start' in the current undo sequence.")
  15089. (defvar org-agenda-pending-undo-list nil
  15090. "In a series of undo commands, this is the list of remaning undo items.")
  15091. (defmacro org-if-unprotected (&rest body)
  15092. "Execute BODY if there is no `org-protected' text property at point."
  15093. (declare (debug t))
  15094. `(unless (get-text-property (point) 'org-protected)
  15095. ,@body))
  15096. (defmacro org-with-remote-undo (_buffer &rest _body)
  15097. "Execute BODY while recording undo information in two buffers."
  15098. (declare (indent 1) (debug t))
  15099. `(let ((_cline (org-current-line))
  15100. (_cmd this-command)
  15101. (_buf1 (current-buffer))
  15102. (_buf2 ,_buffer)
  15103. (_undo1 buffer-undo-list)
  15104. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  15105. _c1 _c2)
  15106. ,@_body
  15107. (when org-agenda-allow-remote-undo
  15108. (setq _c1 (org-verify-change-for-undo
  15109. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  15110. _c2 (org-verify-change-for-undo
  15111. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  15112. (when (or _c1 _c2)
  15113. ;; make sure there are undo boundaries
  15114. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  15115. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  15116. ;; remember which buffer to undo
  15117. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  15118. org-agenda-undo-list)))))
  15119. (defun org-agenda-undo ()
  15120. "Undo a remote editing step in the agenda.
  15121. This undoes changes both in the agenda buffer and in the remote buffer
  15122. that have been changed along."
  15123. (interactive)
  15124. (or org-agenda-allow-remote-undo
  15125. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  15126. (if (not (eq this-command last-command))
  15127. (setq org-agenda-undo-has-started-in nil
  15128. org-agenda-pending-undo-list org-agenda-undo-list))
  15129. (if (not org-agenda-pending-undo-list)
  15130. (error "No further undo information"))
  15131. (let* ((entry (pop org-agenda-pending-undo-list))
  15132. buf line cmd rembuf)
  15133. (setq cmd (pop entry) line (pop entry))
  15134. (setq rembuf (nth 2 entry))
  15135. (org-with-remote-undo rembuf
  15136. (while (bufferp (setq buf (pop entry)))
  15137. (if (pop entry)
  15138. (with-current-buffer buf
  15139. (let ((last-undo-buffer buf)
  15140. (inhibit-read-only t))
  15141. (unless (memq buf org-agenda-undo-has-started-in)
  15142. (push buf org-agenda-undo-has-started-in)
  15143. (make-local-variable 'pending-undo-list)
  15144. (undo-start))
  15145. (while (and pending-undo-list
  15146. (listp pending-undo-list)
  15147. (not (car pending-undo-list)))
  15148. (pop pending-undo-list))
  15149. (undo-more 1))))))
  15150. (goto-line line)
  15151. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  15152. (defun org-verify-change-for-undo (l1 l2)
  15153. "Verify that a real change occurred between the undo lists L1 and L2."
  15154. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  15155. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  15156. (not (eq l1 l2)))
  15157. ;;; Agenda dispatch
  15158. (defvar org-agenda-restrict nil)
  15159. (defvar org-agenda-restrict-begin (make-marker))
  15160. (defvar org-agenda-restrict-end (make-marker))
  15161. (defvar org-agenda-last-dispatch-buffer nil)
  15162. ;;;###autoload
  15163. (defun org-agenda (arg)
  15164. "Dispatch agenda commands to collect entries to the agenda buffer.
  15165. Prompts for a character to select a command. Any prefix arg will be passed
  15166. on to the selected command. The default selections are:
  15167. g
  15168. a Call `org-agenda-list' to display the agenda for current day or week.
  15169. t Call `org-todo-list' to display the global todo list.
  15170. T Call `org-todo-list' to display the global todo list, select only
  15171. entries with a specific TODO keyword (the user gets a prompt).
  15172. m Call `org-tags-view' to display headlines with tags matching
  15173. a condition (the user is prompted for the condition).
  15174. M Like `m', but select only TODO entries, no ordinary headlines.
  15175. L Create a timeline for the current buffer.
  15176. e Export views to associated files.
  15177. More commands can be added by configuring the variable
  15178. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  15179. searches can be pre-defined in this way.
  15180. If the current buffer is in Org-mode and visiting a file, you can also
  15181. first press `1' to indicate that the agenda should be temporarily (until the
  15182. next use of \\[org-agenda]) restricted to the current file."
  15183. (interactive "P")
  15184. (catch 'exit
  15185. (let* ((buf (current-buffer))
  15186. (bfn (buffer-file-name (buffer-base-buffer)))
  15187. (restrict-ok (and bfn (org-mode-p)))
  15188. (custom org-agenda-custom-commands)
  15189. c entry key type match lprops)
  15190. ;; Turn off restriction
  15191. (put 'org-agenda-files 'org-restrict nil)
  15192. (setq org-agenda-restrict nil)
  15193. (move-marker org-agenda-restrict-begin nil)
  15194. (move-marker org-agenda-restrict-end nil)
  15195. ;; Delete old local properties
  15196. (put 'org-agenda-redo-command 'org-lprops nil)
  15197. ;; Remember where this call originated
  15198. (setq org-agenda-last-dispatch-buffer (current-buffer))
  15199. (save-window-excursion
  15200. (delete-other-windows)
  15201. (switch-to-buffer-other-window " *Agenda Commands*")
  15202. (erase-buffer)
  15203. (insert (eval-when-compile
  15204. (let ((header
  15205. "Press key for an agenda command:
  15206. -------------------------------- C Configure custom agenda commands
  15207. a Agenda for current week or day e Export agenda views
  15208. t List of all TODO entries T Entries with special TODO kwd
  15209. m Match a TAGS query M Like m, but only TODO entries
  15210. L Timeline for current buffer # List stuck projects (!=configure)
  15211. ")
  15212. (start 0))
  15213. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  15214. (setq start (match-end 0))
  15215. (add-text-properties (match-beginning 2) (match-end 2)
  15216. '(face bold) header))
  15217. header)))
  15218. (while (setq entry (pop custom))
  15219. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  15220. (insert (format "\n%-4s%-14s: %s"
  15221. (org-add-props (copy-sequence key)
  15222. '(face bold))
  15223. (cond
  15224. ((stringp type) type)
  15225. ((eq type 'agenda) "Agenda for current week or day")
  15226. ((eq type 'alltodo) "List of all TODO entries")
  15227. ((eq type 'stuck) "List of stuck projects")
  15228. ((eq type 'todo) "TODO keyword")
  15229. ((eq type 'tags) "Tags query")
  15230. ((eq type 'tags-todo) "Tags (TODO)")
  15231. ((eq type 'tags-tree) "Tags tree")
  15232. ((eq type 'todo-tree) "TODO kwd tree")
  15233. ((eq type 'occur-tree) "Occur tree")
  15234. ((functionp type) (symbol-name type))
  15235. (t "???"))
  15236. (if (stringp match)
  15237. (org-add-props match nil 'face 'org-warning)
  15238. (format "set of %d commands" (length match))))))
  15239. (if restrict-ok
  15240. (insert "\n"
  15241. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  15242. (goto-char (point-min))
  15243. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  15244. (message "Press key for agenda command%s"
  15245. (if restrict-ok ", or [1] or [0] to restrict" ""))
  15246. (setq c (read-char-exclusive))
  15247. (message "")
  15248. (when (memq c '(?L ?1 ?0))
  15249. (if restrict-ok
  15250. (put 'org-agenda-files 'org-restrict (list bfn))
  15251. (error "Cannot restrict agenda to current buffer"))
  15252. (with-current-buffer " *Agenda Commands*"
  15253. (goto-char (point-max))
  15254. (delete-region (point-at-bol) (point))
  15255. (goto-char (point-min)))
  15256. (when (eq c ?0)
  15257. (setq org-agenda-restrict t)
  15258. (with-current-buffer buf
  15259. (if (org-region-active-p)
  15260. (progn
  15261. (move-marker org-agenda-restrict-begin (region-beginning))
  15262. (move-marker org-agenda-restrict-end (region-end)))
  15263. (save-excursion
  15264. (org-back-to-heading t)
  15265. (move-marker org-agenda-restrict-begin (point))
  15266. (move-marker org-agenda-restrict-end
  15267. (progn (org-end-of-subtree t)))))))
  15268. (unless (eq c ?L)
  15269. (message "Press key for agenda command%s"
  15270. (if restrict-ok " (restricted to current file)" ""))
  15271. (setq c (read-char-exclusive)))
  15272. (message "")))
  15273. (require 'calendar) ; FIXME: can we avoid this for some commands?
  15274. ;; For example the todo list should not need it (but does...)
  15275. (cond
  15276. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  15277. (if (symbolp (nth 1 entry))
  15278. (progn
  15279. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  15280. lprops (nth 3 entry))
  15281. (put 'org-agenda-redo-command 'org-lprops lprops)
  15282. (cond
  15283. ((eq type 'agenda)
  15284. (org-let lprops '(org-agenda-list current-prefix-arg)))
  15285. ((eq type 'alltodo)
  15286. (org-let lprops '(org-todo-list current-prefix-arg)))
  15287. ((eq type 'stuck)
  15288. (org-let lprops '(org-agenda-list-stuck-projects
  15289. current-prefix-arg)))
  15290. ((eq type 'tags)
  15291. (org-let lprops '(org-tags-view current-prefix-arg match)))
  15292. ((eq type 'tags-todo)
  15293. (org-let lprops '(org-tags-view '(4) match)))
  15294. ((eq type 'todo)
  15295. (org-let lprops '(org-todo-list match)))
  15296. ((eq type 'tags-tree)
  15297. (org-check-for-org-mode)
  15298. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  15299. ((eq type 'todo-tree)
  15300. (org-check-for-org-mode)
  15301. (org-let lprops
  15302. '(org-occur (concat "^" outline-regexp "[ \t]*"
  15303. (regexp-quote match) "\\>"))))
  15304. ((eq type 'occur-tree)
  15305. (org-check-for-org-mode)
  15306. (org-let lprops '(org-occur match)))
  15307. ((fboundp type)
  15308. (org-let lprops '(funcall type match)))
  15309. (t (error "Invalid custom agenda command type %s" type))))
  15310. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  15311. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  15312. ((equal c ?a) (call-interactively 'org-agenda-list))
  15313. ((equal c ?t) (call-interactively 'org-todo-list))
  15314. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  15315. ((equal c ?m) (call-interactively 'org-tags-view))
  15316. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  15317. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  15318. ((equal c ?L)
  15319. (unless restrict-ok
  15320. (error "This is not an Org-mode file"))
  15321. (org-call-with-arg 'org-timeline arg))
  15322. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  15323. ((equal c ?!) (customize-variable 'org-stuck-projects))
  15324. (t (error "Invalid key"))))))
  15325. (defun org-run-agenda-series (name series)
  15326. (org-prepare-agenda name)
  15327. (let* ((org-agenda-multi t)
  15328. (redo (list 'org-run-agenda-series name (list 'quote series)))
  15329. (cmds (car series))
  15330. (gprops (nth 1 series))
  15331. match ;; The byte compiler incorrectly complains about this. Keep it!
  15332. cmd type lprops)
  15333. (while (setq cmd (pop cmds))
  15334. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  15335. (cond
  15336. ((eq type 'agenda)
  15337. (org-let2 gprops lprops
  15338. '(call-interactively 'org-agenda-list)))
  15339. ((eq type 'alltodo)
  15340. (org-let2 gprops lprops
  15341. '(call-interactively 'org-todo-list)))
  15342. ((eq type 'stuck)
  15343. (org-let2 gprops lprops
  15344. '(call-interactively 'org-agenda-list-stuck-projects)))
  15345. ((eq type 'tags)
  15346. (org-let2 gprops lprops
  15347. '(org-tags-view current-prefix-arg match)))
  15348. ((eq type 'tags-todo)
  15349. (org-let2 gprops lprops
  15350. '(org-tags-view '(4) match)))
  15351. ((eq type 'todo)
  15352. (org-let2 gprops lprops
  15353. '(org-todo-list match)))
  15354. ((fboundp type)
  15355. (org-let2 gprops lprops
  15356. '(funcall type match)))
  15357. (t (error "Invalid type in command series"))))
  15358. (widen)
  15359. (setq org-agenda-redo-command redo)
  15360. (goto-char (point-min)))
  15361. (org-finalize-agenda))
  15362. ;;;###autoload
  15363. (defmacro org-batch-agenda (cmd-key &rest parameters)
  15364. "Run an agenda command in batch mode and send the result to STDOUT.
  15365. If CMD-KEY is a string of length 1, it is used as a key in
  15366. `org-agenda-custom-commands' and triggers this command. If it is a
  15367. longer string is is used as a tags/todo match string.
  15368. Paramters are alternating variable names and values that will be bound
  15369. before running the agenda command."
  15370. (let (pars)
  15371. (while parameters
  15372. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15373. (if (> (length cmd-key) 1)
  15374. (eval (list 'let (nreverse pars)
  15375. (list 'org-tags-view nil cmd-key)))
  15376. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15377. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15378. (set-buffer org-agenda-buffer-name)
  15379. (princ (org-encode-for-stdout (buffer-string)))))
  15380. (defun org-encode-for-stdout (string)
  15381. (if (fboundp 'encode-coding-string)
  15382. (encode-coding-string string buffer-file-coding-system)
  15383. string))
  15384. (defvar org-agenda-info nil)
  15385. ;;;###autoload
  15386. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  15387. "Run an agenda command in batch mode and send the result to STDOUT.
  15388. If CMD-KEY is a string of length 1, it is used as a key in
  15389. `org-agenda-custom-commands' and triggers this command. If it is a
  15390. longer string is is used as a tags/todo match string.
  15391. Paramters are alternating variable names and values that will be bound
  15392. before running the agenda command.
  15393. The output gives a line for each selected agenda item. Each
  15394. item is a list of comma-separated values, like this:
  15395. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  15396. category The category of the item
  15397. head The headline, without TODO kwd, TAGS and PRIORITY
  15398. type The type of the agenda entry, can be
  15399. todo selected in TODO match
  15400. tagsmatch selected in tags match
  15401. diary imported from diary
  15402. deadline a deadline on given date
  15403. scheduled scheduled on given date
  15404. timestamp entry has timestamp on given date
  15405. closed entry was closed on given date
  15406. upcoming-deadline warning about deadline
  15407. past-scheduled forwarded scheduled item
  15408. block entry has date block including g. date
  15409. todo The todo keyword, if any
  15410. tags All tags including inherited ones, separated by colons
  15411. date The relevant date, like 2007-2-14
  15412. time The time, like 15:00-16:50
  15413. extra Sting with extra planning info
  15414. priority-l The priority letter if any was given
  15415. priority-n The computed numerical priority
  15416. agenda-day The day in the agenda where this is listed"
  15417. (let (pars)
  15418. (while parameters
  15419. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15420. (push (list 'org-agenda-remove-tags t) pars)
  15421. (if (> (length cmd-key) 1)
  15422. (eval (list 'let (nreverse pars)
  15423. (list 'org-tags-view nil cmd-key)))
  15424. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  15425. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  15426. (set-buffer org-agenda-buffer-name)
  15427. (let* ((lines (org-split-string (buffer-string) "\n"))
  15428. line)
  15429. (while (setq line (pop lines))
  15430. (catch 'next
  15431. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  15432. (setq org-agenda-info
  15433. (org-fix-agenda-info (text-properties-at 0 line)))
  15434. (princ
  15435. (org-encode-for-stdout
  15436. (mapconcat 'org-agenda-export-csv-mapper
  15437. '(org-category txt type todo tags date time-of-day extra
  15438. priority-letter priority agenda-day)
  15439. ",")))
  15440. (princ "\n"))))))
  15441. (defun org-fix-agenda-info (props)
  15442. "FIXME"
  15443. (let (tmp re)
  15444. (when (setq tmp (plist-get props 'tags))
  15445. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  15446. (when (setq tmp (plist-get props 'date))
  15447. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15448. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15449. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  15450. (setq tmp (calendar-date-string tmp)))
  15451. (setq props (plist-put props 'date tmp)))
  15452. (when (setq tmp (plist-get props 'day))
  15453. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  15454. (let ((calendar-date-display-form '(year "-" month "-" day)))
  15455. (setq tmp (calendar-date-string tmp)))
  15456. (setq props (plist-put props 'day tmp))
  15457. (setq props (plist-put props 'agenda-day tmp)))
  15458. (when (setq tmp (plist-get props 'txt))
  15459. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  15460. (plist-put props 'priority-letter (match-string 1 tmp))
  15461. (setq tmp (replace-match "" t t tmp)))
  15462. (when (and (setq re (plist-get props 'org-todo-regexp))
  15463. (setq re (concat "\\`\\.*" re " ?"))
  15464. (string-match re tmp))
  15465. (plist-put props 'todo (match-string 1 tmp))
  15466. (setq tmp (replace-match "" t t tmp)))
  15467. (plist-put props 'txt tmp)))
  15468. props)
  15469. (defun org-agenda-export-csv-mapper (prop)
  15470. (let ((res (plist-get org-agenda-info prop)))
  15471. (setq res
  15472. (cond
  15473. ((not res) "")
  15474. ((stringp res) res)
  15475. (t (prin1-to-string res))))
  15476. (while (string-match "," res)
  15477. (setq res (replace-match ";" t t res)))
  15478. (org-trim res)))
  15479. ;;;###autoload
  15480. (defun org-store-agenda-views (&rest parameters)
  15481. (interactive)
  15482. (eval (list 'org-batch-store-agenda-views)))
  15483. ;; FIXME, why is this a macro?????
  15484. ;;;###autoload
  15485. (defmacro org-batch-store-agenda-views (&rest parameters)
  15486. "Run all custom agenda commands that have a file argument."
  15487. (let ((cmds org-agenda-custom-commands)
  15488. (dir default-directory)
  15489. pars cmd thiscmdkey files opts)
  15490. (while parameters
  15491. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  15492. (setq pars (reverse pars))
  15493. (save-window-excursion
  15494. (while cmds
  15495. (setq cmd (pop cmds)
  15496. thiscmdkey (car cmd)
  15497. opts (nth 3 cmd)
  15498. files (nth 4 cmd))
  15499. (if (stringp files) (setq files (list files)))
  15500. (when files
  15501. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  15502. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15503. '(org-agenda nil))))
  15504. (set-buffer org-agenda-buffer-name)
  15505. (while files
  15506. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  15507. (list 'org-write-agenda
  15508. (expand-file-name (pop files) dir) t))))
  15509. (and (get-buffer org-agenda-buffer-name)
  15510. (kill-buffer org-agenda-buffer-name)))))))
  15511. (defun org-write-agenda (file &optional nosettings)
  15512. "Write the current buffer (an agenda view) as a file.
  15513. Depending on the extension of the file name, plain text (.txt),
  15514. HTML (.html or .htm) or Postscript (.ps) is produced.
  15515. If NOSETTINGS is given, do not scope the settings of
  15516. `org-agenda-exporter-settings' into the export commands. This is used when
  15517. the settings have already been scoped and we do not wish to overrule other,
  15518. higher priority settings."
  15519. (interactive "FWrite agenda to file: ")
  15520. (if (not (file-writable-p file))
  15521. (error "Cannot write agenda to file %s" file))
  15522. (cond
  15523. ((string-match "\\.html?\\'" file) (require 'htmlize))
  15524. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  15525. (org-let (if nosettings nil org-agenda-exporter-settings)
  15526. '(save-excursion
  15527. (save-window-excursion
  15528. (cond
  15529. ((string-match "\\.html?\\'" file)
  15530. (set-buffer (htmlize-buffer (current-buffer)))
  15531. (when (and org-agenda-export-html-style
  15532. (string-match "<style>" org-agenda-export-html-style))
  15533. ;; replace <style> section with org-agenda-export-html-style
  15534. (goto-char (point-min))
  15535. (kill-region (- (search-forward "<style") 6)
  15536. (search-forward "</style>"))
  15537. (insert org-agenda-export-html-style))
  15538. (write-file file)
  15539. (kill-buffer (current-buffer))
  15540. (message "HTML written to %s" file))
  15541. ((string-match "\\.ps\\'" file)
  15542. (ps-print-buffer-with-faces file)
  15543. (message "Postscript written to %s" file))
  15544. (t
  15545. (let ((bs (buffer-string)))
  15546. (find-file file)
  15547. (insert bs)
  15548. (save-buffer 0)
  15549. (kill-buffer (current-buffer))
  15550. (message "Plain text written to %s" file))))))
  15551. (set-buffer org-agenda-buffer-name)))
  15552. (defmacro org-no-read-only (&rest body)
  15553. "Inhibit read-only for BODY."
  15554. `(let ((inhibit-read-only t)) ,@body))
  15555. (defun org-check-for-org-mode ()
  15556. "Make sure current buffer is in org-mode. Error if not."
  15557. (or (org-mode-p)
  15558. (error "Cannot execute org-mode agenda command on buffer in %s."
  15559. major-mode)))
  15560. (defun org-fit-agenda-window ()
  15561. "Fit the window to the buffer size."
  15562. (and (memq org-agenda-window-setup '(reorganize-frame))
  15563. (fboundp 'fit-window-to-buffer)
  15564. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  15565. (/ (frame-height) 2))))
  15566. ;;; Agenda file list
  15567. (defun org-agenda-files (&optional unrestricted)
  15568. "Get the list of agenda files.
  15569. Optional UNRESTRICTED means return the full list even if a restriction
  15570. is currently in place."
  15571. (cond
  15572. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15573. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15574. ((listp org-agenda-files) org-agenda-files)
  15575. (t (error "Invalid value of `org-agenda-files'"))))
  15576. (defun org-edit-agenda-file-list ()
  15577. "Edit the list of agenda files.
  15578. Depending on setup, this either uses customize to edit the variable
  15579. `org-agenda-files', or it visits the file that is holding the list. In the
  15580. latter case, the buffer is set up in a way that saving it automatically kills
  15581. the buffer and restores the previous window configuration."
  15582. (interactive)
  15583. (if (stringp org-agenda-files)
  15584. (let ((cw (current-window-configuration)))
  15585. (find-file org-agenda-files)
  15586. (org-set-local 'org-window-configuration cw)
  15587. (org-add-hook 'after-save-hook
  15588. (lambda ()
  15589. (set-window-configuration
  15590. (prog1 org-window-configuration
  15591. (kill-buffer (current-buffer))))
  15592. (org-install-agenda-files-menu)
  15593. (message "New agenda file list installed"))
  15594. nil 'local)
  15595. (message (substitute-command-keys
  15596. "Edit list and finish with \\[save-buffer]")))
  15597. (customize-variable 'org-agenda-files)))
  15598. (defun org-store-new-agenda-file-list (list)
  15599. "Set new value for the agenda file list and save it correcly."
  15600. (if (stringp org-agenda-files)
  15601. (let ((f org-agenda-files) b)
  15602. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  15603. (with-temp-file f
  15604. (insert (mapconcat 'identity list "\n") "\n")))
  15605. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  15606. (setq org-agenda-files list)
  15607. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15608. (defun org-read-agenda-file-list ()
  15609. "Read the list of agenda files from a file."
  15610. (when (stringp org-agenda-files)
  15611. (with-temp-buffer
  15612. (insert-file-contents org-agenda-files)
  15613. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  15614. ;;;###autoload
  15615. (defun org-cycle-agenda-files ()
  15616. "Cycle through the files in `org-agenda-files'.
  15617. If the current buffer visits an agenda file, find the next one in the list.
  15618. If the current buffer does not, find the first agenda file."
  15619. (interactive)
  15620. (let* ((fs (org-agenda-files t))
  15621. (files (append fs (list (car fs))))
  15622. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15623. file)
  15624. (unless files (error "No agenda files"))
  15625. (catch 'exit
  15626. (while (setq file (pop files))
  15627. (if (equal (file-truename file) tcf)
  15628. (when (car files)
  15629. (find-file (car files))
  15630. (throw 'exit t))))
  15631. (find-file (car fs)))
  15632. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  15633. (defun org-agenda-file-to-front (&optional to-end)
  15634. "Move/add the current file to the top of the agenda file list.
  15635. If the file is not present in the list, it is added to the front. If it is
  15636. present, it is moved there. With optional argument TO-END, add/move to the
  15637. end of the list."
  15638. (interactive "P")
  15639. (let ((file-alist (mapcar (lambda (x)
  15640. (cons (file-truename x) x))
  15641. (org-agenda-files t)))
  15642. (ctf (file-truename buffer-file-name))
  15643. x had)
  15644. (setq x (assoc ctf file-alist) had x)
  15645. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15646. (if to-end
  15647. (setq file-alist (append (delq x file-alist) (list x)))
  15648. (setq file-alist (cons x (delq x file-alist))))
  15649. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15650. (org-install-agenda-files-menu)
  15651. (message "File %s to %s of agenda file list"
  15652. (if had "moved" "added") (if to-end "end" "front"))))
  15653. (defun org-remove-file (&optional file)
  15654. "Remove current file from the list of files in variable `org-agenda-files'.
  15655. These are the files which are being checked for agenda entries.
  15656. Optional argument FILE means, use this file instead of the current."
  15657. (interactive)
  15658. (let* ((file (or file buffer-file-name))
  15659. (true-file (file-truename file))
  15660. (afile (abbreviate-file-name file))
  15661. (files (delq nil (mapcar
  15662. (lambda (x)
  15663. (if (equal true-file
  15664. (file-truename x))
  15665. nil x))
  15666. (org-agenda-files t)))))
  15667. (if (not (= (length files) (length (org-agenda-files t))))
  15668. (progn
  15669. (org-store-new-agenda-file-list files)
  15670. (org-install-agenda-files-menu)
  15671. (message "Removed file: %s" afile))
  15672. (message "File was not in list: %s" afile))))
  15673. (defun org-file-menu-entry (file)
  15674. (vector file (list 'find-file file) t))
  15675. (defun org-check-agenda-file (file)
  15676. "Make sure FILE exists. If not, ask user what to do."
  15677. (when (not (file-exists-p file))
  15678. (message "non-existent file %s. [R]emove from list or [A]bort?"
  15679. (abbreviate-file-name file))
  15680. (let ((r (downcase (read-char-exclusive))))
  15681. (cond
  15682. ((equal r ?r)
  15683. (org-remove-file file)
  15684. (throw 'nextfile t))
  15685. (t (error "Abort"))))))
  15686. ;;; Agenda prepare and finalize
  15687. (defvar org-agenda-multi nil) ; dynammically scoped
  15688. (defvar org-agenda-buffer-name "*Org Agenda*")
  15689. (defvar org-pre-agenda-window-conf nil)
  15690. (defvar org-agenda-name nil)
  15691. (defun org-prepare-agenda (&optional name)
  15692. (setq org-todo-keywords-for-agenda nil)
  15693. (setq org-done-keywords-for-agenda nil)
  15694. (if org-agenda-multi
  15695. (progn
  15696. (setq buffer-read-only nil)
  15697. (goto-char (point-max))
  15698. (unless (bobp)
  15699. (insert "\n" (make-string (window-width) ?=) "\n"))
  15700. (narrow-to-region (point) (point-max)))
  15701. (org-agenda-maybe-reset-markers 'force)
  15702. (org-prepare-agenda-buffers (org-agenda-files))
  15703. (setq org-todo-keywords-for-agenda
  15704. (org-uniquify org-todo-keywords-for-agenda))
  15705. (setq org-done-keywords-for-agenda
  15706. (org-uniquify org-done-keywords-for-agenda))
  15707. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  15708. (awin (get-buffer-window abuf)))
  15709. (cond
  15710. ((equal (current-buffer) abuf) nil)
  15711. (awin (select-window awin))
  15712. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  15713. ((equal org-agenda-window-setup 'current-window)
  15714. (switch-to-buffer abuf))
  15715. ((equal org-agenda-window-setup 'other-window)
  15716. (switch-to-buffer-other-window abuf))
  15717. ((equal org-agenda-window-setup 'other-frame)
  15718. (switch-to-buffer-other-frame abuf))
  15719. ((equal org-agenda-window-setup 'reorganize-frame)
  15720. (delete-other-windows)
  15721. (switch-to-buffer-other-window abuf))))
  15722. (setq buffer-read-only nil)
  15723. (erase-buffer)
  15724. (org-agenda-mode)
  15725. (and name (not org-agenda-name)
  15726. (org-set-local 'org-agenda-name name)))
  15727. (setq buffer-read-only nil))
  15728. (defun org-finalize-agenda ()
  15729. "Finishing touch for the agenda buffer, called just before displaying it."
  15730. (unless org-agenda-multi
  15731. (save-excursion
  15732. (let ((inhibit-read-only t))
  15733. (goto-char (point-min))
  15734. (while (org-activate-bracket-links (point-max))
  15735. (add-text-properties (match-beginning 0) (match-end 0)
  15736. '(face org-link)))
  15737. (org-agenda-align-tags)
  15738. (unless org-agenda-with-colors
  15739. (remove-text-properties (point-min) (point-max) '(face nil))))
  15740. (if (and (boundp 'org-overriding-columns-format)
  15741. org-overriding-columns-format)
  15742. (org-set-local 'org-overriding-columns-format
  15743. org-overriding-columns-format))
  15744. (if (and (boundp 'org-agenda-view-columns-initially)
  15745. org-agenda-view-columns-initially)
  15746. (org-agenda-columns))
  15747. (run-hooks 'org-finalize-agenda-hook))))
  15748. (defun org-prepare-agenda-buffers (files)
  15749. "Create buffers for all agenda files, protect archived trees and comments."
  15750. (interactive)
  15751. (let ((pa '(:org-archived t))
  15752. (pc '(:org-comment t))
  15753. (pall '(:org-archived t :org-comment t))
  15754. (inhibit-read-only t)
  15755. (rea (concat ":" org-archive-tag ":"))
  15756. bmp file re)
  15757. (save-excursion
  15758. (save-restriction
  15759. (while (setq file (pop files))
  15760. (org-check-agenda-file file)
  15761. (set-buffer (org-get-agenda-file-buffer file))
  15762. (widen)
  15763. (setq bmp (buffer-modified-p))
  15764. (setq org-todo-keywords-for-agenda
  15765. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15766. (setq org-done-keywords-for-agenda
  15767. (append org-done-keywords-for-agenda org-done-keywords))
  15768. (save-excursion
  15769. (remove-text-properties (point-min) (point-max) pall)
  15770. (when org-agenda-skip-archived-trees
  15771. (goto-char (point-min))
  15772. (while (re-search-forward rea nil t)
  15773. (if (org-on-heading-p t)
  15774. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15775. (goto-char (point-min))
  15776. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  15777. (while (re-search-forward re nil t)
  15778. (add-text-properties
  15779. (match-beginning 0) (org-end-of-subtree t) pc)))
  15780. (set-buffer-modified-p bmp))))))
  15781. (defvar org-agenda-skip-function nil
  15782. "Function to be called at each match during agenda construction.
  15783. If this function returns nil, the current match should not be skipped.
  15784. Otherwise, the function must return a position from where the search
  15785. should be continued.
  15786. This may also be a Lisp form, it will be evaluated.
  15787. Never set this variable using `setq' or so, because then it will apply
  15788. to all future agenda commands. Instead, bind it with `let' to scope
  15789. it dynamically into the agenda-constructing command. A good way to set
  15790. it is through options in org-agenda-custom-commands.")
  15791. (defun org-agenda-skip ()
  15792. "Throw to `:skip' in places that should be skipped.
  15793. Also moves point to the end of the skipped region, so that search can
  15794. continue from there."
  15795. (let ((p (point-at-bol)) to fp)
  15796. (and org-agenda-skip-archived-trees
  15797. (get-text-property p :org-archived)
  15798. (org-end-of-subtree t)
  15799. (throw :skip t))
  15800. (and (get-text-property p :org-comment)
  15801. (org-end-of-subtree t)
  15802. (throw :skip t))
  15803. (if (equal (char-after p) ?#) (throw :skip t))
  15804. (when (and (or (setq fp (functionp org-agenda-skip-function))
  15805. (consp org-agenda-skip-function))
  15806. (setq to (save-excursion
  15807. (save-match-data
  15808. (if fp
  15809. (funcall org-agenda-skip-function)
  15810. (eval org-agenda-skip-function))))))
  15811. (goto-char to)
  15812. (throw :skip t))))
  15813. (defvar org-agenda-markers nil
  15814. "List of all currently active markers created by `org-agenda'.")
  15815. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  15816. "Creation time of the last agenda marker.")
  15817. (defun org-agenda-new-marker (&optional pos)
  15818. "Return a new agenda marker.
  15819. Org-mode keeps a list of these markers and resets them when they are
  15820. no longer in use."
  15821. (let ((m (copy-marker (or pos (point)))))
  15822. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  15823. (push m org-agenda-markers)
  15824. m))
  15825. (defun org-agenda-maybe-reset-markers (&optional force)
  15826. "Reset markers created by `org-agenda'. But only if they are old enough."
  15827. (if (or (and force (not org-agenda-multi))
  15828. (> (- (time-to-seconds (current-time))
  15829. org-agenda-last-marker-time)
  15830. 5))
  15831. (while org-agenda-markers
  15832. (move-marker (pop org-agenda-markers) nil))))
  15833. (defvar org-agenda-new-buffers nil
  15834. "Buffers created to visit agenda files.")
  15835. (defun org-get-agenda-file-buffer (file)
  15836. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15837. it to the list of buffers which might be released later."
  15838. (let ((buf (org-find-base-buffer-visiting file)))
  15839. (if buf
  15840. buf ; just return it
  15841. ;; Make a new buffer and remember it
  15842. (setq buf (find-file-noselect file))
  15843. (if buf (push buf org-agenda-new-buffers))
  15844. buf)))
  15845. (defun org-release-buffers (blist)
  15846. "Release all buffers in list, asking the user for confirmation when needed.
  15847. When a buffer is unmodified, it is just killed. When modified, it is saved
  15848. \(if the user agrees) and then killed."
  15849. (let (buf file)
  15850. (while (setq buf (pop blist))
  15851. (setq file (buffer-file-name buf))
  15852. (when (and (buffer-modified-p buf)
  15853. file
  15854. (y-or-n-p (format "Save file %s? " file)))
  15855. (with-current-buffer buf (save-buffer)))
  15856. (kill-buffer buf))))
  15857. (defvar org-category-table nil)
  15858. (defun org-get-category-table ()
  15859. "Get the table of categories and positions in current buffer."
  15860. (let (tbl)
  15861. (save-excursion
  15862. (save-restriction
  15863. (widen)
  15864. (goto-char (point-min))
  15865. (while (re-search-forward "^#\\+CATEGORY:[ \t]*\\(.*\\)"
  15866. nil t)
  15867. (push (cons (match-beginning 1)
  15868. (org-trim (match-string 1))) tbl))))
  15869. tbl))
  15870. (defun org-get-category (&optional pos)
  15871. "Get the category applying to position POS."
  15872. (if (not org-category-table)
  15873. (cond
  15874. ((null org-category)
  15875. (setq org-category
  15876. (if buffer-file-name
  15877. (file-name-sans-extension
  15878. (file-name-nondirectory buffer-file-name))
  15879. "???")))
  15880. ((symbolp org-category) (symbol-name org-category))
  15881. (t org-category))
  15882. (let ((tbl org-category-table)
  15883. (pos (or pos (point))))
  15884. (while (and tbl (> (caar tbl) pos))
  15885. (pop tbl))
  15886. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  15887. org-category-table))))))
  15888. ;;; Agenda timeline
  15889. (defun org-timeline (&optional include-all)
  15890. "Show a time-sorted view of the entries in the current org file.
  15891. Only entries with a time stamp of today or later will be listed. With
  15892. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  15893. under the current date.
  15894. If the buffer contains an active region, only check the region for
  15895. dates."
  15896. (interactive "P")
  15897. (require 'calendar)
  15898. (org-compile-prefix-format 'timeline)
  15899. (org-set-sorting-strategy 'timeline)
  15900. (let* ((dopast t)
  15901. (dotodo include-all)
  15902. (doclosed org-agenda-show-log)
  15903. (entry buffer-file-name)
  15904. (date (calendar-current-date))
  15905. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  15906. (end (if (org-region-active-p) (region-end) (point-max)))
  15907. (day-numbers (org-get-all-dates beg end 'no-ranges
  15908. t doclosed ; always include today
  15909. org-timeline-show-empty-dates))
  15910. (today (time-to-days (current-time)))
  15911. (past t)
  15912. args
  15913. s e rtn d emptyp)
  15914. (setq org-agenda-redo-command
  15915. (list 'progn
  15916. (list 'switch-to-buffer-other-window (current-buffer))
  15917. (list 'org-timeline (list 'quote include-all))))
  15918. (if (not dopast)
  15919. ;; Remove past dates from the list of dates.
  15920. (setq day-numbers (delq nil (mapcar (lambda(x)
  15921. (if (>= x today) x nil))
  15922. day-numbers))))
  15923. (org-prepare-agenda (concat "Timeline "
  15924. (file-name-nondirectory buffer-file-name)))
  15925. (if doclosed (push :closed args))
  15926. (push :timestamp args)
  15927. (push :sexp args)
  15928. (if dotodo (push :todo args))
  15929. (while (setq d (pop day-numbers))
  15930. (if (and (listp d) (eq (car d) :omitted))
  15931. (progn
  15932. (setq s (point))
  15933. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  15934. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  15935. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  15936. (if (and (>= d today)
  15937. dopast
  15938. past)
  15939. (progn
  15940. (setq past nil)
  15941. (insert (make-string 79 ?-) "\n")))
  15942. (setq date (calendar-gregorian-from-absolute d))
  15943. (setq s (point))
  15944. (setq rtn (and (not emptyp)
  15945. (apply 'org-agenda-get-day-entries
  15946. entry date args)))
  15947. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  15948. (progn
  15949. (insert (calendar-day-name date) " "
  15950. (number-to-string (extract-calendar-day date)) " "
  15951. (calendar-month-name (extract-calendar-month date)) " "
  15952. (number-to-string (extract-calendar-year date)) "\n")
  15953. ; FIXME: this gives a timezone problem
  15954. ; (insert (format-time-string org-agenda-date-format
  15955. ; (calendar-time-from-absolute d 0))
  15956. ; "\n")
  15957. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  15958. (put-text-property s (1- (point)) 'org-date-line t)
  15959. (if (equal d today)
  15960. (put-text-property s (1- (point)) 'org-today t))
  15961. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  15962. (put-text-property s (1- (point)) 'day d)))))
  15963. (goto-char (point-min))
  15964. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  15965. (point-min)))
  15966. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  15967. (org-finalize-agenda)
  15968. (setq buffer-read-only t)))
  15969. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  15970. "Return a list of all relevant day numbers from BEG to END buffer positions.
  15971. If NO-RANGES is non-nil, include only the start and end dates of a range,
  15972. not every single day in the range. If FORCE-TODAY is non-nil, make
  15973. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  15974. inactive time stamps (those in square brackets) are included.
  15975. When EMPTY is non-nil, also include days without any entries."
  15976. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  15977. dates dates1 date day day1 day2 ts1 ts2)
  15978. (if force-today
  15979. (setq dates (list (time-to-days (current-time)))))
  15980. (save-excursion
  15981. (goto-char beg)
  15982. (while (re-search-forward re end t)
  15983. (setq day (time-to-days (org-time-string-to-time
  15984. (substring (match-string 1) 0 10))))
  15985. (or (memq day dates) (push day dates)))
  15986. (unless no-ranges
  15987. (goto-char beg)
  15988. (while (re-search-forward org-tr-regexp end t)
  15989. (setq ts1 (substring (match-string 1) 0 10)
  15990. ts2 (substring (match-string 2) 0 10)
  15991. day1 (time-to-days (org-time-string-to-time ts1))
  15992. day2 (time-to-days (org-time-string-to-time ts2)))
  15993. (while (< (setq day1 (1+ day1)) day2)
  15994. (or (memq day1 dates) (push day1 dates)))))
  15995. (setq dates (sort dates '<))
  15996. (when empty
  15997. (while (setq day (pop dates))
  15998. (setq day2 (car dates))
  15999. (push day dates1)
  16000. (when (and day2 empty)
  16001. (if (or (eq empty t)
  16002. (and (numberp empty) (<= (- day2 day) empty)))
  16003. (while (< (setq day (1+ day)) day2)
  16004. (push (list day) dates1))
  16005. (push (cons :omitted (- day2 day)) dates1))))
  16006. (setq dates (nreverse dates1)))
  16007. dates)))
  16008. ;;; Agenda Daily/Weekly
  16009. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  16010. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  16011. (defvar org-agenda-last-arguments nil
  16012. "The arguments of the previous call to org-agenda")
  16013. (defvar org-starting-day nil) ; local variable in the agenda buffer
  16014. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  16015. (defvar org-include-all-loc nil) ; local variable
  16016. ;;;###autoload
  16017. (defun org-agenda-list (&optional include-all start-day ndays)
  16018. "Produce a weekly view from all files in variable `org-agenda-files'.
  16019. The view will be for the current week, but from the overview buffer you
  16020. will be able to go to other weeks.
  16021. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  16022. also be shown, under the current date.
  16023. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  16024. on the days are also shown. See the variable `org-log-done' for how
  16025. to turn on logging.
  16026. START-DAY defaults to TODAY, or to the most recent match for the weekday
  16027. given in `org-agenda-start-on-weekday'.
  16028. NDAYS defaults to `org-agenda-ndays'."
  16029. (interactive "P")
  16030. (setq ndays (or ndays org-agenda-ndays)
  16031. start-day (or start-day org-agenda-start-day))
  16032. (if org-agenda-overriding-arguments
  16033. (setq include-all (car org-agenda-overriding-arguments)
  16034. start-day (nth 1 org-agenda-overriding-arguments)
  16035. ndays (nth 2 org-agenda-overriding-arguments)))
  16036. (if (stringp start-day)
  16037. ;; Convert to an absolute day number
  16038. (setq start-day (time-to-days (org-read-date nil t start-day))))
  16039. (setq org-agenda-last-arguments (list include-all start-day ndays))
  16040. (org-compile-prefix-format 'agenda)
  16041. (org-set-sorting-strategy 'agenda)
  16042. (require 'calendar)
  16043. (let* ((org-agenda-start-on-weekday
  16044. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  16045. org-agenda-start-on-weekday nil))
  16046. (thefiles (org-agenda-files))
  16047. (files thefiles)
  16048. (today (time-to-days (current-time)))
  16049. (sd (or start-day today))
  16050. (start (if (or (null org-agenda-start-on-weekday)
  16051. (< org-agenda-ndays 7))
  16052. sd
  16053. (let* ((nt (calendar-day-of-week
  16054. (calendar-gregorian-from-absolute sd)))
  16055. (n1 org-agenda-start-on-weekday)
  16056. (d (- nt n1)))
  16057. (- sd (+ (if (< d 0) 7 0) d)))))
  16058. (day-numbers (list start))
  16059. (inhibit-redisplay (not debug-on-error))
  16060. s e rtn rtnall file date d start-pos end-pos todayp nd)
  16061. (setq org-agenda-redo-command
  16062. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  16063. ;; Make the list of days
  16064. (setq ndays (or ndays org-agenda-ndays)
  16065. nd ndays)
  16066. (while (> ndays 1)
  16067. (push (1+ (car day-numbers)) day-numbers)
  16068. (setq ndays (1- ndays)))
  16069. (setq day-numbers (nreverse day-numbers))
  16070. (org-prepare-agenda "Day/Week")
  16071. (org-set-local 'org-starting-day (car day-numbers))
  16072. (org-set-local 'org-include-all-loc include-all)
  16073. (org-set-local 'org-agenda-span
  16074. (org-agenda-ndays-to-span nd))
  16075. (when (and (or include-all org-agenda-include-all-todo)
  16076. (member today day-numbers))
  16077. (setq files thefiles
  16078. rtnall nil)
  16079. (while (setq file (pop files))
  16080. (catch 'nextfile
  16081. (org-check-agenda-file file)
  16082. (setq date (calendar-gregorian-from-absolute today)
  16083. rtn (org-agenda-get-day-entries
  16084. file date :todo))
  16085. (setq rtnall (append rtnall rtn))))
  16086. (when rtnall
  16087. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  16088. (add-text-properties (point-min) (1- (point))
  16089. (list 'face 'org-agenda-structure))
  16090. (insert (org-finalize-agenda-entries rtnall) "\n")))
  16091. (setq s (point))
  16092. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  16093. "-agenda:\n")
  16094. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  16095. 'org-date-line t))
  16096. (while (setq d (pop day-numbers))
  16097. (setq date (calendar-gregorian-from-absolute d)
  16098. s (point))
  16099. (if (or (setq todayp (= d today))
  16100. (and (not start-pos) (= d sd)))
  16101. (setq start-pos (point))
  16102. (if (and start-pos (not end-pos))
  16103. (setq end-pos (point))))
  16104. (setq files thefiles
  16105. rtnall nil)
  16106. (while (setq file (pop files))
  16107. (catch 'nextfile
  16108. (org-check-agenda-file file)
  16109. (if org-agenda-show-log
  16110. (setq rtn (org-agenda-get-day-entries
  16111. file date
  16112. :deadline :scheduled :timestamp :sexp :closed))
  16113. (setq rtn (org-agenda-get-day-entries
  16114. file date
  16115. :deadline :scheduled :sexp :timestamp)))
  16116. (setq rtnall (append rtnall rtn))))
  16117. (if org-agenda-include-diary
  16118. (progn
  16119. (require 'diary-lib)
  16120. (setq rtn (org-get-entries-from-diary date))
  16121. (setq rtnall (append rtnall rtn))))
  16122. (if (or rtnall org-agenda-show-all-dates)
  16123. (progn
  16124. (insert (format "%-9s %2d %s %4d\n"
  16125. (calendar-day-name date)
  16126. (extract-calendar-day date)
  16127. (calendar-month-name (extract-calendar-month date))
  16128. (extract-calendar-year date)))
  16129. ; FIXME: this gives a timezone problem
  16130. ; (insert (format-time-string org-agenda-date-format
  16131. ; (calendar-time-from-absolute d 0)) "\n")
  16132. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  16133. (put-text-property s (1- (point)) 'org-date-line t)
  16134. (if todayp (put-text-property s (1- (point)) 'org-today t))
  16135. (if rtnall (insert
  16136. (org-finalize-agenda-entries
  16137. (org-agenda-add-time-grid-maybe
  16138. rtnall nd todayp))
  16139. "\n"))
  16140. (put-text-property s (1- (point)) 'day d))))
  16141. (goto-char (point-min))
  16142. (org-fit-agenda-window)
  16143. (unless (and (pos-visible-in-window-p (point-min))
  16144. (pos-visible-in-window-p (point-max)))
  16145. (goto-char (1- (point-max)))
  16146. (recenter -1)
  16147. (if (not (pos-visible-in-window-p (or start-pos 1)))
  16148. (progn
  16149. (goto-char (or start-pos 1))
  16150. (recenter 1))))
  16151. (goto-char (or start-pos 1))
  16152. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  16153. (org-finalize-agenda)
  16154. (setq buffer-read-only t)
  16155. (message "")))
  16156. (defun org-agenda-ndays-to-span (n)
  16157. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  16158. ;;; Agenda TODO list
  16159. (defvar org-select-this-todo-keyword nil)
  16160. (defvar org-last-arg nil)
  16161. ;;;###autoload
  16162. (defun org-todo-list (arg)
  16163. "Show all TODO entries from all agenda file in a single list.
  16164. The prefix arg can be used to select a specific TODO keyword and limit
  16165. the list to these. When using \\[universal-argument], you will be prompted
  16166. for a keyword. A numeric prefix directly selects the Nth keyword in
  16167. `org-todo-keywords-1'."
  16168. (interactive "P")
  16169. (require 'calendar)
  16170. (org-compile-prefix-format 'todo)
  16171. (org-set-sorting-strategy 'todo)
  16172. (org-prepare-agenda "TODO")
  16173. (let* ((today (time-to-days (current-time)))
  16174. (date (calendar-gregorian-from-absolute today))
  16175. (kwds org-todo-keywords-for-agenda)
  16176. (completion-ignore-case t)
  16177. (org-select-this-todo-keyword
  16178. (if (stringp arg) arg
  16179. (and arg (integerp arg) (> arg 0)
  16180. (nth (1- arg) kwds))))
  16181. rtn rtnall files file pos)
  16182. (when (equal arg '(4))
  16183. (setq org-select-this-todo-keyword
  16184. (completing-read "Keyword (or KWD1|K2D2|...): "
  16185. (mapcar 'list kwds) nil nil)))
  16186. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  16187. (org-set-local 'org-last-arg arg)
  16188. (setq org-agenda-redo-command
  16189. '(org-todo-list (or current-prefix-arg org-last-arg)))
  16190. (setq files (org-agenda-files)
  16191. rtnall nil)
  16192. (while (setq file (pop files))
  16193. (catch 'nextfile
  16194. (org-check-agenda-file file)
  16195. (setq rtn (org-agenda-get-day-entries file date :todo))
  16196. (setq rtnall (append rtnall rtn))))
  16197. (if org-agenda-overriding-header
  16198. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16199. nil 'face 'org-agenda-structure) "\n")
  16200. (insert "Global list of TODO items of type: ")
  16201. (add-text-properties (point-min) (1- (point))
  16202. (list 'face 'org-agenda-structure))
  16203. (setq pos (point))
  16204. (insert (or org-select-this-todo-keyword "ALL") "\n")
  16205. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16206. (setq pos (point))
  16207. (unless org-agenda-multi
  16208. (insert "Available with `N r': (0)ALL")
  16209. (let ((n 0) s)
  16210. (mapc (lambda (x)
  16211. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  16212. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  16213. (insert "\n "))
  16214. (insert " " s))
  16215. kwds))
  16216. (insert "\n"))
  16217. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16218. (when rtnall
  16219. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16220. (goto-char (point-min))
  16221. (org-fit-agenda-window)
  16222. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  16223. (org-finalize-agenda)
  16224. (setq buffer-read-only t)))
  16225. ;;; Agenda tags match
  16226. ;;;###autoload
  16227. (defun org-tags-view (&optional todo-only match)
  16228. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  16229. The prefix arg TODO-ONLY limits the search to TODO entries."
  16230. (interactive "P")
  16231. (org-compile-prefix-format 'tags)
  16232. (org-set-sorting-strategy 'tags)
  16233. (let* ((org-tags-match-list-sublevels
  16234. (if todo-only t org-tags-match-list-sublevels))
  16235. (completion-ignore-case t)
  16236. rtn rtnall files file pos matcher
  16237. buffer)
  16238. (setq matcher (org-make-tags-matcher match)
  16239. match (car matcher) matcher (cdr matcher))
  16240. (org-prepare-agenda (concat "TAGS " match))
  16241. (setq org-agenda-redo-command
  16242. (list 'org-tags-view (list 'quote todo-only)
  16243. (list 'if 'current-prefix-arg nil match)))
  16244. (setq files (org-agenda-files)
  16245. rtnall nil)
  16246. (while (setq file (pop files))
  16247. (catch 'nextfile
  16248. (org-check-agenda-file file)
  16249. (setq buffer (if (file-exists-p file)
  16250. (org-get-agenda-file-buffer file)
  16251. (error "No such file %s" file)))
  16252. (if (not buffer)
  16253. ;; If file does not exist, merror message to agenda
  16254. (setq rtn (list
  16255. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16256. rtnall (append rtnall rtn))
  16257. (with-current-buffer buffer
  16258. (unless (org-mode-p)
  16259. (error "Agenda file %s is not in `org-mode'" file))
  16260. (setq org-category-table (org-get-category-table))
  16261. (save-excursion
  16262. (save-restriction
  16263. (if org-agenda-restrict
  16264. (narrow-to-region org-agenda-restrict-begin
  16265. org-agenda-restrict-end)
  16266. (widen))
  16267. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  16268. (setq rtnall (append rtnall rtn))))))))
  16269. (if org-agenda-overriding-header
  16270. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  16271. nil 'face 'org-agenda-structure) "\n")
  16272. (insert "Headlines with TAGS match: ")
  16273. (add-text-properties (point-min) (1- (point))
  16274. (list 'face 'org-agenda-structure))
  16275. (setq pos (point))
  16276. (insert match "\n")
  16277. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  16278. (setq pos (point))
  16279. (unless org-agenda-multi
  16280. (insert "Press `C-u r' to search again with new search string\n"))
  16281. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  16282. (when rtnall
  16283. (insert (org-finalize-agenda-entries rtnall) "\n"))
  16284. (goto-char (point-min))
  16285. (org-fit-agenda-window)
  16286. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  16287. (org-finalize-agenda)
  16288. (setq buffer-read-only t)))
  16289. ;;; Agenda Finding stuck projects
  16290. (defvar org-agenda-skip-regexp nil
  16291. "Regular expression used in skipping subtrees for the agenda.
  16292. This is basically a temporary global variable that can be set and then
  16293. used by user-defined selections using `org-agenda-skip-function'.")
  16294. (defvar org-agenda-overriding-header nil
  16295. "When this is set during todo and tags searches, will replace header.")
  16296. (defun org-agenda-skip-subtree-when-regexp-matches ()
  16297. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  16298. If yes, it returns the end position of this tree, causing agenda commands
  16299. to skip this subtree. This is a function that can be put into
  16300. `org-agenda-skip-function' for the duration of a command."
  16301. (let ((end (save-excursion (org-end-of-subtree t)))
  16302. skip)
  16303. (save-excursion
  16304. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  16305. (and skip end)))
  16306. (defun org-agenda-skip-entry-if (&rest conditions)
  16307. "Skip entry is any of CONDITIONS is true.
  16308. See `org-agenda-skip-if for details."
  16309. (org-agenda-skip-if nil conditions))
  16310. (defun org-agenda-skip-subtree-if (&rest conditions)
  16311. "Skip entry is any of CONDITIONS is true.
  16312. See `org-agenda-skip-if for details."
  16313. (org-agenda-skip-if t conditions))
  16314. (defun org-agenda-skip-if (subtree conditions)
  16315. "Checks current entity for CONDITIONS.
  16316. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  16317. the entry, i.e. the text before the next heading is checked.
  16318. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  16319. from different tests. Valid conditions are:
  16320. scheduled Check if there is a scheduled cookie
  16321. notscheduled Check if there is no scheduled cookie
  16322. deadline Check if there is a deadline
  16323. notdeadline Check if there is no deadline
  16324. regexp Check if regexp matches
  16325. notregexp Check if regexp does not match.
  16326. The regexp is taken from the conditions list, it must com right after the
  16327. `regexp' of `notregexp' element.
  16328. If any of these conditions is met, this function returns the end point of
  16329. the entity, causing the search to continue from there. This is a function
  16330. that can be put into `org-agenda-skip-function' for the duration of a command."
  16331. (let (beg end m r)
  16332. (org-back-to-heading t)
  16333. (setq beg (point)
  16334. end (if subtree
  16335. (progn (org-end-of-subtree t) (point))
  16336. (progn (outline-next-heading) (1- (point)))))
  16337. (goto-char beg)
  16338. (and
  16339. (or
  16340. (and (memq 'scheduled conditions)
  16341. (re-search-forward org-scheduled-time-regexp end t))
  16342. (and (memq 'notscheduled conditions)
  16343. (not (re-search-forward org-scheduled-time-regexp end t)))
  16344. (and (memq 'deadline conditions)
  16345. (re-search-forward org-deadline-time-regexp end t))
  16346. (and (memq 'notdeadline conditions)
  16347. (not (re-search-forward org-deadline-time-regexp end t)))
  16348. (and (setq m (memq 'regexp conditions))
  16349. (stringp (setq r (nth 1 m)))
  16350. (re-search-forward (nth 1 m) end t))
  16351. (and (setq m (memq 'notregexp conditions))
  16352. (stringp (setq r (nth 1 m)))
  16353. (not (re-search-forward (nth 1 m) end t))))
  16354. end)))
  16355. (defun org-agenda-list-stuck-projects (&rest ignore)
  16356. "Create agenda view for projects that are stuck.
  16357. Stuck projects are project that have no next actions. For the definitions
  16358. of what a project is and how to check if it stuck, customize the variable
  16359. `org-stuck-projects'.
  16360. MATCH is being ignored."
  16361. (interactive)
  16362. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  16363. ;; FIXME: we could have used org-agenda-skip-if here.
  16364. (org-agenda-overriding-header "List of stuck projects: ")
  16365. (matcher (nth 0 org-stuck-projects))
  16366. (todo (nth 1 org-stuck-projects))
  16367. (todo-wds (if (member "*" todo)
  16368. (progn
  16369. (org-prepare-agenda-buffers (org-agenda-files))
  16370. (org-delete-all
  16371. org-done-keywords-for-agenda
  16372. (copy-sequence org-todo-keywords-for-agenda)))
  16373. todo))
  16374. (todo-re (concat "^\\*+[ \t]+\\("
  16375. (mapconcat 'identity todo-wds "\\|")
  16376. "\\)\\>"))
  16377. (tags (nth 2 org-stuck-projects))
  16378. (tags-re (if (member "*" tags)
  16379. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  16380. (concat "^\\*+ .*:\\("
  16381. (mapconcat 'identity tags "\\|")
  16382. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  16383. (gen-re (nth 3 org-stuck-projects))
  16384. (re-list
  16385. (delq nil
  16386. (list
  16387. (if todo todo-re)
  16388. (if tags tags-re)
  16389. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  16390. gen-re)))))
  16391. (setq org-agenda-skip-regexp
  16392. (if re-list
  16393. (mapconcat 'identity re-list "\\|")
  16394. (error "No information how to identify unstuck projects")))
  16395. (org-tags-view nil matcher)
  16396. (with-current-buffer org-agenda-buffer-name
  16397. (setq org-agenda-redo-command
  16398. '(org-agenda-list-stuck-projects
  16399. (or current-prefix-arg org-last-arg))))))
  16400. ;;; Diary integration
  16401. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  16402. (defun org-get-entries-from-diary (date)
  16403. "Get the (Emacs Calendar) diary entries for DATE."
  16404. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  16405. (diary-display-hook '(fancy-diary-display))
  16406. (list-diary-entries-hook
  16407. (cons 'org-diary-default-entry list-diary-entries-hook))
  16408. (diary-file-name-prefix-function nil) ; turn this feature off
  16409. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  16410. entries
  16411. (org-disable-agenda-to-diary t))
  16412. (save-excursion
  16413. (save-window-excursion
  16414. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  16415. (if (not (get-buffer fancy-diary-buffer))
  16416. (setq entries nil)
  16417. (with-current-buffer fancy-diary-buffer
  16418. (setq buffer-read-only nil)
  16419. (if (zerop (buffer-size))
  16420. ;; No entries
  16421. (setq entries nil)
  16422. ;; Omit the date and other unnecessary stuff
  16423. (org-agenda-cleanup-fancy-diary)
  16424. ;; Add prefix to each line and extend the text properties
  16425. (if (zerop (buffer-size))
  16426. (setq entries nil)
  16427. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  16428. (set-buffer-modified-p nil)
  16429. (kill-buffer fancy-diary-buffer)))
  16430. (when entries
  16431. (setq entries (org-split-string entries "\n"))
  16432. (setq entries
  16433. (mapcar
  16434. (lambda (x)
  16435. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  16436. ;; Extend the text properties to the beginning of the line
  16437. (org-add-props x (text-properties-at (1- (length x)) x)
  16438. 'type "diary" 'date date))
  16439. entries)))))
  16440. (defun org-agenda-cleanup-fancy-diary ()
  16441. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  16442. This gets rid of the date, the underline under the date, and
  16443. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  16444. date. It also removes lines that contain only whitespace."
  16445. (goto-char (point-min))
  16446. (if (looking-at ".*?:[ \t]*")
  16447. (progn
  16448. (replace-match "")
  16449. (re-search-forward "\n=+$" nil t)
  16450. (replace-match "")
  16451. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  16452. (re-search-forward "\n=+$" nil t)
  16453. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  16454. (goto-char (point-min))
  16455. (while (re-search-forward "^ +\n" nil t)
  16456. (replace-match ""))
  16457. (goto-char (point-min))
  16458. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  16459. (replace-match "")))
  16460. ;; Make sure entries from the diary have the right text properties.
  16461. (eval-after-load "diary-lib"
  16462. '(if (boundp 'diary-modify-entry-list-string-function)
  16463. ;; We can rely on the hook, nothing to do
  16464. nil
  16465. ;; Hook not avaiable, must use advice to make this work
  16466. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  16467. "Make the position visible."
  16468. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  16469. (stringp string)
  16470. buffer-file-name)
  16471. (setq string (org-modify-diary-entry-string string))))))
  16472. (defun org-modify-diary-entry-string (string)
  16473. "Add text properties to string, allowing org-mode to act on it."
  16474. (org-add-props string nil
  16475. 'mouse-face 'highlight
  16476. 'keymap org-agenda-keymap
  16477. 'help-echo (if buffer-file-name
  16478. (format "mouse-2 or RET jump to diary file %s"
  16479. (abbreviate-file-name buffer-file-name))
  16480. "")
  16481. 'org-agenda-diary-link t
  16482. 'org-marker (org-agenda-new-marker (point-at-bol))))
  16483. (defun org-diary-default-entry ()
  16484. "Add a dummy entry to the diary.
  16485. Needed to avoid empty dates which mess up holiday display."
  16486. ;; Catch the error if dealing with the new add-to-diary-alist
  16487. (when org-disable-agenda-to-diary
  16488. (condition-case nil
  16489. (add-to-diary-list original-date "Org-mode dummy" "")
  16490. (error
  16491. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  16492. ;;;###autoload
  16493. (defun org-diary (&rest args)
  16494. "Return diary information from org-files.
  16495. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  16496. It accesses org files and extracts information from those files to be
  16497. listed in the diary. The function accepts arguments specifying what
  16498. items should be listed. The following arguments are allowed:
  16499. :timestamp List the headlines of items containing a date stamp or
  16500. date range matching the selected date. Deadlines will
  16501. also be listed, on the expiration day.
  16502. :sexp FIXME
  16503. :deadline List any deadlines past due, or due within
  16504. `org-deadline-warning-days'. The listing occurs only
  16505. in the diary for *today*, not at any other date. If
  16506. an entry is marked DONE, it is no longer listed.
  16507. :scheduled List all items which are scheduled for the given date.
  16508. The diary for *today* also contains items which were
  16509. scheduled earlier and are not yet marked DONE.
  16510. :todo List all TODO items from the org-file. This may be a
  16511. long list - so this is not turned on by default.
  16512. Like deadlines, these entries only show up in the
  16513. diary for *today*, not at any other date.
  16514. The call in the diary file should look like this:
  16515. &%%(org-diary) ~/path/to/some/orgfile.org
  16516. Use a separate line for each org file to check. Or, if you omit the file name,
  16517. all files listed in `org-agenda-files' will be checked automatically:
  16518. &%%(org-diary)
  16519. If you don't give any arguments (as in the example above), the default
  16520. arguments (:deadline :scheduled :timestamp :sexp) are used.
  16521. So the example above may also be written as
  16522. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  16523. The function expects the lisp variables `entry' and `date' to be provided
  16524. by the caller, because this is how the calendar works. Don't use this
  16525. function from a program - use `org-agenda-get-day-entries' instead."
  16526. (org-agenda-maybe-reset-markers)
  16527. (org-compile-prefix-format 'agenda)
  16528. (org-set-sorting-strategy 'agenda)
  16529. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16530. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  16531. (list entry)
  16532. (org-agenda-files t)))
  16533. file rtn results)
  16534. (org-prepare-agenda-buffers files)
  16535. ;; If this is called during org-agenda, don't return any entries to
  16536. ;; the calendar. Org Agenda will list these entries itself.
  16537. (if org-disable-agenda-to-diary (setq files nil))
  16538. (while (setq file (pop files))
  16539. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  16540. (setq results (append results rtn)))
  16541. (if results
  16542. (concat (org-finalize-agenda-entries results) "\n"))))
  16543. ;;; Agenda entry finders
  16544. (defun org-agenda-get-day-entries (file date &rest args)
  16545. "Does the work for `org-diary' and `org-agenda'.
  16546. FILE is the path to a file to be checked for entries. DATE is date like
  16547. the one returned by `calendar-current-date'. ARGS are symbols indicating
  16548. which kind of entries should be extracted. For details about these, see
  16549. the documentation of `org-diary'."
  16550. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  16551. (let* ((org-startup-folded nil)
  16552. (org-startup-align-all-tables nil)
  16553. (buffer (if (file-exists-p file)
  16554. (org-get-agenda-file-buffer file)
  16555. (error "No such file %s" file)))
  16556. arg results rtn)
  16557. (if (not buffer)
  16558. ;; If file does not exist, make sure an error message ends up in diary
  16559. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  16560. (with-current-buffer buffer
  16561. (unless (org-mode-p)
  16562. (error "Agenda file %s is not in `org-mode'" file))
  16563. (setq org-category-table (org-get-category-table))
  16564. (let ((case-fold-search nil))
  16565. (save-excursion
  16566. (save-restriction
  16567. (if org-agenda-restrict
  16568. (narrow-to-region org-agenda-restrict-begin
  16569. org-agenda-restrict-end)
  16570. (widen))
  16571. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  16572. (while (setq arg (pop args))
  16573. (cond
  16574. ((and (eq arg :todo)
  16575. (equal date (calendar-current-date)))
  16576. (setq rtn (org-agenda-get-todos))
  16577. (setq results (append results rtn)))
  16578. ((eq arg :timestamp)
  16579. (setq rtn (org-agenda-get-blocks))
  16580. (setq results (append results rtn))
  16581. (setq rtn (org-agenda-get-timestamps))
  16582. (setq results (append results rtn)))
  16583. ((eq arg :sexp)
  16584. (setq rtn (org-agenda-get-sexps))
  16585. (setq results (append results rtn)))
  16586. ((eq arg :scheduled)
  16587. (setq rtn (org-agenda-get-scheduled))
  16588. (setq results (append results rtn)))
  16589. ((eq arg :closed)
  16590. (setq rtn (org-agenda-get-closed))
  16591. (setq results (append results rtn)))
  16592. ((eq arg :deadline)
  16593. (setq rtn (org-agenda-get-deadlines))
  16594. (setq results (append results rtn))))))))
  16595. results))))
  16596. ;; FIXME: this works only if the cursor is not at the
  16597. ;; beginning of the entry
  16598. (defun org-entry-is-done-p ()
  16599. "Is the current entry marked DONE?"
  16600. (save-excursion
  16601. (and (re-search-backward "[\r\n]\\*+ " nil t)
  16602. (looking-at org-nl-done-regexp))))
  16603. (defun org-at-date-range-p (&optional inactive-ok)
  16604. "Is the cursor inside a date range?"
  16605. (interactive)
  16606. (save-excursion
  16607. (catch 'exit
  16608. (let ((pos (point)))
  16609. (skip-chars-backward "^[<\r\n")
  16610. (skip-chars-backward "<[")
  16611. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16612. (>= (match-end 0) pos)
  16613. (throw 'exit t))
  16614. (skip-chars-backward "^<[\r\n")
  16615. (skip-chars-backward "<[")
  16616. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  16617. (>= (match-end 0) pos)
  16618. (throw 'exit t)))
  16619. nil)))
  16620. (defun org-agenda-get-todos ()
  16621. "Return the TODO information for agenda display."
  16622. (let* ((props (list 'face nil
  16623. 'done-face 'org-done
  16624. 'org-not-done-regexp org-not-done-regexp
  16625. 'org-todo-regexp org-todo-regexp
  16626. 'mouse-face 'highlight
  16627. 'keymap org-agenda-keymap
  16628. 'help-echo
  16629. (format "mouse-2 or RET jump to org file %s"
  16630. (abbreviate-file-name buffer-file-name))))
  16631. ;; FIXME: get rid of the \n at some point but watch out
  16632. (regexp (concat "^\\*+[ \t]+\\("
  16633. (if org-select-this-todo-keyword
  16634. (if (equal org-select-this-todo-keyword "*")
  16635. org-todo-regexp
  16636. (concat "\\<\\("
  16637. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  16638. "\\)\\>"))
  16639. org-not-done-regexp)
  16640. "[^\n\r]*\\)"))
  16641. marker priority category tags
  16642. ee txt beg end)
  16643. (goto-char (point-min))
  16644. (while (re-search-forward regexp nil t)
  16645. (catch :skip
  16646. (save-match-data
  16647. (beginning-of-line)
  16648. (setq beg (point) end (progn (outline-next-heading) (point)))
  16649. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  16650. (re-search-forward org-scheduled-time-regexp end t))
  16651. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  16652. (re-search-forward org-deadline-time-regexp end t)
  16653. (org-deadline-close (match-string 1))))
  16654. (goto-char beg)
  16655. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  16656. (throw :skip nil)))
  16657. (goto-char beg)
  16658. (org-agenda-skip)
  16659. (goto-char (match-beginning 1))
  16660. (setq marker (org-agenda-new-marker (match-beginning 0))
  16661. category (org-get-category)
  16662. tags (org-get-tags-at (point))
  16663. txt (org-format-agenda-item "" (match-string 1) category tags)
  16664. priority (1+ (org-get-priority txt)))
  16665. (org-add-props txt props
  16666. 'org-marker marker 'org-hd-marker marker
  16667. 'priority priority 'org-category category
  16668. 'type "todo")
  16669. (push txt ee)
  16670. (if org-agenda-todo-list-sublevels
  16671. (goto-char (match-end 1))
  16672. (org-end-of-subtree 'invisible))))
  16673. (nreverse ee)))
  16674. (defconst org-agenda-no-heading-message
  16675. "No heading for this item in buffer or region.")
  16676. (defun org-agenda-get-timestamps ()
  16677. "Return the date stamp information for agenda display."
  16678. (let* ((props (list 'face nil
  16679. 'org-not-done-regexp org-not-done-regexp
  16680. 'org-todo-regexp org-todo-regexp
  16681. 'mouse-face 'highlight
  16682. 'keymap org-agenda-keymap
  16683. 'help-echo
  16684. (format "mouse-2 or RET jump to org file %s"
  16685. (abbreviate-file-name buffer-file-name))))
  16686. (d1 (calendar-absolute-from-gregorian date))
  16687. (regexp
  16688. (concat
  16689. (regexp-quote
  16690. (substring
  16691. (format-time-string
  16692. (car org-time-stamp-formats)
  16693. (apply 'encode-time ; DATE bound by calendar
  16694. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16695. 0 11))
  16696. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  16697. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  16698. marker hdmarker deadlinep scheduledp donep tmp priority category
  16699. ee txt timestr tags b0 b3 e3)
  16700. (goto-char (point-min))
  16701. (while (re-search-forward regexp nil t)
  16702. (setq b0 (match-beginning 0)
  16703. b3 (match-beginning 3) e3 (match-end 3))
  16704. (catch :skip
  16705. (and (org-at-date-range-p) (throw :skip nil))
  16706. (org-agenda-skip)
  16707. (if (and (match-end 1)
  16708. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  16709. (throw :skip nil))
  16710. (if (and e3
  16711. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  16712. (throw :skip nil))
  16713. (setq marker (org-agenda-new-marker b0)
  16714. category (org-get-category b0)
  16715. tmp (buffer-substring (max (point-min)
  16716. (- b0 org-ds-keyword-length))
  16717. b0)
  16718. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  16719. deadlinep (string-match org-deadline-regexp tmp)
  16720. scheduledp (string-match org-scheduled-regexp tmp)
  16721. donep (org-entry-is-done-p))
  16722. (if (or scheduledp deadlinep) (throw :skip t))
  16723. (if (string-match ">" timestr)
  16724. ;; substring should only run to end of time stamp
  16725. (setq timestr (substring timestr 0 (match-end 0))))
  16726. (save-excursion
  16727. (if (re-search-backward "^\\*+ " nil t)
  16728. (progn
  16729. (goto-char (match-beginning 0))
  16730. (setq hdmarker (org-agenda-new-marker)
  16731. tags (org-get-tags-at))
  16732. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16733. (setq txt (org-format-agenda-item
  16734. nil (match-string 1) category tags timestr)))
  16735. (setq txt org-agenda-no-heading-message))
  16736. (setq priority (org-get-priority txt))
  16737. (org-add-props txt props
  16738. 'org-marker marker 'org-hd-marker hdmarker)
  16739. (org-add-props txt nil 'priority priority
  16740. 'org-category category 'date date
  16741. 'type "timestamp")
  16742. (push txt ee))
  16743. (outline-next-heading)))
  16744. (nreverse ee)))
  16745. (defun org-agenda-get-sexps ()
  16746. "Return the sexp information for agenda display."
  16747. (require 'diary-lib)
  16748. (let* ((props (list 'face nil
  16749. 'mouse-face 'highlight
  16750. 'keymap org-agenda-keymap
  16751. 'help-echo
  16752. (format "mouse-2 or RET jump to org file %s"
  16753. (abbreviate-file-name buffer-file-name))))
  16754. (regexp "^&?%%(")
  16755. marker category ee txt tags entry result beg b sexp sexp-entry)
  16756. (goto-char (point-min))
  16757. (while (re-search-forward regexp nil t)
  16758. (catch :skip
  16759. (org-agenda-skip)
  16760. (setq beg (match-beginning 0))
  16761. (goto-char (1- (match-end 0)))
  16762. (setq b (point))
  16763. (forward-sexp 1)
  16764. (setq sexp (buffer-substring b (point)))
  16765. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  16766. (org-trim (match-string 1))
  16767. ""))
  16768. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  16769. (when result
  16770. (setq marker (org-agenda-new-marker beg)
  16771. category (org-get-category beg))
  16772. (if (string-match "\\S-" result)
  16773. (setq txt result)
  16774. (setq txt "SEXP entry returned empty string"))
  16775. (setq txt (org-format-agenda-item
  16776. "" txt category tags 'time))
  16777. (org-add-props txt props 'org-marker marker)
  16778. (org-add-props txt nil
  16779. 'org-category category 'date date
  16780. 'type "sexp")
  16781. (push txt ee))))
  16782. (nreverse ee)))
  16783. (defun org-agenda-get-closed ()
  16784. "Return the logged TODO entries for agenda display."
  16785. (let* ((props (list 'mouse-face 'highlight
  16786. 'org-not-done-regexp org-not-done-regexp
  16787. 'org-todo-regexp org-todo-regexp
  16788. 'keymap org-agenda-keymap
  16789. 'help-echo
  16790. (format "mouse-2 or RET jump to org file %s"
  16791. (abbreviate-file-name buffer-file-name))))
  16792. (regexp (concat
  16793. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  16794. (regexp-quote
  16795. (substring
  16796. (format-time-string
  16797. (car org-time-stamp-formats)
  16798. (apply 'encode-time ; DATE bound by calendar
  16799. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  16800. 1 11))))
  16801. marker hdmarker priority category tags closedp
  16802. ee txt timestr)
  16803. (goto-char (point-min))
  16804. (while (re-search-forward regexp nil t)
  16805. (catch :skip
  16806. (org-agenda-skip)
  16807. (setq marker (org-agenda-new-marker (match-beginning 0))
  16808. closedp (equal (match-string 1) org-closed-string)
  16809. category (org-get-category (match-beginning 0))
  16810. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  16811. ;; donep (org-entry-is-done-p)
  16812. )
  16813. (if (string-match "\\]" timestr)
  16814. ;; substring should only run to end of time stamp
  16815. (setq timestr (substring timestr 0 (match-end 0))))
  16816. (save-excursion
  16817. (if (re-search-backward "^\\*+ " nil t)
  16818. (progn
  16819. (goto-char (match-beginning 0))
  16820. (setq hdmarker (org-agenda-new-marker)
  16821. tags (org-get-tags-at))
  16822. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  16823. (setq txt (org-format-agenda-item
  16824. (if closedp "Closed: " "Clocked: ")
  16825. (match-string 1) category tags timestr)))
  16826. (setq txt org-agenda-no-heading-message))
  16827. (setq priority 100000)
  16828. (org-add-props txt props
  16829. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  16830. 'priority priority 'org-category category
  16831. 'type "closed" 'date date
  16832. 'undone-face 'org-warning 'done-face 'org-done)
  16833. (push txt ee))
  16834. (outline-next-heading)))
  16835. (nreverse ee)))
  16836. (defun org-agenda-get-deadlines ()
  16837. "Return the deadline information for agenda display."
  16838. (let* ((props (list 'mouse-face 'highlight
  16839. 'org-not-done-regexp org-not-done-regexp
  16840. 'org-todo-regexp org-todo-regexp
  16841. 'keymap org-agenda-keymap
  16842. 'help-echo
  16843. (format "mouse-2 or RET jump to org file %s"
  16844. (abbreviate-file-name buffer-file-name))))
  16845. (regexp org-deadline-time-regexp)
  16846. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  16847. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  16848. d2 diff dfrac wdays pos pos1 category tags
  16849. ee txt head face s upcomingp donep timestr)
  16850. (goto-char (point-min))
  16851. (while (re-search-forward regexp nil t)
  16852. (catch :skip
  16853. (org-agenda-skip)
  16854. (setq s (match-string 1)
  16855. pos (1- (match-beginning 1))
  16856. d2 (org-time-string-to-absolute (match-string 1) d1)
  16857. diff (- d2 d1))
  16858. (if (string-match "-\\([0-9]+\\)\\([dwmy]\\)\\'" s)
  16859. (setq wdays
  16860. (floor
  16861. (* (string-to-number (match-string 1 s))
  16862. (cdr (assoc (match-string 2 s)
  16863. '(("d" . 1) ("w" . 7)
  16864. ("m" . 30.4) ("y" . 365.25)))))))
  16865. (setq wdays org-deadline-warning-days))
  16866. (setq dfrac (/ (* 1.0 (- wdays diff)) wdays))
  16867. (setq upcomingp (and todayp (> diff 0)))
  16868. ;; When to show a deadline in the calendar:
  16869. ;; If the expiration is within wdays warning time.
  16870. ;; Past-due deadlines are only shown on the current date
  16871. (if (or (and (<= diff wdays) todayp)
  16872. (= diff 0))
  16873. (save-excursion
  16874. (setq category (org-get-category))
  16875. (if (re-search-backward "^\\*+[ \t]+" nil t)
  16876. (progn
  16877. (goto-char (match-end 0))
  16878. (setq pos1 (match-beginning 0))
  16879. (setq tags (org-get-tags-at pos1))
  16880. (setq head (buffer-substring-no-properties
  16881. (point)
  16882. (progn (skip-chars-forward "^\r\n")
  16883. (point))))
  16884. (setq donep (string-match org-looking-at-done-regexp head))
  16885. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  16886. (setq timestr (concat (match-string 1 s) " "))
  16887. (setq timestr nil))
  16888. (if (and donep
  16889. (or org-agenda-skip-deadline-if-done
  16890. (not (= diff 0))))
  16891. (setq txt nil)
  16892. (setq txt (org-format-agenda-item
  16893. (if (= diff 0)
  16894. "Deadline: "
  16895. (format "In %3d d.: " diff))
  16896. head category tags timestr))))
  16897. (setq txt org-agenda-no-heading-message))
  16898. (when txt
  16899. (setq face (org-agenda-deadline-face dfrac))
  16900. (org-add-props txt props
  16901. 'org-marker (org-agenda-new-marker pos)
  16902. 'org-hd-marker (org-agenda-new-marker pos1)
  16903. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  16904. (org-get-priority txt))
  16905. 'org-category category
  16906. 'type (if upcomingp "upcoming-deadline" "deadline")
  16907. 'date (if upcomingp date d2)
  16908. 'face (if donep 'org-done face)
  16909. 'undone-face face 'done-face 'org-done)
  16910. (push txt ee))))))
  16911. ee))
  16912. (defun org-agenda-deadline-face (fraction)
  16913. "Return the face to displaying a deadline item.
  16914. FRACTION is what fraction of the head-warning time has passed."
  16915. (let ((faces org-agenda-deadline-faces) f)
  16916. (catch 'exit
  16917. (while (setq f (pop faces))
  16918. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  16919. (defun org-agenda-get-scheduled ()
  16920. "Return the scheduled information for agenda display."
  16921. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  16922. 'org-todo-regexp org-todo-regexp
  16923. 'done-face 'org-done
  16924. 'mouse-face 'highlight
  16925. 'keymap org-agenda-keymap
  16926. 'help-echo
  16927. (format "mouse-2 or RET jump to org file %s"
  16928. (abbreviate-file-name buffer-file-name))))
  16929. (regexp org-scheduled-time-regexp)
  16930. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  16931. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  16932. d2 diff pos pos1 category tags
  16933. ee txt head pastschedp donep face timestr s)
  16934. (goto-char (point-min))
  16935. (while (re-search-forward regexp nil t)
  16936. (catch :skip
  16937. (org-agenda-skip)
  16938. (setq s (match-string 1)
  16939. pos (1- (match-beginning 1))
  16940. d2 (org-time-string-to-absolute (match-string 1) d1)
  16941. diff (- d2 d1))
  16942. (setq pastschedp (and todayp (< diff 0)))
  16943. ;; When to show a scheduled item in the calendar:
  16944. ;; If it is on or past the date.
  16945. (if (or (and (< diff 0) todayp)
  16946. (= diff 0))
  16947. (save-excursion
  16948. (setq category (org-get-category))
  16949. (if (re-search-backward "^\\*+[ \t]+" nil t)
  16950. (progn
  16951. (goto-char (match-end 0))
  16952. (setq pos1 (match-beginning 0))
  16953. (setq tags (org-get-tags-at))
  16954. (setq head (buffer-substring-no-properties
  16955. (point)
  16956. (progn (skip-chars-forward "^\r\n") (point))))
  16957. (setq donep (string-match org-looking-at-done-regexp head))
  16958. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  16959. (setq timestr (concat (match-string 1 s) " "))
  16960. (setq timestr nil))
  16961. (if (and donep
  16962. (or org-agenda-skip-scheduled-if-done
  16963. (not (= diff 0))))
  16964. (setq txt nil)
  16965. (setq txt (org-format-agenda-item
  16966. (if (= diff 0)
  16967. "Scheduled: "
  16968. (format "Sched.%2dx: " (- 1 diff)))
  16969. head category tags timestr))))
  16970. (setq txt org-agenda-no-heading-message))
  16971. (when txt
  16972. (setq face (if pastschedp
  16973. 'org-scheduled-previously
  16974. 'org-scheduled-today))
  16975. (org-add-props txt props
  16976. 'undone-face face
  16977. 'face (if donep 'org-done face)
  16978. 'org-marker (org-agenda-new-marker pos)
  16979. 'org-hd-marker (org-agenda-new-marker pos1)
  16980. 'type (if pastschedp "past-scheduled" "scheduled")
  16981. 'date (if pastschedp d2 date)
  16982. 'priority (+ (- 5 diff) (org-get-priority txt))
  16983. 'org-category category)
  16984. (push txt ee))))))
  16985. ee))
  16986. (defun org-agenda-get-blocks ()
  16987. "Return the date-range information for agenda display."
  16988. (let* ((props (list 'face nil
  16989. 'org-not-done-regexp org-not-done-regexp
  16990. 'org-todo-regexp org-todo-regexp
  16991. 'mouse-face 'highlight
  16992. 'keymap org-agenda-keymap
  16993. 'help-echo
  16994. (format "mouse-2 or RET jump to org file %s"
  16995. (abbreviate-file-name buffer-file-name))))
  16996. (regexp org-tr-regexp)
  16997. (d0 (calendar-absolute-from-gregorian date))
  16998. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  16999. (goto-char (point-min))
  17000. (while (re-search-forward regexp nil t)
  17001. (catch :skip
  17002. (org-agenda-skip)
  17003. (setq pos (point))
  17004. (setq timestr (match-string 0)
  17005. s1 (match-string 1)
  17006. s2 (match-string 2)
  17007. d1 (time-to-days (org-time-string-to-time s1))
  17008. d2 (time-to-days (org-time-string-to-time s2)))
  17009. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  17010. ;; Only allow days between the limits, because the normal
  17011. ;; date stamps will catch the limits.
  17012. (save-excursion
  17013. (setq marker (org-agenda-new-marker (point)))
  17014. (setq category (org-get-category))
  17015. (if (re-search-backward "^\\*+ " nil t)
  17016. (progn
  17017. (goto-char (match-beginning 0))
  17018. (setq hdmarker (org-agenda-new-marker (point)))
  17019. (setq tags (org-get-tags-at))
  17020. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17021. (setq txt (org-format-agenda-item
  17022. (format (if (= d1 d2) "" "(%d/%d): ")
  17023. (1+ (- d0 d1)) (1+ (- d2 d1)))
  17024. (match-string 1) category tags
  17025. (if (= d0 d1) timestr))))
  17026. (setq txt org-agenda-no-heading-message))
  17027. (org-add-props txt props
  17028. 'org-marker marker 'org-hd-marker hdmarker
  17029. 'type "block" 'date date
  17030. 'priority (org-get-priority txt) 'org-category category)
  17031. (push txt ee)))
  17032. (goto-char pos)))
  17033. ;; Sort the entries by expiration date.
  17034. (nreverse ee)))
  17035. ;;; Agenda presentation and sorting
  17036. (defconst org-plain-time-of-day-regexp
  17037. (concat
  17038. "\\(\\<[012]?[0-9]"
  17039. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  17040. "\\(--?"
  17041. "\\(\\<[012]?[0-9]"
  17042. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  17043. "\\)?")
  17044. "Regular expression to match a plain time or time range.
  17045. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  17046. groups carry important information:
  17047. 0 the full match
  17048. 1 the first time, range or not
  17049. 8 the second time, if it is a range.")
  17050. (defconst org-stamp-time-of-day-regexp
  17051. (concat
  17052. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  17053. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  17054. "\\(--?"
  17055. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  17056. "Regular expression to match a timestamp time or time range.
  17057. After a match, the following groups carry important information:
  17058. 0 the full match
  17059. 1 date plus weekday, for backreferencing to make sure both times on same day
  17060. 2 the first time, range or not
  17061. 4 the second time, if it is a range.")
  17062. (defvar org-prefix-has-time nil
  17063. "A flag, set by `org-compile-prefix-format'.
  17064. The flag is set if the currently compiled format contains a `%t'.")
  17065. (defvar org-prefix-has-tag nil
  17066. "A flag, set by `org-compile-prefix-format'.
  17067. The flag is set if the currently compiled format contains a `%T'.")
  17068. (defun org-format-agenda-item (extra txt &optional category tags dotime
  17069. noprefix)
  17070. "Format TXT to be inserted into the agenda buffer.
  17071. In particular, it adds the prefix and corresponding text properties. EXTRA
  17072. must be a string and replaces the `%s' specifier in the prefix format.
  17073. CATEGORY (string, symbol or nil) may be used to overrule the default
  17074. category taken from local variable or file name. It will replace the `%c'
  17075. specifier in the format. DOTIME, when non-nil, indicates that a
  17076. time-of-day should be extracted from TXT for sorting of this entry, and for
  17077. the `%t' specifier in the format. When DOTIME is a string, this string is
  17078. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  17079. only the correctly processes TXT should be returned - this is used by
  17080. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  17081. (save-match-data
  17082. ;; Diary entries sometimes have extra whitespace at the beginning
  17083. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  17084. (let* ((category (or category
  17085. org-category
  17086. (if buffer-file-name
  17087. (file-name-sans-extension
  17088. (file-name-nondirectory buffer-file-name))
  17089. "")))
  17090. (tag (if tags (nth (1- (length tags)) tags) ""))
  17091. time ; time and tag are needed for the eval of the prefix format
  17092. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  17093. (time-of-day (and dotime (org-get-time-of-day ts)))
  17094. stamp plain s0 s1 s2 rtn srp)
  17095. (when (and dotime time-of-day org-prefix-has-time)
  17096. ;; Extract starting and ending time and move them to prefix
  17097. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  17098. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  17099. (setq s0 (match-string 0 ts)
  17100. srp (and stamp (match-end 3))
  17101. s1 (match-string (if plain 1 2) ts)
  17102. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  17103. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  17104. ;; them, we might want to remove them there to avoid duplication.
  17105. ;; The user can turn this off with a variable.
  17106. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  17107. (string-match (concat (regexp-quote s0) " *") txt)
  17108. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  17109. (= (match-beginning 0) 0)
  17110. t))
  17111. (setq txt (replace-match "" nil nil txt))))
  17112. ;; Normalize the time(s) to 24 hour
  17113. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  17114. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  17115. (when (and s1 (not s2) org-agenda-default-appointment-duration
  17116. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  17117. (let ((m (+ (string-to-number (match-string 2 s1))
  17118. (* 60 (string-to-number (match-string 1 s1)))
  17119. org-agenda-default-appointment-duration))
  17120. h)
  17121. (setq h (/ m 60) m (- m (* h 60)))
  17122. (setq s2 (format "%02d:%02d" h m))))
  17123. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  17124. txt)
  17125. ;; Tags are in the string
  17126. (if (or (eq org-agenda-remove-tags t)
  17127. (and org-agenda-remove-tags
  17128. org-prefix-has-tag))
  17129. (setq txt (replace-match "" t t txt))
  17130. (setq txt (replace-match
  17131. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  17132. (match-string 2 txt))
  17133. t t txt))))
  17134. ;; Create the final string
  17135. (if noprefix
  17136. (setq rtn txt)
  17137. ;; Prepare the variables needed in the eval of the compiled format
  17138. (setq time (cond (s2 (concat s1 "-" s2))
  17139. (s1 (concat s1 "......"))
  17140. (t ""))
  17141. extra (or extra "")
  17142. category (if (symbolp category) (symbol-name category) category))
  17143. ;; Evaluate the compiled format
  17144. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  17145. ;; And finally add the text properties
  17146. (org-add-props rtn nil
  17147. 'org-category (downcase category) 'tags tags
  17148. 'prefix-length (- (length rtn) (length txt))
  17149. 'time-of-day time-of-day
  17150. 'txt txt
  17151. 'time time
  17152. 'extra extra
  17153. 'dotime dotime))))
  17154. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  17155. (defvar org-agenda-sorting-strategy-selected nil)
  17156. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  17157. (catch 'exit
  17158. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  17159. ((and todayp (member 'today (car org-agenda-time-grid))))
  17160. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  17161. ((member 'weekly (car org-agenda-time-grid)))
  17162. (t (throw 'exit list)))
  17163. (let* ((have (delq nil (mapcar
  17164. (lambda (x) (get-text-property 1 'time-of-day x))
  17165. list)))
  17166. (string (nth 1 org-agenda-time-grid))
  17167. (gridtimes (nth 2 org-agenda-time-grid))
  17168. (req (car org-agenda-time-grid))
  17169. (remove (member 'remove-match req))
  17170. new time)
  17171. (if (and (member 'require-timed req) (not have))
  17172. ;; don't show empty grid
  17173. (throw 'exit list))
  17174. (while (setq time (pop gridtimes))
  17175. (unless (and remove (member time have))
  17176. (setq time (int-to-string time))
  17177. (push (org-format-agenda-item
  17178. nil string "" nil
  17179. (concat (substring time 0 -2) ":" (substring time -2)))
  17180. new)
  17181. (put-text-property
  17182. 1 (length (car new)) 'face 'org-time-grid (car new))))
  17183. (if (member 'time-up org-agenda-sorting-strategy-selected)
  17184. (append new list)
  17185. (append list new)))))
  17186. (defun org-compile-prefix-format (key)
  17187. "Compile the prefix format into a Lisp form that can be evaluated.
  17188. The resulting form is returned and stored in the variable
  17189. `org-prefix-format-compiled'."
  17190. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  17191. (let ((s (cond
  17192. ((stringp org-agenda-prefix-format)
  17193. org-agenda-prefix-format)
  17194. ((assq key org-agenda-prefix-format)
  17195. (cdr (assq key org-agenda-prefix-format)))
  17196. (t " %-12:c%?-12t% s")))
  17197. (start 0)
  17198. varform vars var e c f opt)
  17199. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  17200. s start)
  17201. (setq var (cdr (assoc (match-string 4 s)
  17202. '(("c" . category) ("t" . time) ("s" . extra)
  17203. ("T" . tag))))
  17204. c (or (match-string 3 s) "")
  17205. opt (match-beginning 1)
  17206. start (1+ (match-beginning 0)))
  17207. (if (equal var 'time) (setq org-prefix-has-time t))
  17208. (if (equal var 'tag) (setq org-prefix-has-tag t))
  17209. (setq f (concat "%" (match-string 2 s) "s"))
  17210. (if opt
  17211. (setq varform
  17212. `(if (equal "" ,var)
  17213. ""
  17214. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  17215. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  17216. (setq s (replace-match "%s" t nil s))
  17217. (push varform vars))
  17218. (setq vars (nreverse vars))
  17219. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  17220. (defun org-set-sorting-strategy (key)
  17221. (if (symbolp (car org-agenda-sorting-strategy))
  17222. ;; the old format
  17223. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  17224. (setq org-agenda-sorting-strategy-selected
  17225. (or (cdr (assq key org-agenda-sorting-strategy))
  17226. (cdr (assq 'agenda org-agenda-sorting-strategy))
  17227. '(time-up category-keep priority-down)))))
  17228. (defun org-get-time-of-day (s &optional string mod24)
  17229. "Check string S for a time of day.
  17230. If found, return it as a military time number between 0 and 2400.
  17231. If not found, return nil.
  17232. The optional STRING argument forces conversion into a 5 character wide string
  17233. HH:MM."
  17234. (save-match-data
  17235. (when
  17236. (or
  17237. (string-match
  17238. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  17239. (string-match
  17240. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  17241. (let* ((h (string-to-number (match-string 1 s)))
  17242. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  17243. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  17244. (am-p (equal ampm "am"))
  17245. (h1 (cond ((not ampm) h)
  17246. ((= h 12) (if am-p 0 12))
  17247. (t (+ h (if am-p 0 12)))))
  17248. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  17249. (mod h1 24) h1))
  17250. (t0 (+ (* 100 h2) m))
  17251. (t1 (concat (if (>= h1 24) "+" " ")
  17252. (if (< t0 100) "0" "")
  17253. (if (< t0 10) "0" "")
  17254. (int-to-string t0))))
  17255. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  17256. (defun org-finalize-agenda-entries (list &optional nosort)
  17257. "Sort and concatenate the agenda items."
  17258. (setq list (mapcar 'org-agenda-highlight-todo list))
  17259. (if nosort
  17260. list
  17261. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  17262. (defun org-agenda-highlight-todo (x)
  17263. (let (re pl)
  17264. (if (eq x 'line)
  17265. (save-excursion
  17266. (beginning-of-line 1)
  17267. (setq re (get-text-property (point) 'org-not-done-regexp))
  17268. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  17269. (and (looking-at (concat "[ \t]*\\.*" re))
  17270. (add-text-properties (match-beginning 0) (match-end 0)
  17271. '(face org-todo))))
  17272. (setq re (concat (get-text-property 0 'org-not-done-regexp x))
  17273. pl (get-text-property 0 'prefix-length x))
  17274. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  17275. (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
  17276. '(face org-todo) x))
  17277. x)))
  17278. (defsubst org-cmp-priority (a b)
  17279. "Compare the priorities of string A and B."
  17280. (let ((pa (or (get-text-property 1 'priority a) 0))
  17281. (pb (or (get-text-property 1 'priority b) 0)))
  17282. (cond ((> pa pb) +1)
  17283. ((< pa pb) -1)
  17284. (t nil))))
  17285. (defsubst org-cmp-category (a b)
  17286. "Compare the string values of categories of strings A and B."
  17287. (let ((ca (or (get-text-property 1 'org-category a) ""))
  17288. (cb (or (get-text-property 1 'org-category b) "")))
  17289. (cond ((string-lessp ca cb) -1)
  17290. ((string-lessp cb ca) +1)
  17291. (t nil))))
  17292. (defsubst org-cmp-tag (a b)
  17293. "Compare the string values of categories of strings A and B."
  17294. (let ((ta (car (last (get-text-property 1 'tags a))))
  17295. (tb (car (last (get-text-property 1 'tags b)))))
  17296. (cond ((not ta) +1)
  17297. ((not tb) -1)
  17298. ((string-lessp ta tb) -1)
  17299. ((string-lessp tb ta) +1)
  17300. (t nil))))
  17301. (defsubst org-cmp-time (a b)
  17302. "Compare the time-of-day values of strings A and B."
  17303. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  17304. (ta (or (get-text-property 1 'time-of-day a) def))
  17305. (tb (or (get-text-property 1 'time-of-day b) def)))
  17306. (cond ((< ta tb) -1)
  17307. ((< tb ta) +1)
  17308. (t nil))))
  17309. (defun org-entries-lessp (a b)
  17310. "Predicate for sorting agenda entries."
  17311. ;; The following variables will be used when the form is evaluated.
  17312. ;; So even though the compiler complains, keep them.
  17313. (let* ((time-up (org-cmp-time a b))
  17314. (time-down (if time-up (- time-up) nil))
  17315. (priority-up (org-cmp-priority a b))
  17316. (priority-down (if priority-up (- priority-up) nil))
  17317. (category-up (org-cmp-category a b))
  17318. (category-down (if category-up (- category-up) nil))
  17319. (category-keep (if category-up +1 nil))
  17320. (tag-up (org-cmp-tag a b))
  17321. (tag-down (if tag-up (- tag-up) nil)))
  17322. (cdr (assoc
  17323. (eval (cons 'or org-agenda-sorting-strategy-selected))
  17324. '((-1 . t) (1 . nil) (nil . nil))))))
  17325. ;;; Agenda commands
  17326. (defun org-agenda-check-type (error &rest types)
  17327. "Check if agenda buffer is of allowed type.
  17328. If ERROR is non-nil, throw an error, otherwise just return nil."
  17329. (if (memq org-agenda-type types)
  17330. t
  17331. (if error
  17332. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  17333. nil)))
  17334. (defun org-agenda-quit ()
  17335. "Exit agenda by removing the window or the buffer."
  17336. (interactive)
  17337. (let ((buf (current-buffer)))
  17338. (if (not (one-window-p)) (delete-window))
  17339. (kill-buffer buf)
  17340. (org-agenda-maybe-reset-markers 'force)
  17341. (org-columns-remove-overlays))
  17342. ;; Maybe restore the pre-agenda window configuration.
  17343. (and org-agenda-restore-windows-after-quit
  17344. (not (eq org-agenda-window-setup 'other-frame))
  17345. org-pre-agenda-window-conf
  17346. (set-window-configuration org-pre-agenda-window-conf)))
  17347. (defun org-agenda-exit ()
  17348. "Exit agenda by removing the window or the buffer.
  17349. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  17350. Org-mode buffers visited directly by the user will not be touched."
  17351. (interactive)
  17352. (org-release-buffers org-agenda-new-buffers)
  17353. (setq org-agenda-new-buffers nil)
  17354. (org-agenda-quit))
  17355. (defun org-save-all-org-buffers ()
  17356. "Save all Org-mode buffers without user confirmation."
  17357. (interactive)
  17358. (message "Saving all Org-mode buffers...")
  17359. (save-some-buffers t 'org-mode-p)
  17360. (message "Saving all Org-mode buffers... done"))
  17361. (defun org-agenda-redo ()
  17362. "Rebuild Agenda.
  17363. When this is the global TODO list, a prefix argument will be interpreted."
  17364. (interactive)
  17365. (let* ((org-agenda-keep-modes t)
  17366. (line (org-current-line))
  17367. (window-line (- line (org-current-line (window-start))))
  17368. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  17369. (message "Rebuilding agenda buffer...")
  17370. (org-let lprops '(eval org-agenda-redo-command))
  17371. (setq org-agenda-undo-list nil
  17372. org-agenda-pending-undo-list nil)
  17373. (message "Rebuilding agenda buffer...done")
  17374. (goto-line line)
  17375. (recenter window-line)))
  17376. (defun org-agenda-goto-date (date)
  17377. "Jump to DATE in agenda."
  17378. (interactive (list (org-read-date)))
  17379. (org-agenda-list nil date))
  17380. (defun org-agenda-goto-today ()
  17381. "Go to today."
  17382. (interactive)
  17383. (org-agenda-check-type t 'timeline 'agenda)
  17384. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  17385. (cond
  17386. (tdpos (goto-char tdpos))
  17387. ((eq org-agenda-type 'agenda)
  17388. (let* ((sd (time-to-days (current-time)))
  17389. (comp (org-agenda-compute-time-span sd org-agenda-span))
  17390. (org-agenda-overriding-arguments org-agenda-last-arguments))
  17391. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  17392. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  17393. (org-agenda-redo)
  17394. (org-agenda-find-today-or-agenda)))
  17395. (t (error "Cannot find today")))))
  17396. (defun org-agenda-find-today-or-agenda ()
  17397. (goto-char
  17398. (or (text-property-any (point-min) (point-max) 'org-today t)
  17399. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  17400. (point-min))))
  17401. (defun org-agenda-later (arg)
  17402. "Go forward in time by thee current span.
  17403. With prefix ARG, go forward that many times the current span."
  17404. (interactive "p")
  17405. (org-agenda-check-type t 'agenda)
  17406. (let* ((span org-agenda-span)
  17407. (sd org-starting-day)
  17408. (greg (calendar-gregorian-from-absolute sd))
  17409. greg2 nd)
  17410. (cond
  17411. ((eq span 'day)
  17412. (setq sd (+ arg sd) nd 1))
  17413. ((eq span 'week)
  17414. (setq sd (+ (* 7 arg) sd) nd 7))
  17415. ((eq span 'month)
  17416. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  17417. sd (calendar-absolute-from-gregorian greg2))
  17418. (setcar greg2 (1+ (car greg2)))
  17419. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  17420. ((eq span 'year)
  17421. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  17422. sd (calendar-absolute-from-gregorian greg2))
  17423. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  17424. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  17425. (let ((org-agenda-overriding-arguments
  17426. (list (car org-agenda-last-arguments) sd nd t)))
  17427. (org-agenda-redo)
  17428. (org-agenda-find-today-or-agenda))))
  17429. (defun org-agenda-earlier (arg)
  17430. "Go backward in time by the current span.
  17431. With prefix ARG, go backward that many times the current span."
  17432. (interactive "p")
  17433. (org-agenda-later (- arg)))
  17434. (defun org-agenda-day-view ()
  17435. "Switch to daily view for agenda."
  17436. (interactive)
  17437. (setq org-agenda-ndays 1)
  17438. (org-agenda-change-time-span 'day))
  17439. (defun org-agenda-week-view ()
  17440. "Switch to daily view for agenda."
  17441. (interactive)
  17442. (setq org-agenda-ndays 7)
  17443. (org-agenda-change-time-span 'week))
  17444. (defun org-agenda-month-view ()
  17445. "Switch to daily view for agenda."
  17446. (interactive)
  17447. (org-agenda-change-time-span 'month))
  17448. (defun org-agenda-year-view ()
  17449. "Switch to daily view for agenda."
  17450. (interactive)
  17451. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  17452. (org-agenda-change-time-span 'year)
  17453. (error "Abort")))
  17454. (defun org-agenda-change-time-span (span)
  17455. "Change the agenda view to SPAN.
  17456. SPAN may be `day', `week', `month', `year'."
  17457. (org-agenda-check-type t 'agenda)
  17458. (if (equal org-agenda-span span)
  17459. (error "Viewing span is already \"%s\"" span))
  17460. (let* ((sd (or (get-text-property (point) 'day)
  17461. org-starting-day))
  17462. (computed (org-agenda-compute-time-span sd span))
  17463. (org-agenda-overriding-arguments
  17464. (list (car org-agenda-last-arguments)
  17465. (car computed) (cdr computed) t)))
  17466. (org-agenda-redo)
  17467. (org-agenda-find-today-or-agenda))
  17468. (org-agenda-set-mode-name)
  17469. (message "Switched to %s view" span))
  17470. (defun org-agenda-compute-time-span (sd span)
  17471. "Compute starting date and number of days for agenda.
  17472. SPAN may be `day', `week', `month', `year'. The return value
  17473. is a cons cell with the starting date and the number of days,
  17474. so that the date SD will be in that range."
  17475. (let* ((greg (calendar-gregorian-from-absolute sd))
  17476. nd)
  17477. (cond
  17478. ((eq span 'day)
  17479. (setq nd 1))
  17480. ((eq span 'week)
  17481. (let* ((nt (calendar-day-of-week
  17482. (calendar-gregorian-from-absolute sd)))
  17483. (d (if org-agenda-start-on-weekday
  17484. (- nt org-agenda-start-on-weekday)
  17485. 0)))
  17486. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  17487. (setq nd 7)))
  17488. ((eq span 'month)
  17489. (setq sd (calendar-absolute-from-gregorian
  17490. (list (car greg) 1 (nth 2 greg)))
  17491. nd (- (calendar-absolute-from-gregorian
  17492. (list (1+ (car greg)) 1 (nth 2 greg)))
  17493. sd)))
  17494. ((eq span 'year)
  17495. (setq sd (calendar-absolute-from-gregorian
  17496. (list 1 1 (nth 2 greg)))
  17497. nd (- (calendar-absolute-from-gregorian
  17498. (list 1 1 (1+ (nth 2 greg))))
  17499. sd))))
  17500. (cons sd nd)))
  17501. ;; FIXME: this no longer works if user make date format that starts with a blank
  17502. (defun org-agenda-next-date-line (&optional arg)
  17503. "Jump to the next line indicating a date in agenda buffer."
  17504. (interactive "p")
  17505. (org-agenda-check-type t 'agenda 'timeline)
  17506. (beginning-of-line 1)
  17507. (if (looking-at "^\\S-") (forward-char 1))
  17508. (if (not (re-search-forward "^\\S-" nil t arg))
  17509. (progn
  17510. (backward-char 1)
  17511. (error "No next date after this line in this buffer")))
  17512. (goto-char (match-beginning 0)))
  17513. (defun org-agenda-previous-date-line (&optional arg)
  17514. "Jump to the previous line indicating a date in agenda buffer."
  17515. (interactive "p")
  17516. (org-agenda-check-type t 'agenda 'timeline)
  17517. (beginning-of-line 1)
  17518. (if (not (re-search-backward "^\\S-" nil t arg))
  17519. (error "No previous date before this line in this buffer")))
  17520. ;; Initialize the highlight
  17521. (defvar org-hl (org-make-overlay 1 1))
  17522. (org-overlay-put org-hl 'face 'highlight)
  17523. (defun org-highlight (begin end &optional buffer)
  17524. "Highlight a region with overlay."
  17525. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  17526. org-hl begin end (or buffer (current-buffer))))
  17527. (defun org-unhighlight ()
  17528. "Detach overlay INDEX."
  17529. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  17530. ;; FIXME this is currently not used.
  17531. (defun org-highlight-until-next-command (beg end &optional buffer)
  17532. (org-highlight beg end buffer)
  17533. (add-hook 'pre-command-hook 'org-unhighlight-once))
  17534. (defun org-unhighlight-once ()
  17535. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  17536. (org-unhighlight))
  17537. (defun org-agenda-follow-mode ()
  17538. "Toggle follow mode in an agenda buffer."
  17539. (interactive)
  17540. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  17541. (org-agenda-set-mode-name)
  17542. (message "Follow mode is %s"
  17543. (if org-agenda-follow-mode "on" "off")))
  17544. (defun org-agenda-log-mode ()
  17545. "Toggle log mode in an agenda buffer."
  17546. (interactive)
  17547. (org-agenda-check-type t 'agenda 'timeline)
  17548. (setq org-agenda-show-log (not org-agenda-show-log))
  17549. (org-agenda-set-mode-name)
  17550. (org-agenda-redo)
  17551. (message "Log mode is %s"
  17552. (if org-agenda-show-log "on" "off")))
  17553. (defun org-agenda-toggle-diary ()
  17554. "Toggle diary inclusion in an agenda buffer."
  17555. (interactive)
  17556. (org-agenda-check-type t 'agenda)
  17557. (setq org-agenda-include-diary (not org-agenda-include-diary))
  17558. (org-agenda-redo)
  17559. (org-agenda-set-mode-name)
  17560. (message "Diary inclusion turned %s"
  17561. (if org-agenda-include-diary "on" "off")))
  17562. (defun org-agenda-toggle-time-grid ()
  17563. "Toggle time grid in an agenda buffer."
  17564. (interactive)
  17565. (org-agenda-check-type t 'agenda)
  17566. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  17567. (org-agenda-redo)
  17568. (org-agenda-set-mode-name)
  17569. (message "Time-grid turned %s"
  17570. (if org-agenda-use-time-grid "on" "off")))
  17571. (defun org-agenda-set-mode-name ()
  17572. "Set the mode name to indicate all the small mode settings."
  17573. (setq mode-name
  17574. (concat "Org-Agenda"
  17575. (if (equal org-agenda-ndays 1) " Day" "")
  17576. (if (equal org-agenda-ndays 7) " Week" "")
  17577. (if org-agenda-follow-mode " Follow" "")
  17578. (if org-agenda-include-diary " Diary" "")
  17579. (if org-agenda-use-time-grid " Grid" "")
  17580. (if org-agenda-show-log " Log" "")))
  17581. (force-mode-line-update))
  17582. (defun org-agenda-post-command-hook ()
  17583. (and (eolp) (not (bolp)) (backward-char 1))
  17584. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  17585. (if (and org-agenda-follow-mode
  17586. (get-text-property (point) 'org-marker))
  17587. (org-agenda-show)))
  17588. (defun org-agenda-show-priority ()
  17589. "Show the priority of the current item.
  17590. This priority is composed of the main priority given with the [#A] cookies,
  17591. and by additional input from the age of a schedules or deadline entry."
  17592. (interactive)
  17593. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  17594. (message "Priority is %d" (if pri pri -1000))))
  17595. (defun org-agenda-show-tags ()
  17596. "Show the tags applicable to the current item."
  17597. (interactive)
  17598. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  17599. (if tags
  17600. (message "Tags are :%s:"
  17601. (org-no-properties (mapconcat 'identity tags ":")))
  17602. (message "No tags associated with this line"))))
  17603. (defun org-agenda-goto (&optional highlight)
  17604. "Go to the Org-mode file which contains the item at point."
  17605. (interactive)
  17606. (let* ((marker (or (get-text-property (point) 'org-marker)
  17607. (org-agenda-error)))
  17608. (buffer (marker-buffer marker))
  17609. (pos (marker-position marker)))
  17610. (switch-to-buffer-other-window buffer)
  17611. (widen)
  17612. (goto-char pos)
  17613. (when (org-mode-p)
  17614. (org-show-context 'agenda)
  17615. (save-excursion
  17616. (and (outline-next-heading)
  17617. (org-flag-heading nil)))) ; show the next heading
  17618. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  17619. (defun org-agenda-kill ()
  17620. "Kill the entry or subtree belonging to the current agenda entry."
  17621. (interactive)
  17622. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17623. (let* ((marker (or (get-text-property (point) 'org-marker)
  17624. (org-agenda-error)))
  17625. (buffer (marker-buffer marker))
  17626. (pos (marker-position marker))
  17627. (type (get-text-property (point) 'type))
  17628. dbeg dend (n 0) conf)
  17629. (org-with-remote-undo buffer
  17630. (with-current-buffer buffer
  17631. (save-excursion
  17632. (goto-char pos)
  17633. (if (and (org-mode-p) (not (member type '("sexp"))))
  17634. (setq dbeg (progn (org-back-to-heading t) (point))
  17635. dend (org-end-of-subtree t t))
  17636. (setq dbeg (point-at-bol)
  17637. dend (min (point-max) (1+ (point-at-eol)))))
  17638. (goto-char dbeg)
  17639. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  17640. (setq conf (or (eq t org-agenda-confirm-kill)
  17641. (and (numberp org-agenda-confirm-kill)
  17642. (> n org-agenda-confirm-kill))))
  17643. (and conf
  17644. (not (y-or-n-p
  17645. (format "Delete entry with %d lines in buffer \"%s\"? "
  17646. n (buffer-name buffer))))
  17647. (error "Abort"))
  17648. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  17649. (with-current-buffer buffer (delete-region dbeg dend))
  17650. (message "Agenda item and source killed"))))
  17651. (defun org-agenda-archive ()
  17652. "Kill the entry or subtree belonging to the current agenda entry."
  17653. (interactive)
  17654. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  17655. (let* ((marker (or (get-text-property (point) 'org-marker)
  17656. (org-agenda-error)))
  17657. (buffer (marker-buffer marker))
  17658. (pos (marker-position marker)))
  17659. (org-with-remote-undo buffer
  17660. (with-current-buffer buffer
  17661. (if (org-mode-p)
  17662. (save-excursion
  17663. (goto-char pos)
  17664. (org-remove-subtree-entries-from-agenda)
  17665. (org-back-to-heading t)
  17666. (org-archive-subtree))
  17667. (error "Archiving works only in Org-mode files"))))))
  17668. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  17669. "Remove all lines in the agenda that correspond to a given subtree.
  17670. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  17671. If this information is not given, the function uses the tree at point."
  17672. (let ((buf (or buf (current-buffer))) m p)
  17673. (save-excursion
  17674. (unless (and beg end)
  17675. (org-back-to-heading t)
  17676. (setq beg (point))
  17677. (org-end-of-subtree t)
  17678. (setq end (point)))
  17679. (set-buffer (get-buffer org-agenda-buffer-name))
  17680. (save-excursion
  17681. (goto-char (point-max))
  17682. (beginning-of-line 1)
  17683. (while (not (bobp))
  17684. (when (and (setq m (get-text-property (point) 'org-marker))
  17685. (equal buf (marker-buffer m))
  17686. (setq p (marker-position m))
  17687. (>= p beg)
  17688. (<= p end))
  17689. (let ((inhibit-read-only t))
  17690. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  17691. (beginning-of-line 0))))))
  17692. (defun org-agenda-open-link ()
  17693. "Follow the link in the current line, if any."
  17694. (interactive)
  17695. (let ((eol (point-at-eol)))
  17696. (save-excursion
  17697. (if (or (re-search-forward org-bracket-link-regexp eol t)
  17698. (re-search-forward org-angle-link-re eol t)
  17699. (re-search-forward org-plain-link-re eol t))
  17700. (call-interactively 'org-open-at-point)
  17701. (error "No link in current line")))))
  17702. (defun org-agenda-switch-to (&optional delete-other-windows)
  17703. "Go to the Org-mode file which contains the item at point."
  17704. (interactive)
  17705. (let* ((marker (or (get-text-property (point) 'org-marker)
  17706. (org-agenda-error)))
  17707. (buffer (marker-buffer marker))
  17708. (pos (marker-position marker)))
  17709. (switch-to-buffer buffer)
  17710. (and delete-other-windows (delete-other-windows))
  17711. (widen)
  17712. (goto-char pos)
  17713. (when (org-mode-p)
  17714. (org-show-context 'agenda)
  17715. (save-excursion
  17716. (and (outline-next-heading)
  17717. (org-flag-heading nil)))))) ; show the next heading
  17718. (defun org-agenda-goto-mouse (ev)
  17719. "Go to the Org-mode file which contains the item at the mouse click."
  17720. (interactive "e")
  17721. (mouse-set-point ev)
  17722. (org-agenda-goto))
  17723. (defun org-agenda-show ()
  17724. "Display the Org-mode file which contains the item at point."
  17725. (interactive)
  17726. (let ((win (selected-window)))
  17727. (org-agenda-goto t)
  17728. (select-window win)))
  17729. (defun org-agenda-recenter (arg)
  17730. "Display the Org-mode file which contains the item at point and recenter."
  17731. (interactive "P")
  17732. (let ((win (selected-window)))
  17733. (org-agenda-goto t)
  17734. (recenter arg)
  17735. (select-window win)))
  17736. (defun org-agenda-show-mouse (ev)
  17737. "Display the Org-mode file which contains the item at the mouse click."
  17738. (interactive "e")
  17739. (mouse-set-point ev)
  17740. (org-agenda-show))
  17741. (defun org-agenda-check-no-diary ()
  17742. "Check if the entry is a diary link and abort if yes."
  17743. (if (get-text-property (point) 'org-agenda-diary-link)
  17744. (org-agenda-error)))
  17745. (defun org-agenda-error ()
  17746. (error "Command not allowed in this line"))
  17747. (defun org-agenda-tree-to-indirect-buffer ()
  17748. "Show the subtree corresponding to the current entry in an indirect buffer.
  17749. This calls the command `org-tree-to-indirect-buffer' from the original
  17750. Org-mode buffer.
  17751. With numerical prefix arg ARG, go up to this level and then take that tree.
  17752. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  17753. dedicated frame)."
  17754. (interactive)
  17755. (org-agenda-check-no-diary)
  17756. (let* ((marker (or (get-text-property (point) 'org-marker)
  17757. (org-agenda-error)))
  17758. (buffer (marker-buffer marker))
  17759. (pos (marker-position marker)))
  17760. (with-current-buffer buffer
  17761. (save-excursion
  17762. (goto-char pos)
  17763. (call-interactively 'org-tree-to-indirect-buffer)))))
  17764. (defvar org-last-heading-marker (make-marker)
  17765. "Marker pointing to the headline that last changed its TODO state
  17766. by a remote command from the agenda.")
  17767. (defun org-agenda-todo-nextset ()
  17768. "Switch TODO entry to next sequence."
  17769. (interactive)
  17770. (org-agenda-todo 'nextset))
  17771. (defun org-agenda-todo-previousset ()
  17772. "Switch TODO entry to previous sequence."
  17773. (interactive)
  17774. (org-agenda-todo 'previousset))
  17775. (defun org-agenda-todo (&optional arg)
  17776. "Cycle TODO state of line at point, also in Org-mode file.
  17777. This changes the line at point, all other lines in the agenda referring to
  17778. the same tree node, and the headline of the tree node in the Org-mode file."
  17779. (interactive "P")
  17780. (org-agenda-check-no-diary)
  17781. (let* ((col (current-column))
  17782. (marker (or (get-text-property (point) 'org-marker)
  17783. (org-agenda-error)))
  17784. (buffer (marker-buffer marker))
  17785. (pos (marker-position marker))
  17786. (hdmarker (get-text-property (point) 'org-hd-marker))
  17787. (inhibit-read-only t)
  17788. newhead)
  17789. (org-with-remote-undo buffer
  17790. (with-current-buffer buffer
  17791. (widen)
  17792. (goto-char pos)
  17793. (org-show-context 'agenda)
  17794. (save-excursion
  17795. (and (outline-next-heading)
  17796. (org-flag-heading nil))) ; show the next heading
  17797. (org-todo arg)
  17798. (and (bolp) (forward-char 1))
  17799. (setq newhead (org-get-heading))
  17800. (save-excursion
  17801. (org-back-to-heading)
  17802. (move-marker org-last-heading-marker (point))))
  17803. (beginning-of-line 1)
  17804. (save-excursion
  17805. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  17806. (move-to-column col))))
  17807. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  17808. "Change all lines in the agenda buffer which match HDMARKER.
  17809. The new content of the line will be NEWHEAD (as modified by
  17810. `org-format-agenda-item'). HDMARKER is checked with
  17811. `equal' against all `org-hd-marker' text properties in the file.
  17812. If FIXFACE is non-nil, the face of each item is modified acording to
  17813. the new TODO state."
  17814. (let* ((inhibit-read-only t)
  17815. props m pl undone-face done-face finish new dotime cat tags)
  17816. (save-excursion
  17817. (goto-char (point-max))
  17818. (beginning-of-line 1)
  17819. (while (not finish)
  17820. (setq finish (bobp))
  17821. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  17822. (equal m hdmarker))
  17823. (setq props (text-properties-at (point))
  17824. dotime (get-text-property (point) 'dotime)
  17825. cat (get-text-property (point) 'org-category)
  17826. tags (get-text-property (point) 'tags)
  17827. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  17828. pl (get-text-property (point) 'prefix-length)
  17829. undone-face (get-text-property (point) 'undone-face)
  17830. done-face (get-text-property (point) 'done-face))
  17831. (move-to-column pl)
  17832. (cond
  17833. ((equal new "")
  17834. (beginning-of-line 1)
  17835. (and (looking-at ".*\n?") (replace-match "")))
  17836. ((looking-at ".*")
  17837. (replace-match new t t)
  17838. (beginning-of-line 1)
  17839. (add-text-properties (point-at-bol) (point-at-eol) props)
  17840. (when fixface
  17841. (add-text-properties
  17842. (point-at-bol) (point-at-eol)
  17843. (list 'face
  17844. (if org-last-todo-state-is-todo
  17845. undone-face done-face))))
  17846. (org-agenda-highlight-todo 'line)
  17847. (beginning-of-line 1))
  17848. (t (error "Line update did not work"))))
  17849. (beginning-of-line 0)))
  17850. (org-finalize-agenda)))
  17851. ;; FIXME: allow negative value for org-agenda-align-tags-to-column
  17852. ;; See the code in set-tags for the way to do this.
  17853. (defun org-agenda-align-tags (&optional line)
  17854. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  17855. (let ((inhibit-read-only t))
  17856. (save-excursion
  17857. (goto-char (if line (point-at-bol) (point-min)))
  17858. (while (re-search-forward (org-re "\\([ \t]+\\):[[:alnum:]_@:]+:[ \t]*$")
  17859. (if line (point-at-eol) nil) t)
  17860. (delete-region (match-beginning 1) (match-end 1))
  17861. (goto-char (match-beginning 1))
  17862. (insert (org-add-props
  17863. (make-string (max 1 (- org-agenda-align-tags-to-column
  17864. (current-column))) ?\ )
  17865. (text-properties-at (point))))))))
  17866. (defun org-agenda-priority-up ()
  17867. "Increase the priority of line at point, also in Org-mode file."
  17868. (interactive)
  17869. (org-agenda-priority 'up))
  17870. (defun org-agenda-priority-down ()
  17871. "Decrease the priority of line at point, also in Org-mode file."
  17872. (interactive)
  17873. (org-agenda-priority 'down))
  17874. (defun org-agenda-priority (&optional force-direction)
  17875. "Set the priority of line at point, also in Org-mode file.
  17876. This changes the line at point, all other lines in the agenda referring to
  17877. the same tree node, and the headline of the tree node in the Org-mode file."
  17878. (interactive)
  17879. (org-agenda-check-no-diary)
  17880. (let* ((marker (or (get-text-property (point) 'org-marker)
  17881. (org-agenda-error)))
  17882. (hdmarker (get-text-property (point) 'org-hd-marker))
  17883. (buffer (marker-buffer hdmarker))
  17884. (pos (marker-position hdmarker))
  17885. (inhibit-read-only t)
  17886. newhead)
  17887. (org-with-remote-undo buffer
  17888. (with-current-buffer buffer
  17889. (widen)
  17890. (goto-char pos)
  17891. (org-show-context 'agenda)
  17892. (save-excursion
  17893. (and (outline-next-heading)
  17894. (org-flag-heading nil))) ; show the next heading
  17895. (funcall 'org-priority force-direction)
  17896. (end-of-line 1)
  17897. (setq newhead (org-get-heading)))
  17898. (org-agenda-change-all-lines newhead hdmarker)
  17899. (beginning-of-line 1))))
  17900. (defun org-get-tags-at (&optional pos)
  17901. "Get a list of all headline tags applicable at POS.
  17902. POS defaults to point. If tags are inherited, the list contains
  17903. the targets in the same sequence as the headlines appear, i.e.
  17904. the tags of the current headline come last."
  17905. (interactive)
  17906. (let (tags)
  17907. (save-excursion
  17908. (save-restriction
  17909. (widen)
  17910. (goto-char (or pos (point)))
  17911. (save-match-data
  17912. (org-back-to-heading t)
  17913. (condition-case nil
  17914. (while t
  17915. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  17916. (setq tags (append (org-split-string
  17917. (org-match-string-no-properties 1) ":")
  17918. tags)))
  17919. (or org-use-tag-inheritance (error ""))
  17920. (org-up-heading-all 1))
  17921. (error nil))))
  17922. tags)))
  17923. ;; FIXME: should fix the tags property of the agenda line.
  17924. (defun org-agenda-set-tags ()
  17925. "Set tags for the current headline."
  17926. (interactive)
  17927. (org-agenda-check-no-diary)
  17928. (if (and (org-region-active-p) (interactive-p))
  17929. (call-interactively 'org-change-tag-in-region)
  17930. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  17931. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  17932. (org-agenda-error)))
  17933. (buffer (marker-buffer hdmarker))
  17934. (pos (marker-position hdmarker))
  17935. (inhibit-read-only t)
  17936. newhead)
  17937. (org-with-remote-undo buffer
  17938. (with-current-buffer buffer
  17939. (widen)
  17940. (goto-char pos)
  17941. (save-excursion
  17942. (org-show-context 'agenda))
  17943. (save-excursion
  17944. (and (outline-next-heading)
  17945. (org-flag-heading nil))) ; show the next heading
  17946. (goto-char pos)
  17947. (call-interactively 'org-set-tags)
  17948. (end-of-line 1)
  17949. (setq newhead (org-get-heading)))
  17950. (org-agenda-change-all-lines newhead hdmarker)
  17951. (beginning-of-line 1)))))
  17952. (defun org-agenda-toggle-archive-tag ()
  17953. "Toggle the archive tag for the current entry."
  17954. (interactive)
  17955. (org-agenda-check-no-diary)
  17956. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  17957. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  17958. (org-agenda-error)))
  17959. (buffer (marker-buffer hdmarker))
  17960. (pos (marker-position hdmarker))
  17961. (inhibit-read-only t)
  17962. newhead)
  17963. (org-with-remote-undo buffer
  17964. (with-current-buffer buffer
  17965. (widen)
  17966. (goto-char pos)
  17967. (org-show-context 'agenda)
  17968. (save-excursion
  17969. (and (outline-next-heading)
  17970. (org-flag-heading nil))) ; show the next heading
  17971. (call-interactively 'org-toggle-archive-tag)
  17972. (end-of-line 1)
  17973. (setq newhead (org-get-heading)))
  17974. (org-agenda-change-all-lines newhead hdmarker)
  17975. (beginning-of-line 1))))
  17976. (defun org-agenda-date-later (arg &optional what)
  17977. "Change the date of this item to one day later."
  17978. (interactive "p")
  17979. (org-agenda-check-type t 'agenda 'timeline)
  17980. (org-agenda-check-no-diary)
  17981. (let* ((marker (or (get-text-property (point) 'org-marker)
  17982. (org-agenda-error)))
  17983. (buffer (marker-buffer marker))
  17984. (pos (marker-position marker)))
  17985. (org-with-remote-undo buffer
  17986. (with-current-buffer buffer
  17987. (widen)
  17988. (goto-char pos)
  17989. (if (not (org-at-timestamp-p))
  17990. (error "Cannot find time stamp"))
  17991. (org-timestamp-change arg (or what 'day)))
  17992. (org-agenda-show-new-time marker org-last-changed-timestamp))
  17993. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  17994. (defun org-agenda-date-earlier (arg &optional what)
  17995. "Change the date of this item to one day earlier."
  17996. (interactive "p")
  17997. (org-agenda-date-later (- arg) what))
  17998. (defun org-agenda-show-new-time (marker stamp)
  17999. "Show new date stamp via text properties."
  18000. ;; We use text properties to make this undoable
  18001. (let ((inhibit-read-only t))
  18002. (setq stamp (concat " => " stamp))
  18003. (save-excursion
  18004. (goto-char (point-max))
  18005. (while (not (bobp))
  18006. (when (equal marker (get-text-property (point) 'org-marker))
  18007. (move-to-column (- (window-width) (length stamp)) t)
  18008. (if (featurep 'xemacs)
  18009. ;; Use `duplicable' property to trigger undo recording
  18010. (let ((ex (make-extent nil nil))
  18011. (gl (make-glyph stamp)))
  18012. (set-glyph-face gl 'secondary-selection)
  18013. (set-extent-properties
  18014. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  18015. (insert-extent ex (1- (point)) (point-at-eol)))
  18016. (add-text-properties
  18017. (1- (point)) (point-at-eol)
  18018. (list 'display (org-add-props stamp nil
  18019. 'face 'secondary-selection))))
  18020. (beginning-of-line 1))
  18021. (beginning-of-line 0)))))
  18022. (defun org-agenda-date-prompt (arg)
  18023. "Change the date of this item. Date is prompted for, with default today.
  18024. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  18025. be used to request time specification in the time stamp."
  18026. (interactive "P")
  18027. (org-agenda-check-type t 'agenda 'timeline)
  18028. (org-agenda-check-no-diary)
  18029. (let* ((marker (or (get-text-property (point) 'org-marker)
  18030. (org-agenda-error)))
  18031. (buffer (marker-buffer marker))
  18032. (pos (marker-position marker)))
  18033. (org-with-remote-undo buffer
  18034. (with-current-buffer buffer
  18035. (widen)
  18036. (goto-char pos)
  18037. (if (not (org-at-timestamp-p))
  18038. (error "Cannot find time stamp"))
  18039. (org-time-stamp arg)
  18040. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  18041. (defun org-agenda-schedule (arg)
  18042. "Schedule the item at point."
  18043. (interactive "P")
  18044. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  18045. (org-agenda-check-no-diary)
  18046. (let* ((marker (or (get-text-property (point) 'org-marker)
  18047. (org-agenda-error)))
  18048. (buffer (marker-buffer marker))
  18049. (pos (marker-position marker))
  18050. (org-insert-labeled-timestamps-at-point nil)
  18051. ts)
  18052. (org-with-remote-undo buffer
  18053. (with-current-buffer buffer
  18054. (widen)
  18055. (goto-char pos)
  18056. (setq ts (org-schedule))
  18057. (message "Item scheduled for %s" ts)))))
  18058. (defun org-agenda-deadline (arg)
  18059. "Schedule the item at point."
  18060. (interactive "P")
  18061. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  18062. (org-agenda-check-no-diary)
  18063. (let* ((marker (or (get-text-property (point) 'org-marker)
  18064. (org-agenda-error)))
  18065. (buffer (marker-buffer marker))
  18066. (pos (marker-position marker))
  18067. (org-insert-labeled-timestamps-at-point nil)
  18068. ts)
  18069. (org-with-remote-undo buffer
  18070. (with-current-buffer buffer
  18071. (widen)
  18072. (goto-char pos)
  18073. (setq ts (org-deadline))
  18074. (message "Deadline for this item set to %s" ts)))))
  18075. (defun org-get-heading ()
  18076. "Return the heading of the current entry, without the stars."
  18077. (save-excursion
  18078. (org-back-to-heading t)
  18079. (if (looking-at "\\*+[ \t]+\\([^\r\n]*\\)") (match-string 1) "")))
  18080. (defun org-agenda-clock-in (&optional arg)
  18081. "Start the clock on the currently selected item."
  18082. (interactive "P")
  18083. (org-agenda-check-no-diary)
  18084. (let* ((marker (or (get-text-property (point) 'org-marker)
  18085. (org-agenda-error)))
  18086. (pos (marker-position marker)))
  18087. (org-with-remote-undo (marker-buffer marker)
  18088. (with-current-buffer (marker-buffer marker)
  18089. (widen)
  18090. (goto-char pos)
  18091. (org-clock-in)))))
  18092. (defun org-agenda-clock-out (&optional arg)
  18093. "Stop the currently running clock."
  18094. (interactive "P")
  18095. (unless (marker-buffer org-clock-marker)
  18096. (error "No running clock"))
  18097. (org-with-remote-undo (marker-buffer org-clock-marker)
  18098. (org-clock-out)))
  18099. (defun org-agenda-clock-cancel (&optional arg)
  18100. "Cancel the currently running clock."
  18101. (interactive "P")
  18102. (unless (marker-buffer org-clock-marker)
  18103. (error "No running clock"))
  18104. (org-with-remote-undo (marker-buffer org-clock-marker)
  18105. (org-clock-cancel)))
  18106. (defun org-agenda-diary-entry ()
  18107. "Make a diary entry, like the `i' command from the calendar.
  18108. All the standard commands work: block, weekly etc."
  18109. (interactive)
  18110. (org-agenda-check-type t 'agenda 'timeline)
  18111. (require 'diary-lib)
  18112. (let* ((char (progn
  18113. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  18114. (read-char-exclusive)))
  18115. (cmd (cdr (assoc char
  18116. '((?d . insert-diary-entry)
  18117. (?w . insert-weekly-diary-entry)
  18118. (?m . insert-monthly-diary-entry)
  18119. (?y . insert-yearly-diary-entry)
  18120. (?a . insert-anniversary-diary-entry)
  18121. (?b . insert-block-diary-entry)
  18122. (?c . insert-cyclic-diary-entry)))))
  18123. (oldf (symbol-function 'calendar-cursor-to-date))
  18124. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  18125. (point (point))
  18126. (mark (or (mark t) (point))))
  18127. (unless cmd
  18128. (error "No command associated with <%c>" char))
  18129. (unless (and (get-text-property point 'day)
  18130. (or (not (equal ?b char))
  18131. (get-text-property mark 'day)))
  18132. (error "Don't know which date to use for diary entry"))
  18133. ;; We implement this by hacking the `calendar-cursor-to-date' function
  18134. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  18135. (let ((calendar-mark-ring
  18136. (list (calendar-gregorian-from-absolute
  18137. (or (get-text-property mark 'day)
  18138. (get-text-property point 'day))))))
  18139. (unwind-protect
  18140. (progn
  18141. (fset 'calendar-cursor-to-date
  18142. (lambda (&optional error)
  18143. (calendar-gregorian-from-absolute
  18144. (get-text-property point 'day))))
  18145. (call-interactively cmd))
  18146. (fset 'calendar-cursor-to-date oldf)))))
  18147. (defun org-agenda-execute-calendar-command (cmd)
  18148. "Execute a calendar command from the agenda, with the date associated to
  18149. the cursor position."
  18150. (org-agenda-check-type t 'agenda 'timeline)
  18151. (require 'diary-lib)
  18152. (unless (get-text-property (point) 'day)
  18153. (error "Don't know which date to use for calendar command"))
  18154. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  18155. (point (point))
  18156. (date (calendar-gregorian-from-absolute
  18157. (get-text-property point 'day)))
  18158. ;; the following 3 vars are needed in the calendar
  18159. (displayed-day (extract-calendar-day date))
  18160. (displayed-month (extract-calendar-month date))
  18161. (displayed-year (extract-calendar-year date)))
  18162. (unwind-protect
  18163. (progn
  18164. (fset 'calendar-cursor-to-date
  18165. (lambda (&optional error)
  18166. (calendar-gregorian-from-absolute
  18167. (get-text-property point 'day))))
  18168. (call-interactively cmd))
  18169. (fset 'calendar-cursor-to-date oldf))))
  18170. (defun org-agenda-phases-of-moon ()
  18171. "Display the phases of the moon for the 3 months around the cursor date."
  18172. (interactive)
  18173. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  18174. (defun org-agenda-holidays ()
  18175. "Display the holidays for the 3 months around the cursor date."
  18176. (interactive)
  18177. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  18178. (defun org-agenda-sunrise-sunset (arg)
  18179. "Display sunrise and sunset for the cursor date.
  18180. Latitude and longitude can be specified with the variables
  18181. `calendar-latitude' and `calendar-longitude'. When called with prefix
  18182. argument, latitude and longitude will be prompted for."
  18183. (interactive "P")
  18184. (let ((calendar-longitude (if arg nil calendar-longitude))
  18185. (calendar-latitude (if arg nil calendar-latitude))
  18186. (calendar-location-name
  18187. (if arg "the given coordinates" calendar-location-name)))
  18188. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  18189. (defun org-agenda-goto-calendar ()
  18190. "Open the Emacs calendar with the date at the cursor."
  18191. (interactive)
  18192. (org-agenda-check-type t 'agenda 'timeline)
  18193. (let* ((day (or (get-text-property (point) 'day)
  18194. (error "Don't know which date to open in calendar")))
  18195. (date (calendar-gregorian-from-absolute day))
  18196. (calendar-move-hook nil)
  18197. (view-calendar-holidays-initially nil)
  18198. (view-diary-entries-initially nil))
  18199. (calendar)
  18200. (calendar-goto-date date)))
  18201. (defun org-calendar-goto-agenda ()
  18202. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  18203. This is a command that has to be installed in `calendar-mode-map'."
  18204. (interactive)
  18205. (org-agenda-list nil (calendar-absolute-from-gregorian
  18206. (calendar-cursor-to-date))
  18207. nil))
  18208. (defun org-agenda-convert-date ()
  18209. (interactive)
  18210. (org-agenda-check-type t 'agenda 'timeline)
  18211. (let ((day (get-text-property (point) 'day))
  18212. date s)
  18213. (unless day
  18214. (error "Don't know which date to convert"))
  18215. (setq date (calendar-gregorian-from-absolute day))
  18216. (setq s (concat
  18217. "Gregorian: " (calendar-date-string date) "\n"
  18218. "ISO: " (calendar-iso-date-string date) "\n"
  18219. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  18220. "Julian: " (calendar-julian-date-string date) "\n"
  18221. "Astron. JD: " (calendar-astro-date-string date)
  18222. " (Julian date number at noon UTC)\n"
  18223. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  18224. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  18225. "French: " (calendar-french-date-string date) "\n"
  18226. "Mayan: " (calendar-mayan-date-string date) "\n"
  18227. "Coptic: " (calendar-coptic-date-string date) "\n"
  18228. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  18229. "Persian: " (calendar-persian-date-string date) "\n"
  18230. "Chinese: " (calendar-chinese-date-string date) "\n"))
  18231. (with-output-to-temp-buffer "*Dates*"
  18232. (princ s))
  18233. (if (fboundp 'fit-window-to-buffer)
  18234. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  18235. ;;;; Embedded LaTeX
  18236. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  18237. "Keymap for the minor `org-cdlatex-mode'.")
  18238. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  18239. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  18240. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  18241. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  18242. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  18243. (defvar org-cdlatex-texmathp-advice-is-done nil
  18244. "Flag remembering if we have applied the advice to texmathp already.")
  18245. (define-minor-mode org-cdlatex-mode
  18246. "Toggle the minor `org-cdlatex-mode'.
  18247. This mode supports entering LaTeX environment and math in LaTeX fragments
  18248. in Org-mode.
  18249. \\{org-cdlatex-mode-map}"
  18250. nil " OCDL" nil
  18251. (when org-cdlatex-mode (require 'cdlatex))
  18252. (unless org-cdlatex-texmathp-advice-is-done
  18253. (setq org-cdlatex-texmathp-advice-is-done t)
  18254. (defadvice texmathp (around org-math-always-on activate)
  18255. "Always return t in org-mode buffers.
  18256. This is because we want to insert math symbols without dollars even outside
  18257. the LaTeX math segments. If Orgmode thinks that point is actually inside
  18258. en embedded LaTeX fragement, let texmathp do its job.
  18259. \\[org-cdlatex-mode-map]"
  18260. (interactive)
  18261. (let (p)
  18262. (cond
  18263. ((not (org-mode-p)) ad-do-it)
  18264. ((eq this-command 'cdlatex-math-symbol)
  18265. (setq ad-return-value t
  18266. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  18267. (t
  18268. (let ((p (org-inside-LaTeX-fragment-p)))
  18269. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  18270. (setq ad-return-value t
  18271. texmathp-why '("Org-mode embedded math" . 0))
  18272. (if p ad-do-it)))))))))
  18273. (defun turn-on-org-cdlatex ()
  18274. "Unconditionally turn on `org-cdlatex-mode'."
  18275. (org-cdlatex-mode 1))
  18276. (defun org-inside-LaTeX-fragment-p ()
  18277. "Test if point is inside a LaTeX fragment.
  18278. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  18279. sequence appearing also before point.
  18280. Even though the matchers for math are configurable, this function assumes
  18281. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  18282. delimiters are skipped when they have been removed by customization.
  18283. The return value is nil, or a cons cell with the delimiter and
  18284. and the position of this delimiter.
  18285. This function does a reasonably good job, but can locally be fooled by
  18286. for example currency specifications. For example it will assume being in
  18287. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  18288. fragments that are properly closed, but during editing, we have to live
  18289. with the uncertainty caused by missing closing delimiters. This function
  18290. looks only before point, not after."
  18291. (catch 'exit
  18292. (let ((pos (point))
  18293. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  18294. (lim (progn
  18295. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  18296. (point)))
  18297. dd-on str (start 0) m re)
  18298. (goto-char pos)
  18299. (when dodollar
  18300. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  18301. re (nth 1 (assoc "$" org-latex-regexps)))
  18302. (while (string-match re str start)
  18303. (cond
  18304. ((= (match-end 0) (length str))
  18305. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  18306. ((= (match-end 0) (- (length str) 5))
  18307. (throw 'exit nil))
  18308. (t (setq start (match-end 0))))))
  18309. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  18310. (goto-char pos)
  18311. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  18312. (and (match-beginning 2) (throw 'exit nil))
  18313. ;; count $$
  18314. (while (re-search-backward "\\$\\$" lim t)
  18315. (setq dd-on (not dd-on)))
  18316. (goto-char pos)
  18317. (if dd-on (cons "$$" m))))))
  18318. (defun org-try-cdlatex-tab ()
  18319. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  18320. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  18321. - inside a LaTeX fragment, or
  18322. - after the first word in a line, where an abbreviation expansion could
  18323. insert a LaTeX environment."
  18324. (when org-cdlatex-mode
  18325. (cond
  18326. ((save-excursion
  18327. (skip-chars-backward "a-zA-Z0-9*")
  18328. (skip-chars-backward " \t")
  18329. (bolp))
  18330. (cdlatex-tab) t)
  18331. ((org-inside-LaTeX-fragment-p)
  18332. (cdlatex-tab) t)
  18333. (t nil))))
  18334. (defun org-cdlatex-underscore-caret (&optional arg)
  18335. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  18336. Revert to the normal definition outside of these fragments."
  18337. (interactive "P")
  18338. (if (org-inside-LaTeX-fragment-p)
  18339. (call-interactively 'cdlatex-sub-superscript)
  18340. (let (org-cdlatex-mode)
  18341. (call-interactively (key-binding (vector last-input-event))))))
  18342. (defun org-cdlatex-math-modify (&optional arg)
  18343. "Execute `cdlatex-math-modify' in LaTeX fragments.
  18344. Revert to the normal definition outside of these fragments."
  18345. (interactive "P")
  18346. (if (org-inside-LaTeX-fragment-p)
  18347. (call-interactively 'cdlatex-math-modify)
  18348. (let (org-cdlatex-mode)
  18349. (call-interactively (key-binding (vector last-input-event))))))
  18350. (defvar org-latex-fragment-image-overlays nil
  18351. "List of overlays carrying the images of latex fragments.")
  18352. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  18353. (defun org-remove-latex-fragment-image-overlays ()
  18354. "Remove all overlays with LaTeX fragment images in current buffer."
  18355. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  18356. (setq org-latex-fragment-image-overlays nil))
  18357. (defun org-preview-latex-fragment (&optional subtree)
  18358. "Preview the LaTeX fragment at point, or all locally or globally.
  18359. If the cursor is in a LaTeX fragment, create the image and overlay
  18360. it over the source code. If there is no fragment at point, display
  18361. all fragments in the current text, from one headline to the next. With
  18362. prefix SUBTREE, display all fragments in the current subtree. With a
  18363. double prefix `C-u C-u', or when the cursor is before the first headline,
  18364. display all fragments in the buffer.
  18365. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  18366. (interactive "P")
  18367. (org-remove-latex-fragment-image-overlays)
  18368. (save-excursion
  18369. (save-restriction
  18370. (let (beg end at msg)
  18371. (cond
  18372. ((or (equal subtree '(16))
  18373. (not (save-excursion
  18374. (re-search-backward (concat "^" outline-regexp) nil t))))
  18375. (setq beg (point-min) end (point-max)
  18376. msg "Creating images for buffer...%s"))
  18377. ((equal subtree '(4))
  18378. (org-back-to-heading)
  18379. (setq beg (point) end (org-end-of-subtree t)
  18380. msg "Creating images for subtree...%s"))
  18381. (t
  18382. (if (setq at (org-inside-LaTeX-fragment-p))
  18383. (goto-char (max (point-min) (- (cdr at) 2)))
  18384. (org-back-to-heading))
  18385. (setq beg (point) end (progn (outline-next-heading) (point))
  18386. msg (if at "Creating image...%s"
  18387. "Creating images for entry...%s"))))
  18388. (message msg "")
  18389. (narrow-to-region beg end)
  18390. (goto-char beg)
  18391. (org-format-latex
  18392. (concat "ltxpng/" (file-name-sans-extension
  18393. (file-name-nondirectory
  18394. buffer-file-name)))
  18395. default-directory 'overlays msg at 'forbuffer)
  18396. (message msg "done. Use `C-c C-c' to remove images.")))))
  18397. (defvar org-latex-regexps
  18398. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  18399. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  18400. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  18401. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  18402. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  18403. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  18404. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  18405. "Regular expressions for matching embedded LaTeX.")
  18406. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  18407. "Replace LaTeX fragments with links to an image, and produce images."
  18408. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  18409. (let* ((prefixnodir (file-name-nondirectory prefix))
  18410. (absprefix (expand-file-name prefix dir))
  18411. (todir (file-name-directory absprefix))
  18412. (opt org-format-latex-options)
  18413. (matchers (plist-get opt :matchers))
  18414. (re-list org-latex-regexps)
  18415. (cnt 0) txt link beg end re e checkdir
  18416. m n block linkfile movefile ov)
  18417. ;; Check if there are old images files with this prefix, and remove them
  18418. (when (file-directory-p todir)
  18419. (mapc 'delete-file
  18420. (directory-files
  18421. todir 'full
  18422. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  18423. ;; Check the different regular expressions
  18424. (while (setq e (pop re-list))
  18425. (setq m (car e) re (nth 1 e) n (nth 2 e)
  18426. block (if (nth 3 e) "\n\n" ""))
  18427. (when (member m matchers)
  18428. (goto-char (point-min))
  18429. (while (re-search-forward re nil t)
  18430. (when (or (not at) (equal (cdr at) (match-beginning n)))
  18431. (setq txt (match-string n)
  18432. beg (match-beginning n) end (match-end n)
  18433. cnt (1+ cnt)
  18434. linkfile (format "%s_%04d.png" prefix cnt)
  18435. movefile (format "%s_%04d.png" absprefix cnt)
  18436. link (concat block "[[file:" linkfile "]]" block))
  18437. (if msg (message msg cnt))
  18438. (goto-char beg)
  18439. (unless checkdir ; make sure the directory exists
  18440. (setq checkdir t)
  18441. (or (file-directory-p todir) (make-directory todir)))
  18442. (org-create-formula-image
  18443. txt movefile opt forbuffer)
  18444. (if overlays
  18445. (progn
  18446. (setq ov (org-make-overlay beg end))
  18447. (if (featurep 'xemacs)
  18448. (progn
  18449. (org-overlay-put ov 'invisible t)
  18450. (org-overlay-put
  18451. ov 'end-glyph
  18452. (make-glyph (vector 'png :file movefile))))
  18453. (org-overlay-put
  18454. ov 'display
  18455. (list 'image :type 'png :file movefile :ascent 'center)))
  18456. (push ov org-latex-fragment-image-overlays)
  18457. (goto-char end))
  18458. (delete-region beg end)
  18459. (insert link))))))))
  18460. ;; This function borrows from Ganesh Swami's latex2png.el
  18461. (defun org-create-formula-image (string tofile options buffer)
  18462. (let* ((tmpdir (if (featurep 'xemacs)
  18463. (temp-directory)
  18464. temporary-file-directory))
  18465. (texfilebase (make-temp-name
  18466. (expand-file-name "orgtex" tmpdir)))
  18467. (texfile (concat texfilebase ".tex"))
  18468. (dvifile (concat texfilebase ".dvi"))
  18469. (pngfile (concat texfilebase ".png"))
  18470. (fnh (face-attribute 'default :height nil))
  18471. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  18472. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  18473. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  18474. "Black"))
  18475. (bg (or (plist-get options (if buffer :background :html-background))
  18476. "Transparent")))
  18477. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  18478. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  18479. (with-temp-file texfile
  18480. (insert org-format-latex-header
  18481. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  18482. (let ((dir default-directory))
  18483. (condition-case nil
  18484. (progn
  18485. (cd tmpdir)
  18486. (call-process "latex" nil nil nil texfile))
  18487. (error nil))
  18488. (cd dir))
  18489. (if (not (file-exists-p dvifile))
  18490. (progn (message "Failed to create dvi file from %s" texfile) nil)
  18491. (call-process "dvipng" nil nil nil
  18492. "-E" "-fg" fg "-bg" bg
  18493. "-D" dpi
  18494. ;;"-x" scale "-y" scale
  18495. "-T" "tight"
  18496. "-o" pngfile
  18497. dvifile)
  18498. (if (not (file-exists-p pngfile))
  18499. (progn (message "Failed to create png file from %s" texfile) nil)
  18500. ;; Use the requested file name and clean up
  18501. (copy-file pngfile tofile 'replace)
  18502. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  18503. (delete-file (concat texfilebase e)))
  18504. pngfile))))
  18505. (defun org-dvipng-color (attr)
  18506. "Return an rgb color specification for dvipng."
  18507. (apply 'format "rgb %s %s %s"
  18508. (mapcar 'org-normalize-color
  18509. (color-values (face-attribute 'default attr nil)))))
  18510. (defun org-normalize-color (value)
  18511. "Return string to be used as color value for an RGB component."
  18512. (format "%g" (/ value 65535.0)))
  18513. ;;;; Exporting
  18514. ;;; Variables, constants, and parameter plists
  18515. (defconst org-level-max 20)
  18516. (defvar org-export-html-preamble nil
  18517. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  18518. (defvar org-export-html-postamble nil
  18519. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  18520. (defvar org-export-html-auto-preamble t
  18521. "Should default preamble be inserted? Set by publishing functions.")
  18522. (defvar org-export-html-auto-postamble t
  18523. "Should default postamble be inserted? Set by publishing functions.")
  18524. (defvar org-current-export-file nil) ; dynamically scoped parameter
  18525. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  18526. (defconst org-export-plist-vars
  18527. '((:language . org-export-default-language)
  18528. (:customtime . org-display-custom-times)
  18529. (:headline-levels . org-export-headline-levels)
  18530. (:section-numbers . org-export-with-section-numbers)
  18531. (:table-of-contents . org-export-with-toc)
  18532. (:preserve-breaks . org-export-preserve-breaks)
  18533. (:archived-trees . org-export-with-archived-trees)
  18534. (:emphasize . org-export-with-emphasize)
  18535. (:sub-superscript . org-export-with-sub-superscripts)
  18536. (:footnotes . org-export-with-footnotes)
  18537. (:property-drawer . org-export-with-property-drawer)
  18538. (:TeX-macros . org-export-with-TeX-macros)
  18539. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  18540. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  18541. (:fixed-width . org-export-with-fixed-width)
  18542. (:timestamps . org-export-with-timestamps)
  18543. (:author-info . org-export-author-info)
  18544. (:time-stamp-file . org-export-time-stamp-file)
  18545. (:tables . org-export-with-tables)
  18546. (:table-auto-headline . org-export-highlight-first-table-line)
  18547. (:style . org-export-html-style)
  18548. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  18549. (:convert-org-links . org-export-html-link-org-files-as-html)
  18550. (:inline-images . org-export-html-inline-images)
  18551. (:expand-quoted-html . org-export-html-expand)
  18552. (:timestamp . org-export-html-with-timestamp)
  18553. (:publishing-directory . org-export-publishing-directory)
  18554. (:preamble . org-export-html-preamble)
  18555. (:postamble . org-export-html-postamble)
  18556. (:auto-preamble . org-export-html-auto-preamble)
  18557. (:auto-postamble . org-export-html-auto-postamble)
  18558. (:author . user-full-name)
  18559. (:email . user-mail-address)))
  18560. (defun org-default-export-plist ()
  18561. "Return the property list with default settings for the export variables."
  18562. (let ((l org-export-plist-vars) rtn e)
  18563. (while (setq e (pop l))
  18564. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  18565. rtn))
  18566. (defun org-infile-export-plist ()
  18567. "Return the property list with file-local settings for export."
  18568. (save-excursion
  18569. (goto-char 0)
  18570. (let ((re (org-make-options-regexp
  18571. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  18572. p key val text options)
  18573. (while (re-search-forward re nil t)
  18574. (setq key (org-match-string-no-properties 1)
  18575. val (org-match-string-no-properties 2))
  18576. (cond
  18577. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  18578. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  18579. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  18580. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  18581. ((string-equal key "TEXT")
  18582. (setq text (if text (concat text "\n" val) val)))
  18583. ((string-equal key "OPTIONS") (setq options val))))
  18584. (setq p (plist-put p :text text))
  18585. (when options
  18586. (let ((op '(("H" . :headline-levels)
  18587. ("num" . :section-numbers)
  18588. ("toc" . :table-of-contents)
  18589. ("\\n" . :preserve-breaks)
  18590. ("@" . :expand-quoted-html)
  18591. (":" . :fixed-width)
  18592. ("|" . :tables)
  18593. ("^" . :sub-superscript)
  18594. ("f" . :footnotes)
  18595. ("p" . :property-drawer)
  18596. ("*" . :emphasize)
  18597. ("TeX" . :TeX-macros)
  18598. ("LaTeX" . :LaTeX-fragments)
  18599. ("skip" . :skip-before-1st-heading)
  18600. ("author" . :author-info)
  18601. ("timestamp" . :time-stamp-file)))
  18602. o)
  18603. (while (setq o (pop op))
  18604. (if (string-match (concat (regexp-quote (car o))
  18605. ":\\([^ \t\n\r;,.]*\\)")
  18606. options)
  18607. (setq p (plist-put p (cdr o)
  18608. (car (read-from-string
  18609. (match-string 1 options)))))))))
  18610. p)))
  18611. (defun org-export-directory (type plist)
  18612. (let* ((val (plist-get plist :publishing-directory))
  18613. (dir (if (listp val)
  18614. (or (cdr (assoc type val)) ".")
  18615. val)))
  18616. dir))
  18617. (defun org-skip-comments (lines)
  18618. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  18619. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  18620. (re2 "^\\(\\*+\\)[ \t\n\r]")
  18621. (case-fold-search nil)
  18622. rtn line level)
  18623. (while (setq line (pop lines))
  18624. (cond
  18625. ((and (string-match re1 line)
  18626. (setq level (- (match-end 1) (match-beginning 1))))
  18627. ;; Beginning of a COMMENT subtree. Skip it.
  18628. (while (and (setq line (pop lines))
  18629. (or (not (string-match re2 line))
  18630. (> (- (match-end 1) (match-beginning 1)) level))))
  18631. (setq lines (cons line lines)))
  18632. ((string-match "^#" line)
  18633. ;; an ordinary comment line
  18634. )
  18635. ((and org-export-table-remove-special-lines
  18636. (string-match "^[ \t]*|" line)
  18637. (or (string-match "^[ \t]*| *[!_^] *|" line)
  18638. (and (string-match "| *<[0-9]+> *|" line)
  18639. (not (string-match "| *[^ <|]" line)))))
  18640. ;; a special table line that should be removed
  18641. )
  18642. (t (setq rtn (cons line rtn)))))
  18643. (nreverse rtn)))
  18644. (defun org-export (&optional arg)
  18645. (interactive)
  18646. (let ((help "[t] insert the export option template
  18647. \[v] limit export to visible part of outline tree
  18648. \[a] export as ASCII
  18649. \[h] export as HTML
  18650. \[H] export as HTML to temporary buffer
  18651. \[R] export region as HTML
  18652. \[b] export as HTML and browse immediately
  18653. \[x] export as XOXO
  18654. \[l] export as LaTeX
  18655. \[L] export as LaTeX to temporary buffer
  18656. \[i] export current file as iCalendar file
  18657. \[I] export all agenda files as iCalendar files
  18658. \[c] export agenda files into combined iCalendar file
  18659. \[F] publish current file
  18660. \[P] publish current project
  18661. \[X] publish... (project will be prompted for)
  18662. \[A] publish all projects")
  18663. (cmds
  18664. '((?t . org-insert-export-options-template)
  18665. (?v . org-export-visible)
  18666. (?a . org-export-as-ascii)
  18667. (?h . org-export-as-html)
  18668. (?b . org-export-as-html-and-open)
  18669. (?H . org-export-as-html-to-buffer)
  18670. (?R . org-export-region-as-html)
  18671. (?x . org-export-as-xoxo)
  18672. (?l . org-export-as-latex)
  18673. (?L . org-export-as-latex-to-buffer)
  18674. (?i . org-export-icalendar-this-file)
  18675. (?I . org-export-icalendar-all-agenda-files)
  18676. (?c . org-export-icalendar-combine-agenda-files)
  18677. (?F . org-publish-current-file)
  18678. (?P . org-publish-current-project)
  18679. (?X . org-publish)
  18680. (?A . org-publish-all)))
  18681. r1 r2 ass)
  18682. (save-window-excursion
  18683. (delete-other-windows)
  18684. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  18685. (princ help))
  18686. (message "Select command: ")
  18687. (setq r1 (read-char-exclusive)))
  18688. (setq r2 (if (< r1 27) (+ r1 96) r1))
  18689. (if (setq ass (assq r2 cmds))
  18690. (call-interactively (cdr ass))
  18691. (error "No command associated with key %c" r1))))
  18692. (defconst org-html-entities
  18693. '(("nbsp")
  18694. ("iexcl")
  18695. ("cent")
  18696. ("pound")
  18697. ("curren")
  18698. ("yen")
  18699. ("brvbar")
  18700. ("vert" . "&#124;")
  18701. ("sect")
  18702. ("uml")
  18703. ("copy")
  18704. ("ordf")
  18705. ("laquo")
  18706. ("not")
  18707. ("shy")
  18708. ("reg")
  18709. ("macr")
  18710. ("deg")
  18711. ("plusmn")
  18712. ("sup2")
  18713. ("sup3")
  18714. ("acute")
  18715. ("micro")
  18716. ("para")
  18717. ("middot")
  18718. ("odot"."o")
  18719. ("star"."*")
  18720. ("cedil")
  18721. ("sup1")
  18722. ("ordm")
  18723. ("raquo")
  18724. ("frac14")
  18725. ("frac12")
  18726. ("frac34")
  18727. ("iquest")
  18728. ("Agrave")
  18729. ("Aacute")
  18730. ("Acirc")
  18731. ("Atilde")
  18732. ("Auml")
  18733. ("Aring") ("AA"."&Aring;")
  18734. ("AElig")
  18735. ("Ccedil")
  18736. ("Egrave")
  18737. ("Eacute")
  18738. ("Ecirc")
  18739. ("Euml")
  18740. ("Igrave")
  18741. ("Iacute")
  18742. ("Icirc")
  18743. ("Iuml")
  18744. ("ETH")
  18745. ("Ntilde")
  18746. ("Ograve")
  18747. ("Oacute")
  18748. ("Ocirc")
  18749. ("Otilde")
  18750. ("Ouml")
  18751. ("times")
  18752. ("Oslash")
  18753. ("Ugrave")
  18754. ("Uacute")
  18755. ("Ucirc")
  18756. ("Uuml")
  18757. ("Yacute")
  18758. ("THORN")
  18759. ("szlig")
  18760. ("agrave")
  18761. ("aacute")
  18762. ("acirc")
  18763. ("atilde")
  18764. ("auml")
  18765. ("aring")
  18766. ("aelig")
  18767. ("ccedil")
  18768. ("egrave")
  18769. ("eacute")
  18770. ("ecirc")
  18771. ("euml")
  18772. ("igrave")
  18773. ("iacute")
  18774. ("icirc")
  18775. ("iuml")
  18776. ("eth")
  18777. ("ntilde")
  18778. ("ograve")
  18779. ("oacute")
  18780. ("ocirc")
  18781. ("otilde")
  18782. ("ouml")
  18783. ("divide")
  18784. ("oslash")
  18785. ("ugrave")
  18786. ("uacute")
  18787. ("ucirc")
  18788. ("uuml")
  18789. ("yacute")
  18790. ("thorn")
  18791. ("yuml")
  18792. ("fnof")
  18793. ("Alpha")
  18794. ("Beta")
  18795. ("Gamma")
  18796. ("Delta")
  18797. ("Epsilon")
  18798. ("Zeta")
  18799. ("Eta")
  18800. ("Theta")
  18801. ("Iota")
  18802. ("Kappa")
  18803. ("Lambda")
  18804. ("Mu")
  18805. ("Nu")
  18806. ("Xi")
  18807. ("Omicron")
  18808. ("Pi")
  18809. ("Rho")
  18810. ("Sigma")
  18811. ("Tau")
  18812. ("Upsilon")
  18813. ("Phi")
  18814. ("Chi")
  18815. ("Psi")
  18816. ("Omega")
  18817. ("alpha")
  18818. ("beta")
  18819. ("gamma")
  18820. ("delta")
  18821. ("epsilon")
  18822. ("varepsilon"."&epsilon;")
  18823. ("zeta")
  18824. ("eta")
  18825. ("theta")
  18826. ("iota")
  18827. ("kappa")
  18828. ("lambda")
  18829. ("mu")
  18830. ("nu")
  18831. ("xi")
  18832. ("omicron")
  18833. ("pi")
  18834. ("rho")
  18835. ("sigmaf") ("varsigma"."&sigmaf;")
  18836. ("sigma")
  18837. ("tau")
  18838. ("upsilon")
  18839. ("phi")
  18840. ("chi")
  18841. ("psi")
  18842. ("omega")
  18843. ("thetasym") ("vartheta"."&thetasym;")
  18844. ("upsih")
  18845. ("piv")
  18846. ("bull") ("bullet"."&bull;")
  18847. ("hellip") ("dots"."&hellip;")
  18848. ("prime")
  18849. ("Prime")
  18850. ("oline")
  18851. ("frasl")
  18852. ("weierp")
  18853. ("image")
  18854. ("real")
  18855. ("trade")
  18856. ("alefsym")
  18857. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  18858. ("uarr") ("uparrow"."&uarr;")
  18859. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  18860. ("darr")("downarrow"."&darr;")
  18861. ("harr") ("leftrightarrow"."&harr;")
  18862. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  18863. ("lArr") ("Leftarrow"."&lArr;")
  18864. ("uArr") ("Uparrow"."&uArr;")
  18865. ("rArr") ("Rightarrow"."&rArr;")
  18866. ("dArr") ("Downarrow"."&dArr;")
  18867. ("hArr") ("Leftrightarrow"."&hArr;")
  18868. ("forall")
  18869. ("part") ("partial"."&part;")
  18870. ("exist") ("exists"."&exist;")
  18871. ("empty") ("emptyset"."&empty;")
  18872. ("nabla")
  18873. ("isin") ("in"."&isin;")
  18874. ("notin")
  18875. ("ni")
  18876. ("prod")
  18877. ("sum")
  18878. ("minus")
  18879. ("lowast") ("ast"."&lowast;")
  18880. ("radic")
  18881. ("prop") ("proptp"."&prop;")
  18882. ("infin") ("infty"."&infin;")
  18883. ("ang") ("angle"."&ang;")
  18884. ("and") ("vee"."&and;")
  18885. ("or") ("wedge"."&or;")
  18886. ("cap")
  18887. ("cup")
  18888. ("int")
  18889. ("there4")
  18890. ("sim")
  18891. ("cong") ("simeq"."&cong;")
  18892. ("asymp")("approx"."&asymp;")
  18893. ("ne") ("neq"."&ne;")
  18894. ("equiv")
  18895. ("le")
  18896. ("ge")
  18897. ("sub") ("subset"."&sub;")
  18898. ("sup") ("supset"."&sup;")
  18899. ("nsub")
  18900. ("sube")
  18901. ("supe")
  18902. ("oplus")
  18903. ("otimes")
  18904. ("perp")
  18905. ("sdot") ("cdot"."&sdot;")
  18906. ("lceil")
  18907. ("rceil")
  18908. ("lfloor")
  18909. ("rfloor")
  18910. ("lang")
  18911. ("rang")
  18912. ("loz") ("Diamond"."&loz;")
  18913. ("spades") ("spadesuit"."&spades;")
  18914. ("clubs") ("clubsuit"."&clubs;")
  18915. ("hearts") ("diamondsuit"."&hearts;")
  18916. ("diams") ("diamondsuit"."&diams;")
  18917. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  18918. ("quot")
  18919. ("amp")
  18920. ("lt")
  18921. ("gt")
  18922. ("OElig")
  18923. ("oelig")
  18924. ("Scaron")
  18925. ("scaron")
  18926. ("Yuml")
  18927. ("circ")
  18928. ("tilde")
  18929. ("ensp")
  18930. ("emsp")
  18931. ("thinsp")
  18932. ("zwnj")
  18933. ("zwj")
  18934. ("lrm")
  18935. ("rlm")
  18936. ("ndash")
  18937. ("mdash")
  18938. ("lsquo")
  18939. ("rsquo")
  18940. ("sbquo")
  18941. ("ldquo")
  18942. ("rdquo")
  18943. ("bdquo")
  18944. ("dagger")
  18945. ("Dagger")
  18946. ("permil")
  18947. ("lsaquo")
  18948. ("rsaquo")
  18949. ("euro")
  18950. ("arccos"."arccos")
  18951. ("arcsin"."arcsin")
  18952. ("arctan"."arctan")
  18953. ("arg"."arg")
  18954. ("cos"."cos")
  18955. ("cosh"."cosh")
  18956. ("cot"."cot")
  18957. ("coth"."coth")
  18958. ("csc"."csc")
  18959. ("deg"."deg")
  18960. ("det"."det")
  18961. ("dim"."dim")
  18962. ("exp"."exp")
  18963. ("gcd"."gcd")
  18964. ("hom"."hom")
  18965. ("inf"."inf")
  18966. ("ker"."ker")
  18967. ("lg"."lg")
  18968. ("lim"."lim")
  18969. ("liminf"."liminf")
  18970. ("limsup"."limsup")
  18971. ("ln"."ln")
  18972. ("log"."log")
  18973. ("max"."max")
  18974. ("min"."min")
  18975. ("Pr"."Pr")
  18976. ("sec"."sec")
  18977. ("sin"."sin")
  18978. ("sinh"."sinh")
  18979. ("sup"."sup")
  18980. ("tan"."tan")
  18981. ("tanh"."tanh")
  18982. )
  18983. "Entities for TeX->HTML translation.
  18984. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  18985. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  18986. In that case, \"\\ent\" will be translated to \"&other;\".
  18987. The list contains HTML entities for Latin-1, Greek and other symbols.
  18988. It is supplemented by a number of commonly used TeX macros with appropriate
  18989. translations. There is currently no way for users to extend this.")
  18990. ;;; General functions for all backends
  18991. (defun org-cleaned-string-for-export (string &rest parameters)
  18992. "Cleanup a buffer STRING so that links can be created safely."
  18993. (interactive)
  18994. (let* ((re-radio (and org-target-link-regexp
  18995. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  18996. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  18997. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  18998. (re-archive (concat ":" org-archive-tag ":"))
  18999. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  19000. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  19001. (htmlp (plist-get parameters :for-html))
  19002. (asciip (plist-get parameters :for-ascii))
  19003. (latexp (plist-get parameters :for-LaTeX))
  19004. (commentsp (plist-get parameters :comments))
  19005. (archived-trees (plist-get parameters :archived-trees))
  19006. (inhibit-read-only t)
  19007. (outline-regexp "\\*+ ")
  19008. a b xx
  19009. rtn p)
  19010. (with-current-buffer (get-buffer-create " org-mode-tmp")
  19011. (erase-buffer)
  19012. (insert string)
  19013. ;; Remove license-to-kill stuff
  19014. (while (setq p (text-property-any (point-min) (point-max)
  19015. :org-license-to-kill t))
  19016. (delete-region p (next-single-property-change p :org-license-to-kill)))
  19017. (let ((org-inhibit-startup t)) (org-mode))
  19018. (untabify (point-min) (point-max))
  19019. ;; Get the correct stuff before the first headline
  19020. (when (plist-get parameters :skip-before-1st-heading)
  19021. (goto-char (point-min))
  19022. (when (re-search-forward "^\\*+[ \t]" nil t)
  19023. (delete-region (point-min) (match-beginning 0))
  19024. (goto-char (point-min))
  19025. (insert "\n")))
  19026. (when (plist-get parameters :add-text)
  19027. (goto-char (point-min))
  19028. (insert (plist-get parameters :add-text) "\n"))
  19029. ;; Get rid of archived trees
  19030. (when (not (eq archived-trees t))
  19031. (goto-char (point-min))
  19032. (while (re-search-forward re-archive nil t)
  19033. (if (not (org-on-heading-p t))
  19034. (org-end-of-subtree t)
  19035. (beginning-of-line 1)
  19036. (setq a (if archived-trees
  19037. (1+ (point-at-eol)) (point))
  19038. b (org-end-of-subtree t))
  19039. (if (> b a) (delete-region a b)))))
  19040. ;; Get rid of property drawers
  19041. (unless org-export-with-property-drawer
  19042. (goto-char (point-min))
  19043. (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t)
  19044. (replace-match "")))
  19045. ;; Find targets in comments and move them out of comments,
  19046. ;; but mark them as targets that should be invisible
  19047. (goto-char (point-min))
  19048. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  19049. (replace-match "\\1(INVISIBLE)"))
  19050. ;; Protect backend specific stuff, throw away the others.
  19051. (goto-char (point-min))
  19052. (let ((formatters
  19053. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  19054. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  19055. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  19056. fmt)
  19057. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  19058. (add-text-properties (match-beginning 0) (match-end 0)
  19059. '(org-protected t)))
  19060. (while formatters
  19061. (setq fmt (pop formatters))
  19062. (when (car fmt)
  19063. (goto-char (point-min))
  19064. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  19065. ":[ \t]*\\(.*\\)") nil t)
  19066. (replace-match "\\1" t)
  19067. (add-text-properties
  19068. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  19069. '(org-protected t))))
  19070. (goto-char (point-min))
  19071. (while (re-search-forward
  19072. (concat "^#\\+"
  19073. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  19074. (cadddr fmt) "\\>.*\n?") nil t)
  19075. (if (car fmt)
  19076. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  19077. '(org-protected t))
  19078. (delete-region (match-beginning 0) (match-end 0))))))
  19079. ;; Protect quoted subtrees
  19080. (goto-char (point-min))
  19081. (while (re-search-forward re-quote nil t)
  19082. (goto-char (match-beginning 0))
  19083. (end-of-line 1)
  19084. (add-text-properties (point) (org-end-of-subtree t)
  19085. '(org-protected t)))
  19086. ;; Remove subtrees that are commented
  19087. (goto-char (point-min))
  19088. (while (re-search-forward re-commented nil t)
  19089. (goto-char (match-beginning 0))
  19090. (delete-region (point) (org-end-of-subtree t)))
  19091. ;; Remove special table lines
  19092. (when org-export-table-remove-special-lines
  19093. (goto-char (point-min))
  19094. (while (re-search-forward "^[ \t]*|" nil t)
  19095. (beginning-of-line 1)
  19096. (if (or (looking-at "[ \t]*| *[!_^] *|")
  19097. (and (looking-at ".*?| *<[0-9]+> *|")
  19098. (not (looking-at ".*?| *[^ <|]"))))
  19099. (delete-region (max (point-min) (1- (point-at-bol)))
  19100. (point-at-eol))
  19101. (end-of-line 1))))
  19102. ;; Specific LaTeX stuff
  19103. (when latexp
  19104. (require 'org-export-latex nil t)
  19105. (org-export-latex-cleaned-string))
  19106. ;; Specific HTML stuff
  19107. (when htmlp
  19108. ;; Convert LaTeX fragments to images
  19109. (when (plist-get parameters :LaTeX-fragments)
  19110. (org-format-latex
  19111. (concat "ltxpng/" (file-name-sans-extension
  19112. (file-name-nondirectory
  19113. org-current-export-file)))
  19114. org-current-export-dir nil "Creating LaTeX image %s"))
  19115. (message "Exporting..."))
  19116. ;; Remove or replace comments
  19117. ;; FIXME: Does LaTeX export take care of its own comments?
  19118. ;; If :comments is set, use this char for commenting out comments and
  19119. ;; protect them. otherwise delete them
  19120. (goto-char (point-min))
  19121. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  19122. (if commentsp
  19123. (progn (add-text-properties
  19124. (match-beginning 0) (match-end 0) '(org-protected t))
  19125. (replace-match (format commentsp (match-string 1)) t t))
  19126. (replace-match "")))
  19127. ;; Find matches for radio targets and turn them into internal links
  19128. (goto-char (point-min))
  19129. (when re-radio
  19130. (while (re-search-forward re-radio nil t)
  19131. (org-if-unprotected
  19132. (replace-match "\\1[[\\2]]"))))
  19133. ;; Find all links that contain a newline and put them into a single line
  19134. (goto-char (point-min))
  19135. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  19136. (org-if-unprotected
  19137. (replace-match "\\1 \\3")
  19138. (goto-char (match-beginning 0))))
  19139. ;; Normalize links: Convert angle and plain links into bracket links
  19140. ;; Expand link abbreviations
  19141. (goto-char (point-min))
  19142. (while (re-search-forward re-plain-link nil t)
  19143. (goto-char (1- (match-end 0)))
  19144. (org-if-unprotected
  19145. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19146. ":" (match-string 3) "]]")))
  19147. ;; added 'org-link face to links
  19148. (put-text-property 0 (length s) 'face 'org-link s)
  19149. (replace-match s t t))))
  19150. (goto-char (point-min))
  19151. (while (re-search-forward re-angle-link nil t)
  19152. (goto-char (1- (match-end 0)))
  19153. (org-if-unprotected
  19154. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  19155. ":" (match-string 3) "]]")))
  19156. (put-text-property 0 (length s) 'face 'org-link s)
  19157. (replace-match s t t))))
  19158. (goto-char (point-min))
  19159. (while (re-search-forward org-bracket-link-regexp nil t)
  19160. (org-if-unprotected
  19161. (let* ((s (concat "[[" (setq xx (save-match-data
  19162. (org-link-expand-abbrev (match-string 1))))
  19163. "]"
  19164. (if (match-end 3)
  19165. (match-string 2)
  19166. (concat "[" xx "]"))
  19167. "]")))
  19168. (put-text-property 0 (length s) 'face 'org-link s)
  19169. (replace-match s t t))))
  19170. ;; Find multiline emphasis and put them into single line
  19171. (when (plist-get parameters :emph-multiline)
  19172. (goto-char (point-min))
  19173. (while (re-search-forward org-emph-re nil t)
  19174. (if (not (= (char-after (match-beginning 3))
  19175. (char-after (match-beginning 4))))
  19176. (org-if-unprotected
  19177. (subst-char-in-region (match-beginning 0) (match-end 0)
  19178. ?\n ?\ t)
  19179. (goto-char (1- (match-end 0))))
  19180. (goto-char (1+ (match-beginning 0))))))
  19181. (setq rtn (buffer-string)))
  19182. (kill-buffer " org-mode-tmp")
  19183. rtn))
  19184. (defun org-export-grab-title-from-buffer ()
  19185. "Get a title for the current document, from looking at the buffer."
  19186. (let ((inhibit-read-only t))
  19187. (save-excursion
  19188. (goto-char (point-min))
  19189. (let ((end (save-excursion (outline-next-heading) (point))))
  19190. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  19191. ;; Mark the line so that it will not be exported as normal text.
  19192. (org-unmodified
  19193. (add-text-properties (match-beginning 0) (match-end 0)
  19194. (list :org-license-to-kill t)))
  19195. ;; Return the title string
  19196. (org-trim (match-string 0)))))))
  19197. (defun org-export-get-title-from-subtree ()
  19198. "Return subtree title and exclude it from export."
  19199. (let (title (m (mark)))
  19200. (save-excursion
  19201. (goto-char (region-beginning))
  19202. (when (and (org-at-heading-p)
  19203. (>= (org-end-of-subtree t t) (region-end)))
  19204. ;; This is a subtree, we take the title from the first heading
  19205. (goto-char (region-beginning))
  19206. (looking-at org-todo-line-regexp)
  19207. (setq title (match-string 3))
  19208. (org-unmodified
  19209. (add-text-properties (point) (1+ (point-at-eol))
  19210. (list :org-license-to-kill t)))))
  19211. title))
  19212. (defun org-solidify-link-text (s &optional alist)
  19213. "Take link text and make a safe target out of it."
  19214. (save-match-data
  19215. (let* ((rtn
  19216. (mapconcat
  19217. 'identity
  19218. (org-split-string s "[ \t\r\n]+") "--"))
  19219. (a (assoc rtn alist)))
  19220. (or (cdr a) rtn))))
  19221. ;; Variable holding the vector with section numbers
  19222. (defvar org-section-numbers (make-vector org-level-max 0))
  19223. (defun org-init-section-numbers ()
  19224. "Initialize the vector for the section numbers."
  19225. (let* ((level -1)
  19226. (numbers (nreverse (org-split-string "" "\\.")))
  19227. (depth (1- (length org-section-numbers)))
  19228. (i depth) number-string)
  19229. (while (>= i 0)
  19230. (if (> i level)
  19231. (aset org-section-numbers i 0)
  19232. (setq number-string (or (car numbers) "0"))
  19233. (if (string-match "\\`[A-Z]\\'" number-string)
  19234. (aset org-section-numbers i
  19235. (- (string-to-char number-string) ?A -1))
  19236. (aset org-section-numbers i (string-to-number number-string)))
  19237. (pop numbers))
  19238. (setq i (1- i)))))
  19239. (defun org-section-number (&optional level)
  19240. "Return a string with the current section number.
  19241. When LEVEL is non-nil, increase section numbers on that level."
  19242. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  19243. (when level
  19244. (when (> level -1)
  19245. (aset org-section-numbers
  19246. level (1+ (aref org-section-numbers level))))
  19247. (setq idx (1+ level))
  19248. (while (<= idx depth)
  19249. (if (not (= idx 1))
  19250. (aset org-section-numbers idx 0))
  19251. (setq idx (1+ idx))))
  19252. (setq idx 0)
  19253. (while (<= idx depth)
  19254. (setq n (aref org-section-numbers idx))
  19255. (setq string (concat string (if (not (string= string "")) "." "")
  19256. (int-to-string n)))
  19257. (setq idx (1+ idx)))
  19258. (save-match-data
  19259. (if (string-match "\\`\\([@0]\\.\\)+" string)
  19260. (setq string (replace-match "" t nil string)))
  19261. (if (string-match "\\(\\.0\\)+\\'" string)
  19262. (setq string (replace-match "" t nil string))))
  19263. string))
  19264. ;;; ASCII export
  19265. (defvar org-last-level nil) ; dynamically scoped variable
  19266. (defvar org-min-level nil) ; dynamically scoped variable
  19267. (defvar org-levels-open nil) ; dynamically scoped parameter
  19268. (defvar org-ascii-current-indentation nil) ; For communication
  19269. (defun org-export-as-ascii (arg)
  19270. "Export the outline as a pretty ASCII file.
  19271. If there is an active region, export only the region.
  19272. The prefix ARG specifies how many levels of the outline should become
  19273. underlined headlines. The default is 3."
  19274. (interactive "P")
  19275. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19276. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19277. (org-infile-export-plist)))
  19278. (region-p (org-region-active-p))
  19279. (subtree-p
  19280. (when region-p
  19281. (save-excursion
  19282. (goto-char (region-beginning))
  19283. (and (org-at-heading-p)
  19284. (>= (org-end-of-subtree t t) (region-end))))))
  19285. (custom-times org-display-custom-times)
  19286. (org-ascii-current-indentation '(0 . 0))
  19287. (level 0) line txt
  19288. (umax nil)
  19289. (umax-toc nil)
  19290. (case-fold-search nil)
  19291. (filename (concat (file-name-as-directory
  19292. (org-export-directory :ascii opt-plist))
  19293. (file-name-sans-extension
  19294. (or (and subtree-p
  19295. (org-entry-get (region-beginning)
  19296. "EXPORT_FILE_NAME" t))
  19297. (file-name-nondirectory buffer-file-name)))
  19298. ".txt"))
  19299. (filename (if (equal (file-truename filename)
  19300. (file-truename buffer-file-name))
  19301. (concat filename ".txt")
  19302. filename))
  19303. (buffer (find-file-noselect filename))
  19304. (org-levels-open (make-vector org-level-max nil))
  19305. (odd org-odd-levels-only)
  19306. (date (format-time-string "%Y/%m/%d" (current-time)))
  19307. (time (format-time-string "%X" (org-current-time)))
  19308. (author (plist-get opt-plist :author))
  19309. (title (or (and subtree-p (org-export-get-title-from-subtree))
  19310. (plist-get opt-plist :title)
  19311. (and (not
  19312. (plist-get opt-plist :skip-before-1st-heading))
  19313. (org-export-grab-title-from-buffer))
  19314. (file-name-sans-extension
  19315. (file-name-nondirectory buffer-file-name))))
  19316. (email (plist-get opt-plist :email))
  19317. (language (plist-get opt-plist :language))
  19318. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  19319. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  19320. (todo nil)
  19321. (lang-words nil)
  19322. (region
  19323. (buffer-substring
  19324. (if (org-region-active-p) (region-beginning) (point-min))
  19325. (if (org-region-active-p) (region-end) (point-max))))
  19326. (lines (org-split-string
  19327. (org-cleaned-string-for-export
  19328. region
  19329. :for-ascii t
  19330. :skip-before-1st-heading
  19331. (plist-get opt-plist :skip-before-1st-heading)
  19332. :archived-trees
  19333. (plist-get opt-plist :archived-trees)
  19334. :add-text (plist-get opt-plist :text))
  19335. "[\r\n]")) ;; FIXME: why \r here???/
  19336. thetoc have-headings first-heading-pos
  19337. table-open table-buffer)
  19338. (let ((inhibit-read-only t))
  19339. (org-unmodified
  19340. (remove-text-properties (point-min) (point-max)
  19341. '(:org-license-to-kill t))))
  19342. (setq org-min-level (org-get-min-level lines))
  19343. (setq org-last-level org-min-level)
  19344. (org-init-section-numbers)
  19345. (find-file-noselect filename)
  19346. (setq lang-words (or (assoc language org-export-language-setup)
  19347. (assoc "en" org-export-language-setup)))
  19348. (switch-to-buffer-other-window buffer)
  19349. (erase-buffer)
  19350. (fundamental-mode)
  19351. ;; create local variables for all options, to make sure all called
  19352. ;; functions get the correct information
  19353. (mapcar (lambda (x)
  19354. (set (make-local-variable (cdr x))
  19355. (plist-get opt-plist (car x))))
  19356. org-export-plist-vars)
  19357. (org-set-local 'org-odd-levels-only odd)
  19358. (setq umax (if arg (prefix-numeric-value arg)
  19359. org-export-headline-levels))
  19360. (setq umax-toc (if (integerp org-export-with-toc)
  19361. (min org-export-with-toc umax)
  19362. umax))
  19363. ;; File header
  19364. (if title (org-insert-centered title ?=))
  19365. (insert "\n")
  19366. (if (and (or author email)
  19367. org-export-author-info)
  19368. (insert (concat (nth 1 lang-words) ": " (or author "")
  19369. (if email (concat " <" email ">") "")
  19370. "\n")))
  19371. (if (and date time org-export-time-stamp-file)
  19372. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  19373. (insert "\n\n")
  19374. (if org-export-with-toc
  19375. (progn
  19376. (push (concat (nth 3 lang-words) "\n") thetoc)
  19377. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  19378. (mapcar '(lambda (line)
  19379. (if (string-match org-todo-line-regexp
  19380. line)
  19381. ;; This is a headline
  19382. (progn
  19383. (setq have-headings t)
  19384. (setq level (- (match-end 1) (match-beginning 1))
  19385. level (org-tr-level level)
  19386. txt (match-string 3 line)
  19387. todo
  19388. (or (and org-export-mark-todo-in-toc
  19389. (match-beginning 2)
  19390. (not (member (match-string 2 line)
  19391. org-done-keywords)))
  19392. ; TODO, not DONE
  19393. (and org-export-mark-todo-in-toc
  19394. (= level umax-toc)
  19395. (org-search-todo-below
  19396. line lines level))))
  19397. (setq txt (org-html-expand-for-ascii txt))
  19398. (if (and (memq org-export-with-tags '(not-in-toc nil))
  19399. (string-match
  19400. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  19401. txt))
  19402. (setq txt (replace-match "" t t txt)))
  19403. (if (string-match quote-re0 txt)
  19404. (setq txt (replace-match "" t t txt)))
  19405. (if org-export-with-section-numbers
  19406. (setq txt (concat (org-section-number level)
  19407. " " txt)))
  19408. (if (<= level umax-toc)
  19409. (progn
  19410. (push
  19411. (concat
  19412. (make-string
  19413. (* (max 0 (- level org-min-level)) 4) ?\ )
  19414. (format (if todo "%s (*)\n" "%s\n") txt))
  19415. thetoc)
  19416. (setq org-last-level level))
  19417. ))))
  19418. lines)
  19419. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  19420. (org-init-section-numbers)
  19421. (while (setq line (pop lines))
  19422. ;; Remove the quoted HTML tags.
  19423. (setq line (org-html-expand-for-ascii line))
  19424. ;; Remove targets
  19425. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  19426. (setq line (replace-match "" t t line)))
  19427. ;; Replace internal links
  19428. (while (string-match org-bracket-link-regexp line)
  19429. (setq line (replace-match
  19430. (if (match-end 3) "[\\3]" "[\\1]")
  19431. t nil line)))
  19432. (when custom-times
  19433. (setq line (org-translate-time line)))
  19434. (cond
  19435. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  19436. ;; a Headline
  19437. (setq first-heading-pos (or first-heading-pos (point)))
  19438. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  19439. txt (match-string 2 line))
  19440. (org-ascii-level-start level txt umax lines))
  19441. ((and org-export-with-tables
  19442. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  19443. (if (not table-open)
  19444. ;; New table starts
  19445. (setq table-open t table-buffer nil))
  19446. ;; Accumulate lines
  19447. (setq table-buffer (cons line table-buffer))
  19448. (when (or (not lines)
  19449. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  19450. (car lines))))
  19451. (setq table-open nil
  19452. table-buffer (nreverse table-buffer))
  19453. (insert (mapconcat
  19454. (lambda (x)
  19455. (org-fix-indentation x org-ascii-current-indentation))
  19456. (org-format-table-ascii table-buffer)
  19457. "\n") "\n")))
  19458. (t
  19459. (setq line (org-fix-indentation line org-ascii-current-indentation))
  19460. (if (and org-export-with-fixed-width
  19461. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  19462. (setq line (replace-match "\\1" nil nil line)))
  19463. (insert line "\n"))))
  19464. (normal-mode)
  19465. ;; insert the table of contents
  19466. (when thetoc
  19467. (goto-char (point-min))
  19468. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  19469. (progn
  19470. (goto-char (match-beginning 0))
  19471. (replace-match ""))
  19472. (goto-char first-heading-pos))
  19473. (mapc 'insert thetoc)
  19474. (or (looking-at "[ \t]*\n[ \t]*\n")
  19475. (insert "\n\n")))
  19476. (save-buffer)
  19477. ;; remove display and invisible chars
  19478. (let (beg end)
  19479. (goto-char (point-min))
  19480. (while (setq beg (next-single-property-change (point) 'display))
  19481. (setq end (next-single-property-change beg 'display))
  19482. (delete-region beg end)
  19483. (goto-char beg)
  19484. (insert "=>"))
  19485. (goto-char (point-min))
  19486. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  19487. (setq end (next-single-property-change beg 'org-cwidth))
  19488. (delete-region beg end)
  19489. (goto-char beg)))
  19490. (goto-char (point-min))))
  19491. (defun org-search-todo-below (line lines level)
  19492. "Search the subtree below LINE for any TODO entries."
  19493. (let ((rest (cdr (memq line lines)))
  19494. (re org-todo-line-regexp)
  19495. line lv todo)
  19496. (catch 'exit
  19497. (while (setq line (pop rest))
  19498. (if (string-match re line)
  19499. (progn
  19500. (setq lv (- (match-end 1) (match-beginning 1))
  19501. todo (and (match-beginning 2)
  19502. (not (member (match-string 2 line)
  19503. org-done-keywords))))
  19504. ; TODO, not DONE
  19505. (if (<= lv level) (throw 'exit nil))
  19506. (if todo (throw 'exit t))))))))
  19507. (defun org-html-expand-for-ascii (line)
  19508. "Handle quoted HTML for ASCII export."
  19509. (if org-export-html-expand
  19510. (while (string-match "@<[^<>\n]*>" line)
  19511. ;; We just remove the tags for now.
  19512. (setq line (replace-match "" nil nil line))))
  19513. line)
  19514. (defun org-insert-centered (s &optional underline)
  19515. "Insert the string S centered and underline it with character UNDERLINE."
  19516. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  19517. (insert (make-string ind ?\ ) s "\n")
  19518. (if underline
  19519. (insert (make-string ind ?\ )
  19520. (make-string (string-width s) underline)
  19521. "\n"))))
  19522. (defun org-ascii-level-start (level title umax &optional lines)
  19523. "Insert a new level in ASCII export."
  19524. (let (char (n (- level umax 1)) (ind 0))
  19525. (if (> level umax)
  19526. (progn
  19527. (insert (make-string (* 2 n) ?\ )
  19528. (char-to-string (nth (% n (length org-export-ascii-bullets))
  19529. org-export-ascii-bullets))
  19530. " " title "\n")
  19531. ;; find the indentation of the next non-empty line
  19532. (catch 'stop
  19533. (while lines
  19534. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  19535. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  19536. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  19537. (pop lines)))
  19538. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  19539. (if (or (not (equal (char-before) ?\n))
  19540. (not (equal (char-before (1- (point))) ?\n)))
  19541. (insert "\n"))
  19542. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  19543. (unless org-export-with-tags
  19544. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  19545. (setq title (replace-match "" t t title))))
  19546. (if org-export-with-section-numbers
  19547. (setq title (concat (org-section-number level) " " title)))
  19548. (insert title "\n" (make-string (string-width title) char) "\n")
  19549. (setq org-ascii-current-indentation '(0 . 0)))))
  19550. (defun org-export-visible (type arg)
  19551. "Create a copy of the visible part of the current buffer, and export it.
  19552. The copy is created in a temporary buffer and removed after use.
  19553. TYPE is the final key (as a string) that also select the export command in
  19554. the `C-c C-e' export dispatcher.
  19555. As a special case, if the you type SPC at the prompt, the temporary
  19556. org-mode file will not be removed but presented to you so that you can
  19557. continue to use it. The prefix arg ARG is passed through to the exporting
  19558. command."
  19559. (interactive
  19560. (list (progn
  19561. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  19562. (read-char-exclusive))
  19563. current-prefix-arg))
  19564. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  19565. (error "Invalid export key"))
  19566. (let* ((binding (cdr (assoc type
  19567. '((?a . org-export-as-ascii)
  19568. (?\C-a . org-export-as-ascii)
  19569. (?b . org-export-as-html-and-open)
  19570. (?\C-b . org-export-as-html-and-open)
  19571. (?h . org-export-as-html)
  19572. (?H . org-export-as-html-to-buffer)
  19573. (?R . org-export-region-as-html)
  19574. (?x . org-export-as-xoxo)))))
  19575. (keepp (equal type ?\ ))
  19576. (file buffer-file-name)
  19577. (buffer (get-buffer-create "*Org Export Visible*"))
  19578. s e)
  19579. (with-current-buffer buffer (erase-buffer))
  19580. (save-excursion
  19581. (setq s (goto-char (point-min)))
  19582. (while (not (= (point) (point-max)))
  19583. (goto-char (org-find-invisible))
  19584. (append-to-buffer buffer s (point))
  19585. (setq s (goto-char (org-find-visible))))
  19586. (goto-char (point-min))
  19587. (unless keepp
  19588. ;; Copy all comment lines to the end, to make sure #+ settings are
  19589. ;; still available for the second export step. Kind of a hack, but
  19590. ;; does do the trick.
  19591. (if (looking-at "#[^\r\n]*")
  19592. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  19593. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  19594. (append-to-buffer buffer (1+ (match-beginning 0))
  19595. (min (point-max) (1+ (match-end 0))))))
  19596. (set-buffer buffer)
  19597. (let ((buffer-file-name file)
  19598. (org-inhibit-startup t))
  19599. (org-mode)
  19600. (show-all)
  19601. (unless keepp (funcall binding arg))))
  19602. (if (not keepp)
  19603. (kill-buffer buffer)
  19604. (switch-to-buffer-other-window buffer)
  19605. (goto-char (point-min)))))
  19606. (defun org-find-visible ()
  19607. (let ((s (point)))
  19608. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19609. (get-char-property s 'invisible)))
  19610. s))
  19611. (defun org-find-invisible ()
  19612. (let ((s (point)))
  19613. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19614. (not (get-char-property s 'invisible))))
  19615. s))
  19616. ;;; HTML export
  19617. (defun org-get-current-options ()
  19618. "Return a string with current options as keyword options.
  19619. Does include HTML export options as well as TODO and CATEGORY stuff."
  19620. (format
  19621. "#+TITLE: %s
  19622. #+AUTHOR: %s
  19623. #+EMAIL: %s
  19624. #+LANGUAGE: %s
  19625. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  19626. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s
  19627. #+CATEGORY: %s
  19628. #+SEQ_TODO: %s
  19629. #+TYP_TODO: %s
  19630. #+PRIORITIES: %c %c %c
  19631. #+STARTUP: %s %s %s %s %s
  19632. #+TAGS: %s
  19633. #+ARCHIVE: %s
  19634. #+LINK: %s
  19635. "
  19636. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  19637. org-export-headline-levels
  19638. org-export-with-section-numbers
  19639. org-export-with-toc
  19640. org-export-preserve-breaks
  19641. org-export-html-expand
  19642. org-export-with-fixed-width
  19643. org-export-with-tables
  19644. org-export-with-sub-superscripts
  19645. org-export-with-footnotes
  19646. org-export-with-emphasize
  19647. org-export-with-TeX-macros
  19648. org-export-with-LaTeX-fragments
  19649. org-export-skip-text-before-1st-heading
  19650. org-export-with-property-drawer
  19651. (file-name-nondirectory buffer-file-name)
  19652. "TODO FEEDBACK VERIFY DONE"
  19653. "Me Jason Marie DONE"
  19654. org-highest-priority org-lowest-priority org-default-priority
  19655. (cdr (assoc org-startup-folded
  19656. '((nil . "showall") (t . "overview") (content . "content"))))
  19657. (if org-odd-levels-only "odd" "oddeven")
  19658. (if org-hide-leading-stars "hidestars" "showstars")
  19659. (if org-startup-align-all-tables "align" "noalign")
  19660. (cond ((eq t org-log-done) "logdone")
  19661. ((not org-log-done) "nologging")
  19662. ((listp org-log-done)
  19663. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  19664. org-log-done " ")))
  19665. (or (mapconcat (lambda (x)
  19666. (cond
  19667. ((equal '(:startgroup) x) "{")
  19668. ((equal '(:endgroup) x) "}")
  19669. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  19670. (t (car x))))
  19671. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  19672. org-archive-location
  19673. "org file:~/org/%s.org"
  19674. ))
  19675. (defun org-insert-export-options-template ()
  19676. "Insert into the buffer a template with information for exporting."
  19677. (interactive)
  19678. (if (not (bolp)) (newline))
  19679. (let ((s (org-get-current-options)))
  19680. (and (string-match "#\\+CATEGORY" s)
  19681. (setq s (substring s 0 (match-beginning 0))))
  19682. (insert s)))
  19683. (defun org-toggle-fixed-width-section (arg)
  19684. "Toggle the fixed-width export.
  19685. If there is no active region, the QUOTE keyword at the current headline is
  19686. inserted or removed. When present, it causes the text between this headline
  19687. and the next to be exported as fixed-width text, and unmodified.
  19688. If there is an active region, this command adds or removes a colon as the
  19689. first character of this line. If the first character of a line is a colon,
  19690. this line is also exported in fixed-width font."
  19691. (interactive "P")
  19692. (let* ((cc 0)
  19693. (regionp (org-region-active-p))
  19694. (beg (if regionp (region-beginning) (point)))
  19695. (end (if regionp (region-end)))
  19696. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19697. (re "[ \t]*\\(:\\)")
  19698. off)
  19699. (if regionp
  19700. (save-excursion
  19701. (goto-char beg)
  19702. (setq cc (current-column))
  19703. (beginning-of-line 1)
  19704. (setq off (looking-at re))
  19705. (while (> nlines 0)
  19706. (setq nlines (1- nlines))
  19707. (beginning-of-line 1)
  19708. (cond
  19709. (arg
  19710. (move-to-column cc t)
  19711. (insert ":\n")
  19712. (forward-line -1))
  19713. ((and off (looking-at re))
  19714. (replace-match "" t t nil 1))
  19715. ((not off) (move-to-column cc t) (insert ":")))
  19716. (forward-line 1)))
  19717. (save-excursion
  19718. (org-back-to-heading)
  19719. (if (looking-at (concat outline-regexp
  19720. "\\( *\\<" org-quote-string "\\>\\)"))
  19721. (replace-match "" t t nil 1)
  19722. (if (looking-at outline-regexp)
  19723. (progn
  19724. (goto-char (match-end 0))
  19725. (insert org-quote-string " "))))))))
  19726. (defun org-export-as-html-and-open (arg)
  19727. "Export the outline as HTML and immediately open it with a browser.
  19728. If there is an active region, export only the region.
  19729. The prefix ARG specifies how many levels of the outline should become
  19730. headlines. The default is 3. Lower levels will become bulleted lists."
  19731. (interactive "P")
  19732. (org-export-as-html arg 'hidden)
  19733. (org-open-file buffer-file-name))
  19734. (defun org-export-as-html-batch ()
  19735. "Call `org-export-as-html', may be used in batch processing as
  19736. emacs --batch
  19737. --load=$HOME/lib/emacs/org.el
  19738. --eval \"(setq org-export-headline-levels 2)\"
  19739. --visit=MyFile --funcall org-export-as-html-batch"
  19740. (org-export-as-html org-export-headline-levels 'hidden))
  19741. (defun org-export-as-html-to-buffer (arg)
  19742. "Call `org-exort-as-html` with output to a temporary buffer.
  19743. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  19744. (interactive "P")
  19745. (org-export-as-html arg nil nil "*Org HTML Export*")
  19746. (switch-to-buffer-other-window "*Org HTML Export*"))
  19747. (defun org-replace-region-by-html (beg end)
  19748. "Assume the current region has org-mode syntax, and convert it to HTML.
  19749. This can be used in any buffer. For example, you could write an
  19750. itemized list in org-mode syntax in an HTML buffer and then use this
  19751. command to convert it."
  19752. (interactive "r")
  19753. (let (reg html buf)
  19754. (save-window-excursion
  19755. (if (org-mode-p)
  19756. (setq html (org-export-region-as-html
  19757. beg end t 'string))
  19758. (setq reg (buffer-substring beg end)
  19759. buf (get-buffer-create "*Org tmp*"))
  19760. (with-current-buffer buf
  19761. (erase-buffer)
  19762. (insert reg)
  19763. (org-mode)
  19764. (setq html (org-export-region-as-html
  19765. (point-min) (point-max) t 'string)))
  19766. (kill-buffer buf)))
  19767. (delete-region beg end)
  19768. (insert html)))
  19769. (defun org-export-region-as-html (beg end &optional body-only buffer)
  19770. "Convert region from BEG to END in org-mode buffer to HTML.
  19771. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  19772. contents, and only produce the region of converted text, useful for
  19773. cut-and-paste operations.
  19774. If BUFFER is a buffer or a string, use/create that buffer as a target
  19775. of the converted HTML. If BUFFER is the symbol `string', return the
  19776. produced HTML as a string and leave not buffer behind. For example,
  19777. a Lisp program could call this function in the following way:
  19778. (setq html (org-export-region-as-html beg end t 'string))
  19779. When called interactively, the output buffer is selected, and shown
  19780. in a window. A non-interactive call will only retunr the buffer."
  19781. (interactive "r\nP")
  19782. (when (interactive-p)
  19783. (setq buffer "*Org HTML Export*"))
  19784. (let ((transient-mark-mode t) (zmacs-regions t)
  19785. rtn)
  19786. (goto-char end)
  19787. (set-mark (point)) ;; to activate the region
  19788. (goto-char beg)
  19789. (setq rtn (org-export-as-html
  19790. nil nil nil
  19791. buffer body-only))
  19792. (if (fboundp 'deactivate-mark) (deactivate-mark))
  19793. (if (and (interactive-p) (bufferp rtn))
  19794. (switch-to-buffer-other-window rtn)
  19795. rtn)))
  19796. (defun org-export-as-html (arg &optional hidden ext-plist
  19797. to-buffer body-only)
  19798. "Export the outline as a pretty HTML file.
  19799. If there is an active region, export only the region. The prefix
  19800. ARG specifies how many levels of the outline should become
  19801. headlines. The default is 3. Lower levels will become bulleted
  19802. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  19803. EXT-PLIST is a property list with external parameters overriding
  19804. org-mode's default settings, but still inferior to file-local
  19805. settings. When TO-BUFFER is non-nil, create a buffer with that
  19806. name and export to that buffer. If TO-BUFFER is the symbol `string',
  19807. don't leave any buffer behind but just return the resulting HTML as
  19808. a string. When BODY-ONLY is set, don't produce the file header and footer,
  19809. simply return the content of <body>...</body>, without even
  19810. the body tags themselves."
  19811. (interactive "P")
  19812. ;; Make sure we have a file name when we need it.
  19813. (when (and (not (or to-buffer body-only))
  19814. (not buffer-file-name))
  19815. (if (buffer-base-buffer)
  19816. (org-set-local 'buffer-file-name
  19817. (with-current-buffer (buffer-base-buffer)
  19818. buffer-file-name))
  19819. (error "Need a file name to be able to export.")))
  19820. (message "Exporting...")
  19821. (setq-default org-todo-line-regexp org-todo-line-regexp)
  19822. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  19823. (setq-default org-done-keywords org-done-keywords)
  19824. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  19825. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  19826. ext-plist
  19827. (org-infile-export-plist)))
  19828. (style (plist-get opt-plist :style))
  19829. (link-validate (plist-get opt-plist :link-validation-function))
  19830. valid thetoc have-headings first-heading-pos
  19831. (odd org-odd-levels-only)
  19832. (region-p (org-region-active-p))
  19833. (subtree-p
  19834. (when region-p
  19835. (save-excursion
  19836. (goto-char (region-beginning))
  19837. (and (org-at-heading-p)
  19838. (>= (org-end-of-subtree t t) (region-end))))))
  19839. ;; The following two are dynamically scoped into other
  19840. ;; routines below.
  19841. (org-current-export-dir (org-export-directory :html opt-plist))
  19842. (org-current-export-file buffer-file-name)
  19843. (level 0) (line "") (origline "") txt todo
  19844. (umax nil)
  19845. (umax-toc nil)
  19846. (filename (if to-buffer nil
  19847. (concat (file-name-as-directory
  19848. (org-export-directory :html opt-plist))
  19849. (file-name-sans-extension
  19850. (or (and subtree-p
  19851. (org-entry-get (region-beginning)
  19852. "EXPORT_FILE_NAME" t))
  19853. (file-name-nondirectory buffer-file-name)))
  19854. ".html")))
  19855. (current-dir (if buffer-file-name
  19856. (file-name-directory buffer-file-name)
  19857. default-directory))
  19858. (buffer (if to-buffer
  19859. (cond
  19860. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  19861. (t (get-buffer-create to-buffer)))
  19862. (find-file-noselect filename)))
  19863. (org-levels-open (make-vector org-level-max nil))
  19864. (date (format-time-string "%Y/%m/%d" (current-time)))
  19865. (time (format-time-string "%X" (org-current-time)))
  19866. (author (plist-get opt-plist :author))
  19867. (title (or (and subtree-p (org-export-get-title-from-subtree))
  19868. (plist-get opt-plist :title)
  19869. (and (not
  19870. (plist-get opt-plist :skip-before-1st-heading))
  19871. (org-export-grab-title-from-buffer))
  19872. (and buffer-file-name
  19873. (file-name-sans-extension
  19874. (file-name-nondirectory buffer-file-name)))
  19875. "UNTITLED"))
  19876. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  19877. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  19878. (inquote nil)
  19879. (infixed nil)
  19880. (in-local-list nil)
  19881. (local-list-num nil)
  19882. (local-list-indent nil)
  19883. (llt org-plain-list-ordered-item-terminator)
  19884. (email (plist-get opt-plist :email))
  19885. (language (plist-get opt-plist :language))
  19886. (lang-words nil)
  19887. (target-alist nil) tg
  19888. (head-count 0) cnt
  19889. (start 0)
  19890. (coding-system (and (boundp 'buffer-file-coding-system)
  19891. buffer-file-coding-system))
  19892. (coding-system-for-write (or org-export-html-coding-system
  19893. coding-system))
  19894. (save-buffer-coding-system (or org-export-html-coding-system
  19895. coding-system))
  19896. (charset (and coding-system-for-write
  19897. (fboundp 'coding-system-get)
  19898. (coding-system-get coding-system-for-write
  19899. 'mime-charset)))
  19900. (region
  19901. (buffer-substring
  19902. (if region-p (region-beginning) (point-min))
  19903. (if region-p (region-end) (point-max))))
  19904. (lines
  19905. (org-split-string
  19906. (org-cleaned-string-for-export
  19907. region
  19908. :emph-multiline t
  19909. :for-html t
  19910. :skip-before-1st-heading
  19911. (plist-get opt-plist :skip-before-1st-heading)
  19912. :archived-trees
  19913. (plist-get opt-plist :archived-trees)
  19914. :add-text
  19915. (plist-get opt-plist :text)
  19916. :LaTeX-fragments
  19917. (plist-get opt-plist :LaTeX-fragments))
  19918. "[\r\n]"))
  19919. table-open type
  19920. table-buffer table-orig-buffer
  19921. ind start-is-num starter didclose
  19922. rpl path desc descp desc1 desc2 link
  19923. )
  19924. (let ((inhibit-read-only t))
  19925. (org-unmodified
  19926. (remove-text-properties (point-min) (point-max)
  19927. '(:org-license-to-kill t))))
  19928. (message "Exporting...")
  19929. (setq org-min-level (org-get-min-level lines))
  19930. (setq org-last-level org-min-level)
  19931. (org-init-section-numbers)
  19932. ;; Get the language-dependent settings
  19933. (setq lang-words (or (assoc language org-export-language-setup)
  19934. (assoc "en" org-export-language-setup)))
  19935. ;; Switch to the output buffer
  19936. (set-buffer buffer)
  19937. (erase-buffer)
  19938. (fundamental-mode)
  19939. (and (fboundp 'set-buffer-file-coding-system)
  19940. (set-buffer-file-coding-system coding-system-for-write))
  19941. (let ((case-fold-search nil)
  19942. (org-odd-levels-only odd))
  19943. ;; create local variables for all options, to make sure all called
  19944. ;; functions get the correct information
  19945. (mapcar (lambda (x)
  19946. (set (make-local-variable (cdr x))
  19947. (plist-get opt-plist (car x))))
  19948. org-export-plist-vars)
  19949. (setq umax (if arg (prefix-numeric-value arg)
  19950. org-export-headline-levels))
  19951. (setq umax-toc (if (integerp org-export-with-toc)
  19952. (min org-export-with-toc umax)
  19953. umax))
  19954. (unless body-only
  19955. ;; File header
  19956. (insert (format
  19957. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  19958. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  19959. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  19960. lang=\"%s\" xml:lang=\"%s\">
  19961. <head>
  19962. <title>%s</title>
  19963. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  19964. <meta name=\"generator\" content=\"Org-mode\"/>
  19965. <meta name=\"generated\" content=\"%s %s\"/>
  19966. <meta name=\"author\" content=\"%s\"/>
  19967. %s
  19968. </head><body>
  19969. "
  19970. language language (org-html-expand title)
  19971. (or charset "iso-8859-1") date time author style))
  19972. (insert (or (plist-get opt-plist :preamble) ""))
  19973. (when (plist-get opt-plist :auto-preamble)
  19974. (if title (insert (format org-export-html-title-format
  19975. (org-html-expand title))))))
  19976. (if (and org-export-with-toc (not body-only))
  19977. (progn
  19978. (push (format "<h%d>%s</h%d>\n"
  19979. org-export-html-toplevel-hlevel
  19980. (nth 3 lang-words)
  19981. org-export-html-toplevel-hlevel)
  19982. thetoc)
  19983. (push "<ul>\n<li>" thetoc)
  19984. (setq lines
  19985. (mapcar '(lambda (line)
  19986. (if (string-match org-todo-line-regexp line)
  19987. ;; This is a headline
  19988. (progn
  19989. (setq have-headings t)
  19990. (setq level (- (match-end 1) (match-beginning 1))
  19991. level (org-tr-level level)
  19992. txt (save-match-data
  19993. (org-html-expand
  19994. (org-export-cleanup-toc-line
  19995. (match-string 3 line))))
  19996. todo
  19997. (or (and org-export-mark-todo-in-toc
  19998. (match-beginning 2)
  19999. (not (member (match-string 2 line)
  20000. org-done-keywords)))
  20001. ; TODO, not DONE
  20002. (and org-export-mark-todo-in-toc
  20003. (= level umax-toc)
  20004. (org-search-todo-below
  20005. line lines level))))
  20006. (if (and (memq org-export-with-tags '(not-in-toc nil))
  20007. (string-match
  20008. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  20009. txt))
  20010. (setq txt (replace-match "" t t txt)))
  20011. (if (string-match quote-re0 txt)
  20012. (setq txt (replace-match "" t t txt)))
  20013. (if org-export-with-section-numbers
  20014. (setq txt (concat (org-section-number level)
  20015. " " txt)))
  20016. (if (<= level (max umax umax-toc))
  20017. (setq head-count (+ head-count 1)))
  20018. (if (<= level umax-toc)
  20019. (progn
  20020. (if (> level org-last-level)
  20021. (progn
  20022. (setq cnt (- level org-last-level))
  20023. (while (>= (setq cnt (1- cnt)) 0)
  20024. (push "\n<ul>\n<li>" thetoc))
  20025. (push "\n" thetoc)))
  20026. (if (< level org-last-level)
  20027. (progn
  20028. (setq cnt (- org-last-level level))
  20029. (while (>= (setq cnt (1- cnt)) 0)
  20030. (push "</li>\n</ul>" thetoc))
  20031. (push "\n" thetoc)))
  20032. ;; Check for targets
  20033. (while (string-match org-target-regexp line)
  20034. (setq tg (match-string 1 line)
  20035. line (replace-match
  20036. (concat "@<span class=\"target\">" tg "@</span> ")
  20037. t t line))
  20038. (push (cons (org-solidify-link-text tg)
  20039. (format "sec-%d" head-count))
  20040. target-alist))
  20041. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  20042. (setq txt (replace-match "" t t txt)))
  20043. (push
  20044. (format
  20045. (if todo
  20046. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  20047. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  20048. head-count txt) thetoc)
  20049. (setq org-last-level level))
  20050. )))
  20051. line)
  20052. lines))
  20053. (while (> org-last-level (1- org-min-level))
  20054. (setq org-last-level (1- org-last-level))
  20055. (push "</li>\n</ul>\n" thetoc))
  20056. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  20057. (setq head-count 0)
  20058. (org-init-section-numbers)
  20059. (while (setq line (pop lines) origline line)
  20060. (catch 'nextline
  20061. ;; end of quote section?
  20062. (when (and inquote (string-match "^\\*+ " line))
  20063. (insert "</pre>\n")
  20064. (setq inquote nil))
  20065. ;; inside a quote section?
  20066. (when inquote
  20067. (insert (org-html-protect line) "\n")
  20068. (throw 'nextline nil))
  20069. ;; verbatim lines
  20070. (when (and org-export-with-fixed-width
  20071. (string-match "^[ \t]*:\\(.*\\)" line))
  20072. (when (not infixed)
  20073. (setq infixed t)
  20074. (insert "<pre>\n"))
  20075. (insert (org-html-protect (match-string 1 line)) "\n")
  20076. (when (and lines
  20077. (not (string-match "^[ \t]*\\(:.*\\)"
  20078. (car lines))))
  20079. (setq infixed nil)
  20080. (insert "</pre>\n"))
  20081. (throw 'nextline nil))
  20082. ;; Protected HTML
  20083. (when (get-text-property 0 'org-protected line)
  20084. (let (par)
  20085. (when (re-search-backward
  20086. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  20087. (setq par (match-string 1))
  20088. (replace-match "\\2\n"))
  20089. (insert line "\n")
  20090. (while (and lines
  20091. (get-text-property 0 'org-protected (car lines)))
  20092. (insert (pop lines) "\n"))
  20093. (and par (insert "<p>\n")))
  20094. (throw 'nextline nil))
  20095. ;; Horizontal line
  20096. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  20097. (insert "\n<hr/>\n")
  20098. (throw 'nextline nil))
  20099. ;; make targets to anchors
  20100. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  20101. (cond
  20102. ((match-end 2)
  20103. (setq line (replace-match
  20104. (concat "@<a name=\""
  20105. (org-solidify-link-text (match-string 1 line))
  20106. "\">\\nbsp@</a>")
  20107. t t line)))
  20108. ((and org-export-with-toc (equal (string-to-char line) ?*))
  20109. (setq line (replace-match
  20110. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  20111. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  20112. t t line)))
  20113. (t
  20114. (setq line (replace-match
  20115. (concat "@<a name=\""
  20116. (org-solidify-link-text (match-string 1 line))
  20117. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  20118. t t line)))))
  20119. (setq line (org-html-handle-time-stamps line))
  20120. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  20121. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  20122. ;; Also handle sub_superscripts and checkboxes
  20123. (setq line (org-html-expand line))
  20124. ;; Format the links
  20125. (setq start 0)
  20126. (while (string-match org-bracket-link-analytic-regexp line start)
  20127. (setq start (match-beginning 0))
  20128. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  20129. (setq path (match-string 3 line))
  20130. (setq desc1 (if (match-end 5) (match-string 5 line))
  20131. desc2 (if (match-end 2) (concat type ":" path) path)
  20132. descp (and desc1 (not (equal desc1 desc2)))
  20133. desc (or desc1 desc2))
  20134. ;; Make an image out of the description if that is so wanted
  20135. (when (and descp (org-file-image-p desc))
  20136. (save-match-data
  20137. (if (string-match "^file:" desc)
  20138. (setq desc (substring desc (match-end 0)))))
  20139. (setq desc (concat "<img src=\"" desc "\"/>")))
  20140. ;; FIXME: do we need to unescape here somewhere?
  20141. (cond
  20142. ((equal type "internal")
  20143. (setq rpl
  20144. (concat
  20145. "<a href=\"#"
  20146. (org-solidify-link-text
  20147. (save-match-data (org-link-unescape path)) target-alist)
  20148. "\">" desc "</a>")))
  20149. ((member type '("http" "https")) ; FIXME: need to test this.
  20150. ;; standard URL, just check if we need to inline an image
  20151. (if (and (or (eq t org-export-html-inline-images)
  20152. (and org-export-html-inline-images (not descp)))
  20153. (org-file-image-p path))
  20154. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  20155. (setq link (concat type ":" path))
  20156. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  20157. ((member type '("ftp" "mailto" "news"))
  20158. ;; standard URL
  20159. (setq link (concat type ":" path))
  20160. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  20161. ((string= type "file")
  20162. ;; FILE link
  20163. (let* ((filename path)
  20164. (abs-p (file-name-absolute-p filename))
  20165. thefile file-is-image-p search)
  20166. (save-match-data
  20167. (if (string-match "::\\(.*\\)" filename)
  20168. (setq search (match-string 1 filename)
  20169. filename (replace-match "" t nil filename)))
  20170. (setq valid
  20171. (if (functionp link-validate)
  20172. (funcall link-validate filename current-dir)
  20173. t))
  20174. (setq file-is-image-p (org-file-image-p filename))
  20175. (setq thefile (if abs-p (expand-file-name filename) filename))
  20176. (when (and org-export-html-link-org-files-as-html
  20177. (string-match "\\.org$" thefile))
  20178. (setq thefile (concat (substring thefile 0
  20179. (match-beginning 0))
  20180. ".html"))
  20181. (if (and search
  20182. ;; make sure this is can be used as target search
  20183. (not (string-match "^[0-9]*$" search))
  20184. (not (string-match "^\\*" search))
  20185. (not (string-match "^/.*/$" search)))
  20186. (setq thefile (concat thefile "#"
  20187. (org-solidify-link-text
  20188. (org-link-unescape search)))))
  20189. (when (string-match "^file:" desc)
  20190. (setq desc (replace-match "" t t desc))
  20191. (if (string-match "\\.org$" desc)
  20192. (setq desc (replace-match "" t t desc))))))
  20193. (setq rpl (if (and file-is-image-p
  20194. (or (eq t org-export-html-inline-images)
  20195. (and org-export-html-inline-images
  20196. (not descp))))
  20197. (concat "<img src=\"" thefile "\"/>")
  20198. (concat "<a href=\"" thefile "\">" desc "</a>")))
  20199. (if (not valid) (setq rpl desc))))
  20200. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  20201. (setq rpl (concat "<i>&lt;" type ":"
  20202. (save-match-data (org-link-unescape path))
  20203. "&gt;</i>"))))
  20204. (setq line (replace-match rpl t t line)
  20205. start (+ start (length rpl))))
  20206. ;; TODO items
  20207. (if (and (string-match org-todo-line-regexp line)
  20208. (match-beginning 2))
  20209. (setq line
  20210. (concat (substring line 0 (match-beginning 2))
  20211. "<span class=\""
  20212. (if (member (match-string 2 line)
  20213. org-done-keywords)
  20214. "done" "todo")
  20215. "\">" (match-string 2 line)
  20216. "</span>" (substring line (match-end 2)))))
  20217. ;; Does this contain a reference to a footnote?
  20218. (when org-export-with-footnotes
  20219. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  20220. (let ((n (match-string 2 line)))
  20221. (setq line
  20222. (replace-match
  20223. (format
  20224. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  20225. (match-string 1 line) n n n)
  20226. t t line)))))
  20227. (cond
  20228. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  20229. ;; This is a headline
  20230. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  20231. txt (match-string 2 line))
  20232. (if (string-match quote-re0 txt)
  20233. (setq txt (replace-match "" t t txt)))
  20234. (if (<= level (max umax umax-toc))
  20235. (setq head-count (+ head-count 1)))
  20236. (when in-local-list
  20237. ;; Close any local lists before inserting a new header line
  20238. (while local-list-num
  20239. (org-close-li)
  20240. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20241. (pop local-list-num))
  20242. (setq local-list-indent nil
  20243. in-local-list nil))
  20244. (setq first-heading-pos (or first-heading-pos (point)))
  20245. (org-html-level-start level txt umax
  20246. (and org-export-with-toc (<= level umax))
  20247. head-count)
  20248. ;; QUOTES
  20249. (when (string-match quote-re line)
  20250. (insert "<pre>")
  20251. (setq inquote t)))
  20252. ((and org-export-with-tables
  20253. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  20254. (if (not table-open)
  20255. ;; New table starts
  20256. (setq table-open t table-buffer nil table-orig-buffer nil))
  20257. ;; Accumulate lines
  20258. (setq table-buffer (cons line table-buffer)
  20259. table-orig-buffer (cons origline table-orig-buffer))
  20260. (when (or (not lines)
  20261. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  20262. (car lines))))
  20263. (setq table-open nil
  20264. table-buffer (nreverse table-buffer)
  20265. table-orig-buffer (nreverse table-orig-buffer))
  20266. (org-close-par-maybe)
  20267. (insert (org-format-table-html table-buffer table-orig-buffer))))
  20268. (t
  20269. ;; Normal lines
  20270. (when (string-match
  20271. (cond
  20272. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20273. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20274. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  20275. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  20276. line)
  20277. (setq ind (org-get-string-indentation line)
  20278. start-is-num (match-beginning 4)
  20279. starter (if (match-beginning 2)
  20280. (substring (match-string 2 line) 0 -1))
  20281. line (substring line (match-beginning 5)))
  20282. (unless (string-match "[^ \t]" line)
  20283. ;; empty line. Pretend indentation is large.
  20284. (setq ind (if org-empty-line-terminates-plain-lists
  20285. 0
  20286. (1+ (or (car local-list-indent) 1)))))
  20287. (setq didclose nil)
  20288. (while (and in-local-list
  20289. (or (and (= ind (car local-list-indent))
  20290. (not starter))
  20291. (< ind (car local-list-indent))))
  20292. (setq didclose t)
  20293. (org-close-li)
  20294. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  20295. (pop local-list-num) (pop local-list-indent)
  20296. (setq in-local-list local-list-indent))
  20297. (cond
  20298. ((and starter
  20299. (or (not in-local-list)
  20300. (> ind (car local-list-indent))))
  20301. ;; Start new (level of) list
  20302. (org-close-par-maybe)
  20303. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  20304. (push start-is-num local-list-num)
  20305. (push ind local-list-indent)
  20306. (setq in-local-list t))
  20307. (starter
  20308. ;; continue current list
  20309. (org-close-li)
  20310. (insert "<li>\n"))
  20311. (didclose
  20312. ;; we did close a list, normal text follows: need <p>
  20313. (org-open-par)))
  20314. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  20315. (setq line
  20316. (replace-match
  20317. (if (equal (match-string 1 line) "X")
  20318. "<b>[X]</b>"
  20319. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  20320. t t line))))
  20321. ;; Empty lines start a new paragraph. If hand-formatted lists
  20322. ;; are not fully interpreted, lines starting with "-", "+", "*"
  20323. ;; also start a new paragraph.
  20324. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  20325. ;; Is this the start of a footnote?
  20326. (when org-export-with-footnotes
  20327. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  20328. (org-close-par-maybe)
  20329. (let ((n (match-string 1 line)))
  20330. (setq line (replace-match
  20331. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  20332. ;; Check if the line break needs to be conserved
  20333. (cond
  20334. ((string-match "\\\\\\\\[ \t]*$" line)
  20335. (setq line (replace-match "<br/>" t t line)))
  20336. (org-export-preserve-breaks
  20337. (setq line (concat line "<br/>"))))
  20338. (insert line "\n")))))
  20339. ;; Properly close all local lists and other lists
  20340. (when inquote (insert "</pre>\n"))
  20341. (when in-local-list
  20342. ;; Close any local lists before inserting a new header line
  20343. (while local-list-num
  20344. (org-close-li)
  20345. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  20346. (pop local-list-num))
  20347. (setq local-list-indent nil
  20348. in-local-list nil))
  20349. (org-html-level-start 1 nil umax
  20350. (and org-export-with-toc (<= level umax))
  20351. head-count)
  20352. (unless body-only
  20353. (when (plist-get opt-plist :auto-postamble)
  20354. (when (and org-export-author-info author)
  20355. (insert "<p class=\"author\"> "
  20356. (nth 1 lang-words) ": " author "\n")
  20357. (when email
  20358. (insert "<a href=\"mailto:" email "\">&lt;"
  20359. email "&gt;</a>\n"))
  20360. (insert "</p>\n"))
  20361. (when (and date time org-export-time-stamp-file)
  20362. (insert "<p class=\"date\"> "
  20363. (nth 2 lang-words) ": "
  20364. date " " time "</p>\n")))
  20365. (if org-export-html-with-timestamp
  20366. (insert org-export-html-html-helper-timestamp))
  20367. (insert (or (plist-get opt-plist :postamble) ""))
  20368. (insert "</body>\n</html>\n"))
  20369. (normal-mode)
  20370. (if (eq major-mode default-major-mode) (html-mode))
  20371. ;; insert the table of contents
  20372. (goto-char (point-min))
  20373. (when thetoc
  20374. (if (or (re-search-forward
  20375. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  20376. (re-search-forward
  20377. "\\[TABLE-OF-CONTENTS\\]" nil t))
  20378. (progn
  20379. (goto-char (match-beginning 0))
  20380. (replace-match ""))
  20381. (goto-char first-heading-pos)
  20382. (when (looking-at "\\s-*</p>")
  20383. (goto-char (match-end 0))
  20384. (insert "\n")))
  20385. (mapc 'insert thetoc))
  20386. ;; remove empty paragraphs and lists
  20387. (goto-char (point-min))
  20388. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  20389. (replace-match ""))
  20390. (goto-char (point-min))
  20391. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  20392. (replace-match ""))
  20393. (or to-buffer (save-buffer))
  20394. (goto-char (point-min))
  20395. (message "Exporting... done")
  20396. (if (eq to-buffer 'string)
  20397. (prog1 (buffer-substring (point-min) (point-max))
  20398. (kill-buffer (current-buffer)))
  20399. (current-buffer)))))
  20400. (defvar org-table-colgroup-info nil) ;; FIXME: mode to a better place
  20401. (defun org-format-table-ascii (lines)
  20402. "Format a table for ascii export."
  20403. (if (stringp lines)
  20404. (setq lines (org-split-string lines "\n")))
  20405. (if (not (string-match "^[ \t]*|" (car lines)))
  20406. ;; Table made by table.el - test for spanning
  20407. lines
  20408. ;; A normal org table
  20409. ;; Get rid of hlines at beginning and end
  20410. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20411. (setq lines (nreverse lines))
  20412. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20413. (setq lines (nreverse lines))
  20414. (when org-export-table-remove-special-lines
  20415. ;; Check if the table has a marking column. If yes remove the
  20416. ;; column and the special lines
  20417. (setq lines (org-table-clean-before-export lines)))
  20418. ;; Get rid of the vertical lines except for grouping
  20419. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  20420. rtn line vl1 start)
  20421. (while (setq line (pop lines))
  20422. (if (string-match org-table-hline-regexp line)
  20423. (and (string-match "|\\(.*\\)|" line)
  20424. (setq line (replace-match " \\1" t nil line)))
  20425. (setq start 0 vl1 vl)
  20426. (while (string-match "|" line start)
  20427. (setq start (match-end 0))
  20428. (or (pop vl1) (setq line (replace-match " " t t line)))))
  20429. (push line rtn))
  20430. (nreverse rtn))))
  20431. (defun org-colgroup-info-to-vline-list (info)
  20432. (let (vl new last)
  20433. (while info
  20434. (setq last new new (pop info))
  20435. (if (or (memq last '(:end :startend))
  20436. (memq new '(:start :startend)))
  20437. (push t vl)
  20438. (push nil vl)))
  20439. (setq vl (cons nil (nreverse vl)))))
  20440. (defun org-format-table-html (lines olines)
  20441. "Find out which HTML converter to use and return the HTML code."
  20442. (if (stringp lines)
  20443. (setq lines (org-split-string lines "\n")))
  20444. (if (string-match "^[ \t]*|" (car lines))
  20445. ;; A normal org table
  20446. (org-format-org-table-html lines)
  20447. ;; Table made by table.el - test for spanning
  20448. (let* ((hlines (delq nil (mapcar
  20449. (lambda (x)
  20450. (if (string-match "^[ \t]*\\+-" x) x
  20451. nil))
  20452. lines)))
  20453. (first (car hlines))
  20454. (ll (and (string-match "\\S-+" first)
  20455. (match-string 0 first)))
  20456. (re (concat "^[ \t]*" (regexp-quote ll)))
  20457. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  20458. hlines))))
  20459. (if (and (not spanning)
  20460. (not org-export-prefer-native-exporter-for-tables))
  20461. ;; We can use my own converter with HTML conversions
  20462. (org-format-table-table-html lines)
  20463. ;; Need to use the code generator in table.el, with the original text.
  20464. (org-format-table-table-html-using-table-generate-source olines)))))
  20465. (defun org-format-org-table-html (lines &optional splice)
  20466. "Format a table into HTML."
  20467. ;; Get rid of hlines at beginning and end
  20468. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20469. (setq lines (nreverse lines))
  20470. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  20471. (setq lines (nreverse lines))
  20472. (when org-export-table-remove-special-lines
  20473. ;; Check if the table has a marking column. If yes remove the
  20474. ;; column and the special lines
  20475. (setq lines (org-table-clean-before-export lines)))
  20476. (let ((head (and org-export-highlight-first-table-line
  20477. (delq nil (mapcar
  20478. (lambda (x) (string-match "^[ \t]*|-" x))
  20479. (cdr lines)))))
  20480. (nlines 0) fnum i
  20481. tbopen line fields html gr colgropen)
  20482. (if splice (setq head nil))
  20483. (unless splice (push (if head "<thead>" "<tbody>") html))
  20484. (setq tbopen t)
  20485. (while (setq line (pop lines))
  20486. (catch 'next-line
  20487. (if (string-match "^[ \t]*|-" line)
  20488. (progn
  20489. (unless splice
  20490. (push (if head "</thead>" "</tbody>") html)
  20491. (if lines (push "<tbody>" html) (setq tbopen nil)))
  20492. (setq head nil) ;; head ends here, first time around
  20493. ;; ignore this line
  20494. (throw 'next-line t)))
  20495. ;; Break the line into fields
  20496. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  20497. (unless fnum (setq fnum (make-vector (length fields) 0)))
  20498. (setq nlines (1+ nlines) i -1)
  20499. (push (concat "<tr>"
  20500. (mapconcat
  20501. (lambda (x)
  20502. (setq i (1+ i))
  20503. (if (and (< i nlines)
  20504. (string-match org-table-number-regexp x))
  20505. (incf (aref fnum i)))
  20506. (if head
  20507. (concat (car org-export-table-header-tags) x
  20508. (cdr org-export-table-header-tags))
  20509. (concat (car org-export-table-data-tags) x
  20510. (cdr org-export-table-data-tags))))
  20511. fields "")
  20512. "</tr>")
  20513. html)))
  20514. (unless splice (if tbopen (push "</tbody>" html)))
  20515. (unless splice (push "</table>\n" html))
  20516. (setq html (nreverse html))
  20517. (unless splice
  20518. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  20519. (push (mapconcat
  20520. (lambda (x)
  20521. (setq gr (pop org-table-colgroup-info))
  20522. (format "%s<COL align=\"%s\"></COL>%s"
  20523. (if (memq gr '(:start :startend))
  20524. (prog1
  20525. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  20526. (setq colgropen t))
  20527. "")
  20528. (if (> (/ (float x) nlines) org-table-number-fraction)
  20529. "right" "left")
  20530. (if (memq gr '(:end :startend))
  20531. (progn (setq colgropen nil) "</colgroup>")
  20532. "")))
  20533. fnum "")
  20534. html)
  20535. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  20536. (push org-export-html-table-tag html))
  20537. (concat (mapconcat 'identity html "\n") "\n")))
  20538. (defun org-table-clean-before-export (lines)
  20539. "Check if the table has a marking column.
  20540. If yes remove the column and the special lines."
  20541. (setq org-table-colgroup-info nil)
  20542. (if (memq nil
  20543. (mapcar
  20544. (lambda (x) (or (string-match "^[ \t]*|-" x)
  20545. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  20546. lines))
  20547. (progn
  20548. (setq org-table-clean-did-remove-column nil)
  20549. (delq nil
  20550. (mapcar
  20551. (lambda (x)
  20552. (cond
  20553. ((string-match "^[ \t]*| */ *|" x)
  20554. (setq org-table-colgroup-info
  20555. (mapcar (lambda (x)
  20556. (cond ((member x '("<" "&lt;")) :start)
  20557. ((member x '(">" "&gt;")) :end)
  20558. ((member x '("<>" "&lt;&gt;")) :startend)
  20559. (t nil)))
  20560. (org-split-string x "[ \t]*|[ \t]*")))
  20561. nil)
  20562. (t x)))
  20563. lines)))
  20564. (setq org-table-clean-did-remove-column t)
  20565. (delq nil
  20566. (mapcar
  20567. (lambda (x)
  20568. (cond
  20569. ((string-match "^[ \t]*| */ *|" x)
  20570. (setq org-table-colgroup-info
  20571. (mapcar (lambda (x)
  20572. (cond ((member x '("<" "&lt;")) :start)
  20573. ((member x '(">" "&gt;")) :end)
  20574. ((member x '("<>" "&lt;&gt;")) :startend)
  20575. (t nil)))
  20576. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  20577. nil)
  20578. ((string-match "^[ \t]*| *[!_^/] *|" x)
  20579. nil) ; ignore this line
  20580. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  20581. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  20582. ;; remove the first column
  20583. (replace-match "\\1|" t nil x))
  20584. (t (error "This should not happen"))))
  20585. lines))))
  20586. (defun org-format-table-table-html (lines)
  20587. "Format a table generated by table.el into HTML.
  20588. This conversion does *not* use `table-generate-source' from table.el.
  20589. This has the advantage that Org-mode's HTML conversions can be used.
  20590. But it has the disadvantage, that no cell- or row-spanning is allowed."
  20591. (let (line field-buffer
  20592. (head org-export-highlight-first-table-line)
  20593. fields html empty)
  20594. (setq html (concat org-export-html-table-tag "\n"))
  20595. (while (setq line (pop lines))
  20596. (setq empty "&nbsp;")
  20597. (catch 'next-line
  20598. (if (string-match "^[ \t]*\\+-" line)
  20599. (progn
  20600. (if field-buffer
  20601. (progn
  20602. (setq
  20603. html
  20604. (concat
  20605. html
  20606. "<tr>"
  20607. (mapconcat
  20608. (lambda (x)
  20609. (if (equal x "") (setq x empty))
  20610. (if head
  20611. (concat (car org-export-table-header-tags) x
  20612. (cdr org-export-table-header-tags))
  20613. (concat (car org-export-table-data-tags) x
  20614. (cdr org-export-table-data-tags))))
  20615. field-buffer "\n")
  20616. "</tr>\n"))
  20617. (setq head nil)
  20618. (setq field-buffer nil)))
  20619. ;; Ignore this line
  20620. (throw 'next-line t)))
  20621. ;; Break the line into fields and store the fields
  20622. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  20623. (if field-buffer
  20624. (setq field-buffer (mapcar
  20625. (lambda (x)
  20626. (concat x "<br/>" (pop fields)))
  20627. field-buffer))
  20628. (setq field-buffer fields))))
  20629. (setq html (concat html "</table>\n"))
  20630. html))
  20631. (defun org-format-table-table-html-using-table-generate-source (lines)
  20632. "Format a table into html, using `table-generate-source' from table.el.
  20633. This has the advantage that cell- or row-spanning is allowed.
  20634. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  20635. (require 'table)
  20636. (with-current-buffer (get-buffer-create " org-tmp1 ")
  20637. (erase-buffer)
  20638. (insert (mapconcat 'identity lines "\n"))
  20639. (goto-char (point-min))
  20640. (if (not (re-search-forward "|[^+]" nil t))
  20641. (error "Error processing table"))
  20642. (table-recognize-table)
  20643. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  20644. (table-generate-source 'html " org-tmp2 ")
  20645. (set-buffer " org-tmp2 ")
  20646. (buffer-substring (point-min) (point-max))))
  20647. (defun org-html-handle-time-stamps (s)
  20648. "Format time stamps in string S, or remove them."
  20649. (catch 'exit
  20650. (let (r b)
  20651. (while (string-match org-maybe-keyword-time-regexp s)
  20652. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  20653. ;; never export CLOCK
  20654. (throw 'exit ""))
  20655. (or b (setq b (substring s 0 (match-beginning 0))))
  20656. (if (not org-export-with-timestamps)
  20657. (setq r (concat r (substring s 0 (match-beginning 0)))
  20658. s (substring s (match-end 0)))
  20659. (setq r (concat
  20660. r (substring s 0 (match-beginning 0))
  20661. (if (match-end 1)
  20662. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  20663. (match-string 1 s)))
  20664. (format " @<span class=\"timestamp\">%s@</span>"
  20665. (substring
  20666. (org-translate-time (match-string 3 s)) 1 -1)))
  20667. s (substring s (match-end 0)))))
  20668. ;; Line break if line started and ended with time stamp stuff
  20669. (if (not r)
  20670. s
  20671. (setq r (concat r s))
  20672. (unless (string-match "\\S-" (concat b s))
  20673. (setq r (concat r "@<br/>")))
  20674. r))))
  20675. (defun org-html-protect (s)
  20676. ;; convert & to &amp;, < to &lt; and > to &gt;
  20677. (let ((start 0))
  20678. (while (string-match "&" s start)
  20679. (setq s (replace-match "&amp;" t t s)
  20680. start (1+ (match-beginning 0))))
  20681. (while (string-match "<" s)
  20682. (setq s (replace-match "&lt;" t t s)))
  20683. (while (string-match ">" s)
  20684. (setq s (replace-match "&gt;" t t s))))
  20685. s)
  20686. (defun org-export-cleanup-toc-line (s)
  20687. "Remove tags and time staps from lines going into the toc."
  20688. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  20689. (setq s (replace-match "" t t s)))
  20690. (when org-export-remove-timestamps-from-toc
  20691. (while (string-match org-maybe-keyword-time-regexp s)
  20692. (setq s (replace-match "" t t s))))
  20693. (while (string-match org-bracket-link-regexp s)
  20694. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  20695. t t s)))
  20696. s)
  20697. (defun org-html-expand (string)
  20698. "Prepare STRING for HTML export. Applies all active conversions.
  20699. If there are links in the string, don't modify these."
  20700. (let* (m s l res)
  20701. (while (setq m (string-match org-bracket-link-regexp string))
  20702. (setq s (substring string 0 m)
  20703. l (match-string 0 string)
  20704. string (substring string (match-end 0)))
  20705. (push (org-html-do-expand s) res)
  20706. (push l res))
  20707. (push (org-html-do-expand string) res)
  20708. (apply 'concat (nreverse res))))
  20709. (defun org-html-do-expand (s)
  20710. "Apply all active conversions to translate special ASCII to HTML."
  20711. (setq s (org-html-protect s))
  20712. (if org-export-html-expand
  20713. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  20714. (setq s (replace-match "<\\1>" t nil s))))
  20715. (if org-export-with-emphasize
  20716. (setq s (org-export-html-convert-emphasize s)))
  20717. (if org-export-with-sub-superscripts
  20718. (setq s (org-export-html-convert-sub-super s)))
  20719. (if org-export-with-TeX-macros
  20720. (let ((start 0) wd ass)
  20721. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  20722. (setq wd (match-string 1 s))
  20723. (if (setq ass (assoc wd org-html-entities))
  20724. (setq s (replace-match (or (cdr ass)
  20725. (concat "&" (car ass) ";"))
  20726. t t s))
  20727. (setq start (+ start (length wd)))))))
  20728. s)
  20729. (defun org-create-multibrace-regexp (left right n)
  20730. "Create a regular expression which will match a balanced sexp.
  20731. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  20732. as single character strings.
  20733. The regexp returned will match the entire expression including the
  20734. delimiters. It will also define a single group which contains the
  20735. match except for the outermost delimiters. The maximum depth of
  20736. stacked delimiters is N. Escaping delimiters is not possible."
  20737. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  20738. (or "\\|")
  20739. (re nothing)
  20740. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  20741. (while (> n 1)
  20742. (setq n (1- n)
  20743. re (concat re or next)
  20744. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  20745. (concat left "\\(" re "\\)" right)))
  20746. (defvar org-match-substring-regexp
  20747. (concat
  20748. "\\([^\\]\\)\\([_^]\\)\\("
  20749. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  20750. "\\|"
  20751. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  20752. "\\|"
  20753. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  20754. "The regular expression matching a sub- or superscript.")
  20755. ;(let ((s "a\\_b"))
  20756. ; (and (string-match org-match-substring-regexp s)
  20757. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  20758. (defun org-export-html-convert-sub-super (string)
  20759. "Convert sub- and superscripts in STRING to HTML."
  20760. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  20761. (while (string-match org-match-substring-regexp string s)
  20762. (if (and requireb (match-end 8))
  20763. (setq s (match-end 2))
  20764. (setq s (match-end 1)
  20765. key (if (string= (match-string 2 string) "_") "sub" "sup")
  20766. c (or (match-string 8 string)
  20767. (match-string 6 string)
  20768. (match-string 5 string))
  20769. string (replace-match
  20770. (concat (match-string 1 string)
  20771. "<" key ">" c "</" key ">")
  20772. t t string))))
  20773. (while (string-match "\\\\\\([_^]\\)" string)
  20774. (setq string (replace-match (match-string 1 string) t t string)))
  20775. string))
  20776. (defun org-export-html-convert-emphasize (string)
  20777. "Apply emphasis."
  20778. (let ((s 0))
  20779. (while (string-match org-emph-re string s)
  20780. (if (not (equal
  20781. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  20782. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  20783. (setq string (replace-match
  20784. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  20785. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  20786. "\\5") t nil string))
  20787. (setq s (1+ s))))
  20788. string))
  20789. (defvar org-par-open nil)
  20790. (defun org-open-par ()
  20791. "Insert <p>, but first close previous paragraph if any."
  20792. (org-close-par-maybe)
  20793. (insert "\n<p>")
  20794. (setq org-par-open t))
  20795. (defun org-close-par-maybe ()
  20796. "Close paragraph if there is one open."
  20797. (when org-par-open
  20798. (insert "</p>")
  20799. (setq org-par-open nil)))
  20800. (defun org-close-li ()
  20801. "Close <li> if necessary."
  20802. (org-close-par-maybe)
  20803. (insert "</li>\n"))
  20804. (defvar body-only) ; dynamically scoped into this.
  20805. (defun org-html-level-start (level title umax with-toc head-count)
  20806. "Insert a new level in HTML export.
  20807. When TITLE is nil, just close all open levels."
  20808. (org-close-par-maybe)
  20809. (let ((l (1+ (max level umax))))
  20810. (while (<= l org-level-max)
  20811. (if (aref org-levels-open (1- l))
  20812. (progn
  20813. (org-html-level-close l)
  20814. (aset org-levels-open (1- l) nil)))
  20815. (setq l (1+ l)))
  20816. (when title
  20817. ;; If title is nil, this means this function is called to close
  20818. ;; all levels, so the rest is done only if title is given
  20819. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  20820. (setq title (replace-match
  20821. (if org-export-with-tags
  20822. (save-match-data
  20823. (concat
  20824. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  20825. (mapconcat 'identity (org-split-string
  20826. (match-string 1 title) ":")
  20827. "&nbsp;")
  20828. "</span>"))
  20829. "")
  20830. t t title)))
  20831. (if (> level umax)
  20832. (progn
  20833. (if (aref org-levels-open (1- level))
  20834. (progn
  20835. (org-close-li)
  20836. (insert "<li>" title "<br/>\n"))
  20837. (aset org-levels-open (1- level) t)
  20838. (org-close-par-maybe)
  20839. (insert "<ul>\n<li>" title "<br/>\n")))
  20840. (if (and org-export-with-section-numbers (not body-only))
  20841. (setq title (concat (org-section-number level) " " title)))
  20842. (setq level (+ level org-export-html-toplevel-hlevel -1))
  20843. (if with-toc
  20844. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  20845. level head-count title level))
  20846. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  20847. (org-open-par)))))
  20848. (defun org-html-level-close (&rest args)
  20849. "Terminate one level in HTML export."
  20850. (org-close-li)
  20851. (insert "</ul>\n"))
  20852. ;;; iCalendar export
  20853. ;;;###autoload
  20854. (defun org-export-icalendar-this-file ()
  20855. "Export current file as an iCalendar file.
  20856. The iCalendar file will be located in the same directory as the Org-mode
  20857. file, but with extension `.ics'."
  20858. (interactive)
  20859. (org-export-icalendar nil buffer-file-name))
  20860. ;;;###autoload
  20861. (defun org-export-icalendar-all-agenda-files ()
  20862. "Export all files in `org-agenda-files' to iCalendar .ics files.
  20863. Each iCalendar file will be located in the same directory as the Org-mode
  20864. file, but with extension `.ics'."
  20865. (interactive)
  20866. (apply 'org-export-icalendar nil (org-agenda-files t)))
  20867. ;;;###autoload
  20868. (defun org-export-icalendar-combine-agenda-files ()
  20869. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  20870. The file is stored under the name `org-combined-agenda-icalendar-file'."
  20871. (interactive)
  20872. (apply 'org-export-icalendar t (org-agenda-files t)))
  20873. (defun org-export-icalendar (combine &rest files)
  20874. "Create iCalendar files for all elements of FILES.
  20875. If COMBINE is non-nil, combine all calendar entries into a single large
  20876. file and store it under the name `org-combined-agenda-icalendar-file'."
  20877. (save-excursion
  20878. (org-prepare-agenda-buffers files)
  20879. (let* ((dir (org-export-directory
  20880. :ical (list :publishing-directory
  20881. org-export-publishing-directory)))
  20882. file ical-file ical-buffer category started org-agenda-new-buffers)
  20883. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  20884. (when combine
  20885. (setq ical-file
  20886. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  20887. org-combined-agenda-icalendar-file
  20888. (expand-file-name org-combined-agenda-icalendar-file dir))
  20889. ical-buffer (org-get-agenda-file-buffer ical-file))
  20890. (set-buffer ical-buffer) (erase-buffer))
  20891. (while (setq file (pop files))
  20892. (catch 'nextfile
  20893. (org-check-agenda-file file)
  20894. (set-buffer (org-get-agenda-file-buffer file))
  20895. (unless combine
  20896. (setq ical-file (concat (file-name-as-directory dir)
  20897. (file-name-sans-extension
  20898. (file-name-nondirectory buffer-file-name))
  20899. ".ics"))
  20900. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  20901. (with-current-buffer ical-buffer (erase-buffer)))
  20902. (setq category (or org-category
  20903. (file-name-sans-extension
  20904. (file-name-nondirectory buffer-file-name))))
  20905. (if (symbolp category) (setq category (symbol-name category)))
  20906. (let ((standard-output ical-buffer))
  20907. (if combine
  20908. (and (not started) (setq started t)
  20909. (org-start-icalendar-file org-icalendar-combined-name))
  20910. (org-start-icalendar-file category))
  20911. (org-print-icalendar-entries combine)
  20912. (when (or (and combine (not files)) (not combine))
  20913. (org-finish-icalendar-file)
  20914. (set-buffer ical-buffer)
  20915. (save-buffer)
  20916. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  20917. (org-release-buffers org-agenda-new-buffers))))
  20918. (defvar org-after-save-iCalendar-file-hook nil
  20919. "Hook run after an iCalendar file has been saved.
  20920. The iCalendar buffer is still current when this hook is run.
  20921. A good way to use this is to tell a desktop calenndar application to re-read
  20922. the iCalendar file.")
  20923. (defun org-print-icalendar-entries (&optional combine)
  20924. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  20925. When COMBINE is non nil, add the category to each line."
  20926. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  20927. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  20928. (org-category-table (org-get-category-table))
  20929. (dts (org-ical-ts-to-string
  20930. (format-time-string (cdr org-time-stamp-formats) (current-time))
  20931. "DTSTART"))
  20932. hd ts ts2 state status (inc t) pos b sexp rrule
  20933. scheduledp deadlinep tmp pri category
  20934. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  20935. (save-excursion
  20936. (goto-char (point-min))
  20937. (while (re-search-forward re1 nil t)
  20938. (catch :skip
  20939. (org-agenda-skip)
  20940. (setq pos (match-beginning 0)
  20941. ts (match-string 0)
  20942. inc t
  20943. hd (org-get-heading)
  20944. category (org-get-category))
  20945. (if (looking-at re2)
  20946. (progn
  20947. (goto-char (match-end 0))
  20948. (setq ts2 (match-string 1) inc nil))
  20949. (setq tmp (buffer-substring (max (point-min)
  20950. (- pos org-ds-keyword-length))
  20951. pos)
  20952. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  20953. (progn
  20954. (setq inc nil)
  20955. (replace-match "\\1" t nil ts))
  20956. ts)
  20957. deadlinep (string-match org-deadline-regexp tmp)
  20958. scheduledp (string-match org-scheduled-regexp tmp)
  20959. ;; donep (org-entry-is-done-p)
  20960. ))
  20961. (if (or (string-match org-tr-regexp hd)
  20962. (string-match org-ts-regexp hd))
  20963. (setq hd (replace-match "" t t hd)))
  20964. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  20965. (setq rrule
  20966. (concat "\nRRULE:FREQ="
  20967. (cdr (assoc
  20968. (match-string 2 ts)
  20969. '(("d" . "DAILY")("w" . "WEEKLY")
  20970. ("m" . "MONTHLY")("y" . "YEARLY"))))
  20971. ";INTERVAL=" (match-string 1 ts)))
  20972. (setq rrule ""))
  20973. (if (string-match org-bracket-link-regexp hd)
  20974. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  20975. (match-string 1 hd))
  20976. t t hd)))
  20977. (if deadlinep (setq hd (concat "DL: " hd)))
  20978. (if scheduledp (setq hd (concat "S: " hd)))
  20979. (if (string-match "\\`<%%" ts)
  20980. (with-current-buffer sexp-buffer
  20981. (insert (substring ts 1 -1) " " hd "\n"))
  20982. (princ (format "BEGIN:VEVENT
  20983. %s
  20984. %s%s
  20985. SUMMARY:%s
  20986. CATEGORIES:%s
  20987. END:VEVENT\n"
  20988. (org-ical-ts-to-string ts "DTSTART")
  20989. (org-ical-ts-to-string ts2 "DTEND" inc)
  20990. rrule hd category)))))
  20991. (when (and org-icalendar-include-sexps
  20992. (condition-case nil (require 'icalendar) (error nil))
  20993. (fboundp 'icalendar-export-region))
  20994. ;; Get all the literal sexps
  20995. (goto-char (point-min))
  20996. (while (re-search-forward "^&?%%(" nil t)
  20997. (catch :skip
  20998. (org-agenda-skip)
  20999. (setq b (match-beginning 0))
  21000. (goto-char (1- (match-end 0)))
  21001. (forward-sexp 1)
  21002. (end-of-line 1)
  21003. (setq sexp (buffer-substring b (point)))
  21004. (with-current-buffer sexp-buffer
  21005. (insert sexp "\n"))
  21006. (princ (org-diary-to-ical-string sexp-buffer)))))
  21007. (when org-icalendar-include-todo
  21008. (goto-char (point-min))
  21009. (while (re-search-forward org-todo-line-regexp nil t)
  21010. (catch :skip
  21011. (org-agenda-skip)
  21012. (setq state (match-string 2))
  21013. (setq status (if (member state org-done-keywords)
  21014. "COMPLETED" "NEEDS-ACTION"))
  21015. (when (and state
  21016. (or (not (member state org-done-keywords))
  21017. (eq org-icalendar-include-todo 'all))
  21018. (not (member org-archive-tag (org-get-tags-at)))
  21019. )
  21020. (setq hd (match-string 3))
  21021. (if (string-match org-bracket-link-regexp hd)
  21022. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  21023. (match-string 1 hd))
  21024. t t hd)))
  21025. (if (string-match org-priority-regexp hd)
  21026. (setq pri (string-to-char (match-string 2 hd))
  21027. hd (concat (substring hd 0 (match-beginning 1))
  21028. (substring hd (match-end 1))))
  21029. (setq pri org-default-priority))
  21030. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  21031. (- org-lowest-priority org-highest-priority))))))
  21032. (princ (format "BEGIN:VTODO
  21033. %s
  21034. SUMMARY:%s
  21035. CATEGORIES:%s
  21036. SEQUENCE:1
  21037. PRIORITY:%d
  21038. STATUS:%s
  21039. END:VTODO\n"
  21040. dts hd category pri status)))))))))
  21041. (defun org-start-icalendar-file (name)
  21042. "Start an iCalendar file by inserting the header."
  21043. (let ((user user-full-name)
  21044. (name (or name "unknown"))
  21045. (timezone (cadr (current-time-zone))))
  21046. (princ
  21047. (format "BEGIN:VCALENDAR
  21048. VERSION:2.0
  21049. X-WR-CALNAME:%s
  21050. PRODID:-//%s//Emacs with Org-mode//EN
  21051. X-WR-TIMEZONE:%s
  21052. CALSCALE:GREGORIAN\n" name user timezone))))
  21053. (defun org-finish-icalendar-file ()
  21054. "Finish an iCalendar file by inserting the END statement."
  21055. (princ "END:VCALENDAR\n"))
  21056. (defun org-ical-ts-to-string (s keyword &optional inc)
  21057. "Take a time string S and convert it to iCalendar format.
  21058. KEYWORD is added in front, to make a complete line like DTSTART....
  21059. When INC is non-nil, increase the hour by two (if time string contains
  21060. a time), or the day by one (if it does not contain a time)."
  21061. (let ((t1 (org-parse-time-string s 'nodefault))
  21062. t2 fmt have-time time)
  21063. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  21064. (setq t2 t1 have-time t)
  21065. (setq t2 (org-parse-time-string s)))
  21066. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  21067. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  21068. (when inc
  21069. (if have-time
  21070. (if org-agenda-default-appointment-duration
  21071. (setq mi (+ org-agenda-default-appointment-duration mi))
  21072. (setq h (+ 2 h)))
  21073. (setq d (1+ d))))
  21074. (setq time (encode-time s mi h d m y)))
  21075. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  21076. (concat keyword (format-time-string fmt time))))
  21077. ;;; XOXO export
  21078. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  21079. (with-current-buffer buffer
  21080. (apply 'insert output)))
  21081. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  21082. (defun org-export-as-xoxo (&optional buffer)
  21083. "Export the org buffer as XOXO.
  21084. The XOXO buffer is named *xoxo-<source buffer name>*"
  21085. (interactive (list (current-buffer)))
  21086. ;; A quickie abstraction
  21087. ;; Output everything as XOXO
  21088. (with-current-buffer (get-buffer buffer)
  21089. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  21090. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21091. (org-infile-export-plist)))
  21092. (filename (concat (file-name-as-directory
  21093. (org-export-directory :xoxo opt-plist))
  21094. (file-name-sans-extension
  21095. (file-name-nondirectory buffer-file-name))
  21096. ".html"))
  21097. (out (find-file-noselect filename))
  21098. (last-level 1)
  21099. (hanging-li nil))
  21100. ;; Check the output buffer is empty.
  21101. (with-current-buffer out (erase-buffer))
  21102. ;; Kick off the output
  21103. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  21104. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  21105. (let* ((hd (match-string-no-properties 1))
  21106. (level (length hd))
  21107. (text (concat
  21108. (match-string-no-properties 2)
  21109. (save-excursion
  21110. (goto-char (match-end 0))
  21111. (let ((str ""))
  21112. (catch 'loop
  21113. (while 't
  21114. (forward-line)
  21115. (if (looking-at "^[ \t]\\(.*\\)")
  21116. (setq str (concat str (match-string-no-properties 1)))
  21117. (throw 'loop str)))))))))
  21118. ;; Handle level rendering
  21119. (cond
  21120. ((> level last-level)
  21121. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  21122. ((< level last-level)
  21123. (dotimes (- (- last-level level) 1)
  21124. (if hanging-li
  21125. (org-export-as-xoxo-insert-into out "</li>\n"))
  21126. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21127. (when hanging-li
  21128. (org-export-as-xoxo-insert-into out "</li>\n")
  21129. (setq hanging-li nil)))
  21130. ((equal level last-level)
  21131. (if hanging-li
  21132. (org-export-as-xoxo-insert-into out "</li>\n")))
  21133. )
  21134. (setq last-level level)
  21135. ;; And output the new li
  21136. (setq hanging-li 't)
  21137. (if (equal ?+ (elt text 0))
  21138. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  21139. (org-export-as-xoxo-insert-into out "<li>" text))))
  21140. ;; Finally finish off the ol
  21141. (dotimes (- last-level 1)
  21142. (if hanging-li
  21143. (org-export-as-xoxo-insert-into out "</li>\n"))
  21144. (org-export-as-xoxo-insert-into out "</ol>\n"))
  21145. ;; Finish the buffer off and clean it up.
  21146. (switch-to-buffer-other-window out)
  21147. (indent-region (point-min) (point-max) nil)
  21148. (save-buffer)
  21149. (goto-char (point-min))
  21150. )))
  21151. ;;;; Key bindings
  21152. ;; Make `C-c C-x' a prefix key
  21153. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  21154. ;; TAB key with modifiers
  21155. (org-defkey org-mode-map "\C-i" 'org-cycle)
  21156. (org-defkey org-mode-map [(tab)] 'org-cycle)
  21157. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  21158. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  21159. (org-defkey org-mode-map "\M-\t" 'org-complete)
  21160. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  21161. ;; The following line is necessary under Suse GNU/Linux
  21162. (unless (featurep 'xemacs)
  21163. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  21164. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  21165. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  21166. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  21167. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  21168. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  21169. ;; Cursor keys with modifiers
  21170. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  21171. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  21172. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  21173. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  21174. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  21175. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  21176. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  21177. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  21178. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  21179. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  21180. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  21181. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  21182. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  21183. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  21184. ;;; Extra keys for tty access.
  21185. ;; We only set them when really needed because otherwise the
  21186. ;; menus don't show the simple keys
  21187. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  21188. (not window-system))
  21189. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  21190. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  21191. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  21192. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  21193. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  21194. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  21195. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  21196. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  21197. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  21198. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  21199. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  21200. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  21201. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  21202. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  21203. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  21204. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  21205. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  21206. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  21207. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  21208. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  21209. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  21210. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  21211. ;; All the other keys
  21212. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  21213. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  21214. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  21215. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  21216. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  21217. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  21218. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  21219. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  21220. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  21221. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  21222. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  21223. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  21224. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  21225. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  21226. (org-defkey org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  21227. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  21228. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  21229. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  21230. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  21231. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  21232. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  21233. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  21234. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  21235. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  21236. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  21237. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  21238. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  21239. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  21240. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  21241. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  21242. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  21243. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  21244. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  21245. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  21246. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  21247. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  21248. (org-defkey org-mode-map "\C-c^" 'org-sort)
  21249. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  21250. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  21251. (org-defkey org-mode-map "\C-m" 'org-return)
  21252. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  21253. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  21254. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  21255. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  21256. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  21257. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  21258. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  21259. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  21260. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  21261. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  21262. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  21263. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  21264. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  21265. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  21266. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  21267. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  21268. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  21269. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  21270. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  21271. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  21272. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  21273. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  21274. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  21275. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  21276. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  21277. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  21278. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  21279. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  21280. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  21281. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  21282. (when (featurep 'xemacs)
  21283. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  21284. (defsubst org-table-p () (org-at-table-p))
  21285. (defun org-self-insert-command (N)
  21286. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  21287. If the cursor is in a table looking at whitespace, the whitespace is
  21288. overwritten, and the table is not marked as requiring realignment."
  21289. (interactive "p")
  21290. (if (and (org-table-p)
  21291. (progn
  21292. ;; check if we blank the field, and if that triggers align
  21293. (and org-table-auto-blank-field
  21294. (member last-command
  21295. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  21296. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  21297. ;; got extra space, this field does not determine column width
  21298. (let (org-table-may-need-update) (org-table-blank-field))
  21299. ;; no extra space, this field may determine column width
  21300. (org-table-blank-field)))
  21301. t)
  21302. (eq N 1)
  21303. (looking-at "[^|\n]* |"))
  21304. (let (org-table-may-need-update)
  21305. (goto-char (1- (match-end 0)))
  21306. (delete-backward-char 1)
  21307. (goto-char (match-beginning 0))
  21308. (self-insert-command N))
  21309. (setq org-table-may-need-update t)
  21310. (self-insert-command N)
  21311. (org-fix-tags-on-the-fly)))
  21312. (defun org-fix-tags-on-the-fly ()
  21313. (when (and (equal (char-after (point-at-bol)) ?*)
  21314. (org-on-heading-p))
  21315. (org-align-tags-here org-tags-column)))
  21316. (defun org-delete-backward-char (N)
  21317. "Like `delete-backward-char', insert whitespace at field end in tables.
  21318. When deleting backwards, in tables this function will insert whitespace in
  21319. front of the next \"|\" separator, to keep the table aligned. The table will
  21320. still be marked for re-alignment if the field did fill the entire column,
  21321. because, in this case the deletion might narrow the column."
  21322. (interactive "p")
  21323. (if (and (org-table-p)
  21324. (eq N 1)
  21325. (string-match "|" (buffer-substring (point-at-bol) (point)))
  21326. (looking-at ".*?|"))
  21327. (let ((pos (point))
  21328. (noalign (looking-at "[^|\n\r]* |"))
  21329. (c org-table-may-need-update))
  21330. (backward-delete-char N)
  21331. (skip-chars-forward "^|")
  21332. (insert " ")
  21333. (goto-char (1- pos))
  21334. ;; noalign: if there were two spaces at the end, this field
  21335. ;; does not determine the width of the column.
  21336. (if noalign (setq org-table-may-need-update c)))
  21337. (backward-delete-char N)
  21338. (org-fix-tags-on-the-fly)))
  21339. (defun org-delete-char (N)
  21340. "Like `delete-char', but insert whitespace at field end in tables.
  21341. When deleting characters, in tables this function will insert whitespace in
  21342. front of the next \"|\" separator, to keep the table aligned. The table will
  21343. still be marked for re-alignment if the field did fill the entire column,
  21344. because, in this case the deletion might narrow the column."
  21345. (interactive "p")
  21346. (if (and (org-table-p)
  21347. (not (bolp))
  21348. (not (= (char-after) ?|))
  21349. (eq N 1))
  21350. (if (looking-at ".*?|")
  21351. (let ((pos (point))
  21352. (noalign (looking-at "[^|\n\r]* |"))
  21353. (c org-table-may-need-update))
  21354. (replace-match (concat
  21355. (substring (match-string 0) 1 -1)
  21356. " |"))
  21357. (goto-char pos)
  21358. ;; noalign: if there were two spaces at the end, this field
  21359. ;; does not determine the width of the column.
  21360. (if noalign (setq org-table-may-need-update c)))
  21361. (delete-char N))
  21362. (delete-char N)
  21363. (org-fix-tags-on-the-fly)))
  21364. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  21365. (put 'org-self-insert-command 'delete-selection t)
  21366. (put 'orgtbl-self-insert-command 'delete-selection t)
  21367. (put 'org-delete-char 'delete-selection 'supersede)
  21368. (put 'org-delete-backward-char 'delete-selection 'supersede)
  21369. ;; Make `flyspell-mode' delay after some commands
  21370. (put 'org-self-insert-command 'flyspell-delayed t)
  21371. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  21372. (put 'org-delete-char 'flyspell-delayed t)
  21373. (put 'org-delete-backward-char 'flyspell-delayed t)
  21374. ;; How to do this: Measure non-white length of current string
  21375. ;; If equal to column width, we should realign.
  21376. (defun org-remap (map &rest commands)
  21377. "In MAP, remap the functions given in COMMANDS.
  21378. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  21379. (let (new old)
  21380. (while commands
  21381. (setq old (pop commands) new (pop commands))
  21382. (if (fboundp 'command-remapping)
  21383. (org-defkey map (vector 'remap old) new)
  21384. (substitute-key-definition old new map global-map)))))
  21385. (when (eq org-enable-table-editor 'optimized)
  21386. ;; If the user wants maximum table support, we need to hijack
  21387. ;; some standard editing functions
  21388. (org-remap org-mode-map
  21389. 'self-insert-command 'org-self-insert-command
  21390. 'delete-char 'org-delete-char
  21391. 'delete-backward-char 'org-delete-backward-char)
  21392. (org-defkey org-mode-map "|" 'org-force-self-insert))
  21393. (defun org-shiftcursor-error ()
  21394. "Throw an error because Shift-Cursor command was applied in wrong context."
  21395. (error "This command is active in special context like tables, headlines or timestamps"))
  21396. (defun org-shifttab (&optional arg)
  21397. "Global visibility cycling or move to previous table field.
  21398. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  21399. on context.
  21400. See the individual commands for more information."
  21401. (interactive "P")
  21402. (cond
  21403. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  21404. (arg (message "Content view to level: ")
  21405. (org-content (prefix-numeric-value arg))
  21406. (setq org-cycle-global-status 'overview))
  21407. (t (call-interactively 'org-global-cycle))))
  21408. (defun org-shiftmetaleft ()
  21409. "Promote subtree or delete table column.
  21410. Calls `org-promote-subtree', `org-outdent-item',
  21411. or `org-table-delete-column', depending on context.
  21412. See the individual commands for more information."
  21413. (interactive)
  21414. (cond
  21415. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  21416. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  21417. ((org-at-item-p) (call-interactively 'org-outdent-item))
  21418. (t (org-shiftcursor-error))))
  21419. (defun org-shiftmetaright ()
  21420. "Demote subtree or insert table column.
  21421. Calls `org-demote-subtree', `org-indent-item',
  21422. or `org-table-insert-column', depending on context.
  21423. See the individual commands for more information."
  21424. (interactive)
  21425. (cond
  21426. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  21427. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  21428. ((org-at-item-p) (call-interactively 'org-indent-item))
  21429. (t (org-shiftcursor-error))))
  21430. (defun org-shiftmetaup (&optional arg)
  21431. "Move subtree up or kill table row.
  21432. Calls `org-move-subtree-up' or `org-table-kill-row' or
  21433. `org-move-item-up' depending on context. See the individual commands
  21434. for more information."
  21435. (interactive "P")
  21436. (cond
  21437. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  21438. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  21439. ((org-at-item-p) (call-interactively 'org-move-item-up))
  21440. (t (org-shiftcursor-error))))
  21441. (defun org-shiftmetadown (&optional arg)
  21442. "Move subtree down or insert table row.
  21443. Calls `org-move-subtree-down' or `org-table-insert-row' or
  21444. `org-move-item-down', depending on context. See the individual
  21445. commands for more information."
  21446. (interactive "P")
  21447. (cond
  21448. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  21449. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  21450. ((org-at-item-p) (call-interactively 'org-move-item-down))
  21451. (t (org-shiftcursor-error))))
  21452. (defun org-metaleft (&optional arg)
  21453. "Promote heading or move table column to left.
  21454. Calls `org-do-promote' or `org-table-move-column', depending on context.
  21455. With no specific context, calls the Emacs default `backward-word'.
  21456. See the individual commands for more information."
  21457. (interactive "P")
  21458. (cond
  21459. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  21460. ((or (org-on-heading-p) (org-region-active-p))
  21461. (call-interactively 'org-do-promote))
  21462. ((org-at-item-p) (call-interactively 'org-outdent-item))
  21463. (t (call-interactively 'backward-word))))
  21464. (defun org-metaright (&optional arg)
  21465. "Demote subtree or move table column to right.
  21466. Calls `org-do-demote' or `org-table-move-column', depending on context.
  21467. With no specific context, calls the Emacs default `forward-word'.
  21468. See the individual commands for more information."
  21469. (interactive "P")
  21470. (cond
  21471. ((org-at-table-p) (call-interactively 'org-table-move-column))
  21472. ((or (org-on-heading-p) (org-region-active-p))
  21473. (call-interactively 'org-do-demote))
  21474. ((org-at-item-p) (call-interactively 'org-indent-item))
  21475. (t (call-interactively 'forward-word))))
  21476. (defun org-metaup (&optional arg)
  21477. "Move subtree up or move table row up.
  21478. Calls `org-move-subtree-up' or `org-table-move-row' or
  21479. `org-move-item-up', depending on context. See the individual commands
  21480. for more information."
  21481. (interactive "P")
  21482. (cond
  21483. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  21484. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  21485. ((org-at-item-p) (call-interactively 'org-move-item-up))
  21486. (t (org-shiftcursor-error))))
  21487. (defun org-metadown (&optional arg)
  21488. "Move subtree down or move table row down.
  21489. Calls `org-move-subtree-down' or `org-table-move-row' or
  21490. `org-move-item-down', depending on context. See the individual
  21491. commands for more information."
  21492. (interactive "P")
  21493. (cond
  21494. ((org-at-table-p) (call-interactively 'org-table-move-row))
  21495. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  21496. ((org-at-item-p) (call-interactively 'org-move-item-down))
  21497. (t (org-shiftcursor-error))))
  21498. (defun org-shiftup (&optional arg)
  21499. "Increase item in timestamp or increase priority of current headline.
  21500. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  21501. depending on context. See the individual commands for more information."
  21502. (interactive "P")
  21503. (cond
  21504. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  21505. ((org-on-heading-p) (call-interactively 'org-priority-up))
  21506. ((org-at-item-p) (call-interactively 'org-previous-item))
  21507. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  21508. (defun org-shiftdown (&optional arg)
  21509. "Decrease item in timestamp or decrease priority of current headline.
  21510. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  21511. depending on context. See the individual commands for more information."
  21512. (interactive "P")
  21513. (cond
  21514. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  21515. ((org-on-heading-p) (call-interactively 'org-priority-down))
  21516. (t (call-interactively 'org-next-item))))
  21517. (defun org-shiftright ()
  21518. "Next TODO keyword or timestamp one day later, depending on context."
  21519. (interactive)
  21520. (cond
  21521. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  21522. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  21523. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  21524. (t (org-shiftcursor-error))))
  21525. (defun org-shiftleft ()
  21526. "Previous TODO keyword or timestamp one day earlier, depending on context."
  21527. (interactive)
  21528. (cond
  21529. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  21530. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  21531. ((org-at-property-p)
  21532. (call-interactively 'org-property-previous-allowed-value))
  21533. (t (org-shiftcursor-error))))
  21534. (defun org-shiftcontrolright ()
  21535. "Switch to next TODO set."
  21536. (interactive)
  21537. (cond
  21538. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  21539. (t (org-shiftcursor-error))))
  21540. (defun org-shiftcontrolleft ()
  21541. "Switch to previous TODO set."
  21542. (interactive)
  21543. (cond
  21544. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  21545. (t (org-shiftcursor-error))))
  21546. (defun org-ctrl-c-ret ()
  21547. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  21548. (interactive)
  21549. (cond
  21550. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  21551. (t (call-interactively 'org-insert-heading))))
  21552. (defun org-copy-special ()
  21553. "Copy region in table or copy current subtree.
  21554. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  21555. See the individual commands for more information."
  21556. (interactive)
  21557. (call-interactively
  21558. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  21559. (defun org-cut-special ()
  21560. "Cut region in table or cut current subtree.
  21561. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  21562. See the individual commands for more information."
  21563. (interactive)
  21564. (call-interactively
  21565. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  21566. (defun org-paste-special (arg)
  21567. "Paste rectangular region into table, or past subtree relative to level.
  21568. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  21569. See the individual commands for more information."
  21570. (interactive "P")
  21571. (if (org-at-table-p)
  21572. (org-table-paste-rectangle)
  21573. (org-paste-subtree arg)))
  21574. (defun org-ctrl-c-ctrl-c (&optional arg)
  21575. "Set tags in headline, or update according to changed information at point.
  21576. This command does many different things, depending on context:
  21577. - If the cursor is in a headline, prompt for tags and insert them
  21578. into the current line, aligned to `org-tags-column'. When called
  21579. with prefix arg, realign all tags in the current buffer.
  21580. - If the cursor is in one of the special #+KEYWORD lines, this
  21581. triggers scanning the buffer for these lines and updating the
  21582. information.
  21583. - If the cursor is inside a table, realign the table. This command
  21584. works even if the automatic table editor has been turned off.
  21585. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  21586. the entire table.
  21587. - If the cursor is inside a table created by the table.el package,
  21588. activate that table.
  21589. - If the current buffer is a remember buffer, close note and file it.
  21590. with a prefix argument, file it without further interaction to the default
  21591. location.
  21592. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  21593. links in this buffer.
  21594. - If the cursor is on a numbered item in a plain list, renumber the
  21595. ordered list."
  21596. (interactive "P")
  21597. (let ((org-enable-table-editor t))
  21598. (cond
  21599. ((or org-clock-overlays
  21600. org-occur-highlights
  21601. org-latex-fragment-image-overlays)
  21602. (org-remove-clock-overlays)
  21603. (org-remove-occur-highlights)
  21604. (org-remove-latex-fragment-image-overlays)
  21605. (message "Temporary highlights/overlays removed from current buffer"))
  21606. ((and (local-variable-p 'org-finish-function (current-buffer))
  21607. (fboundp org-finish-function))
  21608. (funcall org-finish-function))
  21609. ((org-at-property-p)
  21610. (call-interactively 'org-property-action))
  21611. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  21612. ((org-on-heading-p) (call-interactively 'org-set-tags))
  21613. ((org-at-table.el-p)
  21614. (require 'table)
  21615. (beginning-of-line 1)
  21616. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  21617. (call-interactively 'table-recognize-table))
  21618. ((org-at-table-p)
  21619. (org-table-maybe-eval-formula)
  21620. (if arg
  21621. (call-interactively 'org-table-recalculate)
  21622. (org-table-maybe-recalculate-line))
  21623. (call-interactively 'org-table-align))
  21624. ((org-at-item-checkbox-p)
  21625. (call-interactively 'org-toggle-checkbox))
  21626. ((org-at-item-p)
  21627. (call-interactively 'org-maybe-renumber-ordered-list))
  21628. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  21629. (cond
  21630. ((equal (match-string 1) "TBLFM")
  21631. ;; Recalculate the table before this line
  21632. (save-excursion
  21633. (beginning-of-line 1)
  21634. (skip-chars-backward " \r\n\t")
  21635. (if (org-at-table-p)
  21636. (org-call-with-arg 'org-table-recalculate t))))
  21637. (t
  21638. (call-interactively 'org-mode-restart))))
  21639. (t (error "C-c C-c can do nothing useful at this location.")))))
  21640. (defun org-mode-restart ()
  21641. "Restart Org-mode, to scan again for special lines.
  21642. Also updates the keyword regular expressions."
  21643. (interactive)
  21644. (let ((org-inhibit-startup t)) (org-mode))
  21645. (message "Org-mode restarted to refresh keyword and special line setup"))
  21646. (defun org-return ()
  21647. "Goto next table row or insert a newline.
  21648. Calls `org-table-next-row' or `newline', depending on context.
  21649. See the individual commands for more information."
  21650. (interactive)
  21651. (cond
  21652. ((bobp) (newline))
  21653. ((org-at-table-p)
  21654. (org-table-justify-field-maybe)
  21655. (call-interactively 'org-table-next-row))
  21656. (t (newline))))
  21657. (defun org-ctrl-c-minus ()
  21658. "Insert separator line in table or modify bullet type in list.
  21659. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  21660. depending on context."
  21661. (interactive)
  21662. (cond
  21663. ((org-at-table-p)
  21664. (call-interactively 'org-table-insert-hline))
  21665. ((org-in-item-p)
  21666. (call-interactively 'org-cycle-list-bullet))
  21667. (t (error "`C-c -' does have no function here."))))
  21668. (defun org-meta-return (&optional arg)
  21669. "Insert a new heading or wrap a region in a table.
  21670. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  21671. See the individual commands for more information."
  21672. (interactive "P")
  21673. (cond
  21674. ((org-at-table-p)
  21675. (call-interactively 'org-table-wrap-region))
  21676. (t (call-interactively 'org-insert-heading))))
  21677. ;;; Menu entries
  21678. ;; Define the Org-mode menus
  21679. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  21680. '("Tbl"
  21681. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  21682. ["Next Field" org-cycle (org-at-table-p)]
  21683. ["Previous Field" org-shifttab (org-at-table-p)]
  21684. ["Next Row" org-return (org-at-table-p)]
  21685. "--"
  21686. ["Blank Field" org-table-blank-field (org-at-table-p)]
  21687. ["Edit Field" org-table-edit-field (org-at-table-p)]
  21688. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  21689. "--"
  21690. ("Column"
  21691. ["Move Column Left" org-metaleft (org-at-table-p)]
  21692. ["Move Column Right" org-metaright (org-at-table-p)]
  21693. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  21694. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  21695. ("Row"
  21696. ["Move Row Up" org-metaup (org-at-table-p)]
  21697. ["Move Row Down" org-metadown (org-at-table-p)]
  21698. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  21699. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  21700. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  21701. "--"
  21702. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  21703. ("Rectangle"
  21704. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  21705. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  21706. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  21707. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  21708. "--"
  21709. ("Calculate"
  21710. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  21711. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  21712. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  21713. "--"
  21714. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  21715. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  21716. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  21717. "--"
  21718. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  21719. "--"
  21720. ["Sum Column/Rectangle" org-table-sum
  21721. (or (org-at-table-p) (org-region-active-p))]
  21722. ["Which Column?" org-table-current-column (org-at-table-p)])
  21723. ["Debug Formulas"
  21724. org-table-toggle-formula-debugger
  21725. :style toggle :selected org-table-formula-debug]
  21726. ["Show Col/Row Numbers"
  21727. org-table-toggle-coordinate-overlays
  21728. :style toggle :selected org-table-overlay-coordinates]
  21729. "--"
  21730. ["Create" org-table-create (and (not (org-at-table-p))
  21731. org-enable-table-editor)]
  21732. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  21733. ["Import from File" org-table-import (not (org-at-table-p))]
  21734. ["Export to File" org-table-export (org-at-table-p)]
  21735. "--"
  21736. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  21737. (easy-menu-define org-org-menu org-mode-map "Org menu"
  21738. '("Org"
  21739. ("Show/Hide"
  21740. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  21741. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  21742. ["Sparse Tree" org-occur t]
  21743. ["Reveal Context" org-reveal t]
  21744. ["Show All" show-all t]
  21745. "--"
  21746. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  21747. "--"
  21748. ["New Heading" org-insert-heading t]
  21749. ("Navigate Headings"
  21750. ["Up" outline-up-heading t]
  21751. ["Next" outline-next-visible-heading t]
  21752. ["Previous" outline-previous-visible-heading t]
  21753. ["Next Same Level" outline-forward-same-level t]
  21754. ["Previous Same Level" outline-backward-same-level t]
  21755. "--"
  21756. ["Jump" org-goto t])
  21757. ("Edit Structure"
  21758. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  21759. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  21760. "--"
  21761. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  21762. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  21763. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  21764. "--"
  21765. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  21766. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  21767. ["Demote Heading" org-metaright (not (org-at-table-p))]
  21768. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  21769. "--"
  21770. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  21771. "--"
  21772. ["Convert to odd levels" org-convert-to-odd-levels t]
  21773. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  21774. ("Editing"
  21775. ["Emphasis..." org-emphasize t])
  21776. ("Archive"
  21777. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  21778. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  21779. ; :active t :keys "C-u C-c C-x C-a"]
  21780. ["Sparse trees open ARCHIVE trees"
  21781. (setq org-sparse-tree-open-archived-trees
  21782. (not org-sparse-tree-open-archived-trees))
  21783. :style toggle :selected org-sparse-tree-open-archived-trees]
  21784. ["Cycling opens ARCHIVE trees"
  21785. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  21786. :style toggle :selected org-cycle-open-archived-trees]
  21787. ["Agenda includes ARCHIVE trees"
  21788. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  21789. :style toggle :selected (not org-agenda-skip-archived-trees)]
  21790. "--"
  21791. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  21792. ; ["Check and Move Children" (org-archive-subtree '(4))
  21793. ; :active t :keys "C-u C-c C-x C-s"]
  21794. )
  21795. "--"
  21796. ("TODO Lists"
  21797. ["TODO/DONE/-" org-todo t]
  21798. ("Select keyword"
  21799. ["Next keyword" org-shiftright (org-on-heading-p)]
  21800. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  21801. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  21802. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  21803. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  21804. ["Show TODO Tree" org-show-todo-tree t]
  21805. ["Global TODO list" org-todo-list t]
  21806. "--"
  21807. ["Set Priority" org-priority t]
  21808. ["Priority Up" org-shiftup t]
  21809. ["Priority Down" org-shiftdown t])
  21810. ("TAGS and Properties"
  21811. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  21812. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)] ;FIXME
  21813. ["Column view of properties" org-columns t])
  21814. ("Dates and Scheduling"
  21815. ["Timestamp" org-time-stamp t]
  21816. ["Timestamp (inactive)" org-time-stamp-inactive t]
  21817. ("Change Date"
  21818. ["1 Day Later" org-shiftright t]
  21819. ["1 Day Earlier" org-shiftleft t]
  21820. ["1 ... Later" org-shiftup t]
  21821. ["1 ... Earlier" org-shiftdown t])
  21822. ["Compute Time Range" org-evaluate-time-range t]
  21823. ["Schedule Item" org-schedule t]
  21824. ["Deadline" org-deadline t]
  21825. "--"
  21826. ["Custom time format" org-toggle-time-stamp-overlays
  21827. :style radio :selected org-display-custom-times]
  21828. "--"
  21829. ["Goto Calendar" org-goto-calendar t]
  21830. ["Date from Calendar" org-date-from-calendar t])
  21831. ("Logging work"
  21832. ["Clock in" org-clock-in t]
  21833. ["Clock out" org-clock-out t]
  21834. ["Clock cancel" org-clock-cancel t]
  21835. ["Display times" org-clock-display t]
  21836. ["Create clock table" org-clock-report t]
  21837. "--"
  21838. ["Record DONE time"
  21839. (progn (setq org-log-done (not org-log-done))
  21840. (message "Switching to %s will %s record a timestamp"
  21841. (car org-done-keywords)
  21842. (if org-log-done "automatically" "not")))
  21843. :style toggle :selected org-log-done])
  21844. "--"
  21845. ["Agenda Command..." org-agenda t]
  21846. ("File List for Agenda")
  21847. ("Special views current file"
  21848. ["TODO Tree" org-show-todo-tree t]
  21849. ["Check Deadlines" org-check-deadlines t]
  21850. ["Timeline" org-timeline t]
  21851. ["Tags Tree" org-tags-sparse-tree t])
  21852. "--"
  21853. ("Hyperlinks"
  21854. ["Store Link (Global)" org-store-link t]
  21855. ["Insert Link" org-insert-link t]
  21856. ["Follow Link" org-open-at-point t]
  21857. "--"
  21858. ["Next link" org-next-link t]
  21859. ["Previous link" org-previous-link t]
  21860. "--"
  21861. ["Descriptive Links"
  21862. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  21863. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  21864. ["Literal Links"
  21865. (progn
  21866. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  21867. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  21868. "--"
  21869. ["Export/Publish..." org-export t]
  21870. ("LaTeX"
  21871. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  21872. :selected org-cdlatex-mode]
  21873. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  21874. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  21875. ["Modify math symbol" org-cdlatex-math-modify
  21876. (org-inside-LaTeX-fragment-p)]
  21877. ["Export LaTeX fragments as images"
  21878. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  21879. :style toggle :selected org-export-with-LaTeX-fragments])
  21880. "--"
  21881. ("Documentation"
  21882. ["Show Version" org-version t]
  21883. ["Info Documentation" org-info t])
  21884. ("Customize"
  21885. ["Browse Org Group" org-customize t]
  21886. "--"
  21887. ["Expand This Menu" org-create-customize-menu
  21888. (fboundp 'customize-menu-create)])
  21889. "--"
  21890. ["Refresh setup" org-mode-restart t]
  21891. ))
  21892. (defun org-info (&optional node)
  21893. "Read documentation for Org-mode in the info system.
  21894. With optional NODE, go directly to that node."
  21895. (interactive)
  21896. (require 'info)
  21897. (Info-goto-node (format "(org)%s" (or node ""))))
  21898. (defun org-install-agenda-files-menu ()
  21899. (let ((bl (buffer-list)))
  21900. (save-excursion
  21901. (while bl
  21902. (set-buffer (pop bl))
  21903. (if (org-mode-p) (setq bl nil)))
  21904. (when (org-mode-p)
  21905. (easy-menu-change
  21906. '("Org") "File List for Agenda"
  21907. (append
  21908. (list
  21909. ["Edit File List" (org-edit-agenda-file-list) t]
  21910. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  21911. ["Remove Current File from List" org-remove-file t]
  21912. ["Cycle through agenda files" org-cycle-agenda-files t]
  21913. "--")
  21914. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  21915. ;;;; Documentation
  21916. (defun org-customize ()
  21917. "Call the customize function with org as argument."
  21918. (interactive)
  21919. (customize-browse 'org))
  21920. (defun org-create-customize-menu ()
  21921. "Create a full customization menu for Org-mode, insert it into the menu."
  21922. (interactive)
  21923. (if (fboundp 'customize-menu-create)
  21924. (progn
  21925. (easy-menu-change
  21926. '("Org") "Customize"
  21927. `(["Browse Org group" org-customize t]
  21928. "--"
  21929. ,(customize-menu-create 'org)
  21930. ["Set" Custom-set t]
  21931. ["Save" Custom-save t]
  21932. ["Reset to Current" Custom-reset-current t]
  21933. ["Reset to Saved" Custom-reset-saved t]
  21934. ["Reset to Standard Settings" Custom-reset-standard t]))
  21935. (message "\"Org\"-menu now contains full customization menu"))
  21936. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  21937. ;;;; Miscellaneous stuff
  21938. ;;; Generally useful functions
  21939. (defun org-context ()
  21940. "Return a list of contexts of the current cursor position.
  21941. If several contexts apply, all are returned.
  21942. Each context entry is a list with a symbol naming the context, and
  21943. two positions indicating start and end of the context. Possible
  21944. contexts are:
  21945. :headline anywhere in a headline
  21946. :headline-stars on the leading stars in a headline
  21947. :todo-keyword on a TODO keyword (including DONE) in a headline
  21948. :tags on the TAGS in a headline
  21949. :priority on the priority cookie in a headline
  21950. :item on the first line of a plain list item
  21951. :item-bullet on the bullet/number of a plain list item
  21952. :checkbox on the checkbox in a plain list item
  21953. :table in an org-mode table
  21954. :table-special on a special filed in a table
  21955. :table-table in a table.el table
  21956. :link on a hyperlink
  21957. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  21958. :target on a <<target>>
  21959. :radio-target on a <<<radio-target>>>
  21960. :latex-fragment on a LaTeX fragment
  21961. :latex-preview on a LaTeX fragment with overlayed preview image
  21962. This function expects the position to be visible because it uses font-lock
  21963. faces as a help to recognize the following contexts: :table-special, :link,
  21964. and :keyword."
  21965. (let* ((f (get-text-property (point) 'face))
  21966. (faces (if (listp f) f (list f)))
  21967. (p (point)) clist o)
  21968. ;; First the large context
  21969. (cond
  21970. ((org-on-heading-p t)
  21971. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  21972. (when (progn
  21973. (beginning-of-line 1)
  21974. (looking-at org-todo-line-tags-regexp))
  21975. (push (org-point-in-group p 1 :headline-stars) clist)
  21976. (push (org-point-in-group p 2 :todo-keyword) clist)
  21977. (push (org-point-in-group p 4 :tags) clist))
  21978. (goto-char p)
  21979. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  21980. (if (looking-at "\\[#[A-Z0-9]\\]")
  21981. (push (org-point-in-group p 0 :priority) clist)))
  21982. ((org-at-item-p)
  21983. (push (org-point-in-group p 2 :item-bullet) clist)
  21984. (push (list :item (point-at-bol)
  21985. (save-excursion (org-end-of-item) (point)))
  21986. clist)
  21987. (and (org-at-item-checkbox-p)
  21988. (push (org-point-in-group p 0 :checkbox) clist)))
  21989. ((org-at-table-p)
  21990. (push (list :table (org-table-begin) (org-table-end)) clist)
  21991. (if (memq 'org-formula faces)
  21992. (push (list :table-special
  21993. (previous-single-property-change p 'face)
  21994. (next-single-property-change p 'face)) clist)))
  21995. ((org-at-table-p 'any)
  21996. (push (list :table-table) clist)))
  21997. (goto-char p)
  21998. ;; Now the small context
  21999. (cond
  22000. ((org-at-timestamp-p)
  22001. (push (org-point-in-group p 0 :timestamp) clist))
  22002. ((memq 'org-link faces)
  22003. (push (list :link
  22004. (previous-single-property-change p 'face)
  22005. (next-single-property-change p 'face)) clist))
  22006. ((memq 'org-special-keyword faces)
  22007. (push (list :keyword
  22008. (previous-single-property-change p 'face)
  22009. (next-single-property-change p 'face)) clist))
  22010. ((org-on-target-p)
  22011. (push (org-point-in-group p 0 :target) clist)
  22012. (goto-char (1- (match-beginning 0)))
  22013. (if (looking-at org-radio-target-regexp)
  22014. (push (org-point-in-group p 0 :radio-target) clist))
  22015. (goto-char p))
  22016. ((setq o (car (delq nil
  22017. (mapcar
  22018. (lambda (x)
  22019. (if (memq x org-latex-fragment-image-overlays) x))
  22020. (org-overlays-at (point))))))
  22021. (push (list :latex-fragment
  22022. (org-overlay-start o) (org-overlay-end o)) clist)
  22023. (push (list :latex-preview
  22024. (org-overlay-start o) (org-overlay-end o)) clist))
  22025. ((org-inside-LaTeX-fragment-p)
  22026. ;; FIXME: positions wrong.
  22027. (push (list :latex-fragment (point) (point)) clist)))
  22028. (setq clist (nreverse (delq nil clist)))
  22029. clist))
  22030. ;; FIXME Compare with at-regexp-p
  22031. (defun org-in-regexp (re &optional nlines visually)
  22032. "Check if point is inside a match of regexp.
  22033. Normally only the current line is checked, but you can include NLINES extra
  22034. lines both before and after point into the search.
  22035. If VISUALLY is set, require that the cursor is not after the match but
  22036. really on, so that the block visually is on the match."
  22037. (catch 'exit
  22038. (let ((pos (point))
  22039. (eol (point-at-eol (+ 1 (or nlines 0))))
  22040. (inc (if visually 1 0)))
  22041. (save-excursion
  22042. (beginning-of-line (- 1 (or nlines 0)))
  22043. (while (re-search-forward re eol t)
  22044. (if (and (<= (match-beginning 0) pos)
  22045. (>= (+ inc (match-end 0)) pos))
  22046. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  22047. (defun org-at-regexp-p (regexp)
  22048. "Is point inside a match of REGEXP in the current line?"
  22049. (catch 'exit
  22050. (save-excursion
  22051. (let ((pos (point)) (end (point-at-eol)))
  22052. (beginning-of-line 1)
  22053. (while (re-search-forward regexp end t)
  22054. (if (and (<= (match-beginning 0) pos)
  22055. (>= (match-end 0) pos))
  22056. (throw 'exit t)))
  22057. nil))))
  22058. (defun org-uniquify (list)
  22059. "Remove duplicate elements from LIST."
  22060. (let (res)
  22061. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  22062. res))
  22063. (defun org-delete-all (elts list)
  22064. "Remove all elements in ELTS from LIST."
  22065. (while elts
  22066. (setq list (delete (pop elts) list)))
  22067. list)
  22068. (defun org-point-in-group (point group &optional context)
  22069. "Check if POINT is in match-group GROUP.
  22070. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  22071. match. If the match group does ot exist or point is not inside it,
  22072. return nil."
  22073. (and (match-beginning group)
  22074. (>= point (match-beginning group))
  22075. (<= point (match-end group))
  22076. (if context
  22077. (list context (match-beginning group) (match-end group))
  22078. t)))
  22079. (defun org-combine-plists (&rest plists)
  22080. "Create a single property list from all plists in PLISTS.
  22081. The process starts by copying the first list, and then setting properties
  22082. from the other lists. Settings in the last list are the most significant
  22083. ones and overrule settings in the other lists."
  22084. (let ((rtn (copy-sequence (pop plists)))
  22085. p v ls)
  22086. (while plists
  22087. (setq ls (pop plists))
  22088. (while ls
  22089. (setq p (pop ls) v (pop ls))
  22090. (setq rtn (plist-put rtn p v))))
  22091. rtn))
  22092. (defun org-move-line-down (arg)
  22093. "Move the current line down. With prefix argument, move it past ARG lines."
  22094. (interactive "p")
  22095. (let ((col (current-column))
  22096. beg end pos)
  22097. (beginning-of-line 1) (setq beg (point))
  22098. (beginning-of-line 2) (setq end (point))
  22099. (beginning-of-line (+ 1 arg))
  22100. (setq pos (move-marker (make-marker) (point)))
  22101. (insert (delete-and-extract-region beg end))
  22102. (goto-char pos)
  22103. (move-to-column col)))
  22104. (defun org-move-line-up (arg)
  22105. "Move the current line up. With prefix argument, move it past ARG lines."
  22106. (interactive "p")
  22107. (let ((col (current-column))
  22108. beg end pos)
  22109. (beginning-of-line 1) (setq beg (point))
  22110. (beginning-of-line 2) (setq end (point))
  22111. (beginning-of-line (- arg))
  22112. (setq pos (move-marker (make-marker) (point)))
  22113. (insert (delete-and-extract-region beg end))
  22114. (goto-char pos)
  22115. (move-to-column col)))
  22116. (defun org-replace-escapes (string table)
  22117. "Replace %-escapes in STRING with values in TABLE.
  22118. TABLE is an association list with keys line \"%a\" and string values.
  22119. The sequences in STRING may contain normal field width and padding information,
  22120. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  22121. so values can contain further %-escapes if they are define later in TABLE."
  22122. (let ((case-fold-search nil)
  22123. e re rpl)
  22124. (while (setq e (pop table))
  22125. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  22126. (while (string-match re string)
  22127. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  22128. (cdr e)))
  22129. (setq string (replace-match rpl t t string))))
  22130. string))
  22131. (defun org-sublist (list start end)
  22132. "Return a section of LIST, from START to END.
  22133. Counting starts at 1."
  22134. (let (rtn (c start))
  22135. (setq list (nthcdr (1- start) list))
  22136. (while (and list (<= c end))
  22137. (push (pop list) rtn)
  22138. (setq c (1+ c)))
  22139. (nreverse rtn)))
  22140. (defun org-find-base-buffer-visiting (file)
  22141. "Like `find-buffer-visiting' but alway return the base buffer and
  22142. not an indirect buffer"
  22143. (let ((buf (find-buffer-visiting file)))
  22144. (or (buffer-base-buffer buf) buf)))
  22145. (defun org-image-file-name-regexp ()
  22146. "Return regexp matching the file names of images."
  22147. (if (fboundp 'image-file-name-regexp)
  22148. (image-file-name-regexp)
  22149. (let ((image-file-name-extensions
  22150. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  22151. "xbm" "xpm" "pbm" "pgm" "ppm")))
  22152. (concat "\\."
  22153. (regexp-opt (nconc (mapcar 'upcase
  22154. image-file-name-extensions)
  22155. image-file-name-extensions)
  22156. t)
  22157. "\\'"))))
  22158. (defun org-file-image-p (file)
  22159. "Return non-nil if FILE is an image."
  22160. (save-match-data
  22161. (string-match (org-image-file-name-regexp) file)))
  22162. ;;; Paragraph filling stuff.
  22163. ;; We want this to be just right, so use the full arsenal.
  22164. (defun org-indent-line-function ()
  22165. "Indent line like previous, but further if previous was headline or item."
  22166. (interactive)
  22167. (let* ((pos (point))
  22168. (itemp (org-at-item-p))
  22169. column bpos bcol tpos tcol bullet btype bullet-type)
  22170. ;; Find the previous relevant line
  22171. (beginning-of-line 1)
  22172. (cond
  22173. ((looking-at "#") (setq column 0))
  22174. ((looking-at "\\*+ ") (setq column 0))
  22175. (t
  22176. (beginning-of-line 0)
  22177. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  22178. (beginning-of-line 0))
  22179. (cond
  22180. ((looking-at "\\*+[ \t]+")
  22181. (goto-char (match-end 0))
  22182. (setq column (current-column)))
  22183. ((org-in-item-p)
  22184. (org-beginning-of-item)
  22185. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22186. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  22187. (setq bpos (match-beginning 1) tpos (match-end 0)
  22188. bcol (progn (goto-char bpos) (current-column))
  22189. tcol (progn (goto-char tpos) (current-column))
  22190. bullet (match-string 1)
  22191. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  22192. (if (not itemp)
  22193. (setq column tcol)
  22194. (goto-char pos)
  22195. (beginning-of-line 1)
  22196. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  22197. (setq bullet (match-string 1)
  22198. btype (if (string-match "[0-9]" bullet) "n" bullet))
  22199. (setq column (if (equal btype bullet-type) bcol tcol))))
  22200. (t (setq column (org-get-indentation))))))
  22201. (goto-char pos)
  22202. (if (<= (current-column) (current-indentation))
  22203. (indent-line-to column)
  22204. (save-excursion (indent-line-to column)))
  22205. (setq column (current-column))
  22206. (beginning-of-line 1)
  22207. (if (looking-at
  22208. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  22209. (replace-match (concat "\\1" (format org-property-format
  22210. (match-string 2) (match-string 3)))
  22211. t nil))
  22212. (move-to-column column)))
  22213. (defun org-set-autofill-regexps ()
  22214. (interactive)
  22215. ;; In the paragraph separator we include headlines, because filling
  22216. ;; text in a line directly attached to a headline would otherwise
  22217. ;; fill the headline as well.
  22218. (org-set-local 'comment-start-skip "^#+[ \t]*")
  22219. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  22220. ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
  22221. ;; The paragraph starter includes hand-formatted lists.
  22222. (org-set-local 'paragraph-start
  22223. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  22224. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  22225. ;; But only if the user has not turned off tables or fixed-width regions
  22226. (org-set-local
  22227. 'auto-fill-inhibit-regexp
  22228. (concat "\\*+ \\|#\\+"
  22229. "\\|[ \t]*" org-keyword-time-regexp
  22230. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  22231. (concat
  22232. "\\|[ \t]*["
  22233. (if org-enable-table-editor "|" "")
  22234. (if org-enable-fixed-width-editor ":" "")
  22235. "]"))))
  22236. ;; We use our own fill-paragraph function, to make sure that tables
  22237. ;; and fixed-width regions are not wrapped. That function will pass
  22238. ;; through to `fill-paragraph' when appropriate.
  22239. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  22240. ; Adaptive filling: To get full control, first make sure that
  22241. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  22242. (org-set-local 'adaptive-fill-regexp "\000")
  22243. (org-set-local 'adaptive-fill-function
  22244. 'org-adaptive-fill-function))
  22245. (defun org-fill-paragraph (&optional justify)
  22246. "Re-align a table, pass through to fill-paragraph if no table."
  22247. (let ((table-p (org-at-table-p))
  22248. (table.el-p (org-at-table.el-p)))
  22249. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  22250. (table.el-p t) ; skip table.el tables
  22251. (table-p (org-table-align) t) ; align org-mode tables
  22252. (t nil)))) ; call paragraph-fill
  22253. ;; For reference, this is the default value of adaptive-fill-regexp
  22254. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  22255. (defun org-adaptive-fill-function ()
  22256. "Return a fill prefix for org-mode files.
  22257. In particular, this makes sure hanging paragraphs for hand-formatted lists
  22258. work correctly."
  22259. (cond ((looking-at "#[ \t]+")
  22260. (match-string 0))
  22261. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  22262. (save-excursion
  22263. (goto-char (match-end 0))
  22264. (make-string (current-column) ?\ )))
  22265. (t nil)))
  22266. ;;;; Functions extending outline functionality
  22267. (defun org-beginning-of-line (&optional arg)
  22268. "Go to the beginning of the current line. If that is invisible, continue
  22269. to a visible line beginning. This makes the function of C-a more intuitive.
  22270. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22271. first attempt, and only move to after the tags when the cursor is already
  22272. beyond the end of the headline."
  22273. (interactive "P")
  22274. (let ((pos (point)))
  22275. (beginning-of-line 1)
  22276. (if (bobp)
  22277. nil
  22278. (backward-char 1)
  22279. (if (org-invisible-p)
  22280. (while (and (not (bobp)) (org-invisible-p))
  22281. (backward-char 1)
  22282. (beginning-of-line 1))
  22283. (forward-char 1)))
  22284. (when org-special-ctrl-a/e
  22285. (cond
  22286. ((and (looking-at org-todo-line-regexp)
  22287. (= (char-after (match-end 1)) ?\ ))
  22288. (goto-char
  22289. (if (eq org-special-ctrl-a/e t)
  22290. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  22291. ((= pos (point)) (match-beginning 3))
  22292. (t (point)))
  22293. (cond ((> pos (point)) (point))
  22294. ((not (eq last-command this-command)) (point))
  22295. (t (match-beginning 3))))))
  22296. ((org-at-item-p)
  22297. (goto-char
  22298. (if (eq org-special-ctrl-a/e t)
  22299. (cond ((> pos (match-end 4)) (match-end 4))
  22300. ((= pos (point)) (match-end 4))
  22301. (t (point)))
  22302. (cond ((> pos (point)) (point))
  22303. ((not (eq last-command this-command)) (point))
  22304. (t (match-end 4))))))))))
  22305. (defun org-end-of-line (&optional arg)
  22306. "Go to the end of the line.
  22307. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  22308. first attempt, and only move to after the tags when the cursor is already
  22309. beyond the end of the headline."
  22310. (interactive "P")
  22311. (if (or (not org-special-ctrl-a/e)
  22312. (not (org-on-heading-p)))
  22313. (end-of-line arg)
  22314. (let ((pos (point)))
  22315. (beginning-of-line 1)
  22316. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  22317. (if (eq org-special-ctrl-a/e t)
  22318. (if (or (< pos (match-beginning 1))
  22319. (= pos (match-end 0)))
  22320. (goto-char (match-beginning 1))
  22321. (goto-char (match-end 0)))
  22322. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  22323. (goto-char (match-end 0))
  22324. (goto-char (match-beginning 1))))
  22325. (end-of-line arg)))))
  22326. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22327. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22328. (defun org-invisible-p ()
  22329. "Check if point is at a character currently not visible."
  22330. ;; Early versions of noutline don't have `outline-invisible-p'.
  22331. (if (fboundp 'outline-invisible-p)
  22332. (outline-invisible-p)
  22333. (get-char-property (point) 'invisible)))
  22334. (defun org-invisible-p2 ()
  22335. "Check if point is at a character currently not visible."
  22336. (save-excursion
  22337. (if (and (eolp) (not (bobp))) (backward-char 1))
  22338. ;; Early versions of noutline don't have `outline-invisible-p'.
  22339. (if (fboundp 'outline-invisible-p)
  22340. (outline-invisible-p)
  22341. (get-char-property (point) 'invisible))))
  22342. (defalias 'org-back-to-heading 'outline-back-to-heading)
  22343. (defalias 'org-on-heading-p 'outline-on-heading-p)
  22344. (defalias 'org-at-heading-p 'outline-on-heading-p)
  22345. (defun org-at-heading-or-item-p ()
  22346. (or (org-on-heading-p) (org-at-item-p)))
  22347. (defun org-on-target-p ()
  22348. (or (org-in-regexp org-radio-target-regexp)
  22349. (org-in-regexp org-target-regexp)))
  22350. (defun org-up-heading-all (arg)
  22351. "Move to the heading line of which the present line is a subheading.
  22352. This function considers both visible and invisible heading lines.
  22353. With argument, move up ARG levels."
  22354. (if (fboundp 'outline-up-heading-all)
  22355. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22356. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22357. (defun org-goto-sibling (&optional previous)
  22358. "Goto the next sibling, even if it is invisible.
  22359. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22360. when a sibling was found. When none is found, return nil and don't
  22361. move point."
  22362. (let ((fun (if previous 're-search-backward 're-search-forward))
  22363. (pos (point))
  22364. (re (concat "^" outline-regexp))
  22365. level l)
  22366. (when (condition-case nil (org-back-to-heading t) (error nil))
  22367. (setq level (funcall outline-level))
  22368. (catch 'exit
  22369. (or previous (forward-char 1))
  22370. (while (funcall fun re nil t)
  22371. (setq l (funcall outline-level))
  22372. (when (< l level) (goto-char pos) (throw 'exit nil))
  22373. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22374. (goto-char pos)
  22375. nil))))
  22376. (defun org-show-siblings ()
  22377. "Show all siblings of the current headline."
  22378. (save-excursion
  22379. (while (org-goto-sibling) (org-flag-heading nil)))
  22380. (save-excursion
  22381. (while (org-goto-sibling 'previous)
  22382. (org-flag-heading nil))))
  22383. (defun org-show-hidden-entry ()
  22384. "Show an entry where even the heading is hidden."
  22385. (save-excursion
  22386. (org-show-entry)))
  22387. (defun org-flag-heading (flag &optional entry)
  22388. "Flag the current heading. FLAG non-nil means make invisible.
  22389. When ENTRY is non-nil, show the entire entry."
  22390. (save-excursion
  22391. (org-back-to-heading t)
  22392. ;; Check if we should show the entire entry
  22393. (if entry
  22394. (progn
  22395. (org-show-entry)
  22396. (save-excursion
  22397. (and (outline-next-heading)
  22398. (org-flag-heading nil))))
  22399. (outline-flag-region (max (point-min) (1- (point)))
  22400. (save-excursion (outline-end-of-heading) (point))
  22401. flag))))
  22402. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  22403. ;; This is an exact copy of the original function, but it uses
  22404. ;; `org-back-to-heading', to make it work also in invisible
  22405. ;; trees. And is uses an invisible-OK argument.
  22406. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22407. (org-back-to-heading invisible-OK)
  22408. (let ((first t)
  22409. (level (funcall outline-level)))
  22410. (while (and (not (eobp))
  22411. (or first (> (funcall outline-level) level)))
  22412. (setq first nil)
  22413. (outline-next-heading))
  22414. (unless to-heading
  22415. (if (memq (preceding-char) '(?\n ?\^M))
  22416. (progn
  22417. ;; Go to end of line before heading
  22418. (forward-char -1)
  22419. (if (memq (preceding-char) '(?\n ?\^M))
  22420. ;; leave blank line before heading
  22421. (forward-char -1))))))
  22422. (point))
  22423. (defun org-show-subtree ()
  22424. "Show everything after this heading at deeper levels."
  22425. (outline-flag-region
  22426. (point)
  22427. (save-excursion
  22428. (outline-end-of-subtree) (outline-next-heading) (point))
  22429. nil))
  22430. (defun org-show-entry ()
  22431. "Show the body directly following this heading.
  22432. Show the heading too, if it is currently invisible."
  22433. (interactive)
  22434. (save-excursion
  22435. (org-back-to-heading t)
  22436. (outline-flag-region
  22437. (max (point-min) (1- (point)))
  22438. (save-excursion
  22439. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  22440. (or (match-beginning 1) (point-max)))
  22441. nil)))
  22442. (defun org-make-options-regexp (kwds)
  22443. "Make a regular expression for keyword lines."
  22444. (concat
  22445. "^"
  22446. "#?[ \t]*\\+\\("
  22447. (mapconcat 'regexp-quote kwds "\\|")
  22448. "\\):[ \t]*"
  22449. "\\(.+\\)"))
  22450. ;; Make isearch reveal the necessary context
  22451. (defun org-isearch-end ()
  22452. "Reveal context after isearch exits."
  22453. (when isearch-success ; only if search was successful
  22454. (if (featurep 'xemacs)
  22455. ;; Under XEmacs, the hook is run in the correct place,
  22456. ;; we directly show the context.
  22457. (org-show-context 'isearch)
  22458. ;; In Emacs the hook runs *before* restoring the overlays.
  22459. ;; So we have to use a one-time post-command-hook to do this.
  22460. ;; (Emacs 22 has a special variable, see function `org-mode')
  22461. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22462. isearch-mode-end-hook-quit)
  22463. ;; Only when the isearch was not quitted.
  22464. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22465. 'append 'local)))))
  22466. (defun org-isearch-post-command ()
  22467. "Remove self from hook, and show context."
  22468. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22469. (org-show-context 'isearch))
  22470. ;;;; Address problems with some other packages
  22471. ;; Make flyspell not check words in links, to not mess up our keymap
  22472. (defun org-mode-flyspell-verify ()
  22473. "Don't let flyspell put overlays at active buttons."
  22474. (not (get-text-property (point) 'keymap)))
  22475. ;; Make `bookmark-jump' show the jump location if it was hidden.
  22476. (eval-after-load "bookmark"
  22477. '(if (boundp 'bookmark-after-jump-hook)
  22478. ;; We can use the hook
  22479. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22480. ;; Hook not available, use advice
  22481. (defadvice bookmark-jump (after org-make-visible activate)
  22482. "Make the position visible."
  22483. (org-bookmark-jump-unhide))))
  22484. (defun org-bookmark-jump-unhide ()
  22485. "Unhide the current position, to show the bookmark location."
  22486. (and (org-mode-p)
  22487. (or (org-invisible-p)
  22488. (save-excursion (goto-char (max (point-min) (1- (point))))
  22489. (org-invisible-p)))
  22490. (org-show-context 'bookmark-jump)))
  22491. ;; Make session.el ignore our circular variable
  22492. (eval-after-load "session"
  22493. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22494. ;;;; Experimental code
  22495. ;; Make appt aware of appointments from the agenda
  22496. (defun org-agenda-to-appt ()
  22497. "Activate appointments found in `org-agenda-files'."
  22498. (interactive)
  22499. (require 'org)
  22500. (let* ((today (org-date-to-gregorian
  22501. (time-to-days (current-time))))
  22502. (files org-agenda-files) entries file)
  22503. (while (setq file (pop files))
  22504. (setq entries (append entries (org-agenda-get-day-entries
  22505. file today :timestamp))))
  22506. (setq entries (delq nil entries))
  22507. (mapc (lambda(x)
  22508. (let* ((event (org-trim (get-text-property 1 'txt x)))
  22509. (time-of-day (get-text-property 1 'time-of-day x)) tod)
  22510. (when time-of-day
  22511. (setq tod (number-to-string time-of-day)
  22512. tod (when (string-match
  22513. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  22514. (concat (match-string 1 tod) ":"
  22515. (match-string 2 tod))))
  22516. (if tod (appt-add tod event))))) entries)))
  22517. (defun org-closed-in-range ()
  22518. "Sparse tree of items closed in a certain time range.
  22519. Still experimental, may disappear in the furture."
  22520. (interactive)
  22521. ;; Get the time interval from the user.
  22522. (let* ((time1 (time-to-seconds
  22523. (org-read-date nil 'to-time nil "Starting date: ")))
  22524. (time2 (time-to-seconds
  22525. (org-read-date nil 'to-time nil "End date:")))
  22526. ;; callback function
  22527. (callback (lambda ()
  22528. (let ((time
  22529. (time-to-seconds
  22530. (apply 'encode-time
  22531. (org-parse-time-string
  22532. (match-string 1))))))
  22533. ;; check if time in interval
  22534. (and (>= time time1) (<= time time2))))))
  22535. ;; make tree, check each match with the callback
  22536. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  22537. (defun org-fill-paragraph-experimental (&optional justify)
  22538. "Re-align a table, pass through to fill-paragraph if no table."
  22539. (let ((table-p (org-at-table-p))
  22540. (table.el-p (org-at-table.el-p)))
  22541. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  22542. (table.el-p t) ; skip table.el tables
  22543. (table-p (org-table-align) t) ; align org-mode tables
  22544. ((save-excursion
  22545. (let ((pos (1+ (point-at-eol))))
  22546. (backward-paragraph 1)
  22547. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  22548. (save-excursion
  22549. (save-restriction
  22550. (narrow-to-region (1+ (match-end 0)) (point-max))
  22551. (fill-paragraph nil)
  22552. t)))
  22553. (t nil)))) ; call paragraph-fill
  22554. (defun org-get-min-level (lines)
  22555. (let ((re "^\\(\\*+\\) ") l min)
  22556. (catch 'exit
  22557. (while (setq l (pop lines))
  22558. (if (string-match re l)
  22559. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22560. 1)))
  22561. ;;;; Finish up
  22562. (provide 'org)
  22563. (run-hooks 'org-load-hook)
  22564. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  22565. ;;; org.el ends here