org.el 945 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.12a
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (require 'easymenu)
  77. ;;;; Customization variables
  78. ;;; Version
  79. (defconst org-version "5.12a"
  80. "The version number of the file org.el.")
  81. (defun org-version ()
  82. (interactive)
  83. (message "Org-mode version %s" org-version))
  84. ;;; Compatibility constants
  85. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  86. (defconst org-format-transports-properties-p
  87. (let ((x "a"))
  88. (add-text-properties 0 1 '(test t) x)
  89. (get-text-property 0 'test (format "%s" x)))
  90. "Does format transport text properties?")
  91. (defmacro org-unmodified (&rest body)
  92. "Execute body without changing buffer-modified-p."
  93. `(set-buffer-modified-p
  94. (prog1 (buffer-modified-p) ,@body)))
  95. (defmacro org-re (s)
  96. "Replace posix classes in regular expression."
  97. (if (featurep 'xemacs)
  98. (let ((ss s))
  99. (save-match-data
  100. (while (string-match "\\[:alnum:\\]" ss)
  101. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  102. (while (string-match "\\[:alpha:\\]" ss)
  103. (setq ss (replace-match "a-zA-Z" t t ss)))
  104. ss))
  105. s))
  106. (defmacro org-preserve-lc (&rest body)
  107. `(let ((_line (org-current-line))
  108. (_col (current-column)))
  109. (unwind-protect
  110. (progn ,@body)
  111. (goto-line _line)
  112. (move-to-column _col))))
  113. (defmacro org-without-partial-completion (&rest body)
  114. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  115. partial-completion-mode)))
  116. (unwind-protect
  117. (progn
  118. (if pc-mode (partial-completion-mode -1))
  119. ,@body)
  120. (if pc-mode (partial-completion-mode 1)))))
  121. ;;; The custom variables
  122. (defgroup org nil
  123. "Outline-based notes management and organizer."
  124. :tag "Org"
  125. :group 'outlines
  126. :group 'hypermedia
  127. :group 'calendar)
  128. ;; FIXME: Needs a separate group...
  129. (defcustom org-completion-fallback-command 'hippie-expand
  130. "The expansion command called by \\[org-complete] in normal context.
  131. Normal means, no org-mode-specific context."
  132. :group 'org
  133. :type 'function)
  134. (defgroup org-startup nil
  135. "Options concerning startup of Org-mode."
  136. :tag "Org Startup"
  137. :group 'org)
  138. (defcustom org-startup-folded t
  139. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  140. This can also be configured on a per-file basis by adding one of
  141. the following lines anywhere in the buffer:
  142. #+STARTUP: fold
  143. #+STARTUP: nofold
  144. #+STARTUP: content"
  145. :group 'org-startup
  146. :type '(choice
  147. (const :tag "nofold: show all" nil)
  148. (const :tag "fold: overview" t)
  149. (const :tag "content: all headlines" content)))
  150. (defcustom org-startup-truncated t
  151. "Non-nil means, entering Org-mode will set `truncate-lines'.
  152. This is useful since some lines containing links can be very long and
  153. uninteresting. Also tables look terrible when wrapped."
  154. :group 'org-startup
  155. :type 'boolean)
  156. (defcustom org-startup-align-all-tables nil
  157. "Non-nil means, align all tables when visiting a file.
  158. This is useful when the column width in tables is forced with <N> cookies
  159. in table fields. Such tables will look correct only after the first re-align.
  160. This can also be configured on a per-file basis by adding one of
  161. the following lines anywhere in the buffer:
  162. #+STARTUP: align
  163. #+STARTUP: noalign"
  164. :group 'org-startup
  165. :type 'boolean)
  166. (defcustom org-insert-mode-line-in-empty-file nil
  167. "Non-nil means insert the first line setting Org-mode in empty files.
  168. When the function `org-mode' is called interactively in an empty file, this
  169. normally means that the file name does not automatically trigger Org-mode.
  170. To ensure that the file will always be in Org-mode in the future, a
  171. line enforcing Org-mode will be inserted into the buffer, if this option
  172. has been set."
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-replace-disputed-keys nil
  176. "Non-nil means use alternative key bindings for some keys.
  177. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  178. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  179. If you want to use Org-mode together with one of these other modes,
  180. or more generally if you would like to move some Org-mode commands to
  181. other keys, set this variable and configure the keys with the variable
  182. `org-disputed-keys'.
  183. This option is only relevant at load-time of Org-mode, and must be set
  184. *before* org.el is loaded. Changing it requires a restart of Emacs to
  185. become effective."
  186. :group 'org-startup
  187. :type 'boolean)
  188. (if (fboundp 'defvaralias)
  189. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  190. (defcustom org-disputed-keys
  191. '(([(shift up)] . [(meta p)])
  192. ([(shift down)] . [(meta n)])
  193. ([(shift left)] . [(meta -)])
  194. ([(shift right)] . [(meta +)])
  195. ([(control shift right)] . [(meta shift +)])
  196. ([(control shift left)] . [(meta shift -)]))
  197. "Keys for which Org-mode and other modes compete.
  198. This is an alist, cars are the default keys, second element specifies
  199. the alternative to use when `org-replace-disputed-keys' is t.
  200. Keys can be specified in any syntax supported by `define-key'.
  201. The value of this option takes effect only at Org-mode's startup,
  202. therefore you'll have to restart Emacs to apply it after changing."
  203. :group 'org-startup
  204. :type 'alist)
  205. (defun org-key (key)
  206. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  207. Or return the original if not disputed."
  208. (if org-replace-disputed-keys
  209. (let* ((nkey (key-description key))
  210. (x (org-find-if (lambda (x)
  211. (equal (key-description (car x)) nkey))
  212. org-disputed-keys)))
  213. (if x (cdr x) key))
  214. key))
  215. (defun org-find-if (predicate seq)
  216. (catch 'exit
  217. (while seq
  218. (if (funcall predicate (car seq))
  219. (throw 'exit (car seq))
  220. (pop seq)))))
  221. (defun org-defkey (keymap key def)
  222. "Define a key, possibly translated, as returned by `org-key'."
  223. (define-key keymap (org-key key) def))
  224. (defcustom org-ellipsis 'org-link
  225. "The ellipsis to use in the Org-mode outline.
  226. When nil, just use the standard three dots. When a string, use that instead,
  227. When a face, use the standart 3 dots, but with the specified face.
  228. The change affects only Org-mode (which will then use its own display table).
  229. Changing this requires executing `M-x org-mode' in a buffer to become
  230. effective."
  231. :group 'org-startup
  232. :type '(choice (const :tag "Default" nil)
  233. (face :tag "Face" :value org-warning)
  234. (string :tag "String" :value "...#")))
  235. (defvar org-display-table nil
  236. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  237. (defgroup org-keywords nil
  238. "Keywords in Org-mode."
  239. :tag "Org Keywords"
  240. :group 'org)
  241. (defcustom org-deadline-string "DEADLINE:"
  242. "String to mark deadline entries.
  243. A deadline is this string, followed by a time stamp. Should be a word,
  244. terminated by a colon. You can insert a schedule keyword and
  245. a timestamp with \\[org-deadline].
  246. Changes become only effective after restarting Emacs."
  247. :group 'org-keywords
  248. :type 'string)
  249. (defcustom org-scheduled-string "SCHEDULED:"
  250. "String to mark scheduled TODO entries.
  251. A schedule is this string, followed by a time stamp. Should be a word,
  252. terminated by a colon. You can insert a schedule keyword and
  253. a timestamp with \\[org-schedule].
  254. Changes become only effective after restarting Emacs."
  255. :group 'org-keywords
  256. :type 'string)
  257. (defcustom org-closed-string "CLOSED:"
  258. "String used as the prefix for timestamps logging closing a TODO entry."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-clock-string "CLOCK:"
  262. "String used as prefix for timestamps clocking work hours on an item."
  263. :group 'org-keywords
  264. :type 'string)
  265. (defcustom org-comment-string "COMMENT"
  266. "Entries starting with this keyword will never be exported.
  267. An entry can be toggled between COMMENT and normal with
  268. \\[org-toggle-comment].
  269. Changes become only effective after restarting Emacs."
  270. :group 'org-keywords
  271. :type 'string)
  272. (defcustom org-quote-string "QUOTE"
  273. "Entries starting with this keyword will be exported in fixed-width font.
  274. Quoting applies only to the text in the entry following the headline, and does
  275. not extend beyond the next headline, even if that is lower level.
  276. An entry can be toggled between QUOTE and normal with
  277. \\[org-toggle-fixed-width-section]."
  278. :group 'org-keywords
  279. :type 'string)
  280. (defconst org-repeat-re
  281. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  282. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  283. "Regular expression for specifying repeated events.
  284. After a match, group 1 contains the repeat expression.")
  285. (defgroup org-structure nil
  286. "Options concerning the general structure of Org-mode files."
  287. :tag "Org Structure"
  288. :group 'org)
  289. (defgroup org-reveal-location nil
  290. "Options about how to make context of a location visible."
  291. :tag "Org Reveal Location"
  292. :group 'org-structure)
  293. (defcustom org-show-hierarchy-above '((default . t))
  294. "Non-nil means, show full hierarchy when revealing a location.
  295. Org-mode often shows locations in an org-mode file which might have
  296. been invisible before. When this is set, the hierarchy of headings
  297. above the exposed location is shown.
  298. Turning this off for example for sparse trees makes them very compact.
  299. Instead of t, this can also be an alist specifying this option for different
  300. contexts. Valid contexts are
  301. agenda when exposing an entry from the agenda
  302. org-goto when using the command `org-goto' on key C-c C-j
  303. occur-tree when using the command `org-occur' on key C-c /
  304. tags-tree when constructing a sparse tree based on tags matches
  305. link-search when exposing search matches associated with a link
  306. mark-goto when exposing the jump goal of a mark
  307. bookmark-jump when exposing a bookmark location
  308. isearch when exiting from an incremental search
  309. default default for all contexts not set explicitly"
  310. :group 'org-reveal-location
  311. :type '(choice
  312. (const :tag "Always" t)
  313. (const :tag "Never" nil)
  314. (repeat :greedy t :tag "Individual contexts"
  315. (cons
  316. (choice :tag "Context"
  317. (const agenda)
  318. (const org-goto)
  319. (const occur-tree)
  320. (const tags-tree)
  321. (const link-search)
  322. (const mark-goto)
  323. (const bookmark-jump)
  324. (const isearch)
  325. (const default))
  326. (boolean)))))
  327. (defcustom org-show-following-heading '((default . nil))
  328. "Non-nil means, show following heading when revealing a location.
  329. Org-mode often shows locations in an org-mode file which might have
  330. been invisible before. When this is set, the heading following the
  331. match is shown.
  332. Turning this off for example for sparse trees makes them very compact,
  333. but makes it harder to edit the location of the match. In such a case,
  334. use the command \\[org-reveal] to show more context.
  335. Instead of t, this can also be an alist specifying this option for different
  336. contexts. See `org-show-hierarchy-above' for valid contexts."
  337. :group 'org-reveal-location
  338. :type '(choice
  339. (const :tag "Always" t)
  340. (const :tag "Never" nil)
  341. (repeat :greedy t :tag "Individual contexts"
  342. (cons
  343. (choice :tag "Context"
  344. (const agenda)
  345. (const org-goto)
  346. (const occur-tree)
  347. (const tags-tree)
  348. (const link-search)
  349. (const mark-goto)
  350. (const bookmark-jump)
  351. (const isearch)
  352. (const default))
  353. (boolean)))))
  354. (defcustom org-show-siblings '((default . nil) (isearch t))
  355. "Non-nil means, show all sibling heading when revealing a location.
  356. Org-mode often shows locations in an org-mode file which might have
  357. been invisible before. When this is set, the sibling of the current entry
  358. heading are all made visible. If `org-show-hierarchy-above' is t,
  359. the same happens on each level of the hierarchy above the current entry.
  360. By default this is on for the isearch context, off for all other contexts.
  361. Turning this off for example for sparse trees makes them very compact,
  362. but makes it harder to edit the location of the match. In such a case,
  363. use the command \\[org-reveal] to show more context.
  364. Instead of t, this can also be an alist specifying this option for different
  365. contexts. See `org-show-hierarchy-above' for valid contexts."
  366. :group 'org-reveal-location
  367. :type '(choice
  368. (const :tag "Always" t)
  369. (const :tag "Never" nil)
  370. (repeat :greedy t :tag "Individual contexts"
  371. (cons
  372. (choice :tag "Context"
  373. (const agenda)
  374. (const org-goto)
  375. (const occur-tree)
  376. (const tags-tree)
  377. (const link-search)
  378. (const mark-goto)
  379. (const bookmark-jump)
  380. (const isearch)
  381. (const default))
  382. (boolean)))))
  383. (defgroup org-cycle nil
  384. "Options concerning visibility cycling in Org-mode."
  385. :tag "Org Cycle"
  386. :group 'org-structure)
  387. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  388. "Names of drawers. Drawers are not opened by cycling on the headline above.
  389. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  390. this:
  391. :DRAWERNAME:
  392. .....
  393. :END:
  394. The drawer \"PROPERTIES\" is special for capturing properties through
  395. the property API."
  396. :group 'org-structure
  397. :type '(repeat (string :tag "Drawer Name")))
  398. (defcustom org-cycle-global-at-bob nil
  399. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  400. This makes it possible to do global cycling without having to use S-TAB or
  401. C-u TAB. For this special case to work, the first line of the buffer
  402. must not be a headline - it may be empty ot some other text. When used in
  403. this way, `org-cycle-hook' is disables temporarily, to make sure the
  404. cursor stays at the beginning of the buffer.
  405. When this option is nil, don't do anything special at the beginning
  406. of the buffer."
  407. :group 'org-cycle
  408. :type 'boolean)
  409. (defcustom org-cycle-emulate-tab t
  410. "Where should `org-cycle' emulate TAB.
  411. nil Never
  412. white Only in completely white lines
  413. whitestart Only at the beginning of lines, before the first non-white char.
  414. t Everywhere except in headlines
  415. exc-hl-bol Everywhere except at the start of a headline
  416. If TAB is used in a place where it does not emulate TAB, the current subtree
  417. visibility is cycled."
  418. :group 'org-cycle
  419. :type '(choice (const :tag "Never" nil)
  420. (const :tag "Only in completely white lines" white)
  421. (const :tag "Before first char in a line" whitestart)
  422. (const :tag "Everywhere except in headlines" t)
  423. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  424. ))
  425. (defcustom org-cycle-separator-lines 2
  426. "Number of empty lines needed to keep an empty line between collapsed trees.
  427. If you leave an empty line between the end of a subtree and the following
  428. headline, this empty line is hidden when the subtree is folded.
  429. Org-mode will leave (exactly) one empty line visible if the number of
  430. empty lines is equal or larger to the number given in this variable.
  431. So the default 2 means, at least 2 empty lines after the end of a subtree
  432. are needed to produce free space between a collapsed subtree and the
  433. following headline.
  434. Special case: when 0, never leave empty lines in collapsed view."
  435. :group 'org-cycle
  436. :type 'integer)
  437. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  438. org-cycle-hide-drawers
  439. org-cycle-show-empty-lines
  440. org-optimize-window-after-visibility-change)
  441. "Hook that is run after `org-cycle' has changed the buffer visibility.
  442. The function(s) in this hook must accept a single argument which indicates
  443. the new state that was set by the most recent `org-cycle' command. The
  444. argument is a symbol. After a global state change, it can have the values
  445. `overview', `content', or `all'. After a local state change, it can have
  446. the values `folded', `children', or `subtree'."
  447. :group 'org-cycle
  448. :type 'hook)
  449. (defgroup org-edit-structure nil
  450. "Options concerning structure editing in Org-mode."
  451. :tag "Org Edit Structure"
  452. :group 'org-structure)
  453. (defcustom org-special-ctrl-a/e nil
  454. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  455. When t, `C-a' will bring back the cursor to the beginning of the
  456. headline text, i.e. after the stars and after a possible TODO keyword.
  457. In an item, this will be the position after the bullet.
  458. When the cursor is already at that position, another `C-a' will bring
  459. it to the beginning of the line.
  460. `C-e' will jump to the end of the headline, ignoring the presence of tags
  461. in the headline. A second `C-e' will then jump to the true end of the
  462. line, after any tags.
  463. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  464. and only a directly following, identical keypress will bring the cursor
  465. to the special positions."
  466. :group 'org-edit-structure
  467. :type '(choice
  468. (const :tag "off" nil)
  469. (const :tag "after bullet first" t)
  470. (const :tag "border first" reversed)))
  471. (if (fboundp 'defvaralias)
  472. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  473. (defcustom org-odd-levels-only nil
  474. "Non-nil means, skip even levels and only use odd levels for the outline.
  475. This has the effect that two stars are being added/taken away in
  476. promotion/demotion commands. It also influences how levels are
  477. handled by the exporters.
  478. Changing it requires restart of `font-lock-mode' to become effective
  479. for fontification also in regions already fontified.
  480. You may also set this on a per-file basis by adding one of the following
  481. lines to the buffer:
  482. #+STARTUP: odd
  483. #+STARTUP: oddeven"
  484. :group 'org-edit-structure
  485. :group 'org-font-lock
  486. :type 'boolean)
  487. (defcustom org-adapt-indentation t
  488. "Non-nil means, adapt indentation when promoting and demoting.
  489. When this is set and the *entire* text in an entry is indented, the
  490. indentation is increased by one space in a demotion command, and
  491. decreased by one in a promotion command. If any line in the entry
  492. body starts at column 0, indentation is not changed at all."
  493. :group 'org-edit-structure
  494. :type 'boolean)
  495. (defcustom org-blank-before-new-entry '((heading . nil)
  496. (plain-list-item . nil))
  497. "Should `org-insert-heading' leave a blank line before new heading/item?
  498. The value is an alist, with `heading' and `plain-list-item' as car,
  499. and a boolean flag as cdr."
  500. :group 'org-edit-structure
  501. :type '(list
  502. (cons (const heading) (boolean))
  503. (cons (const plain-list-item) (boolean))))
  504. (defcustom org-insert-heading-hook nil
  505. "Hook being run after inserting a new heading."
  506. :group 'org-edit-structure
  507. :type 'boolean)
  508. (defcustom org-enable-fixed-width-editor t
  509. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  510. This currently only means, they are never auto-wrapped.
  511. When nil, such lines will be treated like ordinary lines.
  512. See also the QUOTE keyword."
  513. :group 'org-edit-structure
  514. :type 'boolean)
  515. (defgroup org-sparse-trees nil
  516. "Options concerning sparse trees in Org-mode."
  517. :tag "Org Sparse Trees"
  518. :group 'org-structure)
  519. (defcustom org-highlight-sparse-tree-matches t
  520. "Non-nil means, highlight all matches that define a sparse tree.
  521. The highlights will automatically disappear the next time the buffer is
  522. changed by an edit command."
  523. :group 'org-sparse-trees
  524. :type 'boolean)
  525. (defcustom org-remove-highlights-with-change t
  526. "Non-nil means, any change to the buffer will remove temporary highlights.
  527. Such highlights are created by `org-occur' and `org-clock-display'.
  528. When nil, `C-c C-c needs to be used to get rid of the highlights.
  529. The highlights created by `org-preview-latex-fragment' always need
  530. `C-c C-c' to be removed."
  531. :group 'org-sparse-trees
  532. :group 'org-time
  533. :type 'boolean)
  534. (defcustom org-occur-hook '(org-first-headline-recenter)
  535. "Hook that is run after `org-occur' has constructed a sparse tree.
  536. This can be used to recenter the window to show as much of the structure
  537. as possible."
  538. :group 'org-sparse-trees
  539. :type 'hook)
  540. (defgroup org-plain-lists nil
  541. "Options concerning plain lists in Org-mode."
  542. :tag "Org Plain lists"
  543. :group 'org-structure)
  544. (defcustom org-cycle-include-plain-lists nil
  545. "Non-nil means, include plain lists into visibility cycling.
  546. This means that during cycling, plain list items will *temporarily* be
  547. interpreted as outline headlines with a level given by 1000+i where i is the
  548. indentation of the bullet. In all other operations, plain list items are
  549. not seen as headlines. For example, you cannot assign a TODO keyword to
  550. such an item."
  551. :group 'org-plain-lists
  552. :type 'boolean)
  553. (defcustom org-plain-list-ordered-item-terminator t
  554. "The character that makes a line with leading number an ordered list item.
  555. Valid values are ?. and ?\). To get both terminators, use t. While
  556. ?. may look nicer, it creates the danger that a line with leading
  557. number may be incorrectly interpreted as an item. ?\) therefore is
  558. the safe choice."
  559. :group 'org-plain-lists
  560. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  561. (const :tag "paren like in \"2)\"" ?\))
  562. (const :tab "both" t)))
  563. (defcustom org-auto-renumber-ordered-lists t
  564. "Non-nil means, automatically renumber ordered plain lists.
  565. Renumbering happens when the sequence have been changed with
  566. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  567. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  568. :group 'org-plain-lists
  569. :type 'boolean)
  570. (defcustom org-provide-checkbox-statistics t
  571. "Non-nil means, update checkbox statistics after insert and toggle.
  572. When this is set, checkbox statistics is updated each time you either insert
  573. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  574. with \\[org-ctrl-c-ctrl-c\\]."
  575. :group 'org-plain-lists
  576. :type 'boolean)
  577. (defgroup org-archive nil
  578. "Options concerning archiving in Org-mode."
  579. :tag "Org Archive"
  580. :group 'org-structure)
  581. (defcustom org-archive-tag "ARCHIVE"
  582. "The tag that marks a subtree as archived.
  583. An archived subtree does not open during visibility cycling, and does
  584. not contribute to the agenda listings."
  585. :group 'org-archive
  586. :group 'org-keywords
  587. :type 'string)
  588. (defcustom org-agenda-skip-archived-trees t
  589. "Non-nil means, the agenda will skip any items located in archived trees.
  590. An archived tree is a tree marked with the tag ARCHIVE."
  591. :group 'org-archive
  592. :group 'org-agenda-skip
  593. :type 'boolean)
  594. (defcustom org-cycle-open-archived-trees nil
  595. "Non-nil means, `org-cycle' will open archived trees.
  596. An archived tree is a tree marked with the tag ARCHIVE.
  597. When nil, archived trees will stay folded. You can still open them with
  598. normal outline commands like `show-all', but not with the cycling commands."
  599. :group 'org-archive
  600. :group 'org-cycle
  601. :type 'boolean)
  602. (defcustom org-sparse-tree-open-archived-trees nil
  603. "Non-nil means sparse tree construction shows matches in archived trees.
  604. When nil, matches in these trees are highlighted, but the trees are kept in
  605. collapsed state."
  606. :group 'org-archive
  607. :group 'org-sparse-trees
  608. :type 'boolean)
  609. (defcustom org-archive-location "%s_archive::"
  610. "The location where subtrees should be archived.
  611. This string consists of two parts, separated by a double-colon.
  612. The first part is a file name - when omitted, archiving happens in the same
  613. file. %s will be replaced by the current file name (without directory part).
  614. Archiving to a different file is useful to keep archived entries from
  615. contributing to the Org-mode Agenda.
  616. The part after the double colon is a headline. The archived entries will be
  617. filed under that headline. When omitted, the subtrees are simply filed away
  618. at the end of the file, as top-level entries.
  619. Here are a few examples:
  620. \"%s_archive::\"
  621. If the current file is Projects.org, archive in file
  622. Projects.org_archive, as top-level trees. This is the default.
  623. \"::* Archived Tasks\"
  624. Archive in the current file, under the top-level headline
  625. \"* Archived Tasks\".
  626. \"~/org/archive.org::\"
  627. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  628. \"basement::** Finished Tasks\"
  629. Archive in file ./basement (relative path), as level 3 trees
  630. below the level 2 heading \"** Finished Tasks\".
  631. You may set this option on a per-file basis by adding to the buffer a
  632. line like
  633. #+ARCHIVE: basement::** Finished Tasks"
  634. :group 'org-archive
  635. :type 'string)
  636. (defcustom org-archive-mark-done t
  637. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  638. This can be a string to set the keyword to use. When t, Org-mode will
  639. use the first keyword in its list that means done."
  640. :group 'org-archive
  641. :type '(choice
  642. (const :tag "No" nil)
  643. (const :tag "Yes" t)
  644. (string :tag "Use this keyword")))
  645. (defcustom org-archive-stamp-time t
  646. "Non-nil means, add a time stamp to entries moved to an archive file.
  647. This variable is obsolete and has no effect anymore, instead add ot remove
  648. `time' from the variablle `org-archive-save-context-info'."
  649. :group 'org-archive
  650. :type 'boolean)
  651. (defcustom org-archive-save-context-info '(time file category todo itags)
  652. "Parts of context info that should be stored as properties when archiving.
  653. When a subtree is moved to an archive file, it looses information given by
  654. context, like inherited tags, the category, and possibly also the TODO
  655. state (depending on the variable `org-archive-mark-done').
  656. This variable can be a list of any of the following symbols:
  657. time The time of archiving.
  658. file The file where the entry originates.
  659. itags The local tags, in the headline of the subtree.
  660. ltags The tags the subtree inherits from further up the hierarchy.
  661. todo The pre-archive TODO state.
  662. category The category, taken from file name or #+CATEGORY lines.
  663. For each symbol present in the list, a property will be created in
  664. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  665. information."
  666. :group 'org-archive
  667. :type '(set :greedy t
  668. (const :tag "Time" time)
  669. (const :tag "File" file)
  670. (const :tag "Category" category)
  671. (const :tag "TODO state" todo)
  672. (const :tag "TODO state" priority)
  673. (const :tag "Inherited tags" itags)
  674. (const :tag "Local tags" ltags)))
  675. (defgroup org-table nil
  676. "Options concerning tables in Org-mode."
  677. :tag "Org Table"
  678. :group 'org)
  679. (defcustom org-enable-table-editor 'optimized
  680. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  681. When nil, such lines will be treated like ordinary lines.
  682. When equal to the symbol `optimized', the table editor will be optimized to
  683. do the following:
  684. - Automatic overwrite mode in front of whitespace in table fields.
  685. This makes the structure of the table stay in tact as long as the edited
  686. field does not exceed the column width.
  687. - Minimize the number of realigns. Normally, the table is aligned each time
  688. TAB or RET are pressed to move to another field. With optimization this
  689. happens only if changes to a field might have changed the column width.
  690. Optimization requires replacing the functions `self-insert-command',
  691. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  692. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  693. very good at guessing when a re-align will be necessary, but you can always
  694. force one with \\[org-ctrl-c-ctrl-c].
  695. If you would like to use the optimized version in Org-mode, but the
  696. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  697. This variable can be used to turn on and off the table editor during a session,
  698. but in order to toggle optimization, a restart is required.
  699. See also the variable `org-table-auto-blank-field'."
  700. :group 'org-table
  701. :type '(choice
  702. (const :tag "off" nil)
  703. (const :tag "on" t)
  704. (const :tag "on, optimized" optimized)))
  705. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  706. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  707. In the optimized version, the table editor takes over all simple keys that
  708. normally just insert a character. In tables, the characters are inserted
  709. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  710. for empty fields). Outside tables, the correct binding of the keys is
  711. restored.
  712. The default for this option is t if the optimized version is also used in
  713. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  714. this variable requires a restart of Emacs to become effective."
  715. :group 'org-table
  716. :type 'boolean)
  717. (defcustom orgtbl-radio-table-templates
  718. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  719. % END RECEIVE ORGTBL %n
  720. \\begin{comment}
  721. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  722. | | |
  723. \\end{comment}\n")
  724. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  725. @c END RECEIVE ORGTBL %n
  726. @ignore
  727. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  728. | | |
  729. @end ignore\n")
  730. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  731. <!-- END RECEIVE ORGTBL %n -->
  732. <!--
  733. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  734. | | |
  735. -->\n"))
  736. "Templates for radio tables in different major modes.
  737. All occurrences of %n in a template will be replaced with the name of the
  738. table, obtained by prompting the user."
  739. :group 'org-table
  740. :type '(repeat
  741. (list (symbol :tag "Major mode")
  742. (string :tag "Format"))))
  743. (defgroup org-table-settings nil
  744. "Settings for tables in Org-mode."
  745. :tag "Org Table Settings"
  746. :group 'org-table)
  747. (defcustom org-table-default-size "5x2"
  748. "The default size for newly created tables, Columns x Rows."
  749. :group 'org-table-settings
  750. :type 'string)
  751. (defcustom org-table-number-regexp
  752. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  753. "Regular expression for recognizing numbers in table columns.
  754. If a table column contains mostly numbers, it will be aligned to the
  755. right. If not, it will be aligned to the left.
  756. The default value of this option is a regular expression which allows
  757. anything which looks remotely like a number as used in scientific
  758. context. For example, all of the following will be considered a
  759. number:
  760. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  761. Other options offered by the customize interface are more restrictive."
  762. :group 'org-table-settings
  763. :type '(choice
  764. (const :tag "Positive Integers"
  765. "^[0-9]+$")
  766. (const :tag "Integers"
  767. "^[-+]?[0-9]+$")
  768. (const :tag "Floating Point Numbers"
  769. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  770. (const :tag "Floating Point Number or Integer"
  771. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  772. (const :tag "Exponential, Floating point, Integer"
  773. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  774. (const :tag "Very General Number-Like, including hex"
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  776. (string :tag "Regexp:")))
  777. (defcustom org-table-number-fraction 0.5
  778. "Fraction of numbers in a column required to make the column align right.
  779. In a column all non-white fields are considered. If at least this
  780. fraction of fields is matched by `org-table-number-fraction',
  781. alignment to the right border applies."
  782. :group 'org-table-settings
  783. :type 'number)
  784. (defgroup org-table-editing nil
  785. "Bahavior of tables during editing in Org-mode."
  786. :tag "Org Table Editing"
  787. :group 'org-table)
  788. (defcustom org-table-automatic-realign t
  789. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  790. When nil, aligning is only done with \\[org-table-align], or after column
  791. removal/insertion."
  792. :group 'org-table-editing
  793. :type 'boolean)
  794. (defcustom org-table-auto-blank-field t
  795. "Non-nil means, automatically blank table field when starting to type into it.
  796. This only happens when typing immediately after a field motion
  797. command (TAB, S-TAB or RET).
  798. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  799. :group 'org-table-editing
  800. :type 'boolean)
  801. (defcustom org-table-tab-jumps-over-hlines t
  802. "Non-nil means, tab in the last column of a table with jump over a hline.
  803. If a horizontal separator line is following the current line,
  804. `org-table-next-field' can either create a new row before that line, or jump
  805. over the line. When this option is nil, a new line will be created before
  806. this line."
  807. :group 'org-table-editing
  808. :type 'boolean)
  809. (defcustom org-table-tab-recognizes-table.el t
  810. "Non-nil means, TAB will automatically notice a table.el table.
  811. When it sees such a table, it moves point into it and - if necessary -
  812. calls `table-recognize-table'."
  813. :group 'org-table-editing
  814. :type 'boolean)
  815. (defgroup org-table-calculation nil
  816. "Options concerning tables in Org-mode."
  817. :tag "Org Table Calculation"
  818. :group 'org-table)
  819. (defcustom org-table-use-standard-references t
  820. "Should org-mode work with table refrences like B3 instead of @3$2?
  821. Possible values are:
  822. nil never use them
  823. from accept as input, do not present for editing
  824. t: accept as input and present for editing"
  825. :group 'org-table-calculation
  826. :type '(choice
  827. (const :tag "Never, don't even check unser input for them" nil)
  828. (const :tag "Always, both as user input, and when editing" t)
  829. (const :tag "Convert user input, don't offer during editing" 'from)))
  830. (defcustom org-table-copy-increment t
  831. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  832. :group 'org-table-calculation
  833. :type 'boolean)
  834. (defcustom org-calc-default-modes
  835. '(calc-internal-prec 12
  836. calc-float-format (float 5)
  837. calc-angle-mode deg
  838. calc-prefer-frac nil
  839. calc-symbolic-mode nil
  840. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  841. calc-display-working-message t
  842. )
  843. "List with Calc mode settings for use in calc-eval for table formulas.
  844. The list must contain alternating symbols (Calc modes variables and values).
  845. Don't remove any of the default settings, just change the values. Org-mode
  846. relies on the variables to be present in the list."
  847. :group 'org-table-calculation
  848. :type 'plist)
  849. (defcustom org-table-formula-evaluate-inline t
  850. "Non-nil means, TAB and RET evaluate a formula in current table field.
  851. If the current field starts with an equal sign, it is assumed to be a formula
  852. which should be evaluated as described in the manual and in the documentation
  853. string of the command `org-table-eval-formula'. This feature requires the
  854. Emacs calc package.
  855. When this variable is nil, formula calculation is only available through
  856. the command \\[org-table-eval-formula]."
  857. :group 'org-table-calculation
  858. :type 'boolean)
  859. (defcustom org-table-formula-use-constants t
  860. "Non-nil means, interpret constants in formulas in tables.
  861. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  862. by the value given in `org-table-formula-constants', or by a value obtained
  863. from the `constants.el' package."
  864. :group 'org-table-calculation
  865. :type 'boolean)
  866. (defcustom org-table-formula-constants nil
  867. "Alist with constant names and values, for use in table formulas.
  868. The car of each element is a name of a constant, without the `$' before it.
  869. The cdr is the value as a string. For example, if you'd like to use the
  870. speed of light in a formula, you would configure
  871. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  872. and then use it in an equation like `$1*$c'.
  873. Constants can also be defined on a per-file basis using a line like
  874. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  875. :group 'org-table-calculation
  876. :type '(repeat
  877. (cons (string :tag "name")
  878. (string :tag "value"))))
  879. (defvar org-table-formula-constants-local nil
  880. "Local version of `org-table-formula-constants'.")
  881. (make-variable-buffer-local 'org-table-formula-constants-local)
  882. (defcustom org-table-allow-automatic-line-recalculation t
  883. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  884. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  885. :group 'org-table-calculation
  886. :type 'boolean)
  887. (defgroup org-link nil
  888. "Options concerning links in Org-mode."
  889. :tag "Org Link"
  890. :group 'org)
  891. (defvar org-link-abbrev-alist-local nil
  892. "Buffer-local version of `org-link-abbrev-alist', which see.
  893. The value of this is taken from the #+LINK lines.")
  894. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  895. (defcustom org-link-abbrev-alist nil
  896. "Alist of link abbreviations.
  897. The car of each element is a string, to be replaced at the start of a link.
  898. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  899. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  900. [[linkkey:tag][description]]
  901. If REPLACE is a string, the tag will simply be appended to create the link.
  902. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  903. also be a function that will be called with the tag as the only argument to
  904. create the link. See the manual for examples."
  905. :group 'org-link
  906. :type 'alist)
  907. (defcustom org-descriptive-links t
  908. "Non-nil means, hide link part and only show description of bracket links.
  909. Bracket links are like [[link][descritpion]]. This variable sets the initial
  910. state in new org-mode buffers. The setting can then be toggled on a
  911. per-buffer basis from the Org->Hyperlinks menu."
  912. :group 'org-link
  913. :type 'boolean)
  914. (defcustom org-link-file-path-type 'adaptive
  915. "How the path name in file links should be stored.
  916. Valid values are:
  917. relative relative to the current directory, i.e. the directory of the file
  918. into which the link is being inserted.
  919. absolute absolute path, if possible with ~ for home directory.
  920. noabbrev absolute path, no abbreviation of home directory.
  921. adaptive Use relative path for files in the current directory and sub-
  922. directories of it. For other files, use an absolute path."
  923. :group 'org-link
  924. :type '(choice
  925. (const relative)
  926. (const absolute)
  927. (const noabbrev)
  928. (const adaptive)))
  929. (defcustom org-activate-links '(bracket angle plain radio tag date)
  930. "Types of links that should be activated in Org-mode files.
  931. This is a list of symbols, each leading to the activation of a certain link
  932. type. In principle, it does not hurt to turn on most link types - there may
  933. be a small gain when turning off unused link types. The types are:
  934. bracket The recommended [[link][description]] or [[link]] links with hiding.
  935. angular Links in angular brackes that may contain whitespace like
  936. <bbdb:Carsten Dominik>.
  937. plain Plain links in normal text, no whitespace, like http://google.com.
  938. radio Text that is matched by a radio target, see manual for details.
  939. tag Tag settings in a headline (link to tag search).
  940. date Time stamps (link to calendar).
  941. Changing this variable requires a restart of Emacs to become effective."
  942. :group 'org-link
  943. :type '(set (const :tag "Double bracket links (new style)" bracket)
  944. (const :tag "Angular bracket links (old style)" angular)
  945. (const :tag "plain text links" plain)
  946. (const :tag "Radio target matches" radio)
  947. (const :tag "Tags" tag)
  948. (const :tag "Tags" target)
  949. (const :tag "Timestamps" date)))
  950. (defgroup org-link-store nil
  951. "Options concerning storing links in Org-mode"
  952. :tag "Org Store Link"
  953. :group 'org-link)
  954. (defcustom org-email-link-description-format "Email %c: %.30s"
  955. "Format of the description part of a link to an email or usenet message.
  956. The following %-excapes will be replaced by corresponding information:
  957. %F full \"From\" field
  958. %f name, taken from \"From\" field, address if no name
  959. %T full \"To\" field
  960. %t first name in \"To\" field, address if no name
  961. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  962. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  963. %s subject
  964. %m message-id.
  965. You may use normal field width specification between the % and the letter.
  966. This is for example useful to limit the length of the subject.
  967. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  968. :group 'org-link-store
  969. :type 'string)
  970. (defcustom org-from-is-user-regexp
  971. (let (r1 r2)
  972. (when (and user-mail-address (not (string= user-mail-address "")))
  973. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  974. (when (and user-full-name (not (string= user-full-name "")))
  975. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  976. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  977. "Regexp mached against the \"From:\" header of an email or usenet message.
  978. It should match if the message is from the user him/herself."
  979. :group 'org-link-store
  980. :type 'regexp)
  981. (defcustom org-context-in-file-links t
  982. "Non-nil means, file links from `org-store-link' contain context.
  983. A search string will be added to the file name with :: as separator and
  984. used to find the context when the link is activated by the command
  985. `org-open-at-point'.
  986. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  987. negates this setting for the duration of the command."
  988. :group 'org-link-store
  989. :type 'boolean)
  990. (defcustom org-keep-stored-link-after-insertion nil
  991. "Non-nil means, keep link in list for entire session.
  992. The command `org-store-link' adds a link pointing to the current
  993. location to an internal list. These links accumulate during a session.
  994. The command `org-insert-link' can be used to insert links into any
  995. Org-mode file (offering completion for all stored links). When this
  996. option is nil, every link which has been inserted once using \\[org-insert-link]
  997. will be removed from the list, to make completing the unused links
  998. more efficient."
  999. :group 'org-link-store
  1000. :type 'boolean)
  1001. (defcustom org-usenet-links-prefer-google nil
  1002. "Non-nil means, `org-store-link' will create web links to Google groups.
  1003. When nil, Gnus will be used for such links.
  1004. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1005. negates this setting for the duration of the command."
  1006. :group 'org-link-store
  1007. :type 'boolean)
  1008. (defgroup org-link-follow nil
  1009. "Options concerning following links in Org-mode"
  1010. :tag "Org Follow Link"
  1011. :group 'org-link)
  1012. (defcustom org-tab-follows-link nil
  1013. "Non-nil means, on links TAB will follow the link.
  1014. Needs to be set before org.el is loaded."
  1015. :group 'org-link-follow
  1016. :type 'boolean)
  1017. (defcustom org-return-follows-link nil
  1018. "Non-nil means, on links RET will follow the link.
  1019. Needs to be set before org.el is loaded."
  1020. :group 'org-link-follow
  1021. :type 'boolean)
  1022. (defcustom org-mouse-1-follows-link t
  1023. "Non-nil means, mouse-1 on a link will follow the link.
  1024. A longer mouse click will still set point. Does not wortk on XEmacs.
  1025. Needs to be set before org.el is loaded."
  1026. :group 'org-link-follow
  1027. :type 'boolean)
  1028. (defcustom org-mark-ring-length 4
  1029. "Number of different positions to be recorded in the ring
  1030. Changing this requires a restart of Emacs to work correctly."
  1031. :group 'org-link-follow
  1032. :type 'interger)
  1033. (defcustom org-link-frame-setup
  1034. '((vm . vm-visit-folder-other-frame)
  1035. (gnus . gnus-other-frame)
  1036. (file . find-file-other-window))
  1037. "Setup the frame configuration for following links.
  1038. When following a link with Emacs, it may often be useful to display
  1039. this link in another window or frame. This variable can be used to
  1040. set this up for the different types of links.
  1041. For VM, use any of
  1042. `vm-visit-folder'
  1043. `vm-visit-folder-other-frame'
  1044. For Gnus, use any of
  1045. `gnus'
  1046. `gnus-other-frame'
  1047. For FILE, use any of
  1048. `find-file'
  1049. `find-file-other-window'
  1050. `find-file-other-frame'
  1051. For the calendar, use the variable `calendar-setup'.
  1052. For BBDB, it is currently only possible to display the matches in
  1053. another window."
  1054. :group 'org-link-follow
  1055. :type '(list
  1056. (cons (const vm)
  1057. (choice
  1058. (const vm-visit-folder)
  1059. (const vm-visit-folder-other-window)
  1060. (const vm-visit-folder-other-frame)))
  1061. (cons (const gnus)
  1062. (choice
  1063. (const gnus)
  1064. (const gnus-other-frame)))
  1065. (cons (const file)
  1066. (choice
  1067. (const find-file)
  1068. (const find-file-other-window)
  1069. (const find-file-other-frame)))))
  1070. (defcustom org-display-internal-link-with-indirect-buffer nil
  1071. "Non-nil means, use indirect buffer to display infile links.
  1072. Activating internal links (from one location in a file to another location
  1073. in the same file) normally just jumps to the location. When the link is
  1074. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1075. another window. When this option is set, the other window actually displays
  1076. an indirect buffer clone of the current buffer, to avoid any visibility
  1077. changes to the current buffer."
  1078. :group 'org-link-follow
  1079. :type 'boolean)
  1080. (defcustom org-open-non-existing-files nil
  1081. "Non-nil means, `org-open-file' will open non-existing files.
  1082. When nil, an error will be generated."
  1083. :group 'org-link-follow
  1084. :type 'boolean)
  1085. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1086. "Function and arguments to call for following mailto links.
  1087. This is a list with the first element being a lisp function, and the
  1088. remaining elements being arguments to the function. In string arguments,
  1089. %a will be replaced by the address, and %s will be replaced by the subject
  1090. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1091. :group 'org-link-follow
  1092. :type '(choice
  1093. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1094. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1095. (const :tag "message-mail" (message-mail "%a" "%s"))
  1096. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1097. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1098. "Non-nil means, ask for confirmation before executing shell links.
  1099. Shell links can be dangerous: just think about a link
  1100. [[shell:rm -rf ~/*][Google Search]]
  1101. This link would show up in your Org-mode document as \"Google Search\",
  1102. but really it would remove your entire home directory.
  1103. Therefore we advise against setting this variable to nil.
  1104. Just change it to `y-or-n-p' of you want to confirm with a
  1105. single keystroke rather than having to type \"yes\"."
  1106. :group 'org-link-follow
  1107. :type '(choice
  1108. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1109. (const :tag "with y-or-n (faster)" y-or-n-p)
  1110. (const :tag "no confirmation (dangerous)" nil)))
  1111. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1112. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1113. Elisp links can be dangerous: just think about a link
  1114. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1115. This link would show up in your Org-mode document as \"Google Search\",
  1116. but really it would remove your entire home directory.
  1117. Therefore we advise against setting this variable to nil.
  1118. Just change it to `y-or-n-p' of you want to confirm with a
  1119. single keystroke rather than having to type \"yes\"."
  1120. :group 'org-link-follow
  1121. :type '(choice
  1122. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1123. (const :tag "with y-or-n (faster)" y-or-n-p)
  1124. (const :tag "no confirmation (dangerous)" nil)))
  1125. (defconst org-file-apps-defaults-gnu
  1126. '((remote . emacs)
  1127. (t . mailcap))
  1128. "Default file applications on a UNIX or GNU/Linux system.
  1129. See `org-file-apps'.")
  1130. (defconst org-file-apps-defaults-macosx
  1131. '((remote . emacs)
  1132. (t . "open %s")
  1133. ("ps" . "gv %s")
  1134. ("ps.gz" . "gv %s")
  1135. ("eps" . "gv %s")
  1136. ("eps.gz" . "gv %s")
  1137. ("dvi" . "xdvi %s")
  1138. ("fig" . "xfig %s"))
  1139. "Default file applications on a MacOS X system.
  1140. The system \"open\" is known as a default, but we use X11 applications
  1141. for some files for which the OS does not have a good default.
  1142. See `org-file-apps'.")
  1143. (defconst org-file-apps-defaults-windowsnt
  1144. (list
  1145. '(remote . emacs)
  1146. (cons t
  1147. (list (if (featurep 'xemacs)
  1148. 'mswindows-shell-execute
  1149. 'w32-shell-execute)
  1150. "open" 'file)))
  1151. "Default file applications on a Windows NT system.
  1152. The system \"open\" is used for most files.
  1153. See `org-file-apps'.")
  1154. (defcustom org-file-apps
  1155. '(
  1156. ("txt" . emacs)
  1157. ("tex" . emacs)
  1158. ("ltx" . emacs)
  1159. ("org" . emacs)
  1160. ("el" . emacs)
  1161. ("bib" . emacs)
  1162. )
  1163. "External applications for opening `file:path' items in a document.
  1164. Org-mode uses system defaults for different file types, but
  1165. you can use this variable to set the application for a given file
  1166. extension. The entries in this list are cons cells where the car identifies
  1167. files and the cdr the corresponding command. Possible values for the
  1168. file identifier are
  1169. \"ext\" A string identifying an extension
  1170. `directory' Matches a directory
  1171. `remote' Matches a remote file, accessible through tramp or efs.
  1172. Remote files most likely should be visited through Emacs
  1173. because external applications cannot handle such paths.
  1174. t Default for all remaining files
  1175. Possible values for the command are:
  1176. `emacs' The file will be visited by the current Emacs process.
  1177. `default' Use the default application for this file type.
  1178. string A command to be executed by a shell; %s will be replaced
  1179. by the path to the file.
  1180. sexp A Lisp form which will be evaluated. The file path will
  1181. be available in the Lisp variable `file'.
  1182. For more examples, see the system specific constants
  1183. `org-file-apps-defaults-macosx'
  1184. `org-file-apps-defaults-windowsnt'
  1185. `org-file-apps-defaults-gnu'."
  1186. :group 'org-link-follow
  1187. :type '(repeat
  1188. (cons (choice :value ""
  1189. (string :tag "Extension")
  1190. (const :tag "Default for unrecognized files" t)
  1191. (const :tag "Remote file" remote)
  1192. (const :tag "Links to a directory" directory))
  1193. (choice :value ""
  1194. (const :tag "Visit with Emacs" emacs)
  1195. (const :tag "Use system default" default)
  1196. (string :tag "Command")
  1197. (sexp :tag "Lisp form")))))
  1198. (defcustom org-mhe-search-all-folders nil
  1199. "Non-nil means, that the search for the mh-message will be extended to
  1200. all folders if the message cannot be found in the folder given in the link.
  1201. Searching all folders is very efficient with one of the search engines
  1202. supported by MH-E, but will be slow with pick."
  1203. :group 'org-link-follow
  1204. :type 'boolean)
  1205. (defgroup org-remember nil
  1206. "Options concerning interaction with remember.el."
  1207. :tag "Org Remember"
  1208. :group 'org)
  1209. (defcustom org-directory "~/org"
  1210. "Directory with org files.
  1211. This directory will be used as default to prompt for org files.
  1212. Used by the hooks for remember.el."
  1213. :group 'org-remember
  1214. :type 'directory)
  1215. (defcustom org-default-notes-file "~/.notes"
  1216. "Default target for storing notes.
  1217. Used by the hooks for remember.el. This can be a string, or nil to mean
  1218. the value of `remember-data-file'.
  1219. You can set this on a per-template basis with the variable
  1220. `org-remember-templates'."
  1221. :group 'org-remember
  1222. :type '(choice
  1223. (const :tag "Default from remember-data-file" nil)
  1224. file))
  1225. (defcustom org-remember-store-without-prompt t
  1226. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1227. In this case, you need `C-u C-c C-c' to get the prompts for
  1228. note file and headline.
  1229. When this variable is nil, `C-c C-c' give you the prompts, and
  1230. `C-u C-c C-c' trigger the fasttrack."
  1231. :group 'org-remember
  1232. :type 'boolean)
  1233. (defcustom org-remember-default-headline ""
  1234. "The headline that should be the default location in the notes file.
  1235. When filing remember notes, the cursor will start at that position.
  1236. You can set this on a per-template basis with the variable
  1237. `org-remember-templates'."
  1238. :group 'org-remember
  1239. :type 'string)
  1240. (defcustom org-remember-templates nil
  1241. "Templates for the creation of remember buffers.
  1242. When nil, just let remember make the buffer.
  1243. When not nil, this is a list of 5-element lists. In each entry, the first
  1244. element is a the name of the template, It should be a single short word.
  1245. The second element is a character, a unique key to select this template.
  1246. The third element is the template. The forth element is optional and can
  1247. specify a destination file for remember items created with this template.
  1248. The default file is given by `org-default-notes-file'. An optional fifth
  1249. element can specify the headline in that file that should be offered
  1250. first when the user is asked to file the entry. The default headline is
  1251. given in the variable `org-remember-default-headline'.
  1252. The template specifies the structure of the remember buffer. It should have
  1253. a first line starting with a star, to act as the org-mode headline.
  1254. Furthermore, the following %-escapes will be replaced with content:
  1255. %^{prompt} prompt the user for a string and replace this sequence with it.
  1256. %t time stamp, date only
  1257. %T time stamp with date and time
  1258. %u, %U like the above, but inactive time stamps
  1259. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1260. You may define a prompt like %^{Please specify birthday}t
  1261. %n user name (taken from `user-full-name')
  1262. %a annotation, normally the link created with org-store-link
  1263. %i initial content, the region when remember is called with C-u.
  1264. If %i is indented, the entire inserted text will be indented
  1265. as well.
  1266. %? After completing the template, position cursor here.
  1267. Apart from these general escapes, you can access information specific to the
  1268. link type that is created. For example, calling `remember' in emails or gnus
  1269. will record the author and the subject of the message, which you can access
  1270. with %:author and %:subject, respectively. Here is a complete list of what
  1271. is recorded for each link type.
  1272. Link type | Available information
  1273. -------------------+------------------------------------------------------
  1274. bbdb | %:type %:name %:company
  1275. vm, wl, mh, rmail | %:type %:subject %:message-id
  1276. | %:from %:fromname %:fromaddress
  1277. | %:to %:toname %:toaddress
  1278. | %:fromto (either \"to NAME\" or \"from NAME\")
  1279. gnus | %:group, for messages also all email fields
  1280. w3, w3m | %:type %:url
  1281. info | %:type %:file %:node
  1282. calendar | %:type %:date"
  1283. :group 'org-remember
  1284. :get (lambda (var) ; Make sure all entries have 5 elements
  1285. (mapcar (lambda (x)
  1286. (if (not (stringp (car x))) (setq x (cons "" x)))
  1287. (cond ((= (length x) 4) (append x '("")))
  1288. ((= (length x) 3) (append x '("" "")))
  1289. (t x)))
  1290. (default-value var)))
  1291. :type '(repeat
  1292. :tag "enabled"
  1293. (list :value ("" ?a "\n" nil nil)
  1294. (string :tag "Name")
  1295. (character :tag "Selection Key")
  1296. (string :tag "Template")
  1297. (file :tag "Destination file (optional)")
  1298. (string :tag "Destination headline (optional)"))))
  1299. (defcustom org-reverse-note-order nil
  1300. "Non-nil means, store new notes at the beginning of a file or entry.
  1301. When nil, new notes will be filed to the end of a file or entry."
  1302. :group 'org-remember
  1303. :type '(choice
  1304. (const :tag "Reverse always" t)
  1305. (const :tag "Reverse never" nil)
  1306. (repeat :tag "By file name regexp"
  1307. (cons regexp boolean))))
  1308. (defgroup org-todo nil
  1309. "Options concerning TODO items in Org-mode."
  1310. :tag "Org TODO"
  1311. :group 'org)
  1312. (defgroup org-progress nil
  1313. "Options concerning Progress logging in Org-mode."
  1314. :tag "Org Progress"
  1315. :group 'org-time)
  1316. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1317. "List of TODO entry keyword sequences and their interpretation.
  1318. \\<org-mode-map>This is a list of sequences.
  1319. Each sequence starts with a symbol, either `sequence' or `type',
  1320. indicating if the keywords should be interpreted as a sequence of
  1321. action steps, or as different types of TODO items. The first
  1322. keywords are states requiring action - these states will select a headline
  1323. for inclusion into the global TODO list Org-mode produces. If one of
  1324. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1325. signify that no further action is necessary. If \"|\" is not found,
  1326. the last keyword is treated as the only DONE state of the sequence.
  1327. The command \\[org-todo] cycles an entry through these states, and one
  1328. additional state where no keyword is present. For details about this
  1329. cycling, see the manual.
  1330. TODO keywords and interpretation can also be set on a per-file basis with
  1331. the special #+SEQ_TODO and #+TYP_TODO lines.
  1332. For backward compatibility, this variable may also be just a list
  1333. of keywords - in this case the interptetation (sequence or type) will be
  1334. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1335. :group 'org-todo
  1336. :group 'org-keywords
  1337. :type '(choice
  1338. (repeat :tag "Old syntax, just keywords"
  1339. (string :tag "Keyword"))
  1340. (repeat :tag "New syntax"
  1341. (cons
  1342. (choice
  1343. :tag "Interpretation"
  1344. (const :tag "Sequence (cycling hits every state)" sequence)
  1345. (const :tag "Type (cycling directly to DONE)" type))
  1346. (repeat
  1347. (string :tag "Keyword"))))))
  1348. (defvar org-todo-keywords-1 nil)
  1349. (make-variable-buffer-local 'org-todo-keywords-1)
  1350. (defvar org-todo-keywords-for-agenda nil)
  1351. (defvar org-done-keywords-for-agenda nil)
  1352. (defvar org-not-done-keywords nil)
  1353. (make-variable-buffer-local 'org-not-done-keywords)
  1354. (defvar org-done-keywords nil)
  1355. (make-variable-buffer-local 'org-done-keywords)
  1356. (defvar org-todo-heads nil)
  1357. (make-variable-buffer-local 'org-todo-heads)
  1358. (defvar org-todo-sets nil)
  1359. (make-variable-buffer-local 'org-todo-sets)
  1360. (defvar org-todo-log-states nil)
  1361. (make-variable-buffer-local 'org-todo-log-states)
  1362. (defvar org-todo-kwd-alist nil)
  1363. (make-variable-buffer-local 'org-todo-kwd-alist)
  1364. (defvar org-todo-key-alist nil)
  1365. (make-variable-buffer-local 'org-todo-key-alist)
  1366. (defvar org-todo-key-trigger nil)
  1367. (make-variable-buffer-local 'org-todo-key-trigger)
  1368. (defcustom org-todo-interpretation 'sequence
  1369. "Controls how TODO keywords are interpreted.
  1370. This variable is in principle obsolete and is only used for
  1371. backward compatibility, if the interpretation of todo keywords is
  1372. not given already in `org-todo-keywords'. See that variable for
  1373. more information."
  1374. :group 'org-todo
  1375. :group 'org-keywords
  1376. :type '(choice (const sequence)
  1377. (const type)))
  1378. (defcustom org-use-fast-todo-selection 'prefix
  1379. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1380. This variable describes if and under what circumstances the cycling
  1381. mechanism for TODO keywords will be replaced by a single-key, direct
  1382. selection scheme.
  1383. When nil, fast selection is never used.
  1384. When the symbol `prefix', it will be used when `org-todo' is called with
  1385. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1386. in an agenda buffer.
  1387. When t, fast selection is used by default. In this case, the prefix
  1388. argument forces cycling instead.
  1389. In all cases, the special interface is only used if access keys have actually
  1390. been assigned by the user, i.e. if keywords in the configuration are followed
  1391. by a letter in parenthesis, like TODO(t)."
  1392. :group 'org-todo
  1393. :type '(choice
  1394. (const :tag "Never" nil)
  1395. (const :tag "By default" t)
  1396. (const :tag "Only with C-u C-c C-t" prefix)))
  1397. (defcustom org-after-todo-state-change-hook nil
  1398. "Hook which is run after the state of a TODO item was changed.
  1399. The new state (a string with a TODO keyword, or nil) is available in the
  1400. Lisp variable `state'."
  1401. :group 'org-todo
  1402. :type 'hook)
  1403. (defcustom org-log-done nil
  1404. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1405. When the state of an entry is changed from nothing or a DONE state to
  1406. a not-done TODO state, remove a previous closing date.
  1407. This can also be a list of symbols indicating under which conditions
  1408. the time stamp recording the action should be annotated with a short note.
  1409. Valid members of this list are
  1410. done Offer to record a note when marking entries done
  1411. state Offer to record a note whenever changing the TODO state
  1412. of an item. This is only relevant if TODO keywords are
  1413. interpreted as sequence, see variable `org-todo-interpretation'.
  1414. When `state' is set, this includes tracking `done'.
  1415. clock-out Offer to record a note when clocking out of an item.
  1416. A separate window will then pop up and allow you to type a note.
  1417. After finishing with C-c C-c, the note will be added directly after the
  1418. timestamp, as a plain list item. See also the variable
  1419. `org-log-note-headings'.
  1420. Logging can also be configured on a per-file basis by adding one of
  1421. the following lines anywhere in the buffer:
  1422. #+STARTUP: logdone
  1423. #+STARTUP: nologging
  1424. #+STARTUP: lognotedone
  1425. #+STARTUP: lognotestate
  1426. #+STARTUP: lognoteclock-out
  1427. You can have local logging settings for a subtree by setting the LOGGING
  1428. property to one or more of these keywords."
  1429. :group 'org-todo
  1430. :group 'org-progress
  1431. :type '(choice
  1432. (const :tag "off" nil)
  1433. (const :tag "on" t)
  1434. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1435. (const :tag "when item is marked DONE" done)
  1436. (const :tag "when TODO state changes" state)
  1437. (const :tag "when clocking out" clock-out))))
  1438. (defcustom org-log-done-with-time t
  1439. "Non-nil means, the CLOSED time stamp will contain date and time.
  1440. When nil, only the date will be recorded."
  1441. :group 'org-progress
  1442. :type 'boolean)
  1443. (defcustom org-log-note-headings
  1444. '((done . "CLOSING NOTE %t")
  1445. (state . "State %-12s %t")
  1446. (clock-out . ""))
  1447. "Headings for notes added when clocking out or closing TODO items.
  1448. The value is an alist, with the car being a symbol indicating the note
  1449. context, and the cdr is the heading to be used. The heading may also be the
  1450. empty string.
  1451. %t in the heading will be replaced by a time stamp.
  1452. %s will be replaced by the new TODO state, in double quotes.
  1453. %u will be replaced by the user name.
  1454. %U will be replaced by the full user name."
  1455. :group 'org-todo
  1456. :group 'org-progress
  1457. :type '(list :greedy t
  1458. (cons (const :tag "Heading when closing an item" done) string)
  1459. (cons (const :tag
  1460. "Heading when changing todo state (todo sequence only)"
  1461. state) string)
  1462. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1463. (defcustom org-log-states-order-reversed t
  1464. "Non-nil means, the latest state change note will be directly after heading.
  1465. When nil, the notes will be orderer according to time."
  1466. :group 'org-todo
  1467. :group 'org-progress
  1468. :type 'boolean)
  1469. (defcustom org-log-repeat t
  1470. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1471. When nil, no note will be taken.
  1472. This option can also be set with on a per-file-basis with
  1473. #+STARTUP: logrepeat
  1474. #+STARTUP: nologrepeat
  1475. You can have local logging settings for a subtree by setting the LOGGING
  1476. property to one or more of these keywords."
  1477. :group 'org-todo
  1478. :group 'org-progress
  1479. :type 'boolean)
  1480. (defcustom org-clock-into-drawer 2
  1481. "Should clocking info be wrapped into a drawer?
  1482. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1483. If necessary, the drawer will be created.
  1484. When nil, the drawer will not be created, but used when present.
  1485. When an integer and the number of clocking entries in an item
  1486. reaches or exceeds this number, a drawer will be created."
  1487. :group 'org-todo
  1488. :group 'org-progress
  1489. :type '(choice
  1490. (const :tag "Always" t)
  1491. (const :tag "Only when drawer exists" nil)
  1492. (integer :tag "When at least N clock entries")))
  1493. (defcustom org-clock-out-when-done t
  1494. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1495. Nil means, clock will keep running until stopped explicitly with
  1496. `C-c C-x C-o', or until the clock is started in a different item."
  1497. :group 'org-progress
  1498. :type 'boolean)
  1499. (defgroup org-priorities nil
  1500. "Priorities in Org-mode."
  1501. :tag "Org Priorities"
  1502. :group 'org-todo)
  1503. (defcustom org-highest-priority ?A
  1504. "The highest priority of TODO items. A character like ?A, ?B etc.
  1505. Must have a smaller ASCII number than `org-lowest-priority'."
  1506. :group 'org-priorities
  1507. :type 'character)
  1508. (defcustom org-lowest-priority ?C
  1509. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1510. Must have a larger ASCII number than `org-highest-priority'."
  1511. :group 'org-priorities
  1512. :type 'character)
  1513. (defcustom org-default-priority ?B
  1514. "The default priority of TODO items.
  1515. This is the priority an item get if no explicit priority is given."
  1516. :group 'org-priorities
  1517. :type 'character)
  1518. (defcustom org-priority-start-cycle-with-default t
  1519. "Non-nil means, start with default priority when starting to cycle.
  1520. When this is nil, the first step in the cycle will be (depending on the
  1521. command used) one higher or lower that the default priority."
  1522. :group 'org-priorities
  1523. :type 'boolean)
  1524. (defgroup org-time nil
  1525. "Options concerning time stamps and deadlines in Org-mode."
  1526. :tag "Org Time"
  1527. :group 'org)
  1528. (defcustom org-insert-labeled-timestamps-at-point nil
  1529. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1530. When nil, these labeled time stamps are forces into the second line of an
  1531. entry, just after the headline. When scheduling from the global TODO list,
  1532. the time stamp will always be forced into the second line."
  1533. :group 'org-time
  1534. :type 'boolean)
  1535. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1536. "Formats for `format-time-string' which are used for time stamps.
  1537. It is not recommended to change this constant.")
  1538. (defcustom org-time-stamp-rounding-minutes 0
  1539. "Number of minutes to round time stamps to upon insertion.
  1540. When zero, insert the time unmodified. Useful rounding numbers
  1541. should be factors of 60, so for example 5, 10, 15.
  1542. When this is not zero, you can still force an exact time-stamp by using
  1543. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1544. :group 'org-time
  1545. :type 'integer)
  1546. (defcustom org-display-custom-times nil
  1547. "Non-nil means, overlay custom formats over all time stamps.
  1548. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1549. To turn this on on a per-file basis, insert anywhere in the file:
  1550. #+STARTUP: customtime"
  1551. :group 'org-time
  1552. :set 'set-default
  1553. :type 'sexp)
  1554. (make-variable-buffer-local 'org-display-custom-times)
  1555. (defcustom org-time-stamp-custom-formats
  1556. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1557. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1558. These are overlayed over the default ISO format if the variable
  1559. `org-display-custom-times' is set. Time like %H:%M should be at the
  1560. end of the second format."
  1561. :group 'org-time
  1562. :type 'sexp)
  1563. (defun org-time-stamp-format (&optional long inactive)
  1564. "Get the right format for a time string."
  1565. (let ((f (if long (cdr org-time-stamp-formats)
  1566. (car org-time-stamp-formats))))
  1567. (if inactive
  1568. (concat "[" (substring f 1 -1) "]")
  1569. f)))
  1570. (defcustom org-popup-calendar-for-date-prompt t
  1571. "Non-nil means, pop up a calendar when prompting for a date.
  1572. In the calendar, the date can be selected with mouse-1. However, the
  1573. minibuffer will also be active, and you can simply enter the date as well.
  1574. When nil, only the minibuffer will be available."
  1575. :group 'org-time
  1576. :type 'boolean)
  1577. (defcustom org-edit-timestamp-down-means-later nil
  1578. "Non-nil means, S-down will increase the time in a time stamp.
  1579. When nil, S-up will increase."
  1580. :group 'org-time
  1581. :type 'boolean)
  1582. (defcustom org-calendar-follow-timestamp-change t
  1583. "Non-nil means, make the calendar window follow timestamp changes.
  1584. When a timestamp is modified and the calendar window is visible, it will be
  1585. moved to the new date."
  1586. :group 'org-time
  1587. :type 'boolean)
  1588. (defgroup org-tags nil
  1589. "Options concerning tags in Org-mode."
  1590. :tag "Org Tags"
  1591. :group 'org)
  1592. (defcustom org-tag-alist nil
  1593. "List of tags allowed in Org-mode files.
  1594. When this list is nil, Org-mode will base TAG input on what is already in the
  1595. buffer.
  1596. The value of this variable is an alist, the car of each entry must be a
  1597. keyword as a string, the cdr may be a character that is used to select
  1598. that tag through the fast-tag-selection interface.
  1599. See the manual for details."
  1600. :group 'org-tags
  1601. :type '(repeat
  1602. (choice
  1603. (cons (string :tag "Tag name")
  1604. (character :tag "Access char"))
  1605. (const :tag "Start radio group" (:startgroup))
  1606. (const :tag "End radio group" (:endgroup)))))
  1607. (defcustom org-use-fast-tag-selection 'auto
  1608. "Non-nil means, use fast tag selection scheme.
  1609. This is a special interface to select and deselect tags with single keys.
  1610. When nil, fast selection is never used.
  1611. When the symbol `auto', fast selection is used if and only if selection
  1612. characters for tags have been configured, either through the variable
  1613. `org-tag-alist' or through a #+TAGS line in the buffer.
  1614. When t, fast selection is always used and selection keys are assigned
  1615. automatically if necessary."
  1616. :group 'org-tags
  1617. :type '(choice
  1618. (const :tag "Always" t)
  1619. (const :tag "Never" nil)
  1620. (const :tag "When selection characters are configured" 'auto)))
  1621. (defcustom org-fast-tag-selection-single-key nil
  1622. "Non-nil means, fast tag selection exits after first change.
  1623. When nil, you have to press RET to exit it.
  1624. During fast tag selection, you can toggle this flag with `C-c'.
  1625. This variable can also have the value `expert'. In this case, the window
  1626. displaying the tags menu is not even shown, until you press C-c again."
  1627. :group 'org-tags
  1628. :type '(choice
  1629. (const :tag "No" nil)
  1630. (const :tag "Yes" t)
  1631. (const :tag "Expert" expert)))
  1632. (defvar org-fast-tag-selection-include-todo nil
  1633. "Non-nil means, fast tags selection interface will also offer TODO states.
  1634. This is an undocumented feature, you should not rely on it.")
  1635. (defcustom org-tags-column -80
  1636. "The column to which tags should be indented in a headline.
  1637. If this number is positive, it specifies the column. If it is negative,
  1638. it means that the tags should be flushright to that column. For example,
  1639. -80 works well for a normal 80 character screen."
  1640. :group 'org-tags
  1641. :type 'integer)
  1642. (defcustom org-auto-align-tags t
  1643. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1644. These operations change the length of a headline and therefore shift
  1645. the tags around. With this options turned on, after each such operation
  1646. the tags are again aligned to `org-tags-column'."
  1647. :group 'org-tags
  1648. :type 'boolean)
  1649. (defcustom org-use-tag-inheritance t
  1650. "Non-nil means, tags in levels apply also for sublevels.
  1651. When nil, only the tags directly given in a specific line apply there.
  1652. If you turn off this option, you very likely want to turn on the
  1653. companion option `org-tags-match-list-sublevels'."
  1654. :group 'org-tags
  1655. :type 'boolean)
  1656. (defcustom org-tags-match-list-sublevels nil
  1657. "Non-nil means list also sublevels of headlines matching tag search.
  1658. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1659. the sublevels of a headline matching a tag search often also match
  1660. the same search. Listing all of them can create very long lists.
  1661. Setting this variable to nil causes subtrees of a match to be skipped.
  1662. This option is off by default, because inheritance in on. If you turn
  1663. inheritance off, you very likely want to turn this option on.
  1664. As a special case, if the tag search is restricted to TODO items, the
  1665. value of this variable is ignored and sublevels are always checked, to
  1666. make sure all corresponding TODO items find their way into the list."
  1667. :group 'org-tags
  1668. :type 'boolean)
  1669. (defvar org-tags-history nil
  1670. "History of minibuffer reads for tags.")
  1671. (defvar org-last-tags-completion-table nil
  1672. "The last used completion table for tags.")
  1673. (defvar org-after-tags-change-hook nil
  1674. "Hook that is run after the tags in a line have changed.")
  1675. (defgroup org-properties nil
  1676. "Options concerning properties in Org-mode."
  1677. :tag "Org Properties"
  1678. :group 'org)
  1679. (defcustom org-property-format "%-10s %s"
  1680. "How property key/value pairs should be formatted by `indent-line'.
  1681. When `indent-line' hits a property definition, it will format the line
  1682. according to this format, mainly to make sure that the values are
  1683. lined-up with respect to each other."
  1684. :group 'org-properties
  1685. :type 'string)
  1686. (defcustom org-use-property-inheritance nil
  1687. "Non-nil means, properties apply also for sublevels.
  1688. This can cause significant overhead when doing a search, so this is turned
  1689. off by default.
  1690. When nil, only the properties directly given in the current entry count.
  1691. However, note that some special properties use inheritance under special
  1692. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1693. and the properties ending in \"_ALL\" when they are used as descriptor
  1694. for valid values of a property."
  1695. :group 'org-properties
  1696. :type 'boolean)
  1697. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1698. "The default column format, if no other format has been defined.
  1699. This variable can be set on the per-file basis by inserting a line
  1700. #+COLUMNS: %25ITEM ....."
  1701. :group 'org-properties
  1702. :type 'string)
  1703. (defcustom org-global-properties nil
  1704. "List of property/value pairs that can be inherited by any entry.
  1705. You can set buffer-local values for this by adding lines like
  1706. #+PROPERTY: NAME VALUE"
  1707. :group 'org-properties
  1708. :type '(repeat
  1709. (cons (string :tag "Property")
  1710. (string :tag "Value"))))
  1711. (defvar org-local-properties nil
  1712. "List of property/value pairs that can be inherited by any entry.
  1713. Valid for the current buffer.
  1714. This variable is populated from #+PROPERTY lines.")
  1715. (defgroup org-agenda nil
  1716. "Options concerning agenda views in Org-mode."
  1717. :tag "Org Agenda"
  1718. :group 'org)
  1719. (defvar org-category nil
  1720. "Variable used by org files to set a category for agenda display.
  1721. Such files should use a file variable to set it, for example
  1722. # -*- mode: org; org-category: \"ELisp\"
  1723. or contain a special line
  1724. #+CATEGORY: ELisp
  1725. If the file does not specify a category, then file's base name
  1726. is used instead.")
  1727. (make-variable-buffer-local 'org-category)
  1728. (defcustom org-agenda-files nil
  1729. "The files to be used for agenda display.
  1730. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1731. \\[org-remove-file]. You can also use customize to edit the list.
  1732. If the value of the variable is not a list but a single file name, then
  1733. the list of agenda files is actually stored and maintained in that file, one
  1734. agenda file per line."
  1735. :group 'org-agenda
  1736. :type '(choice
  1737. (repeat :tag "List of files" file)
  1738. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1739. (defcustom org-agenda-skip-unavailable-files nil
  1740. "t means to just skip non-reachable files in `org-agenda-files'.
  1741. Nil means to remove them, after a query, from the list."
  1742. :group 'org-agenda
  1743. :type 'boolean)
  1744. (defcustom org-agenda-multi-occur-extra-files nil
  1745. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1746. The files in `org-agenda-files' are always searched."
  1747. :group 'org-agenda
  1748. :type '(repeat file))
  1749. (defcustom org-agenda-confirm-kill 1
  1750. "When set, remote killing from the agenda buffer needs confirmation.
  1751. When t, a confirmation is always needed. When a number N, confirmation is
  1752. only needed when the text to be killed contains more than N non-white lines."
  1753. :group 'org-agenda
  1754. :type '(choice
  1755. (const :tag "Never" nil)
  1756. (const :tag "Always" t)
  1757. (number :tag "When more than N lines")))
  1758. (defcustom org-calendar-to-agenda-key [?c]
  1759. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1760. The command `org-calendar-goto-agenda' will be bound to this key. The
  1761. default is the character `c' because then `c' can be used to switch back and
  1762. forth between agenda and calendar."
  1763. :group 'org-agenda
  1764. :type 'sexp)
  1765. (defcustom org-agenda-compact-blocks nil
  1766. "Non-nil means, make the block agenda more compact.
  1767. This is done by leaving out unnecessary lines."
  1768. :group 'org-agenda
  1769. :type nil)
  1770. (defgroup org-agenda-export nil
  1771. "Options concerning exporting agenda views in Org-mode."
  1772. :tag "Org Agenda Export"
  1773. :group 'org-agenda)
  1774. (defcustom org-agenda-with-colors t
  1775. "Non-nil means, use colors in agenda views."
  1776. :group 'org-agenda-export
  1777. :type 'boolean)
  1778. (defcustom org-agenda-exporter-settings nil
  1779. "Alist of variable/value pairs that should be active during agenda export.
  1780. This is a good place to set uptions for ps-print and for htmlize."
  1781. :group 'org-agenda-export
  1782. :type '(repeat
  1783. (list
  1784. (variable)
  1785. (sexp :tag "Value"))))
  1786. (defcustom org-agenda-export-html-style ""
  1787. "The style specification for exported HTML Agenda files.
  1788. If this variable contains a string, it will replace the default <style>
  1789. section as produced by `htmlize'.
  1790. Since there are different ways of setting style information, this variable
  1791. needs to contain the full HTML structure to provide a style, including the
  1792. surrounding HTML tags. The style specifications should include definitions
  1793. the fonts used by the agenda, here is an example:
  1794. <style type=\"text/css\">
  1795. p { font-weight: normal; color: gray; }
  1796. .org-agenda-structure {
  1797. font-size: 110%;
  1798. color: #003399;
  1799. font-weight: 600;
  1800. }
  1801. .org-todo {
  1802. color: #cc6666;Week-agenda:
  1803. font-weight: bold;
  1804. }
  1805. .org-done {
  1806. color: #339933;
  1807. }
  1808. .title { text-align: center; }
  1809. .todo, .deadline { color: red; }
  1810. .done { color: green; }
  1811. </style>
  1812. or, if you want to keep the style in a file,
  1813. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1814. As the value of this option simply gets inserted into the HTML <head> header,
  1815. you can \"misuse\" it to also add other text to the header. However,
  1816. <style>...</style> is required, if not present the variable will be ignored."
  1817. :group 'org-agenda-export
  1818. :group 'org-export-html
  1819. :type 'string)
  1820. (defgroup org-agenda-custom-commands nil
  1821. "Options concerning agenda views in Org-mode."
  1822. :tag "Org Agenda Custom Commands"
  1823. :group 'org-agenda)
  1824. (defcustom org-agenda-custom-commands nil
  1825. "Custom commands for the agenda.
  1826. These commands will be offered on the splash screen displayed by the
  1827. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1828. (key type match options files)
  1829. key The key (a single char as a string) to be associated with the command.
  1830. type The command type, any of the following symbols:
  1831. todo Entries with a specific TODO keyword, in all agenda files.
  1832. tags Tags match in all agenda files.
  1833. tags-todo Tags match in all agenda files, TODO entries only.
  1834. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1835. tags-tree Sparse tree with all tags matches in *current* file.
  1836. occur-tree Occur sparse tree for *current* file.
  1837. match What to search for:
  1838. - a single keyword for TODO keyword searches
  1839. - a tags match expression for tags searches
  1840. - a regular expression for occur searches
  1841. options A list of option settings, similar to that in a let form, so like
  1842. this: ((opt1 val1) (opt2 val2) ...)
  1843. files A list of files file to write the produced agenda buffer to
  1844. with the command `org-store-agenda-views'.
  1845. If a file name ends in \".html\", an HTML version of the buffer
  1846. is written out. If it ends in \".ps\", a postscript version is
  1847. produced. Otherwide, only the plain text is written to the file.
  1848. You can also define a set of commands, to create a composite agenda buffer.
  1849. In this case, an entry looks like this:
  1850. (key desc (cmd1 cmd2 ...) general-options file)
  1851. where
  1852. desc A description string to be displayed in the dispatcher menu.
  1853. cmd An agenda command, similar to the above. However, tree commands
  1854. are no allowed, but instead you can get agenda and global todo list.
  1855. So valid commands for a set are:
  1856. (agenda)
  1857. (alltodo)
  1858. (stuck)
  1859. (todo \"match\" options files)
  1860. (tags \"match\" options files)
  1861. (tags-todo \"match\" options files)
  1862. Each command can carry a list of options, and another set of options can be
  1863. given for the whole set of commands. Individual command options take
  1864. precedence over the general options."
  1865. :group 'org-agenda-custom-commands
  1866. :type '(repeat
  1867. (choice :value ("a" tags "" nil)
  1868. (list :tag "Single command"
  1869. (string :tag "Key")
  1870. (choice
  1871. (const :tag "Agenda" agenda)
  1872. (const :tag "TODO list" alltodo)
  1873. (const :tag "Stuck projects" stuck)
  1874. (const :tag "Tags search (all agenda files)" tags)
  1875. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1876. (const :tag "TODO keyword search (all agenda files)" todo)
  1877. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1878. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1879. (const :tag "Occur tree (current buffer)" occur-tree)
  1880. (symbol :tag "Other, user-defined function"))
  1881. (string :tag "Match")
  1882. (repeat :tag "Local options"
  1883. (list (variable :tag "Option") (sexp :tag "Value")))
  1884. (option (repeat :tag "Export" (file :tag "Export to"))))
  1885. (list :tag "Command series, all agenda files"
  1886. (string :tag "Key")
  1887. (string :tag "Description")
  1888. (repeat
  1889. (choice
  1890. (const :tag "Agenda" (agenda))
  1891. (const :tag "TODO list" (alltodo))
  1892. (const :tag "Stuck projects" (stuck))
  1893. (list :tag "Tags search"
  1894. (const :format "" tags)
  1895. (string :tag "Match")
  1896. (repeat :tag "Local options"
  1897. (list (variable :tag "Option")
  1898. (sexp :tag "Value"))))
  1899. (list :tag "Tags search, TODO entries only"
  1900. (const :format "" tags-todo)
  1901. (string :tag "Match")
  1902. (repeat :tag "Local options"
  1903. (list (variable :tag "Option")
  1904. (sexp :tag "Value"))))
  1905. (list :tag "TODO keyword search"
  1906. (const :format "" todo)
  1907. (string :tag "Match")
  1908. (repeat :tag "Local options"
  1909. (list (variable :tag "Option")
  1910. (sexp :tag "Value"))))
  1911. (list :tag "Other, user-defined function"
  1912. (symbol :tag "function")
  1913. (string :tag "Match")
  1914. (repeat :tag "Local options"
  1915. (list (variable :tag "Option")
  1916. (sexp :tag "Value"))))))
  1917. (repeat :tag "General options"
  1918. (list (variable :tag "Option")
  1919. (sexp :tag "Value")))
  1920. (option (repeat :tag "Export" (file :tag "Export to")))))))
  1921. (defcustom org-stuck-projects
  1922. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  1923. "How to identify stuck projects.
  1924. This is a list of four items:
  1925. 1. A tags/todo matcher string that is used to identify a project.
  1926. The entire tree below a headline matched by this is considered one project.
  1927. 2. A list of TODO keywords identifying non-stuck projects.
  1928. If the project subtree contains any headline with one of these todo
  1929. keywords, the project is considered to be not stuck. If you specify
  1930. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  1931. 3. A list of tags identifying non-stuck projects.
  1932. If the project subtree contains any headline with one of these tags,
  1933. the project is considered to be not stuck. If you specify \"*\" as
  1934. a tag, any tag will mark the project unstuck.
  1935. 4. An arbitrary regular expression matching non-stuck projects.
  1936. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  1937. or `C-c a #' to produce the list."
  1938. :group 'org-agenda-custom-commands
  1939. :type '(list
  1940. (string :tag "Tags/TODO match to identify a project")
  1941. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  1942. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  1943. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  1944. (defgroup org-agenda-skip nil
  1945. "Options concerning skipping parts of agenda files."
  1946. :tag "Org Agenda Skip"
  1947. :group 'org-agenda)
  1948. (defcustom org-agenda-todo-list-sublevels t
  1949. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1950. When nil, the sublevels of a TODO entry are not checked, resulting in
  1951. potentially much shorter TODO lists."
  1952. :group 'org-agenda-skip
  1953. :group 'org-todo
  1954. :type 'boolean)
  1955. (defcustom org-agenda-todo-ignore-scheduled nil
  1956. "Non-nil means, don't show scheduled entries in the global todo list.
  1957. The idea behind this is that by scheduling it, you have already taken care
  1958. of this item."
  1959. :group 'org-agenda-skip
  1960. :group 'org-todo
  1961. :type 'boolean)
  1962. (defcustom org-agenda-todo-ignore-deadlines nil
  1963. "Non-nil means, don't show near deadline entries in the global todo list.
  1964. Near means closer than `org-deadline-warning-days' days.
  1965. The idea behind this is that such items will appear in the agenda anyway."
  1966. :group 'org-agenda-skip
  1967. :group 'org-todo
  1968. :type 'boolean)
  1969. (defcustom org-agenda-skip-scheduled-if-done nil
  1970. "Non-nil means don't show scheduled items in agenda when they are done.
  1971. This is relevant for the daily/weekly agenda, not for the TODO list. And
  1972. it applies only to the actual date of the scheduling. Warnings about
  1973. an item with a past scheduling dates are always turned off when the item
  1974. is DONE."
  1975. :group 'org-agenda-skip
  1976. :type 'boolean)
  1977. (defcustom org-agenda-skip-deadline-if-done nil
  1978. "Non-nil means don't show deadines when the corresponding item is done.
  1979. When nil, the deadline is still shown and should give you a happy feeling.
  1980. This is relevant for the daily/weekly agenda. And it applied only to the
  1981. actualy date of the deadline. Warnings about approching and past-due
  1982. deadlines are always turned off when the item is DONE."
  1983. :group 'org-agenda-skip
  1984. :type 'boolean)
  1985. (defcustom org-timeline-show-empty-dates 3
  1986. "Non-nil means, `org-timeline' also shows dates without an entry.
  1987. When nil, only the days which actually have entries are shown.
  1988. When t, all days between the first and the last date are shown.
  1989. When an integer, show also empty dates, but if there is a gap of more than
  1990. N days, just insert a special line indicating the size of the gap."
  1991. :group 'org-agenda-skip
  1992. :type '(choice
  1993. (const :tag "None" nil)
  1994. (const :tag "All" t)
  1995. (number :tag "at most")))
  1996. (defgroup org-agenda-startup nil
  1997. "Options concerning initial settings in the Agenda in Org Mode."
  1998. :tag "Org Agenda Startup"
  1999. :group 'org-agenda)
  2000. (defcustom org-finalize-agenda-hook nil
  2001. "Hook run just before displaying an agenda buffer."
  2002. :group 'org-agenda-startup
  2003. :type 'hook)
  2004. (defcustom org-agenda-mouse-1-follows-link nil
  2005. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2006. A longer mouse click will still set point. Does not wortk on XEmacs.
  2007. Needs to be set before org.el is loaded."
  2008. :group 'org-agenda-startup
  2009. :type 'boolean)
  2010. (defcustom org-agenda-start-with-follow-mode nil
  2011. "The initial value of follow-mode in a newly created agenda window."
  2012. :group 'org-agenda-startup
  2013. :type 'boolean)
  2014. (defgroup org-agenda-windows nil
  2015. "Options concerning the windows used by the Agenda in Org Mode."
  2016. :tag "Org Agenda Windows"
  2017. :group 'org-agenda)
  2018. (defcustom org-agenda-window-setup 'reorganize-frame
  2019. "How the agenda buffer should be displayed.
  2020. Possible values for this option are:
  2021. current-window Show agenda in the current window, keeping all other windows.
  2022. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2023. other-window Use `switch-to-buffer-other-window' to display agenda.
  2024. reorganize-frame Show only two windows on the current frame, the current
  2025. window and the agenda.
  2026. See also the variable `org-agenda-restore-windows-after-quit'."
  2027. :group 'org-agenda-windows
  2028. :type '(choice
  2029. (const current-window)
  2030. (const other-frame)
  2031. (const other-window)
  2032. (const reorganize-frame)))
  2033. (defcustom org-agenda-restore-windows-after-quit nil
  2034. "Non-nil means, restore window configuration open exiting agenda.
  2035. Before the window configuration is changed for displaying the agenda,
  2036. the current status is recorded. When the agenda is exited with
  2037. `q' or `x' and this option is set, the old state is restored. If
  2038. `org-agenda-window-setup' is `other-frame', the value of this
  2039. option will be ignored.."
  2040. :group 'org-agenda-windows
  2041. :type 'boolean)
  2042. (defcustom org-indirect-buffer-display 'other-window
  2043. "How should indirect tree buffers be displayed?
  2044. This applies to indirect buffers created with the commands
  2045. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2046. Valid values are:
  2047. current-window Display in the current window
  2048. other-window Just display in another window.
  2049. dedicated-frame Create one new frame, and re-use it each time.
  2050. new-frame Make a new frame each time."
  2051. :group 'org-structure
  2052. :group 'org-agenda-windows
  2053. :type '(choice
  2054. (const :tag "In current window" current-window)
  2055. (const :tag "In current frame, other window" other-window)
  2056. (const :tag "Each time a new frame" new-frame)
  2057. (const :tag "One dedicated frame" dedicated-frame)))
  2058. (defgroup org-agenda-daily/weekly nil
  2059. "Options concerning the daily/weekly agenda."
  2060. :tag "Org Agenda Daily/Weekly"
  2061. :group 'org-agenda)
  2062. (defcustom org-agenda-ndays 7
  2063. "Number of days to include in overview display.
  2064. Should be 1 or 7."
  2065. :group 'org-agenda-daily/weekly
  2066. :type 'number)
  2067. (defcustom org-agenda-start-on-weekday 1
  2068. "Non-nil means, start the overview always on the specified weekday.
  2069. 0 denotes Sunday, 1 denotes Monday etc.
  2070. When nil, always start on the current day."
  2071. :group 'org-agenda-daily/weekly
  2072. :type '(choice (const :tag "Today" nil)
  2073. (number :tag "Weekday No.")))
  2074. (defcustom org-agenda-show-all-dates t
  2075. "Non-nil means, `org-agenda' shows every day in the selected range.
  2076. When nil, only the days which actually have entries are shown."
  2077. :group 'org-agenda-daily/weekly
  2078. :type 'boolean)
  2079. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2080. "Format string for displaying dates in the agenda.
  2081. Used by the daily/weekly agenda and by the timeline. This should be
  2082. a format string understood by `format-time-string', or a function returning
  2083. the formatted date as a string. The function must take a single argument,
  2084. a calendar-style date list like (month day year)."
  2085. :group 'org-agenda-daily/weekly
  2086. :type '(choice
  2087. (string :tag "Format string")
  2088. (function :tag "Function")))
  2089. (defun org-agenda-format-date-aligned (date)
  2090. "Format a date string for display in the daily/weekly agenda, or timeline.
  2091. This function makes sure that dates are aligned for easy reading."
  2092. (format "%-9s %2d %s %4d"
  2093. (calendar-day-name date)
  2094. (extract-calendar-day date)
  2095. (calendar-month-name (extract-calendar-month date))
  2096. (extract-calendar-year date)))
  2097. (defcustom org-agenda-include-diary nil
  2098. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2099. :group 'org-agenda-daily/weekly
  2100. :type 'boolean)
  2101. (defcustom org-agenda-include-all-todo nil
  2102. "Set means weekly/daily agenda will always contain all TODO entries.
  2103. The TODO entries will be listed at the top of the agenda, before
  2104. the entries for specific days."
  2105. :group 'org-agenda-daily/weekly
  2106. :type 'boolean)
  2107. (defcustom org-agenda-repeating-timestamp-show-all t
  2108. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2109. When nil, only one occurence is shown, either today or the
  2110. nearest into the future."
  2111. :group 'org-agenda-daily/weekly
  2112. :type 'boolean)
  2113. (defcustom org-deadline-warning-days 14
  2114. "No. of days before expiration during which a deadline becomes active.
  2115. This variable governs the display in sparse trees and in the agenda.
  2116. When negative, it means use this number (the absolute value of it)
  2117. even if a deadline has a different individual lead time specified."
  2118. :group 'org-time
  2119. :group 'org-agenda-daily/weekly
  2120. :type 'number)
  2121. (defcustom org-scheduled-past-days 10000
  2122. "No. of days to continue listing scheduled items that are not marked DONE.
  2123. When an item is scheduled on a date, it shows up in the agenda on this
  2124. day and will be listed until it is marked done for the number of days
  2125. given here."
  2126. :group 'org-agenda-daily/weekly
  2127. :type 'number)
  2128. (defgroup org-agenda-time-grid nil
  2129. "Options concerning the time grid in the Org-mode Agenda."
  2130. :tag "Org Agenda Time Grid"
  2131. :group 'org-agenda)
  2132. (defcustom org-agenda-use-time-grid t
  2133. "Non-nil means, show a time grid in the agenda schedule.
  2134. A time grid is a set of lines for specific times (like every two hours between
  2135. 8:00 and 20:00). The items scheduled for a day at specific times are
  2136. sorted in between these lines.
  2137. For details about when the grid will be shown, and what it will look like, see
  2138. the variable `org-agenda-time-grid'."
  2139. :group 'org-agenda-time-grid
  2140. :type 'boolean)
  2141. (defcustom org-agenda-time-grid
  2142. '((daily today require-timed)
  2143. "----------------"
  2144. (800 1000 1200 1400 1600 1800 2000))
  2145. "The settings for time grid for agenda display.
  2146. This is a list of three items. The first item is again a list. It contains
  2147. symbols specifying conditions when the grid should be displayed:
  2148. daily if the agenda shows a single day
  2149. weekly if the agenda shows an entire week
  2150. today show grid on current date, independent of daily/weekly display
  2151. require-timed show grid only if at least one item has a time specification
  2152. The second item is a string which will be places behing the grid time.
  2153. The third item is a list of integers, indicating the times that should have
  2154. a grid line."
  2155. :group 'org-agenda-time-grid
  2156. :type
  2157. '(list
  2158. (set :greedy t :tag "Grid Display Options"
  2159. (const :tag "Show grid in single day agenda display" daily)
  2160. (const :tag "Show grid in weekly agenda display" weekly)
  2161. (const :tag "Always show grid for today" today)
  2162. (const :tag "Show grid only if any timed entries are present"
  2163. require-timed)
  2164. (const :tag "Skip grid times already present in an entry"
  2165. remove-match))
  2166. (string :tag "Grid String")
  2167. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2168. (defgroup org-agenda-sorting nil
  2169. "Options concerning sorting in the Org-mode Agenda."
  2170. :tag "Org Agenda Sorting"
  2171. :group 'org-agenda)
  2172. (let ((sorting-choice
  2173. '(choice
  2174. (const time-up) (const time-down)
  2175. (const category-keep) (const category-up) (const category-down)
  2176. (const tag-down) (const tag-up)
  2177. (const priority-up) (const priority-down))))
  2178. (defcustom org-agenda-sorting-strategy
  2179. '((agenda time-up category-keep priority-down)
  2180. (todo category-keep priority-down)
  2181. (tags category-keep priority-down))
  2182. "Sorting structure for the agenda items of a single day.
  2183. This is a list of symbols which will be used in sequence to determine
  2184. if an entry should be listed before another entry. The following
  2185. symbols are recognized:
  2186. time-up Put entries with time-of-day indications first, early first
  2187. time-down Put entries with time-of-day indications first, late first
  2188. category-keep Keep the default order of categories, corresponding to the
  2189. sequence in `org-agenda-files'.
  2190. category-up Sort alphabetically by category, A-Z.
  2191. category-down Sort alphabetically by category, Z-A.
  2192. tag-up Sort alphabetically by last tag, A-Z.
  2193. tag-down Sort alphabetically by last tag, Z-A.
  2194. priority-up Sort numerically by priority, high priority last.
  2195. priority-down Sort numerically by priority, high priority first.
  2196. The different possibilities will be tried in sequence, and testing stops
  2197. if one comparison returns a \"not-equal\". For example, the default
  2198. '(time-up category-keep priority-down)
  2199. means: Pull out all entries having a specified time of day and sort them,
  2200. in order to make a time schedule for the current day the first thing in the
  2201. agenda listing for the day. Of the entries without a time indication, keep
  2202. the grouped in categories, don't sort the categories, but keep them in
  2203. the sequence given in `org-agenda-files'. Within each category sort by
  2204. priority.
  2205. Leaving out `category-keep' would mean that items will be sorted across
  2206. categories by priority."
  2207. :group 'org-agenda-sorting
  2208. :type `(choice
  2209. (repeat :tag "General" ,sorting-choice)
  2210. (list :tag "Individually"
  2211. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2212. (repeat ,sorting-choice))
  2213. (cons (const :tag "Strategy for TODO lists" todo)
  2214. (repeat ,sorting-choice))
  2215. (cons (const :tag "Strategy for Tags matches" tags)
  2216. (repeat ,sorting-choice))))))
  2217. (defcustom org-sort-agenda-notime-is-late t
  2218. "Non-nil means, items without time are considered late.
  2219. This is only relevant for sorting. When t, items which have no explicit
  2220. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2221. do have a time. When nil, the default time is before 0:00. You can use this
  2222. option to decide if the schedule for today should come before or after timeless
  2223. agenda entries."
  2224. :group 'org-agenda-sorting
  2225. :type 'boolean)
  2226. (defgroup org-agenda-line-format nil
  2227. "Options concerning the entry prefix in the Org-mode agenda display."
  2228. :tag "Org Agenda Line Format"
  2229. :group 'org-agenda)
  2230. (defcustom org-agenda-prefix-format
  2231. '((agenda . " %-12:c%?-12t% s")
  2232. (timeline . " % s")
  2233. (todo . " %-12:c")
  2234. (tags . " %-12:c"))
  2235. "Format specifications for the prefix of items in the agenda views.
  2236. An alist with four entries, for the different agenda types. The keys to the
  2237. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2238. are format strings.
  2239. This format works similar to a printf format, with the following meaning:
  2240. %c the category of the item, \"Diary\" for entries from the diary, or
  2241. as given by the CATEGORY keyword or derived from the file name.
  2242. %T the *last* tag of the item. Last because inherited tags come
  2243. first in the list.
  2244. %t the time-of-day specification if one applies to the entry, in the
  2245. format HH:MM
  2246. %s Scheduling/Deadline information, a short string
  2247. All specifiers work basically like the standard `%s' of printf, but may
  2248. contain two additional characters: A question mark just after the `%' and
  2249. a whitespace/punctuation character just before the final letter.
  2250. If the first character after `%' is a question mark, the entire field
  2251. will only be included if the corresponding value applies to the
  2252. current entry. This is useful for fields which should have fixed
  2253. width when present, but zero width when absent. For example,
  2254. \"%?-12t\" will result in a 12 character time field if a time of the
  2255. day is specified, but will completely disappear in entries which do
  2256. not contain a time.
  2257. If there is punctuation or whitespace character just before the final
  2258. format letter, this character will be appended to the field value if
  2259. the value is not empty. For example, the format \"%-12:c\" leads to
  2260. \"Diary: \" if the category is \"Diary\". If the category were be
  2261. empty, no additional colon would be interted.
  2262. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2263. - Indent the line with two space characters
  2264. - Give the category in a 12 chars wide field, padded with whitespace on
  2265. the right (because of `-'). Append a colon if there is a category
  2266. (because of `:').
  2267. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2268. time, don't put in an empty field, just skip it (because of '?').
  2269. - Finally, put the scheduling information and append a whitespace.
  2270. As another example, if you don't want the time-of-day of entries in
  2271. the prefix, you could use:
  2272. (setq org-agenda-prefix-format \" %-11:c% s\")
  2273. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2274. `org-agenda-remove-tags'."
  2275. :type '(choice
  2276. (string :tag "General format")
  2277. (list :greedy t :tag "View dependent"
  2278. (cons (const agenda) (string :tag "Format"))
  2279. (cons (const timeline) (string :tag "Format"))
  2280. (cons (const todo) (string :tag "Format"))
  2281. (cons (const tags) (string :tag "Format"))))
  2282. :group 'org-agenda-line-format)
  2283. (defvar org-prefix-format-compiled nil
  2284. "The compiled version of the most recently used prefix format.
  2285. See the variable `org-agenda-prefix-format'.")
  2286. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2287. "Text preceeding scheduled items in the agenda view.
  2288. THis is a list with two strings. The first applies when the item is
  2289. scheduled on the current day. The second applies when it has been scheduled
  2290. previously, it may contain a %d to capture how many days ago the item was
  2291. scheduled."
  2292. :group 'org-agenda-line-format
  2293. :type '(list
  2294. (string :tag "Scheduled today ")
  2295. (string :tag "Scheduled previously")))
  2296. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2297. "Text preceeding deadline items in the agenda view.
  2298. This is a list with two strings. The first applies when the item has its
  2299. deadline on the current day. The second applies when it is in the past or
  2300. in the future, it may contain %d to capture how many days away the deadline
  2301. is (was)."
  2302. :group 'org-agenda-line-format
  2303. :type '(list
  2304. (string :tag "Deadline today ")
  2305. (string :tag "Deadline relative")))
  2306. (defcustom org-agenda-remove-times-when-in-prefix t
  2307. "Non-nil means, remove duplicate time specifications in agenda items.
  2308. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2309. time-of-day specification in a headline or diary entry is extracted and
  2310. placed into the prefix. If this option is non-nil, the original specification
  2311. \(a timestamp or -range, or just a plain time(range) specification like
  2312. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2313. cluttered.
  2314. The option can be t or nil. It may also be the symbol `beg', indicating
  2315. that the time should only be removed what it is located at the beginning of
  2316. the headline/diary entry."
  2317. :group 'org-agenda-line-format
  2318. :type '(choice
  2319. (const :tag "Always" t)
  2320. (const :tag "Never" nil)
  2321. (const :tag "When at beginning of entry" beg)))
  2322. (defcustom org-agenda-default-appointment-duration nil
  2323. "Default duration for appointments that only have a starting time.
  2324. When nil, no duration is specified in such cases.
  2325. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2326. :group 'org-agenda-line-format
  2327. :type '(choice
  2328. (integer :tag "Minutes")
  2329. (const :tag "No default duration")))
  2330. (defcustom org-agenda-remove-tags nil
  2331. "Non-nil means, remove the tags from the headline copy in the agenda.
  2332. When this is the symbol `prefix', only remove tags when
  2333. `org-agenda-prefix-format' contains a `%T' specifier."
  2334. :group 'org-agenda-line-format
  2335. :type '(choice
  2336. (const :tag "Always" t)
  2337. (const :tag "Never" nil)
  2338. (const :tag "When prefix format contains %T" prefix)))
  2339. (if (fboundp 'defvaralias)
  2340. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2341. 'org-agenda-remove-tags))
  2342. (defcustom org-agenda-tags-column -80
  2343. "Shift tags in agenda items to this column.
  2344. If this number is positive, it specifies the column. If it is negative,
  2345. it means that the tags should be flushright to that column. For example,
  2346. -80 works well for a normal 80 character screen."
  2347. :group 'org-agenda-line-format
  2348. :type 'integer)
  2349. (if (fboundp 'defvaralias)
  2350. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2351. (defcustom org-agenda-fontify-priorities t
  2352. "Non-nil means, highlight low and high priorities in agenda.
  2353. The highest priority entries are bold, lowest priority italic."
  2354. :group 'org-agenda-line-format
  2355. :type 'boolean)
  2356. (defgroup org-latex nil
  2357. "Options for embedding LaTeX code into Org-mode"
  2358. :tag "Org LaTeX"
  2359. :group 'org)
  2360. (defcustom org-format-latex-options
  2361. '(:foreground default :background default :scale 1.0
  2362. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2363. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2364. "Options for creating images from LaTeX fragments.
  2365. This is a property list with the following properties:
  2366. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2367. `default' means use the forground of the default face.
  2368. :background the background color, or \"Transparent\".
  2369. `default' means use the background of the default face.
  2370. :scale a scaling factor for the size of the images
  2371. :html-foreground, :html-background, :html-scale
  2372. The same numbers for HTML export.
  2373. :matchers a list indicating which matchers should be used to
  2374. find LaTeX fragments. Valid members of this list are:
  2375. \"begin\" find environments
  2376. \"$\" find math expressions surrounded by $...$
  2377. \"$$\" find math expressions surrounded by $$....$$
  2378. \"\\(\" find math expressions surrounded by \\(...\\)
  2379. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2380. :group 'org-latex
  2381. :type 'plist)
  2382. (defcustom org-format-latex-header "\\documentclass{article}
  2383. \\usepackage{fullpage} % do not remove
  2384. \\usepackage{amssymb}
  2385. \\usepackage[usenames]{color}
  2386. \\usepackage{amsmath}
  2387. \\usepackage{latexsym}
  2388. \\usepackage[mathscr]{eucal}
  2389. \\pagestyle{empty} % do not remove"
  2390. "The document header used for processing LaTeX fragments."
  2391. :group 'org-latex
  2392. :type 'string)
  2393. (defgroup org-export nil
  2394. "Options for exporting org-listings."
  2395. :tag "Org Export"
  2396. :group 'org)
  2397. (defgroup org-export-general nil
  2398. "General options for exporting Org-mode files."
  2399. :tag "Org Export General"
  2400. :group 'org-export)
  2401. (defcustom org-export-publishing-directory "."
  2402. "Path to the location where exported files should be located.
  2403. This path may be relative to the directory where the Org-mode file lives.
  2404. The default is to put them into the same directory as the Org-mode file.
  2405. The variable may also be an alist with export types `:html', `:ascii',
  2406. `:ical', `:LaTeX', or `:xoxo' and the corresponding directories.
  2407. If a directory path is relative, it is interpreted relative to the
  2408. directory where the exported Org-mode files lives."
  2409. :group 'org-export-general
  2410. :type '(choice
  2411. (directory)
  2412. (repeat
  2413. (cons
  2414. (choice :tag "Type"
  2415. (const :html) (const :LaTeX)
  2416. (const :ascii) (const :ical) (const :xoxo))
  2417. (directory)))))
  2418. (defcustom org-export-language-setup
  2419. '(("en" "Author" "Date" "Table of Contents")
  2420. ("cs" "Autor" "Datum" "Obsah")
  2421. ("da" "Ophavsmand" "Dato" "Indhold")
  2422. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2423. ("es" "Autor" "Fecha" "\xcdndice")
  2424. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2425. ("it" "Autore" "Data" "Indice")
  2426. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2427. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2428. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2429. "Terms used in export text, translated to different languages.
  2430. Use the variable `org-export-default-language' to set the language,
  2431. or use the +OPTION lines for a per-file setting."
  2432. :group 'org-export-general
  2433. :type '(repeat
  2434. (list
  2435. (string :tag "HTML language tag")
  2436. (string :tag "Author")
  2437. (string :tag "Date")
  2438. (string :tag "Table of Contents"))))
  2439. (defcustom org-export-default-language "en"
  2440. "The default language of HTML export, as a string.
  2441. This should have an association in `org-export-language-setup'."
  2442. :group 'org-export-general
  2443. :type 'string)
  2444. (defcustom org-export-skip-text-before-1st-heading t
  2445. "Non-nil means, skip all text before the first headline when exporting.
  2446. When nil, that text is exported as well."
  2447. :group 'org-export-general
  2448. :type 'boolean)
  2449. (defcustom org-export-headline-levels 3
  2450. "The last level which is still exported as a headline.
  2451. Inferior levels will produce itemize lists when exported.
  2452. Note that a numeric prefix argument to an exporter function overrides
  2453. this setting.
  2454. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2455. :group 'org-export-general
  2456. :type 'number)
  2457. (defcustom org-export-with-section-numbers t
  2458. "Non-nil means, add section numbers to headlines when exporting.
  2459. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2460. :group 'org-export-general
  2461. :type 'boolean)
  2462. (defcustom org-export-with-toc t
  2463. "Non-nil means, create a table of contents in exported files.
  2464. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2465. When an integer, include levels up to N in the toc, this may then be
  2466. different from `org-export-headline-levels', but it will not be allowed
  2467. to be larger than the number of headline levels.
  2468. When nil, no table of contents is made.
  2469. Headlines which contain any TODO items will be marked with \"(*)\" in
  2470. ASCII export, and with red color in HTML output, if the option
  2471. `org-export-mark-todo-in-toc' is set.
  2472. In HTML output, the TOC will be clickable.
  2473. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2474. or \"toc:3\"."
  2475. :group 'org-export-general
  2476. :type '(choice
  2477. (const :tag "No Table of Contents" nil)
  2478. (const :tag "Full Table of Contents" t)
  2479. (integer :tag "TOC to level")))
  2480. (defcustom org-export-mark-todo-in-toc nil
  2481. "Non-nil means, mark TOC lines that contain any open TODO items."
  2482. :group 'org-export-general
  2483. :type 'boolean)
  2484. (defcustom org-export-preserve-breaks nil
  2485. "Non-nil means, preserve all line breaks when exporting.
  2486. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2487. export, line breaks will always be preserved, regardless of this variable.
  2488. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2489. :group 'org-export-general
  2490. :type 'boolean)
  2491. (defcustom org-export-with-archived-trees 'headline
  2492. "Whether subtrees with the ARCHIVE tag should be exported.
  2493. This can have three different values
  2494. nil Do not export, pretend this tree is not present
  2495. t Do export the entire tree
  2496. headline Only export the headline, but skip the tree below it."
  2497. :group 'org-export-general
  2498. :group 'org-archive
  2499. :type '(choice
  2500. (const :tag "not at all" nil)
  2501. (const :tag "headline only" 'headline)
  2502. (const :tag "entirely" t)))
  2503. (defcustom org-export-author-info t
  2504. "Non-nil means, insert author name and email into the exported file.
  2505. This option can also be set with the +OPTIONS line,
  2506. e.g. \"author-info:nil\"."
  2507. :group 'org-export-general
  2508. :type 'boolean)
  2509. (defcustom org-export-time-stamp-file t
  2510. "Non-nil means, insert a time stamp into the exported file.
  2511. The time stamp shows when the file was created.
  2512. This option can also be set with the +OPTIONS line,
  2513. e.g. \"timestamp:nil\"."
  2514. :group 'org-export-general
  2515. :type 'boolean)
  2516. (defcustom org-export-with-timestamps t
  2517. "If nil, do not export time stamps and associated keywords."
  2518. :group 'org-export-general
  2519. :type 'boolean)
  2520. (defcustom org-export-remove-timestamps-from-toc t
  2521. "If nil, remove timestamps from the table of contents entries."
  2522. :group 'org-export-general
  2523. :type 'boolean)
  2524. (defcustom org-export-with-tags 'not-in-toc
  2525. "If nil, do not export tags, just remove them from headlines.
  2526. If this is the symbol `not-in-toc', tags will be removed from table of
  2527. contents entries, but still be shown in the headlines of the document.
  2528. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2529. :group 'org-export-general
  2530. :type '(choice
  2531. (const :tag "Off" nil)
  2532. (const :tag "Not in TOC" not-in-toc)
  2533. (const :tag "On" t)))
  2534. (defcustom org-export-with-property-drawer nil
  2535. "Non-nil means, export property drawers.
  2536. When nil, these drawers are removed before export.
  2537. This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
  2538. :group 'org-export-general
  2539. :type 'boolean)
  2540. (defgroup org-export-translation nil
  2541. "Options for translating special ascii sequences for the export backends."
  2542. :tag "Org Export Translation"
  2543. :group 'org-export)
  2544. (defcustom org-export-with-emphasize t
  2545. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2546. If the export target supports emphasizing text, the word will be
  2547. typeset in bold, italic, or underlined, respectively. Works only for
  2548. single words, but you can say: I *really* *mean* *this*.
  2549. Not all export backends support this.
  2550. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2551. :group 'org-export-translation
  2552. :type 'boolean)
  2553. (defcustom org-export-with-footnotes t
  2554. "If nil, export [1] as a footnote marker.
  2555. Lines starting with [1] will be formatted as footnotes.
  2556. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2557. :group 'org-export-translation
  2558. :type 'boolean)
  2559. (defcustom org-export-with-sub-superscripts t
  2560. "Non-nil means, interpret \"_\" and \"^\" for export.
  2561. When this option is turned on, you can use TeX-like syntax for sub- and
  2562. superscripts. Several characters after \"_\" or \"^\" will be
  2563. considered as a single item - so grouping with {} is normally not
  2564. needed. For example, the following things will be parsed as single
  2565. sub- or superscripts.
  2566. 10^24 or 10^tau several digits will be considered 1 item.
  2567. 10^-12 or 10^-tau a leading sign with digits or a word
  2568. x^2-y^3 will be read as x^2 - y^3, because items are
  2569. terminated by almost any nonword/nondigit char.
  2570. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2571. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2572. sub/superscript. If you set this variable to the symbol `{}',
  2573. the braces are *required* in order to trigger interpretations as
  2574. sub/superscript. This can be helpful in documents that need \"_\"
  2575. frequently in plain text.
  2576. Not all export backends support this, but HTML does.
  2577. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2578. :group 'org-export-translation
  2579. :type '(choice
  2580. (const :tag "Always interpret" t)
  2581. (const :tag "Only with braces" {})
  2582. (const :tag "Never interpret" nil)))
  2583. (defcustom org-export-with-TeX-macros t
  2584. "Non-nil means, interpret simple TeX-like macros when exporting.
  2585. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2586. No only real TeX macros will work here, but the standard HTML entities
  2587. for math can be used as macro names as well. For a list of supported
  2588. names in HTML export, see the constant `org-html-entities'.
  2589. Not all export backends support this.
  2590. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2591. :group 'org-export-translation
  2592. :group 'org-export-latex
  2593. :type 'boolean)
  2594. (defcustom org-export-with-LaTeX-fragments nil
  2595. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2596. When set, the exporter will find LaTeX environments if the \\begin line is
  2597. the first non-white thing on a line. It will also find the math delimiters
  2598. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2599. display math.
  2600. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2601. :group 'org-export-translation
  2602. :group 'org-export-latex
  2603. :type 'boolean)
  2604. (defcustom org-export-with-fixed-width t
  2605. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2606. This can be used to have pre-formatted text, fragments of code etc. For
  2607. example:
  2608. : ;; Some Lisp examples
  2609. : (while (defc cnt)
  2610. : (ding))
  2611. will be looking just like this in also HTML. See also the QUOTE keyword.
  2612. Not all export backends support this.
  2613. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2614. :group 'org-export-translation
  2615. :type 'boolean)
  2616. (defcustom org-match-sexp-depth 3
  2617. "Number of stacked braces for sub/superscript matching.
  2618. This has to be set before loading org.el to be effective."
  2619. :group 'org-export-translation
  2620. :type 'integer)
  2621. (defgroup org-export-tables nil
  2622. "Options for exporting tables in Org-mode."
  2623. :tag "Org Export Tables"
  2624. :group 'org-export)
  2625. (defcustom org-export-with-tables t
  2626. "If non-nil, lines starting with \"|\" define a table.
  2627. For example:
  2628. | Name | Address | Birthday |
  2629. |-------------+----------+-----------|
  2630. | Arthur Dent | England | 29.2.2100 |
  2631. Not all export backends support this.
  2632. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2633. :group 'org-export-tables
  2634. :type 'boolean)
  2635. (defcustom org-export-highlight-first-table-line t
  2636. "Non-nil means, highlight the first table line.
  2637. In HTML export, this means use <th> instead of <td>.
  2638. In tables created with table.el, this applies to the first table line.
  2639. In Org-mode tables, all lines before the first horizontal separator
  2640. line will be formatted with <th> tags."
  2641. :group 'org-export-tables
  2642. :type 'boolean)
  2643. (defcustom org-export-table-remove-special-lines t
  2644. "Remove special lines and marking characters in calculating tables.
  2645. This removes the special marking character column from tables that are set
  2646. up for spreadsheet calculations. It also removes the entire lines
  2647. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2648. the values of constants may be useful to have."
  2649. :group 'org-export-tables
  2650. :type 'boolean)
  2651. (defcustom org-export-prefer-native-exporter-for-tables nil
  2652. "Non-nil means, always export tables created with table.el natively.
  2653. Natively means, use the HTML code generator in table.el.
  2654. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2655. the table does not use row- or column-spanning). This has the
  2656. advantage, that the automatic HTML conversions for math symbols and
  2657. sub/superscripts can be applied. Org-mode's HTML generator is also
  2658. much faster."
  2659. :group 'org-export-tables
  2660. :type 'boolean)
  2661. (defgroup org-export-ascii nil
  2662. "Options specific for ASCII export of Org-mode files."
  2663. :tag "Org Export ASCII"
  2664. :group 'org-export)
  2665. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2666. "Characters for underlining headings in ASCII export.
  2667. In the given sequence, these characters will be used for level 1, 2, ..."
  2668. :group 'org-export-ascii
  2669. :type '(repeat character))
  2670. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2671. "Bullet characters for headlines converted to lists in ASCII export.
  2672. The first character is is used for the first lest level generated in this
  2673. way, and so on. If there are more levels than characters given here,
  2674. the list will be repeated.
  2675. Note that plain lists will keep the same bullets as the have in the
  2676. Org-mode file."
  2677. :group 'org-export-ascii
  2678. :type '(repeat character))
  2679. (defgroup org-export-xml nil
  2680. "Options specific for XML export of Org-mode files."
  2681. :tag "Org Export XML"
  2682. :group 'org-export)
  2683. (defgroup org-export-html nil
  2684. "Options specific for HTML export of Org-mode files."
  2685. :tag "Org Export HTML"
  2686. :group 'org-export)
  2687. (defcustom org-export-html-coding-system nil
  2688. ""
  2689. :group 'org-export-html
  2690. :type 'coding-system)
  2691. (defcustom org-export-html-extension "html"
  2692. "The extension for exported HTML files."
  2693. :group 'org-export-html
  2694. :type 'string)
  2695. (defcustom org-export-html-style
  2696. "<style type=\"text/css\">
  2697. html {
  2698. font-family: Times, serif;
  2699. font-size: 12pt;
  2700. }
  2701. .title { text-align: center; }
  2702. .todo { color: red; }
  2703. .done { color: green; }
  2704. .timestamp { color: grey }
  2705. .timestamp-kwd { color: CadetBlue }
  2706. .tag { background-color:lightblue; font-weight:normal }
  2707. .target { background-color: lavender; }
  2708. pre {
  2709. border: 1pt solid #AEBDCC;
  2710. background-color: #F3F5F7;
  2711. padding: 5pt;
  2712. font-family: courier, monospace;
  2713. }
  2714. table { border-collapse: collapse; }
  2715. td, th {
  2716. vertical-align: top;
  2717. <!--border: 1pt solid #ADB9CC;-->
  2718. }
  2719. </style>"
  2720. "The default style specification for exported HTML files.
  2721. Since there are different ways of setting style information, this variable
  2722. needs to contain the full HTML structure to provide a style, including the
  2723. surrounding HTML tags. The style specifications should include definitions
  2724. for new classes todo, done, title, and deadline. For example, legal values
  2725. would be:
  2726. <style type=\"text/css\">
  2727. p { font-weight: normal; color: gray; }
  2728. h1 { color: black; }
  2729. .title { text-align: center; }
  2730. .todo, .deadline { color: red; }
  2731. .done { color: green; }
  2732. </style>
  2733. or, if you want to keep the style in a file,
  2734. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2735. As the value of this option simply gets inserted into the HTML <head> header,
  2736. you can \"misuse\" it to add arbitrary text to the header."
  2737. :group 'org-export-html
  2738. :type 'string)
  2739. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2740. "Format for typesetting the document title in HTML export."
  2741. :group 'org-export-html
  2742. :type 'string)
  2743. (defcustom org-export-html-toplevel-hlevel 2
  2744. "The <H> level for level 1 headings in HTML export."
  2745. :group 'org-export-html
  2746. :type 'string)
  2747. (defcustom org-export-html-link-org-files-as-html t
  2748. "Non-nil means, make file links to `file.org' point to `file.html'.
  2749. When org-mode is exporting an org-mode file to HTML, links to
  2750. non-html files are directly put into a href tag in HTML.
  2751. However, links to other Org-mode files (recognized by the
  2752. extension `.org.) should become links to the corresponding html
  2753. file, assuming that the linked org-mode file will also be
  2754. converted to HTML.
  2755. When nil, the links still point to the plain `.org' file."
  2756. :group 'org-export-html
  2757. :type 'boolean)
  2758. (defcustom org-export-html-inline-images 'maybe
  2759. "Non-nil means, inline images into exported HTML pages.
  2760. This is done using an <img> tag. When nil, an anchor with href is used to
  2761. link to the image. If this option is `maybe', then images in links with
  2762. an empty description will be inlined, while images with a description will
  2763. be linked only."
  2764. :group 'org-export-html
  2765. :type '(choice (const :tag "Never" nil)
  2766. (const :tag "Always" t)
  2767. (const :tag "When there is no description" maybe)))
  2768. ;; FIXME: rename
  2769. (defcustom org-export-html-expand t
  2770. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2771. When nil, these tags will be exported as plain text and therefore
  2772. not be interpreted by a browser.
  2773. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2774. :group 'org-export-html
  2775. :type 'boolean)
  2776. (defcustom org-export-html-table-tag
  2777. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2778. "The HTML tag that is used to start a table.
  2779. This must be a <table> tag, but you may change the options like
  2780. borders and spacing."
  2781. :group 'org-export-html
  2782. :type 'string)
  2783. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2784. "The opening tag for table header fields.
  2785. This is customizable so that alignment options can be specified."
  2786. :group 'org-export-tables
  2787. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2788. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2789. "The opening tag for table data fields.
  2790. This is customizable so that alignment options can be specified."
  2791. :group 'org-export-tables
  2792. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2793. (defcustom org-export-html-with-timestamp nil
  2794. "If non-nil, write `org-export-html-html-helper-timestamp'
  2795. into the exported HTML text. Otherwise, the buffer will just be saved
  2796. to a file."
  2797. :group 'org-export-html
  2798. :type 'boolean)
  2799. (defcustom org-export-html-html-helper-timestamp
  2800. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2801. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2802. :group 'org-export-html
  2803. :type 'string)
  2804. (defgroup org-export-icalendar nil
  2805. "Options specific for iCalendar export of Org-mode files."
  2806. :tag "Org Export iCalendar"
  2807. :group 'org-export)
  2808. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2809. "The file name for the iCalendar file covering all agenda files.
  2810. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2811. The file name should be absolute, the file will be overwritten without warning."
  2812. :group 'org-export-icalendar
  2813. :type 'file)
  2814. (defcustom org-icalendar-include-todo nil
  2815. "Non-nil means, export to iCalendar files should also cover TODO items."
  2816. :group 'org-export-icalendar
  2817. :type '(choice
  2818. (const :tag "None" nil)
  2819. (const :tag "Unfinished" t)
  2820. (const :tag "All" all)))
  2821. (defcustom org-icalendar-include-sexps t
  2822. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2823. These are entries like in the diary, but directly in an Org-mode file."
  2824. :group 'org-export-icalendar
  2825. :type 'boolean)
  2826. (defcustom org-icalendar-include-body 100
  2827. "Amount of text below headline to be included in iCalendar export.
  2828. This is a number of characters that should maximally be included.
  2829. Properties, scheduling and clocking lines will always be removed.
  2830. The text will be inserted into the DESCRIPTION field."
  2831. :group 'org-export-icalendar
  2832. :type '(choice
  2833. (const :tag "Nothing" nil)
  2834. (const :tag "Everything" t)
  2835. (integer :tag "Max characters")))
  2836. (defcustom org-icalendar-combined-name "OrgMode"
  2837. "Calendar name for the combined iCalendar representing all agenda files."
  2838. :group 'org-export-icalendar
  2839. :type 'string)
  2840. (defgroup org-font-lock nil
  2841. "Font-lock settings for highlighting in Org-mode."
  2842. :tag "Org Font Lock"
  2843. :group 'org)
  2844. (defcustom org-level-color-stars-only nil
  2845. "Non-nil means fontify only the stars in each headline.
  2846. When nil, the entire headline is fontified.
  2847. Changing it requires restart of `font-lock-mode' to become effective
  2848. also in regions already fontified."
  2849. :group 'org-font-lock
  2850. :type 'boolean)
  2851. (defcustom org-hide-leading-stars nil
  2852. "Non-nil means, hide the first N-1 stars in a headline.
  2853. This works by using the face `org-hide' for these stars. This
  2854. face is white for a light background, and black for a dark
  2855. background. You may have to customize the face `org-hide' to
  2856. make this work.
  2857. Changing it requires restart of `font-lock-mode' to become effective
  2858. also in regions already fontified.
  2859. You may also set this on a per-file basis by adding one of the following
  2860. lines to the buffer:
  2861. #+STARTUP: hidestars
  2862. #+STARTUP: showstars"
  2863. :group 'org-font-lock
  2864. :type 'boolean)
  2865. (defcustom org-fontify-done-headline nil
  2866. "Non-nil means, change the face of a headline if it is marked DONE.
  2867. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2868. When this is non-nil, the headline after the keyword is set to the
  2869. `org-headline-done' as an additional indication."
  2870. :group 'org-font-lock
  2871. :type 'boolean)
  2872. (defcustom org-fontify-emphasized-text t
  2873. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2874. Changing this variable requires a restart of Emacs to take effect."
  2875. :group 'org-font-lock
  2876. :type 'boolean)
  2877. (defvar org-emph-re nil
  2878. "Regular expression for matching emphasis.")
  2879. (defvar org-emphasis-regexp-components) ; defined just below
  2880. (defvar org-emphasis-alist) ; defined just below
  2881. (defun org-set-emph-re (var val)
  2882. "Set variable and compute the emphasis regular expression."
  2883. (set var val)
  2884. (when (and (boundp 'org-emphasis-alist)
  2885. (boundp 'org-emphasis-regexp-components)
  2886. org-emphasis-alist org-emphasis-regexp-components)
  2887. (let* ((e org-emphasis-regexp-components)
  2888. (pre (car e))
  2889. (post (nth 1 e))
  2890. (border (nth 2 e))
  2891. (body (nth 3 e))
  2892. (nl (nth 4 e))
  2893. (stacked (nth 5 e))
  2894. (body1 (concat body "*?"))
  2895. (markers (mapconcat 'car org-emphasis-alist "")))
  2896. ;; make sure special characters appear at the right position in the class
  2897. (if (string-match "\\^" markers)
  2898. (setq markers (concat (replace-match "" t t markers) "^")))
  2899. (if (string-match "-" markers)
  2900. (setq markers (concat (replace-match "" t t markers) "-")))
  2901. (if (> nl 0)
  2902. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2903. (int-to-string nl) "\\}")))
  2904. ;; Make the regexp
  2905. (setq org-emph-re
  2906. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2907. "\\("
  2908. "\\([" markers "]\\)"
  2909. "\\("
  2910. "[^" border (if (and nil stacked) markers) "]"
  2911. body1
  2912. "[^" border (if (and nil stacked) markers) "]"
  2913. "\\)"
  2914. "\\3\\)"
  2915. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2916. (defcustom org-emphasis-regexp-components
  2917. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil)
  2918. "Components used to build the reqular expression for emphasis.
  2919. This is a list with 6 entries. Terminology: In an emphasis string
  2920. like \" *strong word* \", we call the initial space PREMATCH, the final
  2921. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2922. and \"trong wor\" is the body. The different components in this variable
  2923. specify what is allowed/forbidden in each part:
  2924. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2925. post Chars allowed as postmatch. End of line will be allowed too.
  2926. border The chars *forbidden* as border characters.
  2927. body-regexp A regexp like \".\" to match a body character. Don't use
  2928. non-shy groups here, and don't allow newline here.
  2929. newline The maximum number of newlines allowed in an emphasis exp.
  2930. stacked Non-nil means, allow stacked styles. This works only in HTML
  2931. export. When this is set, all marker characters (as given in
  2932. `org-emphasis-alist') will be allowed as pre/post, aiding
  2933. inside-out matching.
  2934. Use customize to modify this, or restart Emacs after changing it."
  2935. :group 'org-font-lock
  2936. :set 'org-set-emph-re
  2937. :type '(list
  2938. (sexp :tag "Allowed chars in pre ")
  2939. (sexp :tag "Allowed chars in post ")
  2940. (sexp :tag "Forbidden chars in border ")
  2941. (sexp :tag "Regexp for body ")
  2942. (integer :tag "number of newlines allowed")
  2943. (boolean :tag "Stacking allowed ")))
  2944. (defcustom org-emphasis-alist
  2945. '(("*" bold "<b>" "</b>")
  2946. ("/" italic "<i>" "</i>")
  2947. ("_" underline "<u>" "</u>")
  2948. ("=" org-code "<code>" "</code>")
  2949. ("+" (:strike-through t) "<del>" "</del>")
  2950. )
  2951. "Special syntax for emphasized text.
  2952. Text starting and ending with a special character will be emphasized, for
  2953. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2954. characters, the face to be used by font-lock for highlighting in Org-mode
  2955. Emacs buffers, and the HTML tags to be used for this.
  2956. Use customize to modify this, or restart Emacs after changing it."
  2957. :group 'org-font-lock
  2958. :set 'org-set-emph-re
  2959. :type '(repeat
  2960. (list
  2961. (string :tag "Marker character")
  2962. (choice
  2963. (face :tag "Font-lock-face")
  2964. (plist :tag "Face property list"))
  2965. (string :tag "HTML start tag")
  2966. (string :tag "HTML end tag"))))
  2967. ;;; The faces
  2968. (defgroup org-faces nil
  2969. "Faces in Org-mode."
  2970. :tag "Org Faces"
  2971. :group 'org-font-lock)
  2972. (defun org-compatible-face (inherits specs)
  2973. "Make a compatible face specification.
  2974. If INHERITS is an existing face and if the Emacs version supports it,
  2975. just inherit the face. If not, use SPECS to define the face.
  2976. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2977. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2978. to the top of the list. The `min-colors' attribute will be removed from
  2979. any other entries, and any resulting duplicates will be removed entirely."
  2980. (cond
  2981. ((and inherits (facep inherits)
  2982. (not (featurep 'xemacs)) (> emacs-major-version 22))
  2983. ;; In Emacs 23, we use inheritance where possible.
  2984. ;; We only do this in Emacs 23, because only there the outline
  2985. ;; faces have been changed to the original org-mode-level-faces.
  2986. (list (list t :inherit inherits)))
  2987. ((or (featurep 'xemacs) (< emacs-major-version 22))
  2988. ;; These do not understand the `min-colors' attribute.
  2989. (let (r e a)
  2990. (while (setq e (pop specs))
  2991. (cond
  2992. ((memq (car e) '(t default)) (push e r))
  2993. ((setq a (member '(min-colors 8) (car e)))
  2994. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2995. (cdr e)))))
  2996. ((setq a (assq 'min-colors (car e)))
  2997. (setq e (cons (delq a (car e)) (cdr e)))
  2998. (or (assoc (car e) r) (push e r)))
  2999. (t (or (assoc (car e) r) (push e r)))))
  3000. (nreverse r)))
  3001. (t specs)))
  3002. (defface org-hide
  3003. '((((background light)) (:foreground "white"))
  3004. (((background dark)) (:foreground "black")))
  3005. "Face used to hide leading stars in headlines.
  3006. The forground color of this face should be equal to the background
  3007. color of the frame."
  3008. :group 'org-faces)
  3009. (defface org-level-1 ;; font-lock-function-name-face
  3010. (org-compatible-face
  3011. 'outline-1
  3012. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3013. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3014. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3015. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3016. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3017. (t (:bold t))))
  3018. "Face used for level 1 headlines."
  3019. :group 'org-faces)
  3020. (defface org-level-2 ;; font-lock-variable-name-face
  3021. (org-compatible-face
  3022. 'outline-2
  3023. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3024. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3025. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3026. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3027. (t (:bold t))))
  3028. "Face used for level 2 headlines."
  3029. :group 'org-faces)
  3030. (defface org-level-3 ;; font-lock-keyword-face
  3031. (org-compatible-face
  3032. 'outline-3
  3033. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3034. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3035. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3036. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3037. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3038. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3039. (t (:bold t))))
  3040. "Face used for level 3 headlines."
  3041. :group 'org-faces)
  3042. (defface org-level-4 ;; font-lock-comment-face
  3043. (org-compatible-face
  3044. 'outline-4
  3045. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3046. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3047. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3048. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3049. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3050. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3051. (t (:bold t))))
  3052. "Face used for level 4 headlines."
  3053. :group 'org-faces)
  3054. (defface org-level-5 ;; font-lock-type-face
  3055. (org-compatible-face
  3056. 'outline-5
  3057. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3058. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3059. (((class color) (min-colors 8)) (:foreground "green"))))
  3060. "Face used for level 5 headlines."
  3061. :group 'org-faces)
  3062. (defface org-level-6 ;; font-lock-constant-face
  3063. (org-compatible-face
  3064. 'outline-6
  3065. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3066. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3067. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3068. "Face used for level 6 headlines."
  3069. :group 'org-faces)
  3070. (defface org-level-7 ;; font-lock-builtin-face
  3071. (org-compatible-face
  3072. 'outline-7
  3073. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3074. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3075. (((class color) (min-colors 8)) (:foreground "blue"))))
  3076. "Face used for level 7 headlines."
  3077. :group 'org-faces)
  3078. (defface org-level-8 ;; font-lock-string-face
  3079. (org-compatible-face
  3080. 'outline-8
  3081. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3082. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3083. (((class color) (min-colors 8)) (:foreground "green"))))
  3084. "Face used for level 8 headlines."
  3085. :group 'org-faces)
  3086. (defface org-special-keyword ;; font-lock-string-face
  3087. (org-compatible-face
  3088. nil
  3089. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3090. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3091. (t (:italic t))))
  3092. "Face used for special keywords."
  3093. :group 'org-faces)
  3094. (defface org-drawer ;; font-lock-function-name-face
  3095. (org-compatible-face
  3096. nil
  3097. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3098. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3099. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3100. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3101. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3102. (t (:bold t))))
  3103. "Face used for drawers."
  3104. :group 'org-faces)
  3105. (defface org-property-value nil
  3106. "Face used for the value of a property."
  3107. :group 'org-faces)
  3108. (defface org-column
  3109. (org-compatible-face
  3110. nil
  3111. '((((class color) (min-colors 16) (background light))
  3112. (:background "grey90"))
  3113. (((class color) (min-colors 16) (background dark))
  3114. (:background "grey30"))
  3115. (((class color) (min-colors 8))
  3116. (:background "cyan" :foreground "black"))
  3117. (t (:inverse-video t))))
  3118. "Face for column display of entry properties."
  3119. :group 'org-faces)
  3120. (when (fboundp 'set-face-attribute)
  3121. ;; Make sure that a fixed-width face is used when we have a column table.
  3122. (set-face-attribute 'org-column nil
  3123. :height (face-attribute 'default :height)
  3124. :family (face-attribute 'default :family)))
  3125. (defface org-warning
  3126. (org-compatible-face
  3127. 'font-lock-warning-face
  3128. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3129. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3130. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3131. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3132. (t (:bold t))))
  3133. "Face for deadlines and TODO keywords."
  3134. :group 'org-faces)
  3135. (defface org-archived ; similar to shadow
  3136. (org-compatible-face
  3137. 'shadow
  3138. '((((class color grayscale) (min-colors 88) (background light))
  3139. (:foreground "grey50"))
  3140. (((class color grayscale) (min-colors 88) (background dark))
  3141. (:foreground "grey70"))
  3142. (((class color) (min-colors 8) (background light))
  3143. (:foreground "green"))
  3144. (((class color) (min-colors 8) (background dark))
  3145. (:foreground "yellow"))))
  3146. "Face for headline with the ARCHIVE tag."
  3147. :group 'org-faces)
  3148. (defface org-link
  3149. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3150. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3151. (t (:underline t)))
  3152. "Face for links."
  3153. :group 'org-faces)
  3154. (defface org-target
  3155. '((((class color) (background light)) (:underline t))
  3156. (((class color) (background dark)) (:underline t))
  3157. (t (:underline t)))
  3158. "Face for links."
  3159. :group 'org-faces)
  3160. (defface org-date
  3161. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3162. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3163. (t (:underline t)))
  3164. "Face for links."
  3165. :group 'org-faces)
  3166. (defface org-sexp-date
  3167. '((((class color) (background light)) (:foreground "Purple"))
  3168. (((class color) (background dark)) (:foreground "Cyan"))
  3169. (t (:underline t)))
  3170. "Face for links."
  3171. :group 'org-faces)
  3172. (defface org-tag
  3173. '((t (:bold t)))
  3174. "Face for tags."
  3175. :group 'org-faces)
  3176. (defface org-todo ; font-lock-warning-face
  3177. (org-compatible-face
  3178. nil
  3179. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3180. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3181. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3182. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3183. (t (:inverse-video t :bold t))))
  3184. "Face for TODO keywords."
  3185. :group 'org-faces)
  3186. (defface org-done ;; font-lock-type-face
  3187. (org-compatible-face
  3188. nil
  3189. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3190. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3191. (((class color) (min-colors 8)) (:foreground "green"))
  3192. (t (:bold t))))
  3193. "Face used for todo keywords that indicate DONE items."
  3194. :group 'org-faces)
  3195. (defface org-headline-done ;; font-lock-string-face
  3196. (org-compatible-face
  3197. nil
  3198. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3199. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3200. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3201. "Face used to indicate that a headline is DONE.
  3202. This face is only used if `org-fontify-done-headline' is set. If applies
  3203. to the part of the headline after the DONE keyword."
  3204. :group 'org-faces)
  3205. (defcustom org-todo-keyword-faces nil
  3206. "Faces for specific TODO keywords.
  3207. This is a list of cons cells, with TODO keywords in the car
  3208. and faces in the cdr. The face can be a symbol, or a property
  3209. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3210. :group 'org-faces
  3211. :group 'org-todo
  3212. :type '(repeat
  3213. (cons
  3214. (string :tag "keyword")
  3215. (sexp :tag "face"))))
  3216. (defface org-table ;; font-lock-function-name-face
  3217. (org-compatible-face
  3218. nil
  3219. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3220. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3221. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3222. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3223. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3224. (((class color) (min-colors 8) (background dark)))))
  3225. "Face used for tables."
  3226. :group 'org-faces)
  3227. (defface org-formula
  3228. (org-compatible-face
  3229. nil
  3230. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3231. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3232. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3233. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3234. (t (:bold t :italic t))))
  3235. "Face for formulas."
  3236. :group 'org-faces)
  3237. (defface org-code
  3238. (org-compatible-face
  3239. nil
  3240. '((((class color grayscale) (min-colors 88) (background light))
  3241. (:foreground "grey50"))
  3242. (((class color grayscale) (min-colors 88) (background dark))
  3243. (:foreground "grey70"))
  3244. (((class color) (min-colors 8) (background light))
  3245. (:foreground "green"))
  3246. (((class color) (min-colors 8) (background dark))
  3247. (:foreground "yellow"))))
  3248. "Face for fixed-with text like code snippets."
  3249. :group 'org-faces
  3250. :version "22.1")
  3251. (defface org-agenda-structure ;; font-lock-function-name-face
  3252. (org-compatible-face
  3253. nil
  3254. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3255. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3256. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3257. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3258. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3259. (t (:bold t))))
  3260. "Face used in agenda for captions and dates."
  3261. :group 'org-faces)
  3262. (defface org-scheduled-today
  3263. (org-compatible-face
  3264. nil
  3265. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3266. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3267. (((class color) (min-colors 8)) (:foreground "green"))
  3268. (t (:bold t :italic t))))
  3269. "Face for items scheduled for a certain day."
  3270. :group 'org-faces)
  3271. (defface org-scheduled-previously
  3272. (org-compatible-face
  3273. nil
  3274. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3275. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3276. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3277. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3278. (t (:bold t))))
  3279. "Face for items scheduled previously, and not yet done."
  3280. :group 'org-faces)
  3281. (defface org-upcoming-deadline
  3282. (org-compatible-face
  3283. nil
  3284. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3285. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3286. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3287. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3288. (t (:bold t))))
  3289. "Face for items scheduled previously, and not yet done."
  3290. :group 'org-faces)
  3291. (defcustom org-agenda-deadline-faces
  3292. '((1.0 . org-warning)
  3293. (0.5 . org-upcoming-deadline)
  3294. (0.0 . default))
  3295. "Faces for showing deadlines in the agenda.
  3296. This is a list of cons cells. The cdr of each cess is a face to be used,
  3297. and it can also just be a like like '(:foreground \"yellow\").
  3298. Each car is a fraction of the head-warning time that must have passed for
  3299. this the face in the cdr to be used for display. The numbers must be
  3300. given in descending order. The head-warning time is normally taken
  3301. from `org-deadline-warning-days', but can also be specified in the deadline
  3302. timestamp itself, like this:
  3303. DEADLINE: <2007-08-13 Mon -8d>
  3304. You may use d for days, w for weeks, m for months and y for years. Months
  3305. and years will only be treated in an approximate fashion (30.4 days for a
  3306. month and 365.24 days for a year)."
  3307. :group 'org-faces
  3308. :group 'org-agenda-daily/weekly
  3309. :type '(repeat
  3310. (cons
  3311. (number :tag "Fraction of head-warning time passed")
  3312. (sexp :tag "Face"))))
  3313. (defface org-time-grid ;; font-lock-variable-name-face
  3314. (org-compatible-face
  3315. nil
  3316. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3317. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3318. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3319. "Face used for time grids."
  3320. :group 'org-faces)
  3321. (defconst org-level-faces
  3322. '(org-level-1 org-level-2 org-level-3 org-level-4
  3323. org-level-5 org-level-6 org-level-7 org-level-8
  3324. ))
  3325. (defcustom org-n-level-faces (length org-level-faces)
  3326. "The number different faces to be used for headlines.
  3327. Org-mode defines 8 different headline faces, so this can be at most 8.
  3328. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3329. :type 'number
  3330. :group 'org-faces)
  3331. ;;; Variables for pre-computed regular expressions, all buffer local
  3332. (defvar org-drawer-regexp nil
  3333. "Matches first line of a hidden block.")
  3334. (make-variable-buffer-local 'org-drawer-regexp)
  3335. (defvar org-todo-regexp nil
  3336. "Matches any of the TODO state keywords.")
  3337. (make-variable-buffer-local 'org-todo-regexp)
  3338. (defvar org-not-done-regexp nil
  3339. "Matches any of the TODO state keywords except the last one.")
  3340. (make-variable-buffer-local 'org-not-done-regexp)
  3341. (defvar org-todo-line-regexp nil
  3342. "Matches a headline and puts TODO state into group 2 if present.")
  3343. (make-variable-buffer-local 'org-todo-line-regexp)
  3344. (defvar org-todo-line-tags-regexp nil
  3345. "Matches a headline and puts TODO state into group 2 if present.
  3346. Also put tags into group 4 if tags are present.")
  3347. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3348. (defvar org-nl-done-regexp nil
  3349. "Matches newline followed by a headline with the DONE keyword.")
  3350. (make-variable-buffer-local 'org-nl-done-regexp)
  3351. (defvar org-looking-at-done-regexp nil
  3352. "Matches the DONE keyword a point.")
  3353. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3354. (defvar org-ds-keyword-length 12
  3355. "Maximum length of the Deadline and SCHEDULED keywords.")
  3356. (make-variable-buffer-local 'org-ds-keyword-length)
  3357. (defvar org-deadline-regexp nil
  3358. "Matches the DEADLINE keyword.")
  3359. (make-variable-buffer-local 'org-deadline-regexp)
  3360. (defvar org-deadline-time-regexp nil
  3361. "Matches the DEADLINE keyword together with a time stamp.")
  3362. (make-variable-buffer-local 'org-deadline-time-regexp)
  3363. (defvar org-deadline-line-regexp nil
  3364. "Matches the DEADLINE keyword and the rest of the line.")
  3365. (make-variable-buffer-local 'org-deadline-line-regexp)
  3366. (defvar org-scheduled-regexp nil
  3367. "Matches the SCHEDULED keyword.")
  3368. (make-variable-buffer-local 'org-scheduled-regexp)
  3369. (defvar org-scheduled-time-regexp nil
  3370. "Matches the SCHEDULED keyword together with a time stamp.")
  3371. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3372. (defvar org-closed-time-regexp nil
  3373. "Matches the CLOSED keyword together with a time stamp.")
  3374. (make-variable-buffer-local 'org-closed-time-regexp)
  3375. (defvar org-keyword-time-regexp nil
  3376. "Matches any of the 4 keywords, together with the time stamp.")
  3377. (make-variable-buffer-local 'org-keyword-time-regexp)
  3378. (defvar org-keyword-time-not-clock-regexp nil
  3379. "Matches any of the 3 keywords, together with the time stamp.")
  3380. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3381. (defvar org-maybe-keyword-time-regexp nil
  3382. "Matches a timestamp, possibly preceeded by a keyword.")
  3383. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3384. (defvar org-planning-or-clock-line-re nil
  3385. "Matches a line with planning or clock info.")
  3386. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3387. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3388. rear-nonsticky t mouse-map t fontified t)
  3389. "Properties to remove when a string without properties is wanted.")
  3390. (defsubst org-match-string-no-properties (num &optional string)
  3391. (if (featurep 'xemacs)
  3392. (let ((s (match-string num string)))
  3393. (remove-text-properties 0 (length s) org-rm-props s)
  3394. s)
  3395. (match-string-no-properties num string)))
  3396. (defsubst org-no-properties (s)
  3397. (if (fboundp 'set-text-properties)
  3398. (set-text-properties 0 (length s) nil s)
  3399. (remove-text-properties 0 (length s) org-rm-props s))
  3400. s)
  3401. (defsubst org-get-alist-option (option key)
  3402. (cond ((eq key t) t)
  3403. ((eq option t) t)
  3404. ((assoc key option) (cdr (assoc key option)))
  3405. (t (cdr (assq 'default option)))))
  3406. (defsubst org-inhibit-invisibility ()
  3407. "Modified `buffer-invisibility-spec' for Emacs 21.
  3408. Some ops with invisible text do not work correctly on Emacs 21. For these
  3409. we turn off invisibility temporarily. Use this in a `let' form."
  3410. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3411. (defsubst org-set-local (var value)
  3412. "Make VAR local in current buffer and set it to VALUE."
  3413. (set (make-variable-buffer-local var) value))
  3414. (defsubst org-mode-p ()
  3415. "Check if the current buffer is in Org-mode."
  3416. (eq major-mode 'org-mode))
  3417. (defsubst org-last (list)
  3418. "Return the last element of LIST."
  3419. (car (last list)))
  3420. (defun org-let (list &rest body)
  3421. (eval (cons 'let (cons list body))))
  3422. (put 'org-let 'lisp-indent-function 1)
  3423. (defun org-let2 (list1 list2 &rest body)
  3424. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3425. (put 'org-let2 'lisp-indent-function 2)
  3426. (defconst org-startup-options
  3427. '(("fold" org-startup-folded t)
  3428. ("overview" org-startup-folded t)
  3429. ("nofold" org-startup-folded nil)
  3430. ("showall" org-startup-folded nil)
  3431. ("content" org-startup-folded content)
  3432. ("hidestars" org-hide-leading-stars t)
  3433. ("showstars" org-hide-leading-stars nil)
  3434. ("odd" org-odd-levels-only t)
  3435. ("oddeven" org-odd-levels-only nil)
  3436. ("align" org-startup-align-all-tables t)
  3437. ("noalign" org-startup-align-all-tables nil)
  3438. ("customtime" org-display-custom-times t)
  3439. ("logging" org-log-done t)
  3440. ("logdone" org-log-done t)
  3441. ("nologging" org-log-done nil)
  3442. ("lognotedone" org-log-done done push)
  3443. ("lognotestate" org-log-done state push)
  3444. ("lognoteclock-out" org-log-done clock-out push)
  3445. ("logrepeat" org-log-repeat t)
  3446. ("nologrepeat" org-log-repeat nil)
  3447. ("constcgs" constants-unit-system cgs)
  3448. ("constSI" constants-unit-system SI))
  3449. "Variable associated with STARTUP options for org-mode.
  3450. Each element is a list of three items: The startup options as written
  3451. in the #+STARTUP line, the corresponding variable, and the value to
  3452. set this variable to if the option is found. An optional forth element PUSH
  3453. means to push this value onto the list in the variable.")
  3454. (defun org-set-regexps-and-options ()
  3455. "Precompute regular expressions for current buffer."
  3456. (when (org-mode-p)
  3457. (org-set-local 'org-todo-kwd-alist nil)
  3458. (org-set-local 'org-todo-key-alist nil)
  3459. (org-set-local 'org-todo-key-trigger nil)
  3460. (org-set-local 'org-todo-keywords-1 nil)
  3461. (org-set-local 'org-done-keywords nil)
  3462. (org-set-local 'org-todo-heads nil)
  3463. (org-set-local 'org-todo-sets nil)
  3464. (org-set-local 'org-todo-log-states nil)
  3465. (let ((re (org-make-options-regexp
  3466. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3467. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3468. "CONSTANTS" "PROPERTY")))
  3469. (splitre "[ \t]+")
  3470. kwds kws0 kwsa key value cat arch tags const links hw dws
  3471. tail sep kws1 prio props
  3472. ex log note)
  3473. (save-excursion
  3474. (save-restriction
  3475. (widen)
  3476. (goto-char (point-min))
  3477. (while (re-search-forward re nil t)
  3478. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3479. (cond
  3480. ((equal key "CATEGORY")
  3481. (if (string-match "[ \t]+$" value)
  3482. (setq value (replace-match "" t t value)))
  3483. (setq cat (intern value)))
  3484. ((member key '("SEQ_TODO" "TODO"))
  3485. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3486. ((equal key "TYP_TODO")
  3487. (push (cons 'type (org-split-string value splitre)) kwds))
  3488. ((equal key "TAGS")
  3489. (setq tags (append tags (org-split-string value splitre))))
  3490. ((equal key "COLUMNS")
  3491. (org-set-local 'org-columns-default-format value))
  3492. ((equal key "LINK")
  3493. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3494. (push (cons (match-string 1 value)
  3495. (org-trim (match-string 2 value)))
  3496. links)))
  3497. ((equal key "PRIORITIES")
  3498. (setq prio (org-split-string value " +")))
  3499. ((equal key "PROPERTY")
  3500. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3501. (push (cons (match-string 1 value) (match-string 2 value))
  3502. props)))
  3503. ((equal key "CONSTANTS")
  3504. (setq const (append const (org-split-string value splitre))))
  3505. ((equal key "STARTUP")
  3506. (let ((opts (org-split-string value splitre))
  3507. l var val)
  3508. (while (setq l (pop opts))
  3509. (when (setq l (assoc l org-startup-options))
  3510. (setq var (nth 1 l) val (nth 2 l))
  3511. (if (not (nth 3 l))
  3512. (set (make-local-variable var) val)
  3513. (if (not (listp (symbol-value var)))
  3514. (set (make-local-variable var) nil))
  3515. (set (make-local-variable var) (symbol-value var))
  3516. (add-to-list var val))))))
  3517. ((equal key "ARCHIVE")
  3518. (string-match " *$" value)
  3519. (setq arch (replace-match "" t t value))
  3520. (remove-text-properties 0 (length arch)
  3521. '(face t fontified t) arch)))
  3522. )))
  3523. (and cat (org-set-local 'org-category cat))
  3524. (when prio
  3525. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3526. (setq prio (mapcar 'string-to-char prio))
  3527. (org-set-local 'org-highest-priority (nth 0 prio))
  3528. (org-set-local 'org-lowest-priority (nth 1 prio))
  3529. (org-set-local 'org-default-priority (nth 2 prio)))
  3530. (and props (org-set-local 'org-local-properties (nreverse props)))
  3531. (and arch (org-set-local 'org-archive-location arch))
  3532. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3533. ;; Process the TODO keywords
  3534. (unless kwds
  3535. ;; Use the global values as if they had been given locally.
  3536. (setq kwds (default-value 'org-todo-keywords))
  3537. (if (stringp (car kwds))
  3538. (setq kwds (list (cons org-todo-interpretation
  3539. (default-value 'org-todo-keywords)))))
  3540. (setq kwds (reverse kwds)))
  3541. (setq kwds (nreverse kwds))
  3542. (let (inter kws kw)
  3543. (while (setq kws (pop kwds))
  3544. (setq inter (pop kws) sep (member "|" kws)
  3545. kws0 (delete "|" (copy-sequence kws))
  3546. kwsa nil
  3547. kws1 (mapcar
  3548. (lambda (x)
  3549. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3550. (progn
  3551. (setq kw (match-string 1 x)
  3552. ex (and (match-end 2) (match-string 2 x))
  3553. log (and ex (string-match "@" ex))
  3554. key (and ex (substring ex 0 1)))
  3555. (if (equal key "@") (setq key nil))
  3556. (push (cons kw (and key (string-to-char key))) kwsa)
  3557. (and log (push kw org-todo-log-states))
  3558. kw)
  3559. (error "Invalid TODO keyword %s" x)))
  3560. kws0)
  3561. kwsa (if kwsa (append '((:startgroup))
  3562. (nreverse kwsa)
  3563. '((:endgroup))))
  3564. hw (car kws1)
  3565. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3566. tail (list inter hw (car dws) (org-last dws)))
  3567. (add-to-list 'org-todo-heads hw 'append)
  3568. (push kws1 org-todo-sets)
  3569. (setq org-done-keywords (append org-done-keywords dws nil))
  3570. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3571. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3572. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3573. (setq org-todo-sets (nreverse org-todo-sets)
  3574. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3575. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3576. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3577. ;; Process the constants
  3578. (when const
  3579. (let (e cst)
  3580. (while (setq e (pop const))
  3581. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3582. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3583. (setq org-table-formula-constants-local cst)))
  3584. ;; Process the tags.
  3585. (when tags
  3586. (let (e tgs)
  3587. (while (setq e (pop tags))
  3588. (cond
  3589. ((equal e "{") (push '(:startgroup) tgs))
  3590. ((equal e "}") (push '(:endgroup) tgs))
  3591. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3592. (push (cons (match-string 1 e)
  3593. (string-to-char (match-string 2 e)))
  3594. tgs))
  3595. (t (push (list e) tgs))))
  3596. (org-set-local 'org-tag-alist nil)
  3597. (while (setq e (pop tgs))
  3598. (or (and (stringp (car e))
  3599. (assoc (car e) org-tag-alist))
  3600. (push e org-tag-alist))))))
  3601. ;; Compute the regular expressions and other local variables
  3602. (if (not org-done-keywords)
  3603. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3604. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3605. (length org-scheduled-string)))
  3606. org-drawer-regexp
  3607. (concat "^[ \t]*:\\("
  3608. (mapconcat 'regexp-quote org-drawers "\\|")
  3609. "\\):[ \t]*$")
  3610. org-not-done-keywords
  3611. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3612. org-todo-regexp
  3613. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3614. "\\|") "\\)\\>")
  3615. org-not-done-regexp
  3616. (concat "\\<\\("
  3617. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3618. "\\)\\>")
  3619. org-todo-line-regexp
  3620. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3621. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3622. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3623. org-nl-done-regexp
  3624. (concat "\n\\*+[ \t]+"
  3625. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3626. "\\)" "\\>")
  3627. org-todo-line-tags-regexp
  3628. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3629. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3630. (org-re
  3631. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3632. org-looking-at-done-regexp
  3633. (concat "^" "\\(?:"
  3634. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3635. "\\>")
  3636. org-deadline-regexp (concat "\\<" org-deadline-string)
  3637. org-deadline-time-regexp
  3638. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3639. org-deadline-line-regexp
  3640. (concat "\\<\\(" org-deadline-string "\\).*")
  3641. org-scheduled-regexp
  3642. (concat "\\<" org-scheduled-string)
  3643. org-scheduled-time-regexp
  3644. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3645. org-closed-time-regexp
  3646. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3647. org-keyword-time-regexp
  3648. (concat "\\<\\(" org-scheduled-string
  3649. "\\|" org-deadline-string
  3650. "\\|" org-closed-string
  3651. "\\|" org-clock-string "\\)"
  3652. " *[[<]\\([^]>]+\\)[]>]")
  3653. org-keyword-time-not-clock-regexp
  3654. (concat "\\<\\(" org-scheduled-string
  3655. "\\|" org-deadline-string
  3656. "\\|" org-closed-string
  3657. "\\)"
  3658. " *[[<]\\([^]>]+\\)[]>]")
  3659. org-maybe-keyword-time-regexp
  3660. (concat "\\(\\<\\(" org-scheduled-string
  3661. "\\|" org-deadline-string
  3662. "\\|" org-closed-string
  3663. "\\|" org-clock-string "\\)\\)?"
  3664. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3665. org-planning-or-clock-line-re
  3666. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3667. "\\|" org-deadline-string
  3668. "\\|" org-closed-string "\\|" org-clock-string
  3669. "\\)\\>\\)")
  3670. )
  3671. (org-set-font-lock-defaults)))
  3672. (defun org-remove-keyword-keys (list)
  3673. (mapcar (lambda (x)
  3674. (if (string-match "(..?)$" x)
  3675. (substring x 0 (match-beginning 0))
  3676. x))
  3677. list))
  3678. ;;; Some variables ujsed in various places
  3679. (defvar org-window-configuration nil
  3680. "Used in various places to store a window configuration.")
  3681. (defvar org-finish-function nil
  3682. "Function to be called when `C-c C-c' is used.
  3683. This is for getting out of special buffers like remember.")
  3684. ;;; Foreign variables, to inform the compiler
  3685. ;; XEmacs only
  3686. (defvar outline-mode-menu-heading)
  3687. (defvar outline-mode-menu-show)
  3688. (defvar outline-mode-menu-hide)
  3689. (defvar zmacs-regions) ; XEmacs regions
  3690. ;; Emacs only
  3691. (defvar mark-active)
  3692. ;; Packages that org-mode interacts with
  3693. (defvar calc-embedded-close-formula)
  3694. (defvar calc-embedded-open-formula)
  3695. (defvar font-lock-unfontify-region-function)
  3696. (defvar org-goto-start-pos)
  3697. (defvar vm-message-pointer)
  3698. (defvar vm-folder-directory)
  3699. (defvar wl-summary-buffer-elmo-folder)
  3700. (defvar wl-summary-buffer-folder-name)
  3701. (defvar gnus-other-frame-object)
  3702. (defvar gnus-group-name)
  3703. (defvar gnus-article-current)
  3704. (defvar w3m-current-url)
  3705. (defvar w3m-current-title)
  3706. (defvar mh-progs)
  3707. (defvar mh-current-folder)
  3708. (defvar mh-show-folder-buffer)
  3709. (defvar mh-index-folder)
  3710. (defvar mh-searcher)
  3711. (defvar calendar-mode-map)
  3712. (defvar Info-current-file)
  3713. (defvar Info-current-node)
  3714. (defvar texmathp-why)
  3715. (defvar remember-save-after-remembering)
  3716. (defvar remember-data-file)
  3717. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3718. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3719. (defvar org-latex-regexps)
  3720. (defvar constants-unit-system)
  3721. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3722. ;; FIXME: Occasionally check by commenting these, to make sure
  3723. ;; no other functions uses these, forgetting to let-bind them.
  3724. (defvar entry)
  3725. (defvar state)
  3726. (defvar last-state)
  3727. (defvar date)
  3728. (defvar description)
  3729. ;; Defined somewhere in this file, but used before definition.
  3730. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3731. (defvar org-agenda-buffer-name)
  3732. (defvar org-agenda-undo-list)
  3733. (defvar org-agenda-pending-undo-list)
  3734. (defvar org-agenda-overriding-header)
  3735. (defvar orgtbl-mode)
  3736. (defvar org-html-entities)
  3737. (defvar org-struct-menu)
  3738. (defvar org-org-menu)
  3739. (defvar org-tbl-menu)
  3740. (defvar org-agenda-keymap)
  3741. ;;;; Emacs/XEmacs compatibility
  3742. ;; Overlay compatibility functions
  3743. (defun org-make-overlay (beg end &optional buffer)
  3744. (if (featurep 'xemacs)
  3745. (make-extent beg end buffer)
  3746. (make-overlay beg end buffer)))
  3747. (defun org-delete-overlay (ovl)
  3748. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3749. (defun org-detach-overlay (ovl)
  3750. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3751. (defun org-move-overlay (ovl beg end &optional buffer)
  3752. (if (featurep 'xemacs)
  3753. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3754. (move-overlay ovl beg end buffer)))
  3755. (defun org-overlay-put (ovl prop value)
  3756. (if (featurep 'xemacs)
  3757. (set-extent-property ovl prop value)
  3758. (overlay-put ovl prop value)))
  3759. (defun org-overlay-display (ovl text &optional face evap)
  3760. "Make overlay OVL display TEXT with face FACE."
  3761. (if (featurep 'xemacs)
  3762. (let ((gl (make-glyph text)))
  3763. (and face (set-glyph-face gl face))
  3764. (set-extent-property ovl 'invisible t)
  3765. (set-extent-property ovl 'end-glyph gl))
  3766. (overlay-put ovl 'display text)
  3767. (if face (overlay-put ovl 'face face))
  3768. (if evap (overlay-put ovl 'evaporate t))))
  3769. (defun org-overlay-before-string (ovl text &optional face evap)
  3770. "Make overlay OVL display TEXT with face FACE."
  3771. (if (featurep 'xemacs)
  3772. (let ((gl (make-glyph text)))
  3773. (and face (set-glyph-face gl face))
  3774. (set-extent-property ovl 'begin-glyph gl))
  3775. (if face (org-add-props text nil 'face face))
  3776. (overlay-put ovl 'before-string text)
  3777. (if evap (overlay-put ovl 'evaporate t))))
  3778. (defun org-overlay-get (ovl prop)
  3779. (if (featurep 'xemacs)
  3780. (extent-property ovl prop)
  3781. (overlay-get ovl prop)))
  3782. (defun org-overlays-at (pos)
  3783. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3784. (defun org-overlays-in (&optional start end)
  3785. (if (featurep 'xemacs)
  3786. (extent-list nil start end)
  3787. (overlays-in start end)))
  3788. (defun org-overlay-start (o)
  3789. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  3790. (defun org-overlay-end (o)
  3791. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  3792. (defun org-find-overlays (prop &optional pos delete)
  3793. "Find all overlays specifying PROP at POS or point.
  3794. If DELETE is non-nil, delete all those overlays."
  3795. (let ((overlays (org-overlays-at (or pos (point))))
  3796. ov found)
  3797. (while (setq ov (pop overlays))
  3798. (if (org-overlay-get ov prop)
  3799. (if delete (org-delete-overlay ov) (push ov found))))
  3800. found))
  3801. ;; Region compatibility
  3802. (defun org-add-hook (hook function &optional append local)
  3803. "Add-hook, compatible with both Emacsen."
  3804. (if (and local (featurep 'xemacs))
  3805. (add-local-hook hook function append)
  3806. (add-hook hook function append local)))
  3807. (defvar org-ignore-region nil
  3808. "To temporarily disable the active region.")
  3809. (defun org-region-active-p ()
  3810. "Is `transient-mark-mode' on and the region active?
  3811. Works on both Emacs and XEmacs."
  3812. (if org-ignore-region
  3813. nil
  3814. (if (featurep 'xemacs)
  3815. (and zmacs-regions (region-active-p))
  3816. (and transient-mark-mode mark-active))))
  3817. ;; Invisibility compatibility
  3818. (defun org-add-to-invisibility-spec (arg)
  3819. "Add elements to `buffer-invisibility-spec'.
  3820. See documentation for `buffer-invisibility-spec' for the kind of elements
  3821. that can be added."
  3822. (cond
  3823. ((fboundp 'add-to-invisibility-spec)
  3824. (add-to-invisibility-spec arg))
  3825. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  3826. (setq buffer-invisibility-spec (list arg)))
  3827. (t
  3828. (setq buffer-invisibility-spec
  3829. (cons arg buffer-invisibility-spec)))))
  3830. (defun org-remove-from-invisibility-spec (arg)
  3831. "Remove elements from `buffer-invisibility-spec'."
  3832. (if (fboundp 'remove-from-invisibility-spec)
  3833. (remove-from-invisibility-spec arg)
  3834. (if (consp buffer-invisibility-spec)
  3835. (setq buffer-invisibility-spec
  3836. (delete arg buffer-invisibility-spec)))))
  3837. (defun org-in-invisibility-spec-p (arg)
  3838. "Is ARG a member of `buffer-invisibility-spec'?"
  3839. (if (consp buffer-invisibility-spec)
  3840. (member arg buffer-invisibility-spec)
  3841. nil))
  3842. ;;;; Define the Org-mode
  3843. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3844. (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."))
  3845. ;; We use a before-change function to check if a table might need
  3846. ;; an update.
  3847. (defvar org-table-may-need-update t
  3848. "Indicates that a table might need an update.
  3849. This variable is set by `org-before-change-function'.
  3850. `org-table-align' sets it back to nil.")
  3851. (defvar org-mode-map)
  3852. (defvar org-mode-hook nil)
  3853. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3854. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3855. (defvar org-table-buffer-is-an nil)
  3856. (defconst org-outline-regexp "\\*+ ")
  3857. ;;;###autoload
  3858. (define-derived-mode org-mode outline-mode "Org"
  3859. "Outline-based notes management and organizer, alias
  3860. \"Carsten's outline-mode for keeping track of everything.\"
  3861. Org-mode develops organizational tasks around a NOTES file which
  3862. contains information about projects as plain text. Org-mode is
  3863. implemented on top of outline-mode, which is ideal to keep the content
  3864. of large files well structured. It supports ToDo items, deadlines and
  3865. time stamps, which magically appear in the diary listing of the Emacs
  3866. calendar. Tables are easily created with a built-in table editor.
  3867. Plain text URL-like links connect to websites, emails (VM), Usenet
  3868. messages (Gnus), BBDB entries, and any files related to the project.
  3869. For printing and sharing of notes, an Org-mode file (or a part of it)
  3870. can be exported as a structured ASCII or HTML file.
  3871. The following commands are available:
  3872. \\{org-mode-map}"
  3873. ;; Get rid of Outline menus, they are not needed
  3874. ;; Need to do this here because define-derived-mode sets up
  3875. ;; the keymap so late. Still, it is a waste to call this each time
  3876. ;; we switch another buffer into org-mode.
  3877. (if (featurep 'xemacs)
  3878. (when (boundp 'outline-mode-menu-heading)
  3879. ;; Assume this is Greg's port, it used easymenu
  3880. (easy-menu-remove outline-mode-menu-heading)
  3881. (easy-menu-remove outline-mode-menu-show)
  3882. (easy-menu-remove outline-mode-menu-hide))
  3883. (define-key org-mode-map [menu-bar headings] 'undefined)
  3884. (define-key org-mode-map [menu-bar hide] 'undefined)
  3885. (define-key org-mode-map [menu-bar show] 'undefined))
  3886. (easy-menu-add org-org-menu)
  3887. (easy-menu-add org-tbl-menu)
  3888. (org-install-agenda-files-menu)
  3889. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3890. (org-add-to-invisibility-spec '(org-cwidth))
  3891. (when (featurep 'xemacs)
  3892. (org-set-local 'line-move-ignore-invisible t))
  3893. (org-set-local 'outline-regexp org-outline-regexp)
  3894. (org-set-local 'outline-level 'org-outline-level)
  3895. (when (and org-ellipsis
  3896. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3897. (fboundp 'make-glyph-code))
  3898. (unless org-display-table
  3899. (setq org-display-table (make-display-table)))
  3900. (set-display-table-slot
  3901. org-display-table 4
  3902. (vconcat (mapcar
  3903. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3904. org-ellipsis)))
  3905. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3906. (setq buffer-display-table org-display-table))
  3907. (org-set-regexps-and-options)
  3908. ;; Calc embedded
  3909. (org-set-local 'calc-embedded-open-mode "# ")
  3910. (modify-syntax-entry ?# "<")
  3911. (modify-syntax-entry ?@ "w")
  3912. (if org-startup-truncated (setq truncate-lines t))
  3913. (org-set-local 'font-lock-unfontify-region-function
  3914. 'org-unfontify-region)
  3915. ;; Activate before-change-function
  3916. (org-set-local 'org-table-may-need-update t)
  3917. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3918. 'local)
  3919. ;; Check for running clock before killing a buffer
  3920. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3921. ;; Paragraphs and auto-filling
  3922. (org-set-autofill-regexps)
  3923. (setq indent-line-function 'org-indent-line-function)
  3924. (org-update-radio-target-regexp)
  3925. ;; Comment characters
  3926. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3927. (org-set-local 'comment-padding " ")
  3928. ;; Make isearch reveal context
  3929. (if (or (featurep 'xemacs)
  3930. (not (boundp 'outline-isearch-open-invisible-function)))
  3931. ;; Emacs 21 and XEmacs make use of the hook
  3932. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3933. ;; Emacs 22 deals with this through a special variable
  3934. (org-set-local 'outline-isearch-open-invisible-function
  3935. (lambda (&rest ignore) (org-show-context 'isearch))))
  3936. ;; If empty file that did not turn on org-mode automatically, make it to.
  3937. (if (and org-insert-mode-line-in-empty-file
  3938. (interactive-p)
  3939. (= (point-min) (point-max)))
  3940. (insert "# -*- mode: org -*-\n\n"))
  3941. (unless org-inhibit-startup
  3942. (when org-startup-align-all-tables
  3943. (let ((bmp (buffer-modified-p)))
  3944. (org-table-map-tables 'org-table-align)
  3945. (set-buffer-modified-p bmp)))
  3946. (org-cycle-hide-drawers 'all)
  3947. (cond
  3948. ((eq org-startup-folded t)
  3949. (org-cycle '(4)))
  3950. ((eq org-startup-folded 'content)
  3951. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3952. (org-cycle '(4)) (org-cycle '(4)))))))
  3953. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3954. (defsubst org-call-with-arg (command arg)
  3955. "Call COMMAND interactively, but pretend prefix are was ARG."
  3956. (let ((current-prefix-arg arg)) (call-interactively command)))
  3957. (defsubst org-current-line (&optional pos)
  3958. (save-excursion
  3959. (and pos (goto-char pos))
  3960. ;; works also in narrowed buffer, because we start at 1, not point-min
  3961. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  3962. (defun org-current-time ()
  3963. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3964. (if (> org-time-stamp-rounding-minutes 0)
  3965. (let ((r org-time-stamp-rounding-minutes)
  3966. (time (decode-time)))
  3967. (apply 'encode-time
  3968. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3969. (nthcdr 2 time))))
  3970. (current-time)))
  3971. (defun org-add-props (string plist &rest props)
  3972. "Add text properties to entire string, from beginning to end.
  3973. PLIST may be a list of properties, PROPS are individual properties and values
  3974. that will be added to PLIST. Returns the string that was modified."
  3975. (add-text-properties
  3976. 0 (length string) (if props (append plist props) plist) string)
  3977. string)
  3978. (put 'org-add-props 'lisp-indent-function 2)
  3979. ;;;; Font-Lock stuff, including the activators
  3980. (defvar org-mouse-map (make-sparse-keymap))
  3981. (org-defkey org-mouse-map
  3982. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3983. (org-defkey org-mouse-map
  3984. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3985. (when org-mouse-1-follows-link
  3986. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3987. (when org-tab-follows-link
  3988. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3989. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3990. (when org-return-follows-link
  3991. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3992. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3993. (require 'font-lock)
  3994. (defconst org-non-link-chars "]\t\n\r<>")
  3995. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  3996. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  3997. (defvar org-link-re-with-space nil
  3998. "Matches a link with spaces, optional angular brackets around it.")
  3999. (defvar org-link-re-with-space2 nil
  4000. "Matches a link with spaces, optional angular brackets around it.")
  4001. (defvar org-angle-link-re nil
  4002. "Matches link with angular brackets, spaces are allowed.")
  4003. (defvar org-plain-link-re nil
  4004. "Matches plain link, without spaces.")
  4005. (defvar org-bracket-link-regexp nil
  4006. "Matches a link in double brackets.")
  4007. (defvar org-bracket-link-analytic-regexp nil
  4008. "Regular expression used to analyze links.
  4009. Here is what the match groups contain after a match:
  4010. 1: http:
  4011. 2: http
  4012. 3: path
  4013. 4: [desc]
  4014. 5: desc")
  4015. (defvar org-any-link-re nil
  4016. "Regular expression matching any link.")
  4017. (defun org-make-link-regexps ()
  4018. "Update the link regular expressions.
  4019. This should be called after the variable `org-link-types' has changed."
  4020. (setq org-link-re-with-space
  4021. (concat
  4022. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4023. "\\([^" org-non-link-chars " ]"
  4024. "[^" org-non-link-chars "]*"
  4025. "[^" org-non-link-chars " ]\\)>?")
  4026. org-link-re-with-space2
  4027. (concat
  4028. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4029. "\\([^" org-non-link-chars " ]"
  4030. "[^]\t\n\r]*"
  4031. "[^" org-non-link-chars " ]\\)>?")
  4032. org-angle-link-re
  4033. (concat
  4034. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4035. "\\([^" org-non-link-chars " ]"
  4036. "[^" org-non-link-chars "]*"
  4037. "\\)>")
  4038. org-plain-link-re
  4039. (concat
  4040. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4041. "\\([^]\t\n\r<>,;() ]+\\)")
  4042. org-bracket-link-regexp
  4043. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4044. org-bracket-link-analytic-regexp
  4045. (concat
  4046. "\\[\\["
  4047. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4048. "\\([^]]+\\)"
  4049. "\\]"
  4050. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4051. "\\]")
  4052. org-any-link-re
  4053. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4054. org-angle-link-re "\\)\\|\\("
  4055. org-plain-link-re "\\)")))
  4056. (org-make-link-regexps)
  4057. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4058. "Regular expression for fast time stamp matching.")
  4059. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4060. "Regular expression for fast time stamp matching.")
  4061. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4062. "Regular expression matching time strings for analysis.
  4063. This one does not require the space after the date.")
  4064. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4065. "Regular expression matching time strings for analysis.")
  4066. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4067. "Regular expression matching time stamps, with groups.")
  4068. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4069. "Regular expression matching time stamps (also [..]), with groups.")
  4070. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4071. "Regular expression matching a time stamp range.")
  4072. (defconst org-tr-regexp-both
  4073. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4074. "Regular expression matching a time stamp range.")
  4075. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4076. org-ts-regexp "\\)?")
  4077. "Regular expression matching a time stamp or time stamp range.")
  4078. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4079. org-ts-regexp-both "\\)?")
  4080. "Regular expression matching a time stamp or time stamp range.
  4081. The time stamps may be either active or inactive.")
  4082. (defvar org-emph-face nil)
  4083. (defun org-do-emphasis-faces (limit)
  4084. "Run through the buffer and add overlays to links."
  4085. (let (rtn)
  4086. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4087. (if (not (= (char-after (match-beginning 3))
  4088. (char-after (match-beginning 4))))
  4089. (progn
  4090. (setq rtn t)
  4091. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4092. 'face
  4093. (nth 1 (assoc (match-string 3)
  4094. org-emphasis-alist)))
  4095. (add-text-properties (match-beginning 2) (match-end 2)
  4096. '(font-lock-multiline t))
  4097. (backward-char 1))))
  4098. rtn))
  4099. (defun org-emphasize (&optional char)
  4100. "Insert or change an emphasis, i.e. a font like bold or italic.
  4101. If there is an active region, change that region to a new emphasis.
  4102. If there is no region, just insert the marker characters and position
  4103. the cursor between them.
  4104. CHAR should be either the marker character, or the first character of the
  4105. HTML tag associated with that emphasis. If CHAR is a space, the means
  4106. to remove the emphasis of the selected region.
  4107. If char is not given (for example in an interactive call) it
  4108. will be prompted for."
  4109. (interactive)
  4110. (let ((eal org-emphasis-alist) e det
  4111. (erc org-emphasis-regexp-components)
  4112. (prompt "")
  4113. (string "") beg end move tag c s)
  4114. (if (org-region-active-p)
  4115. (setq beg (region-beginning) end (region-end)
  4116. string (buffer-substring beg end))
  4117. (setq move t))
  4118. (while (setq e (pop eal))
  4119. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4120. c (aref tag 0))
  4121. (push (cons c (string-to-char (car e))) det)
  4122. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4123. (substring tag 1)))))
  4124. (unless char
  4125. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4126. (setq char (read-char-exclusive)))
  4127. (setq char (or (cdr (assoc char det)) char))
  4128. (if (equal char ?\ )
  4129. (setq s "" move nil)
  4130. (unless (assoc (char-to-string char) org-emphasis-alist)
  4131. (error "No such emphasis marker: \"%c\"" char))
  4132. (setq s (char-to-string char)))
  4133. (while (and (> (length string) 1)
  4134. (equal (substring string 0 1) (substring string -1))
  4135. (assoc (substring string 0 1) org-emphasis-alist))
  4136. (setq string (substring string 1 -1)))
  4137. (setq string (concat s string s))
  4138. (if beg (delete-region beg end))
  4139. (unless (or (bolp)
  4140. (string-match (concat "[" (nth 0 erc) "\n]")
  4141. (char-to-string (char-before (point)))))
  4142. (insert " "))
  4143. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4144. (char-to-string (char-after (point))))
  4145. (insert " ") (backward-char 1))
  4146. (insert string)
  4147. (and move (backward-char 1))))
  4148. (defconst org-nonsticky-props
  4149. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4150. (defun org-activate-plain-links (limit)
  4151. "Run through the buffer and add overlays to links."
  4152. (catch 'exit
  4153. (let (f)
  4154. (while (re-search-forward org-plain-link-re limit t)
  4155. (setq f (get-text-property (match-beginning 0) 'face))
  4156. (if (or (eq f 'org-tag)
  4157. (and (listp f) (memq 'org-tag f)))
  4158. nil
  4159. (add-text-properties (match-beginning 0) (match-end 0)
  4160. (list 'mouse-face 'highlight
  4161. 'rear-nonsticky org-nonsticky-props
  4162. 'keymap org-mouse-map
  4163. ))
  4164. (throw 'exit t))))))
  4165. (defun org-activate-code (limit)
  4166. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4167. (unless (get-text-property (match-beginning 1) 'face)
  4168. (remove-text-properties (match-beginning 0) (match-end 0)
  4169. '(display t invisible t intangible t))
  4170. t)))
  4171. (defun org-activate-angle-links (limit)
  4172. "Run through the buffer and add overlays to links."
  4173. (if (re-search-forward org-angle-link-re limit t)
  4174. (progn
  4175. (add-text-properties (match-beginning 0) (match-end 0)
  4176. (list 'mouse-face 'highlight
  4177. 'rear-nonsticky org-nonsticky-props
  4178. 'keymap org-mouse-map
  4179. ))
  4180. t)))
  4181. (defmacro org-maybe-intangible (props)
  4182. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4183. In emacs 21, invisible text is not avoided by the command loop, so the
  4184. intangible property is needed to make sure point skips this text.
  4185. In Emacs 22, this is not necessary. The intangible text property has
  4186. led to problems with flyspell. These problems are fixed in flyspell.el,
  4187. but we still avoid setting the property in Emacs 22 and later.
  4188. We use a macro so that the test can happen at compilation time."
  4189. (if (< emacs-major-version 22)
  4190. `(append '(intangible t) ,props)
  4191. props))
  4192. (defun org-activate-bracket-links (limit)
  4193. "Run through the buffer and add overlays to bracketed links."
  4194. (if (re-search-forward org-bracket-link-regexp limit t)
  4195. (let* ((help (concat "LINK: "
  4196. (org-match-string-no-properties 1)))
  4197. ;; FIXME: above we should remove the escapes.
  4198. ;; but that requires another match, protecting match data,
  4199. ;; a lot of overhead for font-lock.
  4200. (ip (org-maybe-intangible
  4201. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4202. 'keymap org-mouse-map 'mouse-face 'highlight
  4203. 'help-echo help)))
  4204. (vp (list 'rear-nonsticky org-nonsticky-props
  4205. 'keymap org-mouse-map 'mouse-face 'highlight
  4206. 'help-echo help)))
  4207. ;; We need to remove the invisible property here. Table narrowing
  4208. ;; may have made some of this invisible.
  4209. (remove-text-properties (match-beginning 0) (match-end 0)
  4210. '(invisible nil))
  4211. (if (match-end 3)
  4212. (progn
  4213. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4214. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4215. (add-text-properties (match-end 3) (match-end 0) ip))
  4216. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4217. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4218. (add-text-properties (match-end 1) (match-end 0) ip))
  4219. t)))
  4220. (defun org-activate-dates (limit)
  4221. "Run through the buffer and add overlays to dates."
  4222. (if (re-search-forward org-tsr-regexp-both limit t)
  4223. (progn
  4224. (add-text-properties (match-beginning 0) (match-end 0)
  4225. (list 'mouse-face 'highlight
  4226. 'rear-nonsticky org-nonsticky-props
  4227. 'keymap org-mouse-map))
  4228. (when org-display-custom-times
  4229. (if (match-end 3)
  4230. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4231. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4232. t)))
  4233. (defvar org-target-link-regexp nil
  4234. "Regular expression matching radio targets in plain text.")
  4235. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4236. "Regular expression matching a link target.")
  4237. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4238. "Regular expression matching a radio target.")
  4239. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4240. "Regular expression matching any target.")
  4241. (defun org-activate-target-links (limit)
  4242. "Run through the buffer and add overlays to target matches."
  4243. (when org-target-link-regexp
  4244. (let ((case-fold-search t))
  4245. (if (re-search-forward org-target-link-regexp limit t)
  4246. (progn
  4247. (add-text-properties (match-beginning 0) (match-end 0)
  4248. (list 'mouse-face 'highlight
  4249. 'rear-nonsticky org-nonsticky-props
  4250. 'keymap org-mouse-map
  4251. 'help-echo "Radio target link"
  4252. 'org-linked-text t))
  4253. t)))))
  4254. (defun org-update-radio-target-regexp ()
  4255. "Find all radio targets in this file and update the regular expression."
  4256. (interactive)
  4257. (when (memq 'radio org-activate-links)
  4258. (setq org-target-link-regexp
  4259. (org-make-target-link-regexp (org-all-targets 'radio)))
  4260. (org-restart-font-lock)))
  4261. (defun org-hide-wide-columns (limit)
  4262. (let (s e)
  4263. (setq s (text-property-any (point) (or limit (point-max))
  4264. 'org-cwidth t))
  4265. (when s
  4266. (setq e (next-single-property-change s 'org-cwidth))
  4267. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4268. (goto-char e)
  4269. t)))
  4270. (defun org-restart-font-lock ()
  4271. "Restart font-lock-mode, to force refontification."
  4272. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4273. (font-lock-mode -1)
  4274. (font-lock-mode 1)))
  4275. (defun org-all-targets (&optional radio)
  4276. "Return a list of all targets in this file.
  4277. With optional argument RADIO, only find radio targets."
  4278. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4279. rtn)
  4280. (save-excursion
  4281. (goto-char (point-min))
  4282. (while (re-search-forward re nil t)
  4283. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4284. rtn)))
  4285. (defun org-make-target-link-regexp (targets)
  4286. "Make regular expression matching all strings in TARGETS.
  4287. The regular expression finds the targets also if there is a line break
  4288. between words."
  4289. (and targets
  4290. (concat
  4291. "\\<\\("
  4292. (mapconcat
  4293. (lambda (x)
  4294. (while (string-match " +" x)
  4295. (setq x (replace-match "\\s-+" t t x)))
  4296. x)
  4297. targets
  4298. "\\|")
  4299. "\\)\\>")))
  4300. (defun org-activate-tags (limit)
  4301. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4302. (progn
  4303. (add-text-properties (match-beginning 1) (match-end 1)
  4304. (list 'mouse-face 'highlight
  4305. 'rear-nonsticky org-nonsticky-props
  4306. 'keymap org-mouse-map))
  4307. t)))
  4308. (defun org-outline-level ()
  4309. (save-excursion
  4310. (looking-at outline-regexp)
  4311. (if (match-beginning 1)
  4312. (+ (org-get-string-indentation (match-string 1)) 1000)
  4313. (1- (- (match-end 0) (match-beginning 0))))))
  4314. (defvar org-font-lock-keywords nil)
  4315. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4316. "Regular expression matching a property line.")
  4317. (defun org-set-font-lock-defaults ()
  4318. (let* ((em org-fontify-emphasized-text)
  4319. (lk org-activate-links)
  4320. (org-font-lock-extra-keywords
  4321. (list
  4322. ;; Headlines
  4323. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4324. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4325. ;; Table lines
  4326. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4327. (1 'org-table t))
  4328. ;; Table internals
  4329. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4330. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4331. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4332. ;; Drawers
  4333. (list org-drawer-regexp '(0 'org-special-keyword t))
  4334. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4335. ;; Properties
  4336. (list org-property-re
  4337. '(1 'org-special-keyword t)
  4338. '(3 'org-property-value t))
  4339. (if org-format-transports-properties-p
  4340. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4341. ;; Links
  4342. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4343. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4344. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4345. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4346. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4347. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4348. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4349. '(org-hide-wide-columns (0 nil append))
  4350. ;; TODO lines
  4351. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4352. '(1 (org-get-todo-face 1) t))
  4353. ;; DONE
  4354. (if org-fontify-done-headline
  4355. (list (concat "^[*]+ +\\<\\("
  4356. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4357. "\\)\\(.*\\)")
  4358. '(2 'org-headline-done t))
  4359. nil)
  4360. ;; Priorities
  4361. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4362. ;; Special keywords
  4363. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4364. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4365. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4366. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4367. ;; Emphasis
  4368. (if em
  4369. (if (featurep 'xemacs)
  4370. '(org-do-emphasis-faces (0 nil append))
  4371. '(org-do-emphasis-faces)))
  4372. ;; Checkboxes
  4373. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4374. 2 'bold prepend)
  4375. (if org-provide-checkbox-statistics
  4376. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4377. (0 (org-get-checkbox-statistics-face) t)))
  4378. ;; COMMENT
  4379. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4380. "\\|" org-quote-string "\\)\\>")
  4381. '(1 'org-special-keyword t))
  4382. '("^#.*" (0 'font-lock-comment-face t))
  4383. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  4384. ;; Code
  4385. '(org-activate-code (1 'org-code t))
  4386. )))
  4387. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4388. ;; Now set the full font-lock-keywords
  4389. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4390. (org-set-local 'font-lock-defaults
  4391. '(org-font-lock-keywords t nil nil backward-paragraph))
  4392. (kill-local-variable 'font-lock-keywords) nil))
  4393. (defvar org-m nil)
  4394. (defvar org-l nil)
  4395. (defvar org-f nil)
  4396. (defun org-get-level-face (n)
  4397. "Get the right face for match N in font-lock matching of healdines."
  4398. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4399. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4400. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4401. (cond
  4402. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4403. ((eq n 2) org-f)
  4404. (t (if org-level-color-stars-only nil org-f))))
  4405. (defun org-get-todo-face (kwd)
  4406. "Get the right face for a TODO keyword KWD.
  4407. If KWD is a number, get the corresponding match group."
  4408. (if (numberp kwd) (setq kwd (match-string kwd)))
  4409. (or (cdr (assoc kwd org-todo-keyword-faces))
  4410. (and (member kwd org-done-keywords) 'org-done)
  4411. 'org-todo))
  4412. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4413. "Remove fontification and activation overlays from links."
  4414. (font-lock-default-unfontify-region beg end)
  4415. (let* ((buffer-undo-list t)
  4416. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4417. (inhibit-modification-hooks t)
  4418. deactivate-mark buffer-file-name buffer-file-truename)
  4419. (remove-text-properties beg end
  4420. '(mouse-face t keymap t org-linked-text t
  4421. invisible t intangible t))))
  4422. ;;;; Visibility cycling, including org-goto and indirect buffer
  4423. ;;; Cycling
  4424. (defvar org-cycle-global-status nil)
  4425. (make-variable-buffer-local 'org-cycle-global-status)
  4426. (defvar org-cycle-subtree-status nil)
  4427. (make-variable-buffer-local 'org-cycle-subtree-status)
  4428. ;;;###autoload
  4429. (defun org-cycle (&optional arg)
  4430. "Visibility cycling for Org-mode.
  4431. - When this function is called with a prefix argument, rotate the entire
  4432. buffer through 3 states (global cycling)
  4433. 1. OVERVIEW: Show only top-level headlines.
  4434. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4435. 3. SHOW ALL: Show everything.
  4436. - When point is at the beginning of a headline, rotate the subtree started
  4437. by this line through 3 different states (local cycling)
  4438. 1. FOLDED: Only the main headline is shown.
  4439. 2. CHILDREN: The main headline and the direct children are shown.
  4440. From this state, you can move to one of the children
  4441. and zoom in further.
  4442. 3. SUBTREE: Show the entire subtree, including body text.
  4443. - When there is a numeric prefix, go up to a heading with level ARG, do
  4444. a `show-subtree' and return to the previous cursor position. If ARG
  4445. is negative, go up that many levels.
  4446. - When point is not at the beginning of a headline, execute
  4447. `indent-relative', like TAB normally does. See the option
  4448. `org-cycle-emulate-tab' for details.
  4449. - Special case: if point is at the beginning of the buffer and there is
  4450. no headline in line 1, this function will act as if called with prefix arg.
  4451. But only if also the variable `org-cycle-global-at-bob' is t."
  4452. (interactive "P")
  4453. (let* ((outline-regexp
  4454. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4455. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4456. outline-regexp))
  4457. (bob-special (and org-cycle-global-at-bob (bobp)
  4458. (not (looking-at outline-regexp))))
  4459. (org-cycle-hook
  4460. (if bob-special
  4461. (delq 'org-optimize-window-after-visibility-change
  4462. (copy-sequence org-cycle-hook))
  4463. org-cycle-hook))
  4464. (pos (point)))
  4465. (if (or bob-special (equal arg '(4)))
  4466. ;; special case: use global cycling
  4467. (setq arg t))
  4468. (cond
  4469. ((org-at-table-p 'any)
  4470. ;; Enter the table or move to the next field in the table
  4471. (or (org-table-recognize-table.el)
  4472. (progn
  4473. (if arg (org-table-edit-field t)
  4474. (org-table-justify-field-maybe)
  4475. (call-interactively 'org-table-next-field)))))
  4476. ((eq arg t) ;; Global cycling
  4477. (cond
  4478. ((and (eq last-command this-command)
  4479. (eq org-cycle-global-status 'overview))
  4480. ;; We just created the overview - now do table of contents
  4481. ;; This can be slow in very large buffers, so indicate action
  4482. (message "CONTENTS...")
  4483. (org-content)
  4484. (message "CONTENTS...done")
  4485. (setq org-cycle-global-status 'contents)
  4486. (run-hook-with-args 'org-cycle-hook 'contents))
  4487. ((and (eq last-command this-command)
  4488. (eq org-cycle-global-status 'contents))
  4489. ;; We just showed the table of contents - now show everything
  4490. (show-all)
  4491. (message "SHOW ALL")
  4492. (setq org-cycle-global-status 'all)
  4493. (run-hook-with-args 'org-cycle-hook 'all))
  4494. (t
  4495. ;; Default action: go to overview
  4496. (org-overview)
  4497. (message "OVERVIEW")
  4498. (setq org-cycle-global-status 'overview)
  4499. (run-hook-with-args 'org-cycle-hook 'overview))))
  4500. ((and org-drawers org-drawer-regexp
  4501. (save-excursion
  4502. (beginning-of-line 1)
  4503. (looking-at org-drawer-regexp)))
  4504. ;; Toggle block visibility
  4505. (org-flag-drawer
  4506. (not (get-char-property (match-end 0) 'invisible))))
  4507. ((integerp arg)
  4508. ;; Show-subtree, ARG levels up from here.
  4509. (save-excursion
  4510. (org-back-to-heading)
  4511. (outline-up-heading (if (< arg 0) (- arg)
  4512. (- (funcall outline-level) arg)))
  4513. (org-show-subtree)))
  4514. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4515. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4516. ;; At a heading: rotate between three different views
  4517. (org-back-to-heading)
  4518. (let ((goal-column 0) eoh eol eos)
  4519. ;; First, some boundaries
  4520. (save-excursion
  4521. (org-back-to-heading)
  4522. (save-excursion
  4523. (beginning-of-line 2)
  4524. (while (and (not (eobp)) ;; this is like `next-line'
  4525. (get-char-property (1- (point)) 'invisible))
  4526. (beginning-of-line 2)) (setq eol (point)))
  4527. (outline-end-of-heading) (setq eoh (point))
  4528. (org-end-of-subtree t)
  4529. (unless (eobp)
  4530. (skip-chars-forward " \t\n")
  4531. (beginning-of-line 1) ; in case this is an item
  4532. )
  4533. (setq eos (1- (point))))
  4534. ;; Find out what to do next and set `this-command'
  4535. (cond
  4536. ((= eos eoh)
  4537. ;; Nothing is hidden behind this heading
  4538. (message "EMPTY ENTRY")
  4539. (setq org-cycle-subtree-status nil)
  4540. (save-excursion
  4541. (goto-char eos)
  4542. (outline-next-heading)
  4543. (if (org-invisible-p) (org-flag-heading nil))))
  4544. ((or (>= eol eos)
  4545. (not (string-match "\\S-" (buffer-substring eol eos))))
  4546. ;; Entire subtree is hidden in one line: open it
  4547. (org-show-entry)
  4548. (show-children)
  4549. (message "CHILDREN")
  4550. (save-excursion
  4551. (goto-char eos)
  4552. (outline-next-heading)
  4553. (if (org-invisible-p) (org-flag-heading nil)))
  4554. (setq org-cycle-subtree-status 'children)
  4555. (run-hook-with-args 'org-cycle-hook 'children))
  4556. ((and (eq last-command this-command)
  4557. (eq org-cycle-subtree-status 'children))
  4558. ;; We just showed the children, now show everything.
  4559. (org-show-subtree)
  4560. (message "SUBTREE")
  4561. (setq org-cycle-subtree-status 'subtree)
  4562. (run-hook-with-args 'org-cycle-hook 'subtree))
  4563. (t
  4564. ;; Default action: hide the subtree.
  4565. (hide-subtree)
  4566. (message "FOLDED")
  4567. (setq org-cycle-subtree-status 'folded)
  4568. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4569. ;; TAB emulation
  4570. (buffer-read-only (org-back-to-heading))
  4571. ((org-try-cdlatex-tab))
  4572. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4573. (or (not (bolp))
  4574. (not (looking-at outline-regexp))))
  4575. (call-interactively (global-key-binding "\t")))
  4576. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4577. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4578. (or (and (eq org-cycle-emulate-tab 'white)
  4579. (= (match-end 0) (point-at-eol)))
  4580. (and (eq org-cycle-emulate-tab 'whitestart)
  4581. (>= (match-end 0) pos))))
  4582. t
  4583. (eq org-cycle-emulate-tab t))
  4584. (if (and (looking-at "[ \n\r\t]")
  4585. (string-match "^[ \t]*$" (buffer-substring
  4586. (point-at-bol) (point))))
  4587. (progn
  4588. (beginning-of-line 1)
  4589. (and (looking-at "[ \t]+") (replace-match ""))))
  4590. (call-interactively (global-key-binding "\t")))
  4591. (t (save-excursion
  4592. (org-back-to-heading)
  4593. (org-cycle))))))
  4594. ;;;###autoload
  4595. (defun org-global-cycle (&optional arg)
  4596. "Cycle the global visibility. For details see `org-cycle'."
  4597. (interactive "P")
  4598. (let ((org-cycle-include-plain-lists
  4599. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4600. (if (integerp arg)
  4601. (progn
  4602. (show-all)
  4603. (hide-sublevels arg)
  4604. (setq org-cycle-global-status 'contents))
  4605. (org-cycle '(4)))))
  4606. (defun org-overview ()
  4607. "Switch to overview mode, shoing only top-level headlines.
  4608. Really, this shows all headlines with level equal or greater than the level
  4609. of the first headline in the buffer. This is important, because if the
  4610. first headline is not level one, then (hide-sublevels 1) gives confusing
  4611. results."
  4612. (interactive)
  4613. (let ((level (save-excursion
  4614. (goto-char (point-min))
  4615. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4616. (progn
  4617. (goto-char (match-beginning 0))
  4618. (funcall outline-level))))))
  4619. (and level (hide-sublevels level))))
  4620. (defun org-content (&optional arg)
  4621. "Show all headlines in the buffer, like a table of contents.
  4622. With numerical argument N, show content up to level N."
  4623. (interactive "P")
  4624. (save-excursion
  4625. ;; Visit all headings and show their offspring
  4626. (and (integerp arg) (org-overview))
  4627. (goto-char (point-max))
  4628. (catch 'exit
  4629. (while (and (progn (condition-case nil
  4630. (outline-previous-visible-heading 1)
  4631. (error (goto-char (point-min))))
  4632. t)
  4633. (looking-at outline-regexp))
  4634. (if (integerp arg)
  4635. (show-children (1- arg))
  4636. (show-branches))
  4637. (if (bobp) (throw 'exit nil))))))
  4638. (defun org-optimize-window-after-visibility-change (state)
  4639. "Adjust the window after a change in outline visibility.
  4640. This function is the default value of the hook `org-cycle-hook'."
  4641. (when (get-buffer-window (current-buffer))
  4642. (cond
  4643. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4644. ; ((eq state 'overview) (org-beginning-of-line))
  4645. ((eq state 'content) nil)
  4646. ((eq state 'all) nil)
  4647. ((eq state 'folded) nil)
  4648. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4649. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4650. (defun org-cycle-show-empty-lines (state)
  4651. "Show empty lines above all visible headlines.
  4652. The region to be covered depends on STATE when called through
  4653. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4654. entire buffer covered. Note that an empty line is only shown if there
  4655. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4656. (when (> org-cycle-separator-lines 0)
  4657. (save-excursion
  4658. (let* ((n org-cycle-separator-lines)
  4659. (re (cond
  4660. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4661. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4662. (t (let ((ns (number-to-string (- n 2))))
  4663. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4664. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4665. beg end)
  4666. (cond
  4667. ((memq state '(overview contents t))
  4668. (setq beg (point-min) end (point-max)))
  4669. ((memq state '(children folded))
  4670. (setq beg (point) end (progn (org-end-of-subtree t t)
  4671. (beginning-of-line 2)
  4672. (point)))))
  4673. (when beg
  4674. (goto-char beg)
  4675. (while (re-search-forward re end t)
  4676. (if (not (get-char-property (match-end 1) 'invisible))
  4677. (outline-flag-region
  4678. (match-beginning 1) (match-end 1) nil)))))))
  4679. ;; Never hide empty lines at the end of the file.
  4680. (save-excursion
  4681. (goto-char (point-max))
  4682. (outline-previous-heading)
  4683. (outline-end-of-heading)
  4684. (if (and (looking-at "[ \t\n]+")
  4685. (= (match-end 0) (point-max)))
  4686. (outline-flag-region (point) (match-end 0) nil))))
  4687. (defun org-subtree-end-visible-p ()
  4688. "Is the end of the current subtree visible?"
  4689. (pos-visible-in-window-p
  4690. (save-excursion (org-end-of-subtree t) (point))))
  4691. (defun org-first-headline-recenter (&optional N)
  4692. "Move cursor to the first headline and recenter the headline.
  4693. Optional argument N means, put the headline into the Nth line of the window."
  4694. (goto-char (point-min))
  4695. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4696. (beginning-of-line)
  4697. (recenter (prefix-numeric-value N))))
  4698. ;;; Org-goto
  4699. (defvar org-goto-window-configuration nil)
  4700. (defvar org-goto-marker nil)
  4701. (defvar org-goto-map
  4702. (let ((map (make-sparse-keymap)))
  4703. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4704. (while (setq cmd (pop cmds))
  4705. (substitute-key-definition cmd cmd map global-map)))
  4706. (suppress-keymap map)
  4707. (org-defkey map "\C-m" 'org-goto-ret)
  4708. (org-defkey map [(left)] 'org-goto-left)
  4709. (org-defkey map [(right)] 'org-goto-right)
  4710. (org-defkey map [(?q)] 'org-goto-quit)
  4711. (org-defkey map [(control ?g)] 'org-goto-quit)
  4712. (org-defkey map "\C-i" 'org-cycle)
  4713. (org-defkey map [(tab)] 'org-cycle)
  4714. (org-defkey map [(down)] 'outline-next-visible-heading)
  4715. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4716. (org-defkey map "n" 'outline-next-visible-heading)
  4717. (org-defkey map "p" 'outline-previous-visible-heading)
  4718. (org-defkey map "f" 'outline-forward-same-level)
  4719. (org-defkey map "b" 'outline-backward-same-level)
  4720. (org-defkey map "u" 'outline-up-heading)
  4721. (org-defkey map "/" 'org-occur)
  4722. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4723. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4724. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4725. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4726. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4727. map))
  4728. (defconst org-goto-help
  4729. "Browse copy of buffer to find location or copy text.
  4730. RET=jump to location [Q]uit and return to previous location
  4731. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur"
  4732. )
  4733. (defun org-goto ()
  4734. "Look up a different location in the current file, keeping current visibility.
  4735. When you want look-up or go to a different location in a document, the
  4736. fastest way is often to fold the entire buffer and then dive into the tree.
  4737. This method has the disadvantage, that the previous location will be folded,
  4738. which may not be what you want.
  4739. This command works around this by showing a copy of the current buffer
  4740. in an indirect buffer, in overview mode. You can dive into the tree in
  4741. that copy, use org-occur and incremental search to find a location.
  4742. When pressing RET or `Q', the command returns to the original buffer in
  4743. which the visibility is still unchanged. After RET is will also jump to
  4744. the location selected in the indirect buffer and expose the
  4745. the headline hierarchy above."
  4746. (interactive)
  4747. (let* ((org-goto-start-pos (point))
  4748. (selected-point
  4749. (car (org-get-location (current-buffer) org-goto-help))))
  4750. (if selected-point
  4751. (progn
  4752. (org-mark-ring-push org-goto-start-pos)
  4753. (goto-char selected-point)
  4754. (if (or (org-invisible-p) (org-invisible-p2))
  4755. (org-show-context 'org-goto)))
  4756. (message "Quit"))))
  4757. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4758. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4759. (defun org-get-location (buf help)
  4760. "Let the user select a location in the Org-mode buffer BUF.
  4761. This function uses a recursive edit. It returns the selected position
  4762. or nil."
  4763. (let (org-goto-selected-point org-goto-exit-command)
  4764. (save-excursion
  4765. (save-window-excursion
  4766. (delete-other-windows)
  4767. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4768. (switch-to-buffer
  4769. (condition-case nil
  4770. (make-indirect-buffer (current-buffer) "*org-goto*")
  4771. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4772. (with-output-to-temp-buffer "*Help*"
  4773. (princ help))
  4774. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4775. (setq buffer-read-only nil)
  4776. (let ((org-startup-truncated t)
  4777. (org-startup-folded nil)
  4778. (org-startup-align-all-tables nil))
  4779. (org-mode)
  4780. (org-overview))
  4781. (setq buffer-read-only t)
  4782. (if (and (boundp 'org-goto-start-pos)
  4783. (integer-or-marker-p org-goto-start-pos))
  4784. (let ((org-show-hierarchy-above t)
  4785. (org-show-siblings t)
  4786. (org-show-following-heading t))
  4787. (goto-char org-goto-start-pos)
  4788. (and (org-invisible-p) (org-show-context)))
  4789. (goto-char (point-min)))
  4790. (org-beginning-of-line)
  4791. (message "Select location and press RET")
  4792. ;; now we make sure that during selection, ony very few keys work
  4793. ;; and that it is impossible to switch to another window.
  4794. ; (let ((gm (current-global-map))
  4795. ; (overriding-local-map org-goto-map))
  4796. ; (unwind-protect
  4797. ; (progn
  4798. ; (use-global-map org-goto-map)
  4799. ; (recursive-edit))
  4800. ; (use-global-map gm)))
  4801. (use-local-map org-goto-map)
  4802. (recursive-edit)
  4803. ))
  4804. (kill-buffer "*org-goto*")
  4805. (cons org-goto-selected-point org-goto-exit-command)))
  4806. (defun org-goto-ret (&optional arg)
  4807. "Finish `org-goto' by going to the new location."
  4808. (interactive "P")
  4809. (setq org-goto-selected-point (point)
  4810. org-goto-exit-command 'return)
  4811. (throw 'exit nil))
  4812. (defun org-goto-left ()
  4813. "Finish `org-goto' by going to the new location."
  4814. (interactive)
  4815. (if (org-on-heading-p)
  4816. (progn
  4817. (beginning-of-line 1)
  4818. (setq org-goto-selected-point (point)
  4819. org-goto-exit-command 'left)
  4820. (throw 'exit nil))
  4821. (error "Not on a heading")))
  4822. (defun org-goto-right ()
  4823. "Finish `org-goto' by going to the new location."
  4824. (interactive)
  4825. (if (org-on-heading-p)
  4826. (progn
  4827. (setq org-goto-selected-point (point)
  4828. org-goto-exit-command 'right)
  4829. (throw 'exit nil))
  4830. (error "Not on a heading")))
  4831. (defun org-goto-quit ()
  4832. "Finish `org-goto' without cursor motion."
  4833. (interactive)
  4834. (setq org-goto-selected-point nil)
  4835. (setq org-goto-exit-command 'quit)
  4836. (throw 'exit nil))
  4837. ;;; Indirect buffer display of subtrees
  4838. (defvar org-indirect-dedicated-frame nil
  4839. "This is the frame being used for indirect tree display.")
  4840. (defvar org-last-indirect-buffer nil)
  4841. (defun org-tree-to-indirect-buffer (&optional arg)
  4842. "Create indirect buffer and narrow it to current subtree.
  4843. With numerical prefix ARG, go up to this level and then take that tree.
  4844. If ARG is negative, go up that many levels.
  4845. Normally this command removes the indirect buffer previously made
  4846. with this command. However, when called with a C-u prefix, the last buffer
  4847. is kept so that you can work with several indirect buffers at the same time.
  4848. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4849. requests that a new frame be made for the new buffer, so that the dedicated
  4850. frame is not changed."
  4851. (interactive "P")
  4852. (let ((cbuf (current-buffer))
  4853. (cwin (selected-window))
  4854. (pos (point))
  4855. beg end level heading ibuf)
  4856. (save-excursion
  4857. (org-back-to-heading t)
  4858. (when (numberp arg)
  4859. (setq level (org-outline-level))
  4860. (if (< arg 0) (setq arg (+ level arg)))
  4861. (while (> (setq level (org-outline-level)) arg)
  4862. (outline-up-heading 1 t)))
  4863. (setq beg (point)
  4864. heading (org-get-heading))
  4865. (org-end-of-subtree t) (setq end (point)))
  4866. (if (and (not arg)
  4867. (buffer-live-p org-last-indirect-buffer))
  4868. (kill-buffer org-last-indirect-buffer))
  4869. (setq ibuf (org-get-indirect-buffer cbuf)
  4870. org-last-indirect-buffer ibuf)
  4871. (cond
  4872. ((or (eq org-indirect-buffer-display 'new-frame)
  4873. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4874. (select-frame (make-frame))
  4875. (delete-other-windows)
  4876. (switch-to-buffer ibuf)
  4877. (org-set-frame-title heading))
  4878. ((eq org-indirect-buffer-display 'dedicated-frame)
  4879. (raise-frame
  4880. (select-frame (or (and org-indirect-dedicated-frame
  4881. (frame-live-p org-indirect-dedicated-frame)
  4882. org-indirect-dedicated-frame)
  4883. (setq org-indirect-dedicated-frame (make-frame)))))
  4884. (delete-other-windows)
  4885. (switch-to-buffer ibuf)
  4886. (org-set-frame-title (concat "Indirect: " heading)))
  4887. ((eq org-indirect-buffer-display 'current-window)
  4888. (switch-to-buffer ibuf))
  4889. ((eq org-indirect-buffer-display 'other-window)
  4890. (pop-to-buffer ibuf))
  4891. (t (error "Invalid value.")))
  4892. (if (featurep 'xemacs)
  4893. (save-excursion (org-mode) (turn-on-font-lock)))
  4894. (narrow-to-region beg end)
  4895. (show-all)
  4896. (goto-char pos)
  4897. (and (window-live-p cwin) (select-window cwin))))
  4898. (defun org-get-indirect-buffer (&optional buffer)
  4899. (setq buffer (or buffer (current-buffer)))
  4900. (let ((n 1) (base (buffer-name buffer)) bname)
  4901. (while (buffer-live-p
  4902. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4903. (setq n (1+ n)))
  4904. (condition-case nil
  4905. (make-indirect-buffer buffer bname 'clone)
  4906. (error (make-indirect-buffer buffer bname)))))
  4907. (defun org-set-frame-title (title)
  4908. "Set the title of the current frame to the string TITLE."
  4909. ;; FIXME: how to name a single frame in XEmacs???
  4910. (unless (featurep 'xemacs)
  4911. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4912. ;;;; Structure editing
  4913. ;;; Inserting headlines
  4914. (defun org-insert-heading (&optional force-heading)
  4915. "Insert a new heading or item with same depth at point.
  4916. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4917. If point is at the beginning of a headline, insert a sibling before the
  4918. current headline. If point is in the middle of a headline, split the headline
  4919. at that position and make the rest of the headline part of the sibling below
  4920. the current headline."
  4921. (interactive "P")
  4922. (if (= (buffer-size) 0)
  4923. (insert "\n* ")
  4924. (when (or force-heading (not (org-insert-item)))
  4925. (let* ((head (save-excursion
  4926. (condition-case nil
  4927. (progn
  4928. (org-back-to-heading)
  4929. (match-string 0))
  4930. (error "*"))))
  4931. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4932. pos)
  4933. (cond
  4934. ((and (org-on-heading-p) (bolp)
  4935. (or (bobp)
  4936. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4937. (open-line (if blank 2 1)))
  4938. ((and (bolp)
  4939. (or (bobp)
  4940. (save-excursion
  4941. (backward-char 1) (not (org-invisible-p)))))
  4942. nil)
  4943. (t (newline (if blank 2 1))))
  4944. (insert head) (just-one-space)
  4945. (setq pos (point))
  4946. (end-of-line 1)
  4947. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4948. (run-hooks 'org-insert-heading-hook)))))
  4949. (defun org-insert-heading-after-current ()
  4950. "Insert a new heading with same level as current, after current subtree."
  4951. (interactive)
  4952. (org-back-to-heading)
  4953. (org-insert-heading)
  4954. (org-move-subtree-down)
  4955. (end-of-line 1))
  4956. (defun org-insert-todo-heading (arg)
  4957. "Insert a new heading with the same level and TODO state as current heading.
  4958. If the heading has no TODO state, or if the state is DONE, use the first
  4959. state (TODO by default). Also with prefix arg, force first state."
  4960. (interactive "P")
  4961. (when (not (org-insert-item 'checkbox))
  4962. (org-insert-heading)
  4963. (save-excursion
  4964. (org-back-to-heading)
  4965. (outline-previous-heading)
  4966. (looking-at org-todo-line-regexp))
  4967. (if (or arg
  4968. (not (match-beginning 2))
  4969. (member (match-string 2) org-done-keywords))
  4970. (insert (car org-todo-keywords-1) " ")
  4971. (insert (match-string 2) " "))))
  4972. (defun org-insert-subheading (arg)
  4973. "Insert a new subheading and demote it.
  4974. Works for outline headings and for plain lists alike."
  4975. (interactive "P")
  4976. (org-insert-heading arg)
  4977. (cond
  4978. ((org-on-heading-p) (org-do-demote))
  4979. ((org-at-item-p) (org-indent-item 1))))
  4980. (defun org-insert-todo-subheading (arg)
  4981. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4982. Works for outline headings and for plain lists alike."
  4983. (interactive "P")
  4984. (org-insert-todo-heading arg)
  4985. (cond
  4986. ((org-on-heading-p) (org-do-demote))
  4987. ((org-at-item-p) (org-indent-item 1))))
  4988. ;;; Promotion and Demotion
  4989. (defun org-promote-subtree ()
  4990. "Promote the entire subtree.
  4991. See also `org-promote'."
  4992. (interactive)
  4993. (save-excursion
  4994. (org-map-tree 'org-promote))
  4995. (org-fix-position-after-promote))
  4996. (defun org-demote-subtree ()
  4997. "Demote the entire subtree. See `org-demote'.
  4998. See also `org-promote'."
  4999. (interactive)
  5000. (save-excursion
  5001. (org-map-tree 'org-demote))
  5002. (org-fix-position-after-promote))
  5003. (defun org-do-promote ()
  5004. "Promote the current heading higher up the tree.
  5005. If the region is active in `transient-mark-mode', promote all headings
  5006. in the region."
  5007. (interactive)
  5008. (save-excursion
  5009. (if (org-region-active-p)
  5010. (org-map-region 'org-promote (region-beginning) (region-end))
  5011. (org-promote)))
  5012. (org-fix-position-after-promote))
  5013. (defun org-do-demote ()
  5014. "Demote the current heading lower down the tree.
  5015. If the region is active in `transient-mark-mode', demote all headings
  5016. in the region."
  5017. (interactive)
  5018. (save-excursion
  5019. (if (org-region-active-p)
  5020. (org-map-region 'org-demote (region-beginning) (region-end))
  5021. (org-demote)))
  5022. (org-fix-position-after-promote))
  5023. (defun org-fix-position-after-promote ()
  5024. "Make sure that after pro/demotion cursor position is right."
  5025. (let ((pos (point)))
  5026. (when (save-excursion
  5027. (beginning-of-line 1)
  5028. (looking-at org-todo-line-regexp)
  5029. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5030. (cond ((eobp) (insert " "))
  5031. ((eolp) (insert " "))
  5032. ((equal (char-after) ?\ ) (forward-char 1))))))
  5033. (defun org-reduced-level (l)
  5034. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5035. (defun org-get-legal-level (level &optional change)
  5036. "Rectify a level change under the influence of `org-odd-levels-only'
  5037. LEVEL is a current level, CHANGE is by how much the level should be
  5038. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5039. even level numbers will become the next higher odd number."
  5040. (if org-odd-levels-only
  5041. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5042. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5043. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5044. (max 1 (+ level change))))
  5045. (defun org-promote ()
  5046. "Promote the current heading higher up the tree.
  5047. If the region is active in `transient-mark-mode', promote all headings
  5048. in the region."
  5049. (org-back-to-heading t)
  5050. (let* ((level (save-match-data (funcall outline-level)))
  5051. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5052. (diff (abs (- level (length up-head) -1))))
  5053. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5054. (replace-match up-head nil t)
  5055. ;; Fixup tag positioning
  5056. (and org-auto-align-tags (org-set-tags nil t))
  5057. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5058. (defun org-demote ()
  5059. "Demote the current heading lower down the tree.
  5060. If the region is active in `transient-mark-mode', demote all headings
  5061. in the region."
  5062. (org-back-to-heading t)
  5063. (let* ((level (save-match-data (funcall outline-level)))
  5064. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5065. (diff (abs (- level (length down-head) -1))))
  5066. (replace-match down-head nil t)
  5067. ;; Fixup tag positioning
  5068. (and org-auto-align-tags (org-set-tags nil t))
  5069. (if org-adapt-indentation (org-fixup-indentation diff))))
  5070. (defun org-map-tree (fun)
  5071. "Call FUN for every heading underneath the current one."
  5072. (org-back-to-heading)
  5073. (let ((level (funcall outline-level)))
  5074. (save-excursion
  5075. (funcall fun)
  5076. (while (and (progn
  5077. (outline-next-heading)
  5078. (> (funcall outline-level) level))
  5079. (not (eobp)))
  5080. (funcall fun)))))
  5081. (defun org-map-region (fun beg end)
  5082. "Call FUN for every heading between BEG and END."
  5083. (let ((org-ignore-region t))
  5084. (save-excursion
  5085. (setq end (copy-marker end))
  5086. (goto-char beg)
  5087. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5088. (< (point) end))
  5089. (funcall fun))
  5090. (while (and (progn
  5091. (outline-next-heading)
  5092. (< (point) end))
  5093. (not (eobp)))
  5094. (funcall fun)))))
  5095. (defun org-fixup-indentation (diff)
  5096. "Change the indentation in the current entry by DIFF
  5097. However, if any line in the current entry has no indentation, or if it
  5098. would end up with no indentation after the change, nothing at all is done."
  5099. (save-excursion
  5100. (let ((end (save-excursion (outline-next-heading)
  5101. (point-marker)))
  5102. (prohibit (if (> diff 0)
  5103. "^\\S-"
  5104. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5105. col)
  5106. (unless (save-excursion (end-of-line 1)
  5107. (re-search-forward prohibit end t))
  5108. (while (re-search-forward "^[ \t]+" end t)
  5109. (goto-char (match-end 0))
  5110. (setq col (current-column))
  5111. (if (< diff 0) (replace-match ""))
  5112. (indent-to (+ diff col))))
  5113. (move-marker end nil))))
  5114. (defun org-convert-to-odd-levels ()
  5115. "Convert an org-mode file with all levels allowed to one with odd levels.
  5116. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5117. level 5 etc."
  5118. (interactive)
  5119. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5120. (let ((org-odd-levels-only nil) n)
  5121. (save-excursion
  5122. (goto-char (point-min))
  5123. (while (re-search-forward "^\\*\\*+ " nil t)
  5124. (setq n (- (length (match-string 0)) 2))
  5125. (while (>= (setq n (1- n)) 0)
  5126. (org-demote))
  5127. (end-of-line 1))))))
  5128. (defun org-convert-to-oddeven-levels ()
  5129. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5130. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5131. section with an even level, conversion would destroy the structure of the file. An error
  5132. is signaled in this case."
  5133. (interactive)
  5134. (goto-char (point-min))
  5135. ;; First check if there are no even levels
  5136. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5137. (org-show-context t)
  5138. (error "Not all levels are odd in this file. Conversion not possible."))
  5139. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5140. (let ((org-odd-levels-only nil) n)
  5141. (save-excursion
  5142. (goto-char (point-min))
  5143. (while (re-search-forward "^\\*\\*+ " nil t)
  5144. (setq n (/ (length (1- (match-string 0))) 2))
  5145. (while (>= (setq n (1- n)) 0)
  5146. (org-promote))
  5147. (end-of-line 1))))))
  5148. (defun org-tr-level (n)
  5149. "Make N odd if required."
  5150. (if org-odd-levels-only (1+ (/ n 2)) n))
  5151. ;;; Vertical tree motion, cutting and pasting of subtrees
  5152. (defun org-move-subtree-up (&optional arg)
  5153. "Move the current subtree up past ARG headlines of the same level."
  5154. (interactive "p")
  5155. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5156. (defun org-move-subtree-down (&optional arg)
  5157. "Move the current subtree down past ARG headlines of the same level."
  5158. (interactive "p")
  5159. (setq arg (prefix-numeric-value arg))
  5160. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5161. 'outline-get-last-sibling))
  5162. (ins-point (make-marker))
  5163. (cnt (abs arg))
  5164. beg end txt folded)
  5165. ;; Select the tree
  5166. (org-back-to-heading)
  5167. (setq beg (point))
  5168. (save-match-data
  5169. (save-excursion (outline-end-of-heading)
  5170. (setq folded (org-invisible-p)))
  5171. (outline-end-of-subtree))
  5172. (outline-next-heading)
  5173. (setq end (point))
  5174. ;; Find insertion point, with error handling
  5175. (goto-char beg)
  5176. (while (> cnt 0)
  5177. (or (and (funcall movfunc) (looking-at outline-regexp))
  5178. (progn (goto-char beg)
  5179. (error "Cannot move past superior level or buffer limit")))
  5180. (setq cnt (1- cnt)))
  5181. (if (> arg 0)
  5182. ;; Moving forward - still need to move over subtree
  5183. (progn (outline-end-of-subtree)
  5184. (outline-next-heading)
  5185. (if (not (or (looking-at (concat "^" outline-regexp))
  5186. (bolp)))
  5187. (newline))))
  5188. (move-marker ins-point (point))
  5189. (setq txt (buffer-substring beg end))
  5190. (delete-region beg end)
  5191. (insert txt)
  5192. (or (bolp) (insert "\n"))
  5193. (goto-char ins-point)
  5194. (if folded (hide-subtree))
  5195. (move-marker ins-point nil)))
  5196. (defvar org-subtree-clip ""
  5197. "Clipboard for cut and paste of subtrees.
  5198. This is actually only a copy of the kill, because we use the normal kill
  5199. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5200. (defvar org-subtree-clip-folded nil
  5201. "Was the last copied subtree folded?
  5202. This is used to fold the tree back after pasting.")
  5203. (defun org-cut-subtree (&optional n)
  5204. "Cut the current subtree into the clipboard.
  5205. With prefix arg N, cut this many sequential subtrees.
  5206. This is a short-hand for marking the subtree and then cutting it."
  5207. (interactive "p")
  5208. (org-copy-subtree n 'cut))
  5209. (defun org-copy-subtree (&optional n cut)
  5210. "Cut the current subtree into the clipboard.
  5211. With prefix arg N, cut this many sequential subtrees.
  5212. This is a short-hand for marking the subtree and then copying it.
  5213. If CUT is non-nil, actually cut the subtree."
  5214. (interactive "p")
  5215. (let (beg end folded)
  5216. (if (interactive-p)
  5217. (org-back-to-heading nil) ; take what looks like a subtree
  5218. (org-back-to-heading t)) ; take what is really there
  5219. (setq beg (point))
  5220. (save-match-data
  5221. (save-excursion (outline-end-of-heading)
  5222. (setq folded (org-invisible-p)))
  5223. (condition-case nil
  5224. (outline-forward-same-level (1- n))
  5225. (error nil))
  5226. (org-end-of-subtree t t))
  5227. (setq end (point))
  5228. (goto-char beg)
  5229. (when (> end beg)
  5230. (setq org-subtree-clip-folded folded)
  5231. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5232. (setq org-subtree-clip (current-kill 0))
  5233. (message "%s: Subtree(s) with %d characters"
  5234. (if cut "Cut" "Copied")
  5235. (length org-subtree-clip)))))
  5236. (defun org-paste-subtree (&optional level tree)
  5237. "Paste the clipboard as a subtree, with modification of headline level.
  5238. The entire subtree is promoted or demoted in order to match a new headline
  5239. level. By default, the new level is derived from the visible headings
  5240. before and after the insertion point, and taken to be the inferior headline
  5241. level of the two. So if the previous visible heading is level 3 and the
  5242. next is level 4 (or vice versa), level 4 will be used for insertion.
  5243. This makes sure that the subtree remains an independent subtree and does
  5244. not swallow low level entries.
  5245. You can also force a different level, either by using a numeric prefix
  5246. argument, or by inserting the heading marker by hand. For example, if the
  5247. cursor is after \"*****\", then the tree will be shifted to level 5.
  5248. If you want to insert the tree as is, just use \\[yank].
  5249. If optional TREE is given, use this text instead of the kill ring."
  5250. (interactive "P")
  5251. (unless (org-kill-is-subtree-p tree)
  5252. (error
  5253. (substitute-command-keys
  5254. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5255. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5256. (^re (concat "^\\(" outline-regexp "\\)"))
  5257. (re (concat "\\(" outline-regexp "\\)"))
  5258. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5259. (old-level (if (string-match ^re txt)
  5260. (- (match-end 0) (match-beginning 0) 1)
  5261. -1))
  5262. (force-level (cond (level (prefix-numeric-value level))
  5263. ((string-match
  5264. ^re_ (buffer-substring (point-at-bol) (point)))
  5265. (- (match-end 1) (match-beginning 1)))
  5266. (t nil)))
  5267. (previous-level (save-excursion
  5268. (condition-case nil
  5269. (progn
  5270. (outline-previous-visible-heading 1)
  5271. (if (looking-at re)
  5272. (- (match-end 0) (match-beginning 0) 1)
  5273. 1))
  5274. (error 1))))
  5275. (next-level (save-excursion
  5276. (condition-case nil
  5277. (progn
  5278. (or (looking-at outline-regexp)
  5279. (outline-next-visible-heading 1))
  5280. (if (looking-at re)
  5281. (- (match-end 0) (match-beginning 0) 1)
  5282. 1))
  5283. (error 1))))
  5284. (new-level (or force-level (max previous-level next-level)))
  5285. (shift (if (or (= old-level -1)
  5286. (= new-level -1)
  5287. (= old-level new-level))
  5288. 0
  5289. (- new-level old-level)))
  5290. (shift1 shift)
  5291. (delta (if (> shift 0) -1 1))
  5292. (func (if (> shift 0) 'org-demote 'org-promote))
  5293. (org-odd-levels-only nil)
  5294. beg end)
  5295. ;; Remove the forced level indicator
  5296. (if force-level
  5297. (delete-region (point-at-bol) (point)))
  5298. ;; Paste
  5299. (beginning-of-line 1)
  5300. (setq beg (point))
  5301. (insert txt)
  5302. (unless (string-match "\n[ \t]*\\'" txt) (insert "\n"))
  5303. (setq end (point))
  5304. (goto-char beg)
  5305. ;; Shift if necessary
  5306. (unless (= shift 0)
  5307. (save-restriction
  5308. (narrow-to-region beg end)
  5309. (while (not (= shift 0))
  5310. (org-map-region func (point-min) (point-max))
  5311. (setq shift (+ delta shift)))
  5312. (goto-char (point-min))))
  5313. (when (interactive-p)
  5314. (message "Clipboard pasted as level %d subtree" new-level))
  5315. (if (and kill-ring
  5316. (eq org-subtree-clip (current-kill 0))
  5317. org-subtree-clip-folded)
  5318. ;; The tree was folded before it was killed/copied
  5319. (hide-subtree))))
  5320. (defun org-kill-is-subtree-p (&optional txt)
  5321. "Check if the current kill is an outline subtree, or a set of trees.
  5322. Returns nil if kill does not start with a headline, or if the first
  5323. headline level is not the largest headline level in the tree.
  5324. So this will actually accept several entries of equal levels as well,
  5325. which is OK for `org-paste-subtree'.
  5326. If optional TXT is given, check this string instead of the current kill."
  5327. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5328. (start-level (and kill
  5329. (string-match (concat "\\`" org-outline-regexp) kill)
  5330. (- (match-end 0) (match-beginning 0) 1)))
  5331. (re (concat "^" org-outline-regexp))
  5332. (start 1))
  5333. (if (not start-level)
  5334. (progn
  5335. nil) ;; does not even start with a heading
  5336. (catch 'exit
  5337. (while (setq start (string-match re kill (1+ start)))
  5338. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5339. (throw 'exit nil)))
  5340. t))))
  5341. (defun org-narrow-to-subtree ()
  5342. "Narrow buffer to the current subtree."
  5343. (interactive)
  5344. (save-excursion
  5345. (narrow-to-region
  5346. (progn (org-back-to-heading) (point))
  5347. (progn (org-end-of-subtree t t) (point)))))
  5348. ;;; Outline Sorting
  5349. (defun org-sort (with-case)
  5350. "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
  5351. (interactive "P")
  5352. (if (org-at-table-p)
  5353. (org-call-with-arg 'org-table-sort-lines with-case)
  5354. (org-call-with-arg 'org-sort-entries with-case)))
  5355. (defun org-sort-entries (&optional with-case sorting-type)
  5356. "Sort entries on a certain level of an outline tree.
  5357. If there is an active region, the entries in the region are sorted.
  5358. Else, if the cursor is before the first entry, sort the top-level items.
  5359. Else, the children of the entry at point are sorted.
  5360. Sorting can be alphabetically, numerically, and by date/time as given by
  5361. the first time stamp in the entry. The command prompts for the sorting
  5362. type unless it has been given to the function through the SORTING-TYPE
  5363. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
  5364. Comparing entries ignores case by default. However, with an optional argument
  5365. WITH-CASE, the sorting considers case as well. With two prefix arguments
  5366. `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
  5367. (interactive "P")
  5368. (let ((unique (equal with-case '(16)))
  5369. start beg end entries stars re re2 p nentries (nremoved 0)
  5370. last txt what)
  5371. ;; Find beginning and end of region to sort
  5372. (cond
  5373. ((org-region-active-p)
  5374. ;; we will sort the region
  5375. (setq end (region-end)
  5376. what "region")
  5377. (goto-char (region-beginning))
  5378. (if (not (org-on-heading-p)) (outline-next-heading))
  5379. (setq start (point)))
  5380. ((or (org-on-heading-p)
  5381. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5382. ;; we will sort the children of the current headline
  5383. (org-back-to-heading)
  5384. (setq start (point) end (org-end-of-subtree) what "children")
  5385. (goto-char start)
  5386. (show-subtree)
  5387. (outline-next-heading))
  5388. (t
  5389. ;; we will sort the top-level entries in this file
  5390. (goto-char (point-min))
  5391. (or (org-on-heading-p) (outline-next-heading))
  5392. (setq start (point) end (point-max) what "top-level")
  5393. (goto-char start)
  5394. (show-all)))
  5395. (setq beg (point))
  5396. (if (>= (point) end) (error "Nothing to sort"))
  5397. (looking-at "\\(\\*+\\)")
  5398. (setq stars (match-string 1)
  5399. re (concat "^" (regexp-quote stars) " +")
  5400. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5401. txt (buffer-substring beg end))
  5402. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5403. (if (and (not (equal stars "*")) (string-match re2 txt))
  5404. (error "Region to sort contains a level above the first entry"))
  5405. ;; Make a list that can be sorted.
  5406. ;; The car is the string for comparison, the cdr is the subtree
  5407. (message "Sorting entries...")
  5408. (setq entries
  5409. (mapcar
  5410. (lambda (x)
  5411. (string-match "^.*\\(\n.*\\)?" x) ; take two lines
  5412. (cons (match-string 0 x) x))
  5413. (org-split-string txt re)))
  5414. ;; Sort the list
  5415. (save-excursion
  5416. (goto-char start)
  5417. (setq entries (org-do-sort entries what with-case sorting-type)))
  5418. ;; Delete the old stuff
  5419. (goto-char beg)
  5420. (kill-region beg end)
  5421. (setq nentries (length entries))
  5422. ;; Insert the sorted entries, and remove duplicates if this is required
  5423. (while (setq p (pop entries))
  5424. (if (and unique (equal last (setq last (org-trim (cdr p)))))
  5425. (setq nremoved (1+ nremoved)) ; same entry as before, skip it
  5426. (insert stars " " (cdr p))))
  5427. (goto-char start)
  5428. (message "Sorting entries...done (%d entries%s)"
  5429. nentries
  5430. (if unique (format ", %d duplicates removed" nremoved) ""))))
  5431. (defvar org-priority-regexp) ; defined later in the file
  5432. (defun org-do-sort (table what &optional with-case sorting-type)
  5433. "Sort TABLE of WHAT according to SORTING-TYPE.
  5434. The user will be prompted for the SORTING-TYPE if the call to this
  5435. function does not specify it. WHAT is only for the prompt, to indicate
  5436. what is being sorted. The sorting key will be extracted from
  5437. the car of the elements of the table.
  5438. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5439. (unless sorting-type
  5440. (message
  5441. "Sort %s: [a]lphabetic. [n]umeric. [t]ime [p]riority. A/N/T/P means reversed:"
  5442. what)
  5443. (setq sorting-type (read-char-exclusive)))
  5444. (let ((dcst (downcase sorting-type))
  5445. extractfun comparefun)
  5446. ;; Define the appropriate functions
  5447. (cond
  5448. ((= dcst ?n)
  5449. (setq extractfun 'string-to-number
  5450. comparefun (if (= dcst sorting-type) '< '>)))
  5451. ((= dcst ?a)
  5452. (setq extractfun (if with-case 'identity 'downcase)
  5453. comparefun (if (= dcst sorting-type)
  5454. 'string<
  5455. (lambda (a b) (and (not (string< a b))
  5456. (not (string= a b)))))))
  5457. ((= dcst ?t)
  5458. (setq extractfun
  5459. (lambda (x)
  5460. (if (string-match org-ts-regexp x)
  5461. (time-to-seconds
  5462. (org-time-string-to-time (match-string 0 x)))
  5463. 0))
  5464. comparefun (if (= dcst sorting-type) '< '>)))
  5465. ((= dcst ?p)
  5466. (setq extractfun
  5467. (lambda (x)
  5468. (if (string-match org-priority-regexp x)
  5469. (string-to-char (match-string 2 x))
  5470. org-default-priority))
  5471. comparefun (if (= dcst sorting-type) '< '>)))
  5472. (t (error "Invalid sorting type `%c'" sorting-type)))
  5473. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5474. table)
  5475. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5476. ;;;; Plain list items, including checkboxes
  5477. ;;; Plain list items
  5478. (defun org-at-item-p ()
  5479. "Is point in a line starting a hand-formatted item?"
  5480. (let ((llt org-plain-list-ordered-item-terminator))
  5481. (save-excursion
  5482. (goto-char (point-at-bol))
  5483. (looking-at
  5484. (cond
  5485. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5486. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5487. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5488. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5489. (defun org-in-item-p ()
  5490. "It the cursor inside a plain list item.
  5491. Does not have to be the first line."
  5492. (save-excursion
  5493. (condition-case nil
  5494. (progn
  5495. (org-beginning-of-item)
  5496. (org-at-item-p)
  5497. t)
  5498. (error nil))))
  5499. (defun org-insert-item (&optional checkbox)
  5500. "Insert a new item at the current level.
  5501. Return t when things worked, nil when we are not in an item."
  5502. (when (save-excursion
  5503. (condition-case nil
  5504. (progn
  5505. (org-beginning-of-item)
  5506. (org-at-item-p)
  5507. (if (org-invisible-p) (error "Invisible item"))
  5508. t)
  5509. (error nil)))
  5510. (let* ((bul (match-string 0))
  5511. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5512. (match-end 0)))
  5513. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5514. pos)
  5515. (cond
  5516. ((and (org-at-item-p) (<= (point) eow))
  5517. ;; before the bullet
  5518. (beginning-of-line 1)
  5519. (open-line (if blank 2 1)))
  5520. ((<= (point) eow)
  5521. (beginning-of-line 1))
  5522. (t (newline (if blank 2 1))))
  5523. (insert bul (if checkbox "[ ]" ""))
  5524. (just-one-space)
  5525. (setq pos (point))
  5526. (end-of-line 1)
  5527. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5528. (org-maybe-renumber-ordered-list)
  5529. (and checkbox (org-update-checkbox-count-maybe))
  5530. t))
  5531. ;;; Checkboxes
  5532. (defun org-at-item-checkbox-p ()
  5533. "Is point at a line starting a plain-list item with a checklet?"
  5534. (and (org-at-item-p)
  5535. (save-excursion
  5536. (goto-char (match-end 0))
  5537. (skip-chars-forward " \t")
  5538. (looking-at "\\[[- X]\\]"))))
  5539. (defun org-toggle-checkbox (&optional arg)
  5540. "Toggle the checkbox in the current line."
  5541. (interactive "P")
  5542. (catch 'exit
  5543. (let (beg end status (firstnew 'unknown))
  5544. (cond
  5545. ((org-region-active-p)
  5546. (setq beg (region-beginning) end (region-end)))
  5547. ((org-on-heading-p)
  5548. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5549. ((org-at-item-checkbox-p)
  5550. (save-excursion
  5551. (replace-match
  5552. (cond (arg "[-]")
  5553. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5554. (t "[ ]"))
  5555. t t))
  5556. (throw 'exit t))
  5557. (t (error "Not at a checkbox or heading, and no active region")))
  5558. (save-excursion
  5559. (goto-char beg)
  5560. (while (< (point) end)
  5561. (when (org-at-item-checkbox-p)
  5562. (setq status (equal (match-string 0) "[X]"))
  5563. (when (eq firstnew 'unknown)
  5564. (setq firstnew (not status)))
  5565. (replace-match
  5566. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5567. (beginning-of-line 2)))))
  5568. (org-update-checkbox-count-maybe))
  5569. (defun org-update-checkbox-count-maybe ()
  5570. "Update checkbox statistics unless turned off by user."
  5571. (when org-provide-checkbox-statistics
  5572. (org-update-checkbox-count)))
  5573. (defun org-update-checkbox-count (&optional all)
  5574. "Update the checkbox statistics in the current section.
  5575. This will find all statistic cookies like [57%] and [6/12] and update them
  5576. with the current numbers. With optional prefix argument ALL, do this for
  5577. the whole buffer."
  5578. (interactive "P")
  5579. (save-excursion
  5580. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5581. (beg (condition-case nil
  5582. (progn (outline-back-to-heading) (point))
  5583. (error (point-min))))
  5584. (end (move-marker (make-marker)
  5585. (progn (outline-next-heading) (point))))
  5586. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5587. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5588. b1 e1 f1 c-on c-off lim (cstat 0))
  5589. (when all
  5590. (goto-char (point-min))
  5591. (outline-next-heading)
  5592. (setq beg (point) end (point-max)))
  5593. (goto-char beg)
  5594. (while (re-search-forward re end t)
  5595. (setq cstat (1+ cstat)
  5596. b1 (match-beginning 0)
  5597. e1 (match-end 0)
  5598. f1 (match-beginning 1)
  5599. lim (cond
  5600. ((org-on-heading-p) (outline-next-heading) (point))
  5601. ((org-at-item-p) (org-end-of-item) (point))
  5602. (t nil))
  5603. c-on 0 c-off 0)
  5604. (goto-char e1)
  5605. (when lim
  5606. (while (re-search-forward re-box lim t)
  5607. (if (member (match-string 2) '("[ ]" "[-]"))
  5608. (setq c-off (1+ c-off))
  5609. (setq c-on (1+ c-on))))
  5610. ; (delete-region b1 e1)
  5611. (goto-char b1)
  5612. (insert (if f1
  5613. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5614. (format "[%d/%d]" c-on (+ c-on c-off))))
  5615. (and (looking-at "\\[.*?\\]")
  5616. (replace-match ""))))
  5617. (when (interactive-p)
  5618. (message "Checkbox satistics updated %s (%d places)"
  5619. (if all "in entire file" "in current outline entry") cstat)))))
  5620. (defun org-get-checkbox-statistics-face ()
  5621. "Select the face for checkbox statistics.
  5622. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5623. it will be `org-todo'."
  5624. (if (match-end 1)
  5625. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5626. (if (and (> (match-end 2) (match-beginning 2))
  5627. (equal (match-string 2) (match-string 3)))
  5628. 'org-done
  5629. 'org-todo)))
  5630. (defun org-get-indentation (&optional line)
  5631. "Get the indentation of the current line, interpreting tabs.
  5632. When LINE is given, assume it represents a line and compute its indentation."
  5633. (if line
  5634. (if (string-match "^ *" (org-remove-tabs line))
  5635. (match-end 0))
  5636. (save-excursion
  5637. (beginning-of-line 1)
  5638. (skip-chars-forward " \t")
  5639. (current-column))))
  5640. (defun org-remove-tabs (s &optional width)
  5641. "Replace tabulators in S with spaces.
  5642. Assumes that s is a single line, starting in column 0."
  5643. (setq width (or width tab-width))
  5644. (while (string-match "\t" s)
  5645. (setq s (replace-match
  5646. (make-string
  5647. (- (* width (/ (+ (match-beginning 0) width) width))
  5648. (match-beginning 0)) ?\ )
  5649. t t s)))
  5650. s)
  5651. (defun org-fix-indentation (line ind)
  5652. "Fix indentation in LINE.
  5653. IND is a cons cell with target and minimum indentation.
  5654. If the current indenation in LINE is smaller than the minimum,
  5655. leave it alone. If it is larger than ind, set it to the target."
  5656. (let* ((l (org-remove-tabs line))
  5657. (i (org-get-indentation l))
  5658. (i1 (car ind)) (i2 (cdr ind)))
  5659. (if (>= i i2) (setq l (substring line i2)))
  5660. (if (> i1 0)
  5661. (concat (make-string i1 ?\ ) l)
  5662. l)))
  5663. (defcustom org-empty-line-terminates-plain-lists nil
  5664. "Non-nil means, an empty line ends all plain list levels.
  5665. When nil, empty lines are part of the preceeding item."
  5666. :group 'org-plain-lists
  5667. :type 'boolean)
  5668. (defun org-beginning-of-item ()
  5669. "Go to the beginning of the current hand-formatted item.
  5670. If the cursor is not in an item, throw an error."
  5671. (interactive)
  5672. (let ((pos (point))
  5673. (limit (save-excursion
  5674. (condition-case nil
  5675. (progn
  5676. (org-back-to-heading)
  5677. (beginning-of-line 2) (point))
  5678. (error (point-min)))))
  5679. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5680. ind ind1)
  5681. (if (org-at-item-p)
  5682. (beginning-of-line 1)
  5683. (beginning-of-line 1)
  5684. (skip-chars-forward " \t")
  5685. (setq ind (current-column))
  5686. (if (catch 'exit
  5687. (while t
  5688. (beginning-of-line 0)
  5689. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5690. (if (looking-at "[ \t]*$")
  5691. (setq ind1 ind-empty)
  5692. (skip-chars-forward " \t")
  5693. (setq ind1 (current-column)))
  5694. (if (< ind1 ind)
  5695. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5696. nil
  5697. (goto-char pos)
  5698. (error "Not in an item")))))
  5699. (defun org-end-of-item ()
  5700. "Go to the end of the current hand-formatted item.
  5701. If the cursor is not in an item, throw an error."
  5702. (interactive)
  5703. (let* ((pos (point))
  5704. ind1
  5705. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5706. (limit (save-excursion (outline-next-heading) (point)))
  5707. (ind (save-excursion
  5708. (org-beginning-of-item)
  5709. (skip-chars-forward " \t")
  5710. (current-column)))
  5711. (end (catch 'exit
  5712. (while t
  5713. (beginning-of-line 2)
  5714. (if (eobp) (throw 'exit (point)))
  5715. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5716. (if (looking-at "[ \t]*$")
  5717. (setq ind1 ind-empty)
  5718. (skip-chars-forward " \t")
  5719. (setq ind1 (current-column)))
  5720. (if (<= ind1 ind)
  5721. (throw 'exit (point-at-bol)))))))
  5722. (if end
  5723. (goto-char end)
  5724. (goto-char pos)
  5725. (error "Not in an item"))))
  5726. (defun org-next-item ()
  5727. "Move to the beginning of the next item in the current plain list.
  5728. Error if not at a plain list, or if this is the last item in the list."
  5729. (interactive)
  5730. (let (ind ind1 (pos (point)))
  5731. (org-beginning-of-item)
  5732. (setq ind (org-get-indentation))
  5733. (org-end-of-item)
  5734. (setq ind1 (org-get-indentation))
  5735. (unless (and (org-at-item-p) (= ind ind1))
  5736. (goto-char pos)
  5737. (error "On last item"))))
  5738. (defun org-previous-item ()
  5739. "Move to the beginning of the previous item in the current plain list.
  5740. Error if not at a plain list, or if this is the first item in the list."
  5741. (interactive)
  5742. (let (beg ind ind1 (pos (point)))
  5743. (org-beginning-of-item)
  5744. (setq beg (point))
  5745. (setq ind (org-get-indentation))
  5746. (goto-char beg)
  5747. (catch 'exit
  5748. (while t
  5749. (beginning-of-line 0)
  5750. (if (looking-at "[ \t]*$")
  5751. nil
  5752. (if (<= (setq ind1 (org-get-indentation)) ind)
  5753. (throw 'exit t)))))
  5754. (condition-case nil
  5755. (if (or (not (org-at-item-p))
  5756. (< ind1 (1- ind)))
  5757. (error "")
  5758. (org-beginning-of-item))
  5759. (error (goto-char pos)
  5760. (error "On first item")))))
  5761. (defun org-move-item-down ()
  5762. "Move the plain list item at point down, i.e. swap with following item.
  5763. Subitems (items with larger indentation) are considered part of the item,
  5764. so this really moves item trees."
  5765. (interactive)
  5766. (let (beg end ind ind1 (pos (point)) txt)
  5767. (org-beginning-of-item)
  5768. (setq beg (point))
  5769. (setq ind (org-get-indentation))
  5770. (org-end-of-item)
  5771. (setq end (point))
  5772. (setq ind1 (org-get-indentation))
  5773. (if (and (org-at-item-p) (= ind ind1))
  5774. (progn
  5775. (org-end-of-item)
  5776. (setq txt (buffer-substring beg end))
  5777. (save-excursion
  5778. (delete-region beg end))
  5779. (setq pos (point))
  5780. (insert txt)
  5781. (goto-char pos)
  5782. (org-maybe-renumber-ordered-list))
  5783. (goto-char pos)
  5784. (error "Cannot move this item further down"))))
  5785. (defun org-move-item-up (arg)
  5786. "Move the plain list item at point up, i.e. swap with previous item.
  5787. Subitems (items with larger indentation) are considered part of the item,
  5788. so this really moves item trees."
  5789. (interactive "p")
  5790. (let (beg end ind ind1 (pos (point)) txt)
  5791. (org-beginning-of-item)
  5792. (setq beg (point))
  5793. (setq ind (org-get-indentation))
  5794. (org-end-of-item)
  5795. (setq end (point))
  5796. (goto-char beg)
  5797. (catch 'exit
  5798. (while t
  5799. (beginning-of-line 0)
  5800. (if (looking-at "[ \t]*$")
  5801. (if org-empty-line-terminates-plain-lists
  5802. (progn
  5803. (goto-char pos)
  5804. (error "Cannot move this item further up"))
  5805. nil)
  5806. (if (<= (setq ind1 (org-get-indentation)) ind)
  5807. (throw 'exit t)))))
  5808. (condition-case nil
  5809. (org-beginning-of-item)
  5810. (error (goto-char beg)
  5811. (error "Cannot move this item further up")))
  5812. (setq ind1 (org-get-indentation))
  5813. (if (and (org-at-item-p) (= ind ind1))
  5814. (progn
  5815. (setq txt (buffer-substring beg end))
  5816. (save-excursion
  5817. (delete-region beg end))
  5818. (setq pos (point))
  5819. (insert txt)
  5820. (goto-char pos)
  5821. (org-maybe-renumber-ordered-list))
  5822. (goto-char pos)
  5823. (error "Cannot move this item further up"))))
  5824. (defun org-maybe-renumber-ordered-list ()
  5825. "Renumber the ordered list at point if setup allows it.
  5826. This tests the user option `org-auto-renumber-ordered-lists' before
  5827. doing the renumbering."
  5828. (interactive)
  5829. (when (and org-auto-renumber-ordered-lists
  5830. (org-at-item-p))
  5831. (if (match-beginning 3)
  5832. (org-renumber-ordered-list 1)
  5833. (org-fix-bullet-type))))
  5834. (defun org-maybe-renumber-ordered-list-safe ()
  5835. (condition-case nil
  5836. (save-excursion
  5837. (org-maybe-renumber-ordered-list))
  5838. (error nil)))
  5839. (defun org-cycle-list-bullet (&optional which)
  5840. "Cycle through the different itemize/enumerate bullets.
  5841. This cycle the entire list level through the sequence:
  5842. `-' -> `+' -> `*' -> `1.' -> `1)'
  5843. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5844. 0 meand `-', 1 means `+' etc."
  5845. (interactive "P")
  5846. (org-preserve-lc
  5847. (org-beginning-of-item-list)
  5848. (org-at-item-p)
  5849. (beginning-of-line 1)
  5850. (let ((current (match-string 0)) new)
  5851. (setq new (cond
  5852. ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5853. ((string-match "-" current) "+")
  5854. ((string-match "\\+" current)
  5855. (if (looking-at "\\S-") "1." "*"))
  5856. ((string-match "\\*" current) "1.")
  5857. ((string-match "\\." current) "1)")
  5858. ((string-match ")" current) "-")
  5859. (t (error "This should not happen"))))
  5860. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5861. (org-fix-bullet-type)
  5862. (org-maybe-renumber-ordered-list))))
  5863. (defun org-get-string-indentation (s)
  5864. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5865. (let ((n -1) (i 0) (w tab-width) c)
  5866. (catch 'exit
  5867. (while (< (setq n (1+ n)) (length s))
  5868. (setq c (aref s n))
  5869. (cond ((= c ?\ ) (setq i (1+ i)))
  5870. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5871. (t (throw 'exit t)))))
  5872. i))
  5873. (defun org-renumber-ordered-list (arg)
  5874. "Renumber an ordered plain list.
  5875. Cursor needs to be in the first line of an item, the line that starts
  5876. with something like \"1.\" or \"2)\"."
  5877. (interactive "p")
  5878. (unless (and (org-at-item-p)
  5879. (match-beginning 3))
  5880. (error "This is not an ordered list"))
  5881. (let ((line (org-current-line))
  5882. (col (current-column))
  5883. (ind (org-get-string-indentation
  5884. (buffer-substring (point-at-bol) (match-beginning 3))))
  5885. ;; (term (substring (match-string 3) -1))
  5886. ind1 (n (1- arg))
  5887. fmt)
  5888. ;; find where this list begins
  5889. (org-beginning-of-item-list)
  5890. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5891. (setq fmt (concat "%d" (match-string 1)))
  5892. (beginning-of-line 0)
  5893. ;; walk forward and replace these numbers
  5894. (catch 'exit
  5895. (while t
  5896. (catch 'next
  5897. (beginning-of-line 2)
  5898. (if (eobp) (throw 'exit nil))
  5899. (if (looking-at "[ \t]*$") (throw 'next nil))
  5900. (skip-chars-forward " \t") (setq ind1 (current-column))
  5901. (if (> ind1 ind) (throw 'next t))
  5902. (if (< ind1 ind) (throw 'exit t))
  5903. (if (not (org-at-item-p)) (throw 'exit nil))
  5904. (delete-region (match-beginning 2) (match-end 2))
  5905. (goto-char (match-beginning 2))
  5906. (insert (format fmt (setq n (1+ n)))))))
  5907. (goto-line line)
  5908. (move-to-column col)))
  5909. (defun org-fix-bullet-type ()
  5910. "Make sure all items in this list have the same bullet as the firsst item."
  5911. (interactive)
  5912. (unless (org-at-item-p) (error "This is not a list"))
  5913. (let ((line (org-current-line))
  5914. (col (current-column))
  5915. (ind (current-indentation))
  5916. ind1 bullet)
  5917. ;; find where this list begins
  5918. (org-beginning-of-item-list)
  5919. (beginning-of-line 1)
  5920. ;; find out what the bullet type is
  5921. (looking-at "[ \t]*\\(\\S-+\\)")
  5922. (setq bullet (match-string 1))
  5923. ;; walk forward and replace these numbers
  5924. (beginning-of-line 0)
  5925. (catch 'exit
  5926. (while t
  5927. (catch 'next
  5928. (beginning-of-line 2)
  5929. (if (eobp) (throw 'exit nil))
  5930. (if (looking-at "[ \t]*$") (throw 'next nil))
  5931. (skip-chars-forward " \t") (setq ind1 (current-column))
  5932. (if (> ind1 ind) (throw 'next t))
  5933. (if (< ind1 ind) (throw 'exit t))
  5934. (if (not (org-at-item-p)) (throw 'exit nil))
  5935. (skip-chars-forward " \t")
  5936. (looking-at "\\S-+")
  5937. (replace-match bullet))))
  5938. (goto-line line)
  5939. (move-to-column col)
  5940. (if (string-match "[0-9]" bullet)
  5941. (org-renumber-ordered-list 1))))
  5942. (defun org-beginning-of-item-list ()
  5943. "Go to the beginning of the current item list.
  5944. I.e. to the first item in this list."
  5945. (interactive)
  5946. (org-beginning-of-item)
  5947. (let ((pos (point-at-bol))
  5948. (ind (org-get-indentation))
  5949. ind1)
  5950. ;; find where this list begins
  5951. (catch 'exit
  5952. (while t
  5953. (catch 'next
  5954. (beginning-of-line 0)
  5955. (if (looking-at "[ \t]*$")
  5956. (throw (if (bobp) 'exit 'next) t))
  5957. (skip-chars-forward " \t") (setq ind1 (current-column))
  5958. (if (or (< ind1 ind)
  5959. (and (= ind1 ind)
  5960. (not (org-at-item-p)))
  5961. (bobp))
  5962. (throw 'exit t)
  5963. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5964. (goto-char pos)))
  5965. (defvar org-last-indent-begin-marker (make-marker))
  5966. (defvar org-last-indent-end-marker (make-marker))
  5967. (defun org-outdent-item (arg)
  5968. "Outdent a local list item."
  5969. (interactive "p")
  5970. (org-indent-item (- arg)))
  5971. (defun org-indent-item (arg)
  5972. "Indent a local list item."
  5973. (interactive "p")
  5974. (unless (org-at-item-p)
  5975. (error "Not on an item"))
  5976. (save-excursion
  5977. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5978. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5979. (setq beg org-last-indent-begin-marker
  5980. end org-last-indent-end-marker)
  5981. (org-beginning-of-item)
  5982. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5983. (org-end-of-item)
  5984. (setq end (move-marker org-last-indent-end-marker (point))))
  5985. (goto-char beg)
  5986. (setq tmp (org-item-indent-positions)
  5987. ind (car tmp)
  5988. ind-down (nth 2 tmp)
  5989. ind-up (nth 1 tmp)
  5990. delta (if (> arg 0)
  5991. (if ind-down (- ind-down ind) 2)
  5992. (if ind-up (- ind-up ind) -2)))
  5993. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5994. (while (< (point) end)
  5995. (beginning-of-line 1)
  5996. (skip-chars-forward " \t") (setq ind1 (current-column))
  5997. (delete-region (point-at-bol) (point))
  5998. (or (eolp) (indent-to-column (+ ind1 delta)))
  5999. (beginning-of-line 2))))
  6000. (org-fix-bullet-type)
  6001. (org-maybe-renumber-ordered-list-safe)
  6002. (save-excursion
  6003. (beginning-of-line 0)
  6004. (condition-case nil (org-beginning-of-item) (error nil))
  6005. (org-maybe-renumber-ordered-list-safe)))
  6006. (defun org-item-indent-positions ()
  6007. "Return indentation for plain list items.
  6008. This returns a list with three values: The current indentation, the
  6009. parent indentation and the indentation a child should habe.
  6010. Assumes cursor in item line."
  6011. (let* ((bolpos (point-at-bol))
  6012. (ind (org-get-indentation))
  6013. ind-down ind-up pos)
  6014. (save-excursion
  6015. (org-beginning-of-item-list)
  6016. (skip-chars-backward "\n\r \t")
  6017. (when (org-in-item-p)
  6018. (org-beginning-of-item)
  6019. (setq ind-up (org-get-indentation))))
  6020. (setq pos (point))
  6021. (save-excursion
  6022. (cond
  6023. ((and (condition-case nil (progn (org-previous-item) t)
  6024. (error nil))
  6025. (or (forward-char 1) t)
  6026. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6027. (setq ind-down (org-get-indentation)))
  6028. ((and (goto-char pos)
  6029. (org-at-item-p))
  6030. (goto-char (match-end 0))
  6031. (skip-chars-forward " \t")
  6032. (setq ind-down (current-column)))))
  6033. (list ind ind-up ind-down)))
  6034. ;;; The orgstruct minor mode
  6035. ;; Define a minor mode which can be used in other modes in order to
  6036. ;; integrate the org-mode structure editing commands.
  6037. ;; This is really a hack, because the org-mode structure commands use
  6038. ;; keys which normally belong to the major mode. Here is how it
  6039. ;; works: The minor mode defines all the keys necessary to operate the
  6040. ;; structure commands, but wraps the commands into a function which
  6041. ;; tests if the cursor is currently at a headline or a plain list
  6042. ;; item. If that is the case, the structure command is used,
  6043. ;; temporarily setting many Org-mode variables like regular
  6044. ;; expressions for filling etc. However, when any of those keys is
  6045. ;; used at a different location, function uses `key-binding' to look
  6046. ;; up if the key has an associated command in another currently active
  6047. ;; keymap (minor modes, major mode, global), and executes that
  6048. ;; command. There might be problems if any of the keys is otherwise
  6049. ;; used as a prefix key.
  6050. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6051. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6052. ;; addresses this by checking explicitly for both bindings.
  6053. (defvar orgstruct-mode-map (make-sparse-keymap)
  6054. "Keymap for the minor `orgstruct-mode'.")
  6055. (defvar org-local-vars nil
  6056. "List of local variables, for use by `orgstruct-mode'")
  6057. ;;;###autoload
  6058. (define-minor-mode orgstruct-mode
  6059. "Toggle the minor more `orgstruct-mode'.
  6060. This mode is for using Org-mode structure commands in other modes.
  6061. The following key behave as if Org-mode was active, if the cursor
  6062. is on a headline, or on a plain list item (both in the definition
  6063. of Org-mode).
  6064. M-up Move entry/item up
  6065. M-down Move entry/item down
  6066. M-left Promote
  6067. M-right Demote
  6068. M-S-up Move entry/item up
  6069. M-S-down Move entry/item down
  6070. M-S-left Promote subtree
  6071. M-S-right Demote subtree
  6072. M-q Fill paragraph and items like in Org-mode
  6073. C-c ^ Sort entries
  6074. C-c - Cycle list bullet
  6075. TAB Cycle item visibility
  6076. M-RET Insert new heading/item
  6077. S-M-RET Insert new TODO heading / Chekbox item
  6078. C-c C-c Set tags / toggle checkbox"
  6079. nil " OrgStruct" nil
  6080. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6081. ;;;###autoload
  6082. (defun turn-on-orgstruct ()
  6083. "Unconditionally turn on `orgstruct-mode'."
  6084. (orgstruct-mode 1))
  6085. ;;;###autoload
  6086. (defun turn-on-orgstruct++ ()
  6087. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6088. In addition to setting orgstruct-mode, this also exports all indentation and
  6089. autofilling variables from org-mode into the buffer. Note that turning
  6090. off orgstruct-mode will *not* remove these additonal settings."
  6091. (orgstruct-mode 1)
  6092. (let (var val)
  6093. (mapc
  6094. (lambda (x)
  6095. (when (string-match
  6096. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6097. (symbol-name (car x)))
  6098. (setq var (car x) val (nth 1 x))
  6099. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6100. org-local-vars)))
  6101. (defun orgstruct-error ()
  6102. "Error when there is no default binding for a structure key."
  6103. (interactive)
  6104. (error "This key is has no function outside structure elements"))
  6105. (defun orgstruct-setup ()
  6106. "Setup orgstruct keymaps."
  6107. (let ((nfunc 0)
  6108. (bindings
  6109. (list
  6110. '([(meta up)] org-metaup)
  6111. '([(meta down)] org-metadown)
  6112. '([(meta left)] org-metaleft)
  6113. '([(meta right)] org-metaright)
  6114. '([(meta shift up)] org-shiftmetaup)
  6115. '([(meta shift down)] org-shiftmetadown)
  6116. '([(meta shift left)] org-shiftmetaleft)
  6117. '([(meta shift right)] org-shiftmetaright)
  6118. '([(shift up)] org-shiftup)
  6119. '([(shift down)] org-shiftdown)
  6120. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6121. '("\M-q" fill-paragraph)
  6122. '("\C-c^" org-sort)
  6123. '("\C-c-" org-cycle-list-bullet)))
  6124. elt key fun cmd)
  6125. (while (setq elt (pop bindings))
  6126. (setq nfunc (1+ nfunc))
  6127. (setq key (org-key (car elt))
  6128. fun (nth 1 elt)
  6129. cmd (orgstruct-make-binding fun nfunc key))
  6130. (org-defkey orgstruct-mode-map key cmd))
  6131. ;; Special treatment needed for TAB and RET
  6132. (org-defkey orgstruct-mode-map [(tab)]
  6133. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6134. (org-defkey orgstruct-mode-map "\C-i"
  6135. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6136. (org-defkey orgstruct-mode-map "\M-\C-m"
  6137. (orgstruct-make-binding 'org-insert-heading 105
  6138. "\M-\C-m" [(meta return)]))
  6139. (org-defkey orgstruct-mode-map [(meta return)]
  6140. (orgstruct-make-binding 'org-insert-heading 106
  6141. [(meta return)] "\M-\C-m"))
  6142. (org-defkey orgstruct-mode-map [(shift meta return)]
  6143. (orgstruct-make-binding 'org-insert-todo-heading 107
  6144. [(meta return)] "\M-\C-m"))
  6145. (unless org-local-vars
  6146. (setq org-local-vars (org-get-local-variables)))
  6147. t))
  6148. (defun orgstruct-make-binding (fun n &rest keys)
  6149. "Create a function for binding in the structure minor mode.
  6150. FUN is the command to call inside a table. N is used to create a unique
  6151. command name. KEYS are keys that should be checked in for a command
  6152. to execute outside of tables."
  6153. (eval
  6154. (list 'defun
  6155. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6156. '(arg)
  6157. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6158. "Outside of structure, run the binding of `"
  6159. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6160. "'.")
  6161. '(interactive "p")
  6162. (list 'if
  6163. '(org-context-p 'headline 'item)
  6164. (list 'org-run-like-in-org-mode (list 'quote fun))
  6165. (list 'let '(orgstruct-mode)
  6166. (list 'call-interactively
  6167. (append '(or)
  6168. (mapcar (lambda (k)
  6169. (list 'key-binding k))
  6170. keys)
  6171. '('orgstruct-error))))))))
  6172. (defun org-context-p (&rest contexts)
  6173. "Check if local context is and of CONTEXTS.
  6174. Possible values in the list of contexts are `table', `headline', and `item'."
  6175. (let ((pos (point)))
  6176. (goto-char (point-at-bol))
  6177. (prog1 (or (and (memq 'table contexts)
  6178. (looking-at "[ \t]*|"))
  6179. (and (memq 'headline contexts)
  6180. (looking-at "\\*+"))
  6181. (and (memq 'item contexts)
  6182. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6183. (goto-char pos))))
  6184. (defun org-get-local-variables ()
  6185. "Return a list of all local variables in an org-mode buffer."
  6186. (let (varlist)
  6187. (with-current-buffer (get-buffer-create "*Org tmp*")
  6188. (erase-buffer)
  6189. (org-mode)
  6190. (setq varlist (buffer-local-variables)))
  6191. (kill-buffer "*Org tmp*")
  6192. (delq nil
  6193. (mapcar
  6194. (lambda (x)
  6195. (setq x
  6196. (if (symbolp x)
  6197. (list x)
  6198. (list (car x) (list 'quote (cdr x)))))
  6199. (if (string-match
  6200. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6201. (symbol-name (car x)))
  6202. x nil))
  6203. varlist))))
  6204. ;;;###autoload
  6205. (defun org-run-like-in-org-mode (cmd)
  6206. (unless org-local-vars
  6207. (setq org-local-vars (org-get-local-variables)))
  6208. (eval (list 'let org-local-vars
  6209. (list 'call-interactively (list 'quote cmd)))))
  6210. ;;;; Archiving
  6211. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6212. (defun org-archive-subtree (&optional find-done)
  6213. "Move the current subtree to the archive.
  6214. The archive can be a certain top-level heading in the current file, or in
  6215. a different file. The tree will be moved to that location, the subtree
  6216. heading be marked DONE, and the current time will be added.
  6217. When called with prefix argument FIND-DONE, find whole trees without any
  6218. open TODO items and archive them (after getting confirmation from the user).
  6219. If the cursor is not at a headline when this comand is called, try all level
  6220. 1 trees. If the cursor is on a headline, only try the direct children of
  6221. this heading."
  6222. (interactive "P")
  6223. (if find-done
  6224. (org-archive-all-done)
  6225. ;; Save all relevant TODO keyword-relatex variables
  6226. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6227. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6228. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6229. (tr-org-done-keywords org-done-keywords)
  6230. (tr-org-todo-regexp org-todo-regexp)
  6231. (tr-org-todo-line-regexp org-todo-line-regexp)
  6232. (tr-org-odd-levels-only org-odd-levels-only)
  6233. (this-buffer (current-buffer))
  6234. (org-archive-location org-archive-location)
  6235. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6236. (file (abbreviate-file-name (buffer-file-name)))
  6237. (time (format-time-string
  6238. (substring (cdr org-time-stamp-formats) 1 -1)
  6239. (current-time)))
  6240. afile heading buffer level newfile-p
  6241. category todo priority ltags itags prop)
  6242. ;; Try to find a local archive location
  6243. (save-excursion
  6244. (save-restriction
  6245. (widen)
  6246. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6247. (if (and prop (string-match "\\S-" prop))
  6248. (setq org-archive-location prop)
  6249. (if (or (re-search-backward re nil t)
  6250. (re-search-forward re nil t))
  6251. (setq org-archive-location (match-string 1))))))
  6252. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6253. (progn
  6254. (setq afile (format (match-string 1 org-archive-location)
  6255. (file-name-nondirectory buffer-file-name))
  6256. heading (match-string 2 org-archive-location)))
  6257. (error "Invalid `org-archive-location'"))
  6258. (if (> (length afile) 0)
  6259. (setq newfile-p (not (file-exists-p afile))
  6260. buffer (find-file-noselect afile))
  6261. (setq buffer (current-buffer)))
  6262. (unless buffer
  6263. (error "Cannot access file \"%s\"" afile))
  6264. (if (and (> (length heading) 0)
  6265. (string-match "^\\*+" heading))
  6266. (setq level (match-end 0))
  6267. (setq heading nil level 0))
  6268. (save-excursion
  6269. (org-back-to-heading t)
  6270. ;; Get context information that will be lost by moving the tree
  6271. (org-refresh-category-properties)
  6272. (setq category (org-get-category)
  6273. todo (and (looking-at org-todo-line-regexp)
  6274. (match-string 2))
  6275. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6276. ltags (org-get-tags)
  6277. itags (org-delete-all ltags (org-get-tags-at)))
  6278. (setq ltags (mapconcat 'identity ltags " ")
  6279. itags (mapconcat 'identity itags " "))
  6280. ;; We first only copy, in case something goes wrong
  6281. ;; we need to protect this-command, to avoid kill-region sets it,
  6282. ;; which would lead to duplication of subtrees
  6283. (let (this-command) (org-copy-subtree))
  6284. (set-buffer buffer)
  6285. ;; Enforce org-mode for the archive buffer
  6286. (if (not (org-mode-p))
  6287. ;; Force the mode for future visits.
  6288. (let ((org-insert-mode-line-in-empty-file t)
  6289. (org-inhibit-startup t))
  6290. (call-interactively 'org-mode)))
  6291. (when newfile-p
  6292. (goto-char (point-max))
  6293. (insert (format "\nArchived entries from file %s\n\n"
  6294. (buffer-file-name this-buffer))))
  6295. ;; Force the TODO keywords of the original buffer
  6296. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6297. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6298. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6299. (org-done-keywords tr-org-done-keywords)
  6300. (org-todo-regexp tr-org-todo-regexp)
  6301. (org-todo-line-regexp tr-org-todo-line-regexp)
  6302. (org-odd-levels-only
  6303. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6304. org-odd-levels-only
  6305. tr-org-odd-levels-only)))
  6306. (goto-char (point-min))
  6307. (if heading
  6308. (progn
  6309. (if (re-search-forward
  6310. (concat "^" (regexp-quote heading)
  6311. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6312. nil t)
  6313. (goto-char (match-end 0))
  6314. ;; Heading not found, just insert it at the end
  6315. (goto-char (point-max))
  6316. (or (bolp) (insert "\n"))
  6317. (insert "\n" heading "\n")
  6318. (end-of-line 0))
  6319. ;; Make the subtree visible
  6320. (show-subtree)
  6321. (org-end-of-subtree t)
  6322. (skip-chars-backward " \t\r\n")
  6323. (and (looking-at "[ \t\r\n]*")
  6324. (replace-match "\n\n")))
  6325. ;; No specific heading, just go to end of file.
  6326. (goto-char (point-max)) (insert "\n"))
  6327. ;; Paste
  6328. (org-paste-subtree (org-get-legal-level level 1))
  6329. ;; Mark the entry as done
  6330. (when (and org-archive-mark-done
  6331. (looking-at org-todo-line-regexp)
  6332. (or (not (match-end 2))
  6333. (not (member (match-string 2) org-done-keywords))))
  6334. (let (org-log-done)
  6335. (org-todo
  6336. (car (or (member org-archive-mark-done org-done-keywords)
  6337. org-done-keywords)))))
  6338. ;; Add the context info
  6339. (when org-archive-save-context-info
  6340. (let ((l org-archive-save-context-info) e n v)
  6341. (while (setq e (pop l))
  6342. (when (and (setq v (symbol-value e))
  6343. (stringp v) (string-match "\\S-" v))
  6344. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6345. (org-entry-put (point) n v)))))
  6346. ;; Save the buffer, if it is not the same buffer.
  6347. (if (not (eq this-buffer buffer)) (save-buffer))))
  6348. ;; Here we are back in the original buffer. Everything seems to have
  6349. ;; worked. So now cut the tree and finish up.
  6350. (let (this-command) (org-cut-subtree))
  6351. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  6352. (message "Subtree archived %s"
  6353. (if (eq this-buffer buffer)
  6354. (concat "under heading: " heading)
  6355. (concat "in file: " (abbreviate-file-name afile)))))))
  6356. (defun org-refresh-category-properties ()
  6357. "Refresh category text properties in teh buffer."
  6358. (let ((def-cat (cond
  6359. ((null org-category)
  6360. (if buffer-file-name
  6361. (file-name-sans-extension
  6362. (file-name-nondirectory buffer-file-name))
  6363. "???"))
  6364. ((symbolp org-category) (symbol-name org-category))
  6365. (t org-category)))
  6366. beg end cat pos optionp)
  6367. (org-unmodified
  6368. (save-excursion
  6369. (save-restriction
  6370. (widen)
  6371. (goto-char (point-min))
  6372. (put-text-property (point) (point-max) 'org-category def-cat)
  6373. (while (re-search-forward
  6374. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6375. (setq pos (match-end 0)
  6376. optionp (equal (char-after (match-beginning 0)) ?#)
  6377. cat (org-trim (match-string 2)))
  6378. (if optionp
  6379. (setq beg (point-at-bol) end (point-max))
  6380. (org-back-to-heading t)
  6381. (setq beg (point) end (org-end-of-subtree t t)))
  6382. (put-text-property beg end 'org-category cat)
  6383. (goto-char pos)))))))
  6384. (defun org-archive-all-done (&optional tag)
  6385. "Archive sublevels of the current tree without open TODO items.
  6386. If the cursor is not on a headline, try all level 1 trees. If
  6387. it is on a headline, try all direct children.
  6388. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  6389. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  6390. (rea (concat ".*:" org-archive-tag ":"))
  6391. (begm (make-marker))
  6392. (endm (make-marker))
  6393. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  6394. "Move subtree to archive (no open TODO items)? "))
  6395. beg end (cntarch 0))
  6396. (if (org-on-heading-p)
  6397. (progn
  6398. (setq re1 (concat "^" (regexp-quote
  6399. (make-string
  6400. (1+ (- (match-end 0) (match-beginning 0)))
  6401. ?*))
  6402. " "))
  6403. (move-marker begm (point))
  6404. (move-marker endm (org-end-of-subtree t)))
  6405. (setq re1 "^* ")
  6406. (move-marker begm (point-min))
  6407. (move-marker endm (point-max)))
  6408. (save-excursion
  6409. (goto-char begm)
  6410. (while (re-search-forward re1 endm t)
  6411. (setq beg (match-beginning 0)
  6412. end (save-excursion (org-end-of-subtree t) (point)))
  6413. (goto-char beg)
  6414. (if (re-search-forward re end t)
  6415. (goto-char end)
  6416. (goto-char beg)
  6417. (if (and (or (not tag) (not (looking-at rea)))
  6418. (y-or-n-p question))
  6419. (progn
  6420. (if tag
  6421. (org-toggle-tag org-archive-tag 'on)
  6422. (org-archive-subtree))
  6423. (setq cntarch (1+ cntarch)))
  6424. (goto-char end)))))
  6425. (message "%d trees archived" cntarch)))
  6426. (defun org-cycle-hide-drawers (state)
  6427. "Re-hide all drawers after a visibility state change."
  6428. (when (and (org-mode-p)
  6429. (not (memq state '(overview folded))))
  6430. (save-excursion
  6431. (let* ((globalp (memq state '(contents all)))
  6432. (beg (if globalp (point-min) (point)))
  6433. (end (if globalp (point-max) (org-end-of-subtree t))))
  6434. (goto-char beg)
  6435. (while (re-search-forward org-drawer-regexp end t)
  6436. (org-flag-drawer t))))))
  6437. (defun org-flag-drawer (flag)
  6438. (save-excursion
  6439. (beginning-of-line 1)
  6440. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6441. (let ((b (match-end 0)))
  6442. (if (re-search-forward
  6443. "^[ \t]*:END:"
  6444. (save-excursion (outline-next-heading) (point)) t)
  6445. (outline-flag-region b (point-at-eol) flag)
  6446. (error ":END: line missing"))))))
  6447. (defun org-cycle-hide-archived-subtrees (state)
  6448. "Re-hide all archived subtrees after a visibility state change."
  6449. (when (and (not org-cycle-open-archived-trees)
  6450. (not (memq state '(overview folded))))
  6451. (save-excursion
  6452. (let* ((globalp (memq state '(contents all)))
  6453. (beg (if globalp (point-min) (point)))
  6454. (end (if globalp (point-max) (org-end-of-subtree t))))
  6455. (org-hide-archived-subtrees beg end)
  6456. (goto-char beg)
  6457. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6458. (message (substitute-command-keys
  6459. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6460. (defun org-force-cycle-archived ()
  6461. "Cycle subtree even if it is archived."
  6462. (interactive)
  6463. (setq this-command 'org-cycle)
  6464. (let ((org-cycle-open-archived-trees t))
  6465. (call-interactively 'org-cycle)))
  6466. (defun org-hide-archived-subtrees (beg end)
  6467. "Re-hide all archived subtrees after a visibility state change."
  6468. (save-excursion
  6469. (let* ((re (concat ":" org-archive-tag ":")))
  6470. (goto-char beg)
  6471. (while (re-search-forward re end t)
  6472. (and (org-on-heading-p) (hide-subtree))
  6473. (org-end-of-subtree t)))))
  6474. (defun org-toggle-tag (tag &optional onoff)
  6475. "Toggle the tag TAG for the current line.
  6476. If ONOFF is `on' or `off', don't toggle but set to this state."
  6477. (unless (org-on-heading-p t) (error "Not on headling"))
  6478. (let (res current)
  6479. (save-excursion
  6480. (beginning-of-line)
  6481. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6482. (point-at-eol) t)
  6483. (progn
  6484. (setq current (match-string 1))
  6485. (replace-match ""))
  6486. (setq current ""))
  6487. (setq current (nreverse (org-split-string current ":")))
  6488. (cond
  6489. ((eq onoff 'on)
  6490. (setq res t)
  6491. (or (member tag current) (push tag current)))
  6492. ((eq onoff 'off)
  6493. (or (not (member tag current)) (setq current (delete tag current))))
  6494. (t (if (member tag current)
  6495. (setq current (delete tag current))
  6496. (setq res t)
  6497. (push tag current))))
  6498. (end-of-line 1)
  6499. (when current
  6500. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  6501. (org-set-tags nil t)
  6502. res)
  6503. (run-hooks 'org-after-tags-change-hook)))
  6504. (defun org-toggle-archive-tag (&optional arg)
  6505. "Toggle the archive tag for the current headline.
  6506. With prefix ARG, check all children of current headline and offer tagging
  6507. the children that do not contain any open TODO items."
  6508. (interactive "P")
  6509. (if arg
  6510. (org-archive-all-done 'tag)
  6511. (let (set)
  6512. (save-excursion
  6513. (org-back-to-heading t)
  6514. (setq set (org-toggle-tag org-archive-tag))
  6515. (when set (hide-subtree)))
  6516. (and set (beginning-of-line 1))
  6517. (message "Subtree %s" (if set "archived" "unarchived")))))
  6518. ;;;; Tables
  6519. ;;; The table editor
  6520. ;; Watch out: Here we are talking about two different kind of tables.
  6521. ;; Most of the code is for the tables created with the Org-mode table editor.
  6522. ;; Sometimes, we talk about tables created and edited with the table.el
  6523. ;; Emacs package. We call the former org-type tables, and the latter
  6524. ;; table.el-type tables.
  6525. (defun org-before-change-function (beg end)
  6526. "Every change indicates that a table might need an update."
  6527. (setq org-table-may-need-update t))
  6528. (defconst org-table-line-regexp "^[ \t]*|"
  6529. "Detects an org-type table line.")
  6530. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  6531. "Detects an org-type table line.")
  6532. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  6533. "Detects a table line marked for automatic recalculation.")
  6534. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  6535. "Detects a table line marked for automatic recalculation.")
  6536. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  6537. "Detects a table line marked for automatic recalculation.")
  6538. (defconst org-table-hline-regexp "^[ \t]*|-"
  6539. "Detects an org-type table hline.")
  6540. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  6541. "Detects a table-type table hline.")
  6542. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  6543. "Detects an org-type or table-type table.")
  6544. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  6545. "Searching from within a table (any type) this finds the first line
  6546. outside the table.")
  6547. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  6548. "Searching from within a table (any type) this finds the first line
  6549. outside the table.")
  6550. (defvar org-table-last-highlighted-reference nil)
  6551. (defvar org-table-formula-history nil)
  6552. (defvar org-table-column-names nil
  6553. "Alist with column names, derived from the `!' line.")
  6554. (defvar org-table-column-name-regexp nil
  6555. "Regular expression matching the current column names.")
  6556. (defvar org-table-local-parameters nil
  6557. "Alist with parameter names, derived from the `$' line.")
  6558. (defvar org-table-named-field-locations nil
  6559. "Alist with locations of named fields.")
  6560. (defvar org-table-current-line-types nil
  6561. "Table row types, non-nil only for the duration of a comand.")
  6562. (defvar org-table-current-begin-line nil
  6563. "Table begin line, non-nil only for the duration of a comand.")
  6564. (defvar org-table-current-begin-pos nil
  6565. "Table begin position, non-nil only for the duration of a comand.")
  6566. (defvar org-table-dlines nil
  6567. "Vector of data line line numbers in the current table.")
  6568. (defvar org-table-hlines nil
  6569. "Vector of hline line numbers in the current table.")
  6570. (defconst org-table-range-regexp
  6571. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6572. ;; 1 2 3 4 5
  6573. "Regular expression for matching ranges in formulas.")
  6574. (defconst org-table-range-regexp2
  6575. (concat
  6576. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6577. "\\.\\."
  6578. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6579. "Match a range for reference display.")
  6580. (defconst org-table-translate-regexp
  6581. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6582. "Match a reference that needs translation, for reference display.")
  6583. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6584. (defun org-table-create-with-table.el ()
  6585. "Use the table.el package to insert a new table.
  6586. If there is already a table at point, convert between Org-mode tables
  6587. and table.el tables."
  6588. (interactive)
  6589. (require 'table)
  6590. (cond
  6591. ((org-at-table.el-p)
  6592. (if (y-or-n-p "Convert table to Org-mode table? ")
  6593. (org-table-convert)))
  6594. ((org-at-table-p)
  6595. (if (y-or-n-p "Convert table to table.el table? ")
  6596. (org-table-convert)))
  6597. (t (call-interactively 'table-insert))))
  6598. (defun org-table-create-or-convert-from-region (arg)
  6599. "Convert region to table, or create an empty table.
  6600. If there is an active region, convert it to a table, using the function
  6601. `org-table-convert-region'. See the documentation of that function
  6602. to learn how the prefix argument is interpreted to determine the field
  6603. separator.
  6604. If there is no such region, create an empty table with `org-table-create'."
  6605. (interactive "P")
  6606. (if (org-region-active-p)
  6607. (org-table-convert-region (region-beginning) (region-end) arg)
  6608. (org-table-create arg)))
  6609. (defun org-table-create (&optional size)
  6610. "Query for a size and insert a table skeleton.
  6611. SIZE is a string Columns x Rows like for example \"3x2\"."
  6612. (interactive "P")
  6613. (unless size
  6614. (setq size (read-string
  6615. (concat "Table size Columns x Rows [e.g. "
  6616. org-table-default-size "]: ")
  6617. "" nil org-table-default-size)))
  6618. (let* ((pos (point))
  6619. (indent (make-string (current-column) ?\ ))
  6620. (split (org-split-string size " *x *"))
  6621. (rows (string-to-number (nth 1 split)))
  6622. (columns (string-to-number (car split)))
  6623. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6624. "\n")))
  6625. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6626. (point-at-bol) (point)))
  6627. (beginning-of-line 1)
  6628. (newline))
  6629. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6630. (dotimes (i rows) (insert line))
  6631. (goto-char pos)
  6632. (if (> rows 1)
  6633. ;; Insert a hline after the first row.
  6634. (progn
  6635. (end-of-line 1)
  6636. (insert "\n|-")
  6637. (goto-char pos)))
  6638. (org-table-align)))
  6639. (defun org-table-convert-region (beg0 end0 &optional separator)
  6640. "Convert region to a table.
  6641. The region goes from BEG0 to END0, but these borders will be moved
  6642. slightly, to make sure a beginning of line in the first line is included.
  6643. SEPARATOR specifies the field separator in the lines. It can have the
  6644. following values:
  6645. '(4) Use the comma as a field separator
  6646. '(16) Use a TAB as field separator
  6647. integer When a number, use that many spaces as field separator
  6648. nil When nil, the command tries to be smart and figure out the
  6649. separator in the following way:
  6650. - when each line contains a TAB, assume TAB-separated material
  6651. - when each line contains a comme, assume CSV material
  6652. - else, assume one or more SPACE charcters as separator."
  6653. (interactive "rP")
  6654. (let* ((beg (min beg0 end0))
  6655. (end (max beg0 end0))
  6656. sep-re re)
  6657. (goto-char beg)
  6658. (beginning-of-line 1)
  6659. (setq beg (move-marker (make-marker) (point)))
  6660. (goto-char end)
  6661. (if (bolp) (backward-char 1) (end-of-line 1))
  6662. (setq end (move-marker (make-marker) (point)))
  6663. ;; Get the right field separator
  6664. (unless separator
  6665. (goto-char beg)
  6666. (setq separator
  6667. (cond
  6668. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  6669. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  6670. (t 1))))
  6671. (setq re (cond
  6672. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  6673. ((equal separator '(16)) "^\\|\t")
  6674. ((integerp separator)
  6675. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  6676. (t (error "This should not happen"))))
  6677. (goto-char beg)
  6678. (while (re-search-forward re end t)
  6679. (replace-match "| " t t))
  6680. (goto-char beg)
  6681. (insert " ")
  6682. (org-table-align)))
  6683. (defun org-table-import (file arg)
  6684. "Import FILE as a table.
  6685. The file is assumed to be tab-separated. Such files can be produced by most
  6686. spreadsheet and database applications. If no tabs (at least one per line)
  6687. are found, lines will be split on whitespace into fields."
  6688. (interactive "f\nP")
  6689. (or (bolp) (newline))
  6690. (let ((beg (point))
  6691. (pm (point-max)))
  6692. (insert-file-contents file)
  6693. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  6694. (defun org-table-export ()
  6695. "Export table as a tab-separated file.
  6696. Such a file can be imported into a spreadsheet program like Excel."
  6697. (interactive)
  6698. (let* ((beg (org-table-begin))
  6699. (end (org-table-end))
  6700. (table (buffer-substring beg end))
  6701. (file (read-file-name "Export table to: "))
  6702. buf)
  6703. (unless (or (not (file-exists-p file))
  6704. (y-or-n-p (format "Overwrite file %s? " file)))
  6705. (error "Abort"))
  6706. (with-current-buffer (find-file-noselect file)
  6707. (setq buf (current-buffer))
  6708. (erase-buffer)
  6709. (fundamental-mode)
  6710. (insert table)
  6711. (goto-char (point-min))
  6712. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  6713. (replace-match "" t t)
  6714. (end-of-line 1))
  6715. (goto-char (point-min))
  6716. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  6717. (replace-match "" t t)
  6718. (goto-char (min (1+ (point)) (point-max))))
  6719. (goto-char (point-min))
  6720. (while (re-search-forward "^-[-+]*$" nil t)
  6721. (replace-match "")
  6722. (if (looking-at "\n")
  6723. (delete-char 1)))
  6724. (goto-char (point-min))
  6725. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  6726. (replace-match "\t" t t))
  6727. (save-buffer))
  6728. (kill-buffer buf)))
  6729. (defvar org-table-aligned-begin-marker (make-marker)
  6730. "Marker at the beginning of the table last aligned.
  6731. Used to check if cursor still is in that table, to minimize realignment.")
  6732. (defvar org-table-aligned-end-marker (make-marker)
  6733. "Marker at the end of the table last aligned.
  6734. Used to check if cursor still is in that table, to minimize realignment.")
  6735. (defvar org-table-last-alignment nil
  6736. "List of flags for flushright alignment, from the last re-alignment.
  6737. This is being used to correctly align a single field after TAB or RET.")
  6738. (defvar org-table-last-column-widths nil
  6739. "List of max width of fields in each column.
  6740. This is being used to correctly align a single field after TAB or RET.")
  6741. (defvar org-table-overlay-coordinates nil
  6742. "Overlay coordinates after each align of a table.")
  6743. (make-variable-buffer-local 'org-table-overlay-coordinates)
  6744. (defvar org-last-recalc-line nil)
  6745. (defconst org-narrow-column-arrow "=>"
  6746. "Used as display property in narrowed table columns.")
  6747. (defun org-table-align ()
  6748. "Align the table at point by aligning all vertical bars."
  6749. (interactive)
  6750. (let* (
  6751. ;; Limits of table
  6752. (beg (org-table-begin))
  6753. (end (org-table-end))
  6754. ;; Current cursor position
  6755. (linepos (org-current-line))
  6756. (colpos (org-table-current-column))
  6757. (winstart (window-start))
  6758. (winstartline (org-current-line (min winstart (1- (point-max)))))
  6759. lines (new "") lengths l typenums ty fields maxfields i
  6760. column
  6761. (indent "") cnt frac
  6762. rfmt hfmt
  6763. (spaces '(1 . 1))
  6764. (sp1 (car spaces))
  6765. (sp2 (cdr spaces))
  6766. (rfmt1 (concat
  6767. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  6768. (hfmt1 (concat
  6769. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  6770. emptystrings links dates narrow fmax f1 len c e)
  6771. (untabify beg end)
  6772. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  6773. ;; Check if we have links or dates
  6774. (goto-char beg)
  6775. (setq links (re-search-forward org-bracket-link-regexp end t))
  6776. (goto-char beg)
  6777. (setq dates (and org-display-custom-times
  6778. (re-search-forward org-ts-regexp-both end t)))
  6779. ;; Make sure the link properties are right
  6780. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  6781. ;; Make sure the date properties are right
  6782. (when dates (goto-char beg) (while (org-activate-dates end)))
  6783. ;; Check if we are narrowing any columns
  6784. (goto-char beg)
  6785. (setq narrow (and org-format-transports-properties-p
  6786. (re-search-forward "<[0-9]+>" end t)))
  6787. ;; Get the rows
  6788. (setq lines (org-split-string
  6789. (buffer-substring beg end) "\n"))
  6790. ;; Store the indentation of the first line
  6791. (if (string-match "^ *" (car lines))
  6792. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  6793. ;; Mark the hlines by setting the corresponding element to nil
  6794. ;; At the same time, we remove trailing space.
  6795. (setq lines (mapcar (lambda (l)
  6796. (if (string-match "^ *|-" l)
  6797. nil
  6798. (if (string-match "[ \t]+$" l)
  6799. (substring l 0 (match-beginning 0))
  6800. l)))
  6801. lines))
  6802. ;; Get the data fields by splitting the lines.
  6803. (setq fields (mapcar
  6804. (lambda (l)
  6805. (org-split-string l " *| *"))
  6806. (delq nil (copy-sequence lines))))
  6807. ;; How many fields in the longest line?
  6808. (condition-case nil
  6809. (setq maxfields (apply 'max (mapcar 'length fields)))
  6810. (error
  6811. (kill-region beg end)
  6812. (org-table-create org-table-default-size)
  6813. (error "Empty table - created default table")))
  6814. ;; A list of empty strings to fill any short rows on output
  6815. (setq emptystrings (make-list maxfields ""))
  6816. ;; Check for special formatting.
  6817. (setq i -1)
  6818. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  6819. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  6820. ;; Check if there is an explicit width specified
  6821. (when narrow
  6822. (setq c column fmax nil)
  6823. (while c
  6824. (setq e (pop c))
  6825. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  6826. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  6827. ;; Find fields that are wider than fmax, and shorten them
  6828. (when fmax
  6829. (loop for xx in column do
  6830. (when (and (stringp xx)
  6831. (> (org-string-width xx) fmax))
  6832. (org-add-props xx nil
  6833. 'help-echo
  6834. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  6835. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  6836. (unless (> f1 1)
  6837. (error "Cannot narrow field starting with wide link \"%s\""
  6838. (match-string 0 xx)))
  6839. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  6840. (add-text-properties (- f1 2) f1
  6841. (list 'display org-narrow-column-arrow)
  6842. xx)))))
  6843. ;; Get the maximum width for each column
  6844. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  6845. ;; Get the fraction of numbers, to decide about alignment of the column
  6846. (setq cnt 0 frac 0.0)
  6847. (loop for x in column do
  6848. (if (equal x "")
  6849. nil
  6850. (setq frac ( / (+ (* frac cnt)
  6851. (if (string-match org-table-number-regexp x) 1 0))
  6852. (setq cnt (1+ cnt))))))
  6853. (push (>= frac org-table-number-fraction) typenums))
  6854. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  6855. ;; Store the alignment of this table, for later editing of single fields
  6856. (setq org-table-last-alignment typenums
  6857. org-table-last-column-widths lengths)
  6858. ;; With invisible characters, `format' does not get the field width right
  6859. ;; So we need to make these fields wide by hand.
  6860. (when links
  6861. (loop for i from 0 upto (1- maxfields) do
  6862. (setq len (nth i lengths))
  6863. (loop for j from 0 upto (1- (length fields)) do
  6864. (setq c (nthcdr i (car (nthcdr j fields))))
  6865. (if (and (stringp (car c))
  6866. (string-match org-bracket-link-regexp (car c))
  6867. (< (org-string-width (car c)) len))
  6868. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  6869. ;; Compute the formats needed for output of the table
  6870. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  6871. (while (setq l (pop lengths))
  6872. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  6873. (setq rfmt (concat rfmt (format rfmt1 ty l))
  6874. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  6875. (setq rfmt (concat rfmt "\n")
  6876. hfmt (concat (substring hfmt 0 -1) "|\n"))
  6877. (setq new (mapconcat
  6878. (lambda (l)
  6879. (if l (apply 'format rfmt
  6880. (append (pop fields) emptystrings))
  6881. hfmt))
  6882. lines ""))
  6883. ;; Replace the old one
  6884. (delete-region beg end)
  6885. (move-marker end nil)
  6886. (move-marker org-table-aligned-begin-marker (point))
  6887. (insert new)
  6888. (move-marker org-table-aligned-end-marker (point))
  6889. (when (and orgtbl-mode (not (org-mode-p)))
  6890. (goto-char org-table-aligned-begin-marker)
  6891. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  6892. ;; Try to move to the old location
  6893. (goto-line winstartline)
  6894. (setq winstart (point-at-bol))
  6895. (goto-line linepos)
  6896. (set-window-start (selected-window) winstart 'noforce)
  6897. (org-table-goto-column colpos)
  6898. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  6899. (setq org-table-may-need-update nil)
  6900. ))
  6901. (defun org-string-width (s)
  6902. "Compute width of string, ignoring invisible characters.
  6903. This ignores character with invisibility property `org-link', and also
  6904. characters with property `org-cwidth', because these will become invisible
  6905. upon the next fontification round."
  6906. (let (b l)
  6907. (when (or (eq t buffer-invisibility-spec)
  6908. (assq 'org-link buffer-invisibility-spec))
  6909. (while (setq b (text-property-any 0 (length s)
  6910. 'invisible 'org-link s))
  6911. (setq s (concat (substring s 0 b)
  6912. (substring s (or (next-single-property-change
  6913. b 'invisible s) (length s)))))))
  6914. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  6915. (setq s (concat (substring s 0 b)
  6916. (substring s (or (next-single-property-change
  6917. b 'org-cwidth s) (length s))))))
  6918. (setq l (string-width s) b -1)
  6919. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  6920. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  6921. l))
  6922. (defun org-table-begin (&optional table-type)
  6923. "Find the beginning of the table and return its position.
  6924. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  6925. (save-excursion
  6926. (if (not (re-search-backward
  6927. (if table-type org-table-any-border-regexp
  6928. org-table-border-regexp)
  6929. nil t))
  6930. (progn (goto-char (point-min)) (point))
  6931. (goto-char (match-beginning 0))
  6932. (beginning-of-line 2)
  6933. (point))))
  6934. (defun org-table-end (&optional table-type)
  6935. "Find the end of the table and return its position.
  6936. With argument TABLE-TYPE, go to the end of a table.el-type table."
  6937. (save-excursion
  6938. (if (not (re-search-forward
  6939. (if table-type org-table-any-border-regexp
  6940. org-table-border-regexp)
  6941. nil t))
  6942. (goto-char (point-max))
  6943. (goto-char (match-beginning 0)))
  6944. (point-marker)))
  6945. (defun org-table-justify-field-maybe (&optional new)
  6946. "Justify the current field, text to left, number to right.
  6947. Optional argument NEW may specify text to replace the current field content."
  6948. (cond
  6949. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  6950. ((org-at-table-hline-p))
  6951. ((and (not new)
  6952. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  6953. (current-buffer)))
  6954. (< (point) org-table-aligned-begin-marker)
  6955. (>= (point) org-table-aligned-end-marker)))
  6956. ;; This is not the same table, force a full re-align
  6957. (setq org-table-may-need-update t))
  6958. (t ;; realign the current field, based on previous full realign
  6959. (let* ((pos (point)) s
  6960. (col (org-table-current-column))
  6961. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  6962. l f n o e)
  6963. (when (> col 0)
  6964. (skip-chars-backward "^|\n")
  6965. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  6966. (progn
  6967. (setq s (match-string 1)
  6968. o (match-string 0)
  6969. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  6970. e (not (= (match-beginning 2) (match-end 2))))
  6971. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  6972. l (if e "|" (setq org-table-may-need-update t) ""))
  6973. n (format f s))
  6974. (if new
  6975. (if (<= (length new) l) ;; FIXME: length -> str-width?
  6976. (setq n (format f new))
  6977. (setq n (concat new "|") org-table-may-need-update t)))
  6978. (or (equal n o)
  6979. (let (org-table-may-need-update)
  6980. (replace-match n t t))))
  6981. (setq org-table-may-need-update t))
  6982. (goto-char pos))))))
  6983. (defun org-table-next-field ()
  6984. "Go to the next field in the current table, creating new lines as needed.
  6985. Before doing so, re-align the table if necessary."
  6986. (interactive)
  6987. (org-table-maybe-eval-formula)
  6988. (org-table-maybe-recalculate-line)
  6989. (if (and org-table-automatic-realign
  6990. org-table-may-need-update)
  6991. (org-table-align))
  6992. (let ((end (org-table-end)))
  6993. (if (org-at-table-hline-p)
  6994. (end-of-line 1))
  6995. (condition-case nil
  6996. (progn
  6997. (re-search-forward "|" end)
  6998. (if (looking-at "[ \t]*$")
  6999. (re-search-forward "|" end))
  7000. (if (and (looking-at "-")
  7001. org-table-tab-jumps-over-hlines
  7002. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7003. (goto-char (match-beginning 1)))
  7004. (if (looking-at "-")
  7005. (progn
  7006. (beginning-of-line 0)
  7007. (org-table-insert-row 'below))
  7008. (if (looking-at " ") (forward-char 1))))
  7009. (error
  7010. (org-table-insert-row 'below)))))
  7011. (defun org-table-previous-field ()
  7012. "Go to the previous field in the table.
  7013. Before doing so, re-align the table if necessary."
  7014. (interactive)
  7015. (org-table-justify-field-maybe)
  7016. (org-table-maybe-recalculate-line)
  7017. (if (and org-table-automatic-realign
  7018. org-table-may-need-update)
  7019. (org-table-align))
  7020. (if (org-at-table-hline-p)
  7021. (end-of-line 1))
  7022. (re-search-backward "|" (org-table-begin))
  7023. (re-search-backward "|" (org-table-begin))
  7024. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7025. (re-search-backward "|" (org-table-begin)))
  7026. (if (looking-at "| ?")
  7027. (goto-char (match-end 0))))
  7028. (defun org-table-next-row ()
  7029. "Go to the next row (same column) in the current table.
  7030. Before doing so, re-align the table if necessary."
  7031. (interactive)
  7032. (org-table-maybe-eval-formula)
  7033. (org-table-maybe-recalculate-line)
  7034. (if (or (looking-at "[ \t]*$")
  7035. (save-excursion (skip-chars-backward " \t") (bolp)))
  7036. (newline)
  7037. (if (and org-table-automatic-realign
  7038. org-table-may-need-update)
  7039. (org-table-align))
  7040. (let ((col (org-table-current-column)))
  7041. (beginning-of-line 2)
  7042. (if (or (not (org-at-table-p))
  7043. (org-at-table-hline-p))
  7044. (progn
  7045. (beginning-of-line 0)
  7046. (org-table-insert-row 'below)))
  7047. (org-table-goto-column col)
  7048. (skip-chars-backward "^|\n\r")
  7049. (if (looking-at " ") (forward-char 1)))))
  7050. (defun org-table-copy-down (n)
  7051. "Copy a field down in the current column.
  7052. If the field at the cursor is empty, copy into it the content of the nearest
  7053. non-empty field above. With argument N, use the Nth non-empty field.
  7054. If the current field is not empty, it is copied down to the next row, and
  7055. the cursor is moved with it. Therefore, repeating this command causes the
  7056. column to be filled row-by-row.
  7057. If the variable `org-table-copy-increment' is non-nil and the field is an
  7058. integer or a timestamp, it will be incremented while copying. In the case of
  7059. a timestamp, if the cursor is on the year, change the year. If it is on the
  7060. month or the day, change that. Point will stay on the current date field
  7061. in order to easily repeat the interval."
  7062. (interactive "p")
  7063. (let* ((colpos (org-table-current-column))
  7064. (col (current-column))
  7065. (field (org-table-get-field))
  7066. (non-empty (string-match "[^ \t]" field))
  7067. (beg (org-table-begin))
  7068. txt)
  7069. (org-table-check-inside-data-field)
  7070. (if non-empty
  7071. (progn
  7072. (setq txt (org-trim field))
  7073. (org-table-next-row)
  7074. (org-table-blank-field))
  7075. (save-excursion
  7076. (setq txt
  7077. (catch 'exit
  7078. (while (progn (beginning-of-line 1)
  7079. (re-search-backward org-table-dataline-regexp
  7080. beg t))
  7081. (org-table-goto-column colpos t)
  7082. (if (and (looking-at
  7083. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7084. (= (setq n (1- n)) 0))
  7085. (throw 'exit (match-string 1))))))))
  7086. (if txt
  7087. (progn
  7088. (if (and org-table-copy-increment
  7089. (string-match "^[0-9]+$" txt))
  7090. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7091. (insert txt)
  7092. (move-to-column col)
  7093. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7094. (org-timestamp-up 1)
  7095. (org-table-maybe-recalculate-line))
  7096. (org-table-align)
  7097. (move-to-column col))
  7098. (error "No non-empty field found"))))
  7099. (defun org-table-check-inside-data-field ()
  7100. "Is point inside a table data field?
  7101. I.e. not on a hline or before the first or after the last column?
  7102. This actually throws an error, so it aborts the current command."
  7103. (if (or (not (org-at-table-p))
  7104. (= (org-table-current-column) 0)
  7105. (org-at-table-hline-p)
  7106. (looking-at "[ \t]*$"))
  7107. (error "Not in table data field")))
  7108. (defvar org-table-clip nil
  7109. "Clipboard for table regions.")
  7110. (defun org-table-blank-field ()
  7111. "Blank the current table field or active region."
  7112. (interactive)
  7113. (org-table-check-inside-data-field)
  7114. (if (and (interactive-p) (org-region-active-p))
  7115. (let (org-table-clip)
  7116. (org-table-cut-region (region-beginning) (region-end)))
  7117. (skip-chars-backward "^|")
  7118. (backward-char 1)
  7119. (if (looking-at "|[^|\n]+")
  7120. (let* ((pos (match-beginning 0))
  7121. (match (match-string 0))
  7122. (len (org-string-width match)))
  7123. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7124. (goto-char (+ 2 pos))
  7125. (substring match 1)))))
  7126. (defun org-table-get-field (&optional n replace)
  7127. "Return the value of the field in column N of current row.
  7128. N defaults to current field.
  7129. If REPLACE is a string, replace field with this value. The return value
  7130. is always the old value."
  7131. (and n (org-table-goto-column n))
  7132. (skip-chars-backward "^|\n")
  7133. (backward-char 1)
  7134. (if (looking-at "|[^|\r\n]*")
  7135. (let* ((pos (match-beginning 0))
  7136. (val (buffer-substring (1+ pos) (match-end 0))))
  7137. (if replace
  7138. (replace-match (concat "|" replace) t t))
  7139. (goto-char (min (point-at-eol) (+ 2 pos)))
  7140. val)
  7141. (forward-char 1) ""))
  7142. (defun org-table-field-info (arg)
  7143. "Show info about the current field, and highlight any reference at point."
  7144. (interactive "P")
  7145. (org-table-get-specials)
  7146. (save-excursion
  7147. (let* ((pos (point))
  7148. (col (org-table-current-column))
  7149. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7150. (name (car (rassoc (list (org-current-line) col)
  7151. org-table-named-field-locations)))
  7152. (eql (org-table-get-stored-formulas))
  7153. (dline (org-table-current-dline))
  7154. (ref (format "@%d$%d" dline col))
  7155. (ref1 (org-table-convert-refs-to-an ref))
  7156. (fequation (or (assoc name eql) (assoc ref eql)))
  7157. (cequation (assoc (int-to-string col) eql))
  7158. (eqn (or fequation cequation)))
  7159. (goto-char pos)
  7160. (condition-case nil
  7161. (org-table-show-reference 'local)
  7162. (error nil))
  7163. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7164. dline col
  7165. (if cname (concat " or $" cname) "")
  7166. dline col ref1
  7167. (if name (concat " or $" name) "")
  7168. ;; FIXME: formula info not correct if special table line
  7169. (if eqn
  7170. (concat ", formula: "
  7171. (org-table-formula-to-user
  7172. (concat
  7173. (if (string-match "^[$@]"(car eqn)) "" "$")
  7174. (car eqn) "=" (cdr eqn))))
  7175. "")))))
  7176. (defun org-table-current-column ()
  7177. "Find out which column we are in.
  7178. When called interactively, column is also displayed in echo area."
  7179. (interactive)
  7180. (if (interactive-p) (org-table-check-inside-data-field))
  7181. (save-excursion
  7182. (let ((cnt 0) (pos (point)))
  7183. (beginning-of-line 1)
  7184. (while (search-forward "|" pos t)
  7185. (setq cnt (1+ cnt)))
  7186. (if (interactive-p) (message "This is table column %d" cnt))
  7187. cnt)))
  7188. (defun org-table-current-dline ()
  7189. "Find out what table data line we are in.
  7190. Only datalins count for this."
  7191. (interactive)
  7192. (if (interactive-p) (org-table-check-inside-data-field))
  7193. (save-excursion
  7194. (let ((cnt 0) (pos (point)))
  7195. (goto-char (org-table-begin))
  7196. (while (<= (point) pos)
  7197. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7198. (beginning-of-line 2))
  7199. (if (interactive-p) (message "This is table line %d" cnt))
  7200. cnt)))
  7201. (defun org-table-goto-column (n &optional on-delim force)
  7202. "Move the cursor to the Nth column in the current table line.
  7203. With optional argument ON-DELIM, stop with point before the left delimiter
  7204. of the field.
  7205. If there are less than N fields, just go to after the last delimiter.
  7206. However, when FORCE is non-nil, create new columns if necessary."
  7207. (interactive "p")
  7208. (let ((pos (point-at-eol)))
  7209. (beginning-of-line 1)
  7210. (when (> n 0)
  7211. (while (and (> (setq n (1- n)) -1)
  7212. (or (search-forward "|" pos t)
  7213. (and force
  7214. (progn (end-of-line 1)
  7215. (skip-chars-backward "^|")
  7216. (insert " | "))))))
  7217. ; (backward-char 2) t)))))
  7218. (when (and force (not (looking-at ".*|")))
  7219. (save-excursion (end-of-line 1) (insert " | ")))
  7220. (if on-delim
  7221. (backward-char 1)
  7222. (if (looking-at " ") (forward-char 1))))))
  7223. (defun org-at-table-p (&optional table-type)
  7224. "Return t if the cursor is inside an org-type table.
  7225. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7226. (if org-enable-table-editor
  7227. (save-excursion
  7228. (beginning-of-line 1)
  7229. (looking-at (if table-type org-table-any-line-regexp
  7230. org-table-line-regexp)))
  7231. nil))
  7232. (defun org-at-table.el-p ()
  7233. "Return t if and only if we are at a table.el table."
  7234. (and (org-at-table-p 'any)
  7235. (save-excursion
  7236. (goto-char (org-table-begin 'any))
  7237. (looking-at org-table1-hline-regexp))))
  7238. (defun org-table-recognize-table.el ()
  7239. "If there is a table.el table nearby, recognize it and move into it."
  7240. (if org-table-tab-recognizes-table.el
  7241. (if (org-at-table.el-p)
  7242. (progn
  7243. (beginning-of-line 1)
  7244. (if (looking-at org-table-dataline-regexp)
  7245. nil
  7246. (if (looking-at org-table1-hline-regexp)
  7247. (progn
  7248. (beginning-of-line 2)
  7249. (if (looking-at org-table-any-border-regexp)
  7250. (beginning-of-line -1)))))
  7251. (if (re-search-forward "|" (org-table-end t) t)
  7252. (progn
  7253. (require 'table)
  7254. (if (table--at-cell-p (point))
  7255. t
  7256. (message "recognizing table.el table...")
  7257. (table-recognize-table)
  7258. (message "recognizing table.el table...done")))
  7259. (error "This should not happen..."))
  7260. t)
  7261. nil)
  7262. nil))
  7263. (defun org-at-table-hline-p ()
  7264. "Return t if the cursor is inside a hline in a table."
  7265. (if org-enable-table-editor
  7266. (save-excursion
  7267. (beginning-of-line 1)
  7268. (looking-at org-table-hline-regexp))
  7269. nil))
  7270. (defun org-table-insert-column ()
  7271. "Insert a new column into the table."
  7272. (interactive)
  7273. (if (not (org-at-table-p))
  7274. (error "Not at a table"))
  7275. (org-table-find-dataline)
  7276. (let* ((col (max 1 (org-table-current-column)))
  7277. (beg (org-table-begin))
  7278. (end (org-table-end))
  7279. ;; Current cursor position
  7280. (linepos (org-current-line))
  7281. (colpos col))
  7282. (goto-char beg)
  7283. (while (< (point) end)
  7284. (if (org-at-table-hline-p)
  7285. nil
  7286. (org-table-goto-column col t)
  7287. (insert "| "))
  7288. (beginning-of-line 2))
  7289. (move-marker end nil)
  7290. (goto-line linepos)
  7291. (org-table-goto-column colpos)
  7292. (org-table-align)
  7293. (org-table-fix-formulas "$" nil (1- col) 1)))
  7294. (defun org-table-find-dataline ()
  7295. "Find a dataline in the current table, which is needed for column commands."
  7296. (if (and (org-at-table-p)
  7297. (not (org-at-table-hline-p)))
  7298. t
  7299. (let ((col (current-column))
  7300. (end (org-table-end)))
  7301. (move-to-column col)
  7302. (while (and (< (point) end)
  7303. (or (not (= (current-column) col))
  7304. (org-at-table-hline-p)))
  7305. (beginning-of-line 2)
  7306. (move-to-column col))
  7307. (if (and (org-at-table-p)
  7308. (not (org-at-table-hline-p)))
  7309. t
  7310. (error
  7311. "Please position cursor in a data line for column operations")))))
  7312. (defun org-table-delete-column ()
  7313. "Delete a column from the table."
  7314. (interactive)
  7315. (if (not (org-at-table-p))
  7316. (error "Not at a table"))
  7317. (org-table-find-dataline)
  7318. (org-table-check-inside-data-field)
  7319. (let* ((col (org-table-current-column))
  7320. (beg (org-table-begin))
  7321. (end (org-table-end))
  7322. ;; Current cursor position
  7323. (linepos (org-current-line))
  7324. (colpos col))
  7325. (goto-char beg)
  7326. (while (< (point) end)
  7327. (if (org-at-table-hline-p)
  7328. nil
  7329. (org-table-goto-column col t)
  7330. (and (looking-at "|[^|\n]+|")
  7331. (replace-match "|")))
  7332. (beginning-of-line 2))
  7333. (move-marker end nil)
  7334. (goto-line linepos)
  7335. (org-table-goto-column colpos)
  7336. (org-table-align)
  7337. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7338. col -1 col)))
  7339. (defun org-table-move-column-right ()
  7340. "Move column to the right."
  7341. (interactive)
  7342. (org-table-move-column nil))
  7343. (defun org-table-move-column-left ()
  7344. "Move column to the left."
  7345. (interactive)
  7346. (org-table-move-column 'left))
  7347. (defun org-table-move-column (&optional left)
  7348. "Move the current column to the right. With arg LEFT, move to the left."
  7349. (interactive "P")
  7350. (if (not (org-at-table-p))
  7351. (error "Not at a table"))
  7352. (org-table-find-dataline)
  7353. (org-table-check-inside-data-field)
  7354. (let* ((col (org-table-current-column))
  7355. (col1 (if left (1- col) col))
  7356. (beg (org-table-begin))
  7357. (end (org-table-end))
  7358. ;; Current cursor position
  7359. (linepos (org-current-line))
  7360. (colpos (if left (1- col) (1+ col))))
  7361. (if (and left (= col 1))
  7362. (error "Cannot move column further left"))
  7363. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  7364. (error "Cannot move column further right"))
  7365. (goto-char beg)
  7366. (while (< (point) end)
  7367. (if (org-at-table-hline-p)
  7368. nil
  7369. (org-table-goto-column col1 t)
  7370. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  7371. (replace-match "|\\2|\\1|")))
  7372. (beginning-of-line 2))
  7373. (move-marker end nil)
  7374. (goto-line linepos)
  7375. (org-table-goto-column colpos)
  7376. (org-table-align)
  7377. (org-table-fix-formulas
  7378. "$" (list (cons (number-to-string col) (number-to-string colpos))
  7379. (cons (number-to-string colpos) (number-to-string col))))))
  7380. (defun org-table-move-row-down ()
  7381. "Move table row down."
  7382. (interactive)
  7383. (org-table-move-row nil))
  7384. (defun org-table-move-row-up ()
  7385. "Move table row up."
  7386. (interactive)
  7387. (org-table-move-row 'up))
  7388. (defun org-table-move-row (&optional up)
  7389. "Move the current table line down. With arg UP, move it up."
  7390. (interactive "P")
  7391. (let* ((col (current-column))
  7392. (pos (point))
  7393. (hline1p (save-excursion (beginning-of-line 1)
  7394. (looking-at org-table-hline-regexp)))
  7395. (dline1 (org-table-current-dline))
  7396. (dline2 (+ dline1 (if up -1 1)))
  7397. (tonew (if up 0 2))
  7398. txt hline2p)
  7399. (beginning-of-line tonew)
  7400. (unless (org-at-table-p)
  7401. (goto-char pos)
  7402. (error "Cannot move row further"))
  7403. (setq hline2p (looking-at org-table-hline-regexp))
  7404. (goto-char pos)
  7405. (beginning-of-line 1)
  7406. (setq pos (point))
  7407. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  7408. (delete-region (point) (1+ (point-at-eol)))
  7409. (beginning-of-line tonew)
  7410. (insert txt)
  7411. (beginning-of-line 0)
  7412. (move-to-column col)
  7413. (unless (or hline1p hline2p)
  7414. (org-table-fix-formulas
  7415. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  7416. (cons (number-to-string dline2) (number-to-string dline1)))))))
  7417. (defun org-table-insert-row (&optional arg)
  7418. "Insert a new row above the current line into the table.
  7419. With prefix ARG, insert below the current line."
  7420. (interactive "P")
  7421. (if (not (org-at-table-p))
  7422. (error "Not at a table"))
  7423. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  7424. (new (org-table-clean-line line)))
  7425. ;; Fix the first field if necessary
  7426. (if (string-match "^[ \t]*| *[#$] *|" line)
  7427. (setq new (replace-match (match-string 0 line) t t new)))
  7428. (beginning-of-line (if arg 2 1))
  7429. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  7430. (beginning-of-line 0)
  7431. (re-search-forward "| ?" (point-at-eol) t)
  7432. (and (or org-table-may-need-update org-table-overlay-coordinates)
  7433. (org-table-align))
  7434. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  7435. (defun org-table-insert-hline (&optional above)
  7436. "Insert a horizontal-line below the current line into the table.
  7437. With prefix ABOVE, insert above the current line."
  7438. (interactive "P")
  7439. (if (not (org-at-table-p))
  7440. (error "Not at a table"))
  7441. (let ((line (org-table-clean-line
  7442. (buffer-substring (point-at-bol) (point-at-eol))))
  7443. (col (current-column)))
  7444. (while (string-match "|\\( +\\)|" line)
  7445. (setq line (replace-match
  7446. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  7447. ?-) "|") t t line)))
  7448. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  7449. (beginning-of-line (if above 1 2))
  7450. (insert line "\n")
  7451. (beginning-of-line (if above 1 -1))
  7452. (move-to-column col)
  7453. (and org-table-overlay-coordinates (org-table-align))))
  7454. (defun org-table-hline-and-move (&optional same-column)
  7455. "Insert a hline and move to the row below that line."
  7456. (interactive "P")
  7457. (let ((col (org-table-current-column)))
  7458. (org-table-maybe-eval-formula)
  7459. (org-table-maybe-recalculate-line)
  7460. (org-table-insert-hline)
  7461. (end-of-line 2)
  7462. (if (looking-at "\n[ \t]*|-")
  7463. (progn (insert "\n|") (org-table-align))
  7464. (org-table-next-field))
  7465. (if same-column (org-table-goto-column col))))
  7466. (defun org-table-clean-line (s)
  7467. "Convert a table line S into a string with only \"|\" and space.
  7468. In particular, this does handle wide and invisible characters."
  7469. (if (string-match "^[ \t]*|-" s)
  7470. ;; It's a hline, just map the characters
  7471. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  7472. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  7473. (setq s (replace-match
  7474. (concat "|" (make-string (org-string-width (match-string 1 s))
  7475. ?\ ) "|")
  7476. t t s)))
  7477. s))
  7478. (defun org-table-kill-row ()
  7479. "Delete the current row or horizontal line from the table."
  7480. (interactive)
  7481. (if (not (org-at-table-p))
  7482. (error "Not at a table"))
  7483. (let ((col (current-column))
  7484. (dline (org-table-current-dline)))
  7485. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  7486. (if (not (org-at-table-p)) (beginning-of-line 0))
  7487. (move-to-column col)
  7488. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  7489. dline -1 dline)))
  7490. (defun org-table-sort-lines (with-case &optional sorting-type)
  7491. "Sort table lines according to the column at point.
  7492. The position of point indicates the column to be used for
  7493. sorting, and the range of lines is the range between the nearest
  7494. horizontal separator lines, or the entire table of no such lines
  7495. exist. If point is before the first column, you will be prompted
  7496. for the sorting column. If there is an active region, the mark
  7497. specifies the first line and the sorting column, while point
  7498. should be in the last line to be included into the sorting.
  7499. The command then prompts for the sorting type which can be
  7500. alphabetically, numerically, or by time (as given in a time stamp
  7501. in the field). Sorting in reverse order is also possible.
  7502. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  7503. If SORTING-TYPE is specified when this function is called from a Lisp
  7504. program, no prompting will take place. SORTING-TYPE must be a character,
  7505. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  7506. should be done in reverse order."
  7507. (interactive "P")
  7508. (let* ((thisline (org-current-line))
  7509. (thiscol (org-table-current-column))
  7510. beg end bcol ecol tend tbeg column lns pos)
  7511. (when (equal thiscol 0)
  7512. (if (interactive-p)
  7513. (setq thiscol
  7514. (string-to-number
  7515. (read-string "Use column N for sorting: ")))
  7516. (setq thiscol 1))
  7517. (org-table-goto-column thiscol))
  7518. (org-table-check-inside-data-field)
  7519. (if (org-region-active-p)
  7520. (progn
  7521. (setq beg (region-beginning) end (region-end))
  7522. (goto-char beg)
  7523. (setq column (org-table-current-column)
  7524. beg (point-at-bol))
  7525. (goto-char end)
  7526. (setq end (point-at-bol 2)))
  7527. (setq column (org-table-current-column)
  7528. pos (point)
  7529. tbeg (org-table-begin)
  7530. tend (org-table-end))
  7531. (if (re-search-backward org-table-hline-regexp tbeg t)
  7532. (setq beg (point-at-bol 2))
  7533. (goto-char tbeg)
  7534. (setq beg (point-at-bol 1)))
  7535. (goto-char pos)
  7536. (if (re-search-forward org-table-hline-regexp tend t)
  7537. (setq end (point-at-bol 1))
  7538. (goto-char tend)
  7539. (setq end (point-at-bol))))
  7540. (setq beg (move-marker (make-marker) beg)
  7541. end (move-marker (make-marker) end))
  7542. (untabify beg end)
  7543. (goto-char beg)
  7544. (org-table-goto-column column)
  7545. (skip-chars-backward "^|")
  7546. (setq bcol (current-column))
  7547. (org-table-goto-column (1+ column))
  7548. (skip-chars-backward "^|")
  7549. (setq ecol (1- (current-column)))
  7550. (org-table-goto-column column)
  7551. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  7552. (org-split-string (buffer-substring beg end) "\n")))
  7553. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  7554. (delete-region beg end)
  7555. (move-marker beg nil)
  7556. (move-marker end nil)
  7557. (insert (mapconcat 'cdr lns "\n") "\n")
  7558. (goto-line thisline)
  7559. (org-table-goto-column thiscol)
  7560. (message "%d lines sorted, based on column %d" (length lns) column)))
  7561. (defun org-table-cut-region (beg end)
  7562. "Copy region in table to the clipboard and blank all relevant fields."
  7563. (interactive "r")
  7564. (org-table-copy-region beg end 'cut))
  7565. (defun org-table-copy-region (beg end &optional cut)
  7566. "Copy rectangular region in table to clipboard.
  7567. A special clipboard is used which can only be accessed
  7568. with `org-table-paste-rectangle'."
  7569. (interactive "rP")
  7570. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7571. region cols
  7572. (rpl (if cut " " nil)))
  7573. (goto-char beg)
  7574. (org-table-check-inside-data-field)
  7575. (setq l01 (org-current-line)
  7576. c01 (org-table-current-column))
  7577. (goto-char end)
  7578. (org-table-check-inside-data-field)
  7579. (setq l02 (org-current-line)
  7580. c02 (org-table-current-column))
  7581. (setq l1 (min l01 l02) l2 (max l01 l02)
  7582. c1 (min c01 c02) c2 (max c01 c02))
  7583. (catch 'exit
  7584. (while t
  7585. (catch 'nextline
  7586. (if (> l1 l2) (throw 'exit t))
  7587. (goto-line l1)
  7588. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7589. (setq cols nil ic1 c1 ic2 c2)
  7590. (while (< ic1 (1+ ic2))
  7591. (push (org-table-get-field ic1 rpl) cols)
  7592. (setq ic1 (1+ ic1)))
  7593. (push (nreverse cols) region)
  7594. (setq l1 (1+ l1)))))
  7595. (setq org-table-clip (nreverse region))
  7596. (if cut (org-table-align))
  7597. org-table-clip))
  7598. (defun org-table-paste-rectangle ()
  7599. "Paste a rectangular region into a table.
  7600. The upper right corner ends up in the current field. All involved fields
  7601. will be overwritten. If the rectangle does not fit into the present table,
  7602. the table is enlarged as needed. The process ignores horizontal separator
  7603. lines."
  7604. (interactive)
  7605. (unless (and org-table-clip (listp org-table-clip))
  7606. (error "First cut/copy a region to paste!"))
  7607. (org-table-check-inside-data-field)
  7608. (let* ((clip org-table-clip)
  7609. (line (org-current-line))
  7610. (col (org-table-current-column))
  7611. (org-enable-table-editor t)
  7612. (org-table-automatic-realign nil)
  7613. c cols field)
  7614. (while (setq cols (pop clip))
  7615. (while (org-at-table-hline-p) (beginning-of-line 2))
  7616. (if (not (org-at-table-p))
  7617. (progn (end-of-line 0) (org-table-next-field)))
  7618. (setq c col)
  7619. (while (setq field (pop cols))
  7620. (org-table-goto-column c nil 'force)
  7621. (org-table-get-field nil field)
  7622. (setq c (1+ c)))
  7623. (beginning-of-line 2))
  7624. (goto-line line)
  7625. (org-table-goto-column col)
  7626. (org-table-align)))
  7627. (defun org-table-convert ()
  7628. "Convert from `org-mode' table to table.el and back.
  7629. Obviously, this only works within limits. When an Org-mode table is
  7630. converted to table.el, all horizontal separator lines get lost, because
  7631. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7632. A table.el table can be converted to an Org-mode table only if it does not
  7633. do row or column spanning. Multiline cells will become multiple cells.
  7634. Beware, Org-mode does not test if the table can be successfully converted - it
  7635. blindly applies a recipe that works for simple tables."
  7636. (interactive)
  7637. (require 'table)
  7638. (if (org-at-table.el-p)
  7639. ;; convert to Org-mode table
  7640. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7641. (end (move-marker (make-marker) (org-table-end t))))
  7642. (table-unrecognize-region beg end)
  7643. (goto-char beg)
  7644. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7645. (replace-match ""))
  7646. (goto-char beg))
  7647. (if (org-at-table-p)
  7648. ;; convert to table.el table
  7649. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7650. (end (move-marker (make-marker) (org-table-end))))
  7651. ;; first, get rid of all horizontal lines
  7652. (goto-char beg)
  7653. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7654. (replace-match ""))
  7655. ;; insert a hline before first
  7656. (goto-char beg)
  7657. (org-table-insert-hline 'above)
  7658. (beginning-of-line -1)
  7659. ;; insert a hline after each line
  7660. (while (progn (beginning-of-line 3) (< (point) end))
  7661. (org-table-insert-hline))
  7662. (goto-char beg)
  7663. (setq end (move-marker end (org-table-end)))
  7664. ;; replace "+" at beginning and ending of hlines
  7665. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  7666. (replace-match "\\1+-"))
  7667. (goto-char beg)
  7668. (while (re-search-forward "-|[ \t]*$" end t)
  7669. (replace-match "-+"))
  7670. (goto-char beg)))))
  7671. (defun org-table-wrap-region (arg)
  7672. "Wrap several fields in a column like a paragraph.
  7673. This is useful if you'd like to spread the contents of a field over several
  7674. lines, in order to keep the table compact.
  7675. If there is an active region, and both point and mark are in the same column,
  7676. the text in the column is wrapped to minimum width for the given number of
  7677. lines. Generally, this makes the table more compact. A prefix ARG may be
  7678. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  7679. formats the selected text to two lines. If the region was longer than two
  7680. lines, the remaining lines remain empty. A negative prefix argument reduces
  7681. the current number of lines by that amount. The wrapped text is pasted back
  7682. into the table. If you formatted it to more lines than it was before, fields
  7683. further down in the table get overwritten - so you might need to make space in
  7684. the table first.
  7685. If there is no region, the current field is split at the cursor position and
  7686. the text fragment to the right of the cursor is prepended to the field one
  7687. line down.
  7688. If there is no region, but you specify a prefix ARG, the current field gets
  7689. blank, and the content is appended to the field above."
  7690. (interactive "P")
  7691. (org-table-check-inside-data-field)
  7692. (if (org-region-active-p)
  7693. ;; There is a region: fill as a paragraph
  7694. (let* ((beg (region-beginning))
  7695. (cline (save-excursion (goto-char beg) (org-current-line)))
  7696. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  7697. nlines)
  7698. (org-table-cut-region (region-beginning) (region-end))
  7699. (if (> (length (car org-table-clip)) 1)
  7700. (error "Region must be limited to single column"))
  7701. (setq nlines (if arg
  7702. (if (< arg 1)
  7703. (+ (length org-table-clip) arg)
  7704. arg)
  7705. (length org-table-clip)))
  7706. (setq org-table-clip
  7707. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  7708. nil nlines)))
  7709. (goto-line cline)
  7710. (org-table-goto-column ccol)
  7711. (org-table-paste-rectangle))
  7712. ;; No region, split the current field at point
  7713. (if arg
  7714. ;; combine with field above
  7715. (let ((s (org-table-blank-field))
  7716. (col (org-table-current-column)))
  7717. (beginning-of-line 0)
  7718. (while (org-at-table-hline-p) (beginning-of-line 0))
  7719. (org-table-goto-column col)
  7720. (skip-chars-forward "^|")
  7721. (skip-chars-backward " ")
  7722. (insert " " (org-trim s))
  7723. (org-table-align))
  7724. ;; split field
  7725. (when (looking-at "\\([^|]+\\)+|")
  7726. (let ((s (match-string 1)))
  7727. (replace-match " |")
  7728. (goto-char (match-beginning 0))
  7729. (org-table-next-row)
  7730. (insert (org-trim s) " ")
  7731. (org-table-align))))))
  7732. (defvar org-field-marker nil)
  7733. (defun org-table-edit-field (arg)
  7734. "Edit table field in a different window.
  7735. This is mainly useful for fields that contain hidden parts.
  7736. When called with a \\[universal-argument] prefix, just make the full field visible so that
  7737. it can be edited in place."
  7738. (interactive "P")
  7739. (if arg
  7740. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  7741. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  7742. (remove-text-properties b e '(org-cwidth t invisible t
  7743. display t intangible t))
  7744. (if (and (boundp 'font-lock-mode) font-lock-mode)
  7745. (font-lock-fontify-block)))
  7746. (let ((pos (move-marker (make-marker) (point)))
  7747. (field (org-table-get-field))
  7748. (cw (current-window-configuration))
  7749. p)
  7750. (org-switch-to-buffer-other-window "*Org tmp*")
  7751. (erase-buffer)
  7752. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  7753. (let ((org-inhibit-startup t)) (org-mode))
  7754. (goto-char (setq p (point-max)))
  7755. (insert (org-trim field))
  7756. (remove-text-properties p (point-max)
  7757. '(invisible t org-cwidth t display t
  7758. intangible t))
  7759. (goto-char p)
  7760. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  7761. (org-set-local 'org-window-configuration cw)
  7762. (org-set-local 'org-field-marker pos)
  7763. (message "Edit and finish with C-c C-c"))))
  7764. (defun org-table-finish-edit-field ()
  7765. "Finish editing a table data field.
  7766. Remove all newline characters, insert the result into the table, realign
  7767. the table and kill the editing buffer."
  7768. (let ((pos org-field-marker)
  7769. (cw org-window-configuration)
  7770. (cb (current-buffer))
  7771. text)
  7772. (goto-char (point-min))
  7773. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  7774. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  7775. (replace-match " "))
  7776. (setq text (org-trim (buffer-string)))
  7777. (set-window-configuration cw)
  7778. (kill-buffer cb)
  7779. (select-window (get-buffer-window (marker-buffer pos)))
  7780. (goto-char pos)
  7781. (move-marker pos nil)
  7782. (org-table-check-inside-data-field)
  7783. (org-table-get-field nil text)
  7784. (org-table-align)
  7785. (message "New field value inserted")))
  7786. (defun org-trim (s)
  7787. "Remove whitespace at beginning and end of string."
  7788. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  7789. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  7790. s)
  7791. (defun org-wrap (string &optional width lines)
  7792. "Wrap string to either a number of lines, or a width in characters.
  7793. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  7794. that costs. If there is a word longer than WIDTH, the text is actually
  7795. wrapped to the length of that word.
  7796. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  7797. many lines, whatever width that takes.
  7798. The return value is a list of lines, without newlines at the end."
  7799. (let* ((words (org-split-string string "[ \t\n]+"))
  7800. (maxword (apply 'max (mapcar 'org-string-width words)))
  7801. w ll)
  7802. (cond (width
  7803. (org-do-wrap words (max maxword width)))
  7804. (lines
  7805. (setq w maxword)
  7806. (setq ll (org-do-wrap words maxword))
  7807. (if (<= (length ll) lines)
  7808. ll
  7809. (setq ll words)
  7810. (while (> (length ll) lines)
  7811. (setq w (1+ w))
  7812. (setq ll (org-do-wrap words w)))
  7813. ll))
  7814. (t (error "Cannot wrap this")))))
  7815. (defun org-do-wrap (words width)
  7816. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  7817. (let (lines line)
  7818. (while words
  7819. (setq line (pop words))
  7820. (while (and words (< (+ (length line) (length (car words))) width))
  7821. (setq line (concat line " " (pop words))))
  7822. (setq lines (push line lines)))
  7823. (nreverse lines)))
  7824. (defun org-split-string (string &optional separators)
  7825. "Splits STRING into substrings at SEPARATORS.
  7826. No empty strings are returned if there are matches at the beginning
  7827. and end of string."
  7828. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  7829. (start 0)
  7830. notfirst
  7831. (list nil))
  7832. (while (and (string-match rexp string
  7833. (if (and notfirst
  7834. (= start (match-beginning 0))
  7835. (< start (length string)))
  7836. (1+ start) start))
  7837. (< (match-beginning 0) (length string)))
  7838. (setq notfirst t)
  7839. (or (eq (match-beginning 0) 0)
  7840. (and (eq (match-beginning 0) (match-end 0))
  7841. (eq (match-beginning 0) start))
  7842. (setq list
  7843. (cons (substring string start (match-beginning 0))
  7844. list)))
  7845. (setq start (match-end 0)))
  7846. (or (eq start (length string))
  7847. (setq list
  7848. (cons (substring string start)
  7849. list)))
  7850. (nreverse list)))
  7851. (defun org-table-map-tables (function)
  7852. "Apply FUNCTION to the start of all tables in the buffer."
  7853. (save-excursion
  7854. (save-restriction
  7855. (widen)
  7856. (goto-char (point-min))
  7857. (while (re-search-forward org-table-any-line-regexp nil t)
  7858. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  7859. (beginning-of-line 1)
  7860. (if (looking-at org-table-line-regexp)
  7861. (save-excursion (funcall function)))
  7862. (re-search-forward org-table-any-border-regexp nil 1))))
  7863. (message "Mapping tables: done"))
  7864. (defvar org-timecnt) ; dynamically scoped parameter
  7865. (defun org-table-sum (&optional beg end nlast)
  7866. "Sum numbers in region of current table column.
  7867. The result will be displayed in the echo area, and will be available
  7868. as kill to be inserted with \\[yank].
  7869. If there is an active region, it is interpreted as a rectangle and all
  7870. numbers in that rectangle will be summed. If there is no active
  7871. region and point is located in a table column, sum all numbers in that
  7872. column.
  7873. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  7874. numbers are assumed to be times as well (in decimal hours) and the
  7875. numbers are added as such.
  7876. If NLAST is a number, only the NLAST fields will actually be summed."
  7877. (interactive)
  7878. (save-excursion
  7879. (let (col (org-timecnt 0) diff h m s org-table-clip)
  7880. (cond
  7881. ((and beg end)) ; beg and end given explicitly
  7882. ((org-region-active-p)
  7883. (setq beg (region-beginning) end (region-end)))
  7884. (t
  7885. (setq col (org-table-current-column))
  7886. (goto-char (org-table-begin))
  7887. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  7888. (error "No table data"))
  7889. (org-table-goto-column col)
  7890. (setq beg (point))
  7891. (goto-char (org-table-end))
  7892. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  7893. (error "No table data"))
  7894. (org-table-goto-column col)
  7895. (setq end (point))))
  7896. (let* ((items (apply 'append (org-table-copy-region beg end)))
  7897. (items1 (cond ((not nlast) items)
  7898. ((>= nlast (length items)) items)
  7899. (t (setq items (reverse items))
  7900. (setcdr (nthcdr (1- nlast) items) nil)
  7901. (nreverse items))))
  7902. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  7903. items1)))
  7904. (res (apply '+ numbers))
  7905. (sres (if (= org-timecnt 0)
  7906. (format "%g" res)
  7907. (setq diff (* 3600 res)
  7908. h (floor (/ diff 3600)) diff (mod diff 3600)
  7909. m (floor (/ diff 60)) diff (mod diff 60)
  7910. s diff)
  7911. (format "%d:%02d:%02d" h m s))))
  7912. (kill-new sres)
  7913. (if (interactive-p)
  7914. (message "%s"
  7915. (substitute-command-keys
  7916. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  7917. (length numbers) sres))))
  7918. sres))))
  7919. (defun org-table-get-number-for-summing (s)
  7920. (let (n)
  7921. (if (string-match "^ *|? *" s)
  7922. (setq s (replace-match "" nil nil s)))
  7923. (if (string-match " *|? *$" s)
  7924. (setq s (replace-match "" nil nil s)))
  7925. (setq n (string-to-number s))
  7926. (cond
  7927. ((and (string-match "0" s)
  7928. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  7929. ((string-match "\\`[ \t]+\\'" s) nil)
  7930. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  7931. (let ((h (string-to-number (or (match-string 1 s) "0")))
  7932. (m (string-to-number (or (match-string 2 s) "0")))
  7933. (s (string-to-number (or (match-string 4 s) "0"))))
  7934. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  7935. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  7936. ((equal n 0) nil)
  7937. (t n))))
  7938. (defun org-table-current-field-formula (&optional key noerror)
  7939. "Return the formula active for the current field.
  7940. Assumes that specials are in place.
  7941. If KEY is given, return the key to this formula.
  7942. Otherwise return the formula preceeded with \"=\" or \":=\"."
  7943. (let* ((name (car (rassoc (list (org-current-line)
  7944. (org-table-current-column))
  7945. org-table-named-field-locations)))
  7946. (col (org-table-current-column))
  7947. (scol (int-to-string col))
  7948. (ref (format "@%d$%d" (org-table-current-dline) col))
  7949. (stored-list (org-table-get-stored-formulas noerror))
  7950. (ass (or (assoc name stored-list)
  7951. (assoc ref stored-list)
  7952. (assoc scol stored-list))))
  7953. (if key
  7954. (car ass)
  7955. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  7956. (cdr ass))))))
  7957. (defun org-table-get-formula (&optional equation named)
  7958. "Read a formula from the minibuffer, offer stored formula as default.
  7959. When NAMED is non-nil, look for a named equation."
  7960. (let* ((stored-list (org-table-get-stored-formulas))
  7961. (name (car (rassoc (list (org-current-line)
  7962. (org-table-current-column))
  7963. org-table-named-field-locations)))
  7964. (ref (format "@%d$%d" (org-table-current-dline)
  7965. (org-table-current-column)))
  7966. (refass (assoc ref stored-list))
  7967. (scol (if named
  7968. (if name name ref)
  7969. (int-to-string (org-table-current-column))))
  7970. (dummy (and (or name refass) (not named)
  7971. (not (y-or-n-p "Replace field formula with column formula? " ))
  7972. (error "Abort")))
  7973. (name (or name ref))
  7974. (org-table-may-need-update nil)
  7975. (stored (cdr (assoc scol stored-list)))
  7976. (eq (cond
  7977. ((and stored equation (string-match "^ *=? *$" equation))
  7978. stored)
  7979. ((stringp equation)
  7980. equation)
  7981. (t (org-table-formula-from-user
  7982. (read-string
  7983. (org-table-formula-to-user
  7984. (format "%s formula %s%s="
  7985. (if named "Field" "Column")
  7986. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  7987. scol))
  7988. (if stored (org-table-formula-to-user stored) "")
  7989. 'org-table-formula-history
  7990. )))))
  7991. mustsave)
  7992. (when (not (string-match "\\S-" eq))
  7993. ;; remove formula
  7994. (setq stored-list (delq (assoc scol stored-list) stored-list))
  7995. (org-table-store-formulas stored-list)
  7996. (error "Formula removed"))
  7997. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  7998. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  7999. (if (and name (not named))
  8000. ;; We set the column equation, delete the named one.
  8001. (setq stored-list (delq (assoc name stored-list) stored-list)
  8002. mustsave t))
  8003. (if stored
  8004. (setcdr (assoc scol stored-list) eq)
  8005. (setq stored-list (cons (cons scol eq) stored-list)))
  8006. (if (or mustsave (not (equal stored eq)))
  8007. (org-table-store-formulas stored-list))
  8008. eq))
  8009. (defun org-table-store-formulas (alist)
  8010. "Store the list of formulas below the current table."
  8011. (setq alist (sort alist 'org-table-formula-less-p))
  8012. (save-excursion
  8013. (goto-char (org-table-end))
  8014. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8015. (progn
  8016. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8017. (goto-char (match-beginning 2))
  8018. (delete-region (match-beginning 2) (match-end 0)))
  8019. (insert "#+TBLFM:"))
  8020. (insert " "
  8021. (mapconcat (lambda (x)
  8022. (concat
  8023. (if (equal (string-to-char (car x)) ?@) "" "$")
  8024. (car x) "=" (cdr x)))
  8025. alist "::")
  8026. "\n")))
  8027. (defsubst org-table-formula-make-cmp-string (a)
  8028. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8029. (concat
  8030. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8031. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8032. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8033. (defun org-table-formula-less-p (a b)
  8034. "Compare two formulas for sorting."
  8035. (let ((as (org-table-formula-make-cmp-string (car a)))
  8036. (bs (org-table-formula-make-cmp-string (car b))))
  8037. (and as bs (string< as bs))))
  8038. (defun org-table-get-stored-formulas (&optional noerror)
  8039. "Return an alist with the stored formulas directly after current table."
  8040. (interactive)
  8041. (let (scol eq eq-alist strings string seen)
  8042. (save-excursion
  8043. (goto-char (org-table-end))
  8044. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8045. (setq strings (org-split-string (match-string 2) " *:: *"))
  8046. (while (setq string (pop strings))
  8047. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8048. (setq scol (if (match-end 2)
  8049. (match-string 2 string)
  8050. (match-string 1 string))
  8051. eq (match-string 3 string)
  8052. eq-alist (cons (cons scol eq) eq-alist))
  8053. (if (member scol seen)
  8054. (if noerror
  8055. (progn
  8056. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8057. (ding)
  8058. (sit-for 2))
  8059. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8060. (push scol seen))))))
  8061. (nreverse eq-alist)))
  8062. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8063. "Modify the equations after the table structure has been edited.
  8064. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8065. For all numbers larger than LIMIT, shift them by DELTA."
  8066. (save-excursion
  8067. (goto-char (org-table-end))
  8068. (when (looking-at "#\\+TBLFM:")
  8069. (let ((re (concat key "\\([0-9]+\\)"))
  8070. (re2
  8071. (when remove
  8072. (if (equal key "$")
  8073. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8074. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8075. s n a)
  8076. (when remove
  8077. (while (re-search-forward re2 (point-at-eol) t)
  8078. (replace-match "")))
  8079. (while (re-search-forward re (point-at-eol) t)
  8080. (setq s (match-string 1) n (string-to-number s))
  8081. (cond
  8082. ((setq a (assoc s replace))
  8083. (replace-match (concat key (cdr a)) t t))
  8084. ((and limit (> n limit))
  8085. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8086. (defun org-table-get-specials ()
  8087. "Get the column names and local parameters for this table."
  8088. (save-excursion
  8089. (let ((beg (org-table-begin)) (end (org-table-end))
  8090. names name fields fields1 field cnt
  8091. c v l line col types dlines hlines)
  8092. (setq org-table-column-names nil
  8093. org-table-local-parameters nil
  8094. org-table-named-field-locations nil
  8095. org-table-current-begin-line nil
  8096. org-table-current-begin-pos nil
  8097. org-table-current-line-types nil)
  8098. (goto-char beg)
  8099. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8100. (setq names (org-split-string (match-string 1) " *| *")
  8101. cnt 1)
  8102. (while (setq name (pop names))
  8103. (setq cnt (1+ cnt))
  8104. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8105. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8106. (setq org-table-column-names (nreverse org-table-column-names))
  8107. (setq org-table-column-name-regexp
  8108. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8109. (goto-char beg)
  8110. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8111. (setq fields (org-split-string (match-string 1) " *| *"))
  8112. (while (setq field (pop fields))
  8113. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8114. (push (cons (match-string 1 field) (match-string 2 field))
  8115. org-table-local-parameters))))
  8116. (goto-char beg)
  8117. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8118. (setq c (match-string 1)
  8119. fields (org-split-string (match-string 2) " *| *"))
  8120. (save-excursion
  8121. (beginning-of-line (if (equal c "_") 2 0))
  8122. (setq line (org-current-line) col 1)
  8123. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8124. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8125. (while (and fields1 (setq field (pop fields)))
  8126. (setq v (pop fields1) col (1+ col))
  8127. (when (and (stringp field) (stringp v)
  8128. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8129. (push (cons field v) org-table-local-parameters)
  8130. (push (list field line col) org-table-named-field-locations))))
  8131. ;; Analyse the line types
  8132. (goto-char beg)
  8133. (setq org-table-current-begin-line (org-current-line)
  8134. org-table-current-begin-pos (point)
  8135. l org-table-current-begin-line)
  8136. (while (looking-at "[ \t]*|\\(-\\)?")
  8137. (push (if (match-end 1) 'hline 'dline) types)
  8138. (if (match-end 1) (push l hlines) (push l dlines))
  8139. (beginning-of-line 2)
  8140. (setq l (1+ l)))
  8141. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8142. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8143. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8144. (defun org-table-maybe-eval-formula ()
  8145. "Check if the current field starts with \"=\" or \":=\".
  8146. If yes, store the formula and apply it."
  8147. ;; We already know we are in a table. Get field will only return a formula
  8148. ;; when appropriate. It might return a separator line, but no problem.
  8149. (when org-table-formula-evaluate-inline
  8150. (let* ((field (org-trim (or (org-table-get-field) "")))
  8151. named eq)
  8152. (when (string-match "^:?=\\(.*\\)" field)
  8153. (setq named (equal (string-to-char field) ?:)
  8154. eq (match-string 1 field))
  8155. (if (or (fboundp 'calc-eval)
  8156. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8157. (org-table-eval-formula (if named '(4) nil)
  8158. (org-table-formula-from-user eq))
  8159. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8160. (defvar org-recalc-commands nil
  8161. "List of commands triggering the recalculation of a line.
  8162. Will be filled automatically during use.")
  8163. (defvar org-recalc-marks
  8164. '((" " . "Unmarked: no special line, no automatic recalculation")
  8165. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8166. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8167. ("!" . "Column name definition line. Reference in formula as $name.")
  8168. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8169. ("_" . "Names for values in row below this one.")
  8170. ("^" . "Names for values in row above this one.")))
  8171. (defun org-table-rotate-recalc-marks (&optional newchar)
  8172. "Rotate the recalculation mark in the first column.
  8173. If in any row, the first field is not consistent with a mark,
  8174. insert a new column for the markers.
  8175. When there is an active region, change all the lines in the region,
  8176. after prompting for the marking character.
  8177. After each change, a message will be displayed indicating the meaning
  8178. of the new mark."
  8179. (interactive)
  8180. (unless (org-at-table-p) (error "Not at a table"))
  8181. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8182. (beg (org-table-begin))
  8183. (end (org-table-end))
  8184. (l (org-current-line))
  8185. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8186. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8187. (have-col
  8188. (save-excursion
  8189. (goto-char beg)
  8190. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8191. (col (org-table-current-column))
  8192. (forcenew (car (assoc newchar org-recalc-marks)))
  8193. epos new)
  8194. (when l1
  8195. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8196. (setq newchar (char-to-string (read-char-exclusive))
  8197. forcenew (car (assoc newchar org-recalc-marks))))
  8198. (if (and newchar (not forcenew))
  8199. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8200. newchar))
  8201. (if l1 (goto-line l1))
  8202. (save-excursion
  8203. (beginning-of-line 1)
  8204. (unless (looking-at org-table-dataline-regexp)
  8205. (error "Not at a table data line")))
  8206. (unless have-col
  8207. (org-table-goto-column 1)
  8208. (org-table-insert-column)
  8209. (org-table-goto-column (1+ col)))
  8210. (setq epos (point-at-eol))
  8211. (save-excursion
  8212. (beginning-of-line 1)
  8213. (org-table-get-field
  8214. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8215. (concat " "
  8216. (setq new (or forcenew
  8217. (cadr (member (match-string 1) marks))))
  8218. " ")
  8219. " # ")))
  8220. (if (and l1 l2)
  8221. (progn
  8222. (goto-line l1)
  8223. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8224. (and (looking-at org-table-dataline-regexp)
  8225. (org-table-get-field 1 (concat " " new " "))))
  8226. (goto-line l1)))
  8227. (if (not (= epos (point-at-eol))) (org-table-align))
  8228. (goto-line l)
  8229. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  8230. (defun org-table-maybe-recalculate-line ()
  8231. "Recompute the current line if marked for it, and if we haven't just done it."
  8232. (interactive)
  8233. (and org-table-allow-automatic-line-recalculation
  8234. (not (and (memq last-command org-recalc-commands)
  8235. (equal org-last-recalc-line (org-current-line))))
  8236. (save-excursion (beginning-of-line 1)
  8237. (looking-at org-table-auto-recalculate-regexp))
  8238. (org-table-recalculate) t))
  8239. (defvar org-table-formula-debug nil
  8240. "Non-nil means, debug table formulas.
  8241. When nil, simply write \"#ERROR\" in corrupted fields.")
  8242. (make-variable-buffer-local 'org-table-formula-debug)
  8243. (defvar modes)
  8244. (defsubst org-set-calc-mode (var &optional value)
  8245. (if (stringp var)
  8246. (setq var (assoc var '(("D" calc-angle-mode deg)
  8247. ("R" calc-angle-mode rad)
  8248. ("F" calc-prefer-frac t)
  8249. ("S" calc-symbolic-mode t)))
  8250. value (nth 2 var) var (nth 1 var)))
  8251. (if (memq var modes)
  8252. (setcar (cdr (memq var modes)) value)
  8253. (cons var (cons value modes)))
  8254. modes)
  8255. (defun org-table-eval-formula (&optional arg equation
  8256. suppress-align suppress-const
  8257. suppress-store suppress-analysis)
  8258. "Replace the table field value at the cursor by the result of a calculation.
  8259. This function makes use of Dave Gillespie's Calc package, in my view the
  8260. most exciting program ever written for GNU Emacs. So you need to have Calc
  8261. installed in order to use this function.
  8262. In a table, this command replaces the value in the current field with the
  8263. result of a formula. It also installs the formula as the \"current\" column
  8264. formula, by storing it in a special line below the table. When called
  8265. with a `C-u' prefix, the current field must ba a named field, and the
  8266. formula is installed as valid in only this specific field.
  8267. When called with two `C-u' prefixes, insert the active equation
  8268. for the field back into the current field, so that it can be
  8269. edited there. This is useful in order to use \\[org-table-show-reference]
  8270. to check the referenced fields.
  8271. When called, the command first prompts for a formula, which is read in
  8272. the minibuffer. Previously entered formulas are available through the
  8273. history list, and the last used formula is offered as a default.
  8274. These stored formulas are adapted correctly when moving, inserting, or
  8275. deleting columns with the corresponding commands.
  8276. The formula can be any algebraic expression understood by the Calc package.
  8277. For details, see the Org-mode manual.
  8278. This function can also be called from Lisp programs and offers
  8279. additional arguments: EQUATION can be the formula to apply. If this
  8280. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8281. used to speed-up recursive calls by by-passing unnecessary aligns.
  8282. SUPPRESS-CONST suppresses the interpretation of constants in the
  8283. formula, assuming that this has been done already outside the function.
  8284. SUPPRESS-STORE means the formula should not be stored, either because
  8285. it is already stored, or because it is a modified equation that should
  8286. not overwrite the stored one."
  8287. (interactive "P")
  8288. (org-table-check-inside-data-field)
  8289. (or suppress-analysis (org-table-get-specials))
  8290. (if (equal arg '(16))
  8291. (let ((eq (org-table-current-field-formula)))
  8292. (or eq (error "No equation active for current field"))
  8293. (org-table-get-field nil eq)
  8294. (org-table-align)
  8295. (setq org-table-may-need-update t))
  8296. (let* (fields
  8297. (ndown (if (integerp arg) arg 1))
  8298. (org-table-automatic-realign nil)
  8299. (case-fold-search nil)
  8300. (down (> ndown 1))
  8301. (formula (if (and equation suppress-store)
  8302. equation
  8303. (org-table-get-formula equation (equal arg '(4)))))
  8304. (n0 (org-table-current-column))
  8305. (modes (copy-sequence org-calc-default-modes))
  8306. (numbers nil) ; was a variable, now fixed default
  8307. (keep-empty nil)
  8308. n form form0 bw fmt x ev orig c lispp literal)
  8309. ;; Parse the format string. Since we have a lot of modes, this is
  8310. ;; a lot of work. However, I think calc still uses most of the time.
  8311. (if (string-match ";" formula)
  8312. (let ((tmp (org-split-string formula ";")))
  8313. (setq formula (car tmp)
  8314. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8315. (nth 1 tmp)))
  8316. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8317. (setq c (string-to-char (match-string 1 fmt))
  8318. n (string-to-number (match-string 2 fmt)))
  8319. (if (= c ?p)
  8320. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8321. (setq modes (org-set-calc-mode
  8322. 'calc-float-format
  8323. (list (cdr (assoc c '((?n . float) (?f . fix)
  8324. (?s . sci) (?e . eng))))
  8325. n))))
  8326. (setq fmt (replace-match "" t t fmt)))
  8327. (if (string-match "[NT]" fmt)
  8328. (setq numbers (equal (match-string 0 fmt) "N")
  8329. fmt (replace-match "" t t fmt)))
  8330. (if (string-match "L" fmt)
  8331. (setq literal t
  8332. fmt (replace-match "" t t fmt)))
  8333. (if (string-match "E" fmt)
  8334. (setq keep-empty t
  8335. fmt (replace-match "" t t fmt)))
  8336. (while (string-match "[DRFS]" fmt)
  8337. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  8338. (setq fmt (replace-match "" t t fmt)))
  8339. (unless (string-match "\\S-" fmt)
  8340. (setq fmt nil))))
  8341. (if (and (not suppress-const) org-table-formula-use-constants)
  8342. (setq formula (org-table-formula-substitute-names formula)))
  8343. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  8344. (while (> ndown 0)
  8345. (setq fields (org-split-string
  8346. (org-no-properties
  8347. (buffer-substring (point-at-bol) (point-at-eol)))
  8348. " *| *"))
  8349. (if (eq numbers t)
  8350. (setq fields (mapcar
  8351. (lambda (x) (number-to-string (string-to-number x)))
  8352. fields)))
  8353. (setq ndown (1- ndown))
  8354. (setq form (copy-sequence formula)
  8355. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  8356. (if (and lispp literal) (setq lispp 'literal))
  8357. ;; Check for old vertical references
  8358. (setq form (org-rewrite-old-row-references form))
  8359. ;; Insert complex ranges
  8360. (while (string-match org-table-range-regexp form)
  8361. (setq form
  8362. (replace-match
  8363. (save-match-data
  8364. (org-table-make-reference
  8365. (org-table-get-range (match-string 0 form) nil n0)
  8366. keep-empty numbers lispp))
  8367. t t form)))
  8368. ;; Insert simple ranges
  8369. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  8370. (setq form
  8371. (replace-match
  8372. (save-match-data
  8373. (org-table-make-reference
  8374. (org-sublist
  8375. fields (string-to-number (match-string 1 form))
  8376. (string-to-number (match-string 2 form)))
  8377. keep-empty numbers lispp))
  8378. t t form)))
  8379. (setq form0 form)
  8380. ;; Insert the references to fields in same row
  8381. (while (string-match "\\$\\([0-9]+\\)" form)
  8382. (setq n (string-to-number (match-string 1 form))
  8383. x (nth (1- (if (= n 0) n0 n)) fields))
  8384. (unless x (error "Invalid field specifier \"%s\""
  8385. (match-string 0 form)))
  8386. (setq form (replace-match
  8387. (save-match-data
  8388. (org-table-make-reference x nil numbers lispp))
  8389. t t form)))
  8390. (if lispp
  8391. (setq ev (condition-case nil
  8392. (eval (eval (read form)))
  8393. (error "#ERROR"))
  8394. ev (if (numberp ev) (number-to-string ev) ev))
  8395. (or (fboundp 'calc-eval)
  8396. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  8397. (setq ev (calc-eval (cons form modes)
  8398. (if numbers 'num))))
  8399. (when org-table-formula-debug
  8400. (with-output-to-temp-buffer "*Substitution History*"
  8401. (princ (format "Substitution history of formula
  8402. Orig: %s
  8403. $xyz-> %s
  8404. @r$c-> %s
  8405. $1-> %s\n" orig formula form0 form))
  8406. (if (listp ev)
  8407. (princ (format " %s^\nError: %s"
  8408. (make-string (car ev) ?\-) (nth 1 ev)))
  8409. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  8410. ev (or fmt "NONE")
  8411. (if fmt (format fmt (string-to-number ev)) ev)))))
  8412. (setq bw (get-buffer-window "*Substitution History*"))
  8413. (shrink-window-if-larger-than-buffer bw)
  8414. (unless (and (interactive-p) (not ndown))
  8415. (unless (let (inhibit-redisplay)
  8416. (y-or-n-p "Debugging Formula. Continue to next? "))
  8417. (org-table-align)
  8418. (error "Abort"))
  8419. (delete-window bw)
  8420. (message "")))
  8421. (if (listp ev) (setq fmt nil ev "#ERROR"))
  8422. (org-table-justify-field-maybe
  8423. (if fmt (format fmt (string-to-number ev)) ev))
  8424. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  8425. (call-interactively 'org-return)
  8426. (setq ndown 0)))
  8427. (and down (org-table-maybe-recalculate-line))
  8428. (or suppress-align (and org-table-may-need-update
  8429. (org-table-align))))))
  8430. (defun org-table-put-field-property (prop value)
  8431. (save-excursion
  8432. (put-text-property (progn (skip-chars-backward "^|") (point))
  8433. (progn (skip-chars-forward "^|") (point))
  8434. prop value)))
  8435. (defun org-table-get-range (desc &optional tbeg col highlight)
  8436. "Get a calc vector from a column, accorting to descriptor DESC.
  8437. Optional arguments TBEG and COL can give the beginning of the table and
  8438. the current column, to avoid unnecessary parsing.
  8439. HIGHLIGHT means, just highlight the range."
  8440. (if (not (equal (string-to-char desc) ?@))
  8441. (setq desc (concat "@" desc)))
  8442. (save-excursion
  8443. (or tbeg (setq tbeg (org-table-begin)))
  8444. (or col (setq col (org-table-current-column)))
  8445. (let ((thisline (org-current-line))
  8446. beg end c1 c2 r1 r2 rangep tmp)
  8447. (unless (string-match org-table-range-regexp desc)
  8448. (error "Invalid table range specifier `%s'" desc))
  8449. (setq rangep (match-end 3)
  8450. r1 (and (match-end 1) (match-string 1 desc))
  8451. r2 (and (match-end 4) (match-string 4 desc))
  8452. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  8453. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  8454. (and c1 (setq c1 (+ (string-to-number c1)
  8455. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  8456. (and c2 (setq c2 (+ (string-to-number c2)
  8457. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  8458. (if (equal r1 "") (setq r1 nil))
  8459. (if (equal r2 "") (setq r2 nil))
  8460. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  8461. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  8462. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  8463. (if (not r1) (setq r1 thisline))
  8464. (if (not r2) (setq r2 thisline))
  8465. (if (not c1) (setq c1 col))
  8466. (if (not c2) (setq c2 col))
  8467. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  8468. ;; just one field
  8469. (progn
  8470. (goto-line r1)
  8471. (while (not (looking-at org-table-dataline-regexp))
  8472. (beginning-of-line 2))
  8473. (prog1 (org-trim (org-table-get-field c1))
  8474. (if highlight (org-table-highlight-rectangle (point) (point)))))
  8475. ;; A range, return a vector
  8476. ;; First sort the numbers to get a regular ractangle
  8477. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  8478. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  8479. (goto-line r1)
  8480. (while (not (looking-at org-table-dataline-regexp))
  8481. (beginning-of-line 2))
  8482. (org-table-goto-column c1)
  8483. (setq beg (point))
  8484. (goto-line r2)
  8485. (while (not (looking-at org-table-dataline-regexp))
  8486. (beginning-of-line 0))
  8487. (org-table-goto-column c2)
  8488. (setq end (point))
  8489. (if highlight
  8490. (org-table-highlight-rectangle
  8491. beg (progn (skip-chars-forward "^|\n") (point))))
  8492. ;; return string representation of calc vector
  8493. (mapcar 'org-trim
  8494. (apply 'append (org-table-copy-region beg end)))))))
  8495. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  8496. "Analyze descriptor DESC and retrieve the corresponding line number.
  8497. The cursor is currently in line CLINE, the table begins in line BLINE,
  8498. and TABLE is a vector with line types."
  8499. (if (string-match "^[0-9]+$" desc)
  8500. (aref org-table-dlines (string-to-number desc))
  8501. (setq cline (or cline (org-current-line))
  8502. bline (or bline org-table-current-begin-line)
  8503. table (or table org-table-current-line-types))
  8504. (if (or
  8505. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  8506. ;; 1 2 3 4 5 6
  8507. (and (not (match-end 3)) (not (match-end 6)))
  8508. (and (match-end 3) (match-end 6) (not (match-end 5))))
  8509. (error "invalid row descriptor `%s'" desc))
  8510. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  8511. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  8512. (odir (and (match-end 5) (match-string 5 desc)))
  8513. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  8514. (i (- cline bline))
  8515. (rel (and (match-end 6)
  8516. (or (and (match-end 1) (not (match-end 3)))
  8517. (match-end 5)))))
  8518. (if (and hn (not hdir))
  8519. (progn
  8520. (setq i 0 hdir "+")
  8521. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  8522. (if (and (not hn) on (not odir))
  8523. (error "should never happen");;(aref org-table-dlines on)
  8524. (if (and hn (> hn 0))
  8525. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  8526. (if on
  8527. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  8528. (+ bline i)))))
  8529. (defun org-find-row-type (table i type backwards relative n)
  8530. (let ((l (length table)))
  8531. (while (> n 0)
  8532. (while (and (setq i (+ i (if backwards -1 1)))
  8533. (>= i 0) (< i l)
  8534. (not (eq (aref table i) type))
  8535. (if (and relative (eq (aref table i) 'hline))
  8536. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  8537. t)))
  8538. (setq n (1- n)))
  8539. (if (or (< i 0) (>= i l))
  8540. (error "Row descriptior leads outside table")
  8541. i)))
  8542. (defun org-rewrite-old-row-references (s)
  8543. (if (string-match "&[-+0-9I]" s)
  8544. (error "Formula contains old &row reference, please rewrite using @-syntax")
  8545. s))
  8546. (defun org-table-make-reference (elements keep-empty numbers lispp)
  8547. "Convert list ELEMENTS to something appropriate to insert into formula.
  8548. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  8549. NUMBERS indicates that everything should be converted to numbers.
  8550. LISPP means to return something appropriate for a Lisp list."
  8551. (if (stringp elements) ; just a single val
  8552. (if lispp
  8553. (if (eq lispp 'literal)
  8554. elements
  8555. (prin1-to-string (if numbers (string-to-number elements) elements)))
  8556. (if (equal elements "") (setq elements "0"))
  8557. (if numbers (number-to-string (string-to-number elements)) elements))
  8558. (unless keep-empty
  8559. (setq elements
  8560. (delq nil
  8561. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8562. elements))))
  8563. (setq elements (or elements '("0")))
  8564. (if lispp
  8565. (mapconcat
  8566. (lambda (x)
  8567. (if (eq lispp 'literal)
  8568. x
  8569. (prin1-to-string (if numbers (string-to-number x) x))))
  8570. elements " ")
  8571. (concat "[" (mapconcat
  8572. (lambda (x)
  8573. (if numbers (number-to-string (string-to-number x)) x))
  8574. elements
  8575. ",") "]"))))
  8576. (defun org-table-recalculate (&optional all noalign)
  8577. "Recalculate the current table line by applying all stored formulas.
  8578. With prefix arg ALL, do this for all lines in the table."
  8579. (interactive "P")
  8580. (or (memq this-command org-recalc-commands)
  8581. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8582. (unless (org-at-table-p) (error "Not at a table"))
  8583. (if (equal all '(16))
  8584. (org-table-iterate)
  8585. (org-table-get-specials)
  8586. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8587. (lambda (a b) (string< (car a) (car b)))))
  8588. (inhibit-redisplay (not debug-on-error))
  8589. (line-re org-table-dataline-regexp)
  8590. (thisline (org-current-line))
  8591. (thiscol (org-table-current-column))
  8592. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8593. ;; Insert constants in all formulas
  8594. (setq eqlist
  8595. (mapcar (lambda (x)
  8596. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8597. x)
  8598. eqlist))
  8599. ;; Split the equation list
  8600. (while (setq eq (pop eqlist))
  8601. (if (<= (string-to-char (car eq)) ?9)
  8602. (push eq eqlnum)
  8603. (push eq eqlname)))
  8604. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8605. (if all
  8606. (progn
  8607. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8608. (goto-char (setq beg (org-table-begin)))
  8609. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8610. ;; This is a table with marked lines, compute selected lines
  8611. (setq line-re org-table-recalculate-regexp)
  8612. ;; Move forward to the first non-header line
  8613. (if (and (re-search-forward org-table-dataline-regexp end t)
  8614. (re-search-forward org-table-hline-regexp end t)
  8615. (re-search-forward org-table-dataline-regexp end t))
  8616. (setq beg (match-beginning 0))
  8617. nil))) ;; just leave beg where it is
  8618. (setq beg (point-at-bol)
  8619. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8620. (goto-char beg)
  8621. (and all (message "Re-applying formulas to full table..."))
  8622. ;; First find the named fields, and mark them untouchanble
  8623. (remove-text-properties beg end '(org-untouchable t))
  8624. (while (setq eq (pop eqlname))
  8625. (setq name (car eq)
  8626. a (assoc name org-table-named-field-locations))
  8627. (and (not a)
  8628. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8629. (setq a (list name
  8630. (aref org-table-dlines
  8631. (string-to-number (match-string 1 name)))
  8632. (string-to-number (match-string 2 name)))))
  8633. (when (and a (or all (equal (nth 1 a) thisline)))
  8634. (message "Re-applying formula to field: %s" name)
  8635. (goto-line (nth 1 a))
  8636. (org-table-goto-column (nth 2 a))
  8637. (push (append a (list (cdr eq))) eqlname1)
  8638. (org-table-put-field-property :org-untouchable t)))
  8639. ;; Now evauluate the column formulas, but skip fields covered by
  8640. ;; field formulas
  8641. (goto-char beg)
  8642. (while (re-search-forward line-re end t)
  8643. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8644. ;; Unprotected line, recalculate
  8645. (and all (message "Re-applying formulas to full table...(line %d)"
  8646. (setq cnt (1+ cnt))))
  8647. (setq org-last-recalc-line (org-current-line))
  8648. (setq eql eqlnum)
  8649. (while (setq entry (pop eql))
  8650. (goto-line org-last-recalc-line)
  8651. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8652. (unless (get-text-property (point) :org-untouchable)
  8653. (org-table-eval-formula nil (cdr entry)
  8654. 'noalign 'nocst 'nostore 'noanalysis)))))
  8655. ;; Now evaluate the field formulas
  8656. (while (setq eq (pop eqlname1))
  8657. (message "Re-applying formula to field: %s" (car eq))
  8658. (goto-line (nth 1 eq))
  8659. (org-table-goto-column (nth 2 eq))
  8660. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  8661. 'nostore 'noanalysis))
  8662. (goto-line thisline)
  8663. (org-table-goto-column thiscol)
  8664. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  8665. (or noalign (and org-table-may-need-update (org-table-align))
  8666. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  8667. ;; back to initial position
  8668. (message "Re-applying formulas...done")
  8669. (goto-line thisline)
  8670. (org-table-goto-column thiscol)
  8671. (or noalign (and org-table-may-need-update (org-table-align))
  8672. (and all (message "Re-applying formulas...done"))))))
  8673. (defun org-table-iterate (&optional arg)
  8674. "Recalculate the table until it does not change anymore."
  8675. (interactive "P")
  8676. (let ((imax (if arg (prefix-numeric-value arg) 10))
  8677. (i 0)
  8678. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  8679. thistbl)
  8680. (catch 'exit
  8681. (while (< i imax)
  8682. (setq i (1+ i))
  8683. (org-table-recalculate 'all)
  8684. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  8685. (if (not (string= lasttbl thistbl))
  8686. (setq lasttbl thistbl)
  8687. (if (> i 1)
  8688. (message "Convergence after %d iterations" i)
  8689. (message "Table was already stable"))
  8690. (throw 'exit t)))
  8691. (error "No convergence after %d iterations" i))))
  8692. (defun org-table-formula-substitute-names (f)
  8693. "Replace $const with values in string F."
  8694. (let ((start 0) a (f1 f))
  8695. ;; First, check for column names
  8696. (while (setq start (string-match org-table-column-name-regexp f start))
  8697. (setq start (1+ start))
  8698. (setq a (assoc (match-string 1 f) org-table-column-names))
  8699. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  8700. ;; Parameters and constants
  8701. (setq start 0)
  8702. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  8703. (setq start (1+ start))
  8704. (if (setq a (save-match-data
  8705. (org-table-get-constant (match-string 1 f))))
  8706. (setq f (replace-match (concat "(" a ")") t t f))))
  8707. (if org-table-formula-debug
  8708. (put-text-property 0 (length f) :orig-formula f1 f))
  8709. f))
  8710. (defun org-table-get-constant (const)
  8711. "Find the value for a parameter or constant in a formula.
  8712. Parameters get priority."
  8713. (or (cdr (assoc const org-table-local-parameters))
  8714. (cdr (assoc const org-table-formula-constants-local))
  8715. (cdr (assoc const org-table-formula-constants))
  8716. (and (fboundp 'constants-get) (constants-get const))
  8717. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  8718. (org-entry-get nil (substring const 5) 'inherit))
  8719. "#UNDEFINED_NAME"))
  8720. (defvar org-table-fedit-map
  8721. (let ((map (make-sparse-keymap)))
  8722. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  8723. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  8724. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  8725. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  8726. (org-defkey map "\C-c?" 'org-table-show-reference)
  8727. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  8728. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  8729. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  8730. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  8731. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  8732. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  8733. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  8734. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  8735. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  8736. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  8737. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  8738. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  8739. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  8740. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  8741. map))
  8742. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  8743. '("Edit-Formulas"
  8744. ["Finish and Install" org-table-fedit-finish t]
  8745. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  8746. ["Abort" org-table-fedit-abort t]
  8747. "--"
  8748. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  8749. ["Complete Lisp Symbol" lisp-complete-symbol t]
  8750. "--"
  8751. "Shift Reference at Point"
  8752. ["Up" org-table-fedit-ref-up t]
  8753. ["Down" org-table-fedit-ref-down t]
  8754. ["Left" org-table-fedit-ref-left t]
  8755. ["Right" org-table-fedit-ref-right t]
  8756. "-"
  8757. "Change Test Row for Column Formulas"
  8758. ["Up" org-table-fedit-line-up t]
  8759. ["Down" org-table-fedit-line-down t]
  8760. "--"
  8761. ["Scroll Table Window" org-table-fedit-scroll t]
  8762. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  8763. ["Show Table Grid" org-table-fedit-toggle-coordinates
  8764. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  8765. org-table-overlay-coordinates)]
  8766. "--"
  8767. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  8768. :style toggle :selected org-table-buffer-is-an]))
  8769. (defvar org-pos)
  8770. (defun org-table-edit-formulas ()
  8771. "Edit the formulas of the current table in a separate buffer."
  8772. (interactive)
  8773. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  8774. (beginning-of-line 0))
  8775. (unless (org-at-table-p) (error "Not at a table"))
  8776. (org-table-get-specials)
  8777. (let ((key (org-table-current-field-formula 'key 'noerror))
  8778. (eql (sort (org-table-get-stored-formulas 'noerror)
  8779. 'org-table-formula-less-p))
  8780. (pos (move-marker (make-marker) (point)))
  8781. (startline 1)
  8782. (wc (current-window-configuration))
  8783. (titles '((column . "# Column Formulas\n")
  8784. (field . "# Field Formulas\n")
  8785. (named . "# Named Field Formulas\n")))
  8786. entry s type title)
  8787. (org-switch-to-buffer-other-window "*Edit Formulas*")
  8788. (erase-buffer)
  8789. ;; Keep global-font-lock-mode from turning on font-lock-mode
  8790. (let ((font-lock-global-modes '(not fundamental-mode)))
  8791. (fundamental-mode))
  8792. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  8793. (org-set-local 'org-pos pos)
  8794. (org-set-local 'org-window-configuration wc)
  8795. (use-local-map org-table-fedit-map)
  8796. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  8797. (easy-menu-add org-table-fedit-menu)
  8798. (setq startline (org-current-line))
  8799. (while (setq entry (pop eql))
  8800. (setq type (cond
  8801. ((equal (string-to-char (car entry)) ?@) 'field)
  8802. ((string-match "^[0-9]" (car entry)) 'column)
  8803. (t 'named)))
  8804. (when (setq title (assq type titles))
  8805. (or (bobp) (insert "\n"))
  8806. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  8807. (setq titles (delq title titles)))
  8808. (if (equal key (car entry)) (setq startline (org-current-line)))
  8809. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  8810. (car entry) " = " (cdr entry) "\n"))
  8811. (remove-text-properties 0 (length s) '(face nil) s)
  8812. (insert s))
  8813. (if (eq org-table-use-standard-references t)
  8814. (org-table-fedit-toggle-ref-type))
  8815. (goto-line startline)
  8816. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  8817. (defun org-table-fedit-post-command ()
  8818. (when (not (memq this-command '(lisp-complete-symbol)))
  8819. (let ((win (selected-window)))
  8820. (save-excursion
  8821. (condition-case nil
  8822. (org-table-show-reference)
  8823. (error nil))
  8824. (select-window win)))))
  8825. (defun org-table-formula-to-user (s)
  8826. "Convert a formula from internal to user representation."
  8827. (if (eq org-table-use-standard-references t)
  8828. (org-table-convert-refs-to-an s)
  8829. s))
  8830. (defun org-table-formula-from-user (s)
  8831. "Convert a formula from user to internal representation."
  8832. (if org-table-use-standard-references
  8833. (org-table-convert-refs-to-rc s)
  8834. s))
  8835. (defun org-table-convert-refs-to-rc (s)
  8836. "Convert spreadsheet references from AB7 to @7$28.
  8837. Works for single references, but also for entire formulas and even the
  8838. full TBLFM line."
  8839. (let ((start 0))
  8840. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  8841. (cond
  8842. ((match-end 3)
  8843. ;; format match, just advance
  8844. (setq start (match-end 0)))
  8845. ((and (> (match-beginning 0) 0)
  8846. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  8847. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  8848. ;; 3.e5 or something like this.
  8849. (setq start (match-end 0)))
  8850. (t
  8851. (setq start (match-beginning 0)
  8852. s (replace-match
  8853. (if (equal (match-string 2 s) "&")
  8854. (format "$%d" (org-letters-to-number (match-string 1 s)))
  8855. (format "@%d$%d"
  8856. (string-to-number (match-string 2 s))
  8857. (org-letters-to-number (match-string 1 s))))
  8858. t t s)))))
  8859. s))
  8860. (defun org-table-convert-refs-to-an (s)
  8861. "Convert spreadsheet references from to @7$28 to AB7.
  8862. Works for single references, but also for entire formulas and even the
  8863. full TBLFM line."
  8864. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  8865. (setq s (replace-match
  8866. (format "%s%d"
  8867. (org-number-to-letters
  8868. (string-to-number (match-string 2 s)))
  8869. (string-to-number (match-string 1 s)))
  8870. t t s)))
  8871. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  8872. (setq s (replace-match (concat "\\1"
  8873. (org-number-to-letters
  8874. (string-to-number (match-string 2 s))) "&")
  8875. t nil s)))
  8876. s)
  8877. (defun org-letters-to-number (s)
  8878. "Convert a base 26 number represented by letters into an integer.
  8879. For example: AB -> 28."
  8880. (let ((n 0))
  8881. (setq s (upcase s))
  8882. (while (> (length s) 0)
  8883. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  8884. s (substring s 1)))
  8885. n))
  8886. (defun org-number-to-letters (n)
  8887. "Convert an integer into a base 26 number represented by letters.
  8888. For example: 28 -> AB."
  8889. (let ((s ""))
  8890. (while (> n 0)
  8891. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  8892. n (/ (1- n) 26)))
  8893. s))
  8894. (defun org-table-fedit-convert-buffer (function)
  8895. "Convert all references in this buffer, using FUNTION."
  8896. (let ((line (org-current-line)))
  8897. (goto-char (point-min))
  8898. (while (not (eobp))
  8899. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  8900. (delete-region (point) (point-at-eol))
  8901. (or (eobp) (forward-char 1)))
  8902. (goto-line line)))
  8903. (defun org-table-fedit-toggle-ref-type ()
  8904. "Convert all references in the buffer from B3 to @3$2 and back."
  8905. (interactive)
  8906. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  8907. (org-table-fedit-convert-buffer
  8908. (if org-table-buffer-is-an
  8909. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  8910. (message "Reference type switched to %s"
  8911. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  8912. (defun org-table-fedit-ref-up ()
  8913. "Shift the reference at point one row/hline up."
  8914. (interactive)
  8915. (org-table-fedit-shift-reference 'up))
  8916. (defun org-table-fedit-ref-down ()
  8917. "Shift the reference at point one row/hline down."
  8918. (interactive)
  8919. (org-table-fedit-shift-reference 'down))
  8920. (defun org-table-fedit-ref-left ()
  8921. "Shift the reference at point one field to the left."
  8922. (interactive)
  8923. (org-table-fedit-shift-reference 'left))
  8924. (defun org-table-fedit-ref-right ()
  8925. "Shift the reference at point one field to the right."
  8926. (interactive)
  8927. (org-table-fedit-shift-reference 'right))
  8928. (defun org-table-fedit-shift-reference (dir)
  8929. (cond
  8930. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  8931. (if (memq dir '(left right))
  8932. (org-rematch-and-replace 1 (eq dir 'left))
  8933. (error "Cannot shift reference in this direction")))
  8934. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  8935. ;; A B3-like reference
  8936. (if (memq dir '(up down))
  8937. (org-rematch-and-replace 2 (eq dir 'up))
  8938. (org-rematch-and-replace 1 (eq dir 'left))))
  8939. ((org-at-regexp-p
  8940. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  8941. ;; An internal reference
  8942. (if (memq dir '(up down))
  8943. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  8944. (org-rematch-and-replace 5 (eq dir 'left))))))
  8945. (defun org-rematch-and-replace (n &optional decr hline)
  8946. "Re-match the group N, and replace it with the shifted refrence."
  8947. (or (match-end n) (error "Cannot shift reference in this direction"))
  8948. (goto-char (match-beginning n))
  8949. (and (looking-at (regexp-quote (match-string n)))
  8950. (replace-match (org-shift-refpart (match-string 0) decr hline)
  8951. t t)))
  8952. (defun org-shift-refpart (ref &optional decr hline)
  8953. "Shift a refrence part REF.
  8954. If DECR is set, decrease the references row/column, else increase.
  8955. If HLINE is set, this may be a hline reference, it certainly is not
  8956. a translation reference."
  8957. (save-match-data
  8958. (let* ((sign (string-match "^[-+]" ref)) n)
  8959. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  8960. (cond
  8961. ((and hline (string-match "^I+" ref))
  8962. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  8963. (setq n (+ n (if decr -1 1)))
  8964. (if (= n 0) (setq n (+ n (if decr -1 1))))
  8965. (if sign
  8966. (setq sign (if (< n 0) "-" "+") n (abs n))
  8967. (setq n (max 1 n)))
  8968. (concat sign (make-string n ?I)))
  8969. ((string-match "^[0-9]+" ref)
  8970. (setq n (string-to-number (concat sign ref)))
  8971. (setq n (+ n (if decr -1 1)))
  8972. (if sign
  8973. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  8974. (number-to-string (max 1 n))))
  8975. ((string-match "^[a-zA-Z]+" ref)
  8976. (org-number-to-letters
  8977. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  8978. (t (error "Cannot shift reference"))))))
  8979. (defun org-table-fedit-toggle-coordinates ()
  8980. "Toggle the display of coordinates in the refrenced table."
  8981. (interactive)
  8982. (let ((pos (marker-position org-pos)))
  8983. (with-current-buffer (marker-buffer org-pos)
  8984. (save-excursion
  8985. (goto-char pos)
  8986. (org-table-toggle-coordinate-overlays)))))
  8987. (defun org-table-fedit-finish (&optional arg)
  8988. "Parse the buffer for formula definitions and install them.
  8989. With prefix ARG, apply the new formulas to the table."
  8990. (interactive "P")
  8991. (org-table-remove-rectangle-highlight)
  8992. (if org-table-use-standard-references
  8993. (progn
  8994. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  8995. (setq org-table-buffer-is-an nil)))
  8996. (let ((pos org-pos) eql var form)
  8997. (goto-char (point-min))
  8998. (while (re-search-forward
  8999. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9000. nil t)
  9001. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9002. form (match-string 3))
  9003. (setq form (org-trim form))
  9004. (when (not (equal form ""))
  9005. (while (string-match "[ \t]*\n[ \t]*" form)
  9006. (setq form (replace-match " " t t form)))
  9007. (when (assoc var eql)
  9008. (error "Double formulas for %s" var))
  9009. (push (cons var form) eql)))
  9010. (setq org-pos nil)
  9011. (set-window-configuration org-window-configuration)
  9012. (select-window (get-buffer-window (marker-buffer pos)))
  9013. (goto-char pos)
  9014. (unless (org-at-table-p)
  9015. (error "Lost table position - cannot install formulae"))
  9016. (org-table-store-formulas eql)
  9017. (move-marker pos nil)
  9018. (kill-buffer "*Edit Formulas*")
  9019. (if arg
  9020. (org-table-recalculate 'all)
  9021. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9022. (defun org-table-fedit-abort ()
  9023. "Abort editing formulas, without installing the changes."
  9024. (interactive)
  9025. (org-table-remove-rectangle-highlight)
  9026. (let ((pos org-pos))
  9027. (set-window-configuration org-window-configuration)
  9028. (select-window (get-buffer-window (marker-buffer pos)))
  9029. (goto-char pos)
  9030. (move-marker pos nil)
  9031. (message "Formula editing aborted without installing changes")))
  9032. (defun org-table-fedit-lisp-indent ()
  9033. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9034. (interactive)
  9035. (let ((pos (point)) beg end ind)
  9036. (beginning-of-line 1)
  9037. (cond
  9038. ((looking-at "[ \t]")
  9039. (goto-char pos)
  9040. (call-interactively 'lisp-indent-line))
  9041. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9042. ((not (fboundp 'pp-buffer))
  9043. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9044. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9045. (goto-char (- (match-end 0) 2))
  9046. (setq beg (point))
  9047. (setq ind (make-string (current-column) ?\ ))
  9048. (condition-case nil (forward-sexp 1)
  9049. (error
  9050. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9051. (setq end (point))
  9052. (save-restriction
  9053. (narrow-to-region beg end)
  9054. (if (eq last-command this-command)
  9055. (progn
  9056. (goto-char (point-min))
  9057. (setq this-command nil)
  9058. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9059. (replace-match " ")))
  9060. (pp-buffer)
  9061. (untabify (point-min) (point-max))
  9062. (goto-char (1+ (point-min)))
  9063. (while (re-search-forward "^." nil t)
  9064. (beginning-of-line 1)
  9065. (insert ind))
  9066. (goto-char (point-max))
  9067. (backward-delete-char 1)))
  9068. (goto-char beg))
  9069. (t nil))))
  9070. (defvar org-show-positions nil)
  9071. (defun org-table-show-reference (&optional local)
  9072. "Show the location/value of the $ expression at point."
  9073. (interactive)
  9074. (org-table-remove-rectangle-highlight)
  9075. (catch 'exit
  9076. (let ((pos (if local (point) org-pos))
  9077. (face2 'highlight)
  9078. (org-inhibit-highlight-removal t)
  9079. (win (selected-window))
  9080. (org-show-positions nil)
  9081. var name e what match dest)
  9082. (if local (org-table-get-specials))
  9083. (setq what (cond
  9084. ((or (org-at-regexp-p org-table-range-regexp2)
  9085. (org-at-regexp-p org-table-translate-regexp)
  9086. (org-at-regexp-p org-table-range-regexp))
  9087. (setq match
  9088. (save-match-data
  9089. (org-table-convert-refs-to-rc (match-string 0))))
  9090. 'range)
  9091. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9092. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9093. ((not local) nil)
  9094. (t (error "No reference at point")))
  9095. match (and what (or match (match-string 0))))
  9096. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9097. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9098. 'secondary-selection))
  9099. (org-add-hook 'before-change-functions
  9100. 'org-table-remove-rectangle-highlight)
  9101. (if (eq what 'name) (setq var (substring match 1)))
  9102. (when (eq what 'range)
  9103. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9104. (setq match (org-table-formula-substitute-names match)))
  9105. (unless local
  9106. (save-excursion
  9107. (end-of-line 1)
  9108. (re-search-backward "^\\S-" nil t)
  9109. (beginning-of-line 1)
  9110. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9111. (setq dest
  9112. (save-match-data
  9113. (org-table-convert-refs-to-rc (match-string 1))))
  9114. (org-table-add-rectangle-overlay
  9115. (match-beginning 1) (match-end 1) face2))))
  9116. (if (and (markerp pos) (marker-buffer pos))
  9117. (if (get-buffer-window (marker-buffer pos))
  9118. (select-window (get-buffer-window (marker-buffer pos)))
  9119. (org-switch-to-buffer-other-window (get-buffer-window
  9120. (marker-buffer pos)))))
  9121. (goto-char pos)
  9122. (org-table-force-dataline)
  9123. (when dest
  9124. (setq name (substring dest 1))
  9125. (cond
  9126. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9127. (setq e (assoc name org-table-named-field-locations))
  9128. (goto-line (nth 1 e))
  9129. (org-table-goto-column (nth 2 e)))
  9130. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9131. (let ((l (string-to-number (match-string 1 dest)))
  9132. (c (string-to-number (match-string 2 dest))))
  9133. (goto-line (aref org-table-dlines l))
  9134. (org-table-goto-column c)))
  9135. (t (org-table-goto-column (string-to-number name))))
  9136. (move-marker pos (point))
  9137. (org-table-highlight-rectangle nil nil face2))
  9138. (cond
  9139. ((equal dest match))
  9140. ((not match))
  9141. ((eq what 'range)
  9142. (condition-case nil
  9143. (save-excursion
  9144. (org-table-get-range match nil nil 'highlight))
  9145. (error nil)))
  9146. ((setq e (assoc var org-table-named-field-locations))
  9147. (goto-line (nth 1 e))
  9148. (org-table-goto-column (nth 2 e))
  9149. (org-table-highlight-rectangle (point) (point))
  9150. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9151. ((setq e (assoc var org-table-column-names))
  9152. (org-table-goto-column (string-to-number (cdr e)))
  9153. (org-table-highlight-rectangle (point) (point))
  9154. (goto-char (org-table-begin))
  9155. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9156. (org-table-end) t)
  9157. (progn
  9158. (goto-char (match-beginning 1))
  9159. (org-table-highlight-rectangle)
  9160. (message "Named column (column %s)" (cdr e)))
  9161. (error "Column name not found")))
  9162. ((eq what 'column)
  9163. ;; column number
  9164. (org-table-goto-column (string-to-number (substring match 1)))
  9165. (org-table-highlight-rectangle (point) (point))
  9166. (message "Column %s" (substring match 1)))
  9167. ((setq e (assoc var org-table-local-parameters))
  9168. (goto-char (org-table-begin))
  9169. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9170. (progn
  9171. (goto-char (match-beginning 1))
  9172. (org-table-highlight-rectangle)
  9173. (message "Local parameter."))
  9174. (error "Parameter not found")))
  9175. (t
  9176. (cond
  9177. ((not var) (error "No reference at point"))
  9178. ((setq e (assoc var org-table-formula-constants-local))
  9179. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9180. var (cdr e)))
  9181. ((setq e (assoc var org-table-formula-constants))
  9182. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9183. var (cdr e)))
  9184. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9185. (message "Constant: $%s=%s, from `constants.el'%s."
  9186. var e (format " (%s units)" constants-unit-system)))
  9187. (t (error "Undefined name $%s" var)))))
  9188. (goto-char pos)
  9189. (when (and org-show-positions
  9190. (not (memq this-command '(org-table-fedit-scroll
  9191. org-table-fedit-scroll-down))))
  9192. (push pos org-show-positions)
  9193. (push org-table-current-begin-pos org-show-positions)
  9194. (let ((min (apply 'min org-show-positions))
  9195. (max (apply 'max org-show-positions)))
  9196. (goto-char min) (recenter 0)
  9197. (goto-char max)
  9198. (or (pos-visible-in-window-p max) (recenter -1))))
  9199. (select-window win))))
  9200. (defun org-table-force-dataline ()
  9201. "Make sure the cursor is in a dataline in a table."
  9202. (unless (save-excursion
  9203. (beginning-of-line 1)
  9204. (looking-at org-table-dataline-regexp))
  9205. (let* ((re org-table-dataline-regexp)
  9206. (p1 (save-excursion (re-search-forward re nil 'move)))
  9207. (p2 (save-excursion (re-search-backward re nil 'move))))
  9208. (cond ((and p1 p2)
  9209. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9210. p1 p2)))
  9211. ((or p1 p2) (goto-char (or p1 p2)))
  9212. (t (error "No table dataline around here"))))))
  9213. (defun org-table-fedit-line-up ()
  9214. "Move cursor one line up in the window showing the table."
  9215. (interactive)
  9216. (org-table-fedit-move 'previous-line))
  9217. (defun org-table-fedit-line-down ()
  9218. "Move cursor one line down in the window showing the table."
  9219. (interactive)
  9220. (org-table-fedit-move 'next-line))
  9221. (defun org-table-fedit-move (command)
  9222. "Move the cursor in the window shoinw the table.
  9223. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9224. (let ((org-table-allow-automatic-line-recalculation nil)
  9225. (pos org-pos) (win (selected-window)) p)
  9226. (select-window (get-buffer-window (marker-buffer org-pos)))
  9227. (setq p (point))
  9228. (call-interactively command)
  9229. (while (and (org-at-table-p)
  9230. (org-at-table-hline-p))
  9231. (call-interactively command))
  9232. (or (org-at-table-p) (goto-char p))
  9233. (move-marker pos (point))
  9234. (select-window win)))
  9235. (defun org-table-fedit-scroll (N)
  9236. (interactive "p")
  9237. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9238. (scroll-other-window N)))
  9239. (defun org-table-fedit-scroll-down (N)
  9240. (interactive "p")
  9241. (org-table-fedit-scroll (- N)))
  9242. (defvar org-table-rectangle-overlays nil)
  9243. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9244. "Add a new overlay."
  9245. (let ((ov (org-make-overlay beg end)))
  9246. (org-overlay-put ov 'face (or face 'secondary-selection))
  9247. (push ov org-table-rectangle-overlays)))
  9248. (defun org-table-highlight-rectangle (&optional beg end face)
  9249. "Highlight rectangular region in a table."
  9250. (setq beg (or beg (point)) end (or end (point)))
  9251. (let ((b (min beg end))
  9252. (e (max beg end))
  9253. l1 c1 l2 c2 tmp)
  9254. (and (boundp 'org-show-positions)
  9255. (setq org-show-positions (cons b (cons e org-show-positions))))
  9256. (goto-char (min beg end))
  9257. (setq l1 (org-current-line)
  9258. c1 (org-table-current-column))
  9259. (goto-char (max beg end))
  9260. (setq l2 (org-current-line)
  9261. c2 (org-table-current-column))
  9262. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  9263. (goto-line l1)
  9264. (beginning-of-line 1)
  9265. (loop for line from l1 to l2 do
  9266. (when (looking-at org-table-dataline-regexp)
  9267. (org-table-goto-column c1)
  9268. (skip-chars-backward "^|\n") (setq beg (point))
  9269. (org-table-goto-column c2)
  9270. (skip-chars-forward "^|\n") (setq end (point))
  9271. (org-table-add-rectangle-overlay beg end face))
  9272. (beginning-of-line 2))
  9273. (goto-char b))
  9274. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  9275. (defun org-table-remove-rectangle-highlight (&rest ignore)
  9276. "Remove the rectangle overlays."
  9277. (unless org-inhibit-highlight-removal
  9278. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  9279. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  9280. (setq org-table-rectangle-overlays nil)))
  9281. (defvar org-table-coordinate-overlays nil
  9282. "Collects the cooordinate grid overlays, so that they can be removed.")
  9283. (make-variable-buffer-local 'org-table-coordinate-overlays)
  9284. (defun org-table-overlay-coordinates ()
  9285. "Add overlays to the table at point, to show row/column coordinates."
  9286. (interactive)
  9287. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9288. (setq org-table-coordinate-overlays nil)
  9289. (save-excursion
  9290. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  9291. (goto-char (org-table-begin))
  9292. (while (org-at-table-p)
  9293. (setq eol (point-at-eol))
  9294. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9295. (push ov org-table-coordinate-overlays)
  9296. (setq hline (looking-at org-table-hline-regexp))
  9297. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9298. (format "%4d" (setq id (1+ id)))))
  9299. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9300. (when hline
  9301. (setq ic 0)
  9302. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9303. (setq beg (1+ (match-beginning 0))
  9304. ic (1+ ic)
  9305. s1 (concat "$" (int-to-string ic))
  9306. s2 (org-number-to-letters ic)
  9307. str (if (eq org-table-use-standard-references t) s2 s1))
  9308. (setq ov (org-make-overlay beg (+ beg (length str))))
  9309. (push ov org-table-coordinate-overlays)
  9310. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9311. (beginning-of-line 2)))))
  9312. (defun org-table-toggle-coordinate-overlays ()
  9313. "Toggle the display of Row/Column numbers in tables."
  9314. (interactive)
  9315. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9316. (message "Row/Column number display turned %s"
  9317. (if org-table-overlay-coordinates "on" "off"))
  9318. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9319. (org-table-align))
  9320. (unless org-table-overlay-coordinates
  9321. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9322. (setq org-table-coordinate-overlays nil)))
  9323. (defun org-table-toggle-formula-debugger ()
  9324. "Toggle the formula debugger in tables."
  9325. (interactive)
  9326. (setq org-table-formula-debug (not org-table-formula-debug))
  9327. (message "Formula debugging has been turned %s"
  9328. (if org-table-formula-debug "on" "off")))
  9329. ;;; The orgtbl minor mode
  9330. ;; Define a minor mode which can be used in other modes in order to
  9331. ;; integrate the org-mode table editor.
  9332. ;; This is really a hack, because the org-mode table editor uses several
  9333. ;; keys which normally belong to the major mode, for example the TAB and
  9334. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9335. ;; necessary to operate the table editor, but wraps the commands into a
  9336. ;; function which tests if the cursor is currently inside a table. If that
  9337. ;; is the case, the table editor command is executed. However, when any of
  9338. ;; those keys is used outside a table, the function uses `key-binding' to
  9339. ;; look up if the key has an associated command in another currently active
  9340. ;; keymap (minor modes, major mode, global), and executes that command.
  9341. ;; There might be problems if any of the keys used by the table editor is
  9342. ;; otherwise used as a prefix key.
  9343. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9344. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9345. ;; addresses this by checking explicitly for both bindings.
  9346. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9347. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9348. ;; Some modes bind other commands to simple characters, for example
  9349. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9350. ;; active, this binding is ignored inside tables and replaced with a
  9351. ;; modified self-insert.
  9352. (defvar orgtbl-mode nil
  9353. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9354. table editor in arbitrary modes.")
  9355. (make-variable-buffer-local 'orgtbl-mode)
  9356. (defvar orgtbl-mode-map (make-keymap)
  9357. "Keymap for `orgtbl-mode'.")
  9358. ;;;###autoload
  9359. (defun turn-on-orgtbl ()
  9360. "Unconditionally turn on `orgtbl-mode'."
  9361. (orgtbl-mode 1))
  9362. (defvar org-old-auto-fill-inhibit-regexp nil
  9363. "Local variable used by `orgtbl-mode'")
  9364. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  9365. "Matches a line belonging to an orgtbl.")
  9366. (defconst orgtbl-extra-font-lock-keywords
  9367. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  9368. 0 (quote 'org-table) 'prepend))
  9369. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  9370. ;;;###autoload
  9371. (defun orgtbl-mode (&optional arg)
  9372. "The `org-mode' table editor as a minor mode for use in other modes."
  9373. (interactive)
  9374. (if (org-mode-p)
  9375. ;; Exit without error, in case some hook functions calls this
  9376. ;; by accident in org-mode.
  9377. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9378. (setq orgtbl-mode
  9379. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9380. (if orgtbl-mode
  9381. (progn
  9382. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9383. ;; Make sure we are first in minor-mode-map-alist
  9384. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9385. (and c (setq minor-mode-map-alist
  9386. (cons c (delq c minor-mode-map-alist)))))
  9387. (org-set-local (quote org-table-may-need-update) t)
  9388. (org-add-hook 'before-change-functions 'org-before-change-function
  9389. nil 'local)
  9390. (org-set-local 'org-old-auto-fill-inhibit-regexp
  9391. auto-fill-inhibit-regexp)
  9392. (org-set-local 'auto-fill-inhibit-regexp
  9393. (if auto-fill-inhibit-regexp
  9394. (concat orgtbl-line-start-regexp "\\|"
  9395. auto-fill-inhibit-regexp)
  9396. orgtbl-line-start-regexp))
  9397. (org-add-to-invisibility-spec '(org-cwidth))
  9398. (when (fboundp 'font-lock-add-keywords)
  9399. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  9400. (org-restart-font-lock))
  9401. (easy-menu-add orgtbl-mode-menu)
  9402. (run-hooks 'orgtbl-mode-hook))
  9403. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9404. (org-cleanup-narrow-column-properties)
  9405. (org-remove-from-invisibility-spec '(org-cwidth))
  9406. (remove-hook 'before-change-functions 'org-before-change-function t)
  9407. (when (fboundp 'font-lock-remove-keywords)
  9408. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  9409. (org-restart-font-lock))
  9410. (easy-menu-remove orgtbl-mode-menu)
  9411. (force-mode-line-update 'all))))
  9412. (defun org-cleanup-narrow-column-properties ()
  9413. "Remove all properties related to narrow-column invisibility."
  9414. (let ((s 1))
  9415. (while (setq s (text-property-any s (point-max)
  9416. 'display org-narrow-column-arrow))
  9417. (remove-text-properties s (1+ s) '(display t)))
  9418. (setq s 1)
  9419. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9420. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9421. (setq s 1)
  9422. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9423. (remove-text-properties s (1+ s) '(invisible t)))))
  9424. ;; Install it as a minor mode.
  9425. (put 'orgtbl-mode :included t)
  9426. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9427. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9428. (defun orgtbl-make-binding (fun n &rest keys)
  9429. "Create a function for binding in the table minor mode.
  9430. FUN is the command to call inside a table. N is used to create a unique
  9431. command name. KEYS are keys that should be checked in for a command
  9432. to execute outside of tables."
  9433. (eval
  9434. (list 'defun
  9435. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9436. '(arg)
  9437. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9438. "Outside of tables, run the binding of `"
  9439. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9440. "'.")
  9441. '(interactive "p")
  9442. (list 'if
  9443. '(org-at-table-p)
  9444. (list 'call-interactively (list 'quote fun))
  9445. (list 'let '(orgtbl-mode)
  9446. (list 'call-interactively
  9447. (append '(or)
  9448. (mapcar (lambda (k)
  9449. (list 'key-binding k))
  9450. keys)
  9451. '('orgtbl-error))))))))
  9452. (defun orgtbl-error ()
  9453. "Error when there is no default binding for a table key."
  9454. (interactive)
  9455. (error "This key is has no function outside tables"))
  9456. (defun orgtbl-setup ()
  9457. "Setup orgtbl keymaps."
  9458. (let ((nfunc 0)
  9459. (bindings
  9460. (list
  9461. '([(meta shift left)] org-table-delete-column)
  9462. '([(meta left)] org-table-move-column-left)
  9463. '([(meta right)] org-table-move-column-right)
  9464. '([(meta shift right)] org-table-insert-column)
  9465. '([(meta shift up)] org-table-kill-row)
  9466. '([(meta shift down)] org-table-insert-row)
  9467. '([(meta up)] org-table-move-row-up)
  9468. '([(meta down)] org-table-move-row-down)
  9469. '("\C-c\C-w" org-table-cut-region)
  9470. '("\C-c\M-w" org-table-copy-region)
  9471. '("\C-c\C-y" org-table-paste-rectangle)
  9472. '("\C-c-" org-table-insert-hline)
  9473. '("\C-c}" org-table-toggle-coordinate-overlays)
  9474. '("\C-c{" org-table-toggle-formula-debugger)
  9475. '("\C-m" org-table-next-row)
  9476. '([(shift return)] org-table-copy-down)
  9477. '("\C-c\C-q" org-table-wrap-region)
  9478. '("\C-c?" org-table-field-info)
  9479. '("\C-c " org-table-blank-field)
  9480. '("\C-c+" org-table-sum)
  9481. '("\C-c=" org-table-eval-formula)
  9482. '("\C-c'" org-table-edit-formulas)
  9483. '("\C-c`" org-table-edit-field)
  9484. '("\C-c*" org-table-recalculate)
  9485. '("\C-c|" org-table-create-or-convert-from-region)
  9486. '("\C-c^" org-table-sort-lines)
  9487. '([(control ?#)] org-table-rotate-recalc-marks)))
  9488. elt key fun cmd)
  9489. (while (setq elt (pop bindings))
  9490. (setq nfunc (1+ nfunc))
  9491. (setq key (org-key (car elt))
  9492. fun (nth 1 elt)
  9493. cmd (orgtbl-make-binding fun nfunc key))
  9494. (org-defkey orgtbl-mode-map key cmd))
  9495. ;; Special treatment needed for TAB and RET
  9496. (org-defkey orgtbl-mode-map [(return)]
  9497. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9498. (org-defkey orgtbl-mode-map "\C-m"
  9499. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9500. (org-defkey orgtbl-mode-map [(tab)]
  9501. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9502. (org-defkey orgtbl-mode-map "\C-i"
  9503. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  9504. (org-defkey orgtbl-mode-map [(shift tab)]
  9505. (orgtbl-make-binding 'org-table-previous-field 104
  9506. [(shift tab)] [(tab)] "\C-i"))
  9507. (org-defkey orgtbl-mode-map "\M-\C-m"
  9508. (orgtbl-make-binding 'org-table-wrap-region 105
  9509. "\M-\C-m" [(meta return)]))
  9510. (org-defkey orgtbl-mode-map [(meta return)]
  9511. (orgtbl-make-binding 'org-table-wrap-region 106
  9512. [(meta return)] "\M-\C-m"))
  9513. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  9514. (when orgtbl-optimized
  9515. ;; If the user wants maximum table support, we need to hijack
  9516. ;; some standard editing functions
  9517. (org-remap orgtbl-mode-map
  9518. 'self-insert-command 'orgtbl-self-insert-command
  9519. 'delete-char 'org-delete-char
  9520. 'delete-backward-char 'org-delete-backward-char)
  9521. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  9522. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9523. '("OrgTbl"
  9524. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9525. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9526. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9527. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9528. "--"
  9529. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9530. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9531. ["Copy Field from Above"
  9532. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9533. "--"
  9534. ("Column"
  9535. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9536. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9537. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9538. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9539. ("Row"
  9540. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9541. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9542. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9543. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9544. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9545. "--"
  9546. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9547. ("Rectangle"
  9548. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9549. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9550. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9551. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9552. "--"
  9553. ("Radio tables"
  9554. ["Insert table template" orgtbl-insert-radio-table
  9555. (assq major-mode orgtbl-radio-table-templates)]
  9556. ["Comment/uncomment table" orgtbl-toggle-comment t])
  9557. "--"
  9558. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9559. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9560. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9561. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9562. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9563. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9564. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9565. ["Sum Column/Rectangle" org-table-sum
  9566. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9567. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9568. ["Debug Formulas"
  9569. org-table-toggle-formula-debugger :active (org-at-table-p)
  9570. :keys "C-c {"
  9571. :style toggle :selected org-table-formula-debug]
  9572. ["Show Col/Row Numbers"
  9573. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9574. :keys "C-c }"
  9575. :style toggle :selected org-table-overlay-coordinates]
  9576. ))
  9577. t))
  9578. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9579. "If the cursor is inside a table, realign the table.
  9580. It it is a table to be sent away to a receiver, do it.
  9581. With prefix arg, also recompute table."
  9582. (interactive "P")
  9583. (let ((pos (point)) action)
  9584. (save-excursion
  9585. (beginning-of-line 1)
  9586. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9587. ((looking-at "[ \t]*|") pos)
  9588. ((looking-at "#\\+TBLFM:") 'recalc))))
  9589. (cond
  9590. ((integerp action)
  9591. (goto-char action)
  9592. (org-table-maybe-eval-formula)
  9593. (if arg
  9594. (call-interactively 'org-table-recalculate)
  9595. (org-table-maybe-recalculate-line))
  9596. (call-interactively 'org-table-align)
  9597. (orgtbl-send-table 'maybe))
  9598. ((eq action 'recalc)
  9599. (save-excursion
  9600. (beginning-of-line 1)
  9601. (skip-chars-backward " \r\n\t")
  9602. (if (org-at-table-p)
  9603. (org-call-with-arg 'org-table-recalculate t))))
  9604. (t (let (orgtbl-mode)
  9605. (call-interactively (key-binding "\C-c\C-c")))))))
  9606. (defun orgtbl-tab (arg)
  9607. "Justification and field motion for `orgtbl-mode'."
  9608. (interactive "P")
  9609. (if arg (org-table-edit-field t)
  9610. (org-table-justify-field-maybe)
  9611. (org-table-next-field)))
  9612. (defun orgtbl-ret ()
  9613. "Justification and field motion for `orgtbl-mode'."
  9614. (interactive)
  9615. (org-table-justify-field-maybe)
  9616. (org-table-next-row))
  9617. (defun orgtbl-self-insert-command (N)
  9618. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9619. If the cursor is in a table looking at whitespace, the whitespace is
  9620. overwritten, and the table is not marked as requiring realignment."
  9621. (interactive "p")
  9622. (if (and (org-at-table-p)
  9623. (or
  9624. (and org-table-auto-blank-field
  9625. (member last-command
  9626. '(orgtbl-hijacker-command-100
  9627. orgtbl-hijacker-command-101
  9628. orgtbl-hijacker-command-102
  9629. orgtbl-hijacker-command-103
  9630. orgtbl-hijacker-command-104
  9631. orgtbl-hijacker-command-105))
  9632. (org-table-blank-field))
  9633. t)
  9634. (eq N 1)
  9635. (looking-at "[^|\n]* +|"))
  9636. (let (org-table-may-need-update)
  9637. (goto-char (1- (match-end 0)))
  9638. (delete-backward-char 1)
  9639. (goto-char (match-beginning 0))
  9640. (self-insert-command N))
  9641. (setq org-table-may-need-update t)
  9642. (let (orgtbl-mode)
  9643. (call-interactively (key-binding (vector last-input-event))))))
  9644. (defun org-force-self-insert (N)
  9645. "Needed to enforce self-insert under remapping."
  9646. (interactive "p")
  9647. (self-insert-command N))
  9648. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9649. "Regula expression matching exponentials as produced by calc.")
  9650. (defvar org-table-clean-did-remove-column nil)
  9651. (defun orgtbl-export (table target)
  9652. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9653. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9654. org-table-last-alignment org-table-last-column-widths
  9655. maxcol column)
  9656. (if (not (fboundp func))
  9657. (error "Cannot export orgtbl table to %s" target))
  9658. (setq lines (org-table-clean-before-export lines))
  9659. (setq table
  9660. (mapcar
  9661. (lambda (x)
  9662. (if (string-match org-table-hline-regexp x)
  9663. 'hline
  9664. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  9665. lines))
  9666. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  9667. table)))
  9668. (loop for i from (1- maxcol) downto 0 do
  9669. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  9670. (setq column (delq nil column))
  9671. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  9672. (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))
  9673. (funcall func table nil)))
  9674. (defun orgtbl-send-table (&optional maybe)
  9675. "Send a tranformed version of this table to the receiver position.
  9676. With argument MAYBE, fail quietly if no transformation is defined for
  9677. this table."
  9678. (interactive)
  9679. (catch 'exit
  9680. (unless (org-at-table-p) (error "Not at a table"))
  9681. ;; when non-interactive, we assume align has just happened.
  9682. (when (interactive-p) (org-table-align))
  9683. (save-excursion
  9684. (goto-char (org-table-begin))
  9685. (beginning-of-line 0)
  9686. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  9687. (if maybe
  9688. (throw 'exit nil)
  9689. (error "Don't know how to transform this table."))))
  9690. (let* ((name (match-string 1))
  9691. beg
  9692. (transform (intern (match-string 2)))
  9693. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  9694. (skip (plist-get params :skip))
  9695. (skipcols (plist-get params :skipcols))
  9696. (txt (buffer-substring-no-properties
  9697. (org-table-begin) (org-table-end)))
  9698. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  9699. (lines (org-table-clean-before-export lines))
  9700. (i0 (if org-table-clean-did-remove-column 2 1))
  9701. (table (mapcar
  9702. (lambda (x)
  9703. (if (string-match org-table-hline-regexp x)
  9704. 'hline
  9705. (org-remove-by-index
  9706. (org-split-string (org-trim x) "\\s-*|\\s-*")
  9707. skipcols i0)))
  9708. lines))
  9709. (fun (if (= i0 2) 'cdr 'identity))
  9710. (org-table-last-alignment
  9711. (org-remove-by-index (funcall fun org-table-last-alignment)
  9712. skipcols i0))
  9713. (org-table-last-column-widths
  9714. (org-remove-by-index (funcall fun org-table-last-column-widths)
  9715. skipcols i0)))
  9716. (unless (fboundp transform)
  9717. (error "No such transformation function %s" transform))
  9718. (setq txt (funcall transform table params))
  9719. ;; Find the insertion place
  9720. (save-excursion
  9721. (goto-char (point-min))
  9722. (unless (re-search-forward
  9723. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  9724. (error "Don't know where to insert translated table"))
  9725. (goto-char (match-beginning 0))
  9726. (beginning-of-line 2)
  9727. (setq beg (point))
  9728. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  9729. (error "Cannot find end of insertion region"))
  9730. (beginning-of-line 1)
  9731. (delete-region beg (point))
  9732. (goto-char beg)
  9733. (insert txt "\n"))
  9734. (message "Table converted and installed at receiver location"))))
  9735. (defun org-remove-by-index (list indices &optional i0)
  9736. "Remove the elements in LIST with indices in INDICES.
  9737. First element has index 0, or I0 if given."
  9738. (if (not indices)
  9739. list
  9740. (if (integerp indices) (setq indices (list indices)))
  9741. (setq i0 (1- (or i0 0)))
  9742. (delq :rm (mapcar (lambda (x)
  9743. (setq i0 (1+ i0))
  9744. (if (memq i0 indices) :rm x))
  9745. list))))
  9746. (defun orgtbl-toggle-comment ()
  9747. "Comment or uncomment the orgtbl at point."
  9748. (interactive)
  9749. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  9750. (re2 (concat "^" orgtbl-line-start-regexp))
  9751. (commented (save-excursion (beginning-of-line 1)
  9752. (cond ((looking-at re1) t)
  9753. ((looking-at re2) nil)
  9754. (t (error "Not at an org table")))))
  9755. (re (if commented re1 re2))
  9756. beg end)
  9757. (save-excursion
  9758. (beginning-of-line 1)
  9759. (while (looking-at re) (beginning-of-line 0))
  9760. (beginning-of-line 2)
  9761. (setq beg (point))
  9762. (while (looking-at re) (beginning-of-line 2))
  9763. (setq end (point)))
  9764. (comment-region beg end (if commented '(4) nil))))
  9765. (defun orgtbl-insert-radio-table ()
  9766. "Insert a radio table template appropriate for this major mode."
  9767. (interactive)
  9768. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  9769. (txt (nth 1 e))
  9770. name pos)
  9771. (unless e (error "No radio table setup defined for %s" major-mode))
  9772. (setq name (read-string "Table name: "))
  9773. (while (string-match "%n" txt)
  9774. (setq txt (replace-match name t t txt)))
  9775. (or (bolp) (insert "\n"))
  9776. (setq pos (point))
  9777. (insert txt)
  9778. (goto-char pos)))
  9779. (defun org-get-param (params header i sym &optional hsym)
  9780. "Get parameter value for symbol SYM.
  9781. If this is a header line, actually get the value for the symbol with an
  9782. additional \"h\" inserted after the colon.
  9783. If the value is a protperty list, get the element for the current column.
  9784. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  9785. (let ((val (plist-get params sym)))
  9786. (and hsym header (setq val (or (plist-get params hsym) val)))
  9787. (if (consp val) (plist-get val i) val)))
  9788. (defun orgtbl-to-generic (table params)
  9789. "Convert the orgtbl-mode TABLE to some other format.
  9790. This generic routine can be used for many standard cases.
  9791. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9792. separator line, or a list of fields for that line.
  9793. PARAMS is a property list of parameters that can influence the conversion.
  9794. For the generic converter, some parameters are obligatory: You need to
  9795. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  9796. :splice, you must have :tstart and :tend.
  9797. Valid parameters are
  9798. :tstart String to start the table. Ignored when :splice is t.
  9799. :tend String to end the table. Ignored when :splice is t.
  9800. :splice When set to t, return only table body lines, don't wrap
  9801. them into :tstart and :tend. Default is nil.
  9802. :hline String to be inserted on horizontal separation lines.
  9803. May be nil to ignore hlines.
  9804. :lstart String to start a new table line.
  9805. :lend String to end a table line
  9806. :sep Separator between two fields
  9807. :lfmt Format for entire line, with enough %s to capture all fields.
  9808. If this is present, :lstart, :lend, and :sep are ignored.
  9809. :fmt A format to be used to wrap the field, should contain
  9810. %s for the original field value. For example, to wrap
  9811. everything in dollars, you could use :fmt \"$%s$\".
  9812. This may also be a property list with column numbers and
  9813. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9814. :hlstart :hlend :hlsep :hlfmt :hfmt
  9815. Same as above, specific for the header lines in the table.
  9816. All lines before the first hline are treated as header.
  9817. If any of these is not present, the data line value is used.
  9818. :efmt Use this format to print numbers with exponentials.
  9819. The format should have %s twice for inserting mantissa
  9820. and exponent, for example \"%s\\\\times10^{%s}\". This
  9821. may also be a property list with column numbers and
  9822. formats. :fmt will still be applied after :efmt.
  9823. In addition to this, the parameters :skip and :skipcols are always handled
  9824. directly by `orgtbl-send-table'. See manual."
  9825. (interactive)
  9826. (let* ((p params)
  9827. (splicep (plist-get p :splice))
  9828. (hline (plist-get p :hline))
  9829. rtn line i fm efm lfmt h)
  9830. ;; Do we have a header?
  9831. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  9832. (setq h t))
  9833. ;; Put header
  9834. (unless splicep
  9835. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  9836. ;; Now loop over all lines
  9837. (while (setq line (pop table))
  9838. (if (eq line 'hline)
  9839. ;; A horizontal separator line
  9840. (progn (if hline (push hline rtn))
  9841. (setq h nil)) ; no longer in header
  9842. ;; A normal line. Convert the fields, push line onto the result list
  9843. (setq i 0)
  9844. (setq line
  9845. (mapcar
  9846. (lambda (f)
  9847. (setq i (1+ i)
  9848. fm (org-get-param p h i :fmt :hfmt)
  9849. efm (org-get-param p h i :efmt))
  9850. (if (and efm (string-match orgtbl-exp-regexp f))
  9851. (setq f (format
  9852. efm (match-string 1 f) (match-string 2 f))))
  9853. (if fm (setq f (format fm f)))
  9854. f)
  9855. line))
  9856. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  9857. (push (apply 'format lfmt line) rtn)
  9858. (push (concat
  9859. (org-get-param p h i :lstart :hlstart)
  9860. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  9861. (org-get-param p h i :lend :hlend))
  9862. rtn))))
  9863. (unless splicep
  9864. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  9865. (mapconcat 'identity (nreverse rtn) "\n")))
  9866. (defun orgtbl-to-latex (table params)
  9867. "Convert the orgtbl-mode TABLE to LaTeX.
  9868. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9869. separator line, or a list of fields for that line.
  9870. PARAMS is a property list of parameters that can influence the conversion.
  9871. Supports all parameters from `orgtbl-to-generic'. Most important for
  9872. LaTeX are:
  9873. :splice When set to t, return only table body lines, don't wrap
  9874. them into a tabular environment. Default is nil.
  9875. :fmt A format to be used to wrap the field, should contain %s for the
  9876. original field value. For example, to wrap everything in dollars,
  9877. use :fmt \"$%s$\". This may also be a property list with column
  9878. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  9879. :efmt Format for transforming numbers with exponentials. The format
  9880. should have %s twice for inserting mantissa and exponent, for
  9881. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  9882. This may also be a property list with column numbers and formats.
  9883. The general parameters :skip and :skipcols have already been applied when
  9884. this function is called."
  9885. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  9886. org-table-last-alignment ""))
  9887. (params2
  9888. (list
  9889. :tstart (concat "\\begin{tabular}{" alignment "}")
  9890. :tend "\\end{tabular}"
  9891. :lstart "" :lend " \\\\" :sep " & "
  9892. :efmt "%s\\,(%s)" :hline "\\hline")))
  9893. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9894. (defun orgtbl-to-html (table params)
  9895. "Convert the orgtbl-mode TABLE to LaTeX.
  9896. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9897. separator line, or a list of fields for that line.
  9898. PARAMS is a property list of parameters that can influence the conversion.
  9899. Currently this function recognizes the following parameters:
  9900. :splice When set to t, return only table body lines, don't wrap
  9901. them into a <table> environment. Default is nil.
  9902. The general parameters :skip and :skipcols have already been applied when
  9903. this function is called. The function does *not* use `orgtbl-to-generic',
  9904. so you cannot specify parameters for it."
  9905. (let* ((splicep (plist-get params :splice))
  9906. html)
  9907. ;; Just call the formatter we already have
  9908. ;; We need to make text lines for it, so put the fields back together.
  9909. (setq html (org-format-org-table-html
  9910. (mapcar
  9911. (lambda (x)
  9912. (if (eq x 'hline)
  9913. "|----+----|"
  9914. (concat "| " (mapconcat 'identity x " | ") " |")))
  9915. table)
  9916. splicep))
  9917. (if (string-match "\n+\\'" html)
  9918. (setq html (replace-match "" t t html)))
  9919. html))
  9920. (defun orgtbl-to-texinfo (table params)
  9921. "Convert the orgtbl-mode TABLE to TeXInfo.
  9922. TABLE is a list, each entry either the symbol `hline' for a horizontal
  9923. separator line, or a list of fields for that line.
  9924. PARAMS is a property list of parameters that can influence the conversion.
  9925. Supports all parameters from `orgtbl-to-generic'. Most important for
  9926. TeXInfo are:
  9927. :splice nil/t When set to t, return only table body lines, don't wrap
  9928. them into a multitable environment. Default is nil.
  9929. :fmt fmt A format to be used to wrap the field, should contain
  9930. %s for the original field value. For example, to wrap
  9931. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  9932. This may also be a property list with column numbers and
  9933. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  9934. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  9935. are computed automatically from the width of the columns
  9936. under org-mode.
  9937. The general parameters :skip and :skipcols have already been applied when
  9938. this function is called."
  9939. (let* ((total (float (apply '+ org-table-last-column-widths)))
  9940. (colfrac (or (plist-get params :cf)
  9941. (mapconcat
  9942. (lambda (x) (format "%.3f" (/ (float x) total)))
  9943. org-table-last-column-widths " ")))
  9944. (params2
  9945. (list
  9946. :tstart (concat "@multitable @columnfractions " colfrac)
  9947. :tend "@end multitable"
  9948. :lstart "@item " :lend "" :sep " @tab "
  9949. :hlstart "@headitem ")))
  9950. (orgtbl-to-generic table (org-combine-plists params2 params))))
  9951. ;;;; Link Stuff
  9952. ;;; Link abbreviations
  9953. (defun org-link-expand-abbrev (link)
  9954. "Apply replacements as defined in `org-link-abbrev-alist."
  9955. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  9956. (let* ((key (match-string 1 link))
  9957. (as (or (assoc key org-link-abbrev-alist-local)
  9958. (assoc key org-link-abbrev-alist)))
  9959. (tag (and (match-end 2) (match-string 3 link)))
  9960. rpl)
  9961. (if (not as)
  9962. link
  9963. (setq rpl (cdr as))
  9964. (cond
  9965. ((symbolp rpl) (funcall rpl tag))
  9966. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  9967. (t (concat rpl tag)))))
  9968. link))
  9969. ;;; Storing and inserting links
  9970. (defvar org-insert-link-history nil
  9971. "Minibuffer history for links inserted with `org-insert-link'.")
  9972. (defvar org-stored-links nil
  9973. "Contains the links stored with `org-store-link'.")
  9974. (defvar org-store-link-plist nil
  9975. "Plist with info about the most recently link created with `org-store-link'.")
  9976. (defvar org-link-protocols nil
  9977. "Link protocols added to Org-mode using `org-add-link-type'.")
  9978. (defvar org-store-link-functions nil
  9979. "List of functions that are called to create and store a link.
  9980. Each function will be called in turn until one returns a non-nil
  9981. value. Each function should check if it is responsible for creating
  9982. this link (for example by looking at the major mode).
  9983. If not, it must exit and return nil.
  9984. If yes, it should return a non-nil value after a calling
  9985. `org-store-link-properties' with a list of properties and values.
  9986. Special properties are:
  9987. :type The link prefix. like \"http\". This must be given.
  9988. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  9989. This is obligatory as well.
  9990. :description Optional default description for the second pair
  9991. of brackets in an Org-mode link. The user can still change
  9992. this when inserting this link into an Org-mode buffer.
  9993. In addition to these, any additional properties can be specified
  9994. and then used in remember templates.")
  9995. (defun org-add-link-type (type &optional follow publish)
  9996. "Add TYPE to the list of `org-link-types'.
  9997. Re-compute all regular expressions depending on `org-link-types'
  9998. FOLLOW and PUBLISH are two functions. Both take the link path as
  9999. an argument.
  10000. FOLLOW should do whatever is necessary to follow the link, for example
  10001. to find a file or display a mail message.
  10002. PUBLISH takes the path and retuns the string that should be used when
  10003. this document is published."
  10004. (add-to-list 'org-link-types type t)
  10005. (org-make-link-regexps)
  10006. (add-to-list 'org-link-protocols
  10007. (list type follow publish)))
  10008. (defun org-add-agenda-custom-command (entry)
  10009. "Replace or add a command in `org-agenda-custom-commands'.
  10010. This is mostly for hacking and trying a new command - once the command
  10011. works you probably want to add it to `org-agenda-custom-commands' for good."
  10012. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10013. (if ass
  10014. (setcdr ass (cdr entry))
  10015. (push entry org-agenda-custom-commands))))
  10016. ;;;###autoload
  10017. (defun org-store-link (arg)
  10018. "\\<org-mode-map>Store an org-link to the current location.
  10019. This link can later be inserted into an org-buffer with
  10020. \\[org-insert-link].
  10021. For some link types, a prefix arg is interpreted:
  10022. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10023. For file links, arg negates `org-context-in-file-links'."
  10024. (interactive "P")
  10025. (setq org-store-link-plist nil) ; reset
  10026. (let (link cpltxt desc description search txt)
  10027. (cond
  10028. ((run-hook-with-args-until-success 'org-store-link-functions)
  10029. (setq link (plist-get org-store-link-plist :link)
  10030. desc (or (plist-get org-store-link-plist :description) link)))
  10031. ((eq major-mode 'bbdb-mode)
  10032. (let ((name (bbdb-record-name (bbdb-current-record)))
  10033. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10034. (setq cpltxt (concat "bbdb:" (or name company))
  10035. link (org-make-link cpltxt))
  10036. (org-store-link-props :type "bbdb" :name name :company company)))
  10037. ((eq major-mode 'Info-mode)
  10038. (setq link (org-make-link "info:"
  10039. (file-name-nondirectory Info-current-file)
  10040. ":" Info-current-node))
  10041. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10042. ":" Info-current-node))
  10043. (org-store-link-props :type "info" :file Info-current-file
  10044. :node Info-current-node))
  10045. ((eq major-mode 'calendar-mode)
  10046. (let ((cd (calendar-cursor-to-date)))
  10047. (setq link
  10048. (format-time-string
  10049. (car org-time-stamp-formats)
  10050. (apply 'encode-time
  10051. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10052. nil nil nil))))
  10053. (org-store-link-props :type "calendar" :date cd)))
  10054. ((or (eq major-mode 'vm-summary-mode)
  10055. (eq major-mode 'vm-presentation-mode))
  10056. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10057. (vm-follow-summary-cursor)
  10058. (save-excursion
  10059. (vm-select-folder-buffer)
  10060. (let* ((message (car vm-message-pointer))
  10061. (folder buffer-file-name)
  10062. (subject (vm-su-subject message))
  10063. (to (vm-get-header-contents message "To"))
  10064. (from (vm-get-header-contents message "From"))
  10065. (message-id (vm-su-message-id message)))
  10066. (org-store-link-props :type "vm" :from from :to to :subject subject
  10067. :message-id message-id)
  10068. (setq message-id (org-remove-angle-brackets message-id))
  10069. (setq folder (abbreviate-file-name folder))
  10070. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10071. folder)
  10072. (setq folder (replace-match "" t t folder)))
  10073. (setq cpltxt (org-email-link-description))
  10074. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10075. ((eq major-mode 'wl-summary-mode)
  10076. (let* ((msgnum (wl-summary-message-number))
  10077. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10078. msgnum 'message-id))
  10079. (wl-message-entity
  10080. (if (fboundp 'elmo-message-entity)
  10081. (elmo-message-entity
  10082. wl-summary-buffer-elmo-folder msgnum)
  10083. (elmo-msgdb-overview-get-entity
  10084. msgnum (wl-summary-buffer-msgdb))))
  10085. (from (wl-summary-line-from))
  10086. (to (elmo-message-entity-field wl-message-entity 'to))
  10087. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10088. (wl-summary-line-subject))))
  10089. (org-store-link-props :type "wl" :from from :to to
  10090. :subject subject :message-id message-id)
  10091. (setq message-id (org-remove-angle-brackets message-id))
  10092. (setq cpltxt (org-email-link-description))
  10093. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10094. "#" message-id))))
  10095. ((or (equal major-mode 'mh-folder-mode)
  10096. (equal major-mode 'mh-show-mode))
  10097. (let ((from (org-mhe-get-header "From:"))
  10098. (to (org-mhe-get-header "To:"))
  10099. (message-id (org-mhe-get-header "Message-Id:"))
  10100. (subject (org-mhe-get-header "Subject:")))
  10101. (org-store-link-props :type "mh" :from from :to to
  10102. :subject subject :message-id message-id)
  10103. (setq cpltxt (org-email-link-description))
  10104. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10105. (org-remove-angle-brackets message-id)))))
  10106. ((eq major-mode 'rmail-mode)
  10107. (save-excursion
  10108. (save-restriction
  10109. (rmail-narrow-to-non-pruned-header)
  10110. (let ((folder buffer-file-name)
  10111. (message-id (mail-fetch-field "message-id"))
  10112. (from (mail-fetch-field "from"))
  10113. (to (mail-fetch-field "to"))
  10114. (subject (mail-fetch-field "subject")))
  10115. (org-store-link-props
  10116. :type "rmail" :from from :to to
  10117. :subject subject :message-id message-id)
  10118. (setq message-id (org-remove-angle-brackets message-id))
  10119. (setq cpltxt (org-email-link-description))
  10120. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10121. ((eq major-mode 'gnus-group-mode)
  10122. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10123. (gnus-group-group-name)) ; version
  10124. ((fboundp 'gnus-group-name)
  10125. (gnus-group-name))
  10126. (t "???"))))
  10127. (unless group (error "Not on a group"))
  10128. (org-store-link-props :type "gnus" :group group)
  10129. (setq cpltxt (concat
  10130. (if (org-xor arg org-usenet-links-prefer-google)
  10131. "http://groups.google.com/groups?group="
  10132. "gnus:")
  10133. group)
  10134. link (org-make-link cpltxt))))
  10135. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10136. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10137. (let* ((group gnus-newsgroup-name)
  10138. (article (gnus-summary-article-number))
  10139. (header (gnus-summary-article-header article))
  10140. (from (mail-header-from header))
  10141. (message-id (mail-header-id header))
  10142. (date (mail-header-date header))
  10143. (subject (gnus-summary-subject-string)))
  10144. (org-store-link-props :type "gnus" :from from :subject subject
  10145. :message-id message-id :group group)
  10146. (setq cpltxt (org-email-link-description))
  10147. (if (org-xor arg org-usenet-links-prefer-google)
  10148. (setq link
  10149. (concat
  10150. cpltxt "\n "
  10151. (format "http://groups.google.com/groups?as_umsgid=%s"
  10152. (org-fixup-message-id-for-http message-id))))
  10153. (setq link (org-make-link "gnus:" group
  10154. "#" (number-to-string article))))))
  10155. ((eq major-mode 'w3-mode)
  10156. (setq cpltxt (url-view-url t)
  10157. link (org-make-link cpltxt))
  10158. (org-store-link-props :type "w3" :url (url-view-url t)))
  10159. ((eq major-mode 'w3m-mode)
  10160. (setq cpltxt (or w3m-current-title w3m-current-url)
  10161. link (org-make-link w3m-current-url))
  10162. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10163. ((setq search (run-hook-with-args-until-success
  10164. 'org-create-file-search-functions))
  10165. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10166. "::" search))
  10167. (setq cpltxt (or description link)))
  10168. ((eq major-mode 'image-mode)
  10169. (setq cpltxt (concat "file:"
  10170. (abbreviate-file-name buffer-file-name))
  10171. link (org-make-link cpltxt))
  10172. (org-store-link-props :type "image" :file buffer-file-name))
  10173. ((eq major-mode 'dired-mode)
  10174. ;; link to the file in the current line
  10175. (setq cpltxt (concat "file:"
  10176. (abbreviate-file-name
  10177. (expand-file-name
  10178. (dired-get-filename nil t))))
  10179. link (org-make-link cpltxt)))
  10180. ((and buffer-file-name (org-mode-p))
  10181. ;; Just link to current headline
  10182. (setq cpltxt (concat "file:"
  10183. (abbreviate-file-name buffer-file-name)))
  10184. ;; Add a context search string
  10185. (when (org-xor org-context-in-file-links arg)
  10186. ;; Check if we are on a target
  10187. (if (org-in-regexp "<<\\(.*?\\)>>")
  10188. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10189. (setq txt (cond
  10190. ((org-on-heading-p) nil)
  10191. ((org-region-active-p)
  10192. (buffer-substring (region-beginning) (region-end)))
  10193. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10194. (when (or (null txt) (string-match "\\S-" txt))
  10195. (setq cpltxt
  10196. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10197. desc "NONE"))))
  10198. (if (string-match "::\\'" cpltxt)
  10199. (setq cpltxt (substring cpltxt 0 -2)))
  10200. (setq link (org-make-link cpltxt)))
  10201. ((buffer-file-name (buffer-base-buffer))
  10202. ;; Just link to this file here.
  10203. (setq cpltxt (concat "file:"
  10204. (abbreviate-file-name
  10205. (buffer-file-name (buffer-base-buffer)))))
  10206. ;; Add a context string
  10207. (when (org-xor org-context-in-file-links arg)
  10208. (setq txt (if (org-region-active-p)
  10209. (buffer-substring (region-beginning) (region-end))
  10210. (buffer-substring (point-at-bol) (point-at-eol))))
  10211. ;; Only use search option if there is some text.
  10212. (when (string-match "\\S-" txt)
  10213. (setq cpltxt
  10214. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10215. desc "NONE")))
  10216. (setq link (org-make-link cpltxt)))
  10217. ((interactive-p)
  10218. (error "Cannot link to a buffer which is not visiting a file"))
  10219. (t (setq link nil)))
  10220. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10221. (setq link (or link cpltxt)
  10222. desc (or desc cpltxt))
  10223. (if (equal desc "NONE") (setq desc nil))
  10224. (if (and (interactive-p) link)
  10225. (progn
  10226. (setq org-stored-links
  10227. (cons (list link desc) org-stored-links))
  10228. (message "Stored: %s" (or desc link)))
  10229. (and link (org-make-link-string link desc)))))
  10230. (defun org-store-link-props (&rest plist)
  10231. "Store link properties, extract names and addresses."
  10232. (let (x adr)
  10233. (when (setq x (plist-get plist :from))
  10234. (setq adr (mail-extract-address-components x))
  10235. (plist-put plist :fromname (car adr))
  10236. (plist-put plist :fromaddress (nth 1 adr)))
  10237. (when (setq x (plist-get plist :to))
  10238. (setq adr (mail-extract-address-components x))
  10239. (plist-put plist :toname (car adr))
  10240. (plist-put plist :toaddress (nth 1 adr))))
  10241. (let ((from (plist-get plist :from))
  10242. (to (plist-get plist :to)))
  10243. (when (and from to org-from-is-user-regexp)
  10244. (plist-put plist :fromto
  10245. (if (string-match org-from-is-user-regexp from)
  10246. (concat "to %t")
  10247. (concat "from %f")))))
  10248. (setq org-store-link-plist plist))
  10249. (defun org-email-link-description (&optional fmt)
  10250. "Return the description part of an email link.
  10251. This takes information from `org-store-link-plist' and formats it
  10252. according to FMT (default from `org-email-link-description-format')."
  10253. (setq fmt (or fmt org-email-link-description-format))
  10254. (let* ((p org-store-link-plist)
  10255. (to (plist-get p :toaddress))
  10256. (from (plist-get p :fromaddress))
  10257. (table
  10258. (list
  10259. (cons "%c" (plist-get p :fromto))
  10260. (cons "%F" (plist-get p :from))
  10261. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  10262. (cons "%T" (plist-get p :to))
  10263. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  10264. (cons "%s" (plist-get p :subject))
  10265. (cons "%m" (plist-get p :message-id)))))
  10266. (when (string-match "%c" fmt)
  10267. ;; Check if the user wrote this message
  10268. (if (and org-from-is-user-regexp from to
  10269. (save-match-data (string-match org-from-is-user-regexp from)))
  10270. (setq fmt (replace-match "to %t" t t fmt))
  10271. (setq fmt (replace-match "from %f" t t fmt))))
  10272. (org-replace-escapes fmt table)))
  10273. (defun org-make-org-heading-search-string (&optional string heading)
  10274. "Make search string for STRING or current headline."
  10275. (interactive)
  10276. (let ((s (or string (org-get-heading))))
  10277. (unless (and string (not heading))
  10278. ;; We are using a headline, clean up garbage in there.
  10279. (if (string-match org-todo-regexp s)
  10280. (setq s (replace-match "" t t s)))
  10281. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  10282. (setq s (replace-match "" t t s)))
  10283. (setq s (org-trim s))
  10284. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10285. org-comment-string "\\)") s)
  10286. (setq s (replace-match "" t t s)))
  10287. (while (string-match org-ts-regexp s)
  10288. (setq s (replace-match "" t t s))))
  10289. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10290. (setq s (replace-match " " t t s)))
  10291. (or string (setq s (concat "*" s))) ; Add * for headlines
  10292. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10293. (defun org-make-link (&rest strings)
  10294. "Concatenate STRINGS."
  10295. (apply 'concat strings))
  10296. (defun org-make-link-string (link &optional description)
  10297. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10298. (unless (string-match "\\S-" link)
  10299. (error "Empty link"))
  10300. (when (stringp description)
  10301. ;; Remove brackets from the description, they are fatal.
  10302. (while (string-match "\\[\\|\\]" description)
  10303. (setq description (replace-match "" t t description))))
  10304. (when (equal (org-link-escape link) description)
  10305. ;; No description needed, it is identical
  10306. (setq description nil))
  10307. (when (and (not description)
  10308. (not (equal link (org-link-escape link))))
  10309. (setq description link))
  10310. (concat "[[" (org-link-escape link) "]"
  10311. (if description (concat "[" description "]") "")
  10312. "]"))
  10313. (defconst org-link-escape-chars
  10314. '((" " . "%20")
  10315. ("[" . "%5B")
  10316. ("]" . "%5d")
  10317. ("\340" . "%E0") ; `a
  10318. ("\342" . "%E2") ; ^a
  10319. ("\347" . "%E7") ; ,c
  10320. ("\350" . "%E8") ; `e
  10321. ("\351" . "%E9") ; 'e
  10322. ("\352" . "%EA") ; ^e
  10323. ("\356" . "%EE") ; ^i
  10324. ("\364" . "%F4") ; ^o
  10325. ("\371" . "%F9") ; `u
  10326. ("\373" . "%FB") ; ^u
  10327. (";" . "%3B")
  10328. ("?" . "%3F")
  10329. ("=" . "%3D")
  10330. ("+" . "%2B")
  10331. )
  10332. "Association list of escapes for some characters problematic in links.
  10333. This is the list that is used for internal purposes.")
  10334. (defconst org-link-escape-chars-browser
  10335. '((" " . "%20"))
  10336. "Association list of escapes for some characters problematic in links.
  10337. This is the list that is used before handing over to the browser.")
  10338. (defun org-link-escape (text &optional table)
  10339. "Escape charaters in TEXT that are problematic for links."
  10340. (setq table (or table org-link-escape-chars))
  10341. (when text
  10342. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  10343. table "\\|")))
  10344. (while (string-match re text)
  10345. (setq text
  10346. (replace-match
  10347. (cdr (assoc (match-string 0 text) table))
  10348. t t text)))
  10349. text)))
  10350. (defun org-link-unescape (text &optional table)
  10351. "Reverse the action of `org-link-escape'."
  10352. (setq table (or table org-link-escape-chars))
  10353. (when text
  10354. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10355. table "\\|")))
  10356. (while (string-match re text)
  10357. (setq text
  10358. (replace-match
  10359. (car (rassoc (match-string 0 text) table))
  10360. t t text)))
  10361. text)))
  10362. (defun org-xor (a b)
  10363. "Exclusive or."
  10364. (if a (not b) b))
  10365. (defun org-get-header (header)
  10366. "Find a header field in the current buffer."
  10367. (save-excursion
  10368. (goto-char (point-min))
  10369. (let ((case-fold-search t) s)
  10370. (cond
  10371. ((eq header 'from)
  10372. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10373. (setq s (match-string 1)))
  10374. (while (string-match "\"" s)
  10375. (setq s (replace-match "" t t s)))
  10376. (if (string-match "[<(].*" s)
  10377. (setq s (replace-match "" t t s))))
  10378. ((eq header 'message-id)
  10379. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10380. (setq s (match-string 1))))
  10381. ((eq header 'subject)
  10382. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  10383. (setq s (match-string 1)))))
  10384. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  10385. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  10386. s)))
  10387. (defun org-fixup-message-id-for-http (s)
  10388. "Replace special characters in a message id, so it can be used in an http query."
  10389. (while (string-match "<" s)
  10390. (setq s (replace-match "%3C" t t s)))
  10391. (while (string-match ">" s)
  10392. (setq s (replace-match "%3E" t t s)))
  10393. (while (string-match "@" s)
  10394. (setq s (replace-match "%40" t t s)))
  10395. s)
  10396. ;;;###autoload
  10397. (defun org-insert-link-global ()
  10398. "Insert a link like Org-mode does.
  10399. This command can be called in any mode to insert a link in Org-mode syntax."
  10400. (interactive)
  10401. (org-run-like-in-org-mode 'org-insert-link))
  10402. (defun org-insert-link (&optional complete-file)
  10403. "Insert a link. At the prompt, enter the link.
  10404. Completion can be used to select a link previously stored with
  10405. `org-store-link'. When the empty string is entered (i.e. if you just
  10406. press RET at the prompt), the link defaults to the most recently
  10407. stored link. As SPC triggers completion in the minibuffer, you need to
  10408. use M-SPC or C-q SPC to force the insertion of a space character.
  10409. You will also be prompted for a description, and if one is given, it will
  10410. be displayed in the buffer instead of the link.
  10411. If there is already a link at point, this command will allow you to edit link
  10412. and description parts.
  10413. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10414. selected using completion. The path to the file will be relative to
  10415. the current directory if the file is in the current directory or a
  10416. subdirectory. Otherwise, the link will be the absolute path as
  10417. completed in the minibuffer (i.e. normally ~/path/to/file).
  10418. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10419. is in the current directory or below.
  10420. With three \\[universal-argument] prefixes, negate the meaning of
  10421. `org-keep-stored-link-after-insertion'."
  10422. (interactive "P")
  10423. (let* ((wcf (current-window-configuration))
  10424. (region (if (org-region-active-p)
  10425. (buffer-substring (region-beginning) (region-end))))
  10426. (remove (and region (list (region-beginning) (region-end))))
  10427. (desc region)
  10428. tmphist ; byte-compile incorrectly complains about this
  10429. link entry file)
  10430. (cond
  10431. ((org-in-regexp org-bracket-link-regexp 1)
  10432. ;; We do have a link at point, and we are going to edit it.
  10433. (setq remove (list (match-beginning 0) (match-end 0)))
  10434. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10435. (setq link (read-string "Link: "
  10436. (org-link-unescape
  10437. (org-match-string-no-properties 1)))))
  10438. ((or (org-in-regexp org-angle-link-re)
  10439. (org-in-regexp org-plain-link-re))
  10440. ;; Convert to bracket link
  10441. (setq remove (list (match-beginning 0) (match-end 0))
  10442. link (read-string "Link: "
  10443. (org-remove-angle-brackets (match-string 0)))))
  10444. ((equal complete-file '(4))
  10445. ;; Completing read for file names.
  10446. (setq file (read-file-name "File: "))
  10447. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10448. (pwd1 (file-name-as-directory (abbreviate-file-name
  10449. (expand-file-name ".")))))
  10450. (cond
  10451. ((equal complete-file '(16))
  10452. (setq link (org-make-link
  10453. "file:"
  10454. (abbreviate-file-name (expand-file-name file)))))
  10455. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10456. (setq link (org-make-link "file:" (match-string 1 file))))
  10457. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10458. (expand-file-name file))
  10459. (setq link (org-make-link
  10460. "file:" (match-string 1 (expand-file-name file)))))
  10461. (t (setq link (org-make-link "file:" file))))))
  10462. (t
  10463. ;; Read link, with completion for stored links.
  10464. (with-output-to-temp-buffer "*Org Links*"
  10465. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  10466. (when org-stored-links
  10467. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  10468. (princ (mapconcat
  10469. (lambda (x)
  10470. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  10471. (reverse org-stored-links) "\n"))))
  10472. (let ((cw (selected-window)))
  10473. (select-window (get-buffer-window "*Org Links*"))
  10474. (shrink-window-if-larger-than-buffer)
  10475. (setq truncate-lines t)
  10476. (select-window cw))
  10477. ;; Fake a link history, containing the stored links.
  10478. (setq tmphist (append (mapcar 'car org-stored-links)
  10479. org-insert-link-history))
  10480. (unwind-protect
  10481. (setq link (org-completing-read
  10482. "Link: "
  10483. (append
  10484. (mapcar (lambda (x) (list (concat (car x) ":")))
  10485. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  10486. (mapcar (lambda (x) (list (concat x ":")))
  10487. org-link-types))
  10488. nil nil nil
  10489. 'tmphist
  10490. (or (car (car org-stored-links)))))
  10491. (set-window-configuration wcf)
  10492. (kill-buffer "*Org Links*"))
  10493. (setq entry (assoc link org-stored-links))
  10494. (or entry (push link org-insert-link-history))
  10495. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10496. (not org-keep-stored-link-after-insertion))
  10497. (setq org-stored-links (delq (assoc link org-stored-links)
  10498. org-stored-links)))
  10499. (setq desc (or desc (nth 1 entry)))))
  10500. (if (string-match org-plain-link-re link)
  10501. ;; URL-like link, normalize the use of angular brackets.
  10502. (setq link (org-make-link (org-remove-angle-brackets link))))
  10503. ;; Check if we are linking to the current file with a search option
  10504. ;; If yes, simplify the link by using only the search option.
  10505. (when (and buffer-file-name
  10506. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10507. (let* ((path (match-string 1 link))
  10508. (case-fold-search nil)
  10509. (search (match-string 2 link)))
  10510. (save-match-data
  10511. (if (equal (file-truename buffer-file-name) (file-truename path))
  10512. ;; We are linking to this same file, with a search option
  10513. (setq link search)))))
  10514. ;; Check if we can/should use a relative path. If yes, simplify the link
  10515. (when (string-match "\\<file:\\(.*\\)" link)
  10516. (let* ((path (match-string 1 link))
  10517. (origpath path)
  10518. (desc-is-link (equal link desc))
  10519. (case-fold-search nil))
  10520. (cond
  10521. ((eq org-link-file-path-type 'absolute)
  10522. (setq path (abbreviate-file-name (expand-file-name path))))
  10523. ((eq org-link-file-path-type 'noabbrev)
  10524. (setq path (expand-file-name path)))
  10525. ((eq org-link-file-path-type 'relative)
  10526. (setq path (file-relative-name path)))
  10527. (t
  10528. (save-match-data
  10529. (if (string-match (concat "^" (regexp-quote
  10530. (file-name-as-directory
  10531. (expand-file-name "."))))
  10532. (expand-file-name path))
  10533. ;; We are linking a file with relative path name.
  10534. (setq path (substring (expand-file-name path)
  10535. (match-end 0)))))))
  10536. (setq link (concat "file:" path))
  10537. (if (equal desc origpath)
  10538. (setq desc path))))
  10539. (setq desc (read-string "Description: " desc))
  10540. (unless (string-match "\\S-" desc) (setq desc nil))
  10541. (if remove (apply 'delete-region remove))
  10542. (insert (org-make-link-string link desc))))
  10543. (defun org-completing-read (&rest args)
  10544. (let ((minibuffer-local-completion-map
  10545. (copy-keymap minibuffer-local-completion-map)))
  10546. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  10547. (apply 'completing-read args)))
  10548. ;;; Opening/following a link
  10549. (defvar org-link-search-failed nil)
  10550. (defun org-next-link ()
  10551. "Move forward to the next link.
  10552. If the link is in hidden text, expose it."
  10553. (interactive)
  10554. (when (and org-link-search-failed (eq this-command last-command))
  10555. (goto-char (point-min))
  10556. (message "Link search wrapped back to beginning of buffer"))
  10557. (setq org-link-search-failed nil)
  10558. (let* ((pos (point))
  10559. (ct (org-context))
  10560. (a (assoc :link ct)))
  10561. (if a (goto-char (nth 2 a)))
  10562. (if (re-search-forward org-any-link-re nil t)
  10563. (progn
  10564. (goto-char (match-beginning 0))
  10565. (if (org-invisible-p) (org-show-context)))
  10566. (goto-char pos)
  10567. (setq org-link-search-failed t)
  10568. (error "No further link found"))))
  10569. (defun org-previous-link ()
  10570. "Move backward to the previous link.
  10571. If the link is in hidden text, expose it."
  10572. (interactive)
  10573. (when (and org-link-search-failed (eq this-command last-command))
  10574. (goto-char (point-max))
  10575. (message "Link search wrapped back to end of buffer"))
  10576. (setq org-link-search-failed nil)
  10577. (let* ((pos (point))
  10578. (ct (org-context))
  10579. (a (assoc :link ct)))
  10580. (if a (goto-char (nth 1 a)))
  10581. (if (re-search-backward org-any-link-re nil t)
  10582. (progn
  10583. (goto-char (match-beginning 0))
  10584. (if (org-invisible-p) (org-show-context)))
  10585. (goto-char pos)
  10586. (setq org-link-search-failed t)
  10587. (error "No further link found"))))
  10588. (defun org-find-file-at-mouse (ev)
  10589. "Open file link or URL at mouse."
  10590. (interactive "e")
  10591. (mouse-set-point ev)
  10592. (org-open-at-point 'in-emacs))
  10593. (defun org-open-at-mouse (ev)
  10594. "Open file link or URL at mouse."
  10595. (interactive "e")
  10596. (mouse-set-point ev)
  10597. (org-open-at-point))
  10598. (defvar org-window-config-before-follow-link nil
  10599. "The window configuration before following a link.
  10600. This is saved in case the need arises to restore it.")
  10601. (defvar org-open-link-marker (make-marker)
  10602. "Marker pointing to the location where `org-open-at-point; was called.")
  10603. ;;;###autoload
  10604. (defun org-open-at-point-global ()
  10605. "Follow a link like Org-mode does.
  10606. This command can be called in any mode to follow a link that has
  10607. Org-mode syntax."
  10608. (interactive)
  10609. (org-run-like-in-org-mode 'org-open-at-point))
  10610. (defun org-open-at-point (&optional in-emacs)
  10611. "Open link at or after point.
  10612. If there is no link at point, this function will search forward up to
  10613. the end of the current subtree.
  10614. Normally, files will be opened by an appropriate application. If the
  10615. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  10616. (interactive "P")
  10617. (move-marker org-open-link-marker (point))
  10618. (setq org-window-config-before-follow-link (current-window-configuration))
  10619. (org-remove-occur-highlights nil nil t)
  10620. (if (org-at-timestamp-p t)
  10621. (org-follow-timestamp-link)
  10622. (let (type path link line search (pos (point)))
  10623. (catch 'match
  10624. (save-excursion
  10625. (skip-chars-forward "^]\n\r")
  10626. (when (org-in-regexp org-bracket-link-regexp)
  10627. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  10628. (while (string-match " *\n *" link)
  10629. (setq link (replace-match " " t t link)))
  10630. (setq link (org-link-expand-abbrev link))
  10631. (if (string-match org-link-re-with-space2 link)
  10632. (setq type (match-string 1 link) path (match-string 2 link))
  10633. (setq type "thisfile" path link))
  10634. (throw 'match t)))
  10635. (when (get-text-property (point) 'org-linked-text)
  10636. (setq type "thisfile"
  10637. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  10638. (1+ (point)) (point))
  10639. path (buffer-substring
  10640. (previous-single-property-change pos 'org-linked-text)
  10641. (next-single-property-change pos 'org-linked-text)))
  10642. (throw 'match t))
  10643. (save-excursion
  10644. (when (or (org-in-regexp org-angle-link-re)
  10645. (org-in-regexp org-plain-link-re))
  10646. (setq type (match-string 1) path (match-string 2))
  10647. (throw 'match t)))
  10648. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  10649. (setq type "tree-match"
  10650. path (match-string 1))
  10651. (throw 'match t))
  10652. (save-excursion
  10653. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  10654. (setq type "tags"
  10655. path (match-string 1))
  10656. (while (string-match ":" path)
  10657. (setq path (replace-match "+" t t path)))
  10658. (throw 'match t))))
  10659. (unless path
  10660. (error "No link found"))
  10661. ;; Remove any trailing spaces in path
  10662. (if (string-match " +\\'" path)
  10663. (setq path (replace-match "" t t path)))
  10664. (cond
  10665. ((assoc type org-link-protocols)
  10666. (funcall (nth 1 (assoc type org-link-protocols)) path))
  10667. ((equal type "mailto")
  10668. (let ((cmd (car org-link-mailto-program))
  10669. (args (cdr org-link-mailto-program)) args1
  10670. (address path) (subject "") a)
  10671. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  10672. (setq address (match-string 1 path)
  10673. subject (org-link-escape (match-string 2 path))))
  10674. (while args
  10675. (cond
  10676. ((not (stringp (car args))) (push (pop args) args1))
  10677. (t (setq a (pop args))
  10678. (if (string-match "%a" a)
  10679. (setq a (replace-match address t t a)))
  10680. (if (string-match "%s" a)
  10681. (setq a (replace-match subject t t a)))
  10682. (push a args1))))
  10683. (apply cmd (nreverse args1))))
  10684. ((member type '("http" "https" "ftp" "news"))
  10685. (browse-url (concat type ":" (org-link-escape
  10686. path org-link-escape-chars-browser))))
  10687. ((string= type "tags")
  10688. (org-tags-view in-emacs path))
  10689. ((string= type "thisfile")
  10690. (if in-emacs
  10691. (switch-to-buffer-other-window
  10692. (org-get-buffer-for-internal-link (current-buffer)))
  10693. (org-mark-ring-push))
  10694. (let ((cmd `(org-link-search
  10695. ,path
  10696. ,(cond ((equal in-emacs '(4)) 'occur)
  10697. ((equal in-emacs '(16)) 'org-occur)
  10698. (t nil))
  10699. ,pos)))
  10700. (condition-case nil (eval cmd)
  10701. (error (progn (widen) (eval cmd))))))
  10702. ((string= type "tree-match")
  10703. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  10704. ((string= type "file")
  10705. (if (string-match "::\\([0-9]+\\)\\'" path)
  10706. (setq line (string-to-number (match-string 1 path))
  10707. path (substring path 0 (match-beginning 0)))
  10708. (if (string-match "::\\(.+\\)\\'" path)
  10709. (setq search (match-string 1 path)
  10710. path (substring path 0 (match-beginning 0)))))
  10711. (org-open-file path in-emacs line search))
  10712. ((string= type "news")
  10713. (org-follow-gnus-link path))
  10714. ((string= type "bbdb")
  10715. (org-follow-bbdb-link path))
  10716. ((string= type "info")
  10717. (org-follow-info-link path))
  10718. ((string= type "gnus")
  10719. (let (group article)
  10720. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10721. (error "Error in Gnus link"))
  10722. (setq group (match-string 1 path)
  10723. article (match-string 3 path))
  10724. (org-follow-gnus-link group article)))
  10725. ((string= type "vm")
  10726. (let (folder article)
  10727. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10728. (error "Error in VM link"))
  10729. (setq folder (match-string 1 path)
  10730. article (match-string 3 path))
  10731. ;; in-emacs is the prefix arg, will be interpreted as read-only
  10732. (org-follow-vm-link folder article in-emacs)))
  10733. ((string= type "wl")
  10734. (let (folder article)
  10735. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10736. (error "Error in Wanderlust link"))
  10737. (setq folder (match-string 1 path)
  10738. article (match-string 3 path))
  10739. (org-follow-wl-link folder article)))
  10740. ((string= type "mhe")
  10741. (let (folder article)
  10742. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10743. (error "Error in MHE link"))
  10744. (setq folder (match-string 1 path)
  10745. article (match-string 3 path))
  10746. (org-follow-mhe-link folder article)))
  10747. ((string= type "rmail")
  10748. (let (folder article)
  10749. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  10750. (error "Error in RMAIL link"))
  10751. (setq folder (match-string 1 path)
  10752. article (match-string 3 path))
  10753. (org-follow-rmail-link folder article)))
  10754. ((string= type "shell")
  10755. (let ((cmd path))
  10756. ;; The following is only for backward compatibility
  10757. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  10758. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  10759. (if (or (not org-confirm-shell-link-function)
  10760. (funcall org-confirm-shell-link-function
  10761. (format "Execute \"%s\" in shell? "
  10762. (org-add-props cmd nil
  10763. 'face 'org-warning))))
  10764. (progn
  10765. (message "Executing %s" cmd)
  10766. (shell-command cmd))
  10767. (error "Abort"))))
  10768. ((string= type "elisp")
  10769. (let ((cmd path))
  10770. (if (or (not org-confirm-elisp-link-function)
  10771. (funcall org-confirm-elisp-link-function
  10772. (format "Execute \"%s\" as elisp? "
  10773. (org-add-props cmd nil
  10774. 'face 'org-warning))))
  10775. (message "%s => %s" cmd (eval (read cmd)))
  10776. (error "Abort"))))
  10777. (t
  10778. (browse-url-at-point)))))
  10779. (move-marker org-open-link-marker nil))
  10780. ;;; File search
  10781. (defvar org-create-file-search-functions nil
  10782. "List of functions to construct the right search string for a file link.
  10783. These functions are called in turn with point at the location to
  10784. which the link should point.
  10785. A function in the hook should first test if it would like to
  10786. handle this file type, for example by checking the major-mode or
  10787. the file extension. If it decides not to handle this file, it
  10788. should just return nil to give other functions a chance. If it
  10789. does handle the file, it must return the search string to be used
  10790. when following the link. The search string will be part of the
  10791. file link, given after a double colon, and `org-open-at-point'
  10792. will automatically search for it. If special measures must be
  10793. taken to make the search successful, another function should be
  10794. added to the companion hook `org-execute-file-search-functions',
  10795. which see.
  10796. A function in this hook may also use `setq' to set the variable
  10797. `description' to provide a suggestion for the descriptive text to
  10798. be used for this link when it gets inserted into an Org-mode
  10799. buffer with \\[org-insert-link].")
  10800. (defvar org-execute-file-search-functions nil
  10801. "List of functions to execute a file search triggered by a link.
  10802. Functions added to this hook must accept a single argument, the
  10803. search string that was part of the file link, the part after the
  10804. double colon. The function must first check if it would like to
  10805. handle this search, for example by checking the major-mode or the
  10806. file extension. If it decides not to handle this search, it
  10807. should just return nil to give other functions a chance. If it
  10808. does handle the search, it must return a non-nil value to keep
  10809. other functions from trying.
  10810. Each function can access the current prefix argument through the
  10811. variable `current-prefix-argument'. Note that a single prefix is
  10812. used to force opening a link in Emacs, so it may be good to only
  10813. use a numeric or double prefix to guide the search function.
  10814. In case this is needed, a function in this hook can also restore
  10815. the window configuration before `org-open-at-point' was called using:
  10816. (set-window-configuration org-window-config-before-follow-link)")
  10817. (defun org-link-search (s &optional type avoid-pos)
  10818. "Search for a link search option.
  10819. If S is surrounded by forward slashes, it is interpreted as a
  10820. regular expression. In org-mode files, this will create an `org-occur'
  10821. sparse tree. In ordinary files, `occur' will be used to list matches.
  10822. If the current buffer is in `dired-mode', grep will be used to search
  10823. in all files. If AVOID-POS is given, ignore matches near that position."
  10824. (let ((case-fold-search t)
  10825. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  10826. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  10827. (append '(("") (" ") ("\t") ("\n"))
  10828. org-emphasis-alist)
  10829. "\\|") "\\)"))
  10830. (pos (point))
  10831. (pre "") (post "")
  10832. words re0 re1 re2 re3 re4 re5 re2a reall)
  10833. (cond
  10834. ;; First check if there are any special
  10835. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  10836. ;; Now try the builtin stuff
  10837. ((save-excursion
  10838. (goto-char (point-min))
  10839. (and
  10840. (re-search-forward
  10841. (concat "<<" (regexp-quote s0) ">>") nil t)
  10842. (setq pos (match-beginning 0))))
  10843. ;; There is an exact target for this
  10844. (goto-char pos))
  10845. ((string-match "^/\\(.*\\)/$" s)
  10846. ;; A regular expression
  10847. (cond
  10848. ((org-mode-p)
  10849. (org-occur (match-string 1 s)))
  10850. ;;((eq major-mode 'dired-mode)
  10851. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  10852. (t (org-do-occur (match-string 1 s)))))
  10853. (t
  10854. ;; A normal search strings
  10855. (when (equal (string-to-char s) ?*)
  10856. ;; Anchor on headlines, post may include tags.
  10857. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10858. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  10859. s (substring s 1)))
  10860. (remove-text-properties
  10861. 0 (length s)
  10862. '(face nil mouse-face nil keymap nil fontified nil) s)
  10863. ;; Make a series of regular expressions to find a match
  10864. (setq words (org-split-string s "[ \n\r\t]+")
  10865. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10866. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10867. "\\)" markers)
  10868. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  10869. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10870. re1 (concat pre re2 post)
  10871. re3 (concat pre re4 post)
  10872. re5 (concat pre ".*" re4)
  10873. re2 (concat pre re2)
  10874. re2a (concat pre re2a)
  10875. re4 (concat pre re4)
  10876. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10877. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10878. re5 "\\)"
  10879. ))
  10880. (cond
  10881. ((eq type 'org-occur) (org-occur reall))
  10882. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10883. (t (goto-char (point-min))
  10884. (if (or (org-search-not-self 1 re0 nil t)
  10885. (org-search-not-self 1 re1 nil t)
  10886. (org-search-not-self 1 re2 nil t)
  10887. (org-search-not-self 1 re2a nil t)
  10888. (org-search-not-self 1 re3 nil t)
  10889. (org-search-not-self 1 re4 nil t)
  10890. (org-search-not-self 1 re5 nil t)
  10891. )
  10892. (goto-char (match-beginning 1))
  10893. (goto-char pos)
  10894. (error "No match")))))
  10895. (t
  10896. ;; Normal string-search
  10897. (goto-char (point-min))
  10898. (if (search-forward s nil t)
  10899. (goto-char (match-beginning 0))
  10900. (error "No match"))))
  10901. (and (org-mode-p) (org-show-context 'link-search))))
  10902. (defun org-search-not-self (group &rest args)
  10903. "Execute `re-search-forward', but only accept matches that do not
  10904. enclose the position of `org-open-link-marker'."
  10905. (let ((m org-open-link-marker))
  10906. (catch 'exit
  10907. (while (apply 're-search-forward args)
  10908. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10909. (goto-char (match-end group))
  10910. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10911. (> (match-beginning 0) (marker-position m))
  10912. (< (match-end 0) (marker-position m)))
  10913. (save-match-data
  10914. (or (not (org-in-regexp
  10915. org-bracket-link-analytic-regexp 1))
  10916. (not (match-end 4)) ; no description
  10917. (and (<= (match-beginning 4) (point))
  10918. (>= (match-end 4) (point))))))
  10919. (throw 'exit (point))))))))
  10920. (defun org-get-buffer-for-internal-link (buffer)
  10921. "Return a buffer to be used for displaying the link target of internal links."
  10922. (cond
  10923. ((not org-display-internal-link-with-indirect-buffer)
  10924. buffer)
  10925. ((string-match "(Clone)$" (buffer-name buffer))
  10926. (message "Buffer is already a clone, not making another one")
  10927. ;; we also do not modify visibility in this case
  10928. buffer)
  10929. (t ; make a new indirect buffer for displaying the link
  10930. (let* ((bn (buffer-name buffer))
  10931. (ibn (concat bn "(Clone)"))
  10932. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10933. (with-current-buffer ib (org-overview))
  10934. ib))))
  10935. (defun org-do-occur (regexp &optional cleanup)
  10936. "Call the Emacs command `occur'.
  10937. If CLEANUP is non-nil, remove the printout of the regular expression
  10938. in the *Occur* buffer. This is useful if the regex is long and not useful
  10939. to read."
  10940. (occur regexp)
  10941. (when cleanup
  10942. (let ((cwin (selected-window)) win beg end)
  10943. (when (setq win (get-buffer-window "*Occur*"))
  10944. (select-window win))
  10945. (goto-char (point-min))
  10946. (when (re-search-forward "match[a-z]+" nil t)
  10947. (setq beg (match-end 0))
  10948. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10949. (setq end (1- (match-beginning 0)))))
  10950. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10951. (goto-char (point-min))
  10952. (select-window cwin))))
  10953. ;;; The mark ring for links jumps
  10954. (defvar org-mark-ring nil
  10955. "Mark ring for positions before jumps in Org-mode.")
  10956. (defvar org-mark-ring-last-goto nil
  10957. "Last position in the mark ring used to go back.")
  10958. ;; Fill and close the ring
  10959. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10960. (loop for i from 1 to org-mark-ring-length do
  10961. (push (make-marker) org-mark-ring))
  10962. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10963. org-mark-ring)
  10964. (defun org-mark-ring-push (&optional pos buffer)
  10965. "Put the current position or POS into the mark ring and rotate it."
  10966. (interactive)
  10967. (setq pos (or pos (point)))
  10968. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10969. (move-marker (car org-mark-ring)
  10970. (or pos (point))
  10971. (or buffer (current-buffer)))
  10972. (message
  10973. (substitute-command-keys
  10974. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10975. (defun org-mark-ring-goto (&optional n)
  10976. "Jump to the previous position in the mark ring.
  10977. With prefix arg N, jump back that many stored positions. When
  10978. called several times in succession, walk through the entire ring.
  10979. Org-mode commands jumping to a different position in the current file,
  10980. or to another Org-mode file, automatically push the old position
  10981. onto the ring."
  10982. (interactive "p")
  10983. (let (p m)
  10984. (if (eq last-command this-command)
  10985. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10986. (setq p org-mark-ring))
  10987. (setq org-mark-ring-last-goto p)
  10988. (setq m (car p))
  10989. (switch-to-buffer (marker-buffer m))
  10990. (goto-char m)
  10991. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10992. (defun org-remove-angle-brackets (s)
  10993. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10994. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10995. s)
  10996. (defun org-add-angle-brackets (s)
  10997. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10998. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10999. s)
  11000. ;;; Following specific links
  11001. (defun org-follow-timestamp-link ()
  11002. (cond
  11003. ((org-at-date-range-p t)
  11004. (let ((org-agenda-start-on-weekday)
  11005. (t1 (match-string 1))
  11006. (t2 (match-string 2)))
  11007. (setq t1 (time-to-days (org-time-string-to-time t1))
  11008. t2 (time-to-days (org-time-string-to-time t2)))
  11009. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11010. ((org-at-timestamp-p t)
  11011. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11012. (substring (match-string 1) 0 10)))
  11013. 1))
  11014. (t (error "This should not happen"))))
  11015. (defun org-follow-bbdb-link (name)
  11016. "Follow a BBDB link to NAME."
  11017. (require 'bbdb)
  11018. (let ((inhibit-redisplay (not debug-on-error))
  11019. (bbdb-electric-p nil))
  11020. (catch 'exit
  11021. ;; Exact match on name
  11022. (bbdb-name (concat "\\`" name "\\'") nil)
  11023. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11024. ;; Exact match on name
  11025. (bbdb-company (concat "\\`" name "\\'") nil)
  11026. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11027. ;; Partial match on name
  11028. (bbdb-name name nil)
  11029. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11030. ;; Partial match on company
  11031. (bbdb-company name nil)
  11032. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11033. ;; General match including network address and notes
  11034. (bbdb name nil)
  11035. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11036. (delete-window (get-buffer-window "*BBDB*"))
  11037. (error "No matching BBDB record")))))
  11038. (defun org-follow-info-link (name)
  11039. "Follow an info file & node link to NAME."
  11040. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11041. (string-match "\\(.*\\)" name))
  11042. (progn
  11043. (require 'info)
  11044. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11045. (Info-find-node (match-string 1 name) (match-string 2 name))
  11046. (Info-find-node (match-string 1 name) "Top")))
  11047. (message (concat "Could not open: " name))))
  11048. (defun org-follow-gnus-link (&optional group article)
  11049. "Follow a Gnus link to GROUP and ARTICLE."
  11050. (require 'gnus)
  11051. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11052. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11053. (cond ((and group article)
  11054. (gnus-group-read-group 1 nil group)
  11055. (gnus-summary-goto-article (string-to-number article) nil t))
  11056. (group (gnus-group-jump-to-group group))))
  11057. (defun org-follow-vm-link (&optional folder article readonly)
  11058. "Follow a VM link to FOLDER and ARTICLE."
  11059. (require 'vm)
  11060. (setq article (org-add-angle-brackets article))
  11061. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11062. ;; ange-ftp or efs or tramp access
  11063. (let ((user (or (match-string 1 folder) (user-login-name)))
  11064. (host (match-string 2 folder))
  11065. (file (match-string 3 folder)))
  11066. (cond
  11067. ((featurep 'tramp)
  11068. ;; use tramp to access the file
  11069. (if (featurep 'xemacs)
  11070. (setq folder (format "[%s@%s]%s" user host file))
  11071. (setq folder (format "/%s@%s:%s" user host file))))
  11072. (t
  11073. ;; use ange-ftp or efs
  11074. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11075. (setq folder (format "/%s@%s:%s" user host file))))))
  11076. (when folder
  11077. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11078. (sit-for 0.1)
  11079. (when article
  11080. (vm-select-folder-buffer)
  11081. (widen)
  11082. (let ((case-fold-search t))
  11083. (goto-char (point-min))
  11084. (if (not (re-search-forward
  11085. (concat "^" "message-id: *" (regexp-quote article))))
  11086. (error "Could not find the specified message in this folder"))
  11087. (vm-isearch-update)
  11088. (vm-isearch-narrow)
  11089. (vm-beginning-of-message)
  11090. (vm-summarize)))))
  11091. (defun org-follow-wl-link (folder article)
  11092. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11093. (if (and (string= folder "%")
  11094. article
  11095. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11096. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11097. ;; Thus, we recompose folder and article ids.
  11098. (setq folder (format "%s#%s" folder (match-string 1 article))
  11099. article (match-string 3 article)))
  11100. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11101. (error "No such folder: %s" folder))
  11102. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11103. (and article
  11104. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11105. (wl-summary-redisplay)))
  11106. (defun org-follow-rmail-link (folder article)
  11107. "Follow an RMAIL link to FOLDER and ARTICLE."
  11108. (setq article (org-add-angle-brackets article))
  11109. (let (message-number)
  11110. (save-excursion
  11111. (save-window-excursion
  11112. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11113. (setq message-number
  11114. (save-restriction
  11115. (widen)
  11116. (goto-char (point-max))
  11117. (if (re-search-backward
  11118. (concat "^Message-ID:\\s-+" (regexp-quote
  11119. (or article "")))
  11120. nil t)
  11121. (rmail-what-message))))))
  11122. (if message-number
  11123. (progn
  11124. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11125. (rmail-show-message message-number)
  11126. message-number)
  11127. (error "Message not found"))))
  11128. ;;; mh-e integration based on planner-mode
  11129. (defun org-mhe-get-message-real-folder ()
  11130. "Return the name of the current message real folder, so if you use
  11131. sequences, it will now work."
  11132. (save-excursion
  11133. (let* ((folder
  11134. (if (equal major-mode 'mh-folder-mode)
  11135. mh-current-folder
  11136. ;; Refer to the show buffer
  11137. mh-show-folder-buffer))
  11138. (end-index
  11139. (if (boundp 'mh-index-folder)
  11140. (min (length mh-index-folder) (length folder))))
  11141. )
  11142. ;; a simple test on mh-index-data does not work, because
  11143. ;; mh-index-data is always nil in a show buffer.
  11144. (if (and (boundp 'mh-index-folder)
  11145. (string= mh-index-folder (substring folder 0 end-index)))
  11146. (if (equal major-mode 'mh-show-mode)
  11147. (save-window-excursion
  11148. (let (pop-up-frames)
  11149. (when (buffer-live-p (get-buffer folder))
  11150. (progn
  11151. (pop-to-buffer folder)
  11152. (org-mhe-get-message-folder-from-index)
  11153. )
  11154. )))
  11155. (org-mhe-get-message-folder-from-index)
  11156. )
  11157. folder
  11158. )
  11159. )))
  11160. (defun org-mhe-get-message-folder-from-index ()
  11161. "Returns the name of the message folder in a index folder buffer."
  11162. (save-excursion
  11163. (mh-index-previous-folder)
  11164. (re-search-forward "^\\(+.*\\)$" nil t)
  11165. (message (match-string 1))))
  11166. (defun org-mhe-get-message-folder ()
  11167. "Return the name of the current message folder. Be careful if you
  11168. use sequences."
  11169. (save-excursion
  11170. (if (equal major-mode 'mh-folder-mode)
  11171. mh-current-folder
  11172. ;; Refer to the show buffer
  11173. mh-show-folder-buffer)))
  11174. (defun org-mhe-get-message-num ()
  11175. "Return the number of the current message. Be careful if you
  11176. use sequences."
  11177. (save-excursion
  11178. (if (equal major-mode 'mh-folder-mode)
  11179. (mh-get-msg-num nil)
  11180. ;; Refer to the show buffer
  11181. (mh-show-buffer-message-number))))
  11182. (defun org-mhe-get-header (header)
  11183. "Return a header of the message in folder mode. This will create a
  11184. show buffer for the corresponding message. If you have a more clever
  11185. idea..."
  11186. (let* ((folder (org-mhe-get-message-folder))
  11187. (num (org-mhe-get-message-num))
  11188. (buffer (get-buffer-create (concat "show-" folder)))
  11189. (header-field))
  11190. (with-current-buffer buffer
  11191. (mh-display-msg num folder)
  11192. (if (equal major-mode 'mh-folder-mode)
  11193. (mh-header-display)
  11194. (mh-show-header-display))
  11195. (set-buffer buffer)
  11196. (setq header-field (mh-get-header-field header))
  11197. (if (equal major-mode 'mh-folder-mode)
  11198. (mh-show)
  11199. (mh-show-show))
  11200. header-field)))
  11201. (defun org-follow-mhe-link (folder article)
  11202. "Follow an MHE link to FOLDER and ARTICLE.
  11203. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11204. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11205. ARTICLE is searched in all folders. Indexed searches (swish++,
  11206. namazu, and others supported by MH-E) will always search in all
  11207. folders."
  11208. (require 'mh-e)
  11209. (require 'mh-search)
  11210. (require 'mh-utils)
  11211. (mh-find-path)
  11212. (if (not article)
  11213. (mh-visit-folder (mh-normalize-folder-name folder))
  11214. (setq article (org-add-angle-brackets article))
  11215. (mh-search-choose)
  11216. (if (equal mh-searcher 'pick)
  11217. (progn
  11218. (mh-search folder (list "--message-id" article))
  11219. (when (and org-mhe-search-all-folders
  11220. (not (org-mhe-get-message-real-folder)))
  11221. (kill-this-buffer)
  11222. (mh-search "+" (list "--message-id" article))))
  11223. (mh-search "+" article))
  11224. (if (org-mhe-get-message-real-folder)
  11225. (mh-show-msg 1)
  11226. (kill-this-buffer)
  11227. (error "Message not found"))))
  11228. ;;; BibTeX links
  11229. ;; Use the custom search meachnism to construct and use search strings for
  11230. ;; file links to BibTeX database entries.
  11231. (defun org-create-file-search-in-bibtex ()
  11232. "Create the search string and description for a BibTeX database entry."
  11233. (when (eq major-mode 'bibtex-mode)
  11234. ;; yes, we want to construct this search string.
  11235. ;; Make a good description for this entry, using names, year and the title
  11236. ;; Put it into the `description' variable which is dynamically scoped.
  11237. (let ((bibtex-autokey-names 1)
  11238. (bibtex-autokey-names-stretch 1)
  11239. (bibtex-autokey-name-case-convert-function 'identity)
  11240. (bibtex-autokey-name-separator " & ")
  11241. (bibtex-autokey-additional-names " et al.")
  11242. (bibtex-autokey-year-length 4)
  11243. (bibtex-autokey-name-year-separator " ")
  11244. (bibtex-autokey-titlewords 3)
  11245. (bibtex-autokey-titleword-separator " ")
  11246. (bibtex-autokey-titleword-case-convert-function 'identity)
  11247. (bibtex-autokey-titleword-length 'infty)
  11248. (bibtex-autokey-year-title-separator ": "))
  11249. (setq description (bibtex-generate-autokey)))
  11250. ;; Now parse the entry, get the key and return it.
  11251. (save-excursion
  11252. (bibtex-beginning-of-entry)
  11253. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11254. (defun org-execute-file-search-in-bibtex (s)
  11255. "Find the link search string S as a key for a database entry."
  11256. (when (eq major-mode 'bibtex-mode)
  11257. ;; Yes, we want to do the search in this file.
  11258. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11259. (goto-char (point-min))
  11260. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11261. (regexp-quote s) "[ \t\n]*,") nil t)
  11262. (goto-char (match-beginning 0)))
  11263. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11264. ;; Use double prefix to indicate that any web link should be browsed
  11265. (let ((b (current-buffer)) (p (point)))
  11266. ;; Restore the window configuration because we just use the web link
  11267. (set-window-configuration org-window-config-before-follow-link)
  11268. (save-excursion (set-buffer b) (goto-char p)
  11269. (bibtex-url)))
  11270. (recenter 0)) ; Move entry start to beginning of window
  11271. ;; return t to indicate that the search is done.
  11272. t))
  11273. ;; Finally add the functions to the right hooks.
  11274. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11275. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11276. ;; end of Bibtex link setup
  11277. ;;; Following file links
  11278. (defun org-open-file (path &optional in-emacs line search)
  11279. "Open the file at PATH.
  11280. First, this expands any special file name abbreviations. Then the
  11281. configuration variable `org-file-apps' is checked if it contains an
  11282. entry for this file type, and if yes, the corresponding command is launched.
  11283. If no application is found, Emacs simply visits the file.
  11284. With optional argument IN-EMACS, Emacs will visit the file.
  11285. Optional LINE specifies a line to go to, optional SEARCH a string to
  11286. search for. If LINE or SEARCH is given, the file will always be
  11287. opened in Emacs.
  11288. If the file does not exist, an error is thrown."
  11289. (setq in-emacs (or in-emacs line search))
  11290. (let* ((file (if (equal path "")
  11291. buffer-file-name
  11292. (substitute-in-file-name (expand-file-name path))))
  11293. (apps (append org-file-apps (org-default-apps)))
  11294. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11295. (dirp (if remp nil (file-directory-p file)))
  11296. (dfile (downcase file))
  11297. (old-buffer (current-buffer))
  11298. (old-pos (point))
  11299. (old-mode major-mode)
  11300. ext cmd)
  11301. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11302. (setq ext (match-string 1 dfile))
  11303. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11304. (setq ext (match-string 1 dfile))))
  11305. (if in-emacs
  11306. (setq cmd 'emacs)
  11307. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11308. (and dirp (cdr (assoc 'directory apps)))
  11309. (cdr (assoc ext apps))
  11310. (cdr (assoc t apps)))))
  11311. (when (eq cmd 'mailcap)
  11312. (require 'mailcap)
  11313. (mailcap-parse-mailcaps)
  11314. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11315. (command (mailcap-mime-info mime-type)))
  11316. (if (stringp command)
  11317. (setq cmd command)
  11318. (setq cmd 'emacs))))
  11319. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11320. (not (file-exists-p file))
  11321. (not org-open-non-existing-files))
  11322. (error "No such file: %s" file))
  11323. (cond
  11324. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  11325. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  11326. (if (string-match "['\"]%s['\"]" cmd)
  11327. (setq cmd (replace-match "%s" t t cmd)))
  11328. (setq cmd (format cmd (shell-quote-argument file)))
  11329. (save-window-excursion
  11330. (start-process-shell-command cmd nil cmd)))
  11331. ((or (stringp cmd)
  11332. (eq cmd 'emacs))
  11333. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  11334. (widen)
  11335. (if line (goto-line line)
  11336. (if search (org-link-search search))))
  11337. ((consp cmd)
  11338. (eval cmd))
  11339. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  11340. (and (org-mode-p) (eq old-mode 'org-mode)
  11341. (or (not (equal old-buffer (current-buffer)))
  11342. (not (equal old-pos (point))))
  11343. (org-mark-ring-push old-pos old-buffer))))
  11344. (defun org-default-apps ()
  11345. "Return the default applications for this operating system."
  11346. (cond
  11347. ((eq system-type 'darwin)
  11348. org-file-apps-defaults-macosx)
  11349. ((eq system-type 'windows-nt)
  11350. org-file-apps-defaults-windowsnt)
  11351. (t org-file-apps-defaults-gnu)))
  11352. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  11353. (defun org-file-remote-p (file)
  11354. "Test whether FILE specifies a location on a remote system.
  11355. Return non-nil if the location is indeed remote.
  11356. For example, the filename \"/user@host:/foo\" specifies a location
  11357. on the system \"/user@host:\"."
  11358. (cond ((fboundp 'file-remote-p)
  11359. (file-remote-p file))
  11360. ((fboundp 'tramp-handle-file-remote-p)
  11361. (tramp-handle-file-remote-p file))
  11362. ((and (boundp 'ange-ftp-name-format)
  11363. (string-match (car ange-ftp-name-format) file))
  11364. t)
  11365. (t nil)))
  11366. ;;;; Hooks for remember.el
  11367. ;;;###autoload
  11368. (defun org-remember-annotation ()
  11369. "Return a link to the current location as an annotation for remember.el.
  11370. If you are using Org-mode files as target for data storage with
  11371. remember.el, then the annotations should include a link compatible with the
  11372. conventions in Org-mode. This function returns such a link."
  11373. (org-store-link nil))
  11374. (defconst org-remember-help
  11375. "Select a destination location for the note.
  11376. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  11377. RET on headline -> Store as sublevel entry to current headline
  11378. RET at beg-of-buf -> Append to file as level 2 headline
  11379. <left>/<right> -> before/after current headline, same headings level")
  11380. (defvar org-remember-previous-location nil)
  11381. (defvar org-force-remember-template-char) ;; dynamically scoped
  11382. ;;;###autoload
  11383. (defun org-remember-apply-template (&optional use-char skip-interactive)
  11384. "Initialize *remember* buffer with template, invoke `org-mode'.
  11385. This function should be placed into `remember-mode-hook' and in fact requires
  11386. to be run from that hook to fucntion properly."
  11387. (if org-remember-templates
  11388. (let* ((templates (mapcar (lambda (x)
  11389. (if (stringp (car x))
  11390. (append (list (nth 1 x) (car x)) (cddr x))
  11391. (append (list (car x) "") (cdr x))))
  11392. org-remember-templates))
  11393. (char (or use-char
  11394. (cond
  11395. ((= (length templates) 1)
  11396. (caar templates))
  11397. ((and (boundp 'org-force-remember-template-char)
  11398. org-force-remember-template-char)
  11399. (if (stringp org-force-remember-template-char)
  11400. (string-to-char org-force-remember-template-char)
  11401. org-force-remember-template-char))
  11402. (t
  11403. (message "Select template: %s"
  11404. (mapconcat
  11405. (lambda (x)
  11406. (cond
  11407. ((not (string-match "\\S-" (nth 1 x)))
  11408. (format "[%c]" (car x)))
  11409. ((equal (downcase (car x))
  11410. (downcase (aref (nth 1 x) 0)))
  11411. (format "[%c]%s" (car x) (substring (nth 1 x) 1)))
  11412. (t (format "[%c]%s" (car x) (nth 1 x)))))
  11413. templates " "))
  11414. (read-char-exclusive)))))
  11415. (entry (cddr (assoc char templates)))
  11416. (tpl (car entry))
  11417. (plist-p (if org-store-link-plist t nil))
  11418. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  11419. (string-match "\\S-" (nth 1 entry)))
  11420. (nth 1 entry)
  11421. org-default-notes-file))
  11422. (headline (nth 2 entry))
  11423. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  11424. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  11425. (v-u (concat "[" (substring v-t 1 -1) "]"))
  11426. (v-U (concat "[" (substring v-T 1 -1) "]"))
  11427. ;; `initial' and `annotation' are bound in `remember'
  11428. (v-i (if (boundp 'initial) initial))
  11429. (v-a (if (and (boundp 'annotation) annotation)
  11430. (if (equal annotation "[[]]") "" annotation)
  11431. ""))
  11432. (v-A (if (and v-a
  11433. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  11434. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  11435. v-a))
  11436. (v-n user-full-name)
  11437. (org-startup-folded nil)
  11438. org-time-was-given org-end-time-was-given x prompt char time)
  11439. (setq org-store-link-plist
  11440. (append (list :annotation v-a :initial v-i)
  11441. org-store-link-plist))
  11442. (unless tpl (setq tpl "") (message "No template") (ding))
  11443. (erase-buffer)
  11444. (insert (substitute-command-keys
  11445. (format
  11446. "## Filing location: Select interactively, default, or last used:
  11447. ## %s to select file and header location interactively.
  11448. ## %s \"%s\" -> \"* %s\"
  11449. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  11450. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  11451. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  11452. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  11453. (abbreviate-file-name (or file org-default-notes-file))
  11454. (or headline "")
  11455. (or (car org-remember-previous-location) "???")
  11456. (or (cdr org-remember-previous-location) "???"))))
  11457. (insert tpl) (goto-char (point-min))
  11458. ;; Simple %-escapes
  11459. (while (re-search-forward "%\\([tTuUaiA]\\)" nil t)
  11460. (when (and initial (equal (match-string 0) "%i"))
  11461. (save-match-data
  11462. (let* ((lead (buffer-substring
  11463. (point-at-bol) (match-beginning 0))))
  11464. (setq v-i (mapconcat 'identity
  11465. (org-split-string initial "\n")
  11466. (concat "\n" lead))))))
  11467. (replace-match
  11468. (or (eval (intern (concat "v-" (match-string 1)))) "")
  11469. t t))
  11470. ;; From the property list
  11471. (when plist-p
  11472. (goto-char (point-min))
  11473. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  11474. (and (setq x (plist-get org-store-link-plist
  11475. (intern (match-string 1))))
  11476. (replace-match x t t))))
  11477. ;; Turn on org-mode in the remember buffer, set local variables
  11478. (org-mode)
  11479. (org-set-local 'org-finish-function 'remember-buffer)
  11480. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  11481. (org-set-local 'org-default-notes-file file))
  11482. (if (and headline (stringp headline) (string-match "\\S-" headline))
  11483. (org-set-local 'org-remember-default-headline headline))
  11484. ;; Interactive template entries
  11485. (goto-char (point-min))
  11486. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  11487. (setq char (if (match-end 3) (match-string 3))
  11488. prompt (if (match-end 2) (match-string 2)))
  11489. (goto-char (match-beginning 0))
  11490. (replace-match "")
  11491. (cond
  11492. ((member char '("G" "g"))
  11493. (let* ((org-last-tags-completion-table
  11494. (org-global-tags-completion-table
  11495. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  11496. (org-add-colon-after-tag-completion t)
  11497. (ins (completing-read
  11498. (if prompt (concat prompt ": ") "Tags: ")
  11499. 'org-tags-completion-function nil nil nil
  11500. 'org-tags-history)))
  11501. (setq ins (mapconcat 'identity
  11502. (org-split-string ins (org-re "[^[:alnum:]]+"))
  11503. ":"))
  11504. (when (string-match "\\S-" ins)
  11505. (or (equal (char-before) ?:) (insert ":"))
  11506. (insert ins)
  11507. (or (equal (char-after) ?:) (insert ":")))))
  11508. (char
  11509. (setq org-time-was-given (equal (upcase char) char))
  11510. (setq time (org-read-date (equal (upcase char) "U") t nil
  11511. prompt))
  11512. (org-insert-time-stamp time org-time-was-given
  11513. (member char '("u" "U"))
  11514. nil nil (list org-end-time-was-given)))
  11515. (t
  11516. (insert (read-string
  11517. (if prompt (concat prompt ": ") "Enter string"))))))
  11518. (goto-char (point-min))
  11519. (if (re-search-forward "%\\?" nil t)
  11520. (replace-match "")
  11521. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  11522. (org-mode)
  11523. (org-set-local 'org-finish-function 'remember-buffer)))
  11524. ;;;###autoload
  11525. (defun org-remember (&optional org-force-remember-template-char)
  11526. "Call `remember'. If this is already a remember buffer, re-apply template.
  11527. If there is an active region, make sure remember uses it as initial content
  11528. of the remember buffer."
  11529. (interactive)
  11530. (if (eq org-finish-function 'remember-buffer)
  11531. (progn
  11532. (when (< (length org-remember-templates) 2)
  11533. (error "No other template available"))
  11534. (erase-buffer)
  11535. (let ((annotation (plist-get org-store-link-plist :annotation))
  11536. (initial (plist-get org-store-link-plist :initial)))
  11537. (org-remember-apply-template))
  11538. (message "Press C-c C-c to remember data"))
  11539. (if (org-region-active-p)
  11540. (remember (buffer-substring (point) (mark)))
  11541. (call-interactively 'remember))))
  11542. (defvar org-note-abort nil) ; dynamically scoped
  11543. ;;;###autoload
  11544. (defun org-remember-handler ()
  11545. "Store stuff from remember.el into an org file.
  11546. First prompts for an org file. If the user just presses return, the value
  11547. of `org-default-notes-file' is used.
  11548. Then the command offers the headings tree of the selected file in order to
  11549. file the text at a specific location.
  11550. You can either immediately press RET to get the note appended to the
  11551. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  11552. find a better place. Then press RET or <left> or <right> in insert the note.
  11553. Key Cursor position Note gets inserted
  11554. -----------------------------------------------------------------------------
  11555. RET buffer-start as level 1 heading at end of file
  11556. RET on headline as sublevel of the heading at cursor
  11557. RET no heading at cursor position, level taken from context.
  11558. Or use prefix arg to specify level manually.
  11559. <left> on headline as same level, before current heading
  11560. <right> on headline as same level, after current heading
  11561. So the fastest way to store the note is to press RET RET to append it to
  11562. the default file. This way your current train of thought is not
  11563. interrupted, in accordance with the principles of remember.el.
  11564. You can also get the fast execution without prompting by using
  11565. C-u C-c C-c to exit the remember buffer. See also the variable
  11566. `org-remember-store-without-prompt'.
  11567. Before being stored away, the function ensures that the text has a
  11568. headline, i.e. a first line that starts with a \"*\". If not, a headline
  11569. is constructed from the current date and some additional data.
  11570. If the variable `org-adapt-indentation' is non-nil, the entire text is
  11571. also indented so that it starts in the same column as the headline
  11572. \(i.e. after the stars).
  11573. See also the variable `org-reverse-note-order'."
  11574. (goto-char (point-min))
  11575. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  11576. (replace-match ""))
  11577. (goto-char (point-max))
  11578. (unless (equal (char-before) ?\n) (insert "\n"))
  11579. (catch 'quit
  11580. (if org-note-abort (throw 'quit nil))
  11581. (let* ((txt (buffer-substring (point-min) (point-max)))
  11582. (fastp (org-xor (equal current-prefix-arg '(4))
  11583. org-remember-store-without-prompt))
  11584. (file (if fastp org-default-notes-file (org-get-org-file)))
  11585. (heading org-remember-default-headline)
  11586. (visiting (org-find-base-buffer-visiting file))
  11587. (org-startup-folded nil)
  11588. (org-startup-align-all-tables nil)
  11589. (org-goto-start-pos 1)
  11590. spos exitcmd level indent reversed)
  11591. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  11592. (setq file (car org-remember-previous-location)
  11593. heading (cdr org-remember-previous-location)))
  11594. (setq current-prefix-arg nil)
  11595. ;; Modify text so that it becomes a nice subtree which can be inserted
  11596. ;; into an org tree.
  11597. (let* ((lines (split-string txt "\n"))
  11598. first)
  11599. (setq first (car lines) lines (cdr lines))
  11600. (if (string-match "^\\*+ " first)
  11601. ;; Is already a headline
  11602. (setq indent nil)
  11603. ;; We need to add a headline: Use time and first buffer line
  11604. (setq lines (cons first lines)
  11605. first (concat "* " (current-time-string)
  11606. " (" (remember-buffer-desc) ")")
  11607. indent " "))
  11608. (if (and org-adapt-indentation indent)
  11609. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  11610. (setq txt (concat first "\n"
  11611. (mapconcat 'identity lines "\n"))))
  11612. ;; Find the file
  11613. (if (not visiting) (find-file-noselect file))
  11614. (with-current-buffer (or visiting (get-file-buffer file))
  11615. (unless (org-mode-p)
  11616. (error "Target files for remember notes must be in Org-mode"))
  11617. (save-excursion
  11618. (save-restriction
  11619. (widen)
  11620. (and (goto-char (point-min))
  11621. (not (re-search-forward "^\\* " nil t))
  11622. (insert "\n* " (or heading "Notes") "\n"))
  11623. (setq reversed (org-notes-order-reversed-p))
  11624. ;; Find the default location
  11625. (when (and heading (stringp heading) (string-match "\\S-" heading))
  11626. (goto-char (point-min))
  11627. (if (re-search-forward
  11628. (concat "^\\*+[ \t]+" (regexp-quote heading)
  11629. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  11630. nil t)
  11631. (setq org-goto-start-pos (match-beginning 0))
  11632. (when fastp
  11633. (goto-char (point-max))
  11634. (unless (bolp) (newline))
  11635. (insert "* " heading "\n")
  11636. (setq org-goto-start-pos (point-at-bol 0)))))
  11637. ;; Ask the User for a location
  11638. (if fastp
  11639. (setq spos org-goto-start-pos
  11640. exitcmd 'return)
  11641. (setq spos (org-get-location (current-buffer) org-remember-help)
  11642. exitcmd (cdr spos)
  11643. spos (car spos)))
  11644. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  11645. ; not handle this note
  11646. (goto-char spos)
  11647. (cond ((org-on-heading-p t)
  11648. (org-back-to-heading t)
  11649. (setq level (funcall outline-level))
  11650. (cond
  11651. ((eq exitcmd 'return)
  11652. ;; sublevel of current
  11653. (setq org-remember-previous-location
  11654. (cons (abbreviate-file-name file)
  11655. (org-get-heading 'notags)))
  11656. (if reversed
  11657. (outline-next-heading)
  11658. (org-end-of-subtree)
  11659. (if (not (bolp))
  11660. (if (looking-at "[ \t]*\n")
  11661. (beginning-of-line 2)
  11662. (end-of-line 1)
  11663. (insert "\n"))))
  11664. (org-paste-subtree (org-get-legal-level level 1) txt))
  11665. ((eq exitcmd 'left)
  11666. ;; before current
  11667. (org-paste-subtree level txt))
  11668. ((eq exitcmd 'right)
  11669. ;; after current
  11670. (org-end-of-subtree t)
  11671. (org-paste-subtree level txt))
  11672. (t (error "This should not happen"))))
  11673. ((and (bobp) (not reversed))
  11674. ;; Put it at the end, one level below level 1
  11675. (save-restriction
  11676. (widen)
  11677. (goto-char (point-max))
  11678. (if (not (bolp)) (newline))
  11679. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  11680. ((and (bobp) reversed)
  11681. ;; Put it at the start, as level 1
  11682. (save-restriction
  11683. (widen)
  11684. (goto-char (point-min))
  11685. (re-search-forward "^\\*+ " nil t)
  11686. (beginning-of-line 1)
  11687. (org-paste-subtree 1 txt)))
  11688. (t
  11689. ;; Put it right there, with automatic level determined by
  11690. ;; org-paste-subtree or from prefix arg
  11691. (org-paste-subtree
  11692. (if (numberp current-prefix-arg) current-prefix-arg)
  11693. txt)))
  11694. (when remember-save-after-remembering
  11695. (save-buffer)
  11696. (if (not visiting) (kill-buffer (current-buffer)))))))))
  11697. t) ;; return t to indicate that we took care of this note.
  11698. (defun org-get-org-file ()
  11699. "Read a filename, with default directory `org-directory'."
  11700. (let ((default (or org-default-notes-file remember-data-file)))
  11701. (read-file-name (format "File name [%s]: " default)
  11702. (file-name-as-directory org-directory)
  11703. default)))
  11704. (defun org-notes-order-reversed-p ()
  11705. "Check if the current file should receive notes in reversed order."
  11706. (cond
  11707. ((not org-reverse-note-order) nil)
  11708. ((eq t org-reverse-note-order) t)
  11709. ((not (listp org-reverse-note-order)) nil)
  11710. (t (catch 'exit
  11711. (let ((all org-reverse-note-order)
  11712. entry)
  11713. (while (setq entry (pop all))
  11714. (if (string-match (car entry) buffer-file-name)
  11715. (throw 'exit (cdr entry))))
  11716. nil)))))
  11717. ;;;; Dynamic blocks
  11718. (defun org-find-dblock (name)
  11719. "Find the first dynamic block with name NAME in the buffer.
  11720. If not found, stay at current position and return nil."
  11721. (let (pos)
  11722. (save-excursion
  11723. (goto-char (point-min))
  11724. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  11725. nil t)
  11726. (match-beginning 0))))
  11727. (if pos (goto-char pos))
  11728. pos))
  11729. (defconst org-dblock-start-re
  11730. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  11731. "Matches the startline of a dynamic block, with parameters.")
  11732. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  11733. "Matches the end of a dyhamic block.")
  11734. (defun org-create-dblock (plist)
  11735. "Create a dynamic block section, with parameters taken from PLIST.
  11736. PLIST must containe a :name entry which is used as name of the block."
  11737. (unless (bolp) (newline))
  11738. (let ((name (plist-get plist :name)))
  11739. (insert "#+BEGIN: " name)
  11740. (while plist
  11741. (if (eq (car plist) :name)
  11742. (setq plist (cddr plist))
  11743. (insert " " (prin1-to-string (pop plist)))))
  11744. (insert "\n\n#+END:\n")
  11745. (beginning-of-line -2)))
  11746. (defun org-prepare-dblock ()
  11747. "Prepare dynamic block for refresh.
  11748. This empties the block, puts the cursor at the insert position and returns
  11749. the property list including an extra property :name with the block name."
  11750. (unless (looking-at org-dblock-start-re)
  11751. (error "Not at a dynamic block"))
  11752. (let* ((begdel (1+ (match-end 0)))
  11753. (name (org-no-properties (match-string 1)))
  11754. (params (append (list :name name)
  11755. (read (concat "(" (match-string 3) ")")))))
  11756. (unless (re-search-forward org-dblock-end-re nil t)
  11757. (error "Dynamic block not terminated"))
  11758. (delete-region begdel (match-beginning 0))
  11759. (goto-char begdel)
  11760. (open-line 1)
  11761. params))
  11762. (defun org-map-dblocks (&optional command)
  11763. "Apply COMMAND to all dynamic blocks in the current buffer.
  11764. If COMMAND is not given, use `org-update-dblock'."
  11765. (let ((cmd (or command 'org-update-dblock))
  11766. pos)
  11767. (save-excursion
  11768. (goto-char (point-min))
  11769. (while (re-search-forward org-dblock-start-re nil t)
  11770. (goto-char (setq pos (match-beginning 0)))
  11771. (condition-case nil
  11772. (funcall cmd)
  11773. (error (message "Error during update of dynamic block")))
  11774. (goto-char pos)
  11775. (unless (re-search-forward org-dblock-end-re nil t)
  11776. (error "Dynamic block not terminated"))))))
  11777. (defun org-dblock-update (&optional arg)
  11778. "User command for updating dynamic blocks.
  11779. Update the dynamic block at point. With prefix ARG, update all dynamic
  11780. blocks in the buffer."
  11781. (interactive "P")
  11782. (if arg
  11783. (org-update-all-dblocks)
  11784. (or (looking-at org-dblock-start-re)
  11785. (org-beginning-of-dblock))
  11786. (org-update-dblock)))
  11787. (defun org-update-dblock ()
  11788. "Update the dynamic block at point
  11789. This means to empty the block, parse for parameters and then call
  11790. the correct writing function."
  11791. (save-window-excursion
  11792. (let* ((pos (point))
  11793. (line (org-current-line))
  11794. (params (org-prepare-dblock))
  11795. (name (plist-get params :name))
  11796. (cmd (intern (concat "org-dblock-write:" name))))
  11797. (message "Updating dynamic block `%s' at line %d..." name line)
  11798. (funcall cmd params)
  11799. (message "Updating dynamic block `%s' at line %d...done" name line)
  11800. (goto-char pos))))
  11801. (defun org-beginning-of-dblock ()
  11802. "Find the beginning of the dynamic block at point.
  11803. Error if there is no scuh block at point."
  11804. (let ((pos (point))
  11805. beg)
  11806. (end-of-line 1)
  11807. (if (and (re-search-backward org-dblock-start-re nil t)
  11808. (setq beg (match-beginning 0))
  11809. (re-search-forward org-dblock-end-re nil t)
  11810. (> (match-end 0) pos))
  11811. (goto-char beg)
  11812. (goto-char pos)
  11813. (error "Not in a dynamic block"))))
  11814. (defun org-update-all-dblocks ()
  11815. "Update all dynamic blocks in the buffer.
  11816. This function can be used in a hook."
  11817. (when (org-mode-p)
  11818. (org-map-dblocks 'org-update-dblock)))
  11819. ;;;; Completion
  11820. (defconst org-additional-option-like-keywords
  11821. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  11822. "ORGTBL" "HTML:" "LaTeX:"))
  11823. (defun org-complete (&optional arg)
  11824. "Perform completion on word at point.
  11825. At the beginning of a headline, this completes TODO keywords as given in
  11826. `org-todo-keywords'.
  11827. If the current word is preceded by a backslash, completes the TeX symbols
  11828. that are supported for HTML support.
  11829. If the current word is preceded by \"#+\", completes special words for
  11830. setting file options.
  11831. In the line after \"#+STARTUP:, complete valid keywords.\"
  11832. At all other locations, this simply calls the value of
  11833. `org-completion-fallback-command'."
  11834. (interactive "P")
  11835. (org-without-partial-completion
  11836. (catch 'exit
  11837. (let* ((end (point))
  11838. (beg1 (save-excursion
  11839. (skip-chars-backward (org-re "[:alnum:]_@"))
  11840. (point)))
  11841. (beg (save-excursion
  11842. (skip-chars-backward "a-zA-Z0-9_:$")
  11843. (point)))
  11844. (confirm (lambda (x) (stringp (car x))))
  11845. (searchhead (equal (char-before beg) ?*))
  11846. (tag (and (equal (char-before beg1) ?:)
  11847. (equal (char-after (point-at-bol)) ?*)))
  11848. (prop (and (equal (char-before beg1) ?:)
  11849. (not (equal (char-after (point-at-bol)) ?*))))
  11850. (texp (equal (char-before beg) ?\\))
  11851. (link (equal (char-before beg) ?\[))
  11852. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  11853. beg)
  11854. "#+"))
  11855. (startup (string-match "^#\\+STARTUP:.*"
  11856. (buffer-substring (point-at-bol) (point))))
  11857. (completion-ignore-case opt)
  11858. (type nil)
  11859. (tbl nil)
  11860. (table (cond
  11861. (opt
  11862. (setq type :opt)
  11863. (append
  11864. (mapcar
  11865. (lambda (x)
  11866. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  11867. (cons (match-string 2 x) (match-string 1 x)))
  11868. (org-split-string (org-get-current-options) "\n"))
  11869. (mapcar 'list org-additional-option-like-keywords)))
  11870. (startup
  11871. (setq type :startup)
  11872. org-startup-options)
  11873. (link (append org-link-abbrev-alist-local
  11874. org-link-abbrev-alist))
  11875. (texp
  11876. (setq type :tex)
  11877. org-html-entities)
  11878. ((string-match "\\`\\*+[ \t]+\\'"
  11879. (buffer-substring (point-at-bol) beg))
  11880. (setq type :todo)
  11881. (mapcar 'list org-todo-keywords-1))
  11882. (searchhead
  11883. (setq type :searchhead)
  11884. (save-excursion
  11885. (goto-char (point-min))
  11886. (while (re-search-forward org-todo-line-regexp nil t)
  11887. (push (list
  11888. (org-make-org-heading-search-string
  11889. (match-string 3) t))
  11890. tbl)))
  11891. tbl)
  11892. (tag (setq type :tag beg beg1)
  11893. (or org-tag-alist (org-get-buffer-tags)))
  11894. (prop (setq type :prop beg beg1)
  11895. (mapcar 'list (org-buffer-property-keys)))
  11896. (t (progn
  11897. (call-interactively org-completion-fallback-command)
  11898. (throw 'exit nil)))))
  11899. (pattern (buffer-substring-no-properties beg end))
  11900. (completion (try-completion pattern table confirm)))
  11901. (cond ((eq completion t)
  11902. (if (not (assoc (upcase pattern) table))
  11903. (message "Already complete")
  11904. (if (equal type :opt)
  11905. (insert (substring (cdr (assoc (upcase pattern) table))
  11906. (length pattern)))
  11907. (if (memq type '(:tag :prop)) (insert ":")))))
  11908. ((null completion)
  11909. (message "Can't find completion for \"%s\"" pattern)
  11910. (ding))
  11911. ((not (string= pattern completion))
  11912. (delete-region beg end)
  11913. (if (string-match " +$" completion)
  11914. (setq completion (replace-match "" t t completion)))
  11915. (insert completion)
  11916. (if (get-buffer-window "*Completions*")
  11917. (delete-window (get-buffer-window "*Completions*")))
  11918. (if (assoc completion table)
  11919. (if (eq type :todo) (insert " ")
  11920. (if (memq type '(:tag :prop)) (insert ":"))))
  11921. (if (and (equal type :opt) (assoc completion table))
  11922. (message "%s" (substitute-command-keys
  11923. "Press \\[org-complete] again to insert example settings"))))
  11924. (t
  11925. (message "Making completion list...")
  11926. (let ((list (sort (all-completions pattern table confirm)
  11927. 'string<)))
  11928. (with-output-to-temp-buffer "*Completions*"
  11929. (condition-case nil
  11930. ;; Protection needed for XEmacs and emacs 21
  11931. (display-completion-list list pattern)
  11932. (error (display-completion-list list)))))
  11933. (message "Making completion list...%s" "done")))))))
  11934. ;;;; TODO, DEADLINE, Comments
  11935. (defun org-toggle-comment ()
  11936. "Change the COMMENT state of an entry."
  11937. (interactive)
  11938. (save-excursion
  11939. (org-back-to-heading)
  11940. (if (looking-at (concat outline-regexp
  11941. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  11942. (replace-match "" t t nil 1)
  11943. (if (looking-at outline-regexp)
  11944. (progn
  11945. (goto-char (match-end 0))
  11946. (insert org-comment-string " "))))))
  11947. (defvar org-last-todo-state-is-todo nil
  11948. "This is non-nil when the last TODO state change led to a TODO state.
  11949. If the last change removed the TODO tag or switched to DONE, then
  11950. this is nil.")
  11951. (defvar org-setting-tags nil) ; dynamically skiped
  11952. ;; FIXME: better place
  11953. (defun org-property-or-variable-value (var &optional inherit)
  11954. "Check if there is a property fixing the value of VAR.
  11955. If yes, return this value. If not, return the current value of the variable."
  11956. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11957. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11958. (read prop)
  11959. (symbol-value var))))
  11960. (defun org-todo (&optional arg)
  11961. "Change the TODO state of an item.
  11962. The state of an item is given by a keyword at the start of the heading,
  11963. like
  11964. *** TODO Write paper
  11965. *** DONE Call mom
  11966. The different keywords are specified in the variable `org-todo-keywords'.
  11967. By default the available states are \"TODO\" and \"DONE\".
  11968. So for this example: when the item starts with TODO, it is changed to DONE.
  11969. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11970. DONE are present, add TODO at the beginning of the heading.
  11971. With C-u prefix arg, use completion to determine the new state.
  11972. With numeric prefix arg, switch to that state.
  11973. For calling through lisp, arg is also interpreted in the following way:
  11974. 'none -> empty state
  11975. \"\"(empty string) -> switch to empty state
  11976. 'done -> switch to DONE
  11977. 'nextset -> switch to the next set of keywords
  11978. 'previousset -> switch to the previous set of keywords
  11979. \"WAITING\" -> switch to the specified keyword, but only if it
  11980. really is a member of `org-todo-keywords'."
  11981. (interactive "P")
  11982. (save-excursion
  11983. (org-back-to-heading)
  11984. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  11985. (or (looking-at (concat " +" org-todo-regexp " *"))
  11986. (looking-at " *"))
  11987. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  11988. (org-log-done (org-parse-local-options logging 'org-log-done))
  11989. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  11990. (this (match-string 1))
  11991. (hl-pos (match-beginning 0))
  11992. (head (org-get-todo-sequence-head this))
  11993. (ass (assoc head org-todo-kwd-alist))
  11994. (interpret (nth 1 ass))
  11995. (done-word (nth 3 ass))
  11996. (final-done-word (nth 4 ass))
  11997. (last-state (or this ""))
  11998. (completion-ignore-case t)
  11999. (member (member this org-todo-keywords-1))
  12000. (tail (cdr member))
  12001. (state (cond
  12002. ((and org-todo-key-trigger
  12003. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  12004. (and (not arg) org-use-fast-todo-selection
  12005. (not (eq org-use-fast-todo-selection 'prefix)))))
  12006. ;; Use fast selection
  12007. (org-fast-todo-selection))
  12008. ((and (equal arg '(4))
  12009. (or (not org-use-fast-todo-selection)
  12010. (not org-todo-key-trigger)))
  12011. ;; Read a state with completion
  12012. (completing-read "State: " (mapcar (lambda(x) (list x))
  12013. org-todo-keywords-1)
  12014. nil t))
  12015. ((eq arg 'right)
  12016. (if this
  12017. (if tail (car tail) nil)
  12018. (car org-todo-keywords-1)))
  12019. ((eq arg 'left)
  12020. (if (equal member org-todo-keywords-1)
  12021. nil
  12022. (if this
  12023. (nth (- (length org-todo-keywords-1) (length tail) 2)
  12024. org-todo-keywords-1)
  12025. (org-last org-todo-keywords-1))))
  12026. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  12027. (setq arg nil))) ; hack to fall back to cycling
  12028. (arg
  12029. ;; user or caller requests a specific state
  12030. (cond
  12031. ((equal arg "") nil)
  12032. ((eq arg 'none) nil)
  12033. ((eq arg 'done) (or done-word (car org-done-keywords)))
  12034. ((eq arg 'nextset)
  12035. (or (car (cdr (member head org-todo-heads)))
  12036. (car org-todo-heads)))
  12037. ((eq arg 'previousset)
  12038. (let ((org-todo-heads (reverse org-todo-heads)))
  12039. (or (car (cdr (member head org-todo-heads)))
  12040. (car org-todo-heads))))
  12041. ((car (member arg org-todo-keywords-1)))
  12042. ((nth (1- (prefix-numeric-value arg))
  12043. org-todo-keywords-1))))
  12044. ((null member) (or head (car org-todo-keywords-1)))
  12045. ((equal this final-done-word) nil) ;; -> make empty
  12046. ((null tail) nil) ;; -> first entry
  12047. ((eq interpret 'sequence)
  12048. (car tail))
  12049. ((memq interpret '(type priority))
  12050. (if (eq this-command last-command)
  12051. (car tail)
  12052. (if (> (length tail) 0)
  12053. (or done-word (car org-done-keywords))
  12054. nil)))
  12055. (t nil)))
  12056. (next (if state (concat " " state " ") " "))
  12057. dostates)
  12058. (replace-match next t t)
  12059. (unless (pos-visible-in-window-p hl-pos)
  12060. (message "TODO state changed to %s" (org-trim next)))
  12061. (unless head
  12062. (setq head (org-get-todo-sequence-head state)
  12063. ass (assoc head org-todo-kwd-alist)
  12064. interpret (nth 1 ass)
  12065. done-word (nth 3 ass)
  12066. final-done-word (nth 4 ass)))
  12067. (when (memq arg '(nextset previousset))
  12068. (message "Keyword-Set %d/%d: %s"
  12069. (- (length org-todo-sets) -1
  12070. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  12071. (length org-todo-sets)
  12072. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  12073. (setq org-last-todo-state-is-todo
  12074. (not (member state org-done-keywords)))
  12075. (when (and org-log-done (not (memq arg '(nextset previousset))))
  12076. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  12077. (or (not org-todo-log-states)
  12078. (member state org-todo-log-states))))
  12079. (cond
  12080. ((and state (member state org-not-done-keywords)
  12081. (not (member this org-not-done-keywords)))
  12082. ;; This is now a todo state and was not one before
  12083. ;; Remove any CLOSED timestamp, and possibly log the state change
  12084. (org-add-planning-info nil nil 'closed)
  12085. (and dostates (org-add-log-maybe 'state state 'findpos)))
  12086. ((and state dostates)
  12087. ;; This is a non-nil state, and we need to log it
  12088. (org-add-log-maybe 'state state 'findpos))
  12089. ((and (member state org-done-keywords)
  12090. (not (member this org-done-keywords)))
  12091. ;; It is now done, and it was not done before
  12092. (org-add-planning-info 'closed (org-current-time))
  12093. (org-add-log-maybe 'done state 'findpos))))
  12094. ;; Fixup tag positioning
  12095. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  12096. (run-hooks 'org-after-todo-state-change-hook)
  12097. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  12098. (if (and arg (not (member state org-done-keywords)))
  12099. (setq head (org-get-todo-sequence-head state)))
  12100. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
  12101. ;; Fixup cursor location if close to the keyword
  12102. (if (and (outline-on-heading-p)
  12103. (not (bolp))
  12104. (save-excursion (beginning-of-line 1)
  12105. (looking-at org-todo-line-regexp))
  12106. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  12107. (progn
  12108. (goto-char (or (match-end 2) (match-end 1)))
  12109. (just-one-space))))
  12110. (defun org-get-todo-sequence-head (kwd)
  12111. "Return the head of the TODO sequence to which KWD belongs.
  12112. If KWD is not set, check if there is a text property remembering the
  12113. right sequence."
  12114. (let (p)
  12115. (cond
  12116. ((not kwd)
  12117. (or (get-text-property (point-at-bol) 'org-todo-head)
  12118. (progn
  12119. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  12120. nil (point-at-eol)))
  12121. (get-text-property p 'org-todo-head))))
  12122. ((not (member kwd org-todo-keywords-1))
  12123. (car org-todo-keywords-1))
  12124. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  12125. (defun org-fast-todo-selection ()
  12126. "Fast TODO keyword selection with single keys.
  12127. Returns the new TODO keyword, or nil if no state change should occur."
  12128. (let* ((fulltable org-todo-key-alist)
  12129. (done-keywords org-done-keywords) ;; needed for the faces.
  12130. (maxlen (apply 'max (mapcar
  12131. (lambda (x)
  12132. (if (stringp (car x)) (string-width (car x)) 0))
  12133. fulltable)))
  12134. (buf (current-buffer))
  12135. (expert nil)
  12136. (fwidth (+ maxlen 3 1 3))
  12137. (ncol (/ (- (window-width) 4) fwidth))
  12138. tg cnt e c char c1 c2 ntable tbl rtn
  12139. groups ingroup)
  12140. (save-window-excursion
  12141. (if expert
  12142. (set-buffer (get-buffer-create " *Org todo*"))
  12143. ; (delete-other-windows)
  12144. ; (split-window-vertically)
  12145. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12146. (erase-buffer)
  12147. (org-set-local 'org-done-keywords done-keywords)
  12148. (setq tbl fulltable char ?a cnt 0)
  12149. (while (setq e (pop tbl))
  12150. (cond
  12151. ((equal e '(:startgroup))
  12152. (push '() groups) (setq ingroup t)
  12153. (when (not (= cnt 0))
  12154. (setq cnt 0)
  12155. (insert "\n"))
  12156. (insert "{ "))
  12157. ((equal e '(:endgroup))
  12158. (setq ingroup nil cnt 0)
  12159. (insert "}\n"))
  12160. (t
  12161. (setq tg (car e) c (cdr e))
  12162. (if ingroup (push tg (car groups)))
  12163. (setq tg (org-add-props tg nil 'face
  12164. (org-get-todo-face tg)))
  12165. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12166. (insert "[" c "] " tg (make-string
  12167. (- fwidth 4 (length tg)) ?\ ))
  12168. (when (= (setq cnt (1+ cnt)) ncol)
  12169. (insert "\n")
  12170. (if ingroup (insert " "))
  12171. (setq cnt 0)))))
  12172. (insert "\n")
  12173. (goto-char (point-min))
  12174. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12175. (fit-window-to-buffer))
  12176. (message "[a-z..]:Set [SPC]:clear")
  12177. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12178. (cond
  12179. ((or (= c ?\C-g)
  12180. (and (= c ?q) (not (rassoc c fulltable))))
  12181. (setq quit-flag t))
  12182. ((= c ?\ ) nil)
  12183. ((setq e (rassoc c fulltable) tg (car e))
  12184. tg)
  12185. (t (setq quit-flag t))))))
  12186. (defun org-get-repeat ()
  12187. "Check if tere is a deadline/schedule with repeater in this entry."
  12188. (save-match-data
  12189. (save-excursion
  12190. (org-back-to-heading t)
  12191. (if (re-search-forward
  12192. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  12193. (match-string 1)))))
  12194. (defvar org-last-changed-timestamp)
  12195. (defvar org-log-post-message)
  12196. (defun org-auto-repeat-maybe ()
  12197. "Check if the current headline contains a repeated deadline/schedule.
  12198. If yes, set TODO state back to what it was and change the base date
  12199. of repeating deadline/scheduled time stamps to new date.
  12200. This function should be run in the `org-after-todo-state-change-hook'."
  12201. ;; last-state is dynamically scoped into this function
  12202. (let* ((repeat (org-get-repeat))
  12203. (aa (assoc last-state org-todo-kwd-alist))
  12204. (interpret (nth 1 aa))
  12205. (head (nth 2 aa))
  12206. (done-word (nth 3 aa))
  12207. (whata '(("d" . day) ("m" . month) ("y" . year)))
  12208. (msg "Entry repeats: ")
  12209. (org-log-done)
  12210. re type n what ts)
  12211. (when repeat
  12212. (org-todo (if (eq interpret 'type) last-state head))
  12213. (when (and org-log-repeat
  12214. (not (memq 'org-add-log-note
  12215. (default-value 'post-command-hook))))
  12216. ;; Make sure a note is taken
  12217. (let ((org-log-done '(done)))
  12218. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  12219. 'findpos)))
  12220. (org-back-to-heading t)
  12221. (org-add-planning-info nil nil 'closed)
  12222. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  12223. org-deadline-time-regexp "\\)"))
  12224. (while (re-search-forward
  12225. re (save-excursion (outline-next-heading) (point)) t)
  12226. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  12227. ts (match-string (if (match-end 2) 2 4)))
  12228. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  12229. (setq n (string-to-number (match-string 1 ts))
  12230. what (match-string 2 ts))
  12231. (if (equal what "w") (setq n (* n 7) what "d"))
  12232. (org-timestamp-change n (cdr (assoc what whata))))
  12233. (setq msg (concat msg type org-last-changed-timestamp " ")))
  12234. (setq org-log-post-message msg)
  12235. (message msg))))
  12236. (defun org-show-todo-tree (arg)
  12237. "Make a compact tree which shows all headlines marked with TODO.
  12238. The tree will show the lines where the regexp matches, and all higher
  12239. headlines above the match.
  12240. With \\[universal-argument] prefix, also show the DONE entries.
  12241. With a numeric prefix N, construct a sparse tree for the Nth element
  12242. of `org-todo-keywords-1'."
  12243. (interactive "P")
  12244. (let ((case-fold-search nil)
  12245. (kwd-re
  12246. (cond ((null arg) org-not-done-regexp)
  12247. ((equal arg '(4))
  12248. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  12249. (mapcar 'list org-todo-keywords-1))))
  12250. (concat "\\("
  12251. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12252. "\\)\\>")))
  12253. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12254. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12255. org-todo-keywords-1)))
  12256. (t (error "Invalid prefix argument: %s" arg)))))
  12257. (message "%d TODO entries found"
  12258. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  12259. (defun org-deadline (&optional remove)
  12260. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12261. With argument REMOVE, remove any deadline from the item."
  12262. (interactive "P")
  12263. (if remove
  12264. (progn
  12265. (org-add-planning-info nil nil 'deadline)
  12266. (message "Item no longer has a deadline."))
  12267. (org-add-planning-info 'deadline nil 'closed)))
  12268. (defun org-schedule (&optional remove)
  12269. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12270. With argument REMOVE, remove any scheduling date from the item."
  12271. (interactive "P")
  12272. (if remove
  12273. (progn
  12274. (org-add-planning-info nil nil 'scheduled)
  12275. (message "Item is no longer scheduled."))
  12276. (org-add-planning-info 'scheduled nil 'closed)))
  12277. (defun org-add-planning-info (what &optional time &rest remove)
  12278. "Insert new timestamp with keyword in the line directly after the headline.
  12279. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  12280. If non is given, the user is prompted for a date.
  12281. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12282. be removed."
  12283. (interactive)
  12284. (let (org-time-was-given org-end-time-was-given)
  12285. (when what (setq time (or time (org-read-date nil 'to-time))))
  12286. (when (and org-insert-labeled-timestamps-at-point
  12287. (member what '(scheduled deadline)))
  12288. (insert
  12289. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12290. (org-insert-time-stamp time org-time-was-given
  12291. nil nil nil (list org-end-time-was-given))
  12292. (setq what nil))
  12293. (save-excursion
  12294. (save-restriction
  12295. (let (col list elt ts buffer-invisibility-spec)
  12296. (org-back-to-heading t)
  12297. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  12298. (goto-char (match-end 1))
  12299. (setq col (current-column))
  12300. (goto-char (match-end 0))
  12301. (if (eobp) (insert "\n"))
  12302. (forward-char 1)
  12303. (if (and (not (looking-at outline-regexp))
  12304. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12305. "[^\r\n]*"))
  12306. (not (equal (match-string 1) org-clock-string)))
  12307. (narrow-to-region (match-beginning 0) (match-end 0))
  12308. (insert-before-markers "\n")
  12309. (backward-char 1)
  12310. (narrow-to-region (point) (point))
  12311. (indent-to-column col))
  12312. ;; Check if we have to remove something.
  12313. (setq list (cons what remove))
  12314. (while list
  12315. (setq elt (pop list))
  12316. (goto-char (point-min))
  12317. (when (or (and (eq elt 'scheduled)
  12318. (re-search-forward org-scheduled-time-regexp nil t))
  12319. (and (eq elt 'deadline)
  12320. (re-search-forward org-deadline-time-regexp nil t))
  12321. (and (eq elt 'closed)
  12322. (re-search-forward org-closed-time-regexp nil t)))
  12323. (replace-match "")
  12324. (if (looking-at "--+<[^>]+>") (replace-match ""))
  12325. (if (looking-at " +") (replace-match ""))))
  12326. (goto-char (point-max))
  12327. (when what
  12328. (insert
  12329. (if (not (equal (char-before) ?\ )) " " "")
  12330. (cond ((eq what 'scheduled) org-scheduled-string)
  12331. ((eq what 'deadline) org-deadline-string)
  12332. ((eq what 'closed) org-closed-string))
  12333. " ")
  12334. (setq ts (org-insert-time-stamp
  12335. time
  12336. (or org-time-was-given
  12337. (and (eq what 'closed) org-log-done-with-time))
  12338. (eq what 'closed)
  12339. nil nil (list org-end-time-was-given)))
  12340. (end-of-line 1))
  12341. (goto-char (point-min))
  12342. (widen)
  12343. (if (looking-at "[ \t]+\r?\n")
  12344. (replace-match ""))
  12345. ts)))))
  12346. (defvar org-log-note-marker (make-marker))
  12347. (defvar org-log-note-purpose nil)
  12348. (defvar org-log-note-state nil)
  12349. (defvar org-log-note-window-configuration nil)
  12350. (defvar org-log-note-return-to (make-marker))
  12351. (defvar org-log-post-message nil
  12352. "Message to be displayed after a log note has been stored.
  12353. The auto-repeater uses this.")
  12354. (defun org-add-log-maybe (&optional purpose state findpos)
  12355. "Set up the post command hook to take a note."
  12356. (save-excursion
  12357. (when (and (listp org-log-done)
  12358. (memq purpose org-log-done))
  12359. (when findpos
  12360. (org-back-to-heading t)
  12361. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  12362. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12363. "[^\r\n]*\\)?"))
  12364. (goto-char (match-end 0))
  12365. (unless org-log-states-order-reversed
  12366. (and (= (char-after) ?\n) (forward-char 1))
  12367. (org-skip-over-state-notes)
  12368. (skip-chars-backward " \t\n\r")))
  12369. (move-marker org-log-note-marker (point))
  12370. (setq org-log-note-purpose purpose)
  12371. (setq org-log-note-state state)
  12372. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  12373. (defun org-skip-over-state-notes ()
  12374. "Skip past the list of State notes in an entry."
  12375. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12376. (while (looking-at "[ \t]*- State")
  12377. (condition-case nil
  12378. (org-next-item)
  12379. (error (org-end-of-item)))))
  12380. (defun org-add-log-note (&optional purpose)
  12381. "Pop up a window for taking a note, and add this note later at point."
  12382. (remove-hook 'post-command-hook 'org-add-log-note)
  12383. (setq org-log-note-window-configuration (current-window-configuration))
  12384. (delete-other-windows)
  12385. (move-marker org-log-note-return-to (point))
  12386. (switch-to-buffer (marker-buffer org-log-note-marker))
  12387. (goto-char org-log-note-marker)
  12388. (org-switch-to-buffer-other-window "*Org Note*")
  12389. (erase-buffer)
  12390. (let ((org-inhibit-startup t)) (org-mode))
  12391. (insert (format "# Insert note for %s.
  12392. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12393. (cond
  12394. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12395. ((eq org-log-note-purpose 'done) "closed todo item")
  12396. ((eq org-log-note-purpose 'state)
  12397. (format "state change to \"%s\"" org-log-note-state))
  12398. (t (error "This should not happen")))))
  12399. (org-set-local 'org-finish-function 'org-store-log-note))
  12400. (defun org-store-log-note ()
  12401. "Finish taking a log note, and insert it to where it belongs."
  12402. (let ((txt (buffer-string))
  12403. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12404. lines ind)
  12405. (kill-buffer (current-buffer))
  12406. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  12407. (setq txt (replace-match "" t t txt)))
  12408. (if (string-match "\\s-+\\'" txt)
  12409. (setq txt (replace-match "" t t txt)))
  12410. (setq lines (org-split-string txt "\n"))
  12411. (when (and note (string-match "\\S-" note))
  12412. (setq note
  12413. (org-replace-escapes
  12414. note
  12415. (list (cons "%u" (user-login-name))
  12416. (cons "%U" user-full-name)
  12417. (cons "%t" (format-time-string
  12418. (org-time-stamp-format 'long 'inactive)
  12419. (current-time)))
  12420. (cons "%s" (if org-log-note-state
  12421. (concat "\"" org-log-note-state "\"")
  12422. "")))))
  12423. (if lines (setq note (concat note " \\\\")))
  12424. (push note lines))
  12425. (when (or current-prefix-arg org-note-abort (setq lines nil)))
  12426. (when lines
  12427. (save-excursion
  12428. (set-buffer (marker-buffer org-log-note-marker))
  12429. (save-excursion
  12430. (goto-char org-log-note-marker)
  12431. (move-marker org-log-note-marker nil)
  12432. (end-of-line 1)
  12433. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12434. (indent-relative nil)
  12435. (insert " - " (pop lines))
  12436. (org-indent-line-function)
  12437. (beginning-of-line 1)
  12438. (looking-at "[ \t]*")
  12439. (setq ind (concat (match-string 0) " "))
  12440. (end-of-line 1)
  12441. (while lines (insert "\n" ind (pop lines)))))))
  12442. (set-window-configuration org-log-note-window-configuration)
  12443. (with-current-buffer (marker-buffer org-log-note-return-to)
  12444. (goto-char org-log-note-return-to))
  12445. (move-marker org-log-note-return-to nil)
  12446. (and org-log-post-message (message org-log-post-message)))
  12447. (defvar org-occur-highlights nil)
  12448. (make-variable-buffer-local 'org-occur-highlights)
  12449. (defun org-occur (regexp &optional keep-previous callback)
  12450. "Make a compact tree which shows all matches of REGEXP.
  12451. The tree will show the lines where the regexp matches, and all higher
  12452. headlines above the match. It will also show the heading after the match,
  12453. to make sure editing the matching entry is easy.
  12454. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12455. call to `org-occur' will be kept, to allow stacking of calls to this
  12456. command.
  12457. If CALLBACK is non-nil, it is a function which is called to confirm
  12458. that the match should indeed be shown."
  12459. (interactive "sRegexp: \nP")
  12460. (or keep-previous (org-remove-occur-highlights nil nil t))
  12461. (let ((cnt 0))
  12462. (save-excursion
  12463. (goto-char (point-min))
  12464. (if (or (not keep-previous) ; do not want to keep
  12465. (not org-occur-highlights)) ; no previous matches
  12466. ;; hide everything
  12467. (org-overview))
  12468. (while (re-search-forward regexp nil t)
  12469. (when (or (not callback)
  12470. (save-match-data (funcall callback)))
  12471. (setq cnt (1+ cnt))
  12472. (when org-highlight-sparse-tree-matches
  12473. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12474. (org-show-context 'occur-tree))))
  12475. (when org-remove-highlights-with-change
  12476. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12477. nil 'local))
  12478. (unless org-sparse-tree-open-archived-trees
  12479. (org-hide-archived-subtrees (point-min) (point-max)))
  12480. (run-hooks 'org-occur-hook)
  12481. (if (interactive-p)
  12482. (message "%d match(es) for regexp %s" cnt regexp))
  12483. cnt))
  12484. (defun org-show-context (&optional key)
  12485. "Make sure point and context and visible.
  12486. How much context is shown depends upon the variables
  12487. `org-show-hierarchy-above', `org-show-following-heading'. and
  12488. `org-show-siblings'."
  12489. (let ((heading-p (org-on-heading-p t))
  12490. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12491. (following-p (org-get-alist-option org-show-following-heading key))
  12492. (siblings-p (org-get-alist-option org-show-siblings key)))
  12493. (catch 'exit
  12494. ;; Show heading or entry text
  12495. (if heading-p
  12496. (org-flag-heading nil) ; only show the heading
  12497. (and (or (org-invisible-p) (org-invisible-p2))
  12498. (org-show-hidden-entry))) ; show entire entry
  12499. (when following-p
  12500. ;; Show next sibling, or heading below text
  12501. (save-excursion
  12502. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12503. (org-flag-heading nil))))
  12504. (when siblings-p (org-show-siblings))
  12505. (when hierarchy-p
  12506. ;; show all higher headings, possibly with siblings
  12507. (save-excursion
  12508. (while (and (condition-case nil
  12509. (progn (org-up-heading-all 1) t)
  12510. (error nil))
  12511. (not (bobp)))
  12512. (org-flag-heading nil)
  12513. (when siblings-p (org-show-siblings))))))))
  12514. (defun org-reveal (&optional siblings)
  12515. "Show current entry, hierarchy above it, and the following headline.
  12516. This can be used to show a consistent set of context around locations
  12517. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12518. not t for the search context.
  12519. With optional argument SIBLINGS, on each level of the hierarchy all
  12520. siblings are shown. This repairs the tree structure to what it would
  12521. look like when opened with hierarchical calls to `org-cycle'."
  12522. (interactive "P")
  12523. (let ((org-show-hierarchy-above t)
  12524. (org-show-following-heading t)
  12525. (org-show-siblings (if siblings t org-show-siblings)))
  12526. (org-show-context nil)))
  12527. (defun org-highlight-new-match (beg end)
  12528. "Highlight from BEG to END and mark the highlight is an occur headline."
  12529. (let ((ov (org-make-overlay beg end)))
  12530. (org-overlay-put ov 'face 'secondary-selection)
  12531. (push ov org-occur-highlights)))
  12532. (defun org-remove-occur-highlights (&optional beg end noremove)
  12533. "Remove the occur highlights from the buffer.
  12534. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12535. from the `before-change-functions' in the current buffer."
  12536. (interactive)
  12537. (unless org-inhibit-highlight-removal
  12538. (mapc 'org-delete-overlay org-occur-highlights)
  12539. (setq org-occur-highlights nil)
  12540. (unless noremove
  12541. (remove-hook 'before-change-functions
  12542. 'org-remove-occur-highlights 'local))))
  12543. ;;;; Priorities
  12544. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12545. "Regular expression matching the priority indicator.")
  12546. (defvar org-remove-priority-next-time nil)
  12547. (defun org-priority-up ()
  12548. "Increase the priority of the current item."
  12549. (interactive)
  12550. (org-priority 'up))
  12551. (defun org-priority-down ()
  12552. "Decrease the priority of the current item."
  12553. (interactive)
  12554. (org-priority 'down))
  12555. (defun org-priority (&optional action)
  12556. "Change the priority of an item by ARG.
  12557. ACTION can be `set', `up', `down', or a character."
  12558. (interactive)
  12559. (setq action (or action 'set))
  12560. (let (current new news have remove)
  12561. (save-excursion
  12562. (org-back-to-heading)
  12563. (if (looking-at org-priority-regexp)
  12564. (setq current (string-to-char (match-string 2))
  12565. have t)
  12566. (setq current org-default-priority))
  12567. (cond
  12568. ((or (eq action 'set) (integerp action))
  12569. (if (integerp action)
  12570. (setq new action)
  12571. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  12572. (setq new (read-char-exclusive)))
  12573. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12574. (= (upcase org-lowest-priority) org-lowest-priority))
  12575. (setq new (upcase new)))
  12576. (cond ((equal new ?\ ) (setq remove t))
  12577. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12578. (error "Priority must be between `%c' and `%c'"
  12579. org-highest-priority org-lowest-priority))))
  12580. ((eq action 'up)
  12581. (if (and (not have) (eq last-command this-command))
  12582. (setq new org-lowest-priority)
  12583. (setq new (if (and org-priority-start-cycle-with-default (not have))
  12584. org-default-priority (1- current)))))
  12585. ((eq action 'down)
  12586. (if (and (not have) (eq last-command this-command))
  12587. (setq new org-highest-priority)
  12588. (setq new (if (and org-priority-start-cycle-with-default (not have))
  12589. org-default-priority (1+ current)))))
  12590. (t (error "Invalid action")))
  12591. (if (or (< (upcase new) org-highest-priority)
  12592. (> (upcase new) org-lowest-priority))
  12593. (setq remove t))
  12594. (setq news (format "%c" new))
  12595. (if have
  12596. (if remove
  12597. (replace-match "" t t nil 1)
  12598. (replace-match news t t nil 2))
  12599. (if remove
  12600. (error "No priority cookie found in line")
  12601. (looking-at org-todo-line-regexp)
  12602. (if (match-end 2)
  12603. (progn
  12604. (goto-char (match-end 2))
  12605. (insert " [#" news "]"))
  12606. (goto-char (match-beginning 3))
  12607. (insert "[#" news "] ")))))
  12608. (org-preserve-lc (org-set-tags nil 'align))
  12609. (if remove
  12610. (message "Priority removed")
  12611. (message "Priority of current item set to %s" news))))
  12612. (defun org-get-priority (s)
  12613. "Find priority cookie and return priority."
  12614. (save-match-data
  12615. (if (not (string-match org-priority-regexp s))
  12616. (* 1000 (- org-lowest-priority org-default-priority))
  12617. (* 1000 (- org-lowest-priority
  12618. (string-to-char (match-string 2 s)))))))
  12619. ;;;; Tags
  12620. (defun org-scan-tags (action matcher &optional todo-only)
  12621. "Scan headline tags with inheritance and produce output ACTION.
  12622. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  12623. evaluated, testing if a given set of tags qualifies a headline for
  12624. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  12625. are included in the output."
  12626. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  12627. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12628. (org-re
  12629. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  12630. (props (list 'face nil
  12631. 'done-face 'org-done
  12632. 'undone-face nil
  12633. 'mouse-face 'highlight
  12634. 'org-not-done-regexp org-not-done-regexp
  12635. 'org-todo-regexp org-todo-regexp
  12636. 'keymap org-agenda-keymap
  12637. 'help-echo
  12638. (format "mouse-2 or RET jump to org file %s"
  12639. (abbreviate-file-name buffer-file-name))))
  12640. (case-fold-search nil)
  12641. lspos
  12642. tags tags-list tags-alist (llast 0) rtn level category i txt
  12643. todo marker entry priority)
  12644. (save-excursion
  12645. (goto-char (point-min))
  12646. (when (eq action 'sparse-tree)
  12647. (org-overview)
  12648. (org-remove-occur-highlights))
  12649. (while (re-search-forward re nil t)
  12650. (catch :skip
  12651. (setq todo (if (match-end 1) (match-string 2))
  12652. tags (if (match-end 4) (match-string 4)))
  12653. (goto-char (setq lspos (1+ (match-beginning 0))))
  12654. (setq level (org-reduced-level (funcall outline-level))
  12655. category (org-get-category))
  12656. (setq i llast llast level)
  12657. ;; remove tag lists from same and sublevels
  12658. (while (>= i level)
  12659. (when (setq entry (assoc i tags-alist))
  12660. (setq tags-alist (delete entry tags-alist)))
  12661. (setq i (1- i)))
  12662. ;; add the nex tags
  12663. (when tags
  12664. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  12665. tags-alist
  12666. (cons (cons level tags) tags-alist)))
  12667. ;; compile tags for current headline
  12668. (setq tags-list
  12669. (if org-use-tag-inheritance
  12670. (apply 'append (mapcar 'cdr tags-alist))
  12671. tags))
  12672. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  12673. (eval matcher)
  12674. (or (not org-agenda-skip-archived-trees)
  12675. (not (member org-archive-tag tags-list))))
  12676. (and (eq action 'agenda) (org-agenda-skip))
  12677. ;; list this headline
  12678. (if (eq action 'sparse-tree)
  12679. (progn
  12680. (and org-highlight-sparse-tree-matches
  12681. (org-get-heading) (match-end 0)
  12682. (org-highlight-new-match
  12683. (match-beginning 0) (match-beginning 1)))
  12684. (org-show-context 'tags-tree))
  12685. (setq txt (org-format-agenda-item
  12686. ""
  12687. (concat
  12688. (if org-tags-match-list-sublevels
  12689. (make-string (1- level) ?.) "")
  12690. (org-get-heading))
  12691. category tags-list)
  12692. priority (org-get-priority txt))
  12693. (goto-char lspos)
  12694. (setq marker (org-agenda-new-marker))
  12695. (org-add-props txt props
  12696. 'org-marker marker 'org-hd-marker marker 'org-category category
  12697. 'priority priority 'type "tagsmatch")
  12698. (push txt rtn))
  12699. ;; if we are to skip sublevels, jump to end of subtree
  12700. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  12701. (when (and (eq action 'sparse-tree)
  12702. (not org-sparse-tree-open-archived-trees))
  12703. (org-hide-archived-subtrees (point-min) (point-max)))
  12704. (nreverse rtn)))
  12705. (defvar todo-only) ;; dynamically scoped
  12706. (defun org-tags-sparse-tree (&optional todo-only match)
  12707. "Create a sparse tree according to tags string MATCH.
  12708. MATCH can contain positive and negative selection of tags, like
  12709. \"+WORK+URGENT-WITHBOSS\".
  12710. If optional argument TODO_ONLY is non-nil, only select lines that are
  12711. also TODO lines."
  12712. (interactive "P")
  12713. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12714. (defvar org-cached-props nil)
  12715. (defun org-cached-entry-get (pom property)
  12716. (if org-use-property-inheritance
  12717. ;; Caching is not possible, check it directly
  12718. (org-entry-get pom property 'inherit)
  12719. ;; Get all properties, so that we can do complicated checks easily
  12720. (cdr (assoc property (or org-cached-props
  12721. (setq org-cached-props
  12722. (org-entry-properties pom)))))))
  12723. (defun org-global-tags-completion-table (&optional files)
  12724. "Return the list of all tags in all agenda buffer/files."
  12725. (save-excursion
  12726. (org-uniquify
  12727. (apply 'append
  12728. (mapcar
  12729. (lambda (file)
  12730. (set-buffer (find-file-noselect file))
  12731. (org-get-buffer-tags))
  12732. (if (and files (car files))
  12733. files
  12734. (org-agenda-files)))))))
  12735. (defun org-make-tags-matcher (match)
  12736. "Create the TAGS//TODO matcher form for the selection string MATCH."
  12737. ;; todo-only is scoped dynamically into this function, and the function
  12738. ;; may change it it the matcher asksk for it.
  12739. (unless match
  12740. ;; Get a new match request, with completion
  12741. (let ((org-last-tags-completion-table
  12742. (org-global-tags-completion-table)))
  12743. (setq match (completing-read
  12744. "Match: " 'org-tags-completion-function nil nil nil
  12745. 'org-tags-history))))
  12746. ;; Parse the string and create a lisp form
  12747. (let ((match0 match)
  12748. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  12749. minus tag mm
  12750. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12751. orterms term orlist re-p level-p prop-p pn pv)
  12752. (if (string-match "/+" match)
  12753. ;; match contains also a todo-matching request
  12754. (progn
  12755. (setq tagsmatch (substring match 0 (match-beginning 0))
  12756. todomatch (substring match (match-end 0)))
  12757. (if (string-match "^!" todomatch)
  12758. (setq todo-only t todomatch (substring todomatch 1)))
  12759. (if (string-match "^\\s-*$" todomatch)
  12760. (setq todomatch nil)))
  12761. ;; only matching tags
  12762. (setq tagsmatch match todomatch nil))
  12763. ;; Make the tags matcher
  12764. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12765. (setq tagsmatcher t)
  12766. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12767. (while (setq term (pop orterms))
  12768. (while (and (equal (substring term -1) "\\") orterms)
  12769. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12770. (while (string-match re term)
  12771. (setq minus (and (match-end 1)
  12772. (equal (match-string 1 term) "-"))
  12773. tag (match-string 2 term)
  12774. re-p (equal (string-to-char tag) ?{)
  12775. level-p (match-end 3)
  12776. prop-p (match-end 4)
  12777. mm (cond
  12778. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12779. (level-p `(= level ,(string-to-number
  12780. (match-string 3 term))))
  12781. (prop-p
  12782. (setq pn (match-string 4 term)
  12783. pv (match-string 5 term)
  12784. re-p (equal (string-to-char pv) ?{)
  12785. pv (substring pv 1 -1))
  12786. (if re-p
  12787. `(string-match ,pv (or (org-cached-entry-get nil ,pn) ""))
  12788. `(equal ,pv (org-cached-entry-get nil ,pn))))
  12789. (t `(member ,(downcase tag) tags-list)))
  12790. mm (if minus (list 'not mm) mm)
  12791. term (substring term (match-end 0)))
  12792. (push mm tagsmatcher))
  12793. (push (if (> (length tagsmatcher) 1)
  12794. (cons 'and tagsmatcher)
  12795. (car tagsmatcher))
  12796. orlist)
  12797. (setq tagsmatcher nil))
  12798. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12799. (setq tagsmatcher
  12800. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12801. ;; Make the todo matcher
  12802. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12803. (setq todomatcher t)
  12804. (setq orterms (org-split-string todomatch "|") orlist nil)
  12805. (while (setq term (pop orterms))
  12806. (while (string-match re term)
  12807. (setq minus (and (match-end 1)
  12808. (equal (match-string 1 term) "-"))
  12809. kwd (match-string 2 term)
  12810. re-p (equal (string-to-char kwd) ?{)
  12811. term (substring term (match-end 0))
  12812. mm (if re-p
  12813. `(string-match ,(substring kwd 1 -1) todo)
  12814. (list 'equal 'todo kwd))
  12815. mm (if minus (list 'not mm) mm))
  12816. (push mm todomatcher))
  12817. (push (if (> (length todomatcher) 1)
  12818. (cons 'and todomatcher)
  12819. (car todomatcher))
  12820. orlist)
  12821. (setq todomatcher nil))
  12822. (setq todomatcher (if (> (length orlist) 1)
  12823. (cons 'or orlist) (car orlist))))
  12824. ;; Return the string and lisp forms of the matcher
  12825. (setq matcher (if todomatcher
  12826. (list 'and tagsmatcher todomatcher)
  12827. tagsmatcher))
  12828. (cons match0 matcher)))
  12829. (defun org-match-any-p (re list)
  12830. "Does re match any element of list?"
  12831. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12832. (delq nil list))
  12833. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  12834. (defvar org-tags-overlay (org-make-overlay 1 1))
  12835. (org-detach-overlay org-tags-overlay)
  12836. (defun org-align-tags-here (to-col)
  12837. ;; Assumes that this is a headline
  12838. (let ((pos (point)) (col (current-column)) tags)
  12839. (beginning-of-line 1)
  12840. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12841. (< pos (match-beginning 2)))
  12842. (progn
  12843. (setq tags (match-string 2))
  12844. (goto-char (match-beginning 1))
  12845. (insert " ")
  12846. (delete-region (point) (1+ (match-end 0)))
  12847. (backward-char 1)
  12848. (move-to-column
  12849. (max (1+ (current-column))
  12850. (1+ col)
  12851. (if (> to-col 0)
  12852. to-col
  12853. (- (abs to-col) (length tags))))
  12854. t)
  12855. (insert tags)
  12856. (move-to-column (min (current-column) col) t))
  12857. (goto-char pos))))
  12858. (defun org-set-tags (&optional arg just-align)
  12859. "Set the tags for the current headline.
  12860. With prefix ARG, realign all tags in headings in the current buffer."
  12861. (interactive "P")
  12862. (let* ((re (concat "^" outline-regexp))
  12863. (current (org-get-tags-string))
  12864. (col (current-column))
  12865. (org-setting-tags t)
  12866. table current-tags inherited-tags ; computed below when needed
  12867. tags p0 c0 c1 rpl)
  12868. (if arg
  12869. (save-excursion
  12870. (goto-char (point-min))
  12871. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12872. (while (re-search-forward re nil t)
  12873. (org-set-tags nil t)
  12874. (end-of-line 1)))
  12875. (message "All tags realigned to column %d" org-tags-column))
  12876. (if just-align
  12877. (setq tags current)
  12878. ;; Get a new set of tags from the user
  12879. (save-excursion
  12880. (setq table (or org-tag-alist (org-get-buffer-tags))
  12881. org-last-tags-completion-table table
  12882. current-tags (org-split-string current ":")
  12883. inherited-tags (nreverse
  12884. (nthcdr (length current-tags)
  12885. (nreverse (org-get-tags-at))))
  12886. tags
  12887. (if (or (eq t org-use-fast-tag-selection)
  12888. (and org-use-fast-tag-selection
  12889. (delq nil (mapcar 'cdr table))))
  12890. (org-fast-tag-selection
  12891. current-tags inherited-tags table
  12892. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  12893. (let ((org-add-colon-after-tag-completion t))
  12894. (org-trim
  12895. (org-without-partial-completion
  12896. (completing-read "Tags: " 'org-tags-completion-function
  12897. nil nil current 'org-tags-history)))))))
  12898. (while (string-match "[-+&]+" tags)
  12899. ;; No boolean logic, just a list
  12900. (setq tags (replace-match ":" t t tags))))
  12901. (if (string-match "\\`[\t ]*\\'" tags)
  12902. (setq tags "")
  12903. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12904. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12905. ;; Insert new tags at the correct column
  12906. (beginning-of-line 1)
  12907. (cond
  12908. ((and (equal current "") (equal tags "")))
  12909. ((re-search-forward
  12910. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12911. (point-at-eol) t)
  12912. (if (equal tags "")
  12913. (setq rpl "")
  12914. (goto-char (match-beginning 0))
  12915. (setq c0 (current-column) p0 (point)
  12916. c1 (max (1+ c0) (if (> org-tags-column 0)
  12917. org-tags-column
  12918. (- (- org-tags-column) (length tags))))
  12919. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12920. (replace-match rpl t t)
  12921. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  12922. tags)
  12923. (t (error "Tags alignment failed")))
  12924. (move-to-column col)
  12925. (unless just-align
  12926. (run-hooks 'org-after-tags-change-hook)))))
  12927. (defun org-change-tag-in-region (beg end tag off)
  12928. "Add or remove TAG for each entry in the region.
  12929. This works in the agenda, and also in an org-mode buffer."
  12930. (interactive
  12931. (list (region-beginning) (region-end)
  12932. (let ((org-last-tags-completion-table
  12933. (if (org-mode-p)
  12934. (org-get-buffer-tags)
  12935. (org-global-tags-completion-table))))
  12936. (completing-read
  12937. "Tag: " 'org-tags-completion-function nil nil nil
  12938. 'org-tags-history))
  12939. (progn
  12940. (message "[s]et or [r]emove? ")
  12941. (equal (read-char-exclusive) ?r))))
  12942. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12943. (let ((agendap (equal major-mode 'org-agenda-mode))
  12944. l1 l2 m buf pos newhead (cnt 0))
  12945. (goto-char end)
  12946. (setq l2 (1- (org-current-line)))
  12947. (goto-char beg)
  12948. (setq l1 (org-current-line))
  12949. (loop for l from l1 to l2 do
  12950. (goto-line l)
  12951. (setq m (get-text-property (point) 'org-hd-marker))
  12952. (when (or (and (org-mode-p) (org-on-heading-p))
  12953. (and agendap m))
  12954. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12955. pos (if agendap m (point)))
  12956. (with-current-buffer buf
  12957. (save-excursion
  12958. (save-restriction
  12959. (goto-char pos)
  12960. (setq cnt (1+ cnt))
  12961. (org-toggle-tag tag (if off 'off 'on))
  12962. (setq newhead (org-get-heading)))))
  12963. (and agendap (org-agenda-change-all-lines newhead m))))
  12964. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12965. (defun org-tags-completion-function (string predicate &optional flag)
  12966. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12967. (confirm (lambda (x) (stringp (car x)))))
  12968. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  12969. (setq s1 (match-string 1 string)
  12970. s2 (match-string 2 string))
  12971. (setq s1 "" s2 string))
  12972. (cond
  12973. ((eq flag nil)
  12974. ;; try completion
  12975. (setq rtn (try-completion s2 ctable confirm))
  12976. (if (stringp rtn)
  12977. (setq rtn
  12978. (concat s1 s2 (substring rtn (length s2))
  12979. (if (and org-add-colon-after-tag-completion
  12980. (assoc rtn ctable))
  12981. ":" ""))))
  12982. rtn)
  12983. ((eq flag t)
  12984. ;; all-completions
  12985. (all-completions s2 ctable confirm)
  12986. )
  12987. ((eq flag 'lambda)
  12988. ;; exact match?
  12989. (assoc s2 ctable)))
  12990. ))
  12991. (defun org-fast-tag-insert (kwd tags face &optional end)
  12992. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  12993. (insert (format "%-12s" (concat kwd ":"))
  12994. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12995. (or end "")))
  12996. (defun org-fast-tag-show-exit (flag)
  12997. (save-excursion
  12998. (goto-line 3)
  12999. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13000. (replace-match ""))
  13001. (when flag
  13002. (end-of-line 1)
  13003. (move-to-column (- (window-width) 19) t)
  13004. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13005. (defun org-set-current-tags-overlay (current prefix)
  13006. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13007. (if (featurep 'xemacs)
  13008. (org-overlay-display org-tags-overlay (concat prefix s)
  13009. 'secondary-selection)
  13010. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13011. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13012. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13013. "Fast tag selection with single keys.
  13014. CURRENT is the current list of tags in the headline, INHERITED is the
  13015. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13016. possibly with grouping information. TODO-TABLE is a similar table with
  13017. TODO keywords, should these have keys assigned to them.
  13018. If the keys are nil, a-z are automatically assigned.
  13019. Returns the new tags string, or nil to not change the current settings."
  13020. (let* ((fulltable (append table todo-table))
  13021. (maxlen (apply 'max (mapcar
  13022. (lambda (x)
  13023. (if (stringp (car x)) (string-width (car x)) 0))
  13024. fulltable)))
  13025. (buf (current-buffer))
  13026. (expert (eq org-fast-tag-selection-single-key 'expert))
  13027. (buffer-tags nil)
  13028. (fwidth (+ maxlen 3 1 3))
  13029. (ncol (/ (- (window-width) 4) fwidth))
  13030. (i-face 'org-done)
  13031. (c-face 'org-todo)
  13032. tg cnt e c char c1 c2 ntable tbl rtn
  13033. ov-start ov-end ov-prefix
  13034. (exit-after-next org-fast-tag-selection-single-key)
  13035. (done-keywords org-done-keywords)
  13036. groups ingroup)
  13037. (save-excursion
  13038. (beginning-of-line 1)
  13039. (if (looking-at
  13040. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13041. (setq ov-start (match-beginning 1)
  13042. ov-end (match-end 1)
  13043. ov-prefix "")
  13044. (setq ov-start (1- (point-at-eol))
  13045. ov-end (1+ ov-start))
  13046. (skip-chars-forward "^\n\r")
  13047. (setq ov-prefix
  13048. (concat
  13049. (buffer-substring (1- (point)) (point))
  13050. (if (> (current-column) org-tags-column)
  13051. " "
  13052. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13053. (org-move-overlay org-tags-overlay ov-start ov-end)
  13054. (save-window-excursion
  13055. (if expert
  13056. (set-buffer (get-buffer-create " *Org tags*"))
  13057. (delete-other-windows)
  13058. (split-window-vertically)
  13059. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13060. (erase-buffer)
  13061. (org-set-local 'org-done-keywords done-keywords)
  13062. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13063. (org-fast-tag-insert "Current" current c-face "\n\n")
  13064. (org-fast-tag-show-exit exit-after-next)
  13065. (org-set-current-tags-overlay current ov-prefix)
  13066. (setq tbl fulltable char ?a cnt 0)
  13067. (while (setq e (pop tbl))
  13068. (cond
  13069. ((equal e '(:startgroup))
  13070. (push '() groups) (setq ingroup t)
  13071. (when (not (= cnt 0))
  13072. (setq cnt 0)
  13073. (insert "\n"))
  13074. (insert "{ "))
  13075. ((equal e '(:endgroup))
  13076. (setq ingroup nil cnt 0)
  13077. (insert "}\n"))
  13078. (t
  13079. (setq tg (car e) c2 nil)
  13080. (if (cdr e)
  13081. (setq c (cdr e))
  13082. ;; automatically assign a character.
  13083. (setq c1 (string-to-char
  13084. (downcase (substring
  13085. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13086. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13087. (while (or (rassoc char ntable) (rassoc char table))
  13088. (setq char (1+ char)))
  13089. (setq c2 c1))
  13090. (setq c (or c2 char)))
  13091. (if ingroup (push tg (car groups)))
  13092. (setq tg (org-add-props tg nil 'face
  13093. (cond
  13094. ((not (assoc tg table))
  13095. (org-get-todo-face tg))
  13096. ((member tg current) c-face)
  13097. ((member tg inherited) i-face)
  13098. (t nil))))
  13099. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13100. (insert "[" c "] " tg (make-string
  13101. (- fwidth 4 (length tg)) ?\ ))
  13102. (push (cons tg c) ntable)
  13103. (when (= (setq cnt (1+ cnt)) ncol)
  13104. (insert "\n")
  13105. (if ingroup (insert " "))
  13106. (setq cnt 0)))))
  13107. (setq ntable (nreverse ntable))
  13108. (insert "\n")
  13109. (goto-char (point-min))
  13110. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13111. (fit-window-to-buffer))
  13112. (setq rtn
  13113. (catch 'exit
  13114. (while t
  13115. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  13116. (if groups " [!] no groups" " [!]groups")
  13117. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13118. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13119. (cond
  13120. ((= c ?\r) (throw 'exit t))
  13121. ((= c ?!)
  13122. (setq groups (not groups))
  13123. (goto-char (point-min))
  13124. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13125. ((= c ?\C-c)
  13126. (if (not expert)
  13127. (org-fast-tag-show-exit
  13128. (setq exit-after-next (not exit-after-next)))
  13129. (setq expert nil)
  13130. (delete-other-windows)
  13131. (split-window-vertically)
  13132. (org-switch-to-buffer-other-window " *Org tags*")
  13133. (and (fboundp 'fit-window-to-buffer)
  13134. (fit-window-to-buffer))))
  13135. ((or (= c ?\C-g)
  13136. (and (= c ?q) (not (rassoc c ntable))))
  13137. (org-detach-overlay org-tags-overlay)
  13138. (setq quit-flag t))
  13139. ((= c ?\ )
  13140. (setq current nil)
  13141. (if exit-after-next (setq exit-after-next 'now)))
  13142. ((= c ?\t)
  13143. (condition-case nil
  13144. (setq tg (completing-read
  13145. "Tag: "
  13146. (or buffer-tags
  13147. (with-current-buffer buf
  13148. (org-get-buffer-tags)))))
  13149. (quit (setq tg "")))
  13150. (when (string-match "\\S-" tg)
  13151. (add-to-list 'buffer-tags (list tg))
  13152. (if (member tg current)
  13153. (setq current (delete tg current))
  13154. (push tg current)))
  13155. (if exit-after-next (setq exit-after-next 'now)))
  13156. ((setq e (rassoc c todo-table) tg (car e))
  13157. (with-current-buffer buf
  13158. (save-excursion (org-todo tg)))
  13159. (if exit-after-next (setq exit-after-next 'now)))
  13160. ((setq e (rassoc c ntable) tg (car e))
  13161. (if (member tg current)
  13162. (setq current (delete tg current))
  13163. (loop for g in groups do
  13164. (if (member tg g)
  13165. (mapcar (lambda (x)
  13166. (setq current (delete x current)))
  13167. g)))
  13168. (push tg current))
  13169. (if exit-after-next (setq exit-after-next 'now))))
  13170. ;; Create a sorted list
  13171. (setq current
  13172. (sort current
  13173. (lambda (a b)
  13174. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13175. (if (eq exit-after-next 'now) (throw 'exit t))
  13176. (goto-char (point-min))
  13177. (beginning-of-line 2)
  13178. (delete-region (point) (point-at-eol))
  13179. (org-fast-tag-insert "Current" current c-face)
  13180. (org-set-current-tags-overlay current ov-prefix)
  13181. (while (re-search-forward
  13182. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  13183. (setq tg (match-string 1))
  13184. (add-text-properties
  13185. (match-beginning 1) (match-end 1)
  13186. (list 'face
  13187. (cond
  13188. ((member tg current) c-face)
  13189. ((member tg inherited) i-face)
  13190. (t (get-text-property (match-beginning 1) 'face))))))
  13191. (goto-char (point-min)))))
  13192. (org-detach-overlay org-tags-overlay)
  13193. (if rtn
  13194. (mapconcat 'identity current ":")
  13195. nil))))
  13196. (defun org-get-tags-string ()
  13197. "Get the TAGS string in the current headline."
  13198. (unless (org-on-heading-p t)
  13199. (error "Not on a heading"))
  13200. (save-excursion
  13201. (beginning-of-line 1)
  13202. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13203. (org-match-string-no-properties 1)
  13204. "")))
  13205. (defun org-get-tags ()
  13206. "Get the list of tags specified in the current headline."
  13207. (org-split-string (org-get-tags-string) ":"))
  13208. (defun org-get-buffer-tags ()
  13209. "Get a table of all tags used in the buffer, for completion."
  13210. (let (tags)
  13211. (save-excursion
  13212. (goto-char (point-min))
  13213. (while (re-search-forward
  13214. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  13215. (when (equal (char-after (point-at-bol 0)) ?*)
  13216. (mapc (lambda (x) (add-to-list 'tags x))
  13217. (org-split-string (org-match-string-no-properties 1) ":")))))
  13218. (mapcar 'list tags)))
  13219. ;;;; Properties
  13220. ;;; Setting and retrieving properties
  13221. (defconst org-special-properties
  13222. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY")
  13223. "The special properties valid in Org-mode.
  13224. These are properties that are not defined in the property drawer,
  13225. but in some other way.")
  13226. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13227. "Regular expression matching the first line of a property drawer.")
  13228. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13229. "Regular expression matching the first line of a property drawer.")
  13230. (defun org-property-action ()
  13231. "Do an action on properties."
  13232. (interactive)
  13233. (let (c prop)
  13234. (org-at-property-p)
  13235. (setq prop (match-string 2))
  13236. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13237. (setq c (read-char-exclusive))
  13238. (cond
  13239. ((equal c ?s)
  13240. (call-interactively 'org-set-property))
  13241. ((equal c ?d)
  13242. (call-interactively 'org-delete-property))
  13243. ((equal c ?D)
  13244. (call-interactively 'org-delete-property-globally))
  13245. ((equal c ?c)
  13246. (call-interactively 'org-compute-property-at-point))
  13247. (t (error "No such property action %c" c)))))
  13248. (defun org-at-property-p ()
  13249. "Is the cursor in a property line?"
  13250. ;; FIXME: Does not check if we are actually in the drawer.
  13251. ;; FIXME: also returns true on any drawers.....
  13252. ;; This is used by C-c C-c for property action.
  13253. (save-excursion
  13254. (beginning-of-line 1)
  13255. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  13256. (defmacro org-with-point-at (pom &rest body)
  13257. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  13258. (declare (indent 1) (debug t))
  13259. `(save-excursion
  13260. (if (markerp pom) (set-buffer (marker-buffer pom)))
  13261. (save-excursion
  13262. (goto-char (or pom (point)))
  13263. ,@body)))
  13264. (defun org-get-property-block (&optional beg end force)
  13265. "Return the (beg . end) range of the body of the property drawer.
  13266. BEG and END can be beginning and end of subtree, if not given
  13267. they will be found.
  13268. If the drawer does not exist and FORCE is non-nil, create the drawer."
  13269. (catch 'exit
  13270. (save-excursion
  13271. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  13272. (end (or end (progn (outline-next-heading) (point)))))
  13273. (goto-char beg)
  13274. (if (re-search-forward org-property-start-re end t)
  13275. (setq beg (1+ (match-end 0)))
  13276. (if force
  13277. (save-excursion
  13278. (org-insert-property-drawer)
  13279. (setq end (progn (outline-next-heading) (point))))
  13280. (throw 'exit nil))
  13281. (goto-char beg)
  13282. (if (re-search-forward org-property-start-re end t)
  13283. (setq beg (1+ (match-end 0)))))
  13284. (if (re-search-forward org-property-end-re end t)
  13285. (setq end (match-beginning 0))
  13286. (or force (throw 'exit nil))
  13287. (goto-char beg)
  13288. (setq end beg)
  13289. (org-indent-line-function)
  13290. (insert ":END:\n"))
  13291. (cons beg end)))))
  13292. (defun org-entry-properties (&optional pom which)
  13293. "Get all properties of the entry at point-or-marker POM.
  13294. This includes the TODO keyword, the tags, time strings for deadline,
  13295. scheduled, and clocking, and any additional properties defined in the
  13296. entry. The return value is an alist, keys may occur multiple times
  13297. if the property key was used several times.
  13298. POM may also be nil, in which case the current entry is used.
  13299. If WHICH is nil or `all', get all properties. If WHICH is
  13300. `special' or `standard', only get that subclass."
  13301. (setq which (or which 'all))
  13302. (org-with-point-at pom
  13303. (let ((clockstr (substring org-clock-string 0 -1))
  13304. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  13305. beg end range props sum-props key value)
  13306. (save-excursion
  13307. (when (condition-case nil (org-back-to-heading t) (error nil))
  13308. (setq beg (point))
  13309. (setq sum-props (get-text-property (point) 'org-summaries))
  13310. (outline-next-heading)
  13311. (setq end (point))
  13312. (when (memq which '(all special))
  13313. ;; Get the special properties, like TODO and tags
  13314. (goto-char beg)
  13315. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  13316. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13317. (when (looking-at org-priority-regexp)
  13318. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13319. (when (and (setq value (org-get-tags-string))
  13320. (string-match "\\S-" value))
  13321. (push (cons "TAGS" value) props))
  13322. (when (setq value (org-get-tags-at))
  13323. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  13324. props))
  13325. (while (re-search-forward org-keyword-time-regexp end t)
  13326. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  13327. (unless (member key excluded) (push key excluded))
  13328. (push (cons key
  13329. (if (equal key clockstr)
  13330. (org-no-properties
  13331. (org-trim
  13332. (buffer-substring
  13333. (match-beginning 2) (point-at-eol))))
  13334. (org-match-string-no-properties 2)))
  13335. props)))
  13336. (when (memq which '(all standard))
  13337. ;; Get the standard properties, like :PORP: ...
  13338. (setq range (org-get-property-block beg end))
  13339. (when range
  13340. (goto-char (car range))
  13341. (while (re-search-forward
  13342. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13343. (cdr range) t)
  13344. (setq key (org-match-string-no-properties 1)
  13345. value (org-trim (or (org-match-string-no-properties 2) "")))
  13346. (unless (member key excluded)
  13347. (push (cons key (or value "")) props)))))
  13348. (append sum-props (nreverse props)))))))
  13349. (defun org-entry-get (pom property &optional inherit)
  13350. "Get value of PROPERTY for entry at point-or-marker POM.
  13351. If INHERIT is non-nil and the entry does not have the property,
  13352. then also check higher levels of the hierarchy.
  13353. If the property is present but empty, the return value is the empty string.
  13354. If the property is not present at all, nil is returned."
  13355. (org-with-point-at pom
  13356. (if inherit
  13357. (org-entry-get-with-inheritance property)
  13358. (if (member property org-special-properties)
  13359. ;; We need a special property. Use brute force, get all properties.
  13360. (cdr (assoc property (org-entry-properties nil 'special)))
  13361. (let ((range (org-get-property-block)))
  13362. (if (and range
  13363. (goto-char (car range))
  13364. (re-search-forward
  13365. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  13366. (cdr range) t))
  13367. ;; Found the property, return it.
  13368. (if (match-end 1)
  13369. (org-match-string-no-properties 1)
  13370. "")))))))
  13371. (defun org-entry-delete (pom property)
  13372. "Delete the property PROPERTY from entry at point-or-marker POM."
  13373. (org-with-point-at pom
  13374. (if (member property org-special-properties)
  13375. nil ; cannot delete these properties.
  13376. (let ((range (org-get-property-block)))
  13377. (if (and range
  13378. (goto-char (car range))
  13379. (re-search-forward
  13380. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  13381. (cdr range) t))
  13382. (progn
  13383. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13384. t)
  13385. nil)))))
  13386. (defvar org-entry-property-inherited-from (make-marker))
  13387. (defun org-entry-get-with-inheritance (property)
  13388. "Get entry property, and search higher levels if not present."
  13389. (let (tmp)
  13390. (save-excursion
  13391. (catch 'ex
  13392. (while t
  13393. (when (setq tmp (org-entry-get nil property))
  13394. (org-back-to-heading t)
  13395. (move-marker org-entry-property-inherited-from (point))
  13396. (throw 'ex tmp))
  13397. (or (org-up-heading-safe) (throw 'ex nil)))))
  13398. (or tmp (cdr (assoc property org-local-properties))
  13399. (cdr (assoc property org-global-properties)))))
  13400. (defun org-entry-put (pom property value)
  13401. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13402. (org-with-point-at pom
  13403. (org-back-to-heading t)
  13404. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13405. range)
  13406. (cond
  13407. ((equal property "TODO")
  13408. (when (and (stringp value) (string-match "\\S-" value)
  13409. (not (member value org-todo-keywords-1)))
  13410. (error "\"%s\" is not a valid TODO state" value))
  13411. (if (or (not value)
  13412. (not (string-match "\\S-" value)))
  13413. (setq value 'none))
  13414. (org-todo value)
  13415. (org-set-tags nil 'align))
  13416. ((equal property "PRIORITY")
  13417. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13418. (string-to-char value) ?\ ))
  13419. (org-set-tags nil 'align))
  13420. ((equal property "SCHEDULED")
  13421. (if (re-search-forward org-scheduled-time-regexp end t)
  13422. (cond
  13423. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13424. ((eq value 'later) (org-timestamp-change 1 'day))
  13425. (t (call-interactively 'org-schedule)))
  13426. (call-interactively 'org-schedule)))
  13427. ((equal property "DEADLINE")
  13428. (if (re-search-forward org-deadline-time-regexp end t)
  13429. (cond
  13430. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13431. ((eq value 'later) (org-timestamp-change 1 'day))
  13432. (t (call-interactively 'org-deadline)))
  13433. (call-interactively 'org-deadline)))
  13434. ((member property org-special-properties)
  13435. (error "The %s property can not yet be set with `org-entry-put'"
  13436. property))
  13437. (t ; a non-special property
  13438. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13439. (setq range (org-get-property-block beg end 'force))
  13440. (goto-char (car range))
  13441. (if (re-search-forward
  13442. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  13443. (progn
  13444. (delete-region (match-beginning 1) (match-end 1))
  13445. (goto-char (match-beginning 1)))
  13446. (goto-char (cdr range))
  13447. (insert "\n")
  13448. (backward-char 1)
  13449. (org-indent-line-function)
  13450. (insert ":" property ":"))
  13451. (and value (insert " " value))
  13452. (org-indent-line-function)))))))
  13453. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  13454. "Get all property keys in the current buffer.
  13455. With INCLUDE-SPECIALS, also list the special properties that relect things
  13456. like tags and TODO state.
  13457. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13458. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  13459. (let (rtn range)
  13460. (save-excursion
  13461. (save-restriction
  13462. (widen)
  13463. (goto-char (point-min))
  13464. (while (re-search-forward org-property-start-re nil t)
  13465. (setq range (org-get-property-block))
  13466. (goto-char (car range))
  13467. (while (re-search-forward
  13468. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  13469. (cdr range) t)
  13470. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13471. (outline-next-heading))))
  13472. (when include-specials
  13473. (setq rtn (append org-special-properties rtn)))
  13474. (when include-defaults
  13475. (add-to-list 'rtn "CATEGORY")
  13476. (add-to-list 'rtn "ARCHIVE"))
  13477. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13478. (defun org-property-values (key)
  13479. "Return a list of all values of property KEY."
  13480. (save-excursion
  13481. (save-restriction
  13482. (widen)
  13483. (goto-char (point-min))
  13484. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  13485. values)
  13486. (while (re-search-forward re nil t)
  13487. (add-to-list 'values (org-trim (match-string 1))))
  13488. (delete "" values)))))
  13489. (defun org-insert-property-drawer ()
  13490. "Insert a property drawer into the current entry."
  13491. (interactive)
  13492. (org-back-to-heading t)
  13493. (looking-at outline-regexp)
  13494. (let ((indent (- (match-end 0)(match-beginning 0)))
  13495. (beg (point))
  13496. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13497. end hiddenp)
  13498. (outline-next-heading)
  13499. (setq end (point))
  13500. (goto-char beg)
  13501. (while (re-search-forward re end t))
  13502. (setq hiddenp (org-invisible-p))
  13503. (end-of-line 1)
  13504. (and (= (char-after) ?\n) (forward-char 1))
  13505. (org-skip-over-state-notes)
  13506. (skip-chars-backward " \t\n\r")
  13507. (insert "\n:PROPERTIES:\n:END:")
  13508. (beginning-of-line 0)
  13509. (indent-to-column indent)
  13510. (beginning-of-line 2)
  13511. (indent-to-column indent)
  13512. (beginning-of-line 0)
  13513. (if hiddenp
  13514. (save-excursion
  13515. (org-back-to-heading t)
  13516. (hide-entry))
  13517. (org-flag-drawer t))))
  13518. (defun org-set-property (property value)
  13519. "In the current entry, set PROPERTY to VALUE."
  13520. (interactive
  13521. (let* ((prop (completing-read "Property: "
  13522. (mapcar 'list (org-buffer-property-keys))))
  13523. (cur (org-entry-get nil prop))
  13524. (allowed (org-property-get-allowed-values nil prop 'table))
  13525. (val (if allowed
  13526. (completing-read "Value: " allowed nil 'req-match)
  13527. (read-string
  13528. (concat "Value" (if (and cur (string-match "\\S-" cur))
  13529. (concat "[" cur "]") "")
  13530. ": ")
  13531. "" cur))))
  13532. (list prop (if (equal val "") cur val))))
  13533. (unless (equal (org-entry-get nil property) value)
  13534. (org-entry-put nil property value)))
  13535. (defun org-delete-property (property)
  13536. "In the current entry, delete PROPERTY."
  13537. (interactive
  13538. (let* ((prop (completing-read
  13539. "Property: " (org-entry-properties nil 'standard))))
  13540. (list prop)))
  13541. (message (concat "Property " property
  13542. (if (org-entry-delete nil property)
  13543. " deleted"
  13544. " was not present in the entry"))))
  13545. (defun org-delete-property-globally (property)
  13546. "Remove PROPERTY globally, from all entries."
  13547. (interactive
  13548. (let* ((prop (completing-read
  13549. "Globally remove property: "
  13550. (mapcar 'list (org-buffer-property-keys)))))
  13551. (list prop)))
  13552. (save-excursion
  13553. (save-restriction
  13554. (widen)
  13555. (goto-char (point-min))
  13556. (let ((cnt 0))
  13557. (while (re-search-forward
  13558. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  13559. nil t)
  13560. (setq cnt (1+ cnt))
  13561. (replace-match ""))
  13562. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13563. (defvar org-columns-current-fmt-compiled) ; defined below
  13564. (defun org-compute-property-at-point ()
  13565. "Compute the property at point.
  13566. This looks for an enclosing column format, extracts the operator and
  13567. then applies it to the proerty in the column format's scope."
  13568. (interactive)
  13569. (unless (org-at-property-p)
  13570. (error "Not at a property"))
  13571. (let ((prop (org-match-string-no-properties 2)))
  13572. (org-columns-get-format-and-top-level)
  13573. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13574. (error "No operator defined for property %s" prop))
  13575. (org-columns-compute prop)))
  13576. (defun org-property-get-allowed-values (pom property &optional table)
  13577. "Get allowed values for the property PROPERTY.
  13578. When TABLE is non-nil, return an alist that can directly be used for
  13579. completion."
  13580. (let (vals)
  13581. (cond
  13582. ((equal property "TODO")
  13583. (setq vals (org-with-point-at pom
  13584. (append org-todo-keywords-1 '("")))))
  13585. ((equal property "PRIORITY")
  13586. (let ((n org-lowest-priority))
  13587. (while (>= n org-highest-priority)
  13588. (push (char-to-string n) vals)
  13589. (setq n (1- n)))))
  13590. ((member property org-special-properties))
  13591. (t
  13592. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13593. (when (and vals (string-match "\\S-" vals))
  13594. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13595. (setq vals (mapcar (lambda (x)
  13596. (cond ((stringp x) x)
  13597. ((numberp x) (number-to-string x))
  13598. ((symbolp x) (symbol-name x))
  13599. (t "???")))
  13600. vals)))))
  13601. (if table (mapcar 'list vals) vals)))
  13602. (defun org-property-previous-allowed-value (&optional previous)
  13603. "Switch to the next allowed value for this property."
  13604. (interactive)
  13605. (org-property-next-allowed-value t))
  13606. (defun org-property-next-allowed-value (&optional previous)
  13607. "Switch to the next allowed value for this property."
  13608. (interactive)
  13609. (unless (org-at-property-p)
  13610. (error "Not at a property"))
  13611. (let* ((key (match-string 2))
  13612. (value (match-string 3))
  13613. (allowed (or (org-property-get-allowed-values (point) key)
  13614. (and (member value '("[ ]" "[-]" "[X]"))
  13615. '("[ ]" "[X]"))))
  13616. nval)
  13617. (unless allowed
  13618. (error "Allowed values for this property have not been defined"))
  13619. (if previous (setq allowed (reverse allowed)))
  13620. (if (member value allowed)
  13621. (setq nval (car (cdr (member value allowed)))))
  13622. (setq nval (or nval (car allowed)))
  13623. (if (equal nval value)
  13624. (error "Only one allowed value for this property"))
  13625. (org-at-property-p)
  13626. (replace-match (concat " :" key ": " nval) t t)
  13627. (org-indent-line-function)
  13628. (beginning-of-line 1)
  13629. (skip-chars-forward " \t")))
  13630. ;;; Column View
  13631. (defvar org-columns-overlays nil
  13632. "Holds the list of current column overlays.")
  13633. (defvar org-columns-current-fmt nil
  13634. "Local variable, holds the currently active column format.")
  13635. (defvar org-columns-current-fmt-compiled nil
  13636. "Local variable, holds the currently active column format.
  13637. This is the compiled version of the format.")
  13638. (defvar org-columns-current-maxwidths nil
  13639. "Loval variable, holds the currently active maximum column widths.")
  13640. (defvar org-columns-begin-marker (make-marker)
  13641. "Points to the position where last a column creation command was called.")
  13642. (defvar org-columns-top-level-marker (make-marker)
  13643. "Points to the position where current columns region starts.")
  13644. (defvar org-columns-map (make-sparse-keymap)
  13645. "The keymap valid in column display.")
  13646. (defun org-columns-content ()
  13647. "Switch to contents view while in columns view."
  13648. (interactive)
  13649. (org-overview)
  13650. (org-content))
  13651. (org-defkey org-columns-map "c" 'org-columns-content)
  13652. (org-defkey org-columns-map "o" 'org-overview)
  13653. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  13654. (org-defkey org-columns-map "v" 'org-columns-show-value)
  13655. (org-defkey org-columns-map "q" 'org-columns-quit)
  13656. (org-defkey org-columns-map "r" 'org-columns-redo)
  13657. (org-defkey org-columns-map [left] 'backward-char)
  13658. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  13659. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  13660. (org-defkey org-columns-map [right] 'forward-char)
  13661. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  13662. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  13663. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-next-allowed-value)
  13664. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  13665. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  13666. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  13667. (org-defkey org-columns-map "<" 'org-columns-narrow)
  13668. (org-defkey org-columns-map ">" 'org-columns-widen)
  13669. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  13670. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  13671. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  13672. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  13673. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  13674. '("Column"
  13675. ["Edit property" org-columns-edit-value t]
  13676. ["Next allowed value" org-columns-next-allowed-value t]
  13677. ["Previous allowed value" org-columns-previous-allowed-value t]
  13678. ["Show full value" org-columns-show-value t]
  13679. ["Edit allowed" org-columns-edit-allowed t]
  13680. "--"
  13681. ["Edit column attributes" org-columns-edit-attributes t]
  13682. ["Increase column width" org-columns-widen t]
  13683. ["Decrease column width" org-columns-narrow t]
  13684. "--"
  13685. ["Move column right" org-columns-move-right t]
  13686. ["Move column left" org-columns-move-left t]
  13687. ["Add column" org-columns-new t]
  13688. ["Delete column" org-columns-delete t]
  13689. "--"
  13690. ["CONTENTS" org-columns-content t]
  13691. ["OVERVIEW" org-overview t]
  13692. ["Refresh columns display" org-columns-redo t]
  13693. "--"
  13694. ["Quit" org-columns-quit t]))
  13695. (defun org-columns-new-overlay (beg end &optional string face)
  13696. "Create a new column overlay and add it to the list."
  13697. (let ((ov (org-make-overlay beg end)))
  13698. (org-overlay-put ov 'face (or face 'secondary-selection))
  13699. (org-overlay-display ov string face)
  13700. (push ov org-columns-overlays)
  13701. ov))
  13702. (defun org-columns-display-here (&optional props)
  13703. "Overlay the current line with column display."
  13704. (interactive)
  13705. (let* ((fmt org-columns-current-fmt-compiled)
  13706. (beg (point-at-bol))
  13707. (level-face (save-excursion
  13708. (beginning-of-line 1)
  13709. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  13710. (org-get-level-face 2))))
  13711. (color (list :foreground
  13712. (face-attribute (or level-face 'default) :foreground)))
  13713. props pom property ass width f string ov column)
  13714. ;; Check if the entry is in another buffer.
  13715. (unless props
  13716. (if (eq major-mode 'org-agenda-mode)
  13717. (setq pom (or (get-text-property (point) 'org-hd-marker)
  13718. (get-text-property (point) 'org-marker))
  13719. props (if pom (org-entry-properties pom) nil))
  13720. (setq props (org-entry-properties nil))))
  13721. ;; Walk the format
  13722. (while (setq column (pop fmt))
  13723. (setq property (car column)
  13724. ass (if (equal property "ITEM")
  13725. (cons "ITEM"
  13726. (save-match-data
  13727. (org-no-properties
  13728. (org-remove-tabs
  13729. (buffer-substring-no-properties
  13730. (point-at-bol) (point-at-eol))))))
  13731. (assoc property props))
  13732. width (or (cdr (assoc property org-columns-current-maxwidths))
  13733. (nth 2 column))
  13734. f (format "%%-%d.%ds | " width width)
  13735. string (format f (or (cdr ass) "")))
  13736. ;; Create the overlay
  13737. (org-unmodified
  13738. (setq ov (org-columns-new-overlay
  13739. beg (setq beg (1+ beg)) string
  13740. (list color 'org-column)))
  13741. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  13742. (org-overlay-put ov 'keymap org-columns-map)
  13743. (org-overlay-put ov 'org-columns-key property)
  13744. (org-overlay-put ov 'org-columns-value (cdr ass))
  13745. (org-overlay-put ov 'org-columns-pom pom)
  13746. (org-overlay-put ov 'org-columns-format f))
  13747. (if (or (not (char-after beg))
  13748. (equal (char-after beg) ?\n))
  13749. (let ((inhibit-read-only t))
  13750. (save-excursion
  13751. (goto-char beg)
  13752. (insert " ")))))
  13753. ;; Make the rest of the line disappear.
  13754. (org-unmodified
  13755. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  13756. (org-overlay-put ov 'invisible t)
  13757. (org-overlay-put ov 'keymap org-columns-map)
  13758. (org-overlay-put ov 'intangible t)
  13759. (push ov org-columns-overlays)
  13760. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  13761. (org-overlay-put ov 'keymap org-columns-map)
  13762. (push ov org-columns-overlays)
  13763. (let ((inhibit-read-only t))
  13764. (put-text-property (max (point-min) (1- (point-at-bol)))
  13765. (min (point-max) (1+ (point-at-eol)))
  13766. 'read-only "Type `e' to edit property")))))
  13767. (defvar org-previous-header-line-format nil
  13768. "The header line format before column view was turned on.")
  13769. (defvar org-columns-inhibit-recalculation nil
  13770. "Inhibit recomputing of columns on column view startup.")
  13771. (defvar header-line-format)
  13772. (defun org-columns-display-here-title ()
  13773. "Overlay the newline before the current line with the table title."
  13774. (interactive)
  13775. (let ((fmt org-columns-current-fmt-compiled)
  13776. string (title "")
  13777. property width f column str)
  13778. (while (setq column (pop fmt))
  13779. (setq property (car column)
  13780. str (or (nth 1 column) property)
  13781. width (or (cdr (assoc property org-columns-current-maxwidths))
  13782. (nth 2 column))
  13783. f (format "%%-%d.%ds | " width width)
  13784. string (format f str)
  13785. title (concat title string)))
  13786. (setq title (concat
  13787. (org-add-props " " nil 'display '(space :align-to 0))
  13788. (org-add-props title nil 'face '(:weight bold :underline t))))
  13789. (org-set-local 'org-previous-header-line-format header-line-format)
  13790. (setq header-line-format title)))
  13791. (defun org-columns-remove-overlays ()
  13792. "Remove all currently active column overlays."
  13793. (interactive)
  13794. (when (marker-buffer org-columns-begin-marker)
  13795. (with-current-buffer (marker-buffer org-columns-begin-marker)
  13796. (when (local-variable-p 'org-previous-header-line-format)
  13797. (setq header-line-format org-previous-header-line-format)
  13798. (kill-local-variable 'org-previous-header-line-format))
  13799. (move-marker org-columns-begin-marker nil)
  13800. (move-marker org-columns-top-level-marker nil)
  13801. (org-unmodified
  13802. (mapc 'org-delete-overlay org-columns-overlays)
  13803. (setq org-columns-overlays nil)
  13804. (let ((inhibit-read-only t))
  13805. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  13806. (defun org-columns-show-value ()
  13807. "Show the full value of the property."
  13808. (interactive)
  13809. (let ((value (get-char-property (point) 'org-columns-value)))
  13810. (message "Value is: %s" (or value ""))))
  13811. (defun org-columns-quit ()
  13812. "Remove the column overlays and in this way exit column editing."
  13813. (interactive)
  13814. (org-unmodified
  13815. (org-columns-remove-overlays)
  13816. (let ((inhibit-read-only t))
  13817. (remove-text-properties (point-min) (point-max) '(read-only t))))
  13818. (when (eq major-mode 'org-agenda-mode)
  13819. (message
  13820. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  13821. (defun org-columns-check-computed ()
  13822. "Check if this column value is computed.
  13823. If yes, throw an error indicating that changing it does not make sense."
  13824. (let ((val (get-char-property (point) 'org-columns-value)))
  13825. (when (and (stringp val)
  13826. (get-char-property 0 'org-computed val))
  13827. (error "This value is computed from the entry's children"))))
  13828. (defun org-columns-edit-value ()
  13829. "Edit the value of the property at point in column view.
  13830. Where possible, use the standard interface for changing this line."
  13831. (interactive)
  13832. (org-columns-check-computed)
  13833. (let* ((col (current-column))
  13834. (key (get-char-property (point) 'org-columns-key))
  13835. (value (get-char-property (point) 'org-columns-value))
  13836. (bol (point-at-bol)) (eol (point-at-eol))
  13837. (pom (or (get-text-property bol 'org-hd-marker)
  13838. (point))) ; keep despite of compiler waring
  13839. (line-overlays
  13840. (delq nil (mapcar (lambda (x)
  13841. (and (eq (overlay-buffer x) (current-buffer))
  13842. (>= (overlay-start x) bol)
  13843. (<= (overlay-start x) eol)
  13844. x))
  13845. org-columns-overlays)))
  13846. nval eval allowed)
  13847. (when (equal key "ITEM")
  13848. (error "Cannot edit item headline from here"))
  13849. (cond
  13850. ((equal key "TODO")
  13851. (setq eval '(org-with-point-at pom
  13852. (let ((current-prefix-arg '(4))) (org-todo '(4))))))
  13853. ((equal key "PRIORITY")
  13854. (setq eval '(org-with-point-at pom
  13855. (call-interactively 'org-priority))))
  13856. ((equal key "TAGS")
  13857. (setq eval '(org-with-point-at pom
  13858. (let ((org-fast-tag-selection-single-key
  13859. (if (eq org-fast-tag-selection-single-key 'expert)
  13860. t org-fast-tag-selection-single-key)))
  13861. (call-interactively 'org-set-tags)))))
  13862. ((equal key "DEADLINE")
  13863. (setq eval '(org-with-point-at pom
  13864. (call-interactively 'org-deadline))))
  13865. ((equal key "SCHEDULED")
  13866. (setq eval '(org-with-point-at pom
  13867. (call-interactively 'org-schedule))))
  13868. (t
  13869. (setq allowed (org-property-get-allowed-values pom key 'table))
  13870. (if allowed
  13871. (setq nval (completing-read "Value: " allowed nil t))
  13872. (setq nval (read-string "Edit: " value)))
  13873. (setq nval (org-trim nval))
  13874. (when (not (equal nval value))
  13875. (setq eval '(org-entry-put pom key nval)))))
  13876. (when eval
  13877. (let ((inhibit-read-only t))
  13878. (remove-text-properties (1- bol) eol '(read-only t))
  13879. (unwind-protect
  13880. (progn
  13881. (setq org-columns-overlays
  13882. (org-delete-all line-overlays org-columns-overlays))
  13883. (mapc 'org-delete-overlay line-overlays)
  13884. (org-columns-eval eval))
  13885. (org-columns-display-here))))
  13886. (move-to-column col)
  13887. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13888. (org-columns-update key))))
  13889. (defun org-columns-edit-allowed ()
  13890. "Edit the list of allowed values for the current property."
  13891. (interactive)
  13892. (let* ((col (current-column))
  13893. (key (get-char-property (point) 'org-columns-key))
  13894. (key1 (concat key "_ALL"))
  13895. (value (get-char-property (point) 'org-columns-value))
  13896. (allowed (org-entry-get (point) key1 t))
  13897. nval)
  13898. (setq nval (read-string "Allowed: " allowed))
  13899. (org-entry-put
  13900. (cond ((marker-position org-entry-property-inherited-from)
  13901. org-entry-property-inherited-from)
  13902. ((marker-position org-columns-top-level-marker)
  13903. org-columns-top-level-marker))
  13904. key1 nval)))
  13905. (defun org-columns-eval (form)
  13906. (let (hidep)
  13907. (save-excursion
  13908. (beginning-of-line 1)
  13909. (next-line 1)
  13910. (setq hidep (org-on-heading-p 1)))
  13911. (eval form)
  13912. (and hidep (hide-entry))))
  13913. (defun org-columns-previous-allowed-value ()
  13914. "Switch to the previous allowed value for this column."
  13915. (interactive)
  13916. (org-columns-next-allowed-value t))
  13917. (defun org-columns-next-allowed-value (&optional previous)
  13918. "Switch to the next allowed value for this column."
  13919. (interactive)
  13920. (org-columns-check-computed)
  13921. (let* ((col (current-column))
  13922. (key (get-char-property (point) 'org-columns-key))
  13923. (value (get-char-property (point) 'org-columns-value))
  13924. (bol (point-at-bol)) (eol (point-at-eol))
  13925. (pom (or (get-text-property bol 'org-hd-marker)
  13926. (point))) ; keep despite of compiler waring
  13927. (line-overlays
  13928. (delq nil (mapcar (lambda (x)
  13929. (and (eq (overlay-buffer x) (current-buffer))
  13930. (>= (overlay-start x) bol)
  13931. (<= (overlay-start x) eol)
  13932. x))
  13933. org-columns-overlays)))
  13934. (allowed (or (org-property-get-allowed-values pom key)
  13935. (and (equal
  13936. (nth 4 (assoc key org-columns-current-fmt-compiled))
  13937. 'checkbox) '("[ ]" "[X]"))))
  13938. nval)
  13939. (when (equal key "ITEM")
  13940. (error "Cannot edit item headline from here"))
  13941. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  13942. (error "Allowed values for this property have not been defined"))
  13943. (if (member key '("SCHEDULED" "DEADLINE"))
  13944. (setq nval (if previous 'earlier 'later))
  13945. (if previous (setq allowed (reverse allowed)))
  13946. (if (member value allowed)
  13947. (setq nval (car (cdr (member value allowed)))))
  13948. (setq nval (or nval (car allowed)))
  13949. (if (equal nval value)
  13950. (error "Only one allowed value for this property")))
  13951. (let ((inhibit-read-only t))
  13952. (remove-text-properties (1- bol) eol '(read-only t))
  13953. (unwind-protect
  13954. (progn
  13955. (setq org-columns-overlays
  13956. (org-delete-all line-overlays org-columns-overlays))
  13957. (mapc 'org-delete-overlay line-overlays)
  13958. (org-columns-eval '(org-entry-put pom key nval)))
  13959. (org-columns-display-here)))
  13960. (move-to-column col)
  13961. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  13962. (org-columns-update key))))
  13963. (defun org-verify-version (task)
  13964. (cond
  13965. ((eq task 'columns)
  13966. (if (or (featurep 'xemacs)
  13967. (< emacs-major-version 22))
  13968. (error "Emacs 22 is required for the columns feature")))))
  13969. (defun org-columns-get-format-and-top-level ()
  13970. (let (fmt)
  13971. (when (condition-case nil (org-back-to-heading) (error nil))
  13972. (move-marker org-entry-property-inherited-from nil)
  13973. (setq fmt (org-entry-get nil "COLUMNS" t)))
  13974. (setq fmt (or fmt org-columns-default-format))
  13975. (org-set-local 'org-columns-current-fmt fmt)
  13976. (org-columns-compile-format fmt)
  13977. (if (marker-position org-entry-property-inherited-from)
  13978. (move-marker org-columns-top-level-marker
  13979. org-entry-property-inherited-from)
  13980. (move-marker org-columns-top-level-marker (point)))
  13981. fmt))
  13982. (defun org-columns ()
  13983. "Turn on column view on an org-mode file."
  13984. (interactive)
  13985. (org-verify-version 'columns)
  13986. (org-columns-remove-overlays)
  13987. (move-marker org-columns-begin-marker (point))
  13988. (let (beg end fmt cache maxwidths)
  13989. (setq fmt (org-columns-get-format-and-top-level))
  13990. (save-excursion
  13991. (goto-char org-columns-top-level-marker)
  13992. (setq beg (point))
  13993. (unless org-columns-inhibit-recalculation
  13994. (org-columns-compute-all))
  13995. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  13996. (point-max)))
  13997. (goto-char beg)
  13998. ;; Get and cache the properties
  13999. (while (re-search-forward (concat "^" outline-regexp) end t)
  14000. (push (cons (org-current-line) (org-entry-properties)) cache))
  14001. (when cache
  14002. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  14003. (org-set-local 'org-columns-current-maxwidths maxwidths)
  14004. (org-columns-display-here-title)
  14005. (mapc (lambda (x)
  14006. (goto-line (car x))
  14007. (org-columns-display-here (cdr x)))
  14008. cache)))))
  14009. (defun org-columns-new (&optional prop title width op fmt)
  14010. "Insert a new column, to the leeft o the current column."
  14011. (interactive)
  14012. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  14013. cell)
  14014. (setq prop (completing-read
  14015. "Property: " (mapcar 'list (org-buffer-property-keys t))
  14016. nil nil prop))
  14017. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  14018. (setq width (read-string "Column width: " (if width (number-to-string width))))
  14019. (if (string-match "\\S-" width)
  14020. (setq width (string-to-number width))
  14021. (setq width nil))
  14022. (setq fmt (completing-read "Summary [none]: "
  14023. '(("none") ("add_numbers") ("add_times") ("checkbox"))
  14024. nil t))
  14025. (if (string-match "\\S-" fmt)
  14026. (setq fmt (intern fmt))
  14027. (setq fmt nil))
  14028. (if (eq fmt 'none) (setq fmt nil))
  14029. (if editp
  14030. (progn
  14031. (setcar editp prop)
  14032. (setcdr editp (list title width nil fmt)))
  14033. (setq cell (nthcdr (1- (current-column))
  14034. org-columns-current-fmt-compiled))
  14035. (setcdr cell (cons (list prop title width nil fmt)
  14036. (cdr cell))))
  14037. (org-columns-store-format)
  14038. (org-columns-redo)))
  14039. (defun org-columns-delete ()
  14040. "Delete the column at point from columns view."
  14041. (interactive)
  14042. (let* ((n (current-column))
  14043. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  14044. (when (y-or-n-p
  14045. (format "Are you sure you want to remove column \"%s\"? " title))
  14046. (setq org-columns-current-fmt-compiled
  14047. (delq (nth n org-columns-current-fmt-compiled)
  14048. org-columns-current-fmt-compiled))
  14049. (org-columns-store-format)
  14050. (org-columns-redo)
  14051. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  14052. (backward-char 1)))))
  14053. (defun org-columns-edit-attributes ()
  14054. "Edit the attributes of the current column."
  14055. (interactive)
  14056. (let* ((n (current-column))
  14057. (info (nth n org-columns-current-fmt-compiled)))
  14058. (apply 'org-columns-new info)))
  14059. (defun org-columns-widen (arg)
  14060. "Make the column wider by ARG characters."
  14061. (interactive "p")
  14062. (let* ((n (current-column))
  14063. (entry (nth n org-columns-current-fmt-compiled))
  14064. (width (or (nth 2 entry)
  14065. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  14066. (setq width (max 1 (+ width arg)))
  14067. (setcar (nthcdr 2 entry) width)
  14068. (org-columns-store-format)
  14069. (org-columns-redo)))
  14070. (defun org-columns-narrow (arg)
  14071. "Make the column nrrower by ARG characters."
  14072. (interactive "p")
  14073. (org-columns-widen (- arg)))
  14074. (defun org-columns-move-right ()
  14075. "Swap this column with the one to the right."
  14076. (interactive)
  14077. (let* ((n (current-column))
  14078. (cell (nthcdr n org-columns-current-fmt-compiled))
  14079. e)
  14080. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  14081. (error "Cannot shift this column further to the right"))
  14082. (setq e (car cell))
  14083. (setcar cell (car (cdr cell)))
  14084. (setcdr cell (cons e (cdr (cdr cell))))
  14085. (org-columns-store-format)
  14086. (org-columns-redo)
  14087. (forward-char 1)))
  14088. (defun org-columns-move-left ()
  14089. "Swap this column with the one to the left."
  14090. (interactive)
  14091. (let* ((n (current-column)))
  14092. (when (= n 0)
  14093. (error "Cannot shift this column further to the left"))
  14094. (backward-char 1)
  14095. (org-columns-move-right)
  14096. (backward-char 1)))
  14097. (defun org-columns-store-format ()
  14098. "Store the text version of the current columns format in appropriate place.
  14099. This is either in the COLUMNS property of the node starting the current column
  14100. display, or in the #+COLUMNS line of the current buffer."
  14101. (let (fmt)
  14102. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  14103. (if (marker-position org-columns-top-level-marker)
  14104. (save-excursion
  14105. (goto-char org-columns-top-level-marker)
  14106. (if (org-entry-get nil "COLUMNS")
  14107. (org-entry-put nil "COLUMNS" fmt)
  14108. (goto-char (point-min))
  14109. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  14110. (replace-match (concat "#+COLUMNS: " fmt t t)))))
  14111. (setq org-columns-current-fmt fmt))))
  14112. (defvar org-overriding-columns-format nil
  14113. "When set, overrides any other definition.")
  14114. (defvar org-agenda-view-columns-initially nil
  14115. "When set, switch to columns view immediately after creating the agenda.")
  14116. (defun org-agenda-columns ()
  14117. "Turn on column view in the agenda."
  14118. (interactive)
  14119. (org-verify-version 'columns)
  14120. (org-columns-remove-overlays)
  14121. (move-marker org-columns-begin-marker (point))
  14122. (let (fmt cache maxwidths m)
  14123. (cond
  14124. ((and (local-variable-p 'org-overriding-columns-format)
  14125. org-overriding-columns-format)
  14126. (setq fmt org-overriding-columns-format))
  14127. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  14128. (setq fmt (org-entry-get m "COLUMNS" t)))
  14129. ((and (boundp 'org-columns-current-fmt)
  14130. (local-variable-p 'org-columns-current-fmt)
  14131. org-columns-current-fmt)
  14132. (setq fmt org-columns-current-fmt))
  14133. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  14134. (setq m (get-text-property m 'org-hd-marker))
  14135. (setq fmt (org-entry-get m "COLUMNS" t))))
  14136. (setq fmt (or fmt org-columns-default-format))
  14137. (org-set-local 'org-columns-current-fmt fmt)
  14138. (org-columns-compile-format fmt)
  14139. (save-excursion
  14140. ;; Get and cache the properties
  14141. (goto-char (point-min))
  14142. (while (not (eobp))
  14143. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  14144. (get-text-property (point) 'org-marker)))
  14145. (push (cons (org-current-line) (org-entry-properties m)) cache))
  14146. (beginning-of-line 2))
  14147. (when cache
  14148. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  14149. (org-set-local 'org-columns-current-maxwidths maxwidths)
  14150. (org-columns-display-here-title)
  14151. (mapc (lambda (x)
  14152. (goto-line (car x))
  14153. (org-columns-display-here (cdr x)))
  14154. cache)))))
  14155. (defun org-columns-get-autowidth-alist (s cache)
  14156. "Derive the maximum column widths from the format and the cache."
  14157. (let ((start 0) rtn)
  14158. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  14159. (push (cons (match-string 1 s) 1) rtn)
  14160. (setq start (match-end 0)))
  14161. (mapc (lambda (x)
  14162. (setcdr x (apply 'max
  14163. (mapcar
  14164. (lambda (y)
  14165. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  14166. cache))))
  14167. rtn)
  14168. rtn))
  14169. (defun org-columns-compute-all ()
  14170. "Compute all columns that have operators defined."
  14171. (org-unmodified
  14172. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  14173. (let ((columns org-columns-current-fmt-compiled) col)
  14174. (while (setq col (pop columns))
  14175. (when (nth 3 col)
  14176. (save-excursion
  14177. (org-columns-compute (car col)))))))
  14178. (defun org-columns-update (property)
  14179. "Recompute PROPERTY, and update the columns display for it."
  14180. (org-columns-compute property)
  14181. (let (fmt val pos)
  14182. (save-excursion
  14183. (mapc (lambda (ov)
  14184. (when (equal (org-overlay-get ov 'org-columns-key) property)
  14185. (setq pos (org-overlay-start ov))
  14186. (goto-char pos)
  14187. (when (setq val (cdr (assoc property
  14188. (get-text-property
  14189. (point-at-bol) 'org-summaries))))
  14190. (setq fmt (org-overlay-get ov 'org-columns-format))
  14191. (org-overlay-put ov 'org-columns-value val)
  14192. (org-overlay-put ov 'display (format fmt val)))))
  14193. org-columns-overlays))))
  14194. (defun org-columns-compute (property)
  14195. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  14196. (interactive)
  14197. (let* ((re (concat "^" outline-regexp))
  14198. (lmax 30) ; Does anyone use deeper levels???
  14199. (lsum (make-vector lmax 0))
  14200. (lflag (make-vector lmax nil))
  14201. (level 0)
  14202. (ass (assoc property org-columns-current-fmt-compiled))
  14203. (format (nth 4 ass))
  14204. (beg org-columns-top-level-marker)
  14205. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  14206. (save-excursion
  14207. ;; Find the region to compute
  14208. (goto-char beg)
  14209. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  14210. (goto-char end)
  14211. ;; Walk the tree from the back and do the computations
  14212. (while (re-search-backward re beg t)
  14213. (setq sumpos (match-beginning 0)
  14214. last-level level
  14215. level (org-outline-level)
  14216. val (org-entry-get nil property)
  14217. valflag (and val (string-match "\\S-" val)))
  14218. (cond
  14219. ((< level last-level)
  14220. ;; put the sum of lower levels here as a property
  14221. (setq sum (aref lsum last-level) ; current sum
  14222. flag (aref lflag last-level) ; any valid entries from children?
  14223. str (org-column-number-to-string sum format)
  14224. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  14225. useval (if flag str1 (if valflag val ""))
  14226. sum-alist (get-text-property sumpos 'org-summaries))
  14227. (if (assoc property sum-alist)
  14228. (setcdr (assoc property sum-alist) useval)
  14229. (push (cons property useval) sum-alist)
  14230. (org-unmodified
  14231. (add-text-properties sumpos (1+ sumpos)
  14232. (list 'org-summaries sum-alist))))
  14233. (when val
  14234. (org-entry-put nil property (if flag str val)))
  14235. ;; add current to current level accumulator
  14236. (when (or flag valflag)
  14237. ;; FIXME: is this ok?????????
  14238. (aset lsum level (+ (aref lsum level)
  14239. (if flag sum (org-column-string-to-number
  14240. (if flag str val) format))))
  14241. (aset lflag level t))
  14242. ;; clear accumulators for deeper levels
  14243. (loop for l from (1+ level) to (1- lmax) do
  14244. (aset lsum l 0)
  14245. (aset lflag l nil)))
  14246. ((>= level last-level)
  14247. ;; add what we have here to the accumulator for this level
  14248. (aset lsum level (+ (aref lsum level)
  14249. (org-column-string-to-number (or val "0") format)))
  14250. (and valflag (aset lflag level t)))
  14251. (t (error "This should not happen")))))))
  14252. (defun org-columns-redo ()
  14253. "Construct the column display again."
  14254. (interactive)
  14255. (message "Recomputing columns...")
  14256. (save-excursion
  14257. (if (marker-position org-columns-begin-marker)
  14258. (goto-char org-columns-begin-marker))
  14259. (org-columns-remove-overlays)
  14260. (if (org-mode-p)
  14261. (call-interactively 'org-columns)
  14262. (call-interactively 'org-agenda-columns)))
  14263. (message "Recomputing columns...done"))
  14264. (defun org-columns-not-in-agenda ()
  14265. (if (eq major-mode 'org-agenda-mode)
  14266. (error "This command is only allowed in Org-mode buffers")))
  14267. (defun org-string-to-number (s)
  14268. "Convert string to number, and interpret hh:mm:ss."
  14269. (if (not (string-match ":" s))
  14270. (string-to-number s)
  14271. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14272. (while l
  14273. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14274. sum)))
  14275. (defun org-column-number-to-string (n fmt)
  14276. "Convert a computed column number to a string value, according to FMT."
  14277. (cond
  14278. ((eq fmt 'add_times)
  14279. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  14280. (format "%d:%02d" h m)))
  14281. ((eq fmt 'checkbox)
  14282. (cond ((= n (floor n)) "[X]")
  14283. ((> n 1.) "[-]")
  14284. (t "[ ]")))
  14285. (t (number-to-string n))))
  14286. (defun org-column-string-to-number (s fmt)
  14287. "Convert a column value to a number that can be used for column computing."
  14288. (cond
  14289. ((string-match ":" s)
  14290. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14291. (while l
  14292. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14293. sum))
  14294. ((eq fmt 'checkbox)
  14295. (if (equal s "[X]") 1. 0.000001))
  14296. (t (string-to-number s))))
  14297. (defun org-columns-uncompile-format (cfmt)
  14298. "Turn the compiled columns format back into a string representation."
  14299. (let ((rtn "") e s prop title op width fmt)
  14300. (while (setq e (pop cfmt))
  14301. (setq prop (car e)
  14302. title (nth 1 e)
  14303. width (nth 2 e)
  14304. op (nth 3 e)
  14305. fmt (nth 4 e))
  14306. (cond
  14307. ((eq fmt 'add_times) (setq op ":"))
  14308. ((eq fmt 'checkbox) (setq op "X"))
  14309. ((eq fmt 'add_numbers) (setq op "+")))
  14310. (if (equal title prop) (setq title nil))
  14311. (setq s (concat "%" (if width (number-to-string width))
  14312. prop
  14313. (if title (concat "(" title ")"))
  14314. (if op (concat "{" op "}"))))
  14315. (setq rtn (concat rtn " " s)))
  14316. (org-trim rtn)))
  14317. (defun org-columns-compile-format (fmt)
  14318. "Turn a column format string into an alist of specifications.
  14319. The alist has one entry for each column in the format. The elements of
  14320. that list are:
  14321. property the property
  14322. title the title field for the columns
  14323. width the column width in characters, can be nil for automatic
  14324. operator the operator if any
  14325. format the output format for computed results, derived from operator"
  14326. (let ((start 0) width prop title op f)
  14327. (setq org-columns-current-fmt-compiled nil)
  14328. (while (string-match
  14329. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  14330. fmt start)
  14331. (setq start (match-end 0)
  14332. width (match-string 1 fmt)
  14333. prop (match-string 2 fmt)
  14334. title (or (match-string 3 fmt) prop)
  14335. op (match-string 4 fmt)
  14336. f nil)
  14337. (if width (setq width (string-to-number width)))
  14338. (cond
  14339. ((equal op "+") (setq f 'add_numbers))
  14340. ((equal op ":") (setq f 'add_times))
  14341. ((equal op "X") (setq f 'checkbox)))
  14342. (push (list prop title width op f) org-columns-current-fmt-compiled))
  14343. (setq org-columns-current-fmt-compiled
  14344. (nreverse org-columns-current-fmt-compiled))))
  14345. ;;;; Timestamps
  14346. (defvar org-last-changed-timestamp nil)
  14347. (defvar org-time-was-given) ; dynamically scoped parameter
  14348. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14349. (defvar org-ts-what) ; dynamically scoped parameter
  14350. (defun org-time-stamp (arg)
  14351. "Prompt for a date/time and insert a time stamp.
  14352. If the user specifies a time like HH:MM, or if this command is called
  14353. with a prefix argument, the time stamp will contain date and time.
  14354. Otherwise, only the date will be included. All parts of a date not
  14355. specified by the user will be filled in from the current date/time.
  14356. So if you press just return without typing anything, the time stamp
  14357. will represent the current date/time. If there is already a timestamp
  14358. at the cursor, it will be modified."
  14359. (interactive "P")
  14360. (let ((default-time
  14361. ;; Default time is either today, or, when entering a range,
  14362. ;; the range start.
  14363. (if (or (org-at-timestamp-p t)
  14364. (save-excursion
  14365. (re-search-backward
  14366. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14367. (- (point) 20) t)))
  14368. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14369. (current-time)))
  14370. org-time-was-given org-end-time-was-given time)
  14371. (cond
  14372. ((and (org-at-timestamp-p)
  14373. (eq last-command 'org-time-stamp)
  14374. (eq this-command 'org-time-stamp))
  14375. (insert "--")
  14376. (setq time (let ((this-command this-command))
  14377. (org-read-date arg 'totime nil nil default-time)))
  14378. (org-insert-time-stamp time (or org-time-was-given arg)))
  14379. ((org-at-timestamp-p)
  14380. (setq time (let ((this-command this-command))
  14381. (org-read-date arg 'totime nil nil default-time)))
  14382. (when (org-at-timestamp-p) ; just to get the match data
  14383. (replace-match "")
  14384. (setq org-last-changed-timestamp
  14385. (org-insert-time-stamp
  14386. time (or org-time-was-given arg)
  14387. nil nil nil (list org-end-time-was-given))))
  14388. (message "Timestamp updated"))
  14389. (t
  14390. (setq time (let ((this-command this-command))
  14391. (org-read-date arg 'totime nil nil default-time)))
  14392. (org-insert-time-stamp time (or org-time-was-given arg)
  14393. nil nil nil (list org-end-time-was-given))))))
  14394. (defun org-time-stamp-inactive (&optional arg)
  14395. "Insert an inactive time stamp.
  14396. An inactive time stamp is enclosed in square brackets instead of angle
  14397. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14398. does not link to the calendar and cannot be changed with the S-cursor keys.
  14399. So these are more for recording a certain time/date."
  14400. (interactive "P")
  14401. (let (org-time-was-given org-end-time-was-given time)
  14402. (setq time (org-read-date arg 'totime))
  14403. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  14404. nil nil (list org-end-time-was-given))))
  14405. (defvar org-date-ovl (org-make-overlay 1 1))
  14406. (org-overlay-put org-date-ovl 'face 'org-warning)
  14407. (org-detach-overlay org-date-ovl)
  14408. (defvar org-ans1) ; dynamically scoped parameter
  14409. (defvar org-ans2) ; dynamically scoped parameter
  14410. (defvar org-plain-time-of-day-regexp) ; defined below
  14411. (defun org-read-date (&optional with-time to-time from-string prompt
  14412. default-time)
  14413. "Read a date and make things smooth for the user.
  14414. The prompt will suggest to enter an ISO date, but you can also enter anything
  14415. which will at least partially be understood by `parse-time-string'.
  14416. Unrecognized parts of the date will default to the current day, month, year,
  14417. hour and minute. If this command is called to replace a timestamp at point,
  14418. of to enter the second timestamp of a range, the default time is taken from the
  14419. existing stamp. For example,
  14420. 3-2-5 --> 2003-02-05
  14421. feb 15 --> currentyear-02-15
  14422. sep 12 9 --> 2009-09-12
  14423. 12:45 --> today 12:45
  14424. 22 sept 0:34 --> currentyear-09-22 0:34
  14425. 12 --> currentyear-currentmonth-12
  14426. Fri --> nearest Friday (today or later)
  14427. etc.
  14428. Furthermore you can specify a relative date by giving, as the *first* thing
  14429. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  14430. change in days weeks, months, years.
  14431. With a single plus or minus, the date is relative to today. With a double
  14432. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14433. +4d --> four days from today
  14434. +4 --> same as above
  14435. +2w --> two weeks from today
  14436. ++5 --> five days from default date
  14437. The function understands only English month and weekday abbreviations,
  14438. but this can be configured with the variables `parse-time-months' and
  14439. `parse-time-weekdays'.
  14440. While prompting, a calendar is popped up - you can also select the
  14441. date with the mouse (button 1). The calendar shows a period of three
  14442. months. To scroll it to other months, use the keys `>' and `<'.
  14443. If you don't like the calendar, turn it off with
  14444. \(setq org-popup-calendar-for-date-prompt nil)
  14445. With optional argument TO-TIME, the date will immediately be converted
  14446. to an internal time.
  14447. With an optional argument WITH-TIME, the prompt will suggest to also
  14448. insert a time. Note that when WITH-TIME is not set, you can still
  14449. enter a time, and this function will inform the calling routine about
  14450. this change. The calling routine may then choose to change the format
  14451. used to insert the time stamp into the buffer to include the time.
  14452. With optional argument FROM-STRING, read fomr this string instead from
  14453. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14454. the time/date that is used for everything that is not specified by the
  14455. user."
  14456. (require 'parse-time)
  14457. (let* ((org-time-stamp-rounding-minutes
  14458. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  14459. (ct (org-current-time))
  14460. (def (or default-time ct))
  14461. (calendar-move-hook nil)
  14462. (view-diary-entries-initially nil)
  14463. (view-calendar-holidays-initially nil)
  14464. (timestr (format-time-string
  14465. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  14466. (prompt (concat (if prompt (concat prompt " ") "")
  14467. (format "Date and/or time (default [%s]): " timestr)))
  14468. ans (org-ans0 "") org-ans1 org-ans2 deltan deltaw deltarel
  14469. second minute hour day month year tl wday wday1 pm h2 m2)
  14470. (cond
  14471. (from-string (setq ans from-string))
  14472. (org-popup-calendar-for-date-prompt
  14473. (save-excursion
  14474. (save-window-excursion
  14475. (calendar)
  14476. (calendar-forward-day (- (time-to-days def)
  14477. (calendar-absolute-from-gregorian
  14478. (calendar-current-date))))
  14479. (org-eval-in-calendar nil t)
  14480. (let* ((old-map (current-local-map))
  14481. (map (copy-keymap calendar-mode-map))
  14482. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14483. (org-defkey map (kbd "RET") 'org-calendar-select)
  14484. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  14485. 'org-calendar-select-mouse)
  14486. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  14487. 'org-calendar-select-mouse)
  14488. (org-defkey minibuffer-local-map [(meta shift left)]
  14489. (lambda () (interactive)
  14490. (org-eval-in-calendar '(calendar-backward-month 1))))
  14491. (org-defkey minibuffer-local-map [(meta shift right)]
  14492. (lambda () (interactive)
  14493. (org-eval-in-calendar '(calendar-forward-month 1))))
  14494. (org-defkey minibuffer-local-map [(shift up)]
  14495. (lambda () (interactive)
  14496. (org-eval-in-calendar '(calendar-backward-week 1))))
  14497. (org-defkey minibuffer-local-map [(shift down)]
  14498. (lambda () (interactive)
  14499. (org-eval-in-calendar '(calendar-forward-week 1))))
  14500. (org-defkey minibuffer-local-map [(shift left)]
  14501. (lambda () (interactive)
  14502. (org-eval-in-calendar '(calendar-backward-day 1))))
  14503. (org-defkey minibuffer-local-map [(shift right)]
  14504. (lambda () (interactive)
  14505. (org-eval-in-calendar '(calendar-forward-day 1))))
  14506. (org-defkey minibuffer-local-map ">"
  14507. (lambda () (interactive)
  14508. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14509. (org-defkey minibuffer-local-map "<"
  14510. (lambda () (interactive)
  14511. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14512. (unwind-protect
  14513. (progn
  14514. (use-local-map map)
  14515. (setq org-ans0 (read-string prompt "" nil nil))
  14516. ;; org-ans0: from prompt
  14517. ;; org-ans1: from mouse click
  14518. ;; org-ans2: from calendar motion
  14519. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14520. (use-local-map old-map))))))
  14521. (t ; Naked prompt only
  14522. (setq ans (read-string prompt "" nil timestr))))
  14523. (org-detach-overlay org-date-ovl)
  14524. (when (string-match
  14525. "\\`[ \t]*\\([-+]\\)?\\([-+][0-9]+\\)\\([dwmy]?\\)\\([ \t\n]\\|$\\)"
  14526. org-ans0)
  14527. (setq deltan (string-to-number (match-string 2 ans))
  14528. deltaw (match-string 3 ans)
  14529. deltarel (match-end 1)))
  14530. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  14531. (when (string-match
  14532. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14533. (setq year (if (match-end 2)
  14534. (string-to-number (match-string 2 ans))
  14535. (string-to-number (format-time-string "%Y")))
  14536. month (string-to-number (match-string 3 ans))
  14537. day (string-to-number (match-string 4 ans)))
  14538. (if (< year 100) (setq year (+ 2000 year)))
  14539. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14540. t nil ans)))
  14541. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14542. ;; If there is a time with am/pm, and *no* time without it, we convert
  14543. ;; so that matching will be successful.
  14544. (loop for i from 1 to 2 do ; twice, for end time as well
  14545. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14546. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14547. (setq hour (string-to-number (match-string 1 ans))
  14548. minute (if (match-end 3)
  14549. (string-to-number (match-string 3 ans))
  14550. 0)
  14551. pm (equal ?p
  14552. (string-to-char (downcase (match-string 4 ans)))))
  14553. (if (and (= hour 12) (not pm))
  14554. (setq hour 0)
  14555. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14556. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14557. t t ans))))
  14558. ;; Check if a time range is given as a duration
  14559. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14560. (setq hour (string-to-number (match-string 1 ans))
  14561. h2 (+ hour (string-to-number (match-string 3 ans)))
  14562. minute (string-to-number (match-string 2 ans))
  14563. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  14564. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  14565. ;; Check if there is a time range
  14566. (when (and (boundp 'org-end-time-was-given)
  14567. (string-match org-plain-time-of-day-regexp ans)
  14568. (match-end 8))
  14569. (setq org-end-time-was-given (match-string 8 ans))
  14570. (setq ans (concat (substring ans 0 (match-beginning 7))
  14571. (substring ans (match-end 7)))))
  14572. (setq tl (parse-time-string ans)
  14573. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" def)))
  14574. month (or (nth 4 tl) (string-to-number (format-time-string "%m" def)))
  14575. day (or (nth 3 tl) (string-to-number (format-time-string "%d" def)))
  14576. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" def)))
  14577. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" def)))
  14578. second (or (nth 0 tl) 0)
  14579. wday (nth 6 tl))
  14580. (when deltan
  14581. (unless deltarel
  14582. (let ((now (decode-time (current-time))))
  14583. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14584. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14585. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14586. ((equal deltaw "m") (setq month (+ month deltan)))
  14587. ((equal deltaw "y") (setq year (+ year deltan)))))
  14588. (when (and wday (not (nth 3 tl)))
  14589. ;; Weekday was given, but no day, so pick that day in the week
  14590. ;; on or after the derived date.
  14591. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14592. (unless (equal wday wday1)
  14593. (setq day (+ day (% (- wday wday1 -7) 7)))))
  14594. (if (and (boundp 'org-time-was-given)
  14595. (nth 2 tl))
  14596. (setq org-time-was-given t))
  14597. (if (< year 100) (setq year (+ 2000 year)))
  14598. (if to-time
  14599. (encode-time second minute hour day month year)
  14600. (if (or (nth 1 tl) (nth 2 tl))
  14601. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  14602. (format "%04d-%02d-%02d" year month day)))))
  14603. (defun org-eval-in-calendar (form &optional keepdate)
  14604. "Eval FORM in the calendar window and return to current window.
  14605. Also, store the cursor date in variable org-ans2."
  14606. (let ((sw (selected-window)))
  14607. (select-window (get-buffer-window "*Calendar*"))
  14608. (eval form)
  14609. (when (and (not keepdate) (calendar-cursor-to-date))
  14610. (let* ((date (calendar-cursor-to-date))
  14611. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14612. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14613. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14614. (select-window sw)
  14615. ;; Update the prompt to show new default date
  14616. (save-excursion
  14617. (goto-char (point-min))
  14618. (when (and org-ans2
  14619. (re-search-forward "\\[[-0-9]+\\]" nil t)
  14620. (get-text-property (match-end 0) 'field))
  14621. (let ((inhibit-read-only t))
  14622. (replace-match (concat "[" org-ans2 "]") t t)
  14623. (add-text-properties (point-min) (1+ (match-end 0))
  14624. (text-properties-at (1+ (point-min)))))))))
  14625. (defun org-calendar-select ()
  14626. "Return to `org-read-date' with the date currently selected.
  14627. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14628. (interactive)
  14629. (when (calendar-cursor-to-date)
  14630. (let* ((date (calendar-cursor-to-date))
  14631. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14632. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14633. (if (active-minibuffer-window) (exit-minibuffer))))
  14634. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14635. "Insert a date stamp for the date given by the internal TIME.
  14636. WITH-HM means, use the stamp format that includes the time of the day.
  14637. INACTIVE means use square brackets instead of angular ones, so that the
  14638. stamp will not contribute to the agenda.
  14639. PRE and POST are optional strings to be inserted before and after the
  14640. stamp.
  14641. The command returns the inserted time stamp."
  14642. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14643. stamp)
  14644. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14645. (insert-before-markers (or pre ""))
  14646. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14647. (when (listp extra)
  14648. (setq extra (car extra))
  14649. (if (and (stringp extra)
  14650. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14651. (setq extra (format "-%02d:%02d"
  14652. (string-to-number (match-string 1 extra))
  14653. (string-to-number (match-string 2 extra))))
  14654. (setq extra nil)))
  14655. (when extra
  14656. (backward-char 1)
  14657. (insert-before-markers extra)
  14658. (forward-char 1))
  14659. (insert-before-markers (or post ""))
  14660. stamp))
  14661. (defun org-toggle-time-stamp-overlays ()
  14662. "Toggle the use of custom time stamp formats."
  14663. (interactive)
  14664. (setq org-display-custom-times (not org-display-custom-times))
  14665. (unless org-display-custom-times
  14666. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14667. (while (setq p (next-single-property-change p 'display))
  14668. (if (and (get-text-property p 'display)
  14669. (eq (get-text-property p 'face) 'org-date))
  14670. (remove-text-properties
  14671. p (setq p (next-single-property-change p 'display))
  14672. '(display t))))
  14673. (set-buffer-modified-p bmp)))
  14674. (if (featurep 'xemacs)
  14675. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14676. (org-restart-font-lock)
  14677. (setq org-table-may-need-update t)
  14678. (if org-display-custom-times
  14679. (message "Time stamps are overlayed with custom format")
  14680. (message "Time stamp overlays removed")))
  14681. (defun org-display-custom-time (beg end)
  14682. "Overlay modified time stamp format over timestamp between BED and END."
  14683. (let* ((ts (buffer-substring beg end))
  14684. t1 w1 with-hm tf time str w2 (off 0))
  14685. (save-match-data
  14686. (setq t1 (org-parse-time-string ts t))
  14687. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  14688. (setq off (- (match-end 0) (match-beginning 0)))))
  14689. (setq end (- end off))
  14690. (setq w1 (- end beg)
  14691. with-hm (and (nth 1 t1) (nth 2 t1))
  14692. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14693. time (org-fix-decoded-time t1)
  14694. str (org-add-props
  14695. (format-time-string
  14696. (substring tf 1 -1) (apply 'encode-time time))
  14697. nil 'mouse-face 'highlight)
  14698. w2 (length str))
  14699. (if (not (= w2 w1))
  14700. (add-text-properties (1+ beg) (+ 2 beg)
  14701. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14702. (if (featurep 'xemacs)
  14703. (progn
  14704. (put-text-property beg end 'invisible t)
  14705. (put-text-property beg end 'end-glyph (make-glyph str)))
  14706. (put-text-property beg end 'display str))))
  14707. (defun org-translate-time (string)
  14708. "Translate all timestamps in STRING to custom format.
  14709. But do this only if the variable `org-display-custom-times' is set."
  14710. (when org-display-custom-times
  14711. (save-match-data
  14712. (let* ((start 0)
  14713. (re org-ts-regexp-both)
  14714. t1 with-hm inactive tf time str beg end)
  14715. (while (setq start (string-match re string start))
  14716. (setq beg (match-beginning 0)
  14717. end (match-end 0)
  14718. t1 (save-match-data
  14719. (org-parse-time-string (substring string beg end) t))
  14720. with-hm (and (nth 1 t1) (nth 2 t1))
  14721. inactive (equal (substring string beg (1+ beg)) "[")
  14722. tf (funcall (if with-hm 'cdr 'car)
  14723. org-time-stamp-custom-formats)
  14724. time (org-fix-decoded-time t1)
  14725. str (format-time-string
  14726. (concat
  14727. (if inactive "[" "<") (substring tf 1 -1)
  14728. (if inactive "]" ">"))
  14729. (apply 'encode-time time))
  14730. string (replace-match str t t string)
  14731. start (+ start (length str)))))))
  14732. string)
  14733. (defun org-fix-decoded-time (time)
  14734. "Set 0 instead of nil for the first 6 elements of time.
  14735. Don't touch the rest."
  14736. (let ((n 0))
  14737. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14738. (defun org-days-to-time (timestamp-string)
  14739. "Difference between TIMESTAMP-STRING and now in days."
  14740. (- (time-to-days (org-time-string-to-time timestamp-string))
  14741. (time-to-days (current-time))))
  14742. (defun org-deadline-close (timestamp-string &optional ndays)
  14743. "Is the time in TIMESTAMP-STRING close to the current date?"
  14744. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14745. (and (< (org-days-to-time timestamp-string) ndays)
  14746. (not (org-entry-is-done-p))))
  14747. (defun org-get-wdays (ts)
  14748. "Get the deadline lead time appropriate for timestring TS."
  14749. (cond
  14750. ((<= org-deadline-warning-days 0)
  14751. ;; 0 or negative, enforce this value no matter what
  14752. (- org-deadline-warning-days))
  14753. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  14754. ;; lead time is specified.
  14755. (floor (* (string-to-number (match-string 1 ts))
  14756. (cdr (assoc (match-string 2 ts)
  14757. '(("d" . 1) ("w" . 7)
  14758. ("m" . 30.4) ("y" . 365.25)))))))
  14759. ;; go for the default.
  14760. (t org-deadline-warning-days)))
  14761. (defun org-calendar-select-mouse (ev)
  14762. "Return to `org-read-date' with the date currently selected.
  14763. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14764. (interactive "e")
  14765. (mouse-set-point ev)
  14766. (when (calendar-cursor-to-date)
  14767. (let* ((date (calendar-cursor-to-date))
  14768. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14769. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14770. (if (active-minibuffer-window) (exit-minibuffer))))
  14771. (defun org-check-deadlines (ndays)
  14772. "Check if there are any deadlines due or past due.
  14773. A deadline is considered due if it happens within `org-deadline-warning-days'
  14774. days from today's date. If the deadline appears in an entry marked DONE,
  14775. it is not shown. The prefix arg NDAYS can be used to test that many
  14776. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14777. (interactive "P")
  14778. (let* ((org-warn-days
  14779. (cond
  14780. ((equal ndays '(4)) 100000)
  14781. (ndays (prefix-numeric-value ndays))
  14782. (t (abs org-deadline-warning-days))))
  14783. (case-fold-search nil)
  14784. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14785. (callback
  14786. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14787. (message "%d deadlines past-due or due within %d days"
  14788. (org-occur regexp nil callback)
  14789. org-warn-days)))
  14790. (defun org-evaluate-time-range (&optional to-buffer)
  14791. "Evaluate a time range by computing the difference between start and end.
  14792. Normally the result is just printed in the echo area, but with prefix arg
  14793. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14794. If the time range is actually in a table, the result is inserted into the
  14795. next column.
  14796. For time difference computation, a year is assumed to be exactly 365
  14797. days in order to avoid rounding problems."
  14798. (interactive "P")
  14799. (or
  14800. (org-clock-update-time-maybe)
  14801. (save-excursion
  14802. (unless (org-at-date-range-p t)
  14803. (goto-char (point-at-bol))
  14804. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14805. (if (not (org-at-date-range-p t))
  14806. (error "Not at a time-stamp range, and none found in current line")))
  14807. (let* ((ts1 (match-string 1))
  14808. (ts2 (match-string 2))
  14809. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14810. (match-end (match-end 0))
  14811. (time1 (org-time-string-to-time ts1))
  14812. (time2 (org-time-string-to-time ts2))
  14813. (t1 (time-to-seconds time1))
  14814. (t2 (time-to-seconds time2))
  14815. (diff (abs (- t2 t1)))
  14816. (negative (< (- t2 t1) 0))
  14817. ;; (ys (floor (* 365 24 60 60)))
  14818. (ds (* 24 60 60))
  14819. (hs (* 60 60))
  14820. (fy "%dy %dd %02d:%02d")
  14821. (fy1 "%dy %dd")
  14822. (fd "%dd %02d:%02d")
  14823. (fd1 "%dd")
  14824. (fh "%02d:%02d")
  14825. y d h m align)
  14826. (if havetime
  14827. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14828. y 0
  14829. d (floor (/ diff ds)) diff (mod diff ds)
  14830. h (floor (/ diff hs)) diff (mod diff hs)
  14831. m (floor (/ diff 60)))
  14832. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14833. y 0
  14834. d (floor (+ (/ diff ds) 0.5))
  14835. h 0 m 0))
  14836. (if (not to-buffer)
  14837. (message (org-make-tdiff-string y d h m))
  14838. (when (org-at-table-p)
  14839. (goto-char match-end)
  14840. (setq align t)
  14841. (and (looking-at " *|") (goto-char (match-end 0))))
  14842. (if (looking-at
  14843. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14844. (replace-match ""))
  14845. (if negative (insert " -"))
  14846. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14847. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14848. (insert " " (format fh h m))))
  14849. (if align (org-table-align))
  14850. (message "Time difference inserted")))))
  14851. (defun org-make-tdiff-string (y d h m)
  14852. (let ((fmt "")
  14853. (l nil))
  14854. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14855. l (push y l)))
  14856. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14857. l (push d l)))
  14858. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14859. l (push h l)))
  14860. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14861. l (push m l)))
  14862. (apply 'format fmt (nreverse l))))
  14863. (defun org-time-string-to-time (s)
  14864. (apply 'encode-time (org-parse-time-string s)))
  14865. (defun org-time-string-to-absolute (s &optional daynr)
  14866. "Convert a time stamp to an absolute day number.
  14867. If there is a specifyer for a cyclic time stamp, get the closest date to
  14868. DAYNR."
  14869. (cond
  14870. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14871. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14872. daynr
  14873. (+ daynr 1000)))
  14874. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14875. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14876. (time-to-days (current-time))) (match-string 0 s)))
  14877. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  14878. (defun org-time-from-absolute (d)
  14879. "Return the time corresponding to date D.
  14880. D may be an absolute day number, or a calendar-type list (month day year)."
  14881. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14882. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14883. (defun org-calendar-holiday ()
  14884. "List of holidays, for Diary display in Org-mode."
  14885. (let ((hl (check-calendar-holidays date)))
  14886. (if hl (mapconcat 'identity hl "; "))))
  14887. (defun org-diary-sexp-entry (sexp entry date)
  14888. "Process a SEXP diary ENTRY for DATE."
  14889. (require 'diary-lib)
  14890. (let ((result (if calendar-debug-sexp
  14891. (let ((stack-trace-on-error t))
  14892. (eval (car (read-from-string sexp))))
  14893. (condition-case nil
  14894. (eval (car (read-from-string sexp)))
  14895. (error
  14896. (beep)
  14897. (message "Bad sexp at line %d in %s: %s"
  14898. (org-current-line)
  14899. (buffer-file-name) sexp)
  14900. (sleep-for 2))))))
  14901. (cond ((stringp result) result)
  14902. ((and (consp result)
  14903. (stringp (cdr result))) (cdr result))
  14904. (result entry)
  14905. (t nil))))
  14906. (defun org-diary-to-ical-string (frombuf)
  14907. "Get iCalendar entreis from diary entries in buffer FROMBUF.
  14908. This uses the icalendar.el library."
  14909. (let* ((tmpdir (if (featurep 'xemacs)
  14910. (temp-directory)
  14911. temporary-file-directory))
  14912. (tmpfile (make-temp-name
  14913. (expand-file-name "orgics" tmpdir)))
  14914. buf rtn b e)
  14915. (save-excursion
  14916. (set-buffer frombuf)
  14917. (icalendar-export-region (point-min) (point-max) tmpfile)
  14918. (setq buf (find-buffer-visiting tmpfile))
  14919. (set-buffer buf)
  14920. (goto-char (point-min))
  14921. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14922. (setq b (match-beginning 0)))
  14923. (goto-char (point-max))
  14924. (if (re-search-backward "^END:VEVENT" nil t)
  14925. (setq e (match-end 0)))
  14926. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14927. (kill-buffer buf)
  14928. (kill-buffer frombuf)
  14929. (delete-file tmpfile)
  14930. rtn))
  14931. (defun org-closest-date (start current change)
  14932. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  14933. ;; Make the proper lists from the dates
  14934. (catch 'exit
  14935. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14936. dn dw sday cday n1 n2
  14937. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14938. (setq start (org-date-to-gregorian start)
  14939. current (org-date-to-gregorian
  14940. (if org-agenda-repeating-timestamp-show-all
  14941. current
  14942. (time-to-days (current-time))))
  14943. sday (calendar-absolute-from-gregorian start)
  14944. cday (calendar-absolute-from-gregorian current))
  14945. (if (<= cday sday) (throw 'exit sday))
  14946. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14947. (setq dn (string-to-number (match-string 1 change))
  14948. dw (cdr (assoc (match-string 2 change) a1)))
  14949. (error "Invalid change specifyer: %s" change))
  14950. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14951. (cond
  14952. ((eq dw 'day)
  14953. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14954. n2 (+ n1 dn)))
  14955. ((eq dw 'year)
  14956. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14957. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14958. (setq date1 (list m d y1)
  14959. n1 (calendar-absolute-from-gregorian date1)
  14960. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14961. n2 (calendar-absolute-from-gregorian date2)))
  14962. ((eq dw 'month)
  14963. ;; approx number of month between the tow dates
  14964. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14965. ;; How often does dn fit in there?
  14966. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14967. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14968. m (+ m nm)
  14969. ny (floor (/ m 12))
  14970. y (+ y ny)
  14971. m (- m (* ny 12)))
  14972. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14973. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14974. (setq m2 (+ m dn) y2 y)
  14975. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14976. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14977. (while (< n2 cday)
  14978. (setq n1 n2 m m2 y y2)
  14979. (setq m2 (+ m dn) y2 y)
  14980. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14981. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14982. (if org-agenda-repeating-timestamp-show-all
  14983. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  14984. (if (= cday n1) n1 n2)))))
  14985. (defun org-date-to-gregorian (date)
  14986. "Turn any specification of DATE into a gregorian date for the calendar."
  14987. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14988. ((and (listp date) (= (length date) 3)) date)
  14989. ((stringp date)
  14990. (setq date (org-parse-time-string date))
  14991. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14992. ((listp date)
  14993. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14994. (defun org-parse-time-string (s &optional nodefault)
  14995. "Parse the standard Org-mode time string.
  14996. This should be a lot faster than the normal `parse-time-string'.
  14997. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14998. hour and minute fields will be nil if not given."
  14999. (if (string-match org-ts-regexp0 s)
  15000. (list 0
  15001. (if (or (match-beginning 8) (not nodefault))
  15002. (string-to-number (or (match-string 8 s) "0")))
  15003. (if (or (match-beginning 7) (not nodefault))
  15004. (string-to-number (or (match-string 7 s) "0")))
  15005. (string-to-number (match-string 4 s))
  15006. (string-to-number (match-string 3 s))
  15007. (string-to-number (match-string 2 s))
  15008. nil nil nil)
  15009. (make-list 9 0)))
  15010. (defun org-timestamp-up (&optional arg)
  15011. "Increase the date item at the cursor by one.
  15012. If the cursor is on the year, change the year. If it is on the month or
  15013. the day, change that.
  15014. With prefix ARG, change by that many units."
  15015. (interactive "p")
  15016. (org-timestamp-change (prefix-numeric-value arg)))
  15017. (defun org-timestamp-down (&optional arg)
  15018. "Decrease the date item at the cursor by one.
  15019. If the cursor is on the year, change the year. If it is on the month or
  15020. the day, change that.
  15021. With prefix ARG, change by that many units."
  15022. (interactive "p")
  15023. (org-timestamp-change (- (prefix-numeric-value arg))))
  15024. (defun org-timestamp-up-day (&optional arg)
  15025. "Increase the date in the time stamp by one day.
  15026. With prefix ARG, change that many days."
  15027. (interactive "p")
  15028. (if (and (not (org-at-timestamp-p t))
  15029. (org-on-heading-p))
  15030. (org-todo 'up)
  15031. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  15032. (defun org-timestamp-down-day (&optional arg)
  15033. "Decrease the date in the time stamp by one day.
  15034. With prefix ARG, change that many days."
  15035. (interactive "p")
  15036. (if (and (not (org-at-timestamp-p t))
  15037. (org-on-heading-p))
  15038. (org-todo 'down)
  15039. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  15040. (defsubst org-pos-in-match-range (pos n)
  15041. (and (match-beginning n)
  15042. (<= (match-beginning n) pos)
  15043. (>= (match-end n) pos)))
  15044. (defun org-at-timestamp-p (&optional inactive-ok)
  15045. "Determine if the cursor is in or at a timestamp."
  15046. (interactive)
  15047. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15048. (pos (point))
  15049. (ans (or (looking-at tsr)
  15050. (save-excursion
  15051. (skip-chars-backward "^[<\n\r\t")
  15052. (if (> (point) (point-min)) (backward-char 1))
  15053. (and (looking-at tsr)
  15054. (> (- (match-end 0) pos) -1))))))
  15055. (and ans
  15056. (boundp 'org-ts-what)
  15057. (setq org-ts-what
  15058. (cond
  15059. ((org-pos-in-match-range pos 2) 'year)
  15060. ((org-pos-in-match-range pos 3) 'month)
  15061. ((org-pos-in-match-range pos 7) 'hour)
  15062. ((org-pos-in-match-range pos 8) 'minute)
  15063. ((or (org-pos-in-match-range pos 4)
  15064. (org-pos-in-match-range pos 5)) 'day)
  15065. ((and (> pos (or (match-end 8) (match-end 5)))
  15066. (< pos (match-end 0)))
  15067. (- pos (or (match-end 8) (match-end 5))))
  15068. (t 'day))))
  15069. ans))
  15070. (defun org-timestamp-change (n &optional what)
  15071. "Change the date in the time stamp at point.
  15072. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15073. `year', `minute', `second'. If WHAT is not given, the cursor position
  15074. in the timestamp determines what will be changed."
  15075. (let ((pos (point))
  15076. with-hm inactive
  15077. org-ts-what
  15078. extra
  15079. ts time time0)
  15080. (if (not (org-at-timestamp-p t))
  15081. (error "Not at a timestamp"))
  15082. (if (and (not what) (not (eq org-ts-what 'day))
  15083. org-display-custom-times
  15084. (get-text-property (point) 'display)
  15085. (not (get-text-property (1- (point)) 'display)))
  15086. (setq org-ts-what 'day))
  15087. (setq org-ts-what (or what org-ts-what)
  15088. inactive (= (char-after (match-beginning 0)) ?\[)
  15089. ts (match-string 0))
  15090. (replace-match "")
  15091. (if (string-match
  15092. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  15093. ts)
  15094. (setq extra (match-string 1 ts)))
  15095. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15096. (setq with-hm t))
  15097. (setq time0 (org-parse-time-string ts))
  15098. (setq time
  15099. (encode-time (or (car time0) 0)
  15100. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15101. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15102. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15103. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15104. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15105. (nthcdr 6 time0)))
  15106. (when (integerp org-ts-what)
  15107. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  15108. (if (eq what 'calendar)
  15109. (let ((cal-date (org-get-date-from-calendar)))
  15110. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15111. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15112. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15113. (setcar time0 (or (car time0) 0))
  15114. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15115. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15116. (setq time (apply 'encode-time time0))))
  15117. (setq org-last-changed-timestamp
  15118. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15119. (org-clock-update-time-maybe)
  15120. (goto-char pos)
  15121. ;; Try to recenter the calendar window, if any
  15122. (if (and org-calendar-follow-timestamp-change
  15123. (get-buffer-window "*Calendar*" t)
  15124. (memq org-ts-what '(day month year)))
  15125. (org-recenter-calendar (time-to-days time)))))
  15126. ;; FIXME: does not yet work for lead times
  15127. (defun org-modify-ts-extra (s pos n)
  15128. "Change the different parts of the lead-time and repeat fields in timestamp."
  15129. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15130. ng h m new)
  15131. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15132. (cond
  15133. ((or (org-pos-in-match-range pos 2)
  15134. (org-pos-in-match-range pos 3))
  15135. (setq m (string-to-number (match-string 3 s))
  15136. h (string-to-number (match-string 2 s)))
  15137. (if (org-pos-in-match-range pos 2)
  15138. (setq h (+ h n))
  15139. (setq m (+ m n)))
  15140. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15141. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15142. (setq h (min 24 (max 0 h)))
  15143. (setq ng 1 new (format "-%02d:%02d" h m)))
  15144. ((org-pos-in-match-range pos 6)
  15145. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15146. ((org-pos-in-match-range pos 5)
  15147. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  15148. (when ng
  15149. (setq s (concat
  15150. (substring s 0 (match-beginning ng))
  15151. new
  15152. (substring s (match-end ng))))))
  15153. s))
  15154. (defun org-recenter-calendar (date)
  15155. "If the calendar is visible, recenter it to DATE."
  15156. (let* ((win (selected-window))
  15157. (cwin (get-buffer-window "*Calendar*" t))
  15158. (calendar-move-hook nil))
  15159. (when cwin
  15160. (select-window cwin)
  15161. (calendar-goto-date (if (listp date) date
  15162. (calendar-gregorian-from-absolute date)))
  15163. (select-window win))))
  15164. (defun org-goto-calendar (&optional arg)
  15165. "Go to the Emacs calendar at the current date.
  15166. If there is a time stamp in the current line, go to that date.
  15167. A prefix ARG can be used to force the current date."
  15168. (interactive "P")
  15169. (let ((tsr org-ts-regexp) diff
  15170. (calendar-move-hook nil)
  15171. (view-calendar-holidays-initially nil)
  15172. (view-diary-entries-initially nil))
  15173. (if (or (org-at-timestamp-p)
  15174. (save-excursion
  15175. (beginning-of-line 1)
  15176. (looking-at (concat ".*" tsr))))
  15177. (let ((d1 (time-to-days (current-time)))
  15178. (d2 (time-to-days
  15179. (org-time-string-to-time (match-string 1)))))
  15180. (setq diff (- d2 d1))))
  15181. (calendar)
  15182. (calendar-goto-today)
  15183. (if (and diff (not arg)) (calendar-forward-day diff))))
  15184. (defun org-get-date-from-calendar ()
  15185. "Return a list (month day year) of date at point in calendar."
  15186. (with-current-buffer "*Calendar*"
  15187. (save-match-data
  15188. (calendar-cursor-to-date))))
  15189. (defun org-date-from-calendar ()
  15190. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15191. If there is already a time stamp at the cursor position, update it."
  15192. (interactive)
  15193. (if (org-at-timestamp-p t)
  15194. (org-timestamp-change 0 'calendar)
  15195. (let ((cal-date (org-get-date-from-calendar)))
  15196. (org-insert-time-stamp
  15197. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15198. ;; Make appt aware of appointments from the agenda
  15199. (defun org-agenda-to-appt (&optional filter)
  15200. "Activate appointments found in `org-agenda-files'.
  15201. When prefixed, prompt for a regular expression and use it as a
  15202. filter: only add entries if they match this regular expression.
  15203. FILTER can be a string. In this case, use this string as a
  15204. regular expression to filter results.
  15205. FILTER can also be an alist, with the car of each cell being
  15206. either 'headline or 'category. For example:
  15207. '((headline \"IMPORTANT\")
  15208. (category \"Work\"))
  15209. will only add headlines containing IMPORTANT or headlines
  15210. belonging to the category \"Work\"."
  15211. (interactive "P")
  15212. (require 'org)
  15213. (if (equal filter '(4))
  15214. (setq filter (read-from-minibuffer "Regexp filter: ")))
  15215. (let* ((today (org-date-to-gregorian
  15216. (time-to-days (current-time))))
  15217. (files org-agenda-files) entries file)
  15218. (while (setq file (pop files))
  15219. (setq entries (append entries (org-agenda-get-day-entries
  15220. file today :timestamp))))
  15221. (setq entries (delq nil entries))
  15222. (mapc
  15223. (lambda(x)
  15224. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  15225. (cat (get-text-property 1 'org-category x))
  15226. (tod (get-text-property 1 'time-of-day x))
  15227. (ok (or (and (stringp filter) (string-match filter evt))
  15228. (and (not (null filter)) (listp filter)
  15229. (or (string-match
  15230. (cadr (assoc 'category filter)) cat)
  15231. (string-match
  15232. (cadr (assoc 'headline filter)) evt))))))
  15233. ;; (setq evt (set-text-properties 0 (length event) nil evt))
  15234. (when (and ok tod)
  15235. (setq tod (number-to-string tod)
  15236. tod (when (string-match
  15237. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  15238. (concat (match-string 1 tod) ":"
  15239. (match-string 2 tod))))
  15240. (appt-add tod evt)))) entries)
  15241. nil))
  15242. ;;; The clock for measuring work time.
  15243. (defvar org-mode-line-string "")
  15244. (put 'org-mode-line-string 'risky-local-variable t)
  15245. (defvar org-mode-line-timer nil)
  15246. (defvar org-clock-heading "")
  15247. (defvar org-clock-start-time "")
  15248. (defun org-update-mode-line ()
  15249. (let* ((delta (- (time-to-seconds (current-time))
  15250. (time-to-seconds org-clock-start-time)))
  15251. (h (floor delta 3600))
  15252. (m (floor (- delta (* 3600 h)) 60)))
  15253. (setq org-mode-line-string
  15254. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  15255. 'help-echo "Org-mode clock is running"))
  15256. (force-mode-line-update)))
  15257. (defvar org-clock-marker (make-marker)
  15258. "Marker recording the last clock-in.")
  15259. (defvar org-clock-mode-line-entry nil
  15260. "Information for the modeline about the running clock.")
  15261. (defun org-clock-in ()
  15262. "Start the clock on the current item.
  15263. If necessary, clock-out of the currently active clock."
  15264. (interactive)
  15265. (org-clock-out t)
  15266. (let (ts)
  15267. (save-excursion
  15268. (org-back-to-heading t)
  15269. (if (looking-at org-todo-line-regexp)
  15270. (setq org-clock-heading (match-string 3))
  15271. (setq org-clock-heading "???"))
  15272. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  15273. (org-clock-find-position)
  15274. (insert "\n") (backward-char 1)
  15275. (indent-relative)
  15276. (insert org-clock-string " ")
  15277. (setq org-clock-start-time (current-time))
  15278. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  15279. (move-marker org-clock-marker (point) (buffer-base-buffer))
  15280. (or global-mode-string (setq global-mode-string '("")))
  15281. (or (memq 'org-mode-line-string global-mode-string)
  15282. (setq global-mode-string
  15283. (append global-mode-string '(org-mode-line-string))))
  15284. (org-update-mode-line)
  15285. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  15286. (message "Clock started at %s" ts))))
  15287. (defun org-clock-find-position ()
  15288. "Find the location where the next clock line should be inserted."
  15289. (org-back-to-heading t)
  15290. (catch 'exit
  15291. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  15292. (re (concat "^[ \t]*" org-clock-string))
  15293. (cnt 0)
  15294. first last)
  15295. (goto-char beg)
  15296. (when (eobp) (newline) (setq end (max (point) end)))
  15297. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  15298. ;; we seem to have a CLOCK drawer, so go there.
  15299. (beginning-of-line 2)
  15300. (throw 'exit t))
  15301. ;; Lets count the CLOCK lines
  15302. (goto-char beg)
  15303. (while (re-search-forward re end t)
  15304. (setq first (or first (match-beginning 0))
  15305. last (match-beginning 0)
  15306. cnt (1+ cnt)))
  15307. (when (and (integerp org-clock-into-drawer)
  15308. (>= (1+ cnt) org-clock-into-drawer))
  15309. ;; Wrap current entries into a new drawer
  15310. (goto-char last)
  15311. (beginning-of-line 2)
  15312. (if (org-at-item-p) (org-end-of-item))
  15313. (insert ":END:\n")
  15314. (beginning-of-line 0)
  15315. (org-indent-line-function)
  15316. (goto-char first)
  15317. (insert ":CLOCK:\n")
  15318. (beginning-of-line 0)
  15319. (org-indent-line-function)
  15320. (org-flag-drawer t)
  15321. (beginning-of-line 2)
  15322. (throw 'exit nil))
  15323. (goto-char beg)
  15324. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  15325. (not (equal (match-string 1) org-clock-string)))
  15326. ;; Planning info, skip to after it
  15327. (beginning-of-line 2)
  15328. (or (bolp) (newline)))
  15329. (when (eq t org-clock-into-drawer)
  15330. (insert ":CLOCK:\n:END:\n")
  15331. (beginning-of-line -1)
  15332. (org-indent-line-function)
  15333. (org-flag-drawer t)
  15334. (beginning-of-line 2)
  15335. (org-indent-line-function)))))
  15336. (defun org-clock-out (&optional fail-quietly)
  15337. "Stop the currently running clock.
  15338. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  15339. (interactive)
  15340. (catch 'exit
  15341. (if (not (marker-buffer org-clock-marker))
  15342. (if fail-quietly (throw 'exit t) (error "No active clock")))
  15343. (let (ts te s h m)
  15344. (save-excursion
  15345. (set-buffer (marker-buffer org-clock-marker))
  15346. (goto-char org-clock-marker)
  15347. (beginning-of-line 1)
  15348. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  15349. (equal (match-string 1) org-clock-string))
  15350. (setq ts (match-string 2))
  15351. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  15352. (goto-char (match-end 0))
  15353. (delete-region (point) (point-at-eol))
  15354. (insert "--")
  15355. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  15356. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  15357. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  15358. h (floor (/ s 3600))
  15359. s (- s (* 3600 h))
  15360. m (floor (/ s 60))
  15361. s (- s (* 60 s)))
  15362. (insert " => " (format "%2d:%02d" h m))
  15363. (move-marker org-clock-marker nil)
  15364. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  15365. (org-log-done (org-parse-local-options logging 'org-log-done))
  15366. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  15367. (org-add-log-maybe 'clock-out))
  15368. (when org-mode-line-timer
  15369. (cancel-timer org-mode-line-timer)
  15370. (setq org-mode-line-timer nil))
  15371. (setq global-mode-string
  15372. (delq 'org-mode-line-string global-mode-string))
  15373. (force-mode-line-update)
  15374. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  15375. (defun org-clock-cancel ()
  15376. "Cancel the running clock be removing the start timestamp."
  15377. (interactive)
  15378. (if (not (marker-buffer org-clock-marker))
  15379. (error "No active clock"))
  15380. (save-excursion
  15381. (set-buffer (marker-buffer org-clock-marker))
  15382. (goto-char org-clock-marker)
  15383. (delete-region (1- (point-at-bol)) (point-at-eol)))
  15384. (message "Clock canceled"))
  15385. (defun org-clock-goto (&optional delete-windows)
  15386. "Go to the currently clocked-in entry."
  15387. (interactive "P")
  15388. (if (not (marker-buffer org-clock-marker))
  15389. (error "No active clock"))
  15390. (switch-to-buffer-other-window
  15391. (marker-buffer org-clock-marker))
  15392. (if delete-windows (delete-other-windows))
  15393. (goto-char org-clock-marker)
  15394. (org-show-entry)
  15395. (org-back-to-heading)
  15396. (recenter))
  15397. (defvar org-clock-file-total-minutes nil
  15398. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  15399. (make-variable-buffer-local 'org-clock-file-total-minutes)
  15400. (defun org-clock-sum (&optional tstart tend)
  15401. "Sum the times for each subtree.
  15402. Puts the resulting times in minutes as a text property on each headline."
  15403. (interactive)
  15404. (let* ((bmp (buffer-modified-p))
  15405. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  15406. org-clock-string
  15407. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  15408. (lmax 30)
  15409. (ltimes (make-vector lmax 0))
  15410. (t1 0)
  15411. (level 0)
  15412. ts te dt
  15413. time)
  15414. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  15415. (save-excursion
  15416. (goto-char (point-max))
  15417. (while (re-search-backward re nil t)
  15418. (cond
  15419. ((match-end 2)
  15420. ;; Two time stamps
  15421. (setq ts (match-string 2)
  15422. te (match-string 3)
  15423. ts (time-to-seconds
  15424. (apply 'encode-time (org-parse-time-string ts)))
  15425. te (time-to-seconds
  15426. (apply 'encode-time (org-parse-time-string te)))
  15427. ts (if tstart (max ts tstart) ts)
  15428. te (if tend (min te tend) te)
  15429. dt (- te ts)
  15430. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  15431. ((match-end 4)
  15432. ;; A naket time
  15433. (setq t1 (+ t1 (string-to-number (match-string 5))
  15434. (* 60 (string-to-number (match-string 4))))))
  15435. (t ;; A headline
  15436. (setq level (- (match-end 1) (match-beginning 1)))
  15437. (when (or (> t1 0) (> (aref ltimes level) 0))
  15438. (loop for l from 0 to level do
  15439. (aset ltimes l (+ (aref ltimes l) t1)))
  15440. (setq t1 0 time (aref ltimes level))
  15441. (loop for l from level to (1- lmax) do
  15442. (aset ltimes l 0))
  15443. (goto-char (match-beginning 0))
  15444. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  15445. (setq org-clock-file-total-minutes (aref ltimes 0)))
  15446. (set-buffer-modified-p bmp)))
  15447. (defun org-clock-display (&optional total-only)
  15448. "Show subtree times in the entire buffer.
  15449. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  15450. in the echo area."
  15451. (interactive)
  15452. (org-remove-clock-overlays)
  15453. (let (time h m p)
  15454. (org-clock-sum)
  15455. (unless total-only
  15456. (save-excursion
  15457. (goto-char (point-min))
  15458. (while (or (and (equal (setq p (point)) (point-min))
  15459. (get-text-property p :org-clock-minutes))
  15460. (setq p (next-single-property-change
  15461. (point) :org-clock-minutes)))
  15462. (goto-char p)
  15463. (when (setq time (get-text-property p :org-clock-minutes))
  15464. (org-put-clock-overlay time (funcall outline-level))))
  15465. (setq h (/ org-clock-file-total-minutes 60)
  15466. m (- org-clock-file-total-minutes (* 60 h)))
  15467. ;; Arrange to remove the overlays upon next change.
  15468. (when org-remove-highlights-with-change
  15469. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  15470. nil 'local))))
  15471. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  15472. (defvar org-clock-overlays nil)
  15473. (make-variable-buffer-local 'org-clock-overlays)
  15474. (defun org-put-clock-overlay (time &optional level)
  15475. "Put an overlays on the current line, displaying TIME.
  15476. If LEVEL is given, prefix time with a corresponding number of stars.
  15477. This creates a new overlay and stores it in `org-clock-overlays', so that it
  15478. will be easy to remove."
  15479. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  15480. (l (if level (org-get-legal-level level 0) 0))
  15481. (off 0)
  15482. ov tx)
  15483. (move-to-column c)
  15484. (unless (eolp) (skip-chars-backward "^ \t"))
  15485. (skip-chars-backward " \t")
  15486. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  15487. tx (concat (buffer-substring (1- (point)) (point))
  15488. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  15489. (org-add-props (format "%s %2d:%02d%s"
  15490. (make-string l ?*) h m
  15491. (make-string (- 10 l) ?\ ))
  15492. '(face secondary-selection))
  15493. ""))
  15494. (if (not (featurep 'xemacs))
  15495. (org-overlay-put ov 'display tx)
  15496. (org-overlay-put ov 'invisible t)
  15497. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  15498. (push ov org-clock-overlays)))
  15499. (defun org-remove-clock-overlays (&optional beg end noremove)
  15500. "Remove the occur highlights from the buffer.
  15501. BEG and END are ignored. If NOREMOVE is nil, remove this function
  15502. from the `before-change-functions' in the current buffer."
  15503. (interactive)
  15504. (unless org-inhibit-highlight-removal
  15505. (mapc 'org-delete-overlay org-clock-overlays)
  15506. (setq org-clock-overlays nil)
  15507. (unless noremove
  15508. (remove-hook 'before-change-functions
  15509. 'org-remove-clock-overlays 'local))))
  15510. (defun org-clock-out-if-current ()
  15511. "Clock out if the current entry contains the running clock.
  15512. This is used to stop the clock after a TODO entry is marked DONE,
  15513. and is only done if the variable `org-clock-out-when-done' is not nil."
  15514. (when (and org-clock-out-when-done
  15515. (member state org-done-keywords)
  15516. (equal (marker-buffer org-clock-marker) (current-buffer))
  15517. (< (point) org-clock-marker)
  15518. (> (save-excursion (outline-next-heading) (point))
  15519. org-clock-marker))
  15520. ;; Clock out, but don't accept a logging message for this.
  15521. (let ((org-log-done (if (and (listp org-log-done)
  15522. (member 'clock-out org-log-done))
  15523. '(done)
  15524. org-log-done)))
  15525. (org-clock-out))))
  15526. (add-hook 'org-after-todo-state-change-hook
  15527. 'org-clock-out-if-current)
  15528. (defun org-check-running-clock ()
  15529. "Check if the current buffer contains the running clock.
  15530. If yes, offer to stop it and to save the buffer with the changes."
  15531. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  15532. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  15533. (buffer-name))))
  15534. (org-clock-out)
  15535. (when (y-or-n-p "Save changed buffer?")
  15536. (save-buffer))))
  15537. (defun org-clock-report (&optional arg)
  15538. "Create a table containing a report about clocked time.
  15539. If the cursor is inside an existing clocktable block, then the table
  15540. will be updated. If not, a new clocktable will be inserted.
  15541. When called with a prefix argument, move to the first clock table in the
  15542. buffer and update it."
  15543. (interactive "P")
  15544. (org-remove-clock-overlays)
  15545. (when arg (org-find-dblock "clocktable"))
  15546. (if (org-in-clocktable-p)
  15547. (goto-char (org-in-clocktable-p))
  15548. (org-create-dblock (list :name "clocktable"
  15549. :maxlevel 2 :scope 'file)))
  15550. (org-update-dblock))
  15551. (defun org-in-clocktable-p ()
  15552. "Check if the cursor is in a clocktable."
  15553. (let ((pos (point)) start)
  15554. (save-excursion
  15555. (end-of-line 1)
  15556. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  15557. (setq start (match-beginning 0))
  15558. (re-search-forward "^#\\+END:.*" nil t)
  15559. (>= (match-end 0) pos)
  15560. start))))
  15561. (defun org-clock-update-time-maybe ()
  15562. "If this is a CLOCK line, update it and return t.
  15563. Otherwise, return nil."
  15564. (interactive)
  15565. (save-excursion
  15566. (beginning-of-line 1)
  15567. (skip-chars-forward " \t")
  15568. (when (looking-at org-clock-string)
  15569. (let ((re (concat "[ \t]*" org-clock-string
  15570. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  15571. "\\([ \t]*=>.*\\)?"))
  15572. ts te h m s)
  15573. (if (not (looking-at re))
  15574. nil
  15575. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  15576. (end-of-line 1)
  15577. (setq ts (match-string 1)
  15578. te (match-string 2))
  15579. (setq s (- (time-to-seconds
  15580. (apply 'encode-time (org-parse-time-string te)))
  15581. (time-to-seconds
  15582. (apply 'encode-time (org-parse-time-string ts))))
  15583. h (floor (/ s 3600))
  15584. s (- s (* 3600 h))
  15585. m (floor (/ s 60))
  15586. s (- s (* 60 s)))
  15587. (insert " => " (format "%2d:%02d" h m))
  15588. t)))))
  15589. (defun org-clock-special-range (key &optional time as-strings)
  15590. "Return two times bordering a special time range.
  15591. Key is a symbol specifying the range and can be one of `today', `yesterday',
  15592. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  15593. A week starts Monday 0:00 and ends Sunday 24:00.
  15594. The range is determined relative to TIME. TIME defaults to the current time.
  15595. The return value is a cons cell with two internal times like the ones
  15596. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  15597. the returned times will be formatted strings."
  15598. (let* ((tm (decode-time (or time (current-time))))
  15599. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  15600. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  15601. (dow (nth 6 tm))
  15602. s1 m1 h1 d1 month1 y1 diff ts te fm)
  15603. (cond
  15604. ((eq key 'today)
  15605. (setq h 0 m 0 h1 24 m1 0))
  15606. ((eq key 'yesterday)
  15607. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  15608. ((eq key 'thisweek)
  15609. (setq diff (if (= dow 0) 6 (1- dow))
  15610. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  15611. ((eq key 'lastweek)
  15612. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  15613. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  15614. ((eq key 'thismonth)
  15615. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  15616. ((eq key 'lastmonth)
  15617. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  15618. ((eq key 'thisyear)
  15619. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  15620. ((eq key 'lastyear)
  15621. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  15622. (t (error "No such time block %s" key)))
  15623. (setq ts (encode-time s m h d month y)
  15624. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  15625. (or d1 d) (or month1 month) (or y1 y)))
  15626. (setq fm (cdr org-time-stamp-formats))
  15627. (if as-strings
  15628. (cons (format-time-string fm ts) (format-time-string fm te))
  15629. (cons ts te))))
  15630. (defun org-dblock-write:clocktable (params)
  15631. "Write the standard clocktable."
  15632. (let ((hlchars '((1 . "*") (2 . "/")))
  15633. (emph nil)
  15634. (ins (make-marker))
  15635. (total-time nil)
  15636. ipos time h m p level hlc hdl maxlevel
  15637. ts te cc block beg end pos scope tbl tostring multifile)
  15638. (setq scope (plist-get params :scope)
  15639. tostring (plist-get params :tostring)
  15640. multifile (plist-get params :multifile)
  15641. maxlevel (or (plist-get params :maxlevel) 3)
  15642. emph (plist-get params :emphasize)
  15643. ts (plist-get params :tstart)
  15644. te (plist-get params :tend)
  15645. block (plist-get params :block))
  15646. (when block
  15647. (setq cc (org-clock-special-range block nil t)
  15648. ts (car cc) te (cdr cc)))
  15649. (if ts (setq ts (time-to-seconds
  15650. (apply 'encode-time (org-parse-time-string ts)))))
  15651. (if te (setq te (time-to-seconds
  15652. (apply 'encode-time (org-parse-time-string te)))))
  15653. (move-marker ins (point))
  15654. (setq ipos (point))
  15655. ;; Get the right scope
  15656. (setq pos (point))
  15657. (save-restriction
  15658. (cond
  15659. ((not scope))
  15660. ((eq scope 'file) (widen))
  15661. ((eq scope 'subtree) (org-narrow-to-subtree))
  15662. ((eq scope 'tree)
  15663. (while (org-up-heading-safe))
  15664. (org-narrow-to-subtree))
  15665. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  15666. (symbol-name scope)))
  15667. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  15668. (catch 'exit
  15669. (while (org-up-heading-safe)
  15670. (looking-at outline-regexp)
  15671. (if (<= (org-reduced-level (funcall outline-level)) level)
  15672. (throw 'exit nil))))
  15673. (org-narrow-to-subtree))
  15674. ((or (listp scope) (eq scope 'agenda))
  15675. (let* ((files (if (listp scope) scope (org-agenda-files)))
  15676. (scope 'agenda)
  15677. (p1 (copy-sequence params))
  15678. file)
  15679. (plist-put p1 :tostring t)
  15680. (plist-put p1 :multifile t)
  15681. (plist-put p1 :scope 'file)
  15682. (org-prepare-agenda-buffers files)
  15683. (while (setq file (pop files))
  15684. (with-current-buffer (find-buffer-visiting file)
  15685. (push (org-clocktable-add-file
  15686. file (org-dblock-write:clocktable p1)) tbl)
  15687. (setq total-time (+ (or total-time 0)
  15688. org-clock-file-total-minutes)))))))
  15689. (goto-char pos)
  15690. (unless (eq scope 'agenda)
  15691. (org-clock-sum ts te)
  15692. (goto-char (point-min))
  15693. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  15694. (goto-char p)
  15695. (when (setq time (get-text-property p :org-clock-minutes))
  15696. (save-excursion
  15697. (beginning-of-line 1)
  15698. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  15699. (setq level (org-reduced-level
  15700. (- (match-end 1) (match-beginning 1))))
  15701. (<= level maxlevel))
  15702. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  15703. hdl (match-string 2)
  15704. h (/ time 60)
  15705. m (- time (* 60 h)))
  15706. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  15707. (push (concat
  15708. "| " (int-to-string level) "|" hlc hdl hlc " |"
  15709. (make-string (1- level) ?|)
  15710. hlc (format "%d:%02d" h m) hlc
  15711. " |") tbl))))))
  15712. (setq tbl (nreverse tbl))
  15713. (if tostring
  15714. (if tbl (mapconcat 'identity tbl "\n") nil)
  15715. (goto-char ins)
  15716. (insert-before-markers
  15717. "Clock summary at ["
  15718. (substring
  15719. (format-time-string (cdr org-time-stamp-formats))
  15720. 1 -1)
  15721. "]."
  15722. (if block
  15723. (format " Considered range is /%s/." block)
  15724. "")
  15725. "\n\n"
  15726. (if (eq scope 'agenda) "|File" "")
  15727. "|L|Headline|Time|\n")
  15728. (setq total-time (or total-time org-clock-file-total-minutes)
  15729. h (/ total-time 60)
  15730. m (- total-time (* 60 h)))
  15731. (insert-before-markers
  15732. "|-\n|"
  15733. (if (eq scope 'agenda) "|" "")
  15734. "|"
  15735. "*Total time*| "
  15736. (format "*%d:%02d*" h m)
  15737. "|\n|-\n")
  15738. (setq tbl (delq nil tbl))
  15739. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  15740. (equal (substring (car tbl) 0 2) "|-"))
  15741. (pop tbl))
  15742. (insert-before-markers (mapconcat
  15743. 'identity (delq nil tbl)
  15744. (if (eq scope 'agenda) "\n|-\n" "\n")))
  15745. (backward-delete-char 1)
  15746. (goto-char ipos)
  15747. (skip-chars-forward "^|")
  15748. (org-table-align)))))
  15749. (defun org-clocktable-add-file (file table)
  15750. (if table
  15751. (let ((lines (org-split-string table "\n"))
  15752. (ff (file-name-nondirectory file)))
  15753. (mapconcat 'identity
  15754. (mapcar (lambda (x)
  15755. (if (string-match org-table-dataline-regexp x)
  15756. (concat "|" ff x)
  15757. x))
  15758. lines)
  15759. "\n"))))
  15760. ;; FIXME: I don't think anybody uses this, ask David
  15761. (defun org-collect-clock-time-entries ()
  15762. "Return an internal list with clocking information.
  15763. This list has one entry for each CLOCK interval.
  15764. FIXME: describe the elements."
  15765. (interactive)
  15766. (let ((re (concat "^[ \t]*" org-clock-string
  15767. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  15768. rtn beg end next cont level title total closedp leafp
  15769. clockpos titlepos h m donep)
  15770. (save-excursion
  15771. (org-clock-sum)
  15772. (goto-char (point-min))
  15773. (while (re-search-forward re nil t)
  15774. (setq clockpos (match-beginning 0)
  15775. beg (match-string 1) end (match-string 2)
  15776. cont (match-end 0))
  15777. (setq beg (apply 'encode-time (org-parse-time-string beg))
  15778. end (apply 'encode-time (org-parse-time-string end)))
  15779. (org-back-to-heading t)
  15780. (setq donep (org-entry-is-done-p))
  15781. (setq titlepos (point)
  15782. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  15783. h (/ total 60) m (- total (* 60 h))
  15784. total (cons h m))
  15785. (looking-at "\\(\\*+\\) +\\(.*\\)")
  15786. (setq level (- (match-end 1) (match-beginning 1))
  15787. title (org-match-string-no-properties 2))
  15788. (save-excursion (outline-next-heading) (setq next (point)))
  15789. (setq closedp (re-search-forward org-closed-time-regexp next t))
  15790. (goto-char next)
  15791. (setq leafp (and (looking-at "^\\*+ ")
  15792. (<= (- (match-end 0) (point)) level)))
  15793. (push (list beg end clockpos closedp donep
  15794. total title titlepos level leafp)
  15795. rtn)
  15796. (goto-char cont)))
  15797. (nreverse rtn)))
  15798. ;;;; Agenda, and Diary Integration
  15799. ;;; Define the Org-agenda-mode
  15800. (defvar org-agenda-mode-map (make-sparse-keymap)
  15801. "Keymap for `org-agenda-mode'.")
  15802. (defvar org-agenda-menu) ; defined later in this file.
  15803. (defvar org-agenda-follow-mode nil)
  15804. (defvar org-agenda-show-log nil)
  15805. (defvar org-agenda-redo-command nil)
  15806. (defvar org-agenda-mode-hook nil)
  15807. (defvar org-agenda-type nil)
  15808. (defvar org-agenda-force-single-file nil)
  15809. (defun org-agenda-mode ()
  15810. "Mode for time-sorted view on action items in Org-mode files.
  15811. The following commands are available:
  15812. \\{org-agenda-mode-map}"
  15813. (interactive)
  15814. (kill-all-local-variables)
  15815. (setq org-agenda-undo-list nil
  15816. org-agenda-pending-undo-list nil)
  15817. (setq major-mode 'org-agenda-mode)
  15818. ;; Keep global-font-lock-mode from turning on font-lock-mode
  15819. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  15820. (setq mode-name "Org-Agenda")
  15821. (use-local-map org-agenda-mode-map)
  15822. (easy-menu-add org-agenda-menu)
  15823. (if org-startup-truncated (setq truncate-lines t))
  15824. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  15825. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  15826. ;; Make sure properties are removed when copying text
  15827. (when (boundp 'buffer-substring-filters)
  15828. (org-set-local 'buffer-substring-filters
  15829. (cons (lambda (x)
  15830. (set-text-properties 0 (length x) nil x) x)
  15831. buffer-substring-filters)))
  15832. (unless org-agenda-keep-modes
  15833. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  15834. org-agenda-show-log nil))
  15835. (easy-menu-change
  15836. '("Agenda") "Agenda Files"
  15837. (append
  15838. (list
  15839. (vector
  15840. (if (get 'org-agenda-files 'org-restrict)
  15841. "Restricted to single file"
  15842. "Edit File List")
  15843. '(org-edit-agenda-file-list)
  15844. (not (get 'org-agenda-files 'org-restrict)))
  15845. "--")
  15846. (mapcar 'org-file-menu-entry (org-agenda-files))))
  15847. (org-agenda-set-mode-name)
  15848. (apply
  15849. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  15850. (list 'org-agenda-mode-hook)))
  15851. (substitute-key-definition 'undo 'org-agenda-undo
  15852. org-agenda-mode-map global-map)
  15853. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  15854. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  15855. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  15856. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  15857. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  15858. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  15859. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  15860. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  15861. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  15862. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  15863. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  15864. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  15865. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  15866. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  15867. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  15868. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  15869. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  15870. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  15871. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  15872. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  15873. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  15874. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  15875. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  15876. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  15877. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  15878. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  15879. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  15880. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  15881. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  15882. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  15883. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  15884. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  15885. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  15886. (while l (org-defkey org-agenda-mode-map
  15887. (int-to-string (pop l)) 'digit-argument)))
  15888. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  15889. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  15890. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  15891. (org-defkey org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  15892. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  15893. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  15894. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  15895. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  15896. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  15897. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  15898. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  15899. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  15900. (org-defkey org-agenda-mode-map "n" 'next-line)
  15901. (org-defkey org-agenda-mode-map "p" 'previous-line)
  15902. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  15903. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  15904. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  15905. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  15906. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  15907. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  15908. (eval-after-load "calendar"
  15909. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  15910. 'org-calendar-goto-agenda))
  15911. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  15912. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  15913. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  15914. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  15915. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  15916. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  15917. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  15918. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  15919. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  15920. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  15921. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  15922. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15923. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  15924. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  15925. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  15926. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  15927. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  15928. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  15929. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  15930. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  15931. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  15932. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  15933. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  15934. "Local keymap for agenda entries from Org-mode.")
  15935. (org-defkey org-agenda-keymap
  15936. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  15937. (org-defkey org-agenda-keymap
  15938. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  15939. (when org-agenda-mouse-1-follows-link
  15940. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  15941. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  15942. '("Agenda"
  15943. ("Agenda Files")
  15944. "--"
  15945. ["Show" org-agenda-show t]
  15946. ["Go To (other window)" org-agenda-goto t]
  15947. ["Go To (this window)" org-agenda-switch-to t]
  15948. ["Follow Mode" org-agenda-follow-mode
  15949. :style toggle :selected org-agenda-follow-mode :active t]
  15950. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  15951. "--"
  15952. ["Cycle TODO" org-agenda-todo t]
  15953. ["Archive subtree" org-agenda-archive t]
  15954. ["Delete subtree" org-agenda-kill t]
  15955. "--"
  15956. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  15957. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  15958. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  15959. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  15960. "--"
  15961. ("Tags and Properties"
  15962. ["Show all Tags" org-agenda-show-tags t]
  15963. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  15964. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  15965. "--"
  15966. ["Column View" org-columns t])
  15967. ("Date/Schedule"
  15968. ["Schedule" org-agenda-schedule t]
  15969. ["Set Deadline" org-agenda-deadline t]
  15970. "--"
  15971. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  15972. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  15973. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  15974. ("Clock"
  15975. ["Clock in" org-agenda-clock-in t]
  15976. ["Clock out" org-agenda-clock-out t]
  15977. ["Clock cancel" org-agenda-clock-cancel t]
  15978. ["Goto running clock" org-clock-goto t])
  15979. ("Priority"
  15980. ["Set Priority" org-agenda-priority t]
  15981. ["Increase Priority" org-agenda-priority-up t]
  15982. ["Decrease Priority" org-agenda-priority-down t]
  15983. ["Show Priority" org-agenda-show-priority t])
  15984. ("Calendar/Diary"
  15985. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  15986. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  15987. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  15988. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  15989. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  15990. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  15991. "--"
  15992. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  15993. "--"
  15994. ("View"
  15995. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  15996. :style radio :selected (equal org-agenda-ndays 1)]
  15997. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  15998. :style radio :selected (equal org-agenda-ndays 7)]
  15999. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  16000. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  16001. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  16002. :style radio :selected (member org-agenda-ndays '(365 366))]
  16003. "--"
  16004. ["Show Logbook entries" org-agenda-log-mode
  16005. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  16006. ["Include Diary" org-agenda-toggle-diary
  16007. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  16008. ["Use Time Grid" org-agenda-toggle-time-grid
  16009. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  16010. ["Write view to file" org-write-agenda t]
  16011. ["Rebuild buffer" org-agenda-redo t]
  16012. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  16013. "--"
  16014. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  16015. "--"
  16016. ["Quit" org-agenda-quit t]
  16017. ["Exit and Release Buffers" org-agenda-exit t]
  16018. ))
  16019. ;;; Agenda undo
  16020. (defvar org-agenda-allow-remote-undo t
  16021. "Non-nil means, allow remote undo from the agenda buffer.")
  16022. (defvar org-agenda-undo-list nil
  16023. "List of undoable operations in the agenda since last refresh.")
  16024. (defvar org-agenda-undo-has-started-in nil
  16025. "Buffers that have already seen `undo-start' in the current undo sequence.")
  16026. (defvar org-agenda-pending-undo-list nil
  16027. "In a series of undo commands, this is the list of remaning undo items.")
  16028. (defmacro org-if-unprotected (&rest body)
  16029. "Execute BODY if there is no `org-protected' text property at point."
  16030. (declare (debug t))
  16031. `(unless (get-text-property (point) 'org-protected)
  16032. ,@body))
  16033. (defmacro org-with-remote-undo (_buffer &rest _body)
  16034. "Execute BODY while recording undo information in two buffers."
  16035. (declare (indent 1) (debug t))
  16036. `(let ((_cline (org-current-line))
  16037. (_cmd this-command)
  16038. (_buf1 (current-buffer))
  16039. (_buf2 ,_buffer)
  16040. (_undo1 buffer-undo-list)
  16041. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  16042. _c1 _c2)
  16043. ,@_body
  16044. (when org-agenda-allow-remote-undo
  16045. (setq _c1 (org-verify-change-for-undo
  16046. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  16047. _c2 (org-verify-change-for-undo
  16048. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  16049. (when (or _c1 _c2)
  16050. ;; make sure there are undo boundaries
  16051. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  16052. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  16053. ;; remember which buffer to undo
  16054. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  16055. org-agenda-undo-list)))))
  16056. (defun org-agenda-undo ()
  16057. "Undo a remote editing step in the agenda.
  16058. This undoes changes both in the agenda buffer and in the remote buffer
  16059. that have been changed along."
  16060. (interactive)
  16061. (or org-agenda-allow-remote-undo
  16062. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  16063. (if (not (eq this-command last-command))
  16064. (setq org-agenda-undo-has-started-in nil
  16065. org-agenda-pending-undo-list org-agenda-undo-list))
  16066. (if (not org-agenda-pending-undo-list)
  16067. (error "No further undo information"))
  16068. (let* ((entry (pop org-agenda-pending-undo-list))
  16069. buf line cmd rembuf)
  16070. (setq cmd (pop entry) line (pop entry))
  16071. (setq rembuf (nth 2 entry))
  16072. (org-with-remote-undo rembuf
  16073. (while (bufferp (setq buf (pop entry)))
  16074. (if (pop entry)
  16075. (with-current-buffer buf
  16076. (let ((last-undo-buffer buf)
  16077. (inhibit-read-only t))
  16078. (unless (memq buf org-agenda-undo-has-started-in)
  16079. (push buf org-agenda-undo-has-started-in)
  16080. (make-local-variable 'pending-undo-list)
  16081. (undo-start))
  16082. (while (and pending-undo-list
  16083. (listp pending-undo-list)
  16084. (not (car pending-undo-list)))
  16085. (pop pending-undo-list))
  16086. (undo-more 1))))))
  16087. (goto-line line)
  16088. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  16089. (defun org-verify-change-for-undo (l1 l2)
  16090. "Verify that a real change occurred between the undo lists L1 and L2."
  16091. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  16092. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  16093. (not (eq l1 l2)))
  16094. ;;; Agenda dispatch
  16095. (defvar org-agenda-restrict nil)
  16096. (defvar org-agenda-restrict-begin (make-marker))
  16097. (defvar org-agenda-restrict-end (make-marker))
  16098. (defvar org-agenda-last-dispatch-buffer nil)
  16099. ;;;###autoload
  16100. (defun org-agenda (arg)
  16101. "Dispatch agenda commands to collect entries to the agenda buffer.
  16102. Prompts for a character to select a command. Any prefix arg will be passed
  16103. on to the selected command. The default selections are:
  16104. a Call `org-agenda-list' to display the agenda for current day or week.
  16105. t Call `org-todo-list' to display the global todo list.
  16106. T Call `org-todo-list' to display the global todo list, select only
  16107. entries with a specific TODO keyword (the user gets a prompt).
  16108. m Call `org-tags-view' to display headlines with tags matching
  16109. a condition (the user is prompted for the condition).
  16110. M Like `m', but select only TODO entries, no ordinary headlines.
  16111. L Create a timeline for the current buffer.
  16112. e Export views to associated files.
  16113. More commands can be added by configuring the variable
  16114. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  16115. searches can be pre-defined in this way.
  16116. If the current buffer is in Org-mode and visiting a file, you can also
  16117. first press `1' to indicate that the agenda should be temporarily (until the
  16118. next use of \\[org-agenda]) restricted to the current file."
  16119. (interactive "P")
  16120. (catch 'exit
  16121. (let* ((buf (current-buffer))
  16122. (bfn (buffer-file-name (buffer-base-buffer)))
  16123. (restrict-ok (and bfn (org-mode-p)))
  16124. (custom org-agenda-custom-commands)
  16125. c entry key type match lprops)
  16126. ;; Turn off restriction
  16127. (put 'org-agenda-files 'org-restrict nil)
  16128. (setq org-agenda-restrict nil)
  16129. (move-marker org-agenda-restrict-begin nil)
  16130. (move-marker org-agenda-restrict-end nil)
  16131. ;; Delete old local properties
  16132. (put 'org-agenda-redo-command 'org-lprops nil)
  16133. ;; Remember where this call originated
  16134. (setq org-agenda-last-dispatch-buffer (current-buffer))
  16135. (save-window-excursion
  16136. (delete-other-windows)
  16137. (org-switch-to-buffer-other-window " *Agenda Commands*")
  16138. (erase-buffer)
  16139. (insert (eval-when-compile
  16140. (let ((header
  16141. "Press key for an agenda command:
  16142. -------------------------------- C Configure custom agenda commands
  16143. a Agenda for current week or day e Export agenda views
  16144. t List of all TODO entries T Entries with special TODO kwd
  16145. m Match a TAGS query M Like m, but only TODO entries
  16146. L Timeline for current buffer # List stuck projects (!=configure)
  16147. / Multi-occur
  16148. ")
  16149. (start 0))
  16150. (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start)
  16151. (setq start (match-end 0))
  16152. (add-text-properties (match-beginning 2) (match-end 2)
  16153. '(face bold) header))
  16154. header)))
  16155. (while (setq entry (pop custom))
  16156. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  16157. (insert (format "\n%-4s%-14s: %s"
  16158. (org-add-props (copy-sequence key)
  16159. '(face bold))
  16160. (cond
  16161. ((stringp type) type)
  16162. ((eq type 'agenda) "Agenda for current week or day")
  16163. ((eq type 'alltodo) "List of all TODO entries")
  16164. ((eq type 'stuck) "List of stuck projects")
  16165. ((eq type 'todo) "TODO keyword")
  16166. ((eq type 'tags) "Tags query")
  16167. ((eq type 'tags-todo) "Tags (TODO)")
  16168. ((eq type 'tags-tree) "Tags tree")
  16169. ((eq type 'todo-tree) "TODO kwd tree")
  16170. ((eq type 'occur-tree) "Occur tree")
  16171. ((functionp type) (symbol-name type))
  16172. (t "???"))
  16173. (if (stringp match)
  16174. (org-add-props match nil 'face 'org-warning)
  16175. (format "set of %d commands" (length match))))))
  16176. (if restrict-ok
  16177. (insert "\n"
  16178. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  16179. (goto-char (point-min))
  16180. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  16181. (message "Press key for agenda command%s"
  16182. (if restrict-ok ", or [1] or [0] to restrict" ""))
  16183. (setq c (read-char-exclusive))
  16184. (message "")
  16185. (when (memq c '(?L ?1 ?0))
  16186. (if restrict-ok
  16187. (put 'org-agenda-files 'org-restrict (list bfn))
  16188. (error "Cannot restrict agenda to current buffer"))
  16189. (with-current-buffer " *Agenda Commands*"
  16190. (goto-char (point-max))
  16191. (delete-region (point-at-bol) (point))
  16192. (goto-char (point-min)))
  16193. (when (eq c ?0)
  16194. (setq org-agenda-restrict t)
  16195. (with-current-buffer buf
  16196. (if (org-region-active-p)
  16197. (progn
  16198. (move-marker org-agenda-restrict-begin (region-beginning))
  16199. (move-marker org-agenda-restrict-end (region-end)))
  16200. (save-excursion
  16201. (org-back-to-heading t)
  16202. (move-marker org-agenda-restrict-begin (point))
  16203. (move-marker org-agenda-restrict-end
  16204. (progn (org-end-of-subtree t)))))))
  16205. (unless (eq c ?L)
  16206. (message "Press key for agenda command%s"
  16207. (if restrict-ok " (restricted to current file)" ""))
  16208. (setq c (read-char-exclusive)))
  16209. (message "")))
  16210. (require 'calendar) ; FIXME: can we avoid this for some commands?
  16211. ;; For example the todo list should not need it (but does...)
  16212. (cond
  16213. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  16214. (if (symbolp (nth 1 entry))
  16215. (progn
  16216. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  16217. lprops (nth 3 entry))
  16218. (put 'org-agenda-redo-command 'org-lprops lprops)
  16219. (cond
  16220. ((eq type 'agenda)
  16221. (org-let lprops '(org-agenda-list current-prefix-arg)))
  16222. ((eq type 'alltodo)
  16223. (org-let lprops '(org-todo-list current-prefix-arg)))
  16224. ((eq type 'stuck)
  16225. (org-let lprops '(org-agenda-list-stuck-projects
  16226. current-prefix-arg)))
  16227. ((eq type 'tags)
  16228. (org-let lprops '(org-tags-view current-prefix-arg match)))
  16229. ((eq type 'tags-todo)
  16230. (org-let lprops '(org-tags-view '(4) match)))
  16231. ((eq type 'todo)
  16232. (org-let lprops '(org-todo-list match)))
  16233. ((eq type 'tags-tree)
  16234. (org-check-for-org-mode)
  16235. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  16236. ((eq type 'todo-tree)
  16237. (org-check-for-org-mode)
  16238. (org-let lprops
  16239. '(org-occur (concat "^" outline-regexp "[ \t]*"
  16240. (regexp-quote match) "\\>"))))
  16241. ((eq type 'occur-tree)
  16242. (org-check-for-org-mode)
  16243. (org-let lprops '(org-occur match)))
  16244. ((fboundp type)
  16245. (org-let lprops '(funcall type match)))
  16246. (t (error "Invalid custom agenda command type %s" type))))
  16247. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  16248. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  16249. ((equal c ?a) (call-interactively 'org-agenda-list))
  16250. ((equal c ?t) (call-interactively 'org-todo-list))
  16251. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  16252. ((equal c ?m) (call-interactively 'org-tags-view))
  16253. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  16254. ((equal c ?e) (call-interactively 'org-store-agenda-views))
  16255. ((equal c ?L)
  16256. (unless restrict-ok
  16257. (error "This is not an Org-mode file"))
  16258. (org-call-with-arg 'org-timeline arg))
  16259. ((equal c ?#) (call-interactively 'org-agenda-list-stuck-projects))
  16260. ((equal c ?/) (call-interactively 'org-occur-in-agenda-files))
  16261. ((equal c ?!) (customize-variable 'org-stuck-projects))
  16262. (t (error "Invalid key"))))))
  16263. (defun org-run-agenda-series (name series)
  16264. (org-prepare-agenda name)
  16265. (let* ((org-agenda-multi t)
  16266. (redo (list 'org-run-agenda-series name (list 'quote series)))
  16267. (cmds (car series))
  16268. (gprops (nth 1 series))
  16269. match ;; The byte compiler incorrectly complains about this. Keep it!
  16270. cmd type lprops)
  16271. (while (setq cmd (pop cmds))
  16272. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  16273. (cond
  16274. ((eq type 'agenda)
  16275. (org-let2 gprops lprops
  16276. '(call-interactively 'org-agenda-list)))
  16277. ((eq type 'alltodo)
  16278. (org-let2 gprops lprops
  16279. '(call-interactively 'org-todo-list)))
  16280. ((eq type 'stuck)
  16281. (org-let2 gprops lprops
  16282. '(call-interactively 'org-agenda-list-stuck-projects)))
  16283. ((eq type 'tags)
  16284. (org-let2 gprops lprops
  16285. '(org-tags-view current-prefix-arg match)))
  16286. ((eq type 'tags-todo)
  16287. (org-let2 gprops lprops
  16288. '(org-tags-view '(4) match)))
  16289. ((eq type 'todo)
  16290. (org-let2 gprops lprops
  16291. '(org-todo-list match)))
  16292. ((fboundp type)
  16293. (org-let2 gprops lprops
  16294. '(funcall type match)))
  16295. (t (error "Invalid type in command series"))))
  16296. (widen)
  16297. (setq org-agenda-redo-command redo)
  16298. (goto-char (point-min)))
  16299. (org-finalize-agenda))
  16300. ;;;###autoload
  16301. (defmacro org-batch-agenda (cmd-key &rest parameters)
  16302. "Run an agenda command in batch mode and send the result to STDOUT.
  16303. If CMD-KEY is a string of length 1, it is used as a key in
  16304. `org-agenda-custom-commands' and triggers this command. If it is a
  16305. longer string is is used as a tags/todo match string.
  16306. Paramters are alternating variable names and values that will be bound
  16307. before running the agenda command."
  16308. (let (pars)
  16309. (while parameters
  16310. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  16311. (if (> (length cmd-key) 1)
  16312. (eval (list 'let (nreverse pars)
  16313. (list 'org-tags-view nil cmd-key)))
  16314. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  16315. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  16316. (set-buffer org-agenda-buffer-name)
  16317. (princ (org-encode-for-stdout (buffer-string)))))
  16318. (defun org-encode-for-stdout (string)
  16319. (if (fboundp 'encode-coding-string)
  16320. (encode-coding-string string buffer-file-coding-system)
  16321. string))
  16322. (defvar org-agenda-info nil)
  16323. ;;;###autoload
  16324. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  16325. "Run an agenda command in batch mode and send the result to STDOUT.
  16326. If CMD-KEY is a string of length 1, it is used as a key in
  16327. `org-agenda-custom-commands' and triggers this command. If it is a
  16328. longer string is is used as a tags/todo match string.
  16329. Paramters are alternating variable names and values that will be bound
  16330. before running the agenda command.
  16331. The output gives a line for each selected agenda item. Each
  16332. item is a list of comma-separated values, like this:
  16333. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  16334. category The category of the item
  16335. head The headline, without TODO kwd, TAGS and PRIORITY
  16336. type The type of the agenda entry, can be
  16337. todo selected in TODO match
  16338. tagsmatch selected in tags match
  16339. diary imported from diary
  16340. deadline a deadline on given date
  16341. scheduled scheduled on given date
  16342. timestamp entry has timestamp on given date
  16343. closed entry was closed on given date
  16344. upcoming-deadline warning about deadline
  16345. past-scheduled forwarded scheduled item
  16346. block entry has date block including g. date
  16347. todo The todo keyword, if any
  16348. tags All tags including inherited ones, separated by colons
  16349. date The relevant date, like 2007-2-14
  16350. time The time, like 15:00-16:50
  16351. extra Sting with extra planning info
  16352. priority-l The priority letter if any was given
  16353. priority-n The computed numerical priority
  16354. agenda-day The day in the agenda where this is listed"
  16355. (let (pars)
  16356. (while parameters
  16357. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  16358. (push (list 'org-agenda-remove-tags t) pars)
  16359. (if (> (length cmd-key) 1)
  16360. (eval (list 'let (nreverse pars)
  16361. (list 'org-tags-view nil cmd-key)))
  16362. (flet ((read-char-exclusive () (string-to-char cmd-key)))
  16363. (eval (list 'let (nreverse pars) '(org-agenda nil)))))
  16364. (set-buffer org-agenda-buffer-name)
  16365. (let* ((lines (org-split-string (buffer-string) "\n"))
  16366. line)
  16367. (while (setq line (pop lines))
  16368. (catch 'next
  16369. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  16370. (setq org-agenda-info
  16371. (org-fix-agenda-info (text-properties-at 0 line)))
  16372. (princ
  16373. (org-encode-for-stdout
  16374. (mapconcat 'org-agenda-export-csv-mapper
  16375. '(org-category txt type todo tags date time-of-day extra
  16376. priority-letter priority agenda-day)
  16377. ",")))
  16378. (princ "\n"))))))
  16379. (defun org-fix-agenda-info (props)
  16380. "Make sure all properties on an agenda item have a canonical form,
  16381. so the the export commands caneasily use it."
  16382. (let (tmp re)
  16383. (when (setq tmp (plist-get props 'tags))
  16384. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  16385. (when (setq tmp (plist-get props 'date))
  16386. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  16387. (let ((calendar-date-display-form '(year "-" month "-" day)))
  16388. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  16389. (setq tmp (calendar-date-string tmp)))
  16390. (setq props (plist-put props 'date tmp)))
  16391. (when (setq tmp (plist-get props 'day))
  16392. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  16393. (let ((calendar-date-display-form '(year "-" month "-" day)))
  16394. (setq tmp (calendar-date-string tmp)))
  16395. (setq props (plist-put props 'day tmp))
  16396. (setq props (plist-put props 'agenda-day tmp)))
  16397. (when (setq tmp (plist-get props 'txt))
  16398. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  16399. (plist-put props 'priority-letter (match-string 1 tmp))
  16400. (setq tmp (replace-match "" t t tmp)))
  16401. (when (and (setq re (plist-get props 'org-todo-regexp))
  16402. (setq re (concat "\\`\\.*" re " ?"))
  16403. (string-match re tmp))
  16404. (plist-put props 'todo (match-string 1 tmp))
  16405. (setq tmp (replace-match "" t t tmp)))
  16406. (plist-put props 'txt tmp)))
  16407. props)
  16408. (defun org-agenda-export-csv-mapper (prop)
  16409. (let ((res (plist-get org-agenda-info prop)))
  16410. (setq res
  16411. (cond
  16412. ((not res) "")
  16413. ((stringp res) res)
  16414. (t (prin1-to-string res))))
  16415. (while (string-match "," res)
  16416. (setq res (replace-match ";" t t res)))
  16417. (org-trim res)))
  16418. ;;;###autoload
  16419. (defun org-store-agenda-views (&rest parameters)
  16420. (interactive)
  16421. (eval (list 'org-batch-store-agenda-views)))
  16422. ;; FIXME, why is this a macro?????
  16423. ;;;###autoload
  16424. (defmacro org-batch-store-agenda-views (&rest parameters)
  16425. "Run all custom agenda commands that have a file argument."
  16426. (let ((cmds org-agenda-custom-commands)
  16427. (pop-up-frames nil)
  16428. (dir default-directory)
  16429. pars cmd thiscmdkey files opts)
  16430. (while parameters
  16431. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  16432. (setq pars (reverse pars))
  16433. (save-window-excursion
  16434. (while cmds
  16435. (setq cmd (pop cmds)
  16436. thiscmdkey (car cmd)
  16437. opts (nth 3 cmd)
  16438. files (nth 4 cmd))
  16439. (if (stringp files) (setq files (list files)))
  16440. (when files
  16441. (flet ((read-char-exclusive () (string-to-char thiscmdkey)))
  16442. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  16443. '(org-agenda nil))))
  16444. (set-buffer org-agenda-buffer-name)
  16445. (while files
  16446. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  16447. (list 'org-write-agenda
  16448. (expand-file-name (pop files) dir) t))))
  16449. (and (get-buffer org-agenda-buffer-name)
  16450. (kill-buffer org-agenda-buffer-name)))))))
  16451. (defun org-write-agenda (file &optional nosettings)
  16452. "Write the current buffer (an agenda view) as a file.
  16453. Depending on the extension of the file name, plain text (.txt),
  16454. HTML (.html or .htm) or Postscript (.ps) is produced.
  16455. If NOSETTINGS is given, do not scope the settings of
  16456. `org-agenda-exporter-settings' into the export commands. This is used when
  16457. the settings have already been scoped and we do not wish to overrule other,
  16458. higher priority settings."
  16459. (interactive "FWrite agenda to file: ")
  16460. (if (not (file-writable-p file))
  16461. (error "Cannot write agenda to file %s" file))
  16462. (cond
  16463. ((string-match "\\.html?\\'" file) (require 'htmlize))
  16464. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  16465. (org-let (if nosettings nil org-agenda-exporter-settings)
  16466. '(save-excursion
  16467. (save-window-excursion
  16468. (cond
  16469. ((string-match "\\.html?\\'" file)
  16470. (set-buffer (htmlize-buffer (current-buffer)))
  16471. (when (and org-agenda-export-html-style
  16472. (string-match "<style>" org-agenda-export-html-style))
  16473. ;; replace <style> section with org-agenda-export-html-style
  16474. (goto-char (point-min))
  16475. (kill-region (- (search-forward "<style") 6)
  16476. (search-forward "</style>"))
  16477. (insert org-agenda-export-html-style))
  16478. (write-file file)
  16479. (kill-buffer (current-buffer))
  16480. (message "HTML written to %s" file))
  16481. ((string-match "\\.ps\\'" file)
  16482. (ps-print-buffer-with-faces file)
  16483. (message "Postscript written to %s" file))
  16484. (t
  16485. (let ((bs (buffer-string)))
  16486. (find-file file)
  16487. (insert bs)
  16488. (save-buffer 0)
  16489. (kill-buffer (current-buffer))
  16490. (message "Plain text written to %s" file))))))
  16491. (set-buffer org-agenda-buffer-name)))
  16492. (defmacro org-no-read-only (&rest body)
  16493. "Inhibit read-only for BODY."
  16494. `(let ((inhibit-read-only t)) ,@body))
  16495. (defun org-check-for-org-mode ()
  16496. "Make sure current buffer is in org-mode. Error if not."
  16497. (or (org-mode-p)
  16498. (error "Cannot execute org-mode agenda command on buffer in %s."
  16499. major-mode)))
  16500. (defun org-fit-agenda-window ()
  16501. "Fit the window to the buffer size."
  16502. (and (memq org-agenda-window-setup '(reorganize-frame))
  16503. (fboundp 'fit-window-to-buffer)
  16504. (fit-window-to-buffer)))
  16505. ;;; Agenda file list
  16506. (defun org-agenda-files (&optional unrestricted)
  16507. "Get the list of agenda files.
  16508. Optional UNRESTRICTED means return the full list even if a restriction
  16509. is currently in place."
  16510. (let ((files
  16511. (cond
  16512. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16513. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16514. ((listp org-agenda-files) org-agenda-files)
  16515. (t (error "Invalid value of `org-agenda-files'")))))
  16516. (if org-agenda-skip-unavailable-files
  16517. (delq nil
  16518. (mapcar (function
  16519. (lambda (file)
  16520. (and (file-readable-p file) file)))
  16521. files))
  16522. files))) ; `org-check-agenda-file' will remove them from the list
  16523. (defun org-edit-agenda-file-list ()
  16524. "Edit the list of agenda files.
  16525. Depending on setup, this either uses customize to edit the variable
  16526. `org-agenda-files', or it visits the file that is holding the list. In the
  16527. latter case, the buffer is set up in a way that saving it automatically kills
  16528. the buffer and restores the previous window configuration."
  16529. (interactive)
  16530. (if (stringp org-agenda-files)
  16531. (let ((cw (current-window-configuration)))
  16532. (find-file org-agenda-files)
  16533. (org-set-local 'org-window-configuration cw)
  16534. (org-add-hook 'after-save-hook
  16535. (lambda ()
  16536. (set-window-configuration
  16537. (prog1 org-window-configuration
  16538. (kill-buffer (current-buffer))))
  16539. (org-install-agenda-files-menu)
  16540. (message "New agenda file list installed"))
  16541. nil 'local)
  16542. (message (substitute-command-keys
  16543. "Edit list and finish with \\[save-buffer]")))
  16544. (customize-variable 'org-agenda-files)))
  16545. (defun org-store-new-agenda-file-list (list)
  16546. "Set new value for the agenda file list and save it correcly."
  16547. (if (stringp org-agenda-files)
  16548. (let ((f org-agenda-files) b)
  16549. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  16550. (with-temp-file f
  16551. (insert (mapconcat 'identity list "\n") "\n")))
  16552. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  16553. (setq org-agenda-files list)
  16554. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16555. (defun org-read-agenda-file-list ()
  16556. "Read the list of agenda files from a file."
  16557. (when (stringp org-agenda-files)
  16558. (with-temp-buffer
  16559. (insert-file-contents org-agenda-files)
  16560. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  16561. ;;;###autoload
  16562. (defun org-cycle-agenda-files ()
  16563. "Cycle through the files in `org-agenda-files'.
  16564. If the current buffer visits an agenda file, find the next one in the list.
  16565. If the current buffer does not, find the first agenda file."
  16566. (interactive)
  16567. (let* ((fs (org-agenda-files t))
  16568. (files (append fs (list (car fs))))
  16569. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16570. file)
  16571. (unless files (error "No agenda files"))
  16572. (catch 'exit
  16573. (while (setq file (pop files))
  16574. (if (equal (file-truename file) tcf)
  16575. (when (car files)
  16576. (find-file (car files))
  16577. (throw 'exit t))))
  16578. (find-file (car fs)))
  16579. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  16580. (defun org-agenda-file-to-front (&optional to-end)
  16581. "Move/add the current file to the top of the agenda file list.
  16582. If the file is not present in the list, it is added to the front. If it is
  16583. present, it is moved there. With optional argument TO-END, add/move to the
  16584. end of the list."
  16585. (interactive "P")
  16586. (let ((org-agenda-skip-unavailable-files nil)
  16587. (file-alist (mapcar (lambda (x)
  16588. (cons (file-truename x) x))
  16589. (org-agenda-files t)))
  16590. (ctf (file-truename buffer-file-name))
  16591. x had)
  16592. (setq x (assoc ctf file-alist) had x)
  16593. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16594. (if to-end
  16595. (setq file-alist (append (delq x file-alist) (list x)))
  16596. (setq file-alist (cons x (delq x file-alist))))
  16597. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16598. (org-install-agenda-files-menu)
  16599. (message "File %s to %s of agenda file list"
  16600. (if had "moved" "added") (if to-end "end" "front"))))
  16601. (defun org-remove-file (&optional file)
  16602. "Remove current file from the list of files in variable `org-agenda-files'.
  16603. These are the files which are being checked for agenda entries.
  16604. Optional argument FILE means, use this file instead of the current."
  16605. (interactive)
  16606. (let* ((org-agenda-skip-unavailable-files nil)
  16607. (file (or file buffer-file-name))
  16608. (true-file (file-truename file))
  16609. (afile (abbreviate-file-name file))
  16610. (files (delq nil (mapcar
  16611. (lambda (x)
  16612. (if (equal true-file
  16613. (file-truename x))
  16614. nil x))
  16615. (org-agenda-files t)))))
  16616. (if (not (= (length files) (length (org-agenda-files t))))
  16617. (progn
  16618. (org-store-new-agenda-file-list files)
  16619. (org-install-agenda-files-menu)
  16620. (message "Removed file: %s" afile))
  16621. (message "File was not in list: %s" afile))))
  16622. (defun org-file-menu-entry (file)
  16623. (vector file (list 'find-file file) t))
  16624. (defun org-check-agenda-file (file)
  16625. "Make sure FILE exists. If not, ask user what to do."
  16626. (when (not (file-exists-p file))
  16627. (message "non-existent file %s. [R]emove from list or [A]bort?"
  16628. (abbreviate-file-name file))
  16629. (let ((r (downcase (read-char-exclusive))))
  16630. (cond
  16631. ((equal r ?r)
  16632. (org-remove-file file)
  16633. (throw 'nextfile t))
  16634. (t (error "Abort"))))))
  16635. ;;; Agenda prepare and finalize
  16636. (defvar org-agenda-multi nil) ; dynammically scoped
  16637. (defvar org-agenda-buffer-name "*Org Agenda*")
  16638. (defvar org-pre-agenda-window-conf nil)
  16639. (defvar org-agenda-name nil)
  16640. (defun org-prepare-agenda (&optional name)
  16641. (setq org-todo-keywords-for-agenda nil)
  16642. (setq org-done-keywords-for-agenda nil)
  16643. (if org-agenda-multi
  16644. (progn
  16645. (setq buffer-read-only nil)
  16646. (goto-char (point-max))
  16647. (unless (or (bobp) org-agenda-compact-blocks)
  16648. (insert "\n" (make-string (window-width) ?=) "\n"))
  16649. (narrow-to-region (point) (point-max)))
  16650. (org-agenda-maybe-reset-markers 'force)
  16651. (org-prepare-agenda-buffers (org-agenda-files))
  16652. (setq org-todo-keywords-for-agenda
  16653. (org-uniquify org-todo-keywords-for-agenda))
  16654. (setq org-done-keywords-for-agenda
  16655. (org-uniquify org-done-keywords-for-agenda))
  16656. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  16657. (awin (get-buffer-window abuf)))
  16658. (cond
  16659. ((equal (current-buffer) abuf) nil)
  16660. (awin (select-window awin))
  16661. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  16662. ((equal org-agenda-window-setup 'current-window)
  16663. (switch-to-buffer abuf))
  16664. ((equal org-agenda-window-setup 'other-window)
  16665. (org-switch-to-buffer-other-window abuf))
  16666. ((equal org-agenda-window-setup 'other-frame)
  16667. (switch-to-buffer-other-frame abuf))
  16668. ((equal org-agenda-window-setup 'reorganize-frame)
  16669. (delete-other-windows)
  16670. (org-switch-to-buffer-other-window abuf))))
  16671. (setq buffer-read-only nil)
  16672. (erase-buffer)
  16673. (org-agenda-mode)
  16674. (and name (not org-agenda-name)
  16675. (org-set-local 'org-agenda-name name)))
  16676. (setq buffer-read-only nil))
  16677. (defun org-finalize-agenda ()
  16678. "Finishing touch for the agenda buffer, called just before displaying it."
  16679. (unless org-agenda-multi
  16680. (save-excursion
  16681. (let ((inhibit-read-only t))
  16682. (goto-char (point-min))
  16683. (while (org-activate-bracket-links (point-max))
  16684. (add-text-properties (match-beginning 0) (match-end 0)
  16685. '(face org-link)))
  16686. (org-agenda-align-tags)
  16687. (unless org-agenda-with-colors
  16688. (remove-text-properties (point-min) (point-max) '(face nil))))
  16689. (if (and (boundp 'org-overriding-columns-format)
  16690. org-overriding-columns-format)
  16691. (org-set-local 'org-overriding-columns-format
  16692. org-overriding-columns-format))
  16693. (if (and (boundp 'org-agenda-view-columns-initially)
  16694. org-agenda-view-columns-initially)
  16695. (org-agenda-columns))
  16696. (when org-agenda-fontify-priorities
  16697. (org-fontify-priorities))
  16698. (run-hooks 'org-finalize-agenda-hook))))
  16699. (defun org-fontify-priorities ()
  16700. "Make highest priority lines bold, and lowest italic."
  16701. (interactive)
  16702. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  16703. (org-delete-overlay o)))
  16704. (overlays-in (point-min) (point-max)))
  16705. (save-excursion
  16706. (let ((ovs (org-overlays-in (point-min) (point-max)))
  16707. (inhibit-read-only t)
  16708. b e p ov h l)
  16709. (goto-char (point-min))
  16710. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  16711. (setq h (or (get-char-property (point) 'org-highest-priority)
  16712. org-highest-priority)
  16713. l (or (get-char-property (point) 'org-lowest-priority)
  16714. org-lowest-priority)
  16715. p (string-to-char (match-string 1))
  16716. b (match-beginning 0) e (line-end-position)
  16717. ov (org-make-overlay b e))
  16718. (org-overlay-put ov 'face (cond ((equal p l) 'italic)
  16719. ((equal p h) 'bold)))
  16720. (org-overlay-put ov 'org-type 'org-priority)))))
  16721. (defun org-prepare-agenda-buffers (files)
  16722. "Create buffers for all agenda files, protect archived trees and comments."
  16723. (interactive)
  16724. (let ((pa '(:org-archived t))
  16725. (pc '(:org-comment t))
  16726. (pall '(:org-archived t :org-comment t))
  16727. (inhibit-read-only t)
  16728. (rea (concat ":" org-archive-tag ":"))
  16729. bmp file re)
  16730. (save-excursion
  16731. (save-restriction
  16732. (while (setq file (pop files))
  16733. (org-check-agenda-file file)
  16734. (set-buffer (org-get-agenda-file-buffer file))
  16735. (widen)
  16736. (setq bmp (buffer-modified-p))
  16737. (org-refresh-category-properties)
  16738. (setq org-todo-keywords-for-agenda
  16739. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16740. (setq org-done-keywords-for-agenda
  16741. (append org-done-keywords-for-agenda org-done-keywords))
  16742. (save-excursion
  16743. (remove-text-properties (point-min) (point-max) pall)
  16744. (when org-agenda-skip-archived-trees
  16745. (goto-char (point-min))
  16746. (while (re-search-forward rea nil t)
  16747. (if (org-on-heading-p t)
  16748. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16749. (goto-char (point-min))
  16750. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  16751. (while (re-search-forward re nil t)
  16752. (add-text-properties
  16753. (match-beginning 0) (org-end-of-subtree t) pc)))
  16754. (set-buffer-modified-p bmp))))))
  16755. (defvar org-agenda-skip-function nil
  16756. "Function to be called at each match during agenda construction.
  16757. If this function returns nil, the current match should not be skipped.
  16758. Otherwise, the function must return a position from where the search
  16759. should be continued.
  16760. This may also be a Lisp form, it will be evaluated.
  16761. Never set this variable using `setq' or so, because then it will apply
  16762. to all future agenda commands. Instead, bind it with `let' to scope
  16763. it dynamically into the agenda-constructing command. A good way to set
  16764. it is through options in org-agenda-custom-commands.")
  16765. (defun org-agenda-skip ()
  16766. "Throw to `:skip' in places that should be skipped.
  16767. Also moves point to the end of the skipped region, so that search can
  16768. continue from there."
  16769. (let ((p (point-at-bol)) to fp)
  16770. (and org-agenda-skip-archived-trees
  16771. (get-text-property p :org-archived)
  16772. (org-end-of-subtree t)
  16773. (throw :skip t))
  16774. (and (get-text-property p :org-comment)
  16775. (org-end-of-subtree t)
  16776. (throw :skip t))
  16777. (if (equal (char-after p) ?#) (throw :skip t))
  16778. (when (and (or (setq fp (functionp org-agenda-skip-function))
  16779. (consp org-agenda-skip-function))
  16780. (setq to (save-excursion
  16781. (save-match-data
  16782. (if fp
  16783. (funcall org-agenda-skip-function)
  16784. (eval org-agenda-skip-function))))))
  16785. (goto-char to)
  16786. (throw :skip t))))
  16787. (defvar org-agenda-markers nil
  16788. "List of all currently active markers created by `org-agenda'.")
  16789. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  16790. "Creation time of the last agenda marker.")
  16791. (defun org-agenda-new-marker (&optional pos)
  16792. "Return a new agenda marker.
  16793. Org-mode keeps a list of these markers and resets them when they are
  16794. no longer in use."
  16795. (let ((m (copy-marker (or pos (point)))))
  16796. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  16797. (push m org-agenda-markers)
  16798. m))
  16799. (defun org-agenda-maybe-reset-markers (&optional force)
  16800. "Reset markers created by `org-agenda'. But only if they are old enough."
  16801. (if (or (and force (not org-agenda-multi))
  16802. (> (- (time-to-seconds (current-time))
  16803. org-agenda-last-marker-time)
  16804. 5))
  16805. (while org-agenda-markers
  16806. (move-marker (pop org-agenda-markers) nil))))
  16807. (defvar org-agenda-new-buffers nil
  16808. "Buffers created to visit agenda files.")
  16809. (defun org-get-agenda-file-buffer (file)
  16810. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16811. it to the list of buffers which might be released later."
  16812. (let ((buf (org-find-base-buffer-visiting file)))
  16813. (if buf
  16814. buf ; just return it
  16815. ;; Make a new buffer and remember it
  16816. (setq buf (find-file-noselect file))
  16817. (if buf (push buf org-agenda-new-buffers))
  16818. buf)))
  16819. (defun org-release-buffers (blist)
  16820. "Release all buffers in list, asking the user for confirmation when needed.
  16821. When a buffer is unmodified, it is just killed. When modified, it is saved
  16822. \(if the user agrees) and then killed."
  16823. (let (buf file)
  16824. (while (setq buf (pop blist))
  16825. (setq file (buffer-file-name buf))
  16826. (when (and (buffer-modified-p buf)
  16827. file
  16828. (y-or-n-p (format "Save file %s? " file)))
  16829. (with-current-buffer buf (save-buffer)))
  16830. (kill-buffer buf))))
  16831. (defun org-get-category (&optional pos)
  16832. "Get the category applying to position POS."
  16833. (get-text-property (or pos (point)) 'org-category))
  16834. ;;; Agenda timeline
  16835. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  16836. (defun org-timeline (&optional include-all)
  16837. "Show a time-sorted view of the entries in the current org file.
  16838. Only entries with a time stamp of today or later will be listed. With
  16839. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  16840. under the current date.
  16841. If the buffer contains an active region, only check the region for
  16842. dates."
  16843. (interactive "P")
  16844. (require 'calendar)
  16845. (org-compile-prefix-format 'timeline)
  16846. (org-set-sorting-strategy 'timeline)
  16847. (let* ((dopast t)
  16848. (dotodo include-all)
  16849. (doclosed org-agenda-show-log)
  16850. (entry buffer-file-name)
  16851. (date (calendar-current-date))
  16852. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  16853. (end (if (org-region-active-p) (region-end) (point-max)))
  16854. (day-numbers (org-get-all-dates beg end 'no-ranges
  16855. t doclosed ; always include today
  16856. org-timeline-show-empty-dates))
  16857. (org-deadline-warning-days 0)
  16858. (org-agenda-only-exact-dates t)
  16859. (today (time-to-days (current-time)))
  16860. (past t)
  16861. args
  16862. s e rtn d emptyp)
  16863. (setq org-agenda-redo-command
  16864. (list 'progn
  16865. (list 'org-switch-to-buffer-other-window (current-buffer))
  16866. (list 'org-timeline (list 'quote include-all))))
  16867. (if (not dopast)
  16868. ;; Remove past dates from the list of dates.
  16869. (setq day-numbers (delq nil (mapcar (lambda(x)
  16870. (if (>= x today) x nil))
  16871. day-numbers))))
  16872. (org-prepare-agenda (concat "Timeline "
  16873. (file-name-nondirectory buffer-file-name)))
  16874. (if doclosed (push :closed args))
  16875. (push :timestamp args)
  16876. (push :deadline args)
  16877. (push :scheduled args)
  16878. (push :sexp args)
  16879. (if dotodo (push :todo args))
  16880. (while (setq d (pop day-numbers))
  16881. (if (and (listp d) (eq (car d) :omitted))
  16882. (progn
  16883. (setq s (point))
  16884. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  16885. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  16886. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  16887. (if (and (>= d today)
  16888. dopast
  16889. past)
  16890. (progn
  16891. (setq past nil)
  16892. (insert (make-string 79 ?-) "\n")))
  16893. (setq date (calendar-gregorian-from-absolute d))
  16894. (setq s (point))
  16895. (setq rtn (and (not emptyp)
  16896. (apply 'org-agenda-get-day-entries entry
  16897. date args)))
  16898. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  16899. (progn
  16900. (insert
  16901. (if (stringp org-agenda-format-date)
  16902. (format-time-string org-agenda-format-date
  16903. (org-time-from-absolute date))
  16904. (funcall org-agenda-format-date date))
  16905. "\n")
  16906. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  16907. (put-text-property s (1- (point)) 'org-date-line t)
  16908. (if (equal d today)
  16909. (put-text-property s (1- (point)) 'org-today t))
  16910. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  16911. (put-text-property s (1- (point)) 'day d)))))
  16912. (goto-char (point-min))
  16913. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  16914. (point-min)))
  16915. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  16916. (org-finalize-agenda)
  16917. (setq buffer-read-only t)))
  16918. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  16919. "Return a list of all relevant day numbers from BEG to END buffer positions.
  16920. If NO-RANGES is non-nil, include only the start and end dates of a range,
  16921. not every single day in the range. If FORCE-TODAY is non-nil, make
  16922. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  16923. inactive time stamps (those in square brackets) are included.
  16924. When EMPTY is non-nil, also include days without any entries."
  16925. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  16926. dates dates1 date day day1 day2 ts1 ts2)
  16927. (if force-today
  16928. (setq dates (list (time-to-days (current-time)))))
  16929. (save-excursion
  16930. (goto-char beg)
  16931. (while (re-search-forward re end t)
  16932. (setq day (time-to-days (org-time-string-to-time
  16933. (substring (match-string 1) 0 10))))
  16934. (or (memq day dates) (push day dates)))
  16935. (unless no-ranges
  16936. (goto-char beg)
  16937. (while (re-search-forward org-tr-regexp end t)
  16938. (setq ts1 (substring (match-string 1) 0 10)
  16939. ts2 (substring (match-string 2) 0 10)
  16940. day1 (time-to-days (org-time-string-to-time ts1))
  16941. day2 (time-to-days (org-time-string-to-time ts2)))
  16942. (while (< (setq day1 (1+ day1)) day2)
  16943. (or (memq day1 dates) (push day1 dates)))))
  16944. (setq dates (sort dates '<))
  16945. (when empty
  16946. (while (setq day (pop dates))
  16947. (setq day2 (car dates))
  16948. (push day dates1)
  16949. (when (and day2 empty)
  16950. (if (or (eq empty t)
  16951. (and (numberp empty) (<= (- day2 day) empty)))
  16952. (while (< (setq day (1+ day)) day2)
  16953. (push (list day) dates1))
  16954. (push (cons :omitted (- day2 day)) dates1))))
  16955. (setq dates (nreverse dates1)))
  16956. dates)))
  16957. ;;; Agenda Daily/Weekly
  16958. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  16959. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  16960. (defvar org-agenda-last-arguments nil
  16961. "The arguments of the previous call to org-agenda")
  16962. (defvar org-starting-day nil) ; local variable in the agenda buffer
  16963. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  16964. (defvar org-include-all-loc nil) ; local variable
  16965. (defvar org-agenda-remove-date nil) ; dynamically scoped
  16966. ;;;###autoload
  16967. (defun org-agenda-list (&optional include-all start-day ndays)
  16968. "Produce a weekly view from all files in variable `org-agenda-files'.
  16969. The view will be for the current week, but from the overview buffer you
  16970. will be able to go to other weeks.
  16971. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  16972. also be shown, under the current date.
  16973. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  16974. on the days are also shown. See the variable `org-log-done' for how
  16975. to turn on logging.
  16976. START-DAY defaults to TODAY, or to the most recent match for the weekday
  16977. given in `org-agenda-start-on-weekday'.
  16978. NDAYS defaults to `org-agenda-ndays'."
  16979. (interactive "P")
  16980. (setq ndays (or ndays org-agenda-ndays)
  16981. start-day (or start-day org-agenda-start-day))
  16982. (if org-agenda-overriding-arguments
  16983. (setq include-all (car org-agenda-overriding-arguments)
  16984. start-day (nth 1 org-agenda-overriding-arguments)
  16985. ndays (nth 2 org-agenda-overriding-arguments)))
  16986. (if (stringp start-day)
  16987. ;; Convert to an absolute day number
  16988. (setq start-day (time-to-days (org-read-date nil t start-day))))
  16989. (setq org-agenda-last-arguments (list include-all start-day ndays))
  16990. (org-compile-prefix-format 'agenda)
  16991. (org-set-sorting-strategy 'agenda)
  16992. (require 'calendar)
  16993. (let* ((org-agenda-start-on-weekday
  16994. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  16995. org-agenda-start-on-weekday nil))
  16996. (thefiles (org-agenda-files))
  16997. (files thefiles)
  16998. (today (time-to-days (current-time)))
  16999. (sd (or start-day today))
  17000. (start (if (or (null org-agenda-start-on-weekday)
  17001. (< org-agenda-ndays 7))
  17002. sd
  17003. (let* ((nt (calendar-day-of-week
  17004. (calendar-gregorian-from-absolute sd)))
  17005. (n1 org-agenda-start-on-weekday)
  17006. (d (- nt n1)))
  17007. (- sd (+ (if (< d 0) 7 0) d)))))
  17008. (day-numbers (list start))
  17009. (day-cnt 0)
  17010. (inhibit-redisplay (not debug-on-error))
  17011. s e rtn rtnall file date d start-pos end-pos todayp nd)
  17012. (setq org-agenda-redo-command
  17013. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  17014. ;; Make the list of days
  17015. (setq ndays (or ndays org-agenda-ndays)
  17016. nd ndays)
  17017. (while (> ndays 1)
  17018. (push (1+ (car day-numbers)) day-numbers)
  17019. (setq ndays (1- ndays)))
  17020. (setq day-numbers (nreverse day-numbers))
  17021. (org-prepare-agenda "Day/Week")
  17022. (org-set-local 'org-starting-day (car day-numbers))
  17023. (org-set-local 'org-include-all-loc include-all)
  17024. (org-set-local 'org-agenda-span
  17025. (org-agenda-ndays-to-span nd))
  17026. (when (and (or include-all org-agenda-include-all-todo)
  17027. (member today day-numbers))
  17028. (setq files thefiles
  17029. rtnall nil)
  17030. (while (setq file (pop files))
  17031. (catch 'nextfile
  17032. (org-check-agenda-file file)
  17033. (setq date (calendar-gregorian-from-absolute today)
  17034. rtn (org-agenda-get-day-entries
  17035. file date :todo))
  17036. (setq rtnall (append rtnall rtn))))
  17037. (when rtnall
  17038. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  17039. (add-text-properties (point-min) (1- (point))
  17040. (list 'face 'org-agenda-structure))
  17041. (insert (org-finalize-agenda-entries rtnall) "\n")))
  17042. (unless org-agenda-compact-blocks
  17043. (setq s (point))
  17044. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  17045. "-agenda:\n")
  17046. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  17047. 'org-date-line t)))
  17048. (while (setq d (pop day-numbers))
  17049. (setq date (calendar-gregorian-from-absolute d)
  17050. s (point))
  17051. (if (or (setq todayp (= d today))
  17052. (and (not start-pos) (= d sd)))
  17053. (setq start-pos (point))
  17054. (if (and start-pos (not end-pos))
  17055. (setq end-pos (point))))
  17056. (setq files thefiles
  17057. rtnall nil)
  17058. (while (setq file (pop files))
  17059. (catch 'nextfile
  17060. (org-check-agenda-file file)
  17061. (if org-agenda-show-log
  17062. (setq rtn (org-agenda-get-day-entries
  17063. file date
  17064. :deadline :scheduled :timestamp :sexp :closed))
  17065. (setq rtn (org-agenda-get-day-entries
  17066. file date
  17067. :deadline :scheduled :sexp :timestamp)))
  17068. (setq rtnall (append rtnall rtn))))
  17069. (if org-agenda-include-diary
  17070. (progn
  17071. (require 'diary-lib)
  17072. (setq rtn (org-get-entries-from-diary date))
  17073. (setq rtnall (append rtnall rtn))))
  17074. (if (or rtnall org-agenda-show-all-dates)
  17075. (progn
  17076. (setq day-cnt (1+ day-cnt))
  17077. (insert
  17078. (if (stringp org-agenda-format-date)
  17079. (format-time-string org-agenda-format-date
  17080. (org-time-from-absolute date))
  17081. (funcall org-agenda-format-date date))
  17082. "\n")
  17083. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  17084. (put-text-property s (1- (point)) 'org-date-line t)
  17085. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  17086. (if todayp (put-text-property s (1- (point)) 'org-today t))
  17087. (if rtnall (insert
  17088. (org-finalize-agenda-entries
  17089. (org-agenda-add-time-grid-maybe
  17090. rtnall nd todayp))
  17091. "\n"))
  17092. (put-text-property s (1- (point)) 'day d)
  17093. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  17094. (goto-char (point-min))
  17095. (org-fit-agenda-window)
  17096. (unless (and (pos-visible-in-window-p (point-min))
  17097. (pos-visible-in-window-p (point-max)))
  17098. (goto-char (1- (point-max)))
  17099. (recenter -1)
  17100. (if (not (pos-visible-in-window-p (or start-pos 1)))
  17101. (progn
  17102. (goto-char (or start-pos 1))
  17103. (recenter 1))))
  17104. (goto-char (or start-pos 1))
  17105. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  17106. (org-finalize-agenda)
  17107. (setq buffer-read-only t)
  17108. (message "")))
  17109. (defun org-agenda-ndays-to-span (n)
  17110. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  17111. ;;; Agenda TODO list
  17112. (defvar org-select-this-todo-keyword nil)
  17113. (defvar org-last-arg nil)
  17114. ;;;###autoload
  17115. (defun org-todo-list (arg)
  17116. "Show all TODO entries from all agenda file in a single list.
  17117. The prefix arg can be used to select a specific TODO keyword and limit
  17118. the list to these. When using \\[universal-argument], you will be prompted
  17119. for a keyword. A numeric prefix directly selects the Nth keyword in
  17120. `org-todo-keywords-1'."
  17121. (interactive "P")
  17122. (require 'calendar)
  17123. (org-compile-prefix-format 'todo)
  17124. (org-set-sorting-strategy 'todo)
  17125. (org-prepare-agenda "TODO")
  17126. (let* ((today (time-to-days (current-time)))
  17127. (date (calendar-gregorian-from-absolute today))
  17128. (kwds org-todo-keywords-for-agenda)
  17129. (completion-ignore-case t)
  17130. (org-select-this-todo-keyword
  17131. (if (stringp arg) arg
  17132. (and arg (integerp arg) (> arg 0)
  17133. (nth (1- arg) kwds))))
  17134. rtn rtnall files file pos)
  17135. (when (equal arg '(4))
  17136. (setq org-select-this-todo-keyword
  17137. (completing-read "Keyword (or KWD1|K2D2|...): "
  17138. (mapcar 'list kwds) nil nil)))
  17139. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  17140. (org-set-local 'org-last-arg arg)
  17141. (setq org-agenda-redo-command
  17142. '(org-todo-list (or current-prefix-arg org-last-arg)))
  17143. (setq files (org-agenda-files)
  17144. rtnall nil)
  17145. (while (setq file (pop files))
  17146. (catch 'nextfile
  17147. (org-check-agenda-file file)
  17148. (setq rtn (org-agenda-get-day-entries file date :todo))
  17149. (setq rtnall (append rtnall rtn))))
  17150. (if org-agenda-overriding-header
  17151. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  17152. nil 'face 'org-agenda-structure) "\n")
  17153. (insert "Global list of TODO items of type: ")
  17154. (add-text-properties (point-min) (1- (point))
  17155. (list 'face 'org-agenda-structure))
  17156. (setq pos (point))
  17157. (insert (or org-select-this-todo-keyword "ALL") "\n")
  17158. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  17159. (setq pos (point))
  17160. (unless org-agenda-multi
  17161. (insert "Available with `N r': (0)ALL")
  17162. (let ((n 0) s)
  17163. (mapc (lambda (x)
  17164. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  17165. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  17166. (insert "\n "))
  17167. (insert " " s))
  17168. kwds))
  17169. (insert "\n"))
  17170. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  17171. (when rtnall
  17172. (insert (org-finalize-agenda-entries rtnall) "\n"))
  17173. (goto-char (point-min))
  17174. (org-fit-agenda-window)
  17175. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  17176. (org-finalize-agenda)
  17177. (setq buffer-read-only t)))
  17178. ;;; Agenda tags match
  17179. ;;;###autoload
  17180. (defun org-tags-view (&optional todo-only match)
  17181. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  17182. The prefix arg TODO-ONLY limits the search to TODO entries."
  17183. (interactive "P")
  17184. (org-compile-prefix-format 'tags)
  17185. (org-set-sorting-strategy 'tags)
  17186. (let* ((org-tags-match-list-sublevels
  17187. (if todo-only t org-tags-match-list-sublevels))
  17188. (completion-ignore-case t)
  17189. rtn rtnall files file pos matcher
  17190. buffer)
  17191. (setq matcher (org-make-tags-matcher match)
  17192. match (car matcher) matcher (cdr matcher))
  17193. (org-prepare-agenda (concat "TAGS " match))
  17194. (setq org-agenda-redo-command
  17195. (list 'org-tags-view (list 'quote todo-only)
  17196. (list 'if 'current-prefix-arg nil match)))
  17197. (setq files (org-agenda-files)
  17198. rtnall nil)
  17199. (while (setq file (pop files))
  17200. (catch 'nextfile
  17201. (org-check-agenda-file file)
  17202. (setq buffer (if (file-exists-p file)
  17203. (org-get-agenda-file-buffer file)
  17204. (error "No such file %s" file)))
  17205. (if (not buffer)
  17206. ;; If file does not exist, merror message to agenda
  17207. (setq rtn (list
  17208. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  17209. rtnall (append rtnall rtn))
  17210. (with-current-buffer buffer
  17211. (unless (org-mode-p)
  17212. (error "Agenda file %s is not in `org-mode'" file))
  17213. (save-excursion
  17214. (save-restriction
  17215. (if org-agenda-restrict
  17216. (narrow-to-region org-agenda-restrict-begin
  17217. org-agenda-restrict-end)
  17218. (widen))
  17219. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  17220. (setq rtnall (append rtnall rtn))))))))
  17221. (if org-agenda-overriding-header
  17222. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  17223. nil 'face 'org-agenda-structure) "\n")
  17224. (insert "Headlines with TAGS match: ")
  17225. (add-text-properties (point-min) (1- (point))
  17226. (list 'face 'org-agenda-structure))
  17227. (setq pos (point))
  17228. (insert match "\n")
  17229. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  17230. (setq pos (point))
  17231. (unless org-agenda-multi
  17232. (insert "Press `C-u r' to search again with new search string\n"))
  17233. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  17234. (when rtnall
  17235. (insert (org-finalize-agenda-entries rtnall) "\n"))
  17236. (goto-char (point-min))
  17237. (org-fit-agenda-window)
  17238. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  17239. (org-finalize-agenda)
  17240. (setq buffer-read-only t)))
  17241. ;;; Agenda Finding stuck projects
  17242. (defvar org-agenda-skip-regexp nil
  17243. "Regular expression used in skipping subtrees for the agenda.
  17244. This is basically a temporary global variable that can be set and then
  17245. used by user-defined selections using `org-agenda-skip-function'.")
  17246. (defvar org-agenda-overriding-header nil
  17247. "When this is set during todo and tags searches, will replace header.")
  17248. (defun org-agenda-skip-subtree-when-regexp-matches ()
  17249. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  17250. If yes, it returns the end position of this tree, causing agenda commands
  17251. to skip this subtree. This is a function that can be put into
  17252. `org-agenda-skip-function' for the duration of a command."
  17253. (let ((end (save-excursion (org-end-of-subtree t)))
  17254. skip)
  17255. (save-excursion
  17256. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  17257. (and skip end)))
  17258. (defun org-agenda-skip-entry-if (&rest conditions)
  17259. "Skip entry if any of CONDITIONS is true.
  17260. See `org-agenda-skip-if for details."
  17261. (org-agenda-skip-if nil conditions))
  17262. (defun org-agenda-skip-subtree-if (&rest conditions)
  17263. "Skip entry if any of CONDITIONS is true.
  17264. See `org-agenda-skip-if for details."
  17265. (org-agenda-skip-if t conditions))
  17266. (defun org-agenda-skip-if (subtree conditions)
  17267. "Checks current entity for CONDITIONS.
  17268. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  17269. the entry, i.e. the text before the next heading is checked.
  17270. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  17271. from different tests. Valid conditions are:
  17272. scheduled Check if there is a scheduled cookie
  17273. notscheduled Check if there is no scheduled cookie
  17274. deadline Check if there is a deadline
  17275. notdeadline Check if there is no deadline
  17276. regexp Check if regexp matches
  17277. notregexp Check if regexp does not match.
  17278. The regexp is taken from the conditions list, it must com right after the
  17279. `regexp' of `notregexp' element.
  17280. If any of these conditions is met, this function returns the end point of
  17281. the entity, causing the search to continue from there. This is a function
  17282. that can be put into `org-agenda-skip-function' for the duration of a command."
  17283. (let (beg end m r)
  17284. (org-back-to-heading t)
  17285. (setq beg (point)
  17286. end (if subtree
  17287. (progn (org-end-of-subtree t) (point))
  17288. (progn (outline-next-heading) (1- (point)))))
  17289. (goto-char beg)
  17290. (and
  17291. (or
  17292. (and (memq 'scheduled conditions)
  17293. (re-search-forward org-scheduled-time-regexp end t))
  17294. (and (memq 'notscheduled conditions)
  17295. (not (re-search-forward org-scheduled-time-regexp end t)))
  17296. (and (memq 'deadline conditions)
  17297. (re-search-forward org-deadline-time-regexp end t))
  17298. (and (memq 'notdeadline conditions)
  17299. (not (re-search-forward org-deadline-time-regexp end t)))
  17300. (and (setq m (memq 'regexp conditions))
  17301. (stringp (setq r (nth 1 m)))
  17302. (re-search-forward (nth 1 m) end t))
  17303. (and (setq m (memq 'notregexp conditions))
  17304. (stringp (setq r (nth 1 m)))
  17305. (not (re-search-forward (nth 1 m) end t))))
  17306. end)))
  17307. (defun org-agenda-list-stuck-projects (&rest ignore)
  17308. "Create agenda view for projects that are stuck.
  17309. Stuck projects are project that have no next actions. For the definitions
  17310. of what a project is and how to check if it stuck, customize the variable
  17311. `org-stuck-projects'.
  17312. MATCH is being ignored."
  17313. (interactive)
  17314. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  17315. ;; FIXME: we could have used org-agenda-skip-if here.
  17316. (org-agenda-overriding-header "List of stuck projects: ")
  17317. (matcher (nth 0 org-stuck-projects))
  17318. (todo (nth 1 org-stuck-projects))
  17319. (todo-wds (if (member "*" todo)
  17320. (progn
  17321. (org-prepare-agenda-buffers (org-agenda-files))
  17322. (org-delete-all
  17323. org-done-keywords-for-agenda
  17324. (copy-sequence org-todo-keywords-for-agenda)))
  17325. todo))
  17326. (todo-re (concat "^\\*+[ \t]+\\("
  17327. (mapconcat 'identity todo-wds "\\|")
  17328. "\\)\\>"))
  17329. (tags (nth 2 org-stuck-projects))
  17330. (tags-re (if (member "*" tags)
  17331. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  17332. (concat "^\\*+ .*:\\("
  17333. (mapconcat 'identity tags "\\|")
  17334. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  17335. (gen-re (nth 3 org-stuck-projects))
  17336. (re-list
  17337. (delq nil
  17338. (list
  17339. (if todo todo-re)
  17340. (if tags tags-re)
  17341. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  17342. gen-re)))))
  17343. (setq org-agenda-skip-regexp
  17344. (if re-list
  17345. (mapconcat 'identity re-list "\\|")
  17346. (error "No information how to identify unstuck projects")))
  17347. (org-tags-view nil matcher)
  17348. (with-current-buffer org-agenda-buffer-name
  17349. (setq org-agenda-redo-command
  17350. '(org-agenda-list-stuck-projects
  17351. (or current-prefix-arg org-last-arg))))))
  17352. ;;; Diary integration
  17353. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  17354. (defun org-get-entries-from-diary (date)
  17355. "Get the (Emacs Calendar) diary entries for DATE."
  17356. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  17357. (diary-display-hook '(fancy-diary-display))
  17358. (pop-up-frames nil)
  17359. (list-diary-entries-hook
  17360. (cons 'org-diary-default-entry list-diary-entries-hook))
  17361. (diary-file-name-prefix-function nil) ; turn this feature off
  17362. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  17363. entries
  17364. (org-disable-agenda-to-diary t))
  17365. (save-excursion
  17366. (save-window-excursion
  17367. (funcall (if (fboundp 'diary-list-entries)
  17368. 'diary-list-entries 'list-diary-entries)
  17369. date 1)))
  17370. (if (not (get-buffer fancy-diary-buffer))
  17371. (setq entries nil)
  17372. (with-current-buffer fancy-diary-buffer
  17373. (setq buffer-read-only nil)
  17374. (if (zerop (buffer-size))
  17375. ;; No entries
  17376. (setq entries nil)
  17377. ;; Omit the date and other unnecessary stuff
  17378. (org-agenda-cleanup-fancy-diary)
  17379. ;; Add prefix to each line and extend the text properties
  17380. (if (zerop (buffer-size))
  17381. (setq entries nil)
  17382. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  17383. (set-buffer-modified-p nil)
  17384. (kill-buffer fancy-diary-buffer)))
  17385. (when entries
  17386. (setq entries (org-split-string entries "\n"))
  17387. (setq entries
  17388. (mapcar
  17389. (lambda (x)
  17390. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  17391. ;; Extend the text properties to the beginning of the line
  17392. (org-add-props x (text-properties-at (1- (length x)) x)
  17393. 'type "diary" 'date date))
  17394. entries)))))
  17395. (defun org-agenda-cleanup-fancy-diary ()
  17396. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  17397. This gets rid of the date, the underline under the date, and
  17398. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  17399. date. It also removes lines that contain only whitespace."
  17400. (goto-char (point-min))
  17401. (if (looking-at ".*?:[ \t]*")
  17402. (progn
  17403. (replace-match "")
  17404. (re-search-forward "\n=+$" nil t)
  17405. (replace-match "")
  17406. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  17407. (re-search-forward "\n=+$" nil t)
  17408. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  17409. (goto-char (point-min))
  17410. (while (re-search-forward "^ +\n" nil t)
  17411. (replace-match ""))
  17412. (goto-char (point-min))
  17413. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  17414. (replace-match "")))
  17415. ;; Make sure entries from the diary have the right text properties.
  17416. (eval-after-load "diary-lib"
  17417. '(if (boundp 'diary-modify-entry-list-string-function)
  17418. ;; We can rely on the hook, nothing to do
  17419. nil
  17420. ;; Hook not avaiable, must use advice to make this work
  17421. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  17422. "Make the position visible."
  17423. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  17424. (stringp string)
  17425. buffer-file-name)
  17426. (setq string (org-modify-diary-entry-string string))))))
  17427. (defun org-modify-diary-entry-string (string)
  17428. "Add text properties to string, allowing org-mode to act on it."
  17429. (org-add-props string nil
  17430. 'mouse-face 'highlight
  17431. 'keymap org-agenda-keymap
  17432. 'help-echo (if buffer-file-name
  17433. (format "mouse-2 or RET jump to diary file %s"
  17434. (abbreviate-file-name buffer-file-name))
  17435. "")
  17436. 'org-agenda-diary-link t
  17437. 'org-marker (org-agenda-new-marker (point-at-bol))))
  17438. (defun org-diary-default-entry ()
  17439. "Add a dummy entry to the diary.
  17440. Needed to avoid empty dates which mess up holiday display."
  17441. ;; Catch the error if dealing with the new add-to-diary-alist
  17442. (when org-disable-agenda-to-diary
  17443. (condition-case nil
  17444. (add-to-diary-list original-date "Org-mode dummy" "")
  17445. (error
  17446. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  17447. ;;;###autoload
  17448. (defun org-diary (&rest args)
  17449. "Return diary information from org-files.
  17450. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  17451. It accesses org files and extracts information from those files to be
  17452. listed in the diary. The function accepts arguments specifying what
  17453. items should be listed. The following arguments are allowed:
  17454. :timestamp List the headlines of items containing a date stamp or
  17455. date range matching the selected date. Deadlines will
  17456. also be listed, on the expiration day.
  17457. :sexp List entries resulting from diary-like sexps.
  17458. :deadline List any deadlines past due, or due within
  17459. `org-deadline-warning-days'. The listing occurs only
  17460. in the diary for *today*, not at any other date. If
  17461. an entry is marked DONE, it is no longer listed.
  17462. :scheduled List all items which are scheduled for the given date.
  17463. The diary for *today* also contains items which were
  17464. scheduled earlier and are not yet marked DONE.
  17465. :todo List all TODO items from the org-file. This may be a
  17466. long list - so this is not turned on by default.
  17467. Like deadlines, these entries only show up in the
  17468. diary for *today*, not at any other date.
  17469. The call in the diary file should look like this:
  17470. &%%(org-diary) ~/path/to/some/orgfile.org
  17471. Use a separate line for each org file to check. Or, if you omit the file name,
  17472. all files listed in `org-agenda-files' will be checked automatically:
  17473. &%%(org-diary)
  17474. If you don't give any arguments (as in the example above), the default
  17475. arguments (:deadline :scheduled :timestamp :sexp) are used.
  17476. So the example above may also be written as
  17477. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  17478. The function expects the lisp variables `entry' and `date' to be provided
  17479. by the caller, because this is how the calendar works. Don't use this
  17480. function from a program - use `org-agenda-get-day-entries' instead."
  17481. (org-agenda-maybe-reset-markers)
  17482. (org-compile-prefix-format 'agenda)
  17483. (org-set-sorting-strategy 'agenda)
  17484. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  17485. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  17486. (list entry)
  17487. (org-agenda-files t)))
  17488. file rtn results)
  17489. (org-prepare-agenda-buffers files)
  17490. ;; If this is called during org-agenda, don't return any entries to
  17491. ;; the calendar. Org Agenda will list these entries itself.
  17492. (if org-disable-agenda-to-diary (setq files nil))
  17493. (while (setq file (pop files))
  17494. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  17495. (setq results (append results rtn)))
  17496. (if results
  17497. (concat (org-finalize-agenda-entries results) "\n"))))
  17498. ;;; Agenda entry finders
  17499. (defun org-agenda-get-day-entries (file date &rest args)
  17500. "Does the work for `org-diary' and `org-agenda'.
  17501. FILE is the path to a file to be checked for entries. DATE is date like
  17502. the one returned by `calendar-current-date'. ARGS are symbols indicating
  17503. which kind of entries should be extracted. For details about these, see
  17504. the documentation of `org-diary'."
  17505. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  17506. (let* ((org-startup-folded nil)
  17507. (org-startup-align-all-tables nil)
  17508. (buffer (if (file-exists-p file)
  17509. (org-get-agenda-file-buffer file)
  17510. (error "No such file %s" file)))
  17511. arg results rtn)
  17512. (if (not buffer)
  17513. ;; If file does not exist, make sure an error message ends up in diary
  17514. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  17515. (with-current-buffer buffer
  17516. (unless (org-mode-p)
  17517. (error "Agenda file %s is not in `org-mode'" file))
  17518. (let ((case-fold-search nil))
  17519. (save-excursion
  17520. (save-restriction
  17521. (if org-agenda-restrict
  17522. (narrow-to-region org-agenda-restrict-begin
  17523. org-agenda-restrict-end)
  17524. (widen))
  17525. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  17526. (while (setq arg (pop args))
  17527. (cond
  17528. ((and (eq arg :todo)
  17529. (equal date (calendar-current-date)))
  17530. (setq rtn (org-agenda-get-todos))
  17531. (setq results (append results rtn)))
  17532. ((eq arg :timestamp)
  17533. (setq rtn (org-agenda-get-blocks))
  17534. (setq results (append results rtn))
  17535. (setq rtn (org-agenda-get-timestamps))
  17536. (setq results (append results rtn)))
  17537. ((eq arg :sexp)
  17538. (setq rtn (org-agenda-get-sexps))
  17539. (setq results (append results rtn)))
  17540. ((eq arg :scheduled)
  17541. (setq rtn (org-agenda-get-scheduled))
  17542. (setq results (append results rtn)))
  17543. ((eq arg :closed)
  17544. (setq rtn (org-agenda-get-closed))
  17545. (setq results (append results rtn)))
  17546. ((eq arg :deadline)
  17547. (setq rtn (org-agenda-get-deadlines))
  17548. (setq results (append results rtn))))))))
  17549. results))))
  17550. ;; FIXME: this works only if the cursor is *not* at the
  17551. ;; beginning of the entry
  17552. (defun org-entry-is-done-p ()
  17553. "Is the current entry marked DONE?"
  17554. (save-excursion
  17555. (and (re-search-backward "[\r\n]\\*+ " nil t)
  17556. (looking-at org-nl-done-regexp))))
  17557. (defun org-at-date-range-p (&optional inactive-ok)
  17558. "Is the cursor inside a date range?"
  17559. (interactive)
  17560. (save-excursion
  17561. (catch 'exit
  17562. (let ((pos (point)))
  17563. (skip-chars-backward "^[<\r\n")
  17564. (skip-chars-backward "<[")
  17565. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  17566. (>= (match-end 0) pos)
  17567. (throw 'exit t))
  17568. (skip-chars-backward "^<[\r\n")
  17569. (skip-chars-backward "<[")
  17570. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  17571. (>= (match-end 0) pos)
  17572. (throw 'exit t)))
  17573. nil)))
  17574. (defun org-agenda-get-todos ()
  17575. "Return the TODO information for agenda display."
  17576. (let* ((props (list 'face nil
  17577. 'done-face 'org-done
  17578. 'org-not-done-regexp org-not-done-regexp
  17579. 'org-todo-regexp org-todo-regexp
  17580. 'mouse-face 'highlight
  17581. 'keymap org-agenda-keymap
  17582. 'help-echo
  17583. (format "mouse-2 or RET jump to org file %s"
  17584. (abbreviate-file-name buffer-file-name))))
  17585. ;; FIXME: get rid of the \n at some point but watch out
  17586. (regexp (concat "^\\*+[ \t]+\\("
  17587. (if org-select-this-todo-keyword
  17588. (if (equal org-select-this-todo-keyword "*")
  17589. org-todo-regexp
  17590. (concat "\\<\\("
  17591. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  17592. "\\)\\>"))
  17593. org-not-done-regexp)
  17594. "[^\n\r]*\\)"))
  17595. marker priority category tags
  17596. ee txt beg end)
  17597. (goto-char (point-min))
  17598. (while (re-search-forward regexp nil t)
  17599. (catch :skip
  17600. (save-match-data
  17601. (beginning-of-line)
  17602. (setq beg (point) end (progn (outline-next-heading) (point)))
  17603. (when (or (and org-agenda-todo-ignore-scheduled (goto-char beg)
  17604. (re-search-forward org-scheduled-time-regexp end t))
  17605. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  17606. (re-search-forward org-deadline-time-regexp end t)
  17607. (org-deadline-close (match-string 1))))
  17608. (goto-char (1+ beg))
  17609. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  17610. (throw :skip nil)))
  17611. (goto-char beg)
  17612. (org-agenda-skip)
  17613. (goto-char (match-beginning 1))
  17614. (setq marker (org-agenda-new-marker (match-beginning 0))
  17615. category (org-get-category)
  17616. tags (org-get-tags-at (point))
  17617. txt (org-format-agenda-item "" (match-string 1) category tags)
  17618. priority (1+ (org-get-priority txt)))
  17619. (org-add-props txt props
  17620. 'org-marker marker 'org-hd-marker marker
  17621. 'priority priority 'org-category category
  17622. 'type "todo")
  17623. (push txt ee)
  17624. (if org-agenda-todo-list-sublevels
  17625. (goto-char (match-end 1))
  17626. (org-end-of-subtree 'invisible))))
  17627. (nreverse ee)))
  17628. (defconst org-agenda-no-heading-message
  17629. "No heading for this item in buffer or region.")
  17630. (defun org-agenda-get-timestamps ()
  17631. "Return the date stamp information for agenda display."
  17632. (let* ((props (list 'face nil
  17633. 'org-not-done-regexp org-not-done-regexp
  17634. 'org-todo-regexp org-todo-regexp
  17635. 'mouse-face 'highlight
  17636. 'keymap org-agenda-keymap
  17637. 'help-echo
  17638. (format "mouse-2 or RET jump to org file %s"
  17639. (abbreviate-file-name buffer-file-name))))
  17640. (d1 (calendar-absolute-from-gregorian date))
  17641. (remove-re
  17642. (concat
  17643. (regexp-quote
  17644. (format-time-string
  17645. "<%Y-%m-%d"
  17646. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17647. ".*?>"))
  17648. (regexp
  17649. (concat
  17650. (regexp-quote
  17651. (substring
  17652. (format-time-string
  17653. (car org-time-stamp-formats)
  17654. (apply 'encode-time ; DATE bound by calendar
  17655. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  17656. 0 11))
  17657. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  17658. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  17659. marker hdmarker deadlinep scheduledp donep tmp priority category
  17660. ee txt timestr tags b0 b3 e3)
  17661. (goto-char (point-min))
  17662. (while (re-search-forward regexp nil t)
  17663. (setq b0 (match-beginning 0)
  17664. b3 (match-beginning 3) e3 (match-end 3))
  17665. (catch :skip
  17666. (and (org-at-date-range-p) (throw :skip nil))
  17667. (org-agenda-skip)
  17668. (if (and (match-end 1)
  17669. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  17670. (throw :skip nil))
  17671. (if (and e3
  17672. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  17673. (throw :skip nil))
  17674. (setq marker (org-agenda-new-marker b0)
  17675. category (org-get-category b0)
  17676. tmp (buffer-substring (max (point-min)
  17677. (- b0 org-ds-keyword-length))
  17678. b0)
  17679. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  17680. deadlinep (string-match org-deadline-regexp tmp)
  17681. scheduledp (string-match org-scheduled-regexp tmp)
  17682. donep (org-entry-is-done-p))
  17683. (if (or scheduledp deadlinep) (throw :skip t))
  17684. (if (string-match ">" timestr)
  17685. ;; substring should only run to end of time stamp
  17686. (setq timestr (substring timestr 0 (match-end 0))))
  17687. (save-excursion
  17688. (if (re-search-backward "^\\*+ " nil t)
  17689. (progn
  17690. (goto-char (match-beginning 0))
  17691. (setq hdmarker (org-agenda-new-marker)
  17692. tags (org-get-tags-at))
  17693. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17694. (setq txt (org-format-agenda-item
  17695. nil (match-string 1) category tags timestr nil
  17696. remove-re)))
  17697. (setq txt org-agenda-no-heading-message))
  17698. (setq priority (org-get-priority txt))
  17699. (org-add-props txt props
  17700. 'org-marker marker 'org-hd-marker hdmarker)
  17701. (org-add-props txt nil 'priority priority
  17702. 'org-category category 'date date
  17703. 'type "timestamp")
  17704. (push txt ee))
  17705. (outline-next-heading)))
  17706. (nreverse ee)))
  17707. (defun org-agenda-get-sexps ()
  17708. "Return the sexp information for agenda display."
  17709. (require 'diary-lib)
  17710. (let* ((props (list 'face nil
  17711. 'mouse-face 'highlight
  17712. 'keymap org-agenda-keymap
  17713. 'help-echo
  17714. (format "mouse-2 or RET jump to org file %s"
  17715. (abbreviate-file-name buffer-file-name))))
  17716. (regexp "^&?%%(")
  17717. marker category ee txt tags entry result beg b sexp sexp-entry)
  17718. (goto-char (point-min))
  17719. (while (re-search-forward regexp nil t)
  17720. (catch :skip
  17721. (org-agenda-skip)
  17722. (setq beg (match-beginning 0))
  17723. (goto-char (1- (match-end 0)))
  17724. (setq b (point))
  17725. (forward-sexp 1)
  17726. (setq sexp (buffer-substring b (point)))
  17727. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  17728. (org-trim (match-string 1))
  17729. ""))
  17730. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  17731. (when result
  17732. (setq marker (org-agenda-new-marker beg)
  17733. category (org-get-category beg))
  17734. (if (string-match "\\S-" result)
  17735. (setq txt result)
  17736. (setq txt "SEXP entry returned empty string"))
  17737. (setq txt (org-format-agenda-item
  17738. "" txt category tags 'time))
  17739. (org-add-props txt props 'org-marker marker)
  17740. (org-add-props txt nil
  17741. 'org-category category 'date date
  17742. 'type "sexp")
  17743. (push txt ee))))
  17744. (nreverse ee)))
  17745. (defun org-agenda-get-closed ()
  17746. "Return the logged TODO entries for agenda display."
  17747. (let* ((props (list 'mouse-face 'highlight
  17748. 'org-not-done-regexp org-not-done-regexp
  17749. 'org-todo-regexp org-todo-regexp
  17750. 'keymap org-agenda-keymap
  17751. 'help-echo
  17752. (format "mouse-2 or RET jump to org file %s"
  17753. (abbreviate-file-name buffer-file-name))))
  17754. (regexp (concat
  17755. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  17756. (regexp-quote
  17757. (substring
  17758. (format-time-string
  17759. (car org-time-stamp-formats)
  17760. (apply 'encode-time ; DATE bound by calendar
  17761. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  17762. 1 11))))
  17763. marker hdmarker priority category tags closedp
  17764. ee txt timestr)
  17765. (goto-char (point-min))
  17766. (while (re-search-forward regexp nil t)
  17767. (catch :skip
  17768. (org-agenda-skip)
  17769. (setq marker (org-agenda-new-marker (match-beginning 0))
  17770. closedp (equal (match-string 1) org-closed-string)
  17771. category (org-get-category (match-beginning 0))
  17772. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  17773. ;; donep (org-entry-is-done-p)
  17774. )
  17775. (if (string-match "\\]" timestr)
  17776. ;; substring should only run to end of time stamp
  17777. (setq timestr (substring timestr 0 (match-end 0))))
  17778. (save-excursion
  17779. (if (re-search-backward "^\\*+ " nil t)
  17780. (progn
  17781. (goto-char (match-beginning 0))
  17782. (setq hdmarker (org-agenda-new-marker)
  17783. tags (org-get-tags-at))
  17784. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17785. (setq txt (org-format-agenda-item
  17786. (if closedp "Closed: " "Clocked: ")
  17787. (match-string 1) category tags timestr)))
  17788. (setq txt org-agenda-no-heading-message))
  17789. (setq priority 100000)
  17790. (org-add-props txt props
  17791. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  17792. 'priority priority 'org-category category
  17793. 'type "closed" 'date date
  17794. 'undone-face 'org-warning 'done-face 'org-done)
  17795. (push txt ee))
  17796. (outline-next-heading)))
  17797. (nreverse ee)))
  17798. (defun org-agenda-get-deadlines ()
  17799. "Return the deadline information for agenda display."
  17800. (let* ((props (list 'mouse-face 'highlight
  17801. 'org-not-done-regexp org-not-done-regexp
  17802. 'org-todo-regexp org-todo-regexp
  17803. 'keymap org-agenda-keymap
  17804. 'help-echo
  17805. (format "mouse-2 or RET jump to org file %s"
  17806. (abbreviate-file-name buffer-file-name))))
  17807. (regexp org-deadline-time-regexp)
  17808. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  17809. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  17810. d2 diff dfrac wdays pos pos1 category tags
  17811. ee txt head face s upcomingp donep timestr)
  17812. (goto-char (point-min))
  17813. (while (re-search-forward regexp nil t)
  17814. (catch :skip
  17815. (org-agenda-skip)
  17816. (setq s (match-string 1)
  17817. pos (1- (match-beginning 1))
  17818. d2 (org-time-string-to-absolute (match-string 1) d1)
  17819. diff (- d2 d1)
  17820. wdays (org-get-wdays s)
  17821. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  17822. upcomingp (and todayp (> diff 0)))
  17823. ;; When to show a deadline in the calendar:
  17824. ;; If the expiration is within wdays warning time.
  17825. ;; Past-due deadlines are only shown on the current date
  17826. (if (or (and (<= diff wdays)
  17827. (and todayp (not org-agenda-only-exact-dates)))
  17828. (= diff 0))
  17829. (save-excursion
  17830. (setq category (org-get-category))
  17831. (if (re-search-backward "^\\*+[ \t]+" nil t)
  17832. (progn
  17833. (goto-char (match-end 0))
  17834. (setq pos1 (match-beginning 0))
  17835. (setq tags (org-get-tags-at pos1))
  17836. (setq head (buffer-substring-no-properties
  17837. (point)
  17838. (progn (skip-chars-forward "^\r\n")
  17839. (point))))
  17840. (setq donep (string-match org-looking-at-done-regexp head))
  17841. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  17842. (setq timestr
  17843. (concat (substring s (match-beginning 1)) " "))
  17844. (setq timestr 'time))
  17845. (if (and donep
  17846. (or org-agenda-skip-deadline-if-done
  17847. (not (= diff 0))))
  17848. (setq txt nil)
  17849. (setq txt (org-format-agenda-item
  17850. (if (= diff 0)
  17851. (car org-agenda-deadline-leaders)
  17852. (format (nth 1 org-agenda-deadline-leaders)
  17853. diff))
  17854. head category tags timestr))))
  17855. (setq txt org-agenda-no-heading-message))
  17856. (when txt
  17857. (setq face (org-agenda-deadline-face dfrac))
  17858. (org-add-props txt props
  17859. 'org-marker (org-agenda-new-marker pos)
  17860. 'org-hd-marker (org-agenda-new-marker pos1)
  17861. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  17862. (org-get-priority txt))
  17863. 'org-category category
  17864. 'type (if upcomingp "upcoming-deadline" "deadline")
  17865. 'date (if upcomingp date d2)
  17866. 'face (if donep 'org-done face)
  17867. 'undone-face face 'done-face 'org-done)
  17868. (push txt ee))))))
  17869. (nreverse ee)))
  17870. (defun org-agenda-deadline-face (fraction)
  17871. "Return the face to displaying a deadline item.
  17872. FRACTION is what fraction of the head-warning time has passed."
  17873. (let ((faces org-agenda-deadline-faces) f)
  17874. (catch 'exit
  17875. (while (setq f (pop faces))
  17876. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  17877. (defun org-agenda-get-scheduled ()
  17878. "Return the scheduled information for agenda display."
  17879. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  17880. 'org-todo-regexp org-todo-regexp
  17881. 'done-face 'org-done
  17882. 'mouse-face 'highlight
  17883. 'keymap org-agenda-keymap
  17884. 'help-echo
  17885. (format "mouse-2 or RET jump to org file %s"
  17886. (abbreviate-file-name buffer-file-name))))
  17887. (regexp org-scheduled-time-regexp)
  17888. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  17889. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  17890. d2 diff pos pos1 category tags
  17891. ee txt head pastschedp donep face timestr s)
  17892. (goto-char (point-min))
  17893. (while (re-search-forward regexp nil t)
  17894. (catch :skip
  17895. (org-agenda-skip)
  17896. (setq s (match-string 1)
  17897. pos (1- (match-beginning 1))
  17898. d2 (org-time-string-to-absolute (match-string 1) d1)
  17899. diff (- d2 d1))
  17900. (setq pastschedp (and todayp (< diff 0)))
  17901. ;; When to show a scheduled item in the calendar:
  17902. ;; If it is on or past the date.
  17903. (if (or (and (< diff 0)
  17904. (and todayp (not org-agenda-only-exact-dates)))
  17905. (= diff 0))
  17906. (save-excursion
  17907. (setq category (org-get-category))
  17908. (if (re-search-backward "^\\*+[ \t]+" nil t)
  17909. (progn
  17910. (goto-char (match-end 0))
  17911. (setq pos1 (match-beginning 0))
  17912. (setq tags (org-get-tags-at))
  17913. (setq head (buffer-substring-no-properties
  17914. (point)
  17915. (progn (skip-chars-forward "^\r\n") (point))))
  17916. (setq donep (string-match org-looking-at-done-regexp head))
  17917. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  17918. (setq timestr
  17919. (concat (substring s (match-beginning 1)) " "))
  17920. (setq timestr 'time))
  17921. (if (and donep
  17922. (or org-agenda-skip-scheduled-if-done
  17923. (not (= diff 0))))
  17924. (setq txt nil)
  17925. (setq txt (org-format-agenda-item
  17926. (if (= diff 0)
  17927. (car org-agenda-scheduled-leaders)
  17928. (format (nth 1 org-agenda-scheduled-leaders)
  17929. (- 1 diff)))
  17930. head category tags timestr))))
  17931. (setq txt org-agenda-no-heading-message))
  17932. (when txt
  17933. (setq face (if pastschedp
  17934. 'org-scheduled-previously
  17935. 'org-scheduled-today))
  17936. (org-add-props txt props
  17937. 'undone-face face
  17938. 'face (if donep 'org-done face)
  17939. 'org-marker (org-agenda-new-marker pos)
  17940. 'org-hd-marker (org-agenda-new-marker pos1)
  17941. 'type (if pastschedp "past-scheduled" "scheduled")
  17942. 'date (if pastschedp d2 date)
  17943. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  17944. 'org-category category)
  17945. (push txt ee))))))
  17946. (nreverse ee)))
  17947. (defun org-agenda-get-blocks ()
  17948. "Return the date-range information for agenda display."
  17949. (let* ((props (list 'face nil
  17950. 'org-not-done-regexp org-not-done-regexp
  17951. 'org-todo-regexp org-todo-regexp
  17952. 'mouse-face 'highlight
  17953. 'keymap org-agenda-keymap
  17954. 'help-echo
  17955. (format "mouse-2 or RET jump to org file %s"
  17956. (abbreviate-file-name buffer-file-name))))
  17957. (regexp org-tr-regexp)
  17958. (d0 (calendar-absolute-from-gregorian date))
  17959. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  17960. (goto-char (point-min))
  17961. (while (re-search-forward regexp nil t)
  17962. (catch :skip
  17963. (org-agenda-skip)
  17964. (setq pos (point))
  17965. (setq timestr (match-string 0)
  17966. s1 (match-string 1)
  17967. s2 (match-string 2)
  17968. d1 (time-to-days (org-time-string-to-time s1))
  17969. d2 (time-to-days (org-time-string-to-time s2)))
  17970. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  17971. ;; Only allow days between the limits, because the normal
  17972. ;; date stamps will catch the limits.
  17973. (save-excursion
  17974. (setq marker (org-agenda-new-marker (point)))
  17975. (setq category (org-get-category))
  17976. (if (re-search-backward "^\\*+ " nil t)
  17977. (progn
  17978. (goto-char (match-beginning 0))
  17979. (setq hdmarker (org-agenda-new-marker (point)))
  17980. (setq tags (org-get-tags-at))
  17981. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  17982. (setq txt (org-format-agenda-item
  17983. (format (if (= d1 d2) "" "(%d/%d): ")
  17984. (1+ (- d0 d1)) (1+ (- d2 d1)))
  17985. (match-string 1) category tags
  17986. (if (= d0 d1) timestr))))
  17987. (setq txt org-agenda-no-heading-message))
  17988. (org-add-props txt props
  17989. 'org-marker marker 'org-hd-marker hdmarker
  17990. 'type "block" 'date date
  17991. 'priority (org-get-priority txt) 'org-category category)
  17992. (push txt ee)))
  17993. (goto-char pos)))
  17994. ;; Sort the entries by expiration date.
  17995. (nreverse ee)))
  17996. ;;; Agenda presentation and sorting
  17997. (defconst org-plain-time-of-day-regexp
  17998. (concat
  17999. "\\(\\<[012]?[0-9]"
  18000. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  18001. "\\(--?"
  18002. "\\(\\<[012]?[0-9]"
  18003. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  18004. "\\)?")
  18005. "Regular expression to match a plain time or time range.
  18006. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  18007. groups carry important information:
  18008. 0 the full match
  18009. 1 the first time, range or not
  18010. 8 the second time, if it is a range.")
  18011. (defconst org-plain-time-extension-regexp
  18012. (concat
  18013. "\\(\\<[012]?[0-9]"
  18014. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  18015. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  18016. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  18017. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  18018. groups carry important information:
  18019. 0 the full match
  18020. 7 hours of duration
  18021. 9 minutes of duration")
  18022. (defconst org-stamp-time-of-day-regexp
  18023. (concat
  18024. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  18025. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  18026. "\\(--?"
  18027. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  18028. "Regular expression to match a timestamp time or time range.
  18029. After a match, the following groups carry important information:
  18030. 0 the full match
  18031. 1 date plus weekday, for backreferencing to make sure both times on same day
  18032. 2 the first time, range or not
  18033. 4 the second time, if it is a range.")
  18034. (defvar org-prefix-has-time nil
  18035. "A flag, set by `org-compile-prefix-format'.
  18036. The flag is set if the currently compiled format contains a `%t'.")
  18037. (defvar org-prefix-has-tag nil
  18038. "A flag, set by `org-compile-prefix-format'.
  18039. The flag is set if the currently compiled format contains a `%T'.")
  18040. (defun org-format-agenda-item (extra txt &optional category tags dotime
  18041. noprefix remove-re)
  18042. "Format TXT to be inserted into the agenda buffer.
  18043. In particular, it adds the prefix and corresponding text properties. EXTRA
  18044. must be a string and replaces the `%s' specifier in the prefix format.
  18045. CATEGORY (string, symbol or nil) may be used to overrule the default
  18046. category taken from local variable or file name. It will replace the `%c'
  18047. specifier in the format. DOTIME, when non-nil, indicates that a
  18048. time-of-day should be extracted from TXT for sorting of this entry, and for
  18049. the `%t' specifier in the format. When DOTIME is a string, this string is
  18050. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  18051. only the correctly processes TXT should be returned - this is used by
  18052. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  18053. Any match of REMOVE-RE will be removed from TXT."
  18054. (save-match-data
  18055. ;; Diary entries sometimes have extra whitespace at the beginning
  18056. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  18057. (let* ((category (or category
  18058. org-category
  18059. (if buffer-file-name
  18060. (file-name-sans-extension
  18061. (file-name-nondirectory buffer-file-name))
  18062. "")))
  18063. (tag (if tags (nth (1- (length tags)) tags) ""))
  18064. time ; time and tag are needed for the eval of the prefix format
  18065. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  18066. (time-of-day (and dotime (org-get-time-of-day ts)))
  18067. stamp plain s0 s1 s2 rtn srp)
  18068. (when (and dotime time-of-day org-prefix-has-time)
  18069. ;; Extract starting and ending time and move them to prefix
  18070. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  18071. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  18072. (setq s0 (match-string 0 ts)
  18073. srp (and stamp (match-end 3))
  18074. s1 (match-string (if plain 1 2) ts)
  18075. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  18076. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  18077. ;; them, we might want to remove them there to avoid duplication.
  18078. ;; The user can turn this off with a variable.
  18079. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  18080. (string-match (concat (regexp-quote s0) " *") txt)
  18081. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  18082. (= (match-beginning 0) 0)
  18083. t))
  18084. (setq txt (replace-match "" nil nil txt))))
  18085. ;; Normalize the time(s) to 24 hour
  18086. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  18087. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  18088. (when (and s1 (not s2) org-agenda-default-appointment-duration
  18089. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  18090. (let ((m (+ (string-to-number (match-string 2 s1))
  18091. (* 60 (string-to-number (match-string 1 s1)))
  18092. org-agenda-default-appointment-duration))
  18093. h)
  18094. (setq h (/ m 60) m (- m (* h 60)))
  18095. (setq s2 (format "%02d:%02d" h m))))
  18096. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  18097. txt)
  18098. ;; Tags are in the string
  18099. (if (or (eq org-agenda-remove-tags t)
  18100. (and org-agenda-remove-tags
  18101. org-prefix-has-tag))
  18102. (setq txt (replace-match "" t t txt))
  18103. (setq txt (replace-match
  18104. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  18105. (match-string 2 txt))
  18106. t t txt))))
  18107. (when remove-re
  18108. (while (string-match remove-re txt)
  18109. (setq txt (replace-match "" t t txt))))
  18110. ;; Create the final string
  18111. (if noprefix
  18112. (setq rtn txt)
  18113. ;; Prepare the variables needed in the eval of the compiled format
  18114. (setq time (cond (s2 (concat s1 "-" s2))
  18115. (s1 (concat s1 "......"))
  18116. (t ""))
  18117. extra (or extra "")
  18118. category (if (symbolp category) (symbol-name category) category))
  18119. ;; Evaluate the compiled format
  18120. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  18121. ;; And finally add the text properties
  18122. (org-add-props rtn nil
  18123. 'org-category (downcase category) 'tags tags
  18124. 'org-highest-priority org-highest-priority
  18125. 'org-lowest-priority org-lowest-priority
  18126. 'prefix-length (- (length rtn) (length txt))
  18127. 'time-of-day time-of-day
  18128. 'txt txt
  18129. 'time time
  18130. 'extra extra
  18131. 'dotime dotime))))
  18132. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  18133. (defvar org-agenda-sorting-strategy-selected nil)
  18134. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  18135. (catch 'exit
  18136. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  18137. ((and todayp (member 'today (car org-agenda-time-grid))))
  18138. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  18139. ((member 'weekly (car org-agenda-time-grid)))
  18140. (t (throw 'exit list)))
  18141. (let* ((have (delq nil (mapcar
  18142. (lambda (x) (get-text-property 1 'time-of-day x))
  18143. list)))
  18144. (string (nth 1 org-agenda-time-grid))
  18145. (gridtimes (nth 2 org-agenda-time-grid))
  18146. (req (car org-agenda-time-grid))
  18147. (remove (member 'remove-match req))
  18148. new time)
  18149. (if (and (member 'require-timed req) (not have))
  18150. ;; don't show empty grid
  18151. (throw 'exit list))
  18152. (while (setq time (pop gridtimes))
  18153. (unless (and remove (member time have))
  18154. (setq time (int-to-string time))
  18155. (push (org-format-agenda-item
  18156. nil string "" nil
  18157. (concat (substring time 0 -2) ":" (substring time -2)))
  18158. new)
  18159. (put-text-property
  18160. 1 (length (car new)) 'face 'org-time-grid (car new))))
  18161. (if (member 'time-up org-agenda-sorting-strategy-selected)
  18162. (append new list)
  18163. (append list new)))))
  18164. (defun org-compile-prefix-format (key)
  18165. "Compile the prefix format into a Lisp form that can be evaluated.
  18166. The resulting form is returned and stored in the variable
  18167. `org-prefix-format-compiled'."
  18168. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  18169. (let ((s (cond
  18170. ((stringp org-agenda-prefix-format)
  18171. org-agenda-prefix-format)
  18172. ((assq key org-agenda-prefix-format)
  18173. (cdr (assq key org-agenda-prefix-format)))
  18174. (t " %-12:c%?-12t% s")))
  18175. (start 0)
  18176. varform vars var e c f opt)
  18177. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  18178. s start)
  18179. (setq var (cdr (assoc (match-string 4 s)
  18180. '(("c" . category) ("t" . time) ("s" . extra)
  18181. ("T" . tag))))
  18182. c (or (match-string 3 s) "")
  18183. opt (match-beginning 1)
  18184. start (1+ (match-beginning 0)))
  18185. (if (equal var 'time) (setq org-prefix-has-time t))
  18186. (if (equal var 'tag) (setq org-prefix-has-tag t))
  18187. (setq f (concat "%" (match-string 2 s) "s"))
  18188. (if opt
  18189. (setq varform
  18190. `(if (equal "" ,var)
  18191. ""
  18192. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  18193. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  18194. (setq s (replace-match "%s" t nil s))
  18195. (push varform vars))
  18196. (setq vars (nreverse vars))
  18197. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  18198. (defun org-set-sorting-strategy (key)
  18199. (if (symbolp (car org-agenda-sorting-strategy))
  18200. ;; the old format
  18201. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  18202. (setq org-agenda-sorting-strategy-selected
  18203. (or (cdr (assq key org-agenda-sorting-strategy))
  18204. (cdr (assq 'agenda org-agenda-sorting-strategy))
  18205. '(time-up category-keep priority-down)))))
  18206. (defun org-get-time-of-day (s &optional string mod24)
  18207. "Check string S for a time of day.
  18208. If found, return it as a military time number between 0 and 2400.
  18209. If not found, return nil.
  18210. The optional STRING argument forces conversion into a 5 character wide string
  18211. HH:MM."
  18212. (save-match-data
  18213. (when
  18214. (and (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  18215. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  18216. (not (equal (string-to-char (substring s (match-end 0))) ?\])))
  18217. (let* ((h (string-to-number (match-string 1 s)))
  18218. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  18219. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  18220. (am-p (equal ampm "am"))
  18221. (h1 (cond ((not ampm) h)
  18222. ((= h 12) (if am-p 0 12))
  18223. (t (+ h (if am-p 0 12)))))
  18224. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  18225. (mod h1 24) h1))
  18226. (t0 (+ (* 100 h2) m))
  18227. (t1 (concat (if (>= h1 24) "+" " ")
  18228. (if (< t0 100) "0" "")
  18229. (if (< t0 10) "0" "")
  18230. (int-to-string t0))))
  18231. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  18232. (defun org-finalize-agenda-entries (list &optional nosort)
  18233. "Sort and concatenate the agenda items."
  18234. (setq list (mapcar 'org-agenda-highlight-todo list))
  18235. (if nosort
  18236. list
  18237. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  18238. (defun org-agenda-highlight-todo (x)
  18239. (let (re pl)
  18240. (if (eq x 'line)
  18241. (save-excursion
  18242. (beginning-of-line 1)
  18243. (setq re (get-text-property (point) 'org-todo-regexp))
  18244. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  18245. (and (looking-at (concat "[ \t]*\\.*" re))
  18246. (add-text-properties (match-beginning 0) (match-end 0)
  18247. (list 'face (org-get-todo-face 0)))))
  18248. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  18249. pl (get-text-property 0 'prefix-length x))
  18250. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  18251. (add-text-properties
  18252. (or (match-end 1) (match-end 0)) (match-end 0)
  18253. (list 'face (org-get-todo-face (match-string 2 x)))
  18254. x))
  18255. x)))
  18256. (defsubst org-cmp-priority (a b)
  18257. "Compare the priorities of string A and B."
  18258. (let ((pa (or (get-text-property 1 'priority a) 0))
  18259. (pb (or (get-text-property 1 'priority b) 0)))
  18260. (cond ((> pa pb) +1)
  18261. ((< pa pb) -1)
  18262. (t nil))))
  18263. (defsubst org-cmp-category (a b)
  18264. "Compare the string values of categories of strings A and B."
  18265. (let ((ca (or (get-text-property 1 'org-category a) ""))
  18266. (cb (or (get-text-property 1 'org-category b) "")))
  18267. (cond ((string-lessp ca cb) -1)
  18268. ((string-lessp cb ca) +1)
  18269. (t nil))))
  18270. (defsubst org-cmp-tag (a b)
  18271. "Compare the string values of categories of strings A and B."
  18272. (let ((ta (car (last (get-text-property 1 'tags a))))
  18273. (tb (car (last (get-text-property 1 'tags b)))))
  18274. (cond ((not ta) +1)
  18275. ((not tb) -1)
  18276. ((string-lessp ta tb) -1)
  18277. ((string-lessp tb ta) +1)
  18278. (t nil))))
  18279. (defsubst org-cmp-time (a b)
  18280. "Compare the time-of-day values of strings A and B."
  18281. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  18282. (ta (or (get-text-property 1 'time-of-day a) def))
  18283. (tb (or (get-text-property 1 'time-of-day b) def)))
  18284. (cond ((< ta tb) -1)
  18285. ((< tb ta) +1)
  18286. (t nil))))
  18287. (defun org-entries-lessp (a b)
  18288. "Predicate for sorting agenda entries."
  18289. ;; The following variables will be used when the form is evaluated.
  18290. ;; So even though the compiler complains, keep them.
  18291. (let* ((time-up (org-cmp-time a b))
  18292. (time-down (if time-up (- time-up) nil))
  18293. (priority-up (org-cmp-priority a b))
  18294. (priority-down (if priority-up (- priority-up) nil))
  18295. (category-up (org-cmp-category a b))
  18296. (category-down (if category-up (- category-up) nil))
  18297. (category-keep (if category-up +1 nil))
  18298. (tag-up (org-cmp-tag a b))
  18299. (tag-down (if tag-up (- tag-up) nil)))
  18300. (cdr (assoc
  18301. (eval (cons 'or org-agenda-sorting-strategy-selected))
  18302. '((-1 . t) (1 . nil) (nil . nil))))))
  18303. ;;; Agenda commands
  18304. (defun org-agenda-check-type (error &rest types)
  18305. "Check if agenda buffer is of allowed type.
  18306. If ERROR is non-nil, throw an error, otherwise just return nil."
  18307. (if (memq org-agenda-type types)
  18308. t
  18309. (if error
  18310. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  18311. nil)))
  18312. (defun org-agenda-quit ()
  18313. "Exit agenda by removing the window or the buffer."
  18314. (interactive)
  18315. (let ((buf (current-buffer)))
  18316. (if (not (one-window-p)) (delete-window))
  18317. (kill-buffer buf)
  18318. (org-agenda-maybe-reset-markers 'force)
  18319. (org-columns-remove-overlays))
  18320. ;; Maybe restore the pre-agenda window configuration.
  18321. (and org-agenda-restore-windows-after-quit
  18322. (not (eq org-agenda-window-setup 'other-frame))
  18323. org-pre-agenda-window-conf
  18324. (set-window-configuration org-pre-agenda-window-conf)))
  18325. (defun org-agenda-exit ()
  18326. "Exit agenda by removing the window or the buffer.
  18327. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  18328. Org-mode buffers visited directly by the user will not be touched."
  18329. (interactive)
  18330. (org-release-buffers org-agenda-new-buffers)
  18331. (setq org-agenda-new-buffers nil)
  18332. (org-agenda-quit))
  18333. (defun org-save-all-org-buffers ()
  18334. "Save all Org-mode buffers without user confirmation."
  18335. (interactive)
  18336. (message "Saving all Org-mode buffers...")
  18337. (save-some-buffers t 'org-mode-p)
  18338. (message "Saving all Org-mode buffers... done"))
  18339. (defun org-agenda-redo ()
  18340. "Rebuild Agenda.
  18341. When this is the global TODO list, a prefix argument will be interpreted."
  18342. (interactive)
  18343. (let* ((org-agenda-keep-modes t)
  18344. (line (org-current-line))
  18345. (window-line (- line (org-current-line (window-start))))
  18346. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  18347. (message "Rebuilding agenda buffer...")
  18348. (org-let lprops '(eval org-agenda-redo-command))
  18349. (setq org-agenda-undo-list nil
  18350. org-agenda-pending-undo-list nil)
  18351. (message "Rebuilding agenda buffer...done")
  18352. (goto-line line)
  18353. (recenter window-line)))
  18354. (defun org-agenda-goto-date (date)
  18355. "Jump to DATE in agenda."
  18356. (interactive (list (org-read-date)))
  18357. (org-agenda-list nil date))
  18358. (defun org-agenda-goto-today ()
  18359. "Go to today."
  18360. (interactive)
  18361. (org-agenda-check-type t 'timeline 'agenda)
  18362. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  18363. (cond
  18364. (tdpos (goto-char tdpos))
  18365. ((eq org-agenda-type 'agenda)
  18366. (let* ((sd (time-to-days (current-time)))
  18367. (comp (org-agenda-compute-time-span sd org-agenda-span))
  18368. (org-agenda-overriding-arguments org-agenda-last-arguments))
  18369. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  18370. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  18371. (org-agenda-redo)
  18372. (org-agenda-find-same-or-today-or-agenda)))
  18373. (t (error "Cannot find today")))))
  18374. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  18375. (goto-char
  18376. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  18377. (text-property-any (point-min) (point-max) 'org-today t)
  18378. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  18379. (point-min))))
  18380. (defun org-agenda-later (arg)
  18381. "Go forward in time by thee current span.
  18382. With prefix ARG, go forward that many times the current span."
  18383. (interactive "p")
  18384. (org-agenda-check-type t 'agenda)
  18385. (let* ((span org-agenda-span)
  18386. (sd org-starting-day)
  18387. (greg (calendar-gregorian-from-absolute sd))
  18388. (cnt (get-text-property (point) 'org-day-cnt))
  18389. greg2 nd)
  18390. (cond
  18391. ((eq span 'day)
  18392. (setq sd (+ arg sd) nd 1))
  18393. ((eq span 'week)
  18394. (setq sd (+ (* 7 arg) sd) nd 7))
  18395. ((eq span 'month)
  18396. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  18397. sd (calendar-absolute-from-gregorian greg2))
  18398. (setcar greg2 (1+ (car greg2)))
  18399. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  18400. ((eq span 'year)
  18401. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  18402. sd (calendar-absolute-from-gregorian greg2))
  18403. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  18404. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  18405. (let ((org-agenda-overriding-arguments
  18406. (list (car org-agenda-last-arguments) sd nd t)))
  18407. (org-agenda-redo)
  18408. (org-agenda-find-same-or-today-or-agenda cnt))))
  18409. (defun org-agenda-earlier (arg)
  18410. "Go backward in time by the current span.
  18411. With prefix ARG, go backward that many times the current span."
  18412. (interactive "p")
  18413. (org-agenda-later (- arg)))
  18414. (defun org-agenda-day-view ()
  18415. "Switch to daily view for agenda."
  18416. (interactive)
  18417. (setq org-agenda-ndays 1)
  18418. (org-agenda-change-time-span 'day))
  18419. (defun org-agenda-week-view ()
  18420. "Switch to daily view for agenda."
  18421. (interactive)
  18422. (setq org-agenda-ndays 7)
  18423. (org-agenda-change-time-span 'week))
  18424. (defun org-agenda-month-view ()
  18425. "Switch to daily view for agenda."
  18426. (interactive)
  18427. (org-agenda-change-time-span 'month))
  18428. (defun org-agenda-year-view ()
  18429. "Switch to daily view for agenda."
  18430. (interactive)
  18431. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  18432. (org-agenda-change-time-span 'year)
  18433. (error "Abort")))
  18434. (defun org-agenda-change-time-span (span)
  18435. "Change the agenda view to SPAN.
  18436. SPAN may be `day', `week', `month', `year'."
  18437. (org-agenda-check-type t 'agenda)
  18438. (if (equal org-agenda-span span)
  18439. (error "Viewing span is already \"%s\"" span))
  18440. (let* ((sd (or (get-text-property (point) 'day)
  18441. org-starting-day))
  18442. (computed (org-agenda-compute-time-span sd span))
  18443. (org-agenda-overriding-arguments
  18444. (list (car org-agenda-last-arguments)
  18445. (car computed) (cdr computed) t)))
  18446. (org-agenda-redo)
  18447. (org-agenda-find-same-or-today-or-agenda))
  18448. (org-agenda-set-mode-name)
  18449. (message "Switched to %s view" span))
  18450. (defun org-agenda-compute-time-span (sd span)
  18451. "Compute starting date and number of days for agenda.
  18452. SPAN may be `day', `week', `month', `year'. The return value
  18453. is a cons cell with the starting date and the number of days,
  18454. so that the date SD will be in that range."
  18455. (let* ((greg (calendar-gregorian-from-absolute sd))
  18456. nd)
  18457. (cond
  18458. ((eq span 'day)
  18459. (setq nd 1))
  18460. ((eq span 'week)
  18461. (let* ((nt (calendar-day-of-week
  18462. (calendar-gregorian-from-absolute sd)))
  18463. (d (if org-agenda-start-on-weekday
  18464. (- nt org-agenda-start-on-weekday)
  18465. 0)))
  18466. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  18467. (setq nd 7)))
  18468. ((eq span 'month)
  18469. (setq sd (calendar-absolute-from-gregorian
  18470. (list (car greg) 1 (nth 2 greg)))
  18471. nd (- (calendar-absolute-from-gregorian
  18472. (list (1+ (car greg)) 1 (nth 2 greg)))
  18473. sd)))
  18474. ((eq span 'year)
  18475. (setq sd (calendar-absolute-from-gregorian
  18476. (list 1 1 (nth 2 greg)))
  18477. nd (- (calendar-absolute-from-gregorian
  18478. (list 1 1 (1+ (nth 2 greg))))
  18479. sd))))
  18480. (cons sd nd)))
  18481. ;; FIXME: does not work if user makes date format that starts with a blank
  18482. (defun org-agenda-next-date-line (&optional arg)
  18483. "Jump to the next line indicating a date in agenda buffer."
  18484. (interactive "p")
  18485. (org-agenda-check-type t 'agenda 'timeline)
  18486. (beginning-of-line 1)
  18487. (if (looking-at "^\\S-") (forward-char 1))
  18488. (if (not (re-search-forward "^\\S-" nil t arg))
  18489. (progn
  18490. (backward-char 1)
  18491. (error "No next date after this line in this buffer")))
  18492. (goto-char (match-beginning 0)))
  18493. (defun org-agenda-previous-date-line (&optional arg)
  18494. "Jump to the previous line indicating a date in agenda buffer."
  18495. (interactive "p")
  18496. (org-agenda-check-type t 'agenda 'timeline)
  18497. (beginning-of-line 1)
  18498. (if (not (re-search-backward "^\\S-" nil t arg))
  18499. (error "No previous date before this line in this buffer")))
  18500. ;; Initialize the highlight
  18501. (defvar org-hl (org-make-overlay 1 1))
  18502. (org-overlay-put org-hl 'face 'highlight)
  18503. (defun org-highlight (begin end &optional buffer)
  18504. "Highlight a region with overlay."
  18505. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  18506. org-hl begin end (or buffer (current-buffer))))
  18507. (defun org-unhighlight ()
  18508. "Detach overlay INDEX."
  18509. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  18510. ;; FIXME this is currently not used.
  18511. (defun org-highlight-until-next-command (beg end &optional buffer)
  18512. (org-highlight beg end buffer)
  18513. (add-hook 'pre-command-hook 'org-unhighlight-once))
  18514. (defun org-unhighlight-once ()
  18515. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  18516. (org-unhighlight))
  18517. (defun org-agenda-follow-mode ()
  18518. "Toggle follow mode in an agenda buffer."
  18519. (interactive)
  18520. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  18521. (org-agenda-set-mode-name)
  18522. (message "Follow mode is %s"
  18523. (if org-agenda-follow-mode "on" "off")))
  18524. (defun org-agenda-log-mode ()
  18525. "Toggle log mode in an agenda buffer."
  18526. (interactive)
  18527. (org-agenda-check-type t 'agenda 'timeline)
  18528. (setq org-agenda-show-log (not org-agenda-show-log))
  18529. (org-agenda-set-mode-name)
  18530. (org-agenda-redo)
  18531. (message "Log mode is %s"
  18532. (if org-agenda-show-log "on" "off")))
  18533. (defun org-agenda-toggle-diary ()
  18534. "Toggle diary inclusion in an agenda buffer."
  18535. (interactive)
  18536. (org-agenda-check-type t 'agenda)
  18537. (setq org-agenda-include-diary (not org-agenda-include-diary))
  18538. (org-agenda-redo)
  18539. (org-agenda-set-mode-name)
  18540. (message "Diary inclusion turned %s"
  18541. (if org-agenda-include-diary "on" "off")))
  18542. (defun org-agenda-toggle-time-grid ()
  18543. "Toggle time grid in an agenda buffer."
  18544. (interactive)
  18545. (org-agenda-check-type t 'agenda)
  18546. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  18547. (org-agenda-redo)
  18548. (org-agenda-set-mode-name)
  18549. (message "Time-grid turned %s"
  18550. (if org-agenda-use-time-grid "on" "off")))
  18551. (defun org-agenda-set-mode-name ()
  18552. "Set the mode name to indicate all the small mode settings."
  18553. (setq mode-name
  18554. (concat "Org-Agenda"
  18555. (if (equal org-agenda-ndays 1) " Day" "")
  18556. (if (equal org-agenda-ndays 7) " Week" "")
  18557. (if org-agenda-follow-mode " Follow" "")
  18558. (if org-agenda-include-diary " Diary" "")
  18559. (if org-agenda-use-time-grid " Grid" "")
  18560. (if org-agenda-show-log " Log" "")))
  18561. (force-mode-line-update))
  18562. (defun org-agenda-post-command-hook ()
  18563. (and (eolp) (not (bolp)) (backward-char 1))
  18564. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18565. (if (and org-agenda-follow-mode
  18566. (get-text-property (point) 'org-marker))
  18567. (org-agenda-show)))
  18568. (defun org-agenda-show-priority ()
  18569. "Show the priority of the current item.
  18570. This priority is composed of the main priority given with the [#A] cookies,
  18571. and by additional input from the age of a schedules or deadline entry."
  18572. (interactive)
  18573. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  18574. (message "Priority is %d" (if pri pri -1000))))
  18575. (defun org-agenda-show-tags ()
  18576. "Show the tags applicable to the current item."
  18577. (interactive)
  18578. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  18579. (if tags
  18580. (message "Tags are :%s:"
  18581. (org-no-properties (mapconcat 'identity tags ":")))
  18582. (message "No tags associated with this line"))))
  18583. (defun org-agenda-goto (&optional highlight)
  18584. "Go to the Org-mode file which contains the item at point."
  18585. (interactive)
  18586. (let* ((marker (or (get-text-property (point) 'org-marker)
  18587. (org-agenda-error)))
  18588. (buffer (marker-buffer marker))
  18589. (pos (marker-position marker)))
  18590. (switch-to-buffer-other-window buffer)
  18591. (widen)
  18592. (goto-char pos)
  18593. (when (org-mode-p)
  18594. (org-show-context 'agenda)
  18595. (save-excursion
  18596. (and (outline-next-heading)
  18597. (org-flag-heading nil)))) ; show the next heading
  18598. (run-hooks 'org-agenda-after-show-hook)
  18599. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  18600. (defvar org-agenda-after-show-hook nil
  18601. "Normal hook run after an item has been shown from the agenda.
  18602. Point is in the buffer where the item originated.")
  18603. (defun org-agenda-kill ()
  18604. "Kill the entry or subtree belonging to the current agenda entry."
  18605. (interactive)
  18606. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  18607. (let* ((marker (or (get-text-property (point) 'org-marker)
  18608. (org-agenda-error)))
  18609. (buffer (marker-buffer marker))
  18610. (pos (marker-position marker))
  18611. (type (get-text-property (point) 'type))
  18612. dbeg dend (n 0) conf)
  18613. (org-with-remote-undo buffer
  18614. (with-current-buffer buffer
  18615. (save-excursion
  18616. (goto-char pos)
  18617. (if (and (org-mode-p) (not (member type '("sexp"))))
  18618. (setq dbeg (progn (org-back-to-heading t) (point))
  18619. dend (org-end-of-subtree t t))
  18620. (setq dbeg (point-at-bol)
  18621. dend (min (point-max) (1+ (point-at-eol)))))
  18622. (goto-char dbeg)
  18623. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  18624. (setq conf (or (eq t org-agenda-confirm-kill)
  18625. (and (numberp org-agenda-confirm-kill)
  18626. (> n org-agenda-confirm-kill))))
  18627. (and conf
  18628. (not (y-or-n-p
  18629. (format "Delete entry with %d lines in buffer \"%s\"? "
  18630. n (buffer-name buffer))))
  18631. (error "Abort"))
  18632. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  18633. (with-current-buffer buffer (delete-region dbeg dend))
  18634. (message "Agenda item and source killed"))))
  18635. (defun org-agenda-archive ()
  18636. "Kill the entry or subtree belonging to the current agenda entry."
  18637. (interactive)
  18638. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  18639. (let* ((marker (or (get-text-property (point) 'org-marker)
  18640. (org-agenda-error)))
  18641. (buffer (marker-buffer marker))
  18642. (pos (marker-position marker)))
  18643. (org-with-remote-undo buffer
  18644. (with-current-buffer buffer
  18645. (if (org-mode-p)
  18646. (save-excursion
  18647. (goto-char pos)
  18648. (org-remove-subtree-entries-from-agenda)
  18649. (org-back-to-heading t)
  18650. (org-archive-subtree))
  18651. (error "Archiving works only in Org-mode files"))))))
  18652. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  18653. "Remove all lines in the agenda that correspond to a given subtree.
  18654. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  18655. If this information is not given, the function uses the tree at point."
  18656. (let ((buf (or buf (current-buffer))) m p)
  18657. (save-excursion
  18658. (unless (and beg end)
  18659. (org-back-to-heading t)
  18660. (setq beg (point))
  18661. (org-end-of-subtree t)
  18662. (setq end (point)))
  18663. (set-buffer (get-buffer org-agenda-buffer-name))
  18664. (save-excursion
  18665. (goto-char (point-max))
  18666. (beginning-of-line 1)
  18667. (while (not (bobp))
  18668. (when (and (setq m (get-text-property (point) 'org-marker))
  18669. (equal buf (marker-buffer m))
  18670. (setq p (marker-position m))
  18671. (>= p beg)
  18672. (<= p end))
  18673. (let ((inhibit-read-only t))
  18674. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  18675. (beginning-of-line 0))))))
  18676. (defun org-agenda-open-link ()
  18677. "Follow the link in the current line, if any."
  18678. (interactive)
  18679. (let ((eol (point-at-eol)))
  18680. (save-excursion
  18681. (if (or (re-search-forward org-bracket-link-regexp eol t)
  18682. (re-search-forward org-angle-link-re eol t)
  18683. (re-search-forward org-plain-link-re eol t))
  18684. (call-interactively 'org-open-at-point)
  18685. (error "No link in current line")))))
  18686. (defun org-agenda-switch-to (&optional delete-other-windows)
  18687. "Go to the Org-mode file which contains the item at point."
  18688. (interactive)
  18689. (let* ((marker (or (get-text-property (point) 'org-marker)
  18690. (org-agenda-error)))
  18691. (buffer (marker-buffer marker))
  18692. (pos (marker-position marker)))
  18693. (switch-to-buffer buffer)
  18694. (and delete-other-windows (delete-other-windows))
  18695. (widen)
  18696. (goto-char pos)
  18697. (when (org-mode-p)
  18698. (org-show-context 'agenda)
  18699. (save-excursion
  18700. (and (outline-next-heading)
  18701. (org-flag-heading nil)))))) ; show the next heading
  18702. (defun org-agenda-goto-mouse (ev)
  18703. "Go to the Org-mode file which contains the item at the mouse click."
  18704. (interactive "e")
  18705. (mouse-set-point ev)
  18706. (org-agenda-goto))
  18707. (defun org-agenda-show ()
  18708. "Display the Org-mode file which contains the item at point."
  18709. (interactive)
  18710. (let ((win (selected-window)))
  18711. (org-agenda-goto t)
  18712. (select-window win)))
  18713. (defun org-agenda-recenter (arg)
  18714. "Display the Org-mode file which contains the item at point and recenter."
  18715. (interactive "P")
  18716. (let ((win (selected-window)))
  18717. (org-agenda-goto t)
  18718. (recenter arg)
  18719. (select-window win)))
  18720. (defun org-agenda-show-mouse (ev)
  18721. "Display the Org-mode file which contains the item at the mouse click."
  18722. (interactive "e")
  18723. (mouse-set-point ev)
  18724. (org-agenda-show))
  18725. (defun org-agenda-check-no-diary ()
  18726. "Check if the entry is a diary link and abort if yes."
  18727. (if (get-text-property (point) 'org-agenda-diary-link)
  18728. (org-agenda-error)))
  18729. (defun org-agenda-error ()
  18730. (error "Command not allowed in this line"))
  18731. (defun org-agenda-tree-to-indirect-buffer ()
  18732. "Show the subtree corresponding to the current entry in an indirect buffer.
  18733. This calls the command `org-tree-to-indirect-buffer' from the original
  18734. Org-mode buffer.
  18735. With numerical prefix arg ARG, go up to this level and then take that tree.
  18736. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  18737. dedicated frame)."
  18738. (interactive)
  18739. (org-agenda-check-no-diary)
  18740. (let* ((marker (or (get-text-property (point) 'org-marker)
  18741. (org-agenda-error)))
  18742. (buffer (marker-buffer marker))
  18743. (pos (marker-position marker)))
  18744. (with-current-buffer buffer
  18745. (save-excursion
  18746. (goto-char pos)
  18747. (call-interactively 'org-tree-to-indirect-buffer)))))
  18748. (defvar org-last-heading-marker (make-marker)
  18749. "Marker pointing to the headline that last changed its TODO state
  18750. by a remote command from the agenda.")
  18751. (defun org-agenda-todo-nextset ()
  18752. "Switch TODO entry to next sequence."
  18753. (interactive)
  18754. (org-agenda-todo 'nextset))
  18755. (defun org-agenda-todo-previousset ()
  18756. "Switch TODO entry to previous sequence."
  18757. (interactive)
  18758. (org-agenda-todo 'previousset))
  18759. (defun org-agenda-todo (&optional arg)
  18760. "Cycle TODO state of line at point, also in Org-mode file.
  18761. This changes the line at point, all other lines in the agenda referring to
  18762. the same tree node, and the headline of the tree node in the Org-mode file."
  18763. (interactive "P")
  18764. (org-agenda-check-no-diary)
  18765. (let* ((col (current-column))
  18766. (marker (or (get-text-property (point) 'org-marker)
  18767. (org-agenda-error)))
  18768. (buffer (marker-buffer marker))
  18769. (pos (marker-position marker))
  18770. (hdmarker (get-text-property (point) 'org-hd-marker))
  18771. (inhibit-read-only t)
  18772. newhead)
  18773. (org-with-remote-undo buffer
  18774. (with-current-buffer buffer
  18775. (widen)
  18776. (goto-char pos)
  18777. (org-show-context 'agenda)
  18778. (save-excursion
  18779. (and (outline-next-heading)
  18780. (org-flag-heading nil))) ; show the next heading
  18781. (org-todo arg)
  18782. (and (bolp) (forward-char 1))
  18783. (setq newhead (org-get-heading))
  18784. (save-excursion
  18785. (org-back-to-heading)
  18786. (move-marker org-last-heading-marker (point))))
  18787. (beginning-of-line 1)
  18788. (save-excursion
  18789. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  18790. (move-to-column col))))
  18791. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  18792. "Change all lines in the agenda buffer which match HDMARKER.
  18793. The new content of the line will be NEWHEAD (as modified by
  18794. `org-format-agenda-item'). HDMARKER is checked with
  18795. `equal' against all `org-hd-marker' text properties in the file.
  18796. If FIXFACE is non-nil, the face of each item is modified acording to
  18797. the new TODO state."
  18798. (let* ((inhibit-read-only t)
  18799. props m pl undone-face done-face finish new dotime cat tags)
  18800. (save-excursion
  18801. (goto-char (point-max))
  18802. (beginning-of-line 1)
  18803. (while (not finish)
  18804. (setq finish (bobp))
  18805. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  18806. (equal m hdmarker))
  18807. (setq props (text-properties-at (point))
  18808. dotime (get-text-property (point) 'dotime)
  18809. cat (get-text-property (point) 'org-category)
  18810. tags (get-text-property (point) 'tags)
  18811. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  18812. pl (get-text-property (point) 'prefix-length)
  18813. undone-face (get-text-property (point) 'undone-face)
  18814. done-face (get-text-property (point) 'done-face))
  18815. (move-to-column pl)
  18816. (cond
  18817. ((equal new "")
  18818. (beginning-of-line 1)
  18819. (and (looking-at ".*\n?") (replace-match "")))
  18820. ((looking-at ".*")
  18821. (replace-match new t t)
  18822. (beginning-of-line 1)
  18823. (add-text-properties (point-at-bol) (point-at-eol) props)
  18824. (when fixface
  18825. (add-text-properties
  18826. (point-at-bol) (point-at-eol)
  18827. (list 'face
  18828. (if org-last-todo-state-is-todo
  18829. undone-face done-face))))
  18830. (org-agenda-highlight-todo 'line)
  18831. (beginning-of-line 1))
  18832. (t (error "Line update did not work"))))
  18833. (beginning-of-line 0)))
  18834. (org-finalize-agenda)))
  18835. (defun org-agenda-align-tags (&optional line)
  18836. "Align all tags in agenda items to `org-agenda-tags-column'."
  18837. (let ((inhibit-read-only t) l c)
  18838. (save-excursion
  18839. (goto-char (if line (point-at-bol) (point-min)))
  18840. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  18841. (if line (point-at-eol) nil) t)
  18842. (add-text-properties
  18843. (match-beginning 2) (match-end 2)
  18844. (list 'face (list 'org-tag (get-text-property
  18845. (match-beginning 2) 'face))))
  18846. (setq l (- (match-end 2) (match-beginning 2))
  18847. c (if (< org-agenda-tags-column 0)
  18848. (- (abs org-agenda-tags-column) l)
  18849. org-agenda-tags-column))
  18850. (delete-region (match-beginning 1) (match-end 1))
  18851. (goto-char (match-beginning 1))
  18852. (insert (org-add-props
  18853. (make-string (max 1 (- c (current-column))) ?\ )
  18854. (text-properties-at (point))))))))
  18855. (defun org-agenda-priority-up ()
  18856. "Increase the priority of line at point, also in Org-mode file."
  18857. (interactive)
  18858. (org-agenda-priority 'up))
  18859. (defun org-agenda-priority-down ()
  18860. "Decrease the priority of line at point, also in Org-mode file."
  18861. (interactive)
  18862. (org-agenda-priority 'down))
  18863. (defun org-agenda-priority (&optional force-direction)
  18864. "Set the priority of line at point, also in Org-mode file.
  18865. This changes the line at point, all other lines in the agenda referring to
  18866. the same tree node, and the headline of the tree node in the Org-mode file."
  18867. (interactive)
  18868. (org-agenda-check-no-diary)
  18869. (let* ((marker (or (get-text-property (point) 'org-marker)
  18870. (org-agenda-error)))
  18871. (hdmarker (get-text-property (point) 'org-hd-marker))
  18872. (buffer (marker-buffer hdmarker))
  18873. (pos (marker-position hdmarker))
  18874. (inhibit-read-only t)
  18875. newhead)
  18876. (org-with-remote-undo buffer
  18877. (with-current-buffer buffer
  18878. (widen)
  18879. (goto-char pos)
  18880. (org-show-context 'agenda)
  18881. (save-excursion
  18882. (and (outline-next-heading)
  18883. (org-flag-heading nil))) ; show the next heading
  18884. (funcall 'org-priority force-direction)
  18885. (end-of-line 1)
  18886. (setq newhead (org-get-heading)))
  18887. (org-agenda-change-all-lines newhead hdmarker)
  18888. (beginning-of-line 1))))
  18889. (defun org-get-tags-at (&optional pos)
  18890. "Get a list of all headline tags applicable at POS.
  18891. POS defaults to point. If tags are inherited, the list contains
  18892. the targets in the same sequence as the headlines appear, i.e.
  18893. the tags of the current headline come last."
  18894. (interactive)
  18895. (let (tags lastpos)
  18896. (save-excursion
  18897. (save-restriction
  18898. (widen)
  18899. (goto-char (or pos (point)))
  18900. (save-match-data
  18901. (org-back-to-heading t)
  18902. (condition-case nil
  18903. (while (not (equal lastpos (point)))
  18904. (setq lastpos (point))
  18905. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  18906. (setq tags (append (org-split-string
  18907. (org-match-string-no-properties 1) ":")
  18908. tags)))
  18909. (or org-use-tag-inheritance (error ""))
  18910. (org-up-heading-all 1))
  18911. (error nil))))
  18912. tags)))
  18913. ;; FIXME: should fix the tags property of the agenda line.
  18914. (defun org-agenda-set-tags ()
  18915. "Set tags for the current headline."
  18916. (interactive)
  18917. (org-agenda-check-no-diary)
  18918. (if (and (org-region-active-p) (interactive-p))
  18919. (call-interactively 'org-change-tag-in-region)
  18920. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  18921. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  18922. (org-agenda-error)))
  18923. (buffer (marker-buffer hdmarker))
  18924. (pos (marker-position hdmarker))
  18925. (inhibit-read-only t)
  18926. newhead)
  18927. (org-with-remote-undo buffer
  18928. (with-current-buffer buffer
  18929. (widen)
  18930. (goto-char pos)
  18931. (save-excursion
  18932. (org-show-context 'agenda))
  18933. (save-excursion
  18934. (and (outline-next-heading)
  18935. (org-flag-heading nil))) ; show the next heading
  18936. (goto-char pos)
  18937. (call-interactively 'org-set-tags)
  18938. (end-of-line 1)
  18939. (setq newhead (org-get-heading)))
  18940. (org-agenda-change-all-lines newhead hdmarker)
  18941. (beginning-of-line 1)))))
  18942. (defun org-agenda-toggle-archive-tag ()
  18943. "Toggle the archive tag for the current entry."
  18944. (interactive)
  18945. (org-agenda-check-no-diary)
  18946. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  18947. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  18948. (org-agenda-error)))
  18949. (buffer (marker-buffer hdmarker))
  18950. (pos (marker-position hdmarker))
  18951. (inhibit-read-only t)
  18952. newhead)
  18953. (org-with-remote-undo buffer
  18954. (with-current-buffer buffer
  18955. (widen)
  18956. (goto-char pos)
  18957. (org-show-context 'agenda)
  18958. (save-excursion
  18959. (and (outline-next-heading)
  18960. (org-flag-heading nil))) ; show the next heading
  18961. (call-interactively 'org-toggle-archive-tag)
  18962. (end-of-line 1)
  18963. (setq newhead (org-get-heading)))
  18964. (org-agenda-change-all-lines newhead hdmarker)
  18965. (beginning-of-line 1))))
  18966. (defun org-agenda-date-later (arg &optional what)
  18967. "Change the date of this item to one day later."
  18968. (interactive "p")
  18969. (org-agenda-check-type t 'agenda 'timeline)
  18970. (org-agenda-check-no-diary)
  18971. (let* ((marker (or (get-text-property (point) 'org-marker)
  18972. (org-agenda-error)))
  18973. (buffer (marker-buffer marker))
  18974. (pos (marker-position marker)))
  18975. (org-with-remote-undo buffer
  18976. (with-current-buffer buffer
  18977. (widen)
  18978. (goto-char pos)
  18979. (if (not (org-at-timestamp-p))
  18980. (error "Cannot find time stamp"))
  18981. (org-timestamp-change arg (or what 'day)))
  18982. (org-agenda-show-new-time marker org-last-changed-timestamp))
  18983. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  18984. (defun org-agenda-date-earlier (arg &optional what)
  18985. "Change the date of this item to one day earlier."
  18986. (interactive "p")
  18987. (org-agenda-date-later (- arg) what))
  18988. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  18989. "Show new date stamp via text properties."
  18990. ;; We use text properties to make this undoable
  18991. (let ((inhibit-read-only t))
  18992. (setq stamp (concat " " prefix " => " stamp))
  18993. (save-excursion
  18994. (goto-char (point-max))
  18995. (while (not (bobp))
  18996. (when (equal marker (get-text-property (point) 'org-marker))
  18997. (move-to-column (- (window-width) (length stamp)) t)
  18998. (if (featurep 'xemacs)
  18999. ;; Use `duplicable' property to trigger undo recording
  19000. (let ((ex (make-extent nil nil))
  19001. (gl (make-glyph stamp)))
  19002. (set-glyph-face gl 'secondary-selection)
  19003. (set-extent-properties
  19004. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  19005. (insert-extent ex (1- (point)) (point-at-eol)))
  19006. (add-text-properties
  19007. (1- (point)) (point-at-eol)
  19008. (list 'display (org-add-props stamp nil
  19009. 'face 'secondary-selection))))
  19010. (beginning-of-line 1))
  19011. (beginning-of-line 0)))))
  19012. (defun org-agenda-date-prompt (arg)
  19013. "Change the date of this item. Date is prompted for, with default today.
  19014. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  19015. be used to request time specification in the time stamp."
  19016. (interactive "P")
  19017. (org-agenda-check-type t 'agenda 'timeline)
  19018. (org-agenda-check-no-diary)
  19019. (let* ((marker (or (get-text-property (point) 'org-marker)
  19020. (org-agenda-error)))
  19021. (buffer (marker-buffer marker))
  19022. (pos (marker-position marker)))
  19023. (org-with-remote-undo buffer
  19024. (with-current-buffer buffer
  19025. (widen)
  19026. (goto-char pos)
  19027. (if (not (org-at-timestamp-p))
  19028. (error "Cannot find time stamp"))
  19029. (org-time-stamp arg)
  19030. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  19031. (defun org-agenda-schedule (arg)
  19032. "Schedule the item at point."
  19033. (interactive "P")
  19034. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  19035. (org-agenda-check-no-diary)
  19036. (let* ((marker (or (get-text-property (point) 'org-marker)
  19037. (org-agenda-error)))
  19038. (buffer (marker-buffer marker))
  19039. (pos (marker-position marker))
  19040. (org-insert-labeled-timestamps-at-point nil)
  19041. ts)
  19042. (org-with-remote-undo buffer
  19043. (with-current-buffer buffer
  19044. (widen)
  19045. (goto-char pos)
  19046. (setq ts (org-schedule arg)))
  19047. (org-agenda-show-new-time marker ts "S"))
  19048. (message "Item scheduled for %s" ts)))
  19049. (defun org-agenda-deadline (arg)
  19050. "Schedule the item at point."
  19051. (interactive "P")
  19052. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  19053. (org-agenda-check-no-diary)
  19054. (let* ((marker (or (get-text-property (point) 'org-marker)
  19055. (org-agenda-error)))
  19056. (buffer (marker-buffer marker))
  19057. (pos (marker-position marker))
  19058. (org-insert-labeled-timestamps-at-point nil)
  19059. ts)
  19060. (org-with-remote-undo buffer
  19061. (with-current-buffer buffer
  19062. (widen)
  19063. (goto-char pos)
  19064. (setq ts (org-deadline arg)))
  19065. (org-agenda-show-new-time marker ts "S"))
  19066. (message "Deadline for this item set to %s" ts)))
  19067. (defun org-get-heading (&optional no-tags)
  19068. "Return the heading of the current entry, without the stars."
  19069. (save-excursion
  19070. (org-back-to-heading t)
  19071. (if (looking-at
  19072. (if no-tags
  19073. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  19074. "\\*+[ \t]+\\([^\r\n]*\\)"))
  19075. (match-string 1) "")))
  19076. (defun org-agenda-clock-in (&optional arg)
  19077. "Start the clock on the currently selected item."
  19078. (interactive "P")
  19079. (org-agenda-check-no-diary)
  19080. (let* ((marker (or (get-text-property (point) 'org-marker)
  19081. (org-agenda-error)))
  19082. (pos (marker-position marker)))
  19083. (org-with-remote-undo (marker-buffer marker)
  19084. (with-current-buffer (marker-buffer marker)
  19085. (widen)
  19086. (goto-char pos)
  19087. (org-clock-in)))))
  19088. (defun org-agenda-clock-out (&optional arg)
  19089. "Stop the currently running clock."
  19090. (interactive "P")
  19091. (unless (marker-buffer org-clock-marker)
  19092. (error "No running clock"))
  19093. (org-with-remote-undo (marker-buffer org-clock-marker)
  19094. (org-clock-out)))
  19095. (defun org-agenda-clock-cancel (&optional arg)
  19096. "Cancel the currently running clock."
  19097. (interactive "P")
  19098. (unless (marker-buffer org-clock-marker)
  19099. (error "No running clock"))
  19100. (org-with-remote-undo (marker-buffer org-clock-marker)
  19101. (org-clock-cancel)))
  19102. (defun org-agenda-diary-entry ()
  19103. "Make a diary entry, like the `i' command from the calendar.
  19104. All the standard commands work: block, weekly etc."
  19105. (interactive)
  19106. (org-agenda-check-type t 'agenda 'timeline)
  19107. (require 'diary-lib)
  19108. (let* ((char (progn
  19109. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  19110. (read-char-exclusive)))
  19111. (cmd (cdr (assoc char
  19112. '((?d . insert-diary-entry)
  19113. (?w . insert-weekly-diary-entry)
  19114. (?m . insert-monthly-diary-entry)
  19115. (?y . insert-yearly-diary-entry)
  19116. (?a . insert-anniversary-diary-entry)
  19117. (?b . insert-block-diary-entry)
  19118. (?c . insert-cyclic-diary-entry)))))
  19119. (oldf (symbol-function 'calendar-cursor-to-date))
  19120. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  19121. (point (point))
  19122. (mark (or (mark t) (point))))
  19123. (unless cmd
  19124. (error "No command associated with <%c>" char))
  19125. (unless (and (get-text-property point 'day)
  19126. (or (not (equal ?b char))
  19127. (get-text-property mark 'day)))
  19128. (error "Don't know which date to use for diary entry"))
  19129. ;; We implement this by hacking the `calendar-cursor-to-date' function
  19130. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  19131. (let ((calendar-mark-ring
  19132. (list (calendar-gregorian-from-absolute
  19133. (or (get-text-property mark 'day)
  19134. (get-text-property point 'day))))))
  19135. (unwind-protect
  19136. (progn
  19137. (fset 'calendar-cursor-to-date
  19138. (lambda (&optional error)
  19139. (calendar-gregorian-from-absolute
  19140. (get-text-property point 'day))))
  19141. (call-interactively cmd))
  19142. (fset 'calendar-cursor-to-date oldf)))))
  19143. (defun org-agenda-execute-calendar-command (cmd)
  19144. "Execute a calendar command from the agenda, with the date associated to
  19145. the cursor position."
  19146. (org-agenda-check-type t 'agenda 'timeline)
  19147. (require 'diary-lib)
  19148. (unless (get-text-property (point) 'day)
  19149. (error "Don't know which date to use for calendar command"))
  19150. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  19151. (point (point))
  19152. (date (calendar-gregorian-from-absolute
  19153. (get-text-property point 'day)))
  19154. ;; the following 3 vars are needed in the calendar
  19155. (displayed-day (extract-calendar-day date))
  19156. (displayed-month (extract-calendar-month date))
  19157. (displayed-year (extract-calendar-year date)))
  19158. (unwind-protect
  19159. (progn
  19160. (fset 'calendar-cursor-to-date
  19161. (lambda (&optional error)
  19162. (calendar-gregorian-from-absolute
  19163. (get-text-property point 'day))))
  19164. (call-interactively cmd))
  19165. (fset 'calendar-cursor-to-date oldf))))
  19166. (defun org-agenda-phases-of-moon ()
  19167. "Display the phases of the moon for the 3 months around the cursor date."
  19168. (interactive)
  19169. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  19170. (defun org-agenda-holidays ()
  19171. "Display the holidays for the 3 months around the cursor date."
  19172. (interactive)
  19173. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  19174. (defun org-agenda-sunrise-sunset (arg)
  19175. "Display sunrise and sunset for the cursor date.
  19176. Latitude and longitude can be specified with the variables
  19177. `calendar-latitude' and `calendar-longitude'. When called with prefix
  19178. argument, latitude and longitude will be prompted for."
  19179. (interactive "P")
  19180. (let ((calendar-longitude (if arg nil calendar-longitude))
  19181. (calendar-latitude (if arg nil calendar-latitude))
  19182. (calendar-location-name
  19183. (if arg "the given coordinates" calendar-location-name)))
  19184. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  19185. (defun org-agenda-goto-calendar ()
  19186. "Open the Emacs calendar with the date at the cursor."
  19187. (interactive)
  19188. (org-agenda-check-type t 'agenda 'timeline)
  19189. (let* ((day (or (get-text-property (point) 'day)
  19190. (error "Don't know which date to open in calendar")))
  19191. (date (calendar-gregorian-from-absolute day))
  19192. (calendar-move-hook nil)
  19193. (view-calendar-holidays-initially nil)
  19194. (view-diary-entries-initially nil))
  19195. (calendar)
  19196. (calendar-goto-date date)))
  19197. (defun org-calendar-goto-agenda ()
  19198. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  19199. This is a command that has to be installed in `calendar-mode-map'."
  19200. (interactive)
  19201. (org-agenda-list nil (calendar-absolute-from-gregorian
  19202. (calendar-cursor-to-date))
  19203. nil))
  19204. (defun org-agenda-convert-date ()
  19205. (interactive)
  19206. (org-agenda-check-type t 'agenda 'timeline)
  19207. (let ((day (get-text-property (point) 'day))
  19208. date s)
  19209. (unless day
  19210. (error "Don't know which date to convert"))
  19211. (setq date (calendar-gregorian-from-absolute day))
  19212. (setq s (concat
  19213. "Gregorian: " (calendar-date-string date) "\n"
  19214. "ISO: " (calendar-iso-date-string date) "\n"
  19215. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  19216. "Julian: " (calendar-julian-date-string date) "\n"
  19217. "Astron. JD: " (calendar-astro-date-string date)
  19218. " (Julian date number at noon UTC)\n"
  19219. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  19220. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  19221. "French: " (calendar-french-date-string date) "\n"
  19222. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  19223. "Mayan: " (calendar-mayan-date-string date) "\n"
  19224. "Coptic: " (calendar-coptic-date-string date) "\n"
  19225. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  19226. "Persian: " (calendar-persian-date-string date) "\n"
  19227. "Chinese: " (calendar-chinese-date-string date) "\n"))
  19228. (with-output-to-temp-buffer "*Dates*"
  19229. (princ s))
  19230. (if (fboundp 'fit-window-to-buffer)
  19231. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  19232. ;;;; Embedded LaTeX
  19233. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  19234. "Keymap for the minor `org-cdlatex-mode'.")
  19235. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  19236. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  19237. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  19238. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  19239. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  19240. (defvar org-cdlatex-texmathp-advice-is-done nil
  19241. "Flag remembering if we have applied the advice to texmathp already.")
  19242. (define-minor-mode org-cdlatex-mode
  19243. "Toggle the minor `org-cdlatex-mode'.
  19244. This mode supports entering LaTeX environment and math in LaTeX fragments
  19245. in Org-mode.
  19246. \\{org-cdlatex-mode-map}"
  19247. nil " OCDL" nil
  19248. (when org-cdlatex-mode (require 'cdlatex))
  19249. (unless org-cdlatex-texmathp-advice-is-done
  19250. (setq org-cdlatex-texmathp-advice-is-done t)
  19251. (defadvice texmathp (around org-math-always-on activate)
  19252. "Always return t in org-mode buffers.
  19253. This is because we want to insert math symbols without dollars even outside
  19254. the LaTeX math segments. If Orgmode thinks that point is actually inside
  19255. en embedded LaTeX fragement, let texmathp do its job.
  19256. \\[org-cdlatex-mode-map]"
  19257. (interactive)
  19258. (let (p)
  19259. (cond
  19260. ((not (org-mode-p)) ad-do-it)
  19261. ((eq this-command 'cdlatex-math-symbol)
  19262. (setq ad-return-value t
  19263. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  19264. (t
  19265. (let ((p (org-inside-LaTeX-fragment-p)))
  19266. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  19267. (setq ad-return-value t
  19268. texmathp-why '("Org-mode embedded math" . 0))
  19269. (if p ad-do-it)))))))))
  19270. (defun turn-on-org-cdlatex ()
  19271. "Unconditionally turn on `org-cdlatex-mode'."
  19272. (org-cdlatex-mode 1))
  19273. (defun org-inside-LaTeX-fragment-p ()
  19274. "Test if point is inside a LaTeX fragment.
  19275. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  19276. sequence appearing also before point.
  19277. Even though the matchers for math are configurable, this function assumes
  19278. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  19279. delimiters are skipped when they have been removed by customization.
  19280. The return value is nil, or a cons cell with the delimiter and
  19281. and the position of this delimiter.
  19282. This function does a reasonably good job, but can locally be fooled by
  19283. for example currency specifications. For example it will assume being in
  19284. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  19285. fragments that are properly closed, but during editing, we have to live
  19286. with the uncertainty caused by missing closing delimiters. This function
  19287. looks only before point, not after."
  19288. (catch 'exit
  19289. (let ((pos (point))
  19290. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  19291. (lim (progn
  19292. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  19293. (point)))
  19294. dd-on str (start 0) m re)
  19295. (goto-char pos)
  19296. (when dodollar
  19297. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  19298. re (nth 1 (assoc "$" org-latex-regexps)))
  19299. (while (string-match re str start)
  19300. (cond
  19301. ((= (match-end 0) (length str))
  19302. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  19303. ((= (match-end 0) (- (length str) 5))
  19304. (throw 'exit nil))
  19305. (t (setq start (match-end 0))))))
  19306. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  19307. (goto-char pos)
  19308. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  19309. (and (match-beginning 2) (throw 'exit nil))
  19310. ;; count $$
  19311. (while (re-search-backward "\\$\\$" lim t)
  19312. (setq dd-on (not dd-on)))
  19313. (goto-char pos)
  19314. (if dd-on (cons "$$" m))))))
  19315. (defun org-try-cdlatex-tab ()
  19316. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  19317. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  19318. - inside a LaTeX fragment, or
  19319. - after the first word in a line, where an abbreviation expansion could
  19320. insert a LaTeX environment."
  19321. (when org-cdlatex-mode
  19322. (cond
  19323. ((save-excursion
  19324. (skip-chars-backward "a-zA-Z0-9*")
  19325. (skip-chars-backward " \t")
  19326. (bolp))
  19327. (cdlatex-tab) t)
  19328. ((org-inside-LaTeX-fragment-p)
  19329. (cdlatex-tab) t)
  19330. (t nil))))
  19331. (defun org-cdlatex-underscore-caret (&optional arg)
  19332. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  19333. Revert to the normal definition outside of these fragments."
  19334. (interactive "P")
  19335. (if (org-inside-LaTeX-fragment-p)
  19336. (call-interactively 'cdlatex-sub-superscript)
  19337. (let (org-cdlatex-mode)
  19338. (call-interactively (key-binding (vector last-input-event))))))
  19339. (defun org-cdlatex-math-modify (&optional arg)
  19340. "Execute `cdlatex-math-modify' in LaTeX fragments.
  19341. Revert to the normal definition outside of these fragments."
  19342. (interactive "P")
  19343. (if (org-inside-LaTeX-fragment-p)
  19344. (call-interactively 'cdlatex-math-modify)
  19345. (let (org-cdlatex-mode)
  19346. (call-interactively (key-binding (vector last-input-event))))))
  19347. (defvar org-latex-fragment-image-overlays nil
  19348. "List of overlays carrying the images of latex fragments.")
  19349. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  19350. (defun org-remove-latex-fragment-image-overlays ()
  19351. "Remove all overlays with LaTeX fragment images in current buffer."
  19352. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  19353. (setq org-latex-fragment-image-overlays nil))
  19354. (defun org-preview-latex-fragment (&optional subtree)
  19355. "Preview the LaTeX fragment at point, or all locally or globally.
  19356. If the cursor is in a LaTeX fragment, create the image and overlay
  19357. it over the source code. If there is no fragment at point, display
  19358. all fragments in the current text, from one headline to the next. With
  19359. prefix SUBTREE, display all fragments in the current subtree. With a
  19360. double prefix `C-u C-u', or when the cursor is before the first headline,
  19361. display all fragments in the buffer.
  19362. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  19363. (interactive "P")
  19364. (org-remove-latex-fragment-image-overlays)
  19365. (save-excursion
  19366. (save-restriction
  19367. (let (beg end at msg)
  19368. (cond
  19369. ((or (equal subtree '(16))
  19370. (not (save-excursion
  19371. (re-search-backward (concat "^" outline-regexp) nil t))))
  19372. (setq beg (point-min) end (point-max)
  19373. msg "Creating images for buffer...%s"))
  19374. ((equal subtree '(4))
  19375. (org-back-to-heading)
  19376. (setq beg (point) end (org-end-of-subtree t)
  19377. msg "Creating images for subtree...%s"))
  19378. (t
  19379. (if (setq at (org-inside-LaTeX-fragment-p))
  19380. (goto-char (max (point-min) (- (cdr at) 2)))
  19381. (org-back-to-heading))
  19382. (setq beg (point) end (progn (outline-next-heading) (point))
  19383. msg (if at "Creating image...%s"
  19384. "Creating images for entry...%s"))))
  19385. (message msg "")
  19386. (narrow-to-region beg end)
  19387. (goto-char beg)
  19388. (org-format-latex
  19389. (concat "ltxpng/" (file-name-sans-extension
  19390. (file-name-nondirectory
  19391. buffer-file-name)))
  19392. default-directory 'overlays msg at 'forbuffer)
  19393. (message msg "done. Use `C-c C-c' to remove images.")))))
  19394. (defvar org-latex-regexps
  19395. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  19396. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  19397. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  19398. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  19399. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  19400. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  19401. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  19402. "Regular expressions for matching embedded LaTeX.")
  19403. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  19404. "Replace LaTeX fragments with links to an image, and produce images."
  19405. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  19406. (let* ((prefixnodir (file-name-nondirectory prefix))
  19407. (absprefix (expand-file-name prefix dir))
  19408. (todir (file-name-directory absprefix))
  19409. (opt org-format-latex-options)
  19410. (matchers (plist-get opt :matchers))
  19411. (re-list org-latex-regexps)
  19412. (cnt 0) txt link beg end re e checkdir
  19413. m n block linkfile movefile ov)
  19414. ;; Check if there are old images files with this prefix, and remove them
  19415. (when (file-directory-p todir)
  19416. (mapc 'delete-file
  19417. (directory-files
  19418. todir 'full
  19419. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  19420. ;; Check the different regular expressions
  19421. (while (setq e (pop re-list))
  19422. (setq m (car e) re (nth 1 e) n (nth 2 e)
  19423. block (if (nth 3 e) "\n\n" ""))
  19424. (when (member m matchers)
  19425. (goto-char (point-min))
  19426. (while (re-search-forward re nil t)
  19427. (when (or (not at) (equal (cdr at) (match-beginning n)))
  19428. (setq txt (match-string n)
  19429. beg (match-beginning n) end (match-end n)
  19430. cnt (1+ cnt)
  19431. linkfile (format "%s_%04d.png" prefix cnt)
  19432. movefile (format "%s_%04d.png" absprefix cnt)
  19433. link (concat block "[[file:" linkfile "]]" block))
  19434. (if msg (message msg cnt))
  19435. (goto-char beg)
  19436. (unless checkdir ; make sure the directory exists
  19437. (setq checkdir t)
  19438. (or (file-directory-p todir) (make-directory todir)))
  19439. (org-create-formula-image
  19440. txt movefile opt forbuffer)
  19441. (if overlays
  19442. (progn
  19443. (setq ov (org-make-overlay beg end))
  19444. (if (featurep 'xemacs)
  19445. (progn
  19446. (org-overlay-put ov 'invisible t)
  19447. (org-overlay-put
  19448. ov 'end-glyph
  19449. (make-glyph (vector 'png :file movefile))))
  19450. (org-overlay-put
  19451. ov 'display
  19452. (list 'image :type 'png :file movefile :ascent 'center)))
  19453. (push ov org-latex-fragment-image-overlays)
  19454. (goto-char end))
  19455. (delete-region beg end)
  19456. (insert link))))))))
  19457. ;; This function borrows from Ganesh Swami's latex2png.el
  19458. (defun org-create-formula-image (string tofile options buffer)
  19459. (let* ((tmpdir (if (featurep 'xemacs)
  19460. (temp-directory)
  19461. temporary-file-directory))
  19462. (texfilebase (make-temp-name
  19463. (expand-file-name "orgtex" tmpdir)))
  19464. (texfile (concat texfilebase ".tex"))
  19465. (dvifile (concat texfilebase ".dvi"))
  19466. (pngfile (concat texfilebase ".png"))
  19467. (fnh (face-attribute 'default :height nil))
  19468. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  19469. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  19470. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  19471. "Black"))
  19472. (bg (or (plist-get options (if buffer :background :html-background))
  19473. "Transparent")))
  19474. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  19475. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  19476. (with-temp-file texfile
  19477. (insert org-format-latex-header
  19478. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  19479. (let ((dir default-directory))
  19480. (condition-case nil
  19481. (progn
  19482. (cd tmpdir)
  19483. (call-process "latex" nil nil nil texfile))
  19484. (error nil))
  19485. (cd dir))
  19486. (if (not (file-exists-p dvifile))
  19487. (progn (message "Failed to create dvi file from %s" texfile) nil)
  19488. (call-process "dvipng" nil nil nil
  19489. "-E" "-fg" fg "-bg" bg
  19490. "-D" dpi
  19491. ;;"-x" scale "-y" scale
  19492. "-T" "tight"
  19493. "-o" pngfile
  19494. dvifile)
  19495. (if (not (file-exists-p pngfile))
  19496. (progn (message "Failed to create png file from %s" texfile) nil)
  19497. ;; Use the requested file name and clean up
  19498. (copy-file pngfile tofile 'replace)
  19499. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  19500. (delete-file (concat texfilebase e)))
  19501. pngfile))))
  19502. (defun org-dvipng-color (attr)
  19503. "Return an rgb color specification for dvipng."
  19504. (apply 'format "rgb %s %s %s"
  19505. (mapcar 'org-normalize-color
  19506. (color-values (face-attribute 'default attr nil)))))
  19507. (defun org-normalize-color (value)
  19508. "Return string to be used as color value for an RGB component."
  19509. (format "%g" (/ value 65535.0)))
  19510. ;;;; Exporting
  19511. ;;; Variables, constants, and parameter plists
  19512. (defconst org-level-max 20)
  19513. (defvar org-export-html-preamble nil
  19514. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  19515. (defvar org-export-html-postamble nil
  19516. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  19517. (defvar org-export-html-auto-preamble t
  19518. "Should default preamble be inserted? Set by publishing functions.")
  19519. (defvar org-export-html-auto-postamble t
  19520. "Should default postamble be inserted? Set by publishing functions.")
  19521. (defvar org-current-export-file nil) ; dynamically scoped parameter
  19522. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  19523. (defconst org-export-plist-vars
  19524. '((:language . org-export-default-language)
  19525. (:customtime . org-display-custom-times)
  19526. (:headline-levels . org-export-headline-levels)
  19527. (:section-numbers . org-export-with-section-numbers)
  19528. (:table-of-contents . org-export-with-toc)
  19529. (:preserve-breaks . org-export-preserve-breaks)
  19530. (:archived-trees . org-export-with-archived-trees)
  19531. (:emphasize . org-export-with-emphasize)
  19532. (:sub-superscript . org-export-with-sub-superscripts)
  19533. (:footnotes . org-export-with-footnotes)
  19534. (:property-drawer . org-export-with-property-drawer)
  19535. (:tags . org-export-with-tags)
  19536. (:TeX-macros . org-export-with-TeX-macros)
  19537. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  19538. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  19539. (:fixed-width . org-export-with-fixed-width)
  19540. (:timestamps . org-export-with-timestamps)
  19541. (:author-info . org-export-author-info)
  19542. (:time-stamp-file . org-export-time-stamp-file)
  19543. (:tables . org-export-with-tables)
  19544. (:table-auto-headline . org-export-highlight-first-table-line)
  19545. (:style . org-export-html-style)
  19546. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  19547. (:convert-org-links . org-export-html-link-org-files-as-html)
  19548. (:inline-images . org-export-html-inline-images)
  19549. (:html-extension . org-export-html-extension)
  19550. (:expand-quoted-html . org-export-html-expand)
  19551. (:timestamp . org-export-html-with-timestamp)
  19552. (:publishing-directory . org-export-publishing-directory)
  19553. (:preamble . org-export-html-preamble)
  19554. (:postamble . org-export-html-postamble)
  19555. (:auto-preamble . org-export-html-auto-preamble)
  19556. (:auto-postamble . org-export-html-auto-postamble)
  19557. (:author . user-full-name)
  19558. (:email . user-mail-address)))
  19559. (defun org-default-export-plist ()
  19560. "Return the property list with default settings for the export variables."
  19561. (let ((l org-export-plist-vars) rtn e)
  19562. (while (setq e (pop l))
  19563. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  19564. rtn))
  19565. (defun org-infile-export-plist ()
  19566. "Return the property list with file-local settings for export."
  19567. (save-excursion
  19568. (goto-char 0)
  19569. (let ((re (org-make-options-regexp
  19570. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  19571. p key val text options)
  19572. (while (re-search-forward re nil t)
  19573. (setq key (org-match-string-no-properties 1)
  19574. val (org-match-string-no-properties 2))
  19575. (cond
  19576. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  19577. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  19578. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  19579. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  19580. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  19581. ((string-equal key "TEXT")
  19582. (setq text (if text (concat text "\n" val) val)))
  19583. ((string-equal key "OPTIONS") (setq options val))))
  19584. (setq p (plist-put p :text text))
  19585. (when options
  19586. (let ((op '(("H" . :headline-levels)
  19587. ("num" . :section-numbers)
  19588. ("toc" . :table-of-contents)
  19589. ("\\n" . :preserve-breaks)
  19590. ("@" . :expand-quoted-html)
  19591. (":" . :fixed-width)
  19592. ("|" . :tables)
  19593. ("^" . :sub-superscript)
  19594. ("f" . :footnotes)
  19595. ("p" . :property-drawer)
  19596. ("tags" . :tags)
  19597. ("*" . :emphasize)
  19598. ("TeX" . :TeX-macros)
  19599. ("LaTeX" . :LaTeX-fragments)
  19600. ("skip" . :skip-before-1st-heading)
  19601. ("author" . :author-info)
  19602. ("timestamp" . :time-stamp-file)))
  19603. o)
  19604. (while (setq o (pop op))
  19605. (if (string-match (concat (regexp-quote (car o))
  19606. ":\\([^ \t\n\r;,.]*\\)")
  19607. options)
  19608. (setq p (plist-put p (cdr o)
  19609. (car (read-from-string
  19610. (match-string 1 options)))))))))
  19611. p)))
  19612. (defun org-export-directory (type plist)
  19613. (let* ((val (plist-get plist :publishing-directory))
  19614. (dir (if (listp val)
  19615. (or (cdr (assoc type val)) ".")
  19616. val)))
  19617. dir))
  19618. (defun org-skip-comments (lines)
  19619. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  19620. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  19621. (re2 "^\\(\\*+\\)[ \t\n\r]")
  19622. (case-fold-search nil)
  19623. rtn line level)
  19624. (while (setq line (pop lines))
  19625. (cond
  19626. ((and (string-match re1 line)
  19627. (setq level (- (match-end 1) (match-beginning 1))))
  19628. ;; Beginning of a COMMENT subtree. Skip it.
  19629. (while (and (setq line (pop lines))
  19630. (or (not (string-match re2 line))
  19631. (> (- (match-end 1) (match-beginning 1)) level))))
  19632. (setq lines (cons line lines)))
  19633. ((string-match "^#" line)
  19634. ;; an ordinary comment line
  19635. )
  19636. ((and org-export-table-remove-special-lines
  19637. (string-match "^[ \t]*|" line)
  19638. (or (string-match "^[ \t]*| *[!_^] *|" line)
  19639. (and (string-match "| *<[0-9]+> *|" line)
  19640. (not (string-match "| *[^ <|]" line)))))
  19641. ;; a special table line that should be removed
  19642. )
  19643. (t (setq rtn (cons line rtn)))))
  19644. (nreverse rtn)))
  19645. (defun org-export (&optional arg)
  19646. (interactive)
  19647. (let ((help "[t] insert the export option template
  19648. \[v] limit export to visible part of outline tree
  19649. \[a] export as ASCII
  19650. \[h] export as HTML
  19651. \[H] export as HTML to temporary buffer
  19652. \[R] export region as HTML
  19653. \[b] export as HTML and browse immediately
  19654. \[x] export as XOXO
  19655. \[l] export as LaTeX
  19656. \[L] export as LaTeX to temporary buffer
  19657. \[i] export current file as iCalendar file
  19658. \[I] export all agenda files as iCalendar files
  19659. \[c] export agenda files into combined iCalendar file
  19660. \[F] publish current file
  19661. \[P] publish current project
  19662. \[X] publish... (project will be prompted for)
  19663. \[A] publish all projects")
  19664. (cmds
  19665. '((?t . org-insert-export-options-template)
  19666. (?v . org-export-visible)
  19667. (?a . org-export-as-ascii)
  19668. (?h . org-export-as-html)
  19669. (?b . org-export-as-html-and-open)
  19670. (?H . org-export-as-html-to-buffer)
  19671. (?R . org-export-region-as-html)
  19672. (?x . org-export-as-xoxo)
  19673. (?l . org-export-as-latex)
  19674. (?L . org-export-as-latex-to-buffer)
  19675. (?i . org-export-icalendar-this-file)
  19676. (?I . org-export-icalendar-all-agenda-files)
  19677. (?c . org-export-icalendar-combine-agenda-files)
  19678. (?F . org-publish-current-file)
  19679. (?P . org-publish-current-project)
  19680. (?X . org-publish)
  19681. (?A . org-publish-all)))
  19682. r1 r2 ass)
  19683. (save-window-excursion
  19684. (delete-other-windows)
  19685. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  19686. (princ help))
  19687. (message "Select command: ")
  19688. (setq r1 (read-char-exclusive)))
  19689. (setq r2 (if (< r1 27) (+ r1 96) r1))
  19690. (if (setq ass (assq r2 cmds))
  19691. (call-interactively (cdr ass))
  19692. (error "No command associated with key %c" r1))))
  19693. (defconst org-html-entities
  19694. '(("nbsp")
  19695. ("iexcl")
  19696. ("cent")
  19697. ("pound")
  19698. ("curren")
  19699. ("yen")
  19700. ("brvbar")
  19701. ("vert" . "&#124;")
  19702. ("sect")
  19703. ("uml")
  19704. ("copy")
  19705. ("ordf")
  19706. ("laquo")
  19707. ("not")
  19708. ("shy")
  19709. ("reg")
  19710. ("macr")
  19711. ("deg")
  19712. ("plusmn")
  19713. ("sup2")
  19714. ("sup3")
  19715. ("acute")
  19716. ("micro")
  19717. ("para")
  19718. ("middot")
  19719. ("odot"."o")
  19720. ("star"."*")
  19721. ("cedil")
  19722. ("sup1")
  19723. ("ordm")
  19724. ("raquo")
  19725. ("frac14")
  19726. ("frac12")
  19727. ("frac34")
  19728. ("iquest")
  19729. ("Agrave")
  19730. ("Aacute")
  19731. ("Acirc")
  19732. ("Atilde")
  19733. ("Auml")
  19734. ("Aring") ("AA"."&Aring;")
  19735. ("AElig")
  19736. ("Ccedil")
  19737. ("Egrave")
  19738. ("Eacute")
  19739. ("Ecirc")
  19740. ("Euml")
  19741. ("Igrave")
  19742. ("Iacute")
  19743. ("Icirc")
  19744. ("Iuml")
  19745. ("ETH")
  19746. ("Ntilde")
  19747. ("Ograve")
  19748. ("Oacute")
  19749. ("Ocirc")
  19750. ("Otilde")
  19751. ("Ouml")
  19752. ("times")
  19753. ("Oslash")
  19754. ("Ugrave")
  19755. ("Uacute")
  19756. ("Ucirc")
  19757. ("Uuml")
  19758. ("Yacute")
  19759. ("THORN")
  19760. ("szlig")
  19761. ("agrave")
  19762. ("aacute")
  19763. ("acirc")
  19764. ("atilde")
  19765. ("auml")
  19766. ("aring")
  19767. ("aelig")
  19768. ("ccedil")
  19769. ("egrave")
  19770. ("eacute")
  19771. ("ecirc")
  19772. ("euml")
  19773. ("igrave")
  19774. ("iacute")
  19775. ("icirc")
  19776. ("iuml")
  19777. ("eth")
  19778. ("ntilde")
  19779. ("ograve")
  19780. ("oacute")
  19781. ("ocirc")
  19782. ("otilde")
  19783. ("ouml")
  19784. ("divide")
  19785. ("oslash")
  19786. ("ugrave")
  19787. ("uacute")
  19788. ("ucirc")
  19789. ("uuml")
  19790. ("yacute")
  19791. ("thorn")
  19792. ("yuml")
  19793. ("fnof")
  19794. ("Alpha")
  19795. ("Beta")
  19796. ("Gamma")
  19797. ("Delta")
  19798. ("Epsilon")
  19799. ("Zeta")
  19800. ("Eta")
  19801. ("Theta")
  19802. ("Iota")
  19803. ("Kappa")
  19804. ("Lambda")
  19805. ("Mu")
  19806. ("Nu")
  19807. ("Xi")
  19808. ("Omicron")
  19809. ("Pi")
  19810. ("Rho")
  19811. ("Sigma")
  19812. ("Tau")
  19813. ("Upsilon")
  19814. ("Phi")
  19815. ("Chi")
  19816. ("Psi")
  19817. ("Omega")
  19818. ("alpha")
  19819. ("beta")
  19820. ("gamma")
  19821. ("delta")
  19822. ("epsilon")
  19823. ("varepsilon"."&epsilon;")
  19824. ("zeta")
  19825. ("eta")
  19826. ("theta")
  19827. ("iota")
  19828. ("kappa")
  19829. ("lambda")
  19830. ("mu")
  19831. ("nu")
  19832. ("xi")
  19833. ("omicron")
  19834. ("pi")
  19835. ("rho")
  19836. ("sigmaf") ("varsigma"."&sigmaf;")
  19837. ("sigma")
  19838. ("tau")
  19839. ("upsilon")
  19840. ("phi")
  19841. ("chi")
  19842. ("psi")
  19843. ("omega")
  19844. ("thetasym") ("vartheta"."&thetasym;")
  19845. ("upsih")
  19846. ("piv")
  19847. ("bull") ("bullet"."&bull;")
  19848. ("hellip") ("dots"."&hellip;")
  19849. ("prime")
  19850. ("Prime")
  19851. ("oline")
  19852. ("frasl")
  19853. ("weierp")
  19854. ("image")
  19855. ("real")
  19856. ("trade")
  19857. ("alefsym")
  19858. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  19859. ("uarr") ("uparrow"."&uarr;")
  19860. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  19861. ("darr")("downarrow"."&darr;")
  19862. ("harr") ("leftrightarrow"."&harr;")
  19863. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  19864. ("lArr") ("Leftarrow"."&lArr;")
  19865. ("uArr") ("Uparrow"."&uArr;")
  19866. ("rArr") ("Rightarrow"."&rArr;")
  19867. ("dArr") ("Downarrow"."&dArr;")
  19868. ("hArr") ("Leftrightarrow"."&hArr;")
  19869. ("forall")
  19870. ("part") ("partial"."&part;")
  19871. ("exist") ("exists"."&exist;")
  19872. ("empty") ("emptyset"."&empty;")
  19873. ("nabla")
  19874. ("isin") ("in"."&isin;")
  19875. ("notin")
  19876. ("ni")
  19877. ("prod")
  19878. ("sum")
  19879. ("minus")
  19880. ("lowast") ("ast"."&lowast;")
  19881. ("radic")
  19882. ("prop") ("proptp"."&prop;")
  19883. ("infin") ("infty"."&infin;")
  19884. ("ang") ("angle"."&ang;")
  19885. ("and") ("vee"."&and;")
  19886. ("or") ("wedge"."&or;")
  19887. ("cap")
  19888. ("cup")
  19889. ("int")
  19890. ("there4")
  19891. ("sim")
  19892. ("cong") ("simeq"."&cong;")
  19893. ("asymp")("approx"."&asymp;")
  19894. ("ne") ("neq"."&ne;")
  19895. ("equiv")
  19896. ("le")
  19897. ("ge")
  19898. ("sub") ("subset"."&sub;")
  19899. ("sup") ("supset"."&sup;")
  19900. ("nsub")
  19901. ("sube")
  19902. ("supe")
  19903. ("oplus")
  19904. ("otimes")
  19905. ("perp")
  19906. ("sdot") ("cdot"."&sdot;")
  19907. ("lceil")
  19908. ("rceil")
  19909. ("lfloor")
  19910. ("rfloor")
  19911. ("lang")
  19912. ("rang")
  19913. ("loz") ("Diamond"."&loz;")
  19914. ("spades") ("spadesuit"."&spades;")
  19915. ("clubs") ("clubsuit"."&clubs;")
  19916. ("hearts") ("diamondsuit"."&hearts;")
  19917. ("diams") ("diamondsuit"."&diams;")
  19918. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  19919. ("quot")
  19920. ("amp")
  19921. ("lt")
  19922. ("gt")
  19923. ("OElig")
  19924. ("oelig")
  19925. ("Scaron")
  19926. ("scaron")
  19927. ("Yuml")
  19928. ("circ")
  19929. ("tilde")
  19930. ("ensp")
  19931. ("emsp")
  19932. ("thinsp")
  19933. ("zwnj")
  19934. ("zwj")
  19935. ("lrm")
  19936. ("rlm")
  19937. ("ndash")
  19938. ("mdash")
  19939. ("lsquo")
  19940. ("rsquo")
  19941. ("sbquo")
  19942. ("ldquo")
  19943. ("rdquo")
  19944. ("bdquo")
  19945. ("dagger")
  19946. ("Dagger")
  19947. ("permil")
  19948. ("lsaquo")
  19949. ("rsaquo")
  19950. ("euro")
  19951. ("arccos"."arccos")
  19952. ("arcsin"."arcsin")
  19953. ("arctan"."arctan")
  19954. ("arg"."arg")
  19955. ("cos"."cos")
  19956. ("cosh"."cosh")
  19957. ("cot"."cot")
  19958. ("coth"."coth")
  19959. ("csc"."csc")
  19960. ("deg"."deg")
  19961. ("det"."det")
  19962. ("dim"."dim")
  19963. ("exp"."exp")
  19964. ("gcd"."gcd")
  19965. ("hom"."hom")
  19966. ("inf"."inf")
  19967. ("ker"."ker")
  19968. ("lg"."lg")
  19969. ("lim"."lim")
  19970. ("liminf"."liminf")
  19971. ("limsup"."limsup")
  19972. ("ln"."ln")
  19973. ("log"."log")
  19974. ("max"."max")
  19975. ("min"."min")
  19976. ("Pr"."Pr")
  19977. ("sec"."sec")
  19978. ("sin"."sin")
  19979. ("sinh"."sinh")
  19980. ("sup"."sup")
  19981. ("tan"."tan")
  19982. ("tanh"."tanh")
  19983. )
  19984. "Entities for TeX->HTML translation.
  19985. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  19986. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  19987. In that case, \"\\ent\" will be translated to \"&other;\".
  19988. The list contains HTML entities for Latin-1, Greek and other symbols.
  19989. It is supplemented by a number of commonly used TeX macros with appropriate
  19990. translations. There is currently no way for users to extend this.")
  19991. ;;; General functions for all backends
  19992. (defun org-cleaned-string-for-export (string &rest parameters)
  19993. "Cleanup a buffer STRING so that links can be created safely."
  19994. (interactive)
  19995. (let* ((re-radio (and org-target-link-regexp
  19996. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  19997. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  19998. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  19999. (re-archive (concat ":" org-archive-tag ":"))
  20000. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  20001. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  20002. (htmlp (plist-get parameters :for-html))
  20003. (asciip (plist-get parameters :for-ascii))
  20004. (latexp (plist-get parameters :for-LaTeX))
  20005. (commentsp (plist-get parameters :comments))
  20006. (archived-trees (plist-get parameters :archived-trees))
  20007. (inhibit-read-only t)
  20008. (outline-regexp "\\*+ ")
  20009. a b xx
  20010. rtn p)
  20011. (with-current-buffer (get-buffer-create " org-mode-tmp")
  20012. (erase-buffer)
  20013. (insert string)
  20014. ;; Remove license-to-kill stuff
  20015. (while (setq p (text-property-any (point-min) (point-max)
  20016. :org-license-to-kill t))
  20017. (delete-region p (next-single-property-change p :org-license-to-kill)))
  20018. (let ((org-inhibit-startup t)) (org-mode))
  20019. (untabify (point-min) (point-max))
  20020. ;; Get the correct stuff before the first headline
  20021. (when (plist-get parameters :skip-before-1st-heading)
  20022. (goto-char (point-min))
  20023. (when (re-search-forward "^\\*+[ \t]" nil t)
  20024. (delete-region (point-min) (match-beginning 0))
  20025. (goto-char (point-min))
  20026. (insert "\n")))
  20027. (when (plist-get parameters :add-text)
  20028. (goto-char (point-min))
  20029. (insert (plist-get parameters :add-text) "\n"))
  20030. ;; Get rid of archived trees
  20031. (when (not (eq archived-trees t))
  20032. (goto-char (point-min))
  20033. (while (re-search-forward re-archive nil t)
  20034. (if (not (org-on-heading-p t))
  20035. (org-end-of-subtree t)
  20036. (beginning-of-line 1)
  20037. (setq a (if archived-trees
  20038. (1+ (point-at-eol)) (point))
  20039. b (org-end-of-subtree t))
  20040. (if (> b a) (delete-region a b)))))
  20041. ;; Get rid of property drawers
  20042. (unless org-export-with-property-drawer
  20043. (goto-char (point-min))
  20044. (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t)
  20045. (replace-match "")))
  20046. ;; Find targets in comments and move them out of comments,
  20047. ;; but mark them as targets that should be invisible
  20048. (goto-char (point-min))
  20049. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  20050. (replace-match "\\1(INVISIBLE)"))
  20051. ;; Protect backend specific stuff, throw away the others.
  20052. (goto-char (point-min))
  20053. (let ((formatters
  20054. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  20055. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  20056. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  20057. fmt)
  20058. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  20059. (add-text-properties (match-beginning 0) (match-end 0)
  20060. '(org-protected t)))
  20061. (while formatters
  20062. (setq fmt (pop formatters))
  20063. (when (car fmt)
  20064. (goto-char (point-min))
  20065. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  20066. ":[ \t]*\\(.*\\)") nil t)
  20067. (replace-match "\\1" t)
  20068. (add-text-properties
  20069. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  20070. '(org-protected t))))
  20071. (goto-char (point-min))
  20072. (while (re-search-forward
  20073. (concat "^#\\+"
  20074. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  20075. (cadddr fmt) "\\>.*\n?") nil t)
  20076. (if (car fmt)
  20077. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  20078. '(org-protected t))
  20079. (delete-region (match-beginning 0) (match-end 0))))))
  20080. ;; Protect quoted subtrees
  20081. (goto-char (point-min))
  20082. (while (re-search-forward re-quote nil t)
  20083. (goto-char (match-beginning 0))
  20084. (end-of-line 1)
  20085. (add-text-properties (point) (org-end-of-subtree t)
  20086. '(org-protected t)))
  20087. ;; Remove subtrees that are commented
  20088. (goto-char (point-min))
  20089. (while (re-search-forward re-commented nil t)
  20090. (goto-char (match-beginning 0))
  20091. (delete-region (point) (org-end-of-subtree t)))
  20092. ;; Remove special table lines
  20093. (when org-export-table-remove-special-lines
  20094. (goto-char (point-min))
  20095. (while (re-search-forward "^[ \t]*|" nil t)
  20096. (beginning-of-line 1)
  20097. (if (or (looking-at "[ \t]*| *[!_^] *|")
  20098. (and (looking-at ".*?| *<[0-9]+> *|")
  20099. (not (looking-at ".*?| *[^ <|]"))))
  20100. (delete-region (max (point-min) (1- (point-at-bol)))
  20101. (point-at-eol))
  20102. (end-of-line 1))))
  20103. ;; Specific LaTeX stuff
  20104. (when latexp
  20105. (require 'org-export-latex nil)
  20106. (org-export-latex-cleaned-string))
  20107. ;; Specific HTML stuff
  20108. (when htmlp
  20109. ;; Convert LaTeX fragments to images
  20110. (when (plist-get parameters :LaTeX-fragments)
  20111. (org-format-latex
  20112. (concat "ltxpng/" (file-name-sans-extension
  20113. (file-name-nondirectory
  20114. org-current-export-file)))
  20115. org-current-export-dir nil "Creating LaTeX image %s"))
  20116. (message "Exporting..."))
  20117. ;; Remove or replace comments
  20118. (goto-char (point-min))
  20119. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  20120. (if commentsp
  20121. (progn (add-text-properties
  20122. (match-beginning 0) (match-end 0) '(org-protected t))
  20123. (replace-match (format commentsp (match-string 1)) t t))
  20124. (replace-match "")))
  20125. ;; Find matches for radio targets and turn them into internal links
  20126. (goto-char (point-min))
  20127. (when re-radio
  20128. (while (re-search-forward re-radio nil t)
  20129. (org-if-unprotected
  20130. (replace-match "\\1[[\\2]]"))))
  20131. ;; Find all links that contain a newline and put them into a single line
  20132. (goto-char (point-min))
  20133. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  20134. (org-if-unprotected
  20135. (replace-match "\\1 \\3")
  20136. (goto-char (match-beginning 0))))
  20137. ;; Normalize links: Convert angle and plain links into bracket links
  20138. ;; Expand link abbreviations
  20139. (goto-char (point-min))
  20140. (while (re-search-forward re-plain-link nil t)
  20141. (goto-char (1- (match-end 0)))
  20142. (org-if-unprotected
  20143. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  20144. ":" (match-string 3) "]]")))
  20145. ;; added 'org-link face to links
  20146. (put-text-property 0 (length s) 'face 'org-link s)
  20147. (replace-match s t t))))
  20148. (goto-char (point-min))
  20149. (while (re-search-forward re-angle-link nil t)
  20150. (goto-char (1- (match-end 0)))
  20151. (org-if-unprotected
  20152. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  20153. ":" (match-string 3) "]]")))
  20154. (put-text-property 0 (length s) 'face 'org-link s)
  20155. (replace-match s t t))))
  20156. (goto-char (point-min))
  20157. (while (re-search-forward org-bracket-link-regexp nil t)
  20158. (org-if-unprotected
  20159. (let* ((s (concat "[[" (setq xx (save-match-data
  20160. (org-link-expand-abbrev (match-string 1))))
  20161. "]"
  20162. (if (match-end 3)
  20163. (match-string 2)
  20164. (concat "[" xx "]"))
  20165. "]")))
  20166. (put-text-property 0 (length s) 'face 'org-link s)
  20167. (replace-match s t t))))
  20168. ;; Find multiline emphasis and put them into single line
  20169. (when (plist-get parameters :emph-multiline)
  20170. (goto-char (point-min))
  20171. (while (re-search-forward org-emph-re nil t)
  20172. (if (not (= (char-after (match-beginning 3))
  20173. (char-after (match-beginning 4))))
  20174. (org-if-unprotected
  20175. (subst-char-in-region (match-beginning 0) (match-end 0)
  20176. ?\n ?\ t)
  20177. (goto-char (1- (match-end 0))))
  20178. (goto-char (1+ (match-beginning 0))))))
  20179. (setq rtn (buffer-string)))
  20180. (kill-buffer " org-mode-tmp")
  20181. rtn))
  20182. (defun org-export-grab-title-from-buffer ()
  20183. "Get a title for the current document, from looking at the buffer."
  20184. (let ((inhibit-read-only t))
  20185. (save-excursion
  20186. (goto-char (point-min))
  20187. (let ((end (save-excursion (outline-next-heading) (point))))
  20188. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  20189. ;; Mark the line so that it will not be exported as normal text.
  20190. (org-unmodified
  20191. (add-text-properties (match-beginning 0) (match-end 0)
  20192. (list :org-license-to-kill t)))
  20193. ;; Return the title string
  20194. (org-trim (match-string 0)))))))
  20195. (defun org-export-get-title-from-subtree ()
  20196. "Return subtree title and exclude it from export."
  20197. (let (title (m (mark)))
  20198. (save-excursion
  20199. (goto-char (region-beginning))
  20200. (when (and (org-at-heading-p)
  20201. (>= (org-end-of-subtree t t) (region-end)))
  20202. ;; This is a subtree, we take the title from the first heading
  20203. (goto-char (region-beginning))
  20204. (looking-at org-todo-line-regexp)
  20205. (setq title (match-string 3))
  20206. (org-unmodified
  20207. (add-text-properties (point) (1+ (point-at-eol))
  20208. (list :org-license-to-kill t)))))
  20209. title))
  20210. (defun org-solidify-link-text (s &optional alist)
  20211. "Take link text and make a safe target out of it."
  20212. (save-match-data
  20213. (let* ((rtn
  20214. (mapconcat
  20215. 'identity
  20216. (org-split-string s "[ \t\r\n]+") "--"))
  20217. (a (assoc rtn alist)))
  20218. (or (cdr a) rtn))))
  20219. (defun org-get-min-level (lines)
  20220. "Get the minimum level in LINES."
  20221. (let ((re "^\\(\\*+\\) ") l min)
  20222. (catch 'exit
  20223. (while (setq l (pop lines))
  20224. (if (string-match re l)
  20225. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  20226. 1)))
  20227. ;; Variable holding the vector with section numbers
  20228. (defvar org-section-numbers (make-vector org-level-max 0))
  20229. (defun org-init-section-numbers ()
  20230. "Initialize the vector for the section numbers."
  20231. (let* ((level -1)
  20232. (numbers (nreverse (org-split-string "" "\\.")))
  20233. (depth (1- (length org-section-numbers)))
  20234. (i depth) number-string)
  20235. (while (>= i 0)
  20236. (if (> i level)
  20237. (aset org-section-numbers i 0)
  20238. (setq number-string (or (car numbers) "0"))
  20239. (if (string-match "\\`[A-Z]\\'" number-string)
  20240. (aset org-section-numbers i
  20241. (- (string-to-char number-string) ?A -1))
  20242. (aset org-section-numbers i (string-to-number number-string)))
  20243. (pop numbers))
  20244. (setq i (1- i)))))
  20245. (defun org-section-number (&optional level)
  20246. "Return a string with the current section number.
  20247. When LEVEL is non-nil, increase section numbers on that level."
  20248. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  20249. (when level
  20250. (when (> level -1)
  20251. (aset org-section-numbers
  20252. level (1+ (aref org-section-numbers level))))
  20253. (setq idx (1+ level))
  20254. (while (<= idx depth)
  20255. (if (not (= idx 1))
  20256. (aset org-section-numbers idx 0))
  20257. (setq idx (1+ idx))))
  20258. (setq idx 0)
  20259. (while (<= idx depth)
  20260. (setq n (aref org-section-numbers idx))
  20261. (setq string (concat string (if (not (string= string "")) "." "")
  20262. (int-to-string n)))
  20263. (setq idx (1+ idx)))
  20264. (save-match-data
  20265. (if (string-match "\\`\\([@0]\\.\\)+" string)
  20266. (setq string (replace-match "" t nil string)))
  20267. (if (string-match "\\(\\.0\\)+\\'" string)
  20268. (setq string (replace-match "" t nil string))))
  20269. string))
  20270. ;;; ASCII export
  20271. (defvar org-last-level nil) ; dynamically scoped variable
  20272. (defvar org-min-level nil) ; dynamically scoped variable
  20273. (defvar org-levels-open nil) ; dynamically scoped parameter
  20274. (defvar org-ascii-current-indentation nil) ; For communication
  20275. (defun org-export-as-ascii (arg)
  20276. "Export the outline as a pretty ASCII file.
  20277. If there is an active region, export only the region.
  20278. The prefix ARG specifies how many levels of the outline should become
  20279. underlined headlines. The default is 3."
  20280. (interactive "P")
  20281. (setq-default org-todo-line-regexp org-todo-line-regexp)
  20282. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  20283. (org-infile-export-plist)))
  20284. (region-p (org-region-active-p))
  20285. (subtree-p
  20286. (when region-p
  20287. (save-excursion
  20288. (goto-char (region-beginning))
  20289. (and (org-at-heading-p)
  20290. (>= (org-end-of-subtree t t) (region-end))))))
  20291. (custom-times org-display-custom-times)
  20292. (org-ascii-current-indentation '(0 . 0))
  20293. (level 0) line txt
  20294. (umax nil)
  20295. (umax-toc nil)
  20296. (case-fold-search nil)
  20297. (filename (concat (file-name-as-directory
  20298. (org-export-directory :ascii opt-plist))
  20299. (file-name-sans-extension
  20300. (or (and subtree-p
  20301. (org-entry-get (region-beginning)
  20302. "EXPORT_FILE_NAME" t))
  20303. (file-name-nondirectory buffer-file-name)))
  20304. ".txt"))
  20305. (filename (if (equal (file-truename filename)
  20306. (file-truename buffer-file-name))
  20307. (concat filename ".txt")
  20308. filename))
  20309. (buffer (find-file-noselect filename))
  20310. (org-levels-open (make-vector org-level-max nil))
  20311. (odd org-odd-levels-only)
  20312. (date (plist-get opt-plist :date))
  20313. (author (plist-get opt-plist :author))
  20314. (title (or (and subtree-p (org-export-get-title-from-subtree))
  20315. (plist-get opt-plist :title)
  20316. (and (not
  20317. (plist-get opt-plist :skip-before-1st-heading))
  20318. (org-export-grab-title-from-buffer))
  20319. (file-name-sans-extension
  20320. (file-name-nondirectory buffer-file-name))))
  20321. (email (plist-get opt-plist :email))
  20322. (language (plist-get opt-plist :language))
  20323. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  20324. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  20325. (todo nil)
  20326. (lang-words nil)
  20327. (region
  20328. (buffer-substring
  20329. (if (org-region-active-p) (region-beginning) (point-min))
  20330. (if (org-region-active-p) (region-end) (point-max))))
  20331. (lines (org-split-string
  20332. (org-cleaned-string-for-export
  20333. region
  20334. :for-ascii t
  20335. :skip-before-1st-heading
  20336. (plist-get opt-plist :skip-before-1st-heading)
  20337. :archived-trees
  20338. (plist-get opt-plist :archived-trees)
  20339. :add-text (plist-get opt-plist :text))
  20340. "\n"))
  20341. thetoc have-headings first-heading-pos
  20342. table-open table-buffer)
  20343. (let ((inhibit-read-only t))
  20344. (org-unmodified
  20345. (remove-text-properties (point-min) (point-max)
  20346. '(:org-license-to-kill t))))
  20347. (setq org-min-level (org-get-min-level lines))
  20348. (setq org-last-level org-min-level)
  20349. (org-init-section-numbers)
  20350. (find-file-noselect filename)
  20351. (setq lang-words (or (assoc language org-export-language-setup)
  20352. (assoc "en" org-export-language-setup)))
  20353. (switch-to-buffer-other-window buffer)
  20354. (erase-buffer)
  20355. (fundamental-mode)
  20356. ;; create local variables for all options, to make sure all called
  20357. ;; functions get the correct information
  20358. (mapcar (lambda (x)
  20359. (set (make-local-variable (cdr x))
  20360. (plist-get opt-plist (car x))))
  20361. org-export-plist-vars)
  20362. (org-set-local 'org-odd-levels-only odd)
  20363. (setq umax (if arg (prefix-numeric-value arg)
  20364. org-export-headline-levels))
  20365. (setq umax-toc (if (integerp org-export-with-toc)
  20366. (min org-export-with-toc umax)
  20367. umax))
  20368. ;; File header
  20369. (if title (org-insert-centered title ?=))
  20370. (insert "\n")
  20371. (if (and (or author email)
  20372. org-export-author-info)
  20373. (insert (concat (nth 1 lang-words) ": " (or author "")
  20374. (if email (concat " <" email ">") "")
  20375. "\n")))
  20376. (cond
  20377. ((and date (string-match "%" date))
  20378. (setq date (format-time-string date (current-time))))
  20379. (date)
  20380. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  20381. (if (and date org-export-time-stamp-file)
  20382. (insert (concat (nth 2 lang-words) ": " date"\n")))
  20383. (insert "\n\n")
  20384. (if org-export-with-toc
  20385. (progn
  20386. (push (concat (nth 3 lang-words) "\n") thetoc)
  20387. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  20388. (mapcar '(lambda (line)
  20389. (if (string-match org-todo-line-regexp
  20390. line)
  20391. ;; This is a headline
  20392. (progn
  20393. (setq have-headings t)
  20394. (setq level (- (match-end 1) (match-beginning 1))
  20395. level (org-tr-level level)
  20396. txt (match-string 3 line)
  20397. todo
  20398. (or (and org-export-mark-todo-in-toc
  20399. (match-beginning 2)
  20400. (not (member (match-string 2 line)
  20401. org-done-keywords)))
  20402. ; TODO, not DONE
  20403. (and org-export-mark-todo-in-toc
  20404. (= level umax-toc)
  20405. (org-search-todo-below
  20406. line lines level))))
  20407. (setq txt (org-html-expand-for-ascii txt))
  20408. (while (string-match org-bracket-link-regexp txt)
  20409. (setq txt
  20410. (replace-match
  20411. (match-string (if (match-end 2) 3 1) txt)
  20412. t t txt)))
  20413. (if (and (memq org-export-with-tags '(not-in-toc nil))
  20414. (string-match
  20415. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  20416. txt))
  20417. (setq txt (replace-match "" t t txt)))
  20418. (if (string-match quote-re0 txt)
  20419. (setq txt (replace-match "" t t txt)))
  20420. (if org-export-with-section-numbers
  20421. (setq txt (concat (org-section-number level)
  20422. " " txt)))
  20423. (if (<= level umax-toc)
  20424. (progn
  20425. (push
  20426. (concat
  20427. (make-string
  20428. (* (max 0 (- level org-min-level)) 4) ?\ )
  20429. (format (if todo "%s (*)\n" "%s\n") txt))
  20430. thetoc)
  20431. (setq org-last-level level))
  20432. ))))
  20433. lines)
  20434. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  20435. (org-init-section-numbers)
  20436. (while (setq line (pop lines))
  20437. ;; Remove the quoted HTML tags.
  20438. (setq line (org-html-expand-for-ascii line))
  20439. ;; Remove targets
  20440. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  20441. (setq line (replace-match "" t t line)))
  20442. ;; Replace internal links
  20443. (while (string-match org-bracket-link-regexp line)
  20444. (setq line (replace-match
  20445. (if (match-end 3) "[\\3]" "[\\1]")
  20446. t nil line)))
  20447. (when custom-times
  20448. (setq line (org-translate-time line)))
  20449. (cond
  20450. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  20451. ;; a Headline
  20452. (setq first-heading-pos (or first-heading-pos (point)))
  20453. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  20454. txt (match-string 2 line))
  20455. (org-ascii-level-start level txt umax lines))
  20456. ((and org-export-with-tables
  20457. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  20458. (if (not table-open)
  20459. ;; New table starts
  20460. (setq table-open t table-buffer nil))
  20461. ;; Accumulate lines
  20462. (setq table-buffer (cons line table-buffer))
  20463. (when (or (not lines)
  20464. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  20465. (car lines))))
  20466. (setq table-open nil
  20467. table-buffer (nreverse table-buffer))
  20468. (insert (mapconcat
  20469. (lambda (x)
  20470. (org-fix-indentation x org-ascii-current-indentation))
  20471. (org-format-table-ascii table-buffer)
  20472. "\n") "\n")))
  20473. (t
  20474. (setq line (org-fix-indentation line org-ascii-current-indentation))
  20475. (if (and org-export-with-fixed-width
  20476. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  20477. (setq line (replace-match "\\1" nil nil line)))
  20478. (insert line "\n"))))
  20479. (normal-mode)
  20480. ;; insert the table of contents
  20481. (when thetoc
  20482. (goto-char (point-min))
  20483. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  20484. (progn
  20485. (goto-char (match-beginning 0))
  20486. (replace-match ""))
  20487. (goto-char first-heading-pos))
  20488. (mapc 'insert thetoc)
  20489. (or (looking-at "[ \t]*\n[ \t]*\n")
  20490. (insert "\n\n")))
  20491. (save-buffer)
  20492. ;; remove display and invisible chars
  20493. (let (beg end)
  20494. (goto-char (point-min))
  20495. (while (setq beg (next-single-property-change (point) 'display))
  20496. (setq end (next-single-property-change beg 'display))
  20497. (delete-region beg end)
  20498. (goto-char beg)
  20499. (insert "=>"))
  20500. (goto-char (point-min))
  20501. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  20502. (setq end (next-single-property-change beg 'org-cwidth))
  20503. (delete-region beg end)
  20504. (goto-char beg)))
  20505. (goto-char (point-min))))
  20506. (defun org-search-todo-below (line lines level)
  20507. "Search the subtree below LINE for any TODO entries."
  20508. (let ((rest (cdr (memq line lines)))
  20509. (re org-todo-line-regexp)
  20510. line lv todo)
  20511. (catch 'exit
  20512. (while (setq line (pop rest))
  20513. (if (string-match re line)
  20514. (progn
  20515. (setq lv (- (match-end 1) (match-beginning 1))
  20516. todo (and (match-beginning 2)
  20517. (not (member (match-string 2 line)
  20518. org-done-keywords))))
  20519. ; TODO, not DONE
  20520. (if (<= lv level) (throw 'exit nil))
  20521. (if todo (throw 'exit t))))))))
  20522. (defun org-html-expand-for-ascii (line)
  20523. "Handle quoted HTML for ASCII export."
  20524. (if org-export-html-expand
  20525. (while (string-match "@<[^<>\n]*>" line)
  20526. ;; We just remove the tags for now.
  20527. (setq line (replace-match "" nil nil line))))
  20528. line)
  20529. (defun org-insert-centered (s &optional underline)
  20530. "Insert the string S centered and underline it with character UNDERLINE."
  20531. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  20532. (insert (make-string ind ?\ ) s "\n")
  20533. (if underline
  20534. (insert (make-string ind ?\ )
  20535. (make-string (string-width s) underline)
  20536. "\n"))))
  20537. (defun org-ascii-level-start (level title umax &optional lines)
  20538. "Insert a new level in ASCII export."
  20539. (let (char (n (- level umax 1)) (ind 0))
  20540. (if (> level umax)
  20541. (progn
  20542. (insert (make-string (* 2 n) ?\ )
  20543. (char-to-string (nth (% n (length org-export-ascii-bullets))
  20544. org-export-ascii-bullets))
  20545. " " title "\n")
  20546. ;; find the indentation of the next non-empty line
  20547. (catch 'stop
  20548. (while lines
  20549. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  20550. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  20551. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  20552. (pop lines)))
  20553. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  20554. (if (or (not (equal (char-before) ?\n))
  20555. (not (equal (char-before (1- (point))) ?\n)))
  20556. (insert "\n"))
  20557. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  20558. (unless org-export-with-tags
  20559. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  20560. (setq title (replace-match "" t t title))))
  20561. (if org-export-with-section-numbers
  20562. (setq title (concat (org-section-number level) " " title)))
  20563. (insert title "\n" (make-string (string-width title) char) "\n")
  20564. (setq org-ascii-current-indentation '(0 . 0)))))
  20565. (defun org-export-visible (type arg)
  20566. "Create a copy of the visible part of the current buffer, and export it.
  20567. The copy is created in a temporary buffer and removed after use.
  20568. TYPE is the final key (as a string) that also select the export command in
  20569. the `C-c C-e' export dispatcher.
  20570. As a special case, if the you type SPC at the prompt, the temporary
  20571. org-mode file will not be removed but presented to you so that you can
  20572. continue to use it. The prefix arg ARG is passed through to the exporting
  20573. command."
  20574. (interactive
  20575. (list (progn
  20576. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  20577. (read-char-exclusive))
  20578. current-prefix-arg))
  20579. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  20580. (error "Invalid export key"))
  20581. (let* ((binding (cdr (assoc type
  20582. '((?a . org-export-as-ascii)
  20583. (?\C-a . org-export-as-ascii)
  20584. (?b . org-export-as-html-and-open)
  20585. (?\C-b . org-export-as-html-and-open)
  20586. (?h . org-export-as-html)
  20587. (?H . org-export-as-html-to-buffer)
  20588. (?R . org-export-region-as-html)
  20589. (?x . org-export-as-xoxo)))))
  20590. (keepp (equal type ?\ ))
  20591. (file buffer-file-name)
  20592. (buffer (get-buffer-create "*Org Export Visible*"))
  20593. s e)
  20594. ;; Need to hack the drawers here.
  20595. (save-excursion
  20596. (goto-char (point-min))
  20597. (while (re-search-forward org-drawer-regexp nil t)
  20598. (goto-char (match-beginning 1))
  20599. (or (org-invisible-p) (org-flag-drawer nil))))
  20600. (with-current-buffer buffer (erase-buffer))
  20601. (save-excursion
  20602. (setq s (goto-char (point-min)))
  20603. (while (not (= (point) (point-max)))
  20604. (goto-char (org-find-invisible))
  20605. (append-to-buffer buffer s (point))
  20606. (setq s (goto-char (org-find-visible))))
  20607. (org-cycle-hide-drawers 'all)
  20608. (goto-char (point-min))
  20609. (unless keepp
  20610. ;; Copy all comment lines to the end, to make sure #+ settings are
  20611. ;; still available for the second export step. Kind of a hack, but
  20612. ;; does do the trick.
  20613. (if (looking-at "#[^\r\n]*")
  20614. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  20615. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  20616. (append-to-buffer buffer (1+ (match-beginning 0))
  20617. (min (point-max) (1+ (match-end 0))))))
  20618. (set-buffer buffer)
  20619. (let ((buffer-file-name file)
  20620. (org-inhibit-startup t))
  20621. (org-mode)
  20622. (show-all)
  20623. (unless keepp (funcall binding arg))))
  20624. (if (not keepp)
  20625. (kill-buffer buffer)
  20626. (switch-to-buffer-other-window buffer)
  20627. (goto-char (point-min)))))
  20628. (defun org-find-visible ()
  20629. (let ((s (point)))
  20630. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  20631. (get-char-property s 'invisible)))
  20632. s))
  20633. (defun org-find-invisible ()
  20634. (let ((s (point)))
  20635. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  20636. (not (get-char-property s 'invisible))))
  20637. s))
  20638. ;;; HTML export
  20639. (defun org-get-current-options ()
  20640. "Return a string with current options as keyword options.
  20641. Does include HTML export options as well as TODO and CATEGORY stuff."
  20642. (format
  20643. "#+TITLE: %s
  20644. #+AUTHOR: %s
  20645. #+EMAIL: %s
  20646. #+LANGUAGE: %s
  20647. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  20648. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s tags:%s
  20649. #+CATEGORY: %s
  20650. #+SEQ_TODO: %s
  20651. #+TYP_TODO: %s
  20652. #+PRIORITIES: %c %c %c
  20653. #+STARTUP: %s %s %s %s %s
  20654. #+TAGS: %s
  20655. #+ARCHIVE: %s
  20656. #+LINK: %s
  20657. "
  20658. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  20659. org-export-headline-levels
  20660. org-export-with-section-numbers
  20661. org-export-with-toc
  20662. org-export-preserve-breaks
  20663. org-export-html-expand
  20664. org-export-with-fixed-width
  20665. org-export-with-tables
  20666. org-export-with-sub-superscripts
  20667. org-export-with-footnotes
  20668. org-export-with-emphasize
  20669. org-export-with-TeX-macros
  20670. org-export-with-LaTeX-fragments
  20671. org-export-skip-text-before-1st-heading
  20672. org-export-with-property-drawer
  20673. org-export-with-tags
  20674. (file-name-nondirectory buffer-file-name)
  20675. "TODO FEEDBACK VERIFY DONE"
  20676. "Me Jason Marie DONE"
  20677. org-highest-priority org-lowest-priority org-default-priority
  20678. (cdr (assoc org-startup-folded
  20679. '((nil . "showall") (t . "overview") (content . "content"))))
  20680. (if org-odd-levels-only "odd" "oddeven")
  20681. (if org-hide-leading-stars "hidestars" "showstars")
  20682. (if org-startup-align-all-tables "align" "noalign")
  20683. (cond ((eq t org-log-done) "logdone")
  20684. ((not org-log-done) "nologging")
  20685. ((listp org-log-done)
  20686. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  20687. org-log-done " ")))
  20688. (or (mapconcat (lambda (x)
  20689. (cond
  20690. ((equal '(:startgroup) x) "{")
  20691. ((equal '(:endgroup) x) "}")
  20692. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  20693. (t (car x))))
  20694. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  20695. org-archive-location
  20696. "org file:~/org/%s.org"
  20697. ))
  20698. (defun org-insert-export-options-template ()
  20699. "Insert into the buffer a template with information for exporting."
  20700. (interactive)
  20701. (if (not (bolp)) (newline))
  20702. (let ((s (org-get-current-options)))
  20703. (and (string-match "#\\+CATEGORY" s)
  20704. (setq s (substring s 0 (match-beginning 0))))
  20705. (insert s)))
  20706. (defun org-toggle-fixed-width-section (arg)
  20707. "Toggle the fixed-width export.
  20708. If there is no active region, the QUOTE keyword at the current headline is
  20709. inserted or removed. When present, it causes the text between this headline
  20710. and the next to be exported as fixed-width text, and unmodified.
  20711. If there is an active region, this command adds or removes a colon as the
  20712. first character of this line. If the first character of a line is a colon,
  20713. this line is also exported in fixed-width font."
  20714. (interactive "P")
  20715. (let* ((cc 0)
  20716. (regionp (org-region-active-p))
  20717. (beg (if regionp (region-beginning) (point)))
  20718. (end (if regionp (region-end)))
  20719. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20720. (re "[ \t]*\\(:\\)")
  20721. off)
  20722. (if regionp
  20723. (save-excursion
  20724. (goto-char beg)
  20725. (setq cc (current-column))
  20726. (beginning-of-line 1)
  20727. (setq off (looking-at re))
  20728. (while (> nlines 0)
  20729. (setq nlines (1- nlines))
  20730. (beginning-of-line 1)
  20731. (cond
  20732. (arg
  20733. (move-to-column cc t)
  20734. (insert ":\n")
  20735. (forward-line -1))
  20736. ((and off (looking-at re))
  20737. (replace-match "" t t nil 1))
  20738. ((not off) (move-to-column cc t) (insert ":")))
  20739. (forward-line 1)))
  20740. (save-excursion
  20741. (org-back-to-heading)
  20742. (if (looking-at (concat outline-regexp
  20743. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  20744. (replace-match "" t t nil 1)
  20745. (if (looking-at outline-regexp)
  20746. (progn
  20747. (goto-char (match-end 0))
  20748. (insert org-quote-string " "))))))))
  20749. (defun org-export-as-html-and-open (arg)
  20750. "Export the outline as HTML and immediately open it with a browser.
  20751. If there is an active region, export only the region.
  20752. The prefix ARG specifies how many levels of the outline should become
  20753. headlines. The default is 3. Lower levels will become bulleted lists."
  20754. (interactive "P")
  20755. (org-export-as-html arg 'hidden)
  20756. (org-open-file buffer-file-name))
  20757. (defun org-export-as-html-batch ()
  20758. "Call `org-export-as-html', may be used in batch processing as
  20759. emacs --batch
  20760. --load=$HOME/lib/emacs/org.el
  20761. --eval \"(setq org-export-headline-levels 2)\"
  20762. --visit=MyFile --funcall org-export-as-html-batch"
  20763. (org-export-as-html org-export-headline-levels 'hidden))
  20764. (defun org-export-as-html-to-buffer (arg)
  20765. "Call `org-exort-as-html` with output to a temporary buffer.
  20766. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  20767. (interactive "P")
  20768. (org-export-as-html arg nil nil "*Org HTML Export*")
  20769. (switch-to-buffer-other-window "*Org HTML Export*"))
  20770. (defun org-replace-region-by-html (beg end)
  20771. "Assume the current region has org-mode syntax, and convert it to HTML.
  20772. This can be used in any buffer. For example, you could write an
  20773. itemized list in org-mode syntax in an HTML buffer and then use this
  20774. command to convert it."
  20775. (interactive "r")
  20776. (let (reg html buf pop-up-frames)
  20777. (save-window-excursion
  20778. (if (org-mode-p)
  20779. (setq html (org-export-region-as-html
  20780. beg end t 'string))
  20781. (setq reg (buffer-substring beg end)
  20782. buf (get-buffer-create "*Org tmp*"))
  20783. (with-current-buffer buf
  20784. (erase-buffer)
  20785. (insert reg)
  20786. (org-mode)
  20787. (setq html (org-export-region-as-html
  20788. (point-min) (point-max) t 'string)))
  20789. (kill-buffer buf)))
  20790. (delete-region beg end)
  20791. (insert html)))
  20792. (defun org-export-region-as-html (beg end &optional body-only buffer)
  20793. "Convert region from BEG to END in org-mode buffer to HTML.
  20794. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  20795. contents, and only produce the region of converted text, useful for
  20796. cut-and-paste operations.
  20797. If BUFFER is a buffer or a string, use/create that buffer as a target
  20798. of the converted HTML. If BUFFER is the symbol `string', return the
  20799. produced HTML as a string and leave not buffer behind. For example,
  20800. a Lisp program could call this function in the following way:
  20801. (setq html (org-export-region-as-html beg end t 'string))
  20802. When called interactively, the output buffer is selected, and shown
  20803. in a window. A non-interactive call will only retunr the buffer."
  20804. (interactive "r\nP")
  20805. (when (interactive-p)
  20806. (setq buffer "*Org HTML Export*"))
  20807. (let ((transient-mark-mode t) (zmacs-regions t)
  20808. rtn)
  20809. (goto-char end)
  20810. (set-mark (point)) ;; to activate the region
  20811. (goto-char beg)
  20812. (setq rtn (org-export-as-html
  20813. nil nil nil
  20814. buffer body-only))
  20815. (if (fboundp 'deactivate-mark) (deactivate-mark))
  20816. (if (and (interactive-p) (bufferp rtn))
  20817. (switch-to-buffer-other-window rtn)
  20818. rtn)))
  20819. (defun org-export-as-html (arg &optional hidden ext-plist
  20820. to-buffer body-only)
  20821. "Export the outline as a pretty HTML file.
  20822. If there is an active region, export only the region. The prefix
  20823. ARG specifies how many levels of the outline should become
  20824. headlines. The default is 3. Lower levels will become bulleted
  20825. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  20826. EXT-PLIST is a property list with external parameters overriding
  20827. org-mode's default settings, but still inferior to file-local
  20828. settings. When TO-BUFFER is non-nil, create a buffer with that
  20829. name and export to that buffer. If TO-BUFFER is the symbol `string',
  20830. don't leave any buffer behind but just return the resulting HTML as
  20831. a string. When BODY-ONLY is set, don't produce the file header and footer,
  20832. simply return the content of <body>...</body>, without even
  20833. the body tags themselves."
  20834. (interactive "P")
  20835. ;; Make sure we have a file name when we need it.
  20836. (when (and (not (or to-buffer body-only))
  20837. (not buffer-file-name))
  20838. (if (buffer-base-buffer)
  20839. (org-set-local 'buffer-file-name
  20840. (with-current-buffer (buffer-base-buffer)
  20841. buffer-file-name))
  20842. (error "Need a file name to be able to export.")))
  20843. (message "Exporting...")
  20844. (setq-default org-todo-line-regexp org-todo-line-regexp)
  20845. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  20846. (setq-default org-done-keywords org-done-keywords)
  20847. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  20848. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  20849. ext-plist
  20850. (org-infile-export-plist)))
  20851. (style (plist-get opt-plist :style))
  20852. (link-validate (plist-get opt-plist :link-validation-function))
  20853. valid thetoc have-headings first-heading-pos
  20854. (odd org-odd-levels-only)
  20855. (region-p (org-region-active-p))
  20856. (subtree-p
  20857. (when region-p
  20858. (save-excursion
  20859. (goto-char (region-beginning))
  20860. (and (org-at-heading-p)
  20861. (>= (org-end-of-subtree t t) (region-end))))))
  20862. ;; The following two are dynamically scoped into other
  20863. ;; routines below.
  20864. (org-current-export-dir (org-export-directory :html opt-plist))
  20865. (org-current-export-file buffer-file-name)
  20866. (level 0) (line "") (origline "") txt todo
  20867. (umax nil)
  20868. (umax-toc nil)
  20869. (filename (if to-buffer nil
  20870. (concat (file-name-as-directory
  20871. (org-export-directory :html opt-plist))
  20872. (file-name-sans-extension
  20873. (or (and subtree-p
  20874. (org-entry-get (region-beginning)
  20875. "EXPORT_FILE_NAME" t))
  20876. (file-name-nondirectory buffer-file-name)))
  20877. "." org-export-html-extension)))
  20878. (current-dir (if buffer-file-name
  20879. (file-name-directory buffer-file-name)
  20880. default-directory))
  20881. (buffer (if to-buffer
  20882. (cond
  20883. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  20884. (t (get-buffer-create to-buffer)))
  20885. (find-file-noselect filename)))
  20886. (org-levels-open (make-vector org-level-max nil))
  20887. (date (plist-get opt-plist :date))
  20888. (author (plist-get opt-plist :author))
  20889. (title (or (and subtree-p (org-export-get-title-from-subtree))
  20890. (plist-get opt-plist :title)
  20891. (and (not
  20892. (plist-get opt-plist :skip-before-1st-heading))
  20893. (org-export-grab-title-from-buffer))
  20894. (and buffer-file-name
  20895. (file-name-sans-extension
  20896. (file-name-nondirectory buffer-file-name)))
  20897. "UNTITLED"))
  20898. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  20899. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  20900. (inquote nil)
  20901. (infixed nil)
  20902. (in-local-list nil)
  20903. (local-list-num nil)
  20904. (local-list-indent nil)
  20905. (llt org-plain-list-ordered-item-terminator)
  20906. (email (plist-get opt-plist :email))
  20907. (language (plist-get opt-plist :language))
  20908. (lang-words nil)
  20909. (target-alist nil) tg
  20910. (head-count 0) cnt
  20911. (start 0)
  20912. (coding-system (and (boundp 'buffer-file-coding-system)
  20913. buffer-file-coding-system))
  20914. (coding-system-for-write (or org-export-html-coding-system
  20915. coding-system))
  20916. (save-buffer-coding-system (or org-export-html-coding-system
  20917. coding-system))
  20918. (charset (and coding-system-for-write
  20919. (fboundp 'coding-system-get)
  20920. (coding-system-get coding-system-for-write
  20921. 'mime-charset)))
  20922. (region
  20923. (buffer-substring
  20924. (if region-p (region-beginning) (point-min))
  20925. (if region-p (region-end) (point-max))))
  20926. (lines
  20927. (org-split-string
  20928. (org-cleaned-string-for-export
  20929. region
  20930. :emph-multiline t
  20931. :for-html t
  20932. :skip-before-1st-heading
  20933. (plist-get opt-plist :skip-before-1st-heading)
  20934. :archived-trees
  20935. (plist-get opt-plist :archived-trees)
  20936. :add-text
  20937. (plist-get opt-plist :text)
  20938. :LaTeX-fragments
  20939. (plist-get opt-plist :LaTeX-fragments))
  20940. "[\r\n]"))
  20941. table-open type
  20942. table-buffer table-orig-buffer
  20943. ind start-is-num starter didclose
  20944. rpl path desc descp desc1 desc2 link
  20945. )
  20946. (let ((inhibit-read-only t))
  20947. (org-unmodified
  20948. (remove-text-properties (point-min) (point-max)
  20949. '(:org-license-to-kill t))))
  20950. (message "Exporting...")
  20951. (setq org-min-level (org-get-min-level lines))
  20952. (setq org-last-level org-min-level)
  20953. (org-init-section-numbers)
  20954. (cond
  20955. ((and date (string-match "%" date))
  20956. (setq date (format-time-string date (current-time))))
  20957. (date)
  20958. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  20959. ;; Get the language-dependent settings
  20960. (setq lang-words (or (assoc language org-export-language-setup)
  20961. (assoc "en" org-export-language-setup)))
  20962. ;; Switch to the output buffer
  20963. (set-buffer buffer)
  20964. (erase-buffer)
  20965. (fundamental-mode)
  20966. (and (fboundp 'set-buffer-file-coding-system)
  20967. (set-buffer-file-coding-system coding-system-for-write))
  20968. (let ((case-fold-search nil)
  20969. (org-odd-levels-only odd))
  20970. ;; create local variables for all options, to make sure all called
  20971. ;; functions get the correct information
  20972. (mapcar (lambda (x)
  20973. (set (make-local-variable (cdr x))
  20974. (plist-get opt-plist (car x))))
  20975. org-export-plist-vars)
  20976. (setq umax (if arg (prefix-numeric-value arg)
  20977. org-export-headline-levels))
  20978. (setq umax-toc (if (integerp org-export-with-toc)
  20979. (min org-export-with-toc umax)
  20980. umax))
  20981. (unless body-only
  20982. ;; File header
  20983. (insert (format
  20984. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  20985. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  20986. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  20987. lang=\"%s\" xml:lang=\"%s\">
  20988. <head>
  20989. <title>%s</title>
  20990. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  20991. <meta name=\"generator\" content=\"Org-mode\"/>
  20992. <meta name=\"generated\" content=\"%s\"/>
  20993. <meta name=\"author\" content=\"%s\"/>
  20994. %s
  20995. </head><body>
  20996. "
  20997. language language (org-html-expand title)
  20998. (or charset "iso-8859-1") date author style))
  20999. (insert (or (plist-get opt-plist :preamble) ""))
  21000. (when (plist-get opt-plist :auto-preamble)
  21001. (if title (insert (format org-export-html-title-format
  21002. (org-html-expand title))))))
  21003. (if (and org-export-with-toc (not body-only))
  21004. (progn
  21005. (push (format "<h%d>%s</h%d>\n"
  21006. org-export-html-toplevel-hlevel
  21007. (nth 3 lang-words)
  21008. org-export-html-toplevel-hlevel)
  21009. thetoc)
  21010. (push "<ul>\n<li>" thetoc)
  21011. (setq lines
  21012. (mapcar '(lambda (line)
  21013. (if (string-match org-todo-line-regexp line)
  21014. ;; This is a headline
  21015. (progn
  21016. (setq have-headings t)
  21017. (setq level (- (match-end 1) (match-beginning 1))
  21018. level (org-tr-level level)
  21019. txt (save-match-data
  21020. (org-html-expand
  21021. (org-export-cleanup-toc-line
  21022. (match-string 3 line))))
  21023. todo
  21024. (or (and org-export-mark-todo-in-toc
  21025. (match-beginning 2)
  21026. (not (member (match-string 2 line)
  21027. org-done-keywords)))
  21028. ; TODO, not DONE
  21029. (and org-export-mark-todo-in-toc
  21030. (= level umax-toc)
  21031. (org-search-todo-below
  21032. line lines level))))
  21033. (if (and (memq org-export-with-tags '(not-in-toc nil))
  21034. (string-match
  21035. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  21036. txt))
  21037. (setq txt (replace-match "" t t txt)))
  21038. (if (string-match quote-re0 txt)
  21039. (setq txt (replace-match "" t t txt)))
  21040. (if org-export-with-section-numbers
  21041. (setq txt (concat (org-section-number level)
  21042. " " txt)))
  21043. (if (<= level (max umax umax-toc))
  21044. (setq head-count (+ head-count 1)))
  21045. (if (<= level umax-toc)
  21046. (progn
  21047. (if (> level org-last-level)
  21048. (progn
  21049. (setq cnt (- level org-last-level))
  21050. (while (>= (setq cnt (1- cnt)) 0)
  21051. (push "\n<ul>\n<li>" thetoc))
  21052. (push "\n" thetoc)))
  21053. (if (< level org-last-level)
  21054. (progn
  21055. (setq cnt (- org-last-level level))
  21056. (while (>= (setq cnt (1- cnt)) 0)
  21057. (push "</li>\n</ul>" thetoc))
  21058. (push "\n" thetoc)))
  21059. ;; Check for targets
  21060. (while (string-match org-target-regexp line)
  21061. (setq tg (match-string 1 line)
  21062. line (replace-match
  21063. (concat "@<span class=\"target\">" tg "@</span> ")
  21064. t t line))
  21065. (push (cons (org-solidify-link-text tg)
  21066. (format "sec-%d" head-count))
  21067. target-alist))
  21068. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  21069. (setq txt (replace-match "" t t txt)))
  21070. (push
  21071. (format
  21072. (if todo
  21073. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  21074. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  21075. head-count txt) thetoc)
  21076. (setq org-last-level level))
  21077. )))
  21078. line)
  21079. lines))
  21080. (while (> org-last-level (1- org-min-level))
  21081. (setq org-last-level (1- org-last-level))
  21082. (push "</li>\n</ul>\n" thetoc))
  21083. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  21084. (setq head-count 0)
  21085. (org-init-section-numbers)
  21086. (while (setq line (pop lines) origline line)
  21087. (catch 'nextline
  21088. ;; end of quote section?
  21089. (when (and inquote (string-match "^\\*+ " line))
  21090. (insert "</pre>\n")
  21091. (setq inquote nil))
  21092. ;; inside a quote section?
  21093. (when inquote
  21094. (insert (org-html-protect line) "\n")
  21095. (throw 'nextline nil))
  21096. ;; verbatim lines
  21097. (when (and org-export-with-fixed-width
  21098. (string-match "^[ \t]*:\\(.*\\)" line))
  21099. (when (not infixed)
  21100. (setq infixed t)
  21101. (insert "<pre>\n"))
  21102. (insert (org-html-protect (match-string 1 line)) "\n")
  21103. (when (and lines
  21104. (not (string-match "^[ \t]*\\(:.*\\)"
  21105. (car lines))))
  21106. (setq infixed nil)
  21107. (insert "</pre>\n"))
  21108. (throw 'nextline nil))
  21109. ;; Protected HTML
  21110. (when (get-text-property 0 'org-protected line)
  21111. (let (par)
  21112. (when (re-search-backward
  21113. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  21114. (setq par (match-string 1))
  21115. (replace-match "\\2\n"))
  21116. (insert line "\n")
  21117. (while (and lines
  21118. (get-text-property 0 'org-protected (car lines)))
  21119. (insert (pop lines) "\n"))
  21120. (and par (insert "<p>\n")))
  21121. (throw 'nextline nil))
  21122. ;; Horizontal line
  21123. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  21124. (insert "\n<hr/>\n")
  21125. (throw 'nextline nil))
  21126. ;; make targets to anchors
  21127. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  21128. (cond
  21129. ((match-end 2)
  21130. (setq line (replace-match
  21131. (concat "@<a name=\""
  21132. (org-solidify-link-text (match-string 1 line))
  21133. "\">\\nbsp@</a>")
  21134. t t line)))
  21135. ((and org-export-with-toc (equal (string-to-char line) ?*))
  21136. (setq line (replace-match
  21137. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  21138. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  21139. t t line)))
  21140. (t
  21141. (setq line (replace-match
  21142. (concat "@<a name=\""
  21143. (org-solidify-link-text (match-string 1 line))
  21144. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  21145. t t line)))))
  21146. (setq line (org-html-handle-time-stamps line))
  21147. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  21148. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  21149. ;; Also handle sub_superscripts and checkboxes
  21150. (setq line (org-html-expand line))
  21151. ;; Format the links
  21152. (setq start 0)
  21153. (while (string-match org-bracket-link-analytic-regexp line start)
  21154. (setq start (match-beginning 0))
  21155. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  21156. (setq path (match-string 3 line))
  21157. (setq desc1 (if (match-end 5) (match-string 5 line))
  21158. desc2 (if (match-end 2) (concat type ":" path) path)
  21159. descp (and desc1 (not (equal desc1 desc2)))
  21160. desc (or desc1 desc2))
  21161. ;; Make an image out of the description if that is so wanted
  21162. (when (and descp (org-file-image-p desc))
  21163. (save-match-data
  21164. (if (string-match "^file:" desc)
  21165. (setq desc (substring desc (match-end 0)))))
  21166. (setq desc (concat "<img src=\"" desc "\"/>")))
  21167. ;; FIXME: do we need to unescape here somewhere?
  21168. (cond
  21169. ((equal type "internal")
  21170. (setq rpl
  21171. (concat
  21172. "<a href=\"#"
  21173. (org-solidify-link-text
  21174. (save-match-data (org-link-unescape path)) target-alist)
  21175. "\">" desc "</a>")))
  21176. ((member type '("http" "https"))
  21177. ;; standard URL, just check if we need to inline an image
  21178. (if (and (or (eq t org-export-html-inline-images)
  21179. (and org-export-html-inline-images (not descp)))
  21180. (org-file-image-p path))
  21181. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  21182. (setq link (concat type ":" path))
  21183. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  21184. ((member type '("ftp" "mailto" "news"))
  21185. ;; standard URL
  21186. (setq link (concat type ":" path))
  21187. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  21188. ((string= type "file")
  21189. ;; FILE link
  21190. (let* ((filename path)
  21191. (abs-p (file-name-absolute-p filename))
  21192. thefile file-is-image-p search)
  21193. (save-match-data
  21194. (if (string-match "::\\(.*\\)" filename)
  21195. (setq search (match-string 1 filename)
  21196. filename (replace-match "" t nil filename)))
  21197. (setq valid
  21198. (if (functionp link-validate)
  21199. (funcall link-validate filename current-dir)
  21200. t))
  21201. (setq file-is-image-p (org-file-image-p filename))
  21202. (setq thefile (if abs-p (expand-file-name filename) filename))
  21203. (when (and org-export-html-link-org-files-as-html
  21204. (string-match "\\.org$" thefile))
  21205. (setq thefile (concat (substring thefile 0
  21206. (match-beginning 0))
  21207. "." org-export-html-extension))
  21208. (if (and search
  21209. ;; make sure this is can be used as target search
  21210. (not (string-match "^[0-9]*$" search))
  21211. (not (string-match "^\\*" search))
  21212. (not (string-match "^/.*/$" search)))
  21213. (setq thefile (concat thefile "#"
  21214. (org-solidify-link-text
  21215. (org-link-unescape search)))))
  21216. (when (string-match "^file:" desc)
  21217. (setq desc (replace-match "" t t desc))
  21218. (if (string-match "\\.org$" desc)
  21219. (setq desc (replace-match "" t t desc))))))
  21220. (setq rpl (if (and file-is-image-p
  21221. (or (eq t org-export-html-inline-images)
  21222. (and org-export-html-inline-images
  21223. (not descp))))
  21224. (concat "<img src=\"" thefile "\"/>")
  21225. (concat "<a href=\"" thefile "\">" desc "</a>")))
  21226. (if (not valid) (setq rpl desc))))
  21227. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  21228. (setq rpl (concat "<i>&lt;" type ":"
  21229. (save-match-data (org-link-unescape path))
  21230. "&gt;</i>"))))
  21231. (setq line (replace-match rpl t t line)
  21232. start (+ start (length rpl))))
  21233. ;; TODO items
  21234. (if (and (string-match org-todo-line-regexp line)
  21235. (match-beginning 2))
  21236. (setq line
  21237. (concat (substring line 0 (match-beginning 2))
  21238. "<span class=\""
  21239. (if (member (match-string 2 line)
  21240. org-done-keywords)
  21241. "done" "todo")
  21242. "\">" (match-string 2 line)
  21243. "</span>" (substring line (match-end 2)))))
  21244. ;; Does this contain a reference to a footnote?
  21245. (when org-export-with-footnotes
  21246. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line)
  21247. (let ((n (match-string 2 line)))
  21248. (setq line
  21249. (replace-match
  21250. (format
  21251. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  21252. (match-string 1 line) n n n)
  21253. t t line)))))
  21254. (cond
  21255. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  21256. ;; This is a headline
  21257. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  21258. txt (match-string 2 line))
  21259. (if (string-match quote-re0 txt)
  21260. (setq txt (replace-match "" t t txt)))
  21261. (if (<= level (max umax umax-toc))
  21262. (setq head-count (+ head-count 1)))
  21263. (when in-local-list
  21264. ;; Close any local lists before inserting a new header line
  21265. (while local-list-num
  21266. (org-close-li)
  21267. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  21268. (pop local-list-num))
  21269. (setq local-list-indent nil
  21270. in-local-list nil))
  21271. (setq first-heading-pos (or first-heading-pos (point)))
  21272. (org-html-level-start level txt umax
  21273. (and org-export-with-toc (<= level umax))
  21274. head-count)
  21275. ;; QUOTES
  21276. (when (string-match quote-re line)
  21277. (insert "<pre>")
  21278. (setq inquote t)))
  21279. ((and org-export-with-tables
  21280. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  21281. (if (not table-open)
  21282. ;; New table starts
  21283. (setq table-open t table-buffer nil table-orig-buffer nil))
  21284. ;; Accumulate lines
  21285. (setq table-buffer (cons line table-buffer)
  21286. table-orig-buffer (cons origline table-orig-buffer))
  21287. (when (or (not lines)
  21288. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  21289. (car lines))))
  21290. (setq table-open nil
  21291. table-buffer (nreverse table-buffer)
  21292. table-orig-buffer (nreverse table-orig-buffer))
  21293. (org-close-par-maybe)
  21294. (insert (org-format-table-html table-buffer table-orig-buffer))))
  21295. (t
  21296. ;; Normal lines
  21297. (when (string-match
  21298. (cond
  21299. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  21300. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  21301. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  21302. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  21303. line)
  21304. (setq ind (org-get-string-indentation line)
  21305. start-is-num (match-beginning 4)
  21306. starter (if (match-beginning 2)
  21307. (substring (match-string 2 line) 0 -1))
  21308. line (substring line (match-beginning 5)))
  21309. (unless (string-match "[^ \t]" line)
  21310. ;; empty line. Pretend indentation is large.
  21311. (setq ind (if org-empty-line-terminates-plain-lists
  21312. 0
  21313. (1+ (or (car local-list-indent) 1)))))
  21314. (setq didclose nil)
  21315. (while (and in-local-list
  21316. (or (and (= ind (car local-list-indent))
  21317. (not starter))
  21318. (< ind (car local-list-indent))))
  21319. (setq didclose t)
  21320. (org-close-li)
  21321. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  21322. (pop local-list-num) (pop local-list-indent)
  21323. (setq in-local-list local-list-indent))
  21324. (cond
  21325. ((and starter
  21326. (or (not in-local-list)
  21327. (> ind (car local-list-indent))))
  21328. ;; Start new (level of) list
  21329. (org-close-par-maybe)
  21330. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  21331. (push start-is-num local-list-num)
  21332. (push ind local-list-indent)
  21333. (setq in-local-list t))
  21334. (starter
  21335. ;; continue current list
  21336. (org-close-li)
  21337. (insert "<li>\n"))
  21338. (didclose
  21339. ;; we did close a list, normal text follows: need <p>
  21340. (org-open-par)))
  21341. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  21342. (setq line
  21343. (replace-match
  21344. (if (equal (match-string 1 line) "X")
  21345. "<b>[X]</b>"
  21346. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  21347. t t line))))
  21348. ;; Empty lines start a new paragraph. If hand-formatted lists
  21349. ;; are not fully interpreted, lines starting with "-", "+", "*"
  21350. ;; also start a new paragraph.
  21351. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  21352. ;; Is this the start of a footnote?
  21353. (when org-export-with-footnotes
  21354. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  21355. (org-close-par-maybe)
  21356. (let ((n (match-string 1 line)))
  21357. (setq line (replace-match
  21358. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  21359. ;; Check if the line break needs to be conserved
  21360. (cond
  21361. ((string-match "\\\\\\\\[ \t]*$" line)
  21362. (setq line (replace-match "<br/>" t t line)))
  21363. (org-export-preserve-breaks
  21364. (setq line (concat line "<br/>"))))
  21365. (insert line "\n")))))
  21366. ;; Properly close all local lists and other lists
  21367. (when inquote (insert "</pre>\n"))
  21368. (when in-local-list
  21369. ;; Close any local lists before inserting a new header line
  21370. (while local-list-num
  21371. (org-close-li)
  21372. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  21373. (pop local-list-num))
  21374. (setq local-list-indent nil
  21375. in-local-list nil))
  21376. (org-html-level-start 1 nil umax
  21377. (and org-export-with-toc (<= level umax))
  21378. head-count)
  21379. (unless body-only
  21380. (when (plist-get opt-plist :auto-postamble)
  21381. (when (and org-export-author-info author)
  21382. (insert "<p class=\"author\"> "
  21383. (nth 1 lang-words) ": " author "\n")
  21384. (when email
  21385. (insert "<a href=\"mailto:" email "\">&lt;"
  21386. email "&gt;</a>\n"))
  21387. (insert "</p>\n"))
  21388. (when (and date org-export-time-stamp-file)
  21389. (insert "<p class=\"date\"> "
  21390. (nth 2 lang-words) ": "
  21391. date "</p>\n")))
  21392. (if org-export-html-with-timestamp
  21393. (insert org-export-html-html-helper-timestamp))
  21394. (insert (or (plist-get opt-plist :postamble) ""))
  21395. (insert "</body>\n</html>\n"))
  21396. (normal-mode)
  21397. (if (eq major-mode default-major-mode) (html-mode))
  21398. ;; insert the table of contents
  21399. (goto-char (point-min))
  21400. (when thetoc
  21401. (if (or (re-search-forward
  21402. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  21403. (re-search-forward
  21404. "\\[TABLE-OF-CONTENTS\\]" nil t))
  21405. (progn
  21406. (goto-char (match-beginning 0))
  21407. (replace-match ""))
  21408. (goto-char first-heading-pos)
  21409. (when (looking-at "\\s-*</p>")
  21410. (goto-char (match-end 0))
  21411. (insert "\n")))
  21412. (mapc 'insert thetoc))
  21413. ;; remove empty paragraphs and lists
  21414. (goto-char (point-min))
  21415. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  21416. (replace-match ""))
  21417. (goto-char (point-min))
  21418. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  21419. (replace-match ""))
  21420. (or to-buffer (save-buffer))
  21421. (goto-char (point-min))
  21422. (message "Exporting... done")
  21423. (if (eq to-buffer 'string)
  21424. (prog1 (buffer-substring (point-min) (point-max))
  21425. (kill-buffer (current-buffer)))
  21426. (current-buffer)))))
  21427. (defvar org-table-colgroup-info nil)
  21428. (defun org-format-table-ascii (lines)
  21429. "Format a table for ascii export."
  21430. (if (stringp lines)
  21431. (setq lines (org-split-string lines "\n")))
  21432. (if (not (string-match "^[ \t]*|" (car lines)))
  21433. ;; Table made by table.el - test for spanning
  21434. lines
  21435. ;; A normal org table
  21436. ;; Get rid of hlines at beginning and end
  21437. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21438. (setq lines (nreverse lines))
  21439. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21440. (setq lines (nreverse lines))
  21441. (when org-export-table-remove-special-lines
  21442. ;; Check if the table has a marking column. If yes remove the
  21443. ;; column and the special lines
  21444. (setq lines (org-table-clean-before-export lines)))
  21445. ;; Get rid of the vertical lines except for grouping
  21446. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  21447. rtn line vl1 start)
  21448. (while (setq line (pop lines))
  21449. (if (string-match org-table-hline-regexp line)
  21450. (and (string-match "|\\(.*\\)|" line)
  21451. (setq line (replace-match " \\1" t nil line)))
  21452. (setq start 0 vl1 vl)
  21453. (while (string-match "|" line start)
  21454. (setq start (match-end 0))
  21455. (or (pop vl1) (setq line (replace-match " " t t line)))))
  21456. (push line rtn))
  21457. (nreverse rtn))))
  21458. (defun org-colgroup-info-to-vline-list (info)
  21459. (let (vl new last)
  21460. (while info
  21461. (setq last new new (pop info))
  21462. (if (or (memq last '(:end :startend))
  21463. (memq new '(:start :startend)))
  21464. (push t vl)
  21465. (push nil vl)))
  21466. (setq vl (nreverse vl))
  21467. (and vl (setcar vl nil))
  21468. vl))
  21469. (defun org-format-table-html (lines olines)
  21470. "Find out which HTML converter to use and return the HTML code."
  21471. (if (stringp lines)
  21472. (setq lines (org-split-string lines "\n")))
  21473. (if (string-match "^[ \t]*|" (car lines))
  21474. ;; A normal org table
  21475. (org-format-org-table-html lines)
  21476. ;; Table made by table.el - test for spanning
  21477. (let* ((hlines (delq nil (mapcar
  21478. (lambda (x)
  21479. (if (string-match "^[ \t]*\\+-" x) x
  21480. nil))
  21481. lines)))
  21482. (first (car hlines))
  21483. (ll (and (string-match "\\S-+" first)
  21484. (match-string 0 first)))
  21485. (re (concat "^[ \t]*" (regexp-quote ll)))
  21486. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  21487. hlines))))
  21488. (if (and (not spanning)
  21489. (not org-export-prefer-native-exporter-for-tables))
  21490. ;; We can use my own converter with HTML conversions
  21491. (org-format-table-table-html lines)
  21492. ;; Need to use the code generator in table.el, with the original text.
  21493. (org-format-table-table-html-using-table-generate-source olines)))))
  21494. (defun org-format-org-table-html (lines &optional splice)
  21495. "Format a table into HTML."
  21496. ;; Get rid of hlines at beginning and end
  21497. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21498. (setq lines (nreverse lines))
  21499. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  21500. (setq lines (nreverse lines))
  21501. (when org-export-table-remove-special-lines
  21502. ;; Check if the table has a marking column. If yes remove the
  21503. ;; column and the special lines
  21504. (setq lines (org-table-clean-before-export lines)))
  21505. (let ((head (and org-export-highlight-first-table-line
  21506. (delq nil (mapcar
  21507. (lambda (x) (string-match "^[ \t]*|-" x))
  21508. (cdr lines)))))
  21509. (nlines 0) fnum i
  21510. tbopen line fields html gr colgropen)
  21511. (if splice (setq head nil))
  21512. (unless splice (push (if head "<thead>" "<tbody>") html))
  21513. (setq tbopen t)
  21514. (while (setq line (pop lines))
  21515. (catch 'next-line
  21516. (if (string-match "^[ \t]*|-" line)
  21517. (progn
  21518. (unless splice
  21519. (push (if head "</thead>" "</tbody>") html)
  21520. (if lines (push "<tbody>" html) (setq tbopen nil)))
  21521. (setq head nil) ;; head ends here, first time around
  21522. ;; ignore this line
  21523. (throw 'next-line t)))
  21524. ;; Break the line into fields
  21525. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  21526. (unless fnum (setq fnum (make-vector (length fields) 0)))
  21527. (setq nlines (1+ nlines) i -1)
  21528. (push (concat "<tr>"
  21529. (mapconcat
  21530. (lambda (x)
  21531. (setq i (1+ i))
  21532. (if (and (< i nlines)
  21533. (string-match org-table-number-regexp x))
  21534. (incf (aref fnum i)))
  21535. (if head
  21536. (concat (car org-export-table-header-tags) x
  21537. (cdr org-export-table-header-tags))
  21538. (concat (car org-export-table-data-tags) x
  21539. (cdr org-export-table-data-tags))))
  21540. fields "")
  21541. "</tr>")
  21542. html)))
  21543. (unless splice (if tbopen (push "</tbody>" html)))
  21544. (unless splice (push "</table>\n" html))
  21545. (setq html (nreverse html))
  21546. (unless splice
  21547. ;; Put in COL tags with the alignment (unfortuntely often ignored...)
  21548. (push (mapconcat
  21549. (lambda (x)
  21550. (setq gr (pop org-table-colgroup-info))
  21551. (format "%s<COL align=\"%s\"></COL>%s"
  21552. (if (memq gr '(:start :startend))
  21553. (prog1
  21554. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  21555. (setq colgropen t))
  21556. "")
  21557. (if (> (/ (float x) nlines) org-table-number-fraction)
  21558. "right" "left")
  21559. (if (memq gr '(:end :startend))
  21560. (progn (setq colgropen nil) "</colgroup>")
  21561. "")))
  21562. fnum "")
  21563. html)
  21564. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  21565. (push org-export-html-table-tag html))
  21566. (concat (mapconcat 'identity html "\n") "\n")))
  21567. (defun org-table-clean-before-export (lines)
  21568. "Check if the table has a marking column.
  21569. If yes remove the column and the special lines."
  21570. (setq org-table-colgroup-info nil)
  21571. (if (memq nil
  21572. (mapcar
  21573. (lambda (x) (or (string-match "^[ \t]*|-" x)
  21574. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  21575. lines))
  21576. (progn
  21577. (setq org-table-clean-did-remove-column nil)
  21578. (delq nil
  21579. (mapcar
  21580. (lambda (x)
  21581. (cond
  21582. ((string-match "^[ \t]*| */ *|" x)
  21583. (setq org-table-colgroup-info
  21584. (mapcar (lambda (x)
  21585. (cond ((member x '("<" "&lt;")) :start)
  21586. ((member x '(">" "&gt;")) :end)
  21587. ((member x '("<>" "&lt;&gt;")) :startend)
  21588. (t nil)))
  21589. (org-split-string x "[ \t]*|[ \t]*")))
  21590. nil)
  21591. (t x)))
  21592. lines)))
  21593. (setq org-table-clean-did-remove-column t)
  21594. (delq nil
  21595. (mapcar
  21596. (lambda (x)
  21597. (cond
  21598. ((string-match "^[ \t]*| */ *|" x)
  21599. (setq org-table-colgroup-info
  21600. (mapcar (lambda (x)
  21601. (cond ((member x '("<" "&lt;")) :start)
  21602. ((member x '(">" "&gt;")) :end)
  21603. ((member x '("<>" "&lt;&gt;")) :startend)
  21604. (t nil)))
  21605. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  21606. nil)
  21607. ((string-match "^[ \t]*| *[!_^/] *|" x)
  21608. nil) ; ignore this line
  21609. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  21610. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  21611. ;; remove the first column
  21612. (replace-match "\\1|" t nil x))
  21613. (t (error "This should not happen"))))
  21614. lines))))
  21615. (defun org-format-table-table-html (lines)
  21616. "Format a table generated by table.el into HTML.
  21617. This conversion does *not* use `table-generate-source' from table.el.
  21618. This has the advantage that Org-mode's HTML conversions can be used.
  21619. But it has the disadvantage, that no cell- or row-spanning is allowed."
  21620. (let (line field-buffer
  21621. (head org-export-highlight-first-table-line)
  21622. fields html empty)
  21623. (setq html (concat org-export-html-table-tag "\n"))
  21624. (while (setq line (pop lines))
  21625. (setq empty "&nbsp;")
  21626. (catch 'next-line
  21627. (if (string-match "^[ \t]*\\+-" line)
  21628. (progn
  21629. (if field-buffer
  21630. (progn
  21631. (setq
  21632. html
  21633. (concat
  21634. html
  21635. "<tr>"
  21636. (mapconcat
  21637. (lambda (x)
  21638. (if (equal x "") (setq x empty))
  21639. (if head
  21640. (concat (car org-export-table-header-tags) x
  21641. (cdr org-export-table-header-tags))
  21642. (concat (car org-export-table-data-tags) x
  21643. (cdr org-export-table-data-tags))))
  21644. field-buffer "\n")
  21645. "</tr>\n"))
  21646. (setq head nil)
  21647. (setq field-buffer nil)))
  21648. ;; Ignore this line
  21649. (throw 'next-line t)))
  21650. ;; Break the line into fields and store the fields
  21651. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  21652. (if field-buffer
  21653. (setq field-buffer (mapcar
  21654. (lambda (x)
  21655. (concat x "<br/>" (pop fields)))
  21656. field-buffer))
  21657. (setq field-buffer fields))))
  21658. (setq html (concat html "</table>\n"))
  21659. html))
  21660. (defun org-format-table-table-html-using-table-generate-source (lines)
  21661. "Format a table into html, using `table-generate-source' from table.el.
  21662. This has the advantage that cell- or row-spanning is allowed.
  21663. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  21664. (require 'table)
  21665. (with-current-buffer (get-buffer-create " org-tmp1 ")
  21666. (erase-buffer)
  21667. (insert (mapconcat 'identity lines "\n"))
  21668. (goto-char (point-min))
  21669. (if (not (re-search-forward "|[^+]" nil t))
  21670. (error "Error processing table"))
  21671. (table-recognize-table)
  21672. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  21673. (table-generate-source 'html " org-tmp2 ")
  21674. (set-buffer " org-tmp2 ")
  21675. (buffer-substring (point-min) (point-max))))
  21676. (defun org-html-handle-time-stamps (s)
  21677. "Format time stamps in string S, or remove them."
  21678. (catch 'exit
  21679. (let (r b)
  21680. (while (string-match org-maybe-keyword-time-regexp s)
  21681. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  21682. ;; never export CLOCK
  21683. (throw 'exit ""))
  21684. (or b (setq b (substring s 0 (match-beginning 0))))
  21685. (if (not org-export-with-timestamps)
  21686. (setq r (concat r (substring s 0 (match-beginning 0)))
  21687. s (substring s (match-end 0)))
  21688. (setq r (concat
  21689. r (substring s 0 (match-beginning 0))
  21690. (if (match-end 1)
  21691. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  21692. (match-string 1 s)))
  21693. (format " @<span class=\"timestamp\">%s@</span>"
  21694. (substring
  21695. (org-translate-time (match-string 3 s)) 1 -1)))
  21696. s (substring s (match-end 0)))))
  21697. ;; Line break if line started and ended with time stamp stuff
  21698. (if (not r)
  21699. s
  21700. (setq r (concat r s))
  21701. (unless (string-match "\\S-" (concat b s))
  21702. (setq r (concat r "@<br/>")))
  21703. r))))
  21704. (defun org-html-protect (s)
  21705. ;; convert & to &amp;, < to &lt; and > to &gt;
  21706. (let ((start 0))
  21707. (while (string-match "&" s start)
  21708. (setq s (replace-match "&amp;" t t s)
  21709. start (1+ (match-beginning 0))))
  21710. (while (string-match "<" s)
  21711. (setq s (replace-match "&lt;" t t s)))
  21712. (while (string-match ">" s)
  21713. (setq s (replace-match "&gt;" t t s))))
  21714. s)
  21715. (defun org-export-cleanup-toc-line (s)
  21716. "Remove tags and time staps from lines going into the toc."
  21717. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  21718. (setq s (replace-match "" t t s)))
  21719. (when org-export-remove-timestamps-from-toc
  21720. (while (string-match org-maybe-keyword-time-regexp s)
  21721. (setq s (replace-match "" t t s))))
  21722. (while (string-match org-bracket-link-regexp s)
  21723. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  21724. t t s)))
  21725. s)
  21726. (defun org-html-expand (string)
  21727. "Prepare STRING for HTML export. Applies all active conversions.
  21728. If there are links in the string, don't modify these."
  21729. (let* (m s l res)
  21730. (while (setq m (string-match org-bracket-link-regexp string))
  21731. (setq s (substring string 0 m)
  21732. l (match-string 0 string)
  21733. string (substring string (match-end 0)))
  21734. (push (org-html-do-expand s) res)
  21735. (push l res))
  21736. (push (org-html-do-expand string) res)
  21737. (apply 'concat (nreverse res))))
  21738. (defun org-html-do-expand (s)
  21739. "Apply all active conversions to translate special ASCII to HTML."
  21740. (setq s (org-html-protect s))
  21741. (if org-export-html-expand
  21742. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  21743. (setq s (replace-match "<\\1>" t nil s))))
  21744. (if org-export-with-emphasize
  21745. (setq s (org-export-html-convert-emphasize s)))
  21746. (if org-export-with-sub-superscripts
  21747. (setq s (org-export-html-convert-sub-super s)))
  21748. (if org-export-with-TeX-macros
  21749. (let ((start 0) wd ass)
  21750. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  21751. (setq wd (match-string 1 s))
  21752. (if (setq ass (assoc wd org-html-entities))
  21753. (setq s (replace-match (or (cdr ass)
  21754. (concat "&" (car ass) ";"))
  21755. t t s))
  21756. (setq start (+ start (length wd)))))))
  21757. s)
  21758. (defun org-create-multibrace-regexp (left right n)
  21759. "Create a regular expression which will match a balanced sexp.
  21760. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  21761. as single character strings.
  21762. The regexp returned will match the entire expression including the
  21763. delimiters. It will also define a single group which contains the
  21764. match except for the outermost delimiters. The maximum depth of
  21765. stacked delimiters is N. Escaping delimiters is not possible."
  21766. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  21767. (or "\\|")
  21768. (re nothing)
  21769. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  21770. (while (> n 1)
  21771. (setq n (1- n)
  21772. re (concat re or next)
  21773. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  21774. (concat left "\\(" re "\\)" right)))
  21775. (defvar org-match-substring-regexp
  21776. (concat
  21777. "\\([^\\]\\)\\([_^]\\)\\("
  21778. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  21779. "\\|"
  21780. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  21781. "\\|"
  21782. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  21783. "The regular expression matching a sub- or superscript.")
  21784. ;(let ((s "a\\_b"))
  21785. ; (and (string-match org-match-substring-regexp s)
  21786. ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
  21787. (defun org-export-html-convert-sub-super (string)
  21788. "Convert sub- and superscripts in STRING to HTML."
  21789. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  21790. (while (string-match org-match-substring-regexp string s)
  21791. (if (and requireb (match-end 8))
  21792. (setq s (match-end 2))
  21793. (setq s (match-end 1)
  21794. key (if (string= (match-string 2 string) "_") "sub" "sup")
  21795. c (or (match-string 8 string)
  21796. (match-string 6 string)
  21797. (match-string 5 string))
  21798. string (replace-match
  21799. (concat (match-string 1 string)
  21800. "<" key ">" c "</" key ">")
  21801. t t string))))
  21802. (while (string-match "\\\\\\([_^]\\)" string)
  21803. (setq string (replace-match (match-string 1 string) t t string)))
  21804. string))
  21805. (defun org-export-html-convert-emphasize (string)
  21806. "Apply emphasis."
  21807. (let ((s 0))
  21808. (while (string-match org-emph-re string s)
  21809. (if (not (equal
  21810. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  21811. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  21812. (setq string (replace-match
  21813. (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  21814. "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist))
  21815. "\\5") t nil string))
  21816. (setq s (1+ s))))
  21817. string))
  21818. (defvar org-par-open nil)
  21819. (defun org-open-par ()
  21820. "Insert <p>, but first close previous paragraph if any."
  21821. (org-close-par-maybe)
  21822. (insert "\n<p>")
  21823. (setq org-par-open t))
  21824. (defun org-close-par-maybe ()
  21825. "Close paragraph if there is one open."
  21826. (when org-par-open
  21827. (insert "</p>")
  21828. (setq org-par-open nil)))
  21829. (defun org-close-li ()
  21830. "Close <li> if necessary."
  21831. (org-close-par-maybe)
  21832. (insert "</li>\n"))
  21833. (defvar body-only) ; dynamically scoped into this.
  21834. (defun org-html-level-start (level title umax with-toc head-count)
  21835. "Insert a new level in HTML export.
  21836. When TITLE is nil, just close all open levels."
  21837. (org-close-par-maybe)
  21838. (let ((l (1+ (max level umax))))
  21839. (while (<= l org-level-max)
  21840. (if (aref org-levels-open (1- l))
  21841. (progn
  21842. (org-html-level-close l)
  21843. (aset org-levels-open (1- l) nil)))
  21844. (setq l (1+ l)))
  21845. (when title
  21846. ;; If title is nil, this means this function is called to close
  21847. ;; all levels, so the rest is done only if title is given
  21848. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  21849. (setq title (replace-match
  21850. (if org-export-with-tags
  21851. (save-match-data
  21852. (concat
  21853. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  21854. (mapconcat 'identity (org-split-string
  21855. (match-string 1 title) ":")
  21856. "&nbsp;")
  21857. "</span>"))
  21858. "")
  21859. t t title)))
  21860. (if (> level umax)
  21861. (progn
  21862. (if (aref org-levels-open (1- level))
  21863. (progn
  21864. (org-close-li)
  21865. (insert "<li>" title "<br/>\n"))
  21866. (aset org-levels-open (1- level) t)
  21867. (org-close-par-maybe)
  21868. (insert "<ul>\n<li>" title "<br/>\n")))
  21869. (if (and org-export-with-section-numbers (not body-only))
  21870. (setq title (concat (org-section-number level) " " title)))
  21871. (setq level (+ level org-export-html-toplevel-hlevel -1))
  21872. (if with-toc
  21873. (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
  21874. level head-count title level))
  21875. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  21876. (org-open-par)))))
  21877. (defun org-html-level-close (&rest args)
  21878. "Terminate one level in HTML export."
  21879. (org-close-li)
  21880. (insert "</ul>\n"))
  21881. ;;; iCalendar export
  21882. ;;;###autoload
  21883. (defun org-export-icalendar-this-file ()
  21884. "Export current file as an iCalendar file.
  21885. The iCalendar file will be located in the same directory as the Org-mode
  21886. file, but with extension `.ics'."
  21887. (interactive)
  21888. (org-export-icalendar nil buffer-file-name))
  21889. ;;;###autoload
  21890. (defun org-export-icalendar-all-agenda-files ()
  21891. "Export all files in `org-agenda-files' to iCalendar .ics files.
  21892. Each iCalendar file will be located in the same directory as the Org-mode
  21893. file, but with extension `.ics'."
  21894. (interactive)
  21895. (apply 'org-export-icalendar nil (org-agenda-files t)))
  21896. ;;;###autoload
  21897. (defun org-export-icalendar-combine-agenda-files ()
  21898. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  21899. The file is stored under the name `org-combined-agenda-icalendar-file'."
  21900. (interactive)
  21901. (apply 'org-export-icalendar t (org-agenda-files t)))
  21902. (defun org-export-icalendar (combine &rest files)
  21903. "Create iCalendar files for all elements of FILES.
  21904. If COMBINE is non-nil, combine all calendar entries into a single large
  21905. file and store it under the name `org-combined-agenda-icalendar-file'."
  21906. (save-excursion
  21907. (org-prepare-agenda-buffers files)
  21908. (let* ((dir (org-export-directory
  21909. :ical (list :publishing-directory
  21910. org-export-publishing-directory)))
  21911. file ical-file ical-buffer category started org-agenda-new-buffers)
  21912. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  21913. (when combine
  21914. (setq ical-file
  21915. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  21916. org-combined-agenda-icalendar-file
  21917. (expand-file-name org-combined-agenda-icalendar-file dir))
  21918. ical-buffer (org-get-agenda-file-buffer ical-file))
  21919. (set-buffer ical-buffer) (erase-buffer))
  21920. (while (setq file (pop files))
  21921. (catch 'nextfile
  21922. (org-check-agenda-file file)
  21923. (set-buffer (org-get-agenda-file-buffer file))
  21924. (unless combine
  21925. (setq ical-file (concat (file-name-as-directory dir)
  21926. (file-name-sans-extension
  21927. (file-name-nondirectory buffer-file-name))
  21928. ".ics"))
  21929. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  21930. (with-current-buffer ical-buffer (erase-buffer)))
  21931. (setq category (or org-category
  21932. (file-name-sans-extension
  21933. (file-name-nondirectory buffer-file-name))))
  21934. (if (symbolp category) (setq category (symbol-name category)))
  21935. (let ((standard-output ical-buffer))
  21936. (if combine
  21937. (and (not started) (setq started t)
  21938. (org-start-icalendar-file org-icalendar-combined-name))
  21939. (org-start-icalendar-file category))
  21940. (org-print-icalendar-entries combine)
  21941. (when (or (and combine (not files)) (not combine))
  21942. (org-finish-icalendar-file)
  21943. (set-buffer ical-buffer)
  21944. (save-buffer)
  21945. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  21946. (org-release-buffers org-agenda-new-buffers))))
  21947. (defvar org-after-save-iCalendar-file-hook nil
  21948. "Hook run after an iCalendar file has been saved.
  21949. The iCalendar buffer is still current when this hook is run.
  21950. A good way to use this is to tell a desktop calenndar application to re-read
  21951. the iCalendar file.")
  21952. (defun org-print-icalendar-entries (&optional combine)
  21953. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  21954. When COMBINE is non nil, add the category to each line."
  21955. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  21956. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  21957. (dts (org-ical-ts-to-string
  21958. (format-time-string (cdr org-time-stamp-formats) (current-time))
  21959. "DTSTART"))
  21960. hd ts ts2 state status (inc t) pos b sexp rrule
  21961. scheduledp deadlinep tmp pri category entry location summary desc
  21962. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  21963. (org-refresh-category-properties)
  21964. (save-excursion
  21965. (goto-char (point-min))
  21966. (while (re-search-forward re1 nil t)
  21967. (catch :skip
  21968. (org-agenda-skip)
  21969. (setq pos (match-beginning 0)
  21970. ts (match-string 0)
  21971. inc t
  21972. hd (org-get-heading)
  21973. summary (org-entry-get nil "SUMMARY")
  21974. desc (or (org-entry-get nil "DESCRIPTION")
  21975. (org-get-cleaned-entry org-icalendar-include-body))
  21976. location (org-entry-get nil "LOCATION")
  21977. category (org-get-category))
  21978. (if (looking-at re2)
  21979. (progn
  21980. (goto-char (match-end 0))
  21981. (setq ts2 (match-string 1) inc nil))
  21982. (setq tmp (buffer-substring (max (point-min)
  21983. (- pos org-ds-keyword-length))
  21984. pos)
  21985. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  21986. (progn
  21987. (setq inc nil)
  21988. (replace-match "\\1" t nil ts))
  21989. ts)
  21990. deadlinep (string-match org-deadline-regexp tmp)
  21991. scheduledp (string-match org-scheduled-regexp tmp)
  21992. ;; donep (org-entry-is-done-p)
  21993. ))
  21994. (if (or (string-match org-tr-regexp hd)
  21995. (string-match org-ts-regexp hd))
  21996. (setq hd (replace-match "" t t hd)))
  21997. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  21998. (setq rrule
  21999. (concat "\nRRULE:FREQ="
  22000. (cdr (assoc
  22001. (match-string 2 ts)
  22002. '(("d" . "DAILY")("w" . "WEEKLY")
  22003. ("m" . "MONTHLY")("y" . "YEARLY"))))
  22004. ";INTERVAL=" (match-string 1 ts)))
  22005. (setq rrule ""))
  22006. (setq summary (or summary hd))
  22007. (if (string-match org-bracket-link-regexp summary)
  22008. (setq summary
  22009. (replace-match (if (match-end 3)
  22010. (match-string 3 summary)
  22011. (match-string 1 summary))
  22012. t t summary)))
  22013. (if deadlinep (setq summary (concat "DL: " summary)))
  22014. (if scheduledp (setq summary (concat "S: " summary)))
  22015. (if (string-match "\\`<%%" ts)
  22016. (with-current-buffer sexp-buffer
  22017. (insert (substring ts 1 -1) " " summary "\n"))
  22018. (princ (format "BEGIN:VEVENT
  22019. %s
  22020. %s%s
  22021. SUMMARY:%s%s%s
  22022. CATEGORIES:%s
  22023. END:VEVENT\n"
  22024. (org-ical-ts-to-string ts "DTSTART")
  22025. (org-ical-ts-to-string ts2 "DTEND" inc)
  22026. rrule summary
  22027. (if (and desc (string-match "\\S-" desc))
  22028. (concat "\nDESCRIPTION: " desc) "")
  22029. (if (and location (string-match "\\S-" location))
  22030. (concat "\nLOCATION: " location) "")
  22031. category)))))
  22032. (when (and org-icalendar-include-sexps
  22033. (condition-case nil (require 'icalendar) (error nil))
  22034. (fboundp 'icalendar-export-region))
  22035. ;; Get all the literal sexps
  22036. (goto-char (point-min))
  22037. (while (re-search-forward "^&?%%(" nil t)
  22038. (catch :skip
  22039. (org-agenda-skip)
  22040. (setq b (match-beginning 0))
  22041. (goto-char (1- (match-end 0)))
  22042. (forward-sexp 1)
  22043. (end-of-line 1)
  22044. (setq sexp (buffer-substring b (point)))
  22045. (with-current-buffer sexp-buffer
  22046. (insert sexp "\n"))
  22047. (princ (org-diary-to-ical-string sexp-buffer)))))
  22048. (when org-icalendar-include-todo
  22049. (goto-char (point-min))
  22050. (while (re-search-forward org-todo-line-regexp nil t)
  22051. (catch :skip
  22052. (org-agenda-skip)
  22053. (setq state (match-string 2))
  22054. (setq status (if (member state org-done-keywords)
  22055. "COMPLETED" "NEEDS-ACTION"))
  22056. (when (and state
  22057. (or (not (member state org-done-keywords))
  22058. (eq org-icalendar-include-todo 'all))
  22059. (not (member org-archive-tag (org-get-tags-at)))
  22060. )
  22061. (setq hd (match-string 3)
  22062. summary (org-entry-get nil "SUMMARY")
  22063. desc (or (org-entry-get nil "DESCRIPTION")
  22064. (org-get-cleaned-entry org-icalendar-include-body))
  22065. location (org-entry-get nil "LOCATION"))
  22066. (if (string-match org-bracket-link-regexp hd)
  22067. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  22068. (match-string 1 hd))
  22069. t t hd)))
  22070. (if (string-match org-priority-regexp hd)
  22071. (setq pri (string-to-char (match-string 2 hd))
  22072. hd (concat (substring hd 0 (match-beginning 1))
  22073. (substring hd (match-end 1))))
  22074. (setq pri org-default-priority))
  22075. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  22076. (- org-lowest-priority org-highest-priority))))))
  22077. (princ (format "BEGIN:VTODO
  22078. %s
  22079. SUMMARY:%s%s%s
  22080. CATEGORIES:%s
  22081. SEQUENCE:1
  22082. PRIORITY:%d
  22083. STATUS:%s
  22084. END:VTODO\n"
  22085. dts
  22086. (or summary hd)
  22087. (if (and location (string-match "\\S-" location))
  22088. (concat "\nLOCATION: " location) "")
  22089. (if (and desc (string-match "\\S-" desc))
  22090. (concat "\nDESCRIPTION: " desc) "")
  22091. category pri status)))))))))
  22092. (defun org-get-cleaned-entry (what)
  22093. "Clean-up description string."
  22094. (when what
  22095. (save-excursion
  22096. (org-back-to-heading t)
  22097. (let ((s (buffer-substring (point-at-bol 2) (org-end-of-subtree t)))
  22098. (re (concat org-drawer-regexp "[^\000]*?:END:.*\n?"))
  22099. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  22100. (while (string-match re s) (setq s (replace-match "" t t s)))
  22101. (while (string-match re2 s) (setq s (replace-match "" t t s)))
  22102. (if (string-match "[ \t\r\n]+\\'" s) (setq s (replace-match "" t t s)))
  22103. (while (string-match "[ \t]*\n[ \t]*" s)
  22104. (setq s (replace-match "\\n" t t s)))
  22105. (setq s (org-trim s))
  22106. (if (and (numberp what)
  22107. (> (length s) what))
  22108. (substring s 0 what)
  22109. s)))))
  22110. (defun org-start-icalendar-file (name)
  22111. "Start an iCalendar file by inserting the header."
  22112. (let ((user user-full-name)
  22113. (name (or name "unknown"))
  22114. (timezone (cadr (current-time-zone))))
  22115. (princ
  22116. (format "BEGIN:VCALENDAR
  22117. VERSION:2.0
  22118. X-WR-CALNAME:%s
  22119. PRODID:-//%s//Emacs with Org-mode//EN
  22120. X-WR-TIMEZONE:%s
  22121. CALSCALE:GREGORIAN\n" name user timezone))))
  22122. (defun org-finish-icalendar-file ()
  22123. "Finish an iCalendar file by inserting the END statement."
  22124. (princ "END:VCALENDAR\n"))
  22125. (defun org-ical-ts-to-string (s keyword &optional inc)
  22126. "Take a time string S and convert it to iCalendar format.
  22127. KEYWORD is added in front, to make a complete line like DTSTART....
  22128. When INC is non-nil, increase the hour by two (if time string contains
  22129. a time), or the day by one (if it does not contain a time)."
  22130. (let ((t1 (org-parse-time-string s 'nodefault))
  22131. t2 fmt have-time time)
  22132. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  22133. (setq t2 t1 have-time t)
  22134. (setq t2 (org-parse-time-string s)))
  22135. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  22136. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  22137. (when inc
  22138. (if have-time
  22139. (if org-agenda-default-appointment-duration
  22140. (setq mi (+ org-agenda-default-appointment-duration mi))
  22141. (setq h (+ 2 h)))
  22142. (setq d (1+ d))))
  22143. (setq time (encode-time s mi h d m y)))
  22144. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  22145. (concat keyword (format-time-string fmt time))))
  22146. ;;; XOXO export
  22147. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  22148. (with-current-buffer buffer
  22149. (apply 'insert output)))
  22150. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  22151. (defun org-export-as-xoxo (&optional buffer)
  22152. "Export the org buffer as XOXO.
  22153. The XOXO buffer is named *xoxo-<source buffer name>*"
  22154. (interactive (list (current-buffer)))
  22155. ;; A quickie abstraction
  22156. ;; Output everything as XOXO
  22157. (with-current-buffer (get-buffer buffer)
  22158. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  22159. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22160. (org-infile-export-plist)))
  22161. (filename (concat (file-name-as-directory
  22162. (org-export-directory :xoxo opt-plist))
  22163. (file-name-sans-extension
  22164. (file-name-nondirectory buffer-file-name))
  22165. ".html"))
  22166. (out (find-file-noselect filename))
  22167. (last-level 1)
  22168. (hanging-li nil))
  22169. ;; Check the output buffer is empty.
  22170. (with-current-buffer out (erase-buffer))
  22171. ;; Kick off the output
  22172. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  22173. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  22174. (let* ((hd (match-string-no-properties 1))
  22175. (level (length hd))
  22176. (text (concat
  22177. (match-string-no-properties 2)
  22178. (save-excursion
  22179. (goto-char (match-end 0))
  22180. (let ((str ""))
  22181. (catch 'loop
  22182. (while 't
  22183. (forward-line)
  22184. (if (looking-at "^[ \t]\\(.*\\)")
  22185. (setq str (concat str (match-string-no-properties 1)))
  22186. (throw 'loop str)))))))))
  22187. ;; Handle level rendering
  22188. (cond
  22189. ((> level last-level)
  22190. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  22191. ((< level last-level)
  22192. (dotimes (- (- last-level level) 1)
  22193. (if hanging-li
  22194. (org-export-as-xoxo-insert-into out "</li>\n"))
  22195. (org-export-as-xoxo-insert-into out "</ol>\n"))
  22196. (when hanging-li
  22197. (org-export-as-xoxo-insert-into out "</li>\n")
  22198. (setq hanging-li nil)))
  22199. ((equal level last-level)
  22200. (if hanging-li
  22201. (org-export-as-xoxo-insert-into out "</li>\n")))
  22202. )
  22203. (setq last-level level)
  22204. ;; And output the new li
  22205. (setq hanging-li 't)
  22206. (if (equal ?+ (elt text 0))
  22207. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  22208. (org-export-as-xoxo-insert-into out "<li>" text))))
  22209. ;; Finally finish off the ol
  22210. (dotimes (- last-level 1)
  22211. (if hanging-li
  22212. (org-export-as-xoxo-insert-into out "</li>\n"))
  22213. (org-export-as-xoxo-insert-into out "</ol>\n"))
  22214. ;; Finish the buffer off and clean it up.
  22215. (switch-to-buffer-other-window out)
  22216. (indent-region (point-min) (point-max) nil)
  22217. (save-buffer)
  22218. (goto-char (point-min))
  22219. )))
  22220. ;;;; Key bindings
  22221. ;; Make `C-c C-x' a prefix key
  22222. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  22223. ;; TAB key with modifiers
  22224. (org-defkey org-mode-map "\C-i" 'org-cycle)
  22225. (org-defkey org-mode-map [(tab)] 'org-cycle)
  22226. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  22227. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  22228. (org-defkey org-mode-map "\M-\t" 'org-complete)
  22229. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  22230. ;; The following line is necessary under Suse GNU/Linux
  22231. (unless (featurep 'xemacs)
  22232. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  22233. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  22234. (define-key org-mode-map (kbd "<backtab>") 'org-shifttab)
  22235. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  22236. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  22237. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  22238. ;; Cursor keys with modifiers
  22239. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  22240. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  22241. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  22242. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  22243. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  22244. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  22245. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  22246. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  22247. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  22248. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  22249. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  22250. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  22251. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  22252. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  22253. ;;; Extra keys for tty access.
  22254. ;; We only set them when really needed because otherwise the
  22255. ;; menus don't show the simple keys
  22256. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  22257. (not window-system))
  22258. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  22259. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  22260. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  22261. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  22262. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  22263. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  22264. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  22265. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  22266. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  22267. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  22268. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  22269. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  22270. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  22271. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  22272. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  22273. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  22274. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  22275. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  22276. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  22277. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  22278. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  22279. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  22280. ;; All the other keys
  22281. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  22282. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  22283. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  22284. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  22285. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  22286. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  22287. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  22288. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  22289. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  22290. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  22291. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  22292. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  22293. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  22294. (org-defkey org-mode-map "\C-c\C-w" 'org-check-deadlines)
  22295. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  22296. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  22297. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  22298. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  22299. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  22300. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  22301. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  22302. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  22303. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  22304. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  22305. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  22306. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  22307. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  22308. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  22309. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  22310. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  22311. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  22312. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  22313. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  22314. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  22315. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  22316. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  22317. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  22318. (org-defkey org-mode-map "\C-c^" 'org-sort)
  22319. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  22320. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  22321. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  22322. (org-defkey org-mode-map "\C-m" 'org-return)
  22323. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  22324. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  22325. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  22326. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  22327. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  22328. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  22329. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  22330. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  22331. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  22332. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  22333. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  22334. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  22335. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  22336. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  22337. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  22338. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  22339. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  22340. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  22341. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  22342. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  22343. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  22344. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  22345. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  22346. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  22347. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  22348. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  22349. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  22350. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  22351. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  22352. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  22353. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  22354. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  22355. (when (featurep 'xemacs)
  22356. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  22357. (defsubst org-table-p () (org-at-table-p))
  22358. (defun org-self-insert-command (N)
  22359. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  22360. If the cursor is in a table looking at whitespace, the whitespace is
  22361. overwritten, and the table is not marked as requiring realignment."
  22362. (interactive "p")
  22363. (if (and (org-table-p)
  22364. (progn
  22365. ;; check if we blank the field, and if that triggers align
  22366. (and org-table-auto-blank-field
  22367. (member last-command
  22368. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  22369. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  22370. ;; got extra space, this field does not determine column width
  22371. (let (org-table-may-need-update) (org-table-blank-field))
  22372. ;; no extra space, this field may determine column width
  22373. (org-table-blank-field)))
  22374. t)
  22375. (eq N 1)
  22376. (looking-at "[^|\n]* |"))
  22377. (let (org-table-may-need-update)
  22378. (goto-char (1- (match-end 0)))
  22379. (delete-backward-char 1)
  22380. (goto-char (match-beginning 0))
  22381. (self-insert-command N))
  22382. (setq org-table-may-need-update t)
  22383. (self-insert-command N)
  22384. (org-fix-tags-on-the-fly)))
  22385. (defun org-fix-tags-on-the-fly ()
  22386. (when (and (equal (char-after (point-at-bol)) ?*)
  22387. (org-on-heading-p))
  22388. (org-align-tags-here org-tags-column)))
  22389. (defun org-delete-backward-char (N)
  22390. "Like `delete-backward-char', insert whitespace at field end in tables.
  22391. When deleting backwards, in tables this function will insert whitespace in
  22392. front of the next \"|\" separator, to keep the table aligned. The table will
  22393. still be marked for re-alignment if the field did fill the entire column,
  22394. because, in this case the deletion might narrow the column."
  22395. (interactive "p")
  22396. (if (and (org-table-p)
  22397. (eq N 1)
  22398. (string-match "|" (buffer-substring (point-at-bol) (point)))
  22399. (looking-at ".*?|"))
  22400. (let ((pos (point))
  22401. (noalign (looking-at "[^|\n\r]* |"))
  22402. (c org-table-may-need-update))
  22403. (backward-delete-char N)
  22404. (skip-chars-forward "^|")
  22405. (insert " ")
  22406. (goto-char (1- pos))
  22407. ;; noalign: if there were two spaces at the end, this field
  22408. ;; does not determine the width of the column.
  22409. (if noalign (setq org-table-may-need-update c)))
  22410. (backward-delete-char N)
  22411. (org-fix-tags-on-the-fly)))
  22412. (defun org-delete-char (N)
  22413. "Like `delete-char', but insert whitespace at field end in tables.
  22414. When deleting characters, in tables this function will insert whitespace in
  22415. front of the next \"|\" separator, to keep the table aligned. The table will
  22416. still be marked for re-alignment if the field did fill the entire column,
  22417. because, in this case the deletion might narrow the column."
  22418. (interactive "p")
  22419. (if (and (org-table-p)
  22420. (not (bolp))
  22421. (not (= (char-after) ?|))
  22422. (eq N 1))
  22423. (if (looking-at ".*?|")
  22424. (let ((pos (point))
  22425. (noalign (looking-at "[^|\n\r]* |"))
  22426. (c org-table-may-need-update))
  22427. (replace-match (concat
  22428. (substring (match-string 0) 1 -1)
  22429. " |"))
  22430. (goto-char pos)
  22431. ;; noalign: if there were two spaces at the end, this field
  22432. ;; does not determine the width of the column.
  22433. (if noalign (setq org-table-may-need-update c)))
  22434. (delete-char N))
  22435. (delete-char N)
  22436. (org-fix-tags-on-the-fly)))
  22437. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  22438. (put 'org-self-insert-command 'delete-selection t)
  22439. (put 'orgtbl-self-insert-command 'delete-selection t)
  22440. (put 'org-delete-char 'delete-selection 'supersede)
  22441. (put 'org-delete-backward-char 'delete-selection 'supersede)
  22442. ;; Make `flyspell-mode' delay after some commands
  22443. (put 'org-self-insert-command 'flyspell-delayed t)
  22444. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  22445. (put 'org-delete-char 'flyspell-delayed t)
  22446. (put 'org-delete-backward-char 'flyspell-delayed t)
  22447. (eval-after-load "pabbrev"
  22448. '(progn
  22449. (add-to-list 'pabbrev-expand-after-command-list
  22450. 'orgtbl-self-insert-command t)
  22451. (add-to-list 'pabbrev-expand-after-command-list
  22452. 'org-self-insert-command t)))
  22453. ;; How to do this: Measure non-white length of current string
  22454. ;; If equal to column width, we should realign.
  22455. (defun org-remap (map &rest commands)
  22456. "In MAP, remap the functions given in COMMANDS.
  22457. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  22458. (let (new old)
  22459. (while commands
  22460. (setq old (pop commands) new (pop commands))
  22461. (if (fboundp 'command-remapping)
  22462. (org-defkey map (vector 'remap old) new)
  22463. (substitute-key-definition old new map global-map)))))
  22464. (when (eq org-enable-table-editor 'optimized)
  22465. ;; If the user wants maximum table support, we need to hijack
  22466. ;; some standard editing functions
  22467. (org-remap org-mode-map
  22468. 'self-insert-command 'org-self-insert-command
  22469. 'delete-char 'org-delete-char
  22470. 'delete-backward-char 'org-delete-backward-char)
  22471. (org-defkey org-mode-map "|" 'org-force-self-insert))
  22472. (defun org-shiftcursor-error ()
  22473. "Throw an error because Shift-Cursor command was applied in wrong context."
  22474. (error "This command is active in special context like tables, headlines or timestamps"))
  22475. (defun org-shifttab (&optional arg)
  22476. "Global visibility cycling or move to previous table field.
  22477. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  22478. on context.
  22479. See the individual commands for more information."
  22480. (interactive "P")
  22481. (cond
  22482. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  22483. (arg (message "Content view to level: ")
  22484. (org-content (prefix-numeric-value arg))
  22485. (setq org-cycle-global-status 'overview))
  22486. (t (call-interactively 'org-global-cycle))))
  22487. (defun org-shiftmetaleft ()
  22488. "Promote subtree or delete table column.
  22489. Calls `org-promote-subtree', `org-outdent-item',
  22490. or `org-table-delete-column', depending on context.
  22491. See the individual commands for more information."
  22492. (interactive)
  22493. (cond
  22494. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  22495. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  22496. ((org-at-item-p) (call-interactively 'org-outdent-item))
  22497. (t (org-shiftcursor-error))))
  22498. (defun org-shiftmetaright ()
  22499. "Demote subtree or insert table column.
  22500. Calls `org-demote-subtree', `org-indent-item',
  22501. or `org-table-insert-column', depending on context.
  22502. See the individual commands for more information."
  22503. (interactive)
  22504. (cond
  22505. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  22506. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  22507. ((org-at-item-p) (call-interactively 'org-indent-item))
  22508. (t (org-shiftcursor-error))))
  22509. (defun org-shiftmetaup (&optional arg)
  22510. "Move subtree up or kill table row.
  22511. Calls `org-move-subtree-up' or `org-table-kill-row' or
  22512. `org-move-item-up' depending on context. See the individual commands
  22513. for more information."
  22514. (interactive "P")
  22515. (cond
  22516. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  22517. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  22518. ((org-at-item-p) (call-interactively 'org-move-item-up))
  22519. (t (org-shiftcursor-error))))
  22520. (defun org-shiftmetadown (&optional arg)
  22521. "Move subtree down or insert table row.
  22522. Calls `org-move-subtree-down' or `org-table-insert-row' or
  22523. `org-move-item-down', depending on context. See the individual
  22524. commands for more information."
  22525. (interactive "P")
  22526. (cond
  22527. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  22528. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  22529. ((org-at-item-p) (call-interactively 'org-move-item-down))
  22530. (t (org-shiftcursor-error))))
  22531. (defun org-metaleft (&optional arg)
  22532. "Promote heading or move table column to left.
  22533. Calls `org-do-promote' or `org-table-move-column', depending on context.
  22534. With no specific context, calls the Emacs default `backward-word'.
  22535. See the individual commands for more information."
  22536. (interactive "P")
  22537. (cond
  22538. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  22539. ((or (org-on-heading-p) (org-region-active-p))
  22540. (call-interactively 'org-do-promote))
  22541. ((org-at-item-p) (call-interactively 'org-outdent-item))
  22542. (t (call-interactively 'backward-word))))
  22543. (defun org-metaright (&optional arg)
  22544. "Demote subtree or move table column to right.
  22545. Calls `org-do-demote' or `org-table-move-column', depending on context.
  22546. With no specific context, calls the Emacs default `forward-word'.
  22547. See the individual commands for more information."
  22548. (interactive "P")
  22549. (cond
  22550. ((org-at-table-p) (call-interactively 'org-table-move-column))
  22551. ((or (org-on-heading-p) (org-region-active-p))
  22552. (call-interactively 'org-do-demote))
  22553. ((org-at-item-p) (call-interactively 'org-indent-item))
  22554. (t (call-interactively 'forward-word))))
  22555. (defun org-metaup (&optional arg)
  22556. "Move subtree up or move table row up.
  22557. Calls `org-move-subtree-up' or `org-table-move-row' or
  22558. `org-move-item-up', depending on context. See the individual commands
  22559. for more information."
  22560. (interactive "P")
  22561. (cond
  22562. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  22563. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  22564. ((org-at-item-p) (call-interactively 'org-move-item-up))
  22565. (t (org-shiftcursor-error))))
  22566. (defun org-metadown (&optional arg)
  22567. "Move subtree down or move table row down.
  22568. Calls `org-move-subtree-down' or `org-table-move-row' or
  22569. `org-move-item-down', depending on context. See the individual
  22570. commands for more information."
  22571. (interactive "P")
  22572. (cond
  22573. ((org-at-table-p) (call-interactively 'org-table-move-row))
  22574. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  22575. ((org-at-item-p) (call-interactively 'org-move-item-down))
  22576. (t (org-shiftcursor-error))))
  22577. (defun org-shiftup (&optional arg)
  22578. "Increase item in timestamp or increase priority of current headline.
  22579. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  22580. depending on context. See the individual commands for more information."
  22581. (interactive "P")
  22582. (cond
  22583. ((org-at-timestamp-p t)
  22584. (call-interactively (if org-edit-timestamp-down-means-later
  22585. 'org-timestamp-down 'org-timestamp-up)))
  22586. ((org-on-heading-p) (call-interactively 'org-priority-up))
  22587. ((org-at-item-p) (call-interactively 'org-previous-item))
  22588. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  22589. (defun org-shiftdown (&optional arg)
  22590. "Decrease item in timestamp or decrease priority of current headline.
  22591. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  22592. depending on context. See the individual commands for more information."
  22593. (interactive "P")
  22594. (cond
  22595. ((org-at-timestamp-p t)
  22596. (call-interactively (if org-edit-timestamp-down-means-later
  22597. 'org-timestamp-up 'org-timestamp-down)))
  22598. ((org-on-heading-p) (call-interactively 'org-priority-down))
  22599. (t (call-interactively 'org-next-item))))
  22600. (defun org-shiftright ()
  22601. "Next TODO keyword or timestamp one day later, depending on context."
  22602. (interactive)
  22603. (cond
  22604. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  22605. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  22606. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  22607. (t (org-shiftcursor-error))))
  22608. (defun org-shiftleft ()
  22609. "Previous TODO keyword or timestamp one day earlier, depending on context."
  22610. (interactive)
  22611. (cond
  22612. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  22613. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  22614. ((org-at-property-p)
  22615. (call-interactively 'org-property-previous-allowed-value))
  22616. (t (org-shiftcursor-error))))
  22617. (defun org-shiftcontrolright ()
  22618. "Switch to next TODO set."
  22619. (interactive)
  22620. (cond
  22621. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  22622. (t (org-shiftcursor-error))))
  22623. (defun org-shiftcontrolleft ()
  22624. "Switch to previous TODO set."
  22625. (interactive)
  22626. (cond
  22627. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  22628. (t (org-shiftcursor-error))))
  22629. (defun org-ctrl-c-ret ()
  22630. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  22631. (interactive)
  22632. (cond
  22633. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  22634. (t (call-interactively 'org-insert-heading))))
  22635. (defun org-copy-special ()
  22636. "Copy region in table or copy current subtree.
  22637. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  22638. See the individual commands for more information."
  22639. (interactive)
  22640. (call-interactively
  22641. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  22642. (defun org-cut-special ()
  22643. "Cut region in table or cut current subtree.
  22644. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  22645. See the individual commands for more information."
  22646. (interactive)
  22647. (call-interactively
  22648. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  22649. (defun org-paste-special (arg)
  22650. "Paste rectangular region into table, or past subtree relative to level.
  22651. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  22652. See the individual commands for more information."
  22653. (interactive "P")
  22654. (if (org-at-table-p)
  22655. (org-table-paste-rectangle)
  22656. (org-paste-subtree arg)))
  22657. (defun org-ctrl-c-ctrl-c (&optional arg)
  22658. "Set tags in headline, or update according to changed information at point.
  22659. This command does many different things, depending on context:
  22660. - If the cursor is in a headline, prompt for tags and insert them
  22661. into the current line, aligned to `org-tags-column'. When called
  22662. with prefix arg, realign all tags in the current buffer.
  22663. - If the cursor is in one of the special #+KEYWORD lines, this
  22664. triggers scanning the buffer for these lines and updating the
  22665. information.
  22666. - If the cursor is inside a table, realign the table. This command
  22667. works even if the automatic table editor has been turned off.
  22668. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  22669. the entire table.
  22670. - If the cursor is a the beginning of a dynamic block, update it.
  22671. - If the cursor is inside a table created by the table.el package,
  22672. activate that table.
  22673. - If the current buffer is a remember buffer, close note and file it.
  22674. with a prefix argument, file it without further interaction to the default
  22675. location.
  22676. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  22677. links in this buffer.
  22678. - If the cursor is on a numbered item in a plain list, renumber the
  22679. ordered list."
  22680. (interactive "P")
  22681. (let ((org-enable-table-editor t))
  22682. (cond
  22683. ((or org-clock-overlays
  22684. org-occur-highlights
  22685. org-latex-fragment-image-overlays)
  22686. (org-remove-clock-overlays)
  22687. (org-remove-occur-highlights)
  22688. (org-remove-latex-fragment-image-overlays)
  22689. (message "Temporary highlights/overlays removed from current buffer"))
  22690. ((and (local-variable-p 'org-finish-function (current-buffer))
  22691. (fboundp org-finish-function))
  22692. (funcall org-finish-function))
  22693. ((org-at-property-p)
  22694. (call-interactively 'org-property-action))
  22695. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  22696. ((org-on-heading-p) (call-interactively 'org-set-tags))
  22697. ((org-at-table.el-p)
  22698. (require 'table)
  22699. (beginning-of-line 1)
  22700. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  22701. (call-interactively 'table-recognize-table))
  22702. ((org-at-table-p)
  22703. (org-table-maybe-eval-formula)
  22704. (if arg
  22705. (call-interactively 'org-table-recalculate)
  22706. (org-table-maybe-recalculate-line))
  22707. (call-interactively 'org-table-align))
  22708. ((org-at-item-checkbox-p)
  22709. (call-interactively 'org-toggle-checkbox))
  22710. ((org-at-item-p)
  22711. (call-interactively 'org-maybe-renumber-ordered-list))
  22712. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  22713. ;; Dynamic block
  22714. (beginning-of-line 1)
  22715. (org-update-dblock))
  22716. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  22717. (cond
  22718. ((equal (match-string 1) "TBLFM")
  22719. ;; Recalculate the table before this line
  22720. (save-excursion
  22721. (beginning-of-line 1)
  22722. (skip-chars-backward " \r\n\t")
  22723. (if (org-at-table-p)
  22724. (org-call-with-arg 'org-table-recalculate t))))
  22725. (t
  22726. (call-interactively 'org-mode-restart))))
  22727. (t (error "C-c C-c can do nothing useful at this location.")))))
  22728. (defun org-mode-restart ()
  22729. "Restart Org-mode, to scan again for special lines.
  22730. Also updates the keyword regular expressions."
  22731. (interactive)
  22732. (let ((org-inhibit-startup t)) (org-mode))
  22733. (message "Org-mode restarted to refresh keyword and special line setup"))
  22734. (defun org-kill-note-or-show-branches ()
  22735. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  22736. (interactive)
  22737. (if (not org-finish-function)
  22738. (call-interactively 'show-branches)
  22739. (let ((org-note-abort t))
  22740. (funcall org-finish-function))))
  22741. (defun org-return ()
  22742. "Goto next table row or insert a newline.
  22743. Calls `org-table-next-row' or `newline', depending on context.
  22744. See the individual commands for more information."
  22745. (interactive)
  22746. (cond
  22747. ((bobp) (newline))
  22748. ((org-at-table-p)
  22749. (org-table-justify-field-maybe)
  22750. (call-interactively 'org-table-next-row))
  22751. (t (newline))))
  22752. (defun org-ctrl-c-minus ()
  22753. "Insert separator line in table or modify bullet type in list.
  22754. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  22755. depending on context."
  22756. (interactive)
  22757. (cond
  22758. ((org-at-table-p)
  22759. (call-interactively 'org-table-insert-hline))
  22760. ((org-in-item-p)
  22761. (call-interactively 'org-cycle-list-bullet))
  22762. (t (error "`C-c -' does have no function here."))))
  22763. (defun org-meta-return (&optional arg)
  22764. "Insert a new heading or wrap a region in a table.
  22765. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  22766. See the individual commands for more information."
  22767. (interactive "P")
  22768. (cond
  22769. ((org-at-table-p)
  22770. (call-interactively 'org-table-wrap-region))
  22771. (t (call-interactively 'org-insert-heading))))
  22772. ;;; Menu entries
  22773. ;; Define the Org-mode menus
  22774. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  22775. '("Tbl"
  22776. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  22777. ["Next Field" org-cycle (org-at-table-p)]
  22778. ["Previous Field" org-shifttab (org-at-table-p)]
  22779. ["Next Row" org-return (org-at-table-p)]
  22780. "--"
  22781. ["Blank Field" org-table-blank-field (org-at-table-p)]
  22782. ["Edit Field" org-table-edit-field (org-at-table-p)]
  22783. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  22784. "--"
  22785. ("Column"
  22786. ["Move Column Left" org-metaleft (org-at-table-p)]
  22787. ["Move Column Right" org-metaright (org-at-table-p)]
  22788. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  22789. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  22790. ("Row"
  22791. ["Move Row Up" org-metaup (org-at-table-p)]
  22792. ["Move Row Down" org-metadown (org-at-table-p)]
  22793. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  22794. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  22795. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  22796. "--"
  22797. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  22798. ("Rectangle"
  22799. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  22800. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  22801. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  22802. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  22803. "--"
  22804. ("Calculate"
  22805. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  22806. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  22807. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  22808. "--"
  22809. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  22810. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  22811. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  22812. "--"
  22813. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  22814. "--"
  22815. ["Sum Column/Rectangle" org-table-sum
  22816. (or (org-at-table-p) (org-region-active-p))]
  22817. ["Which Column?" org-table-current-column (org-at-table-p)])
  22818. ["Debug Formulas"
  22819. org-table-toggle-formula-debugger
  22820. :style toggle :selected org-table-formula-debug]
  22821. ["Show Col/Row Numbers"
  22822. org-table-toggle-coordinate-overlays
  22823. :style toggle :selected org-table-overlay-coordinates]
  22824. "--"
  22825. ["Create" org-table-create (and (not (org-at-table-p))
  22826. org-enable-table-editor)]
  22827. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  22828. ["Import from File" org-table-import (not (org-at-table-p))]
  22829. ["Export to File" org-table-export (org-at-table-p)]
  22830. "--"
  22831. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  22832. (easy-menu-define org-org-menu org-mode-map "Org menu"
  22833. '("Org"
  22834. ("Show/Hide"
  22835. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  22836. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  22837. ["Sparse Tree" org-occur t]
  22838. ["Reveal Context" org-reveal t]
  22839. ["Show All" show-all t]
  22840. "--"
  22841. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  22842. "--"
  22843. ["New Heading" org-insert-heading t]
  22844. ("Navigate Headings"
  22845. ["Up" outline-up-heading t]
  22846. ["Next" outline-next-visible-heading t]
  22847. ["Previous" outline-previous-visible-heading t]
  22848. ["Next Same Level" outline-forward-same-level t]
  22849. ["Previous Same Level" outline-backward-same-level t]
  22850. "--"
  22851. ["Jump" org-goto t])
  22852. ("Edit Structure"
  22853. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  22854. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  22855. "--"
  22856. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  22857. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  22858. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  22859. "--"
  22860. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  22861. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  22862. ["Demote Heading" org-metaright (not (org-at-table-p))]
  22863. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  22864. "--"
  22865. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  22866. "--"
  22867. ["Convert to odd levels" org-convert-to-odd-levels t]
  22868. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  22869. ("Editing"
  22870. ["Emphasis..." org-emphasize t])
  22871. ("Archive"
  22872. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  22873. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  22874. ; :active t :keys "C-u C-c C-x C-a"]
  22875. ["Sparse trees open ARCHIVE trees"
  22876. (setq org-sparse-tree-open-archived-trees
  22877. (not org-sparse-tree-open-archived-trees))
  22878. :style toggle :selected org-sparse-tree-open-archived-trees]
  22879. ["Cycling opens ARCHIVE trees"
  22880. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  22881. :style toggle :selected org-cycle-open-archived-trees]
  22882. ["Agenda includes ARCHIVE trees"
  22883. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  22884. :style toggle :selected (not org-agenda-skip-archived-trees)]
  22885. "--"
  22886. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  22887. ; ["Check and Move Children" (org-archive-subtree '(4))
  22888. ; :active t :keys "C-u C-c C-x C-s"]
  22889. )
  22890. "--"
  22891. ("TODO Lists"
  22892. ["TODO/DONE/-" org-todo t]
  22893. ("Select keyword"
  22894. ["Next keyword" org-shiftright (org-on-heading-p)]
  22895. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  22896. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  22897. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  22898. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  22899. ["Show TODO Tree" org-show-todo-tree t]
  22900. ["Global TODO list" org-todo-list t]
  22901. "--"
  22902. ["Set Priority" org-priority t]
  22903. ["Priority Up" org-shiftup t]
  22904. ["Priority Down" org-shiftdown t])
  22905. ("TAGS and Properties"
  22906. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  22907. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  22908. "--"
  22909. ["Set property" 'org-set-property t]
  22910. ["Column view of properties" org-columns t])
  22911. ("Dates and Scheduling"
  22912. ["Timestamp" org-time-stamp t]
  22913. ["Timestamp (inactive)" org-time-stamp-inactive t]
  22914. ("Change Date"
  22915. ["1 Day Later" org-shiftright t]
  22916. ["1 Day Earlier" org-shiftleft t]
  22917. ["1 ... Later" org-shiftup t]
  22918. ["1 ... Earlier" org-shiftdown t])
  22919. ["Compute Time Range" org-evaluate-time-range t]
  22920. ["Schedule Item" org-schedule t]
  22921. ["Deadline" org-deadline t]
  22922. "--"
  22923. ["Custom time format" org-toggle-time-stamp-overlays
  22924. :style radio :selected org-display-custom-times]
  22925. "--"
  22926. ["Goto Calendar" org-goto-calendar t]
  22927. ["Date from Calendar" org-date-from-calendar t])
  22928. ("Logging work"
  22929. ["Clock in" org-clock-in t]
  22930. ["Clock out" org-clock-out t]
  22931. ["Clock cancel" org-clock-cancel t]
  22932. ["Goto running clock" org-clock-goto t]
  22933. ["Display times" org-clock-display t]
  22934. ["Create clock table" org-clock-report t]
  22935. "--"
  22936. ["Record DONE time"
  22937. (progn (setq org-log-done (not org-log-done))
  22938. (message "Switching to %s will %s record a timestamp"
  22939. (car org-done-keywords)
  22940. (if org-log-done "automatically" "not")))
  22941. :style toggle :selected org-log-done])
  22942. "--"
  22943. ["Agenda Command..." org-agenda t]
  22944. ("File List for Agenda")
  22945. ("Special views current file"
  22946. ["TODO Tree" org-show-todo-tree t]
  22947. ["Check Deadlines" org-check-deadlines t]
  22948. ["Timeline" org-timeline t]
  22949. ["Tags Tree" org-tags-sparse-tree t])
  22950. "--"
  22951. ("Hyperlinks"
  22952. ["Store Link (Global)" org-store-link t]
  22953. ["Insert Link" org-insert-link t]
  22954. ["Follow Link" org-open-at-point t]
  22955. "--"
  22956. ["Next link" org-next-link t]
  22957. ["Previous link" org-previous-link t]
  22958. "--"
  22959. ["Descriptive Links"
  22960. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  22961. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  22962. ["Literal Links"
  22963. (progn
  22964. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  22965. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  22966. "--"
  22967. ["Export/Publish..." org-export t]
  22968. ("LaTeX"
  22969. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  22970. :selected org-cdlatex-mode]
  22971. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  22972. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  22973. ["Modify math symbol" org-cdlatex-math-modify
  22974. (org-inside-LaTeX-fragment-p)]
  22975. ["Export LaTeX fragments as images"
  22976. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  22977. :style toggle :selected org-export-with-LaTeX-fragments])
  22978. "--"
  22979. ("Documentation"
  22980. ["Show Version" org-version t]
  22981. ["Info Documentation" org-info t])
  22982. ("Customize"
  22983. ["Browse Org Group" org-customize t]
  22984. "--"
  22985. ["Expand This Menu" org-create-customize-menu
  22986. (fboundp 'customize-menu-create)])
  22987. "--"
  22988. ["Refresh setup" org-mode-restart t]
  22989. ))
  22990. (defun org-info (&optional node)
  22991. "Read documentation for Org-mode in the info system.
  22992. With optional NODE, go directly to that node."
  22993. (interactive)
  22994. (require 'info)
  22995. (Info-goto-node (format "(org)%s" (or node ""))))
  22996. (defun org-install-agenda-files-menu ()
  22997. (let ((bl (buffer-list)))
  22998. (save-excursion
  22999. (while bl
  23000. (set-buffer (pop bl))
  23001. (if (org-mode-p) (setq bl nil)))
  23002. (when (org-mode-p)
  23003. (easy-menu-change
  23004. '("Org") "File List for Agenda"
  23005. (append
  23006. (list
  23007. ["Edit File List" (org-edit-agenda-file-list) t]
  23008. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  23009. ["Remove Current File from List" org-remove-file t]
  23010. ["Cycle through agenda files" org-cycle-agenda-files t]
  23011. ["Occur in all agenda files" org-occur-in-agenda-files t]
  23012. "--")
  23013. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  23014. ;;;; Documentation
  23015. (defun org-customize ()
  23016. "Call the customize function with org as argument."
  23017. (interactive)
  23018. (customize-browse 'org))
  23019. (defun org-create-customize-menu ()
  23020. "Create a full customization menu for Org-mode, insert it into the menu."
  23021. (interactive)
  23022. (if (fboundp 'customize-menu-create)
  23023. (progn
  23024. (easy-menu-change
  23025. '("Org") "Customize"
  23026. `(["Browse Org group" org-customize t]
  23027. "--"
  23028. ,(customize-menu-create 'org)
  23029. ["Set" Custom-set t]
  23030. ["Save" Custom-save t]
  23031. ["Reset to Current" Custom-reset-current t]
  23032. ["Reset to Saved" Custom-reset-saved t]
  23033. ["Reset to Standard Settings" Custom-reset-standard t]))
  23034. (message "\"Org\"-menu now contains full customization menu"))
  23035. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  23036. ;;;; Miscellaneous stuff
  23037. ;;; Generally useful functions
  23038. (defun org-context ()
  23039. "Return a list of contexts of the current cursor position.
  23040. If several contexts apply, all are returned.
  23041. Each context entry is a list with a symbol naming the context, and
  23042. two positions indicating start and end of the context. Possible
  23043. contexts are:
  23044. :headline anywhere in a headline
  23045. :headline-stars on the leading stars in a headline
  23046. :todo-keyword on a TODO keyword (including DONE) in a headline
  23047. :tags on the TAGS in a headline
  23048. :priority on the priority cookie in a headline
  23049. :item on the first line of a plain list item
  23050. :item-bullet on the bullet/number of a plain list item
  23051. :checkbox on the checkbox in a plain list item
  23052. :table in an org-mode table
  23053. :table-special on a special filed in a table
  23054. :table-table in a table.el table
  23055. :link on a hyperlink
  23056. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  23057. :target on a <<target>>
  23058. :radio-target on a <<<radio-target>>>
  23059. :latex-fragment on a LaTeX fragment
  23060. :latex-preview on a LaTeX fragment with overlayed preview image
  23061. This function expects the position to be visible because it uses font-lock
  23062. faces as a help to recognize the following contexts: :table-special, :link,
  23063. and :keyword."
  23064. (let* ((f (get-text-property (point) 'face))
  23065. (faces (if (listp f) f (list f)))
  23066. (p (point)) clist o)
  23067. ;; First the large context
  23068. (cond
  23069. ((org-on-heading-p t)
  23070. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  23071. (when (progn
  23072. (beginning-of-line 1)
  23073. (looking-at org-todo-line-tags-regexp))
  23074. (push (org-point-in-group p 1 :headline-stars) clist)
  23075. (push (org-point-in-group p 2 :todo-keyword) clist)
  23076. (push (org-point-in-group p 4 :tags) clist))
  23077. (goto-char p)
  23078. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  23079. (if (looking-at "\\[#[A-Z0-9]\\]")
  23080. (push (org-point-in-group p 0 :priority) clist)))
  23081. ((org-at-item-p)
  23082. (push (org-point-in-group p 2 :item-bullet) clist)
  23083. (push (list :item (point-at-bol)
  23084. (save-excursion (org-end-of-item) (point)))
  23085. clist)
  23086. (and (org-at-item-checkbox-p)
  23087. (push (org-point-in-group p 0 :checkbox) clist)))
  23088. ((org-at-table-p)
  23089. (push (list :table (org-table-begin) (org-table-end)) clist)
  23090. (if (memq 'org-formula faces)
  23091. (push (list :table-special
  23092. (previous-single-property-change p 'face)
  23093. (next-single-property-change p 'face)) clist)))
  23094. ((org-at-table-p 'any)
  23095. (push (list :table-table) clist)))
  23096. (goto-char p)
  23097. ;; Now the small context
  23098. (cond
  23099. ((org-at-timestamp-p)
  23100. (push (org-point-in-group p 0 :timestamp) clist))
  23101. ((memq 'org-link faces)
  23102. (push (list :link
  23103. (previous-single-property-change p 'face)
  23104. (next-single-property-change p 'face)) clist))
  23105. ((memq 'org-special-keyword faces)
  23106. (push (list :keyword
  23107. (previous-single-property-change p 'face)
  23108. (next-single-property-change p 'face)) clist))
  23109. ((org-on-target-p)
  23110. (push (org-point-in-group p 0 :target) clist)
  23111. (goto-char (1- (match-beginning 0)))
  23112. (if (looking-at org-radio-target-regexp)
  23113. (push (org-point-in-group p 0 :radio-target) clist))
  23114. (goto-char p))
  23115. ((setq o (car (delq nil
  23116. (mapcar
  23117. (lambda (x)
  23118. (if (memq x org-latex-fragment-image-overlays) x))
  23119. (org-overlays-at (point))))))
  23120. (push (list :latex-fragment
  23121. (org-overlay-start o) (org-overlay-end o)) clist)
  23122. (push (list :latex-preview
  23123. (org-overlay-start o) (org-overlay-end o)) clist))
  23124. ((org-inside-LaTeX-fragment-p)
  23125. ;; FIXME: positions wrong.
  23126. (push (list :latex-fragment (point) (point)) clist)))
  23127. (setq clist (nreverse (delq nil clist)))
  23128. clist))
  23129. ;; FIXME: Compare with at-regexp-p Do we need both?
  23130. (defun org-in-regexp (re &optional nlines visually)
  23131. "Check if point is inside a match of regexp.
  23132. Normally only the current line is checked, but you can include NLINES extra
  23133. lines both before and after point into the search.
  23134. If VISUALLY is set, require that the cursor is not after the match but
  23135. really on, so that the block visually is on the match."
  23136. (catch 'exit
  23137. (let ((pos (point))
  23138. (eol (point-at-eol (+ 1 (or nlines 0))))
  23139. (inc (if visually 1 0)))
  23140. (save-excursion
  23141. (beginning-of-line (- 1 (or nlines 0)))
  23142. (while (re-search-forward re eol t)
  23143. (if (and (<= (match-beginning 0) pos)
  23144. (>= (+ inc (match-end 0)) pos))
  23145. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  23146. (defun org-at-regexp-p (regexp)
  23147. "Is point inside a match of REGEXP in the current line?"
  23148. (catch 'exit
  23149. (save-excursion
  23150. (let ((pos (point)) (end (point-at-eol)))
  23151. (beginning-of-line 1)
  23152. (while (re-search-forward regexp end t)
  23153. (if (and (<= (match-beginning 0) pos)
  23154. (>= (match-end 0) pos))
  23155. (throw 'exit t)))
  23156. nil))))
  23157. (defun org-occur-in-agenda-files (regexp &optional nlines)
  23158. "Call `multi-occur' with buffers for all agenda files."
  23159. (interactive "sOrg-files matching: \np")
  23160. (let* ((files (org-agenda-files))
  23161. (tnames (mapcar 'file-truename files))
  23162. (extra org-agenda-multi-occur-extra-files)
  23163. f)
  23164. (while (setq f (pop extra))
  23165. (unless (member (file-truename f) tnames)
  23166. (add-to-list 'files f 'append)
  23167. (add-to-list 'tnames (file-truename f) 'append)))
  23168. (multi-occur
  23169. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  23170. regexp)))
  23171. (defun org-uniquify (list)
  23172. "Remove duplicate elements from LIST."
  23173. (let (res)
  23174. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  23175. res))
  23176. (defun org-delete-all (elts list)
  23177. "Remove all elements in ELTS from LIST."
  23178. (while elts
  23179. (setq list (delete (pop elts) list)))
  23180. list)
  23181. (defun org-point-in-group (point group &optional context)
  23182. "Check if POINT is in match-group GROUP.
  23183. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  23184. match. If the match group does ot exist or point is not inside it,
  23185. return nil."
  23186. (and (match-beginning group)
  23187. (>= point (match-beginning group))
  23188. (<= point (match-end group))
  23189. (if context
  23190. (list context (match-beginning group) (match-end group))
  23191. t)))
  23192. (defun org-switch-to-buffer-other-window (&rest args)
  23193. "Switch to buffer in a second window on the current frame.
  23194. In particular, do not allow pop-up frames."
  23195. (let (pop-up-frames special-display-buffer-names special-display-regexps
  23196. special-display-function)
  23197. (apply 'switch-to-buffer-other-window args)))
  23198. (defun org-combine-plists (&rest plists)
  23199. "Create a single property list from all plists in PLISTS.
  23200. The process starts by copying the first list, and then setting properties
  23201. from the other lists. Settings in the last list are the most significant
  23202. ones and overrule settings in the other lists."
  23203. (let ((rtn (copy-sequence (pop plists)))
  23204. p v ls)
  23205. (while plists
  23206. (setq ls (pop plists))
  23207. (while ls
  23208. (setq p (pop ls) v (pop ls))
  23209. (setq rtn (plist-put rtn p v))))
  23210. rtn))
  23211. (defun org-move-line-down (arg)
  23212. "Move the current line down. With prefix argument, move it past ARG lines."
  23213. (interactive "p")
  23214. (let ((col (current-column))
  23215. beg end pos)
  23216. (beginning-of-line 1) (setq beg (point))
  23217. (beginning-of-line 2) (setq end (point))
  23218. (beginning-of-line (+ 1 arg))
  23219. (setq pos (move-marker (make-marker) (point)))
  23220. (insert (delete-and-extract-region beg end))
  23221. (goto-char pos)
  23222. (move-to-column col)))
  23223. (defun org-move-line-up (arg)
  23224. "Move the current line up. With prefix argument, move it past ARG lines."
  23225. (interactive "p")
  23226. (let ((col (current-column))
  23227. beg end pos)
  23228. (beginning-of-line 1) (setq beg (point))
  23229. (beginning-of-line 2) (setq end (point))
  23230. (beginning-of-line (- arg))
  23231. (setq pos (move-marker (make-marker) (point)))
  23232. (insert (delete-and-extract-region beg end))
  23233. (goto-char pos)
  23234. (move-to-column col)))
  23235. (defun org-replace-escapes (string table)
  23236. "Replace %-escapes in STRING with values in TABLE.
  23237. TABLE is an association list with keys like \"%a\" and string values.
  23238. The sequences in STRING may contain normal field width and padding information,
  23239. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  23240. so values can contain further %-escapes if they are define later in TABLE."
  23241. (let ((case-fold-search nil)
  23242. e re rpl)
  23243. (while (setq e (pop table))
  23244. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  23245. (while (string-match re string)
  23246. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  23247. (cdr e)))
  23248. (setq string (replace-match rpl t t string))))
  23249. string))
  23250. (defun org-sublist (list start end)
  23251. "Return a section of LIST, from START to END.
  23252. Counting starts at 1."
  23253. (let (rtn (c start))
  23254. (setq list (nthcdr (1- start) list))
  23255. (while (and list (<= c end))
  23256. (push (pop list) rtn)
  23257. (setq c (1+ c)))
  23258. (nreverse rtn)))
  23259. (defun org-find-base-buffer-visiting (file)
  23260. "Like `find-buffer-visiting' but alway return the base buffer and
  23261. not an indirect buffer"
  23262. (let ((buf (find-buffer-visiting file)))
  23263. (if buf
  23264. (or (buffer-base-buffer buf) buf)
  23265. nil)))
  23266. (defun org-image-file-name-regexp ()
  23267. "Return regexp matching the file names of images."
  23268. (if (fboundp 'image-file-name-regexp)
  23269. (image-file-name-regexp)
  23270. (let ((image-file-name-extensions
  23271. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  23272. "xbm" "xpm" "pbm" "pgm" "ppm")))
  23273. (concat "\\."
  23274. (regexp-opt (nconc (mapcar 'upcase
  23275. image-file-name-extensions)
  23276. image-file-name-extensions)
  23277. t)
  23278. "\\'"))))
  23279. (defun org-file-image-p (file)
  23280. "Return non-nil if FILE is an image."
  23281. (save-match-data
  23282. (string-match (org-image-file-name-regexp) file)))
  23283. ;;; Paragraph filling stuff.
  23284. ;; We want this to be just right, so use the full arsenal.
  23285. (defun org-indent-line-function ()
  23286. "Indent line like previous, but further if previous was headline or item."
  23287. (interactive)
  23288. (let* ((pos (point))
  23289. (itemp (org-at-item-p))
  23290. column bpos bcol tpos tcol bullet btype bullet-type)
  23291. ;; Find the previous relevant line
  23292. (beginning-of-line 1)
  23293. (cond
  23294. ((looking-at "#") (setq column 0))
  23295. ((looking-at "\\*+ ") (setq column 0))
  23296. (t
  23297. (beginning-of-line 0)
  23298. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  23299. (beginning-of-line 0))
  23300. (cond
  23301. ((looking-at "\\*+[ \t]+")
  23302. (goto-char (match-end 0))
  23303. (setq column (current-column)))
  23304. ((org-in-item-p)
  23305. (org-beginning-of-item)
  23306. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  23307. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  23308. (setq bpos (match-beginning 1) tpos (match-end 0)
  23309. bcol (progn (goto-char bpos) (current-column))
  23310. tcol (progn (goto-char tpos) (current-column))
  23311. bullet (match-string 1)
  23312. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  23313. (if (not itemp)
  23314. (setq column tcol)
  23315. (goto-char pos)
  23316. (beginning-of-line 1)
  23317. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  23318. (setq bullet (match-string 1)
  23319. btype (if (string-match "[0-9]" bullet) "n" bullet))
  23320. (setq column (if (equal btype bullet-type) bcol tcol))))
  23321. (t (setq column (org-get-indentation))))))
  23322. (goto-char pos)
  23323. (if (<= (current-column) (current-indentation))
  23324. (indent-line-to column)
  23325. (save-excursion (indent-line-to column)))
  23326. (setq column (current-column))
  23327. (beginning-of-line 1)
  23328. (if (looking-at
  23329. "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  23330. (replace-match (concat "\\1" (format org-property-format
  23331. (match-string 2) (match-string 3)))
  23332. t nil))
  23333. (move-to-column column)))
  23334. (defun org-set-autofill-regexps ()
  23335. (interactive)
  23336. ;; In the paragraph separator we include headlines, because filling
  23337. ;; text in a line directly attached to a headline would otherwise
  23338. ;; fill the headline as well.
  23339. (org-set-local 'comment-start-skip "^#+[ \t]*")
  23340. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  23341. ;; The paragraph starter includes hand-formatted lists.
  23342. (org-set-local 'paragraph-start
  23343. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  23344. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  23345. ;; But only if the user has not turned off tables or fixed-width regions
  23346. (org-set-local
  23347. 'auto-fill-inhibit-regexp
  23348. (concat "\\*+ \\|#\\+"
  23349. "\\|[ \t]*" org-keyword-time-regexp
  23350. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  23351. (concat
  23352. "\\|[ \t]*["
  23353. (if org-enable-table-editor "|" "")
  23354. (if org-enable-fixed-width-editor ":" "")
  23355. "]"))))
  23356. ;; We use our own fill-paragraph function, to make sure that tables
  23357. ;; and fixed-width regions are not wrapped. That function will pass
  23358. ;; through to `fill-paragraph' when appropriate.
  23359. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  23360. ; Adaptive filling: To get full control, first make sure that
  23361. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  23362. (org-set-local 'adaptive-fill-regexp "\000")
  23363. (org-set-local 'adaptive-fill-function
  23364. 'org-adaptive-fill-function))
  23365. (defun org-fill-paragraph (&optional justify)
  23366. "Re-align a table, pass through to fill-paragraph if no table."
  23367. (let ((table-p (org-at-table-p))
  23368. (table.el-p (org-at-table.el-p)))
  23369. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  23370. (table.el-p t) ; skip table.el tables
  23371. (table-p (org-table-align) t) ; align org-mode tables
  23372. (t nil)))) ; call paragraph-fill
  23373. ;; For reference, this is the default value of adaptive-fill-regexp
  23374. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  23375. (defun org-adaptive-fill-function ()
  23376. "Return a fill prefix for org-mode files.
  23377. In particular, this makes sure hanging paragraphs for hand-formatted lists
  23378. work correctly."
  23379. (cond ((looking-at "#[ \t]+")
  23380. (match-string 0))
  23381. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  23382. (save-excursion
  23383. (goto-char (match-end 0))
  23384. (make-string (current-column) ?\ )))
  23385. (t nil)))
  23386. ;;;; Functions extending outline functionality
  23387. (defun org-beginning-of-line (&optional arg)
  23388. "Go to the beginning of the current line. If that is invisible, continue
  23389. to a visible line beginning. This makes the function of C-a more intuitive.
  23390. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  23391. first attempt, and only move to after the tags when the cursor is already
  23392. beyond the end of the headline."
  23393. (interactive "P")
  23394. (let ((pos (point)))
  23395. (beginning-of-line 1)
  23396. (if (bobp)
  23397. nil
  23398. (backward-char 1)
  23399. (if (org-invisible-p)
  23400. (while (and (not (bobp)) (org-invisible-p))
  23401. (backward-char 1)
  23402. (beginning-of-line 1))
  23403. (forward-char 1)))
  23404. (when org-special-ctrl-a/e
  23405. (cond
  23406. ((and (looking-at org-todo-line-regexp)
  23407. (= (char-after (match-end 1)) ?\ ))
  23408. (goto-char
  23409. (if (eq org-special-ctrl-a/e t)
  23410. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  23411. ((= pos (point)) (match-beginning 3))
  23412. (t (point)))
  23413. (cond ((> pos (point)) (point))
  23414. ((not (eq last-command this-command)) (point))
  23415. (t (match-beginning 3))))))
  23416. ((org-at-item-p)
  23417. (goto-char
  23418. (if (eq org-special-ctrl-a/e t)
  23419. (cond ((> pos (match-end 4)) (match-end 4))
  23420. ((= pos (point)) (match-end 4))
  23421. (t (point)))
  23422. (cond ((> pos (point)) (point))
  23423. ((not (eq last-command this-command)) (point))
  23424. (t (match-end 4))))))))))
  23425. (defun org-end-of-line (&optional arg)
  23426. "Go to the end of the line.
  23427. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  23428. first attempt, and only move to after the tags when the cursor is already
  23429. beyond the end of the headline."
  23430. (interactive "P")
  23431. (if (or (not org-special-ctrl-a/e)
  23432. (not (org-on-heading-p)))
  23433. (end-of-line arg)
  23434. (let ((pos (point)))
  23435. (beginning-of-line 1)
  23436. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  23437. (if (eq org-special-ctrl-a/e t)
  23438. (if (or (< pos (match-beginning 1))
  23439. (= pos (match-end 0)))
  23440. (goto-char (match-beginning 1))
  23441. (goto-char (match-end 0)))
  23442. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  23443. (goto-char (match-end 0))
  23444. (goto-char (match-beginning 1))))
  23445. (end-of-line arg)))))
  23446. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  23447. (define-key org-mode-map "\C-e" 'org-end-of-line)
  23448. (defun org-invisible-p ()
  23449. "Check if point is at a character currently not visible."
  23450. ;; Early versions of noutline don't have `outline-invisible-p'.
  23451. (if (fboundp 'outline-invisible-p)
  23452. (outline-invisible-p)
  23453. (get-char-property (point) 'invisible)))
  23454. (defun org-invisible-p2 ()
  23455. "Check if point is at a character currently not visible."
  23456. (save-excursion
  23457. (if (and (eolp) (not (bobp))) (backward-char 1))
  23458. ;; Early versions of noutline don't have `outline-invisible-p'.
  23459. (if (fboundp 'outline-invisible-p)
  23460. (outline-invisible-p)
  23461. (get-char-property (point) 'invisible))))
  23462. (defalias 'org-back-to-heading 'outline-back-to-heading)
  23463. (defalias 'org-on-heading-p 'outline-on-heading-p)
  23464. (defalias 'org-at-heading-p 'outline-on-heading-p)
  23465. (defun org-at-heading-or-item-p ()
  23466. (or (org-on-heading-p) (org-at-item-p)))
  23467. (defun org-on-target-p ()
  23468. (or (org-in-regexp org-radio-target-regexp)
  23469. (org-in-regexp org-target-regexp)))
  23470. (defun org-up-heading-all (arg)
  23471. "Move to the heading line of which the present line is a subheading.
  23472. This function considers both visible and invisible heading lines.
  23473. With argument, move up ARG levels."
  23474. (if (fboundp 'outline-up-heading-all)
  23475. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  23476. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  23477. (defun org-up-heading-safe ()
  23478. "Move to the heading line of which the present line is a subheading.
  23479. This version will not throw an error. It will return the level of the
  23480. headline found, or nil if no higher level is found."
  23481. (let ((pos (point)) start-level level
  23482. (re (concat "^" outline-regexp)))
  23483. (catch 'exit
  23484. (outline-back-to-heading t)
  23485. (setq start-level (funcall outline-level))
  23486. (if (equal start-level 1) (throw 'exit nil))
  23487. (while (re-search-backward re nil t)
  23488. (setq level (funcall outline-level))
  23489. (if (< level start-level) (throw 'exit level)))
  23490. nil)))
  23491. (defun org-goto-sibling (&optional previous)
  23492. "Goto the next sibling, even if it is invisible.
  23493. When PREVIOUS is set, go to the previous sibling instead. Returns t
  23494. when a sibling was found. When none is found, return nil and don't
  23495. move point."
  23496. (let ((fun (if previous 're-search-backward 're-search-forward))
  23497. (pos (point))
  23498. (re (concat "^" outline-regexp))
  23499. level l)
  23500. (when (condition-case nil (org-back-to-heading t) (error nil))
  23501. (setq level (funcall outline-level))
  23502. (catch 'exit
  23503. (or previous (forward-char 1))
  23504. (while (funcall fun re nil t)
  23505. (setq l (funcall outline-level))
  23506. (when (< l level) (goto-char pos) (throw 'exit nil))
  23507. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  23508. (goto-char pos)
  23509. nil))))
  23510. (defun org-show-siblings ()
  23511. "Show all siblings of the current headline."
  23512. (save-excursion
  23513. (while (org-goto-sibling) (org-flag-heading nil)))
  23514. (save-excursion
  23515. (while (org-goto-sibling 'previous)
  23516. (org-flag-heading nil))))
  23517. (defun org-show-hidden-entry ()
  23518. "Show an entry where even the heading is hidden."
  23519. (save-excursion
  23520. (org-show-entry)))
  23521. (defun org-flag-heading (flag &optional entry)
  23522. "Flag the current heading. FLAG non-nil means make invisible.
  23523. When ENTRY is non-nil, show the entire entry."
  23524. (save-excursion
  23525. (org-back-to-heading t)
  23526. ;; Check if we should show the entire entry
  23527. (if entry
  23528. (progn
  23529. (org-show-entry)
  23530. (save-excursion
  23531. (and (outline-next-heading)
  23532. (org-flag-heading nil))))
  23533. (outline-flag-region (max (point-min) (1- (point)))
  23534. (save-excursion (outline-end-of-heading) (point))
  23535. flag))))
  23536. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  23537. ;; This is an exact copy of the original function, but it uses
  23538. ;; `org-back-to-heading', to make it work also in invisible
  23539. ;; trees. And is uses an invisible-OK argument.
  23540. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  23541. (org-back-to-heading invisible-OK)
  23542. (let ((first t)
  23543. (level (funcall outline-level)))
  23544. (while (and (not (eobp))
  23545. (or first (> (funcall outline-level) level)))
  23546. (setq first nil)
  23547. (outline-next-heading))
  23548. (unless to-heading
  23549. (if (memq (preceding-char) '(?\n ?\^M))
  23550. (progn
  23551. ;; Go to end of line before heading
  23552. (forward-char -1)
  23553. (if (memq (preceding-char) '(?\n ?\^M))
  23554. ;; leave blank line before heading
  23555. (forward-char -1))))))
  23556. (point))
  23557. (defun org-show-subtree ()
  23558. "Show everything after this heading at deeper levels."
  23559. (outline-flag-region
  23560. (point)
  23561. (save-excursion
  23562. (outline-end-of-subtree) (outline-next-heading) (point))
  23563. nil))
  23564. (defun org-show-entry ()
  23565. "Show the body directly following this heading.
  23566. Show the heading too, if it is currently invisible."
  23567. (interactive)
  23568. (save-excursion
  23569. (condition-case nil
  23570. (progn
  23571. (org-back-to-heading t)
  23572. (outline-flag-region
  23573. (max (point-min) (1- (point)))
  23574. (save-excursion
  23575. (re-search-forward
  23576. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  23577. (or (match-beginning 1) (point-max)))
  23578. nil))
  23579. (error nil))))
  23580. (defun org-make-options-regexp (kwds)
  23581. "Make a regular expression for keyword lines."
  23582. (concat
  23583. "^"
  23584. "#?[ \t]*\\+\\("
  23585. (mapconcat 'regexp-quote kwds "\\|")
  23586. "\\):[ \t]*"
  23587. "\\(.+\\)"))
  23588. ;; Make isearch reveal the necessary context
  23589. (defun org-isearch-end ()
  23590. "Reveal context after isearch exits."
  23591. (when isearch-success ; only if search was successful
  23592. (if (featurep 'xemacs)
  23593. ;; Under XEmacs, the hook is run in the correct place,
  23594. ;; we directly show the context.
  23595. (org-show-context 'isearch)
  23596. ;; In Emacs the hook runs *before* restoring the overlays.
  23597. ;; So we have to use a one-time post-command-hook to do this.
  23598. ;; (Emacs 22 has a special variable, see function `org-mode')
  23599. (unless (and (boundp 'isearch-mode-end-hook-quit)
  23600. isearch-mode-end-hook-quit)
  23601. ;; Only when the isearch was not quitted.
  23602. (org-add-hook 'post-command-hook 'org-isearch-post-command
  23603. 'append 'local)))))
  23604. (defun org-isearch-post-command ()
  23605. "Remove self from hook, and show context."
  23606. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  23607. (org-show-context 'isearch))
  23608. ;;;; Address problems with some other packages
  23609. ;; Make flyspell not check words in links, to not mess up our keymap
  23610. (defun org-mode-flyspell-verify ()
  23611. "Don't let flyspell put overlays at active buttons."
  23612. (not (get-text-property (point) 'keymap)))
  23613. ;; Make `bookmark-jump' show the jump location if it was hidden.
  23614. (eval-after-load "bookmark"
  23615. '(if (boundp 'bookmark-after-jump-hook)
  23616. ;; We can use the hook
  23617. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23618. ;; Hook not available, use advice
  23619. (defadvice bookmark-jump (after org-make-visible activate)
  23620. "Make the position visible."
  23621. (org-bookmark-jump-unhide))))
  23622. (defun org-bookmark-jump-unhide ()
  23623. "Unhide the current position, to show the bookmark location."
  23624. (and (org-mode-p)
  23625. (or (org-invisible-p)
  23626. (save-excursion (goto-char (max (point-min) (1- (point))))
  23627. (org-invisible-p)))
  23628. (org-show-context 'bookmark-jump)))
  23629. ;; Make session.el ignore our circular variable
  23630. (eval-after-load "session"
  23631. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23632. ;;;; Experimental code
  23633. (defun org-closed-in-range ()
  23634. "Sparse tree of items closed in a certain time range.
  23635. Still experimental, may disappear in the furture."
  23636. (interactive)
  23637. ;; Get the time interval from the user.
  23638. (let* ((time1 (time-to-seconds
  23639. (org-read-date nil 'to-time nil "Starting date: ")))
  23640. (time2 (time-to-seconds
  23641. (org-read-date nil 'to-time nil "End date:")))
  23642. ;; callback function
  23643. (callback (lambda ()
  23644. (let ((time
  23645. (time-to-seconds
  23646. (apply 'encode-time
  23647. (org-parse-time-string
  23648. (match-string 1))))))
  23649. ;; check if time in interval
  23650. (and (>= time time1) (<= time time2))))))
  23651. ;; make tree, check each match with the callback
  23652. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  23653. (defun org-fill-paragraph-experimental (&optional justify)
  23654. "Re-align a table, pass through to fill-paragraph if no table."
  23655. (let ((table-p (org-at-table-p))
  23656. (table.el-p (org-at-table.el-p)))
  23657. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  23658. (table.el-p t) ; skip table.el tables
  23659. (table-p (org-table-align) t) ; align org-mode tables
  23660. ((save-excursion
  23661. (let ((pos (1+ (point-at-eol))))
  23662. (backward-paragraph 1)
  23663. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  23664. (save-excursion
  23665. (save-restriction
  23666. (narrow-to-region (1+ (match-end 0)) (point-max))
  23667. (fill-paragraph nil)
  23668. t)))
  23669. (t nil)))) ; call paragraph-fill
  23670. ;; FIXME: this needs a much better algorithm
  23671. (defun org-assign-fast-keys (alist)
  23672. "Assign fast keys to a keyword-key alist.
  23673. Respect keys that are already there."
  23674. (let (new e k c c1 c2 (char ?a))
  23675. (while (setq e (pop alist))
  23676. (cond
  23677. ((equal e '(:startgroup)) (push e new))
  23678. ((equal e '(:endgroup)) (push e new))
  23679. (t
  23680. (setq k (car e) c2 nil)
  23681. (if (cdr e)
  23682. (setq c (cdr e))
  23683. ;; automatically assign a character.
  23684. (setq c1 (string-to-char
  23685. (downcase (substring
  23686. k (if (= (string-to-char k) ?@) 1 0)))))
  23687. (if (or (rassoc c1 new) (rassoc c1 alist))
  23688. (while (or (rassoc char new) (rassoc char alist))
  23689. (setq char (1+ char)))
  23690. (setq c2 c1))
  23691. (setq c (or c2 char)))
  23692. (push (cons k c) new))))
  23693. (nreverse new)))
  23694. (defun org-parse-local-options (string var)
  23695. "Parse STRING for startup setting relevant for variable VAR."
  23696. (let ((rtn (symbol-value var))
  23697. e opts)
  23698. (save-match-data
  23699. (if (or (not string) (not (string-match "\\S-" string)))
  23700. rtn
  23701. (setq opts (delq nil (mapcar (lambda (x)
  23702. (setq e (assoc x org-startup-options))
  23703. (if (eq (nth 1 e) var) e nil))
  23704. (org-split-string string "[ \t]+"))))
  23705. (if (not opts)
  23706. rtn
  23707. (setq rtn nil)
  23708. (while (setq e (pop opts))
  23709. (if (not (nth 3 e))
  23710. (setq rtn (nth 2 e))
  23711. (if (not (listp rtn)) (setq rtn nil))
  23712. (push (nth 2 e) rtn)))
  23713. rtn)))))
  23714. ;; FIXME: what else would be useful?
  23715. ;; - priority
  23716. ;; - date
  23717. (defun org-sparse-tree (&optional arg)
  23718. "Create a sparse tree, prompt for the details.
  23719. This command can create sparse trees. You first need to select the type
  23720. of match used to create the tree:
  23721. t Show entries with a specific TODO keyword.
  23722. T Show entries selected by a tags match.
  23723. p Enter a property name and its value (both with completion on existing
  23724. names/values) and show entries with that property.
  23725. r Show entries matching a regular expression"
  23726. (interactive "P")
  23727. (let (ans kwd value)
  23728. (message "Sparse tree: [r]egexp [t]odo-kwd [T]ag [p]roperty")
  23729. (setq ans (read-char-exclusive))
  23730. (cond
  23731. ((equal ans ?t)
  23732. (org-show-todo-tree '(4)))
  23733. ((equal ans ?T)
  23734. (call-interactively 'org-tags-sparse-tree))
  23735. ((member ans '(?p ?P))
  23736. (setq kwd (completing-read "Property: "
  23737. (mapcar 'list (org-buffer-property-keys))))
  23738. (setq value (completing-read "Value: "
  23739. (mapcar 'list (org-property-values kwd))))
  23740. (unless (string-match "\\`{.*}\\'" value)
  23741. (setq value (concat "\"" value "\"")))
  23742. (org-tags-sparse-tree arg (concat kwd "=" value)))
  23743. ((member ans '(?r ?R))
  23744. (call-interactively 'org-occur))
  23745. (t (error "No such sparse tree command \"%c\"" ans)))))
  23746. ;;;; Finish up
  23747. (provide 'org)
  23748. (run-hooks 'org-load-hook)
  23749. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  23750. ;;; org.el ends here