org.el 1.0 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.21
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.21"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. ;; FIXME: Needs a separate group...
  138. (defcustom org-completion-fallback-command 'hippie-expand
  139. "The expansion command called by \\[org-complete] in normal context.
  140. Normal means, no org-mode-specific context."
  141. :group 'org
  142. :type 'function)
  143. (defgroup org-startup nil
  144. "Options concerning startup of Org-mode."
  145. :tag "Org Startup"
  146. :group 'org)
  147. (defcustom org-startup-folded t
  148. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  149. This can also be configured on a per-file basis by adding one of
  150. the following lines anywhere in the buffer:
  151. #+STARTUP: fold
  152. #+STARTUP: nofold
  153. #+STARTUP: content"
  154. :group 'org-startup
  155. :type '(choice
  156. (const :tag "nofold: show all" nil)
  157. (const :tag "fold: overview" t)
  158. (const :tag "content: all headlines" content)))
  159. (defcustom org-startup-truncated t
  160. "Non-nil means, entering Org-mode will set `truncate-lines'.
  161. This is useful since some lines containing links can be very long and
  162. uninteresting. Also tables look terrible when wrapped."
  163. :group 'org-startup
  164. :type 'boolean)
  165. (defcustom org-startup-align-all-tables nil
  166. "Non-nil means, align all tables when visiting a file.
  167. This is useful when the column width in tables is forced with <N> cookies
  168. in table fields. Such tables will look correct only after the first re-align.
  169. This can also be configured on a per-file basis by adding one of
  170. the following lines anywhere in the buffer:
  171. #+STARTUP: align
  172. #+STARTUP: noalign"
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-insert-mode-line-in-empty-file nil
  176. "Non-nil means insert the first line setting Org-mode in empty files.
  177. When the function `org-mode' is called interactively in an empty file, this
  178. normally means that the file name does not automatically trigger Org-mode.
  179. To ensure that the file will always be in Org-mode in the future, a
  180. line enforcing Org-mode will be inserted into the buffer, if this option
  181. has been set."
  182. :group 'org-startup
  183. :type 'boolean)
  184. (defcustom org-replace-disputed-keys nil
  185. "Non-nil means use alternative key bindings for some keys.
  186. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  187. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  188. If you want to use Org-mode together with one of these other modes,
  189. or more generally if you would like to move some Org-mode commands to
  190. other keys, set this variable and configure the keys with the variable
  191. `org-disputed-keys'.
  192. This option is only relevant at load-time of Org-mode, and must be set
  193. *before* org.el is loaded. Changing it requires a restart of Emacs to
  194. become effective."
  195. :group 'org-startup
  196. :type 'boolean)
  197. (if (fboundp 'defvaralias)
  198. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  199. (defcustom org-disputed-keys
  200. '(([(shift up)] . [(meta p)])
  201. ([(shift down)] . [(meta n)])
  202. ([(shift left)] . [(meta -)])
  203. ([(shift right)] . [(meta +)])
  204. ([(control shift right)] . [(meta shift +)])
  205. ([(control shift left)] . [(meta shift -)]))
  206. "Keys for which Org-mode and other modes compete.
  207. This is an alist, cars are the default keys, second element specifies
  208. the alternative to use when `org-replace-disputed-keys' is t.
  209. Keys can be specified in any syntax supported by `define-key'.
  210. The value of this option takes effect only at Org-mode's startup,
  211. therefore you'll have to restart Emacs to apply it after changing."
  212. :group 'org-startup
  213. :type 'alist)
  214. (defun org-key (key)
  215. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  216. Or return the original if not disputed."
  217. (if org-replace-disputed-keys
  218. (let* ((nkey (key-description key))
  219. (x (org-find-if (lambda (x)
  220. (equal (key-description (car x)) nkey))
  221. org-disputed-keys)))
  222. (if x (cdr x) key))
  223. key))
  224. (defun org-find-if (predicate seq)
  225. (catch 'exit
  226. (while seq
  227. (if (funcall predicate (car seq))
  228. (throw 'exit (car seq))
  229. (pop seq)))))
  230. (defun org-defkey (keymap key def)
  231. "Define a key, possibly translated, as returned by `org-key'."
  232. (define-key keymap (org-key key) def))
  233. (defcustom org-ellipsis nil
  234. "The ellipsis to use in the Org-mode outline.
  235. When nil, just use the standard three dots. When a string, use that instead,
  236. When a face, use the standart 3 dots, but with the specified face.
  237. The change affects only Org-mode (which will then use its own display table).
  238. Changing this requires executing `M-x org-mode' in a buffer to become
  239. effective."
  240. :group 'org-startup
  241. :type '(choice (const :tag "Default" nil)
  242. (face :tag "Face" :value org-warning)
  243. (string :tag "String" :value "...#")))
  244. (defvar org-display-table nil
  245. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  246. (defgroup org-keywords nil
  247. "Keywords in Org-mode."
  248. :tag "Org Keywords"
  249. :group 'org)
  250. (defcustom org-deadline-string "DEADLINE:"
  251. "String to mark deadline entries.
  252. A deadline is this string, followed by a time stamp. Should be a word,
  253. terminated by a colon. You can insert a schedule keyword and
  254. a timestamp with \\[org-deadline].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-scheduled-string "SCHEDULED:"
  259. "String to mark scheduled TODO entries.
  260. A schedule is this string, followed by a time stamp. Should be a word,
  261. terminated by a colon. You can insert a schedule keyword and
  262. a timestamp with \\[org-schedule].
  263. Changes become only effective after restarting Emacs."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defcustom org-closed-string "CLOSED:"
  267. "String used as the prefix for timestamps logging closing a TODO entry."
  268. :group 'org-keywords
  269. :type 'string)
  270. (defcustom org-clock-string "CLOCK:"
  271. "String used as prefix for timestamps clocking work hours on an item."
  272. :group 'org-keywords
  273. :type 'string)
  274. (defcustom org-comment-string "COMMENT"
  275. "Entries starting with this keyword will never be exported.
  276. An entry can be toggled between COMMENT and normal with
  277. \\[org-toggle-comment].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-quote-string "QUOTE"
  282. "Entries starting with this keyword will be exported in fixed-width font.
  283. Quoting applies only to the text in the entry following the headline, and does
  284. not extend beyond the next headline, even if that is lower level.
  285. An entry can be toggled between QUOTE and normal with
  286. \\[org-toggle-fixed-width-section]."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defconst org-repeat-re
  290. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  291. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  292. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  293. "Regular expression for specifying repeated events.
  294. After a match, group 1 contains the repeat expression.")
  295. (defgroup org-structure nil
  296. "Options concerning the general structure of Org-mode files."
  297. :tag "Org Structure"
  298. :group 'org)
  299. (defgroup org-reveal-location nil
  300. "Options about how to make context of a location visible."
  301. :tag "Org Reveal Location"
  302. :group 'org-structure)
  303. (defconst org-context-choice
  304. '(choice
  305. (const :tag "Always" t)
  306. (const :tag "Never" nil)
  307. (repeat :greedy t :tag "Individual contexts"
  308. (cons
  309. (choice :tag "Context"
  310. (const agenda)
  311. (const org-goto)
  312. (const occur-tree)
  313. (const tags-tree)
  314. (const link-search)
  315. (const mark-goto)
  316. (const bookmark-jump)
  317. (const isearch)
  318. (const default))
  319. (boolean))))
  320. "Contexts for the reveal options.")
  321. (defcustom org-show-hierarchy-above '((default . t))
  322. "Non-nil means, show full hierarchy when revealing a location.
  323. Org-mode often shows locations in an org-mode file which might have
  324. been invisible before. When this is set, the hierarchy of headings
  325. above the exposed location is shown.
  326. Turning this off for example for sparse trees makes them very compact.
  327. Instead of t, this can also be an alist specifying this option for different
  328. contexts. Valid contexts are
  329. agenda when exposing an entry from the agenda
  330. org-goto when using the command `org-goto' on key C-c C-j
  331. occur-tree when using the command `org-occur' on key C-c /
  332. tags-tree when constructing a sparse tree based on tags matches
  333. link-search when exposing search matches associated with a link
  334. mark-goto when exposing the jump goal of a mark
  335. bookmark-jump when exposing a bookmark location
  336. isearch when exiting from an incremental search
  337. default default for all contexts not set explicitly"
  338. :group 'org-reveal-location
  339. :type org-context-choice)
  340. (defcustom org-show-following-heading '((default . nil))
  341. "Non-nil means, show following heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the heading following the
  344. match is shown.
  345. Turning this off for example for sparse trees makes them very compact,
  346. but makes it harder to edit the location of the match. In such a case,
  347. use the command \\[org-reveal] to show more context.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. See `org-show-hierarchy-above' for valid contexts."
  350. :group 'org-reveal-location
  351. :type org-context-choice)
  352. (defcustom org-show-siblings '((default . nil) (isearch t))
  353. "Non-nil means, show all sibling heading when revealing a location.
  354. Org-mode often shows locations in an org-mode file which might have
  355. been invisible before. When this is set, the sibling of the current entry
  356. heading are all made visible. If `org-show-hierarchy-above' is t,
  357. the same happens on each level of the hierarchy above the current entry.
  358. By default this is on for the isearch context, off for all other contexts.
  359. Turning this off for example for sparse trees makes them very compact,
  360. but makes it harder to edit the location of the match. In such a case,
  361. use the command \\[org-reveal] to show more context.
  362. Instead of t, this can also be an alist specifying this option for different
  363. contexts. See `org-show-hierarchy-above' for valid contexts."
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-entry-below '((default . nil))
  367. "Non-nil means, show the entry below a headline when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the text below the headline that is
  370. exposed is also shown.
  371. By default this is off for all contexts.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defgroup org-cycle nil
  377. "Options concerning visibility cycling in Org-mode."
  378. :tag "Org Cycle"
  379. :group 'org-structure)
  380. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  381. "Names of drawers. Drawers are not opened by cycling on the headline above.
  382. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  383. this:
  384. :DRAWERNAME:
  385. .....
  386. :END:
  387. The drawer \"PROPERTIES\" is special for capturing properties through
  388. the property API.
  389. Drawers can be defined on the per-file basis with a line like:
  390. #+DRAWERS: HIDDEN STATE PROPERTIES"
  391. :group 'org-structure
  392. :type '(repeat (string :tag "Drawer Name")))
  393. (defcustom org-cycle-global-at-bob nil
  394. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  395. This makes it possible to do global cycling without having to use S-TAB or
  396. C-u TAB. For this special case to work, the first line of the buffer
  397. must not be a headline - it may be empty ot some other text. When used in
  398. this way, `org-cycle-hook' is disables temporarily, to make sure the
  399. cursor stays at the beginning of the buffer.
  400. When this option is nil, don't do anything special at the beginning
  401. of the buffer."
  402. :group 'org-cycle
  403. :type 'boolean)
  404. (defcustom org-cycle-emulate-tab t
  405. "Where should `org-cycle' emulate TAB.
  406. nil Never
  407. white Only in completely white lines
  408. whitestart Only at the beginning of lines, before the first non-white char
  409. t Everywhere except in headlines
  410. exc-hl-bol Everywhere except at the start of a headline
  411. If TAB is used in a place where it does not emulate TAB, the current subtree
  412. visibility is cycled."
  413. :group 'org-cycle
  414. :type '(choice (const :tag "Never" nil)
  415. (const :tag "Only in completely white lines" white)
  416. (const :tag "Before first char in a line" whitestart)
  417. (const :tag "Everywhere except in headlines" t)
  418. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  419. ))
  420. (defcustom org-cycle-separator-lines 2
  421. "Number of empty lines needed to keep an empty line between collapsed trees.
  422. If you leave an empty line between the end of a subtree and the following
  423. headline, this empty line is hidden when the subtree is folded.
  424. Org-mode will leave (exactly) one empty line visible if the number of
  425. empty lines is equal or larger to the number given in this variable.
  426. So the default 2 means, at least 2 empty lines after the end of a subtree
  427. are needed to produce free space between a collapsed subtree and the
  428. following headline.
  429. Special case: when 0, never leave empty lines in collapsed view."
  430. :group 'org-cycle
  431. :type 'integer)
  432. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  433. org-cycle-hide-drawers
  434. org-cycle-show-empty-lines
  435. org-optimize-window-after-visibility-change)
  436. "Hook that is run after `org-cycle' has changed the buffer visibility.
  437. The function(s) in this hook must accept a single argument which indicates
  438. the new state that was set by the most recent `org-cycle' command. The
  439. argument is a symbol. After a global state change, it can have the values
  440. `overview', `content', or `all'. After a local state change, it can have
  441. the values `folded', `children', or `subtree'."
  442. :group 'org-cycle
  443. :type 'hook)
  444. (defgroup org-edit-structure nil
  445. "Options concerning structure editing in Org-mode."
  446. :tag "Org Edit Structure"
  447. :group 'org-structure)
  448. (defcustom org-special-ctrl-a/e nil
  449. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  450. When t, `C-a' will bring back the cursor to the beginning of the
  451. headline text, i.e. after the stars and after a possible TODO keyword.
  452. In an item, this will be the position after the bullet.
  453. When the cursor is already at that position, another `C-a' will bring
  454. it to the beginning of the line.
  455. `C-e' will jump to the end of the headline, ignoring the presence of tags
  456. in the headline. A second `C-e' will then jump to the true end of the
  457. line, after any tags.
  458. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  459. and only a directly following, identical keypress will bring the cursor
  460. to the special positions."
  461. :group 'org-edit-structure
  462. :type '(choice
  463. (const :tag "off" nil)
  464. (const :tag "after bullet first" t)
  465. (const :tag "border first" reversed)))
  466. (if (fboundp 'defvaralias)
  467. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  468. (defcustom org-special-ctrl-k nil
  469. "Non-nil means `C-k' will behave specially in headlines.
  470. When nil, `C-k' will call the default `kill-line' command.
  471. When t, the following will happen while the cursor is in the headline:
  472. - When the cursor is at the beginning of a headline, kill the entire
  473. line and possible the folded subtree below the line.
  474. - When in the middle of the headline text, kill the headline up to the tags.
  475. - When after the headline text, kill the tags."
  476. :group 'org-edit-structure
  477. :type 'boolean)
  478. (defcustom org-odd-levels-only nil
  479. "Non-nil means, skip even levels and only use odd levels for the outline.
  480. This has the effect that two stars are being added/taken away in
  481. promotion/demotion commands. It also influences how levels are
  482. handled by the exporters.
  483. Changing it requires restart of `font-lock-mode' to become effective
  484. for fontification also in regions already fontified.
  485. You may also set this on a per-file basis by adding one of the following
  486. lines to the buffer:
  487. #+STARTUP: odd
  488. #+STARTUP: oddeven"
  489. :group 'org-edit-structure
  490. :group 'org-font-lock
  491. :type 'boolean)
  492. (defcustom org-adapt-indentation t
  493. "Non-nil means, adapt indentation when promoting and demoting.
  494. When this is set and the *entire* text in an entry is indented, the
  495. indentation is increased by one space in a demotion command, and
  496. decreased by one in a promotion command. If any line in the entry
  497. body starts at column 0, indentation is not changed at all."
  498. :group 'org-edit-structure
  499. :type 'boolean)
  500. (defcustom org-blank-before-new-entry '((heading . nil)
  501. (plain-list-item . nil))
  502. "Should `org-insert-heading' leave a blank line before new heading/item?
  503. The value is an alist, with `heading' and `plain-list-item' as car,
  504. and a boolean flag as cdr."
  505. :group 'org-edit-structure
  506. :type '(list
  507. (cons (const heading) (boolean))
  508. (cons (const plain-list-item) (boolean))))
  509. (defcustom org-insert-heading-hook nil
  510. "Hook being run after inserting a new heading."
  511. :group 'org-edit-structure
  512. :type 'hook)
  513. (defcustom org-enable-fixed-width-editor t
  514. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  515. This currently only means, they are never auto-wrapped.
  516. When nil, such lines will be treated like ordinary lines.
  517. See also the QUOTE keyword."
  518. :group 'org-edit-structure
  519. :type 'boolean)
  520. (defcustom org-goto-auto-isearch t
  521. "Non-nil means, typing characters in org-goto starts incremental search."
  522. :group 'org-edit-structure
  523. :type 'boolean)
  524. (defgroup org-sparse-trees nil
  525. "Options concerning sparse trees in Org-mode."
  526. :tag "Org Sparse Trees"
  527. :group 'org-structure)
  528. (defcustom org-highlight-sparse-tree-matches t
  529. "Non-nil means, highlight all matches that define a sparse tree.
  530. The highlights will automatically disappear the next time the buffer is
  531. changed by an edit command."
  532. :group 'org-sparse-trees
  533. :type 'boolean)
  534. (defcustom org-remove-highlights-with-change t
  535. "Non-nil means, any change to the buffer will remove temporary highlights.
  536. Such highlights are created by `org-occur' and `org-clock-display'.
  537. When nil, `C-c C-c needs to be used to get rid of the highlights.
  538. The highlights created by `org-preview-latex-fragment' always need
  539. `C-c C-c' to be removed."
  540. :group 'org-sparse-trees
  541. :group 'org-time
  542. :type 'boolean)
  543. (defcustom org-occur-hook '(org-first-headline-recenter)
  544. "Hook that is run after `org-occur' has constructed a sparse tree.
  545. This can be used to recenter the window to show as much of the structure
  546. as possible."
  547. :group 'org-sparse-trees
  548. :type 'hook)
  549. (defgroup org-plain-lists nil
  550. "Options concerning plain lists in Org-mode."
  551. :tag "Org Plain lists"
  552. :group 'org-structure)
  553. (defcustom org-cycle-include-plain-lists nil
  554. "Non-nil means, include plain lists into visibility cycling.
  555. This means that during cycling, plain list items will *temporarily* be
  556. interpreted as outline headlines with a level given by 1000+i where i is the
  557. indentation of the bullet. In all other operations, plain list items are
  558. not seen as headlines. For example, you cannot assign a TODO keyword to
  559. such an item."
  560. :group 'org-plain-lists
  561. :type 'boolean)
  562. (defcustom org-plain-list-ordered-item-terminator t
  563. "The character that makes a line with leading number an ordered list item.
  564. Valid values are ?. and ?\). To get both terminators, use t. While
  565. ?. may look nicer, it creates the danger that a line with leading
  566. number may be incorrectly interpreted as an item. ?\) therefore is
  567. the safe choice."
  568. :group 'org-plain-lists
  569. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  570. (const :tag "paren like in \"2)\"" ?\))
  571. (const :tab "both" t)))
  572. (defcustom org-auto-renumber-ordered-lists t
  573. "Non-nil means, automatically renumber ordered plain lists.
  574. Renumbering happens when the sequence have been changed with
  575. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  576. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  577. :group 'org-plain-lists
  578. :type 'boolean)
  579. (defcustom org-provide-checkbox-statistics t
  580. "Non-nil means, update checkbox statistics after insert and toggle.
  581. When this is set, checkbox statistics is updated each time you either insert
  582. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  583. with \\[org-ctrl-c-ctrl-c\\]."
  584. :group 'org-plain-lists
  585. :type 'boolean)
  586. (defgroup org-archive nil
  587. "Options concerning archiving in Org-mode."
  588. :tag "Org Archive"
  589. :group 'org-structure)
  590. (defcustom org-archive-tag "ARCHIVE"
  591. "The tag that marks a subtree as archived.
  592. An archived subtree does not open during visibility cycling, and does
  593. not contribute to the agenda listings.
  594. After changing this, font-lock must be restarted in the relevant buffers to
  595. get the proper fontification."
  596. :group 'org-archive
  597. :group 'org-keywords
  598. :type 'string)
  599. (defcustom org-agenda-skip-archived-trees t
  600. "Non-nil means, the agenda will skip any items located in archived trees.
  601. An archived tree is a tree marked with the tag ARCHIVE."
  602. :group 'org-archive
  603. :group 'org-agenda-skip
  604. :type 'boolean)
  605. (defcustom org-cycle-open-archived-trees nil
  606. "Non-nil means, `org-cycle' will open archived trees.
  607. An archived tree is a tree marked with the tag ARCHIVE.
  608. When nil, archived trees will stay folded. You can still open them with
  609. normal outline commands like `show-all', but not with the cycling commands."
  610. :group 'org-archive
  611. :group 'org-cycle
  612. :type 'boolean)
  613. (defcustom org-sparse-tree-open-archived-trees nil
  614. "Non-nil means sparse tree construction shows matches in archived trees.
  615. When nil, matches in these trees are highlighted, but the trees are kept in
  616. collapsed state."
  617. :group 'org-archive
  618. :group 'org-sparse-trees
  619. :type 'boolean)
  620. (defcustom org-archive-location "%s_archive::"
  621. "The location where subtrees should be archived.
  622. This string consists of two parts, separated by a double-colon.
  623. The first part is a file name - when omitted, archiving happens in the same
  624. file. %s will be replaced by the current file name (without directory part).
  625. Archiving to a different file is useful to keep archived entries from
  626. contributing to the Org-mode Agenda.
  627. The part after the double colon is a headline. The archived entries will be
  628. filed under that headline. When omitted, the subtrees are simply filed away
  629. at the end of the file, as top-level entries.
  630. Here are a few examples:
  631. \"%s_archive::\"
  632. If the current file is Projects.org, archive in file
  633. Projects.org_archive, as top-level trees. This is the default.
  634. \"::* Archived Tasks\"
  635. Archive in the current file, under the top-level headline
  636. \"* Archived Tasks\".
  637. \"~/org/archive.org::\"
  638. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  639. \"basement::** Finished Tasks\"
  640. Archive in file ./basement (relative path), as level 3 trees
  641. below the level 2 heading \"** Finished Tasks\".
  642. You may set this option on a per-file basis by adding to the buffer a
  643. line like
  644. #+ARCHIVE: basement::** Finished Tasks"
  645. :group 'org-archive
  646. :type 'string)
  647. (defcustom org-archive-mark-done t
  648. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  649. This can be a string to set the keyword to use. When t, Org-mode will
  650. use the first keyword in its list that means done."
  651. :group 'org-archive
  652. :type '(choice
  653. (const :tag "No" nil)
  654. (const :tag "Yes" t)
  655. (string :tag "Use this keyword")))
  656. (defcustom org-archive-stamp-time t
  657. "Non-nil means, add a time stamp to entries moved to an archive file.
  658. This variable is obsolete and has no effect anymore, instead add ot remove
  659. `time' from the variablle `org-archive-save-context-info'."
  660. :group 'org-archive
  661. :type 'boolean)
  662. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  663. "Parts of context info that should be stored as properties when archiving.
  664. When a subtree is moved to an archive file, it looses information given by
  665. context, like inherited tags, the category, and possibly also the TODO
  666. state (depending on the variable `org-archive-mark-done').
  667. This variable can be a list of any of the following symbols:
  668. time The time of archiving.
  669. file The file where the entry originates.
  670. itags The local tags, in the headline of the subtree.
  671. ltags The tags the subtree inherits from further up the hierarchy.
  672. todo The pre-archive TODO state.
  673. category The category, taken from file name or #+CATEGORY lines.
  674. olpath The outline path to the item. These are all headlines above
  675. the current item, separated by /, like a file path.
  676. For each symbol present in the list, a property will be created in
  677. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  678. information."
  679. :group 'org-archive
  680. :type '(set :greedy t
  681. (const :tag "Time" time)
  682. (const :tag "File" file)
  683. (const :tag "Category" category)
  684. (const :tag "TODO state" todo)
  685. (const :tag "TODO state" priority)
  686. (const :tag "Inherited tags" itags)
  687. (const :tag "Outline path" olpath)
  688. (const :tag "Local tags" ltags)))
  689. (defgroup org-imenu-and-speedbar nil
  690. "Options concerning imenu and speedbar in Org-mode."
  691. :tag "Org Imenu and Speedbar"
  692. :group 'org-structure)
  693. (defcustom org-imenu-depth 2
  694. "The maximum level for Imenu access to Org-mode headlines.
  695. This also applied for speedbar access."
  696. :group 'org-imenu-and-speedbar
  697. :type 'number)
  698. (defgroup org-table nil
  699. "Options concerning tables in Org-mode."
  700. :tag "Org Table"
  701. :group 'org)
  702. (defcustom org-enable-table-editor 'optimized
  703. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  704. When nil, such lines will be treated like ordinary lines.
  705. When equal to the symbol `optimized', the table editor will be optimized to
  706. do the following:
  707. - Automatic overwrite mode in front of whitespace in table fields.
  708. This makes the structure of the table stay in tact as long as the edited
  709. field does not exceed the column width.
  710. - Minimize the number of realigns. Normally, the table is aligned each time
  711. TAB or RET are pressed to move to another field. With optimization this
  712. happens only if changes to a field might have changed the column width.
  713. Optimization requires replacing the functions `self-insert-command',
  714. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  715. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  716. very good at guessing when a re-align will be necessary, but you can always
  717. force one with \\[org-ctrl-c-ctrl-c].
  718. If you would like to use the optimized version in Org-mode, but the
  719. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  720. This variable can be used to turn on and off the table editor during a session,
  721. but in order to toggle optimization, a restart is required.
  722. See also the variable `org-table-auto-blank-field'."
  723. :group 'org-table
  724. :type '(choice
  725. (const :tag "off" nil)
  726. (const :tag "on" t)
  727. (const :tag "on, optimized" optimized)))
  728. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  729. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  730. In the optimized version, the table editor takes over all simple keys that
  731. normally just insert a character. In tables, the characters are inserted
  732. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  733. for empty fields). Outside tables, the correct binding of the keys is
  734. restored.
  735. The default for this option is t if the optimized version is also used in
  736. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  737. this variable requires a restart of Emacs to become effective."
  738. :group 'org-table
  739. :type 'boolean)
  740. (defcustom orgtbl-radio-table-templates
  741. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  742. % END RECEIVE ORGTBL %n
  743. \\begin{comment}
  744. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  745. | | |
  746. \\end{comment}\n")
  747. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  748. @c END RECEIVE ORGTBL %n
  749. @ignore
  750. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  751. | | |
  752. @end ignore\n")
  753. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  754. <!-- END RECEIVE ORGTBL %n -->
  755. <!--
  756. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  757. | | |
  758. -->\n"))
  759. "Templates for radio tables in different major modes.
  760. All occurrences of %n in a template will be replaced with the name of the
  761. table, obtained by prompting the user."
  762. :group 'org-table
  763. :type '(repeat
  764. (list (symbol :tag "Major mode")
  765. (string :tag "Format"))))
  766. (defgroup org-table-settings nil
  767. "Settings for tables in Org-mode."
  768. :tag "Org Table Settings"
  769. :group 'org-table)
  770. (defcustom org-table-default-size "5x2"
  771. "The default size for newly created tables, Columns x Rows."
  772. :group 'org-table-settings
  773. :type 'string)
  774. (defcustom org-table-number-regexp
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  776. "Regular expression for recognizing numbers in table columns.
  777. If a table column contains mostly numbers, it will be aligned to the
  778. right. If not, it will be aligned to the left.
  779. The default value of this option is a regular expression which allows
  780. anything which looks remotely like a number as used in scientific
  781. context. For example, all of the following will be considered a
  782. number:
  783. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  784. Other options offered by the customize interface are more restrictive."
  785. :group 'org-table-settings
  786. :type '(choice
  787. (const :tag "Positive Integers"
  788. "^[0-9]+$")
  789. (const :tag "Integers"
  790. "^[-+]?[0-9]+$")
  791. (const :tag "Floating Point Numbers"
  792. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  793. (const :tag "Floating Point Number or Integer"
  794. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  795. (const :tag "Exponential, Floating point, Integer"
  796. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  797. (const :tag "Very General Number-Like, including hex"
  798. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  799. (string :tag "Regexp:")))
  800. (defcustom org-table-number-fraction 0.5
  801. "Fraction of numbers in a column required to make the column align right.
  802. In a column all non-white fields are considered. If at least this
  803. fraction of fields is matched by `org-table-number-fraction',
  804. alignment to the right border applies."
  805. :group 'org-table-settings
  806. :type 'number)
  807. (defgroup org-table-editing nil
  808. "Behavior of tables during editing in Org-mode."
  809. :tag "Org Table Editing"
  810. :group 'org-table)
  811. (defcustom org-table-automatic-realign t
  812. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  813. When nil, aligning is only done with \\[org-table-align], or after column
  814. removal/insertion."
  815. :group 'org-table-editing
  816. :type 'boolean)
  817. (defcustom org-table-auto-blank-field t
  818. "Non-nil means, automatically blank table field when starting to type into it.
  819. This only happens when typing immediately after a field motion
  820. command (TAB, S-TAB or RET).
  821. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  822. :group 'org-table-editing
  823. :type 'boolean)
  824. (defcustom org-table-tab-jumps-over-hlines t
  825. "Non-nil means, tab in the last column of a table with jump over a hline.
  826. If a horizontal separator line is following the current line,
  827. `org-table-next-field' can either create a new row before that line, or jump
  828. over the line. When this option is nil, a new line will be created before
  829. this line."
  830. :group 'org-table-editing
  831. :type 'boolean)
  832. (defcustom org-table-tab-recognizes-table.el t
  833. "Non-nil means, TAB will automatically notice a table.el table.
  834. When it sees such a table, it moves point into it and - if necessary -
  835. calls `table-recognize-table'."
  836. :group 'org-table-editing
  837. :type 'boolean)
  838. (defgroup org-table-calculation nil
  839. "Options concerning tables in Org-mode."
  840. :tag "Org Table Calculation"
  841. :group 'org-table)
  842. (defcustom org-table-use-standard-references t
  843. "Should org-mode work with table refrences like B3 instead of @3$2?
  844. Possible values are:
  845. nil never use them
  846. from accept as input, do not present for editing
  847. t: accept as input and present for editing"
  848. :group 'org-table-calculation
  849. :type '(choice
  850. (const :tag "Never, don't even check unser input for them" nil)
  851. (const :tag "Always, both as user input, and when editing" t)
  852. (const :tag "Convert user input, don't offer during editing" 'from)))
  853. (defcustom org-table-copy-increment t
  854. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  855. :group 'org-table-calculation
  856. :type 'boolean)
  857. (defcustom org-calc-default-modes
  858. '(calc-internal-prec 12
  859. calc-float-format (float 5)
  860. calc-angle-mode deg
  861. calc-prefer-frac nil
  862. calc-symbolic-mode nil
  863. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  864. calc-display-working-message t
  865. )
  866. "List with Calc mode settings for use in calc-eval for table formulas.
  867. The list must contain alternating symbols (Calc modes variables and values).
  868. Don't remove any of the default settings, just change the values. Org-mode
  869. relies on the variables to be present in the list."
  870. :group 'org-table-calculation
  871. :type 'plist)
  872. (defcustom org-table-formula-evaluate-inline t
  873. "Non-nil means, TAB and RET evaluate a formula in current table field.
  874. If the current field starts with an equal sign, it is assumed to be a formula
  875. which should be evaluated as described in the manual and in the documentation
  876. string of the command `org-table-eval-formula'. This feature requires the
  877. Emacs calc package.
  878. When this variable is nil, formula calculation is only available through
  879. the command \\[org-table-eval-formula]."
  880. :group 'org-table-calculation
  881. :type 'boolean)
  882. (defcustom org-table-formula-use-constants t
  883. "Non-nil means, interpret constants in formulas in tables.
  884. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  885. by the value given in `org-table-formula-constants', or by a value obtained
  886. from the `constants.el' package."
  887. :group 'org-table-calculation
  888. :type 'boolean)
  889. (defcustom org-table-formula-constants nil
  890. "Alist with constant names and values, for use in table formulas.
  891. The car of each element is a name of a constant, without the `$' before it.
  892. The cdr is the value as a string. For example, if you'd like to use the
  893. speed of light in a formula, you would configure
  894. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  895. and then use it in an equation like `$1*$c'.
  896. Constants can also be defined on a per-file basis using a line like
  897. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  898. :group 'org-table-calculation
  899. :type '(repeat
  900. (cons (string :tag "name")
  901. (string :tag "value"))))
  902. (defvar org-table-formula-constants-local nil
  903. "Local version of `org-table-formula-constants'.")
  904. (make-variable-buffer-local 'org-table-formula-constants-local)
  905. (defcustom org-table-allow-automatic-line-recalculation t
  906. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  907. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  908. :group 'org-table-calculation
  909. :type 'boolean)
  910. (defgroup org-link nil
  911. "Options concerning links in Org-mode."
  912. :tag "Org Link"
  913. :group 'org)
  914. (defvar org-link-abbrev-alist-local nil
  915. "Buffer-local version of `org-link-abbrev-alist', which see.
  916. The value of this is taken from the #+LINK lines.")
  917. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  918. (defcustom org-link-abbrev-alist nil
  919. "Alist of link abbreviations.
  920. The car of each element is a string, to be replaced at the start of a link.
  921. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  922. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  923. [[linkkey:tag][description]]
  924. If REPLACE is a string, the tag will simply be appended to create the link.
  925. If the string contains \"%s\", the tag will be inserted there.
  926. REPLACE may also be a function that will be called with the tag as the
  927. only argument to create the link, which should be returned as a string.
  928. See the manual for examples."
  929. :group 'org-link
  930. :type 'alist)
  931. (defcustom org-descriptive-links t
  932. "Non-nil means, hide link part and only show description of bracket links.
  933. Bracket links are like [[link][descritpion]]. This variable sets the initial
  934. state in new org-mode buffers. The setting can then be toggled on a
  935. per-buffer basis from the Org->Hyperlinks menu."
  936. :group 'org-link
  937. :type 'boolean)
  938. (defcustom org-link-file-path-type 'adaptive
  939. "How the path name in file links should be stored.
  940. Valid values are:
  941. relative Relative to the current directory, i.e. the directory of the file
  942. into which the link is being inserted.
  943. absolute Absolute path, if possible with ~ for home directory.
  944. noabbrev Absolute path, no abbreviation of home directory.
  945. adaptive Use relative path for files in the current directory and sub-
  946. directories of it. For other files, use an absolute path."
  947. :group 'org-link
  948. :type '(choice
  949. (const relative)
  950. (const absolute)
  951. (const noabbrev)
  952. (const adaptive)))
  953. (defcustom org-activate-links '(bracket angle plain radio tag date)
  954. "Types of links that should be activated in Org-mode files.
  955. This is a list of symbols, each leading to the activation of a certain link
  956. type. In principle, it does not hurt to turn on most link types - there may
  957. be a small gain when turning off unused link types. The types are:
  958. bracket The recommended [[link][description]] or [[link]] links with hiding.
  959. angular Links in angular brackes that may contain whitespace like
  960. <bbdb:Carsten Dominik>.
  961. plain Plain links in normal text, no whitespace, like http://google.com.
  962. radio Text that is matched by a radio target, see manual for details.
  963. tag Tag settings in a headline (link to tag search).
  964. date Time stamps (link to calendar).
  965. Changing this variable requires a restart of Emacs to become effective."
  966. :group 'org-link
  967. :type '(set (const :tag "Double bracket links (new style)" bracket)
  968. (const :tag "Angular bracket links (old style)" angular)
  969. (const :tag "plain text links" plain)
  970. (const :tag "Radio target matches" radio)
  971. (const :tag "Tags" tag)
  972. (const :tag "Tags" target)
  973. (const :tag "Timestamps" date)))
  974. (defgroup org-link-store nil
  975. "Options concerning storing links in Org-mode"
  976. :tag "Org Store Link"
  977. :group 'org-link)
  978. (defcustom org-email-link-description-format "Email %c: %.30s"
  979. "Format of the description part of a link to an email or usenet message.
  980. The following %-excapes will be replaced by corresponding information:
  981. %F full \"From\" field
  982. %f name, taken from \"From\" field, address if no name
  983. %T full \"To\" field
  984. %t first name in \"To\" field, address if no name
  985. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  986. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  987. %s subject
  988. %m message-id.
  989. You may use normal field width specification between the % and the letter.
  990. This is for example useful to limit the length of the subject.
  991. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  992. :group 'org-link-store
  993. :type 'string)
  994. (defcustom org-from-is-user-regexp
  995. (let (r1 r2)
  996. (when (and user-mail-address (not (string= user-mail-address "")))
  997. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  998. (when (and user-full-name (not (string= user-full-name "")))
  999. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1000. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1001. "Regexp mached against the \"From:\" header of an email or usenet message.
  1002. It should match if the message is from the user him/herself."
  1003. :group 'org-link-store
  1004. :type 'regexp)
  1005. (defcustom org-context-in-file-links t
  1006. "Non-nil means, file links from `org-store-link' contain context.
  1007. A search string will be added to the file name with :: as separator and
  1008. used to find the context when the link is activated by the command
  1009. `org-open-at-point'.
  1010. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1011. negates this setting for the duration of the command."
  1012. :group 'org-link-store
  1013. :type 'boolean)
  1014. (defcustom org-keep-stored-link-after-insertion nil
  1015. "Non-nil means, keep link in list for entire session.
  1016. The command `org-store-link' adds a link pointing to the current
  1017. location to an internal list. These links accumulate during a session.
  1018. The command `org-insert-link' can be used to insert links into any
  1019. Org-mode file (offering completion for all stored links). When this
  1020. option is nil, every link which has been inserted once using \\[org-insert-link]
  1021. will be removed from the list, to make completing the unused links
  1022. more efficient."
  1023. :group 'org-link-store
  1024. :type 'boolean)
  1025. (defcustom org-usenet-links-prefer-google nil
  1026. "Non-nil means, `org-store-link' will create web links to Google groups.
  1027. When nil, Gnus will be used for such links.
  1028. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1029. negates this setting for the duration of the command."
  1030. :group 'org-link-store
  1031. :type 'boolean)
  1032. (defgroup org-link-follow nil
  1033. "Options concerning following links in Org-mode"
  1034. :tag "Org Follow Link"
  1035. :group 'org-link)
  1036. (defcustom org-tab-follows-link nil
  1037. "Non-nil means, on links TAB will follow the link.
  1038. Needs to be set before org.el is loaded."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-return-follows-link nil
  1042. "Non-nil means, on links RET will follow the link.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-mouse-1-follows-link t
  1047. "Non-nil means, mouse-1 on a link will follow the link.
  1048. A longer mouse click will still set point. Does not wortk on XEmacs.
  1049. Needs to be set before org.el is loaded."
  1050. :group 'org-link-follow
  1051. :type 'boolean)
  1052. (defcustom org-mark-ring-length 4
  1053. "Number of different positions to be recorded in the ring
  1054. Changing this requires a restart of Emacs to work correctly."
  1055. :group 'org-link-follow
  1056. :type 'interger)
  1057. (defcustom org-link-frame-setup
  1058. '((vm . vm-visit-folder-other-frame)
  1059. (gnus . gnus-other-frame)
  1060. (file . find-file-other-window))
  1061. "Setup the frame configuration for following links.
  1062. When following a link with Emacs, it may often be useful to display
  1063. this link in another window or frame. This variable can be used to
  1064. set this up for the different types of links.
  1065. For VM, use any of
  1066. `vm-visit-folder'
  1067. `vm-visit-folder-other-frame'
  1068. For Gnus, use any of
  1069. `gnus'
  1070. `gnus-other-frame'
  1071. For FILE, use any of
  1072. `find-file'
  1073. `find-file-other-window'
  1074. `find-file-other-frame'
  1075. For the calendar, use the variable `calendar-setup'.
  1076. For BBDB, it is currently only possible to display the matches in
  1077. another window."
  1078. :group 'org-link-follow
  1079. :type '(list
  1080. (cons (const vm)
  1081. (choice
  1082. (const vm-visit-folder)
  1083. (const vm-visit-folder-other-window)
  1084. (const vm-visit-folder-other-frame)))
  1085. (cons (const gnus)
  1086. (choice
  1087. (const gnus)
  1088. (const gnus-other-frame)))
  1089. (cons (const file)
  1090. (choice
  1091. (const find-file)
  1092. (const find-file-other-window)
  1093. (const find-file-other-frame)))))
  1094. (defcustom org-display-internal-link-with-indirect-buffer nil
  1095. "Non-nil means, use indirect buffer to display infile links.
  1096. Activating internal links (from one location in a file to another location
  1097. in the same file) normally just jumps to the location. When the link is
  1098. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1099. another window. When this option is set, the other window actually displays
  1100. an indirect buffer clone of the current buffer, to avoid any visibility
  1101. changes to the current buffer."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-open-non-existing-files nil
  1105. "Non-nil means, `org-open-file' will open non-existing files.
  1106. When nil, an error will be generated."
  1107. :group 'org-link-follow
  1108. :type 'boolean)
  1109. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1110. "Function and arguments to call for following mailto links.
  1111. This is a list with the first element being a lisp function, and the
  1112. remaining elements being arguments to the function. In string arguments,
  1113. %a will be replaced by the address, and %s will be replaced by the subject
  1114. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1115. :group 'org-link-follow
  1116. :type '(choice
  1117. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1118. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1119. (const :tag "message-mail" (message-mail "%a" "%s"))
  1120. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1121. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1122. "Non-nil means, ask for confirmation before executing shell links.
  1123. Shell links can be dangerous: just think about a link
  1124. [[shell:rm -rf ~/*][Google Search]]
  1125. This link would show up in your Org-mode document as \"Google Search\",
  1126. but really it would remove your entire home directory.
  1127. Therefore we advise against setting this variable to nil.
  1128. Just change it to `y-or-n-p' of you want to confirm with a
  1129. single keystroke rather than having to type \"yes\"."
  1130. :group 'org-link-follow
  1131. :type '(choice
  1132. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1133. (const :tag "with y-or-n (faster)" y-or-n-p)
  1134. (const :tag "no confirmation (dangerous)" nil)))
  1135. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1136. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1137. Elisp links can be dangerous: just think about a link
  1138. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1139. This link would show up in your Org-mode document as \"Google Search\",
  1140. but really it would remove your entire home directory.
  1141. Therefore we advise against setting this variable to nil.
  1142. Just change it to `y-or-n-p' of you want to confirm with a
  1143. single keystroke rather than having to type \"yes\"."
  1144. :group 'org-link-follow
  1145. :type '(choice
  1146. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1147. (const :tag "with y-or-n (faster)" y-or-n-p)
  1148. (const :tag "no confirmation (dangerous)" nil)))
  1149. (defconst org-file-apps-defaults-gnu
  1150. '((remote . emacs)
  1151. (t . mailcap))
  1152. "Default file applications on a UNIX or GNU/Linux system.
  1153. See `org-file-apps'.")
  1154. (defconst org-file-apps-defaults-macosx
  1155. '((remote . emacs)
  1156. (t . "open %s")
  1157. ("ps" . "gv %s")
  1158. ("ps.gz" . "gv %s")
  1159. ("eps" . "gv %s")
  1160. ("eps.gz" . "gv %s")
  1161. ("dvi" . "xdvi %s")
  1162. ("fig" . "xfig %s"))
  1163. "Default file applications on a MacOS X system.
  1164. The system \"open\" is known as a default, but we use X11 applications
  1165. for some files for which the OS does not have a good default.
  1166. See `org-file-apps'.")
  1167. (defconst org-file-apps-defaults-windowsnt
  1168. (list
  1169. '(remote . emacs)
  1170. (cons t
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. ("txt" . emacs)
  1181. ("tex" . emacs)
  1182. ("ltx" . emacs)
  1183. ("org" . emacs)
  1184. ("el" . emacs)
  1185. ("bib" . emacs)
  1186. )
  1187. "External applications for opening `file:path' items in a document.
  1188. Org-mode uses system defaults for different file types, but
  1189. you can use this variable to set the application for a given file
  1190. extension. The entries in this list are cons cells where the car identifies
  1191. files and the cdr the corresponding command. Possible values for the
  1192. file identifier are
  1193. \"ext\" A string identifying an extension
  1194. `directory' Matches a directory
  1195. `remote' Matches a remote file, accessible through tramp or efs.
  1196. Remote files most likely should be visited through Emacs
  1197. because external applications cannot handle such paths.
  1198. t Default for all remaining files
  1199. Possible values for the command are:
  1200. `emacs' The file will be visited by the current Emacs process.
  1201. `default' Use the default application for this file type.
  1202. string A command to be executed by a shell; %s will be replaced
  1203. by the path to the file.
  1204. sexp A Lisp form which will be evaluated. The file path will
  1205. be available in the Lisp variable `file'.
  1206. For more examples, see the system specific constants
  1207. `org-file-apps-defaults-macosx'
  1208. `org-file-apps-defaults-windowsnt'
  1209. `org-file-apps-defaults-gnu'."
  1210. :group 'org-link-follow
  1211. :type '(repeat
  1212. (cons (choice :value ""
  1213. (string :tag "Extension")
  1214. (const :tag "Default for unrecognized files" t)
  1215. (const :tag "Remote file" remote)
  1216. (const :tag "Links to a directory" directory))
  1217. (choice :value ""
  1218. (const :tag "Visit with Emacs" emacs)
  1219. (const :tag "Use system default" default)
  1220. (string :tag "Command")
  1221. (sexp :tag "Lisp form")))))
  1222. (defcustom org-mhe-search-all-folders nil
  1223. "Non-nil means, that the search for the mh-message will be extended to
  1224. all folders if the message cannot be found in the folder given in the link.
  1225. Searching all folders is very efficient with one of the search engines
  1226. supported by MH-E, but will be slow with pick."
  1227. :group 'org-link-follow
  1228. :type 'boolean)
  1229. (defgroup org-remember nil
  1230. "Options concerning interaction with remember.el."
  1231. :tag "Org Remember"
  1232. :group 'org)
  1233. (defcustom org-directory "~/org"
  1234. "Directory with org files.
  1235. This directory will be used as default to prompt for org files.
  1236. Used by the hooks for remember.el."
  1237. :group 'org-remember
  1238. :type 'directory)
  1239. (defcustom org-default-notes-file "~/.notes"
  1240. "Default target for storing notes.
  1241. Used by the hooks for remember.el. This can be a string, or nil to mean
  1242. the value of `remember-data-file'.
  1243. You can set this on a per-template basis with the variable
  1244. `org-remember-templates'."
  1245. :group 'org-remember
  1246. :type '(choice
  1247. (const :tag "Default from remember-data-file" nil)
  1248. file))
  1249. (defcustom org-remember-store-without-prompt t
  1250. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1251. In this case, you need `C-u C-c C-c' to get the prompts for
  1252. note file and headline.
  1253. When this variable is nil, `C-c C-c' give you the prompts, and
  1254. `C-u C-c C-c' trigger the fasttrack."
  1255. :group 'org-remember
  1256. :type 'boolean)
  1257. (defcustom org-remember-interactive-interface 'refile
  1258. "The interface to be used for interactive filing of remember notes.
  1259. This is only used when the interactive mode for selecting a filing
  1260. location is used (see the variable `org-remember-store-without-prompt').
  1261. Allowed vaues are:
  1262. outline The interface shows an outline of the relevant file
  1263. and the correct heading is found by moving through
  1264. the outline or by searching with incremental search.
  1265. outline-path-completion Headlines in the current buffer are offered via
  1266. completion.
  1267. refile Use the refile interface, and offer headlines,
  1268. possibly from different buffers."
  1269. :group 'org-remember
  1270. :type '(choice
  1271. (const :tag "Refile" refile)
  1272. (const :tag "Outline" outline)
  1273. (const :tag "Outline-path-completion" outline-path-completion)))
  1274. (defcustom org-goto-interface 'outline
  1275. "The default interface to be used for `org-goto'.
  1276. Allowed vaues are:
  1277. outline The interface shows an outline of the relevant file
  1278. and the correct heading is found by moving through
  1279. the outline or by searching with incremental search.
  1280. outline-path-completion Headlines in the current buffer are offered via
  1281. completion."
  1282. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1283. :type '(choice
  1284. (const :tag "Outline" outline)
  1285. (const :tag "Outline-path-completion" outline-path-completion)))
  1286. (defcustom org-remember-default-headline ""
  1287. "The headline that should be the default location in the notes file.
  1288. When filing remember notes, the cursor will start at that position.
  1289. You can set this on a per-template basis with the variable
  1290. `org-remember-templates'."
  1291. :group 'org-remember
  1292. :type 'string)
  1293. (defcustom org-remember-templates nil
  1294. "Templates for the creation of remember buffers.
  1295. When nil, just let remember make the buffer.
  1296. When not nil, this is a list of 5-element lists. In each entry, the first
  1297. element is the name of the template, which should be a single short word.
  1298. The second element is a character, a unique key to select this template.
  1299. The third element is the template. The fourth element is optional and can
  1300. specify a destination file for remember items created with this template.
  1301. The default file is given by `org-default-notes-file'. An optional fifth
  1302. element can specify the headline in that file that should be offered
  1303. first when the user is asked to file the entry. The default headline is
  1304. given in the variable `org-remember-default-headline'.
  1305. The template specifies the structure of the remember buffer. It should have
  1306. a first line starting with a star, to act as the org-mode headline.
  1307. Furthermore, the following %-escapes will be replaced with content:
  1308. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1309. A default value and a completion table ca be specified like this:
  1310. %^{prompt|default|completion2|completion3|...}
  1311. %t time stamp, date only
  1312. %T time stamp with date and time
  1313. %u, %U like the above, but inactive time stamps
  1314. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1315. You may define a prompt like %^{Please specify birthday}t
  1316. %n user name (taken from `user-full-name')
  1317. %a annotation, normally the link created with org-store-link
  1318. %i initial content, the region when remember is called with C-u.
  1319. If %i is indented, the entire inserted text will be indented
  1320. as well.
  1321. %c content of the clipboard, or current kill ring head
  1322. %^g prompt for tags, with completion on tags in target file
  1323. %^G prompt for tags, with completion all tags in all agenda files
  1324. %:keyword specific information for certain link types, see below
  1325. %[pathname] insert the contents of the file given by `pathname'
  1326. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1327. %! Store this note immediately after filling the template
  1328. %? After completing the template, position cursor here.
  1329. Apart from these general escapes, you can access information specific to the
  1330. link type that is created. For example, calling `remember' in emails or gnus
  1331. will record the author and the subject of the message, which you can access
  1332. with %:author and %:subject, respectively. Here is a complete list of what
  1333. is recorded for each link type.
  1334. Link type | Available information
  1335. -------------------+------------------------------------------------------
  1336. bbdb | %:type %:name %:company
  1337. vm, wl, mh, rmail | %:type %:subject %:message-id
  1338. | %:from %:fromname %:fromaddress
  1339. | %:to %:toname %:toaddress
  1340. | %:fromto (either \"to NAME\" or \"from NAME\")
  1341. gnus | %:group, for messages also all email fields
  1342. w3, w3m | %:type %:url
  1343. info | %:type %:file %:node
  1344. calendar | %:type %:date"
  1345. :group 'org-remember
  1346. :get (lambda (var) ; Make sure all entries have 5 elements
  1347. (mapcar (lambda (x)
  1348. (if (not (stringp (car x))) (setq x (cons "" x)))
  1349. (cond ((= (length x) 4) (append x '("")))
  1350. ((= (length x) 3) (append x '("" "")))
  1351. (t x)))
  1352. (default-value var)))
  1353. :type '(repeat
  1354. :tag "enabled"
  1355. (list :value ("" ?a "\n" nil nil)
  1356. (string :tag "Name")
  1357. (character :tag "Selection Key")
  1358. (string :tag "Template")
  1359. (choice
  1360. (file :tag "Destination file")
  1361. (const :tag "Prompt for file" nil))
  1362. (choice
  1363. (string :tag "Destination headline")
  1364. (const :tag "Selection interface for heading")))))
  1365. (defcustom org-reverse-note-order nil
  1366. "Non-nil means, store new notes at the beginning of a file or entry.
  1367. When nil, new notes will be filed to the end of a file or entry.
  1368. This can also be a list with cons cells of regular expressions that
  1369. are matched against file names, and values."
  1370. :group 'org-remember
  1371. :type '(choice
  1372. (const :tag "Reverse always" t)
  1373. (const :tag "Reverse never" nil)
  1374. (repeat :tag "By file name regexp"
  1375. (cons regexp boolean))))
  1376. (defcustom org-refile-targets nil
  1377. "Targets for refiling entries with \\[org-refile].
  1378. This is list of cons cells. Each cell contains:
  1379. - a specification of the files to be considered, either a list of files,
  1380. or a symbol whose function or value fields will be used to retrieve
  1381. a file name or a list of file names. Nil means, refile to a different
  1382. heading in the current buffer.
  1383. - A specification of how to find candidate refile targets. This may be
  1384. any of
  1385. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1386. This tag has to be present in all target headlines, inheritance will
  1387. not be considered.
  1388. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1389. todo keyword.
  1390. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1391. headlines that are refiling targets.
  1392. - a cons cell (:level . N). Any headline of level N is considered a target.
  1393. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1394. ;; FIXME: what if there are a var and func with same name???
  1395. :group 'org-remember
  1396. :type '(repeat
  1397. (cons
  1398. (choice :value org-agenda-files
  1399. (const :tag "All agenda files" org-agenda-files)
  1400. (const :tag "Current buffer" nil)
  1401. (function) (variable) (file))
  1402. (choice :tag "Identify target headline by"
  1403. (cons :tag "Specific tag" (const :tag) (string))
  1404. (cons :tag "TODO keyword" (const :todo) (string))
  1405. (cons :tag "Regular expression" (const :regexp) (regexp))
  1406. (cons :tag "Level number" (const :level) (integer))
  1407. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1408. (defcustom org-refile-use-outline-path nil
  1409. "Non-nil means, provide refile targets as paths.
  1410. So a level 3 headline will be available as level1/level2/level3.
  1411. When the value is `file', also include the file name (without directory)
  1412. into the path. When `full-file-path', include the full file path."
  1413. :group 'org-remember
  1414. :type '(choice
  1415. (const :tag "Not" nil)
  1416. (const :tag "Yes" t)
  1417. (const :tag "Start with file name" file)
  1418. (const :tag "Start with full file path" full-file-path)))
  1419. (defgroup org-todo nil
  1420. "Options concerning TODO items in Org-mode."
  1421. :tag "Org TODO"
  1422. :group 'org)
  1423. (defgroup org-progress nil
  1424. "Options concerning Progress logging in Org-mode."
  1425. :tag "Org Progress"
  1426. :group 'org-time)
  1427. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1428. "List of TODO entry keyword sequences and their interpretation.
  1429. \\<org-mode-map>This is a list of sequences.
  1430. Each sequence starts with a symbol, either `sequence' or `type',
  1431. indicating if the keywords should be interpreted as a sequence of
  1432. action steps, or as different types of TODO items. The first
  1433. keywords are states requiring action - these states will select a headline
  1434. for inclusion into the global TODO list Org-mode produces. If one of
  1435. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1436. signify that no further action is necessary. If \"|\" is not found,
  1437. the last keyword is treated as the only DONE state of the sequence.
  1438. The command \\[org-todo] cycles an entry through these states, and one
  1439. additional state where no keyword is present. For details about this
  1440. cycling, see the manual.
  1441. TODO keywords and interpretation can also be set on a per-file basis with
  1442. the special #+SEQ_TODO and #+TYP_TODO lines.
  1443. For backward compatibility, this variable may also be just a list
  1444. of keywords - in this case the interptetation (sequence or type) will be
  1445. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1446. :group 'org-todo
  1447. :group 'org-keywords
  1448. :type '(choice
  1449. (repeat :tag "Old syntax, just keywords"
  1450. (string :tag "Keyword"))
  1451. (repeat :tag "New syntax"
  1452. (cons
  1453. (choice
  1454. :tag "Interpretation"
  1455. (const :tag "Sequence (cycling hits every state)" sequence)
  1456. (const :tag "Type (cycling directly to DONE)" type))
  1457. (repeat
  1458. (string :tag "Keyword"))))))
  1459. (defvar org-todo-keywords-1 nil)
  1460. (make-variable-buffer-local 'org-todo-keywords-1)
  1461. (defvar org-todo-keywords-for-agenda nil)
  1462. (defvar org-done-keywords-for-agenda nil)
  1463. (defvar org-not-done-keywords nil)
  1464. (make-variable-buffer-local 'org-not-done-keywords)
  1465. (defvar org-done-keywords nil)
  1466. (make-variable-buffer-local 'org-done-keywords)
  1467. (defvar org-todo-heads nil)
  1468. (make-variable-buffer-local 'org-todo-heads)
  1469. (defvar org-todo-sets nil)
  1470. (make-variable-buffer-local 'org-todo-sets)
  1471. (defvar org-todo-log-states nil)
  1472. (make-variable-buffer-local 'org-todo-log-states)
  1473. (defvar org-todo-kwd-alist nil)
  1474. (make-variable-buffer-local 'org-todo-kwd-alist)
  1475. (defvar org-todo-key-alist nil)
  1476. (make-variable-buffer-local 'org-todo-key-alist)
  1477. (defvar org-todo-key-trigger nil)
  1478. (make-variable-buffer-local 'org-todo-key-trigger)
  1479. (defcustom org-todo-interpretation 'sequence
  1480. "Controls how TODO keywords are interpreted.
  1481. This variable is in principle obsolete and is only used for
  1482. backward compatibility, if the interpretation of todo keywords is
  1483. not given already in `org-todo-keywords'. See that variable for
  1484. more information."
  1485. :group 'org-todo
  1486. :group 'org-keywords
  1487. :type '(choice (const sequence)
  1488. (const type)))
  1489. (defcustom org-use-fast-todo-selection 'prefix
  1490. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1491. This variable describes if and under what circumstances the cycling
  1492. mechanism for TODO keywords will be replaced by a single-key, direct
  1493. selection scheme.
  1494. When nil, fast selection is never used.
  1495. When the symbol `prefix', it will be used when `org-todo' is called with
  1496. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1497. in an agenda buffer.
  1498. When t, fast selection is used by default. In this case, the prefix
  1499. argument forces cycling instead.
  1500. In all cases, the special interface is only used if access keys have actually
  1501. been assigned by the user, i.e. if keywords in the configuration are followed
  1502. by a letter in parenthesis, like TODO(t)."
  1503. :group 'org-todo
  1504. :type '(choice
  1505. (const :tag "Never" nil)
  1506. (const :tag "By default" t)
  1507. (const :tag "Only with C-u C-c C-t" prefix)))
  1508. (defcustom org-after-todo-state-change-hook nil
  1509. "Hook which is run after the state of a TODO item was changed.
  1510. The new state (a string with a TODO keyword, or nil) is available in the
  1511. Lisp variable `state'."
  1512. :group 'org-todo
  1513. :type 'hook)
  1514. (defcustom org-log-done nil
  1515. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1516. When the state of an entry is changed from nothing or a DONE state to
  1517. a not-done TODO state, remove a previous closing date.
  1518. This can also be a list of symbols indicating under which conditions
  1519. the time stamp recording the action should be annotated with a short note.
  1520. Valid members of this list are
  1521. done Offer to record a note when marking entries done
  1522. state Offer to record a note whenever changing the TODO state
  1523. of an item. This is only relevant if TODO keywords are
  1524. interpreted as sequence, see variable `org-todo-interpretation'.
  1525. When `state' is set, this includes tracking `done'.
  1526. clock-out Offer to record a note when clocking out of an item.
  1527. A separate window will then pop up and allow you to type a note.
  1528. After finishing with C-c C-c, the note will be added directly after the
  1529. timestamp, as a plain list item. See also the variable
  1530. `org-log-note-headings'.
  1531. Logging can also be configured on a per-file basis by adding one of
  1532. the following lines anywhere in the buffer:
  1533. #+STARTUP: logdone
  1534. #+STARTUP: nologging
  1535. #+STARTUP: lognotedone
  1536. #+STARTUP: lognotestate
  1537. #+STARTUP: lognoteclock-out
  1538. You can have local logging settings for a subtree by setting the LOGGING
  1539. property to one or more of these keywords."
  1540. :group 'org-todo
  1541. :group 'org-progress
  1542. :type '(choice
  1543. (const :tag "off" nil)
  1544. (const :tag "on" t)
  1545. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1546. (const :tag "when item is marked DONE" done)
  1547. (const :tag "when TODO state changes" state)
  1548. (const :tag "when clocking out" clock-out))))
  1549. (defcustom org-log-done-with-time t
  1550. "Non-nil means, the CLOSED time stamp will contain date and time.
  1551. When nil, only the date will be recorded."
  1552. :group 'org-progress
  1553. :type 'boolean)
  1554. (defcustom org-log-note-headings
  1555. '((done . "CLOSING NOTE %t")
  1556. (state . "State %-12s %t")
  1557. (clock-out . ""))
  1558. "Headings for notes added when clocking out or closing TODO items.
  1559. The value is an alist, with the car being a symbol indicating the note
  1560. context, and the cdr is the heading to be used. The heading may also be the
  1561. empty string.
  1562. %t in the heading will be replaced by a time stamp.
  1563. %s will be replaced by the new TODO state, in double quotes.
  1564. %u will be replaced by the user name.
  1565. %U will be replaced by the full user name."
  1566. :group 'org-todo
  1567. :group 'org-progress
  1568. :type '(list :greedy t
  1569. (cons (const :tag "Heading when closing an item" done) string)
  1570. (cons (const :tag
  1571. "Heading when changing todo state (todo sequence only)"
  1572. state) string)
  1573. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1574. (defcustom org-log-states-order-reversed t
  1575. "Non-nil means, the latest state change note will be directly after heading.
  1576. When nil, the notes will be orderer according to time."
  1577. :group 'org-todo
  1578. :group 'org-progress
  1579. :type 'boolean)
  1580. (defcustom org-log-repeat t
  1581. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1582. When nil, no note will be taken.
  1583. This option can also be set with on a per-file-basis with
  1584. #+STARTUP: logrepeat
  1585. #+STARTUP: nologrepeat
  1586. You can have local logging settings for a subtree by setting the LOGGING
  1587. property to one or more of these keywords."
  1588. :group 'org-todo
  1589. :group 'org-progress
  1590. :type 'boolean)
  1591. (defcustom org-clock-into-drawer 2
  1592. "Should clocking info be wrapped into a drawer?
  1593. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1594. If necessary, the drawer will be created.
  1595. When nil, the drawer will not be created, but used when present.
  1596. When an integer and the number of clocking entries in an item
  1597. reaches or exceeds this number, a drawer will be created."
  1598. :group 'org-todo
  1599. :group 'org-progress
  1600. :type '(choice
  1601. (const :tag "Always" t)
  1602. (const :tag "Only when drawer exists" nil)
  1603. (integer :tag "When at least N clock entries")))
  1604. (defcustom org-clock-out-when-done t
  1605. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1606. Nil means, clock will keep running until stopped explicitly with
  1607. `C-c C-x C-o', or until the clock is started in a different item."
  1608. :group 'org-progress
  1609. :type 'boolean)
  1610. (defcustom org-clock-in-switch-to-state nil
  1611. "Set task to a special todo state while clocking it.
  1612. The value should be the state to which the entry should be switched."
  1613. :group 'org-progress
  1614. :group 'org-todo
  1615. :type '(choice
  1616. (const :tag "Don't force a state" nil)
  1617. (string :tag "State")))
  1618. (defgroup org-priorities nil
  1619. "Priorities in Org-mode."
  1620. :tag "Org Priorities"
  1621. :group 'org-todo)
  1622. (defcustom org-highest-priority ?A
  1623. "The highest priority of TODO items. A character like ?A, ?B etc.
  1624. Must have a smaller ASCII number than `org-lowest-priority'."
  1625. :group 'org-priorities
  1626. :type 'character)
  1627. (defcustom org-lowest-priority ?C
  1628. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1629. Must have a larger ASCII number than `org-highest-priority'."
  1630. :group 'org-priorities
  1631. :type 'character)
  1632. (defcustom org-default-priority ?B
  1633. "The default priority of TODO items.
  1634. This is the priority an item get if no explicit priority is given."
  1635. :group 'org-priorities
  1636. :type 'character)
  1637. (defcustom org-priority-start-cycle-with-default t
  1638. "Non-nil means, start with default priority when starting to cycle.
  1639. When this is nil, the first step in the cycle will be (depending on the
  1640. command used) one higher or lower that the default priority."
  1641. :group 'org-priorities
  1642. :type 'boolean)
  1643. (defgroup org-time nil
  1644. "Options concerning time stamps and deadlines in Org-mode."
  1645. :tag "Org Time"
  1646. :group 'org)
  1647. (defcustom org-insert-labeled-timestamps-at-point nil
  1648. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1649. When nil, these labeled time stamps are forces into the second line of an
  1650. entry, just after the headline. When scheduling from the global TODO list,
  1651. the time stamp will always be forced into the second line."
  1652. :group 'org-time
  1653. :type 'boolean)
  1654. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1655. "Formats for `format-time-string' which are used for time stamps.
  1656. It is not recommended to change this constant.")
  1657. (defcustom org-time-stamp-rounding-minutes 0
  1658. "Number of minutes to round time stamps to upon insertion.
  1659. When zero, insert the time unmodified. Useful rounding numbers
  1660. should be factors of 60, so for example 5, 10, 15.
  1661. When this is not zero, you can still force an exact time-stamp by using
  1662. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1663. :group 'org-time
  1664. :type 'integer)
  1665. (defcustom org-display-custom-times nil
  1666. "Non-nil means, overlay custom formats over all time stamps.
  1667. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1668. To turn this on on a per-file basis, insert anywhere in the file:
  1669. #+STARTUP: customtime"
  1670. :group 'org-time
  1671. :set 'set-default
  1672. :type 'sexp)
  1673. (make-variable-buffer-local 'org-display-custom-times)
  1674. (defcustom org-time-stamp-custom-formats
  1675. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1676. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1677. These are overlayed over the default ISO format if the variable
  1678. `org-display-custom-times' is set. Time like %H:%M should be at the
  1679. end of the second format."
  1680. :group 'org-time
  1681. :type 'sexp)
  1682. (defun org-time-stamp-format (&optional long inactive)
  1683. "Get the right format for a time string."
  1684. (let ((f (if long (cdr org-time-stamp-formats)
  1685. (car org-time-stamp-formats))))
  1686. (if inactive
  1687. (concat "[" (substring f 1 -1) "]")
  1688. f)))
  1689. (defcustom org-read-date-prefer-future t
  1690. "Non-nil means, assume future for incomplete date input from user.
  1691. This affects the following situations:
  1692. 1. The user gives a day, but no month.
  1693. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1694. read this as the third of *next* month. However, if you enter \"17\",
  1695. it will be considered as *this* month.
  1696. 2. The user gives a month but not a year.
  1697. For example, if it is april and you enter \"feb 2\", this will be read
  1698. as feb 2, *next* year. \"May 5\", however, will be this year.
  1699. When this option is nil, the current month and year will always be used
  1700. as defaults."
  1701. :group 'org-time
  1702. :type 'boolean)
  1703. (defcustom org-read-date-display-live t
  1704. "Non-nil means, display current interpretation of date prompt live.
  1705. This display will be in an overlay, in the minibuffer."
  1706. :group 'org-time
  1707. :type 'boolean)
  1708. (defcustom org-read-date-popup-calendar t
  1709. "Non-nil means, pop up a calendar when prompting for a date.
  1710. In the calendar, the date can be selected with mouse-1. However, the
  1711. minibuffer will also be active, and you can simply enter the date as well.
  1712. When nil, only the minibuffer will be available."
  1713. :group 'org-time
  1714. :type 'boolean)
  1715. (if (fboundp 'defvaralias)
  1716. (defvaralias 'org-popup-calendar-for-date-prompt
  1717. 'org-read-date-popup-calendar))
  1718. (defcustom org-extend-today-until 0
  1719. "The hour when your day really ends.
  1720. This has influence for the following applications:
  1721. - When switching the agenda to \"today\". It it is still earlier than
  1722. the time given here, the day recognized as TODAY is actually yesterday.
  1723. - When a date is read from the user and it is still before the time given
  1724. here, the current date and time will be assumed to be yesterday, 23:59.
  1725. FIXME:
  1726. IMPORTANT: This is still a very experimental feature, it may disappear
  1727. again or it may be extended to mean more things."
  1728. :group 'org-time
  1729. :type 'number)
  1730. (defcustom org-edit-timestamp-down-means-later nil
  1731. "Non-nil means, S-down will increase the time in a time stamp.
  1732. When nil, S-up will increase."
  1733. :group 'org-time
  1734. :type 'boolean)
  1735. (defcustom org-calendar-follow-timestamp-change t
  1736. "Non-nil means, make the calendar window follow timestamp changes.
  1737. When a timestamp is modified and the calendar window is visible, it will be
  1738. moved to the new date."
  1739. :group 'org-time
  1740. :type 'boolean)
  1741. (defcustom org-clock-heading-function nil
  1742. "When non-nil, should be a function to create `org-clock-heading'.
  1743. This is the string shown in the mode line when a clock is running.
  1744. The function is called with point at the beginning of the headline."
  1745. :group 'org-time ; FIXME: Should we have a separate group????
  1746. :type 'function)
  1747. (defgroup org-tags nil
  1748. "Options concerning tags in Org-mode."
  1749. :tag "Org Tags"
  1750. :group 'org)
  1751. (defcustom org-tag-alist nil
  1752. "List of tags allowed in Org-mode files.
  1753. When this list is nil, Org-mode will base TAG input on what is already in the
  1754. buffer.
  1755. The value of this variable is an alist, the car of each entry must be a
  1756. keyword as a string, the cdr may be a character that is used to select
  1757. that tag through the fast-tag-selection interface.
  1758. See the manual for details."
  1759. :group 'org-tags
  1760. :type '(repeat
  1761. (choice
  1762. (cons (string :tag "Tag name")
  1763. (character :tag "Access char"))
  1764. (const :tag "Start radio group" (:startgroup))
  1765. (const :tag "End radio group" (:endgroup)))))
  1766. (defcustom org-use-fast-tag-selection 'auto
  1767. "Non-nil means, use fast tag selection scheme.
  1768. This is a special interface to select and deselect tags with single keys.
  1769. When nil, fast selection is never used.
  1770. When the symbol `auto', fast selection is used if and only if selection
  1771. characters for tags have been configured, either through the variable
  1772. `org-tag-alist' or through a #+TAGS line in the buffer.
  1773. When t, fast selection is always used and selection keys are assigned
  1774. automatically if necessary."
  1775. :group 'org-tags
  1776. :type '(choice
  1777. (const :tag "Always" t)
  1778. (const :tag "Never" nil)
  1779. (const :tag "When selection characters are configured" 'auto)))
  1780. (defcustom org-fast-tag-selection-single-key nil
  1781. "Non-nil means, fast tag selection exits after first change.
  1782. When nil, you have to press RET to exit it.
  1783. During fast tag selection, you can toggle this flag with `C-c'.
  1784. This variable can also have the value `expert'. In this case, the window
  1785. displaying the tags menu is not even shown, until you press C-c again."
  1786. :group 'org-tags
  1787. :type '(choice
  1788. (const :tag "No" nil)
  1789. (const :tag "Yes" t)
  1790. (const :tag "Expert" expert)))
  1791. (defvar org-fast-tag-selection-include-todo nil
  1792. "Non-nil means, fast tags selection interface will also offer TODO states.
  1793. This is an undocumented feature, you should not rely on it.")
  1794. (defcustom org-tags-column -80
  1795. "The column to which tags should be indented in a headline.
  1796. If this number is positive, it specifies the column. If it is negative,
  1797. it means that the tags should be flushright to that column. For example,
  1798. -80 works well for a normal 80 character screen."
  1799. :group 'org-tags
  1800. :type 'integer)
  1801. (defcustom org-auto-align-tags t
  1802. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1803. These operations change the length of a headline and therefore shift
  1804. the tags around. With this options turned on, after each such operation
  1805. the tags are again aligned to `org-tags-column'."
  1806. :group 'org-tags
  1807. :type 'boolean)
  1808. (defcustom org-use-tag-inheritance t
  1809. "Non-nil means, tags in levels apply also for sublevels.
  1810. When nil, only the tags directly given in a specific line apply there.
  1811. If you turn off this option, you very likely want to turn on the
  1812. companion option `org-tags-match-list-sublevels'."
  1813. :group 'org-tags
  1814. :type 'boolean)
  1815. (defcustom org-tags-match-list-sublevels nil
  1816. "Non-nil means list also sublevels of headlines matching tag search.
  1817. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1818. the sublevels of a headline matching a tag search often also match
  1819. the same search. Listing all of them can create very long lists.
  1820. Setting this variable to nil causes subtrees of a match to be skipped.
  1821. This option is off by default, because inheritance in on. If you turn
  1822. inheritance off, you very likely want to turn this option on.
  1823. As a special case, if the tag search is restricted to TODO items, the
  1824. value of this variable is ignored and sublevels are always checked, to
  1825. make sure all corresponding TODO items find their way into the list."
  1826. :group 'org-tags
  1827. :type 'boolean)
  1828. (defvar org-tags-history nil
  1829. "History of minibuffer reads for tags.")
  1830. (defvar org-last-tags-completion-table nil
  1831. "The last used completion table for tags.")
  1832. (defvar org-after-tags-change-hook nil
  1833. "Hook that is run after the tags in a line have changed.")
  1834. (defgroup org-properties nil
  1835. "Options concerning properties in Org-mode."
  1836. :tag "Org Properties"
  1837. :group 'org)
  1838. (defcustom org-property-format "%-10s %s"
  1839. "How property key/value pairs should be formatted by `indent-line'.
  1840. When `indent-line' hits a property definition, it will format the line
  1841. according to this format, mainly to make sure that the values are
  1842. lined-up with respect to each other."
  1843. :group 'org-properties
  1844. :type 'string)
  1845. (defcustom org-use-property-inheritance nil
  1846. "Non-nil means, properties apply also for sublevels.
  1847. This setting is only relevant during property searches, not when querying
  1848. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1849. you need to call `org-entry-get' with the inheritance flag.
  1850. Turning this on can cause significant overhead when doing a search, so
  1851. this is turned off by default.
  1852. When nil, only the properties directly given in the current entry count.
  1853. The value may also be a list of properties that shouldhave inheritance.
  1854. However, note that some special properties use inheritance under special
  1855. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1856. and the properties ending in \"_ALL\" when they are used as descriptor
  1857. for valid values of a property."
  1858. :group 'org-properties
  1859. :type '(choice
  1860. (const :tag "Not" nil)
  1861. (const :tag "Always" nil)
  1862. (repeat :tag "Specific properties" (string :tag "Property"))))
  1863. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1864. "The default column format, if no other format has been defined.
  1865. This variable can be set on the per-file basis by inserting a line
  1866. #+COLUMNS: %25ITEM ....."
  1867. :group 'org-properties
  1868. :type 'string)
  1869. (defcustom org-global-properties nil
  1870. "List of property/value pairs that can be inherited by any entry.
  1871. You can set buffer-local values for this by adding lines like
  1872. #+PROPERTY: NAME VALUE"
  1873. :group 'org-properties
  1874. :type '(repeat
  1875. (cons (string :tag "Property")
  1876. (string :tag "Value"))))
  1877. (defvar org-local-properties nil
  1878. "List of property/value pairs that can be inherited by any entry.
  1879. Valid for the current buffer.
  1880. This variable is populated from #+PROPERTY lines.")
  1881. (defgroup org-agenda nil
  1882. "Options concerning agenda views in Org-mode."
  1883. :tag "Org Agenda"
  1884. :group 'org)
  1885. (defvar org-category nil
  1886. "Variable used by org files to set a category for agenda display.
  1887. Such files should use a file variable to set it, for example
  1888. # -*- mode: org; org-category: \"ELisp\"
  1889. or contain a special line
  1890. #+CATEGORY: ELisp
  1891. If the file does not specify a category, then file's base name
  1892. is used instead.")
  1893. (make-variable-buffer-local 'org-category)
  1894. (defcustom org-agenda-files nil
  1895. "The files to be used for agenda display.
  1896. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1897. \\[org-remove-file]. You can also use customize to edit the list.
  1898. If an entry is a directory, all files in that directory that are matched by
  1899. `org-agenda-file-regexp' will be part of the file list.
  1900. If the value of the variable is not a list but a single file name, then
  1901. the list of agenda files is actually stored and maintained in that file, one
  1902. agenda file per line."
  1903. :group 'org-agenda
  1904. :type '(choice
  1905. (repeat :tag "List of files and directories" file)
  1906. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1907. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1908. "Regular expression to match files for `org-agenda-files'.
  1909. If any element in the list in that variable contains a directory instead
  1910. of a normal file, all files in that directory that are matched by this
  1911. regular expression will be included."
  1912. :group 'org-agenda
  1913. :type 'regexp)
  1914. (defcustom org-agenda-skip-unavailable-files nil
  1915. "t means to just skip non-reachable files in `org-agenda-files'.
  1916. Nil means to remove them, after a query, from the list."
  1917. :group 'org-agenda
  1918. :type 'boolean)
  1919. (defcustom org-agenda-multi-occur-extra-files nil
  1920. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1921. The files in `org-agenda-files' are always searched."
  1922. :group 'org-agenda
  1923. :type '(repeat file))
  1924. (defcustom org-agenda-confirm-kill 1
  1925. "When set, remote killing from the agenda buffer needs confirmation.
  1926. When t, a confirmation is always needed. When a number N, confirmation is
  1927. only needed when the text to be killed contains more than N non-white lines."
  1928. :group 'org-agenda
  1929. :type '(choice
  1930. (const :tag "Never" nil)
  1931. (const :tag "Always" t)
  1932. (number :tag "When more than N lines")))
  1933. (defcustom org-calendar-to-agenda-key [?c]
  1934. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1935. The command `org-calendar-goto-agenda' will be bound to this key. The
  1936. default is the character `c' because then `c' can be used to switch back and
  1937. forth between agenda and calendar."
  1938. :group 'org-agenda
  1939. :type 'sexp)
  1940. (defcustom org-agenda-compact-blocks nil
  1941. "Non-nil means, make the block agenda more compact.
  1942. This is done by leaving out unnecessary lines."
  1943. :group 'org-agenda
  1944. :type nil)
  1945. (defgroup org-agenda-export nil
  1946. "Options concerning exporting agenda views in Org-mode."
  1947. :tag "Org Agenda Export"
  1948. :group 'org-agenda)
  1949. (defcustom org-agenda-with-colors t
  1950. "Non-nil means, use colors in agenda views."
  1951. :group 'org-agenda-export
  1952. :type 'boolean)
  1953. (defcustom org-agenda-exporter-settings nil
  1954. "Alist of variable/value pairs that should be active during agenda export.
  1955. This is a good place to set uptions for ps-print and for htmlize."
  1956. :group 'org-agenda-export
  1957. :type '(repeat
  1958. (list
  1959. (variable)
  1960. (sexp :tag "Value"))))
  1961. (defcustom org-agenda-export-html-style ""
  1962. "The style specification for exported HTML Agenda files.
  1963. If this variable contains a string, it will replace the default <style>
  1964. section as produced by `htmlize'.
  1965. Since there are different ways of setting style information, this variable
  1966. needs to contain the full HTML structure to provide a style, including the
  1967. surrounding HTML tags. The style specifications should include definitions
  1968. the fonts used by the agenda, here is an example:
  1969. <style type=\"text/css\">
  1970. p { font-weight: normal; color: gray; }
  1971. .org-agenda-structure {
  1972. font-size: 110%;
  1973. color: #003399;
  1974. font-weight: 600;
  1975. }
  1976. .org-todo {
  1977. color: #cc6666;Week-agenda:
  1978. font-weight: bold;
  1979. }
  1980. .org-done {
  1981. color: #339933;
  1982. }
  1983. .title { text-align: center; }
  1984. .todo, .deadline { color: red; }
  1985. .done { color: green; }
  1986. </style>
  1987. or, if you want to keep the style in a file,
  1988. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1989. As the value of this option simply gets inserted into the HTML <head> header,
  1990. you can \"misuse\" it to also add other text to the header. However,
  1991. <style>...</style> is required, if not present the variable will be ignored."
  1992. :group 'org-agenda-export
  1993. :group 'org-export-html
  1994. :type 'string)
  1995. (defgroup org-agenda-custom-commands nil
  1996. "Options concerning agenda views in Org-mode."
  1997. :tag "Org Agenda Custom Commands"
  1998. :group 'org-agenda)
  1999. (defcustom org-agenda-custom-commands nil
  2000. "Custom commands for the agenda.
  2001. These commands will be offered on the splash screen displayed by the
  2002. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2003. (key desc type match options files)
  2004. key The key (one or more characters as a string) to be associated
  2005. with the command.
  2006. desc A description of the commend, when omitted or nil, a default
  2007. description is built using MATCH.
  2008. type The command type, any of the following symbols:
  2009. todo Entries with a specific TODO keyword, in all agenda files.
  2010. tags Tags match in all agenda files.
  2011. tags-todo Tags match in all agenda files, TODO entries only.
  2012. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2013. tags-tree Sparse tree with all tags matches in *current* file.
  2014. occur-tree Occur sparse tree for *current* file.
  2015. ... A user-defined function.
  2016. match What to search for:
  2017. - a single keyword for TODO keyword searches
  2018. - a tags match expression for tags searches
  2019. - a regular expression for occur searches
  2020. options A list of option settings, similar to that in a let form, so like
  2021. this: ((opt1 val1) (opt2 val2) ...)
  2022. files A list of files file to write the produced agenda buffer to
  2023. with the command `org-store-agenda-views'.
  2024. If a file name ends in \".html\", an HTML version of the buffer
  2025. is written out. If it ends in \".ps\", a postscript version is
  2026. produced. Otherwide, only the plain text is written to the file.
  2027. You can also define a set of commands, to create a composite agenda buffer.
  2028. In this case, an entry looks like this:
  2029. (key desc (cmd1 cmd2 ...) general-options file)
  2030. where
  2031. desc A description string to be displayed in the dispatcher menu.
  2032. cmd An agenda command, similar to the above. However, tree commands
  2033. are no allowed, but instead you can get agenda and global todo list.
  2034. So valid commands for a set are:
  2035. (agenda)
  2036. (alltodo)
  2037. (stuck)
  2038. (todo \"match\" options files)
  2039. (tags \"match\" options files)
  2040. (tags-todo \"match\" options files)
  2041. Each command can carry a list of options, and another set of options can be
  2042. given for the whole set of commands. Individual command options take
  2043. precedence over the general options.
  2044. When using several characters as key to a command, the first characters
  2045. are prefix commands. For the dispatcher to display useful information, you
  2046. should provide a description for the prefix, like
  2047. (setq org-agenda-custom-commands
  2048. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2049. (\"hl\" tags \"+HOME+Lisa\")
  2050. (\"hp\" tags \"+HOME+Peter\")
  2051. (\"hk\" tags \"+HOME+Kim\")))"
  2052. :group 'org-agenda-custom-commands
  2053. :type '(repeat
  2054. (choice :value ("a" "" tags "" nil)
  2055. (list :tag "Single command"
  2056. (string :tag "Access Key(s) ")
  2057. (option (string :tag "Description"))
  2058. (choice
  2059. (const :tag "Agenda" agenda)
  2060. (const :tag "TODO list" alltodo)
  2061. (const :tag "Stuck projects" stuck)
  2062. (const :tag "Tags search (all agenda files)" tags)
  2063. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2064. (const :tag "TODO keyword search (all agenda files)" todo)
  2065. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2066. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2067. (const :tag "Occur tree (current buffer)" occur-tree)
  2068. (sexp :tag "Other, user-defined function"))
  2069. (string :tag "Match")
  2070. (repeat :tag "Local options"
  2071. (list (variable :tag "Option") (sexp :tag "Value")))
  2072. (option (repeat :tag "Export" (file :tag "Export to"))))
  2073. (list :tag "Command series, all agenda files"
  2074. (string :tag "Access Key(s)")
  2075. (string :tag "Description ")
  2076. (repeat
  2077. (choice
  2078. (const :tag "Agenda" (agenda))
  2079. (const :tag "TODO list" (alltodo))
  2080. (const :tag "Stuck projects" (stuck))
  2081. (list :tag "Tags search"
  2082. (const :format "" tags)
  2083. (string :tag "Match")
  2084. (repeat :tag "Local options"
  2085. (list (variable :tag "Option")
  2086. (sexp :tag "Value"))))
  2087. (list :tag "Tags search, TODO entries only"
  2088. (const :format "" tags-todo)
  2089. (string :tag "Match")
  2090. (repeat :tag "Local options"
  2091. (list (variable :tag "Option")
  2092. (sexp :tag "Value"))))
  2093. (list :tag "TODO keyword search"
  2094. (const :format "" todo)
  2095. (string :tag "Match")
  2096. (repeat :tag "Local options"
  2097. (list (variable :tag "Option")
  2098. (sexp :tag "Value"))))
  2099. (list :tag "Other, user-defined function"
  2100. (symbol :tag "function")
  2101. (string :tag "Match")
  2102. (repeat :tag "Local options"
  2103. (list (variable :tag "Option")
  2104. (sexp :tag "Value"))))))
  2105. (repeat :tag "General options"
  2106. (list (variable :tag "Option")
  2107. (sexp :tag "Value")))
  2108. (option (repeat :tag "Export" (file :tag "Export to"))))
  2109. (cons :tag "Prefix key documentation"
  2110. (string :tag "Access Key(s)")
  2111. (string :tag "Description ")))))
  2112. (defcustom org-stuck-projects
  2113. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2114. "How to identify stuck projects.
  2115. This is a list of four items:
  2116. 1. A tags/todo matcher string that is used to identify a project.
  2117. The entire tree below a headline matched by this is considered one project.
  2118. 2. A list of TODO keywords identifying non-stuck projects.
  2119. If the project subtree contains any headline with one of these todo
  2120. keywords, the project is considered to be not stuck. If you specify
  2121. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2122. 3. A list of tags identifying non-stuck projects.
  2123. If the project subtree contains any headline with one of these tags,
  2124. the project is considered to be not stuck. If you specify \"*\" as
  2125. a tag, any tag will mark the project unstuck.
  2126. 4. An arbitrary regular expression matching non-stuck projects.
  2127. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2128. or `C-c a #' to produce the list."
  2129. :group 'org-agenda-custom-commands
  2130. :type '(list
  2131. (string :tag "Tags/TODO match to identify a project")
  2132. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2133. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2134. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2135. (defgroup org-agenda-skip nil
  2136. "Options concerning skipping parts of agenda files."
  2137. :tag "Org Agenda Skip"
  2138. :group 'org-agenda)
  2139. (defcustom org-agenda-todo-list-sublevels t
  2140. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2141. When nil, the sublevels of a TODO entry are not checked, resulting in
  2142. potentially much shorter TODO lists."
  2143. :group 'org-agenda-skip
  2144. :group 'org-todo
  2145. :type 'boolean)
  2146. (defcustom org-agenda-todo-ignore-with-date nil
  2147. "Non-nil means, don't show entries with a date in the global todo list.
  2148. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2149. but don't want them to show up in the TODO list.
  2150. When this is set, it also covers deadlines and scheduled items, the settings
  2151. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2152. will be ignored."
  2153. :group 'org-agenda-skip
  2154. :group 'org-todo
  2155. :type 'boolean)
  2156. (defcustom org-agenda-todo-ignore-scheduled nil
  2157. "Non-nil means, don't show scheduled entries in the global todo list.
  2158. The idea behind this is that by scheduling it, you have already taken care
  2159. of this item.
  2160. See also `org-agenda-todo-ignore-with-date'."
  2161. :group 'org-agenda-skip
  2162. :group 'org-todo
  2163. :type 'boolean)
  2164. (defcustom org-agenda-todo-ignore-deadlines nil
  2165. "Non-nil means, don't show near deadline entries in the global todo list.
  2166. Near means closer than `org-deadline-warning-days' days.
  2167. The idea behind this is that such items will appear in the agenda anyway.
  2168. See also `org-agenda-todo-ignore-with-date'."
  2169. :group 'org-agenda-skip
  2170. :group 'org-todo
  2171. :type 'boolean)
  2172. (defcustom org-agenda-skip-scheduled-if-done nil
  2173. "Non-nil means don't show scheduled items in agenda when they are done.
  2174. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2175. it applies only to the actual date of the scheduling. Warnings about
  2176. an item with a past scheduling dates are always turned off when the item
  2177. is DONE."
  2178. :group 'org-agenda-skip
  2179. :type 'boolean)
  2180. (defcustom org-agenda-skip-deadline-if-done nil
  2181. "Non-nil means don't show deadines when the corresponding item is done.
  2182. When nil, the deadline is still shown and should give you a happy feeling.
  2183. This is relevant for the daily/weekly agenda. And it applied only to the
  2184. actualy date of the deadline. Warnings about approching and past-due
  2185. deadlines are always turned off when the item is DONE."
  2186. :group 'org-agenda-skip
  2187. :type 'boolean)
  2188. (defcustom org-agenda-skip-timestamp-if-done nil
  2189. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2190. :group 'org-agenda-skip
  2191. :type 'boolean)
  2192. (defcustom org-timeline-show-empty-dates 3
  2193. "Non-nil means, `org-timeline' also shows dates without an entry.
  2194. When nil, only the days which actually have entries are shown.
  2195. When t, all days between the first and the last date are shown.
  2196. When an integer, show also empty dates, but if there is a gap of more than
  2197. N days, just insert a special line indicating the size of the gap."
  2198. :group 'org-agenda-skip
  2199. :type '(choice
  2200. (const :tag "None" nil)
  2201. (const :tag "All" t)
  2202. (number :tag "at most")))
  2203. (defgroup org-agenda-startup nil
  2204. "Options concerning initial settings in the Agenda in Org Mode."
  2205. :tag "Org Agenda Startup"
  2206. :group 'org-agenda)
  2207. (defcustom org-finalize-agenda-hook nil
  2208. "Hook run just before displaying an agenda buffer."
  2209. :group 'org-agenda-startup
  2210. :type 'hook)
  2211. (defcustom org-agenda-mouse-1-follows-link nil
  2212. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2213. A longer mouse click will still set point. Does not wortk on XEmacs.
  2214. Needs to be set before org.el is loaded."
  2215. :group 'org-agenda-startup
  2216. :type 'boolean)
  2217. (defcustom org-agenda-start-with-follow-mode nil
  2218. "The initial value of follow-mode in a newly created agenda window."
  2219. :group 'org-agenda-startup
  2220. :type 'boolean)
  2221. (defgroup org-agenda-windows nil
  2222. "Options concerning the windows used by the Agenda in Org Mode."
  2223. :tag "Org Agenda Windows"
  2224. :group 'org-agenda)
  2225. (defcustom org-agenda-window-setup 'reorganize-frame
  2226. "How the agenda buffer should be displayed.
  2227. Possible values for this option are:
  2228. current-window Show agenda in the current window, keeping all other windows.
  2229. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2230. other-window Use `switch-to-buffer-other-window' to display agenda.
  2231. reorganize-frame Show only two windows on the current frame, the current
  2232. window and the agenda.
  2233. See also the variable `org-agenda-restore-windows-after-quit'."
  2234. :group 'org-agenda-windows
  2235. :type '(choice
  2236. (const current-window)
  2237. (const other-frame)
  2238. (const other-window)
  2239. (const reorganize-frame)))
  2240. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2241. "The min and max height of the agenda window as a fraction of frame height.
  2242. The value of the variable is a cons cell with two numbers between 0 and 1.
  2243. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2244. :group 'org-agenda-windows
  2245. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2246. (defcustom org-agenda-restore-windows-after-quit nil
  2247. "Non-nil means, restore window configuration open exiting agenda.
  2248. Before the window configuration is changed for displaying the agenda,
  2249. the current status is recorded. When the agenda is exited with
  2250. `q' or `x' and this option is set, the old state is restored. If
  2251. `org-agenda-window-setup' is `other-frame', the value of this
  2252. option will be ignored.."
  2253. :group 'org-agenda-windows
  2254. :type 'boolean)
  2255. (defcustom org-indirect-buffer-display 'other-window
  2256. "How should indirect tree buffers be displayed?
  2257. This applies to indirect buffers created with the commands
  2258. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2259. Valid values are:
  2260. current-window Display in the current window
  2261. other-window Just display in another window.
  2262. dedicated-frame Create one new frame, and re-use it each time.
  2263. new-frame Make a new frame each time. Note that in this case
  2264. previously-made indirect buffers are kept, and you need to
  2265. kill these buffers yourself."
  2266. :group 'org-structure
  2267. :group 'org-agenda-windows
  2268. :type '(choice
  2269. (const :tag "In current window" current-window)
  2270. (const :tag "In current frame, other window" other-window)
  2271. (const :tag "Each time a new frame" new-frame)
  2272. (const :tag "One dedicated frame" dedicated-frame)))
  2273. (defgroup org-agenda-daily/weekly nil
  2274. "Options concerning the daily/weekly agenda."
  2275. :tag "Org Agenda Daily/Weekly"
  2276. :group 'org-agenda)
  2277. (defcustom org-agenda-ndays 7
  2278. "Number of days to include in overview display.
  2279. Should be 1 or 7."
  2280. :group 'org-agenda-daily/weekly
  2281. :type 'number)
  2282. (defcustom org-agenda-start-on-weekday 1
  2283. "Non-nil means, start the overview always on the specified weekday.
  2284. 0 denotes Sunday, 1 denotes Monday etc.
  2285. When nil, always start on the current day."
  2286. :group 'org-agenda-daily/weekly
  2287. :type '(choice (const :tag "Today" nil)
  2288. (number :tag "Weekday No.")))
  2289. (defcustom org-agenda-show-all-dates t
  2290. "Non-nil means, `org-agenda' shows every day in the selected range.
  2291. When nil, only the days which actually have entries are shown."
  2292. :group 'org-agenda-daily/weekly
  2293. :type 'boolean)
  2294. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2295. "Format string for displaying dates in the agenda.
  2296. Used by the daily/weekly agenda and by the timeline. This should be
  2297. a format string understood by `format-time-string', or a function returning
  2298. the formatted date as a string. The function must take a single argument,
  2299. a calendar-style date list like (month day year)."
  2300. :group 'org-agenda-daily/weekly
  2301. :type '(choice
  2302. (string :tag "Format string")
  2303. (function :tag "Function")))
  2304. (defun org-agenda-format-date-aligned (date)
  2305. "Format a date string for display in the daily/weekly agenda, or timeline.
  2306. This function makes sure that dates are aligned for easy reading."
  2307. (format "%-9s %2d %s %4d"
  2308. (calendar-day-name date)
  2309. (extract-calendar-day date)
  2310. (calendar-month-name (extract-calendar-month date))
  2311. (extract-calendar-year date)))
  2312. (defcustom org-agenda-include-diary nil
  2313. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2314. :group 'org-agenda-daily/weekly
  2315. :type 'boolean)
  2316. (defcustom org-agenda-include-all-todo nil
  2317. "Set means weekly/daily agenda will always contain all TODO entries.
  2318. The TODO entries will be listed at the top of the agenda, before
  2319. the entries for specific days."
  2320. :group 'org-agenda-daily/weekly
  2321. :type 'boolean)
  2322. (defcustom org-agenda-repeating-timestamp-show-all t
  2323. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2324. When nil, only one occurence is shown, either today or the
  2325. nearest into the future."
  2326. :group 'org-agenda-daily/weekly
  2327. :type 'boolean)
  2328. (defcustom org-deadline-warning-days 14
  2329. "No. of days before expiration during which a deadline becomes active.
  2330. This variable governs the display in sparse trees and in the agenda.
  2331. When negative, it means use this number (the absolute value of it)
  2332. even if a deadline has a different individual lead time specified."
  2333. :group 'org-time
  2334. :group 'org-agenda-daily/weekly
  2335. :type 'number)
  2336. (defcustom org-scheduled-past-days 10000
  2337. "No. of days to continue listing scheduled items that are not marked DONE.
  2338. When an item is scheduled on a date, it shows up in the agenda on this
  2339. day and will be listed until it is marked done for the number of days
  2340. given here."
  2341. :group 'org-agenda-daily/weekly
  2342. :type 'number)
  2343. (defgroup org-agenda-time-grid nil
  2344. "Options concerning the time grid in the Org-mode Agenda."
  2345. :tag "Org Agenda Time Grid"
  2346. :group 'org-agenda)
  2347. (defcustom org-agenda-use-time-grid t
  2348. "Non-nil means, show a time grid in the agenda schedule.
  2349. A time grid is a set of lines for specific times (like every two hours between
  2350. 8:00 and 20:00). The items scheduled for a day at specific times are
  2351. sorted in between these lines.
  2352. For details about when the grid will be shown, and what it will look like, see
  2353. the variable `org-agenda-time-grid'."
  2354. :group 'org-agenda-time-grid
  2355. :type 'boolean)
  2356. (defcustom org-agenda-time-grid
  2357. '((daily today require-timed)
  2358. "----------------"
  2359. (800 1000 1200 1400 1600 1800 2000))
  2360. "The settings for time grid for agenda display.
  2361. This is a list of three items. The first item is again a list. It contains
  2362. symbols specifying conditions when the grid should be displayed:
  2363. daily if the agenda shows a single day
  2364. weekly if the agenda shows an entire week
  2365. today show grid on current date, independent of daily/weekly display
  2366. require-timed show grid only if at least one item has a time specification
  2367. The second item is a string which will be places behing the grid time.
  2368. The third item is a list of integers, indicating the times that should have
  2369. a grid line."
  2370. :group 'org-agenda-time-grid
  2371. :type
  2372. '(list
  2373. (set :greedy t :tag "Grid Display Options"
  2374. (const :tag "Show grid in single day agenda display" daily)
  2375. (const :tag "Show grid in weekly agenda display" weekly)
  2376. (const :tag "Always show grid for today" today)
  2377. (const :tag "Show grid only if any timed entries are present"
  2378. require-timed)
  2379. (const :tag "Skip grid times already present in an entry"
  2380. remove-match))
  2381. (string :tag "Grid String")
  2382. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2383. (defgroup org-agenda-sorting nil
  2384. "Options concerning sorting in the Org-mode Agenda."
  2385. :tag "Org Agenda Sorting"
  2386. :group 'org-agenda)
  2387. (defconst org-sorting-choice
  2388. '(choice
  2389. (const time-up) (const time-down)
  2390. (const category-keep) (const category-up) (const category-down)
  2391. (const tag-down) (const tag-up)
  2392. (const priority-up) (const priority-down))
  2393. "Sorting choices.")
  2394. (defcustom org-agenda-sorting-strategy
  2395. '((agenda time-up category-keep priority-down)
  2396. (todo category-keep priority-down)
  2397. (tags category-keep priority-down))
  2398. "Sorting structure for the agenda items of a single day.
  2399. This is a list of symbols which will be used in sequence to determine
  2400. if an entry should be listed before another entry. The following
  2401. symbols are recognized:
  2402. time-up Put entries with time-of-day indications first, early first
  2403. time-down Put entries with time-of-day indications first, late first
  2404. category-keep Keep the default order of categories, corresponding to the
  2405. sequence in `org-agenda-files'.
  2406. category-up Sort alphabetically by category, A-Z.
  2407. category-down Sort alphabetically by category, Z-A.
  2408. tag-up Sort alphabetically by last tag, A-Z.
  2409. tag-down Sort alphabetically by last tag, Z-A.
  2410. priority-up Sort numerically by priority, high priority last.
  2411. priority-down Sort numerically by priority, high priority first.
  2412. The different possibilities will be tried in sequence, and testing stops
  2413. if one comparison returns a \"not-equal\". For example, the default
  2414. '(time-up category-keep priority-down)
  2415. means: Pull out all entries having a specified time of day and sort them,
  2416. in order to make a time schedule for the current day the first thing in the
  2417. agenda listing for the day. Of the entries without a time indication, keep
  2418. the grouped in categories, don't sort the categories, but keep them in
  2419. the sequence given in `org-agenda-files'. Within each category sort by
  2420. priority.
  2421. Leaving out `category-keep' would mean that items will be sorted across
  2422. categories by priority.
  2423. Instead of a single list, this can also be a set of list for specific
  2424. contents, with a context symbol in the car of the list, any of
  2425. `agenda', `todo', `tags' for the corresponding agenda views."
  2426. :group 'org-agenda-sorting
  2427. :type `(choice
  2428. (repeat :tag "General" ,org-sorting-choice)
  2429. (list :tag "Individually"
  2430. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2431. (repeat ,org-sorting-choice))
  2432. (cons (const :tag "Strategy for TODO lists" todo)
  2433. (repeat ,org-sorting-choice))
  2434. (cons (const :tag "Strategy for Tags matches" tags)
  2435. (repeat ,org-sorting-choice)))))
  2436. (defcustom org-sort-agenda-notime-is-late t
  2437. "Non-nil means, items without time are considered late.
  2438. This is only relevant for sorting. When t, items which have no explicit
  2439. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2440. do have a time. When nil, the default time is before 0:00. You can use this
  2441. option to decide if the schedule for today should come before or after timeless
  2442. agenda entries."
  2443. :group 'org-agenda-sorting
  2444. :type 'boolean)
  2445. (defgroup org-agenda-line-format nil
  2446. "Options concerning the entry prefix in the Org-mode agenda display."
  2447. :tag "Org Agenda Line Format"
  2448. :group 'org-agenda)
  2449. (defcustom org-agenda-prefix-format
  2450. '((agenda . " %-12:c%?-12t% s")
  2451. (timeline . " % s")
  2452. (todo . " %-12:c")
  2453. (tags . " %-12:c"))
  2454. "Format specifications for the prefix of items in the agenda views.
  2455. An alist with four entries, for the different agenda types. The keys to the
  2456. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2457. are format strings.
  2458. This format works similar to a printf format, with the following meaning:
  2459. %c the category of the item, \"Diary\" for entries from the diary, or
  2460. as given by the CATEGORY keyword or derived from the file name.
  2461. %T the *last* tag of the item. Last because inherited tags come
  2462. first in the list.
  2463. %t the time-of-day specification if one applies to the entry, in the
  2464. format HH:MM
  2465. %s Scheduling/Deadline information, a short string
  2466. All specifiers work basically like the standard `%s' of printf, but may
  2467. contain two additional characters: A question mark just after the `%' and
  2468. a whitespace/punctuation character just before the final letter.
  2469. If the first character after `%' is a question mark, the entire field
  2470. will only be included if the corresponding value applies to the
  2471. current entry. This is useful for fields which should have fixed
  2472. width when present, but zero width when absent. For example,
  2473. \"%?-12t\" will result in a 12 character time field if a time of the
  2474. day is specified, but will completely disappear in entries which do
  2475. not contain a time.
  2476. If there is punctuation or whitespace character just before the final
  2477. format letter, this character will be appended to the field value if
  2478. the value is not empty. For example, the format \"%-12:c\" leads to
  2479. \"Diary: \" if the category is \"Diary\". If the category were be
  2480. empty, no additional colon would be interted.
  2481. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2482. - Indent the line with two space characters
  2483. - Give the category in a 12 chars wide field, padded with whitespace on
  2484. the right (because of `-'). Append a colon if there is a category
  2485. (because of `:').
  2486. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2487. time, don't put in an empty field, just skip it (because of '?').
  2488. - Finally, put the scheduling information and append a whitespace.
  2489. As another example, if you don't want the time-of-day of entries in
  2490. the prefix, you could use:
  2491. (setq org-agenda-prefix-format \" %-11:c% s\")
  2492. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2493. `org-agenda-remove-tags'."
  2494. :type '(choice
  2495. (string :tag "General format")
  2496. (list :greedy t :tag "View dependent"
  2497. (cons (const agenda) (string :tag "Format"))
  2498. (cons (const timeline) (string :tag "Format"))
  2499. (cons (const todo) (string :tag "Format"))
  2500. (cons (const tags) (string :tag "Format"))))
  2501. :group 'org-agenda-line-format)
  2502. (defvar org-prefix-format-compiled nil
  2503. "The compiled version of the most recently used prefix format.
  2504. See the variable `org-agenda-prefix-format'.")
  2505. (defcustom org-agenda-todo-keyword-format "%-1s"
  2506. "Format for the TODO keyword in agenda lines.
  2507. Set this to something like \"%-12s\" if you want all TODO keywords
  2508. to occupy a fixed space in the agenda display."
  2509. :group 'org-agenda-line-format
  2510. :type 'string)
  2511. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2512. "Text preceeding scheduled items in the agenda view.
  2513. This is a list with two strings. The first applies when the item is
  2514. scheduled on the current day. The second applies when it has been scheduled
  2515. previously, it may contain a %d to capture how many days ago the item was
  2516. scheduled."
  2517. :group 'org-agenda-line-format
  2518. :type '(list
  2519. (string :tag "Scheduled today ")
  2520. (string :tag "Scheduled previously")))
  2521. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2522. "Text preceeding deadline items in the agenda view.
  2523. This is a list with two strings. The first applies when the item has its
  2524. deadline on the current day. The second applies when it is in the past or
  2525. in the future, it may contain %d to capture how many days away the deadline
  2526. is (was)."
  2527. :group 'org-agenda-line-format
  2528. :type '(list
  2529. (string :tag "Deadline today ")
  2530. (string :tag "Deadline relative")))
  2531. (defcustom org-agenda-remove-times-when-in-prefix t
  2532. "Non-nil means, remove duplicate time specifications in agenda items.
  2533. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2534. time-of-day specification in a headline or diary entry is extracted and
  2535. placed into the prefix. If this option is non-nil, the original specification
  2536. \(a timestamp or -range, or just a plain time(range) specification like
  2537. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2538. cluttered.
  2539. The option can be t or nil. It may also be the symbol `beg', indicating
  2540. that the time should only be removed what it is located at the beginning of
  2541. the headline/diary entry."
  2542. :group 'org-agenda-line-format
  2543. :type '(choice
  2544. (const :tag "Always" t)
  2545. (const :tag "Never" nil)
  2546. (const :tag "When at beginning of entry" beg)))
  2547. (defcustom org-agenda-default-appointment-duration nil
  2548. "Default duration for appointments that only have a starting time.
  2549. When nil, no duration is specified in such cases.
  2550. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2551. :group 'org-agenda-line-format
  2552. :type '(choice
  2553. (integer :tag "Minutes")
  2554. (const :tag "No default duration")))
  2555. (defcustom org-agenda-remove-tags nil
  2556. "Non-nil means, remove the tags from the headline copy in the agenda.
  2557. When this is the symbol `prefix', only remove tags when
  2558. `org-agenda-prefix-format' contains a `%T' specifier."
  2559. :group 'org-agenda-line-format
  2560. :type '(choice
  2561. (const :tag "Always" t)
  2562. (const :tag "Never" nil)
  2563. (const :tag "When prefix format contains %T" prefix)))
  2564. (if (fboundp 'defvaralias)
  2565. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2566. 'org-agenda-remove-tags))
  2567. (defcustom org-agenda-tags-column -80
  2568. "Shift tags in agenda items to this column.
  2569. If this number is positive, it specifies the column. If it is negative,
  2570. it means that the tags should be flushright to that column. For example,
  2571. -80 works well for a normal 80 character screen."
  2572. :group 'org-agenda-line-format
  2573. :type 'integer)
  2574. (if (fboundp 'defvaralias)
  2575. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2576. (defcustom org-agenda-fontify-priorities t
  2577. "Non-nil means, highlight low and high priorities in agenda.
  2578. When t, the highest priority entries are bold, lowest priority italic.
  2579. This may also be an association list of priority faces. The face may be
  2580. a names face, or a list like `(:background \"Red\")'."
  2581. :group 'org-agenda-line-format
  2582. :type '(choice
  2583. (const :tag "Never" nil)
  2584. (const :tag "Defaults" t)
  2585. (repeat :tag "Specify"
  2586. (list (character :tag "Priority" :value ?A)
  2587. (sexp :tag "face")))))
  2588. (defgroup org-latex nil
  2589. "Options for embedding LaTeX code into Org-mode"
  2590. :tag "Org LaTeX"
  2591. :group 'org)
  2592. (defcustom org-format-latex-options
  2593. '(:foreground default :background default :scale 1.0
  2594. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2595. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2596. "Options for creating images from LaTeX fragments.
  2597. This is a property list with the following properties:
  2598. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2599. `default' means use the forground of the default face.
  2600. :background the background color, or \"Transparent\".
  2601. `default' means use the background of the default face.
  2602. :scale a scaling factor for the size of the images
  2603. :html-foreground, :html-background, :html-scale
  2604. The same numbers for HTML export.
  2605. :matchers a list indicating which matchers should be used to
  2606. find LaTeX fragments. Valid members of this list are:
  2607. \"begin\" find environments
  2608. \"$\" find math expressions surrounded by $...$
  2609. \"$$\" find math expressions surrounded by $$....$$
  2610. \"\\(\" find math expressions surrounded by \\(...\\)
  2611. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2612. :group 'org-latex
  2613. :type 'plist)
  2614. (defcustom org-format-latex-header "\\documentclass{article}
  2615. \\usepackage{fullpage} % do not remove
  2616. \\usepackage{amssymb}
  2617. \\usepackage[usenames]{color}
  2618. \\usepackage{amsmath}
  2619. \\usepackage{latexsym}
  2620. \\usepackage[mathscr]{eucal}
  2621. \\pagestyle{empty} % do not remove"
  2622. "The document header used for processing LaTeX fragments."
  2623. :group 'org-latex
  2624. :type 'string)
  2625. (defgroup org-export nil
  2626. "Options for exporting org-listings."
  2627. :tag "Org Export"
  2628. :group 'org)
  2629. (defgroup org-export-general nil
  2630. "General options for exporting Org-mode files."
  2631. :tag "Org Export General"
  2632. :group 'org-export)
  2633. ;; FIXME
  2634. (defvar org-export-publishing-directory nil)
  2635. (defcustom org-export-with-special-strings t
  2636. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2637. When this option is turned on, these strings will be exported as:
  2638. Org HTML LaTeX
  2639. -----+----------+--------
  2640. \\- &shy; \\-
  2641. -- &ndash; --
  2642. --- &mdash; ---
  2643. ... &hellip; \ldots
  2644. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2645. :group 'org-export-translation
  2646. :type 'boolean)
  2647. (defcustom org-export-language-setup
  2648. '(("en" "Author" "Date" "Table of Contents")
  2649. ("cs" "Autor" "Datum" "Obsah")
  2650. ("da" "Ophavsmand" "Dato" "Indhold")
  2651. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2652. ("es" "Autor" "Fecha" "\xcdndice")
  2653. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2654. ("it" "Autore" "Data" "Indice")
  2655. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2656. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2657. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2658. "Terms used in export text, translated to different languages.
  2659. Use the variable `org-export-default-language' to set the language,
  2660. or use the +OPTION lines for a per-file setting."
  2661. :group 'org-export-general
  2662. :type '(repeat
  2663. (list
  2664. (string :tag "HTML language tag")
  2665. (string :tag "Author")
  2666. (string :tag "Date")
  2667. (string :tag "Table of Contents"))))
  2668. (defcustom org-export-default-language "en"
  2669. "The default language of HTML export, as a string.
  2670. This should have an association in `org-export-language-setup'."
  2671. :group 'org-export-general
  2672. :type 'string)
  2673. (defcustom org-export-skip-text-before-1st-heading t
  2674. "Non-nil means, skip all text before the first headline when exporting.
  2675. When nil, that text is exported as well."
  2676. :group 'org-export-general
  2677. :type 'boolean)
  2678. (defcustom org-export-headline-levels 3
  2679. "The last level which is still exported as a headline.
  2680. Inferior levels will produce itemize lists when exported.
  2681. Note that a numeric prefix argument to an exporter function overrides
  2682. this setting.
  2683. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2684. :group 'org-export-general
  2685. :type 'number)
  2686. (defcustom org-export-with-section-numbers t
  2687. "Non-nil means, add section numbers to headlines when exporting.
  2688. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2689. :group 'org-export-general
  2690. :type 'boolean)
  2691. (defcustom org-export-with-toc t
  2692. "Non-nil means, create a table of contents in exported files.
  2693. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2694. When an integer, include levels up to N in the toc, this may then be
  2695. different from `org-export-headline-levels', but it will not be allowed
  2696. to be larger than the number of headline levels.
  2697. When nil, no table of contents is made.
  2698. Headlines which contain any TODO items will be marked with \"(*)\" in
  2699. ASCII export, and with red color in HTML output, if the option
  2700. `org-export-mark-todo-in-toc' is set.
  2701. In HTML output, the TOC will be clickable.
  2702. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2703. or \"toc:3\"."
  2704. :group 'org-export-general
  2705. :type '(choice
  2706. (const :tag "No Table of Contents" nil)
  2707. (const :tag "Full Table of Contents" t)
  2708. (integer :tag "TOC to level")))
  2709. (defcustom org-export-mark-todo-in-toc nil
  2710. "Non-nil means, mark TOC lines that contain any open TODO items."
  2711. :group 'org-export-general
  2712. :type 'boolean)
  2713. (defcustom org-export-preserve-breaks nil
  2714. "Non-nil means, preserve all line breaks when exporting.
  2715. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2716. export, line breaks will always be preserved, regardless of this variable.
  2717. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2718. :group 'org-export-general
  2719. :type 'boolean)
  2720. (defcustom org-export-with-archived-trees 'headline
  2721. "Whether subtrees with the ARCHIVE tag should be exported.
  2722. This can have three different values
  2723. nil Do not export, pretend this tree is not present
  2724. t Do export the entire tree
  2725. headline Only export the headline, but skip the tree below it."
  2726. :group 'org-export-general
  2727. :group 'org-archive
  2728. :type '(choice
  2729. (const :tag "not at all" nil)
  2730. (const :tag "headline only" 'headline)
  2731. (const :tag "entirely" t)))
  2732. (defcustom org-export-author-info t
  2733. "Non-nil means, insert author name and email into the exported file.
  2734. This option can also be set with the +OPTIONS line,
  2735. e.g. \"author-info:nil\"."
  2736. :group 'org-export-general
  2737. :type 'boolean)
  2738. (defcustom org-export-time-stamp-file t
  2739. "Non-nil means, insert a time stamp into the exported file.
  2740. The time stamp shows when the file was created.
  2741. This option can also be set with the +OPTIONS line,
  2742. e.g. \"timestamp:nil\"."
  2743. :group 'org-export-general
  2744. :type 'boolean)
  2745. (defcustom org-export-with-timestamps t
  2746. "If nil, do not export time stamps and associated keywords."
  2747. :group 'org-export-general
  2748. :type 'boolean)
  2749. (defcustom org-export-remove-timestamps-from-toc t
  2750. "If nil, remove timestamps from the table of contents entries."
  2751. :group 'org-export-general
  2752. :type 'boolean)
  2753. (defcustom org-export-with-tags 'not-in-toc
  2754. "If nil, do not export tags, just remove them from headlines.
  2755. If this is the symbol `not-in-toc', tags will be removed from table of
  2756. contents entries, but still be shown in the headlines of the document.
  2757. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2758. :group 'org-export-general
  2759. :type '(choice
  2760. (const :tag "Off" nil)
  2761. (const :tag "Not in TOC" not-in-toc)
  2762. (const :tag "On" t)))
  2763. (defcustom org-export-with-drawers nil
  2764. "Non-nil means, export with drawers like the property drawer.
  2765. When t, all drawers are exported. This may also be a list of
  2766. drawer names to export."
  2767. :group 'org-export-general
  2768. :type '(choice
  2769. (const :tag "All drawers" t)
  2770. (const :tag "None" nil)
  2771. (repeat :tag "Selected drawers"
  2772. (string :tag "Drawer name"))))
  2773. (defgroup org-export-translation nil
  2774. "Options for translating special ascii sequences for the export backends."
  2775. :tag "Org Export Translation"
  2776. :group 'org-export)
  2777. (defcustom org-export-with-emphasize t
  2778. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2779. If the export target supports emphasizing text, the word will be
  2780. typeset in bold, italic, or underlined, respectively. Works only for
  2781. single words, but you can say: I *really* *mean* *this*.
  2782. Not all export backends support this.
  2783. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2784. :group 'org-export-translation
  2785. :type 'boolean)
  2786. (defcustom org-export-with-footnotes t
  2787. "If nil, export [1] as a footnote marker.
  2788. Lines starting with [1] will be formatted as footnotes.
  2789. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2790. :group 'org-export-translation
  2791. :type 'boolean)
  2792. (defcustom org-export-with-sub-superscripts t
  2793. "Non-nil means, interpret \"_\" and \"^\" for export.
  2794. When this option is turned on, you can use TeX-like syntax for sub- and
  2795. superscripts. Several characters after \"_\" or \"^\" will be
  2796. considered as a single item - so grouping with {} is normally not
  2797. needed. For example, the following things will be parsed as single
  2798. sub- or superscripts.
  2799. 10^24 or 10^tau several digits will be considered 1 item.
  2800. 10^-12 or 10^-tau a leading sign with digits or a word
  2801. x^2-y^3 will be read as x^2 - y^3, because items are
  2802. terminated by almost any nonword/nondigit char.
  2803. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2804. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2805. sub/superscript. If you set this variable to the symbol `{}',
  2806. the braces are *required* in order to trigger interpretations as
  2807. sub/superscript. This can be helpful in documents that need \"_\"
  2808. frequently in plain text.
  2809. Not all export backends support this, but HTML does.
  2810. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2811. :group 'org-export-translation
  2812. :type '(choice
  2813. (const :tag "Always interpret" t)
  2814. (const :tag "Only with braces" {})
  2815. (const :tag "Never interpret" nil)))
  2816. (defcustom org-export-with-special-strings t
  2817. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2818. When this option is turned on, these strings will be exported as:
  2819. \\- : &shy;
  2820. -- : &ndash;
  2821. --- : &mdash;
  2822. Not all export backends support this, but HTML does.
  2823. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2824. :group 'org-export-translation
  2825. :type 'boolean)
  2826. (defcustom org-export-with-TeX-macros t
  2827. "Non-nil means, interpret simple TeX-like macros when exporting.
  2828. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2829. No only real TeX macros will work here, but the standard HTML entities
  2830. for math can be used as macro names as well. For a list of supported
  2831. names in HTML export, see the constant `org-html-entities'.
  2832. Not all export backends support this.
  2833. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2834. :group 'org-export-translation
  2835. :group 'org-export-latex
  2836. :type 'boolean)
  2837. (defcustom org-export-with-LaTeX-fragments nil
  2838. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2839. When set, the exporter will find LaTeX environments if the \\begin line is
  2840. the first non-white thing on a line. It will also find the math delimiters
  2841. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2842. display math.
  2843. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2844. :group 'org-export-translation
  2845. :group 'org-export-latex
  2846. :type 'boolean)
  2847. (defcustom org-export-with-fixed-width t
  2848. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2849. This can be used to have pre-formatted text, fragments of code etc. For
  2850. example:
  2851. : ;; Some Lisp examples
  2852. : (while (defc cnt)
  2853. : (ding))
  2854. will be looking just like this in also HTML. See also the QUOTE keyword.
  2855. Not all export backends support this.
  2856. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2857. :group 'org-export-translation
  2858. :type 'boolean)
  2859. (defcustom org-match-sexp-depth 3
  2860. "Number of stacked braces for sub/superscript matching.
  2861. This has to be set before loading org.el to be effective."
  2862. :group 'org-export-translation
  2863. :type 'integer)
  2864. (defgroup org-export-tables nil
  2865. "Options for exporting tables in Org-mode."
  2866. :tag "Org Export Tables"
  2867. :group 'org-export)
  2868. (defcustom org-export-with-tables t
  2869. "If non-nil, lines starting with \"|\" define a table.
  2870. For example:
  2871. | Name | Address | Birthday |
  2872. |-------------+----------+-----------|
  2873. | Arthur Dent | England | 29.2.2100 |
  2874. Not all export backends support this.
  2875. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2876. :group 'org-export-tables
  2877. :type 'boolean)
  2878. (defcustom org-export-highlight-first-table-line t
  2879. "Non-nil means, highlight the first table line.
  2880. In HTML export, this means use <th> instead of <td>.
  2881. In tables created with table.el, this applies to the first table line.
  2882. In Org-mode tables, all lines before the first horizontal separator
  2883. line will be formatted with <th> tags."
  2884. :group 'org-export-tables
  2885. :type 'boolean)
  2886. (defcustom org-export-table-remove-special-lines t
  2887. "Remove special lines and marking characters in calculating tables.
  2888. This removes the special marking character column from tables that are set
  2889. up for spreadsheet calculations. It also removes the entire lines
  2890. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2891. the values of constants may be useful to have."
  2892. :group 'org-export-tables
  2893. :type 'boolean)
  2894. (defcustom org-export-prefer-native-exporter-for-tables nil
  2895. "Non-nil means, always export tables created with table.el natively.
  2896. Natively means, use the HTML code generator in table.el.
  2897. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2898. the table does not use row- or column-spanning). This has the
  2899. advantage, that the automatic HTML conversions for math symbols and
  2900. sub/superscripts can be applied. Org-mode's HTML generator is also
  2901. much faster."
  2902. :group 'org-export-tables
  2903. :type 'boolean)
  2904. (defgroup org-export-ascii nil
  2905. "Options specific for ASCII export of Org-mode files."
  2906. :tag "Org Export ASCII"
  2907. :group 'org-export)
  2908. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2909. "Characters for underlining headings in ASCII export.
  2910. In the given sequence, these characters will be used for level 1, 2, ..."
  2911. :group 'org-export-ascii
  2912. :type '(repeat character))
  2913. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2914. "Bullet characters for headlines converted to lists in ASCII export.
  2915. The first character is used for the first lest level generated in this
  2916. way, and so on. If there are more levels than characters given here,
  2917. the list will be repeated.
  2918. Note that plain lists will keep the same bullets as the have in the
  2919. Org-mode file."
  2920. :group 'org-export-ascii
  2921. :type '(repeat character))
  2922. (defgroup org-export-xml nil
  2923. "Options specific for XML export of Org-mode files."
  2924. :tag "Org Export XML"
  2925. :group 'org-export)
  2926. (defgroup org-export-html nil
  2927. "Options specific for HTML export of Org-mode files."
  2928. :tag "Org Export HTML"
  2929. :group 'org-export)
  2930. (defcustom org-export-html-coding-system nil
  2931. ""
  2932. :group 'org-export-html
  2933. :type 'coding-system)
  2934. (defcustom org-export-html-extension "html"
  2935. "The extension for exported HTML files."
  2936. :group 'org-export-html
  2937. :type 'string)
  2938. (defcustom org-export-html-style
  2939. "<style type=\"text/css\">
  2940. html {
  2941. font-family: Times, serif;
  2942. font-size: 12pt;
  2943. }
  2944. .title { text-align: center; }
  2945. .todo { color: red; }
  2946. .done { color: green; }
  2947. .timestamp { color: grey }
  2948. .timestamp-kwd { color: CadetBlue }
  2949. .tag { background-color:lightblue; font-weight:normal }
  2950. .target { background-color: lavender; }
  2951. pre {
  2952. border: 1pt solid #AEBDCC;
  2953. background-color: #F3F5F7;
  2954. padding: 5pt;
  2955. font-family: courier, monospace;
  2956. }
  2957. table { border-collapse: collapse; }
  2958. td, th {
  2959. vertical-align: top;
  2960. <!--border: 1pt solid #ADB9CC;-->
  2961. }
  2962. </style>"
  2963. "The default style specification for exported HTML files.
  2964. Since there are different ways of setting style information, this variable
  2965. needs to contain the full HTML structure to provide a style, including the
  2966. surrounding HTML tags. The style specifications should include definitions
  2967. for new classes todo, done, title, and deadline. For example, legal values
  2968. would be:
  2969. <style type=\"text/css\">
  2970. p { font-weight: normal; color: gray; }
  2971. h1 { color: black; }
  2972. .title { text-align: center; }
  2973. .todo, .deadline { color: red; }
  2974. .done { color: green; }
  2975. </style>
  2976. or, if you want to keep the style in a file,
  2977. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2978. As the value of this option simply gets inserted into the HTML <head> header,
  2979. you can \"misuse\" it to add arbitrary text to the header."
  2980. :group 'org-export-html
  2981. :type 'string)
  2982. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2983. "Format for typesetting the document title in HTML export."
  2984. :group 'org-export-html
  2985. :type 'string)
  2986. (defcustom org-export-html-toplevel-hlevel 2
  2987. "The <H> level for level 1 headings in HTML export."
  2988. :group 'org-export-html
  2989. :type 'string)
  2990. (defcustom org-export-html-link-org-files-as-html t
  2991. "Non-nil means, make file links to `file.org' point to `file.html'.
  2992. When org-mode is exporting an org-mode file to HTML, links to
  2993. non-html files are directly put into a href tag in HTML.
  2994. However, links to other Org-mode files (recognized by the
  2995. extension `.org.) should become links to the corresponding html
  2996. file, assuming that the linked org-mode file will also be
  2997. converted to HTML.
  2998. When nil, the links still point to the plain `.org' file."
  2999. :group 'org-export-html
  3000. :type 'boolean)
  3001. (defcustom org-export-html-inline-images 'maybe
  3002. "Non-nil means, inline images into exported HTML pages.
  3003. This is done using an <img> tag. When nil, an anchor with href is used to
  3004. link to the image. If this option is `maybe', then images in links with
  3005. an empty description will be inlined, while images with a description will
  3006. be linked only."
  3007. :group 'org-export-html
  3008. :type '(choice (const :tag "Never" nil)
  3009. (const :tag "Always" t)
  3010. (const :tag "When there is no description" maybe)))
  3011. ;; FIXME: rename
  3012. (defcustom org-export-html-expand t
  3013. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3014. When nil, these tags will be exported as plain text and therefore
  3015. not be interpreted by a browser.
  3016. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3017. :group 'org-export-html
  3018. :type 'boolean)
  3019. (defcustom org-export-html-table-tag
  3020. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3021. "The HTML tag that is used to start a table.
  3022. This must be a <table> tag, but you may change the options like
  3023. borders and spacing."
  3024. :group 'org-export-html
  3025. :type 'string)
  3026. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3027. "The opening tag for table header fields.
  3028. This is customizable so that alignment options can be specified."
  3029. :group 'org-export-tables
  3030. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3031. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3032. "The opening tag for table data fields.
  3033. This is customizable so that alignment options can be specified."
  3034. :group 'org-export-tables
  3035. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3036. (defcustom org-export-html-with-timestamp nil
  3037. "If non-nil, write `org-export-html-html-helper-timestamp'
  3038. into the exported HTML text. Otherwise, the buffer will just be saved
  3039. to a file."
  3040. :group 'org-export-html
  3041. :type 'boolean)
  3042. (defcustom org-export-html-html-helper-timestamp
  3043. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3044. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3045. :group 'org-export-html
  3046. :type 'string)
  3047. (defgroup org-export-icalendar nil
  3048. "Options specific for iCalendar export of Org-mode files."
  3049. :tag "Org Export iCalendar"
  3050. :group 'org-export)
  3051. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3052. "The file name for the iCalendar file covering all agenda files.
  3053. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3054. The file name should be absolute, the file will be overwritten without warning."
  3055. :group 'org-export-icalendar
  3056. :type 'file)
  3057. (defcustom org-icalendar-include-todo nil
  3058. "Non-nil means, export to iCalendar files should also cover TODO items."
  3059. :group 'org-export-icalendar
  3060. :type '(choice
  3061. (const :tag "None" nil)
  3062. (const :tag "Unfinished" t)
  3063. (const :tag "All" all)))
  3064. (defcustom org-icalendar-include-sexps t
  3065. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3066. These are entries like in the diary, but directly in an Org-mode file."
  3067. :group 'org-export-icalendar
  3068. :type 'boolean)
  3069. (defcustom org-icalendar-include-body 100
  3070. "Amount of text below headline to be included in iCalendar export.
  3071. This is a number of characters that should maximally be included.
  3072. Properties, scheduling and clocking lines will always be removed.
  3073. The text will be inserted into the DESCRIPTION field."
  3074. :group 'org-export-icalendar
  3075. :type '(choice
  3076. (const :tag "Nothing" nil)
  3077. (const :tag "Everything" t)
  3078. (integer :tag "Max characters")))
  3079. (defcustom org-icalendar-combined-name "OrgMode"
  3080. "Calendar name for the combined iCalendar representing all agenda files."
  3081. :group 'org-export-icalendar
  3082. :type 'string)
  3083. (defgroup org-font-lock nil
  3084. "Font-lock settings for highlighting in Org-mode."
  3085. :tag "Org Font Lock"
  3086. :group 'org)
  3087. (defcustom org-level-color-stars-only nil
  3088. "Non-nil means fontify only the stars in each headline.
  3089. When nil, the entire headline is fontified.
  3090. Changing it requires restart of `font-lock-mode' to become effective
  3091. also in regions already fontified."
  3092. :group 'org-font-lock
  3093. :type 'boolean)
  3094. (defcustom org-hide-leading-stars nil
  3095. "Non-nil means, hide the first N-1 stars in a headline.
  3096. This works by using the face `org-hide' for these stars. This
  3097. face is white for a light background, and black for a dark
  3098. background. You may have to customize the face `org-hide' to
  3099. make this work.
  3100. Changing it requires restart of `font-lock-mode' to become effective
  3101. also in regions already fontified.
  3102. You may also set this on a per-file basis by adding one of the following
  3103. lines to the buffer:
  3104. #+STARTUP: hidestars
  3105. #+STARTUP: showstars"
  3106. :group 'org-font-lock
  3107. :type 'boolean)
  3108. (defcustom org-fontify-done-headline nil
  3109. "Non-nil means, change the face of a headline if it is marked DONE.
  3110. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3111. When this is non-nil, the headline after the keyword is set to the
  3112. `org-headline-done' as an additional indication."
  3113. :group 'org-font-lock
  3114. :type 'boolean)
  3115. (defcustom org-fontify-emphasized-text t
  3116. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3117. Changing this variable requires a restart of Emacs to take effect."
  3118. :group 'org-font-lock
  3119. :type 'boolean)
  3120. (defcustom org-highlight-latex-fragments-and-specials nil
  3121. "Non-nil means, fontify what is treated specially by the exporters."
  3122. :group 'org-font-lock
  3123. :type 'boolean)
  3124. (defcustom org-hide-emphasis-markers nil
  3125. "Non-nil mean font-lock should hide the emphasis marker characters."
  3126. :group 'org-font-lock
  3127. :type 'boolean)
  3128. (defvar org-emph-re nil
  3129. "Regular expression for matching emphasis.")
  3130. (defvar org-verbatim-re nil
  3131. "Regular expression for matching verbatim text.")
  3132. (defvar org-emphasis-regexp-components) ; defined just below
  3133. (defvar org-emphasis-alist) ; defined just below
  3134. (defun org-set-emph-re (var val)
  3135. "Set variable and compute the emphasis regular expression."
  3136. (set var val)
  3137. (when (and (boundp 'org-emphasis-alist)
  3138. (boundp 'org-emphasis-regexp-components)
  3139. org-emphasis-alist org-emphasis-regexp-components)
  3140. (let* ((e org-emphasis-regexp-components)
  3141. (pre (car e))
  3142. (post (nth 1 e))
  3143. (border (nth 2 e))
  3144. (body (nth 3 e))
  3145. (nl (nth 4 e))
  3146. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3147. (body1 (concat body "*?"))
  3148. (markers (mapconcat 'car org-emphasis-alist ""))
  3149. (vmarkers (mapconcat
  3150. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3151. org-emphasis-alist "")))
  3152. ;; make sure special characters appear at the right position in the class
  3153. (if (string-match "\\^" markers)
  3154. (setq markers (concat (replace-match "" t t markers) "^")))
  3155. (if (string-match "-" markers)
  3156. (setq markers (concat (replace-match "" t t markers) "-")))
  3157. (if (string-match "\\^" vmarkers)
  3158. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3159. (if (string-match "-" vmarkers)
  3160. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3161. (if (> nl 0)
  3162. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3163. (int-to-string nl) "\\}")))
  3164. ;; Make the regexp
  3165. (setq org-emph-re
  3166. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3167. "\\("
  3168. "\\([" markers "]\\)"
  3169. "\\("
  3170. "[^" border "]\\|"
  3171. "[^" border (if (and nil stacked) markers) "]"
  3172. body1
  3173. "[^" border (if (and nil stacked) markers) "]"
  3174. "\\)"
  3175. "\\3\\)"
  3176. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3177. (setq org-verbatim-re
  3178. (concat "\\([" pre "]\\|^\\)"
  3179. "\\("
  3180. "\\([" vmarkers "]\\)"
  3181. "\\("
  3182. "[^" border "]\\|"
  3183. "[^" border "]"
  3184. body1
  3185. "[^" border "]"
  3186. "\\)"
  3187. "\\3\\)"
  3188. "\\([" post "]\\|$\\)")))))
  3189. (defcustom org-emphasis-regexp-components
  3190. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3191. "Components used to build the regular expression for emphasis.
  3192. This is a list with 6 entries. Terminology: In an emphasis string
  3193. like \" *strong word* \", we call the initial space PREMATCH, the final
  3194. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3195. and \"trong wor\" is the body. The different components in this variable
  3196. specify what is allowed/forbidden in each part:
  3197. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3198. post Chars allowed as postmatch. End of line will be allowed too.
  3199. border The chars *forbidden* as border characters.
  3200. body-regexp A regexp like \".\" to match a body character. Don't use
  3201. non-shy groups here, and don't allow newline here.
  3202. newline The maximum number of newlines allowed in an emphasis exp.
  3203. Use customize to modify this, or restart Emacs after changing it."
  3204. :group 'org-font-lock
  3205. :set 'org-set-emph-re
  3206. :type '(list
  3207. (sexp :tag "Allowed chars in pre ")
  3208. (sexp :tag "Allowed chars in post ")
  3209. (sexp :tag "Forbidden chars in border ")
  3210. (sexp :tag "Regexp for body ")
  3211. (integer :tag "number of newlines allowed")
  3212. (option (boolean :tag "Stacking (DISABLED) "))))
  3213. (defcustom org-emphasis-alist
  3214. '(("*" bold "<b>" "</b>")
  3215. ("/" italic "<i>" "</i>")
  3216. ("_" underline "<u>" "</u>")
  3217. ("=" org-code "<code>" "</code>" verbatim)
  3218. ("~" org-verbatim "" "" verbatim)
  3219. ("+" (:strike-through t) "<del>" "</del>")
  3220. )
  3221. "Special syntax for emphasized text.
  3222. Text starting and ending with a special character will be emphasized, for
  3223. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3224. characters, the face to be used by font-lock for highlighting in Org-mode
  3225. Emacs buffers, and the HTML tags to be used for this.
  3226. Use customize to modify this, or restart Emacs after changing it."
  3227. :group 'org-font-lock
  3228. :set 'org-set-emph-re
  3229. :type '(repeat
  3230. (list
  3231. (string :tag "Marker character")
  3232. (choice
  3233. (face :tag "Font-lock-face")
  3234. (plist :tag "Face property list"))
  3235. (string :tag "HTML start tag")
  3236. (string :tag "HTML end tag")
  3237. (option (const verbatim)))))
  3238. ;;; The faces
  3239. (defgroup org-faces nil
  3240. "Faces in Org-mode."
  3241. :tag "Org Faces"
  3242. :group 'org-font-lock)
  3243. (defun org-compatible-face (inherits specs)
  3244. "Make a compatible face specification.
  3245. If INHERITS is an existing face and if the Emacs version supports it,
  3246. just inherit the face. If not, use SPECS to define the face.
  3247. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3248. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3249. to the top of the list. The `min-colors' attribute will be removed from
  3250. any other entries, and any resulting duplicates will be removed entirely."
  3251. (cond
  3252. ((and inherits (facep inherits)
  3253. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3254. ;; In Emacs 23, we use inheritance where possible.
  3255. ;; We only do this in Emacs 23, because only there the outline
  3256. ;; faces have been changed to the original org-mode-level-faces.
  3257. (list (list t :inherit inherits)))
  3258. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3259. ;; These do not understand the `min-colors' attribute.
  3260. (let (r e a)
  3261. (while (setq e (pop specs))
  3262. (cond
  3263. ((memq (car e) '(t default)) (push e r))
  3264. ((setq a (member '(min-colors 8) (car e)))
  3265. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3266. (cdr e)))))
  3267. ((setq a (assq 'min-colors (car e)))
  3268. (setq e (cons (delq a (car e)) (cdr e)))
  3269. (or (assoc (car e) r) (push e r)))
  3270. (t (or (assoc (car e) r) (push e r)))))
  3271. (nreverse r)))
  3272. (t specs)))
  3273. (put 'org-compatible-face 'lisp-indent-function 1)
  3274. (defface org-hide
  3275. '((((background light)) (:foreground "white"))
  3276. (((background dark)) (:foreground "black")))
  3277. "Face used to hide leading stars in headlines.
  3278. The forground color of this face should be equal to the background
  3279. color of the frame."
  3280. :group 'org-faces)
  3281. (defface org-level-1 ;; font-lock-function-name-face
  3282. (org-compatible-face 'outline-1
  3283. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3284. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3285. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3286. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3287. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3288. (t (:bold t))))
  3289. "Face used for level 1 headlines."
  3290. :group 'org-faces)
  3291. (defface org-level-2 ;; font-lock-variable-name-face
  3292. (org-compatible-face 'outline-2
  3293. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3294. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3295. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3296. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3297. (t (:bold t))))
  3298. "Face used for level 2 headlines."
  3299. :group 'org-faces)
  3300. (defface org-level-3 ;; font-lock-keyword-face
  3301. (org-compatible-face 'outline-3
  3302. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3303. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3304. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3305. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3306. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3307. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3308. (t (:bold t))))
  3309. "Face used for level 3 headlines."
  3310. :group 'org-faces)
  3311. (defface org-level-4 ;; font-lock-comment-face
  3312. (org-compatible-face 'outline-4
  3313. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3314. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3315. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3316. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3317. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3318. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3319. (t (:bold t))))
  3320. "Face used for level 4 headlines."
  3321. :group 'org-faces)
  3322. (defface org-level-5 ;; font-lock-type-face
  3323. (org-compatible-face 'outline-5
  3324. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3325. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3326. (((class color) (min-colors 8)) (:foreground "green"))))
  3327. "Face used for level 5 headlines."
  3328. :group 'org-faces)
  3329. (defface org-level-6 ;; font-lock-constant-face
  3330. (org-compatible-face 'outline-6
  3331. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3332. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3333. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3334. "Face used for level 6 headlines."
  3335. :group 'org-faces)
  3336. (defface org-level-7 ;; font-lock-builtin-face
  3337. (org-compatible-face 'outline-7
  3338. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3339. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3340. (((class color) (min-colors 8)) (:foreground "blue"))))
  3341. "Face used for level 7 headlines."
  3342. :group 'org-faces)
  3343. (defface org-level-8 ;; font-lock-string-face
  3344. (org-compatible-face 'outline-8
  3345. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3346. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3347. (((class color) (min-colors 8)) (:foreground "green"))))
  3348. "Face used for level 8 headlines."
  3349. :group 'org-faces)
  3350. (defface org-special-keyword ;; font-lock-string-face
  3351. (org-compatible-face nil
  3352. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3353. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3354. (t (:italic t))))
  3355. "Face used for special keywords."
  3356. :group 'org-faces)
  3357. (defface org-drawer ;; font-lock-function-name-face
  3358. (org-compatible-face nil
  3359. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3360. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3361. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3362. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3363. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3364. (t (:bold t))))
  3365. "Face used for drawers."
  3366. :group 'org-faces)
  3367. (defface org-property-value nil
  3368. "Face used for the value of a property."
  3369. :group 'org-faces)
  3370. (defface org-column
  3371. (org-compatible-face nil
  3372. '((((class color) (min-colors 16) (background light))
  3373. (:background "grey90"))
  3374. (((class color) (min-colors 16) (background dark))
  3375. (:background "grey30"))
  3376. (((class color) (min-colors 8))
  3377. (:background "cyan" :foreground "black"))
  3378. (t (:inverse-video t))))
  3379. "Face for column display of entry properties."
  3380. :group 'org-faces)
  3381. (when (fboundp 'set-face-attribute)
  3382. ;; Make sure that a fixed-width face is used when we have a column table.
  3383. (set-face-attribute 'org-column nil
  3384. :height (face-attribute 'default :height)
  3385. :family (face-attribute 'default :family)))
  3386. (defface org-warning
  3387. (org-compatible-face 'font-lock-warning-face
  3388. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3389. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3390. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3391. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3392. (t (:bold t))))
  3393. "Face for deadlines and TODO keywords."
  3394. :group 'org-faces)
  3395. (defface org-archived ; similar to shadow
  3396. (org-compatible-face 'shadow
  3397. '((((class color grayscale) (min-colors 88) (background light))
  3398. (:foreground "grey50"))
  3399. (((class color grayscale) (min-colors 88) (background dark))
  3400. (:foreground "grey70"))
  3401. (((class color) (min-colors 8) (background light))
  3402. (:foreground "green"))
  3403. (((class color) (min-colors 8) (background dark))
  3404. (:foreground "yellow"))))
  3405. "Face for headline with the ARCHIVE tag."
  3406. :group 'org-faces)
  3407. (defface org-link
  3408. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3409. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3410. (t (:underline t)))
  3411. "Face for links."
  3412. :group 'org-faces)
  3413. (defface org-ellipsis
  3414. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3415. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3416. (t (:strike-through t)))
  3417. "Face for the ellipsis in folded text."
  3418. :group 'org-faces)
  3419. (defface org-target
  3420. '((((class color) (background light)) (:underline t))
  3421. (((class color) (background dark)) (:underline t))
  3422. (t (:underline t)))
  3423. "Face for links."
  3424. :group 'org-faces)
  3425. (defface org-date
  3426. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3427. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3428. (t (:underline t)))
  3429. "Face for links."
  3430. :group 'org-faces)
  3431. (defface org-sexp-date
  3432. '((((class color) (background light)) (:foreground "Purple"))
  3433. (((class color) (background dark)) (:foreground "Cyan"))
  3434. (t (:underline t)))
  3435. "Face for links."
  3436. :group 'org-faces)
  3437. (defface org-tag
  3438. '((t (:bold t)))
  3439. "Face for tags."
  3440. :group 'org-faces)
  3441. (defface org-todo ; font-lock-warning-face
  3442. (org-compatible-face nil
  3443. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3444. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3445. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3446. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3447. (t (:inverse-video t :bold t))))
  3448. "Face for TODO keywords."
  3449. :group 'org-faces)
  3450. (defface org-done ;; font-lock-type-face
  3451. (org-compatible-face nil
  3452. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3453. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3454. (((class color) (min-colors 8)) (:foreground "green"))
  3455. (t (:bold t))))
  3456. "Face used for todo keywords that indicate DONE items."
  3457. :group 'org-faces)
  3458. (defface org-headline-done ;; font-lock-string-face
  3459. (org-compatible-face nil
  3460. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3461. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3462. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3463. "Face used to indicate that a headline is DONE.
  3464. This face is only used if `org-fontify-done-headline' is set. If applies
  3465. to the part of the headline after the DONE keyword."
  3466. :group 'org-faces)
  3467. (defcustom org-todo-keyword-faces nil
  3468. "Faces for specific TODO keywords.
  3469. This is a list of cons cells, with TODO keywords in the car
  3470. and faces in the cdr. The face can be a symbol, or a property
  3471. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3472. :group 'org-faces
  3473. :group 'org-todo
  3474. :type '(repeat
  3475. (cons
  3476. (string :tag "keyword")
  3477. (sexp :tag "face"))))
  3478. (defface org-table ;; font-lock-function-name-face
  3479. (org-compatible-face nil
  3480. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3481. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3482. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3483. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3484. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3485. (((class color) (min-colors 8) (background dark)))))
  3486. "Face used for tables."
  3487. :group 'org-faces)
  3488. (defface org-formula
  3489. (org-compatible-face nil
  3490. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3491. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3492. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3493. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3494. (t (:bold t :italic t))))
  3495. "Face for formulas."
  3496. :group 'org-faces)
  3497. (defface org-code
  3498. (org-compatible-face nil
  3499. '((((class color grayscale) (min-colors 88) (background light))
  3500. (:foreground "grey50"))
  3501. (((class color grayscale) (min-colors 88) (background dark))
  3502. (:foreground "grey70"))
  3503. (((class color) (min-colors 8) (background light))
  3504. (:foreground "green"))
  3505. (((class color) (min-colors 8) (background dark))
  3506. (:foreground "yellow"))))
  3507. "Face for fixed-with text like code snippets."
  3508. :group 'org-faces
  3509. :version "22.1")
  3510. (defface org-verbatim
  3511. (org-compatible-face nil
  3512. '((((class color grayscale) (min-colors 88) (background light))
  3513. (:foreground "grey50" :underline t))
  3514. (((class color grayscale) (min-colors 88) (background dark))
  3515. (:foreground "grey70" :underline t))
  3516. (((class color) (min-colors 8) (background light))
  3517. (:foreground "green" :underline t))
  3518. (((class color) (min-colors 8) (background dark))
  3519. (:foreground "yellow" :underline t))))
  3520. "Face for fixed-with text like code snippets."
  3521. :group 'org-faces
  3522. :version "22.1")
  3523. (defface org-agenda-structure ;; font-lock-function-name-face
  3524. (org-compatible-face nil
  3525. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3526. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3527. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3528. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3529. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3530. (t (:bold t))))
  3531. "Face used in agenda for captions and dates."
  3532. :group 'org-faces)
  3533. (defface org-scheduled-today
  3534. (org-compatible-face nil
  3535. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3536. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3537. (((class color) (min-colors 8)) (:foreground "green"))
  3538. (t (:bold t :italic t))))
  3539. "Face for items scheduled for a certain day."
  3540. :group 'org-faces)
  3541. (defface org-scheduled-previously
  3542. (org-compatible-face nil
  3543. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3544. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3545. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3546. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3547. (t (:bold t))))
  3548. "Face for items scheduled previously, and not yet done."
  3549. :group 'org-faces)
  3550. (defface org-upcoming-deadline
  3551. (org-compatible-face nil
  3552. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3553. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3554. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3555. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3556. (t (:bold t))))
  3557. "Face for items scheduled previously, and not yet done."
  3558. :group 'org-faces)
  3559. (defcustom org-agenda-deadline-faces
  3560. '((1.0 . org-warning)
  3561. (0.5 . org-upcoming-deadline)
  3562. (0.0 . default))
  3563. "Faces for showing deadlines in the agenda.
  3564. This is a list of cons cells. The cdr of each cell is a face to be used,
  3565. and it can also just be like '(:foreground \"yellow\").
  3566. Each car is a fraction of the head-warning time that must have passed for
  3567. this the face in the cdr to be used for display. The numbers must be
  3568. given in descending order. The head-warning time is normally taken
  3569. from `org-deadline-warning-days', but can also be specified in the deadline
  3570. timestamp itself, like this:
  3571. DEADLINE: <2007-08-13 Mon -8d>
  3572. You may use d for days, w for weeks, m for months and y for years. Months
  3573. and years will only be treated in an approximate fashion (30.4 days for a
  3574. month and 365.24 days for a year)."
  3575. :group 'org-faces
  3576. :group 'org-agenda-daily/weekly
  3577. :type '(repeat
  3578. (cons
  3579. (number :tag "Fraction of head-warning time passed")
  3580. (sexp :tag "Face"))))
  3581. ;; FIXME: this is not a good face yet.
  3582. (defface org-agenda-restriction-lock
  3583. (org-compatible-face nil
  3584. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3585. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3586. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3587. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3588. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3589. (t (:inverse-video t))))
  3590. "Face for showing the agenda restriction lock."
  3591. :group 'org-faces)
  3592. (defface org-time-grid ;; font-lock-variable-name-face
  3593. (org-compatible-face nil
  3594. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3595. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3596. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3597. "Face used for time grids."
  3598. :group 'org-faces)
  3599. (defconst org-level-faces
  3600. '(org-level-1 org-level-2 org-level-3 org-level-4
  3601. org-level-5 org-level-6 org-level-7 org-level-8
  3602. ))
  3603. (defcustom org-n-level-faces (length org-level-faces)
  3604. "The number different faces to be used for headlines.
  3605. Org-mode defines 8 different headline faces, so this can be at most 8.
  3606. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3607. :type 'number
  3608. :group 'org-faces)
  3609. ;;; Functions and variables from ther packages
  3610. ;; Declared here to avoid compiler warnings
  3611. (eval-and-compile
  3612. (unless (fboundp 'declare-function)
  3613. (defmacro declare-function (fn file &optional arglist fileonly))))
  3614. ;; XEmacs only
  3615. (defvar outline-mode-menu-heading)
  3616. (defvar outline-mode-menu-show)
  3617. (defvar outline-mode-menu-hide)
  3618. (defvar zmacs-regions) ; XEmacs regions
  3619. ;; Emacs only
  3620. (defvar mark-active)
  3621. ;; Various packages
  3622. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3623. (declare-function add-to-diary-list "diary-lib"
  3624. (date string specifier &optional marker globcolor literal))
  3625. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3626. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3627. (declare-function Info-goto-node "info" (nodename &optional fork))
  3628. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3629. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3630. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3631. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3632. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3633. (declare-function bbdb-record-name "ext:bbdb" (record))
  3634. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3635. (declare-function bibtex-generate-autokey "bibtex" ())
  3636. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3637. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3638. (defvar calc-embedded-close-formula)
  3639. (defvar calc-embedded-open-formula)
  3640. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3641. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3642. (declare-function calendar-check-holidays "holidays" (date))
  3643. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3644. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3645. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3646. (declare-function calendar-forward-day "cal-move" (arg))
  3647. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3648. (declare-function calendar-goto-date "cal-move" (date))
  3649. (declare-function calendar-goto-today "cal-move" ())
  3650. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3651. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3652. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3653. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3654. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3655. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3656. (defvar calendar-mode-map)
  3657. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3658. (declare-function cdlatex-tab "ext:cdlatex" ())
  3659. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3660. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3661. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3662. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3663. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3664. (defvar font-lock-unfontify-region-function)
  3665. (declare-function gnus-article-show-summary "gnus-art" ())
  3666. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3667. (defvar gnus-other-frame-object)
  3668. (defvar gnus-group-name)
  3669. (defvar gnus-article-current)
  3670. (defvar Info-current-file)
  3671. (defvar Info-current-node)
  3672. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3673. (declare-function mh-find-path "mh-utils" ())
  3674. (declare-function mh-get-header-field "mh-utils" (field))
  3675. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3676. (declare-function mh-header-display "mh-show" ())
  3677. (declare-function mh-index-previous-folder "mh-search" ())
  3678. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3679. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3680. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3681. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3682. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3683. (declare-function mh-show-header-display "mh-show" t t)
  3684. (declare-function mh-show-msg "mh-show" (msg))
  3685. (declare-function mh-show-show "mh-show" t t)
  3686. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3687. (defvar mh-progs)
  3688. (defvar mh-current-folder)
  3689. (defvar mh-show-folder-buffer)
  3690. (defvar mh-index-folder)
  3691. (defvar mh-searcher)
  3692. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3693. (declare-function parse-time-string "parse-time" (string))
  3694. (declare-function remember "remember" (&optional initial))
  3695. (declare-function remember-buffer-desc "remember" ())
  3696. (declare-function remember-finalize "remember" ())
  3697. (defvar remember-save-after-remembering)
  3698. (defvar remember-data-file)
  3699. (defvar remember-register)
  3700. (defvar remember-buffer)
  3701. (defvar remember-handler-functions)
  3702. (defvar remember-annotation-functions)
  3703. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3704. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3705. (declare-function rmail-what-message "rmail" ())
  3706. (defvar texmathp-why)
  3707. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3708. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3709. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3710. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3711. (declare-function vm-isearch-update "ext:vm-search" ())
  3712. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3713. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3714. (declare-function vm-su-subject "ext:vm-summary" (m))
  3715. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3716. (defvar vm-message-pointer)
  3717. (defvar vm-folder-directory)
  3718. (defvar w3m-current-url)
  3719. (defvar w3m-current-title)
  3720. ;; backward compatibility to old version of wl
  3721. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3722. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3723. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3724. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3725. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3726. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3727. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3728. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3729. (defvar wl-summary-buffer-elmo-folder)
  3730. (defvar wl-summary-buffer-folder-name)
  3731. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3732. (defvar org-latex-regexps)
  3733. (defvar constants-unit-system)
  3734. ;;; Variables for pre-computed regular expressions, all buffer local
  3735. (defvar org-drawer-regexp nil
  3736. "Matches first line of a hidden block.")
  3737. (make-variable-buffer-local 'org-drawer-regexp)
  3738. (defvar org-todo-regexp nil
  3739. "Matches any of the TODO state keywords.")
  3740. (make-variable-buffer-local 'org-todo-regexp)
  3741. (defvar org-not-done-regexp nil
  3742. "Matches any of the TODO state keywords except the last one.")
  3743. (make-variable-buffer-local 'org-not-done-regexp)
  3744. (defvar org-todo-line-regexp nil
  3745. "Matches a headline and puts TODO state into group 2 if present.")
  3746. (make-variable-buffer-local 'org-todo-line-regexp)
  3747. (defvar org-complex-heading-regexp nil
  3748. "Matches a headline and puts everything into groups:
  3749. group 1: the stars
  3750. group 2: The todo keyword, maybe
  3751. group 3: Priority cookie
  3752. group 4: True headline
  3753. group 5: Tags")
  3754. (make-variable-buffer-local 'org-complex-heading-regexp)
  3755. (defvar org-todo-line-tags-regexp nil
  3756. "Matches a headline and puts TODO state into group 2 if present.
  3757. Also put tags into group 4 if tags are present.")
  3758. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3759. (defvar org-nl-done-regexp nil
  3760. "Matches newline followed by a headline with the DONE keyword.")
  3761. (make-variable-buffer-local 'org-nl-done-regexp)
  3762. (defvar org-looking-at-done-regexp nil
  3763. "Matches the DONE keyword a point.")
  3764. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3765. (defvar org-ds-keyword-length 12
  3766. "Maximum length of the Deadline and SCHEDULED keywords.")
  3767. (make-variable-buffer-local 'org-ds-keyword-length)
  3768. (defvar org-deadline-regexp nil
  3769. "Matches the DEADLINE keyword.")
  3770. (make-variable-buffer-local 'org-deadline-regexp)
  3771. (defvar org-deadline-time-regexp nil
  3772. "Matches the DEADLINE keyword together with a time stamp.")
  3773. (make-variable-buffer-local 'org-deadline-time-regexp)
  3774. (defvar org-deadline-line-regexp nil
  3775. "Matches the DEADLINE keyword and the rest of the line.")
  3776. (make-variable-buffer-local 'org-deadline-line-regexp)
  3777. (defvar org-scheduled-regexp nil
  3778. "Matches the SCHEDULED keyword.")
  3779. (make-variable-buffer-local 'org-scheduled-regexp)
  3780. (defvar org-scheduled-time-regexp nil
  3781. "Matches the SCHEDULED keyword together with a time stamp.")
  3782. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3783. (defvar org-closed-time-regexp nil
  3784. "Matches the CLOSED keyword together with a time stamp.")
  3785. (make-variable-buffer-local 'org-closed-time-regexp)
  3786. (defvar org-keyword-time-regexp nil
  3787. "Matches any of the 4 keywords, together with the time stamp.")
  3788. (make-variable-buffer-local 'org-keyword-time-regexp)
  3789. (defvar org-keyword-time-not-clock-regexp nil
  3790. "Matches any of the 3 keywords, together with the time stamp.")
  3791. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3792. (defvar org-maybe-keyword-time-regexp nil
  3793. "Matches a timestamp, possibly preceeded by a keyword.")
  3794. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3795. (defvar org-planning-or-clock-line-re nil
  3796. "Matches a line with planning or clock info.")
  3797. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3798. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3799. rear-nonsticky t mouse-map t fontified t)
  3800. "Properties to remove when a string without properties is wanted.")
  3801. (defsubst org-match-string-no-properties (num &optional string)
  3802. (if (featurep 'xemacs)
  3803. (let ((s (match-string num string)))
  3804. (remove-text-properties 0 (length s) org-rm-props s)
  3805. s)
  3806. (match-string-no-properties num string)))
  3807. (defsubst org-no-properties (s)
  3808. (if (fboundp 'set-text-properties)
  3809. (set-text-properties 0 (length s) nil s)
  3810. (remove-text-properties 0 (length s) org-rm-props s))
  3811. s)
  3812. (defsubst org-get-alist-option (option key)
  3813. (cond ((eq key t) t)
  3814. ((eq option t) t)
  3815. ((assoc key option) (cdr (assoc key option)))
  3816. (t (cdr (assq 'default option)))))
  3817. (defsubst org-inhibit-invisibility ()
  3818. "Modified `buffer-invisibility-spec' for Emacs 21.
  3819. Some ops with invisible text do not work correctly on Emacs 21. For these
  3820. we turn off invisibility temporarily. Use this in a `let' form."
  3821. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3822. (defsubst org-set-local (var value)
  3823. "Make VAR local in current buffer and set it to VALUE."
  3824. (set (make-variable-buffer-local var) value))
  3825. (defsubst org-mode-p ()
  3826. "Check if the current buffer is in Org-mode."
  3827. (eq major-mode 'org-mode))
  3828. (defsubst org-last (list)
  3829. "Return the last element of LIST."
  3830. (car (last list)))
  3831. (defun org-let (list &rest body)
  3832. (eval (cons 'let (cons list body))))
  3833. (put 'org-let 'lisp-indent-function 1)
  3834. (defun org-let2 (list1 list2 &rest body)
  3835. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3836. (put 'org-let2 'lisp-indent-function 2)
  3837. (defconst org-startup-options
  3838. '(("fold" org-startup-folded t)
  3839. ("overview" org-startup-folded t)
  3840. ("nofold" org-startup-folded nil)
  3841. ("showall" org-startup-folded nil)
  3842. ("content" org-startup-folded content)
  3843. ("hidestars" org-hide-leading-stars t)
  3844. ("showstars" org-hide-leading-stars nil)
  3845. ("odd" org-odd-levels-only t)
  3846. ("oddeven" org-odd-levels-only nil)
  3847. ("align" org-startup-align-all-tables t)
  3848. ("noalign" org-startup-align-all-tables nil)
  3849. ("customtime" org-display-custom-times t)
  3850. ("logging" org-log-done t)
  3851. ("logdone" org-log-done t)
  3852. ("nologging" org-log-done nil)
  3853. ("lognotedone" org-log-done done push)
  3854. ("lognotestate" org-log-done state push)
  3855. ("lognoteclock-out" org-log-done clock-out push)
  3856. ("logrepeat" org-log-repeat t)
  3857. ("nologrepeat" org-log-repeat nil)
  3858. ("constcgs" constants-unit-system cgs)
  3859. ("constSI" constants-unit-system SI))
  3860. "Variable associated with STARTUP options for org-mode.
  3861. Each element is a list of three items: The startup options as written
  3862. in the #+STARTUP line, the corresponding variable, and the value to
  3863. set this variable to if the option is found. An optional forth element PUSH
  3864. means to push this value onto the list in the variable.")
  3865. (defun org-set-regexps-and-options ()
  3866. "Precompute regular expressions for current buffer."
  3867. (when (org-mode-p)
  3868. (org-set-local 'org-todo-kwd-alist nil)
  3869. (org-set-local 'org-todo-key-alist nil)
  3870. (org-set-local 'org-todo-key-trigger nil)
  3871. (org-set-local 'org-todo-keywords-1 nil)
  3872. (org-set-local 'org-done-keywords nil)
  3873. (org-set-local 'org-todo-heads nil)
  3874. (org-set-local 'org-todo-sets nil)
  3875. (org-set-local 'org-todo-log-states nil)
  3876. (let ((re (org-make-options-regexp
  3877. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3878. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3879. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3880. (splitre "[ \t]+")
  3881. kwds kws0 kwsa key value cat arch tags const links hw dws
  3882. tail sep kws1 prio props drawers
  3883. ex log)
  3884. (save-excursion
  3885. (save-restriction
  3886. (widen)
  3887. (goto-char (point-min))
  3888. (while (re-search-forward re nil t)
  3889. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3890. (cond
  3891. ((equal key "CATEGORY")
  3892. (if (string-match "[ \t]+$" value)
  3893. (setq value (replace-match "" t t value)))
  3894. (setq cat value))
  3895. ((member key '("SEQ_TODO" "TODO"))
  3896. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3897. ((equal key "TYP_TODO")
  3898. (push (cons 'type (org-split-string value splitre)) kwds))
  3899. ((equal key "TAGS")
  3900. (setq tags (append tags (org-split-string value splitre))))
  3901. ((equal key "COLUMNS")
  3902. (org-set-local 'org-columns-default-format value))
  3903. ((equal key "LINK")
  3904. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3905. (push (cons (match-string 1 value)
  3906. (org-trim (match-string 2 value)))
  3907. links)))
  3908. ((equal key "PRIORITIES")
  3909. (setq prio (org-split-string value " +")))
  3910. ((equal key "PROPERTY")
  3911. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3912. (push (cons (match-string 1 value) (match-string 2 value))
  3913. props)))
  3914. ((equal key "DRAWERS")
  3915. (setq drawers (org-split-string value splitre)))
  3916. ((equal key "CONSTANTS")
  3917. (setq const (append const (org-split-string value splitre))))
  3918. ((equal key "STARTUP")
  3919. (let ((opts (org-split-string value splitre))
  3920. l var val)
  3921. (while (setq l (pop opts))
  3922. (when (setq l (assoc l org-startup-options))
  3923. (setq var (nth 1 l) val (nth 2 l))
  3924. (if (not (nth 3 l))
  3925. (set (make-local-variable var) val)
  3926. (if (not (listp (symbol-value var)))
  3927. (set (make-local-variable var) nil))
  3928. (set (make-local-variable var) (symbol-value var))
  3929. (add-to-list var val))))))
  3930. ((equal key "ARCHIVE")
  3931. (string-match " *$" value)
  3932. (setq arch (replace-match "" t t value))
  3933. (remove-text-properties 0 (length arch)
  3934. '(face t fontified t) arch)))
  3935. )))
  3936. (when cat
  3937. (org-set-local 'org-category (intern cat))
  3938. (push (cons "CATEGORY" cat) props))
  3939. (when prio
  3940. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3941. (setq prio (mapcar 'string-to-char prio))
  3942. (org-set-local 'org-highest-priority (nth 0 prio))
  3943. (org-set-local 'org-lowest-priority (nth 1 prio))
  3944. (org-set-local 'org-default-priority (nth 2 prio)))
  3945. (and props (org-set-local 'org-local-properties (nreverse props)))
  3946. (and drawers (org-set-local 'org-drawers drawers))
  3947. (and arch (org-set-local 'org-archive-location arch))
  3948. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3949. ;; Process the TODO keywords
  3950. (unless kwds
  3951. ;; Use the global values as if they had been given locally.
  3952. (setq kwds (default-value 'org-todo-keywords))
  3953. (if (stringp (car kwds))
  3954. (setq kwds (list (cons org-todo-interpretation
  3955. (default-value 'org-todo-keywords)))))
  3956. (setq kwds (reverse kwds)))
  3957. (setq kwds (nreverse kwds))
  3958. (let (inter kws kw)
  3959. (while (setq kws (pop kwds))
  3960. (setq inter (pop kws) sep (member "|" kws)
  3961. kws0 (delete "|" (copy-sequence kws))
  3962. kwsa nil
  3963. kws1 (mapcar
  3964. (lambda (x)
  3965. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3966. (progn
  3967. (setq kw (match-string 1 x)
  3968. ex (and (match-end 2) (match-string 2 x))
  3969. log (and ex (string-match "@" ex))
  3970. key (and ex (substring ex 0 1)))
  3971. (if (equal key "@") (setq key nil))
  3972. (push (cons kw (and key (string-to-char key))) kwsa)
  3973. (and log (push kw org-todo-log-states))
  3974. kw)
  3975. (error "Invalid TODO keyword %s" x)))
  3976. kws0)
  3977. kwsa (if kwsa (append '((:startgroup))
  3978. (nreverse kwsa)
  3979. '((:endgroup))))
  3980. hw (car kws1)
  3981. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3982. tail (list inter hw (car dws) (org-last dws)))
  3983. (add-to-list 'org-todo-heads hw 'append)
  3984. (push kws1 org-todo-sets)
  3985. (setq org-done-keywords (append org-done-keywords dws nil))
  3986. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3987. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3988. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3989. (setq org-todo-sets (nreverse org-todo-sets)
  3990. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3991. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3992. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3993. ;; Process the constants
  3994. (when const
  3995. (let (e cst)
  3996. (while (setq e (pop const))
  3997. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3998. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3999. (setq org-table-formula-constants-local cst)))
  4000. ;; Process the tags.
  4001. (when tags
  4002. (let (e tgs)
  4003. (while (setq e (pop tags))
  4004. (cond
  4005. ((equal e "{") (push '(:startgroup) tgs))
  4006. ((equal e "}") (push '(:endgroup) tgs))
  4007. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4008. (push (cons (match-string 1 e)
  4009. (string-to-char (match-string 2 e)))
  4010. tgs))
  4011. (t (push (list e) tgs))))
  4012. (org-set-local 'org-tag-alist nil)
  4013. (while (setq e (pop tgs))
  4014. (or (and (stringp (car e))
  4015. (assoc (car e) org-tag-alist))
  4016. (push e org-tag-alist))))))
  4017. ;; Compute the regular expressions and other local variables
  4018. (if (not org-done-keywords)
  4019. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4020. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4021. (length org-scheduled-string)))
  4022. org-drawer-regexp
  4023. (concat "^[ \t]*:\\("
  4024. (mapconcat 'regexp-quote org-drawers "\\|")
  4025. "\\):[ \t]*$")
  4026. org-not-done-keywords
  4027. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4028. org-todo-regexp
  4029. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4030. "\\|") "\\)\\>")
  4031. org-not-done-regexp
  4032. (concat "\\<\\("
  4033. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4034. "\\)\\>")
  4035. org-todo-line-regexp
  4036. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4037. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4038. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4039. org-complex-heading-regexp
  4040. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4041. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4042. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4043. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4044. org-nl-done-regexp
  4045. (concat "\n\\*+[ \t]+"
  4046. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4047. "\\)" "\\>")
  4048. org-todo-line-tags-regexp
  4049. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4050. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4051. (org-re
  4052. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4053. org-looking-at-done-regexp
  4054. (concat "^" "\\(?:"
  4055. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4056. "\\>")
  4057. org-deadline-regexp (concat "\\<" org-deadline-string)
  4058. org-deadline-time-regexp
  4059. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4060. org-deadline-line-regexp
  4061. (concat "\\<\\(" org-deadline-string "\\).*")
  4062. org-scheduled-regexp
  4063. (concat "\\<" org-scheduled-string)
  4064. org-scheduled-time-regexp
  4065. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4066. org-closed-time-regexp
  4067. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4068. org-keyword-time-regexp
  4069. (concat "\\<\\(" org-scheduled-string
  4070. "\\|" org-deadline-string
  4071. "\\|" org-closed-string
  4072. "\\|" org-clock-string "\\)"
  4073. " *[[<]\\([^]>]+\\)[]>]")
  4074. org-keyword-time-not-clock-regexp
  4075. (concat "\\<\\(" org-scheduled-string
  4076. "\\|" org-deadline-string
  4077. "\\|" org-closed-string
  4078. "\\)"
  4079. " *[[<]\\([^]>]+\\)[]>]")
  4080. org-maybe-keyword-time-regexp
  4081. (concat "\\(\\<\\(" org-scheduled-string
  4082. "\\|" org-deadline-string
  4083. "\\|" org-closed-string
  4084. "\\|" org-clock-string "\\)\\)?"
  4085. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4086. org-planning-or-clock-line-re
  4087. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4088. "\\|" org-deadline-string
  4089. "\\|" org-closed-string "\\|" org-clock-string
  4090. "\\)\\>\\)")
  4091. )
  4092. (org-compute-latex-and-specials-regexp)
  4093. (org-set-font-lock-defaults)))
  4094. (defun org-remove-keyword-keys (list)
  4095. (mapcar (lambda (x)
  4096. (if (string-match "(..?)$" x)
  4097. (substring x 0 (match-beginning 0))
  4098. x))
  4099. list))
  4100. ;; FIXME: this could be done much better, using second characters etc.
  4101. (defun org-assign-fast-keys (alist)
  4102. "Assign fast keys to a keyword-key alist.
  4103. Respect keys that are already there."
  4104. (let (new e k c c1 c2 (char ?a))
  4105. (while (setq e (pop alist))
  4106. (cond
  4107. ((equal e '(:startgroup)) (push e new))
  4108. ((equal e '(:endgroup)) (push e new))
  4109. (t
  4110. (setq k (car e) c2 nil)
  4111. (if (cdr e)
  4112. (setq c (cdr e))
  4113. ;; automatically assign a character.
  4114. (setq c1 (string-to-char
  4115. (downcase (substring
  4116. k (if (= (string-to-char k) ?@) 1 0)))))
  4117. (if (or (rassoc c1 new) (rassoc c1 alist))
  4118. (while (or (rassoc char new) (rassoc char alist))
  4119. (setq char (1+ char)))
  4120. (setq c2 c1))
  4121. (setq c (or c2 char)))
  4122. (push (cons k c) new))))
  4123. (nreverse new)))
  4124. ;;; Some variables ujsed in various places
  4125. (defvar org-window-configuration nil
  4126. "Used in various places to store a window configuration.")
  4127. (defvar org-finish-function nil
  4128. "Function to be called when `C-c C-c' is used.
  4129. This is for getting out of special buffers like remember.")
  4130. ;; FIXME: Occasionally check by commenting these, to make sure
  4131. ;; no other functions uses these, forgetting to let-bind them.
  4132. (defvar entry)
  4133. (defvar state)
  4134. (defvar last-state)
  4135. (defvar date)
  4136. (defvar description)
  4137. ;; Defined somewhere in this file, but used before definition.
  4138. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4139. (defvar org-agenda-buffer-name)
  4140. (defvar org-agenda-undo-list)
  4141. (defvar org-agenda-pending-undo-list)
  4142. (defvar org-agenda-overriding-header)
  4143. (defvar orgtbl-mode)
  4144. (defvar org-html-entities)
  4145. (defvar org-struct-menu)
  4146. (defvar org-org-menu)
  4147. (defvar org-tbl-menu)
  4148. (defvar org-agenda-keymap)
  4149. ;;;; Emacs/XEmacs compatibility
  4150. ;; Overlay compatibility functions
  4151. (defun org-make-overlay (beg end &optional buffer)
  4152. (if (featurep 'xemacs)
  4153. (make-extent beg end buffer)
  4154. (make-overlay beg end buffer)))
  4155. (defun org-delete-overlay (ovl)
  4156. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4157. (defun org-detach-overlay (ovl)
  4158. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4159. (defun org-move-overlay (ovl beg end &optional buffer)
  4160. (if (featurep 'xemacs)
  4161. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4162. (move-overlay ovl beg end buffer)))
  4163. (defun org-overlay-put (ovl prop value)
  4164. (if (featurep 'xemacs)
  4165. (set-extent-property ovl prop value)
  4166. (overlay-put ovl prop value)))
  4167. (defun org-overlay-display (ovl text &optional face evap)
  4168. "Make overlay OVL display TEXT with face FACE."
  4169. (if (featurep 'xemacs)
  4170. (let ((gl (make-glyph text)))
  4171. (and face (set-glyph-face gl face))
  4172. (set-extent-property ovl 'invisible t)
  4173. (set-extent-property ovl 'end-glyph gl))
  4174. (overlay-put ovl 'display text)
  4175. (if face (overlay-put ovl 'face face))
  4176. (if evap (overlay-put ovl 'evaporate t))))
  4177. (defun org-overlay-before-string (ovl text &optional face evap)
  4178. "Make overlay OVL display TEXT with face FACE."
  4179. (if (featurep 'xemacs)
  4180. (let ((gl (make-glyph text)))
  4181. (and face (set-glyph-face gl face))
  4182. (set-extent-property ovl 'begin-glyph gl))
  4183. (if face (org-add-props text nil 'face face))
  4184. (overlay-put ovl 'before-string text)
  4185. (if evap (overlay-put ovl 'evaporate t))))
  4186. (defun org-overlay-get (ovl prop)
  4187. (if (featurep 'xemacs)
  4188. (extent-property ovl prop)
  4189. (overlay-get ovl prop)))
  4190. (defun org-overlays-at (pos)
  4191. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4192. (defun org-overlays-in (&optional start end)
  4193. (if (featurep 'xemacs)
  4194. (extent-list nil start end)
  4195. (overlays-in start end)))
  4196. (defun org-overlay-start (o)
  4197. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4198. (defun org-overlay-end (o)
  4199. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4200. (defun org-find-overlays (prop &optional pos delete)
  4201. "Find all overlays specifying PROP at POS or point.
  4202. If DELETE is non-nil, delete all those overlays."
  4203. (let ((overlays (org-overlays-at (or pos (point))))
  4204. ov found)
  4205. (while (setq ov (pop overlays))
  4206. (if (org-overlay-get ov prop)
  4207. (if delete (org-delete-overlay ov) (push ov found))))
  4208. found))
  4209. ;; Region compatibility
  4210. (defun org-add-hook (hook function &optional append local)
  4211. "Add-hook, compatible with both Emacsen."
  4212. (if (and local (featurep 'xemacs))
  4213. (add-local-hook hook function append)
  4214. (add-hook hook function append local)))
  4215. (defvar org-ignore-region nil
  4216. "To temporarily disable the active region.")
  4217. (defun org-region-active-p ()
  4218. "Is `transient-mark-mode' on and the region active?
  4219. Works on both Emacs and XEmacs."
  4220. (if org-ignore-region
  4221. nil
  4222. (if (featurep 'xemacs)
  4223. (and zmacs-regions (region-active-p))
  4224. (if (fboundp 'use-region-p)
  4225. (use-region-p)
  4226. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4227. ;; Invisibility compatibility
  4228. (defun org-add-to-invisibility-spec (arg)
  4229. "Add elements to `buffer-invisibility-spec'.
  4230. See documentation for `buffer-invisibility-spec' for the kind of elements
  4231. that can be added."
  4232. (cond
  4233. ((fboundp 'add-to-invisibility-spec)
  4234. (add-to-invisibility-spec arg))
  4235. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4236. (setq buffer-invisibility-spec (list arg)))
  4237. (t
  4238. (setq buffer-invisibility-spec
  4239. (cons arg buffer-invisibility-spec)))))
  4240. (defun org-remove-from-invisibility-spec (arg)
  4241. "Remove elements from `buffer-invisibility-spec'."
  4242. (if (fboundp 'remove-from-invisibility-spec)
  4243. (remove-from-invisibility-spec arg)
  4244. (if (consp buffer-invisibility-spec)
  4245. (setq buffer-invisibility-spec
  4246. (delete arg buffer-invisibility-spec)))))
  4247. (defun org-in-invisibility-spec-p (arg)
  4248. "Is ARG a member of `buffer-invisibility-spec'?"
  4249. (if (consp buffer-invisibility-spec)
  4250. (member arg buffer-invisibility-spec)
  4251. nil))
  4252. ;;;; Define the Org-mode
  4253. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4254. (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."))
  4255. ;; We use a before-change function to check if a table might need
  4256. ;; an update.
  4257. (defvar org-table-may-need-update t
  4258. "Indicates that a table might need an update.
  4259. This variable is set by `org-before-change-function'.
  4260. `org-table-align' sets it back to nil.")
  4261. (defvar org-mode-map)
  4262. (defvar org-mode-hook nil)
  4263. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4264. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4265. (defvar org-table-buffer-is-an nil)
  4266. (defconst org-outline-regexp "\\*+ ")
  4267. ;;;###autoload
  4268. (define-derived-mode org-mode outline-mode "Org"
  4269. "Outline-based notes management and organizer, alias
  4270. \"Carsten's outline-mode for keeping track of everything.\"
  4271. Org-mode develops organizational tasks around a NOTES file which
  4272. contains information about projects as plain text. Org-mode is
  4273. implemented on top of outline-mode, which is ideal to keep the content
  4274. of large files well structured. It supports ToDo items, deadlines and
  4275. time stamps, which magically appear in the diary listing of the Emacs
  4276. calendar. Tables are easily created with a built-in table editor.
  4277. Plain text URL-like links connect to websites, emails (VM), Usenet
  4278. messages (Gnus), BBDB entries, and any files related to the project.
  4279. For printing and sharing of notes, an Org-mode file (or a part of it)
  4280. can be exported as a structured ASCII or HTML file.
  4281. The following commands are available:
  4282. \\{org-mode-map}"
  4283. ;; Get rid of Outline menus, they are not needed
  4284. ;; Need to do this here because define-derived-mode sets up
  4285. ;; the keymap so late. Still, it is a waste to call this each time
  4286. ;; we switch another buffer into org-mode.
  4287. (if (featurep 'xemacs)
  4288. (when (boundp 'outline-mode-menu-heading)
  4289. ;; Assume this is Greg's port, it used easymenu
  4290. (easy-menu-remove outline-mode-menu-heading)
  4291. (easy-menu-remove outline-mode-menu-show)
  4292. (easy-menu-remove outline-mode-menu-hide))
  4293. (define-key org-mode-map [menu-bar headings] 'undefined)
  4294. (define-key org-mode-map [menu-bar hide] 'undefined)
  4295. (define-key org-mode-map [menu-bar show] 'undefined))
  4296. (easy-menu-add org-org-menu)
  4297. (easy-menu-add org-tbl-menu)
  4298. (org-install-agenda-files-menu)
  4299. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4300. (org-add-to-invisibility-spec '(org-cwidth))
  4301. (when (featurep 'xemacs)
  4302. (org-set-local 'line-move-ignore-invisible t))
  4303. (org-set-local 'outline-regexp org-outline-regexp)
  4304. (org-set-local 'outline-level 'org-outline-level)
  4305. (when (and org-ellipsis
  4306. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4307. (fboundp 'make-glyph-code))
  4308. (unless org-display-table
  4309. (setq org-display-table (make-display-table)))
  4310. (set-display-table-slot
  4311. org-display-table 4
  4312. (vconcat (mapcar
  4313. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4314. org-ellipsis)))
  4315. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4316. (setq buffer-display-table org-display-table))
  4317. (org-set-regexps-and-options)
  4318. ;; Calc embedded
  4319. (org-set-local 'calc-embedded-open-mode "# ")
  4320. (modify-syntax-entry ?# "<")
  4321. (modify-syntax-entry ?@ "w")
  4322. (if org-startup-truncated (setq truncate-lines t))
  4323. (org-set-local 'font-lock-unfontify-region-function
  4324. 'org-unfontify-region)
  4325. ;; Activate before-change-function
  4326. (org-set-local 'org-table-may-need-update t)
  4327. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4328. 'local)
  4329. ;; Check for running clock before killing a buffer
  4330. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4331. ;; Paragraphs and auto-filling
  4332. (org-set-autofill-regexps)
  4333. (setq indent-line-function 'org-indent-line-function)
  4334. (org-update-radio-target-regexp)
  4335. ;; Comment characters
  4336. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4337. (org-set-local 'comment-padding " ")
  4338. ;; Align options lines
  4339. (org-set-local
  4340. 'align-mode-rules-list
  4341. '((org-in-buffer-settings
  4342. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4343. (modes . '(org-mode)))))
  4344. ;; Imenu
  4345. (org-set-local 'imenu-create-index-function
  4346. 'org-imenu-get-tree)
  4347. ;; Make isearch reveal context
  4348. (if (or (featurep 'xemacs)
  4349. (not (boundp 'outline-isearch-open-invisible-function)))
  4350. ;; Emacs 21 and XEmacs make use of the hook
  4351. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4352. ;; Emacs 22 deals with this through a special variable
  4353. (org-set-local 'outline-isearch-open-invisible-function
  4354. (lambda (&rest ignore) (org-show-context 'isearch))))
  4355. ;; If empty file that did not turn on org-mode automatically, make it to.
  4356. (if (and org-insert-mode-line-in-empty-file
  4357. (interactive-p)
  4358. (= (point-min) (point-max)))
  4359. (insert "# -*- mode: org -*-\n\n"))
  4360. (unless org-inhibit-startup
  4361. (when org-startup-align-all-tables
  4362. (let ((bmp (buffer-modified-p)))
  4363. (org-table-map-tables 'org-table-align)
  4364. (set-buffer-modified-p bmp)))
  4365. (org-cycle-hide-drawers 'all)
  4366. (cond
  4367. ((eq org-startup-folded t)
  4368. (org-cycle '(4)))
  4369. ((eq org-startup-folded 'content)
  4370. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4371. (org-cycle '(4)) (org-cycle '(4)))))))
  4372. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4373. (defsubst org-call-with-arg (command arg)
  4374. "Call COMMAND interactively, but pretend prefix are was ARG."
  4375. (let ((current-prefix-arg arg)) (call-interactively command)))
  4376. (defsubst org-current-line (&optional pos)
  4377. (save-excursion
  4378. (and pos (goto-char pos))
  4379. ;; works also in narrowed buffer, because we start at 1, not point-min
  4380. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4381. (defun org-current-time ()
  4382. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4383. (if (> org-time-stamp-rounding-minutes 0)
  4384. (let ((r org-time-stamp-rounding-minutes)
  4385. (time (decode-time)))
  4386. (apply 'encode-time
  4387. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4388. (nthcdr 2 time))))
  4389. (current-time)))
  4390. (defun org-add-props (string plist &rest props)
  4391. "Add text properties to entire string, from beginning to end.
  4392. PLIST may be a list of properties, PROPS are individual properties and values
  4393. that will be added to PLIST. Returns the string that was modified."
  4394. (add-text-properties
  4395. 0 (length string) (if props (append plist props) plist) string)
  4396. string)
  4397. (put 'org-add-props 'lisp-indent-function 2)
  4398. ;;;; Font-Lock stuff, including the activators
  4399. (defvar org-mouse-map (make-sparse-keymap))
  4400. (org-defkey org-mouse-map
  4401. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4402. (org-defkey org-mouse-map
  4403. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4404. (when org-mouse-1-follows-link
  4405. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4406. (when org-tab-follows-link
  4407. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4408. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4409. (when org-return-follows-link
  4410. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4411. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4412. (require 'font-lock)
  4413. (defconst org-non-link-chars "]\t\n\r<>")
  4414. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4415. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4416. (defvar org-link-re-with-space nil
  4417. "Matches a link with spaces, optional angular brackets around it.")
  4418. (defvar org-link-re-with-space2 nil
  4419. "Matches a link with spaces, optional angular brackets around it.")
  4420. (defvar org-angle-link-re nil
  4421. "Matches link with angular brackets, spaces are allowed.")
  4422. (defvar org-plain-link-re nil
  4423. "Matches plain link, without spaces.")
  4424. (defvar org-bracket-link-regexp nil
  4425. "Matches a link in double brackets.")
  4426. (defvar org-bracket-link-analytic-regexp nil
  4427. "Regular expression used to analyze links.
  4428. Here is what the match groups contain after a match:
  4429. 1: http:
  4430. 2: http
  4431. 3: path
  4432. 4: [desc]
  4433. 5: desc")
  4434. (defvar org-any-link-re nil
  4435. "Regular expression matching any link.")
  4436. (defun org-make-link-regexps ()
  4437. "Update the link regular expressions.
  4438. This should be called after the variable `org-link-types' has changed."
  4439. (setq org-link-re-with-space
  4440. (concat
  4441. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4442. "\\([^" org-non-link-chars " ]"
  4443. "[^" org-non-link-chars "]*"
  4444. "[^" org-non-link-chars " ]\\)>?")
  4445. org-link-re-with-space2
  4446. (concat
  4447. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4448. "\\([^" org-non-link-chars " ]"
  4449. "[^]\t\n\r]*"
  4450. "[^" org-non-link-chars " ]\\)>?")
  4451. org-angle-link-re
  4452. (concat
  4453. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4454. "\\([^" org-non-link-chars " ]"
  4455. "[^" org-non-link-chars "]*"
  4456. "\\)>")
  4457. org-plain-link-re
  4458. (concat
  4459. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4460. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4461. org-bracket-link-regexp
  4462. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4463. org-bracket-link-analytic-regexp
  4464. (concat
  4465. "\\[\\["
  4466. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4467. "\\([^]]+\\)"
  4468. "\\]"
  4469. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4470. "\\]")
  4471. org-any-link-re
  4472. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4473. org-angle-link-re "\\)\\|\\("
  4474. org-plain-link-re "\\)")))
  4475. (org-make-link-regexps)
  4476. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4477. "Regular expression for fast time stamp matching.")
  4478. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4479. "Regular expression for fast time stamp matching.")
  4480. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4481. "Regular expression matching time strings for analysis.
  4482. This one does not require the space after the date.")
  4483. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4484. "Regular expression matching time strings for analysis.")
  4485. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4486. "Regular expression matching time stamps, with groups.")
  4487. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4488. "Regular expression matching time stamps (also [..]), with groups.")
  4489. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4490. "Regular expression matching a time stamp range.")
  4491. (defconst org-tr-regexp-both
  4492. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4493. "Regular expression matching a time stamp range.")
  4494. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4495. org-ts-regexp "\\)?")
  4496. "Regular expression matching a time stamp or time stamp range.")
  4497. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4498. org-ts-regexp-both "\\)?")
  4499. "Regular expression matching a time stamp or time stamp range.
  4500. The time stamps may be either active or inactive.")
  4501. (defvar org-emph-face nil)
  4502. (defun org-do-emphasis-faces (limit)
  4503. "Run through the buffer and add overlays to links."
  4504. (let (rtn)
  4505. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4506. (if (not (= (char-after (match-beginning 3))
  4507. (char-after (match-beginning 4))))
  4508. (progn
  4509. (setq rtn t)
  4510. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4511. 'face
  4512. (nth 1 (assoc (match-string 3)
  4513. org-emphasis-alist)))
  4514. (add-text-properties (match-beginning 2) (match-end 2)
  4515. '(font-lock-multiline t))
  4516. (when org-hide-emphasis-markers
  4517. (add-text-properties (match-end 4) (match-beginning 5)
  4518. '(invisible org-link))
  4519. (add-text-properties (match-beginning 3) (match-end 3)
  4520. '(invisible org-link)))))
  4521. (backward-char 1))
  4522. rtn))
  4523. (defun org-emphasize (&optional char)
  4524. "Insert or change an emphasis, i.e. a font like bold or italic.
  4525. If there is an active region, change that region to a new emphasis.
  4526. If there is no region, just insert the marker characters and position
  4527. the cursor between them.
  4528. CHAR should be either the marker character, or the first character of the
  4529. HTML tag associated with that emphasis. If CHAR is a space, the means
  4530. to remove the emphasis of the selected region.
  4531. If char is not given (for example in an interactive call) it
  4532. will be prompted for."
  4533. (interactive)
  4534. (let ((eal org-emphasis-alist) e det
  4535. (erc org-emphasis-regexp-components)
  4536. (prompt "")
  4537. (string "") beg end move tag c s)
  4538. (if (org-region-active-p)
  4539. (setq beg (region-beginning) end (region-end)
  4540. string (buffer-substring beg end))
  4541. (setq move t))
  4542. (while (setq e (pop eal))
  4543. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4544. c (aref tag 0))
  4545. (push (cons c (string-to-char (car e))) det)
  4546. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4547. (substring tag 1)))))
  4548. (unless char
  4549. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4550. (setq char (read-char-exclusive)))
  4551. (setq char (or (cdr (assoc char det)) char))
  4552. (if (equal char ?\ )
  4553. (setq s "" move nil)
  4554. (unless (assoc (char-to-string char) org-emphasis-alist)
  4555. (error "No such emphasis marker: \"%c\"" char))
  4556. (setq s (char-to-string char)))
  4557. (while (and (> (length string) 1)
  4558. (equal (substring string 0 1) (substring string -1))
  4559. (assoc (substring string 0 1) org-emphasis-alist))
  4560. (setq string (substring string 1 -1)))
  4561. (setq string (concat s string s))
  4562. (if beg (delete-region beg end))
  4563. (unless (or (bolp)
  4564. (string-match (concat "[" (nth 0 erc) "\n]")
  4565. (char-to-string (char-before (point)))))
  4566. (insert " "))
  4567. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4568. (char-to-string (char-after (point))))
  4569. (insert " ") (backward-char 1))
  4570. (insert string)
  4571. (and move (backward-char 1))))
  4572. (defconst org-nonsticky-props
  4573. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4574. (defun org-activate-plain-links (limit)
  4575. "Run through the buffer and add overlays to links."
  4576. (catch 'exit
  4577. (let (f)
  4578. (while (re-search-forward org-plain-link-re limit t)
  4579. (setq f (get-text-property (match-beginning 0) 'face))
  4580. (if (or (eq f 'org-tag)
  4581. (and (listp f) (memq 'org-tag f)))
  4582. nil
  4583. (add-text-properties (match-beginning 0) (match-end 0)
  4584. (list 'mouse-face 'highlight
  4585. 'rear-nonsticky org-nonsticky-props
  4586. 'keymap org-mouse-map
  4587. ))
  4588. (throw 'exit t))))))
  4589. (defun org-activate-code (limit)
  4590. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4591. (unless (get-text-property (match-beginning 1) 'face)
  4592. (remove-text-properties (match-beginning 0) (match-end 0)
  4593. '(display t invisible t intangible t))
  4594. t)))
  4595. (defun org-activate-angle-links (limit)
  4596. "Run through the buffer and add overlays to links."
  4597. (if (re-search-forward org-angle-link-re limit t)
  4598. (progn
  4599. (add-text-properties (match-beginning 0) (match-end 0)
  4600. (list 'mouse-face 'highlight
  4601. 'rear-nonsticky org-nonsticky-props
  4602. 'keymap org-mouse-map
  4603. ))
  4604. t)))
  4605. (defmacro org-maybe-intangible (props)
  4606. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4607. In emacs 21, invisible text is not avoided by the command loop, so the
  4608. intangible property is needed to make sure point skips this text.
  4609. In Emacs 22, this is not necessary. The intangible text property has
  4610. led to problems with flyspell. These problems are fixed in flyspell.el,
  4611. but we still avoid setting the property in Emacs 22 and later.
  4612. We use a macro so that the test can happen at compilation time."
  4613. (if (< emacs-major-version 22)
  4614. `(append '(intangible t) ,props)
  4615. props))
  4616. (defun org-activate-bracket-links (limit)
  4617. "Run through the buffer and add overlays to bracketed links."
  4618. (if (re-search-forward org-bracket-link-regexp limit t)
  4619. (let* ((help (concat "LINK: "
  4620. (org-match-string-no-properties 1)))
  4621. ;; FIXME: above we should remove the escapes.
  4622. ;; but that requires another match, protecting match data,
  4623. ;; a lot of overhead for font-lock.
  4624. (ip (org-maybe-intangible
  4625. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4626. 'keymap org-mouse-map 'mouse-face 'highlight
  4627. 'font-lock-multiline t 'help-echo help)))
  4628. (vp (list 'rear-nonsticky org-nonsticky-props
  4629. 'keymap org-mouse-map 'mouse-face 'highlight
  4630. ' font-lock-multiline t 'help-echo help)))
  4631. ;; We need to remove the invisible property here. Table narrowing
  4632. ;; may have made some of this invisible.
  4633. (remove-text-properties (match-beginning 0) (match-end 0)
  4634. '(invisible nil))
  4635. (if (match-end 3)
  4636. (progn
  4637. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4638. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4639. (add-text-properties (match-end 3) (match-end 0) ip))
  4640. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4641. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4642. (add-text-properties (match-end 1) (match-end 0) ip))
  4643. t)))
  4644. (defun org-activate-dates (limit)
  4645. "Run through the buffer and add overlays to dates."
  4646. (if (re-search-forward org-tsr-regexp-both limit t)
  4647. (progn
  4648. (add-text-properties (match-beginning 0) (match-end 0)
  4649. (list 'mouse-face 'highlight
  4650. 'rear-nonsticky org-nonsticky-props
  4651. 'keymap org-mouse-map))
  4652. (when org-display-custom-times
  4653. (if (match-end 3)
  4654. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4655. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4656. t)))
  4657. (defvar org-target-link-regexp nil
  4658. "Regular expression matching radio targets in plain text.")
  4659. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4660. "Regular expression matching a link target.")
  4661. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4662. "Regular expression matching a radio target.")
  4663. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4664. "Regular expression matching any target.")
  4665. (defun org-activate-target-links (limit)
  4666. "Run through the buffer and add overlays to target matches."
  4667. (when org-target-link-regexp
  4668. (let ((case-fold-search t))
  4669. (if (re-search-forward org-target-link-regexp limit t)
  4670. (progn
  4671. (add-text-properties (match-beginning 0) (match-end 0)
  4672. (list 'mouse-face 'highlight
  4673. 'rear-nonsticky org-nonsticky-props
  4674. 'keymap org-mouse-map
  4675. 'help-echo "Radio target link"
  4676. 'org-linked-text t))
  4677. t)))))
  4678. (defun org-update-radio-target-regexp ()
  4679. "Find all radio targets in this file and update the regular expression."
  4680. (interactive)
  4681. (when (memq 'radio org-activate-links)
  4682. (setq org-target-link-regexp
  4683. (org-make-target-link-regexp (org-all-targets 'radio)))
  4684. (org-restart-font-lock)))
  4685. (defun org-hide-wide-columns (limit)
  4686. (let (s e)
  4687. (setq s (text-property-any (point) (or limit (point-max))
  4688. 'org-cwidth t))
  4689. (when s
  4690. (setq e (next-single-property-change s 'org-cwidth))
  4691. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4692. (goto-char e)
  4693. t)))
  4694. (defvar org-latex-and-specials-regexp nil
  4695. "Regular expression for highlighting export special stuff.")
  4696. (defvar org-match-substring-regexp)
  4697. (defvar org-match-substring-with-braces-regexp)
  4698. (defvar org-export-html-special-string-regexps)
  4699. (defun org-compute-latex-and-specials-regexp ()
  4700. "Compute regular expression for stuff treated specially by exporters."
  4701. (if (not org-highlight-latex-fragments-and-specials)
  4702. (org-set-local 'org-latex-and-specials-regexp nil)
  4703. (let*
  4704. ((matchers (plist-get org-format-latex-options :matchers))
  4705. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4706. org-latex-regexps)))
  4707. (options (org-combine-plists (org-default-export-plist)
  4708. (org-infile-export-plist)))
  4709. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4710. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4711. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4712. (org-export-html-expand (plist-get options :expand-quoted-html))
  4713. (org-export-with-special-strings (plist-get options :special-strings))
  4714. (re-sub
  4715. (cond
  4716. ((equal org-export-with-sub-superscripts '{})
  4717. (list org-match-substring-with-braces-regexp))
  4718. (org-export-with-sub-superscripts
  4719. (list org-match-substring-regexp))
  4720. (t nil)))
  4721. (re-latex
  4722. (if org-export-with-LaTeX-fragments
  4723. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4724. (re-macros
  4725. (if org-export-with-TeX-macros
  4726. (list (concat "\\\\"
  4727. (regexp-opt
  4728. (append (mapcar 'car org-html-entities)
  4729. (if (boundp 'org-latex-entities)
  4730. org-latex-entities nil))
  4731. 'words))) ; FIXME
  4732. ))
  4733. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4734. (re-special (if org-export-with-special-strings
  4735. (mapcar (lambda (x) (car x))
  4736. org-export-html-special-string-regexps)))
  4737. (re-rest
  4738. (delq nil
  4739. (list
  4740. (if org-export-html-expand "@<[^>\n]+>")
  4741. ))))
  4742. (org-set-local
  4743. 'org-latex-and-specials-regexp
  4744. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4745. re-rest) "\\|")))))
  4746. (defface org-latex-and-export-specials
  4747. (let ((font (cond ((assq :inherit custom-face-attributes)
  4748. '(:inherit underline))
  4749. (t '(:underline t)))))
  4750. `((((class grayscale) (background light))
  4751. (:foreground "DimGray" ,@font))
  4752. (((class grayscale) (background dark))
  4753. (:foreground "LightGray" ,@font))
  4754. (((class color) (background light))
  4755. (:foreground "SaddleBrown"))
  4756. (((class color) (background dark))
  4757. (:foreground "burlywood"))
  4758. (t (,@font))))
  4759. "Face used to highlight math latex and other special exporter stuff."
  4760. :group 'org-faces)
  4761. (defun org-do-latex-and-special-faces (limit)
  4762. "Run through the buffer and add overlays to links."
  4763. (when org-latex-and-specials-regexp
  4764. (let (rtn d)
  4765. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4766. limit t))
  4767. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4768. 'face))
  4769. '(org-code org-verbatim underline)))
  4770. (progn
  4771. (setq rtn t
  4772. d (cond ((member (char-after (1+ (match-beginning 0)))
  4773. '(?_ ?^)) 1)
  4774. (t 0)))
  4775. (font-lock-prepend-text-property
  4776. (+ d (match-beginning 0)) (match-end 0)
  4777. 'face 'org-latex-and-export-specials)
  4778. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4779. '(font-lock-multiline t)))))
  4780. rtn)))
  4781. (defun org-restart-font-lock ()
  4782. "Restart font-lock-mode, to force refontification."
  4783. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4784. (font-lock-mode -1)
  4785. (font-lock-mode 1)))
  4786. (defun org-all-targets (&optional radio)
  4787. "Return a list of all targets in this file.
  4788. With optional argument RADIO, only find radio targets."
  4789. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4790. rtn)
  4791. (save-excursion
  4792. (goto-char (point-min))
  4793. (while (re-search-forward re nil t)
  4794. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4795. rtn)))
  4796. (defun org-make-target-link-regexp (targets)
  4797. "Make regular expression matching all strings in TARGETS.
  4798. The regular expression finds the targets also if there is a line break
  4799. between words."
  4800. (and targets
  4801. (concat
  4802. "\\<\\("
  4803. (mapconcat
  4804. (lambda (x)
  4805. (while (string-match " +" x)
  4806. (setq x (replace-match "\\s-+" t t x)))
  4807. x)
  4808. targets
  4809. "\\|")
  4810. "\\)\\>")))
  4811. (defun org-activate-tags (limit)
  4812. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4813. (progn
  4814. (add-text-properties (match-beginning 1) (match-end 1)
  4815. (list 'mouse-face 'highlight
  4816. 'rear-nonsticky org-nonsticky-props
  4817. 'keymap org-mouse-map))
  4818. t)))
  4819. (defun org-outline-level ()
  4820. (save-excursion
  4821. (looking-at outline-regexp)
  4822. (if (match-beginning 1)
  4823. (+ (org-get-string-indentation (match-string 1)) 1000)
  4824. (1- (- (match-end 0) (match-beginning 0))))))
  4825. (defvar org-font-lock-keywords nil)
  4826. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4827. "Regular expression matching a property line.")
  4828. (defun org-set-font-lock-defaults ()
  4829. (let* ((em org-fontify-emphasized-text)
  4830. (lk org-activate-links)
  4831. (org-font-lock-extra-keywords
  4832. (list
  4833. ;; Headlines
  4834. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4835. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4836. ;; Table lines
  4837. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4838. (1 'org-table t))
  4839. ;; Table internals
  4840. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4841. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4842. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4843. ;; Drawers
  4844. (list org-drawer-regexp '(0 'org-special-keyword t))
  4845. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4846. ;; Properties
  4847. (list org-property-re
  4848. '(1 'org-special-keyword t)
  4849. '(3 'org-property-value t))
  4850. (if org-format-transports-properties-p
  4851. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4852. ;; Links
  4853. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4854. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4855. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4856. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4857. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4858. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4859. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4860. '(org-hide-wide-columns (0 nil append))
  4861. ;; TODO lines
  4862. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4863. '(1 (org-get-todo-face 1) t))
  4864. ;; DONE
  4865. (if org-fontify-done-headline
  4866. (list (concat "^[*]+ +\\<\\("
  4867. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4868. "\\)\\(.*\\)")
  4869. '(2 'org-headline-done t))
  4870. nil)
  4871. ;; Priorities
  4872. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4873. ;; Special keywords
  4874. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4875. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4876. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4877. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4878. ;; Emphasis
  4879. (if em
  4880. (if (featurep 'xemacs)
  4881. '(org-do-emphasis-faces (0 nil append))
  4882. '(org-do-emphasis-faces)))
  4883. ;; Checkboxes
  4884. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4885. 2 'bold prepend)
  4886. (if org-provide-checkbox-statistics
  4887. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4888. (0 (org-get-checkbox-statistics-face) t)))
  4889. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4890. '(1 'org-archived prepend))
  4891. ;; Specials
  4892. '(org-do-latex-and-special-faces)
  4893. ;; Code
  4894. '(org-activate-code (1 'org-code t))
  4895. ;; COMMENT
  4896. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4897. "\\|" org-quote-string "\\)\\>")
  4898. '(1 'org-special-keyword t))
  4899. '("^#.*" (0 'font-lock-comment-face t))
  4900. )))
  4901. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4902. ;; Now set the full font-lock-keywords
  4903. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4904. (org-set-local 'font-lock-defaults
  4905. '(org-font-lock-keywords t nil nil backward-paragraph))
  4906. (kill-local-variable 'font-lock-keywords) nil))
  4907. (defvar org-m nil)
  4908. (defvar org-l nil)
  4909. (defvar org-f nil)
  4910. (defun org-get-level-face (n)
  4911. "Get the right face for match N in font-lock matching of healdines."
  4912. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4913. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4914. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4915. (cond
  4916. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4917. ((eq n 2) org-f)
  4918. (t (if org-level-color-stars-only nil org-f))))
  4919. (defun org-get-todo-face (kwd)
  4920. "Get the right face for a TODO keyword KWD.
  4921. If KWD is a number, get the corresponding match group."
  4922. (if (numberp kwd) (setq kwd (match-string kwd)))
  4923. (or (cdr (assoc kwd org-todo-keyword-faces))
  4924. (and (member kwd org-done-keywords) 'org-done)
  4925. 'org-todo))
  4926. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4927. "Remove fontification and activation overlays from links."
  4928. (font-lock-default-unfontify-region beg end)
  4929. (let* ((buffer-undo-list t)
  4930. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4931. (inhibit-modification-hooks t)
  4932. deactivate-mark buffer-file-name buffer-file-truename)
  4933. (remove-text-properties beg end
  4934. '(mouse-face t keymap t org-linked-text t
  4935. invisible t intangible t))))
  4936. ;;;; Visibility cycling, including org-goto and indirect buffer
  4937. ;;; Cycling
  4938. (defvar org-cycle-global-status nil)
  4939. (make-variable-buffer-local 'org-cycle-global-status)
  4940. (defvar org-cycle-subtree-status nil)
  4941. (make-variable-buffer-local 'org-cycle-subtree-status)
  4942. ;;;###autoload
  4943. (defun org-cycle (&optional arg)
  4944. "Visibility cycling for Org-mode.
  4945. - When this function is called with a prefix argument, rotate the entire
  4946. buffer through 3 states (global cycling)
  4947. 1. OVERVIEW: Show only top-level headlines.
  4948. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4949. 3. SHOW ALL: Show everything.
  4950. - When point is at the beginning of a headline, rotate the subtree started
  4951. by this line through 3 different states (local cycling)
  4952. 1. FOLDED: Only the main headline is shown.
  4953. 2. CHILDREN: The main headline and the direct children are shown.
  4954. From this state, you can move to one of the children
  4955. and zoom in further.
  4956. 3. SUBTREE: Show the entire subtree, including body text.
  4957. - When there is a numeric prefix, go up to a heading with level ARG, do
  4958. a `show-subtree' and return to the previous cursor position. If ARG
  4959. is negative, go up that many levels.
  4960. - When point is not at the beginning of a headline, execute
  4961. `indent-relative', like TAB normally does. See the option
  4962. `org-cycle-emulate-tab' for details.
  4963. - Special case: if point is at the beginning of the buffer and there is
  4964. no headline in line 1, this function will act as if called with prefix arg.
  4965. But only if also the variable `org-cycle-global-at-bob' is t."
  4966. (interactive "P")
  4967. (let* ((outline-regexp
  4968. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4969. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4970. outline-regexp))
  4971. (bob-special (and org-cycle-global-at-bob (bobp)
  4972. (not (looking-at outline-regexp))))
  4973. (org-cycle-hook
  4974. (if bob-special
  4975. (delq 'org-optimize-window-after-visibility-change
  4976. (copy-sequence org-cycle-hook))
  4977. org-cycle-hook))
  4978. (pos (point)))
  4979. (if (or bob-special (equal arg '(4)))
  4980. ;; special case: use global cycling
  4981. (setq arg t))
  4982. (cond
  4983. ((org-at-table-p 'any)
  4984. ;; Enter the table or move to the next field in the table
  4985. (or (org-table-recognize-table.el)
  4986. (progn
  4987. (if arg (org-table-edit-field t)
  4988. (org-table-justify-field-maybe)
  4989. (call-interactively 'org-table-next-field)))))
  4990. ((eq arg t) ;; Global cycling
  4991. (cond
  4992. ((and (eq last-command this-command)
  4993. (eq org-cycle-global-status 'overview))
  4994. ;; We just created the overview - now do table of contents
  4995. ;; This can be slow in very large buffers, so indicate action
  4996. (message "CONTENTS...")
  4997. (org-content)
  4998. (message "CONTENTS...done")
  4999. (setq org-cycle-global-status 'contents)
  5000. (run-hook-with-args 'org-cycle-hook 'contents))
  5001. ((and (eq last-command this-command)
  5002. (eq org-cycle-global-status 'contents))
  5003. ;; We just showed the table of contents - now show everything
  5004. (show-all)
  5005. (message "SHOW ALL")
  5006. (setq org-cycle-global-status 'all)
  5007. (run-hook-with-args 'org-cycle-hook 'all))
  5008. (t
  5009. ;; Default action: go to overview
  5010. (org-overview)
  5011. (message "OVERVIEW")
  5012. (setq org-cycle-global-status 'overview)
  5013. (run-hook-with-args 'org-cycle-hook 'overview))))
  5014. ((and org-drawers org-drawer-regexp
  5015. (save-excursion
  5016. (beginning-of-line 1)
  5017. (looking-at org-drawer-regexp)))
  5018. ;; Toggle block visibility
  5019. (org-flag-drawer
  5020. (not (get-char-property (match-end 0) 'invisible))))
  5021. ((integerp arg)
  5022. ;; Show-subtree, ARG levels up from here.
  5023. (save-excursion
  5024. (org-back-to-heading)
  5025. (outline-up-heading (if (< arg 0) (- arg)
  5026. (- (funcall outline-level) arg)))
  5027. (org-show-subtree)))
  5028. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5029. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5030. ;; At a heading: rotate between three different views
  5031. (org-back-to-heading)
  5032. (let ((goal-column 0) eoh eol eos)
  5033. ;; First, some boundaries
  5034. (save-excursion
  5035. (org-back-to-heading)
  5036. (save-excursion
  5037. (beginning-of-line 2)
  5038. (while (and (not (eobp)) ;; this is like `next-line'
  5039. (get-char-property (1- (point)) 'invisible))
  5040. (beginning-of-line 2)) (setq eol (point)))
  5041. (outline-end-of-heading) (setq eoh (point))
  5042. (org-end-of-subtree t)
  5043. (unless (eobp)
  5044. (skip-chars-forward " \t\n")
  5045. (beginning-of-line 1) ; in case this is an item
  5046. )
  5047. (setq eos (1- (point))))
  5048. ;; Find out what to do next and set `this-command'
  5049. (cond
  5050. ((= eos eoh)
  5051. ;; Nothing is hidden behind this heading
  5052. (message "EMPTY ENTRY")
  5053. (setq org-cycle-subtree-status nil)
  5054. (save-excursion
  5055. (goto-char eos)
  5056. (outline-next-heading)
  5057. (if (org-invisible-p) (org-flag-heading nil))))
  5058. ((or (>= eol eos)
  5059. (not (string-match "\\S-" (buffer-substring eol eos))))
  5060. ;; Entire subtree is hidden in one line: open it
  5061. (org-show-entry)
  5062. (show-children)
  5063. (message "CHILDREN")
  5064. (save-excursion
  5065. (goto-char eos)
  5066. (outline-next-heading)
  5067. (if (org-invisible-p) (org-flag-heading nil)))
  5068. (setq org-cycle-subtree-status 'children)
  5069. (run-hook-with-args 'org-cycle-hook 'children))
  5070. ((and (eq last-command this-command)
  5071. (eq org-cycle-subtree-status 'children))
  5072. ;; We just showed the children, now show everything.
  5073. (org-show-subtree)
  5074. (message "SUBTREE")
  5075. (setq org-cycle-subtree-status 'subtree)
  5076. (run-hook-with-args 'org-cycle-hook 'subtree))
  5077. (t
  5078. ;; Default action: hide the subtree.
  5079. (hide-subtree)
  5080. (message "FOLDED")
  5081. (setq org-cycle-subtree-status 'folded)
  5082. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5083. ;; TAB emulation
  5084. (buffer-read-only (org-back-to-heading))
  5085. ((org-try-cdlatex-tab))
  5086. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5087. (or (not (bolp))
  5088. (not (looking-at outline-regexp))))
  5089. (call-interactively (global-key-binding "\t")))
  5090. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5091. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5092. (or (and (eq org-cycle-emulate-tab 'white)
  5093. (= (match-end 0) (point-at-eol)))
  5094. (and (eq org-cycle-emulate-tab 'whitestart)
  5095. (>= (match-end 0) pos))))
  5096. t
  5097. (eq org-cycle-emulate-tab t))
  5098. ; (if (and (looking-at "[ \n\r\t]")
  5099. ; (string-match "^[ \t]*$" (buffer-substring
  5100. ; (point-at-bol) (point))))
  5101. ; (progn
  5102. ; (beginning-of-line 1)
  5103. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5104. (call-interactively (global-key-binding "\t")))
  5105. (t (save-excursion
  5106. (org-back-to-heading)
  5107. (org-cycle))))))
  5108. ;;;###autoload
  5109. (defun org-global-cycle (&optional arg)
  5110. "Cycle the global visibility. For details see `org-cycle'."
  5111. (interactive "P")
  5112. (let ((org-cycle-include-plain-lists
  5113. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5114. (if (integerp arg)
  5115. (progn
  5116. (show-all)
  5117. (hide-sublevels arg)
  5118. (setq org-cycle-global-status 'contents))
  5119. (org-cycle '(4)))))
  5120. (defun org-overview ()
  5121. "Switch to overview mode, shoing only top-level headlines.
  5122. Really, this shows all headlines with level equal or greater than the level
  5123. of the first headline in the buffer. This is important, because if the
  5124. first headline is not level one, then (hide-sublevels 1) gives confusing
  5125. results."
  5126. (interactive)
  5127. (let ((level (save-excursion
  5128. (goto-char (point-min))
  5129. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5130. (progn
  5131. (goto-char (match-beginning 0))
  5132. (funcall outline-level))))))
  5133. (and level (hide-sublevels level))))
  5134. (defun org-content (&optional arg)
  5135. "Show all headlines in the buffer, like a table of contents.
  5136. With numerical argument N, show content up to level N."
  5137. (interactive "P")
  5138. (save-excursion
  5139. ;; Visit all headings and show their offspring
  5140. (and (integerp arg) (org-overview))
  5141. (goto-char (point-max))
  5142. (catch 'exit
  5143. (while (and (progn (condition-case nil
  5144. (outline-previous-visible-heading 1)
  5145. (error (goto-char (point-min))))
  5146. t)
  5147. (looking-at outline-regexp))
  5148. (if (integerp arg)
  5149. (show-children (1- arg))
  5150. (show-branches))
  5151. (if (bobp) (throw 'exit nil))))))
  5152. (defun org-optimize-window-after-visibility-change (state)
  5153. "Adjust the window after a change in outline visibility.
  5154. This function is the default value of the hook `org-cycle-hook'."
  5155. (when (get-buffer-window (current-buffer))
  5156. (cond
  5157. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5158. ; ((eq state 'overview) (org-beginning-of-line))
  5159. ((eq state 'content) nil)
  5160. ((eq state 'all) nil)
  5161. ((eq state 'folded) nil)
  5162. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5163. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5164. (defun org-compact-display-after-subtree-move ()
  5165. (let (beg end)
  5166. (save-excursion
  5167. (if (org-up-heading-safe)
  5168. (progn
  5169. (hide-subtree)
  5170. (show-entry)
  5171. (show-children)
  5172. (org-cycle-show-empty-lines 'children)
  5173. (org-cycle-hide-drawers 'children))
  5174. (org-overview)))))
  5175. (defun org-cycle-show-empty-lines (state)
  5176. "Show empty lines above all visible headlines.
  5177. The region to be covered depends on STATE when called through
  5178. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5179. entire buffer covered. Note that an empty line is only shown if there
  5180. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5181. (when (> org-cycle-separator-lines 0)
  5182. (save-excursion
  5183. (let* ((n org-cycle-separator-lines)
  5184. (re (cond
  5185. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5186. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5187. (t (let ((ns (number-to-string (- n 2))))
  5188. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5189. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5190. beg end)
  5191. (cond
  5192. ((memq state '(overview contents t))
  5193. (setq beg (point-min) end (point-max)))
  5194. ((memq state '(children folded))
  5195. (setq beg (point) end (progn (org-end-of-subtree t t)
  5196. (beginning-of-line 2)
  5197. (point)))))
  5198. (when beg
  5199. (goto-char beg)
  5200. (while (re-search-forward re end t)
  5201. (if (not (get-char-property (match-end 1) 'invisible))
  5202. (outline-flag-region
  5203. (match-beginning 1) (match-end 1) nil)))))))
  5204. ;; Never hide empty lines at the end of the file.
  5205. (save-excursion
  5206. (goto-char (point-max))
  5207. (outline-previous-heading)
  5208. (outline-end-of-heading)
  5209. (if (and (looking-at "[ \t\n]+")
  5210. (= (match-end 0) (point-max)))
  5211. (outline-flag-region (point) (match-end 0) nil))))
  5212. (defun org-subtree-end-visible-p ()
  5213. "Is the end of the current subtree visible?"
  5214. (pos-visible-in-window-p
  5215. (save-excursion (org-end-of-subtree t) (point))))
  5216. (defun org-first-headline-recenter (&optional N)
  5217. "Move cursor to the first headline and recenter the headline.
  5218. Optional argument N means, put the headline into the Nth line of the window."
  5219. (goto-char (point-min))
  5220. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5221. (beginning-of-line)
  5222. (recenter (prefix-numeric-value N))))
  5223. ;;; Org-goto
  5224. (defvar org-goto-window-configuration nil)
  5225. (defvar org-goto-marker nil)
  5226. (defvar org-goto-map
  5227. (let ((map (make-sparse-keymap)))
  5228. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5229. (while (setq cmd (pop cmds))
  5230. (substitute-key-definition cmd cmd map global-map)))
  5231. (suppress-keymap map)
  5232. (org-defkey map "\C-m" 'org-goto-ret)
  5233. (org-defkey map [(return)] 'org-goto-ret)
  5234. (org-defkey map [(left)] 'org-goto-left)
  5235. (org-defkey map [(right)] 'org-goto-right)
  5236. (org-defkey map [(control ?g)] 'org-goto-quit)
  5237. (org-defkey map "\C-i" 'org-cycle)
  5238. (org-defkey map [(tab)] 'org-cycle)
  5239. (org-defkey map [(down)] 'outline-next-visible-heading)
  5240. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5241. (if org-goto-auto-isearch
  5242. (if (fboundp 'define-key-after)
  5243. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5244. nil)
  5245. (org-defkey map "q" 'org-goto-quit)
  5246. (org-defkey map "n" 'outline-next-visible-heading)
  5247. (org-defkey map "p" 'outline-previous-visible-heading)
  5248. (org-defkey map "f" 'outline-forward-same-level)
  5249. (org-defkey map "b" 'outline-backward-same-level)
  5250. (org-defkey map "u" 'outline-up-heading))
  5251. (org-defkey map "/" 'org-occur)
  5252. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5253. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5254. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5255. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5256. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5257. map))
  5258. (defconst org-goto-help
  5259. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5260. RET=jump to location [Q]uit and return to previous location
  5261. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5262. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5263. (defun org-goto (&optional alternative-interface)
  5264. "Look up a different location in the current file, keeping current visibility.
  5265. When you want look-up or go to a different location in a document, the
  5266. fastest way is often to fold the entire buffer and then dive into the tree.
  5267. This method has the disadvantage, that the previous location will be folded,
  5268. which may not be what you want.
  5269. This command works around this by showing a copy of the current buffer
  5270. in an indirect buffer, in overview mode. You can dive into the tree in
  5271. that copy, use org-occur and incremental search to find a location.
  5272. When pressing RET or `Q', the command returns to the original buffer in
  5273. which the visibility is still unchanged. After RET is will also jump to
  5274. the location selected in the indirect buffer and expose the
  5275. the headline hierarchy above."
  5276. (interactive "P")
  5277. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5278. (org-refile-use-outline-path t)
  5279. (interface
  5280. (if (not alternative-interface)
  5281. org-goto-interface
  5282. (if (eq org-goto-interface 'outline)
  5283. 'outline-path-completion
  5284. 'outline)))
  5285. (org-goto-start-pos (point))
  5286. (selected-point
  5287. (if (eq interface 'outline)
  5288. (car (org-get-location (current-buffer) org-goto-help))
  5289. (nth 3 (org-refile-get-location "Goto: ")))))
  5290. (if selected-point
  5291. (progn
  5292. (org-mark-ring-push org-goto-start-pos)
  5293. (goto-char selected-point)
  5294. (if (or (org-invisible-p) (org-invisible-p2))
  5295. (org-show-context 'org-goto)))
  5296. (message "Quit"))))
  5297. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5298. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5299. (defvar org-goto-local-auto-isearch-map) ; defined below
  5300. (defun org-get-location (buf help)
  5301. "Let the user select a location in the Org-mode buffer BUF.
  5302. This function uses a recursive edit. It returns the selected position
  5303. or nil."
  5304. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5305. (isearch-hide-immediately nil)
  5306. (isearch-search-fun-function
  5307. (lambda () 'org-goto-local-search-forward-headings))
  5308. (org-goto-selected-point org-goto-exit-command))
  5309. (save-excursion
  5310. (save-window-excursion
  5311. (delete-other-windows)
  5312. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5313. (switch-to-buffer
  5314. (condition-case nil
  5315. (make-indirect-buffer (current-buffer) "*org-goto*")
  5316. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5317. (with-output-to-temp-buffer "*Help*"
  5318. (princ help))
  5319. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5320. (setq buffer-read-only nil)
  5321. (let ((org-startup-truncated t)
  5322. (org-startup-folded nil)
  5323. (org-startup-align-all-tables nil))
  5324. (org-mode)
  5325. (org-overview))
  5326. (setq buffer-read-only t)
  5327. (if (and (boundp 'org-goto-start-pos)
  5328. (integer-or-marker-p org-goto-start-pos))
  5329. (let ((org-show-hierarchy-above t)
  5330. (org-show-siblings t)
  5331. (org-show-following-heading t))
  5332. (goto-char org-goto-start-pos)
  5333. (and (org-invisible-p) (org-show-context)))
  5334. (goto-char (point-min)))
  5335. (org-beginning-of-line)
  5336. (message "Select location and press RET")
  5337. (use-local-map org-goto-map)
  5338. (recursive-edit)
  5339. ))
  5340. (kill-buffer "*org-goto*")
  5341. (cons org-goto-selected-point org-goto-exit-command)))
  5342. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5343. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5344. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5345. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5346. (defun org-goto-local-search-forward-headings (string bound noerror)
  5347. "Search and make sure that anu matches are in headlines."
  5348. (catch 'return
  5349. (while (search-forward string bound noerror)
  5350. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5351. (and (member :headline context)
  5352. (not (member :tags context))))
  5353. (throw 'return (point))))))
  5354. (defun org-goto-local-auto-isearch ()
  5355. "Start isearch."
  5356. (interactive)
  5357. (goto-char (point-min))
  5358. (let ((keys (this-command-keys)))
  5359. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5360. (isearch-mode t)
  5361. (isearch-process-search-char (string-to-char keys)))))
  5362. (defun org-goto-ret (&optional arg)
  5363. "Finish `org-goto' by going to the new location."
  5364. (interactive "P")
  5365. (setq org-goto-selected-point (point)
  5366. org-goto-exit-command 'return)
  5367. (throw 'exit nil))
  5368. (defun org-goto-left ()
  5369. "Finish `org-goto' by going to the new location."
  5370. (interactive)
  5371. (if (org-on-heading-p)
  5372. (progn
  5373. (beginning-of-line 1)
  5374. (setq org-goto-selected-point (point)
  5375. org-goto-exit-command 'left)
  5376. (throw 'exit nil))
  5377. (error "Not on a heading")))
  5378. (defun org-goto-right ()
  5379. "Finish `org-goto' by going to the new location."
  5380. (interactive)
  5381. (if (org-on-heading-p)
  5382. (progn
  5383. (setq org-goto-selected-point (point)
  5384. org-goto-exit-command 'right)
  5385. (throw 'exit nil))
  5386. (error "Not on a heading")))
  5387. (defun org-goto-quit ()
  5388. "Finish `org-goto' without cursor motion."
  5389. (interactive)
  5390. (setq org-goto-selected-point nil)
  5391. (setq org-goto-exit-command 'quit)
  5392. (throw 'exit nil))
  5393. ;;; Indirect buffer display of subtrees
  5394. (defvar org-indirect-dedicated-frame nil
  5395. "This is the frame being used for indirect tree display.")
  5396. (defvar org-last-indirect-buffer nil)
  5397. (defun org-tree-to-indirect-buffer (&optional arg)
  5398. "Create indirect buffer and narrow it to current subtree.
  5399. With numerical prefix ARG, go up to this level and then take that tree.
  5400. If ARG is negative, go up that many levels.
  5401. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5402. indirect buffer previously made with this command, to avoid proliferation of
  5403. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5404. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5405. is kept so that you can work with several indirect buffers at the same time.
  5406. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5407. requests that a new frame be made for the new buffer, so that the dedicated
  5408. frame is not changed."
  5409. (interactive "P")
  5410. (let ((cbuf (current-buffer))
  5411. (cwin (selected-window))
  5412. (pos (point))
  5413. beg end level heading ibuf)
  5414. (save-excursion
  5415. (org-back-to-heading t)
  5416. (when (numberp arg)
  5417. (setq level (org-outline-level))
  5418. (if (< arg 0) (setq arg (+ level arg)))
  5419. (while (> (setq level (org-outline-level)) arg)
  5420. (outline-up-heading 1 t)))
  5421. (setq beg (point)
  5422. heading (org-get-heading))
  5423. (org-end-of-subtree t) (setq end (point)))
  5424. (if (and (buffer-live-p org-last-indirect-buffer)
  5425. (not (eq org-indirect-buffer-display 'new-frame))
  5426. (not arg))
  5427. (kill-buffer org-last-indirect-buffer))
  5428. (setq ibuf (org-get-indirect-buffer cbuf)
  5429. org-last-indirect-buffer ibuf)
  5430. (cond
  5431. ((or (eq org-indirect-buffer-display 'new-frame)
  5432. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5433. (select-frame (make-frame))
  5434. (delete-other-windows)
  5435. (switch-to-buffer ibuf)
  5436. (org-set-frame-title heading))
  5437. ((eq org-indirect-buffer-display 'dedicated-frame)
  5438. (raise-frame
  5439. (select-frame (or (and org-indirect-dedicated-frame
  5440. (frame-live-p org-indirect-dedicated-frame)
  5441. org-indirect-dedicated-frame)
  5442. (setq org-indirect-dedicated-frame (make-frame)))))
  5443. (delete-other-windows)
  5444. (switch-to-buffer ibuf)
  5445. (org-set-frame-title (concat "Indirect: " heading)))
  5446. ((eq org-indirect-buffer-display 'current-window)
  5447. (switch-to-buffer ibuf))
  5448. ((eq org-indirect-buffer-display 'other-window)
  5449. (pop-to-buffer ibuf))
  5450. (t (error "Invalid value.")))
  5451. (if (featurep 'xemacs)
  5452. (save-excursion (org-mode) (turn-on-font-lock)))
  5453. (narrow-to-region beg end)
  5454. (show-all)
  5455. (goto-char pos)
  5456. (and (window-live-p cwin) (select-window cwin))))
  5457. (defun org-get-indirect-buffer (&optional buffer)
  5458. (setq buffer (or buffer (current-buffer)))
  5459. (let ((n 1) (base (buffer-name buffer)) bname)
  5460. (while (buffer-live-p
  5461. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5462. (setq n (1+ n)))
  5463. (condition-case nil
  5464. (make-indirect-buffer buffer bname 'clone)
  5465. (error (make-indirect-buffer buffer bname)))))
  5466. (defun org-set-frame-title (title)
  5467. "Set the title of the current frame to the string TITLE."
  5468. ;; FIXME: how to name a single frame in XEmacs???
  5469. (unless (featurep 'xemacs)
  5470. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5471. ;;;; Structure editing
  5472. ;;; Inserting headlines
  5473. (defun org-insert-heading (&optional force-heading)
  5474. "Insert a new heading or item with same depth at point.
  5475. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5476. If point is at the beginning of a headline, insert a sibling before the
  5477. current headline. If point is not at the beginning, do not split the line,
  5478. but create the new hedline after the current line."
  5479. (interactive "P")
  5480. (if (= (buffer-size) 0)
  5481. (insert "\n* ")
  5482. (when (or force-heading (not (org-insert-item)))
  5483. (let* ((head (save-excursion
  5484. (condition-case nil
  5485. (progn
  5486. (org-back-to-heading)
  5487. (match-string 0))
  5488. (error "*"))))
  5489. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5490. pos)
  5491. (cond
  5492. ((and (org-on-heading-p) (bolp)
  5493. (or (bobp)
  5494. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5495. ;; insert before the current line
  5496. (open-line (if blank 2 1)))
  5497. ((and (bolp)
  5498. (or (bobp)
  5499. (save-excursion
  5500. (backward-char 1) (not (org-invisible-p)))))
  5501. ;; insert right here
  5502. nil)
  5503. (t
  5504. ;; in the middle of the line
  5505. (org-show-entry)
  5506. (end-of-line 1)
  5507. (newline (if blank 2 1))))
  5508. (insert head) (just-one-space)
  5509. (setq pos (point))
  5510. (end-of-line 1)
  5511. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5512. (run-hooks 'org-insert-heading-hook)))))
  5513. (defun org-insert-heading-after-current ()
  5514. "Insert a new heading with same level as current, after current subtree."
  5515. (interactive)
  5516. (org-back-to-heading)
  5517. (org-insert-heading)
  5518. (org-move-subtree-down)
  5519. (end-of-line 1))
  5520. (defun org-insert-todo-heading (arg)
  5521. "Insert a new heading with the same level and TODO state as current heading.
  5522. If the heading has no TODO state, or if the state is DONE, use the first
  5523. state (TODO by default). Also with prefix arg, force first state."
  5524. (interactive "P")
  5525. (when (not (org-insert-item 'checkbox))
  5526. (org-insert-heading)
  5527. (save-excursion
  5528. (org-back-to-heading)
  5529. (outline-previous-heading)
  5530. (looking-at org-todo-line-regexp))
  5531. (if (or arg
  5532. (not (match-beginning 2))
  5533. (member (match-string 2) org-done-keywords))
  5534. (insert (car org-todo-keywords-1) " ")
  5535. (insert (match-string 2) " "))))
  5536. (defun org-insert-subheading (arg)
  5537. "Insert a new subheading and demote it.
  5538. Works for outline headings and for plain lists alike."
  5539. (interactive "P")
  5540. (org-insert-heading arg)
  5541. (cond
  5542. ((org-on-heading-p) (org-do-demote))
  5543. ((org-at-item-p) (org-indent-item 1))))
  5544. (defun org-insert-todo-subheading (arg)
  5545. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5546. Works for outline headings and for plain lists alike."
  5547. (interactive "P")
  5548. (org-insert-todo-heading arg)
  5549. (cond
  5550. ((org-on-heading-p) (org-do-demote))
  5551. ((org-at-item-p) (org-indent-item 1))))
  5552. ;;; Promotion and Demotion
  5553. (defun org-promote-subtree ()
  5554. "Promote the entire subtree.
  5555. See also `org-promote'."
  5556. (interactive)
  5557. (save-excursion
  5558. (org-map-tree 'org-promote))
  5559. (org-fix-position-after-promote))
  5560. (defun org-demote-subtree ()
  5561. "Demote the entire subtree. See `org-demote'.
  5562. See also `org-promote'."
  5563. (interactive)
  5564. (save-excursion
  5565. (org-map-tree 'org-demote))
  5566. (org-fix-position-after-promote))
  5567. (defun org-do-promote ()
  5568. "Promote the current heading higher up the tree.
  5569. If the region is active in `transient-mark-mode', promote all headings
  5570. in the region."
  5571. (interactive)
  5572. (save-excursion
  5573. (if (org-region-active-p)
  5574. (org-map-region 'org-promote (region-beginning) (region-end))
  5575. (org-promote)))
  5576. (org-fix-position-after-promote))
  5577. (defun org-do-demote ()
  5578. "Demote the current heading lower down the tree.
  5579. If the region is active in `transient-mark-mode', demote all headings
  5580. in the region."
  5581. (interactive)
  5582. (save-excursion
  5583. (if (org-region-active-p)
  5584. (org-map-region 'org-demote (region-beginning) (region-end))
  5585. (org-demote)))
  5586. (org-fix-position-after-promote))
  5587. (defun org-fix-position-after-promote ()
  5588. "Make sure that after pro/demotion cursor position is right."
  5589. (let ((pos (point)))
  5590. (when (save-excursion
  5591. (beginning-of-line 1)
  5592. (looking-at org-todo-line-regexp)
  5593. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5594. (cond ((eobp) (insert " "))
  5595. ((eolp) (insert " "))
  5596. ((equal (char-after) ?\ ) (forward-char 1))))))
  5597. (defun org-reduced-level (l)
  5598. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5599. (defun org-get-legal-level (level &optional change)
  5600. "Rectify a level change under the influence of `org-odd-levels-only'
  5601. LEVEL is a current level, CHANGE is by how much the level should be
  5602. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5603. even level numbers will become the next higher odd number."
  5604. (if org-odd-levels-only
  5605. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5606. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5607. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5608. (max 1 (+ level change))))
  5609. (defun org-promote ()
  5610. "Promote the current heading higher up the tree.
  5611. If the region is active in `transient-mark-mode', promote all headings
  5612. in the region."
  5613. (org-back-to-heading t)
  5614. (let* ((level (save-match-data (funcall outline-level)))
  5615. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5616. (diff (abs (- level (length up-head) -1))))
  5617. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5618. (replace-match up-head nil t)
  5619. ;; Fixup tag positioning
  5620. (and org-auto-align-tags (org-set-tags nil t))
  5621. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5622. (defun org-demote ()
  5623. "Demote the current heading lower down the tree.
  5624. If the region is active in `transient-mark-mode', demote all headings
  5625. in the region."
  5626. (org-back-to-heading t)
  5627. (let* ((level (save-match-data (funcall outline-level)))
  5628. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5629. (diff (abs (- level (length down-head) -1))))
  5630. (replace-match down-head nil t)
  5631. ;; Fixup tag positioning
  5632. (and org-auto-align-tags (org-set-tags nil t))
  5633. (if org-adapt-indentation (org-fixup-indentation diff))))
  5634. (defun org-map-tree (fun)
  5635. "Call FUN for every heading underneath the current one."
  5636. (org-back-to-heading)
  5637. (let ((level (funcall outline-level)))
  5638. (save-excursion
  5639. (funcall fun)
  5640. (while (and (progn
  5641. (outline-next-heading)
  5642. (> (funcall outline-level) level))
  5643. (not (eobp)))
  5644. (funcall fun)))))
  5645. (defun org-map-region (fun beg end)
  5646. "Call FUN for every heading between BEG and END."
  5647. (let ((org-ignore-region t))
  5648. (save-excursion
  5649. (setq end (copy-marker end))
  5650. (goto-char beg)
  5651. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5652. (< (point) end))
  5653. (funcall fun))
  5654. (while (and (progn
  5655. (outline-next-heading)
  5656. (< (point) end))
  5657. (not (eobp)))
  5658. (funcall fun)))))
  5659. (defun org-fixup-indentation (diff)
  5660. "Change the indentation in the current entry by DIFF
  5661. However, if any line in the current entry has no indentation, or if it
  5662. would end up with no indentation after the change, nothing at all is done."
  5663. (save-excursion
  5664. (let ((end (save-excursion (outline-next-heading)
  5665. (point-marker)))
  5666. (prohibit (if (> diff 0)
  5667. "^\\S-"
  5668. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5669. col)
  5670. (unless (save-excursion (end-of-line 1)
  5671. (re-search-forward prohibit end t))
  5672. (while (and (< (point) end)
  5673. (re-search-forward "^[ \t]+" end t))
  5674. (goto-char (match-end 0))
  5675. (setq col (current-column))
  5676. (if (< diff 0) (replace-match ""))
  5677. (indent-to (+ diff col))))
  5678. (move-marker end nil))))
  5679. (defun org-convert-to-odd-levels ()
  5680. "Convert an org-mode file with all levels allowed to one with odd levels.
  5681. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5682. level 5 etc."
  5683. (interactive)
  5684. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5685. (let ((org-odd-levels-only nil) n)
  5686. (save-excursion
  5687. (goto-char (point-min))
  5688. (while (re-search-forward "^\\*\\*+ " nil t)
  5689. (setq n (- (length (match-string 0)) 2))
  5690. (while (>= (setq n (1- n)) 0)
  5691. (org-demote))
  5692. (end-of-line 1))))))
  5693. (defun org-convert-to-oddeven-levels ()
  5694. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5695. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5696. section with an even level, conversion would destroy the structure of the file. An error
  5697. is signaled in this case."
  5698. (interactive)
  5699. (goto-char (point-min))
  5700. ;; First check if there are no even levels
  5701. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5702. (org-show-context t)
  5703. (error "Not all levels are odd in this file. Conversion not possible."))
  5704. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5705. (let ((org-odd-levels-only nil) n)
  5706. (save-excursion
  5707. (goto-char (point-min))
  5708. (while (re-search-forward "^\\*\\*+ " nil t)
  5709. (setq n (/ (1- (length (match-string 0))) 2))
  5710. (while (>= (setq n (1- n)) 0)
  5711. (org-promote))
  5712. (end-of-line 1))))))
  5713. (defun org-tr-level (n)
  5714. "Make N odd if required."
  5715. (if org-odd-levels-only (1+ (/ n 2)) n))
  5716. ;;; Vertical tree motion, cutting and pasting of subtrees
  5717. (defun org-move-subtree-up (&optional arg)
  5718. "Move the current subtree up past ARG headlines of the same level."
  5719. (interactive "p")
  5720. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5721. (defun org-move-subtree-down (&optional arg)
  5722. "Move the current subtree down past ARG headlines of the same level."
  5723. (interactive "p")
  5724. (setq arg (prefix-numeric-value arg))
  5725. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5726. 'outline-get-last-sibling))
  5727. (ins-point (make-marker))
  5728. (cnt (abs arg))
  5729. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5730. ;; Select the tree
  5731. (org-back-to-heading)
  5732. (setq beg0 (point))
  5733. (save-excursion
  5734. (setq ne-beg (org-back-over-empty-lines))
  5735. (setq beg (point)))
  5736. (save-match-data
  5737. (save-excursion (outline-end-of-heading)
  5738. (setq folded (org-invisible-p)))
  5739. (outline-end-of-subtree))
  5740. (outline-next-heading)
  5741. (setq ne-end (org-back-over-empty-lines))
  5742. (setq end (point))
  5743. (goto-char beg0)
  5744. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5745. ;; include less whitespace
  5746. (save-excursion
  5747. (goto-char beg)
  5748. (forward-line (- ne-beg ne-end))
  5749. (setq beg (point))))
  5750. ;; Find insertion point, with error handling
  5751. (while (> cnt 0)
  5752. (or (and (funcall movfunc) (looking-at outline-regexp))
  5753. (progn (goto-char beg0)
  5754. (error "Cannot move past superior level or buffer limit")))
  5755. (setq cnt (1- cnt)))
  5756. (if (> arg 0)
  5757. ;; Moving forward - still need to move over subtree
  5758. (progn (org-end-of-subtree t t)
  5759. (save-excursion
  5760. (org-back-over-empty-lines)
  5761. (or (bolp) (newline)))))
  5762. (setq ne-ins (org-back-over-empty-lines))
  5763. (move-marker ins-point (point))
  5764. (setq txt (buffer-substring beg end))
  5765. (delete-region beg end)
  5766. (outline-flag-region (1- beg) beg nil)
  5767. (outline-flag-region (1- (point)) (point) nil)
  5768. (insert txt)
  5769. (or (bolp) (insert "\n"))
  5770. (setq ins-end (point))
  5771. (goto-char ins-point)
  5772. (org-skip-whitespace)
  5773. (when (and (< arg 0)
  5774. (org-first-sibling-p)
  5775. (> ne-ins ne-beg))
  5776. ;; Move whitespace back to beginning
  5777. (save-excursion
  5778. (goto-char ins-end)
  5779. (let ((kill-whole-line t))
  5780. (kill-line (- ne-ins ne-beg)) (point)))
  5781. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5782. (move-marker ins-point nil)
  5783. (org-compact-display-after-subtree-move)
  5784. (unless folded
  5785. (org-show-entry)
  5786. (show-children)
  5787. (org-cycle-hide-drawers 'children))))
  5788. (defvar org-subtree-clip ""
  5789. "Clipboard for cut and paste of subtrees.
  5790. This is actually only a copy of the kill, because we use the normal kill
  5791. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5792. (defvar org-subtree-clip-folded nil
  5793. "Was the last copied subtree folded?
  5794. This is used to fold the tree back after pasting.")
  5795. (defun org-cut-subtree (&optional n)
  5796. "Cut the current subtree into the clipboard.
  5797. With prefix arg N, cut this many sequential subtrees.
  5798. This is a short-hand for marking the subtree and then cutting it."
  5799. (interactive "p")
  5800. (org-copy-subtree n 'cut))
  5801. (defun org-copy-subtree (&optional n cut)
  5802. "Cut the current subtree into the clipboard.
  5803. With prefix arg N, cut this many sequential subtrees.
  5804. This is a short-hand for marking the subtree and then copying it.
  5805. If CUT is non-nil, actually cut the subtree."
  5806. (interactive "p")
  5807. (let (beg end folded (beg0 (point)))
  5808. (if (interactive-p)
  5809. (org-back-to-heading nil) ; take what looks like a subtree
  5810. (org-back-to-heading t)) ; take what is really there
  5811. (org-back-over-empty-lines)
  5812. (setq beg (point))
  5813. (skip-chars-forward " \t\r\n")
  5814. (save-match-data
  5815. (save-excursion (outline-end-of-heading)
  5816. (setq folded (org-invisible-p)))
  5817. (condition-case nil
  5818. (outline-forward-same-level (1- n))
  5819. (error nil))
  5820. (org-end-of-subtree t t))
  5821. (org-back-over-empty-lines)
  5822. (setq end (point))
  5823. (goto-char beg0)
  5824. (when (> end beg)
  5825. (setq org-subtree-clip-folded folded)
  5826. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5827. (setq org-subtree-clip (current-kill 0))
  5828. (message "%s: Subtree(s) with %d characters"
  5829. (if cut "Cut" "Copied")
  5830. (length org-subtree-clip)))))
  5831. (defun org-paste-subtree (&optional level tree)
  5832. "Paste the clipboard as a subtree, with modification of headline level.
  5833. The entire subtree is promoted or demoted in order to match a new headline
  5834. level. By default, the new level is derived from the visible headings
  5835. before and after the insertion point, and taken to be the inferior headline
  5836. level of the two. So if the previous visible heading is level 3 and the
  5837. next is level 4 (or vice versa), level 4 will be used for insertion.
  5838. This makes sure that the subtree remains an independent subtree and does
  5839. not swallow low level entries.
  5840. You can also force a different level, either by using a numeric prefix
  5841. argument, or by inserting the heading marker by hand. For example, if the
  5842. cursor is after \"*****\", then the tree will be shifted to level 5.
  5843. If you want to insert the tree as is, just use \\[yank].
  5844. If optional TREE is given, use this text instead of the kill ring."
  5845. (interactive "P")
  5846. (unless (org-kill-is-subtree-p tree)
  5847. (error "%s"
  5848. (substitute-command-keys
  5849. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5850. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5851. (^re (concat "^\\(" outline-regexp "\\)"))
  5852. (re (concat "\\(" outline-regexp "\\)"))
  5853. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5854. (old-level (if (string-match ^re txt)
  5855. (- (match-end 0) (match-beginning 0) 1)
  5856. -1))
  5857. (force-level (cond (level (prefix-numeric-value level))
  5858. ((string-match
  5859. ^re_ (buffer-substring (point-at-bol) (point)))
  5860. (- (match-end 1) (match-beginning 1)))
  5861. (t nil)))
  5862. (previous-level (save-excursion
  5863. (condition-case nil
  5864. (progn
  5865. (outline-previous-visible-heading 1)
  5866. (if (looking-at re)
  5867. (- (match-end 0) (match-beginning 0) 1)
  5868. 1))
  5869. (error 1))))
  5870. (next-level (save-excursion
  5871. (condition-case nil
  5872. (progn
  5873. (or (looking-at outline-regexp)
  5874. (outline-next-visible-heading 1))
  5875. (if (looking-at re)
  5876. (- (match-end 0) (match-beginning 0) 1)
  5877. 1))
  5878. (error 1))))
  5879. (new-level (or force-level (max previous-level next-level)))
  5880. (shift (if (or (= old-level -1)
  5881. (= new-level -1)
  5882. (= old-level new-level))
  5883. 0
  5884. (- new-level old-level)))
  5885. (delta (if (> shift 0) -1 1))
  5886. (func (if (> shift 0) 'org-demote 'org-promote))
  5887. (org-odd-levels-only nil)
  5888. beg end)
  5889. ;; Remove the forced level indicator
  5890. (if force-level
  5891. (delete-region (point-at-bol) (point)))
  5892. ;; Paste
  5893. (beginning-of-line 1)
  5894. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5895. (setq beg (point))
  5896. (insert-before-markers txt) ;; FIXME: correct fix????
  5897. (unless (string-match "\n\\'" txt) (insert "\n"))
  5898. (setq end (point))
  5899. (goto-char beg)
  5900. (skip-chars-forward " \t\n\r")
  5901. (setq beg (point))
  5902. ;; Shift if necessary
  5903. (unless (= shift 0)
  5904. (save-restriction
  5905. (narrow-to-region beg end)
  5906. (while (not (= shift 0))
  5907. (org-map-region func (point-min) (point-max))
  5908. (setq shift (+ delta shift)))
  5909. (goto-char (point-min))))
  5910. (when (interactive-p)
  5911. (message "Clipboard pasted as level %d subtree" new-level))
  5912. (if (and kill-ring
  5913. (eq org-subtree-clip (current-kill 0))
  5914. org-subtree-clip-folded)
  5915. ;; The tree was folded before it was killed/copied
  5916. (hide-subtree))))
  5917. (defun org-kill-is-subtree-p (&optional txt)
  5918. "Check if the current kill is an outline subtree, or a set of trees.
  5919. Returns nil if kill does not start with a headline, or if the first
  5920. headline level is not the largest headline level in the tree.
  5921. So this will actually accept several entries of equal levels as well,
  5922. which is OK for `org-paste-subtree'.
  5923. If optional TXT is given, check this string instead of the current kill."
  5924. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5925. (start-level (and kill
  5926. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5927. org-outline-regexp "\\)")
  5928. kill)
  5929. (- (match-end 2) (match-beginning 2) 1)))
  5930. (re (concat "^" org-outline-regexp))
  5931. (start (1+ (match-beginning 2))))
  5932. (if (not start-level)
  5933. (progn
  5934. nil) ;; does not even start with a heading
  5935. (catch 'exit
  5936. (while (setq start (string-match re kill (1+ start)))
  5937. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5938. (throw 'exit nil)))
  5939. t))))
  5940. (defun org-narrow-to-subtree ()
  5941. "Narrow buffer to the current subtree."
  5942. (interactive)
  5943. (save-excursion
  5944. (narrow-to-region
  5945. (progn (org-back-to-heading) (point))
  5946. (progn (org-end-of-subtree t t) (point)))))
  5947. ;;; Outline Sorting
  5948. (defun org-sort (with-case)
  5949. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5950. Optional argument WITH-CASE means sort case-sensitively."
  5951. (interactive "P")
  5952. (if (org-at-table-p)
  5953. (org-call-with-arg 'org-table-sort-lines with-case)
  5954. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5955. (defvar org-priority-regexp) ; defined later in the file
  5956. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5957. "Sort entries on a certain level of an outline tree.
  5958. If there is an active region, the entries in the region are sorted.
  5959. Else, if the cursor is before the first entry, sort the top-level items.
  5960. Else, the children of the entry at point are sorted.
  5961. Sorting can be alphabetically, numerically, and by date/time as given by
  5962. the first time stamp in the entry. The command prompts for the sorting
  5963. type unless it has been given to the function through the SORTING-TYPE
  5964. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5965. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5966. called with point at the beginning of the record. It must return either
  5967. a string or a number that should serve as the sorting key for that record.
  5968. Comparing entries ignores case by default. However, with an optional argument
  5969. WITH-CASE, the sorting considers case as well."
  5970. (interactive "P")
  5971. (let ((case-func (if with-case 'identity 'downcase))
  5972. start beg end stars re re2
  5973. txt what tmp plain-list-p)
  5974. ;; Find beginning and end of region to sort
  5975. (cond
  5976. ((org-region-active-p)
  5977. ;; we will sort the region
  5978. (setq end (region-end)
  5979. what "region")
  5980. (goto-char (region-beginning))
  5981. (if (not (org-on-heading-p)) (outline-next-heading))
  5982. (setq start (point)))
  5983. ((org-at-item-p)
  5984. ;; we will sort this plain list
  5985. (org-beginning-of-item-list) (setq start (point))
  5986. (org-end-of-item-list) (setq end (point))
  5987. (goto-char start)
  5988. (setq plain-list-p t
  5989. what "plain list"))
  5990. ((or (org-on-heading-p)
  5991. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5992. ;; we will sort the children of the current headline
  5993. (org-back-to-heading)
  5994. (setq start (point)
  5995. end (progn (org-end-of-subtree t t)
  5996. (org-back-over-empty-lines)
  5997. (point))
  5998. what "children")
  5999. (goto-char start)
  6000. (show-subtree)
  6001. (outline-next-heading))
  6002. (t
  6003. ;; we will sort the top-level entries in this file
  6004. (goto-char (point-min))
  6005. (or (org-on-heading-p) (outline-next-heading))
  6006. (setq start (point) end (point-max) what "top-level")
  6007. (goto-char start)
  6008. (show-all)))
  6009. (setq beg (point))
  6010. (if (>= beg end) (error "Nothing to sort"))
  6011. (unless plain-list-p
  6012. (looking-at "\\(\\*+\\)")
  6013. (setq stars (match-string 1)
  6014. re (concat "^" (regexp-quote stars) " +")
  6015. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6016. txt (buffer-substring beg end))
  6017. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6018. (if (and (not (equal stars "*")) (string-match re2 txt))
  6019. (error "Region to sort contains a level above the first entry")))
  6020. (unless sorting-type
  6021. (message
  6022. (if plain-list-p
  6023. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6024. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6025. what)
  6026. (setq sorting-type (read-char-exclusive))
  6027. (and (= (downcase sorting-type) ?f)
  6028. (setq getkey-func
  6029. (completing-read "Sort using function: "
  6030. obarray 'fboundp t nil nil))
  6031. (setq getkey-func (intern getkey-func)))
  6032. (and (= (downcase sorting-type) ?r)
  6033. (setq property
  6034. (completing-read "Property: "
  6035. (mapcar 'list (org-buffer-property-keys t))
  6036. nil t))))
  6037. (message "Sorting entries...")
  6038. (save-restriction
  6039. (narrow-to-region start end)
  6040. (let ((dcst (downcase sorting-type))
  6041. (now (current-time)))
  6042. (sort-subr
  6043. (/= dcst sorting-type)
  6044. ;; This function moves to the beginning character of the "record" to
  6045. ;; be sorted.
  6046. (if plain-list-p
  6047. (lambda nil
  6048. (if (org-at-item-p) t (goto-char (point-max))))
  6049. (lambda nil
  6050. (if (re-search-forward re nil t)
  6051. (goto-char (match-beginning 0))
  6052. (goto-char (point-max)))))
  6053. ;; This function moves to the last character of the "record" being
  6054. ;; sorted.
  6055. (if plain-list-p
  6056. 'org-end-of-item
  6057. (lambda nil
  6058. (save-match-data
  6059. (condition-case nil
  6060. (outline-forward-same-level 1)
  6061. (error
  6062. (goto-char (point-max)))))))
  6063. ;; This function returns the value that gets sorted against.
  6064. (if plain-list-p
  6065. (lambda nil
  6066. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6067. (cond
  6068. ((= dcst ?n)
  6069. (string-to-number (buffer-substring (match-end 0)
  6070. (point-at-eol))))
  6071. ((= dcst ?a)
  6072. (buffer-substring (match-end 0) (point-at-eol)))
  6073. ((= dcst ?t)
  6074. (if (re-search-forward org-ts-regexp
  6075. (point-at-eol) t)
  6076. (org-time-string-to-time (match-string 0))
  6077. now))
  6078. ((= dcst ?f)
  6079. (if getkey-func
  6080. (progn
  6081. (setq tmp (funcall getkey-func))
  6082. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6083. tmp)
  6084. (error "Invalid key function `%s'" getkey-func)))
  6085. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6086. (lambda nil
  6087. (cond
  6088. ((= dcst ?n)
  6089. (if (looking-at outline-regexp)
  6090. (string-to-number (buffer-substring (match-end 0)
  6091. (point-at-eol)))
  6092. nil))
  6093. ((= dcst ?a)
  6094. (funcall case-func (buffer-substring (point-at-bol)
  6095. (point-at-eol))))
  6096. ((= dcst ?t)
  6097. (if (re-search-forward org-ts-regexp
  6098. (save-excursion
  6099. (forward-line 2)
  6100. (point)) t)
  6101. (org-time-string-to-time (match-string 0))
  6102. now))
  6103. ((= dcst ?p)
  6104. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6105. (string-to-char (match-string 2))
  6106. org-default-priority))
  6107. ((= dcst ?r)
  6108. (or (org-entry-get nil property) ""))
  6109. ((= dcst ?f)
  6110. (if getkey-func
  6111. (progn
  6112. (setq tmp (funcall getkey-func))
  6113. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6114. tmp)
  6115. (error "Invalid key function `%s'" getkey-func)))
  6116. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6117. nil
  6118. (cond
  6119. ((= dcst ?a) 'string<)
  6120. ((= dcst ?t) 'time-less-p)
  6121. (t nil)))))
  6122. (message "Sorting entries...done")))
  6123. (defun org-do-sort (table what &optional with-case sorting-type)
  6124. "Sort TABLE of WHAT according to SORTING-TYPE.
  6125. The user will be prompted for the SORTING-TYPE if the call to this
  6126. function does not specify it. WHAT is only for the prompt, to indicate
  6127. what is being sorted. The sorting key will be extracted from
  6128. the car of the elements of the table.
  6129. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6130. (unless sorting-type
  6131. (message
  6132. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6133. what)
  6134. (setq sorting-type (read-char-exclusive)))
  6135. (let ((dcst (downcase sorting-type))
  6136. extractfun comparefun)
  6137. ;; Define the appropriate functions
  6138. (cond
  6139. ((= dcst ?n)
  6140. (setq extractfun 'string-to-number
  6141. comparefun (if (= dcst sorting-type) '< '>)))
  6142. ((= dcst ?a)
  6143. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6144. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6145. comparefun (if (= dcst sorting-type)
  6146. 'string<
  6147. (lambda (a b) (and (not (string< a b))
  6148. (not (string= a b)))))))
  6149. ((= dcst ?t)
  6150. (setq extractfun
  6151. (lambda (x)
  6152. (if (string-match org-ts-regexp x)
  6153. (time-to-seconds
  6154. (org-time-string-to-time (match-string 0 x)))
  6155. 0))
  6156. comparefun (if (= dcst sorting-type) '< '>)))
  6157. (t (error "Invalid sorting type `%c'" sorting-type)))
  6158. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6159. table)
  6160. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6161. ;;;; Plain list items, including checkboxes
  6162. ;;; Plain list items
  6163. (defun org-at-item-p ()
  6164. "Is point in a line starting a hand-formatted item?"
  6165. (let ((llt org-plain-list-ordered-item-terminator))
  6166. (save-excursion
  6167. (goto-char (point-at-bol))
  6168. (looking-at
  6169. (cond
  6170. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6171. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6172. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6173. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6174. (defun org-in-item-p ()
  6175. "It the cursor inside a plain list item.
  6176. Does not have to be the first line."
  6177. (save-excursion
  6178. (condition-case nil
  6179. (progn
  6180. (org-beginning-of-item)
  6181. (org-at-item-p)
  6182. t)
  6183. (error nil))))
  6184. (defun org-insert-item (&optional checkbox)
  6185. "Insert a new item at the current level.
  6186. Return t when things worked, nil when we are not in an item."
  6187. (when (save-excursion
  6188. (condition-case nil
  6189. (progn
  6190. (org-beginning-of-item)
  6191. (org-at-item-p)
  6192. (if (org-invisible-p) (error "Invisible item"))
  6193. t)
  6194. (error nil)))
  6195. (let* ((bul (match-string 0))
  6196. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6197. (match-end 0)))
  6198. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6199. pos)
  6200. (cond
  6201. ((and (org-at-item-p) (<= (point) eow))
  6202. ;; before the bullet
  6203. (beginning-of-line 1)
  6204. (open-line (if blank 2 1)))
  6205. ((<= (point) eow)
  6206. (beginning-of-line 1))
  6207. (t (newline (if blank 2 1))))
  6208. (insert bul (if checkbox "[ ]" ""))
  6209. (just-one-space)
  6210. (setq pos (point))
  6211. (end-of-line 1)
  6212. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6213. (org-maybe-renumber-ordered-list)
  6214. (and checkbox (org-update-checkbox-count-maybe))
  6215. t))
  6216. ;;; Checkboxes
  6217. (defun org-at-item-checkbox-p ()
  6218. "Is point at a line starting a plain-list item with a checklet?"
  6219. (and (org-at-item-p)
  6220. (save-excursion
  6221. (goto-char (match-end 0))
  6222. (skip-chars-forward " \t")
  6223. (looking-at "\\[[- X]\\]"))))
  6224. (defun org-toggle-checkbox (&optional arg)
  6225. "Toggle the checkbox in the current line."
  6226. (interactive "P")
  6227. (catch 'exit
  6228. (let (beg end status (firstnew 'unknown))
  6229. (cond
  6230. ((org-region-active-p)
  6231. (setq beg (region-beginning) end (region-end)))
  6232. ((org-on-heading-p)
  6233. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6234. ((org-at-item-checkbox-p)
  6235. (let ((pos (point)))
  6236. (replace-match
  6237. (cond (arg "[-]")
  6238. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6239. (t "[ ]"))
  6240. t t)
  6241. (goto-char pos))
  6242. (throw 'exit t))
  6243. (t (error "Not at a checkbox or heading, and no active region")))
  6244. (save-excursion
  6245. (goto-char beg)
  6246. (while (< (point) end)
  6247. (when (org-at-item-checkbox-p)
  6248. (setq status (equal (match-string 0) "[X]"))
  6249. (when (eq firstnew 'unknown)
  6250. (setq firstnew (not status)))
  6251. (replace-match
  6252. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6253. (beginning-of-line 2)))))
  6254. (org-update-checkbox-count-maybe))
  6255. (defun org-update-checkbox-count-maybe ()
  6256. "Update checkbox statistics unless turned off by user."
  6257. (when org-provide-checkbox-statistics
  6258. (org-update-checkbox-count)))
  6259. (defun org-update-checkbox-count (&optional all)
  6260. "Update the checkbox statistics in the current section.
  6261. This will find all statistic cookies like [57%] and [6/12] and update them
  6262. with the current numbers. With optional prefix argument ALL, do this for
  6263. the whole buffer."
  6264. (interactive "P")
  6265. (save-excursion
  6266. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6267. (beg (condition-case nil
  6268. (progn (outline-back-to-heading) (point))
  6269. (error (point-min))))
  6270. (end (move-marker (make-marker)
  6271. (progn (outline-next-heading) (point))))
  6272. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6273. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6274. (re-find (concat re "\\|" re-box))
  6275. beg-cookie end-cookie is-percent c-on c-off lim
  6276. eline curr-ind next-ind continue-from startsearch
  6277. (cstat 0)
  6278. )
  6279. (when all
  6280. (goto-char (point-min))
  6281. (outline-next-heading)
  6282. (setq beg (point) end (point-max)))
  6283. (goto-char end)
  6284. ;; find each statistic cookie
  6285. (while (re-search-backward re-find beg t)
  6286. (setq beg-cookie (match-beginning 1)
  6287. end-cookie (match-end 1)
  6288. cstat (+ cstat (if end-cookie 1 0))
  6289. startsearch (point-at-eol)
  6290. continue-from (point-at-bol)
  6291. is-percent (match-beginning 2)
  6292. lim (cond
  6293. ((org-on-heading-p) (outline-next-heading) (point))
  6294. ((org-at-item-p) (org-end-of-item) (point))
  6295. (t nil))
  6296. c-on 0
  6297. c-off 0)
  6298. (when lim
  6299. ;; find first checkbox for this cookie and gather
  6300. ;; statistics from all that are at this indentation level
  6301. (goto-char startsearch)
  6302. (if (re-search-forward re-box lim t)
  6303. (progn
  6304. (org-beginning-of-item)
  6305. (setq curr-ind (org-get-indentation))
  6306. (setq next-ind curr-ind)
  6307. (while (= curr-ind next-ind)
  6308. (save-excursion (end-of-line) (setq eline (point)))
  6309. (if (re-search-forward re-box eline t)
  6310. (if (member (match-string 2) '("[ ]" "[-]"))
  6311. (setq c-off (1+ c-off))
  6312. (setq c-on (1+ c-on))
  6313. )
  6314. )
  6315. (org-end-of-item)
  6316. (setq next-ind (org-get-indentation))
  6317. )))
  6318. (goto-char continue-from)
  6319. ;; update cookie
  6320. (when end-cookie
  6321. (delete-region beg-cookie end-cookie)
  6322. (goto-char beg-cookie)
  6323. (insert
  6324. (if is-percent
  6325. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6326. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6327. ;; update items checkbox if it has one
  6328. (when (org-at-item-p)
  6329. (org-beginning-of-item)
  6330. (when (and (> (+ c-on c-off) 0)
  6331. (re-search-forward re-box (point-at-eol) t))
  6332. (setq beg-cookie (match-beginning 2)
  6333. end-cookie (match-end 2))
  6334. (delete-region beg-cookie end-cookie)
  6335. (goto-char beg-cookie)
  6336. (cond ((= c-off 0) (insert "[X]"))
  6337. ((= c-on 0) (insert "[ ]"))
  6338. (t (insert "[-]")))
  6339. )))
  6340. (goto-char continue-from))
  6341. (when (interactive-p)
  6342. (message "Checkbox satistics updated %s (%d places)"
  6343. (if all "in entire file" "in current outline entry") cstat)))))
  6344. (defun org-get-checkbox-statistics-face ()
  6345. "Select the face for checkbox statistics.
  6346. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6347. it will be `org-todo'."
  6348. (if (match-end 1)
  6349. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6350. (if (and (> (match-end 2) (match-beginning 2))
  6351. (equal (match-string 2) (match-string 3)))
  6352. 'org-done
  6353. 'org-todo)))
  6354. (defun org-get-indentation (&optional line)
  6355. "Get the indentation of the current line, interpreting tabs.
  6356. When LINE is given, assume it represents a line and compute its indentation."
  6357. (if line
  6358. (if (string-match "^ *" (org-remove-tabs line))
  6359. (match-end 0))
  6360. (save-excursion
  6361. (beginning-of-line 1)
  6362. (skip-chars-forward " \t")
  6363. (current-column))))
  6364. (defun org-remove-tabs (s &optional width)
  6365. "Replace tabulators in S with spaces.
  6366. Assumes that s is a single line, starting in column 0."
  6367. (setq width (or width tab-width))
  6368. (while (string-match "\t" s)
  6369. (setq s (replace-match
  6370. (make-string
  6371. (- (* width (/ (+ (match-beginning 0) width) width))
  6372. (match-beginning 0)) ?\ )
  6373. t t s)))
  6374. s)
  6375. (defun org-fix-indentation (line ind)
  6376. "Fix indentation in LINE.
  6377. IND is a cons cell with target and minimum indentation.
  6378. If the current indenation in LINE is smaller than the minimum,
  6379. leave it alone. If it is larger than ind, set it to the target."
  6380. (let* ((l (org-remove-tabs line))
  6381. (i (org-get-indentation l))
  6382. (i1 (car ind)) (i2 (cdr ind)))
  6383. (if (>= i i2) (setq l (substring line i2)))
  6384. (if (> i1 0)
  6385. (concat (make-string i1 ?\ ) l)
  6386. l)))
  6387. (defcustom org-empty-line-terminates-plain-lists nil
  6388. "Non-nil means, an empty line ends all plain list levels.
  6389. When nil, empty lines are part of the preceeding item."
  6390. :group 'org-plain-lists
  6391. :type 'boolean)
  6392. (defun org-beginning-of-item ()
  6393. "Go to the beginning of the current hand-formatted item.
  6394. If the cursor is not in an item, throw an error."
  6395. (interactive)
  6396. (let ((pos (point))
  6397. (limit (save-excursion
  6398. (condition-case nil
  6399. (progn
  6400. (org-back-to-heading)
  6401. (beginning-of-line 2) (point))
  6402. (error (point-min)))))
  6403. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6404. ind ind1)
  6405. (if (org-at-item-p)
  6406. (beginning-of-line 1)
  6407. (beginning-of-line 1)
  6408. (skip-chars-forward " \t")
  6409. (setq ind (current-column))
  6410. (if (catch 'exit
  6411. (while t
  6412. (beginning-of-line 0)
  6413. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6414. (if (looking-at "[ \t]*$")
  6415. (setq ind1 ind-empty)
  6416. (skip-chars-forward " \t")
  6417. (setq ind1 (current-column)))
  6418. (if (< ind1 ind)
  6419. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6420. nil
  6421. (goto-char pos)
  6422. (error "Not in an item")))))
  6423. (defun org-end-of-item ()
  6424. "Go to the end of the current hand-formatted item.
  6425. If the cursor is not in an item, throw an error."
  6426. (interactive)
  6427. (let* ((pos (point))
  6428. ind1
  6429. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6430. (limit (save-excursion (outline-next-heading) (point)))
  6431. (ind (save-excursion
  6432. (org-beginning-of-item)
  6433. (skip-chars-forward " \t")
  6434. (current-column)))
  6435. (end (catch 'exit
  6436. (while t
  6437. (beginning-of-line 2)
  6438. (if (eobp) (throw 'exit (point)))
  6439. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6440. (if (looking-at "[ \t]*$")
  6441. (setq ind1 ind-empty)
  6442. (skip-chars-forward " \t")
  6443. (setq ind1 (current-column)))
  6444. (if (<= ind1 ind)
  6445. (throw 'exit (point-at-bol)))))))
  6446. (if end
  6447. (goto-char end)
  6448. (goto-char pos)
  6449. (error "Not in an item"))))
  6450. (defun org-next-item ()
  6451. "Move to the beginning of the next item in the current plain list.
  6452. Error if not at a plain list, or if this is the last item in the list."
  6453. (interactive)
  6454. (let (ind ind1 (pos (point)))
  6455. (org-beginning-of-item)
  6456. (setq ind (org-get-indentation))
  6457. (org-end-of-item)
  6458. (setq ind1 (org-get-indentation))
  6459. (unless (and (org-at-item-p) (= ind ind1))
  6460. (goto-char pos)
  6461. (error "On last item"))))
  6462. (defun org-previous-item ()
  6463. "Move to the beginning of the previous item in the current plain list.
  6464. Error if not at a plain list, or if this is the first item in the list."
  6465. (interactive)
  6466. (let (beg ind ind1 (pos (point)))
  6467. (org-beginning-of-item)
  6468. (setq beg (point))
  6469. (setq ind (org-get-indentation))
  6470. (goto-char beg)
  6471. (catch 'exit
  6472. (while t
  6473. (beginning-of-line 0)
  6474. (if (looking-at "[ \t]*$")
  6475. nil
  6476. (if (<= (setq ind1 (org-get-indentation)) ind)
  6477. (throw 'exit t)))))
  6478. (condition-case nil
  6479. (if (or (not (org-at-item-p))
  6480. (< ind1 (1- ind)))
  6481. (error "")
  6482. (org-beginning-of-item))
  6483. (error (goto-char pos)
  6484. (error "On first item")))))
  6485. (defun org-first-list-item-p ()
  6486. "Is this heading the item in a plain list?"
  6487. (unless (org-at-item-p)
  6488. (error "Not at a plain list item"))
  6489. (org-beginning-of-item)
  6490. (= (point) (save-excursion (org-beginning-of-item-list))))
  6491. (defun org-move-item-down ()
  6492. "Move the plain list item at point down, i.e. swap with following item.
  6493. Subitems (items with larger indentation) are considered part of the item,
  6494. so this really moves item trees."
  6495. (interactive)
  6496. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6497. (org-beginning-of-item)
  6498. (setq beg0 (point))
  6499. (save-excursion
  6500. (setq ne-beg (org-back-over-empty-lines))
  6501. (setq beg (point)))
  6502. (goto-char beg0)
  6503. (setq ind (org-get-indentation))
  6504. (org-end-of-item)
  6505. (setq end0 (point))
  6506. (setq ind1 (org-get-indentation))
  6507. (setq ne-end (org-back-over-empty-lines))
  6508. (setq end (point))
  6509. (goto-char beg0)
  6510. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6511. ;; include less whitespace
  6512. (save-excursion
  6513. (goto-char beg)
  6514. (forward-line (- ne-beg ne-end))
  6515. (setq beg (point))))
  6516. (goto-char end0)
  6517. (if (and (org-at-item-p) (= ind ind1))
  6518. (progn
  6519. (org-end-of-item)
  6520. (org-back-over-empty-lines)
  6521. (setq txt (buffer-substring beg end))
  6522. (save-excursion
  6523. (delete-region beg end))
  6524. (setq pos (point))
  6525. (insert txt)
  6526. (goto-char pos) (org-skip-whitespace)
  6527. (org-maybe-renumber-ordered-list))
  6528. (goto-char pos)
  6529. (error "Cannot move this item further down"))))
  6530. (defun org-move-item-up (arg)
  6531. "Move the plain list item at point up, i.e. swap with previous item.
  6532. Subitems (items with larger indentation) are considered part of the item,
  6533. so this really moves item trees."
  6534. (interactive "p")
  6535. (let (beg beg0 end end0 ind ind1 (pos (point)) txt
  6536. ne-beg ne-end ne-ins ins-end)
  6537. (org-beginning-of-item)
  6538. (setq beg0 (point))
  6539. (setq ind (org-get-indentation))
  6540. (save-excursion
  6541. (setq ne-beg (org-back-over-empty-lines))
  6542. (setq beg (point)))
  6543. (goto-char beg0)
  6544. (org-end-of-item)
  6545. (setq ne-end (org-back-over-empty-lines))
  6546. (setq end (point))
  6547. (goto-char beg0)
  6548. (catch 'exit
  6549. (while t
  6550. (beginning-of-line 0)
  6551. (if (looking-at "[ \t]*$")
  6552. (if org-empty-line-terminates-plain-lists
  6553. (progn
  6554. (goto-char pos)
  6555. (error "Cannot move this item further up"))
  6556. nil)
  6557. (if (<= (setq ind1 (org-get-indentation)) ind)
  6558. (throw 'exit t)))))
  6559. (condition-case nil
  6560. (org-beginning-of-item)
  6561. (error (goto-char beg)
  6562. (error "Cannot move this item further up")))
  6563. (setq ind1 (org-get-indentation))
  6564. (if (and (org-at-item-p) (= ind ind1))
  6565. (progn
  6566. (setq ne-ins (org-back-over-empty-lines))
  6567. (setq txt (buffer-substring beg end))
  6568. (save-excursion
  6569. (delete-region beg end))
  6570. (setq pos (point))
  6571. (insert txt)
  6572. (setq ins-end (point))
  6573. (goto-char pos) (org-skip-whitespace)
  6574. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6575. ;; Move whitespace back to beginning
  6576. (save-excursion
  6577. (goto-char ins-end)
  6578. (let ((kill-whole-line t))
  6579. (kill-line (- ne-ins ne-beg)) (point)))
  6580. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6581. (org-maybe-renumber-ordered-list))
  6582. (goto-char pos)
  6583. (error "Cannot move this item further up"))))
  6584. (defun org-maybe-renumber-ordered-list ()
  6585. "Renumber the ordered list at point if setup allows it.
  6586. This tests the user option `org-auto-renumber-ordered-lists' before
  6587. doing the renumbering."
  6588. (interactive)
  6589. (when (and org-auto-renumber-ordered-lists
  6590. (org-at-item-p))
  6591. (if (match-beginning 3)
  6592. (org-renumber-ordered-list 1)
  6593. (org-fix-bullet-type))))
  6594. (defun org-maybe-renumber-ordered-list-safe ()
  6595. (condition-case nil
  6596. (save-excursion
  6597. (org-maybe-renumber-ordered-list))
  6598. (error nil)))
  6599. (defun org-cycle-list-bullet (&optional which)
  6600. "Cycle through the different itemize/enumerate bullets.
  6601. This cycle the entire list level through the sequence:
  6602. `-' -> `+' -> `*' -> `1.' -> `1)'
  6603. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6604. 0 meand `-', 1 means `+' etc."
  6605. (interactive "P")
  6606. (org-preserve-lc
  6607. (org-beginning-of-item-list)
  6608. (org-at-item-p)
  6609. (beginning-of-line 1)
  6610. (let ((current (match-string 0))
  6611. (prevp (eq which 'previous))
  6612. new)
  6613. (setq new (cond
  6614. ((and (numberp which)
  6615. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6616. ((string-match "-" current) (if prevp "1)" "+"))
  6617. ((string-match "\\+" current)
  6618. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6619. ((string-match "\\*" current) (if prevp "+" "1."))
  6620. ((string-match "\\." current) (if prevp "*" "1)"))
  6621. ((string-match ")" current) (if prevp "1." "-"))
  6622. (t (error "This should not happen"))))
  6623. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6624. (org-fix-bullet-type)
  6625. (org-maybe-renumber-ordered-list))))
  6626. (defun org-get-string-indentation (s)
  6627. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6628. (let ((n -1) (i 0) (w tab-width) c)
  6629. (catch 'exit
  6630. (while (< (setq n (1+ n)) (length s))
  6631. (setq c (aref s n))
  6632. (cond ((= c ?\ ) (setq i (1+ i)))
  6633. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6634. (t (throw 'exit t)))))
  6635. i))
  6636. (defun org-renumber-ordered-list (arg)
  6637. "Renumber an ordered plain list.
  6638. Cursor needs to be in the first line of an item, the line that starts
  6639. with something like \"1.\" or \"2)\"."
  6640. (interactive "p")
  6641. (unless (and (org-at-item-p)
  6642. (match-beginning 3))
  6643. (error "This is not an ordered list"))
  6644. (let ((line (org-current-line))
  6645. (col (current-column))
  6646. (ind (org-get-string-indentation
  6647. (buffer-substring (point-at-bol) (match-beginning 3))))
  6648. ;; (term (substring (match-string 3) -1))
  6649. ind1 (n (1- arg))
  6650. fmt)
  6651. ;; find where this list begins
  6652. (org-beginning-of-item-list)
  6653. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6654. (setq fmt (concat "%d" (match-string 1)))
  6655. (beginning-of-line 0)
  6656. ;; walk forward and replace these numbers
  6657. (catch 'exit
  6658. (while t
  6659. (catch 'next
  6660. (beginning-of-line 2)
  6661. (if (eobp) (throw 'exit nil))
  6662. (if (looking-at "[ \t]*$") (throw 'next nil))
  6663. (skip-chars-forward " \t") (setq ind1 (current-column))
  6664. (if (> ind1 ind) (throw 'next t))
  6665. (if (< ind1 ind) (throw 'exit t))
  6666. (if (not (org-at-item-p)) (throw 'exit nil))
  6667. (delete-region (match-beginning 2) (match-end 2))
  6668. (goto-char (match-beginning 2))
  6669. (insert (format fmt (setq n (1+ n)))))))
  6670. (goto-line line)
  6671. (move-to-column col)))
  6672. (defun org-fix-bullet-type ()
  6673. "Make sure all items in this list have the same bullet as the firsst item."
  6674. (interactive)
  6675. (unless (org-at-item-p) (error "This is not a list"))
  6676. (let ((line (org-current-line))
  6677. (col (current-column))
  6678. (ind (current-indentation))
  6679. ind1 bullet)
  6680. ;; find where this list begins
  6681. (org-beginning-of-item-list)
  6682. (beginning-of-line 1)
  6683. ;; find out what the bullet type is
  6684. (looking-at "[ \t]*\\(\\S-+\\)")
  6685. (setq bullet (match-string 1))
  6686. ;; walk forward and replace these numbers
  6687. (beginning-of-line 0)
  6688. (catch 'exit
  6689. (while t
  6690. (catch 'next
  6691. (beginning-of-line 2)
  6692. (if (eobp) (throw 'exit nil))
  6693. (if (looking-at "[ \t]*$") (throw 'next nil))
  6694. (skip-chars-forward " \t") (setq ind1 (current-column))
  6695. (if (> ind1 ind) (throw 'next t))
  6696. (if (< ind1 ind) (throw 'exit t))
  6697. (if (not (org-at-item-p)) (throw 'exit nil))
  6698. (skip-chars-forward " \t")
  6699. (looking-at "\\S-+")
  6700. (replace-match bullet))))
  6701. (goto-line line)
  6702. (move-to-column col)
  6703. (if (string-match "[0-9]" bullet)
  6704. (org-renumber-ordered-list 1))))
  6705. (defun org-beginning-of-item-list ()
  6706. "Go to the beginning of the current item list.
  6707. I.e. to the first item in this list."
  6708. (interactive)
  6709. (org-beginning-of-item)
  6710. (let ((pos (point-at-bol))
  6711. (ind (org-get-indentation))
  6712. ind1)
  6713. ;; find where this list begins
  6714. (catch 'exit
  6715. (while t
  6716. (catch 'next
  6717. (beginning-of-line 0)
  6718. (if (looking-at "[ \t]*$")
  6719. (throw (if (bobp) 'exit 'next) t))
  6720. (skip-chars-forward " \t") (setq ind1 (current-column))
  6721. (if (or (< ind1 ind)
  6722. (and (= ind1 ind)
  6723. (not (org-at-item-p)))
  6724. (bobp))
  6725. (throw 'exit t)
  6726. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6727. (goto-char pos)))
  6728. (defun org-end-of-item-list ()
  6729. "Go to the end of the current item list.
  6730. I.e. to the text after the last item."
  6731. (interactive)
  6732. (org-beginning-of-item)
  6733. (let ((pos (point-at-bol))
  6734. (ind (org-get-indentation))
  6735. ind1)
  6736. ;; find where this list begins
  6737. (catch 'exit
  6738. (while t
  6739. (catch 'next
  6740. (beginning-of-line 2)
  6741. (if (looking-at "[ \t]*$")
  6742. (throw (if (eobp) 'exit 'next) t))
  6743. (skip-chars-forward " \t") (setq ind1 (current-column))
  6744. (if (or (< ind1 ind)
  6745. (and (= ind1 ind)
  6746. (not (org-at-item-p)))
  6747. (eobp))
  6748. (progn
  6749. (setq pos (point-at-bol))
  6750. (throw 'exit t))))))
  6751. (goto-char pos)))
  6752. (defvar org-last-indent-begin-marker (make-marker))
  6753. (defvar org-last-indent-end-marker (make-marker))
  6754. (defun org-outdent-item (arg)
  6755. "Outdent a local list item."
  6756. (interactive "p")
  6757. (org-indent-item (- arg)))
  6758. (defun org-indent-item (arg)
  6759. "Indent a local list item."
  6760. (interactive "p")
  6761. (unless (org-at-item-p)
  6762. (error "Not on an item"))
  6763. (save-excursion
  6764. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6765. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6766. (setq beg org-last-indent-begin-marker
  6767. end org-last-indent-end-marker)
  6768. (org-beginning-of-item)
  6769. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6770. (org-end-of-item)
  6771. (setq end (move-marker org-last-indent-end-marker (point))))
  6772. (goto-char beg)
  6773. (setq tmp (org-item-indent-positions)
  6774. ind (car tmp)
  6775. ind-down (nth 2 tmp)
  6776. ind-up (nth 1 tmp)
  6777. delta (if (> arg 0)
  6778. (if ind-down (- ind-down ind) 2)
  6779. (if ind-up (- ind-up ind) -2)))
  6780. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6781. (while (< (point) end)
  6782. (beginning-of-line 1)
  6783. (skip-chars-forward " \t") (setq ind1 (current-column))
  6784. (delete-region (point-at-bol) (point))
  6785. (or (eolp) (indent-to-column (+ ind1 delta)))
  6786. (beginning-of-line 2))))
  6787. (org-fix-bullet-type)
  6788. (org-maybe-renumber-ordered-list-safe)
  6789. (save-excursion
  6790. (beginning-of-line 0)
  6791. (condition-case nil (org-beginning-of-item) (error nil))
  6792. (org-maybe-renumber-ordered-list-safe)))
  6793. (defun org-item-indent-positions ()
  6794. "Return indentation for plain list items.
  6795. This returns a list with three values: The current indentation, the
  6796. parent indentation and the indentation a child should habe.
  6797. Assumes cursor in item line."
  6798. (let* ((bolpos (point-at-bol))
  6799. (ind (org-get-indentation))
  6800. ind-down ind-up pos)
  6801. (save-excursion
  6802. (org-beginning-of-item-list)
  6803. (skip-chars-backward "\n\r \t")
  6804. (when (org-in-item-p)
  6805. (org-beginning-of-item)
  6806. (setq ind-up (org-get-indentation))))
  6807. (setq pos (point))
  6808. (save-excursion
  6809. (cond
  6810. ((and (condition-case nil (progn (org-previous-item) t)
  6811. (error nil))
  6812. (or (forward-char 1) t)
  6813. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6814. (setq ind-down (org-get-indentation)))
  6815. ((and (goto-char pos)
  6816. (org-at-item-p))
  6817. (goto-char (match-end 0))
  6818. (skip-chars-forward " \t")
  6819. (setq ind-down (current-column)))))
  6820. (list ind ind-up ind-down)))
  6821. ;;; The orgstruct minor mode
  6822. ;; Define a minor mode which can be used in other modes in order to
  6823. ;; integrate the org-mode structure editing commands.
  6824. ;; This is really a hack, because the org-mode structure commands use
  6825. ;; keys which normally belong to the major mode. Here is how it
  6826. ;; works: The minor mode defines all the keys necessary to operate the
  6827. ;; structure commands, but wraps the commands into a function which
  6828. ;; tests if the cursor is currently at a headline or a plain list
  6829. ;; item. If that is the case, the structure command is used,
  6830. ;; temporarily setting many Org-mode variables like regular
  6831. ;; expressions for filling etc. However, when any of those keys is
  6832. ;; used at a different location, function uses `key-binding' to look
  6833. ;; up if the key has an associated command in another currently active
  6834. ;; keymap (minor modes, major mode, global), and executes that
  6835. ;; command. There might be problems if any of the keys is otherwise
  6836. ;; used as a prefix key.
  6837. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6838. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6839. ;; addresses this by checking explicitly for both bindings.
  6840. (defvar orgstruct-mode-map (make-sparse-keymap)
  6841. "Keymap for the minor `orgstruct-mode'.")
  6842. (defvar org-local-vars nil
  6843. "List of local variables, for use by `orgstruct-mode'")
  6844. ;;;###autoload
  6845. (define-minor-mode orgstruct-mode
  6846. "Toggle the minor more `orgstruct-mode'.
  6847. This mode is for using Org-mode structure commands in other modes.
  6848. The following key behave as if Org-mode was active, if the cursor
  6849. is on a headline, or on a plain list item (both in the definition
  6850. of Org-mode).
  6851. M-up Move entry/item up
  6852. M-down Move entry/item down
  6853. M-left Promote
  6854. M-right Demote
  6855. M-S-up Move entry/item up
  6856. M-S-down Move entry/item down
  6857. M-S-left Promote subtree
  6858. M-S-right Demote subtree
  6859. M-q Fill paragraph and items like in Org-mode
  6860. C-c ^ Sort entries
  6861. C-c - Cycle list bullet
  6862. TAB Cycle item visibility
  6863. M-RET Insert new heading/item
  6864. S-M-RET Insert new TODO heading / Chekbox item
  6865. C-c C-c Set tags / toggle checkbox"
  6866. nil " OrgStruct" nil
  6867. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6868. ;;;###autoload
  6869. (defun turn-on-orgstruct ()
  6870. "Unconditionally turn on `orgstruct-mode'."
  6871. (orgstruct-mode 1))
  6872. ;;;###autoload
  6873. (defun turn-on-orgstruct++ ()
  6874. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6875. In addition to setting orgstruct-mode, this also exports all indentation and
  6876. autofilling variables from org-mode into the buffer. Note that turning
  6877. off orgstruct-mode will *not* remove these additional settings."
  6878. (orgstruct-mode 1)
  6879. (let (var val)
  6880. (mapc
  6881. (lambda (x)
  6882. (when (string-match
  6883. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6884. (symbol-name (car x)))
  6885. (setq var (car x) val (nth 1 x))
  6886. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6887. org-local-vars)))
  6888. (defun orgstruct-error ()
  6889. "Error when there is no default binding for a structure key."
  6890. (interactive)
  6891. (error "This key has no function outside structure elements"))
  6892. (defun orgstruct-setup ()
  6893. "Setup orgstruct keymaps."
  6894. (let ((nfunc 0)
  6895. (bindings
  6896. (list
  6897. '([(meta up)] org-metaup)
  6898. '([(meta down)] org-metadown)
  6899. '([(meta left)] org-metaleft)
  6900. '([(meta right)] org-metaright)
  6901. '([(meta shift up)] org-shiftmetaup)
  6902. '([(meta shift down)] org-shiftmetadown)
  6903. '([(meta shift left)] org-shiftmetaleft)
  6904. '([(meta shift right)] org-shiftmetaright)
  6905. '([(shift up)] org-shiftup)
  6906. '([(shift down)] org-shiftdown)
  6907. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6908. '("\M-q" fill-paragraph)
  6909. '("\C-c^" org-sort)
  6910. '("\C-c-" org-cycle-list-bullet)))
  6911. elt key fun cmd)
  6912. (while (setq elt (pop bindings))
  6913. (setq nfunc (1+ nfunc))
  6914. (setq key (org-key (car elt))
  6915. fun (nth 1 elt)
  6916. cmd (orgstruct-make-binding fun nfunc key))
  6917. (org-defkey orgstruct-mode-map key cmd))
  6918. ;; Special treatment needed for TAB and RET
  6919. (org-defkey orgstruct-mode-map [(tab)]
  6920. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6921. (org-defkey orgstruct-mode-map "\C-i"
  6922. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6923. (org-defkey orgstruct-mode-map "\M-\C-m"
  6924. (orgstruct-make-binding 'org-insert-heading 105
  6925. "\M-\C-m" [(meta return)]))
  6926. (org-defkey orgstruct-mode-map [(meta return)]
  6927. (orgstruct-make-binding 'org-insert-heading 106
  6928. [(meta return)] "\M-\C-m"))
  6929. (org-defkey orgstruct-mode-map [(shift meta return)]
  6930. (orgstruct-make-binding 'org-insert-todo-heading 107
  6931. [(meta return)] "\M-\C-m"))
  6932. (unless org-local-vars
  6933. (setq org-local-vars (org-get-local-variables)))
  6934. t))
  6935. (defun orgstruct-make-binding (fun n &rest keys)
  6936. "Create a function for binding in the structure minor mode.
  6937. FUN is the command to call inside a table. N is used to create a unique
  6938. command name. KEYS are keys that should be checked in for a command
  6939. to execute outside of tables."
  6940. (eval
  6941. (list 'defun
  6942. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6943. '(arg)
  6944. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6945. "Outside of structure, run the binding of `"
  6946. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6947. "'.")
  6948. '(interactive "p")
  6949. (list 'if
  6950. '(org-context-p 'headline 'item)
  6951. (list 'org-run-like-in-org-mode (list 'quote fun))
  6952. (list 'let '(orgstruct-mode)
  6953. (list 'call-interactively
  6954. (append '(or)
  6955. (mapcar (lambda (k)
  6956. (list 'key-binding k))
  6957. keys)
  6958. '('orgstruct-error))))))))
  6959. (defun org-context-p (&rest contexts)
  6960. "Check if local context is and of CONTEXTS.
  6961. Possible values in the list of contexts are `table', `headline', and `item'."
  6962. (let ((pos (point)))
  6963. (goto-char (point-at-bol))
  6964. (prog1 (or (and (memq 'table contexts)
  6965. (looking-at "[ \t]*|"))
  6966. (and (memq 'headline contexts)
  6967. (looking-at "\\*+"))
  6968. (and (memq 'item contexts)
  6969. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6970. (goto-char pos))))
  6971. (defun org-get-local-variables ()
  6972. "Return a list of all local variables in an org-mode buffer."
  6973. (let (varlist)
  6974. (with-current-buffer (get-buffer-create "*Org tmp*")
  6975. (erase-buffer)
  6976. (org-mode)
  6977. (setq varlist (buffer-local-variables)))
  6978. (kill-buffer "*Org tmp*")
  6979. (delq nil
  6980. (mapcar
  6981. (lambda (x)
  6982. (setq x
  6983. (if (symbolp x)
  6984. (list x)
  6985. (list (car x) (list 'quote (cdr x)))))
  6986. (if (string-match
  6987. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6988. (symbol-name (car x)))
  6989. x nil))
  6990. varlist))))
  6991. ;;;###autoload
  6992. (defun org-run-like-in-org-mode (cmd)
  6993. (unless org-local-vars
  6994. (setq org-local-vars (org-get-local-variables)))
  6995. (eval (list 'let org-local-vars
  6996. (list 'call-interactively (list 'quote cmd)))))
  6997. ;;;; Archiving
  6998. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6999. (defun org-archive-subtree (&optional find-done)
  7000. "Move the current subtree to the archive.
  7001. The archive can be a certain top-level heading in the current file, or in
  7002. a different file. The tree will be moved to that location, the subtree
  7003. heading be marked DONE, and the current time will be added.
  7004. When called with prefix argument FIND-DONE, find whole trees without any
  7005. open TODO items and archive them (after getting confirmation from the user).
  7006. If the cursor is not at a headline when this comand is called, try all level
  7007. 1 trees. If the cursor is on a headline, only try the direct children of
  7008. this heading."
  7009. (interactive "P")
  7010. (if find-done
  7011. (org-archive-all-done)
  7012. ;; Save all relevant TODO keyword-relatex variables
  7013. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7014. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7015. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7016. (tr-org-done-keywords org-done-keywords)
  7017. (tr-org-todo-regexp org-todo-regexp)
  7018. (tr-org-todo-line-regexp org-todo-line-regexp)
  7019. (tr-org-odd-levels-only org-odd-levels-only)
  7020. (this-buffer (current-buffer))
  7021. (org-archive-location org-archive-location)
  7022. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7023. ;; start of variables that will be used for saving context
  7024. ;; The compiler complains about them - keep them anyway!
  7025. (file (abbreviate-file-name (buffer-file-name)))
  7026. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7027. (time (format-time-string
  7028. (substring (cdr org-time-stamp-formats) 1 -1)
  7029. (current-time)))
  7030. afile heading buffer level newfile-p
  7031. category todo priority
  7032. ;; start of variables that will be used for savind context
  7033. ltags itags prop)
  7034. ;; Try to find a local archive location
  7035. (save-excursion
  7036. (save-restriction
  7037. (widen)
  7038. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7039. (if (and prop (string-match "\\S-" prop))
  7040. (setq org-archive-location prop)
  7041. (if (or (re-search-backward re nil t)
  7042. (re-search-forward re nil t))
  7043. (setq org-archive-location (match-string 1))))))
  7044. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7045. (progn
  7046. (setq afile (format (match-string 1 org-archive-location)
  7047. (file-name-nondirectory buffer-file-name))
  7048. heading (match-string 2 org-archive-location)))
  7049. (error "Invalid `org-archive-location'"))
  7050. (if (> (length afile) 0)
  7051. (setq newfile-p (not (file-exists-p afile))
  7052. buffer (find-file-noselect afile))
  7053. (setq buffer (current-buffer)))
  7054. (unless buffer
  7055. (error "Cannot access file \"%s\"" afile))
  7056. (if (and (> (length heading) 0)
  7057. (string-match "^\\*+" heading))
  7058. (setq level (match-end 0))
  7059. (setq heading nil level 0))
  7060. (save-excursion
  7061. (org-back-to-heading t)
  7062. ;; Get context information that will be lost by moving the tree
  7063. (org-refresh-category-properties)
  7064. (setq category (org-get-category)
  7065. todo (and (looking-at org-todo-line-regexp)
  7066. (match-string 2))
  7067. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7068. ltags (org-get-tags)
  7069. itags (org-delete-all ltags (org-get-tags-at)))
  7070. (setq ltags (mapconcat 'identity ltags " ")
  7071. itags (mapconcat 'identity itags " "))
  7072. ;; We first only copy, in case something goes wrong
  7073. ;; we need to protect this-command, to avoid kill-region sets it,
  7074. ;; which would lead to duplication of subtrees
  7075. (let (this-command) (org-copy-subtree))
  7076. (set-buffer buffer)
  7077. ;; Enforce org-mode for the archive buffer
  7078. (if (not (org-mode-p))
  7079. ;; Force the mode for future visits.
  7080. (let ((org-insert-mode-line-in-empty-file t)
  7081. (org-inhibit-startup t))
  7082. (call-interactively 'org-mode)))
  7083. (when newfile-p
  7084. (goto-char (point-max))
  7085. (insert (format "\nArchived entries from file %s\n\n"
  7086. (buffer-file-name this-buffer))))
  7087. ;; Force the TODO keywords of the original buffer
  7088. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7089. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7090. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7091. (org-done-keywords tr-org-done-keywords)
  7092. (org-todo-regexp tr-org-todo-regexp)
  7093. (org-todo-line-regexp tr-org-todo-line-regexp)
  7094. (org-odd-levels-only
  7095. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7096. org-odd-levels-only
  7097. tr-org-odd-levels-only)))
  7098. (goto-char (point-min))
  7099. (if heading
  7100. (progn
  7101. (if (re-search-forward
  7102. (concat "^" (regexp-quote heading)
  7103. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7104. nil t)
  7105. (goto-char (match-end 0))
  7106. ;; Heading not found, just insert it at the end
  7107. (goto-char (point-max))
  7108. (or (bolp) (insert "\n"))
  7109. (insert "\n" heading "\n")
  7110. (end-of-line 0))
  7111. ;; Make the subtree visible
  7112. (show-subtree)
  7113. (org-end-of-subtree t)
  7114. (skip-chars-backward " \t\r\n")
  7115. (and (looking-at "[ \t\r\n]*")
  7116. (replace-match "\n\n")))
  7117. ;; No specific heading, just go to end of file.
  7118. (goto-char (point-max)) (insert "\n"))
  7119. ;; Paste
  7120. (org-paste-subtree (org-get-legal-level level 1))
  7121. ;; Mark the entry as done
  7122. (when (and org-archive-mark-done
  7123. (looking-at org-todo-line-regexp)
  7124. (or (not (match-end 2))
  7125. (not (member (match-string 2) org-done-keywords))))
  7126. (let (org-log-done)
  7127. (org-todo
  7128. (car (or (member org-archive-mark-done org-done-keywords)
  7129. org-done-keywords)))))
  7130. ;; Add the context info
  7131. (when org-archive-save-context-info
  7132. (let ((l org-archive-save-context-info) e n v)
  7133. (while (setq e (pop l))
  7134. (when (and (setq v (symbol-value e))
  7135. (stringp v) (string-match "\\S-" v))
  7136. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7137. (org-entry-put (point) n v)))))
  7138. ;; Save the buffer, if it is not the same buffer.
  7139. (if (not (eq this-buffer buffer)) (save-buffer))))
  7140. ;; Here we are back in the original buffer. Everything seems to have
  7141. ;; worked. So now cut the tree and finish up.
  7142. (let (this-command) (org-cut-subtree))
  7143. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7144. (message "Subtree archived %s"
  7145. (if (eq this-buffer buffer)
  7146. (concat "under heading: " heading)
  7147. (concat "in file: " (abbreviate-file-name afile)))))))
  7148. (defun org-refresh-category-properties ()
  7149. "Refresh category text properties in teh buffer."
  7150. (let ((def-cat (cond
  7151. ((null org-category)
  7152. (if buffer-file-name
  7153. (file-name-sans-extension
  7154. (file-name-nondirectory buffer-file-name))
  7155. "???"))
  7156. ((symbolp org-category) (symbol-name org-category))
  7157. (t org-category)))
  7158. beg end cat pos optionp)
  7159. (org-unmodified
  7160. (save-excursion
  7161. (save-restriction
  7162. (widen)
  7163. (goto-char (point-min))
  7164. (put-text-property (point) (point-max) 'org-category def-cat)
  7165. (while (re-search-forward
  7166. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7167. (setq pos (match-end 0)
  7168. optionp (equal (char-after (match-beginning 0)) ?#)
  7169. cat (org-trim (match-string 2)))
  7170. (if optionp
  7171. (setq beg (point-at-bol) end (point-max))
  7172. (org-back-to-heading t)
  7173. (setq beg (point) end (org-end-of-subtree t t)))
  7174. (put-text-property beg end 'org-category cat)
  7175. (goto-char pos)))))))
  7176. (defun org-archive-all-done (&optional tag)
  7177. "Archive sublevels of the current tree without open TODO items.
  7178. If the cursor is not on a headline, try all level 1 trees. If
  7179. it is on a headline, try all direct children.
  7180. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7181. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7182. (rea (concat ".*:" org-archive-tag ":"))
  7183. (begm (make-marker))
  7184. (endm (make-marker))
  7185. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7186. "Move subtree to archive (no open TODO items)? "))
  7187. beg end (cntarch 0))
  7188. (if (org-on-heading-p)
  7189. (progn
  7190. (setq re1 (concat "^" (regexp-quote
  7191. (make-string
  7192. (1+ (- (match-end 0) (match-beginning 0) 1))
  7193. ?*))
  7194. " "))
  7195. (move-marker begm (point))
  7196. (move-marker endm (org-end-of-subtree t)))
  7197. (setq re1 "^* ")
  7198. (move-marker begm (point-min))
  7199. (move-marker endm (point-max)))
  7200. (save-excursion
  7201. (goto-char begm)
  7202. (while (re-search-forward re1 endm t)
  7203. (setq beg (match-beginning 0)
  7204. end (save-excursion (org-end-of-subtree t) (point)))
  7205. (goto-char beg)
  7206. (if (re-search-forward re end t)
  7207. (goto-char end)
  7208. (goto-char beg)
  7209. (if (and (or (not tag) (not (looking-at rea)))
  7210. (y-or-n-p question))
  7211. (progn
  7212. (if tag
  7213. (org-toggle-tag org-archive-tag 'on)
  7214. (org-archive-subtree))
  7215. (setq cntarch (1+ cntarch)))
  7216. (goto-char end)))))
  7217. (message "%d trees archived" cntarch)))
  7218. (defun org-cycle-hide-drawers (state)
  7219. "Re-hide all drawers after a visibility state change."
  7220. (when (and (org-mode-p)
  7221. (not (memq state '(overview folded))))
  7222. (save-excursion
  7223. (let* ((globalp (memq state '(contents all)))
  7224. (beg (if globalp (point-min) (point)))
  7225. (end (if globalp (point-max) (org-end-of-subtree t))))
  7226. (goto-char beg)
  7227. (while (re-search-forward org-drawer-regexp end t)
  7228. (org-flag-drawer t))))))
  7229. (defun org-flag-drawer (flag)
  7230. (save-excursion
  7231. (beginning-of-line 1)
  7232. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7233. (let ((b (match-end 0))
  7234. (outline-regexp org-outline-regexp))
  7235. (if (re-search-forward
  7236. "^[ \t]*:END:"
  7237. (save-excursion (outline-next-heading) (point)) t)
  7238. (outline-flag-region b (point-at-eol) flag)
  7239. (error ":END: line missing"))))))
  7240. (defun org-cycle-hide-archived-subtrees (state)
  7241. "Re-hide all archived subtrees after a visibility state change."
  7242. (when (and (not org-cycle-open-archived-trees)
  7243. (not (memq state '(overview folded))))
  7244. (save-excursion
  7245. (let* ((globalp (memq state '(contents all)))
  7246. (beg (if globalp (point-min) (point)))
  7247. (end (if globalp (point-max) (org-end-of-subtree t))))
  7248. (org-hide-archived-subtrees beg end)
  7249. (goto-char beg)
  7250. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7251. (message "%s" (substitute-command-keys
  7252. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7253. (defun org-force-cycle-archived ()
  7254. "Cycle subtree even if it is archived."
  7255. (interactive)
  7256. (setq this-command 'org-cycle)
  7257. (let ((org-cycle-open-archived-trees t))
  7258. (call-interactively 'org-cycle)))
  7259. (defun org-hide-archived-subtrees (beg end)
  7260. "Re-hide all archived subtrees after a visibility state change."
  7261. (save-excursion
  7262. (let* ((re (concat ":" org-archive-tag ":")))
  7263. (goto-char beg)
  7264. (while (re-search-forward re end t)
  7265. (and (org-on-heading-p) (hide-subtree))
  7266. (org-end-of-subtree t)))))
  7267. (defun org-toggle-tag (tag &optional onoff)
  7268. "Toggle the tag TAG for the current line.
  7269. If ONOFF is `on' or `off', don't toggle but set to this state."
  7270. (unless (org-on-heading-p t) (error "Not on headling"))
  7271. (let (res current)
  7272. (save-excursion
  7273. (beginning-of-line)
  7274. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7275. (point-at-eol) t)
  7276. (progn
  7277. (setq current (match-string 1))
  7278. (replace-match ""))
  7279. (setq current ""))
  7280. (setq current (nreverse (org-split-string current ":")))
  7281. (cond
  7282. ((eq onoff 'on)
  7283. (setq res t)
  7284. (or (member tag current) (push tag current)))
  7285. ((eq onoff 'off)
  7286. (or (not (member tag current)) (setq current (delete tag current))))
  7287. (t (if (member tag current)
  7288. (setq current (delete tag current))
  7289. (setq res t)
  7290. (push tag current))))
  7291. (end-of-line 1)
  7292. (if current
  7293. (progn
  7294. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7295. (org-set-tags nil t))
  7296. (delete-horizontal-space))
  7297. (run-hooks 'org-after-tags-change-hook))
  7298. res))
  7299. (defun org-toggle-archive-tag (&optional arg)
  7300. "Toggle the archive tag for the current headline.
  7301. With prefix ARG, check all children of current headline and offer tagging
  7302. the children that do not contain any open TODO items."
  7303. (interactive "P")
  7304. (if arg
  7305. (org-archive-all-done 'tag)
  7306. (let (set)
  7307. (save-excursion
  7308. (org-back-to-heading t)
  7309. (setq set (org-toggle-tag org-archive-tag))
  7310. (when set (hide-subtree)))
  7311. (and set (beginning-of-line 1))
  7312. (message "Subtree %s" (if set "archived" "unarchived")))))
  7313. ;;;; Tables
  7314. ;;; The table editor
  7315. ;; Watch out: Here we are talking about two different kind of tables.
  7316. ;; Most of the code is for the tables created with the Org-mode table editor.
  7317. ;; Sometimes, we talk about tables created and edited with the table.el
  7318. ;; Emacs package. We call the former org-type tables, and the latter
  7319. ;; table.el-type tables.
  7320. (defun org-before-change-function (beg end)
  7321. "Every change indicates that a table might need an update."
  7322. (setq org-table-may-need-update t))
  7323. (defconst org-table-line-regexp "^[ \t]*|"
  7324. "Detects an org-type table line.")
  7325. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7326. "Detects an org-type table line.")
  7327. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7328. "Detects a table line marked for automatic recalculation.")
  7329. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7330. "Detects a table line marked for automatic recalculation.")
  7331. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7332. "Detects a table line marked for automatic recalculation.")
  7333. (defconst org-table-hline-regexp "^[ \t]*|-"
  7334. "Detects an org-type table hline.")
  7335. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7336. "Detects a table-type table hline.")
  7337. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7338. "Detects an org-type or table-type table.")
  7339. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7340. "Searching from within a table (any type) this finds the first line
  7341. outside the table.")
  7342. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7343. "Searching from within a table (any type) this finds the first line
  7344. outside the table.")
  7345. (defvar org-table-last-highlighted-reference nil)
  7346. (defvar org-table-formula-history nil)
  7347. (defvar org-table-column-names nil
  7348. "Alist with column names, derived from the `!' line.")
  7349. (defvar org-table-column-name-regexp nil
  7350. "Regular expression matching the current column names.")
  7351. (defvar org-table-local-parameters nil
  7352. "Alist with parameter names, derived from the `$' line.")
  7353. (defvar org-table-named-field-locations nil
  7354. "Alist with locations of named fields.")
  7355. (defvar org-table-current-line-types nil
  7356. "Table row types, non-nil only for the duration of a comand.")
  7357. (defvar org-table-current-begin-line nil
  7358. "Table begin line, non-nil only for the duration of a comand.")
  7359. (defvar org-table-current-begin-pos nil
  7360. "Table begin position, non-nil only for the duration of a comand.")
  7361. (defvar org-table-dlines nil
  7362. "Vector of data line line numbers in the current table.")
  7363. (defvar org-table-hlines nil
  7364. "Vector of hline line numbers in the current table.")
  7365. (defconst org-table-range-regexp
  7366. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7367. ;; 1 2 3 4 5
  7368. "Regular expression for matching ranges in formulas.")
  7369. (defconst org-table-range-regexp2
  7370. (concat
  7371. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7372. "\\.\\."
  7373. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7374. "Match a range for reference display.")
  7375. (defconst org-table-translate-regexp
  7376. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7377. "Match a reference that needs translation, for reference display.")
  7378. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7379. (defun org-table-create-with-table.el ()
  7380. "Use the table.el package to insert a new table.
  7381. If there is already a table at point, convert between Org-mode tables
  7382. and table.el tables."
  7383. (interactive)
  7384. (require 'table)
  7385. (cond
  7386. ((org-at-table.el-p)
  7387. (if (y-or-n-p "Convert table to Org-mode table? ")
  7388. (org-table-convert)))
  7389. ((org-at-table-p)
  7390. (if (y-or-n-p "Convert table to table.el table? ")
  7391. (org-table-convert)))
  7392. (t (call-interactively 'table-insert))))
  7393. (defun org-table-create-or-convert-from-region (arg)
  7394. "Convert region to table, or create an empty table.
  7395. If there is an active region, convert it to a table, using the function
  7396. `org-table-convert-region'. See the documentation of that function
  7397. to learn how the prefix argument is interpreted to determine the field
  7398. separator.
  7399. If there is no such region, create an empty table with `org-table-create'."
  7400. (interactive "P")
  7401. (if (org-region-active-p)
  7402. (org-table-convert-region (region-beginning) (region-end) arg)
  7403. (org-table-create arg)))
  7404. (defun org-table-create (&optional size)
  7405. "Query for a size and insert a table skeleton.
  7406. SIZE is a string Columns x Rows like for example \"3x2\"."
  7407. (interactive "P")
  7408. (unless size
  7409. (setq size (read-string
  7410. (concat "Table size Columns x Rows [e.g. "
  7411. org-table-default-size "]: ")
  7412. "" nil org-table-default-size)))
  7413. (let* ((pos (point))
  7414. (indent (make-string (current-column) ?\ ))
  7415. (split (org-split-string size " *x *"))
  7416. (rows (string-to-number (nth 1 split)))
  7417. (columns (string-to-number (car split)))
  7418. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7419. "\n")))
  7420. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7421. (point-at-bol) (point)))
  7422. (beginning-of-line 1)
  7423. (newline))
  7424. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7425. (dotimes (i rows) (insert line))
  7426. (goto-char pos)
  7427. (if (> rows 1)
  7428. ;; Insert a hline after the first row.
  7429. (progn
  7430. (end-of-line 1)
  7431. (insert "\n|-")
  7432. (goto-char pos)))
  7433. (org-table-align)))
  7434. (defun org-table-convert-region (beg0 end0 &optional separator)
  7435. "Convert region to a table.
  7436. The region goes from BEG0 to END0, but these borders will be moved
  7437. slightly, to make sure a beginning of line in the first line is included.
  7438. SEPARATOR specifies the field separator in the lines. It can have the
  7439. following values:
  7440. '(4) Use the comma as a field separator
  7441. '(16) Use a TAB as field separator
  7442. integer When a number, use that many spaces as field separator
  7443. nil When nil, the command tries to be smart and figure out the
  7444. separator in the following way:
  7445. - when each line contains a TAB, assume TAB-separated material
  7446. - when each line contains a comme, assume CSV material
  7447. - else, assume one or more SPACE charcters as separator."
  7448. (interactive "rP")
  7449. (let* ((beg (min beg0 end0))
  7450. (end (max beg0 end0))
  7451. re)
  7452. (goto-char beg)
  7453. (beginning-of-line 1)
  7454. (setq beg (move-marker (make-marker) (point)))
  7455. (goto-char end)
  7456. (if (bolp) (backward-char 1) (end-of-line 1))
  7457. (setq end (move-marker (make-marker) (point)))
  7458. ;; Get the right field separator
  7459. (unless separator
  7460. (goto-char beg)
  7461. (setq separator
  7462. (cond
  7463. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7464. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7465. (t 1))))
  7466. (setq re (cond
  7467. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7468. ((equal separator '(16)) "^\\|\t")
  7469. ((integerp separator)
  7470. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7471. (t (error "This should not happen"))))
  7472. (goto-char beg)
  7473. (while (re-search-forward re end t)
  7474. (replace-match "| " t t))
  7475. (goto-char beg)
  7476. (insert " ")
  7477. (org-table-align)))
  7478. (defun org-table-import (file arg)
  7479. "Import FILE as a table.
  7480. The file is assumed to be tab-separated. Such files can be produced by most
  7481. spreadsheet and database applications. If no tabs (at least one per line)
  7482. are found, lines will be split on whitespace into fields."
  7483. (interactive "f\nP")
  7484. (or (bolp) (newline))
  7485. (let ((beg (point))
  7486. (pm (point-max)))
  7487. (insert-file-contents file)
  7488. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7489. (defun org-table-export ()
  7490. "Export table as a tab-separated file.
  7491. Such a file can be imported into a spreadsheet program like Excel."
  7492. (interactive)
  7493. (let* ((beg (org-table-begin))
  7494. (end (org-table-end))
  7495. (table (buffer-substring beg end))
  7496. (file (read-file-name "Export table to: "))
  7497. buf)
  7498. (unless (or (not (file-exists-p file))
  7499. (y-or-n-p (format "Overwrite file %s? " file)))
  7500. (error "Abort"))
  7501. (with-current-buffer (find-file-noselect file)
  7502. (setq buf (current-buffer))
  7503. (erase-buffer)
  7504. (fundamental-mode)
  7505. (insert table)
  7506. (goto-char (point-min))
  7507. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7508. (replace-match "" t t)
  7509. (end-of-line 1))
  7510. (goto-char (point-min))
  7511. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7512. (replace-match "" t t)
  7513. (goto-char (min (1+ (point)) (point-max))))
  7514. (goto-char (point-min))
  7515. (while (re-search-forward "^-[-+]*$" nil t)
  7516. (replace-match "")
  7517. (if (looking-at "\n")
  7518. (delete-char 1)))
  7519. (goto-char (point-min))
  7520. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7521. (replace-match "\t" t t))
  7522. (save-buffer))
  7523. (kill-buffer buf)))
  7524. (defvar org-table-aligned-begin-marker (make-marker)
  7525. "Marker at the beginning of the table last aligned.
  7526. Used to check if cursor still is in that table, to minimize realignment.")
  7527. (defvar org-table-aligned-end-marker (make-marker)
  7528. "Marker at the end of the table last aligned.
  7529. Used to check if cursor still is in that table, to minimize realignment.")
  7530. (defvar org-table-last-alignment nil
  7531. "List of flags for flushright alignment, from the last re-alignment.
  7532. This is being used to correctly align a single field after TAB or RET.")
  7533. (defvar org-table-last-column-widths nil
  7534. "List of max width of fields in each column.
  7535. This is being used to correctly align a single field after TAB or RET.")
  7536. (defvar org-table-overlay-coordinates nil
  7537. "Overlay coordinates after each align of a table.")
  7538. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7539. (defvar org-last-recalc-line nil)
  7540. (defconst org-narrow-column-arrow "=>"
  7541. "Used as display property in narrowed table columns.")
  7542. (defun org-table-align ()
  7543. "Align the table at point by aligning all vertical bars."
  7544. (interactive)
  7545. (let* (
  7546. ;; Limits of table
  7547. (beg (org-table-begin))
  7548. (end (org-table-end))
  7549. ;; Current cursor position
  7550. (linepos (org-current-line))
  7551. (colpos (org-table-current-column))
  7552. (winstart (window-start))
  7553. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7554. lines (new "") lengths l typenums ty fields maxfields i
  7555. column
  7556. (indent "") cnt frac
  7557. rfmt hfmt
  7558. (spaces '(1 . 1))
  7559. (sp1 (car spaces))
  7560. (sp2 (cdr spaces))
  7561. (rfmt1 (concat
  7562. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7563. (hfmt1 (concat
  7564. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7565. emptystrings links dates emph narrow fmax f1 len c e)
  7566. (untabify beg end)
  7567. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7568. ;; Check if we have links or dates
  7569. (goto-char beg)
  7570. (setq links (re-search-forward org-bracket-link-regexp end t))
  7571. (goto-char beg)
  7572. (setq emph (and org-hide-emphasis-markers
  7573. (re-search-forward org-emph-re end t)))
  7574. (goto-char beg)
  7575. (setq dates (and org-display-custom-times
  7576. (re-search-forward org-ts-regexp-both end t)))
  7577. ;; Make sure the link properties are right
  7578. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7579. ;; Make sure the date properties are right
  7580. (when dates (goto-char beg) (while (org-activate-dates end)))
  7581. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7582. ;; Check if we are narrowing any columns
  7583. (goto-char beg)
  7584. (setq narrow (and org-format-transports-properties-p
  7585. (re-search-forward "<[0-9]+>" end t)))
  7586. ;; Get the rows
  7587. (setq lines (org-split-string
  7588. (buffer-substring beg end) "\n"))
  7589. ;; Store the indentation of the first line
  7590. (if (string-match "^ *" (car lines))
  7591. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7592. ;; Mark the hlines by setting the corresponding element to nil
  7593. ;; At the same time, we remove trailing space.
  7594. (setq lines (mapcar (lambda (l)
  7595. (if (string-match "^ *|-" l)
  7596. nil
  7597. (if (string-match "[ \t]+$" l)
  7598. (substring l 0 (match-beginning 0))
  7599. l)))
  7600. lines))
  7601. ;; Get the data fields by splitting the lines.
  7602. (setq fields (mapcar
  7603. (lambda (l)
  7604. (org-split-string l " *| *"))
  7605. (delq nil (copy-sequence lines))))
  7606. ;; How many fields in the longest line?
  7607. (condition-case nil
  7608. (setq maxfields (apply 'max (mapcar 'length fields)))
  7609. (error
  7610. (kill-region beg end)
  7611. (org-table-create org-table-default-size)
  7612. (error "Empty table - created default table")))
  7613. ;; A list of empty strings to fill any short rows on output
  7614. (setq emptystrings (make-list maxfields ""))
  7615. ;; Check for special formatting.
  7616. (setq i -1)
  7617. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7618. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7619. ;; Check if there is an explicit width specified
  7620. (when narrow
  7621. (setq c column fmax nil)
  7622. (while c
  7623. (setq e (pop c))
  7624. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7625. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7626. ;; Find fields that are wider than fmax, and shorten them
  7627. (when fmax
  7628. (loop for xx in column do
  7629. (when (and (stringp xx)
  7630. (> (org-string-width xx) fmax))
  7631. (org-add-props xx nil
  7632. 'help-echo
  7633. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7634. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7635. (unless (> f1 1)
  7636. (error "Cannot narrow field starting with wide link \"%s\""
  7637. (match-string 0 xx)))
  7638. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7639. (add-text-properties (- f1 2) f1
  7640. (list 'display org-narrow-column-arrow)
  7641. xx)))))
  7642. ;; Get the maximum width for each column
  7643. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7644. ;; Get the fraction of numbers, to decide about alignment of the column
  7645. (setq cnt 0 frac 0.0)
  7646. (loop for x in column do
  7647. (if (equal x "")
  7648. nil
  7649. (setq frac ( / (+ (* frac cnt)
  7650. (if (string-match org-table-number-regexp x) 1 0))
  7651. (setq cnt (1+ cnt))))))
  7652. (push (>= frac org-table-number-fraction) typenums))
  7653. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7654. ;; Store the alignment of this table, for later editing of single fields
  7655. (setq org-table-last-alignment typenums
  7656. org-table-last-column-widths lengths)
  7657. ;; With invisible characters, `format' does not get the field width right
  7658. ;; So we need to make these fields wide by hand.
  7659. (when (or links emph)
  7660. (loop for i from 0 upto (1- maxfields) do
  7661. (setq len (nth i lengths))
  7662. (loop for j from 0 upto (1- (length fields)) do
  7663. (setq c (nthcdr i (car (nthcdr j fields))))
  7664. (if (and (stringp (car c))
  7665. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7666. ; (string-match org-bracket-link-regexp (car c))
  7667. (< (org-string-width (car c)) len))
  7668. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7669. ;; Compute the formats needed for output of the table
  7670. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7671. (while (setq l (pop lengths))
  7672. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7673. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7674. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7675. (setq rfmt (concat rfmt "\n")
  7676. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7677. (setq new (mapconcat
  7678. (lambda (l)
  7679. (if l (apply 'format rfmt
  7680. (append (pop fields) emptystrings))
  7681. hfmt))
  7682. lines ""))
  7683. ;; Replace the old one
  7684. (delete-region beg end)
  7685. (move-marker end nil)
  7686. (move-marker org-table-aligned-begin-marker (point))
  7687. (insert new)
  7688. (move-marker org-table-aligned-end-marker (point))
  7689. (when (and orgtbl-mode (not (org-mode-p)))
  7690. (goto-char org-table-aligned-begin-marker)
  7691. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7692. ;; Try to move to the old location
  7693. (goto-line winstartline)
  7694. (setq winstart (point-at-bol))
  7695. (goto-line linepos)
  7696. (set-window-start (selected-window) winstart 'noforce)
  7697. (org-table-goto-column colpos)
  7698. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7699. (setq org-table-may-need-update nil)
  7700. ))
  7701. (defun org-string-width (s)
  7702. "Compute width of string, ignoring invisible characters.
  7703. This ignores character with invisibility property `org-link', and also
  7704. characters with property `org-cwidth', because these will become invisible
  7705. upon the next fontification round."
  7706. (let (b l)
  7707. (when (or (eq t buffer-invisibility-spec)
  7708. (assq 'org-link buffer-invisibility-spec))
  7709. (while (setq b (text-property-any 0 (length s)
  7710. 'invisible 'org-link s))
  7711. (setq s (concat (substring s 0 b)
  7712. (substring s (or (next-single-property-change
  7713. b 'invisible s) (length s)))))))
  7714. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7715. (setq s (concat (substring s 0 b)
  7716. (substring s (or (next-single-property-change
  7717. b 'org-cwidth s) (length s))))))
  7718. (setq l (string-width s) b -1)
  7719. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7720. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7721. l))
  7722. (defun org-table-begin (&optional table-type)
  7723. "Find the beginning of the table and return its position.
  7724. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7725. (save-excursion
  7726. (if (not (re-search-backward
  7727. (if table-type org-table-any-border-regexp
  7728. org-table-border-regexp)
  7729. nil t))
  7730. (progn (goto-char (point-min)) (point))
  7731. (goto-char (match-beginning 0))
  7732. (beginning-of-line 2)
  7733. (point))))
  7734. (defun org-table-end (&optional table-type)
  7735. "Find the end of the table and return its position.
  7736. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7737. (save-excursion
  7738. (if (not (re-search-forward
  7739. (if table-type org-table-any-border-regexp
  7740. org-table-border-regexp)
  7741. nil t))
  7742. (goto-char (point-max))
  7743. (goto-char (match-beginning 0)))
  7744. (point-marker)))
  7745. (defun org-table-justify-field-maybe (&optional new)
  7746. "Justify the current field, text to left, number to right.
  7747. Optional argument NEW may specify text to replace the current field content."
  7748. (cond
  7749. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7750. ((org-at-table-hline-p))
  7751. ((and (not new)
  7752. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7753. (current-buffer)))
  7754. (< (point) org-table-aligned-begin-marker)
  7755. (>= (point) org-table-aligned-end-marker)))
  7756. ;; This is not the same table, force a full re-align
  7757. (setq org-table-may-need-update t))
  7758. (t ;; realign the current field, based on previous full realign
  7759. (let* ((pos (point)) s
  7760. (col (org-table-current-column))
  7761. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7762. l f n o e)
  7763. (when (> col 0)
  7764. (skip-chars-backward "^|\n")
  7765. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7766. (progn
  7767. (setq s (match-string 1)
  7768. o (match-string 0)
  7769. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7770. e (not (= (match-beginning 2) (match-end 2))))
  7771. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7772. l (if e "|" (setq org-table-may-need-update t) ""))
  7773. n (format f s))
  7774. (if new
  7775. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7776. (setq n (format f new))
  7777. (setq n (concat new "|") org-table-may-need-update t)))
  7778. (or (equal n o)
  7779. (let (org-table-may-need-update)
  7780. (replace-match n t t))))
  7781. (setq org-table-may-need-update t))
  7782. (goto-char pos))))))
  7783. (defun org-table-next-field ()
  7784. "Go to the next field in the current table, creating new lines as needed.
  7785. Before doing so, re-align the table if necessary."
  7786. (interactive)
  7787. (org-table-maybe-eval-formula)
  7788. (org-table-maybe-recalculate-line)
  7789. (if (and org-table-automatic-realign
  7790. org-table-may-need-update)
  7791. (org-table-align))
  7792. (let ((end (org-table-end)))
  7793. (if (org-at-table-hline-p)
  7794. (end-of-line 1))
  7795. (condition-case nil
  7796. (progn
  7797. (re-search-forward "|" end)
  7798. (if (looking-at "[ \t]*$")
  7799. (re-search-forward "|" end))
  7800. (if (and (looking-at "-")
  7801. org-table-tab-jumps-over-hlines
  7802. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7803. (goto-char (match-beginning 1)))
  7804. (if (looking-at "-")
  7805. (progn
  7806. (beginning-of-line 0)
  7807. (org-table-insert-row 'below))
  7808. (if (looking-at " ") (forward-char 1))))
  7809. (error
  7810. (org-table-insert-row 'below)))))
  7811. (defun org-table-previous-field ()
  7812. "Go to the previous field in the table.
  7813. Before doing so, re-align the table if necessary."
  7814. (interactive)
  7815. (org-table-justify-field-maybe)
  7816. (org-table-maybe-recalculate-line)
  7817. (if (and org-table-automatic-realign
  7818. org-table-may-need-update)
  7819. (org-table-align))
  7820. (if (org-at-table-hline-p)
  7821. (end-of-line 1))
  7822. (re-search-backward "|" (org-table-begin))
  7823. (re-search-backward "|" (org-table-begin))
  7824. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7825. (re-search-backward "|" (org-table-begin)))
  7826. (if (looking-at "| ?")
  7827. (goto-char (match-end 0))))
  7828. (defun org-table-next-row ()
  7829. "Go to the next row (same column) in the current table.
  7830. Before doing so, re-align the table if necessary."
  7831. (interactive)
  7832. (org-table-maybe-eval-formula)
  7833. (org-table-maybe-recalculate-line)
  7834. (if (or (looking-at "[ \t]*$")
  7835. (save-excursion (skip-chars-backward " \t") (bolp)))
  7836. (newline)
  7837. (if (and org-table-automatic-realign
  7838. org-table-may-need-update)
  7839. (org-table-align))
  7840. (let ((col (org-table-current-column)))
  7841. (beginning-of-line 2)
  7842. (if (or (not (org-at-table-p))
  7843. (org-at-table-hline-p))
  7844. (progn
  7845. (beginning-of-line 0)
  7846. (org-table-insert-row 'below)))
  7847. (org-table-goto-column col)
  7848. (skip-chars-backward "^|\n\r")
  7849. (if (looking-at " ") (forward-char 1)))))
  7850. (defun org-table-copy-down (n)
  7851. "Copy a field down in the current column.
  7852. If the field at the cursor is empty, copy into it the content of the nearest
  7853. non-empty field above. With argument N, use the Nth non-empty field.
  7854. If the current field is not empty, it is copied down to the next row, and
  7855. the cursor is moved with it. Therefore, repeating this command causes the
  7856. column to be filled row-by-row.
  7857. If the variable `org-table-copy-increment' is non-nil and the field is an
  7858. integer or a timestamp, it will be incremented while copying. In the case of
  7859. a timestamp, if the cursor is on the year, change the year. If it is on the
  7860. month or the day, change that. Point will stay on the current date field
  7861. in order to easily repeat the interval."
  7862. (interactive "p")
  7863. (let* ((colpos (org-table-current-column))
  7864. (col (current-column))
  7865. (field (org-table-get-field))
  7866. (non-empty (string-match "[^ \t]" field))
  7867. (beg (org-table-begin))
  7868. txt)
  7869. (org-table-check-inside-data-field)
  7870. (if non-empty
  7871. (progn
  7872. (setq txt (org-trim field))
  7873. (org-table-next-row)
  7874. (org-table-blank-field))
  7875. (save-excursion
  7876. (setq txt
  7877. (catch 'exit
  7878. (while (progn (beginning-of-line 1)
  7879. (re-search-backward org-table-dataline-regexp
  7880. beg t))
  7881. (org-table-goto-column colpos t)
  7882. (if (and (looking-at
  7883. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7884. (= (setq n (1- n)) 0))
  7885. (throw 'exit (match-string 1))))))))
  7886. (if txt
  7887. (progn
  7888. (if (and org-table-copy-increment
  7889. (string-match "^[0-9]+$" txt))
  7890. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7891. (insert txt)
  7892. (move-to-column col)
  7893. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7894. (org-timestamp-up 1)
  7895. (org-table-maybe-recalculate-line))
  7896. (org-table-align)
  7897. (move-to-column col))
  7898. (error "No non-empty field found"))))
  7899. (defun org-table-check-inside-data-field ()
  7900. "Is point inside a table data field?
  7901. I.e. not on a hline or before the first or after the last column?
  7902. This actually throws an error, so it aborts the current command."
  7903. (if (or (not (org-at-table-p))
  7904. (= (org-table-current-column) 0)
  7905. (org-at-table-hline-p)
  7906. (looking-at "[ \t]*$"))
  7907. (error "Not in table data field")))
  7908. (defvar org-table-clip nil
  7909. "Clipboard for table regions.")
  7910. (defun org-table-blank-field ()
  7911. "Blank the current table field or active region."
  7912. (interactive)
  7913. (org-table-check-inside-data-field)
  7914. (if (and (interactive-p) (org-region-active-p))
  7915. (let (org-table-clip)
  7916. (org-table-cut-region (region-beginning) (region-end)))
  7917. (skip-chars-backward "^|")
  7918. (backward-char 1)
  7919. (if (looking-at "|[^|\n]+")
  7920. (let* ((pos (match-beginning 0))
  7921. (match (match-string 0))
  7922. (len (org-string-width match)))
  7923. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7924. (goto-char (+ 2 pos))
  7925. (substring match 1)))))
  7926. (defun org-table-get-field (&optional n replace)
  7927. "Return the value of the field in column N of current row.
  7928. N defaults to current field.
  7929. If REPLACE is a string, replace field with this value. The return value
  7930. is always the old value."
  7931. (and n (org-table-goto-column n))
  7932. (skip-chars-backward "^|\n")
  7933. (backward-char 1)
  7934. (if (looking-at "|[^|\r\n]*")
  7935. (let* ((pos (match-beginning 0))
  7936. (val (buffer-substring (1+ pos) (match-end 0))))
  7937. (if replace
  7938. (replace-match (concat "|" replace) t t))
  7939. (goto-char (min (point-at-eol) (+ 2 pos)))
  7940. val)
  7941. (forward-char 1) ""))
  7942. (defun org-table-field-info (arg)
  7943. "Show info about the current field, and highlight any reference at point."
  7944. (interactive "P")
  7945. (org-table-get-specials)
  7946. (save-excursion
  7947. (let* ((pos (point))
  7948. (col (org-table-current-column))
  7949. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7950. (name (car (rassoc (list (org-current-line) col)
  7951. org-table-named-field-locations)))
  7952. (eql (org-table-get-stored-formulas))
  7953. (dline (org-table-current-dline))
  7954. (ref (format "@%d$%d" dline col))
  7955. (ref1 (org-table-convert-refs-to-an ref))
  7956. (fequation (or (assoc name eql) (assoc ref eql)))
  7957. (cequation (assoc (int-to-string col) eql))
  7958. (eqn (or fequation cequation)))
  7959. (goto-char pos)
  7960. (condition-case nil
  7961. (org-table-show-reference 'local)
  7962. (error nil))
  7963. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7964. dline col
  7965. (if cname (concat " or $" cname) "")
  7966. dline col ref1
  7967. (if name (concat " or $" name) "")
  7968. ;; FIXME: formula info not correct if special table line
  7969. (if eqn
  7970. (concat ", formula: "
  7971. (org-table-formula-to-user
  7972. (concat
  7973. (if (string-match "^[$@]"(car eqn)) "" "$")
  7974. (car eqn) "=" (cdr eqn))))
  7975. "")))))
  7976. (defun org-table-current-column ()
  7977. "Find out which column we are in.
  7978. When called interactively, column is also displayed in echo area."
  7979. (interactive)
  7980. (if (interactive-p) (org-table-check-inside-data-field))
  7981. (save-excursion
  7982. (let ((cnt 0) (pos (point)))
  7983. (beginning-of-line 1)
  7984. (while (search-forward "|" pos t)
  7985. (setq cnt (1+ cnt)))
  7986. (if (interactive-p) (message "This is table column %d" cnt))
  7987. cnt)))
  7988. (defun org-table-current-dline ()
  7989. "Find out what table data line we are in.
  7990. Only datalins count for this."
  7991. (interactive)
  7992. (if (interactive-p) (org-table-check-inside-data-field))
  7993. (save-excursion
  7994. (let ((cnt 0) (pos (point)))
  7995. (goto-char (org-table-begin))
  7996. (while (<= (point) pos)
  7997. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7998. (beginning-of-line 2))
  7999. (if (interactive-p) (message "This is table line %d" cnt))
  8000. cnt)))
  8001. (defun org-table-goto-column (n &optional on-delim force)
  8002. "Move the cursor to the Nth column in the current table line.
  8003. With optional argument ON-DELIM, stop with point before the left delimiter
  8004. of the field.
  8005. If there are less than N fields, just go to after the last delimiter.
  8006. However, when FORCE is non-nil, create new columns if necessary."
  8007. (interactive "p")
  8008. (let ((pos (point-at-eol)))
  8009. (beginning-of-line 1)
  8010. (when (> n 0)
  8011. (while (and (> (setq n (1- n)) -1)
  8012. (or (search-forward "|" pos t)
  8013. (and force
  8014. (progn (end-of-line 1)
  8015. (skip-chars-backward "^|")
  8016. (insert " | "))))))
  8017. ; (backward-char 2) t)))))
  8018. (when (and force (not (looking-at ".*|")))
  8019. (save-excursion (end-of-line 1) (insert " | ")))
  8020. (if on-delim
  8021. (backward-char 1)
  8022. (if (looking-at " ") (forward-char 1))))))
  8023. (defun org-at-table-p (&optional table-type)
  8024. "Return t if the cursor is inside an org-type table.
  8025. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8026. (if org-enable-table-editor
  8027. (save-excursion
  8028. (beginning-of-line 1)
  8029. (looking-at (if table-type org-table-any-line-regexp
  8030. org-table-line-regexp)))
  8031. nil))
  8032. (defun org-at-table.el-p ()
  8033. "Return t if and only if we are at a table.el table."
  8034. (and (org-at-table-p 'any)
  8035. (save-excursion
  8036. (goto-char (org-table-begin 'any))
  8037. (looking-at org-table1-hline-regexp))))
  8038. (defun org-table-recognize-table.el ()
  8039. "If there is a table.el table nearby, recognize it and move into it."
  8040. (if org-table-tab-recognizes-table.el
  8041. (if (org-at-table.el-p)
  8042. (progn
  8043. (beginning-of-line 1)
  8044. (if (looking-at org-table-dataline-regexp)
  8045. nil
  8046. (if (looking-at org-table1-hline-regexp)
  8047. (progn
  8048. (beginning-of-line 2)
  8049. (if (looking-at org-table-any-border-regexp)
  8050. (beginning-of-line -1)))))
  8051. (if (re-search-forward "|" (org-table-end t) t)
  8052. (progn
  8053. (require 'table)
  8054. (if (table--at-cell-p (point))
  8055. t
  8056. (message "recognizing table.el table...")
  8057. (table-recognize-table)
  8058. (message "recognizing table.el table...done")))
  8059. (error "This should not happen..."))
  8060. t)
  8061. nil)
  8062. nil))
  8063. (defun org-at-table-hline-p ()
  8064. "Return t if the cursor is inside a hline in a table."
  8065. (if org-enable-table-editor
  8066. (save-excursion
  8067. (beginning-of-line 1)
  8068. (looking-at org-table-hline-regexp))
  8069. nil))
  8070. (defun org-table-insert-column ()
  8071. "Insert a new column into the table."
  8072. (interactive)
  8073. (if (not (org-at-table-p))
  8074. (error "Not at a table"))
  8075. (org-table-find-dataline)
  8076. (let* ((col (max 1 (org-table-current-column)))
  8077. (beg (org-table-begin))
  8078. (end (org-table-end))
  8079. ;; Current cursor position
  8080. (linepos (org-current-line))
  8081. (colpos col))
  8082. (goto-char beg)
  8083. (while (< (point) end)
  8084. (if (org-at-table-hline-p)
  8085. nil
  8086. (org-table-goto-column col t)
  8087. (insert "| "))
  8088. (beginning-of-line 2))
  8089. (move-marker end nil)
  8090. (goto-line linepos)
  8091. (org-table-goto-column colpos)
  8092. (org-table-align)
  8093. (org-table-fix-formulas "$" nil (1- col) 1)))
  8094. (defun org-table-find-dataline ()
  8095. "Find a dataline in the current table, which is needed for column commands."
  8096. (if (and (org-at-table-p)
  8097. (not (org-at-table-hline-p)))
  8098. t
  8099. (let ((col (current-column))
  8100. (end (org-table-end)))
  8101. (move-to-column col)
  8102. (while (and (< (point) end)
  8103. (or (not (= (current-column) col))
  8104. (org-at-table-hline-p)))
  8105. (beginning-of-line 2)
  8106. (move-to-column col))
  8107. (if (and (org-at-table-p)
  8108. (not (org-at-table-hline-p)))
  8109. t
  8110. (error
  8111. "Please position cursor in a data line for column operations")))))
  8112. (defun org-table-delete-column ()
  8113. "Delete a column from the table."
  8114. (interactive)
  8115. (if (not (org-at-table-p))
  8116. (error "Not at a table"))
  8117. (org-table-find-dataline)
  8118. (org-table-check-inside-data-field)
  8119. (let* ((col (org-table-current-column))
  8120. (beg (org-table-begin))
  8121. (end (org-table-end))
  8122. ;; Current cursor position
  8123. (linepos (org-current-line))
  8124. (colpos col))
  8125. (goto-char beg)
  8126. (while (< (point) end)
  8127. (if (org-at-table-hline-p)
  8128. nil
  8129. (org-table-goto-column col t)
  8130. (and (looking-at "|[^|\n]+|")
  8131. (replace-match "|")))
  8132. (beginning-of-line 2))
  8133. (move-marker end nil)
  8134. (goto-line linepos)
  8135. (org-table-goto-column colpos)
  8136. (org-table-align)
  8137. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8138. col -1 col)))
  8139. (defun org-table-move-column-right ()
  8140. "Move column to the right."
  8141. (interactive)
  8142. (org-table-move-column nil))
  8143. (defun org-table-move-column-left ()
  8144. "Move column to the left."
  8145. (interactive)
  8146. (org-table-move-column 'left))
  8147. (defun org-table-move-column (&optional left)
  8148. "Move the current column to the right. With arg LEFT, move to the left."
  8149. (interactive "P")
  8150. (if (not (org-at-table-p))
  8151. (error "Not at a table"))
  8152. (org-table-find-dataline)
  8153. (org-table-check-inside-data-field)
  8154. (let* ((col (org-table-current-column))
  8155. (col1 (if left (1- col) col))
  8156. (beg (org-table-begin))
  8157. (end (org-table-end))
  8158. ;; Current cursor position
  8159. (linepos (org-current-line))
  8160. (colpos (if left (1- col) (1+ col))))
  8161. (if (and left (= col 1))
  8162. (error "Cannot move column further left"))
  8163. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8164. (error "Cannot move column further right"))
  8165. (goto-char beg)
  8166. (while (< (point) end)
  8167. (if (org-at-table-hline-p)
  8168. nil
  8169. (org-table-goto-column col1 t)
  8170. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8171. (replace-match "|\\2|\\1|")))
  8172. (beginning-of-line 2))
  8173. (move-marker end nil)
  8174. (goto-line linepos)
  8175. (org-table-goto-column colpos)
  8176. (org-table-align)
  8177. (org-table-fix-formulas
  8178. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8179. (cons (number-to-string colpos) (number-to-string col))))))
  8180. (defun org-table-move-row-down ()
  8181. "Move table row down."
  8182. (interactive)
  8183. (org-table-move-row nil))
  8184. (defun org-table-move-row-up ()
  8185. "Move table row up."
  8186. (interactive)
  8187. (org-table-move-row 'up))
  8188. (defun org-table-move-row (&optional up)
  8189. "Move the current table line down. With arg UP, move it up."
  8190. (interactive "P")
  8191. (let* ((col (current-column))
  8192. (pos (point))
  8193. (hline1p (save-excursion (beginning-of-line 1)
  8194. (looking-at org-table-hline-regexp)))
  8195. (dline1 (org-table-current-dline))
  8196. (dline2 (+ dline1 (if up -1 1)))
  8197. (tonew (if up 0 2))
  8198. txt hline2p)
  8199. (beginning-of-line tonew)
  8200. (unless (org-at-table-p)
  8201. (goto-char pos)
  8202. (error "Cannot move row further"))
  8203. (setq hline2p (looking-at org-table-hline-regexp))
  8204. (goto-char pos)
  8205. (beginning-of-line 1)
  8206. (setq pos (point))
  8207. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8208. (delete-region (point) (1+ (point-at-eol)))
  8209. (beginning-of-line tonew)
  8210. (insert txt)
  8211. (beginning-of-line 0)
  8212. (move-to-column col)
  8213. (unless (or hline1p hline2p)
  8214. (org-table-fix-formulas
  8215. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8216. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8217. (defun org-table-insert-row (&optional arg)
  8218. "Insert a new row above the current line into the table.
  8219. With prefix ARG, insert below the current line."
  8220. (interactive "P")
  8221. (if (not (org-at-table-p))
  8222. (error "Not at a table"))
  8223. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8224. (new (org-table-clean-line line)))
  8225. ;; Fix the first field if necessary
  8226. (if (string-match "^[ \t]*| *[#$] *|" line)
  8227. (setq new (replace-match (match-string 0 line) t t new)))
  8228. (beginning-of-line (if arg 2 1))
  8229. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8230. (beginning-of-line 0)
  8231. (re-search-forward "| ?" (point-at-eol) t)
  8232. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8233. (org-table-align))
  8234. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8235. (defun org-table-insert-hline (&optional above)
  8236. "Insert a horizontal-line below the current line into the table.
  8237. With prefix ABOVE, insert above the current line."
  8238. (interactive "P")
  8239. (if (not (org-at-table-p))
  8240. (error "Not at a table"))
  8241. (let ((line (org-table-clean-line
  8242. (buffer-substring (point-at-bol) (point-at-eol))))
  8243. (col (current-column)))
  8244. (while (string-match "|\\( +\\)|" line)
  8245. (setq line (replace-match
  8246. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8247. ?-) "|") t t line)))
  8248. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8249. (beginning-of-line (if above 1 2))
  8250. (insert line "\n")
  8251. (beginning-of-line (if above 1 -1))
  8252. (move-to-column col)
  8253. (and org-table-overlay-coordinates (org-table-align))))
  8254. (defun org-table-hline-and-move (&optional same-column)
  8255. "Insert a hline and move to the row below that line."
  8256. (interactive "P")
  8257. (let ((col (org-table-current-column)))
  8258. (org-table-maybe-eval-formula)
  8259. (org-table-maybe-recalculate-line)
  8260. (org-table-insert-hline)
  8261. (end-of-line 2)
  8262. (if (looking-at "\n[ \t]*|-")
  8263. (progn (insert "\n|") (org-table-align))
  8264. (org-table-next-field))
  8265. (if same-column (org-table-goto-column col))))
  8266. (defun org-table-clean-line (s)
  8267. "Convert a table line S into a string with only \"|\" and space.
  8268. In particular, this does handle wide and invisible characters."
  8269. (if (string-match "^[ \t]*|-" s)
  8270. ;; It's a hline, just map the characters
  8271. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8272. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8273. (setq s (replace-match
  8274. (concat "|" (make-string (org-string-width (match-string 1 s))
  8275. ?\ ) "|")
  8276. t t s)))
  8277. s))
  8278. (defun org-table-kill-row ()
  8279. "Delete the current row or horizontal line from the table."
  8280. (interactive)
  8281. (if (not (org-at-table-p))
  8282. (error "Not at a table"))
  8283. (let ((col (current-column))
  8284. (dline (org-table-current-dline)))
  8285. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8286. (if (not (org-at-table-p)) (beginning-of-line 0))
  8287. (move-to-column col)
  8288. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8289. dline -1 dline)))
  8290. (defun org-table-sort-lines (with-case &optional sorting-type)
  8291. "Sort table lines according to the column at point.
  8292. The position of point indicates the column to be used for
  8293. sorting, and the range of lines is the range between the nearest
  8294. horizontal separator lines, or the entire table of no such lines
  8295. exist. If point is before the first column, you will be prompted
  8296. for the sorting column. If there is an active region, the mark
  8297. specifies the first line and the sorting column, while point
  8298. should be in the last line to be included into the sorting.
  8299. The command then prompts for the sorting type which can be
  8300. alphabetically, numerically, or by time (as given in a time stamp
  8301. in the field). Sorting in reverse order is also possible.
  8302. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8303. If SORTING-TYPE is specified when this function is called from a Lisp
  8304. program, no prompting will take place. SORTING-TYPE must be a character,
  8305. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8306. should be done in reverse order."
  8307. (interactive "P")
  8308. (let* ((thisline (org-current-line))
  8309. (thiscol (org-table-current-column))
  8310. beg end bcol ecol tend tbeg column lns pos)
  8311. (when (equal thiscol 0)
  8312. (if (interactive-p)
  8313. (setq thiscol
  8314. (string-to-number
  8315. (read-string "Use column N for sorting: ")))
  8316. (setq thiscol 1))
  8317. (org-table-goto-column thiscol))
  8318. (org-table-check-inside-data-field)
  8319. (if (org-region-active-p)
  8320. (progn
  8321. (setq beg (region-beginning) end (region-end))
  8322. (goto-char beg)
  8323. (setq column (org-table-current-column)
  8324. beg (point-at-bol))
  8325. (goto-char end)
  8326. (setq end (point-at-bol 2)))
  8327. (setq column (org-table-current-column)
  8328. pos (point)
  8329. tbeg (org-table-begin)
  8330. tend (org-table-end))
  8331. (if (re-search-backward org-table-hline-regexp tbeg t)
  8332. (setq beg (point-at-bol 2))
  8333. (goto-char tbeg)
  8334. (setq beg (point-at-bol 1)))
  8335. (goto-char pos)
  8336. (if (re-search-forward org-table-hline-regexp tend t)
  8337. (setq end (point-at-bol 1))
  8338. (goto-char tend)
  8339. (setq end (point-at-bol))))
  8340. (setq beg (move-marker (make-marker) beg)
  8341. end (move-marker (make-marker) end))
  8342. (untabify beg end)
  8343. (goto-char beg)
  8344. (org-table-goto-column column)
  8345. (skip-chars-backward "^|")
  8346. (setq bcol (current-column))
  8347. (org-table-goto-column (1+ column))
  8348. (skip-chars-backward "^|")
  8349. (setq ecol (1- (current-column)))
  8350. (org-table-goto-column column)
  8351. (setq lns (mapcar (lambda(x) (cons
  8352. (org-sort-remove-invisible
  8353. (nth (1- column)
  8354. (org-split-string x "[ \t]*|[ \t]*")))
  8355. x))
  8356. (org-split-string (buffer-substring beg end) "\n")))
  8357. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8358. (delete-region beg end)
  8359. (move-marker beg nil)
  8360. (move-marker end nil)
  8361. (insert (mapconcat 'cdr lns "\n") "\n")
  8362. (goto-line thisline)
  8363. (org-table-goto-column thiscol)
  8364. (message "%d lines sorted, based on column %d" (length lns) column)))
  8365. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8366. (defun org-sort-remove-invisible (s)
  8367. (remove-text-properties 0 (length s) org-rm-props s)
  8368. (while (string-match org-bracket-link-regexp s)
  8369. (setq s (replace-match (if (match-end 2)
  8370. (match-string 3 s)
  8371. (match-string 1 s)) t t s)))
  8372. s)
  8373. (defun org-table-cut-region (beg end)
  8374. "Copy region in table to the clipboard and blank all relevant fields."
  8375. (interactive "r")
  8376. (org-table-copy-region beg end 'cut))
  8377. (defun org-table-copy-region (beg end &optional cut)
  8378. "Copy rectangular region in table to clipboard.
  8379. A special clipboard is used which can only be accessed
  8380. with `org-table-paste-rectangle'."
  8381. (interactive "rP")
  8382. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8383. region cols
  8384. (rpl (if cut " " nil)))
  8385. (goto-char beg)
  8386. (org-table-check-inside-data-field)
  8387. (setq l01 (org-current-line)
  8388. c01 (org-table-current-column))
  8389. (goto-char end)
  8390. (org-table-check-inside-data-field)
  8391. (setq l02 (org-current-line)
  8392. c02 (org-table-current-column))
  8393. (setq l1 (min l01 l02) l2 (max l01 l02)
  8394. c1 (min c01 c02) c2 (max c01 c02))
  8395. (catch 'exit
  8396. (while t
  8397. (catch 'nextline
  8398. (if (> l1 l2) (throw 'exit t))
  8399. (goto-line l1)
  8400. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8401. (setq cols nil ic1 c1 ic2 c2)
  8402. (while (< ic1 (1+ ic2))
  8403. (push (org-table-get-field ic1 rpl) cols)
  8404. (setq ic1 (1+ ic1)))
  8405. (push (nreverse cols) region)
  8406. (setq l1 (1+ l1)))))
  8407. (setq org-table-clip (nreverse region))
  8408. (if cut (org-table-align))
  8409. org-table-clip))
  8410. (defun org-table-paste-rectangle ()
  8411. "Paste a rectangular region into a table.
  8412. The upper right corner ends up in the current field. All involved fields
  8413. will be overwritten. If the rectangle does not fit into the present table,
  8414. the table is enlarged as needed. The process ignores horizontal separator
  8415. lines."
  8416. (interactive)
  8417. (unless (and org-table-clip (listp org-table-clip))
  8418. (error "First cut/copy a region to paste!"))
  8419. (org-table-check-inside-data-field)
  8420. (let* ((clip org-table-clip)
  8421. (line (org-current-line))
  8422. (col (org-table-current-column))
  8423. (org-enable-table-editor t)
  8424. (org-table-automatic-realign nil)
  8425. c cols field)
  8426. (while (setq cols (pop clip))
  8427. (while (org-at-table-hline-p) (beginning-of-line 2))
  8428. (if (not (org-at-table-p))
  8429. (progn (end-of-line 0) (org-table-next-field)))
  8430. (setq c col)
  8431. (while (setq field (pop cols))
  8432. (org-table-goto-column c nil 'force)
  8433. (org-table-get-field nil field)
  8434. (setq c (1+ c)))
  8435. (beginning-of-line 2))
  8436. (goto-line line)
  8437. (org-table-goto-column col)
  8438. (org-table-align)))
  8439. (defun org-table-convert ()
  8440. "Convert from `org-mode' table to table.el and back.
  8441. Obviously, this only works within limits. When an Org-mode table is
  8442. converted to table.el, all horizontal separator lines get lost, because
  8443. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8444. A table.el table can be converted to an Org-mode table only if it does not
  8445. do row or column spanning. Multiline cells will become multiple cells.
  8446. Beware, Org-mode does not test if the table can be successfully converted - it
  8447. blindly applies a recipe that works for simple tables."
  8448. (interactive)
  8449. (require 'table)
  8450. (if (org-at-table.el-p)
  8451. ;; convert to Org-mode table
  8452. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8453. (end (move-marker (make-marker) (org-table-end t))))
  8454. (table-unrecognize-region beg end)
  8455. (goto-char beg)
  8456. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8457. (replace-match ""))
  8458. (goto-char beg))
  8459. (if (org-at-table-p)
  8460. ;; convert to table.el table
  8461. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8462. (end (move-marker (make-marker) (org-table-end))))
  8463. ;; first, get rid of all horizontal lines
  8464. (goto-char beg)
  8465. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8466. (replace-match ""))
  8467. ;; insert a hline before first
  8468. (goto-char beg)
  8469. (org-table-insert-hline 'above)
  8470. (beginning-of-line -1)
  8471. ;; insert a hline after each line
  8472. (while (progn (beginning-of-line 3) (< (point) end))
  8473. (org-table-insert-hline))
  8474. (goto-char beg)
  8475. (setq end (move-marker end (org-table-end)))
  8476. ;; replace "+" at beginning and ending of hlines
  8477. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8478. (replace-match "\\1+-"))
  8479. (goto-char beg)
  8480. (while (re-search-forward "-|[ \t]*$" end t)
  8481. (replace-match "-+"))
  8482. (goto-char beg)))))
  8483. (defun org-table-wrap-region (arg)
  8484. "Wrap several fields in a column like a paragraph.
  8485. This is useful if you'd like to spread the contents of a field over several
  8486. lines, in order to keep the table compact.
  8487. If there is an active region, and both point and mark are in the same column,
  8488. the text in the column is wrapped to minimum width for the given number of
  8489. lines. Generally, this makes the table more compact. A prefix ARG may be
  8490. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8491. formats the selected text to two lines. If the region was longer than two
  8492. lines, the remaining lines remain empty. A negative prefix argument reduces
  8493. the current number of lines by that amount. The wrapped text is pasted back
  8494. into the table. If you formatted it to more lines than it was before, fields
  8495. further down in the table get overwritten - so you might need to make space in
  8496. the table first.
  8497. If there is no region, the current field is split at the cursor position and
  8498. the text fragment to the right of the cursor is prepended to the field one
  8499. line down.
  8500. If there is no region, but you specify a prefix ARG, the current field gets
  8501. blank, and the content is appended to the field above."
  8502. (interactive "P")
  8503. (org-table-check-inside-data-field)
  8504. (if (org-region-active-p)
  8505. ;; There is a region: fill as a paragraph
  8506. (let* ((beg (region-beginning))
  8507. (cline (save-excursion (goto-char beg) (org-current-line)))
  8508. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8509. nlines)
  8510. (org-table-cut-region (region-beginning) (region-end))
  8511. (if (> (length (car org-table-clip)) 1)
  8512. (error "Region must be limited to single column"))
  8513. (setq nlines (if arg
  8514. (if (< arg 1)
  8515. (+ (length org-table-clip) arg)
  8516. arg)
  8517. (length org-table-clip)))
  8518. (setq org-table-clip
  8519. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8520. nil nlines)))
  8521. (goto-line cline)
  8522. (org-table-goto-column ccol)
  8523. (org-table-paste-rectangle))
  8524. ;; No region, split the current field at point
  8525. (if arg
  8526. ;; combine with field above
  8527. (let ((s (org-table-blank-field))
  8528. (col (org-table-current-column)))
  8529. (beginning-of-line 0)
  8530. (while (org-at-table-hline-p) (beginning-of-line 0))
  8531. (org-table-goto-column col)
  8532. (skip-chars-forward "^|")
  8533. (skip-chars-backward " ")
  8534. (insert " " (org-trim s))
  8535. (org-table-align))
  8536. ;; split field
  8537. (when (looking-at "\\([^|]+\\)+|")
  8538. (let ((s (match-string 1)))
  8539. (replace-match " |")
  8540. (goto-char (match-beginning 0))
  8541. (org-table-next-row)
  8542. (insert (org-trim s) " ")
  8543. (org-table-align))))))
  8544. (defvar org-field-marker nil)
  8545. (defun org-table-edit-field (arg)
  8546. "Edit table field in a different window.
  8547. This is mainly useful for fields that contain hidden parts.
  8548. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8549. it can be edited in place."
  8550. (interactive "P")
  8551. (if arg
  8552. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8553. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8554. (remove-text-properties b e '(org-cwidth t invisible t
  8555. display t intangible t))
  8556. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8557. (font-lock-fontify-block)))
  8558. (let ((pos (move-marker (make-marker) (point)))
  8559. (field (org-table-get-field))
  8560. (cw (current-window-configuration))
  8561. p)
  8562. (org-switch-to-buffer-other-window "*Org tmp*")
  8563. (erase-buffer)
  8564. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8565. (let ((org-inhibit-startup t)) (org-mode))
  8566. (goto-char (setq p (point-max)))
  8567. (insert (org-trim field))
  8568. (remove-text-properties p (point-max)
  8569. '(invisible t org-cwidth t display t
  8570. intangible t))
  8571. (goto-char p)
  8572. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8573. (org-set-local 'org-window-configuration cw)
  8574. (org-set-local 'org-field-marker pos)
  8575. (message "Edit and finish with C-c C-c"))))
  8576. (defun org-table-finish-edit-field ()
  8577. "Finish editing a table data field.
  8578. Remove all newline characters, insert the result into the table, realign
  8579. the table and kill the editing buffer."
  8580. (let ((pos org-field-marker)
  8581. (cw org-window-configuration)
  8582. (cb (current-buffer))
  8583. text)
  8584. (goto-char (point-min))
  8585. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8586. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8587. (replace-match " "))
  8588. (setq text (org-trim (buffer-string)))
  8589. (set-window-configuration cw)
  8590. (kill-buffer cb)
  8591. (select-window (get-buffer-window (marker-buffer pos)))
  8592. (goto-char pos)
  8593. (move-marker pos nil)
  8594. (org-table-check-inside-data-field)
  8595. (org-table-get-field nil text)
  8596. (org-table-align)
  8597. (message "New field value inserted")))
  8598. (defun org-trim (s)
  8599. "Remove whitespace at beginning and end of string."
  8600. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8601. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8602. s)
  8603. (defun org-wrap (string &optional width lines)
  8604. "Wrap string to either a number of lines, or a width in characters.
  8605. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8606. that costs. If there is a word longer than WIDTH, the text is actually
  8607. wrapped to the length of that word.
  8608. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8609. many lines, whatever width that takes.
  8610. The return value is a list of lines, without newlines at the end."
  8611. (let* ((words (org-split-string string "[ \t\n]+"))
  8612. (maxword (apply 'max (mapcar 'org-string-width words)))
  8613. w ll)
  8614. (cond (width
  8615. (org-do-wrap words (max maxword width)))
  8616. (lines
  8617. (setq w maxword)
  8618. (setq ll (org-do-wrap words maxword))
  8619. (if (<= (length ll) lines)
  8620. ll
  8621. (setq ll words)
  8622. (while (> (length ll) lines)
  8623. (setq w (1+ w))
  8624. (setq ll (org-do-wrap words w)))
  8625. ll))
  8626. (t (error "Cannot wrap this")))))
  8627. (defun org-do-wrap (words width)
  8628. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8629. (let (lines line)
  8630. (while words
  8631. (setq line (pop words))
  8632. (while (and words (< (+ (length line) (length (car words))) width))
  8633. (setq line (concat line " " (pop words))))
  8634. (setq lines (push line lines)))
  8635. (nreverse lines)))
  8636. (defun org-split-string (string &optional separators)
  8637. "Splits STRING into substrings at SEPARATORS.
  8638. No empty strings are returned if there are matches at the beginning
  8639. and end of string."
  8640. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8641. (start 0)
  8642. notfirst
  8643. (list nil))
  8644. (while (and (string-match rexp string
  8645. (if (and notfirst
  8646. (= start (match-beginning 0))
  8647. (< start (length string)))
  8648. (1+ start) start))
  8649. (< (match-beginning 0) (length string)))
  8650. (setq notfirst t)
  8651. (or (eq (match-beginning 0) 0)
  8652. (and (eq (match-beginning 0) (match-end 0))
  8653. (eq (match-beginning 0) start))
  8654. (setq list
  8655. (cons (substring string start (match-beginning 0))
  8656. list)))
  8657. (setq start (match-end 0)))
  8658. (or (eq start (length string))
  8659. (setq list
  8660. (cons (substring string start)
  8661. list)))
  8662. (nreverse list)))
  8663. (defun org-table-map-tables (function)
  8664. "Apply FUNCTION to the start of all tables in the buffer."
  8665. (save-excursion
  8666. (save-restriction
  8667. (widen)
  8668. (goto-char (point-min))
  8669. (while (re-search-forward org-table-any-line-regexp nil t)
  8670. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8671. (beginning-of-line 1)
  8672. (if (looking-at org-table-line-regexp)
  8673. (save-excursion (funcall function)))
  8674. (re-search-forward org-table-any-border-regexp nil 1))))
  8675. (message "Mapping tables: done"))
  8676. (defvar org-timecnt) ; dynamically scoped parameter
  8677. (defun org-table-sum (&optional beg end nlast)
  8678. "Sum numbers in region of current table column.
  8679. The result will be displayed in the echo area, and will be available
  8680. as kill to be inserted with \\[yank].
  8681. If there is an active region, it is interpreted as a rectangle and all
  8682. numbers in that rectangle will be summed. If there is no active
  8683. region and point is located in a table column, sum all numbers in that
  8684. column.
  8685. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8686. numbers are assumed to be times as well (in decimal hours) and the
  8687. numbers are added as such.
  8688. If NLAST is a number, only the NLAST fields will actually be summed."
  8689. (interactive)
  8690. (save-excursion
  8691. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8692. (cond
  8693. ((and beg end)) ; beg and end given explicitly
  8694. ((org-region-active-p)
  8695. (setq beg (region-beginning) end (region-end)))
  8696. (t
  8697. (setq col (org-table-current-column))
  8698. (goto-char (org-table-begin))
  8699. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8700. (error "No table data"))
  8701. (org-table-goto-column col)
  8702. (setq beg (point))
  8703. (goto-char (org-table-end))
  8704. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8705. (error "No table data"))
  8706. (org-table-goto-column col)
  8707. (setq end (point))))
  8708. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8709. (items1 (cond ((not nlast) items)
  8710. ((>= nlast (length items)) items)
  8711. (t (setq items (reverse items))
  8712. (setcdr (nthcdr (1- nlast) items) nil)
  8713. (nreverse items))))
  8714. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8715. items1)))
  8716. (res (apply '+ numbers))
  8717. (sres (if (= org-timecnt 0)
  8718. (format "%g" res)
  8719. (setq diff (* 3600 res)
  8720. h (floor (/ diff 3600)) diff (mod diff 3600)
  8721. m (floor (/ diff 60)) diff (mod diff 60)
  8722. s diff)
  8723. (format "%d:%02d:%02d" h m s))))
  8724. (kill-new sres)
  8725. (if (interactive-p)
  8726. (message "%s"
  8727. (substitute-command-keys
  8728. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8729. (length numbers) sres))))
  8730. sres))))
  8731. (defun org-table-get-number-for-summing (s)
  8732. (let (n)
  8733. (if (string-match "^ *|? *" s)
  8734. (setq s (replace-match "" nil nil s)))
  8735. (if (string-match " *|? *$" s)
  8736. (setq s (replace-match "" nil nil s)))
  8737. (setq n (string-to-number s))
  8738. (cond
  8739. ((and (string-match "0" s)
  8740. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8741. ((string-match "\\`[ \t]+\\'" s) nil)
  8742. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8743. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8744. (m (string-to-number (or (match-string 2 s) "0")))
  8745. (s (string-to-number (or (match-string 4 s) "0"))))
  8746. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8747. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8748. ((equal n 0) nil)
  8749. (t n))))
  8750. (defun org-table-current-field-formula (&optional key noerror)
  8751. "Return the formula active for the current field.
  8752. Assumes that specials are in place.
  8753. If KEY is given, return the key to this formula.
  8754. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8755. (let* ((name (car (rassoc (list (org-current-line)
  8756. (org-table-current-column))
  8757. org-table-named-field-locations)))
  8758. (col (org-table-current-column))
  8759. (scol (int-to-string col))
  8760. (ref (format "@%d$%d" (org-table-current-dline) col))
  8761. (stored-list (org-table-get-stored-formulas noerror))
  8762. (ass (or (assoc name stored-list)
  8763. (assoc ref stored-list)
  8764. (assoc scol stored-list))))
  8765. (if key
  8766. (car ass)
  8767. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8768. (cdr ass))))))
  8769. (defun org-table-get-formula (&optional equation named)
  8770. "Read a formula from the minibuffer, offer stored formula as default.
  8771. When NAMED is non-nil, look for a named equation."
  8772. (let* ((stored-list (org-table-get-stored-formulas))
  8773. (name (car (rassoc (list (org-current-line)
  8774. (org-table-current-column))
  8775. org-table-named-field-locations)))
  8776. (ref (format "@%d$%d" (org-table-current-dline)
  8777. (org-table-current-column)))
  8778. (refass (assoc ref stored-list))
  8779. (scol (if named
  8780. (if name name ref)
  8781. (int-to-string (org-table-current-column))))
  8782. (dummy (and (or name refass) (not named)
  8783. (not (y-or-n-p "Replace field formula with column formula? " ))
  8784. (error "Abort")))
  8785. (name (or name ref))
  8786. (org-table-may-need-update nil)
  8787. (stored (cdr (assoc scol stored-list)))
  8788. (eq (cond
  8789. ((and stored equation (string-match "^ *=? *$" equation))
  8790. stored)
  8791. ((stringp equation)
  8792. equation)
  8793. (t (org-table-formula-from-user
  8794. (read-string
  8795. (org-table-formula-to-user
  8796. (format "%s formula %s%s="
  8797. (if named "Field" "Column")
  8798. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8799. scol))
  8800. (if stored (org-table-formula-to-user stored) "")
  8801. 'org-table-formula-history
  8802. )))))
  8803. mustsave)
  8804. (when (not (string-match "\\S-" eq))
  8805. ;; remove formula
  8806. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8807. (org-table-store-formulas stored-list)
  8808. (error "Formula removed"))
  8809. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8810. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8811. (if (and name (not named))
  8812. ;; We set the column equation, delete the named one.
  8813. (setq stored-list (delq (assoc name stored-list) stored-list)
  8814. mustsave t))
  8815. (if stored
  8816. (setcdr (assoc scol stored-list) eq)
  8817. (setq stored-list (cons (cons scol eq) stored-list)))
  8818. (if (or mustsave (not (equal stored eq)))
  8819. (org-table-store-formulas stored-list))
  8820. eq))
  8821. (defun org-table-store-formulas (alist)
  8822. "Store the list of formulas below the current table."
  8823. (setq alist (sort alist 'org-table-formula-less-p))
  8824. (save-excursion
  8825. (goto-char (org-table-end))
  8826. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8827. (progn
  8828. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8829. (goto-char (match-beginning 2))
  8830. (delete-region (match-beginning 2) (match-end 0)))
  8831. (insert "#+TBLFM:"))
  8832. (insert " "
  8833. (mapconcat (lambda (x)
  8834. (concat
  8835. (if (equal (string-to-char (car x)) ?@) "" "$")
  8836. (car x) "=" (cdr x)))
  8837. alist "::")
  8838. "\n")))
  8839. (defsubst org-table-formula-make-cmp-string (a)
  8840. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8841. (concat
  8842. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8843. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8844. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8845. (defun org-table-formula-less-p (a b)
  8846. "Compare two formulas for sorting."
  8847. (let ((as (org-table-formula-make-cmp-string (car a)))
  8848. (bs (org-table-formula-make-cmp-string (car b))))
  8849. (and as bs (string< as bs))))
  8850. (defun org-table-get-stored-formulas (&optional noerror)
  8851. "Return an alist with the stored formulas directly after current table."
  8852. (interactive)
  8853. (let (scol eq eq-alist strings string seen)
  8854. (save-excursion
  8855. (goto-char (org-table-end))
  8856. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8857. (setq strings (org-split-string (match-string 2) " *:: *"))
  8858. (while (setq string (pop strings))
  8859. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8860. (setq scol (if (match-end 2)
  8861. (match-string 2 string)
  8862. (match-string 1 string))
  8863. eq (match-string 3 string)
  8864. eq-alist (cons (cons scol eq) eq-alist))
  8865. (if (member scol seen)
  8866. (if noerror
  8867. (progn
  8868. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8869. (ding)
  8870. (sit-for 2))
  8871. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8872. (push scol seen))))))
  8873. (nreverse eq-alist)))
  8874. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8875. "Modify the equations after the table structure has been edited.
  8876. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8877. For all numbers larger than LIMIT, shift them by DELTA."
  8878. (save-excursion
  8879. (goto-char (org-table-end))
  8880. (when (looking-at "#\\+TBLFM:")
  8881. (let ((re (concat key "\\([0-9]+\\)"))
  8882. (re2
  8883. (when remove
  8884. (if (equal key "$")
  8885. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8886. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8887. s n a)
  8888. (when remove
  8889. (while (re-search-forward re2 (point-at-eol) t)
  8890. (replace-match "")))
  8891. (while (re-search-forward re (point-at-eol) t)
  8892. (setq s (match-string 1) n (string-to-number s))
  8893. (cond
  8894. ((setq a (assoc s replace))
  8895. (replace-match (concat key (cdr a)) t t))
  8896. ((and limit (> n limit))
  8897. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8898. (defun org-table-get-specials ()
  8899. "Get the column names and local parameters for this table."
  8900. (save-excursion
  8901. (let ((beg (org-table-begin)) (end (org-table-end))
  8902. names name fields fields1 field cnt
  8903. c v l line col types dlines hlines)
  8904. (setq org-table-column-names nil
  8905. org-table-local-parameters nil
  8906. org-table-named-field-locations nil
  8907. org-table-current-begin-line nil
  8908. org-table-current-begin-pos nil
  8909. org-table-current-line-types nil)
  8910. (goto-char beg)
  8911. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8912. (setq names (org-split-string (match-string 1) " *| *")
  8913. cnt 1)
  8914. (while (setq name (pop names))
  8915. (setq cnt (1+ cnt))
  8916. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8917. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8918. (setq org-table-column-names (nreverse org-table-column-names))
  8919. (setq org-table-column-name-regexp
  8920. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8921. (goto-char beg)
  8922. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8923. (setq fields (org-split-string (match-string 1) " *| *"))
  8924. (while (setq field (pop fields))
  8925. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8926. (push (cons (match-string 1 field) (match-string 2 field))
  8927. org-table-local-parameters))))
  8928. (goto-char beg)
  8929. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8930. (setq c (match-string 1)
  8931. fields (org-split-string (match-string 2) " *| *"))
  8932. (save-excursion
  8933. (beginning-of-line (if (equal c "_") 2 0))
  8934. (setq line (org-current-line) col 1)
  8935. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8936. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8937. (while (and fields1 (setq field (pop fields)))
  8938. (setq v (pop fields1) col (1+ col))
  8939. (when (and (stringp field) (stringp v)
  8940. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8941. (push (cons field v) org-table-local-parameters)
  8942. (push (list field line col) org-table-named-field-locations))))
  8943. ;; Analyse the line types
  8944. (goto-char beg)
  8945. (setq org-table-current-begin-line (org-current-line)
  8946. org-table-current-begin-pos (point)
  8947. l org-table-current-begin-line)
  8948. (while (looking-at "[ \t]*|\\(-\\)?")
  8949. (push (if (match-end 1) 'hline 'dline) types)
  8950. (if (match-end 1) (push l hlines) (push l dlines))
  8951. (beginning-of-line 2)
  8952. (setq l (1+ l)))
  8953. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8954. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8955. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8956. (defun org-table-maybe-eval-formula ()
  8957. "Check if the current field starts with \"=\" or \":=\".
  8958. If yes, store the formula and apply it."
  8959. ;; We already know we are in a table. Get field will only return a formula
  8960. ;; when appropriate. It might return a separator line, but no problem.
  8961. (when org-table-formula-evaluate-inline
  8962. (let* ((field (org-trim (or (org-table-get-field) "")))
  8963. named eq)
  8964. (when (string-match "^:?=\\(.*\\)" field)
  8965. (setq named (equal (string-to-char field) ?:)
  8966. eq (match-string 1 field))
  8967. (if (or (fboundp 'calc-eval)
  8968. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8969. (org-table-eval-formula (if named '(4) nil)
  8970. (org-table-formula-from-user eq))
  8971. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8972. (defvar org-recalc-commands nil
  8973. "List of commands triggering the recalculation of a line.
  8974. Will be filled automatically during use.")
  8975. (defvar org-recalc-marks
  8976. '((" " . "Unmarked: no special line, no automatic recalculation")
  8977. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8978. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8979. ("!" . "Column name definition line. Reference in formula as $name.")
  8980. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8981. ("_" . "Names for values in row below this one.")
  8982. ("^" . "Names for values in row above this one.")))
  8983. (defun org-table-rotate-recalc-marks (&optional newchar)
  8984. "Rotate the recalculation mark in the first column.
  8985. If in any row, the first field is not consistent with a mark,
  8986. insert a new column for the markers.
  8987. When there is an active region, change all the lines in the region,
  8988. after prompting for the marking character.
  8989. After each change, a message will be displayed indicating the meaning
  8990. of the new mark."
  8991. (interactive)
  8992. (unless (org-at-table-p) (error "Not at a table"))
  8993. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8994. (beg (org-table-begin))
  8995. (end (org-table-end))
  8996. (l (org-current-line))
  8997. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8998. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8999. (have-col
  9000. (save-excursion
  9001. (goto-char beg)
  9002. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9003. (col (org-table-current-column))
  9004. (forcenew (car (assoc newchar org-recalc-marks)))
  9005. epos new)
  9006. (when l1
  9007. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9008. (setq newchar (char-to-string (read-char-exclusive))
  9009. forcenew (car (assoc newchar org-recalc-marks))))
  9010. (if (and newchar (not forcenew))
  9011. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9012. newchar))
  9013. (if l1 (goto-line l1))
  9014. (save-excursion
  9015. (beginning-of-line 1)
  9016. (unless (looking-at org-table-dataline-regexp)
  9017. (error "Not at a table data line")))
  9018. (unless have-col
  9019. (org-table-goto-column 1)
  9020. (org-table-insert-column)
  9021. (org-table-goto-column (1+ col)))
  9022. (setq epos (point-at-eol))
  9023. (save-excursion
  9024. (beginning-of-line 1)
  9025. (org-table-get-field
  9026. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9027. (concat " "
  9028. (setq new (or forcenew
  9029. (cadr (member (match-string 1) marks))))
  9030. " ")
  9031. " # ")))
  9032. (if (and l1 l2)
  9033. (progn
  9034. (goto-line l1)
  9035. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9036. (and (looking-at org-table-dataline-regexp)
  9037. (org-table-get-field 1 (concat " " new " "))))
  9038. (goto-line l1)))
  9039. (if (not (= epos (point-at-eol))) (org-table-align))
  9040. (goto-line l)
  9041. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9042. (defun org-table-maybe-recalculate-line ()
  9043. "Recompute the current line if marked for it, and if we haven't just done it."
  9044. (interactive)
  9045. (and org-table-allow-automatic-line-recalculation
  9046. (not (and (memq last-command org-recalc-commands)
  9047. (equal org-last-recalc-line (org-current-line))))
  9048. (save-excursion (beginning-of-line 1)
  9049. (looking-at org-table-auto-recalculate-regexp))
  9050. (org-table-recalculate) t))
  9051. (defvar org-table-formula-debug nil
  9052. "Non-nil means, debug table formulas.
  9053. When nil, simply write \"#ERROR\" in corrupted fields.")
  9054. (make-variable-buffer-local 'org-table-formula-debug)
  9055. (defvar modes)
  9056. (defsubst org-set-calc-mode (var &optional value)
  9057. (if (stringp var)
  9058. (setq var (assoc var '(("D" calc-angle-mode deg)
  9059. ("R" calc-angle-mode rad)
  9060. ("F" calc-prefer-frac t)
  9061. ("S" calc-symbolic-mode t)))
  9062. value (nth 2 var) var (nth 1 var)))
  9063. (if (memq var modes)
  9064. (setcar (cdr (memq var modes)) value)
  9065. (cons var (cons value modes)))
  9066. modes)
  9067. (defun org-table-eval-formula (&optional arg equation
  9068. suppress-align suppress-const
  9069. suppress-store suppress-analysis)
  9070. "Replace the table field value at the cursor by the result of a calculation.
  9071. This function makes use of Dave Gillespie's Calc package, in my view the
  9072. most exciting program ever written for GNU Emacs. So you need to have Calc
  9073. installed in order to use this function.
  9074. In a table, this command replaces the value in the current field with the
  9075. result of a formula. It also installs the formula as the \"current\" column
  9076. formula, by storing it in a special line below the table. When called
  9077. with a `C-u' prefix, the current field must ba a named field, and the
  9078. formula is installed as valid in only this specific field.
  9079. When called with two `C-u' prefixes, insert the active equation
  9080. for the field back into the current field, so that it can be
  9081. edited there. This is useful in order to use \\[org-table-show-reference]
  9082. to check the referenced fields.
  9083. When called, the command first prompts for a formula, which is read in
  9084. the minibuffer. Previously entered formulas are available through the
  9085. history list, and the last used formula is offered as a default.
  9086. These stored formulas are adapted correctly when moving, inserting, or
  9087. deleting columns with the corresponding commands.
  9088. The formula can be any algebraic expression understood by the Calc package.
  9089. For details, see the Org-mode manual.
  9090. This function can also be called from Lisp programs and offers
  9091. additional arguments: EQUATION can be the formula to apply. If this
  9092. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9093. used to speed-up recursive calls by by-passing unnecessary aligns.
  9094. SUPPRESS-CONST suppresses the interpretation of constants in the
  9095. formula, assuming that this has been done already outside the function.
  9096. SUPPRESS-STORE means the formula should not be stored, either because
  9097. it is already stored, or because it is a modified equation that should
  9098. not overwrite the stored one."
  9099. (interactive "P")
  9100. (org-table-check-inside-data-field)
  9101. (or suppress-analysis (org-table-get-specials))
  9102. (if (equal arg '(16))
  9103. (let ((eq (org-table-current-field-formula)))
  9104. (or eq (error "No equation active for current field"))
  9105. (org-table-get-field nil eq)
  9106. (org-table-align)
  9107. (setq org-table-may-need-update t))
  9108. (let* (fields
  9109. (ndown (if (integerp arg) arg 1))
  9110. (org-table-automatic-realign nil)
  9111. (case-fold-search nil)
  9112. (down (> ndown 1))
  9113. (formula (if (and equation suppress-store)
  9114. equation
  9115. (org-table-get-formula equation (equal arg '(4)))))
  9116. (n0 (org-table-current-column))
  9117. (modes (copy-sequence org-calc-default-modes))
  9118. (numbers nil) ; was a variable, now fixed default
  9119. (keep-empty nil)
  9120. n form form0 bw fmt x ev orig c lispp literal)
  9121. ;; Parse the format string. Since we have a lot of modes, this is
  9122. ;; a lot of work. However, I think calc still uses most of the time.
  9123. (if (string-match ";" formula)
  9124. (let ((tmp (org-split-string formula ";")))
  9125. (setq formula (car tmp)
  9126. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9127. (nth 1 tmp)))
  9128. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9129. (setq c (string-to-char (match-string 1 fmt))
  9130. n (string-to-number (match-string 2 fmt)))
  9131. (if (= c ?p)
  9132. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9133. (setq modes (org-set-calc-mode
  9134. 'calc-float-format
  9135. (list (cdr (assoc c '((?n . float) (?f . fix)
  9136. (?s . sci) (?e . eng))))
  9137. n))))
  9138. (setq fmt (replace-match "" t t fmt)))
  9139. (if (string-match "[NT]" fmt)
  9140. (setq numbers (equal (match-string 0 fmt) "N")
  9141. fmt (replace-match "" t t fmt)))
  9142. (if (string-match "L" fmt)
  9143. (setq literal t
  9144. fmt (replace-match "" t t fmt)))
  9145. (if (string-match "E" fmt)
  9146. (setq keep-empty t
  9147. fmt (replace-match "" t t fmt)))
  9148. (while (string-match "[DRFS]" fmt)
  9149. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9150. (setq fmt (replace-match "" t t fmt)))
  9151. (unless (string-match "\\S-" fmt)
  9152. (setq fmt nil))))
  9153. (if (and (not suppress-const) org-table-formula-use-constants)
  9154. (setq formula (org-table-formula-substitute-names formula)))
  9155. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9156. (while (> ndown 0)
  9157. (setq fields (org-split-string
  9158. (org-no-properties
  9159. (buffer-substring (point-at-bol) (point-at-eol)))
  9160. " *| *"))
  9161. (if (eq numbers t)
  9162. (setq fields (mapcar
  9163. (lambda (x) (number-to-string (string-to-number x)))
  9164. fields)))
  9165. (setq ndown (1- ndown))
  9166. (setq form (copy-sequence formula)
  9167. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9168. (if (and lispp literal) (setq lispp 'literal))
  9169. ;; Check for old vertical references
  9170. (setq form (org-rewrite-old-row-references form))
  9171. ;; Insert complex ranges
  9172. (while (string-match org-table-range-regexp form)
  9173. (setq form
  9174. (replace-match
  9175. (save-match-data
  9176. (org-table-make-reference
  9177. (org-table-get-range (match-string 0 form) nil n0)
  9178. keep-empty numbers lispp))
  9179. t t form)))
  9180. ;; Insert simple ranges
  9181. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9182. (setq form
  9183. (replace-match
  9184. (save-match-data
  9185. (org-table-make-reference
  9186. (org-sublist
  9187. fields (string-to-number (match-string 1 form))
  9188. (string-to-number (match-string 2 form)))
  9189. keep-empty numbers lispp))
  9190. t t form)))
  9191. (setq form0 form)
  9192. ;; Insert the references to fields in same row
  9193. (while (string-match "\\$\\([0-9]+\\)" form)
  9194. (setq n (string-to-number (match-string 1 form))
  9195. x (nth (1- (if (= n 0) n0 n)) fields))
  9196. (unless x (error "Invalid field specifier \"%s\""
  9197. (match-string 0 form)))
  9198. (setq form (replace-match
  9199. (save-match-data
  9200. (org-table-make-reference x nil numbers lispp))
  9201. t t form)))
  9202. (if lispp
  9203. (setq ev (condition-case nil
  9204. (eval (eval (read form)))
  9205. (error "#ERROR"))
  9206. ev (if (numberp ev) (number-to-string ev) ev))
  9207. (or (fboundp 'calc-eval)
  9208. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9209. (setq ev (calc-eval (cons form modes)
  9210. (if numbers 'num))))
  9211. (when org-table-formula-debug
  9212. (with-output-to-temp-buffer "*Substitution History*"
  9213. (princ (format "Substitution history of formula
  9214. Orig: %s
  9215. $xyz-> %s
  9216. @r$c-> %s
  9217. $1-> %s\n" orig formula form0 form))
  9218. (if (listp ev)
  9219. (princ (format " %s^\nError: %s"
  9220. (make-string (car ev) ?\-) (nth 1 ev)))
  9221. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9222. ev (or fmt "NONE")
  9223. (if fmt (format fmt (string-to-number ev)) ev)))))
  9224. (setq bw (get-buffer-window "*Substitution History*"))
  9225. (shrink-window-if-larger-than-buffer bw)
  9226. (unless (and (interactive-p) (not ndown))
  9227. (unless (let (inhibit-redisplay)
  9228. (y-or-n-p "Debugging Formula. Continue to next? "))
  9229. (org-table-align)
  9230. (error "Abort"))
  9231. (delete-window bw)
  9232. (message "")))
  9233. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9234. (org-table-justify-field-maybe
  9235. (if fmt (format fmt (string-to-number ev)) ev))
  9236. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9237. (call-interactively 'org-return)
  9238. (setq ndown 0)))
  9239. (and down (org-table-maybe-recalculate-line))
  9240. (or suppress-align (and org-table-may-need-update
  9241. (org-table-align))))))
  9242. (defun org-table-put-field-property (prop value)
  9243. (save-excursion
  9244. (put-text-property (progn (skip-chars-backward "^|") (point))
  9245. (progn (skip-chars-forward "^|") (point))
  9246. prop value)))
  9247. (defun org-table-get-range (desc &optional tbeg col highlight)
  9248. "Get a calc vector from a column, accorting to descriptor DESC.
  9249. Optional arguments TBEG and COL can give the beginning of the table and
  9250. the current column, to avoid unnecessary parsing.
  9251. HIGHLIGHT means, just highlight the range."
  9252. (if (not (equal (string-to-char desc) ?@))
  9253. (setq desc (concat "@" desc)))
  9254. (save-excursion
  9255. (or tbeg (setq tbeg (org-table-begin)))
  9256. (or col (setq col (org-table-current-column)))
  9257. (let ((thisline (org-current-line))
  9258. beg end c1 c2 r1 r2 rangep tmp)
  9259. (unless (string-match org-table-range-regexp desc)
  9260. (error "Invalid table range specifier `%s'" desc))
  9261. (setq rangep (match-end 3)
  9262. r1 (and (match-end 1) (match-string 1 desc))
  9263. r2 (and (match-end 4) (match-string 4 desc))
  9264. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9265. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9266. (and c1 (setq c1 (+ (string-to-number c1)
  9267. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9268. (and c2 (setq c2 (+ (string-to-number c2)
  9269. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9270. (if (equal r1 "") (setq r1 nil))
  9271. (if (equal r2 "") (setq r2 nil))
  9272. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9273. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9274. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9275. (if (not r1) (setq r1 thisline))
  9276. (if (not r2) (setq r2 thisline))
  9277. (if (not c1) (setq c1 col))
  9278. (if (not c2) (setq c2 col))
  9279. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9280. ;; just one field
  9281. (progn
  9282. (goto-line r1)
  9283. (while (not (looking-at org-table-dataline-regexp))
  9284. (beginning-of-line 2))
  9285. (prog1 (org-trim (org-table-get-field c1))
  9286. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9287. ;; A range, return a vector
  9288. ;; First sort the numbers to get a regular ractangle
  9289. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9290. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9291. (goto-line r1)
  9292. (while (not (looking-at org-table-dataline-regexp))
  9293. (beginning-of-line 2))
  9294. (org-table-goto-column c1)
  9295. (setq beg (point))
  9296. (goto-line r2)
  9297. (while (not (looking-at org-table-dataline-regexp))
  9298. (beginning-of-line 0))
  9299. (org-table-goto-column c2)
  9300. (setq end (point))
  9301. (if highlight
  9302. (org-table-highlight-rectangle
  9303. beg (progn (skip-chars-forward "^|\n") (point))))
  9304. ;; return string representation of calc vector
  9305. (mapcar 'org-trim
  9306. (apply 'append (org-table-copy-region beg end)))))))
  9307. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9308. "Analyze descriptor DESC and retrieve the corresponding line number.
  9309. The cursor is currently in line CLINE, the table begins in line BLINE,
  9310. and TABLE is a vector with line types."
  9311. (if (string-match "^[0-9]+$" desc)
  9312. (aref org-table-dlines (string-to-number desc))
  9313. (setq cline (or cline (org-current-line))
  9314. bline (or bline org-table-current-begin-line)
  9315. table (or table org-table-current-line-types))
  9316. (if (or
  9317. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9318. ;; 1 2 3 4 5 6
  9319. (and (not (match-end 3)) (not (match-end 6)))
  9320. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9321. (error "invalid row descriptor `%s'" desc))
  9322. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9323. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9324. (odir (and (match-end 5) (match-string 5 desc)))
  9325. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9326. (i (- cline bline))
  9327. (rel (and (match-end 6)
  9328. (or (and (match-end 1) (not (match-end 3)))
  9329. (match-end 5)))))
  9330. (if (and hn (not hdir))
  9331. (progn
  9332. (setq i 0 hdir "+")
  9333. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9334. (if (and (not hn) on (not odir))
  9335. (error "should never happen");;(aref org-table-dlines on)
  9336. (if (and hn (> hn 0))
  9337. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9338. (if on
  9339. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9340. (+ bline i)))))
  9341. (defun org-find-row-type (table i type backwards relative n)
  9342. (let ((l (length table)))
  9343. (while (> n 0)
  9344. (while (and (setq i (+ i (if backwards -1 1)))
  9345. (>= i 0) (< i l)
  9346. (not (eq (aref table i) type))
  9347. (if (and relative (eq (aref table i) 'hline))
  9348. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9349. t)))
  9350. (setq n (1- n)))
  9351. (if (or (< i 0) (>= i l))
  9352. (error "Row descriptior leads outside table")
  9353. i)))
  9354. (defun org-rewrite-old-row-references (s)
  9355. (if (string-match "&[-+0-9I]" s)
  9356. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9357. s))
  9358. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9359. "Convert list ELEMENTS to something appropriate to insert into formula.
  9360. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9361. NUMBERS indicates that everything should be converted to numbers.
  9362. LISPP means to return something appropriate for a Lisp list."
  9363. (if (stringp elements) ; just a single val
  9364. (if lispp
  9365. (if (eq lispp 'literal)
  9366. elements
  9367. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9368. (if (equal elements "") (setq elements "0"))
  9369. (if numbers (number-to-string (string-to-number elements)) elements))
  9370. (unless keep-empty
  9371. (setq elements
  9372. (delq nil
  9373. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9374. elements))))
  9375. (setq elements (or elements '("0")))
  9376. (if lispp
  9377. (mapconcat
  9378. (lambda (x)
  9379. (if (eq lispp 'literal)
  9380. x
  9381. (prin1-to-string (if numbers (string-to-number x) x))))
  9382. elements " ")
  9383. (concat "[" (mapconcat
  9384. (lambda (x)
  9385. (if numbers (number-to-string (string-to-number x)) x))
  9386. elements
  9387. ",") "]"))))
  9388. (defun org-table-recalculate (&optional all noalign)
  9389. "Recalculate the current table line by applying all stored formulas.
  9390. With prefix arg ALL, do this for all lines in the table."
  9391. (interactive "P")
  9392. (or (memq this-command org-recalc-commands)
  9393. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9394. (unless (org-at-table-p) (error "Not at a table"))
  9395. (if (equal all '(16))
  9396. (org-table-iterate)
  9397. (org-table-get-specials)
  9398. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9399. (lambda (a b) (string< (car a) (car b)))))
  9400. (inhibit-redisplay (not debug-on-error))
  9401. (line-re org-table-dataline-regexp)
  9402. (thisline (org-current-line))
  9403. (thiscol (org-table-current-column))
  9404. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9405. ;; Insert constants in all formulas
  9406. (setq eqlist
  9407. (mapcar (lambda (x)
  9408. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9409. x)
  9410. eqlist))
  9411. ;; Split the equation list
  9412. (while (setq eq (pop eqlist))
  9413. (if (<= (string-to-char (car eq)) ?9)
  9414. (push eq eqlnum)
  9415. (push eq eqlname)))
  9416. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9417. (if all
  9418. (progn
  9419. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9420. (goto-char (setq beg (org-table-begin)))
  9421. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9422. ;; This is a table with marked lines, compute selected lines
  9423. (setq line-re org-table-recalculate-regexp)
  9424. ;; Move forward to the first non-header line
  9425. (if (and (re-search-forward org-table-dataline-regexp end t)
  9426. (re-search-forward org-table-hline-regexp end t)
  9427. (re-search-forward org-table-dataline-regexp end t))
  9428. (setq beg (match-beginning 0))
  9429. nil))) ;; just leave beg where it is
  9430. (setq beg (point-at-bol)
  9431. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9432. (goto-char beg)
  9433. (and all (message "Re-applying formulas to full table..."))
  9434. ;; First find the named fields, and mark them untouchanble
  9435. (remove-text-properties beg end '(org-untouchable t))
  9436. (while (setq eq (pop eqlname))
  9437. (setq name (car eq)
  9438. a (assoc name org-table-named-field-locations))
  9439. (and (not a)
  9440. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9441. (setq a (list name
  9442. (aref org-table-dlines
  9443. (string-to-number (match-string 1 name)))
  9444. (string-to-number (match-string 2 name)))))
  9445. (when (and a (or all (equal (nth 1 a) thisline)))
  9446. (message "Re-applying formula to field: %s" name)
  9447. (goto-line (nth 1 a))
  9448. (org-table-goto-column (nth 2 a))
  9449. (push (append a (list (cdr eq))) eqlname1)
  9450. (org-table-put-field-property :org-untouchable t)))
  9451. ;; Now evauluate the column formulas, but skip fields covered by
  9452. ;; field formulas
  9453. (goto-char beg)
  9454. (while (re-search-forward line-re end t)
  9455. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9456. ;; Unprotected line, recalculate
  9457. (and all (message "Re-applying formulas to full table...(line %d)"
  9458. (setq cnt (1+ cnt))))
  9459. (setq org-last-recalc-line (org-current-line))
  9460. (setq eql eqlnum)
  9461. (while (setq entry (pop eql))
  9462. (goto-line org-last-recalc-line)
  9463. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9464. (unless (get-text-property (point) :org-untouchable)
  9465. (org-table-eval-formula nil (cdr entry)
  9466. 'noalign 'nocst 'nostore 'noanalysis)))))
  9467. ;; Now evaluate the field formulas
  9468. (while (setq eq (pop eqlname1))
  9469. (message "Re-applying formula to field: %s" (car eq))
  9470. (goto-line (nth 1 eq))
  9471. (org-table-goto-column (nth 2 eq))
  9472. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9473. 'nostore 'noanalysis))
  9474. (goto-line thisline)
  9475. (org-table-goto-column thiscol)
  9476. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9477. (or noalign (and org-table-may-need-update (org-table-align))
  9478. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9479. ;; back to initial position
  9480. (message "Re-applying formulas...done")
  9481. (goto-line thisline)
  9482. (org-table-goto-column thiscol)
  9483. (or noalign (and org-table-may-need-update (org-table-align))
  9484. (and all (message "Re-applying formulas...done"))))))
  9485. (defun org-table-iterate (&optional arg)
  9486. "Recalculate the table until it does not change anymore."
  9487. (interactive "P")
  9488. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9489. (i 0)
  9490. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9491. thistbl)
  9492. (catch 'exit
  9493. (while (< i imax)
  9494. (setq i (1+ i))
  9495. (org-table-recalculate 'all)
  9496. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9497. (if (not (string= lasttbl thistbl))
  9498. (setq lasttbl thistbl)
  9499. (if (> i 1)
  9500. (message "Convergence after %d iterations" i)
  9501. (message "Table was already stable"))
  9502. (throw 'exit t)))
  9503. (error "No convergence after %d iterations" i))))
  9504. (defun org-table-formula-substitute-names (f)
  9505. "Replace $const with values in string F."
  9506. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9507. ;; First, check for column names
  9508. (while (setq start (string-match org-table-column-name-regexp f start))
  9509. (setq start (1+ start))
  9510. (setq a (assoc (match-string 1 f) org-table-column-names))
  9511. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9512. ;; Parameters and constants
  9513. (setq start 0)
  9514. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9515. (setq start (1+ start))
  9516. (if (setq a (save-match-data
  9517. (org-table-get-constant (match-string 1 f))))
  9518. (setq f (replace-match
  9519. (concat (if pp "(") a (if pp ")")) t t f))))
  9520. (if org-table-formula-debug
  9521. (put-text-property 0 (length f) :orig-formula f1 f))
  9522. f))
  9523. (defun org-table-get-constant (const)
  9524. "Find the value for a parameter or constant in a formula.
  9525. Parameters get priority."
  9526. (or (cdr (assoc const org-table-local-parameters))
  9527. (cdr (assoc const org-table-formula-constants-local))
  9528. (cdr (assoc const org-table-formula-constants))
  9529. (and (fboundp 'constants-get) (constants-get const))
  9530. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9531. (org-entry-get nil (substring const 5) 'inherit))
  9532. "#UNDEFINED_NAME"))
  9533. (defvar org-table-fedit-map
  9534. (let ((map (make-sparse-keymap)))
  9535. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9536. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9537. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9538. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9539. (org-defkey map "\C-c?" 'org-table-show-reference)
  9540. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9541. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9542. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9543. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9544. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9545. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9546. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9547. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9548. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9549. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9550. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9551. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9552. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9553. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9554. map))
  9555. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9556. '("Edit-Formulas"
  9557. ["Finish and Install" org-table-fedit-finish t]
  9558. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9559. ["Abort" org-table-fedit-abort t]
  9560. "--"
  9561. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9562. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9563. "--"
  9564. "Shift Reference at Point"
  9565. ["Up" org-table-fedit-ref-up t]
  9566. ["Down" org-table-fedit-ref-down t]
  9567. ["Left" org-table-fedit-ref-left t]
  9568. ["Right" org-table-fedit-ref-right t]
  9569. "-"
  9570. "Change Test Row for Column Formulas"
  9571. ["Up" org-table-fedit-line-up t]
  9572. ["Down" org-table-fedit-line-down t]
  9573. "--"
  9574. ["Scroll Table Window" org-table-fedit-scroll t]
  9575. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9576. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9577. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9578. org-table-overlay-coordinates)]
  9579. "--"
  9580. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9581. :style toggle :selected org-table-buffer-is-an]))
  9582. (defvar org-pos)
  9583. (defun org-table-edit-formulas ()
  9584. "Edit the formulas of the current table in a separate buffer."
  9585. (interactive)
  9586. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9587. (beginning-of-line 0))
  9588. (unless (org-at-table-p) (error "Not at a table"))
  9589. (org-table-get-specials)
  9590. (let ((key (org-table-current-field-formula 'key 'noerror))
  9591. (eql (sort (org-table-get-stored-formulas 'noerror)
  9592. 'org-table-formula-less-p))
  9593. (pos (move-marker (make-marker) (point)))
  9594. (startline 1)
  9595. (wc (current-window-configuration))
  9596. (titles '((column . "# Column Formulas\n")
  9597. (field . "# Field Formulas\n")
  9598. (named . "# Named Field Formulas\n")))
  9599. entry s type title)
  9600. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9601. (erase-buffer)
  9602. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9603. (let ((font-lock-global-modes '(not fundamental-mode)))
  9604. (fundamental-mode))
  9605. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9606. (org-set-local 'org-pos pos)
  9607. (org-set-local 'org-window-configuration wc)
  9608. (use-local-map org-table-fedit-map)
  9609. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9610. (easy-menu-add org-table-fedit-menu)
  9611. (setq startline (org-current-line))
  9612. (while (setq entry (pop eql))
  9613. (setq type (cond
  9614. ((equal (string-to-char (car entry)) ?@) 'field)
  9615. ((string-match "^[0-9]" (car entry)) 'column)
  9616. (t 'named)))
  9617. (when (setq title (assq type titles))
  9618. (or (bobp) (insert "\n"))
  9619. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9620. (setq titles (delq title titles)))
  9621. (if (equal key (car entry)) (setq startline (org-current-line)))
  9622. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9623. (car entry) " = " (cdr entry) "\n"))
  9624. (remove-text-properties 0 (length s) '(face nil) s)
  9625. (insert s))
  9626. (if (eq org-table-use-standard-references t)
  9627. (org-table-fedit-toggle-ref-type))
  9628. (goto-line startline)
  9629. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9630. (defun org-table-fedit-post-command ()
  9631. (when (not (memq this-command '(lisp-complete-symbol)))
  9632. (let ((win (selected-window)))
  9633. (save-excursion
  9634. (condition-case nil
  9635. (org-table-show-reference)
  9636. (error nil))
  9637. (select-window win)))))
  9638. (defun org-table-formula-to-user (s)
  9639. "Convert a formula from internal to user representation."
  9640. (if (eq org-table-use-standard-references t)
  9641. (org-table-convert-refs-to-an s)
  9642. s))
  9643. (defun org-table-formula-from-user (s)
  9644. "Convert a formula from user to internal representation."
  9645. (if org-table-use-standard-references
  9646. (org-table-convert-refs-to-rc s)
  9647. s))
  9648. (defun org-table-convert-refs-to-rc (s)
  9649. "Convert spreadsheet references from AB7 to @7$28.
  9650. Works for single references, but also for entire formulas and even the
  9651. full TBLFM line."
  9652. (let ((start 0))
  9653. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9654. (cond
  9655. ((match-end 3)
  9656. ;; format match, just advance
  9657. (setq start (match-end 0)))
  9658. ((and (> (match-beginning 0) 0)
  9659. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9660. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9661. ;; 3.e5 or something like this.
  9662. (setq start (match-end 0)))
  9663. (t
  9664. (setq start (match-beginning 0)
  9665. s (replace-match
  9666. (if (equal (match-string 2 s) "&")
  9667. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9668. (format "@%d$%d"
  9669. (string-to-number (match-string 2 s))
  9670. (org-letters-to-number (match-string 1 s))))
  9671. t t s)))))
  9672. s))
  9673. (defun org-table-convert-refs-to-an (s)
  9674. "Convert spreadsheet references from to @7$28 to AB7.
  9675. Works for single references, but also for entire formulas and even the
  9676. full TBLFM line."
  9677. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9678. (setq s (replace-match
  9679. (format "%s%d"
  9680. (org-number-to-letters
  9681. (string-to-number (match-string 2 s)))
  9682. (string-to-number (match-string 1 s)))
  9683. t t s)))
  9684. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9685. (setq s (replace-match (concat "\\1"
  9686. (org-number-to-letters
  9687. (string-to-number (match-string 2 s))) "&")
  9688. t nil s)))
  9689. s)
  9690. (defun org-letters-to-number (s)
  9691. "Convert a base 26 number represented by letters into an integer.
  9692. For example: AB -> 28."
  9693. (let ((n 0))
  9694. (setq s (upcase s))
  9695. (while (> (length s) 0)
  9696. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9697. s (substring s 1)))
  9698. n))
  9699. (defun org-number-to-letters (n)
  9700. "Convert an integer into a base 26 number represented by letters.
  9701. For example: 28 -> AB."
  9702. (let ((s ""))
  9703. (while (> n 0)
  9704. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9705. n (/ (1- n) 26)))
  9706. s))
  9707. (defun org-table-fedit-convert-buffer (function)
  9708. "Convert all references in this buffer, using FUNTION."
  9709. (let ((line (org-current-line)))
  9710. (goto-char (point-min))
  9711. (while (not (eobp))
  9712. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9713. (delete-region (point) (point-at-eol))
  9714. (or (eobp) (forward-char 1)))
  9715. (goto-line line)))
  9716. (defun org-table-fedit-toggle-ref-type ()
  9717. "Convert all references in the buffer from B3 to @3$2 and back."
  9718. (interactive)
  9719. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9720. (org-table-fedit-convert-buffer
  9721. (if org-table-buffer-is-an
  9722. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9723. (message "Reference type switched to %s"
  9724. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9725. (defun org-table-fedit-ref-up ()
  9726. "Shift the reference at point one row/hline up."
  9727. (interactive)
  9728. (org-table-fedit-shift-reference 'up))
  9729. (defun org-table-fedit-ref-down ()
  9730. "Shift the reference at point one row/hline down."
  9731. (interactive)
  9732. (org-table-fedit-shift-reference 'down))
  9733. (defun org-table-fedit-ref-left ()
  9734. "Shift the reference at point one field to the left."
  9735. (interactive)
  9736. (org-table-fedit-shift-reference 'left))
  9737. (defun org-table-fedit-ref-right ()
  9738. "Shift the reference at point one field to the right."
  9739. (interactive)
  9740. (org-table-fedit-shift-reference 'right))
  9741. (defun org-table-fedit-shift-reference (dir)
  9742. (cond
  9743. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9744. (if (memq dir '(left right))
  9745. (org-rematch-and-replace 1 (eq dir 'left))
  9746. (error "Cannot shift reference in this direction")))
  9747. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9748. ;; A B3-like reference
  9749. (if (memq dir '(up down))
  9750. (org-rematch-and-replace 2 (eq dir 'up))
  9751. (org-rematch-and-replace 1 (eq dir 'left))))
  9752. ((org-at-regexp-p
  9753. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9754. ;; An internal reference
  9755. (if (memq dir '(up down))
  9756. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9757. (org-rematch-and-replace 5 (eq dir 'left))))))
  9758. (defun org-rematch-and-replace (n &optional decr hline)
  9759. "Re-match the group N, and replace it with the shifted refrence."
  9760. (or (match-end n) (error "Cannot shift reference in this direction"))
  9761. (goto-char (match-beginning n))
  9762. (and (looking-at (regexp-quote (match-string n)))
  9763. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9764. t t)))
  9765. (defun org-shift-refpart (ref &optional decr hline)
  9766. "Shift a refrence part REF.
  9767. If DECR is set, decrease the references row/column, else increase.
  9768. If HLINE is set, this may be a hline reference, it certainly is not
  9769. a translation reference."
  9770. (save-match-data
  9771. (let* ((sign (string-match "^[-+]" ref)) n)
  9772. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9773. (cond
  9774. ((and hline (string-match "^I+" ref))
  9775. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9776. (setq n (+ n (if decr -1 1)))
  9777. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9778. (if sign
  9779. (setq sign (if (< n 0) "-" "+") n (abs n))
  9780. (setq n (max 1 n)))
  9781. (concat sign (make-string n ?I)))
  9782. ((string-match "^[0-9]+" ref)
  9783. (setq n (string-to-number (concat sign ref)))
  9784. (setq n (+ n (if decr -1 1)))
  9785. (if sign
  9786. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9787. (number-to-string (max 1 n))))
  9788. ((string-match "^[a-zA-Z]+" ref)
  9789. (org-number-to-letters
  9790. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9791. (t (error "Cannot shift reference"))))))
  9792. (defun org-table-fedit-toggle-coordinates ()
  9793. "Toggle the display of coordinates in the refrenced table."
  9794. (interactive)
  9795. (let ((pos (marker-position org-pos)))
  9796. (with-current-buffer (marker-buffer org-pos)
  9797. (save-excursion
  9798. (goto-char pos)
  9799. (org-table-toggle-coordinate-overlays)))))
  9800. (defun org-table-fedit-finish (&optional arg)
  9801. "Parse the buffer for formula definitions and install them.
  9802. With prefix ARG, apply the new formulas to the table."
  9803. (interactive "P")
  9804. (org-table-remove-rectangle-highlight)
  9805. (if org-table-use-standard-references
  9806. (progn
  9807. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9808. (setq org-table-buffer-is-an nil)))
  9809. (let ((pos org-pos) eql var form)
  9810. (goto-char (point-min))
  9811. (while (re-search-forward
  9812. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9813. nil t)
  9814. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9815. form (match-string 3))
  9816. (setq form (org-trim form))
  9817. (when (not (equal form ""))
  9818. (while (string-match "[ \t]*\n[ \t]*" form)
  9819. (setq form (replace-match " " t t form)))
  9820. (when (assoc var eql)
  9821. (error "Double formulas for %s" var))
  9822. (push (cons var form) eql)))
  9823. (setq org-pos nil)
  9824. (set-window-configuration org-window-configuration)
  9825. (select-window (get-buffer-window (marker-buffer pos)))
  9826. (goto-char pos)
  9827. (unless (org-at-table-p)
  9828. (error "Lost table position - cannot install formulae"))
  9829. (org-table-store-formulas eql)
  9830. (move-marker pos nil)
  9831. (kill-buffer "*Edit Formulas*")
  9832. (if arg
  9833. (org-table-recalculate 'all)
  9834. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9835. (defun org-table-fedit-abort ()
  9836. "Abort editing formulas, without installing the changes."
  9837. (interactive)
  9838. (org-table-remove-rectangle-highlight)
  9839. (let ((pos org-pos))
  9840. (set-window-configuration org-window-configuration)
  9841. (select-window (get-buffer-window (marker-buffer pos)))
  9842. (goto-char pos)
  9843. (move-marker pos nil)
  9844. (message "Formula editing aborted without installing changes")))
  9845. (defun org-table-fedit-lisp-indent ()
  9846. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9847. (interactive)
  9848. (let ((pos (point)) beg end ind)
  9849. (beginning-of-line 1)
  9850. (cond
  9851. ((looking-at "[ \t]")
  9852. (goto-char pos)
  9853. (call-interactively 'lisp-indent-line))
  9854. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9855. ((not (fboundp 'pp-buffer))
  9856. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9857. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9858. (goto-char (- (match-end 0) 2))
  9859. (setq beg (point))
  9860. (setq ind (make-string (current-column) ?\ ))
  9861. (condition-case nil (forward-sexp 1)
  9862. (error
  9863. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9864. (setq end (point))
  9865. (save-restriction
  9866. (narrow-to-region beg end)
  9867. (if (eq last-command this-command)
  9868. (progn
  9869. (goto-char (point-min))
  9870. (setq this-command nil)
  9871. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9872. (replace-match " ")))
  9873. (pp-buffer)
  9874. (untabify (point-min) (point-max))
  9875. (goto-char (1+ (point-min)))
  9876. (while (re-search-forward "^." nil t)
  9877. (beginning-of-line 1)
  9878. (insert ind))
  9879. (goto-char (point-max))
  9880. (backward-delete-char 1)))
  9881. (goto-char beg))
  9882. (t nil))))
  9883. (defvar org-show-positions nil)
  9884. (defun org-table-show-reference (&optional local)
  9885. "Show the location/value of the $ expression at point."
  9886. (interactive)
  9887. (org-table-remove-rectangle-highlight)
  9888. (catch 'exit
  9889. (let ((pos (if local (point) org-pos))
  9890. (face2 'highlight)
  9891. (org-inhibit-highlight-removal t)
  9892. (win (selected-window))
  9893. (org-show-positions nil)
  9894. var name e what match dest)
  9895. (if local (org-table-get-specials))
  9896. (setq what (cond
  9897. ((or (org-at-regexp-p org-table-range-regexp2)
  9898. (org-at-regexp-p org-table-translate-regexp)
  9899. (org-at-regexp-p org-table-range-regexp))
  9900. (setq match
  9901. (save-match-data
  9902. (org-table-convert-refs-to-rc (match-string 0))))
  9903. 'range)
  9904. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9905. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9906. ((not local) nil)
  9907. (t (error "No reference at point")))
  9908. match (and what (or match (match-string 0))))
  9909. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9910. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9911. 'secondary-selection))
  9912. (org-add-hook 'before-change-functions
  9913. 'org-table-remove-rectangle-highlight)
  9914. (if (eq what 'name) (setq var (substring match 1)))
  9915. (when (eq what 'range)
  9916. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9917. (setq match (org-table-formula-substitute-names match)))
  9918. (unless local
  9919. (save-excursion
  9920. (end-of-line 1)
  9921. (re-search-backward "^\\S-" nil t)
  9922. (beginning-of-line 1)
  9923. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9924. (setq dest
  9925. (save-match-data
  9926. (org-table-convert-refs-to-rc (match-string 1))))
  9927. (org-table-add-rectangle-overlay
  9928. (match-beginning 1) (match-end 1) face2))))
  9929. (if (and (markerp pos) (marker-buffer pos))
  9930. (if (get-buffer-window (marker-buffer pos))
  9931. (select-window (get-buffer-window (marker-buffer pos)))
  9932. (org-switch-to-buffer-other-window (get-buffer-window
  9933. (marker-buffer pos)))))
  9934. (goto-char pos)
  9935. (org-table-force-dataline)
  9936. (when dest
  9937. (setq name (substring dest 1))
  9938. (cond
  9939. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9940. (setq e (assoc name org-table-named-field-locations))
  9941. (goto-line (nth 1 e))
  9942. (org-table-goto-column (nth 2 e)))
  9943. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9944. (let ((l (string-to-number (match-string 1 dest)))
  9945. (c (string-to-number (match-string 2 dest))))
  9946. (goto-line (aref org-table-dlines l))
  9947. (org-table-goto-column c)))
  9948. (t (org-table-goto-column (string-to-number name))))
  9949. (move-marker pos (point))
  9950. (org-table-highlight-rectangle nil nil face2))
  9951. (cond
  9952. ((equal dest match))
  9953. ((not match))
  9954. ((eq what 'range)
  9955. (condition-case nil
  9956. (save-excursion
  9957. (org-table-get-range match nil nil 'highlight))
  9958. (error nil)))
  9959. ((setq e (assoc var org-table-named-field-locations))
  9960. (goto-line (nth 1 e))
  9961. (org-table-goto-column (nth 2 e))
  9962. (org-table-highlight-rectangle (point) (point))
  9963. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9964. ((setq e (assoc var org-table-column-names))
  9965. (org-table-goto-column (string-to-number (cdr e)))
  9966. (org-table-highlight-rectangle (point) (point))
  9967. (goto-char (org-table-begin))
  9968. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9969. (org-table-end) t)
  9970. (progn
  9971. (goto-char (match-beginning 1))
  9972. (org-table-highlight-rectangle)
  9973. (message "Named column (column %s)" (cdr e)))
  9974. (error "Column name not found")))
  9975. ((eq what 'column)
  9976. ;; column number
  9977. (org-table-goto-column (string-to-number (substring match 1)))
  9978. (org-table-highlight-rectangle (point) (point))
  9979. (message "Column %s" (substring match 1)))
  9980. ((setq e (assoc var org-table-local-parameters))
  9981. (goto-char (org-table-begin))
  9982. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9983. (progn
  9984. (goto-char (match-beginning 1))
  9985. (org-table-highlight-rectangle)
  9986. (message "Local parameter."))
  9987. (error "Parameter not found")))
  9988. (t
  9989. (cond
  9990. ((not var) (error "No reference at point"))
  9991. ((setq e (assoc var org-table-formula-constants-local))
  9992. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9993. var (cdr e)))
  9994. ((setq e (assoc var org-table-formula-constants))
  9995. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9996. var (cdr e)))
  9997. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9998. (message "Constant: $%s=%s, from `constants.el'%s."
  9999. var e (format " (%s units)" constants-unit-system)))
  10000. (t (error "Undefined name $%s" var)))))
  10001. (goto-char pos)
  10002. (when (and org-show-positions
  10003. (not (memq this-command '(org-table-fedit-scroll
  10004. org-table-fedit-scroll-down))))
  10005. (push pos org-show-positions)
  10006. (push org-table-current-begin-pos org-show-positions)
  10007. (let ((min (apply 'min org-show-positions))
  10008. (max (apply 'max org-show-positions)))
  10009. (goto-char min) (recenter 0)
  10010. (goto-char max)
  10011. (or (pos-visible-in-window-p max) (recenter -1))))
  10012. (select-window win))))
  10013. (defun org-table-force-dataline ()
  10014. "Make sure the cursor is in a dataline in a table."
  10015. (unless (save-excursion
  10016. (beginning-of-line 1)
  10017. (looking-at org-table-dataline-regexp))
  10018. (let* ((re org-table-dataline-regexp)
  10019. (p1 (save-excursion (re-search-forward re nil 'move)))
  10020. (p2 (save-excursion (re-search-backward re nil 'move))))
  10021. (cond ((and p1 p2)
  10022. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10023. p1 p2)))
  10024. ((or p1 p2) (goto-char (or p1 p2)))
  10025. (t (error "No table dataline around here"))))))
  10026. (defun org-table-fedit-line-up ()
  10027. "Move cursor one line up in the window showing the table."
  10028. (interactive)
  10029. (org-table-fedit-move 'previous-line))
  10030. (defun org-table-fedit-line-down ()
  10031. "Move cursor one line down in the window showing the table."
  10032. (interactive)
  10033. (org-table-fedit-move 'next-line))
  10034. (defun org-table-fedit-move (command)
  10035. "Move the cursor in the window shoinw the table.
  10036. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10037. (let ((org-table-allow-automatic-line-recalculation nil)
  10038. (pos org-pos) (win (selected-window)) p)
  10039. (select-window (get-buffer-window (marker-buffer org-pos)))
  10040. (setq p (point))
  10041. (call-interactively command)
  10042. (while (and (org-at-table-p)
  10043. (org-at-table-hline-p))
  10044. (call-interactively command))
  10045. (or (org-at-table-p) (goto-char p))
  10046. (move-marker pos (point))
  10047. (select-window win)))
  10048. (defun org-table-fedit-scroll (N)
  10049. (interactive "p")
  10050. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10051. (scroll-other-window N)))
  10052. (defun org-table-fedit-scroll-down (N)
  10053. (interactive "p")
  10054. (org-table-fedit-scroll (- N)))
  10055. (defvar org-table-rectangle-overlays nil)
  10056. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10057. "Add a new overlay."
  10058. (let ((ov (org-make-overlay beg end)))
  10059. (org-overlay-put ov 'face (or face 'secondary-selection))
  10060. (push ov org-table-rectangle-overlays)))
  10061. (defun org-table-highlight-rectangle (&optional beg end face)
  10062. "Highlight rectangular region in a table."
  10063. (setq beg (or beg (point)) end (or end (point)))
  10064. (let ((b (min beg end))
  10065. (e (max beg end))
  10066. l1 c1 l2 c2 tmp)
  10067. (and (boundp 'org-show-positions)
  10068. (setq org-show-positions (cons b (cons e org-show-positions))))
  10069. (goto-char (min beg end))
  10070. (setq l1 (org-current-line)
  10071. c1 (org-table-current-column))
  10072. (goto-char (max beg end))
  10073. (setq l2 (org-current-line)
  10074. c2 (org-table-current-column))
  10075. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10076. (goto-line l1)
  10077. (beginning-of-line 1)
  10078. (loop for line from l1 to l2 do
  10079. (when (looking-at org-table-dataline-regexp)
  10080. (org-table-goto-column c1)
  10081. (skip-chars-backward "^|\n") (setq beg (point))
  10082. (org-table-goto-column c2)
  10083. (skip-chars-forward "^|\n") (setq end (point))
  10084. (org-table-add-rectangle-overlay beg end face))
  10085. (beginning-of-line 2))
  10086. (goto-char b))
  10087. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10088. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10089. "Remove the rectangle overlays."
  10090. (unless org-inhibit-highlight-removal
  10091. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10092. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10093. (setq org-table-rectangle-overlays nil)))
  10094. (defvar org-table-coordinate-overlays nil
  10095. "Collects the cooordinate grid overlays, so that they can be removed.")
  10096. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10097. (defun org-table-overlay-coordinates ()
  10098. "Add overlays to the table at point, to show row/column coordinates."
  10099. (interactive)
  10100. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10101. (setq org-table-coordinate-overlays nil)
  10102. (save-excursion
  10103. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10104. (goto-char (org-table-begin))
  10105. (while (org-at-table-p)
  10106. (setq eol (point-at-eol))
  10107. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10108. (push ov org-table-coordinate-overlays)
  10109. (setq hline (looking-at org-table-hline-regexp))
  10110. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10111. (format "%4d" (setq id (1+ id)))))
  10112. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10113. (when hline
  10114. (setq ic 0)
  10115. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10116. (setq beg (1+ (match-beginning 0))
  10117. ic (1+ ic)
  10118. s1 (concat "$" (int-to-string ic))
  10119. s2 (org-number-to-letters ic)
  10120. str (if (eq org-table-use-standard-references t) s2 s1))
  10121. (setq ov (org-make-overlay beg (+ beg (length str))))
  10122. (push ov org-table-coordinate-overlays)
  10123. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10124. (beginning-of-line 2)))))
  10125. (defun org-table-toggle-coordinate-overlays ()
  10126. "Toggle the display of Row/Column numbers in tables."
  10127. (interactive)
  10128. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10129. (message "Row/Column number display turned %s"
  10130. (if org-table-overlay-coordinates "on" "off"))
  10131. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10132. (org-table-align))
  10133. (unless org-table-overlay-coordinates
  10134. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10135. (setq org-table-coordinate-overlays nil)))
  10136. (defun org-table-toggle-formula-debugger ()
  10137. "Toggle the formula debugger in tables."
  10138. (interactive)
  10139. (setq org-table-formula-debug (not org-table-formula-debug))
  10140. (message "Formula debugging has been turned %s"
  10141. (if org-table-formula-debug "on" "off")))
  10142. ;;; The orgtbl minor mode
  10143. ;; Define a minor mode which can be used in other modes in order to
  10144. ;; integrate the org-mode table editor.
  10145. ;; This is really a hack, because the org-mode table editor uses several
  10146. ;; keys which normally belong to the major mode, for example the TAB and
  10147. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10148. ;; necessary to operate the table editor, but wraps the commands into a
  10149. ;; function which tests if the cursor is currently inside a table. If that
  10150. ;; is the case, the table editor command is executed. However, when any of
  10151. ;; those keys is used outside a table, the function uses `key-binding' to
  10152. ;; look up if the key has an associated command in another currently active
  10153. ;; keymap (minor modes, major mode, global), and executes that command.
  10154. ;; There might be problems if any of the keys used by the table editor is
  10155. ;; otherwise used as a prefix key.
  10156. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10157. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10158. ;; addresses this by checking explicitly for both bindings.
  10159. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10160. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10161. ;; Some modes bind other commands to simple characters, for example
  10162. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10163. ;; active, this binding is ignored inside tables and replaced with a
  10164. ;; modified self-insert.
  10165. (defvar orgtbl-mode nil
  10166. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10167. table editor in arbitrary modes.")
  10168. (make-variable-buffer-local 'orgtbl-mode)
  10169. (defvar orgtbl-mode-map (make-keymap)
  10170. "Keymap for `orgtbl-mode'.")
  10171. ;;;###autoload
  10172. (defun turn-on-orgtbl ()
  10173. "Unconditionally turn on `orgtbl-mode'."
  10174. (orgtbl-mode 1))
  10175. (defvar org-old-auto-fill-inhibit-regexp nil
  10176. "Local variable used by `orgtbl-mode'")
  10177. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10178. "Matches a line belonging to an orgtbl.")
  10179. (defconst orgtbl-extra-font-lock-keywords
  10180. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10181. 0 (quote 'org-table) 'prepend))
  10182. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10183. ;;;###autoload
  10184. (defun orgtbl-mode (&optional arg)
  10185. "The `org-mode' table editor as a minor mode for use in other modes."
  10186. (interactive)
  10187. (if (org-mode-p)
  10188. ;; Exit without error, in case some hook functions calls this
  10189. ;; by accident in org-mode.
  10190. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10191. (setq orgtbl-mode
  10192. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10193. (if orgtbl-mode
  10194. (progn
  10195. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10196. ;; Make sure we are first in minor-mode-map-alist
  10197. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10198. (and c (setq minor-mode-map-alist
  10199. (cons c (delq c minor-mode-map-alist)))))
  10200. (org-set-local (quote org-table-may-need-update) t)
  10201. (org-add-hook 'before-change-functions 'org-before-change-function
  10202. nil 'local)
  10203. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10204. auto-fill-inhibit-regexp)
  10205. (org-set-local 'auto-fill-inhibit-regexp
  10206. (if auto-fill-inhibit-regexp
  10207. (concat orgtbl-line-start-regexp "\\|"
  10208. auto-fill-inhibit-regexp)
  10209. orgtbl-line-start-regexp))
  10210. (org-add-to-invisibility-spec '(org-cwidth))
  10211. (when (fboundp 'font-lock-add-keywords)
  10212. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10213. (org-restart-font-lock))
  10214. (easy-menu-add orgtbl-mode-menu)
  10215. (run-hooks 'orgtbl-mode-hook))
  10216. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10217. (org-cleanup-narrow-column-properties)
  10218. (org-remove-from-invisibility-spec '(org-cwidth))
  10219. (remove-hook 'before-change-functions 'org-before-change-function t)
  10220. (when (fboundp 'font-lock-remove-keywords)
  10221. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10222. (org-restart-font-lock))
  10223. (easy-menu-remove orgtbl-mode-menu)
  10224. (force-mode-line-update 'all))))
  10225. (defun org-cleanup-narrow-column-properties ()
  10226. "Remove all properties related to narrow-column invisibility."
  10227. (let ((s 1))
  10228. (while (setq s (text-property-any s (point-max)
  10229. 'display org-narrow-column-arrow))
  10230. (remove-text-properties s (1+ s) '(display t)))
  10231. (setq s 1)
  10232. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10233. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10234. (setq s 1)
  10235. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10236. (remove-text-properties s (1+ s) '(invisible t)))))
  10237. ;; Install it as a minor mode.
  10238. (put 'orgtbl-mode :included t)
  10239. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10240. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10241. (defun orgtbl-make-binding (fun n &rest keys)
  10242. "Create a function for binding in the table minor mode.
  10243. FUN is the command to call inside a table. N is used to create a unique
  10244. command name. KEYS are keys that should be checked in for a command
  10245. to execute outside of tables."
  10246. (eval
  10247. (list 'defun
  10248. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10249. '(arg)
  10250. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10251. "Outside of tables, run the binding of `"
  10252. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10253. "'.")
  10254. '(interactive "p")
  10255. (list 'if
  10256. '(org-at-table-p)
  10257. (list 'call-interactively (list 'quote fun))
  10258. (list 'let '(orgtbl-mode)
  10259. (list 'call-interactively
  10260. (append '(or)
  10261. (mapcar (lambda (k)
  10262. (list 'key-binding k))
  10263. keys)
  10264. '('orgtbl-error))))))))
  10265. (defun orgtbl-error ()
  10266. "Error when there is no default binding for a table key."
  10267. (interactive)
  10268. (error "This key has no function outside tables"))
  10269. (defun orgtbl-setup ()
  10270. "Setup orgtbl keymaps."
  10271. (let ((nfunc 0)
  10272. (bindings
  10273. (list
  10274. '([(meta shift left)] org-table-delete-column)
  10275. '([(meta left)] org-table-move-column-left)
  10276. '([(meta right)] org-table-move-column-right)
  10277. '([(meta shift right)] org-table-insert-column)
  10278. '([(meta shift up)] org-table-kill-row)
  10279. '([(meta shift down)] org-table-insert-row)
  10280. '([(meta up)] org-table-move-row-up)
  10281. '([(meta down)] org-table-move-row-down)
  10282. '("\C-c\C-w" org-table-cut-region)
  10283. '("\C-c\M-w" org-table-copy-region)
  10284. '("\C-c\C-y" org-table-paste-rectangle)
  10285. '("\C-c-" org-table-insert-hline)
  10286. '("\C-c}" org-table-toggle-coordinate-overlays)
  10287. '("\C-c{" org-table-toggle-formula-debugger)
  10288. '("\C-m" org-table-next-row)
  10289. '([(shift return)] org-table-copy-down)
  10290. '("\C-c\C-q" org-table-wrap-region)
  10291. '("\C-c?" org-table-field-info)
  10292. '("\C-c " org-table-blank-field)
  10293. '("\C-c+" org-table-sum)
  10294. '("\C-c=" org-table-eval-formula)
  10295. '("\C-c'" org-table-edit-formulas)
  10296. '("\C-c`" org-table-edit-field)
  10297. '("\C-c*" org-table-recalculate)
  10298. '("\C-c|" org-table-create-or-convert-from-region)
  10299. '("\C-c^" org-table-sort-lines)
  10300. '([(control ?#)] org-table-rotate-recalc-marks)))
  10301. elt key fun cmd)
  10302. (while (setq elt (pop bindings))
  10303. (setq nfunc (1+ nfunc))
  10304. (setq key (org-key (car elt))
  10305. fun (nth 1 elt)
  10306. cmd (orgtbl-make-binding fun nfunc key))
  10307. (org-defkey orgtbl-mode-map key cmd))
  10308. ;; Special treatment needed for TAB and RET
  10309. (org-defkey orgtbl-mode-map [(return)]
  10310. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10311. (org-defkey orgtbl-mode-map "\C-m"
  10312. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10313. (org-defkey orgtbl-mode-map [(tab)]
  10314. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10315. (org-defkey orgtbl-mode-map "\C-i"
  10316. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10317. (org-defkey orgtbl-mode-map [(shift tab)]
  10318. (orgtbl-make-binding 'org-table-previous-field 104
  10319. [(shift tab)] [(tab)] "\C-i"))
  10320. (org-defkey orgtbl-mode-map "\M-\C-m"
  10321. (orgtbl-make-binding 'org-table-wrap-region 105
  10322. "\M-\C-m" [(meta return)]))
  10323. (org-defkey orgtbl-mode-map [(meta return)]
  10324. (orgtbl-make-binding 'org-table-wrap-region 106
  10325. [(meta return)] "\M-\C-m"))
  10326. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10327. (when orgtbl-optimized
  10328. ;; If the user wants maximum table support, we need to hijack
  10329. ;; some standard editing functions
  10330. (org-remap orgtbl-mode-map
  10331. 'self-insert-command 'orgtbl-self-insert-command
  10332. 'delete-char 'org-delete-char
  10333. 'delete-backward-char 'org-delete-backward-char)
  10334. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10335. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10336. '("OrgTbl"
  10337. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10338. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10339. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10340. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10341. "--"
  10342. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10343. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10344. ["Copy Field from Above"
  10345. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10346. "--"
  10347. ("Column"
  10348. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10349. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10350. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10351. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10352. ("Row"
  10353. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10354. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10355. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10356. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10357. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10358. "--"
  10359. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10360. ("Rectangle"
  10361. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10362. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10363. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10364. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10365. "--"
  10366. ("Radio tables"
  10367. ["Insert table template" orgtbl-insert-radio-table
  10368. (assq major-mode orgtbl-radio-table-templates)]
  10369. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10370. "--"
  10371. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10372. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10373. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10374. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10375. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10376. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10377. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10378. ["Sum Column/Rectangle" org-table-sum
  10379. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10380. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10381. ["Debug Formulas"
  10382. org-table-toggle-formula-debugger :active (org-at-table-p)
  10383. :keys "C-c {"
  10384. :style toggle :selected org-table-formula-debug]
  10385. ["Show Col/Row Numbers"
  10386. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10387. :keys "C-c }"
  10388. :style toggle :selected org-table-overlay-coordinates]
  10389. ))
  10390. t))
  10391. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10392. "If the cursor is inside a table, realign the table.
  10393. It it is a table to be sent away to a receiver, do it.
  10394. With prefix arg, also recompute table."
  10395. (interactive "P")
  10396. (let ((pos (point)) action)
  10397. (save-excursion
  10398. (beginning-of-line 1)
  10399. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10400. ((looking-at "[ \t]*|") pos)
  10401. ((looking-at "#\\+TBLFM:") 'recalc))))
  10402. (cond
  10403. ((integerp action)
  10404. (goto-char action)
  10405. (org-table-maybe-eval-formula)
  10406. (if arg
  10407. (call-interactively 'org-table-recalculate)
  10408. (org-table-maybe-recalculate-line))
  10409. (call-interactively 'org-table-align)
  10410. (orgtbl-send-table 'maybe))
  10411. ((eq action 'recalc)
  10412. (save-excursion
  10413. (beginning-of-line 1)
  10414. (skip-chars-backward " \r\n\t")
  10415. (if (org-at-table-p)
  10416. (org-call-with-arg 'org-table-recalculate t))))
  10417. (t (let (orgtbl-mode)
  10418. (call-interactively (key-binding "\C-c\C-c")))))))
  10419. (defun orgtbl-tab (arg)
  10420. "Justification and field motion for `orgtbl-mode'."
  10421. (interactive "P")
  10422. (if arg (org-table-edit-field t)
  10423. (org-table-justify-field-maybe)
  10424. (org-table-next-field)))
  10425. (defun orgtbl-ret ()
  10426. "Justification and field motion for `orgtbl-mode'."
  10427. (interactive)
  10428. (org-table-justify-field-maybe)
  10429. (org-table-next-row))
  10430. (defun orgtbl-self-insert-command (N)
  10431. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10432. If the cursor is in a table looking at whitespace, the whitespace is
  10433. overwritten, and the table is not marked as requiring realignment."
  10434. (interactive "p")
  10435. (if (and (org-at-table-p)
  10436. (or
  10437. (and org-table-auto-blank-field
  10438. (member last-command
  10439. '(orgtbl-hijacker-command-100
  10440. orgtbl-hijacker-command-101
  10441. orgtbl-hijacker-command-102
  10442. orgtbl-hijacker-command-103
  10443. orgtbl-hijacker-command-104
  10444. orgtbl-hijacker-command-105))
  10445. (org-table-blank-field))
  10446. t)
  10447. (eq N 1)
  10448. (looking-at "[^|\n]* +|"))
  10449. (let (org-table-may-need-update)
  10450. (goto-char (1- (match-end 0)))
  10451. (delete-backward-char 1)
  10452. (goto-char (match-beginning 0))
  10453. (self-insert-command N))
  10454. (setq org-table-may-need-update t)
  10455. (let (orgtbl-mode)
  10456. (call-interactively (key-binding (vector last-input-event))))))
  10457. (defun org-force-self-insert (N)
  10458. "Needed to enforce self-insert under remapping."
  10459. (interactive "p")
  10460. (self-insert-command N))
  10461. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10462. "Regula expression matching exponentials as produced by calc.")
  10463. (defvar org-table-clean-did-remove-column nil)
  10464. (defun orgtbl-export (table target)
  10465. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10466. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10467. org-table-last-alignment org-table-last-column-widths
  10468. maxcol column)
  10469. (if (not (fboundp func))
  10470. (error "Cannot export orgtbl table to %s" target))
  10471. (setq lines (org-table-clean-before-export lines))
  10472. (setq table
  10473. (mapcar
  10474. (lambda (x)
  10475. (if (string-match org-table-hline-regexp x)
  10476. 'hline
  10477. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10478. lines))
  10479. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10480. table)))
  10481. (loop for i from (1- maxcol) downto 0 do
  10482. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10483. (setq column (delq nil column))
  10484. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10485. (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))
  10486. (funcall func table nil)))
  10487. (defun orgtbl-send-table (&optional maybe)
  10488. "Send a tranformed version of this table to the receiver position.
  10489. With argument MAYBE, fail quietly if no transformation is defined for
  10490. this table."
  10491. (interactive)
  10492. (catch 'exit
  10493. (unless (org-at-table-p) (error "Not at a table"))
  10494. ;; when non-interactive, we assume align has just happened.
  10495. (when (interactive-p) (org-table-align))
  10496. (save-excursion
  10497. (goto-char (org-table-begin))
  10498. (beginning-of-line 0)
  10499. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10500. (if maybe
  10501. (throw 'exit nil)
  10502. (error "Don't know how to transform this table."))))
  10503. (let* ((name (match-string 1))
  10504. beg
  10505. (transform (intern (match-string 2)))
  10506. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10507. (skip (plist-get params :skip))
  10508. (skipcols (plist-get params :skipcols))
  10509. (txt (buffer-substring-no-properties
  10510. (org-table-begin) (org-table-end)))
  10511. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10512. (lines (org-table-clean-before-export lines))
  10513. (i0 (if org-table-clean-did-remove-column 2 1))
  10514. (table (mapcar
  10515. (lambda (x)
  10516. (if (string-match org-table-hline-regexp x)
  10517. 'hline
  10518. (org-remove-by-index
  10519. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10520. skipcols i0)))
  10521. lines))
  10522. (fun (if (= i0 2) 'cdr 'identity))
  10523. (org-table-last-alignment
  10524. (org-remove-by-index (funcall fun org-table-last-alignment)
  10525. skipcols i0))
  10526. (org-table-last-column-widths
  10527. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10528. skipcols i0)))
  10529. (unless (fboundp transform)
  10530. (error "No such transformation function %s" transform))
  10531. (setq txt (funcall transform table params))
  10532. ;; Find the insertion place
  10533. (save-excursion
  10534. (goto-char (point-min))
  10535. (unless (re-search-forward
  10536. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10537. (error "Don't know where to insert translated table"))
  10538. (goto-char (match-beginning 0))
  10539. (beginning-of-line 2)
  10540. (setq beg (point))
  10541. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10542. (error "Cannot find end of insertion region"))
  10543. (beginning-of-line 1)
  10544. (delete-region beg (point))
  10545. (goto-char beg)
  10546. (insert txt "\n"))
  10547. (message "Table converted and installed at receiver location"))))
  10548. (defun org-remove-by-index (list indices &optional i0)
  10549. "Remove the elements in LIST with indices in INDICES.
  10550. First element has index 0, or I0 if given."
  10551. (if (not indices)
  10552. list
  10553. (if (integerp indices) (setq indices (list indices)))
  10554. (setq i0 (1- (or i0 0)))
  10555. (delq :rm (mapcar (lambda (x)
  10556. (setq i0 (1+ i0))
  10557. (if (memq i0 indices) :rm x))
  10558. list))))
  10559. (defun orgtbl-toggle-comment ()
  10560. "Comment or uncomment the orgtbl at point."
  10561. (interactive)
  10562. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10563. (re2 (concat "^" orgtbl-line-start-regexp))
  10564. (commented (save-excursion (beginning-of-line 1)
  10565. (cond ((looking-at re1) t)
  10566. ((looking-at re2) nil)
  10567. (t (error "Not at an org table")))))
  10568. (re (if commented re1 re2))
  10569. beg end)
  10570. (save-excursion
  10571. (beginning-of-line 1)
  10572. (while (looking-at re) (beginning-of-line 0))
  10573. (beginning-of-line 2)
  10574. (setq beg (point))
  10575. (while (looking-at re) (beginning-of-line 2))
  10576. (setq end (point)))
  10577. (comment-region beg end (if commented '(4) nil))))
  10578. (defun orgtbl-insert-radio-table ()
  10579. "Insert a radio table template appropriate for this major mode."
  10580. (interactive)
  10581. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10582. (txt (nth 1 e))
  10583. name pos)
  10584. (unless e (error "No radio table setup defined for %s" major-mode))
  10585. (setq name (read-string "Table name: "))
  10586. (while (string-match "%n" txt)
  10587. (setq txt (replace-match name t t txt)))
  10588. (or (bolp) (insert "\n"))
  10589. (setq pos (point))
  10590. (insert txt)
  10591. (goto-char pos)))
  10592. (defun org-get-param (params header i sym &optional hsym)
  10593. "Get parameter value for symbol SYM.
  10594. If this is a header line, actually get the value for the symbol with an
  10595. additional \"h\" inserted after the colon.
  10596. If the value is a protperty list, get the element for the current column.
  10597. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10598. (let ((val (plist-get params sym)))
  10599. (and hsym header (setq val (or (plist-get params hsym) val)))
  10600. (if (consp val) (plist-get val i) val)))
  10601. (defun orgtbl-to-generic (table params)
  10602. "Convert the orgtbl-mode TABLE to some other format.
  10603. This generic routine can be used for many standard cases.
  10604. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10605. separator line, or a list of fields for that line.
  10606. PARAMS is a property list of parameters that can influence the conversion.
  10607. For the generic converter, some parameters are obligatory: You need to
  10608. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10609. :splice, you must have :tstart and :tend.
  10610. Valid parameters are
  10611. :tstart String to start the table. Ignored when :splice is t.
  10612. :tend String to end the table. Ignored when :splice is t.
  10613. :splice When set to t, return only table body lines, don't wrap
  10614. them into :tstart and :tend. Default is nil.
  10615. :hline String to be inserted on horizontal separation lines.
  10616. May be nil to ignore hlines.
  10617. :lstart String to start a new table line.
  10618. :lend String to end a table line
  10619. :sep Separator between two fields
  10620. :lfmt Format for entire line, with enough %s to capture all fields.
  10621. If this is present, :lstart, :lend, and :sep are ignored.
  10622. :fmt A format to be used to wrap the field, should contain
  10623. %s for the original field value. For example, to wrap
  10624. everything in dollars, you could use :fmt \"$%s$\".
  10625. This may also be a property list with column numbers and
  10626. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10627. :hlstart :hlend :hlsep :hlfmt :hfmt
  10628. Same as above, specific for the header lines in the table.
  10629. All lines before the first hline are treated as header.
  10630. If any of these is not present, the data line value is used.
  10631. :efmt Use this format to print numbers with exponentials.
  10632. The format should have %s twice for inserting mantissa
  10633. and exponent, for example \"%s\\\\times10^{%s}\". This
  10634. may also be a property list with column numbers and
  10635. formats. :fmt will still be applied after :efmt.
  10636. In addition to this, the parameters :skip and :skipcols are always handled
  10637. directly by `orgtbl-send-table'. See manual."
  10638. (interactive)
  10639. (let* ((p params)
  10640. (splicep (plist-get p :splice))
  10641. (hline (plist-get p :hline))
  10642. rtn line i fm efm lfmt h)
  10643. ;; Do we have a header?
  10644. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10645. (setq h t))
  10646. ;; Put header
  10647. (unless splicep
  10648. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10649. ;; Now loop over all lines
  10650. (while (setq line (pop table))
  10651. (if (eq line 'hline)
  10652. ;; A horizontal separator line
  10653. (progn (if hline (push hline rtn))
  10654. (setq h nil)) ; no longer in header
  10655. ;; A normal line. Convert the fields, push line onto the result list
  10656. (setq i 0)
  10657. (setq line
  10658. (mapcar
  10659. (lambda (f)
  10660. (setq i (1+ i)
  10661. fm (org-get-param p h i :fmt :hfmt)
  10662. efm (org-get-param p h i :efmt))
  10663. (if (and efm (string-match orgtbl-exp-regexp f))
  10664. (setq f (format
  10665. efm (match-string 1 f) (match-string 2 f))))
  10666. (if fm (setq f (format fm f)))
  10667. f)
  10668. line))
  10669. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10670. (push (apply 'format lfmt line) rtn)
  10671. (push (concat
  10672. (org-get-param p h i :lstart :hlstart)
  10673. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10674. (org-get-param p h i :lend :hlend))
  10675. rtn))))
  10676. (unless splicep
  10677. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10678. (mapconcat 'identity (nreverse rtn) "\n")))
  10679. (defun orgtbl-to-latex (table params)
  10680. "Convert the orgtbl-mode TABLE to LaTeX.
  10681. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10682. separator line, or a list of fields for that line.
  10683. PARAMS is a property list of parameters that can influence the conversion.
  10684. Supports all parameters from `orgtbl-to-generic'. Most important for
  10685. LaTeX are:
  10686. :splice When set to t, return only table body lines, don't wrap
  10687. them into a tabular environment. Default is nil.
  10688. :fmt A format to be used to wrap the field, should contain %s for the
  10689. original field value. For example, to wrap everything in dollars,
  10690. use :fmt \"$%s$\". This may also be a property list with column
  10691. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10692. :efmt Format for transforming numbers with exponentials. The format
  10693. should have %s twice for inserting mantissa and exponent, for
  10694. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10695. This may also be a property list with column numbers and formats.
  10696. The general parameters :skip and :skipcols have already been applied when
  10697. this function is called."
  10698. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10699. org-table-last-alignment ""))
  10700. (params2
  10701. (list
  10702. :tstart (concat "\\begin{tabular}{" alignment "}")
  10703. :tend "\\end{tabular}"
  10704. :lstart "" :lend " \\\\" :sep " & "
  10705. :efmt "%s\\,(%s)" :hline "\\hline")))
  10706. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10707. (defun orgtbl-to-html (table params)
  10708. "Convert the orgtbl-mode TABLE to LaTeX.
  10709. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10710. separator line, or a list of fields for that line.
  10711. PARAMS is a property list of parameters that can influence the conversion.
  10712. Currently this function recognizes the following parameters:
  10713. :splice When set to t, return only table body lines, don't wrap
  10714. them into a <table> environment. Default is nil.
  10715. The general parameters :skip and :skipcols have already been applied when
  10716. this function is called. The function does *not* use `orgtbl-to-generic',
  10717. so you cannot specify parameters for it."
  10718. (let* ((splicep (plist-get params :splice))
  10719. html)
  10720. ;; Just call the formatter we already have
  10721. ;; We need to make text lines for it, so put the fields back together.
  10722. (setq html (org-format-org-table-html
  10723. (mapcar
  10724. (lambda (x)
  10725. (if (eq x 'hline)
  10726. "|----+----|"
  10727. (concat "| " (mapconcat 'identity x " | ") " |")))
  10728. table)
  10729. splicep))
  10730. (if (string-match "\n+\\'" html)
  10731. (setq html (replace-match "" t t html)))
  10732. html))
  10733. (defun orgtbl-to-texinfo (table params)
  10734. "Convert the orgtbl-mode TABLE to TeXInfo.
  10735. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10736. separator line, or a list of fields for that line.
  10737. PARAMS is a property list of parameters that can influence the conversion.
  10738. Supports all parameters from `orgtbl-to-generic'. Most important for
  10739. TeXInfo are:
  10740. :splice nil/t When set to t, return only table body lines, don't wrap
  10741. them into a multitable environment. Default is nil.
  10742. :fmt fmt A format to be used to wrap the field, should contain
  10743. %s for the original field value. For example, to wrap
  10744. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10745. This may also be a property list with column numbers and
  10746. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10747. :cf \"f1 f2..\" The column fractions for the table. By default these
  10748. are computed automatically from the width of the columns
  10749. under org-mode.
  10750. The general parameters :skip and :skipcols have already been applied when
  10751. this function is called."
  10752. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10753. (colfrac (or (plist-get params :cf)
  10754. (mapconcat
  10755. (lambda (x) (format "%.3f" (/ (float x) total)))
  10756. org-table-last-column-widths " ")))
  10757. (params2
  10758. (list
  10759. :tstart (concat "@multitable @columnfractions " colfrac)
  10760. :tend "@end multitable"
  10761. :lstart "@item " :lend "" :sep " @tab "
  10762. :hlstart "@headitem ")))
  10763. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10764. ;;;; Link Stuff
  10765. ;;; Link abbreviations
  10766. (defun org-link-expand-abbrev (link)
  10767. "Apply replacements as defined in `org-link-abbrev-alist."
  10768. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10769. (let* ((key (match-string 1 link))
  10770. (as (or (assoc key org-link-abbrev-alist-local)
  10771. (assoc key org-link-abbrev-alist)))
  10772. (tag (and (match-end 2) (match-string 3 link)))
  10773. rpl)
  10774. (if (not as)
  10775. link
  10776. (setq rpl (cdr as))
  10777. (cond
  10778. ((symbolp rpl) (funcall rpl tag))
  10779. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10780. (t (concat rpl tag)))))
  10781. link))
  10782. ;;; Storing and inserting links
  10783. (defvar org-insert-link-history nil
  10784. "Minibuffer history for links inserted with `org-insert-link'.")
  10785. (defvar org-stored-links nil
  10786. "Contains the links stored with `org-store-link'.")
  10787. (defvar org-store-link-plist nil
  10788. "Plist with info about the most recently link created with `org-store-link'.")
  10789. (defvar org-link-protocols nil
  10790. "Link protocols added to Org-mode using `org-add-link-type'.")
  10791. (defvar org-store-link-functions nil
  10792. "List of functions that are called to create and store a link.
  10793. Each function will be called in turn until one returns a non-nil
  10794. value. Each function should check if it is responsible for creating
  10795. this link (for example by looking at the major mode).
  10796. If not, it must exit and return nil.
  10797. If yes, it should return a non-nil value after a calling
  10798. `org-store-link-props' with a list of properties and values.
  10799. Special properties are:
  10800. :type The link prefix. like \"http\". This must be given.
  10801. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10802. This is obligatory as well.
  10803. :description Optional default description for the second pair
  10804. of brackets in an Org-mode link. The user can still change
  10805. this when inserting this link into an Org-mode buffer.
  10806. In addition to these, any additional properties can be specified
  10807. and then used in remember templates.")
  10808. (defun org-add-link-type (type &optional follow publish)
  10809. "Add TYPE to the list of `org-link-types'.
  10810. Re-compute all regular expressions depending on `org-link-types'
  10811. FOLLOW and PUBLISH are two functions. Both take the link path as
  10812. an argument.
  10813. FOLLOW should do whatever is necessary to follow the link, for example
  10814. to find a file or display a mail message.
  10815. PUBLISH takes the path and retuns the string that should be used when
  10816. this document is published. FIMXE: This is actually not yet implemented."
  10817. (add-to-list 'org-link-types type t)
  10818. (org-make-link-regexps)
  10819. (add-to-list 'org-link-protocols
  10820. (list type follow publish)))
  10821. (defun org-add-agenda-custom-command (entry)
  10822. "Replace or add a command in `org-agenda-custom-commands'.
  10823. This is mostly for hacking and trying a new command - once the command
  10824. works you probably want to add it to `org-agenda-custom-commands' for good."
  10825. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10826. (if ass
  10827. (setcdr ass (cdr entry))
  10828. (push entry org-agenda-custom-commands))))
  10829. ;;;###autoload
  10830. (defun org-store-link (arg)
  10831. "\\<org-mode-map>Store an org-link to the current location.
  10832. This link can later be inserted into an org-buffer with
  10833. \\[org-insert-link].
  10834. For some link types, a prefix arg is interpreted:
  10835. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10836. For file links, arg negates `org-context-in-file-links'."
  10837. (interactive "P")
  10838. (setq org-store-link-plist nil) ; reset
  10839. (let (link cpltxt desc description search txt)
  10840. (cond
  10841. ((run-hook-with-args-until-success 'org-store-link-functions)
  10842. (setq link (plist-get org-store-link-plist :link)
  10843. desc (or (plist-get org-store-link-plist :description) link)))
  10844. ((eq major-mode 'bbdb-mode)
  10845. (let ((name (bbdb-record-name (bbdb-current-record)))
  10846. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10847. (setq cpltxt (concat "bbdb:" (or name company))
  10848. link (org-make-link cpltxt))
  10849. (org-store-link-props :type "bbdb" :name name :company company)))
  10850. ((eq major-mode 'Info-mode)
  10851. (setq link (org-make-link "info:"
  10852. (file-name-nondirectory Info-current-file)
  10853. ":" Info-current-node))
  10854. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10855. ":" Info-current-node))
  10856. (org-store-link-props :type "info" :file Info-current-file
  10857. :node Info-current-node))
  10858. ((eq major-mode 'calendar-mode)
  10859. (let ((cd (calendar-cursor-to-date)))
  10860. (setq link
  10861. (format-time-string
  10862. (car org-time-stamp-formats)
  10863. (apply 'encode-time
  10864. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10865. nil nil nil))))
  10866. (org-store-link-props :type "calendar" :date cd)))
  10867. ((or (eq major-mode 'vm-summary-mode)
  10868. (eq major-mode 'vm-presentation-mode))
  10869. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10870. (vm-follow-summary-cursor)
  10871. (save-excursion
  10872. (vm-select-folder-buffer)
  10873. (let* ((message (car vm-message-pointer))
  10874. (folder buffer-file-name)
  10875. (subject (vm-su-subject message))
  10876. (to (vm-get-header-contents message "To"))
  10877. (from (vm-get-header-contents message "From"))
  10878. (message-id (vm-su-message-id message)))
  10879. (org-store-link-props :type "vm" :from from :to to :subject subject
  10880. :message-id message-id)
  10881. (setq message-id (org-remove-angle-brackets message-id))
  10882. (setq folder (abbreviate-file-name folder))
  10883. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10884. folder)
  10885. (setq folder (replace-match "" t t folder)))
  10886. (setq cpltxt (org-email-link-description))
  10887. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10888. ((eq major-mode 'wl-summary-mode)
  10889. (let* ((msgnum (wl-summary-message-number))
  10890. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10891. msgnum 'message-id))
  10892. (wl-message-entity
  10893. (if (fboundp 'elmo-message-entity)
  10894. (elmo-message-entity
  10895. wl-summary-buffer-elmo-folder msgnum)
  10896. (elmo-msgdb-overview-get-entity
  10897. msgnum (wl-summary-buffer-msgdb))))
  10898. (from (wl-summary-line-from))
  10899. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10900. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10901. (wl-summary-line-subject))))
  10902. (org-store-link-props :type "wl" :from from :to to
  10903. :subject subject :message-id message-id)
  10904. (setq message-id (org-remove-angle-brackets message-id))
  10905. (setq cpltxt (org-email-link-description))
  10906. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10907. "#" message-id))))
  10908. ((or (equal major-mode 'mh-folder-mode)
  10909. (equal major-mode 'mh-show-mode))
  10910. (let ((from (org-mhe-get-header "From:"))
  10911. (to (org-mhe-get-header "To:"))
  10912. (message-id (org-mhe-get-header "Message-Id:"))
  10913. (subject (org-mhe-get-header "Subject:")))
  10914. (org-store-link-props :type "mh" :from from :to to
  10915. :subject subject :message-id message-id)
  10916. (setq cpltxt (org-email-link-description))
  10917. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10918. (org-remove-angle-brackets message-id)))))
  10919. ((eq major-mode 'rmail-mode)
  10920. (save-excursion
  10921. (save-restriction
  10922. (rmail-narrow-to-non-pruned-header)
  10923. (let ((folder buffer-file-name)
  10924. (message-id (mail-fetch-field "message-id"))
  10925. (from (mail-fetch-field "from"))
  10926. (to (mail-fetch-field "to"))
  10927. (subject (mail-fetch-field "subject")))
  10928. (org-store-link-props
  10929. :type "rmail" :from from :to to
  10930. :subject subject :message-id message-id)
  10931. (setq message-id (org-remove-angle-brackets message-id))
  10932. (setq cpltxt (org-email-link-description))
  10933. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10934. ((eq major-mode 'gnus-group-mode)
  10935. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10936. (gnus-group-group-name)) ; version
  10937. ((fboundp 'gnus-group-name)
  10938. (gnus-group-name))
  10939. (t "???"))))
  10940. (unless group (error "Not on a group"))
  10941. (org-store-link-props :type "gnus" :group group)
  10942. (setq cpltxt (concat
  10943. (if (org-xor arg org-usenet-links-prefer-google)
  10944. "http://groups.google.com/groups?group="
  10945. "gnus:")
  10946. group)
  10947. link (org-make-link cpltxt))))
  10948. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10949. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10950. (let* ((group gnus-newsgroup-name)
  10951. (article (gnus-summary-article-number))
  10952. (header (gnus-summary-article-header article))
  10953. (from (mail-header-from header))
  10954. (message-id (mail-header-id header))
  10955. (date (mail-header-date header))
  10956. (subject (gnus-summary-subject-string)))
  10957. (org-store-link-props :type "gnus" :from from :subject subject
  10958. :message-id message-id :group group)
  10959. (setq cpltxt (org-email-link-description))
  10960. (if (org-xor arg org-usenet-links-prefer-google)
  10961. (setq link
  10962. (concat
  10963. cpltxt "\n "
  10964. (format "http://groups.google.com/groups?as_umsgid=%s"
  10965. (org-fixup-message-id-for-http message-id))))
  10966. (setq link (org-make-link "gnus:" group
  10967. "#" (number-to-string article))))))
  10968. ((eq major-mode 'w3-mode)
  10969. (setq cpltxt (url-view-url t)
  10970. link (org-make-link cpltxt))
  10971. (org-store-link-props :type "w3" :url (url-view-url t)))
  10972. ((eq major-mode 'w3m-mode)
  10973. (setq cpltxt (or w3m-current-title w3m-current-url)
  10974. link (org-make-link w3m-current-url))
  10975. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10976. ((setq search (run-hook-with-args-until-success
  10977. 'org-create-file-search-functions))
  10978. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10979. "::" search))
  10980. (setq cpltxt (or description link)))
  10981. ((eq major-mode 'image-mode)
  10982. (setq cpltxt (concat "file:"
  10983. (abbreviate-file-name buffer-file-name))
  10984. link (org-make-link cpltxt))
  10985. (org-store-link-props :type "image" :file buffer-file-name))
  10986. ((eq major-mode 'dired-mode)
  10987. ;; link to the file in the current line
  10988. (setq cpltxt (concat "file:"
  10989. (abbreviate-file-name
  10990. (expand-file-name
  10991. (dired-get-filename nil t))))
  10992. link (org-make-link cpltxt)))
  10993. ((and buffer-file-name (org-mode-p))
  10994. ;; Just link to current headline
  10995. (setq cpltxt (concat "file:"
  10996. (abbreviate-file-name buffer-file-name)))
  10997. ;; Add a context search string
  10998. (when (org-xor org-context-in-file-links arg)
  10999. ;; Check if we are on a target
  11000. (if (org-in-regexp "<<\\(.*?\\)>>")
  11001. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11002. (setq txt (cond
  11003. ((org-on-heading-p) nil)
  11004. ((org-region-active-p)
  11005. (buffer-substring (region-beginning) (region-end)))
  11006. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11007. (when (or (null txt) (string-match "\\S-" txt))
  11008. (setq cpltxt
  11009. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11010. desc "NONE"))))
  11011. (if (string-match "::\\'" cpltxt)
  11012. (setq cpltxt (substring cpltxt 0 -2)))
  11013. (setq link (org-make-link cpltxt)))
  11014. ((buffer-file-name (buffer-base-buffer))
  11015. ;; Just link to this file here.
  11016. (setq cpltxt (concat "file:"
  11017. (abbreviate-file-name
  11018. (buffer-file-name (buffer-base-buffer)))))
  11019. ;; Add a context string
  11020. (when (org-xor org-context-in-file-links arg)
  11021. (setq txt (if (org-region-active-p)
  11022. (buffer-substring (region-beginning) (region-end))
  11023. (buffer-substring (point-at-bol) (point-at-eol))))
  11024. ;; Only use search option if there is some text.
  11025. (when (string-match "\\S-" txt)
  11026. (setq cpltxt
  11027. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11028. desc "NONE")))
  11029. (setq link (org-make-link cpltxt)))
  11030. ((interactive-p)
  11031. (error "Cannot link to a buffer which is not visiting a file"))
  11032. (t (setq link nil)))
  11033. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11034. (setq link (or link cpltxt)
  11035. desc (or desc cpltxt))
  11036. (if (equal desc "NONE") (setq desc nil))
  11037. (if (and (interactive-p) link)
  11038. (progn
  11039. (setq org-stored-links
  11040. (cons (list link desc) org-stored-links))
  11041. (message "Stored: %s" (or desc link)))
  11042. (and link (org-make-link-string link desc)))))
  11043. (defun org-store-link-props (&rest plist)
  11044. "Store link properties, extract names and addresses."
  11045. (let (x adr)
  11046. (when (setq x (plist-get plist :from))
  11047. (setq adr (mail-extract-address-components x))
  11048. (plist-put plist :fromname (car adr))
  11049. (plist-put plist :fromaddress (nth 1 adr)))
  11050. (when (setq x (plist-get plist :to))
  11051. (setq adr (mail-extract-address-components x))
  11052. (plist-put plist :toname (car adr))
  11053. (plist-put plist :toaddress (nth 1 adr))))
  11054. (let ((from (plist-get plist :from))
  11055. (to (plist-get plist :to)))
  11056. (when (and from to org-from-is-user-regexp)
  11057. (plist-put plist :fromto
  11058. (if (string-match org-from-is-user-regexp from)
  11059. (concat "to %t")
  11060. (concat "from %f")))))
  11061. (setq org-store-link-plist plist))
  11062. (defun org-email-link-description (&optional fmt)
  11063. "Return the description part of an email link.
  11064. This takes information from `org-store-link-plist' and formats it
  11065. according to FMT (default from `org-email-link-description-format')."
  11066. (setq fmt (or fmt org-email-link-description-format))
  11067. (let* ((p org-store-link-plist)
  11068. (to (plist-get p :toaddress))
  11069. (from (plist-get p :fromaddress))
  11070. (table
  11071. (list
  11072. (cons "%c" (plist-get p :fromto))
  11073. (cons "%F" (plist-get p :from))
  11074. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11075. (cons "%T" (plist-get p :to))
  11076. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11077. (cons "%s" (plist-get p :subject))
  11078. (cons "%m" (plist-get p :message-id)))))
  11079. (when (string-match "%c" fmt)
  11080. ;; Check if the user wrote this message
  11081. (if (and org-from-is-user-regexp from to
  11082. (save-match-data (string-match org-from-is-user-regexp from)))
  11083. (setq fmt (replace-match "to %t" t t fmt))
  11084. (setq fmt (replace-match "from %f" t t fmt))))
  11085. (org-replace-escapes fmt table)))
  11086. (defun org-make-org-heading-search-string (&optional string heading)
  11087. "Make search string for STRING or current headline."
  11088. (interactive)
  11089. (let ((s (or string (org-get-heading))))
  11090. (unless (and string (not heading))
  11091. ;; We are using a headline, clean up garbage in there.
  11092. (if (string-match org-todo-regexp s)
  11093. (setq s (replace-match "" t t s)))
  11094. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11095. (setq s (replace-match "" t t s)))
  11096. (setq s (org-trim s))
  11097. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11098. org-comment-string "\\)") s)
  11099. (setq s (replace-match "" t t s)))
  11100. (while (string-match org-ts-regexp s)
  11101. (setq s (replace-match "" t t s))))
  11102. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11103. (setq s (replace-match " " t t s)))
  11104. (or string (setq s (concat "*" s))) ; Add * for headlines
  11105. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11106. (defun org-make-link (&rest strings)
  11107. "Concatenate STRINGS."
  11108. (apply 'concat strings))
  11109. (defun org-make-link-string (link &optional description)
  11110. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11111. (unless (string-match "\\S-" link)
  11112. (error "Empty link"))
  11113. (when (stringp description)
  11114. ;; Remove brackets from the description, they are fatal.
  11115. (while (string-match "\\[" description)
  11116. (setq description (replace-match "{" t t description)))
  11117. (while (string-match "\\]" description)
  11118. (setq description (replace-match "}" t t description))))
  11119. (when (equal (org-link-escape link) description)
  11120. ;; No description needed, it is identical
  11121. (setq description nil))
  11122. (when (and (not description)
  11123. (not (equal link (org-link-escape link))))
  11124. (setq description link))
  11125. (concat "[[" (org-link-escape link) "]"
  11126. (if description (concat "[" description "]") "")
  11127. "]"))
  11128. (defconst org-link-escape-chars
  11129. '((?\ . "%20")
  11130. (?\[ . "%5B")
  11131. (?\] . "%5D")
  11132. (?\340 . "%E0") ; `a
  11133. (?\342 . "%E2") ; ^a
  11134. (?\347 . "%E7") ; ,c
  11135. (?\350 . "%E8") ; `e
  11136. (?\351 . "%E9") ; 'e
  11137. (?\352 . "%EA") ; ^e
  11138. (?\356 . "%EE") ; ^i
  11139. (?\364 . "%F4") ; ^o
  11140. (?\371 . "%F9") ; `u
  11141. (?\373 . "%FB") ; ^u
  11142. (?\; . "%3B")
  11143. (?? . "%3F")
  11144. (?= . "%3D")
  11145. (?+ . "%2B")
  11146. )
  11147. "Association list of escapes for some characters problematic in links.
  11148. This is the list that is used for internal purposes.")
  11149. (defconst org-link-escape-chars-browser
  11150. '((?\ . "%20")) ; 32 for the SPC char
  11151. "Association list of escapes for some characters problematic in links.
  11152. This is the list that is used before handing over to the browser.")
  11153. (defun org-link-escape (text &optional table)
  11154. "Escape charaters in TEXT that are problematic for links."
  11155. (setq table (or table org-link-escape-chars))
  11156. (when text
  11157. (let ((re (mapconcat (lambda (x) (regexp-quote
  11158. (char-to-string (car x))))
  11159. table "\\|")))
  11160. (while (string-match re text)
  11161. (setq text
  11162. (replace-match
  11163. (cdr (assoc (string-to-char (match-string 0 text))
  11164. table))
  11165. t t text)))
  11166. text)))
  11167. (defun org-link-unescape (text &optional table)
  11168. "Reverse the action of `org-link-escape'."
  11169. (setq table (or table org-link-escape-chars))
  11170. (when text
  11171. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11172. table "\\|")))
  11173. (while (string-match re text)
  11174. (setq text
  11175. (replace-match
  11176. (char-to-string (car (rassoc (match-string 0 text) table)))
  11177. t t text)))
  11178. text)))
  11179. (defun org-xor (a b)
  11180. "Exclusive or."
  11181. (if a (not b) b))
  11182. (defun org-get-header (header)
  11183. "Find a header field in the current buffer."
  11184. (save-excursion
  11185. (goto-char (point-min))
  11186. (let ((case-fold-search t) s)
  11187. (cond
  11188. ((eq header 'from)
  11189. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11190. (setq s (match-string 1)))
  11191. (while (string-match "\"" s)
  11192. (setq s (replace-match "" t t s)))
  11193. (if (string-match "[<(].*" s)
  11194. (setq s (replace-match "" t t s))))
  11195. ((eq header 'message-id)
  11196. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11197. (setq s (match-string 1))))
  11198. ((eq header 'subject)
  11199. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11200. (setq s (match-string 1)))))
  11201. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11202. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11203. s)))
  11204. (defun org-fixup-message-id-for-http (s)
  11205. "Replace special characters in a message id, so it can be used in an http query."
  11206. (while (string-match "<" s)
  11207. (setq s (replace-match "%3C" t t s)))
  11208. (while (string-match ">" s)
  11209. (setq s (replace-match "%3E" t t s)))
  11210. (while (string-match "@" s)
  11211. (setq s (replace-match "%40" t t s)))
  11212. s)
  11213. ;;;###autoload
  11214. (defun org-insert-link-global ()
  11215. "Insert a link like Org-mode does.
  11216. This command can be called in any mode to insert a link in Org-mode syntax."
  11217. (interactive)
  11218. (org-run-like-in-org-mode 'org-insert-link))
  11219. (defun org-insert-link (&optional complete-file)
  11220. "Insert a link. At the prompt, enter the link.
  11221. Completion can be used to select a link previously stored with
  11222. `org-store-link'. When the empty string is entered (i.e. if you just
  11223. press RET at the prompt), the link defaults to the most recently
  11224. stored link. As SPC triggers completion in the minibuffer, you need to
  11225. use M-SPC or C-q SPC to force the insertion of a space character.
  11226. You will also be prompted for a description, and if one is given, it will
  11227. be displayed in the buffer instead of the link.
  11228. If there is already a link at point, this command will allow you to edit link
  11229. and description parts.
  11230. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11231. selected using completion. The path to the file will be relative to
  11232. the current directory if the file is in the current directory or a
  11233. subdirectory. Otherwise, the link will be the absolute path as
  11234. completed in the minibuffer (i.e. normally ~/path/to/file).
  11235. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11236. is in the current directory or below.
  11237. With three \\[universal-argument] prefixes, negate the meaning of
  11238. `org-keep-stored-link-after-insertion'."
  11239. (interactive "P")
  11240. (let* ((wcf (current-window-configuration))
  11241. (region (if (org-region-active-p)
  11242. (buffer-substring (region-beginning) (region-end))))
  11243. (remove (and region (list (region-beginning) (region-end))))
  11244. (desc region)
  11245. tmphist ; byte-compile incorrectly complains about this
  11246. link entry file)
  11247. (cond
  11248. ((org-in-regexp org-bracket-link-regexp 1)
  11249. ;; We do have a link at point, and we are going to edit it.
  11250. (setq remove (list (match-beginning 0) (match-end 0)))
  11251. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11252. (setq link (read-string "Link: "
  11253. (org-link-unescape
  11254. (org-match-string-no-properties 1)))))
  11255. ((or (org-in-regexp org-angle-link-re)
  11256. (org-in-regexp org-plain-link-re))
  11257. ;; Convert to bracket link
  11258. (setq remove (list (match-beginning 0) (match-end 0))
  11259. link (read-string "Link: "
  11260. (org-remove-angle-brackets (match-string 0)))))
  11261. ((equal complete-file '(4))
  11262. ;; Completing read for file names.
  11263. (setq file (read-file-name "File: "))
  11264. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11265. (pwd1 (file-name-as-directory (abbreviate-file-name
  11266. (expand-file-name ".")))))
  11267. (cond
  11268. ((equal complete-file '(16))
  11269. (setq link (org-make-link
  11270. "file:"
  11271. (abbreviate-file-name (expand-file-name file)))))
  11272. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11273. (setq link (org-make-link "file:" (match-string 1 file))))
  11274. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11275. (expand-file-name file))
  11276. (setq link (org-make-link
  11277. "file:" (match-string 1 (expand-file-name file)))))
  11278. (t (setq link (org-make-link "file:" file))))))
  11279. (t
  11280. ;; Read link, with completion for stored links.
  11281. (with-output-to-temp-buffer "*Org Links*"
  11282. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11283. (when org-stored-links
  11284. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11285. (princ (mapconcat
  11286. (lambda (x)
  11287. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11288. (reverse org-stored-links) "\n"))))
  11289. (let ((cw (selected-window)))
  11290. (select-window (get-buffer-window "*Org Links*"))
  11291. (shrink-window-if-larger-than-buffer)
  11292. (setq truncate-lines t)
  11293. (select-window cw))
  11294. ;; Fake a link history, containing the stored links.
  11295. (setq tmphist (append (mapcar 'car org-stored-links)
  11296. org-insert-link-history))
  11297. (unwind-protect
  11298. (setq link (org-completing-read
  11299. "Link: "
  11300. (append
  11301. (mapcar (lambda (x) (list (concat (car x) ":")))
  11302. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11303. (mapcar (lambda (x) (list (concat x ":")))
  11304. org-link-types))
  11305. nil nil nil
  11306. 'tmphist
  11307. (or (car (car org-stored-links)))))
  11308. (set-window-configuration wcf)
  11309. (kill-buffer "*Org Links*"))
  11310. (setq entry (assoc link org-stored-links))
  11311. (or entry (push link org-insert-link-history))
  11312. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11313. (not org-keep-stored-link-after-insertion))
  11314. (setq org-stored-links (delq (assoc link org-stored-links)
  11315. org-stored-links)))
  11316. (setq desc (or desc (nth 1 entry)))))
  11317. (if (string-match org-plain-link-re link)
  11318. ;; URL-like link, normalize the use of angular brackets.
  11319. (setq link (org-make-link (org-remove-angle-brackets link))))
  11320. ;; Check if we are linking to the current file with a search option
  11321. ;; If yes, simplify the link by using only the search option.
  11322. (when (and buffer-file-name
  11323. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11324. (let* ((path (match-string 1 link))
  11325. (case-fold-search nil)
  11326. (search (match-string 2 link)))
  11327. (save-match-data
  11328. (if (equal (file-truename buffer-file-name) (file-truename path))
  11329. ;; We are linking to this same file, with a search option
  11330. (setq link search)))))
  11331. ;; Check if we can/should use a relative path. If yes, simplify the link
  11332. (when (string-match "\\<file:\\(.*\\)" link)
  11333. (let* ((path (match-string 1 link))
  11334. (origpath path)
  11335. (desc-is-link (equal link desc))
  11336. (case-fold-search nil))
  11337. (cond
  11338. ((eq org-link-file-path-type 'absolute)
  11339. (setq path (abbreviate-file-name (expand-file-name path))))
  11340. ((eq org-link-file-path-type 'noabbrev)
  11341. (setq path (expand-file-name path)))
  11342. ((eq org-link-file-path-type 'relative)
  11343. (setq path (file-relative-name path)))
  11344. (t
  11345. (save-match-data
  11346. (if (string-match (concat "^" (regexp-quote
  11347. (file-name-as-directory
  11348. (expand-file-name "."))))
  11349. (expand-file-name path))
  11350. ;; We are linking a file with relative path name.
  11351. (setq path (substring (expand-file-name path)
  11352. (match-end 0)))))))
  11353. (setq link (concat "file:" path))
  11354. (if (equal desc origpath)
  11355. (setq desc path))))
  11356. (setq desc (read-string "Description: " desc))
  11357. (unless (string-match "\\S-" desc) (setq desc nil))
  11358. (if remove (apply 'delete-region remove))
  11359. (insert (org-make-link-string link desc))))
  11360. (defun org-completing-read (&rest args)
  11361. (let ((minibuffer-local-completion-map
  11362. (copy-keymap minibuffer-local-completion-map)))
  11363. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11364. (apply 'completing-read args)))
  11365. ;;; Opening/following a link
  11366. (defvar org-link-search-failed nil)
  11367. (defun org-next-link ()
  11368. "Move forward to the next link.
  11369. If the link is in hidden text, expose it."
  11370. (interactive)
  11371. (when (and org-link-search-failed (eq this-command last-command))
  11372. (goto-char (point-min))
  11373. (message "Link search wrapped back to beginning of buffer"))
  11374. (setq org-link-search-failed nil)
  11375. (let* ((pos (point))
  11376. (ct (org-context))
  11377. (a (assoc :link ct)))
  11378. (if a (goto-char (nth 2 a)))
  11379. (if (re-search-forward org-any-link-re nil t)
  11380. (progn
  11381. (goto-char (match-beginning 0))
  11382. (if (org-invisible-p) (org-show-context)))
  11383. (goto-char pos)
  11384. (setq org-link-search-failed t)
  11385. (error "No further link found"))))
  11386. (defun org-previous-link ()
  11387. "Move backward to the previous link.
  11388. If the link is in hidden text, expose it."
  11389. (interactive)
  11390. (when (and org-link-search-failed (eq this-command last-command))
  11391. (goto-char (point-max))
  11392. (message "Link search wrapped back to end of buffer"))
  11393. (setq org-link-search-failed nil)
  11394. (let* ((pos (point))
  11395. (ct (org-context))
  11396. (a (assoc :link ct)))
  11397. (if a (goto-char (nth 1 a)))
  11398. (if (re-search-backward org-any-link-re nil t)
  11399. (progn
  11400. (goto-char (match-beginning 0))
  11401. (if (org-invisible-p) (org-show-context)))
  11402. (goto-char pos)
  11403. (setq org-link-search-failed t)
  11404. (error "No further link found"))))
  11405. (defun org-find-file-at-mouse (ev)
  11406. "Open file link or URL at mouse."
  11407. (interactive "e")
  11408. (mouse-set-point ev)
  11409. (org-open-at-point 'in-emacs))
  11410. (defun org-open-at-mouse (ev)
  11411. "Open file link or URL at mouse."
  11412. (interactive "e")
  11413. (mouse-set-point ev)
  11414. (org-open-at-point))
  11415. (defvar org-window-config-before-follow-link nil
  11416. "The window configuration before following a link.
  11417. This is saved in case the need arises to restore it.")
  11418. (defvar org-open-link-marker (make-marker)
  11419. "Marker pointing to the location where `org-open-at-point; was called.")
  11420. ;;;###autoload
  11421. (defun org-open-at-point-global ()
  11422. "Follow a link like Org-mode does.
  11423. This command can be called in any mode to follow a link that has
  11424. Org-mode syntax."
  11425. (interactive)
  11426. (org-run-like-in-org-mode 'org-open-at-point))
  11427. (defun org-open-at-point (&optional in-emacs)
  11428. "Open link at or after point.
  11429. If there is no link at point, this function will search forward up to
  11430. the end of the current subtree.
  11431. Normally, files will be opened by an appropriate application. If the
  11432. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11433. (interactive "P")
  11434. (catch 'abort
  11435. (move-marker org-open-link-marker (point))
  11436. (setq org-window-config-before-follow-link (current-window-configuration))
  11437. (org-remove-occur-highlights nil nil t)
  11438. (if (org-at-timestamp-p t)
  11439. (org-follow-timestamp-link)
  11440. (let (type path link line search (pos (point)))
  11441. (catch 'match
  11442. (save-excursion
  11443. (skip-chars-forward "^]\n\r")
  11444. (when (org-in-regexp org-bracket-link-regexp)
  11445. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11446. (while (string-match " *\n *" link)
  11447. (setq link (replace-match " " t t link)))
  11448. (setq link (org-link-expand-abbrev link))
  11449. (if (string-match org-link-re-with-space2 link)
  11450. (setq type (match-string 1 link) path (match-string 2 link))
  11451. (setq type "thisfile" path link))
  11452. (throw 'match t)))
  11453. (when (get-text-property (point) 'org-linked-text)
  11454. (setq type "thisfile"
  11455. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11456. (1+ (point)) (point))
  11457. path (buffer-substring
  11458. (previous-single-property-change pos 'org-linked-text)
  11459. (next-single-property-change pos 'org-linked-text)))
  11460. (throw 'match t))
  11461. (save-excursion
  11462. (when (or (org-in-regexp org-angle-link-re)
  11463. (org-in-regexp org-plain-link-re))
  11464. (setq type (match-string 1) path (match-string 2))
  11465. (throw 'match t)))
  11466. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11467. (setq type "tree-match"
  11468. path (match-string 1))
  11469. (throw 'match t))
  11470. (save-excursion
  11471. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11472. (setq type "tags"
  11473. path (match-string 1))
  11474. (while (string-match ":" path)
  11475. (setq path (replace-match "+" t t path)))
  11476. (throw 'match t))))
  11477. (unless path
  11478. (error "No link found"))
  11479. ;; Remove any trailing spaces in path
  11480. (if (string-match " +\\'" path)
  11481. (setq path (replace-match "" t t path)))
  11482. (cond
  11483. ((assoc type org-link-protocols)
  11484. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11485. ((equal type "mailto")
  11486. (let ((cmd (car org-link-mailto-program))
  11487. (args (cdr org-link-mailto-program)) args1
  11488. (address path) (subject "") a)
  11489. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11490. (setq address (match-string 1 path)
  11491. subject (org-link-escape (match-string 2 path))))
  11492. (while args
  11493. (cond
  11494. ((not (stringp (car args))) (push (pop args) args1))
  11495. (t (setq a (pop args))
  11496. (if (string-match "%a" a)
  11497. (setq a (replace-match address t t a)))
  11498. (if (string-match "%s" a)
  11499. (setq a (replace-match subject t t a)))
  11500. (push a args1))))
  11501. (apply cmd (nreverse args1))))
  11502. ((member type '("http" "https" "ftp" "news"))
  11503. (browse-url (concat type ":" (org-link-escape
  11504. path org-link-escape-chars-browser))))
  11505. ((member type '("message"))
  11506. (browse-url (concat type ":" path)))
  11507. ((string= type "tags")
  11508. (org-tags-view in-emacs path))
  11509. ((string= type "thisfile")
  11510. (if in-emacs
  11511. (switch-to-buffer-other-window
  11512. (org-get-buffer-for-internal-link (current-buffer)))
  11513. (org-mark-ring-push))
  11514. (let ((cmd `(org-link-search
  11515. ,path
  11516. ,(cond ((equal in-emacs '(4)) 'occur)
  11517. ((equal in-emacs '(16)) 'org-occur)
  11518. (t nil))
  11519. ,pos)))
  11520. (condition-case nil (eval cmd)
  11521. (error (progn (widen) (eval cmd))))))
  11522. ((string= type "tree-match")
  11523. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11524. ((string= type "file")
  11525. (if (string-match "::\\([0-9]+\\)\\'" path)
  11526. (setq line (string-to-number (match-string 1 path))
  11527. path (substring path 0 (match-beginning 0)))
  11528. (if (string-match "::\\(.+\\)\\'" path)
  11529. (setq search (match-string 1 path)
  11530. path (substring path 0 (match-beginning 0)))))
  11531. (if (string-match "[*?{]" (file-name-nondirectory path))
  11532. (dired path)
  11533. (org-open-file path in-emacs line search)))
  11534. ((string= type "news")
  11535. (org-follow-gnus-link path))
  11536. ((string= type "bbdb")
  11537. (org-follow-bbdb-link path))
  11538. ((string= type "info")
  11539. (org-follow-info-link path))
  11540. ((string= type "gnus")
  11541. (let (group article)
  11542. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11543. (error "Error in Gnus link"))
  11544. (setq group (match-string 1 path)
  11545. article (match-string 3 path))
  11546. (org-follow-gnus-link group article)))
  11547. ((string= type "vm")
  11548. (let (folder article)
  11549. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11550. (error "Error in VM link"))
  11551. (setq folder (match-string 1 path)
  11552. article (match-string 3 path))
  11553. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11554. (org-follow-vm-link folder article in-emacs)))
  11555. ((string= type "wl")
  11556. (let (folder article)
  11557. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11558. (error "Error in Wanderlust link"))
  11559. (setq folder (match-string 1 path)
  11560. article (match-string 3 path))
  11561. (org-follow-wl-link folder article)))
  11562. ((string= type "mhe")
  11563. (let (folder article)
  11564. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11565. (error "Error in MHE link"))
  11566. (setq folder (match-string 1 path)
  11567. article (match-string 3 path))
  11568. (org-follow-mhe-link folder article)))
  11569. ((string= type "rmail")
  11570. (let (folder article)
  11571. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11572. (error "Error in RMAIL link"))
  11573. (setq folder (match-string 1 path)
  11574. article (match-string 3 path))
  11575. (org-follow-rmail-link folder article)))
  11576. ((string= type "shell")
  11577. (let ((cmd path))
  11578. (if (or (not org-confirm-shell-link-function)
  11579. (funcall org-confirm-shell-link-function
  11580. (format "Execute \"%s\" in shell? "
  11581. (org-add-props cmd nil
  11582. 'face 'org-warning))))
  11583. (progn
  11584. (message "Executing %s" cmd)
  11585. (shell-command cmd))
  11586. (error "Abort"))))
  11587. ((string= type "elisp")
  11588. (let ((cmd path))
  11589. (if (or (not org-confirm-elisp-link-function)
  11590. (funcall org-confirm-elisp-link-function
  11591. (format "Execute \"%s\" as elisp? "
  11592. (org-add-props cmd nil
  11593. 'face 'org-warning))))
  11594. (message "%s => %s" cmd (eval (read cmd)))
  11595. (error "Abort"))))
  11596. (t
  11597. (browse-url-at-point)))))
  11598. (move-marker org-open-link-marker nil)))
  11599. ;;; File search
  11600. (defvar org-create-file-search-functions nil
  11601. "List of functions to construct the right search string for a file link.
  11602. These functions are called in turn with point at the location to
  11603. which the link should point.
  11604. A function in the hook should first test if it would like to
  11605. handle this file type, for example by checking the major-mode or
  11606. the file extension. If it decides not to handle this file, it
  11607. should just return nil to give other functions a chance. If it
  11608. does handle the file, it must return the search string to be used
  11609. when following the link. The search string will be part of the
  11610. file link, given after a double colon, and `org-open-at-point'
  11611. will automatically search for it. If special measures must be
  11612. taken to make the search successful, another function should be
  11613. added to the companion hook `org-execute-file-search-functions',
  11614. which see.
  11615. A function in this hook may also use `setq' to set the variable
  11616. `description' to provide a suggestion for the descriptive text to
  11617. be used for this link when it gets inserted into an Org-mode
  11618. buffer with \\[org-insert-link].")
  11619. (defvar org-execute-file-search-functions nil
  11620. "List of functions to execute a file search triggered by a link.
  11621. Functions added to this hook must accept a single argument, the
  11622. search string that was part of the file link, the part after the
  11623. double colon. The function must first check if it would like to
  11624. handle this search, for example by checking the major-mode or the
  11625. file extension. If it decides not to handle this search, it
  11626. should just return nil to give other functions a chance. If it
  11627. does handle the search, it must return a non-nil value to keep
  11628. other functions from trying.
  11629. Each function can access the current prefix argument through the
  11630. variable `current-prefix-argument'. Note that a single prefix is
  11631. used to force opening a link in Emacs, so it may be good to only
  11632. use a numeric or double prefix to guide the search function.
  11633. In case this is needed, a function in this hook can also restore
  11634. the window configuration before `org-open-at-point' was called using:
  11635. (set-window-configuration org-window-config-before-follow-link)")
  11636. (defun org-link-search (s &optional type avoid-pos)
  11637. "Search for a link search option.
  11638. If S is surrounded by forward slashes, it is interpreted as a
  11639. regular expression. In org-mode files, this will create an `org-occur'
  11640. sparse tree. In ordinary files, `occur' will be used to list matches.
  11641. If the current buffer is in `dired-mode', grep will be used to search
  11642. in all files. If AVOID-POS is given, ignore matches near that position."
  11643. (let ((case-fold-search t)
  11644. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11645. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11646. (append '(("") (" ") ("\t") ("\n"))
  11647. org-emphasis-alist)
  11648. "\\|") "\\)"))
  11649. (pos (point))
  11650. (pre "") (post "")
  11651. words re0 re1 re2 re3 re4 re5 re2a reall)
  11652. (cond
  11653. ;; First check if there are any special
  11654. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11655. ;; Now try the builtin stuff
  11656. ((save-excursion
  11657. (goto-char (point-min))
  11658. (and
  11659. (re-search-forward
  11660. (concat "<<" (regexp-quote s0) ">>") nil t)
  11661. (setq pos (match-beginning 0))))
  11662. ;; There is an exact target for this
  11663. (goto-char pos))
  11664. ((string-match "^/\\(.*\\)/$" s)
  11665. ;; A regular expression
  11666. (cond
  11667. ((org-mode-p)
  11668. (org-occur (match-string 1 s)))
  11669. ;;((eq major-mode 'dired-mode)
  11670. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11671. (t (org-do-occur (match-string 1 s)))))
  11672. (t
  11673. ;; A normal search strings
  11674. (when (equal (string-to-char s) ?*)
  11675. ;; Anchor on headlines, post may include tags.
  11676. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11677. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11678. s (substring s 1)))
  11679. (remove-text-properties
  11680. 0 (length s)
  11681. '(face nil mouse-face nil keymap nil fontified nil) s)
  11682. ;; Make a series of regular expressions to find a match
  11683. (setq words (org-split-string s "[ \n\r\t]+")
  11684. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11685. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11686. "\\)" markers)
  11687. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11688. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11689. re1 (concat pre re2 post)
  11690. re3 (concat pre re4 post)
  11691. re5 (concat pre ".*" re4)
  11692. re2 (concat pre re2)
  11693. re2a (concat pre re2a)
  11694. re4 (concat pre re4)
  11695. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11696. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11697. re5 "\\)"
  11698. ))
  11699. (cond
  11700. ((eq type 'org-occur) (org-occur reall))
  11701. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11702. (t (goto-char (point-min))
  11703. (if (or (org-search-not-self 1 re0 nil t)
  11704. (org-search-not-self 1 re1 nil t)
  11705. (org-search-not-self 1 re2 nil t)
  11706. (org-search-not-self 1 re2a nil t)
  11707. (org-search-not-self 1 re3 nil t)
  11708. (org-search-not-self 1 re4 nil t)
  11709. (org-search-not-self 1 re5 nil t)
  11710. )
  11711. (goto-char (match-beginning 1))
  11712. (goto-char pos)
  11713. (error "No match")))))
  11714. (t
  11715. ;; Normal string-search
  11716. (goto-char (point-min))
  11717. (if (search-forward s nil t)
  11718. (goto-char (match-beginning 0))
  11719. (error "No match"))))
  11720. (and (org-mode-p) (org-show-context 'link-search))))
  11721. (defun org-search-not-self (group &rest args)
  11722. "Execute `re-search-forward', but only accept matches that do not
  11723. enclose the position of `org-open-link-marker'."
  11724. (let ((m org-open-link-marker))
  11725. (catch 'exit
  11726. (while (apply 're-search-forward args)
  11727. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11728. (goto-char (match-end group))
  11729. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11730. (> (match-beginning 0) (marker-position m))
  11731. (< (match-end 0) (marker-position m)))
  11732. (save-match-data
  11733. (or (not (org-in-regexp
  11734. org-bracket-link-analytic-regexp 1))
  11735. (not (match-end 4)) ; no description
  11736. (and (<= (match-beginning 4) (point))
  11737. (>= (match-end 4) (point))))))
  11738. (throw 'exit (point))))))))
  11739. (defun org-get-buffer-for-internal-link (buffer)
  11740. "Return a buffer to be used for displaying the link target of internal links."
  11741. (cond
  11742. ((not org-display-internal-link-with-indirect-buffer)
  11743. buffer)
  11744. ((string-match "(Clone)$" (buffer-name buffer))
  11745. (message "Buffer is already a clone, not making another one")
  11746. ;; we also do not modify visibility in this case
  11747. buffer)
  11748. (t ; make a new indirect buffer for displaying the link
  11749. (let* ((bn (buffer-name buffer))
  11750. (ibn (concat bn "(Clone)"))
  11751. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11752. (with-current-buffer ib (org-overview))
  11753. ib))))
  11754. (defun org-do-occur (regexp &optional cleanup)
  11755. "Call the Emacs command `occur'.
  11756. If CLEANUP is non-nil, remove the printout of the regular expression
  11757. in the *Occur* buffer. This is useful if the regex is long and not useful
  11758. to read."
  11759. (occur regexp)
  11760. (when cleanup
  11761. (let ((cwin (selected-window)) win beg end)
  11762. (when (setq win (get-buffer-window "*Occur*"))
  11763. (select-window win))
  11764. (goto-char (point-min))
  11765. (when (re-search-forward "match[a-z]+" nil t)
  11766. (setq beg (match-end 0))
  11767. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11768. (setq end (1- (match-beginning 0)))))
  11769. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11770. (goto-char (point-min))
  11771. (select-window cwin))))
  11772. ;;; The mark ring for links jumps
  11773. (defvar org-mark-ring nil
  11774. "Mark ring for positions before jumps in Org-mode.")
  11775. (defvar org-mark-ring-last-goto nil
  11776. "Last position in the mark ring used to go back.")
  11777. ;; Fill and close the ring
  11778. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11779. (loop for i from 1 to org-mark-ring-length do
  11780. (push (make-marker) org-mark-ring))
  11781. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11782. org-mark-ring)
  11783. (defun org-mark-ring-push (&optional pos buffer)
  11784. "Put the current position or POS into the mark ring and rotate it."
  11785. (interactive)
  11786. (setq pos (or pos (point)))
  11787. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11788. (move-marker (car org-mark-ring)
  11789. (or pos (point))
  11790. (or buffer (current-buffer)))
  11791. (message "%s"
  11792. (substitute-command-keys
  11793. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11794. (defun org-mark-ring-goto (&optional n)
  11795. "Jump to the previous position in the mark ring.
  11796. With prefix arg N, jump back that many stored positions. When
  11797. called several times in succession, walk through the entire ring.
  11798. Org-mode commands jumping to a different position in the current file,
  11799. or to another Org-mode file, automatically push the old position
  11800. onto the ring."
  11801. (interactive "p")
  11802. (let (p m)
  11803. (if (eq last-command this-command)
  11804. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11805. (setq p org-mark-ring))
  11806. (setq org-mark-ring-last-goto p)
  11807. (setq m (car p))
  11808. (switch-to-buffer (marker-buffer m))
  11809. (goto-char m)
  11810. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11811. (defun org-remove-angle-brackets (s)
  11812. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11813. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11814. s)
  11815. (defun org-add-angle-brackets (s)
  11816. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11817. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11818. s)
  11819. ;;; Following specific links
  11820. (defun org-follow-timestamp-link ()
  11821. (cond
  11822. ((org-at-date-range-p t)
  11823. (let ((org-agenda-start-on-weekday)
  11824. (t1 (match-string 1))
  11825. (t2 (match-string 2)))
  11826. (setq t1 (time-to-days (org-time-string-to-time t1))
  11827. t2 (time-to-days (org-time-string-to-time t2)))
  11828. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11829. ((org-at-timestamp-p t)
  11830. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11831. (substring (match-string 1) 0 10)))
  11832. 1))
  11833. (t (error "This should not happen"))))
  11834. (defun org-follow-bbdb-link (name)
  11835. "Follow a BBDB link to NAME."
  11836. (require 'bbdb)
  11837. (let ((inhibit-redisplay (not debug-on-error))
  11838. (bbdb-electric-p nil))
  11839. (catch 'exit
  11840. ;; Exact match on name
  11841. (bbdb-name (concat "\\`" name "\\'") nil)
  11842. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11843. ;; Exact match on name
  11844. (bbdb-company (concat "\\`" name "\\'") nil)
  11845. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11846. ;; Partial match on name
  11847. (bbdb-name name nil)
  11848. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11849. ;; Partial match on company
  11850. (bbdb-company name nil)
  11851. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11852. ;; General match including network address and notes
  11853. (bbdb name nil)
  11854. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11855. (delete-window (get-buffer-window "*BBDB*"))
  11856. (error "No matching BBDB record")))))
  11857. (defun org-follow-info-link (name)
  11858. "Follow an info file & node link to NAME."
  11859. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11860. (string-match "\\(.*\\)" name))
  11861. (progn
  11862. (require 'info)
  11863. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11864. (Info-find-node (match-string 1 name) (match-string 2 name))
  11865. (Info-find-node (match-string 1 name) "Top")))
  11866. (message "Could not open: %s" name)))
  11867. (defun org-follow-gnus-link (&optional group article)
  11868. "Follow a Gnus link to GROUP and ARTICLE."
  11869. (require 'gnus)
  11870. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11871. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11872. (cond ((and group article)
  11873. (gnus-group-read-group 1 nil group)
  11874. (gnus-summary-goto-article (string-to-number article) nil t))
  11875. (group (gnus-group-jump-to-group group))))
  11876. (defun org-follow-vm-link (&optional folder article readonly)
  11877. "Follow a VM link to FOLDER and ARTICLE."
  11878. (require 'vm)
  11879. (setq article (org-add-angle-brackets article))
  11880. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11881. ;; ange-ftp or efs or tramp access
  11882. (let ((user (or (match-string 1 folder) (user-login-name)))
  11883. (host (match-string 2 folder))
  11884. (file (match-string 3 folder)))
  11885. (cond
  11886. ((featurep 'tramp)
  11887. ;; use tramp to access the file
  11888. (if (featurep 'xemacs)
  11889. (setq folder (format "[%s@%s]%s" user host file))
  11890. (setq folder (format "/%s@%s:%s" user host file))))
  11891. (t
  11892. ;; use ange-ftp or efs
  11893. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11894. (setq folder (format "/%s@%s:%s" user host file))))))
  11895. (when folder
  11896. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11897. (sit-for 0.1)
  11898. (when article
  11899. (vm-select-folder-buffer)
  11900. (widen)
  11901. (let ((case-fold-search t))
  11902. (goto-char (point-min))
  11903. (if (not (re-search-forward
  11904. (concat "^" "message-id: *" (regexp-quote article))))
  11905. (error "Could not find the specified message in this folder"))
  11906. (vm-isearch-update)
  11907. (vm-isearch-narrow)
  11908. (vm-beginning-of-message)
  11909. (vm-summarize)))))
  11910. (defun org-follow-wl-link (folder article)
  11911. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11912. (if (and (string= folder "%")
  11913. article
  11914. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11915. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11916. ;; Thus, we recompose folder and article ids.
  11917. (setq folder (format "%s#%s" folder (match-string 1 article))
  11918. article (match-string 3 article)))
  11919. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11920. (error "No such folder: %s" folder))
  11921. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11922. (and article
  11923. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11924. (wl-summary-redisplay)))
  11925. (defun org-follow-rmail-link (folder article)
  11926. "Follow an RMAIL link to FOLDER and ARTICLE."
  11927. (setq article (org-add-angle-brackets article))
  11928. (let (message-number)
  11929. (save-excursion
  11930. (save-window-excursion
  11931. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11932. (setq message-number
  11933. (save-restriction
  11934. (widen)
  11935. (goto-char (point-max))
  11936. (if (re-search-backward
  11937. (concat "^Message-ID:\\s-+" (regexp-quote
  11938. (or article "")))
  11939. nil t)
  11940. (rmail-what-message))))))
  11941. (if message-number
  11942. (progn
  11943. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11944. (rmail-show-message message-number)
  11945. message-number)
  11946. (error "Message not found"))))
  11947. ;;; mh-e integration based on planner-mode
  11948. (defun org-mhe-get-message-real-folder ()
  11949. "Return the name of the current message real folder, so if you use
  11950. sequences, it will now work."
  11951. (save-excursion
  11952. (let* ((folder
  11953. (if (equal major-mode 'mh-folder-mode)
  11954. mh-current-folder
  11955. ;; Refer to the show buffer
  11956. mh-show-folder-buffer))
  11957. (end-index
  11958. (if (boundp 'mh-index-folder)
  11959. (min (length mh-index-folder) (length folder))))
  11960. )
  11961. ;; a simple test on mh-index-data does not work, because
  11962. ;; mh-index-data is always nil in a show buffer.
  11963. (if (and (boundp 'mh-index-folder)
  11964. (string= mh-index-folder (substring folder 0 end-index)))
  11965. (if (equal major-mode 'mh-show-mode)
  11966. (save-window-excursion
  11967. (let (pop-up-frames)
  11968. (when (buffer-live-p (get-buffer folder))
  11969. (progn
  11970. (pop-to-buffer folder)
  11971. (org-mhe-get-message-folder-from-index)
  11972. )
  11973. )))
  11974. (org-mhe-get-message-folder-from-index)
  11975. )
  11976. folder
  11977. )
  11978. )))
  11979. (defun org-mhe-get-message-folder-from-index ()
  11980. "Returns the name of the message folder in a index folder buffer."
  11981. (save-excursion
  11982. (mh-index-previous-folder)
  11983. (re-search-forward "^\\(+.*\\)$" nil t)
  11984. (message "%s" (match-string 1))))
  11985. (defun org-mhe-get-message-folder ()
  11986. "Return the name of the current message folder. Be careful if you
  11987. use sequences."
  11988. (save-excursion
  11989. (if (equal major-mode 'mh-folder-mode)
  11990. mh-current-folder
  11991. ;; Refer to the show buffer
  11992. mh-show-folder-buffer)))
  11993. (defun org-mhe-get-message-num ()
  11994. "Return the number of the current message. Be careful if you
  11995. use sequences."
  11996. (save-excursion
  11997. (if (equal major-mode 'mh-folder-mode)
  11998. (mh-get-msg-num nil)
  11999. ;; Refer to the show buffer
  12000. (mh-show-buffer-message-number))))
  12001. (defun org-mhe-get-header (header)
  12002. "Return a header of the message in folder mode. This will create a
  12003. show buffer for the corresponding message. If you have a more clever
  12004. idea..."
  12005. (let* ((folder (org-mhe-get-message-folder))
  12006. (num (org-mhe-get-message-num))
  12007. (buffer (get-buffer-create (concat "show-" folder)))
  12008. (header-field))
  12009. (with-current-buffer buffer
  12010. (mh-display-msg num folder)
  12011. (if (equal major-mode 'mh-folder-mode)
  12012. (mh-header-display)
  12013. (mh-show-header-display))
  12014. (set-buffer buffer)
  12015. (setq header-field (mh-get-header-field header))
  12016. (if (equal major-mode 'mh-folder-mode)
  12017. (mh-show)
  12018. (mh-show-show))
  12019. header-field)))
  12020. (defun org-follow-mhe-link (folder article)
  12021. "Follow an MHE link to FOLDER and ARTICLE.
  12022. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12023. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12024. ARTICLE is searched in all folders. Indexed searches (swish++,
  12025. namazu, and others supported by MH-E) will always search in all
  12026. folders."
  12027. (require 'mh-e)
  12028. (require 'mh-search)
  12029. (require 'mh-utils)
  12030. (mh-find-path)
  12031. (if (not article)
  12032. (mh-visit-folder (mh-normalize-folder-name folder))
  12033. (setq article (org-add-angle-brackets article))
  12034. (mh-search-choose)
  12035. (if (equal mh-searcher 'pick)
  12036. (progn
  12037. (mh-search folder (list "--message-id" article))
  12038. (when (and org-mhe-search-all-folders
  12039. (not (org-mhe-get-message-real-folder)))
  12040. (kill-this-buffer)
  12041. (mh-search "+" (list "--message-id" article))))
  12042. (mh-search "+" article))
  12043. (if (org-mhe-get-message-real-folder)
  12044. (mh-show-msg 1)
  12045. (kill-this-buffer)
  12046. (error "Message not found"))))
  12047. ;;; BibTeX links
  12048. ;; Use the custom search meachnism to construct and use search strings for
  12049. ;; file links to BibTeX database entries.
  12050. (defun org-create-file-search-in-bibtex ()
  12051. "Create the search string and description for a BibTeX database entry."
  12052. (when (eq major-mode 'bibtex-mode)
  12053. ;; yes, we want to construct this search string.
  12054. ;; Make a good description for this entry, using names, year and the title
  12055. ;; Put it into the `description' variable which is dynamically scoped.
  12056. (let ((bibtex-autokey-names 1)
  12057. (bibtex-autokey-names-stretch 1)
  12058. (bibtex-autokey-name-case-convert-function 'identity)
  12059. (bibtex-autokey-name-separator " & ")
  12060. (bibtex-autokey-additional-names " et al.")
  12061. (bibtex-autokey-year-length 4)
  12062. (bibtex-autokey-name-year-separator " ")
  12063. (bibtex-autokey-titlewords 3)
  12064. (bibtex-autokey-titleword-separator " ")
  12065. (bibtex-autokey-titleword-case-convert-function 'identity)
  12066. (bibtex-autokey-titleword-length 'infty)
  12067. (bibtex-autokey-year-title-separator ": "))
  12068. (setq description (bibtex-generate-autokey)))
  12069. ;; Now parse the entry, get the key and return it.
  12070. (save-excursion
  12071. (bibtex-beginning-of-entry)
  12072. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12073. (defun org-execute-file-search-in-bibtex (s)
  12074. "Find the link search string S as a key for a database entry."
  12075. (when (eq major-mode 'bibtex-mode)
  12076. ;; Yes, we want to do the search in this file.
  12077. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12078. (goto-char (point-min))
  12079. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12080. (regexp-quote s) "[ \t\n]*,") nil t)
  12081. (goto-char (match-beginning 0)))
  12082. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12083. ;; Use double prefix to indicate that any web link should be browsed
  12084. (let ((b (current-buffer)) (p (point)))
  12085. ;; Restore the window configuration because we just use the web link
  12086. (set-window-configuration org-window-config-before-follow-link)
  12087. (save-excursion (set-buffer b) (goto-char p)
  12088. (bibtex-url)))
  12089. (recenter 0)) ; Move entry start to beginning of window
  12090. ;; return t to indicate that the search is done.
  12091. t))
  12092. ;; Finally add the functions to the right hooks.
  12093. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12094. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12095. ;; end of Bibtex link setup
  12096. ;;; Following file links
  12097. (defun org-open-file (path &optional in-emacs line search)
  12098. "Open the file at PATH.
  12099. First, this expands any special file name abbreviations. Then the
  12100. configuration variable `org-file-apps' is checked if it contains an
  12101. entry for this file type, and if yes, the corresponding command is launched.
  12102. If no application is found, Emacs simply visits the file.
  12103. With optional argument IN-EMACS, Emacs will visit the file.
  12104. Optional LINE specifies a line to go to, optional SEARCH a string to
  12105. search for. If LINE or SEARCH is given, the file will always be
  12106. opened in Emacs.
  12107. If the file does not exist, an error is thrown."
  12108. (setq in-emacs (or in-emacs line search))
  12109. (let* ((file (if (equal path "")
  12110. buffer-file-name
  12111. (substitute-in-file-name (expand-file-name path))))
  12112. (apps (append org-file-apps (org-default-apps)))
  12113. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12114. (dirp (if remp nil (file-directory-p file)))
  12115. (dfile (downcase file))
  12116. (old-buffer (current-buffer))
  12117. (old-pos (point))
  12118. (old-mode major-mode)
  12119. ext cmd)
  12120. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12121. (setq ext (match-string 1 dfile))
  12122. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12123. (setq ext (match-string 1 dfile))))
  12124. (if in-emacs
  12125. (setq cmd 'emacs)
  12126. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12127. (and dirp (cdr (assoc 'directory apps)))
  12128. (cdr (assoc ext apps))
  12129. (cdr (assoc t apps)))))
  12130. (when (eq cmd 'mailcap)
  12131. (require 'mailcap)
  12132. (mailcap-parse-mailcaps)
  12133. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12134. (command (mailcap-mime-info mime-type)))
  12135. (if (stringp command)
  12136. (setq cmd command)
  12137. (setq cmd 'emacs))))
  12138. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12139. (not (file-exists-p file))
  12140. (not org-open-non-existing-files))
  12141. (error "No such file: %s" file))
  12142. (cond
  12143. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12144. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12145. (while (string-match "['\"]%s['\"]" cmd)
  12146. (setq cmd (replace-match "%s" t t cmd)))
  12147. (while (string-match "%s" cmd)
  12148. (setq cmd (replace-match
  12149. (save-match-data (shell-quote-argument file))
  12150. t t cmd)))
  12151. (save-window-excursion
  12152. (start-process-shell-command cmd nil cmd)))
  12153. ((or (stringp cmd)
  12154. (eq cmd 'emacs))
  12155. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12156. (widen)
  12157. (if line (goto-line line)
  12158. (if search (org-link-search search))))
  12159. ((consp cmd)
  12160. (eval cmd))
  12161. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12162. (and (org-mode-p) (eq old-mode 'org-mode)
  12163. (or (not (equal old-buffer (current-buffer)))
  12164. (not (equal old-pos (point))))
  12165. (org-mark-ring-push old-pos old-buffer))))
  12166. (defun org-default-apps ()
  12167. "Return the default applications for this operating system."
  12168. (cond
  12169. ((eq system-type 'darwin)
  12170. org-file-apps-defaults-macosx)
  12171. ((eq system-type 'windows-nt)
  12172. org-file-apps-defaults-windowsnt)
  12173. (t org-file-apps-defaults-gnu)))
  12174. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12175. (defun org-file-remote-p (file)
  12176. "Test whether FILE specifies a location on a remote system.
  12177. Return non-nil if the location is indeed remote.
  12178. For example, the filename \"/user@host:/foo\" specifies a location
  12179. on the system \"/user@host:\"."
  12180. (cond ((fboundp 'file-remote-p)
  12181. (file-remote-p file))
  12182. ((fboundp 'tramp-handle-file-remote-p)
  12183. (tramp-handle-file-remote-p file))
  12184. ((and (boundp 'ange-ftp-name-format)
  12185. (string-match (car ange-ftp-name-format) file))
  12186. t)
  12187. (t nil)))
  12188. ;;;; Hooks for remember.el, and refiling
  12189. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12190. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12191. ;;;###autoload
  12192. (defun org-remember-insinuate ()
  12193. "Setup remember.el for use wiht Org-mode."
  12194. (require 'remember)
  12195. (setq remember-annotation-functions '(org-remember-annotation))
  12196. (setq remember-handler-functions '(org-remember-handler))
  12197. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12198. ;;;###autoload
  12199. (defun org-remember-annotation ()
  12200. "Return a link to the current location as an annotation for remember.el.
  12201. If you are using Org-mode files as target for data storage with
  12202. remember.el, then the annotations should include a link compatible with the
  12203. conventions in Org-mode. This function returns such a link."
  12204. (org-store-link nil))
  12205. (defconst org-remember-help
  12206. "Select a destination location for the note.
  12207. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12208. RET on headline -> Store as sublevel entry to current headline
  12209. RET at beg-of-buf -> Append to file as level 2 headline
  12210. <left>/<right> -> before/after current headline, same headings level")
  12211. (defvar org-remember-previous-location nil)
  12212. (defvar org-force-remember-template-char) ;; dynamically scoped
  12213. (defun org-select-remember-template (&optional use-char)
  12214. (when org-remember-templates
  12215. (let* ((templates (mapcar (lambda (x)
  12216. (if (stringp (car x))
  12217. (append (list (nth 1 x) (car x)) (cddr x))
  12218. (append (list (car x) "") (cdr x))))
  12219. org-remember-templates))
  12220. (char (or use-char
  12221. (cond
  12222. ((= (length templates) 1)
  12223. (caar templates))
  12224. ((and (boundp 'org-force-remember-template-char)
  12225. org-force-remember-template-char)
  12226. (if (stringp org-force-remember-template-char)
  12227. (string-to-char org-force-remember-template-char)
  12228. org-force-remember-template-char))
  12229. (t
  12230. (message "Select template: %s"
  12231. (mapconcat
  12232. (lambda (x)
  12233. (cond
  12234. ((not (string-match "\\S-" (nth 1 x)))
  12235. (format "[%c]" (car x)))
  12236. ((equal (downcase (car x))
  12237. (downcase (aref (nth 1 x) 0)))
  12238. (format "[%c]%s" (car x)
  12239. (substring (nth 1 x) 1)))
  12240. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12241. templates " "))
  12242. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12243. (when (equal char0 ?\C-g)
  12244. (jump-to-register remember-register)
  12245. (kill-buffer remember-buffer))
  12246. char0))))))
  12247. (cddr (assoc char templates)))))
  12248. (defvar x-last-selected-text)
  12249. (defvar x-last-selected-text-primary)
  12250. ;;;###autoload
  12251. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12252. "Initialize *remember* buffer with template, invoke `org-mode'.
  12253. This function should be placed into `remember-mode-hook' and in fact requires
  12254. to be run from that hook to function properly."
  12255. (if org-remember-templates
  12256. (let* ((entry (org-select-remember-template use-char))
  12257. (tpl (car entry))
  12258. (plist-p (if org-store-link-plist t nil))
  12259. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12260. (string-match "\\S-" (nth 1 entry)))
  12261. (nth 1 entry)
  12262. org-default-notes-file))
  12263. (headline (nth 2 entry))
  12264. (v-c (or (and (eq window-system 'x)
  12265. (fboundp 'x-cut-buffer-or-selection-value)
  12266. (x-cut-buffer-or-selection-value))
  12267. (org-bound-and-true-p x-last-selected-text)
  12268. (org-bound-and-true-p x-last-selected-text-primary)
  12269. (and (> (length kill-ring) 0) (current-kill 0))))
  12270. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12271. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12272. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12273. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12274. ;; `initial' and `annotation' are bound in `remember'
  12275. (v-i (if (boundp 'initial) initial))
  12276. (v-a (if (and (boundp 'annotation) annotation)
  12277. (if (equal annotation "[[]]") "" annotation)
  12278. ""))
  12279. (v-A (if (and v-a
  12280. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12281. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12282. v-a))
  12283. (v-n user-full-name)
  12284. (org-startup-folded nil)
  12285. org-time-was-given org-end-time-was-given x
  12286. prompt completions char time pos default histvar)
  12287. (setq org-store-link-plist
  12288. (append (list :annotation v-a :initial v-i)
  12289. org-store-link-plist))
  12290. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12291. (erase-buffer)
  12292. (insert (substitute-command-keys
  12293. (format
  12294. "## Filing location: Select interactively, default, or last used:
  12295. ## %s to select file and header location interactively.
  12296. ## %s \"%s\" -> \"* %s\"
  12297. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12298. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12299. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12300. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12301. (abbreviate-file-name (or file org-default-notes-file))
  12302. (or headline "")
  12303. (or (car org-remember-previous-location) "???")
  12304. (or (cdr org-remember-previous-location) "???"))))
  12305. (insert tpl) (goto-char (point-min))
  12306. ;; Simple %-escapes
  12307. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12308. (when (and initial (equal (match-string 0) "%i"))
  12309. (save-match-data
  12310. (let* ((lead (buffer-substring
  12311. (point-at-bol) (match-beginning 0))))
  12312. (setq v-i (mapconcat 'identity
  12313. (org-split-string initial "\n")
  12314. (concat "\n" lead))))))
  12315. (replace-match
  12316. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12317. t t))
  12318. ;; %[] Insert contents of a file.
  12319. (goto-char (point-min))
  12320. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12321. (let ((start (match-beginning 0))
  12322. (end (match-end 0))
  12323. (filename (expand-file-name (match-string 1))))
  12324. (goto-char start)
  12325. (delete-region start end)
  12326. (condition-case error
  12327. (insert-file-contents filename)
  12328. (error (insert (format "%%![Couldn't insert %s: %s]"
  12329. filename error))))))
  12330. ;; %() embedded elisp
  12331. (goto-char (point-min))
  12332. (while (re-search-forward "%\\((.+)\\)" nil t)
  12333. (goto-char (match-beginning 0))
  12334. (let ((template-start (point)))
  12335. (forward-char 1)
  12336. (let ((result
  12337. (condition-case error
  12338. (eval (read (current-buffer)))
  12339. (error (format "%%![Error: %s]" error)))))
  12340. (delete-region template-start (point))
  12341. (insert result))))
  12342. ;; From the property list
  12343. (when plist-p
  12344. (goto-char (point-min))
  12345. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12346. (and (setq x (or (plist-get org-store-link-plist
  12347. (intern (match-string 1))) ""))
  12348. (replace-match x t t))))
  12349. ;; Turn on org-mode in the remember buffer, set local variables
  12350. (org-mode)
  12351. (org-set-local 'org-finish-function 'org-remember-finalize)
  12352. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12353. (org-set-local 'org-default-notes-file file))
  12354. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12355. (org-set-local 'org-remember-default-headline headline))
  12356. ;; Interactive template entries
  12357. (goto-char (point-min))
  12358. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12359. (setq char (if (match-end 3) (match-string 3))
  12360. prompt (if (match-end 2) (match-string 2)))
  12361. (goto-char (match-beginning 0))
  12362. (replace-match "")
  12363. (setq completions nil default nil)
  12364. (when prompt
  12365. (setq completions (org-split-string prompt "|")
  12366. prompt (pop completions)
  12367. default (car completions)
  12368. histvar (intern (concat
  12369. "org-remember-template-prompt-history::"
  12370. (or prompt "")))
  12371. completions (mapcar 'list completions)))
  12372. (cond
  12373. ((member char '("G" "g"))
  12374. (let* ((org-last-tags-completion-table
  12375. (org-global-tags-completion-table
  12376. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12377. (org-add-colon-after-tag-completion t)
  12378. (ins (completing-read
  12379. (if prompt (concat prompt ": ") "Tags: ")
  12380. 'org-tags-completion-function nil nil nil
  12381. 'org-tags-history)))
  12382. (setq ins (mapconcat 'identity
  12383. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12384. ":"))
  12385. (when (string-match "\\S-" ins)
  12386. (or (equal (char-before) ?:) (insert ":"))
  12387. (insert ins)
  12388. (or (equal (char-after) ?:) (insert ":")))))
  12389. (char
  12390. (setq org-time-was-given (equal (upcase char) char))
  12391. (setq time (org-read-date (equal (upcase char) "U") t nil
  12392. prompt))
  12393. (org-insert-time-stamp time org-time-was-given
  12394. (member char '("u" "U"))
  12395. nil nil (list org-end-time-was-given)))
  12396. (t
  12397. (insert (org-completing-read
  12398. (concat (if prompt prompt "Enter string")
  12399. (if default (concat " [" default "]"))
  12400. ": ")
  12401. completions nil nil nil histvar default)))))
  12402. (goto-char (point-min))
  12403. (if (re-search-forward "%\\?" nil t)
  12404. (replace-match "")
  12405. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12406. (org-mode)
  12407. (org-set-local 'org-finish-function 'org-remember-finalize))
  12408. (when (save-excursion
  12409. (goto-char (point-min))
  12410. (re-search-forward "%!" nil t))
  12411. (replace-match "")
  12412. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12413. (defun org-remember-finish-immediately ()
  12414. "File remember note immediately.
  12415. This should be run in `post-command-hook' and will remove itself
  12416. from that hook."
  12417. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12418. (when org-finish-function
  12419. (funcall org-finish-function)))
  12420. (defvar org-clock-marker) ; Defined below
  12421. (defun org-remember-finalize ()
  12422. "Finalize the remember process."
  12423. (unless (fboundp 'remember-finalize)
  12424. (defalias 'remember-finalize 'remember-buffer))
  12425. (when (and org-clock-marker
  12426. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12427. ;; FIXME: test this, this is w/o notetaking!
  12428. (let (org-log-done) (org-clock-out)))
  12429. (when buffer-file-name
  12430. (save-buffer)
  12431. (setq buffer-file-name nil))
  12432. (remember-finalize))
  12433. ;;;###autoload
  12434. (defun org-remember (&optional goto org-force-remember-template-char)
  12435. "Call `remember'. If this is already a remember buffer, re-apply template.
  12436. If there is an active region, make sure remember uses it as initial content
  12437. of the remember buffer.
  12438. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12439. anything, just go to the file/headline where the selected template usually
  12440. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12441. note stored by remember.
  12442. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12443. associated with a template in `org-remember-templates'."
  12444. (interactive "P")
  12445. (cond
  12446. ((equal goto '(4)) (org-go-to-remember-target))
  12447. ((equal goto '(16)) (org-remember-goto-last-stored))
  12448. (t
  12449. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12450. (progn
  12451. (when (< (length org-remember-templates) 2)
  12452. (error "No other template available"))
  12453. (erase-buffer)
  12454. (let ((annotation (plist-get org-store-link-plist :annotation))
  12455. (initial (plist-get org-store-link-plist :initial)))
  12456. (org-remember-apply-template))
  12457. (message "Press C-c C-c to remember data"))
  12458. (if (org-region-active-p)
  12459. (remember (buffer-substring (point) (mark)))
  12460. (call-interactively 'remember))))))
  12461. (defun org-remember-goto-last-stored ()
  12462. "Go to the location where the last remember note was stored."
  12463. (interactive)
  12464. (bookmark-jump "org-remember-last-stored")
  12465. (message "This is the last note stored by remember"))
  12466. (defun org-go-to-remember-target (&optional template-key)
  12467. "Go to the target location of a remember template.
  12468. The user is queried for the template."
  12469. (interactive)
  12470. (let* ((entry (org-select-remember-template template-key))
  12471. (file (nth 1 entry))
  12472. (heading (nth 2 entry))
  12473. visiting)
  12474. (unless (and file (stringp file) (string-match "\\S-" file))
  12475. (setq file org-default-notes-file))
  12476. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12477. (setq heading org-remember-default-headline))
  12478. (setq visiting (org-find-base-buffer-visiting file))
  12479. (if (not visiting) (find-file-noselect file))
  12480. (switch-to-buffer (or visiting (get-file-buffer file)))
  12481. (widen)
  12482. (goto-char (point-min))
  12483. (if (re-search-forward
  12484. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12485. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12486. nil t)
  12487. (goto-char (match-beginning 0))
  12488. (error "Target headline not found: %s" heading))))
  12489. (defvar org-note-abort nil) ; dynamically scoped
  12490. ;;;###autoload
  12491. (defun org-remember-handler ()
  12492. "Store stuff from remember.el into an org file.
  12493. First prompts for an org file. If the user just presses return, the value
  12494. of `org-default-notes-file' is used.
  12495. Then the command offers the headings tree of the selected file in order to
  12496. file the text at a specific location.
  12497. You can either immediately press RET to get the note appended to the
  12498. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12499. find a better place. Then press RET or <left> or <right> in insert the note.
  12500. Key Cursor position Note gets inserted
  12501. -----------------------------------------------------------------------------
  12502. RET buffer-start as level 1 heading at end of file
  12503. RET on headline as sublevel of the heading at cursor
  12504. RET no heading at cursor position, level taken from context.
  12505. Or use prefix arg to specify level manually.
  12506. <left> on headline as same level, before current heading
  12507. <right> on headline as same level, after current heading
  12508. So the fastest way to store the note is to press RET RET to append it to
  12509. the default file. This way your current train of thought is not
  12510. interrupted, in accordance with the principles of remember.el.
  12511. You can also get the fast execution without prompting by using
  12512. C-u C-c C-c to exit the remember buffer. See also the variable
  12513. `org-remember-store-without-prompt'.
  12514. Before being stored away, the function ensures that the text has a
  12515. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12516. is constructed from the current date and some additional data.
  12517. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12518. also indented so that it starts in the same column as the headline
  12519. \(i.e. after the stars).
  12520. See also the variable `org-reverse-note-order'."
  12521. (goto-char (point-min))
  12522. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12523. (replace-match ""))
  12524. (goto-char (point-max))
  12525. (beginning-of-line 1)
  12526. (while (looking-at "[ \t]*$\\|##.*")
  12527. (delete-region (1- (point)) (point-max))
  12528. (beginning-of-line 1))
  12529. (catch 'quit
  12530. (if org-note-abort (throw 'quit nil))
  12531. (let* ((txt (buffer-substring (point-min) (point-max)))
  12532. (fastp (org-xor (equal current-prefix-arg '(4))
  12533. org-remember-store-without-prompt))
  12534. (file (cond
  12535. (fastp org-default-notes-file)
  12536. ((and (eq org-remember-interactive-interface 'refile)
  12537. org-refile-targets)
  12538. org-default-notes-file)
  12539. ((not (and (equal current-prefix-arg '(16))
  12540. org-remember-previous-location))
  12541. (org-get-org-file))))
  12542. (heading org-remember-default-headline)
  12543. (visiting (and file (org-find-base-buffer-visiting file)))
  12544. (org-startup-folded nil)
  12545. (org-startup-align-all-tables nil)
  12546. (org-goto-start-pos 1)
  12547. spos exitcmd level indent reversed)
  12548. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12549. (setq file (car org-remember-previous-location)
  12550. heading (cdr org-remember-previous-location)
  12551. fastp t))
  12552. (setq current-prefix-arg nil)
  12553. (if (string-match "[ \t\n]+\\'" txt)
  12554. (setq txt (replace-match "" t t txt)))
  12555. ;; Modify text so that it becomes a nice subtree which can be inserted
  12556. ;; into an org tree.
  12557. (let* ((lines (split-string txt "\n"))
  12558. first)
  12559. (setq first (car lines) lines (cdr lines))
  12560. (if (string-match "^\\*+ " first)
  12561. ;; Is already a headline
  12562. (setq indent nil)
  12563. ;; We need to add a headline: Use time and first buffer line
  12564. (setq lines (cons first lines)
  12565. first (concat "* " (current-time-string)
  12566. " (" (remember-buffer-desc) ")")
  12567. indent " "))
  12568. (if (and org-adapt-indentation indent)
  12569. (setq lines (mapcar
  12570. (lambda (x)
  12571. (if (string-match "\\S-" x)
  12572. (concat indent x) x))
  12573. lines)))
  12574. (setq txt (concat first "\n"
  12575. (mapconcat 'identity lines "\n"))))
  12576. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12577. (setq txt (replace-match "\n\n" t t txt))
  12578. (if (string-match "[ \t\n]*\\'" txt)
  12579. (setq txt (replace-match "\n" t t txt))))
  12580. ;; Put the modified text back into the remember buffer, for refile.
  12581. (erase-buffer)
  12582. (insert txt)
  12583. (goto-char (point-min))
  12584. (when (and (eq org-remember-interactive-interface 'refile)
  12585. (not fastp))
  12586. (org-refile nil (or visiting (find-file-noselect file)))
  12587. (throw 'quit t))
  12588. ;; Find the file
  12589. (if (not visiting) (find-file-noselect file))
  12590. (with-current-buffer (or visiting (get-file-buffer file))
  12591. (unless (org-mode-p)
  12592. (error "Target files for remember notes must be in Org-mode"))
  12593. (save-excursion
  12594. (save-restriction
  12595. (widen)
  12596. (and (goto-char (point-min))
  12597. (not (re-search-forward "^\\* " nil t))
  12598. (insert "\n* " (or heading "Notes") "\n"))
  12599. (setq reversed (org-notes-order-reversed-p))
  12600. ;; Find the default location
  12601. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12602. (goto-char (point-min))
  12603. (if (re-search-forward
  12604. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12605. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12606. nil t)
  12607. (setq org-goto-start-pos (match-beginning 0))
  12608. (when fastp
  12609. (goto-char (point-max))
  12610. (unless (bolp) (newline))
  12611. (insert "* " heading "\n")
  12612. (setq org-goto-start-pos (point-at-bol 0)))))
  12613. ;; Ask the User for a location, using the appropriate interface
  12614. (cond
  12615. (fastp (setq spos org-goto-start-pos
  12616. exitcmd 'return))
  12617. ((eq org-remember-interactive-interface 'outline)
  12618. (setq spos (org-get-location (current-buffer)
  12619. org-remember-help)
  12620. exitcmd (cdr spos)
  12621. spos (car spos)))
  12622. ((eq org-remember-interactive-interface 'outline-path-completion)
  12623. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12624. (org-refile-use-outline-path t))
  12625. (setq spos (org-refile-get-location "Heading: ")
  12626. exitcmd 'return
  12627. spos (nth 3 spos))))
  12628. (t (error "this should not hapen")))
  12629. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12630. ; not handle this note
  12631. (goto-char spos)
  12632. (cond ((org-on-heading-p t)
  12633. (org-back-to-heading t)
  12634. (setq level (funcall outline-level))
  12635. (cond
  12636. ((eq exitcmd 'return)
  12637. ;; sublevel of current
  12638. (setq org-remember-previous-location
  12639. (cons (abbreviate-file-name file)
  12640. (org-get-heading 'notags)))
  12641. (if reversed
  12642. (outline-next-heading)
  12643. (org-end-of-subtree t)
  12644. (if (not (bolp))
  12645. (if (looking-at "[ \t]*\n")
  12646. (beginning-of-line 2)
  12647. (end-of-line 1)
  12648. (insert "\n"))))
  12649. (bookmark-set "org-remember-last-stored")
  12650. (org-paste-subtree (org-get-legal-level level 1) txt))
  12651. ((eq exitcmd 'left)
  12652. ;; before current
  12653. (bookmark-set "org-remember-last-stored")
  12654. (org-paste-subtree level txt))
  12655. ((eq exitcmd 'right)
  12656. ;; after current
  12657. (org-end-of-subtree t)
  12658. (bookmark-set "org-remember-last-stored")
  12659. (org-paste-subtree level txt))
  12660. (t (error "This should not happen"))))
  12661. ((and (bobp) (not reversed))
  12662. ;; Put it at the end, one level below level 1
  12663. (save-restriction
  12664. (widen)
  12665. (goto-char (point-max))
  12666. (if (not (bolp)) (newline))
  12667. (bookmark-set "org-remember-last-stored")
  12668. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12669. ((and (bobp) reversed)
  12670. ;; Put it at the start, as level 1
  12671. (save-restriction
  12672. (widen)
  12673. (goto-char (point-min))
  12674. (re-search-forward "^\\*+ " nil t)
  12675. (beginning-of-line 1)
  12676. (bookmark-set "org-remember-last-stored")
  12677. (org-paste-subtree 1 txt)))
  12678. (t
  12679. ;; Put it right there, with automatic level determined by
  12680. ;; org-paste-subtree or from prefix arg
  12681. (bookmark-set "org-remember-last-stored")
  12682. (org-paste-subtree
  12683. (if (numberp current-prefix-arg) current-prefix-arg)
  12684. txt)))
  12685. (when remember-save-after-remembering
  12686. (save-buffer)
  12687. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12688. t) ;; return t to indicate that we took care of this note.
  12689. (defun org-get-org-file ()
  12690. "Read a filename, with default directory `org-directory'."
  12691. (let ((default (or org-default-notes-file remember-data-file)))
  12692. (read-file-name (format "File name [%s]: " default)
  12693. (file-name-as-directory org-directory)
  12694. default)))
  12695. (defun org-notes-order-reversed-p ()
  12696. "Check if the current file should receive notes in reversed order."
  12697. (cond
  12698. ((not org-reverse-note-order) nil)
  12699. ((eq t org-reverse-note-order) t)
  12700. ((not (listp org-reverse-note-order)) nil)
  12701. (t (catch 'exit
  12702. (let ((all org-reverse-note-order)
  12703. entry)
  12704. (while (setq entry (pop all))
  12705. (if (string-match (car entry) buffer-file-name)
  12706. (throw 'exit (cdr entry))))
  12707. nil)))))
  12708. ;;; Refiling
  12709. (defvar org-refile-target-table nil
  12710. "The list of refile targets, created by `org-refile'.")
  12711. (defvar org-agenda-new-buffers nil
  12712. "Buffers created to visit agenda files.")
  12713. (defun org-get-refile-targets (&optional default-buffer)
  12714. "Produce a table with refile targets."
  12715. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12716. targets txt re files f desc descre)
  12717. (with-current-buffer (or default-buffer (current-buffer))
  12718. (while (setq entry (pop entries))
  12719. (setq files (car entry) desc (cdr entry))
  12720. (cond
  12721. ((null files) (setq files (list (current-buffer))))
  12722. ((eq files 'org-agenda-files)
  12723. (setq files (org-agenda-files 'unrestricted)))
  12724. ((and (symbolp files) (fboundp files))
  12725. (setq files (funcall files)))
  12726. ((and (symbolp files) (boundp files))
  12727. (setq files (symbol-value files))))
  12728. (if (stringp files) (setq files (list files)))
  12729. (cond
  12730. ((eq (car desc) :tag)
  12731. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12732. ((eq (car desc) :todo)
  12733. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12734. ((eq (car desc) :regexp)
  12735. (setq descre (cdr desc)))
  12736. ((eq (car desc) :level)
  12737. (setq descre (concat "^\\*\\{" (number-to-string
  12738. (if org-odd-levels-only
  12739. (1- (* 2 (cdr desc)))
  12740. (cdr desc)))
  12741. "\\}[ \t]")))
  12742. ((eq (car desc) :maxlevel)
  12743. (setq descre (concat "^\\*\\{1," (number-to-string
  12744. (if org-odd-levels-only
  12745. (1- (* 2 (cdr desc)))
  12746. (cdr desc)))
  12747. "\\}[ \t]")))
  12748. (t (error "Bad refiling target description %s" desc)))
  12749. (while (setq f (pop files))
  12750. (save-excursion
  12751. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12752. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12753. (save-excursion
  12754. (save-restriction
  12755. (widen)
  12756. (goto-char (point-min))
  12757. (while (re-search-forward descre nil t)
  12758. (goto-char (point-at-bol))
  12759. (when (looking-at org-complex-heading-regexp)
  12760. (setq txt (match-string 4)
  12761. re (concat "^" (regexp-quote
  12762. (buffer-substring (match-beginning 1)
  12763. (match-end 4)))))
  12764. (if (match-end 5) (setq re (concat re "[ \t]+"
  12765. (regexp-quote
  12766. (match-string 5)))))
  12767. (setq re (concat re "[ \t]*$"))
  12768. (when org-refile-use-outline-path
  12769. (setq txt (mapconcat 'identity
  12770. (append
  12771. (if (eq org-refile-use-outline-path 'file)
  12772. (list (file-name-nondirectory
  12773. (buffer-file-name (buffer-base-buffer))))
  12774. (if (eq org-refile-use-outline-path 'full-file-path)
  12775. (list (buffer-file-name (buffer-base-buffer)))))
  12776. (org-get-outline-path)
  12777. (list txt))
  12778. "/")))
  12779. (push (list txt f re (point)) targets))
  12780. (goto-char (point-at-eol))))))))
  12781. (nreverse targets))))
  12782. (defun org-get-outline-path ()
  12783. "Return the outline path to the current entry, as a list."
  12784. (let (rtn)
  12785. (save-excursion
  12786. (while (org-up-heading-safe)
  12787. (when (looking-at org-complex-heading-regexp)
  12788. (push (org-match-string-no-properties 4) rtn)))
  12789. rtn)))
  12790. (defvar org-refile-history nil
  12791. "History for refiling operations.")
  12792. (defun org-refile (&optional goto default-buffer)
  12793. "Move the entry at point to another heading.
  12794. The list of target headings is compiled using the information in
  12795. `org-refile-targets', which see. This list is created upon first use, and
  12796. you can update it by calling this command with a double prefix (`C-u C-u').
  12797. FIXME: Can we find a better way of updating?
  12798. At the target location, the entry is filed as a subitem of the target heading.
  12799. Depending on `org-reverse-note-order', the new subitem will either be the
  12800. first of the last subitem.
  12801. With prefix arg GOTO, the command will only visit the target location,
  12802. not actually move anything.
  12803. With a double prefix `C-c C-c', go to the location where the last refiling
  12804. operation has put the subtree.
  12805. With a double prefix argument, the command can be used to jump to any
  12806. heading in the current buffer."
  12807. (interactive "P")
  12808. (let* ((cbuf (current-buffer))
  12809. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12810. (fname (and filename (file-truename filename)))
  12811. pos it nbuf file re level reversed)
  12812. (if (equal goto '(16))
  12813. (org-refile-goto-last-stored)
  12814. (when (setq it (org-refile-get-location
  12815. (if goto "Goto: " "Refile to: ") default-buffer))
  12816. (setq file (nth 1 it)
  12817. re (nth 2 it)
  12818. pos (nth 3 it))
  12819. (setq nbuf (or (find-buffer-visiting file)
  12820. (find-file-noselect file)))
  12821. (if goto
  12822. (progn
  12823. (switch-to-buffer nbuf)
  12824. (goto-char pos)
  12825. (org-show-context 'org-goto))
  12826. (org-copy-special)
  12827. (save-excursion
  12828. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12829. (find-file-noselect file))))
  12830. (setq reversed (org-notes-order-reversed-p))
  12831. (save-excursion
  12832. (save-restriction
  12833. (widen)
  12834. (goto-char pos)
  12835. (looking-at outline-regexp)
  12836. (setq level (org-get-legal-level (funcall outline-level) 1))
  12837. (goto-char (or (save-excursion
  12838. (if reversed
  12839. (outline-next-heading)
  12840. (outline-get-next-sibling)))
  12841. (point-max)))
  12842. (bookmark-set "org-refile-last-stored")
  12843. (org-paste-subtree level))))
  12844. (org-cut-special)
  12845. (message "Entry refiled to \"%s\"" (car it)))))))
  12846. (defun org-refile-goto-last-stored ()
  12847. "Go to the location where the last refile was stored."
  12848. (interactive)
  12849. (bookmark-jump "org-refile-last-stored")
  12850. (message "This is the location of the last refile"))
  12851. (defun org-refile-get-location (&optional prompt default-buffer)
  12852. "Prompt the user for a refile location, using PROMPT."
  12853. (let ((org-refile-targets org-refile-targets)
  12854. (org-refile-use-outline-path org-refile-use-outline-path))
  12855. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12856. (unless org-refile-target-table
  12857. (error "No refile targets"))
  12858. (let* ((cbuf (current-buffer))
  12859. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12860. (fname (and filename (file-truename filename)))
  12861. (tbl (mapcar
  12862. (lambda (x)
  12863. (if (not (equal fname (file-truename (nth 1 x))))
  12864. (cons (concat (car x) " (" (file-name-nondirectory
  12865. (nth 1 x)) ")")
  12866. (cdr x))
  12867. x))
  12868. org-refile-target-table))
  12869. (completion-ignore-case t)
  12870. pos it nbuf file re level reversed)
  12871. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12872. tbl)))
  12873. ;;;; Dynamic blocks
  12874. (defun org-find-dblock (name)
  12875. "Find the first dynamic block with name NAME in the buffer.
  12876. If not found, stay at current position and return nil."
  12877. (let (pos)
  12878. (save-excursion
  12879. (goto-char (point-min))
  12880. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12881. nil t)
  12882. (match-beginning 0))))
  12883. (if pos (goto-char pos))
  12884. pos))
  12885. (defconst org-dblock-start-re
  12886. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12887. "Matches the startline of a dynamic block, with parameters.")
  12888. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12889. "Matches the end of a dyhamic block.")
  12890. (defun org-create-dblock (plist)
  12891. "Create a dynamic block section, with parameters taken from PLIST.
  12892. PLIST must containe a :name entry which is used as name of the block."
  12893. (unless (bolp) (newline))
  12894. (let ((name (plist-get plist :name)))
  12895. (insert "#+BEGIN: " name)
  12896. (while plist
  12897. (if (eq (car plist) :name)
  12898. (setq plist (cddr plist))
  12899. (insert " " (prin1-to-string (pop plist)))))
  12900. (insert "\n\n#+END:\n")
  12901. (beginning-of-line -2)))
  12902. (defun org-prepare-dblock ()
  12903. "Prepare dynamic block for refresh.
  12904. This empties the block, puts the cursor at the insert position and returns
  12905. the property list including an extra property :name with the block name."
  12906. (unless (looking-at org-dblock-start-re)
  12907. (error "Not at a dynamic block"))
  12908. (let* ((begdel (1+ (match-end 0)))
  12909. (name (org-no-properties (match-string 1)))
  12910. (params (append (list :name name)
  12911. (read (concat "(" (match-string 3) ")")))))
  12912. (unless (re-search-forward org-dblock-end-re nil t)
  12913. (error "Dynamic block not terminated"))
  12914. (delete-region begdel (match-beginning 0))
  12915. (goto-char begdel)
  12916. (open-line 1)
  12917. params))
  12918. (defun org-map-dblocks (&optional command)
  12919. "Apply COMMAND to all dynamic blocks in the current buffer.
  12920. If COMMAND is not given, use `org-update-dblock'."
  12921. (let ((cmd (or command 'org-update-dblock))
  12922. pos)
  12923. (save-excursion
  12924. (goto-char (point-min))
  12925. (while (re-search-forward org-dblock-start-re nil t)
  12926. (goto-char (setq pos (match-beginning 0)))
  12927. (condition-case nil
  12928. (funcall cmd)
  12929. (error (message "Error during update of dynamic block")))
  12930. (goto-char pos)
  12931. (unless (re-search-forward org-dblock-end-re nil t)
  12932. (error "Dynamic block not terminated"))))))
  12933. (defun org-dblock-update (&optional arg)
  12934. "User command for updating dynamic blocks.
  12935. Update the dynamic block at point. With prefix ARG, update all dynamic
  12936. blocks in the buffer."
  12937. (interactive "P")
  12938. (if arg
  12939. (org-update-all-dblocks)
  12940. (or (looking-at org-dblock-start-re)
  12941. (org-beginning-of-dblock))
  12942. (org-update-dblock)))
  12943. (defun org-update-dblock ()
  12944. "Update the dynamic block at point
  12945. This means to empty the block, parse for parameters and then call
  12946. the correct writing function."
  12947. (save-window-excursion
  12948. (let* ((pos (point))
  12949. (line (org-current-line))
  12950. (params (org-prepare-dblock))
  12951. (name (plist-get params :name))
  12952. (cmd (intern (concat "org-dblock-write:" name))))
  12953. (message "Updating dynamic block `%s' at line %d..." name line)
  12954. (funcall cmd params)
  12955. (message "Updating dynamic block `%s' at line %d...done" name line)
  12956. (goto-char pos))))
  12957. (defun org-beginning-of-dblock ()
  12958. "Find the beginning of the dynamic block at point.
  12959. Error if there is no scuh block at point."
  12960. (let ((pos (point))
  12961. beg)
  12962. (end-of-line 1)
  12963. (if (and (re-search-backward org-dblock-start-re nil t)
  12964. (setq beg (match-beginning 0))
  12965. (re-search-forward org-dblock-end-re nil t)
  12966. (> (match-end 0) pos))
  12967. (goto-char beg)
  12968. (goto-char pos)
  12969. (error "Not in a dynamic block"))))
  12970. (defun org-update-all-dblocks ()
  12971. "Update all dynamic blocks in the buffer.
  12972. This function can be used in a hook."
  12973. (when (org-mode-p)
  12974. (org-map-dblocks 'org-update-dblock)))
  12975. ;;;; Completion
  12976. (defconst org-additional-option-like-keywords
  12977. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12978. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12979. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12980. (defun org-complete (&optional arg)
  12981. "Perform completion on word at point.
  12982. At the beginning of a headline, this completes TODO keywords as given in
  12983. `org-todo-keywords'.
  12984. If the current word is preceded by a backslash, completes the TeX symbols
  12985. that are supported for HTML support.
  12986. If the current word is preceded by \"#+\", completes special words for
  12987. setting file options.
  12988. In the line after \"#+STARTUP:, complete valid keywords.\"
  12989. At all other locations, this simply calls the value of
  12990. `org-completion-fallback-command'."
  12991. (interactive "P")
  12992. (org-without-partial-completion
  12993. (catch 'exit
  12994. (let* ((end (point))
  12995. (beg1 (save-excursion
  12996. (skip-chars-backward (org-re "[:alnum:]_@"))
  12997. (point)))
  12998. (beg (save-excursion
  12999. (skip-chars-backward "a-zA-Z0-9_:$")
  13000. (point)))
  13001. (confirm (lambda (x) (stringp (car x))))
  13002. (searchhead (equal (char-before beg) ?*))
  13003. (tag (and (equal (char-before beg1) ?:)
  13004. (equal (char-after (point-at-bol)) ?*)))
  13005. (prop (and (equal (char-before beg1) ?:)
  13006. (not (equal (char-after (point-at-bol)) ?*))))
  13007. (texp (equal (char-before beg) ?\\))
  13008. (link (equal (char-before beg) ?\[))
  13009. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13010. beg)
  13011. "#+"))
  13012. (startup (string-match "^#\\+STARTUP:.*"
  13013. (buffer-substring (point-at-bol) (point))))
  13014. (completion-ignore-case opt)
  13015. (type nil)
  13016. (tbl nil)
  13017. (table (cond
  13018. (opt
  13019. (setq type :opt)
  13020. (append
  13021. (mapcar
  13022. (lambda (x)
  13023. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13024. (cons (match-string 2 x) (match-string 1 x)))
  13025. (org-split-string (org-get-current-options) "\n"))
  13026. (mapcar 'list org-additional-option-like-keywords)))
  13027. (startup
  13028. (setq type :startup)
  13029. org-startup-options)
  13030. (link (append org-link-abbrev-alist-local
  13031. org-link-abbrev-alist))
  13032. (texp
  13033. (setq type :tex)
  13034. org-html-entities)
  13035. ((string-match "\\`\\*+[ \t]+\\'"
  13036. (buffer-substring (point-at-bol) beg))
  13037. (setq type :todo)
  13038. (mapcar 'list org-todo-keywords-1))
  13039. (searchhead
  13040. (setq type :searchhead)
  13041. (save-excursion
  13042. (goto-char (point-min))
  13043. (while (re-search-forward org-todo-line-regexp nil t)
  13044. (push (list
  13045. (org-make-org-heading-search-string
  13046. (match-string 3) t))
  13047. tbl)))
  13048. tbl)
  13049. (tag (setq type :tag beg beg1)
  13050. (or org-tag-alist (org-get-buffer-tags)))
  13051. (prop (setq type :prop beg beg1)
  13052. (mapcar 'list (org-buffer-property-keys)))
  13053. (t (progn
  13054. (call-interactively org-completion-fallback-command)
  13055. (throw 'exit nil)))))
  13056. (pattern (buffer-substring-no-properties beg end))
  13057. (completion (try-completion pattern table confirm)))
  13058. (cond ((eq completion t)
  13059. (if (not (assoc (upcase pattern) table))
  13060. (message "Already complete")
  13061. (if (equal type :opt)
  13062. (insert (substring (cdr (assoc (upcase pattern) table))
  13063. (length pattern)))
  13064. (if (memq type '(:tag :prop)) (insert ":")))))
  13065. ((null completion)
  13066. (message "Can't find completion for \"%s\"" pattern)
  13067. (ding))
  13068. ((not (string= pattern completion))
  13069. (delete-region beg end)
  13070. (if (string-match " +$" completion)
  13071. (setq completion (replace-match "" t t completion)))
  13072. (insert completion)
  13073. (if (get-buffer-window "*Completions*")
  13074. (delete-window (get-buffer-window "*Completions*")))
  13075. (if (assoc completion table)
  13076. (if (eq type :todo) (insert " ")
  13077. (if (memq type '(:tag :prop)) (insert ":"))))
  13078. (if (and (equal type :opt) (assoc completion table))
  13079. (message "%s" (substitute-command-keys
  13080. "Press \\[org-complete] again to insert example settings"))))
  13081. (t
  13082. (message "Making completion list...")
  13083. (let ((list (sort (all-completions pattern table confirm)
  13084. 'string<)))
  13085. (with-output-to-temp-buffer "*Completions*"
  13086. (condition-case nil
  13087. ;; Protection needed for XEmacs and emacs 21
  13088. (display-completion-list list pattern)
  13089. (error (display-completion-list list)))))
  13090. (message "Making completion list...%s" "done")))))))
  13091. ;;;; TODO, DEADLINE, Comments
  13092. (defun org-toggle-comment ()
  13093. "Change the COMMENT state of an entry."
  13094. (interactive)
  13095. (save-excursion
  13096. (org-back-to-heading)
  13097. (let (case-fold-search)
  13098. (if (looking-at (concat outline-regexp
  13099. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13100. (replace-match "" t t nil 1)
  13101. (if (looking-at outline-regexp)
  13102. (progn
  13103. (goto-char (match-end 0))
  13104. (insert org-comment-string " ")))))))
  13105. (defvar org-last-todo-state-is-todo nil
  13106. "This is non-nil when the last TODO state change led to a TODO state.
  13107. If the last change removed the TODO tag or switched to DONE, then
  13108. this is nil.")
  13109. (defvar org-setting-tags nil) ; dynamically skiped
  13110. ;; FIXME: better place
  13111. (defun org-property-or-variable-value (var &optional inherit)
  13112. "Check if there is a property fixing the value of VAR.
  13113. If yes, return this value. If not, return the current value of the variable."
  13114. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13115. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13116. (read prop)
  13117. (symbol-value var))))
  13118. (defun org-parse-local-options (string var)
  13119. "Parse STRING for startup setting relevant for variable VAR."
  13120. (let ((rtn (symbol-value var))
  13121. e opts)
  13122. (save-match-data
  13123. (if (or (not string) (not (string-match "\\S-" string)))
  13124. rtn
  13125. (setq opts (delq nil (mapcar (lambda (x)
  13126. (setq e (assoc x org-startup-options))
  13127. (if (eq (nth 1 e) var) e nil))
  13128. (org-split-string string "[ \t]+"))))
  13129. (if (not opts)
  13130. rtn
  13131. (setq rtn nil)
  13132. (while (setq e (pop opts))
  13133. (if (not (nth 3 e))
  13134. (setq rtn (nth 2 e))
  13135. (if (not (listp rtn)) (setq rtn nil))
  13136. (push (nth 2 e) rtn)))
  13137. rtn)))))
  13138. (defvar org-blocker-hook nil
  13139. "Hook for functions that are allowed to block a state change.
  13140. Each function gets as its single argument a property list, see
  13141. `org-trigger-hook' for more information about this list.
  13142. If any of the functions in this hook returns nil, the state change
  13143. is blocked.")
  13144. (defvar org-trigger-hook nil
  13145. "Hook for functions that are triggered by a state change.
  13146. Each function gets as its single argument a property list with at least
  13147. the following elements:
  13148. (:type type-of-change :position pos-at-entry-start
  13149. :from old-state :to new-state)
  13150. Depending on the type, more properties may be present.
  13151. This mechanism is currently implemented for:
  13152. TODO state changes
  13153. ------------------
  13154. :type todo-state-change
  13155. :from previous state (keyword as a string), or nil
  13156. :to new state (keyword as a string), or nil")
  13157. (defun org-todo (&optional arg)
  13158. "Change the TODO state of an item.
  13159. The state of an item is given by a keyword at the start of the heading,
  13160. like
  13161. *** TODO Write paper
  13162. *** DONE Call mom
  13163. The different keywords are specified in the variable `org-todo-keywords'.
  13164. By default the available states are \"TODO\" and \"DONE\".
  13165. So for this example: when the item starts with TODO, it is changed to DONE.
  13166. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13167. DONE are present, add TODO at the beginning of the heading.
  13168. With C-u prefix arg, use completion to determine the new state.
  13169. With numeric prefix arg, switch to that state.
  13170. For calling through lisp, arg is also interpreted in the following way:
  13171. 'none -> empty state
  13172. \"\"(empty string) -> switch to empty state
  13173. 'done -> switch to DONE
  13174. 'nextset -> switch to the next set of keywords
  13175. 'previousset -> switch to the previous set of keywords
  13176. \"WAITING\" -> switch to the specified keyword, but only if it
  13177. really is a member of `org-todo-keywords'."
  13178. (interactive "P")
  13179. (save-excursion
  13180. (catch 'exit
  13181. (org-back-to-heading)
  13182. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13183. (or (looking-at (concat " +" org-todo-regexp " *"))
  13184. (looking-at " *"))
  13185. (let* ((match-data (match-data))
  13186. (startpos (point-at-bol))
  13187. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13188. (org-log-done (org-parse-local-options logging 'org-log-done))
  13189. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  13190. (this (match-string 1))
  13191. (hl-pos (match-beginning 0))
  13192. (head (org-get-todo-sequence-head this))
  13193. (ass (assoc head org-todo-kwd-alist))
  13194. (interpret (nth 1 ass))
  13195. (done-word (nth 3 ass))
  13196. (final-done-word (nth 4 ass))
  13197. (last-state (or this ""))
  13198. (completion-ignore-case t)
  13199. (member (member this org-todo-keywords-1))
  13200. (tail (cdr member))
  13201. (state (cond
  13202. ((and org-todo-key-trigger
  13203. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13204. (and (not arg) org-use-fast-todo-selection
  13205. (not (eq org-use-fast-todo-selection 'prefix)))))
  13206. ;; Use fast selection
  13207. (org-fast-todo-selection))
  13208. ((and (equal arg '(4))
  13209. (or (not org-use-fast-todo-selection)
  13210. (not org-todo-key-trigger)))
  13211. ;; Read a state with completion
  13212. (completing-read "State: " (mapcar (lambda(x) (list x))
  13213. org-todo-keywords-1)
  13214. nil t))
  13215. ((eq arg 'right)
  13216. (if this
  13217. (if tail (car tail) nil)
  13218. (car org-todo-keywords-1)))
  13219. ((eq arg 'left)
  13220. (if (equal member org-todo-keywords-1)
  13221. nil
  13222. (if this
  13223. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13224. org-todo-keywords-1)
  13225. (org-last org-todo-keywords-1))))
  13226. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13227. (setq arg nil))) ; hack to fall back to cycling
  13228. (arg
  13229. ;; user or caller requests a specific state
  13230. (cond
  13231. ((equal arg "") nil)
  13232. ((eq arg 'none) nil)
  13233. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13234. ((eq arg 'nextset)
  13235. (or (car (cdr (member head org-todo-heads)))
  13236. (car org-todo-heads)))
  13237. ((eq arg 'previousset)
  13238. (let ((org-todo-heads (reverse org-todo-heads)))
  13239. (or (car (cdr (member head org-todo-heads)))
  13240. (car org-todo-heads))))
  13241. ((car (member arg org-todo-keywords-1)))
  13242. ((nth (1- (prefix-numeric-value arg))
  13243. org-todo-keywords-1))))
  13244. ((null member) (or head (car org-todo-keywords-1)))
  13245. ((equal this final-done-word) nil) ;; -> make empty
  13246. ((null tail) nil) ;; -> first entry
  13247. ((eq interpret 'sequence)
  13248. (car tail))
  13249. ((memq interpret '(type priority))
  13250. (if (eq this-command last-command)
  13251. (car tail)
  13252. (if (> (length tail) 0)
  13253. (or done-word (car org-done-keywords))
  13254. nil)))
  13255. (t nil)))
  13256. (next (if state (concat " " state " ") " "))
  13257. (change-plist (list :type 'todo-state-change :from this :to state
  13258. :position startpos))
  13259. dostates)
  13260. (when org-blocker-hook
  13261. (unless (save-excursion
  13262. (save-match-data
  13263. (run-hook-with-args-until-failure
  13264. 'org-blocker-hook change-plist)))
  13265. (if (interactive-p)
  13266. (error "TODO state change from %s to %s blocked" this state)
  13267. ;; fail silently
  13268. (message "TODO state change from %s to %s blocked" this state)
  13269. (throw 'exit nil))))
  13270. (store-match-data match-data)
  13271. (replace-match next t t)
  13272. (unless (pos-visible-in-window-p hl-pos)
  13273. (message "TODO state changed to %s" (org-trim next)))
  13274. (unless head
  13275. (setq head (org-get-todo-sequence-head state)
  13276. ass (assoc head org-todo-kwd-alist)
  13277. interpret (nth 1 ass)
  13278. done-word (nth 3 ass)
  13279. final-done-word (nth 4 ass)))
  13280. (when (memq arg '(nextset previousset))
  13281. (message "Keyword-Set %d/%d: %s"
  13282. (- (length org-todo-sets) -1
  13283. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13284. (length org-todo-sets)
  13285. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13286. (setq org-last-todo-state-is-todo
  13287. (not (member state org-done-keywords)))
  13288. (when (and org-log-done (not (memq arg '(nextset previousset))))
  13289. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  13290. (or (not org-todo-log-states)
  13291. (member state org-todo-log-states))))
  13292. (cond
  13293. ((and state (member state org-not-done-keywords)
  13294. (not (member this org-not-done-keywords)))
  13295. ;; This is now a todo state and was not one before
  13296. ;; Remove any CLOSED timestamp, and possibly log the state change
  13297. (org-add-planning-info nil nil 'closed)
  13298. (and dostates (org-add-log-maybe 'state state 'findpos)))
  13299. ((and state dostates)
  13300. ;; This is a non-nil state, and we need to log it
  13301. (org-add-log-maybe 'state state 'findpos))
  13302. ((and (member state org-done-keywords)
  13303. (not (member this org-done-keywords)))
  13304. ;; It is now done, and it was not done before
  13305. (org-add-planning-info 'closed (org-current-time))
  13306. (org-add-log-maybe 'done state 'findpos))))
  13307. ;; Fixup tag positioning
  13308. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13309. (run-hooks 'org-after-todo-state-change-hook)
  13310. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  13311. (if (and arg (not (member state org-done-keywords)))
  13312. (setq head (org-get-todo-sequence-head state)))
  13313. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13314. ;; Fixup cursor location if close to the keyword
  13315. (if (and (outline-on-heading-p)
  13316. (not (bolp))
  13317. (save-excursion (beginning-of-line 1)
  13318. (looking-at org-todo-line-regexp))
  13319. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13320. (progn
  13321. (goto-char (or (match-end 2) (match-end 1)))
  13322. (just-one-space)))
  13323. (when org-trigger-hook
  13324. (save-excursion
  13325. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13326. (defun org-get-todo-sequence-head (kwd)
  13327. "Return the head of the TODO sequence to which KWD belongs.
  13328. If KWD is not set, check if there is a text property remembering the
  13329. right sequence."
  13330. (let (p)
  13331. (cond
  13332. ((not kwd)
  13333. (or (get-text-property (point-at-bol) 'org-todo-head)
  13334. (progn
  13335. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13336. nil (point-at-eol)))
  13337. (get-text-property p 'org-todo-head))))
  13338. ((not (member kwd org-todo-keywords-1))
  13339. (car org-todo-keywords-1))
  13340. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13341. (defun org-fast-todo-selection ()
  13342. "Fast TODO keyword selection with single keys.
  13343. Returns the new TODO keyword, or nil if no state change should occur."
  13344. (let* ((fulltable org-todo-key-alist)
  13345. (done-keywords org-done-keywords) ;; needed for the faces.
  13346. (maxlen (apply 'max (mapcar
  13347. (lambda (x)
  13348. (if (stringp (car x)) (string-width (car x)) 0))
  13349. fulltable)))
  13350. (expert nil)
  13351. (fwidth (+ maxlen 3 1 3))
  13352. (ncol (/ (- (window-width) 4) fwidth))
  13353. tg cnt e c tbl
  13354. groups ingroup)
  13355. (save-window-excursion
  13356. (if expert
  13357. (set-buffer (get-buffer-create " *Org todo*"))
  13358. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13359. (erase-buffer)
  13360. (org-set-local 'org-done-keywords done-keywords)
  13361. (setq tbl fulltable cnt 0)
  13362. (while (setq e (pop tbl))
  13363. (cond
  13364. ((equal e '(:startgroup))
  13365. (push '() groups) (setq ingroup t)
  13366. (when (not (= cnt 0))
  13367. (setq cnt 0)
  13368. (insert "\n"))
  13369. (insert "{ "))
  13370. ((equal e '(:endgroup))
  13371. (setq ingroup nil cnt 0)
  13372. (insert "}\n"))
  13373. (t
  13374. (setq tg (car e) c (cdr e))
  13375. (if ingroup (push tg (car groups)))
  13376. (setq tg (org-add-props tg nil 'face
  13377. (org-get-todo-face tg)))
  13378. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13379. (insert "[" c "] " tg (make-string
  13380. (- fwidth 4 (length tg)) ?\ ))
  13381. (when (= (setq cnt (1+ cnt)) ncol)
  13382. (insert "\n")
  13383. (if ingroup (insert " "))
  13384. (setq cnt 0)))))
  13385. (insert "\n")
  13386. (goto-char (point-min))
  13387. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13388. (fit-window-to-buffer))
  13389. (message "[a-z..]:Set [SPC]:clear")
  13390. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13391. (cond
  13392. ((or (= c ?\C-g)
  13393. (and (= c ?q) (not (rassoc c fulltable))))
  13394. (setq quit-flag t))
  13395. ((= c ?\ ) nil)
  13396. ((setq e (rassoc c fulltable) tg (car e))
  13397. tg)
  13398. (t (setq quit-flag t))))))
  13399. (defun org-get-repeat ()
  13400. "Check if tere is a deadline/schedule with repeater in this entry."
  13401. (save-match-data
  13402. (save-excursion
  13403. (org-back-to-heading t)
  13404. (if (re-search-forward
  13405. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13406. (match-string 1)))))
  13407. (defvar org-last-changed-timestamp)
  13408. (defvar org-log-post-message)
  13409. (defun org-auto-repeat-maybe ()
  13410. "Check if the current headline contains a repeated deadline/schedule.
  13411. If yes, set TODO state back to what it was and change the base date
  13412. of repeating deadline/scheduled time stamps to new date.
  13413. This function should be run in the `org-after-todo-state-change-hook'."
  13414. ;; last-state is dynamically scoped into this function
  13415. (let* ((repeat (org-get-repeat))
  13416. (aa (assoc last-state org-todo-kwd-alist))
  13417. (interpret (nth 1 aa))
  13418. (head (nth 2 aa))
  13419. (done-word (nth 3 aa))
  13420. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13421. (msg "Entry repeats: ")
  13422. (org-log-done)
  13423. re type n what ts)
  13424. (when repeat
  13425. (org-todo (if (eq interpret 'type) last-state head))
  13426. (when (and org-log-repeat
  13427. (not (memq 'org-add-log-note
  13428. (default-value 'post-command-hook))))
  13429. ;; Make sure a note is taken
  13430. (let ((org-log-done '(done)))
  13431. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  13432. 'findpos)))
  13433. (org-back-to-heading t)
  13434. (org-add-planning-info nil nil 'closed)
  13435. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13436. org-deadline-time-regexp "\\)\\|\\("
  13437. org-ts-regexp "\\)"))
  13438. (while (re-search-forward
  13439. re (save-excursion (outline-next-heading) (point)) t)
  13440. (setq type (if (match-end 1) org-scheduled-string
  13441. (if (match-end 3) org-deadline-string "Plain:"))
  13442. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13443. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13444. (setq n (string-to-number (match-string 1 ts))
  13445. what (match-string 2 ts))
  13446. (if (equal what "w") (setq n (* n 7) what "d"))
  13447. (org-timestamp-change n (cdr (assoc what whata)))
  13448. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13449. (setq org-log-post-message msg)
  13450. (message "%s" msg))))
  13451. (defun org-show-todo-tree (arg)
  13452. "Make a compact tree which shows all headlines marked with TODO.
  13453. The tree will show the lines where the regexp matches, and all higher
  13454. headlines above the match.
  13455. With \\[universal-argument] prefix, also show the DONE entries.
  13456. With a numeric prefix N, construct a sparse tree for the Nth element
  13457. of `org-todo-keywords-1'."
  13458. (interactive "P")
  13459. (let ((case-fold-search nil)
  13460. (kwd-re
  13461. (cond ((null arg) org-not-done-regexp)
  13462. ((equal arg '(4))
  13463. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13464. (mapcar 'list org-todo-keywords-1))))
  13465. (concat "\\("
  13466. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13467. "\\)\\>")))
  13468. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13469. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13470. org-todo-keywords-1)))
  13471. (t (error "Invalid prefix argument: %s" arg)))))
  13472. (message "%d TODO entries found"
  13473. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13474. (defun org-deadline (&optional remove)
  13475. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13476. With argument REMOVE, remove any deadline from the item."
  13477. (interactive "P")
  13478. (if remove
  13479. (progn
  13480. (org-remove-timestamp-with-keyword org-deadline-string)
  13481. (message "Item no longer has a deadline."))
  13482. (org-add-planning-info 'deadline nil 'closed)))
  13483. (defun org-schedule (&optional remove)
  13484. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13485. With argument REMOVE, remove any scheduling date from the item."
  13486. (interactive "P")
  13487. (if remove
  13488. (progn
  13489. (org-remove-timestamp-with-keyword org-scheduled-string)
  13490. (message "Item is no longer scheduled."))
  13491. (org-add-planning-info 'scheduled nil 'closed)))
  13492. (defun org-remove-timestamp-with-keyword (keyword)
  13493. "Remove all time stamps with KEYWORD in the current entry."
  13494. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13495. beg)
  13496. (save-excursion
  13497. (org-back-to-heading t)
  13498. (setq beg (point))
  13499. (org-end-of-subtree t t)
  13500. (while (re-search-backward re beg t)
  13501. (replace-match "")
  13502. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13503. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13504. (defun org-add-planning-info (what &optional time &rest remove)
  13505. "Insert new timestamp with keyword in the line directly after the headline.
  13506. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13507. If non is given, the user is prompted for a date.
  13508. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13509. be removed."
  13510. (interactive)
  13511. (let (org-time-was-given org-end-time-was-given)
  13512. (when what (setq time (or time (org-read-date nil 'to-time))))
  13513. (when (and org-insert-labeled-timestamps-at-point
  13514. (member what '(scheduled deadline)))
  13515. (insert
  13516. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13517. (org-insert-time-stamp time org-time-was-given
  13518. nil nil nil (list org-end-time-was-given))
  13519. (setq what nil))
  13520. (save-excursion
  13521. (save-restriction
  13522. (let (col list elt ts buffer-invisibility-spec)
  13523. (org-back-to-heading t)
  13524. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13525. (goto-char (match-end 1))
  13526. (setq col (current-column))
  13527. (goto-char (match-end 0))
  13528. (if (eobp) (insert "\n") (forward-char 1))
  13529. (if (and (not (looking-at outline-regexp))
  13530. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13531. "[^\r\n]*"))
  13532. (not (equal (match-string 1) org-clock-string)))
  13533. (narrow-to-region (match-beginning 0) (match-end 0))
  13534. (insert-before-markers "\n")
  13535. (backward-char 1)
  13536. (narrow-to-region (point) (point))
  13537. (indent-to-column col))
  13538. ;; Check if we have to remove something.
  13539. (setq list (cons what remove))
  13540. (while list
  13541. (setq elt (pop list))
  13542. (goto-char (point-min))
  13543. (when (or (and (eq elt 'scheduled)
  13544. (re-search-forward org-scheduled-time-regexp nil t))
  13545. (and (eq elt 'deadline)
  13546. (re-search-forward org-deadline-time-regexp nil t))
  13547. (and (eq elt 'closed)
  13548. (re-search-forward org-closed-time-regexp nil t)))
  13549. (replace-match "")
  13550. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13551. (if (looking-at " +") (replace-match ""))))
  13552. (goto-char (point-max))
  13553. (when what
  13554. (insert
  13555. (if (not (equal (char-before) ?\ )) " " "")
  13556. (cond ((eq what 'scheduled) org-scheduled-string)
  13557. ((eq what 'deadline) org-deadline-string)
  13558. ((eq what 'closed) org-closed-string))
  13559. " ")
  13560. (setq ts (org-insert-time-stamp
  13561. time
  13562. (or org-time-was-given
  13563. (and (eq what 'closed) org-log-done-with-time))
  13564. (eq what 'closed)
  13565. nil nil (list org-end-time-was-given)))
  13566. (end-of-line 1))
  13567. (goto-char (point-min))
  13568. (widen)
  13569. (if (looking-at "[ \t]+\r?\n")
  13570. (replace-match ""))
  13571. ts)))))
  13572. (defvar org-log-note-marker (make-marker))
  13573. (defvar org-log-note-purpose nil)
  13574. (defvar org-log-note-state nil)
  13575. (defvar org-log-note-window-configuration nil)
  13576. (defvar org-log-note-return-to (make-marker))
  13577. (defvar org-log-post-message nil
  13578. "Message to be displayed after a log note has been stored.
  13579. The auto-repeater uses this.")
  13580. (defun org-add-log-maybe (&optional purpose state findpos)
  13581. "Set up the post command hook to take a note."
  13582. (save-excursion
  13583. (when (and (listp org-log-done)
  13584. (memq purpose org-log-done))
  13585. (when findpos
  13586. (org-back-to-heading t)
  13587. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13588. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13589. "[^\r\n]*\\)?"))
  13590. (goto-char (match-end 0))
  13591. (unless org-log-states-order-reversed
  13592. (and (= (char-after) ?\n) (forward-char 1))
  13593. (org-skip-over-state-notes)
  13594. (skip-chars-backward " \t\n\r")))
  13595. (move-marker org-log-note-marker (point))
  13596. (setq org-log-note-purpose purpose)
  13597. (setq org-log-note-state state)
  13598. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  13599. (defun org-skip-over-state-notes ()
  13600. "Skip past the list of State notes in an entry."
  13601. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13602. (while (looking-at "[ \t]*- State")
  13603. (condition-case nil
  13604. (org-next-item)
  13605. (error (org-end-of-item)))))
  13606. (defun org-add-log-note (&optional purpose)
  13607. "Pop up a window for taking a note, and add this note later at point."
  13608. (remove-hook 'post-command-hook 'org-add-log-note)
  13609. (setq org-log-note-window-configuration (current-window-configuration))
  13610. (delete-other-windows)
  13611. (move-marker org-log-note-return-to (point))
  13612. (switch-to-buffer (marker-buffer org-log-note-marker))
  13613. (goto-char org-log-note-marker)
  13614. (org-switch-to-buffer-other-window "*Org Note*")
  13615. (erase-buffer)
  13616. (let ((org-inhibit-startup t)) (org-mode))
  13617. (insert (format "# Insert note for %s.
  13618. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13619. (cond
  13620. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13621. ((eq org-log-note-purpose 'done) "closed todo item")
  13622. ((eq org-log-note-purpose 'state)
  13623. (format "state change to \"%s\"" org-log-note-state))
  13624. (t (error "This should not happen")))))
  13625. (org-set-local 'org-finish-function 'org-store-log-note))
  13626. (defun org-store-log-note ()
  13627. "Finish taking a log note, and insert it to where it belongs."
  13628. (let ((txt (buffer-string))
  13629. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13630. lines ind)
  13631. (kill-buffer (current-buffer))
  13632. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13633. (setq txt (replace-match "" t t txt)))
  13634. (if (string-match "\\s-+\\'" txt)
  13635. (setq txt (replace-match "" t t txt)))
  13636. (setq lines (org-split-string txt "\n"))
  13637. (when (and note (string-match "\\S-" note))
  13638. (setq note
  13639. (org-replace-escapes
  13640. note
  13641. (list (cons "%u" (user-login-name))
  13642. (cons "%U" user-full-name)
  13643. (cons "%t" (format-time-string
  13644. (org-time-stamp-format 'long 'inactive)
  13645. (current-time)))
  13646. (cons "%s" (if org-log-note-state
  13647. (concat "\"" org-log-note-state "\"")
  13648. "")))))
  13649. (if lines (setq note (concat note " \\\\")))
  13650. (push note lines))
  13651. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13652. (when lines
  13653. (save-excursion
  13654. (set-buffer (marker-buffer org-log-note-marker))
  13655. (save-excursion
  13656. (goto-char org-log-note-marker)
  13657. (move-marker org-log-note-marker nil)
  13658. (end-of-line 1)
  13659. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13660. (indent-relative nil)
  13661. (insert "- " (pop lines))
  13662. (org-indent-line-function)
  13663. (beginning-of-line 1)
  13664. (looking-at "[ \t]*")
  13665. (setq ind (concat (match-string 0) " "))
  13666. (end-of-line 1)
  13667. (while lines (insert "\n" ind (pop lines)))))))
  13668. (set-window-configuration org-log-note-window-configuration)
  13669. (with-current-buffer (marker-buffer org-log-note-return-to)
  13670. (goto-char org-log-note-return-to))
  13671. (move-marker org-log-note-return-to nil)
  13672. (and org-log-post-message (message "%s" org-log-post-message)))
  13673. ;; FIXME: what else would be useful?
  13674. ;; - priority
  13675. ;; - date
  13676. (defun org-sparse-tree (&optional arg)
  13677. "Create a sparse tree, prompt for the details.
  13678. This command can create sparse trees. You first need to select the type
  13679. of match used to create the tree:
  13680. t Show entries with a specific TODO keyword.
  13681. T Show entries selected by a tags match.
  13682. p Enter a property name and its value (both with completion on existing
  13683. names/values) and show entries with that property.
  13684. r Show entries matching a regular expression
  13685. d Show deadlines due within `org-deadline-warning-days'."
  13686. (interactive "P")
  13687. (let (ans kwd value)
  13688. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13689. (setq ans (read-char-exclusive))
  13690. (cond
  13691. ((equal ans ?d)
  13692. (call-interactively 'org-check-deadlines))
  13693. ((equal ans ?b)
  13694. (call-interactively 'org-check-before-date))
  13695. ((equal ans ?t)
  13696. (org-show-todo-tree '(4)))
  13697. ((equal ans ?T)
  13698. (call-interactively 'org-tags-sparse-tree))
  13699. ((member ans '(?p ?P))
  13700. (setq kwd (completing-read "Property: "
  13701. (mapcar 'list (org-buffer-property-keys))))
  13702. (setq value (completing-read "Value: "
  13703. (mapcar 'list (org-property-values kwd))))
  13704. (unless (string-match "\\`{.*}\\'" value)
  13705. (setq value (concat "\"" value "\"")))
  13706. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13707. ((member ans '(?r ?R ?/))
  13708. (call-interactively 'org-occur))
  13709. (t (error "No such sparse tree command \"%c\"" ans)))))
  13710. (defvar org-occur-highlights nil)
  13711. (make-variable-buffer-local 'org-occur-highlights)
  13712. (defun org-occur (regexp &optional keep-previous callback)
  13713. "Make a compact tree which shows all matches of REGEXP.
  13714. The tree will show the lines where the regexp matches, and all higher
  13715. headlines above the match. It will also show the heading after the match,
  13716. to make sure editing the matching entry is easy.
  13717. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13718. call to `org-occur' will be kept, to allow stacking of calls to this
  13719. command.
  13720. If CALLBACK is non-nil, it is a function which is called to confirm
  13721. that the match should indeed be shown."
  13722. (interactive "sRegexp: \nP")
  13723. (or keep-previous (org-remove-occur-highlights nil nil t))
  13724. (let ((cnt 0))
  13725. (save-excursion
  13726. (goto-char (point-min))
  13727. (if (or (not keep-previous) ; do not want to keep
  13728. (not org-occur-highlights)) ; no previous matches
  13729. ;; hide everything
  13730. (org-overview))
  13731. (while (re-search-forward regexp nil t)
  13732. (when (or (not callback)
  13733. (save-match-data (funcall callback)))
  13734. (setq cnt (1+ cnt))
  13735. (when org-highlight-sparse-tree-matches
  13736. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13737. (org-show-context 'occur-tree))))
  13738. (when org-remove-highlights-with-change
  13739. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13740. nil 'local))
  13741. (unless org-sparse-tree-open-archived-trees
  13742. (org-hide-archived-subtrees (point-min) (point-max)))
  13743. (run-hooks 'org-occur-hook)
  13744. (if (interactive-p)
  13745. (message "%d match(es) for regexp %s" cnt regexp))
  13746. cnt))
  13747. (defun org-show-context (&optional key)
  13748. "Make sure point and context and visible.
  13749. How much context is shown depends upon the variables
  13750. `org-show-hierarchy-above', `org-show-following-heading'. and
  13751. `org-show-siblings'."
  13752. (let ((heading-p (org-on-heading-p t))
  13753. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13754. (following-p (org-get-alist-option org-show-following-heading key))
  13755. (entry-p (org-get-alist-option org-show-entry-below key))
  13756. (siblings-p (org-get-alist-option org-show-siblings key)))
  13757. (catch 'exit
  13758. ;; Show heading or entry text
  13759. (if (and heading-p (not entry-p))
  13760. (org-flag-heading nil) ; only show the heading
  13761. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13762. (org-show-hidden-entry))) ; show entire entry
  13763. (when following-p
  13764. ;; Show next sibling, or heading below text
  13765. (save-excursion
  13766. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13767. (org-flag-heading nil))))
  13768. (when siblings-p (org-show-siblings))
  13769. (when hierarchy-p
  13770. ;; show all higher headings, possibly with siblings
  13771. (save-excursion
  13772. (while (and (condition-case nil
  13773. (progn (org-up-heading-all 1) t)
  13774. (error nil))
  13775. (not (bobp)))
  13776. (org-flag-heading nil)
  13777. (when siblings-p (org-show-siblings))))))))
  13778. (defun org-reveal (&optional siblings)
  13779. "Show current entry, hierarchy above it, and the following headline.
  13780. This can be used to show a consistent set of context around locations
  13781. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13782. not t for the search context.
  13783. With optional argument SIBLINGS, on each level of the hierarchy all
  13784. siblings are shown. This repairs the tree structure to what it would
  13785. look like when opened with hierarchical calls to `org-cycle'."
  13786. (interactive "P")
  13787. (let ((org-show-hierarchy-above t)
  13788. (org-show-following-heading t)
  13789. (org-show-siblings (if siblings t org-show-siblings)))
  13790. (org-show-context nil)))
  13791. (defun org-highlight-new-match (beg end)
  13792. "Highlight from BEG to END and mark the highlight is an occur headline."
  13793. (let ((ov (org-make-overlay beg end)))
  13794. (org-overlay-put ov 'face 'secondary-selection)
  13795. (push ov org-occur-highlights)))
  13796. (defun org-remove-occur-highlights (&optional beg end noremove)
  13797. "Remove the occur highlights from the buffer.
  13798. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13799. from the `before-change-functions' in the current buffer."
  13800. (interactive)
  13801. (unless org-inhibit-highlight-removal
  13802. (mapc 'org-delete-overlay org-occur-highlights)
  13803. (setq org-occur-highlights nil)
  13804. (unless noremove
  13805. (remove-hook 'before-change-functions
  13806. 'org-remove-occur-highlights 'local))))
  13807. ;;;; Priorities
  13808. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13809. "Regular expression matching the priority indicator.")
  13810. (defvar org-remove-priority-next-time nil)
  13811. (defun org-priority-up ()
  13812. "Increase the priority of the current item."
  13813. (interactive)
  13814. (org-priority 'up))
  13815. (defun org-priority-down ()
  13816. "Decrease the priority of the current item."
  13817. (interactive)
  13818. (org-priority 'down))
  13819. (defun org-priority (&optional action)
  13820. "Change the priority of an item by ARG.
  13821. ACTION can be `set', `up', `down', or a character."
  13822. (interactive)
  13823. (setq action (or action 'set))
  13824. (let (current new news have remove)
  13825. (save-excursion
  13826. (org-back-to-heading)
  13827. (if (looking-at org-priority-regexp)
  13828. (setq current (string-to-char (match-string 2))
  13829. have t)
  13830. (setq current org-default-priority))
  13831. (cond
  13832. ((or (eq action 'set) (integerp action))
  13833. (if (integerp action)
  13834. (setq new action)
  13835. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13836. (setq new (read-char-exclusive)))
  13837. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13838. (= (upcase org-lowest-priority) org-lowest-priority))
  13839. (setq new (upcase new)))
  13840. (cond ((equal new ?\ ) (setq remove t))
  13841. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13842. (error "Priority must be between `%c' and `%c'"
  13843. org-highest-priority org-lowest-priority))))
  13844. ((eq action 'up)
  13845. (if (and (not have) (eq last-command this-command))
  13846. (setq new org-lowest-priority)
  13847. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13848. org-default-priority (1- current)))))
  13849. ((eq action 'down)
  13850. (if (and (not have) (eq last-command this-command))
  13851. (setq new org-highest-priority)
  13852. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13853. org-default-priority (1+ current)))))
  13854. (t (error "Invalid action")))
  13855. (if (or (< (upcase new) org-highest-priority)
  13856. (> (upcase new) org-lowest-priority))
  13857. (setq remove t))
  13858. (setq news (format "%c" new))
  13859. (if have
  13860. (if remove
  13861. (replace-match "" t t nil 1)
  13862. (replace-match news t t nil 2))
  13863. (if remove
  13864. (error "No priority cookie found in line")
  13865. (looking-at org-todo-line-regexp)
  13866. (if (match-end 2)
  13867. (progn
  13868. (goto-char (match-end 2))
  13869. (insert " [#" news "]"))
  13870. (goto-char (match-beginning 3))
  13871. (insert "[#" news "] ")))))
  13872. (org-preserve-lc (org-set-tags nil 'align))
  13873. (if remove
  13874. (message "Priority removed")
  13875. (message "Priority of current item set to %s" news))))
  13876. (defun org-get-priority (s)
  13877. "Find priority cookie and return priority."
  13878. (save-match-data
  13879. (if (not (string-match org-priority-regexp s))
  13880. (* 1000 (- org-lowest-priority org-default-priority))
  13881. (* 1000 (- org-lowest-priority
  13882. (string-to-char (match-string 2 s)))))))
  13883. ;;;; Tags
  13884. (defun org-scan-tags (action matcher &optional todo-only)
  13885. "Scan headline tags with inheritance and produce output ACTION.
  13886. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13887. evaluated, testing if a given set of tags qualifies a headline for
  13888. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13889. are included in the output."
  13890. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13891. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13892. (org-re
  13893. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13894. (props (list 'face nil
  13895. 'done-face 'org-done
  13896. 'undone-face nil
  13897. 'mouse-face 'highlight
  13898. 'org-not-done-regexp org-not-done-regexp
  13899. 'org-todo-regexp org-todo-regexp
  13900. 'keymap org-agenda-keymap
  13901. 'help-echo
  13902. (format "mouse-2 or RET jump to org file %s"
  13903. (abbreviate-file-name
  13904. (or (buffer-file-name (buffer-base-buffer))
  13905. (buffer-name (buffer-base-buffer)))))))
  13906. (case-fold-search nil)
  13907. lspos
  13908. tags tags-list tags-alist (llast 0) rtn level category i txt
  13909. todo marker entry priority)
  13910. (save-excursion
  13911. (goto-char (point-min))
  13912. (when (eq action 'sparse-tree)
  13913. (org-overview)
  13914. (org-remove-occur-highlights))
  13915. (while (re-search-forward re nil t)
  13916. (catch :skip
  13917. (setq todo (if (match-end 1) (match-string 2))
  13918. tags (if (match-end 4) (match-string 4)))
  13919. (goto-char (setq lspos (1+ (match-beginning 0))))
  13920. (setq level (org-reduced-level (funcall outline-level))
  13921. category (org-get-category))
  13922. (setq i llast llast level)
  13923. ;; remove tag lists from same and sublevels
  13924. (while (>= i level)
  13925. (when (setq entry (assoc i tags-alist))
  13926. (setq tags-alist (delete entry tags-alist)))
  13927. (setq i (1- i)))
  13928. ;; add the nex tags
  13929. (when tags
  13930. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13931. tags-alist
  13932. (cons (cons level tags) tags-alist)))
  13933. ;; compile tags for current headline
  13934. (setq tags-list
  13935. (if org-use-tag-inheritance
  13936. (apply 'append (mapcar 'cdr tags-alist))
  13937. tags))
  13938. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13939. (eval matcher)
  13940. (or (not org-agenda-skip-archived-trees)
  13941. (not (member org-archive-tag tags-list))))
  13942. (and (eq action 'agenda) (org-agenda-skip))
  13943. ;; list this headline
  13944. (if (eq action 'sparse-tree)
  13945. (progn
  13946. (and org-highlight-sparse-tree-matches
  13947. (org-get-heading) (match-end 0)
  13948. (org-highlight-new-match
  13949. (match-beginning 0) (match-beginning 1)))
  13950. (org-show-context 'tags-tree))
  13951. (setq txt (org-format-agenda-item
  13952. ""
  13953. (concat
  13954. (if org-tags-match-list-sublevels
  13955. (make-string (1- level) ?.) "")
  13956. (org-get-heading))
  13957. category tags-list)
  13958. priority (org-get-priority txt))
  13959. (goto-char lspos)
  13960. (setq marker (org-agenda-new-marker))
  13961. (org-add-props txt props
  13962. 'org-marker marker 'org-hd-marker marker 'org-category category
  13963. 'priority priority 'type "tagsmatch")
  13964. (push txt rtn))
  13965. ;; if we are to skip sublevels, jump to end of subtree
  13966. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13967. (when (and (eq action 'sparse-tree)
  13968. (not org-sparse-tree-open-archived-trees))
  13969. (org-hide-archived-subtrees (point-min) (point-max)))
  13970. (nreverse rtn)))
  13971. (defvar todo-only) ;; dynamically scoped
  13972. (defun org-tags-sparse-tree (&optional todo-only match)
  13973. "Create a sparse tree according to tags string MATCH.
  13974. MATCH can contain positive and negative selection of tags, like
  13975. \"+WORK+URGENT-WITHBOSS\".
  13976. If optional argument TODO_ONLY is non-nil, only select lines that are
  13977. also TODO lines."
  13978. (interactive "P")
  13979. (org-prepare-agenda-buffers (list (current-buffer)))
  13980. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13981. (defvar org-cached-props nil)
  13982. (defun org-cached-entry-get (pom property)
  13983. (if (or (eq t org-use-property-inheritance)
  13984. (member property org-use-property-inheritance))
  13985. ;; Caching is not possible, check it directly
  13986. (org-entry-get pom property 'inherit)
  13987. ;; Get all properties, so that we can do complicated checks easily
  13988. (cdr (assoc property (or org-cached-props
  13989. (setq org-cached-props
  13990. (org-entry-properties pom)))))))
  13991. (defun org-global-tags-completion-table (&optional files)
  13992. "Return the list of all tags in all agenda buffer/files."
  13993. (save-excursion
  13994. (org-uniquify
  13995. (delq nil
  13996. (apply 'append
  13997. (mapcar
  13998. (lambda (file)
  13999. (set-buffer (find-file-noselect file))
  14000. (append (org-get-buffer-tags)
  14001. (mapcar (lambda (x) (if (stringp (car-safe x))
  14002. (list (car-safe x)) nil))
  14003. org-tag-alist)))
  14004. (if (and files (car files))
  14005. files
  14006. (org-agenda-files))))))))
  14007. (defun org-make-tags-matcher (match)
  14008. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14009. ;; todo-only is scoped dynamically into this function, and the function
  14010. ;; may change it it the matcher asksk for it.
  14011. (unless match
  14012. ;; Get a new match request, with completion
  14013. (let ((org-last-tags-completion-table
  14014. (org-global-tags-completion-table)))
  14015. (setq match (completing-read
  14016. "Match: " 'org-tags-completion-function nil nil nil
  14017. 'org-tags-history))))
  14018. ;; Parse the string and create a lisp form
  14019. (let ((match0 match)
  14020. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  14021. minus tag mm
  14022. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14023. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14024. (if (string-match "/+" match)
  14025. ;; match contains also a todo-matching request
  14026. (progn
  14027. (setq tagsmatch (substring match 0 (match-beginning 0))
  14028. todomatch (substring match (match-end 0)))
  14029. (if (string-match "^!" todomatch)
  14030. (setq todo-only t todomatch (substring todomatch 1)))
  14031. (if (string-match "^\\s-*$" todomatch)
  14032. (setq todomatch nil)))
  14033. ;; only matching tags
  14034. (setq tagsmatch match todomatch nil))
  14035. ;; Make the tags matcher
  14036. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14037. (setq tagsmatcher t)
  14038. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14039. (while (setq term (pop orterms))
  14040. (while (and (equal (substring term -1) "\\") orterms)
  14041. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14042. (while (string-match re term)
  14043. (setq minus (and (match-end 1)
  14044. (equal (match-string 1 term) "-"))
  14045. tag (match-string 2 term)
  14046. re-p (equal (string-to-char tag) ?{)
  14047. level-p (match-end 3)
  14048. prop-p (match-end 4)
  14049. mm (cond
  14050. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14051. (level-p `(= level ,(string-to-number
  14052. (match-string 3 term))))
  14053. (prop-p
  14054. (setq pn (match-string 4 term)
  14055. pv (match-string 5 term)
  14056. cat-p (equal pn "CATEGORY")
  14057. re-p (equal (string-to-char pv) ?{)
  14058. pv (substring pv 1 -1))
  14059. (if (equal pn "CATEGORY")
  14060. (setq gv '(get-text-property (point) 'org-category))
  14061. (setq gv `(org-cached-entry-get nil ,pn)))
  14062. (if re-p
  14063. `(string-match ,pv (or ,gv ""))
  14064. `(equal ,pv ,gv)))
  14065. (t `(member ,(downcase tag) tags-list)))
  14066. mm (if minus (list 'not mm) mm)
  14067. term (substring term (match-end 0)))
  14068. (push mm tagsmatcher))
  14069. (push (if (> (length tagsmatcher) 1)
  14070. (cons 'and tagsmatcher)
  14071. (car tagsmatcher))
  14072. orlist)
  14073. (setq tagsmatcher nil))
  14074. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14075. (setq tagsmatcher
  14076. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14077. ;; Make the todo matcher
  14078. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14079. (setq todomatcher t)
  14080. (setq orterms (org-split-string todomatch "|") orlist nil)
  14081. (while (setq term (pop orterms))
  14082. (while (string-match re term)
  14083. (setq minus (and (match-end 1)
  14084. (equal (match-string 1 term) "-"))
  14085. kwd (match-string 2 term)
  14086. re-p (equal (string-to-char kwd) ?{)
  14087. term (substring term (match-end 0))
  14088. mm (if re-p
  14089. `(string-match ,(substring kwd 1 -1) todo)
  14090. (list 'equal 'todo kwd))
  14091. mm (if minus (list 'not mm) mm))
  14092. (push mm todomatcher))
  14093. (push (if (> (length todomatcher) 1)
  14094. (cons 'and todomatcher)
  14095. (car todomatcher))
  14096. orlist)
  14097. (setq todomatcher nil))
  14098. (setq todomatcher (if (> (length orlist) 1)
  14099. (cons 'or orlist) (car orlist))))
  14100. ;; Return the string and lisp forms of the matcher
  14101. (setq matcher (if todomatcher
  14102. (list 'and tagsmatcher todomatcher)
  14103. tagsmatcher))
  14104. (cons match0 matcher)))
  14105. (defun org-match-any-p (re list)
  14106. "Does re match any element of list?"
  14107. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14108. (delq nil list))
  14109. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14110. (defvar org-tags-overlay (org-make-overlay 1 1))
  14111. (org-detach-overlay org-tags-overlay)
  14112. (defun org-align-tags-here (to-col)
  14113. ;; Assumes that this is a headline
  14114. (let ((pos (point)) (col (current-column)) tags)
  14115. (beginning-of-line 1)
  14116. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14117. (< pos (match-beginning 2)))
  14118. (progn
  14119. (setq tags (match-string 2))
  14120. (goto-char (match-beginning 1))
  14121. (insert " ")
  14122. (delete-region (point) (1+ (match-end 0)))
  14123. (backward-char 1)
  14124. (move-to-column
  14125. (max (1+ (current-column))
  14126. (1+ col)
  14127. (if (> to-col 0)
  14128. to-col
  14129. (- (abs to-col) (length tags))))
  14130. t)
  14131. (insert tags)
  14132. (move-to-column (min (current-column) col) t))
  14133. (goto-char pos))))
  14134. (defun org-set-tags (&optional arg just-align)
  14135. "Set the tags for the current headline.
  14136. With prefix ARG, realign all tags in headings in the current buffer."
  14137. (interactive "P")
  14138. (let* ((re (concat "^" outline-regexp))
  14139. (current (org-get-tags-string))
  14140. (col (current-column))
  14141. (org-setting-tags t)
  14142. table current-tags inherited-tags ; computed below when needed
  14143. tags p0 c0 c1 rpl)
  14144. (if arg
  14145. (save-excursion
  14146. (goto-char (point-min))
  14147. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14148. (while (re-search-forward re nil t)
  14149. (org-set-tags nil t)
  14150. (end-of-line 1)))
  14151. (message "All tags realigned to column %d" org-tags-column))
  14152. (if just-align
  14153. (setq tags current)
  14154. ;; Get a new set of tags from the user
  14155. (save-excursion
  14156. (setq table (or org-tag-alist (org-get-buffer-tags))
  14157. org-last-tags-completion-table table
  14158. current-tags (org-split-string current ":")
  14159. inherited-tags (nreverse
  14160. (nthcdr (length current-tags)
  14161. (nreverse (org-get-tags-at))))
  14162. tags
  14163. (if (or (eq t org-use-fast-tag-selection)
  14164. (and org-use-fast-tag-selection
  14165. (delq nil (mapcar 'cdr table))))
  14166. (org-fast-tag-selection
  14167. current-tags inherited-tags table
  14168. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14169. (let ((org-add-colon-after-tag-completion t))
  14170. (org-trim
  14171. (org-without-partial-completion
  14172. (completing-read "Tags: " 'org-tags-completion-function
  14173. nil nil current 'org-tags-history)))))))
  14174. (while (string-match "[-+&]+" tags)
  14175. ;; No boolean logic, just a list
  14176. (setq tags (replace-match ":" t t tags))))
  14177. (if (string-match "\\`[\t ]*\\'" tags)
  14178. (setq tags "")
  14179. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14180. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14181. ;; Insert new tags at the correct column
  14182. (beginning-of-line 1)
  14183. (cond
  14184. ((and (equal current "") (equal tags "")))
  14185. ((re-search-forward
  14186. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14187. (point-at-eol) t)
  14188. (if (equal tags "")
  14189. (setq rpl "")
  14190. (goto-char (match-beginning 0))
  14191. (setq c0 (current-column) p0 (point)
  14192. c1 (max (1+ c0) (if (> org-tags-column 0)
  14193. org-tags-column
  14194. (- (- org-tags-column) (length tags))))
  14195. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14196. (replace-match rpl t t)
  14197. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14198. tags)
  14199. (t (error "Tags alignment failed")))
  14200. (move-to-column col)
  14201. (unless just-align
  14202. (run-hooks 'org-after-tags-change-hook)))))
  14203. (defun org-change-tag-in-region (beg end tag off)
  14204. "Add or remove TAG for each entry in the region.
  14205. This works in the agenda, and also in an org-mode buffer."
  14206. (interactive
  14207. (list (region-beginning) (region-end)
  14208. (let ((org-last-tags-completion-table
  14209. (if (org-mode-p)
  14210. (org-get-buffer-tags)
  14211. (org-global-tags-completion-table))))
  14212. (completing-read
  14213. "Tag: " 'org-tags-completion-function nil nil nil
  14214. 'org-tags-history))
  14215. (progn
  14216. (message "[s]et or [r]emove? ")
  14217. (equal (read-char-exclusive) ?r))))
  14218. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14219. (let ((agendap (equal major-mode 'org-agenda-mode))
  14220. l1 l2 m buf pos newhead (cnt 0))
  14221. (goto-char end)
  14222. (setq l2 (1- (org-current-line)))
  14223. (goto-char beg)
  14224. (setq l1 (org-current-line))
  14225. (loop for l from l1 to l2 do
  14226. (goto-line l)
  14227. (setq m (get-text-property (point) 'org-hd-marker))
  14228. (when (or (and (org-mode-p) (org-on-heading-p))
  14229. (and agendap m))
  14230. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14231. pos (if agendap m (point)))
  14232. (with-current-buffer buf
  14233. (save-excursion
  14234. (save-restriction
  14235. (goto-char pos)
  14236. (setq cnt (1+ cnt))
  14237. (org-toggle-tag tag (if off 'off 'on))
  14238. (setq newhead (org-get-heading)))))
  14239. (and agendap (org-agenda-change-all-lines newhead m))))
  14240. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14241. (defun org-tags-completion-function (string predicate &optional flag)
  14242. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14243. (confirm (lambda (x) (stringp (car x)))))
  14244. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14245. (setq s1 (match-string 1 string)
  14246. s2 (match-string 2 string))
  14247. (setq s1 "" s2 string))
  14248. (cond
  14249. ((eq flag nil)
  14250. ;; try completion
  14251. (setq rtn (try-completion s2 ctable confirm))
  14252. (if (stringp rtn)
  14253. (setq rtn
  14254. (concat s1 s2 (substring rtn (length s2))
  14255. (if (and org-add-colon-after-tag-completion
  14256. (assoc rtn ctable))
  14257. ":" ""))))
  14258. rtn)
  14259. ((eq flag t)
  14260. ;; all-completions
  14261. (all-completions s2 ctable confirm)
  14262. )
  14263. ((eq flag 'lambda)
  14264. ;; exact match?
  14265. (assoc s2 ctable)))
  14266. ))
  14267. (defun org-fast-tag-insert (kwd tags face &optional end)
  14268. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14269. (insert (format "%-12s" (concat kwd ":"))
  14270. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14271. (or end "")))
  14272. (defun org-fast-tag-show-exit (flag)
  14273. (save-excursion
  14274. (goto-line 3)
  14275. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14276. (replace-match ""))
  14277. (when flag
  14278. (end-of-line 1)
  14279. (move-to-column (- (window-width) 19) t)
  14280. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14281. (defun org-set-current-tags-overlay (current prefix)
  14282. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14283. (if (featurep 'xemacs)
  14284. (org-overlay-display org-tags-overlay (concat prefix s)
  14285. 'secondary-selection)
  14286. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14287. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14288. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14289. "Fast tag selection with single keys.
  14290. CURRENT is the current list of tags in the headline, INHERITED is the
  14291. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14292. possibly with grouping information. TODO-TABLE is a similar table with
  14293. TODO keywords, should these have keys assigned to them.
  14294. If the keys are nil, a-z are automatically assigned.
  14295. Returns the new tags string, or nil to not change the current settings."
  14296. (let* ((fulltable (append table todo-table))
  14297. (maxlen (apply 'max (mapcar
  14298. (lambda (x)
  14299. (if (stringp (car x)) (string-width (car x)) 0))
  14300. fulltable)))
  14301. (buf (current-buffer))
  14302. (expert (eq org-fast-tag-selection-single-key 'expert))
  14303. (buffer-tags nil)
  14304. (fwidth (+ maxlen 3 1 3))
  14305. (ncol (/ (- (window-width) 4) fwidth))
  14306. (i-face 'org-done)
  14307. (c-face 'org-todo)
  14308. tg cnt e c char c1 c2 ntable tbl rtn
  14309. ov-start ov-end ov-prefix
  14310. (exit-after-next org-fast-tag-selection-single-key)
  14311. (done-keywords org-done-keywords)
  14312. groups ingroup)
  14313. (save-excursion
  14314. (beginning-of-line 1)
  14315. (if (looking-at
  14316. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14317. (setq ov-start (match-beginning 1)
  14318. ov-end (match-end 1)
  14319. ov-prefix "")
  14320. (setq ov-start (1- (point-at-eol))
  14321. ov-end (1+ ov-start))
  14322. (skip-chars-forward "^\n\r")
  14323. (setq ov-prefix
  14324. (concat
  14325. (buffer-substring (1- (point)) (point))
  14326. (if (> (current-column) org-tags-column)
  14327. " "
  14328. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14329. (org-move-overlay org-tags-overlay ov-start ov-end)
  14330. (save-window-excursion
  14331. (if expert
  14332. (set-buffer (get-buffer-create " *Org tags*"))
  14333. (delete-other-windows)
  14334. (split-window-vertically)
  14335. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14336. (erase-buffer)
  14337. (org-set-local 'org-done-keywords done-keywords)
  14338. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14339. (org-fast-tag-insert "Current" current c-face "\n\n")
  14340. (org-fast-tag-show-exit exit-after-next)
  14341. (org-set-current-tags-overlay current ov-prefix)
  14342. (setq tbl fulltable char ?a cnt 0)
  14343. (while (setq e (pop tbl))
  14344. (cond
  14345. ((equal e '(:startgroup))
  14346. (push '() groups) (setq ingroup t)
  14347. (when (not (= cnt 0))
  14348. (setq cnt 0)
  14349. (insert "\n"))
  14350. (insert "{ "))
  14351. ((equal e '(:endgroup))
  14352. (setq ingroup nil cnt 0)
  14353. (insert "}\n"))
  14354. (t
  14355. (setq tg (car e) c2 nil)
  14356. (if (cdr e)
  14357. (setq c (cdr e))
  14358. ;; automatically assign a character.
  14359. (setq c1 (string-to-char
  14360. (downcase (substring
  14361. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14362. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14363. (while (or (rassoc char ntable) (rassoc char table))
  14364. (setq char (1+ char)))
  14365. (setq c2 c1))
  14366. (setq c (or c2 char)))
  14367. (if ingroup (push tg (car groups)))
  14368. (setq tg (org-add-props tg nil 'face
  14369. (cond
  14370. ((not (assoc tg table))
  14371. (org-get-todo-face tg))
  14372. ((member tg current) c-face)
  14373. ((member tg inherited) i-face)
  14374. (t nil))))
  14375. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14376. (insert "[" c "] " tg (make-string
  14377. (- fwidth 4 (length tg)) ?\ ))
  14378. (push (cons tg c) ntable)
  14379. (when (= (setq cnt (1+ cnt)) ncol)
  14380. (insert "\n")
  14381. (if ingroup (insert " "))
  14382. (setq cnt 0)))))
  14383. (setq ntable (nreverse ntable))
  14384. (insert "\n")
  14385. (goto-char (point-min))
  14386. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14387. (fit-window-to-buffer))
  14388. (setq rtn
  14389. (catch 'exit
  14390. (while t
  14391. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14392. (if groups " [!] no groups" " [!]groups")
  14393. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14394. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14395. (cond
  14396. ((= c ?\r) (throw 'exit t))
  14397. ((= c ?!)
  14398. (setq groups (not groups))
  14399. (goto-char (point-min))
  14400. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14401. ((= c ?\C-c)
  14402. (if (not expert)
  14403. (org-fast-tag-show-exit
  14404. (setq exit-after-next (not exit-after-next)))
  14405. (setq expert nil)
  14406. (delete-other-windows)
  14407. (split-window-vertically)
  14408. (org-switch-to-buffer-other-window " *Org tags*")
  14409. (and (fboundp 'fit-window-to-buffer)
  14410. (fit-window-to-buffer))))
  14411. ((or (= c ?\C-g)
  14412. (and (= c ?q) (not (rassoc c ntable))))
  14413. (org-detach-overlay org-tags-overlay)
  14414. (setq quit-flag t))
  14415. ((= c ?\ )
  14416. (setq current nil)
  14417. (if exit-after-next (setq exit-after-next 'now)))
  14418. ((= c ?\t)
  14419. (condition-case nil
  14420. (setq tg (completing-read
  14421. "Tag: "
  14422. (or buffer-tags
  14423. (with-current-buffer buf
  14424. (org-get-buffer-tags)))))
  14425. (quit (setq tg "")))
  14426. (when (string-match "\\S-" tg)
  14427. (add-to-list 'buffer-tags (list tg))
  14428. (if (member tg current)
  14429. (setq current (delete tg current))
  14430. (push tg current)))
  14431. (if exit-after-next (setq exit-after-next 'now)))
  14432. ((setq e (rassoc c todo-table) tg (car e))
  14433. (with-current-buffer buf
  14434. (save-excursion (org-todo tg)))
  14435. (if exit-after-next (setq exit-after-next 'now)))
  14436. ((setq e (rassoc c ntable) tg (car e))
  14437. (if (member tg current)
  14438. (setq current (delete tg current))
  14439. (loop for g in groups do
  14440. (if (member tg g)
  14441. (mapc (lambda (x)
  14442. (setq current (delete x current)))
  14443. g)))
  14444. (push tg current))
  14445. (if exit-after-next (setq exit-after-next 'now))))
  14446. ;; Create a sorted list
  14447. (setq current
  14448. (sort current
  14449. (lambda (a b)
  14450. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14451. (if (eq exit-after-next 'now) (throw 'exit t))
  14452. (goto-char (point-min))
  14453. (beginning-of-line 2)
  14454. (delete-region (point) (point-at-eol))
  14455. (org-fast-tag-insert "Current" current c-face)
  14456. (org-set-current-tags-overlay current ov-prefix)
  14457. (while (re-search-forward
  14458. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14459. (setq tg (match-string 1))
  14460. (add-text-properties
  14461. (match-beginning 1) (match-end 1)
  14462. (list 'face
  14463. (cond
  14464. ((member tg current) c-face)
  14465. ((member tg inherited) i-face)
  14466. (t (get-text-property (match-beginning 1) 'face))))))
  14467. (goto-char (point-min)))))
  14468. (org-detach-overlay org-tags-overlay)
  14469. (if rtn
  14470. (mapconcat 'identity current ":")
  14471. nil))))
  14472. (defun org-get-tags-string ()
  14473. "Get the TAGS string in the current headline."
  14474. (unless (org-on-heading-p t)
  14475. (error "Not on a heading"))
  14476. (save-excursion
  14477. (beginning-of-line 1)
  14478. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14479. (org-match-string-no-properties 1)
  14480. "")))
  14481. (defun org-get-tags ()
  14482. "Get the list of tags specified in the current headline."
  14483. (org-split-string (org-get-tags-string) ":"))
  14484. (defun org-get-buffer-tags ()
  14485. "Get a table of all tags used in the buffer, for completion."
  14486. (let (tags)
  14487. (save-excursion
  14488. (goto-char (point-min))
  14489. (while (re-search-forward
  14490. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14491. (when (equal (char-after (point-at-bol 0)) ?*)
  14492. (mapc (lambda (x) (add-to-list 'tags x))
  14493. (org-split-string (org-match-string-no-properties 1) ":")))))
  14494. (mapcar 'list tags)))
  14495. ;;;; Properties
  14496. ;;; Setting and retrieving properties
  14497. (defconst org-special-properties
  14498. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14499. "TIMESTAMP" "TIMESTAMP_IA")
  14500. "The special properties valid in Org-mode.
  14501. These are properties that are not defined in the property drawer,
  14502. but in some other way.")
  14503. (defconst org-default-properties
  14504. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14505. "LOCATION" "LOGGING" "COLUMNS")
  14506. "Some properties that are used by Org-mode for various purposes.
  14507. Being in this list makes sure that they are offered for completion.")
  14508. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14509. "Regular expression matching the first line of a property drawer.")
  14510. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14511. "Regular expression matching the first line of a property drawer.")
  14512. (defun org-property-action ()
  14513. "Do an action on properties."
  14514. (interactive)
  14515. (let (c)
  14516. (org-at-property-p)
  14517. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14518. (setq c (read-char-exclusive))
  14519. (cond
  14520. ((equal c ?s)
  14521. (call-interactively 'org-set-property))
  14522. ((equal c ?d)
  14523. (call-interactively 'org-delete-property))
  14524. ((equal c ?D)
  14525. (call-interactively 'org-delete-property-globally))
  14526. ((equal c ?c)
  14527. (call-interactively 'org-compute-property-at-point))
  14528. (t (error "No such property action %c" c)))))
  14529. (defun org-at-property-p ()
  14530. "Is the cursor in a property line?"
  14531. ;; FIXME: Does not check if we are actually in the drawer.
  14532. ;; FIXME: also returns true on any drawers.....
  14533. ;; This is used by C-c C-c for property action.
  14534. (save-excursion
  14535. (beginning-of-line 1)
  14536. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14537. (defmacro org-with-point-at (pom &rest body)
  14538. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14539. (declare (indent 1) (debug t))
  14540. `(save-excursion
  14541. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14542. (save-excursion
  14543. (goto-char (or pom (point)))
  14544. ,@body)))
  14545. (defun org-get-property-block (&optional beg end force)
  14546. "Return the (beg . end) range of the body of the property drawer.
  14547. BEG and END can be beginning and end of subtree, if not given
  14548. they will be found.
  14549. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14550. (catch 'exit
  14551. (save-excursion
  14552. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14553. (end (or end (progn (outline-next-heading) (point)))))
  14554. (goto-char beg)
  14555. (if (re-search-forward org-property-start-re end t)
  14556. (setq beg (1+ (match-end 0)))
  14557. (if force
  14558. (save-excursion
  14559. (org-insert-property-drawer)
  14560. (setq end (progn (outline-next-heading) (point))))
  14561. (throw 'exit nil))
  14562. (goto-char beg)
  14563. (if (re-search-forward org-property-start-re end t)
  14564. (setq beg (1+ (match-end 0)))))
  14565. (if (re-search-forward org-property-end-re end t)
  14566. (setq end (match-beginning 0))
  14567. (or force (throw 'exit nil))
  14568. (goto-char beg)
  14569. (setq end beg)
  14570. (org-indent-line-function)
  14571. (insert ":END:\n"))
  14572. (cons beg end)))))
  14573. (defun org-entry-properties (&optional pom which)
  14574. "Get all properties of the entry at point-or-marker POM.
  14575. This includes the TODO keyword, the tags, time strings for deadline,
  14576. scheduled, and clocking, and any additional properties defined in the
  14577. entry. The return value is an alist, keys may occur multiple times
  14578. if the property key was used several times.
  14579. POM may also be nil, in which case the current entry is used.
  14580. If WHICH is nil or `all', get all properties. If WHICH is
  14581. `special' or `standard', only get that subclass."
  14582. (setq which (or which 'all))
  14583. (org-with-point-at pom
  14584. (let ((clockstr (substring org-clock-string 0 -1))
  14585. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14586. beg end range props sum-props key value string clocksum)
  14587. (save-excursion
  14588. (when (condition-case nil (org-back-to-heading t) (error nil))
  14589. (setq beg (point))
  14590. (setq sum-props (get-text-property (point) 'org-summaries))
  14591. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14592. (outline-next-heading)
  14593. (setq end (point))
  14594. (when (memq which '(all special))
  14595. ;; Get the special properties, like TODO and tags
  14596. (goto-char beg)
  14597. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14598. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14599. (when (looking-at org-priority-regexp)
  14600. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14601. (when (and (setq value (org-get-tags-string))
  14602. (string-match "\\S-" value))
  14603. (push (cons "TAGS" value) props))
  14604. (when (setq value (org-get-tags-at))
  14605. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14606. props))
  14607. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14608. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14609. string (if (equal key clockstr)
  14610. (org-no-properties
  14611. (org-trim
  14612. (buffer-substring
  14613. (match-beginning 3) (goto-char (point-at-eol)))))
  14614. (substring (org-match-string-no-properties 3) 1 -1)))
  14615. (unless key
  14616. (if (= (char-after (match-beginning 3)) ?\[)
  14617. (setq key "TIMESTAMP_IA")
  14618. (setq key "TIMESTAMP")))
  14619. (when (or (equal key clockstr) (not (assoc key props)))
  14620. (push (cons key string) props)))
  14621. )
  14622. (when (memq which '(all standard))
  14623. ;; Get the standard properties, like :PORP: ...
  14624. (setq range (org-get-property-block beg end))
  14625. (when range
  14626. (goto-char (car range))
  14627. (while (re-search-forward
  14628. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14629. (cdr range) t)
  14630. (setq key (org-match-string-no-properties 1)
  14631. value (org-trim (or (org-match-string-no-properties 2) "")))
  14632. (unless (member key excluded)
  14633. (push (cons key (or value "")) props)))))
  14634. (if clocksum
  14635. (push (cons "CLOCKSUM"
  14636. (org-column-number-to-string (/ (float clocksum) 60.)
  14637. 'add_times))
  14638. props))
  14639. (append sum-props (nreverse props)))))))
  14640. (defun org-entry-get (pom property &optional inherit)
  14641. "Get value of PROPERTY for entry at point-or-marker POM.
  14642. If INHERIT is non-nil and the entry does not have the property,
  14643. then also check higher levels of the hierarchy.
  14644. If the property is present but empty, the return value is the empty string.
  14645. If the property is not present at all, nil is returned."
  14646. (org-with-point-at pom
  14647. (if inherit
  14648. (org-entry-get-with-inheritance property)
  14649. (if (member property org-special-properties)
  14650. ;; We need a special property. Use brute force, get all properties.
  14651. (cdr (assoc property (org-entry-properties nil 'special)))
  14652. (let ((range (org-get-property-block)))
  14653. (if (and range
  14654. (goto-char (car range))
  14655. (re-search-forward
  14656. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14657. (cdr range) t))
  14658. ;; Found the property, return it.
  14659. (if (match-end 1)
  14660. (org-match-string-no-properties 1)
  14661. "")))))))
  14662. (defun org-entry-delete (pom property)
  14663. "Delete the property PROPERTY from entry at point-or-marker POM."
  14664. (org-with-point-at pom
  14665. (if (member property org-special-properties)
  14666. nil ; cannot delete these properties.
  14667. (let ((range (org-get-property-block)))
  14668. (if (and range
  14669. (goto-char (car range))
  14670. (re-search-forward
  14671. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14672. (cdr range) t))
  14673. (progn
  14674. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14675. t)
  14676. nil)))))
  14677. ;; Multi-values properties are properties that contain multiple values
  14678. ;; These values are assumed to be single words, separated by whitespace.
  14679. (defun org-entry-add-to-multivalued-property (pom property value)
  14680. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14681. (let* ((old (org-entry-get pom property))
  14682. (values (and old (org-split-string old "[ \t]"))))
  14683. (unless (member value values)
  14684. (setq values (cons value values))
  14685. (org-entry-put pom property
  14686. (mapconcat 'identity values " ")))))
  14687. (defun org-entry-remove-from-multivalued-property (pom property value)
  14688. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14689. (let* ((old (org-entry-get pom property))
  14690. (values (and old (org-split-string old "[ \t]"))))
  14691. (when (member value values)
  14692. (setq values (delete value values))
  14693. (org-entry-put pom property
  14694. (mapconcat 'identity values " ")))))
  14695. (defun org-entry-member-in-multivalued-property (pom property value)
  14696. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14697. (let* ((old (org-entry-get pom property))
  14698. (values (and old (org-split-string old "[ \t]"))))
  14699. (member value values)))
  14700. (defvar org-entry-property-inherited-from (make-marker))
  14701. (defun org-entry-get-with-inheritance (property)
  14702. "Get entry property, and search higher levels if not present."
  14703. (let (tmp)
  14704. (save-excursion
  14705. (save-restriction
  14706. (widen)
  14707. (catch 'ex
  14708. (while t
  14709. (when (setq tmp (org-entry-get nil property))
  14710. (org-back-to-heading t)
  14711. (move-marker org-entry-property-inherited-from (point))
  14712. (throw 'ex tmp))
  14713. (or (org-up-heading-safe) (throw 'ex nil)))))
  14714. (or tmp (cdr (assoc property org-local-properties))
  14715. (cdr (assoc property org-global-properties))))))
  14716. (defun org-entry-put (pom property value)
  14717. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14718. (org-with-point-at pom
  14719. (org-back-to-heading t)
  14720. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14721. range)
  14722. (cond
  14723. ((equal property "TODO")
  14724. (when (and (stringp value) (string-match "\\S-" value)
  14725. (not (member value org-todo-keywords-1)))
  14726. (error "\"%s\" is not a valid TODO state" value))
  14727. (if (or (not value)
  14728. (not (string-match "\\S-" value)))
  14729. (setq value 'none))
  14730. (org-todo value)
  14731. (org-set-tags nil 'align))
  14732. ((equal property "PRIORITY")
  14733. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14734. (string-to-char value) ?\ ))
  14735. (org-set-tags nil 'align))
  14736. ((equal property "SCHEDULED")
  14737. (if (re-search-forward org-scheduled-time-regexp end t)
  14738. (cond
  14739. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14740. ((eq value 'later) (org-timestamp-change 1 'day))
  14741. (t (call-interactively 'org-schedule)))
  14742. (call-interactively 'org-schedule)))
  14743. ((equal property "DEADLINE")
  14744. (if (re-search-forward org-deadline-time-regexp end t)
  14745. (cond
  14746. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14747. ((eq value 'later) (org-timestamp-change 1 'day))
  14748. (t (call-interactively 'org-deadline)))
  14749. (call-interactively 'org-deadline)))
  14750. ((member property org-special-properties)
  14751. (error "The %s property can not yet be set with `org-entry-put'"
  14752. property))
  14753. (t ; a non-special property
  14754. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14755. (setq range (org-get-property-block beg end 'force))
  14756. (goto-char (car range))
  14757. (if (re-search-forward
  14758. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14759. (progn
  14760. (delete-region (match-beginning 1) (match-end 1))
  14761. (goto-char (match-beginning 1)))
  14762. (goto-char (cdr range))
  14763. (insert "\n")
  14764. (backward-char 1)
  14765. (org-indent-line-function)
  14766. (insert ":" property ":"))
  14767. (and value (insert " " value))
  14768. (org-indent-line-function)))))))
  14769. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  14770. "Get all property keys in the current buffer.
  14771. With INCLUDE-SPECIALS, also list the special properties that relect things
  14772. like tags and TODO state.
  14773. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14774. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  14775. (let (rtn range)
  14776. (save-excursion
  14777. (save-restriction
  14778. (widen)
  14779. (goto-char (point-min))
  14780. (while (re-search-forward org-property-start-re nil t)
  14781. (setq range (org-get-property-block))
  14782. (goto-char (car range))
  14783. (while (re-search-forward
  14784. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  14785. (cdr range) t)
  14786. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14787. (outline-next-heading))))
  14788. (when include-specials
  14789. (setq rtn (append org-special-properties rtn)))
  14790. (when include-defaults
  14791. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14792. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14793. (defun org-property-values (key)
  14794. "Return a list of all values of property KEY."
  14795. (save-excursion
  14796. (save-restriction
  14797. (widen)
  14798. (goto-char (point-min))
  14799. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14800. values)
  14801. (while (re-search-forward re nil t)
  14802. (add-to-list 'values (org-trim (match-string 1))))
  14803. (delete "" values)))))
  14804. (defun org-insert-property-drawer ()
  14805. "Insert a property drawer into the current entry."
  14806. (interactive)
  14807. (org-back-to-heading t)
  14808. (looking-at outline-regexp)
  14809. (let ((indent (- (match-end 0)(match-beginning 0)))
  14810. (beg (point))
  14811. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14812. end hiddenp)
  14813. (outline-next-heading)
  14814. (setq end (point))
  14815. (goto-char beg)
  14816. (while (re-search-forward re end t))
  14817. (setq hiddenp (org-invisible-p))
  14818. (end-of-line 1)
  14819. (and (equal (char-after) ?\n) (forward-char 1))
  14820. (org-skip-over-state-notes)
  14821. (skip-chars-backward " \t\n\r")
  14822. (if (eq (char-before) ?*) (forward-char 1))
  14823. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14824. (beginning-of-line 0)
  14825. (indent-to-column indent)
  14826. (beginning-of-line 2)
  14827. (indent-to-column indent)
  14828. (beginning-of-line 0)
  14829. (if hiddenp
  14830. (save-excursion
  14831. (org-back-to-heading t)
  14832. (hide-entry))
  14833. (org-flag-drawer t))))
  14834. (defun org-set-property (property value)
  14835. "In the current entry, set PROPERTY to VALUE.
  14836. When called interactively, this will prompt for a property name, offering
  14837. completion on existing and default properties. And then it will prompt
  14838. for a value, offering competion either on allowed values (via an inherited
  14839. xxx_ALL property) or on existing values in other instances of this property
  14840. in the current file."
  14841. (interactive
  14842. (let* ((prop (completing-read
  14843. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  14844. (cur (org-entry-get nil prop))
  14845. (allowed (org-property-get-allowed-values nil prop 'table))
  14846. (existing (mapcar 'list (org-property-values prop)))
  14847. (val (if allowed
  14848. (completing-read "Value: " allowed nil 'req-match)
  14849. (completing-read
  14850. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14851. (concat "[" cur "]") "")
  14852. ": ")
  14853. existing nil nil "" nil cur))))
  14854. (list prop (if (equal val "") cur val))))
  14855. (unless (equal (org-entry-get nil property) value)
  14856. (org-entry-put nil property value)))
  14857. (defun org-delete-property (property)
  14858. "In the current entry, delete PROPERTY."
  14859. (interactive
  14860. (let* ((prop (completing-read
  14861. "Property: " (org-entry-properties nil 'standard))))
  14862. (list prop)))
  14863. (message "Property %s %s" property
  14864. (if (org-entry-delete nil property)
  14865. "deleted"
  14866. "was not present in the entry")))
  14867. (defun org-delete-property-globally (property)
  14868. "Remove PROPERTY globally, from all entries."
  14869. (interactive
  14870. (let* ((prop (completing-read
  14871. "Globally remove property: "
  14872. (mapcar 'list (org-buffer-property-keys)))))
  14873. (list prop)))
  14874. (save-excursion
  14875. (save-restriction
  14876. (widen)
  14877. (goto-char (point-min))
  14878. (let ((cnt 0))
  14879. (while (re-search-forward
  14880. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14881. nil t)
  14882. (setq cnt (1+ cnt))
  14883. (replace-match ""))
  14884. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14885. (defvar org-columns-current-fmt-compiled) ; defined below
  14886. (defun org-compute-property-at-point ()
  14887. "Compute the property at point.
  14888. This looks for an enclosing column format, extracts the operator and
  14889. then applies it to the proerty in the column format's scope."
  14890. (interactive)
  14891. (unless (org-at-property-p)
  14892. (error "Not at a property"))
  14893. (let ((prop (org-match-string-no-properties 2)))
  14894. (org-columns-get-format-and-top-level)
  14895. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14896. (error "No operator defined for property %s" prop))
  14897. (org-columns-compute prop)))
  14898. (defun org-property-get-allowed-values (pom property &optional table)
  14899. "Get allowed values for the property PROPERTY.
  14900. When TABLE is non-nil, return an alist that can directly be used for
  14901. completion."
  14902. (let (vals)
  14903. (cond
  14904. ((equal property "TODO")
  14905. (setq vals (org-with-point-at pom
  14906. (append org-todo-keywords-1 '("")))))
  14907. ((equal property "PRIORITY")
  14908. (let ((n org-lowest-priority))
  14909. (while (>= n org-highest-priority)
  14910. (push (char-to-string n) vals)
  14911. (setq n (1- n)))))
  14912. ((member property org-special-properties))
  14913. (t
  14914. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14915. (when (and vals (string-match "\\S-" vals))
  14916. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14917. (setq vals (mapcar (lambda (x)
  14918. (cond ((stringp x) x)
  14919. ((numberp x) (number-to-string x))
  14920. ((symbolp x) (symbol-name x))
  14921. (t "???")))
  14922. vals)))))
  14923. (if table (mapcar 'list vals) vals)))
  14924. (defun org-property-previous-allowed-value (&optional previous)
  14925. "Switch to the next allowed value for this property."
  14926. (interactive)
  14927. (org-property-next-allowed-value t))
  14928. (defun org-property-next-allowed-value (&optional previous)
  14929. "Switch to the next allowed value for this property."
  14930. (interactive)
  14931. (unless (org-at-property-p)
  14932. (error "Not at a property"))
  14933. (let* ((key (match-string 2))
  14934. (value (match-string 3))
  14935. (allowed (or (org-property-get-allowed-values (point) key)
  14936. (and (member value '("[ ]" "[-]" "[X]"))
  14937. '("[ ]" "[X]"))))
  14938. nval)
  14939. (unless allowed
  14940. (error "Allowed values for this property have not been defined"))
  14941. (if previous (setq allowed (reverse allowed)))
  14942. (if (member value allowed)
  14943. (setq nval (car (cdr (member value allowed)))))
  14944. (setq nval (or nval (car allowed)))
  14945. (if (equal nval value)
  14946. (error "Only one allowed value for this property"))
  14947. (org-at-property-p)
  14948. (replace-match (concat " :" key ": " nval) t t)
  14949. (org-indent-line-function)
  14950. (beginning-of-line 1)
  14951. (skip-chars-forward " \t")))
  14952. (defun org-find-entry-with-id (ident)
  14953. "Locate the entry that contains the ID property with exact value IDENT.
  14954. IDENT can be a string, a symbol or a number, this function will search for
  14955. the string representation of it.
  14956. Return the position where this entry starts, or nil if there is no such entry."
  14957. (let ((id (cond
  14958. ((stringp ident) ident)
  14959. ((symbol-name ident) (symbol-name ident))
  14960. ((numberp ident) (number-to-string ident))
  14961. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14962. (case-fold-search nil))
  14963. (save-excursion
  14964. (save-restriction
  14965. (goto-char (point-min))
  14966. (when (re-search-forward
  14967. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14968. nil t)
  14969. (org-back-to-heading)
  14970. (point))))))
  14971. ;;; Column View
  14972. (defvar org-columns-overlays nil
  14973. "Holds the list of current column overlays.")
  14974. (defvar org-columns-current-fmt nil
  14975. "Local variable, holds the currently active column format.")
  14976. (defvar org-columns-current-fmt-compiled nil
  14977. "Local variable, holds the currently active column format.
  14978. This is the compiled version of the format.")
  14979. (defvar org-columns-current-widths nil
  14980. "Loval variable, holds the currently widths of fields.")
  14981. (defvar org-columns-current-maxwidths nil
  14982. "Loval variable, holds the currently active maximum column widths.")
  14983. (defvar org-columns-begin-marker (make-marker)
  14984. "Points to the position where last a column creation command was called.")
  14985. (defvar org-columns-top-level-marker (make-marker)
  14986. "Points to the position where current columns region starts.")
  14987. (defvar org-columns-map (make-sparse-keymap)
  14988. "The keymap valid in column display.")
  14989. (defun org-columns-content ()
  14990. "Switch to contents view while in columns view."
  14991. (interactive)
  14992. (org-overview)
  14993. (org-content))
  14994. (org-defkey org-columns-map "c" 'org-columns-content)
  14995. (org-defkey org-columns-map "o" 'org-overview)
  14996. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  14997. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  14998. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  14999. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15000. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15001. (org-defkey org-columns-map "q" 'org-columns-quit)
  15002. (org-defkey org-columns-map "r" 'org-columns-redo)
  15003. (org-defkey org-columns-map "g" 'org-columns-redo)
  15004. (org-defkey org-columns-map [left] 'backward-char)
  15005. (org-defkey org-columns-map "\M-b" 'backward-char)
  15006. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15007. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15008. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15009. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15010. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15011. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15012. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15013. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15014. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15015. (org-defkey org-columns-map ">" 'org-columns-widen)
  15016. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15017. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15018. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15019. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15020. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15021. '("Column"
  15022. ["Edit property" org-columns-edit-value t]
  15023. ["Next allowed value" org-columns-next-allowed-value t]
  15024. ["Previous allowed value" org-columns-previous-allowed-value t]
  15025. ["Show full value" org-columns-show-value t]
  15026. ["Edit allowed values" org-columns-edit-allowed t]
  15027. "--"
  15028. ["Edit column attributes" org-columns-edit-attributes t]
  15029. ["Increase column width" org-columns-widen t]
  15030. ["Decrease column width" org-columns-narrow t]
  15031. "--"
  15032. ["Move column right" org-columns-move-right t]
  15033. ["Move column left" org-columns-move-left t]
  15034. ["Add column" org-columns-new t]
  15035. ["Delete column" org-columns-delete t]
  15036. "--"
  15037. ["CONTENTS" org-columns-content t]
  15038. ["OVERVIEW" org-overview t]
  15039. ["Refresh columns display" org-columns-redo t]
  15040. "--"
  15041. ["Open link" org-columns-open-link t]
  15042. "--"
  15043. ["Quit" org-columns-quit t]))
  15044. (defun org-columns-new-overlay (beg end &optional string face)
  15045. "Create a new column overlay and add it to the list."
  15046. (let ((ov (org-make-overlay beg end)))
  15047. (org-overlay-put ov 'face (or face 'secondary-selection))
  15048. (org-overlay-display ov string face)
  15049. (push ov org-columns-overlays)
  15050. ov))
  15051. (defun org-columns-display-here (&optional props)
  15052. "Overlay the current line with column display."
  15053. (interactive)
  15054. (let* ((fmt org-columns-current-fmt-compiled)
  15055. (beg (point-at-bol))
  15056. (level-face (save-excursion
  15057. (beginning-of-line 1)
  15058. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15059. (org-get-level-face 2))))
  15060. (color (list :foreground
  15061. (face-attribute (or level-face 'default) :foreground)))
  15062. props pom property ass width f string ov column val modval)
  15063. ;; Check if the entry is in another buffer.
  15064. (unless props
  15065. (if (eq major-mode 'org-agenda-mode)
  15066. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15067. (get-text-property (point) 'org-marker))
  15068. props (if pom (org-entry-properties pom) nil))
  15069. (setq props (org-entry-properties nil))))
  15070. ;; Walk the format
  15071. (while (setq column (pop fmt))
  15072. (setq property (car column)
  15073. ass (if (equal property "ITEM")
  15074. (cons "ITEM"
  15075. (save-match-data
  15076. (org-no-properties
  15077. (org-remove-tabs
  15078. (buffer-substring-no-properties
  15079. (point-at-bol) (point-at-eol))))))
  15080. (assoc property props))
  15081. width (or (cdr (assoc property org-columns-current-maxwidths))
  15082. (nth 2 column)
  15083. (length property))
  15084. f (format "%%-%d.%ds | " width width)
  15085. val (or (cdr ass) "")
  15086. modval (if (equal property "ITEM")
  15087. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15088. string (format f (or modval val)))
  15089. ;; Create the overlay
  15090. (org-unmodified
  15091. (setq ov (org-columns-new-overlay
  15092. beg (setq beg (1+ beg)) string
  15093. (list color 'org-column)))
  15094. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15095. (org-overlay-put ov 'keymap org-columns-map)
  15096. (org-overlay-put ov 'org-columns-key property)
  15097. (org-overlay-put ov 'org-columns-value (cdr ass))
  15098. (org-overlay-put ov 'org-columns-value-modified modval)
  15099. (org-overlay-put ov 'org-columns-pom pom)
  15100. (org-overlay-put ov 'org-columns-format f))
  15101. (if (or (not (char-after beg))
  15102. (equal (char-after beg) ?\n))
  15103. (let ((inhibit-read-only t))
  15104. (save-excursion
  15105. (goto-char beg)
  15106. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15107. ;; Make the rest of the line disappear.
  15108. (org-unmodified
  15109. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15110. (org-overlay-put ov 'invisible t)
  15111. (org-overlay-put ov 'keymap org-columns-map)
  15112. (org-overlay-put ov 'intangible t)
  15113. (push ov org-columns-overlays)
  15114. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15115. (org-overlay-put ov 'keymap org-columns-map)
  15116. (push ov org-columns-overlays)
  15117. (let ((inhibit-read-only t))
  15118. (put-text-property (max (point-min) (1- (point-at-bol)))
  15119. (min (point-max) (1+ (point-at-eol)))
  15120. 'read-only "Type `e' to edit property")))))
  15121. (defvar org-previous-header-line-format nil
  15122. "The header line format before column view was turned on.")
  15123. (defvar org-columns-inhibit-recalculation nil
  15124. "Inhibit recomputing of columns on column view startup.")
  15125. (defvar header-line-format)
  15126. (defun org-columns-display-here-title ()
  15127. "Overlay the newline before the current line with the table title."
  15128. (interactive)
  15129. (let ((fmt org-columns-current-fmt-compiled)
  15130. string (title "")
  15131. property width f column str widths)
  15132. (while (setq column (pop fmt))
  15133. (setq property (car column)
  15134. str (or (nth 1 column) property)
  15135. width (or (cdr (assoc property org-columns-current-maxwidths))
  15136. (nth 2 column)
  15137. (length str))
  15138. widths (push width widths)
  15139. f (format "%%-%d.%ds | " width width)
  15140. string (format f str)
  15141. title (concat title string)))
  15142. (setq title (concat
  15143. (org-add-props " " nil 'display '(space :align-to 0))
  15144. (org-add-props title nil 'face '(:weight bold :underline t))))
  15145. (org-set-local 'org-previous-header-line-format header-line-format)
  15146. (org-set-local 'org-columns-current-widths (nreverse widths))
  15147. (setq header-line-format title)))
  15148. (defun org-columns-remove-overlays ()
  15149. "Remove all currently active column overlays."
  15150. (interactive)
  15151. (when (marker-buffer org-columns-begin-marker)
  15152. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15153. (when (local-variable-p 'org-previous-header-line-format)
  15154. (setq header-line-format org-previous-header-line-format)
  15155. (kill-local-variable 'org-previous-header-line-format))
  15156. (move-marker org-columns-begin-marker nil)
  15157. (move-marker org-columns-top-level-marker nil)
  15158. (org-unmodified
  15159. (mapc 'org-delete-overlay org-columns-overlays)
  15160. (setq org-columns-overlays nil)
  15161. (let ((inhibit-read-only t))
  15162. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15163. (defun org-columns-cleanup-item (item fmt)
  15164. "Remove from ITEM what is a column in the format FMT."
  15165. (if (not org-complex-heading-regexp)
  15166. item
  15167. (when (string-match org-complex-heading-regexp item)
  15168. (concat
  15169. (org-add-props (concat (match-string 1 item) " ") nil
  15170. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15171. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15172. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15173. " " (match-string 4 item)
  15174. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15175. (defun org-columns-show-value ()
  15176. "Show the full value of the property."
  15177. (interactive)
  15178. (let ((value (get-char-property (point) 'org-columns-value)))
  15179. (message "Value is: %s" (or value ""))))
  15180. (defun org-columns-quit ()
  15181. "Remove the column overlays and in this way exit column editing."
  15182. (interactive)
  15183. (org-unmodified
  15184. (org-columns-remove-overlays)
  15185. (let ((inhibit-read-only t))
  15186. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15187. (when (eq major-mode 'org-agenda-mode)
  15188. (message
  15189. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15190. (defun org-columns-check-computed ()
  15191. "Check if this column value is computed.
  15192. If yes, throw an error indicating that changing it does not make sense."
  15193. (let ((val (get-char-property (point) 'org-columns-value)))
  15194. (when (and (stringp val)
  15195. (get-char-property 0 'org-computed val))
  15196. (error "This value is computed from the entry's children"))))
  15197. (defun org-columns-todo (&optional arg)
  15198. "Change the TODO state during column view."
  15199. (interactive "P")
  15200. (org-columns-edit-value "TODO"))
  15201. (defun org-columns-set-tags-or-toggle (&optional arg)
  15202. "Toggle checkbox at point, or set tags for current headline."
  15203. (interactive "P")
  15204. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15205. (get-char-property (point) 'org-columns-value))
  15206. (org-columns-next-allowed-value)
  15207. (org-columns-edit-value "TAGS")))
  15208. (defun org-columns-edit-value (&optional key)
  15209. "Edit the value of the property at point in column view.
  15210. Where possible, use the standard interface for changing this line."
  15211. (interactive)
  15212. (org-columns-check-computed)
  15213. (let* ((external-key key)
  15214. (col (current-column))
  15215. (key (or key (get-char-property (point) 'org-columns-key)))
  15216. (value (get-char-property (point) 'org-columns-value))
  15217. (bol (point-at-bol)) (eol (point-at-eol))
  15218. (pom (or (get-text-property bol 'org-hd-marker)
  15219. (point))) ; keep despite of compiler waring
  15220. (line-overlays
  15221. (delq nil (mapcar (lambda (x)
  15222. (and (eq (overlay-buffer x) (current-buffer))
  15223. (>= (overlay-start x) bol)
  15224. (<= (overlay-start x) eol)
  15225. x))
  15226. org-columns-overlays)))
  15227. nval eval allowed)
  15228. (cond
  15229. ((equal key "CLOCKSUM")
  15230. (error "This special column cannot be edited"))
  15231. ((equal key "ITEM")
  15232. (setq eval '(org-with-point-at pom
  15233. (org-edit-headline))))
  15234. ((equal key "TODO")
  15235. (setq eval '(org-with-point-at pom
  15236. (let ((current-prefix-arg
  15237. (if external-key current-prefix-arg '(4))))
  15238. (call-interactively 'org-todo)))))
  15239. ((equal key "PRIORITY")
  15240. (setq eval '(org-with-point-at pom
  15241. (call-interactively 'org-priority))))
  15242. ((equal key "TAGS")
  15243. (setq eval '(org-with-point-at pom
  15244. (let ((org-fast-tag-selection-single-key
  15245. (if (eq org-fast-tag-selection-single-key 'expert)
  15246. t org-fast-tag-selection-single-key)))
  15247. (call-interactively 'org-set-tags)))))
  15248. ((equal key "DEADLINE")
  15249. (setq eval '(org-with-point-at pom
  15250. (call-interactively 'org-deadline))))
  15251. ((equal key "SCHEDULED")
  15252. (setq eval '(org-with-point-at pom
  15253. (call-interactively 'org-schedule))))
  15254. (t
  15255. (setq allowed (org-property-get-allowed-values pom key 'table))
  15256. (if allowed
  15257. (setq nval (completing-read "Value: " allowed nil t))
  15258. (setq nval (read-string "Edit: " value)))
  15259. (setq nval (org-trim nval))
  15260. (when (not (equal nval value))
  15261. (setq eval '(org-entry-put pom key nval)))))
  15262. (when eval
  15263. (let ((inhibit-read-only t))
  15264. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15265. (unwind-protect
  15266. (progn
  15267. (setq org-columns-overlays
  15268. (org-delete-all line-overlays org-columns-overlays))
  15269. (mapc 'org-delete-overlay line-overlays)
  15270. (org-columns-eval eval))
  15271. (org-columns-display-here))))
  15272. (move-to-column col)
  15273. (if (and (org-mode-p)
  15274. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15275. (org-columns-update key))))
  15276. (defun org-edit-headline () ; FIXME: this is not columns specific
  15277. "Edit the current headline, the part without TODO keyword, TAGS."
  15278. (org-back-to-heading)
  15279. (when (looking-at org-todo-line-regexp)
  15280. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15281. (txt (match-string 3))
  15282. (post "")
  15283. txt2)
  15284. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15285. (setq post (match-string 0 txt)
  15286. txt (substring txt 0 (match-beginning 0))))
  15287. (setq txt2 (read-string "Edit: " txt))
  15288. (when (not (equal txt txt2))
  15289. (beginning-of-line 1)
  15290. (insert pre txt2 post)
  15291. (delete-region (point) (point-at-eol))
  15292. (org-set-tags nil t)))))
  15293. (defun org-columns-edit-allowed ()
  15294. "Edit the list of allowed values for the current property."
  15295. (interactive)
  15296. (let* ((key (get-char-property (point) 'org-columns-key))
  15297. (key1 (concat key "_ALL"))
  15298. (allowed (org-entry-get (point) key1 t))
  15299. nval)
  15300. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15301. (setq nval (read-string "Allowed: " allowed))
  15302. (org-entry-put
  15303. (cond ((marker-position org-entry-property-inherited-from)
  15304. org-entry-property-inherited-from)
  15305. ((marker-position org-columns-top-level-marker)
  15306. org-columns-top-level-marker))
  15307. key1 nval)))
  15308. (defmacro org-no-warnings (&rest body)
  15309. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15310. (defun org-columns-eval (form)
  15311. (let (hidep)
  15312. (save-excursion
  15313. (beginning-of-line 1)
  15314. ;; `next-line' is needed here, because it skips invisible line.
  15315. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15316. (setq hidep (org-on-heading-p 1)))
  15317. (eval form)
  15318. (and hidep (hide-entry))))
  15319. (defun org-columns-previous-allowed-value ()
  15320. "Switch to the previous allowed value for this column."
  15321. (interactive)
  15322. (org-columns-next-allowed-value t))
  15323. (defun org-columns-next-allowed-value (&optional previous)
  15324. "Switch to the next allowed value for this column."
  15325. (interactive)
  15326. (org-columns-check-computed)
  15327. (let* ((col (current-column))
  15328. (key (get-char-property (point) 'org-columns-key))
  15329. (value (get-char-property (point) 'org-columns-value))
  15330. (bol (point-at-bol)) (eol (point-at-eol))
  15331. (pom (or (get-text-property bol 'org-hd-marker)
  15332. (point))) ; keep despite of compiler waring
  15333. (line-overlays
  15334. (delq nil (mapcar (lambda (x)
  15335. (and (eq (overlay-buffer x) (current-buffer))
  15336. (>= (overlay-start x) bol)
  15337. (<= (overlay-start x) eol)
  15338. x))
  15339. org-columns-overlays)))
  15340. (allowed (or (org-property-get-allowed-values pom key)
  15341. (and (equal
  15342. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15343. 'checkbox) '("[ ]" "[X]"))))
  15344. nval)
  15345. (when (equal key "ITEM")
  15346. (error "Cannot edit item headline from here"))
  15347. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15348. (error "Allowed values for this property have not been defined"))
  15349. (if (member key '("SCHEDULED" "DEADLINE"))
  15350. (setq nval (if previous 'earlier 'later))
  15351. (if previous (setq allowed (reverse allowed)))
  15352. (if (member value allowed)
  15353. (setq nval (car (cdr (member value allowed)))))
  15354. (setq nval (or nval (car allowed)))
  15355. (if (equal nval value)
  15356. (error "Only one allowed value for this property")))
  15357. (let ((inhibit-read-only t))
  15358. (remove-text-properties (1- bol) eol '(read-only t))
  15359. (unwind-protect
  15360. (progn
  15361. (setq org-columns-overlays
  15362. (org-delete-all line-overlays org-columns-overlays))
  15363. (mapc 'org-delete-overlay line-overlays)
  15364. (org-columns-eval '(org-entry-put pom key nval)))
  15365. (org-columns-display-here)))
  15366. (move-to-column col)
  15367. (if (and (org-mode-p)
  15368. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15369. (org-columns-update key))))
  15370. (defun org-verify-version (task)
  15371. (cond
  15372. ((eq task 'columns)
  15373. (if (or (featurep 'xemacs)
  15374. (< emacs-major-version 22))
  15375. (error "Emacs 22 is required for the columns feature")))))
  15376. (defun org-columns-open-link (&optional arg)
  15377. (interactive "P")
  15378. (let ((key (get-char-property (point) 'org-columns-key))
  15379. (value (get-char-property (point) 'org-columns-value)))
  15380. (org-open-link-from-string arg)))
  15381. (defun org-open-link-from-string (s &optional arg)
  15382. "Open a link in the string S, as if it was in Org-mode."
  15383. (interactive)
  15384. (with-temp-buffer
  15385. (let ((org-inhibit-startup t))
  15386. (org-mode)
  15387. (insert s)
  15388. (goto-char (point-min))
  15389. (org-open-at-point arg))))
  15390. (defun org-columns-get-format-and-top-level ()
  15391. (let (fmt)
  15392. (when (condition-case nil (org-back-to-heading) (error nil))
  15393. (move-marker org-entry-property-inherited-from nil)
  15394. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15395. (setq fmt (or fmt org-columns-default-format))
  15396. (org-set-local 'org-columns-current-fmt fmt)
  15397. (org-columns-compile-format fmt)
  15398. (if (marker-position org-entry-property-inherited-from)
  15399. (move-marker org-columns-top-level-marker
  15400. org-entry-property-inherited-from)
  15401. (move-marker org-columns-top-level-marker (point)))
  15402. fmt))
  15403. (defun org-columns ()
  15404. "Turn on column view on an org-mode file."
  15405. (interactive)
  15406. (org-verify-version 'columns)
  15407. (org-columns-remove-overlays)
  15408. (move-marker org-columns-begin-marker (point))
  15409. (let (beg end fmt cache maxwidths clocksump)
  15410. (setq fmt (org-columns-get-format-and-top-level))
  15411. (save-excursion
  15412. (goto-char org-columns-top-level-marker)
  15413. (setq beg (point))
  15414. (unless org-columns-inhibit-recalculation
  15415. (org-columns-compute-all))
  15416. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15417. (point-max)))
  15418. ;; Get and cache the properties
  15419. (goto-char beg)
  15420. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15421. (setq clocksump t)
  15422. (save-excursion
  15423. (save-restriction
  15424. (narrow-to-region beg end)
  15425. (org-clock-sum))))
  15426. (while (re-search-forward (concat "^" outline-regexp) end t)
  15427. (push (cons (org-current-line) (org-entry-properties)) cache))
  15428. (when cache
  15429. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15430. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15431. (org-columns-display-here-title)
  15432. (mapc (lambda (x)
  15433. (goto-line (car x))
  15434. (org-columns-display-here (cdr x)))
  15435. cache)))))
  15436. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15437. "Insert a new column, to the leeft o the current column."
  15438. (interactive)
  15439. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15440. cell)
  15441. (setq prop (completing-read
  15442. "Property: " (mapcar 'list (org-buffer-property-keys t))
  15443. nil nil prop))
  15444. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15445. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15446. (if (string-match "\\S-" width)
  15447. (setq width (string-to-number width))
  15448. (setq width nil))
  15449. (setq fmt (completing-read "Summary [none]: "
  15450. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox"))
  15451. nil t))
  15452. (if (string-match "\\S-" fmt)
  15453. (setq fmt (intern fmt))
  15454. (setq fmt nil))
  15455. (if (eq fmt 'none) (setq fmt nil))
  15456. (if editp
  15457. (progn
  15458. (setcar editp prop)
  15459. (setcdr editp (list title width nil fmt)))
  15460. (setq cell (nthcdr (1- (current-column))
  15461. org-columns-current-fmt-compiled))
  15462. (setcdr cell (cons (list prop title width nil fmt)
  15463. (cdr cell))))
  15464. (org-columns-store-format)
  15465. (org-columns-redo)))
  15466. (defun org-columns-delete ()
  15467. "Delete the column at point from columns view."
  15468. (interactive)
  15469. (let* ((n (current-column))
  15470. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15471. (when (y-or-n-p
  15472. (format "Are you sure you want to remove column \"%s\"? " title))
  15473. (setq org-columns-current-fmt-compiled
  15474. (delq (nth n org-columns-current-fmt-compiled)
  15475. org-columns-current-fmt-compiled))
  15476. (org-columns-store-format)
  15477. (org-columns-redo)
  15478. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15479. (backward-char 1)))))
  15480. (defun org-columns-edit-attributes ()
  15481. "Edit the attributes of the current column."
  15482. (interactive)
  15483. (let* ((n (current-column))
  15484. (info (nth n org-columns-current-fmt-compiled)))
  15485. (apply 'org-columns-new info)))
  15486. (defun org-columns-widen (arg)
  15487. "Make the column wider by ARG characters."
  15488. (interactive "p")
  15489. (let* ((n (current-column))
  15490. (entry (nth n org-columns-current-fmt-compiled))
  15491. (width (or (nth 2 entry)
  15492. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15493. (setq width (max 1 (+ width arg)))
  15494. (setcar (nthcdr 2 entry) width)
  15495. (org-columns-store-format)
  15496. (org-columns-redo)))
  15497. (defun org-columns-narrow (arg)
  15498. "Make the column nrrower by ARG characters."
  15499. (interactive "p")
  15500. (org-columns-widen (- arg)))
  15501. (defun org-columns-move-right ()
  15502. "Swap this column with the one to the right."
  15503. (interactive)
  15504. (let* ((n (current-column))
  15505. (cell (nthcdr n org-columns-current-fmt-compiled))
  15506. e)
  15507. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15508. (error "Cannot shift this column further to the right"))
  15509. (setq e (car cell))
  15510. (setcar cell (car (cdr cell)))
  15511. (setcdr cell (cons e (cdr (cdr cell))))
  15512. (org-columns-store-format)
  15513. (org-columns-redo)
  15514. (forward-char 1)))
  15515. (defun org-columns-move-left ()
  15516. "Swap this column with the one to the left."
  15517. (interactive)
  15518. (let* ((n (current-column)))
  15519. (when (= n 0)
  15520. (error "Cannot shift this column further to the left"))
  15521. (backward-char 1)
  15522. (org-columns-move-right)
  15523. (backward-char 1)))
  15524. (defun org-columns-store-format ()
  15525. "Store the text version of the current columns format in appropriate place.
  15526. This is either in the COLUMNS property of the node starting the current column
  15527. display, or in the #+COLUMNS line of the current buffer."
  15528. (let (fmt (cnt 0))
  15529. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15530. (org-set-local 'org-columns-current-fmt fmt)
  15531. (if (marker-position org-columns-top-level-marker)
  15532. (save-excursion
  15533. (goto-char org-columns-top-level-marker)
  15534. (if (and (org-at-heading-p)
  15535. (org-entry-get nil "COLUMNS"))
  15536. (org-entry-put nil "COLUMNS" fmt)
  15537. (goto-char (point-min))
  15538. ;; Overwrite all #+COLUMNS lines....
  15539. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15540. (setq cnt (1+ cnt))
  15541. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15542. (unless (> cnt 0)
  15543. (goto-char (point-min))
  15544. (or (org-on-heading-p t) (outline-next-heading))
  15545. (let ((inhibit-read-only t))
  15546. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15547. (org-set-local 'org-columns-default-format fmt))))))
  15548. (defvar org-overriding-columns-format nil
  15549. "When set, overrides any other definition.")
  15550. (defvar org-agenda-view-columns-initially nil
  15551. "When set, switch to columns view immediately after creating the agenda.")
  15552. (defun org-agenda-columns ()
  15553. "Turn on column view in the agenda."
  15554. (interactive)
  15555. (org-verify-version 'columns)
  15556. (org-columns-remove-overlays)
  15557. (move-marker org-columns-begin-marker (point))
  15558. (let (fmt cache maxwidths m)
  15559. (cond
  15560. ((and (local-variable-p 'org-overriding-columns-format)
  15561. org-overriding-columns-format)
  15562. (setq fmt org-overriding-columns-format))
  15563. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15564. (setq fmt (org-entry-get m "COLUMNS" t)))
  15565. ((and (boundp 'org-columns-current-fmt)
  15566. (local-variable-p 'org-columns-current-fmt)
  15567. org-columns-current-fmt)
  15568. (setq fmt org-columns-current-fmt))
  15569. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15570. (setq m (get-text-property m 'org-hd-marker))
  15571. (setq fmt (org-entry-get m "COLUMNS" t))))
  15572. (setq fmt (or fmt org-columns-default-format))
  15573. (org-set-local 'org-columns-current-fmt fmt)
  15574. (org-columns-compile-format fmt)
  15575. (save-excursion
  15576. ;; Get and cache the properties
  15577. (goto-char (point-min))
  15578. (while (not (eobp))
  15579. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15580. (get-text-property (point) 'org-marker)))
  15581. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15582. (beginning-of-line 2))
  15583. (when cache
  15584. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15585. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15586. (org-columns-display-here-title)
  15587. (mapc (lambda (x)
  15588. (goto-line (car x))
  15589. (org-columns-display-here (cdr x)))
  15590. cache)))))
  15591. (defun org-columns-get-autowidth-alist (s cache)
  15592. "Derive the maximum column widths from the format and the cache."
  15593. (let ((start 0) rtn)
  15594. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  15595. (push (cons (match-string 1 s) 1) rtn)
  15596. (setq start (match-end 0)))
  15597. (mapc (lambda (x)
  15598. (setcdr x (apply 'max
  15599. (mapcar
  15600. (lambda (y)
  15601. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15602. cache))))
  15603. rtn)
  15604. rtn))
  15605. (defun org-columns-compute-all ()
  15606. "Compute all columns that have operators defined."
  15607. (org-unmodified
  15608. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15609. (let ((columns org-columns-current-fmt-compiled) col)
  15610. (while (setq col (pop columns))
  15611. (when (nth 3 col)
  15612. (save-excursion
  15613. (org-columns-compute (car col)))))))
  15614. (defun org-columns-update (property)
  15615. "Recompute PROPERTY, and update the columns display for it."
  15616. (org-columns-compute property)
  15617. (let (fmt val pos)
  15618. (save-excursion
  15619. (mapc (lambda (ov)
  15620. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15621. (setq pos (org-overlay-start ov))
  15622. (goto-char pos)
  15623. (when (setq val (cdr (assoc property
  15624. (get-text-property
  15625. (point-at-bol) 'org-summaries))))
  15626. (setq fmt (org-overlay-get ov 'org-columns-format))
  15627. (org-overlay-put ov 'org-columns-value val)
  15628. (org-overlay-put ov 'display (format fmt val)))))
  15629. org-columns-overlays))))
  15630. (defun org-columns-compute (property)
  15631. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15632. (interactive)
  15633. (let* ((re (concat "^" outline-regexp))
  15634. (lmax 30) ; Does anyone use deeper levels???
  15635. (lsum (make-vector lmax 0))
  15636. (lflag (make-vector lmax nil))
  15637. (level 0)
  15638. (ass (assoc property org-columns-current-fmt-compiled))
  15639. (format (nth 4 ass))
  15640. (printf (nth 5 ass))
  15641. (beg org-columns-top-level-marker)
  15642. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15643. (save-excursion
  15644. ;; Find the region to compute
  15645. (goto-char beg)
  15646. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15647. (goto-char end)
  15648. ;; Walk the tree from the back and do the computations
  15649. (while (re-search-backward re beg t)
  15650. (setq sumpos (match-beginning 0)
  15651. last-level level
  15652. level (org-outline-level)
  15653. val (org-entry-get nil property)
  15654. valflag (and val (string-match "\\S-" val)))
  15655. (cond
  15656. ((< level last-level)
  15657. ;; put the sum of lower levels here as a property
  15658. (setq sum (aref lsum last-level) ; current sum
  15659. flag (aref lflag last-level) ; any valid entries from children?
  15660. str (org-column-number-to-string sum format printf)
  15661. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15662. useval (if flag str1 (if valflag val ""))
  15663. sum-alist (get-text-property sumpos 'org-summaries))
  15664. (if (assoc property sum-alist)
  15665. (setcdr (assoc property sum-alist) useval)
  15666. (push (cons property useval) sum-alist)
  15667. (org-unmodified
  15668. (add-text-properties sumpos (1+ sumpos)
  15669. (list 'org-summaries sum-alist))))
  15670. (when val
  15671. (org-entry-put nil property (if flag str val)))
  15672. ;; add current to current level accumulator
  15673. (when (or flag valflag)
  15674. (aset lsum level (+ (aref lsum level)
  15675. (if flag sum (org-column-string-to-number
  15676. (if flag str val) format))))
  15677. (aset lflag level t))
  15678. ;; clear accumulators for deeper levels
  15679. (loop for l from (1+ level) to (1- lmax) do
  15680. (aset lsum l 0)
  15681. (aset lflag l nil)))
  15682. ((>= level last-level)
  15683. ;; add what we have here to the accumulator for this level
  15684. (aset lsum level (+ (aref lsum level)
  15685. (org-column-string-to-number (or val "0") format)))
  15686. (and valflag (aset lflag level t)))
  15687. (t (error "This should not happen")))))))
  15688. (defun org-columns-redo ()
  15689. "Construct the column display again."
  15690. (interactive)
  15691. (message "Recomputing columns...")
  15692. (save-excursion
  15693. (if (marker-position org-columns-begin-marker)
  15694. (goto-char org-columns-begin-marker))
  15695. (org-columns-remove-overlays)
  15696. (if (org-mode-p)
  15697. (call-interactively 'org-columns)
  15698. (call-interactively 'org-agenda-columns)))
  15699. (message "Recomputing columns...done"))
  15700. (defun org-columns-not-in-agenda ()
  15701. (if (eq major-mode 'org-agenda-mode)
  15702. (error "This command is only allowed in Org-mode buffers")))
  15703. (defun org-string-to-number (s)
  15704. "Convert string to number, and interpret hh:mm:ss."
  15705. (if (not (string-match ":" s))
  15706. (string-to-number s)
  15707. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15708. (while l
  15709. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15710. sum)))
  15711. (defun org-column-number-to-string (n fmt &optional printf)
  15712. "Convert a computed column number to a string value, according to FMT."
  15713. (cond
  15714. ((eq fmt 'add_times)
  15715. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15716. (format "%d:%02d" h m)))
  15717. ((eq fmt 'checkbox)
  15718. (cond ((= n (floor n)) "[X]")
  15719. ((> n 1.) "[-]")
  15720. (t "[ ]")))
  15721. (printf (format printf n))
  15722. ((eq fmt 'currency)
  15723. (format "%.2f" n))
  15724. (t (number-to-string n))))
  15725. (defun org-column-string-to-number (s fmt)
  15726. "Convert a column value to a number that can be used for column computing."
  15727. (cond
  15728. ((string-match ":" s)
  15729. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15730. (while l
  15731. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15732. sum))
  15733. ((eq fmt 'checkbox)
  15734. (if (equal s "[X]") 1. 0.000001))
  15735. (t (string-to-number s))))
  15736. (defun org-columns-uncompile-format (cfmt)
  15737. "Turn the compiled columns format back into a string representation."
  15738. (let ((rtn "") e s prop title op width fmt printf)
  15739. (while (setq e (pop cfmt))
  15740. (setq prop (car e)
  15741. title (nth 1 e)
  15742. width (nth 2 e)
  15743. op (nth 3 e)
  15744. fmt (nth 4 e)
  15745. printf (nth 5 e))
  15746. (cond
  15747. ((eq fmt 'add_times) (setq op ":"))
  15748. ((eq fmt 'checkbox) (setq op "X"))
  15749. ((eq fmt 'add_numbers) (setq op "+"))
  15750. ((eq fmt 'currency) (setq op "$")))
  15751. (if (and op printf) (setq op (concat op ";" printf)))
  15752. (if (equal title prop) (setq title nil))
  15753. (setq s (concat "%" (if width (number-to-string width))
  15754. prop
  15755. (if title (concat "(" title ")"))
  15756. (if op (concat "{" op "}"))))
  15757. (setq rtn (concat rtn " " s)))
  15758. (org-trim rtn)))
  15759. (defun org-columns-compile-format (fmt)
  15760. "Turn a column format string into an alist of specifications.
  15761. The alist has one entry for each column in the format. The elements of
  15762. that list are:
  15763. property the property
  15764. title the title field for the columns
  15765. width the column width in characters, can be nil for automatic
  15766. operator the operator if any
  15767. format the output format for computed results, derived from operator
  15768. printf a printf format for computed values"
  15769. (let ((start 0) width prop title op f printf)
  15770. (setq org-columns-current-fmt-compiled nil)
  15771. (while (string-match
  15772. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15773. fmt start)
  15774. (setq start (match-end 0)
  15775. width (match-string 1 fmt)
  15776. prop (match-string 2 fmt)
  15777. title (or (match-string 3 fmt) prop)
  15778. op (match-string 4 fmt)
  15779. f nil
  15780. printf nil)
  15781. (if width (setq width (string-to-number width)))
  15782. (when (and op (string-match ";" op))
  15783. (setq printf (substring op (match-end 0))
  15784. op (substring op 0 (match-beginning 0))))
  15785. (cond
  15786. ((equal op "+") (setq f 'add_numbers))
  15787. ((equal op "$") (setq f 'currency))
  15788. ((equal op ":") (setq f 'add_times))
  15789. ((equal op "X") (setq f 'checkbox)))
  15790. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15791. (setq org-columns-current-fmt-compiled
  15792. (nreverse org-columns-current-fmt-compiled))))
  15793. ;;; Dynamic block for Column view
  15794. (defun org-columns-capture-view ()
  15795. "Get the column view of the current buffer and return it as a list.
  15796. The list will contains the title row and all other rows. Each row is
  15797. a list of fields."
  15798. (save-excursion
  15799. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15800. (n (length title)) row tbl)
  15801. (goto-char (point-min))
  15802. (while (re-search-forward "^\\*+ " nil t)
  15803. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15804. (setq row nil)
  15805. (loop for i from 0 to (1- n) do
  15806. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15807. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15808. "")
  15809. row))
  15810. (setq row (nreverse row))
  15811. (push row tbl)))
  15812. (append (list title 'hline) (nreverse tbl)))))
  15813. (defun org-dblock-write:columnview (params)
  15814. "Write the column view table.
  15815. PARAMS is a property list of parameters:
  15816. :width enforce same column widths with <N> specifiers.
  15817. :id the :ID: property of the entry where the columns view
  15818. should be built, as a string. When `local', call locally.
  15819. When `global' call column view with the cursor at the beginning
  15820. of the buffer (usually this means that the whole buffer switches
  15821. to column view).
  15822. :hlines When t, insert a hline before each item. When a number, insert
  15823. a hline before each level <= that number.
  15824. :vlines When t, make each column a colgroup to enforce vertical lines."
  15825. (let ((pos (move-marker (make-marker) (point)))
  15826. (hlines (plist-get params :hlines))
  15827. (vlines (plist-get params :vlines))
  15828. tbl id idpos nfields tmp)
  15829. (save-excursion
  15830. (save-restriction
  15831. (when (setq id (plist-get params :id))
  15832. (cond ((not id) nil)
  15833. ((eq id 'global) (goto-char (point-min)))
  15834. ((eq id 'local) nil)
  15835. ((setq idpos (org-find-entry-with-id id))
  15836. (goto-char idpos))
  15837. (t (error "Cannot find entry with :ID: %s" id))))
  15838. (org-columns)
  15839. (setq tbl (org-columns-capture-view))
  15840. (setq nfields (length (car tbl)))
  15841. (org-columns-quit)))
  15842. (goto-char pos)
  15843. (move-marker pos nil)
  15844. (when tbl
  15845. (when (plist-get params :hlines)
  15846. (setq tmp nil)
  15847. (while tbl
  15848. (if (eq (car tbl) 'hline)
  15849. (push (pop tbl) tmp)
  15850. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15851. (if (and (not (eq (car tmp) 'hline))
  15852. (or (eq hlines t)
  15853. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15854. (push 'hline tmp)))
  15855. (push (pop tbl) tmp)))
  15856. (setq tbl (nreverse tmp)))
  15857. (when vlines
  15858. (setq tbl (mapcar (lambda (x)
  15859. (if (eq 'hline x) x (cons "" x)))
  15860. tbl))
  15861. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15862. (setq pos (point))
  15863. (insert (org-listtable-to-string tbl))
  15864. (when (plist-get params :width)
  15865. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15866. org-columns-current-widths "|")))
  15867. (goto-char pos)
  15868. (org-table-align))))
  15869. (defun org-listtable-to-string (tbl)
  15870. "Convert a listtable TBL to a string that contains the Org-mode table.
  15871. The table still need to be alligned. The resulting string has no leading
  15872. and tailing newline characters."
  15873. (mapconcat
  15874. (lambda (x)
  15875. (cond
  15876. ((listp x)
  15877. (concat "|" (mapconcat 'identity x "|") "|"))
  15878. ((eq x 'hline) "|-|")
  15879. (t (error "Garbage in listtable: %s" x))))
  15880. tbl "\n"))
  15881. (defun org-insert-columns-dblock ()
  15882. "Create a dynamic block capturing a column view table."
  15883. (interactive)
  15884. (let ((defaults '(:name "columnview" :hlines 1))
  15885. (id (completing-read
  15886. "Capture columns (local, global, entry with :ID: property) [local]: "
  15887. (append '(("global") ("local"))
  15888. (mapcar 'list (org-property-values "ID"))))))
  15889. (if (equal id "") (setq id 'local))
  15890. (if (equal id "global") (setq id 'global))
  15891. (setq defaults (append defaults (list :id id)))
  15892. (org-create-dblock defaults)
  15893. (org-update-dblock)))
  15894. ;;;; Timestamps
  15895. (defvar org-last-changed-timestamp nil)
  15896. (defvar org-time-was-given) ; dynamically scoped parameter
  15897. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15898. (defvar org-ts-what) ; dynamically scoped parameter
  15899. (defun org-time-stamp (arg)
  15900. "Prompt for a date/time and insert a time stamp.
  15901. If the user specifies a time like HH:MM, or if this command is called
  15902. with a prefix argument, the time stamp will contain date and time.
  15903. Otherwise, only the date will be included. All parts of a date not
  15904. specified by the user will be filled in from the current date/time.
  15905. So if you press just return without typing anything, the time stamp
  15906. will represent the current date/time. If there is already a timestamp
  15907. at the cursor, it will be modified."
  15908. (interactive "P")
  15909. (let* ((ts nil)
  15910. (default-time
  15911. ;; Default time is either today, or, when entering a range,
  15912. ;; the range start.
  15913. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15914. (save-excursion
  15915. (re-search-backward
  15916. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15917. (- (point) 20) t)))
  15918. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15919. (current-time)))
  15920. (default-input (and ts (org-get-compact-tod ts)))
  15921. org-time-was-given org-end-time-was-given time)
  15922. (cond
  15923. ((and (org-at-timestamp-p)
  15924. (eq last-command 'org-time-stamp)
  15925. (eq this-command 'org-time-stamp))
  15926. (insert "--")
  15927. (setq time (let ((this-command this-command))
  15928. (org-read-date arg 'totime nil nil default-time default-input)))
  15929. (org-insert-time-stamp time (or org-time-was-given arg)))
  15930. ((org-at-timestamp-p)
  15931. (setq time (let ((this-command this-command))
  15932. (org-read-date arg 'totime nil nil default-time default-input)))
  15933. (when (org-at-timestamp-p) ; just to get the match data
  15934. (replace-match "")
  15935. (setq org-last-changed-timestamp
  15936. (org-insert-time-stamp
  15937. time (or org-time-was-given arg)
  15938. nil nil nil (list org-end-time-was-given))))
  15939. (message "Timestamp updated"))
  15940. (t
  15941. (setq time (let ((this-command this-command))
  15942. (org-read-date arg 'totime nil nil default-time default-input)))
  15943. (org-insert-time-stamp time (or org-time-was-given arg)
  15944. nil nil nil (list org-end-time-was-given))))))
  15945. ;; FIXME: can we use this for something else????
  15946. ;; like computing time differences?????
  15947. (defun org-get-compact-tod (s)
  15948. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15949. (let* ((t1 (match-string 1 s))
  15950. (h1 (string-to-number (match-string 2 s)))
  15951. (m1 (string-to-number (match-string 3 s)))
  15952. (t2 (and (match-end 4) (match-string 5 s)))
  15953. (h2 (and t2 (string-to-number (match-string 6 s))))
  15954. (m2 (and t2 (string-to-number (match-string 7 s))))
  15955. dh dm)
  15956. (if (not t2)
  15957. t1
  15958. (setq dh (- h2 h1) dm (- m2 m1))
  15959. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15960. (concat t1 "+" (number-to-string dh)
  15961. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15962. (defun org-time-stamp-inactive (&optional arg)
  15963. "Insert an inactive time stamp.
  15964. An inactive time stamp is enclosed in square brackets instead of angle
  15965. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15966. does not link to the calendar and cannot be changed with the S-cursor keys.
  15967. So these are more for recording a certain time/date."
  15968. (interactive "P")
  15969. (let (org-time-was-given org-end-time-was-given time)
  15970. (setq time (org-read-date arg 'totime))
  15971. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  15972. nil nil (list org-end-time-was-given))))
  15973. (defvar org-date-ovl (org-make-overlay 1 1))
  15974. (org-overlay-put org-date-ovl 'face 'org-warning)
  15975. (org-detach-overlay org-date-ovl)
  15976. (defvar org-ans1) ; dynamically scoped parameter
  15977. (defvar org-ans2) ; dynamically scoped parameter
  15978. (defvar org-plain-time-of-day-regexp) ; defined below
  15979. (defvar org-read-date-overlay nil)
  15980. (defvar org-dcst nil) ; dynamically scoped
  15981. (defun org-read-date (&optional with-time to-time from-string prompt
  15982. default-time default-input)
  15983. "Read a date, possibly a time, and make things smooth for the user.
  15984. The prompt will suggest to enter an ISO date, but you can also enter anything
  15985. which will at least partially be understood by `parse-time-string'.
  15986. Unrecognized parts of the date will default to the current day, month, year,
  15987. hour and minute. If this command is called to replace a timestamp at point,
  15988. of to enter the second timestamp of a range, the default time is taken from the
  15989. existing stamp. For example,
  15990. 3-2-5 --> 2003-02-05
  15991. feb 15 --> currentyear-02-15
  15992. sep 12 9 --> 2009-09-12
  15993. 12:45 --> today 12:45
  15994. 22 sept 0:34 --> currentyear-09-22 0:34
  15995. 12 --> currentyear-currentmonth-12
  15996. Fri --> nearest Friday (today or later)
  15997. etc.
  15998. Furthermore you can specify a relative date by giving, as the *first* thing
  15999. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16000. change in days weeks, months, years.
  16001. With a single plus or minus, the date is relative to today. With a double
  16002. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16003. +4d --> four days from today
  16004. +4 --> same as above
  16005. +2w --> two weeks from today
  16006. ++5 --> five days from default date
  16007. The function understands only English month and weekday abbreviations,
  16008. but this can be configured with the variables `parse-time-months' and
  16009. `parse-time-weekdays'.
  16010. While prompting, a calendar is popped up - you can also select the
  16011. date with the mouse (button 1). The calendar shows a period of three
  16012. months. To scroll it to other months, use the keys `>' and `<'.
  16013. If you don't like the calendar, turn it off with
  16014. \(setq org-read-date-popup-calendar nil)
  16015. With optional argument TO-TIME, the date will immediately be converted
  16016. to an internal time.
  16017. With an optional argument WITH-TIME, the prompt will suggest to also
  16018. insert a time. Note that when WITH-TIME is not set, you can still
  16019. enter a time, and this function will inform the calling routine about
  16020. this change. The calling routine may then choose to change the format
  16021. used to insert the time stamp into the buffer to include the time.
  16022. With optional argument FROM-STRING, read from this string instead from
  16023. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16024. the time/date that is used for everything that is not specified by the
  16025. user."
  16026. (require 'parse-time)
  16027. (let* ((org-time-stamp-rounding-minutes
  16028. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16029. (org-dcst org-display-custom-times)
  16030. (ct (org-current-time))
  16031. (def (or default-time ct))
  16032. (defdecode (decode-time def))
  16033. (dummy (progn
  16034. (when (< (nth 2 defdecode) org-extend-today-until)
  16035. (setcar (nthcdr 2 defdecode) -1)
  16036. (setcar (nthcdr 1 defdecode) 59)
  16037. (setq def (apply 'encode-time defdecode)
  16038. defdecode (decode-time def)))))
  16039. (calendar-move-hook nil)
  16040. (view-diary-entries-initially nil)
  16041. (view-calendar-holidays-initially nil)
  16042. (timestr (format-time-string
  16043. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16044. (prompt (concat (if prompt (concat prompt " ") "")
  16045. (format "Date+time [%s]: " timestr)))
  16046. ans (org-ans0 "") org-ans1 org-ans2 final)
  16047. (cond
  16048. (from-string (setq ans from-string))
  16049. (org-read-date-popup-calendar
  16050. (save-excursion
  16051. (save-window-excursion
  16052. (calendar)
  16053. (calendar-forward-day (- (time-to-days def)
  16054. (calendar-absolute-from-gregorian
  16055. (calendar-current-date))))
  16056. (org-eval-in-calendar nil t)
  16057. (let* ((old-map (current-local-map))
  16058. (map (copy-keymap calendar-mode-map))
  16059. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16060. (org-defkey map (kbd "RET") 'org-calendar-select)
  16061. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16062. 'org-calendar-select-mouse)
  16063. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16064. 'org-calendar-select-mouse)
  16065. (org-defkey minibuffer-local-map [(meta shift left)]
  16066. (lambda () (interactive)
  16067. (org-eval-in-calendar '(calendar-backward-month 1))))
  16068. (org-defkey minibuffer-local-map [(meta shift right)]
  16069. (lambda () (interactive)
  16070. (org-eval-in-calendar '(calendar-forward-month 1))))
  16071. (org-defkey minibuffer-local-map [(meta shift up)]
  16072. (lambda () (interactive)
  16073. (org-eval-in-calendar '(calendar-backward-year 1))))
  16074. (org-defkey minibuffer-local-map [(meta shift down)]
  16075. (lambda () (interactive)
  16076. (org-eval-in-calendar '(calendar-forward-year 1))))
  16077. (org-defkey minibuffer-local-map [(shift up)]
  16078. (lambda () (interactive)
  16079. (org-eval-in-calendar '(calendar-backward-week 1))))
  16080. (org-defkey minibuffer-local-map [(shift down)]
  16081. (lambda () (interactive)
  16082. (org-eval-in-calendar '(calendar-forward-week 1))))
  16083. (org-defkey minibuffer-local-map [(shift left)]
  16084. (lambda () (interactive)
  16085. (org-eval-in-calendar '(calendar-backward-day 1))))
  16086. (org-defkey minibuffer-local-map [(shift right)]
  16087. (lambda () (interactive)
  16088. (org-eval-in-calendar '(calendar-forward-day 1))))
  16089. (org-defkey minibuffer-local-map ">"
  16090. (lambda () (interactive)
  16091. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16092. (org-defkey minibuffer-local-map "<"
  16093. (lambda () (interactive)
  16094. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16095. (unwind-protect
  16096. (progn
  16097. (use-local-map map)
  16098. (add-hook 'post-command-hook 'org-read-date-display)
  16099. (setq org-ans0 (read-string prompt default-input nil nil))
  16100. ;; org-ans0: from prompt
  16101. ;; org-ans1: from mouse click
  16102. ;; org-ans2: from calendar motion
  16103. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16104. (remove-hook 'post-command-hook 'org-read-date-display)
  16105. (use-local-map old-map)
  16106. (when org-read-date-overlay
  16107. (org-delete-overlay org-read-date-overlay)
  16108. (setq org-read-date-overlay nil)))))))
  16109. (t ; Naked prompt only
  16110. (unwind-protect
  16111. (setq ans (read-string prompt default-input nil timestr))
  16112. (when org-read-date-overlay
  16113. (org-delete-overlay org-read-date-overlay)
  16114. (setq org-read-date-overlay nil)))))
  16115. (setq final (org-read-date-analyze ans def defdecode))
  16116. (if to-time
  16117. (apply 'encode-time final)
  16118. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16119. (format "%04d-%02d-%02d %02d:%02d"
  16120. (nth 5 final) (nth 4 final) (nth 3 final)
  16121. (nth 2 final) (nth 1 final))
  16122. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16123. (defvar def)
  16124. (defvar defdecode)
  16125. (defvar with-time)
  16126. (defun org-read-date-display ()
  16127. "Display the currrent date prompt interpretation in the minibuffer."
  16128. (when org-read-date-display-live
  16129. (when org-read-date-overlay
  16130. (org-delete-overlay org-read-date-overlay))
  16131. (let ((p (point)))
  16132. (end-of-line 1)
  16133. (while (not (equal (buffer-substring
  16134. (max (point-min) (- (point) 4)) (point))
  16135. " "))
  16136. (insert " "))
  16137. (goto-char p))
  16138. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16139. " " (or org-ans1 org-ans2)))
  16140. (org-end-time-was-given nil)
  16141. (f (org-read-date-analyze ans def defdecode))
  16142. (fmts (if org-dcst
  16143. org-time-stamp-custom-formats
  16144. org-time-stamp-formats))
  16145. (fmt (if (or with-time
  16146. (and (boundp 'org-time-was-given) org-time-was-given))
  16147. (cdr fmts)
  16148. (car fmts)))
  16149. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16150. (when (and org-end-time-was-given
  16151. (string-match org-plain-time-of-day-regexp txt))
  16152. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16153. org-end-time-was-given
  16154. (substring txt (match-end 0)))))
  16155. (setq org-read-date-overlay
  16156. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16157. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16158. (defun org-read-date-analyze (ans def defdecode)
  16159. "Analyze the combined answer of the date prompt."
  16160. ;; FIXME: cleanup and comment
  16161. (let (delta deltan deltaw deltadef year month day
  16162. hour minute second wday pm h2 m2 tl wday1)
  16163. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16164. (setq ans (replace-match "" t t ans)
  16165. deltan (car delta)
  16166. deltaw (nth 1 delta)
  16167. deltadef (nth 2 delta)))
  16168. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16169. (when (string-match
  16170. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16171. (setq year (if (match-end 2)
  16172. (string-to-number (match-string 2 ans))
  16173. (string-to-number (format-time-string "%Y")))
  16174. month (string-to-number (match-string 3 ans))
  16175. day (string-to-number (match-string 4 ans)))
  16176. (if (< year 100) (setq year (+ 2000 year)))
  16177. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16178. t nil ans)))
  16179. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16180. ;; If there is a time with am/pm, and *no* time without it, we convert
  16181. ;; so that matching will be successful.
  16182. (loop for i from 1 to 2 do ; twice, for end time as well
  16183. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16184. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16185. (setq hour (string-to-number (match-string 1 ans))
  16186. minute (if (match-end 3)
  16187. (string-to-number (match-string 3 ans))
  16188. 0)
  16189. pm (equal ?p
  16190. (string-to-char (downcase (match-string 4 ans)))))
  16191. (if (and (= hour 12) (not pm))
  16192. (setq hour 0)
  16193. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16194. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16195. t t ans))))
  16196. ;; Check if a time range is given as a duration
  16197. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16198. (setq hour (string-to-number (match-string 1 ans))
  16199. h2 (+ hour (string-to-number (match-string 3 ans)))
  16200. minute (string-to-number (match-string 2 ans))
  16201. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16202. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16203. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16204. ;; Check if there is a time range
  16205. (when (boundp 'org-end-time-was-given)
  16206. (setq org-time-was-given nil)
  16207. (when (and (string-match org-plain-time-of-day-regexp ans)
  16208. (match-end 8))
  16209. (setq org-end-time-was-given (match-string 8 ans))
  16210. (setq ans (concat (substring ans 0 (match-beginning 7))
  16211. (substring ans (match-end 7))))))
  16212. (setq tl (parse-time-string ans)
  16213. day (or (nth 3 tl) (nth 3 defdecode))
  16214. month (or (nth 4 tl)
  16215. (if (and org-read-date-prefer-future
  16216. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16217. (1+ (nth 4 defdecode))
  16218. (nth 4 defdecode)))
  16219. year (or (nth 5 tl)
  16220. (if (and org-read-date-prefer-future
  16221. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16222. (1+ (nth 5 defdecode))
  16223. (nth 5 defdecode)))
  16224. hour (or (nth 2 tl) (nth 2 defdecode))
  16225. minute (or (nth 1 tl) (nth 1 defdecode))
  16226. second (or (nth 0 tl) 0)
  16227. wday (nth 6 tl))
  16228. (when deltan
  16229. (unless deltadef
  16230. (let ((now (decode-time (current-time))))
  16231. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16232. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16233. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16234. ((equal deltaw "m") (setq month (+ month deltan)))
  16235. ((equal deltaw "y") (setq year (+ year deltan)))))
  16236. (when (and wday (not (nth 3 tl)))
  16237. ;; Weekday was given, but no day, so pick that day in the week
  16238. ;; on or after the derived date.
  16239. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16240. (unless (equal wday wday1)
  16241. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16242. (if (and (boundp 'org-time-was-given)
  16243. (nth 2 tl))
  16244. (setq org-time-was-given t))
  16245. (if (< year 100) (setq year (+ 2000 year)))
  16246. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16247. (list second minute hour day month year)))
  16248. (defvar parse-time-weekdays)
  16249. (defun org-read-date-get-relative (s today default)
  16250. "Check string S for special relative date string.
  16251. TODAY and DEFAULT are internal times, for today and for a default.
  16252. Return shift list (N what def-flag)
  16253. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16254. N is the number of WHATs to shift.
  16255. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16256. the DEFAULT date rather than TODAY."
  16257. (when (string-match
  16258. (concat
  16259. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16260. "\\([0-9]+\\)?"
  16261. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16262. "\\([ \t]\\|$\\)") s)
  16263. (let* ((dir (if (match-end 1)
  16264. (string-to-char (substring (match-string 1 s) -1))
  16265. ?+))
  16266. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16267. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16268. (what (if (match-end 3) (match-string 3 s) "d"))
  16269. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16270. (date (if rel default today))
  16271. (wday (nth 6 (decode-time date)))
  16272. delta)
  16273. (if wday1
  16274. (progn
  16275. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16276. (if (= dir ?-) (setq delta (- delta 7)))
  16277. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16278. (list delta "d" rel))
  16279. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16280. (defun org-eval-in-calendar (form &optional keepdate)
  16281. "Eval FORM in the calendar window and return to current window.
  16282. Also, store the cursor date in variable org-ans2."
  16283. (let ((sw (selected-window)))
  16284. (select-window (get-buffer-window "*Calendar*"))
  16285. (eval form)
  16286. (when (and (not keepdate) (calendar-cursor-to-date))
  16287. (let* ((date (calendar-cursor-to-date))
  16288. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16289. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16290. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16291. (select-window sw)))
  16292. ; ;; Update the prompt to show new default date
  16293. ; (save-excursion
  16294. ; (goto-char (point-min))
  16295. ; (when (and org-ans2
  16296. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16297. ; (get-text-property (match-end 0) 'field))
  16298. ; (let ((inhibit-read-only t))
  16299. ; (replace-match (concat "[" org-ans2 "]") t t)
  16300. ; (add-text-properties (point-min) (1+ (match-end 0))
  16301. ; (text-properties-at (1+ (point-min)))))))))
  16302. (defun org-calendar-select ()
  16303. "Return to `org-read-date' with the date currently selected.
  16304. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16305. (interactive)
  16306. (when (calendar-cursor-to-date)
  16307. (let* ((date (calendar-cursor-to-date))
  16308. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16309. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16310. (if (active-minibuffer-window) (exit-minibuffer))))
  16311. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16312. "Insert a date stamp for the date given by the internal TIME.
  16313. WITH-HM means, use the stamp format that includes the time of the day.
  16314. INACTIVE means use square brackets instead of angular ones, so that the
  16315. stamp will not contribute to the agenda.
  16316. PRE and POST are optional strings to be inserted before and after the
  16317. stamp.
  16318. The command returns the inserted time stamp."
  16319. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16320. stamp)
  16321. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16322. (insert-before-markers (or pre ""))
  16323. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16324. (when (listp extra)
  16325. (setq extra (car extra))
  16326. (if (and (stringp extra)
  16327. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16328. (setq extra (format "-%02d:%02d"
  16329. (string-to-number (match-string 1 extra))
  16330. (string-to-number (match-string 2 extra))))
  16331. (setq extra nil)))
  16332. (when extra
  16333. (backward-char 1)
  16334. (insert-before-markers extra)
  16335. (forward-char 1))
  16336. (insert-before-markers (or post ""))
  16337. stamp))
  16338. (defun org-toggle-time-stamp-overlays ()
  16339. "Toggle the use of custom time stamp formats."
  16340. (interactive)
  16341. (setq org-display-custom-times (not org-display-custom-times))
  16342. (unless org-display-custom-times
  16343. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16344. (while (setq p (next-single-property-change p 'display))
  16345. (if (and (get-text-property p 'display)
  16346. (eq (get-text-property p 'face) 'org-date))
  16347. (remove-text-properties
  16348. p (setq p (next-single-property-change p 'display))
  16349. '(display t))))
  16350. (set-buffer-modified-p bmp)))
  16351. (if (featurep 'xemacs)
  16352. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16353. (org-restart-font-lock)
  16354. (setq org-table-may-need-update t)
  16355. (if org-display-custom-times
  16356. (message "Time stamps are overlayed with custom format")
  16357. (message "Time stamp overlays removed")))
  16358. (defun org-display-custom-time (beg end)
  16359. "Overlay modified time stamp format over timestamp between BED and END."
  16360. (let* ((ts (buffer-substring beg end))
  16361. t1 w1 with-hm tf time str w2 (off 0))
  16362. (save-match-data
  16363. (setq t1 (org-parse-time-string ts t))
  16364. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16365. (setq off (- (match-end 0) (match-beginning 0)))))
  16366. (setq end (- end off))
  16367. (setq w1 (- end beg)
  16368. with-hm (and (nth 1 t1) (nth 2 t1))
  16369. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16370. time (org-fix-decoded-time t1)
  16371. str (org-add-props
  16372. (format-time-string
  16373. (substring tf 1 -1) (apply 'encode-time time))
  16374. nil 'mouse-face 'highlight)
  16375. w2 (length str))
  16376. (if (not (= w2 w1))
  16377. (add-text-properties (1+ beg) (+ 2 beg)
  16378. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16379. (if (featurep 'xemacs)
  16380. (progn
  16381. (put-text-property beg end 'invisible t)
  16382. (put-text-property beg end 'end-glyph (make-glyph str)))
  16383. (put-text-property beg end 'display str))))
  16384. (defun org-translate-time (string)
  16385. "Translate all timestamps in STRING to custom format.
  16386. But do this only if the variable `org-display-custom-times' is set."
  16387. (when org-display-custom-times
  16388. (save-match-data
  16389. (let* ((start 0)
  16390. (re org-ts-regexp-both)
  16391. t1 with-hm inactive tf time str beg end)
  16392. (while (setq start (string-match re string start))
  16393. (setq beg (match-beginning 0)
  16394. end (match-end 0)
  16395. t1 (save-match-data
  16396. (org-parse-time-string (substring string beg end) t))
  16397. with-hm (and (nth 1 t1) (nth 2 t1))
  16398. inactive (equal (substring string beg (1+ beg)) "[")
  16399. tf (funcall (if with-hm 'cdr 'car)
  16400. org-time-stamp-custom-formats)
  16401. time (org-fix-decoded-time t1)
  16402. str (format-time-string
  16403. (concat
  16404. (if inactive "[" "<") (substring tf 1 -1)
  16405. (if inactive "]" ">"))
  16406. (apply 'encode-time time))
  16407. string (replace-match str t t string)
  16408. start (+ start (length str)))))))
  16409. string)
  16410. (defun org-fix-decoded-time (time)
  16411. "Set 0 instead of nil for the first 6 elements of time.
  16412. Don't touch the rest."
  16413. (let ((n 0))
  16414. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16415. (defun org-days-to-time (timestamp-string)
  16416. "Difference between TIMESTAMP-STRING and now in days."
  16417. (- (time-to-days (org-time-string-to-time timestamp-string))
  16418. (time-to-days (current-time))))
  16419. (defun org-deadline-close (timestamp-string &optional ndays)
  16420. "Is the time in TIMESTAMP-STRING close to the current date?"
  16421. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16422. (and (< (org-days-to-time timestamp-string) ndays)
  16423. (not (org-entry-is-done-p))))
  16424. (defun org-get-wdays (ts)
  16425. "Get the deadline lead time appropriate for timestring TS."
  16426. (cond
  16427. ((<= org-deadline-warning-days 0)
  16428. ;; 0 or negative, enforce this value no matter what
  16429. (- org-deadline-warning-days))
  16430. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16431. ;; lead time is specified.
  16432. (floor (* (string-to-number (match-string 1 ts))
  16433. (cdr (assoc (match-string 2 ts)
  16434. '(("d" . 1) ("w" . 7)
  16435. ("m" . 30.4) ("y" . 365.25)))))))
  16436. ;; go for the default.
  16437. (t org-deadline-warning-days)))
  16438. (defun org-calendar-select-mouse (ev)
  16439. "Return to `org-read-date' with the date currently selected.
  16440. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16441. (interactive "e")
  16442. (mouse-set-point ev)
  16443. (when (calendar-cursor-to-date)
  16444. (let* ((date (calendar-cursor-to-date))
  16445. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16446. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16447. (if (active-minibuffer-window) (exit-minibuffer))))
  16448. (defun org-check-deadlines (ndays)
  16449. "Check if there are any deadlines due or past due.
  16450. A deadline is considered due if it happens within `org-deadline-warning-days'
  16451. days from today's date. If the deadline appears in an entry marked DONE,
  16452. it is not shown. The prefix arg NDAYS can be used to test that many
  16453. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16454. (interactive "P")
  16455. (let* ((org-warn-days
  16456. (cond
  16457. ((equal ndays '(4)) 100000)
  16458. (ndays (prefix-numeric-value ndays))
  16459. (t (abs org-deadline-warning-days))))
  16460. (case-fold-search nil)
  16461. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16462. (callback
  16463. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16464. (message "%d deadlines past-due or due within %d days"
  16465. (org-occur regexp nil callback)
  16466. org-warn-days)))
  16467. (defun org-check-before-date (date)
  16468. "Check if there are deadlines or scheduled entries before DATE."
  16469. (interactive (list (org-read-date)))
  16470. (let ((case-fold-search nil)
  16471. (regexp (concat "\\<\\(" org-deadline-string
  16472. "\\|" org-scheduled-string
  16473. "\\) *<\\([^>]+\\)>"))
  16474. (callback
  16475. (lambda () (time-less-p
  16476. (org-time-string-to-time (match-string 2))
  16477. (org-time-string-to-time date)))))
  16478. (message "%d entries before %s"
  16479. (org-occur regexp nil callback) date)))
  16480. (defun org-evaluate-time-range (&optional to-buffer)
  16481. "Evaluate a time range by computing the difference between start and end.
  16482. Normally the result is just printed in the echo area, but with prefix arg
  16483. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16484. If the time range is actually in a table, the result is inserted into the
  16485. next column.
  16486. For time difference computation, a year is assumed to be exactly 365
  16487. days in order to avoid rounding problems."
  16488. (interactive "P")
  16489. (or
  16490. (org-clock-update-time-maybe)
  16491. (save-excursion
  16492. (unless (org-at-date-range-p t)
  16493. (goto-char (point-at-bol))
  16494. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16495. (if (not (org-at-date-range-p t))
  16496. (error "Not at a time-stamp range, and none found in current line")))
  16497. (let* ((ts1 (match-string 1))
  16498. (ts2 (match-string 2))
  16499. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16500. (match-end (match-end 0))
  16501. (time1 (org-time-string-to-time ts1))
  16502. (time2 (org-time-string-to-time ts2))
  16503. (t1 (time-to-seconds time1))
  16504. (t2 (time-to-seconds time2))
  16505. (diff (abs (- t2 t1)))
  16506. (negative (< (- t2 t1) 0))
  16507. ;; (ys (floor (* 365 24 60 60)))
  16508. (ds (* 24 60 60))
  16509. (hs (* 60 60))
  16510. (fy "%dy %dd %02d:%02d")
  16511. (fy1 "%dy %dd")
  16512. (fd "%dd %02d:%02d")
  16513. (fd1 "%dd")
  16514. (fh "%02d:%02d")
  16515. y d h m align)
  16516. (if havetime
  16517. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16518. y 0
  16519. d (floor (/ diff ds)) diff (mod diff ds)
  16520. h (floor (/ diff hs)) diff (mod diff hs)
  16521. m (floor (/ diff 60)))
  16522. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16523. y 0
  16524. d (floor (+ (/ diff ds) 0.5))
  16525. h 0 m 0))
  16526. (if (not to-buffer)
  16527. (message "%s" (org-make-tdiff-string y d h m))
  16528. (if (org-at-table-p)
  16529. (progn
  16530. (goto-char match-end)
  16531. (setq align t)
  16532. (and (looking-at " *|") (goto-char (match-end 0))))
  16533. (goto-char match-end))
  16534. (if (looking-at
  16535. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16536. (replace-match ""))
  16537. (if negative (insert " -"))
  16538. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16539. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16540. (insert " " (format fh h m))))
  16541. (if align (org-table-align))
  16542. (message "Time difference inserted")))))
  16543. (defun org-make-tdiff-string (y d h m)
  16544. (let ((fmt "")
  16545. (l nil))
  16546. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16547. l (push y l)))
  16548. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16549. l (push d l)))
  16550. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16551. l (push h l)))
  16552. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16553. l (push m l)))
  16554. (apply 'format fmt (nreverse l))))
  16555. (defun org-time-string-to-time (s)
  16556. (apply 'encode-time (org-parse-time-string s)))
  16557. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16558. "Convert a time stamp to an absolute day number.
  16559. If there is a specifyer for a cyclic time stamp, get the closest date to
  16560. DAYNR."
  16561. (cond
  16562. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16563. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16564. daynr
  16565. (+ daynr 1000)))
  16566. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16567. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16568. (time-to-days (current-time))) (match-string 0 s)
  16569. prefer))
  16570. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16571. (defun org-time-from-absolute (d)
  16572. "Return the time corresponding to date D.
  16573. D may be an absolute day number, or a calendar-type list (month day year)."
  16574. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16575. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16576. (defun org-calendar-holiday ()
  16577. "List of holidays, for Diary display in Org-mode."
  16578. (require 'holidays)
  16579. (let ((hl (funcall
  16580. (if (fboundp 'calendar-check-holidays)
  16581. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16582. (if hl (mapconcat 'identity hl "; "))))
  16583. (defun org-diary-sexp-entry (sexp entry date)
  16584. "Process a SEXP diary ENTRY for DATE."
  16585. (require 'diary-lib)
  16586. (let ((result (if calendar-debug-sexp
  16587. (let ((stack-trace-on-error t))
  16588. (eval (car (read-from-string sexp))))
  16589. (condition-case nil
  16590. (eval (car (read-from-string sexp)))
  16591. (error
  16592. (beep)
  16593. (message "Bad sexp at line %d in %s: %s"
  16594. (org-current-line)
  16595. (buffer-file-name) sexp)
  16596. (sleep-for 2))))))
  16597. (cond ((stringp result) result)
  16598. ((and (consp result)
  16599. (stringp (cdr result))) (cdr result))
  16600. (result entry)
  16601. (t nil))))
  16602. (defun org-diary-to-ical-string (frombuf)
  16603. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16604. This uses the icalendar.el library."
  16605. (let* ((tmpdir (if (featurep 'xemacs)
  16606. (temp-directory)
  16607. temporary-file-directory))
  16608. (tmpfile (make-temp-name
  16609. (expand-file-name "orgics" tmpdir)))
  16610. buf rtn b e)
  16611. (save-excursion
  16612. (set-buffer frombuf)
  16613. (icalendar-export-region (point-min) (point-max) tmpfile)
  16614. (setq buf (find-buffer-visiting tmpfile))
  16615. (set-buffer buf)
  16616. (goto-char (point-min))
  16617. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16618. (setq b (match-beginning 0)))
  16619. (goto-char (point-max))
  16620. (if (re-search-backward "^END:VEVENT" nil t)
  16621. (setq e (match-end 0)))
  16622. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16623. (kill-buffer buf)
  16624. (kill-buffer frombuf)
  16625. (delete-file tmpfile)
  16626. rtn))
  16627. (defun org-closest-date (start current change prefer)
  16628. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16629. When PREFER is `past' return a date that is either CURRENT or past.
  16630. When PREFER is `future', return a date that is either CURRENT or future."
  16631. ;; Make the proper lists from the dates
  16632. (catch 'exit
  16633. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16634. dn dw sday cday n1 n2
  16635. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16636. (setq start (org-date-to-gregorian start)
  16637. current (org-date-to-gregorian
  16638. (if org-agenda-repeating-timestamp-show-all
  16639. current
  16640. (time-to-days (current-time))))
  16641. sday (calendar-absolute-from-gregorian start)
  16642. cday (calendar-absolute-from-gregorian current))
  16643. (if (<= cday sday) (throw 'exit sday))
  16644. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16645. (setq dn (string-to-number (match-string 1 change))
  16646. dw (cdr (assoc (match-string 2 change) a1)))
  16647. (error "Invalid change specifyer: %s" change))
  16648. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16649. (cond
  16650. ((eq dw 'day)
  16651. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16652. n2 (+ n1 dn)))
  16653. ((eq dw 'year)
  16654. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16655. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16656. (setq date1 (list m d y1)
  16657. n1 (calendar-absolute-from-gregorian date1)
  16658. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16659. n2 (calendar-absolute-from-gregorian date2)))
  16660. ((eq dw 'month)
  16661. ;; approx number of month between the tow dates
  16662. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16663. ;; How often does dn fit in there?
  16664. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16665. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16666. m (+ m nm)
  16667. ny (floor (/ m 12))
  16668. y (+ y ny)
  16669. m (- m (* ny 12)))
  16670. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16671. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16672. (setq m2 (+ m dn) y2 y)
  16673. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16674. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16675. (while (< n2 cday)
  16676. (setq n1 n2 m m2 y y2)
  16677. (setq m2 (+ m dn) y2 y)
  16678. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16679. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16680. (if org-agenda-repeating-timestamp-show-all
  16681. (cond
  16682. ((eq prefer 'past) n1)
  16683. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16684. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16685. (cond
  16686. ((eq prefer 'past) n1)
  16687. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16688. (t (if (= cday n1) n1 n2)))))))
  16689. (defun org-date-to-gregorian (date)
  16690. "Turn any specification of DATE into a gregorian date for the calendar."
  16691. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16692. ((and (listp date) (= (length date) 3)) date)
  16693. ((stringp date)
  16694. (setq date (org-parse-time-string date))
  16695. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16696. ((listp date)
  16697. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16698. (defun org-parse-time-string (s &optional nodefault)
  16699. "Parse the standard Org-mode time string.
  16700. This should be a lot faster than the normal `parse-time-string'.
  16701. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16702. hour and minute fields will be nil if not given."
  16703. (if (string-match org-ts-regexp0 s)
  16704. (list 0
  16705. (if (or (match-beginning 8) (not nodefault))
  16706. (string-to-number (or (match-string 8 s) "0")))
  16707. (if (or (match-beginning 7) (not nodefault))
  16708. (string-to-number (or (match-string 7 s) "0")))
  16709. (string-to-number (match-string 4 s))
  16710. (string-to-number (match-string 3 s))
  16711. (string-to-number (match-string 2 s))
  16712. nil nil nil)
  16713. (make-list 9 0)))
  16714. (defun org-timestamp-up (&optional arg)
  16715. "Increase the date item at the cursor by one.
  16716. If the cursor is on the year, change the year. If it is on the month or
  16717. the day, change that.
  16718. With prefix ARG, change by that many units."
  16719. (interactive "p")
  16720. (org-timestamp-change (prefix-numeric-value arg)))
  16721. (defun org-timestamp-down (&optional arg)
  16722. "Decrease the date item at the cursor by one.
  16723. If the cursor is on the year, change the year. If it is on the month or
  16724. the day, change that.
  16725. With prefix ARG, change by that many units."
  16726. (interactive "p")
  16727. (org-timestamp-change (- (prefix-numeric-value arg))))
  16728. (defun org-timestamp-up-day (&optional arg)
  16729. "Increase the date in the time stamp by one day.
  16730. With prefix ARG, change that many days."
  16731. (interactive "p")
  16732. (if (and (not (org-at-timestamp-p t))
  16733. (org-on-heading-p))
  16734. (org-todo 'up)
  16735. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16736. (defun org-timestamp-down-day (&optional arg)
  16737. "Decrease the date in the time stamp by one day.
  16738. With prefix ARG, change that many days."
  16739. (interactive "p")
  16740. (if (and (not (org-at-timestamp-p t))
  16741. (org-on-heading-p))
  16742. (org-todo 'down)
  16743. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16744. (defsubst org-pos-in-match-range (pos n)
  16745. (and (match-beginning n)
  16746. (<= (match-beginning n) pos)
  16747. (>= (match-end n) pos)))
  16748. (defun org-at-timestamp-p (&optional inactive-ok)
  16749. "Determine if the cursor is in or at a timestamp."
  16750. (interactive)
  16751. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16752. (pos (point))
  16753. (ans (or (looking-at tsr)
  16754. (save-excursion
  16755. (skip-chars-backward "^[<\n\r\t")
  16756. (if (> (point) (point-min)) (backward-char 1))
  16757. (and (looking-at tsr)
  16758. (> (- (match-end 0) pos) -1))))))
  16759. (and ans
  16760. (boundp 'org-ts-what)
  16761. (setq org-ts-what
  16762. (cond
  16763. ((= pos (match-beginning 0)) 'bracket)
  16764. ((= pos (1- (match-end 0))) 'bracket)
  16765. ((org-pos-in-match-range pos 2) 'year)
  16766. ((org-pos-in-match-range pos 3) 'month)
  16767. ((org-pos-in-match-range pos 7) 'hour)
  16768. ((org-pos-in-match-range pos 8) 'minute)
  16769. ((or (org-pos-in-match-range pos 4)
  16770. (org-pos-in-match-range pos 5)) 'day)
  16771. ((and (> pos (or (match-end 8) (match-end 5)))
  16772. (< pos (match-end 0)))
  16773. (- pos (or (match-end 8) (match-end 5))))
  16774. (t 'day))))
  16775. ans))
  16776. (defun org-toggle-timestamp-type ()
  16777. ""
  16778. (interactive)
  16779. (when (org-at-timestamp-p t)
  16780. (save-excursion
  16781. (goto-char (match-beginning 0))
  16782. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16783. (goto-char (1- (match-end 0)))
  16784. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16785. (message "Timestamp is now %sactive"
  16786. (if (equal (char-before) ?>) "in" ""))))
  16787. (defun org-timestamp-change (n &optional what)
  16788. "Change the date in the time stamp at point.
  16789. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16790. `year', `minute', `second'. If WHAT is not given, the cursor position
  16791. in the timestamp determines what will be changed."
  16792. (let ((pos (point))
  16793. with-hm inactive
  16794. org-ts-what
  16795. extra
  16796. ts time time0)
  16797. (if (not (org-at-timestamp-p t))
  16798. (error "Not at a timestamp"))
  16799. (if (and (not what) (eq org-ts-what 'bracket))
  16800. (org-toggle-timestamp-type)
  16801. (if (and (not what) (not (eq org-ts-what 'day))
  16802. org-display-custom-times
  16803. (get-text-property (point) 'display)
  16804. (not (get-text-property (1- (point)) 'display)))
  16805. (setq org-ts-what 'day))
  16806. (setq org-ts-what (or what org-ts-what)
  16807. inactive (= (char-after (match-beginning 0)) ?\[)
  16808. ts (match-string 0))
  16809. (replace-match "")
  16810. (if (string-match
  16811. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16812. ts)
  16813. (setq extra (match-string 1 ts)))
  16814. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16815. (setq with-hm t))
  16816. (setq time0 (org-parse-time-string ts))
  16817. (setq time
  16818. (encode-time (or (car time0) 0)
  16819. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16820. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16821. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16822. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16823. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16824. (nthcdr 6 time0)))
  16825. (when (integerp org-ts-what)
  16826. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16827. (if (eq what 'calendar)
  16828. (let ((cal-date (org-get-date-from-calendar)))
  16829. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16830. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16831. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16832. (setcar time0 (or (car time0) 0))
  16833. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16834. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16835. (setq time (apply 'encode-time time0))))
  16836. (setq org-last-changed-timestamp
  16837. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16838. (org-clock-update-time-maybe)
  16839. (goto-char pos)
  16840. ;; Try to recenter the calendar window, if any
  16841. (if (and org-calendar-follow-timestamp-change
  16842. (get-buffer-window "*Calendar*" t)
  16843. (memq org-ts-what '(day month year)))
  16844. (org-recenter-calendar (time-to-days time))))))
  16845. ;; FIXME: does not yet work for lead times
  16846. (defun org-modify-ts-extra (s pos n)
  16847. "Change the different parts of the lead-time and repeat fields in timestamp."
  16848. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16849. ng h m new)
  16850. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16851. (cond
  16852. ((or (org-pos-in-match-range pos 2)
  16853. (org-pos-in-match-range pos 3))
  16854. (setq m (string-to-number (match-string 3 s))
  16855. h (string-to-number (match-string 2 s)))
  16856. (if (org-pos-in-match-range pos 2)
  16857. (setq h (+ h n))
  16858. (setq m (+ m n)))
  16859. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16860. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16861. (setq h (min 24 (max 0 h)))
  16862. (setq ng 1 new (format "-%02d:%02d" h m)))
  16863. ((org-pos-in-match-range pos 6)
  16864. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16865. ((org-pos-in-match-range pos 5)
  16866. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16867. (when ng
  16868. (setq s (concat
  16869. (substring s 0 (match-beginning ng))
  16870. new
  16871. (substring s (match-end ng))))))
  16872. s))
  16873. (defun org-recenter-calendar (date)
  16874. "If the calendar is visible, recenter it to DATE."
  16875. (let* ((win (selected-window))
  16876. (cwin (get-buffer-window "*Calendar*" t))
  16877. (calendar-move-hook nil))
  16878. (when cwin
  16879. (select-window cwin)
  16880. (calendar-goto-date (if (listp date) date
  16881. (calendar-gregorian-from-absolute date)))
  16882. (select-window win))))
  16883. (defun org-goto-calendar (&optional arg)
  16884. "Go to the Emacs calendar at the current date.
  16885. If there is a time stamp in the current line, go to that date.
  16886. A prefix ARG can be used to force the current date."
  16887. (interactive "P")
  16888. (let ((tsr org-ts-regexp) diff
  16889. (calendar-move-hook nil)
  16890. (view-calendar-holidays-initially nil)
  16891. (view-diary-entries-initially nil))
  16892. (if (or (org-at-timestamp-p)
  16893. (save-excursion
  16894. (beginning-of-line 1)
  16895. (looking-at (concat ".*" tsr))))
  16896. (let ((d1 (time-to-days (current-time)))
  16897. (d2 (time-to-days
  16898. (org-time-string-to-time (match-string 1)))))
  16899. (setq diff (- d2 d1))))
  16900. (calendar)
  16901. (calendar-goto-today)
  16902. (if (and diff (not arg)) (calendar-forward-day diff))))
  16903. (defun org-get-date-from-calendar ()
  16904. "Return a list (month day year) of date at point in calendar."
  16905. (with-current-buffer "*Calendar*"
  16906. (save-match-data
  16907. (calendar-cursor-to-date))))
  16908. (defun org-date-from-calendar ()
  16909. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16910. If there is already a time stamp at the cursor position, update it."
  16911. (interactive)
  16912. (if (org-at-timestamp-p t)
  16913. (org-timestamp-change 0 'calendar)
  16914. (let ((cal-date (org-get-date-from-calendar)))
  16915. (org-insert-time-stamp
  16916. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16917. ;; Make appt aware of appointments from the agenda
  16918. ;;;###autoload
  16919. (defun org-agenda-to-appt (&optional filter)
  16920. "Activate appointments found in `org-agenda-files'.
  16921. When prefixed, prompt for a regular expression and use it as a
  16922. filter: only add entries if they match this regular expression.
  16923. FILTER can be a string. In this case, use this string as a
  16924. regular expression to filter results.
  16925. FILTER can also be an alist, with the car of each cell being
  16926. either 'headline or 'category. For example:
  16927. '((headline \"IMPORTANT\")
  16928. (category \"Work\"))
  16929. will only add headlines containing IMPORTANT or headlines
  16930. belonging to the category \"Work\"."
  16931. (interactive "P")
  16932. (require 'calendar)
  16933. (if (equal filter '(4))
  16934. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16935. (let* ((cnt 0) ; count added events
  16936. (org-agenda-new-buffers nil)
  16937. (today (org-date-to-gregorian
  16938. (time-to-days (current-time))))
  16939. (files (org-agenda-files)) entries file)
  16940. ;; Get all entries which may contain an appt
  16941. (while (setq file (pop files))
  16942. (setq entries
  16943. (append entries
  16944. (org-agenda-get-day-entries
  16945. file today
  16946. :timestamp :scheduled :deadline))))
  16947. (setq entries (delq nil entries))
  16948. ;; Map thru entries and find if they pass thru the filter
  16949. (mapc
  16950. (lambda(x)
  16951. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16952. (cat (get-text-property 1 'org-category x))
  16953. (tod (get-text-property 1 'time-of-day x))
  16954. (ok (or (null filter)
  16955. (and (stringp filter) (string-match filter evt))
  16956. (and (listp filter)
  16957. (or (string-match
  16958. (cadr (assoc 'category filter)) cat)
  16959. (string-match
  16960. (cadr (assoc 'headline filter)) evt))))))
  16961. ;; FIXME: Shall we remove text-properties for the appt text?
  16962. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16963. (when (and ok tod)
  16964. (setq tod (number-to-string tod)
  16965. tod (when (string-match
  16966. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  16967. (concat (match-string 1 tod) ":"
  16968. (match-string 2 tod))))
  16969. (appt-add tod evt)
  16970. (setq cnt (1+ cnt))))) entries)
  16971. (org-release-buffers org-agenda-new-buffers)
  16972. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  16973. ;;; The clock for measuring work time.
  16974. (defvar org-mode-line-string "")
  16975. (put 'org-mode-line-string 'risky-local-variable t)
  16976. (defvar org-mode-line-timer nil)
  16977. (defvar org-clock-heading "")
  16978. (defvar org-clock-start-time "")
  16979. (defun org-update-mode-line ()
  16980. (let* ((delta (- (time-to-seconds (current-time))
  16981. (time-to-seconds org-clock-start-time)))
  16982. (h (floor delta 3600))
  16983. (m (floor (- delta (* 3600 h)) 60)))
  16984. (setq org-mode-line-string
  16985. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  16986. 'help-echo "Org-mode clock is running"))
  16987. (force-mode-line-update)))
  16988. (defvar org-clock-marker (make-marker)
  16989. "Marker recording the last clock-in.")
  16990. (defvar org-clock-mode-line-entry nil
  16991. "Information for the modeline about the running clock.")
  16992. (defun org-clock-in ()
  16993. "Start the clock on the current item.
  16994. If necessary, clock-out of the currently active clock."
  16995. (interactive)
  16996. (org-clock-out t)
  16997. (let (ts)
  16998. (save-excursion
  16999. (org-back-to-heading t)
  17000. (when (and org-clock-in-switch-to-state
  17001. (not (looking-at (concat outline-regexp "[ \t]*"
  17002. org-clock-in-switch-to-state
  17003. "\\>"))))
  17004. (org-todo org-clock-in-switch-to-state))
  17005. (if (and org-clock-heading-function
  17006. (functionp org-clock-heading-function))
  17007. (setq org-clock-heading (funcall org-clock-heading-function))
  17008. (if (looking-at org-complex-heading-regexp)
  17009. (setq org-clock-heading (match-string 4))
  17010. (setq org-clock-heading "???")))
  17011. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17012. (org-clock-find-position)
  17013. (insert "\n") (backward-char 1)
  17014. (indent-relative)
  17015. (insert org-clock-string " ")
  17016. (setq org-clock-start-time (current-time))
  17017. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17018. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17019. (or global-mode-string (setq global-mode-string '("")))
  17020. (or (memq 'org-mode-line-string global-mode-string)
  17021. (setq global-mode-string
  17022. (append global-mode-string '(org-mode-line-string))))
  17023. (org-update-mode-line)
  17024. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17025. (message "Clock started at %s" ts))))
  17026. (defun org-clock-find-position ()
  17027. "Find the location where the next clock line should be inserted."
  17028. (org-back-to-heading t)
  17029. (catch 'exit
  17030. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17031. (re (concat "^[ \t]*" org-clock-string))
  17032. (cnt 0)
  17033. first last)
  17034. (goto-char beg)
  17035. (when (eobp) (newline) (setq end (max (point) end)))
  17036. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17037. ;; we seem to have a CLOCK drawer, so go there.
  17038. (beginning-of-line 2)
  17039. (throw 'exit t))
  17040. ;; Lets count the CLOCK lines
  17041. (goto-char beg)
  17042. (while (re-search-forward re end t)
  17043. (setq first (or first (match-beginning 0))
  17044. last (match-beginning 0)
  17045. cnt (1+ cnt)))
  17046. (when (and (integerp org-clock-into-drawer)
  17047. (>= (1+ cnt) org-clock-into-drawer))
  17048. ;; Wrap current entries into a new drawer
  17049. (goto-char last)
  17050. (beginning-of-line 2)
  17051. (if (org-at-item-p) (org-end-of-item))
  17052. (insert ":END:\n")
  17053. (beginning-of-line 0)
  17054. (org-indent-line-function)
  17055. (goto-char first)
  17056. (insert ":CLOCK:\n")
  17057. (beginning-of-line 0)
  17058. (org-indent-line-function)
  17059. (org-flag-drawer t)
  17060. (beginning-of-line 2)
  17061. (throw 'exit nil))
  17062. (goto-char beg)
  17063. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17064. (not (equal (match-string 1) org-clock-string)))
  17065. ;; Planning info, skip to after it
  17066. (beginning-of-line 2)
  17067. (or (bolp) (newline)))
  17068. (when (eq t org-clock-into-drawer)
  17069. (insert ":CLOCK:\n:END:\n")
  17070. (beginning-of-line -1)
  17071. (org-indent-line-function)
  17072. (org-flag-drawer t)
  17073. (beginning-of-line 2)
  17074. (org-indent-line-function)))))
  17075. (defun org-clock-out (&optional fail-quietly)
  17076. "Stop the currently running clock.
  17077. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17078. (interactive)
  17079. (catch 'exit
  17080. (if (not (marker-buffer org-clock-marker))
  17081. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17082. (let (ts te s h m)
  17083. (save-excursion
  17084. (set-buffer (marker-buffer org-clock-marker))
  17085. (goto-char org-clock-marker)
  17086. (beginning-of-line 1)
  17087. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17088. (equal (match-string 1) org-clock-string))
  17089. (setq ts (match-string 2))
  17090. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17091. (goto-char (match-end 0))
  17092. (delete-region (point) (point-at-eol))
  17093. (insert "--")
  17094. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17095. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17096. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17097. h (floor (/ s 3600))
  17098. s (- s (* 3600 h))
  17099. m (floor (/ s 60))
  17100. s (- s (* 60 s)))
  17101. (insert " => " (format "%2d:%02d" h m))
  17102. (move-marker org-clock-marker nil)
  17103. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  17104. (org-log-done (org-parse-local-options logging 'org-log-done))
  17105. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  17106. (org-add-log-maybe 'clock-out))
  17107. (when org-mode-line-timer
  17108. (cancel-timer org-mode-line-timer)
  17109. (setq org-mode-line-timer nil))
  17110. (setq global-mode-string
  17111. (delq 'org-mode-line-string global-mode-string))
  17112. (force-mode-line-update)
  17113. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17114. (defun org-clock-cancel ()
  17115. "Cancel the running clock be removing the start timestamp."
  17116. (interactive)
  17117. (if (not (marker-buffer org-clock-marker))
  17118. (error "No active clock"))
  17119. (save-excursion
  17120. (set-buffer (marker-buffer org-clock-marker))
  17121. (goto-char org-clock-marker)
  17122. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17123. (setq global-mode-string
  17124. (delq 'org-mode-line-string global-mode-string))
  17125. (force-mode-line-update)
  17126. (message "Clock canceled"))
  17127. (defun org-clock-goto (&optional delete-windows)
  17128. "Go to the currently clocked-in entry."
  17129. (interactive "P")
  17130. (if (not (marker-buffer org-clock-marker))
  17131. (error "No active clock"))
  17132. (switch-to-buffer-other-window
  17133. (marker-buffer org-clock-marker))
  17134. (if delete-windows (delete-other-windows))
  17135. (goto-char org-clock-marker)
  17136. (org-show-entry)
  17137. (org-back-to-heading)
  17138. (recenter))
  17139. (defvar org-clock-file-total-minutes nil
  17140. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17141. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17142. (defun org-clock-sum (&optional tstart tend)
  17143. "Sum the times for each subtree.
  17144. Puts the resulting times in minutes as a text property on each headline."
  17145. (interactive)
  17146. (let* ((bmp (buffer-modified-p))
  17147. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17148. org-clock-string
  17149. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17150. (lmax 30)
  17151. (ltimes (make-vector lmax 0))
  17152. (t1 0)
  17153. (level 0)
  17154. ts te dt
  17155. time)
  17156. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17157. (save-excursion
  17158. (goto-char (point-max))
  17159. (while (re-search-backward re nil t)
  17160. (cond
  17161. ((match-end 2)
  17162. ;; Two time stamps
  17163. (setq ts (match-string 2)
  17164. te (match-string 3)
  17165. ts (time-to-seconds
  17166. (apply 'encode-time (org-parse-time-string ts)))
  17167. te (time-to-seconds
  17168. (apply 'encode-time (org-parse-time-string te)))
  17169. ts (if tstart (max ts tstart) ts)
  17170. te (if tend (min te tend) te)
  17171. dt (- te ts)
  17172. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17173. ((match-end 4)
  17174. ;; A naket time
  17175. (setq t1 (+ t1 (string-to-number (match-string 5))
  17176. (* 60 (string-to-number (match-string 4))))))
  17177. (t ;; A headline
  17178. (setq level (- (match-end 1) (match-beginning 1)))
  17179. (when (or (> t1 0) (> (aref ltimes level) 0))
  17180. (loop for l from 0 to level do
  17181. (aset ltimes l (+ (aref ltimes l) t1)))
  17182. (setq t1 0 time (aref ltimes level))
  17183. (loop for l from level to (1- lmax) do
  17184. (aset ltimes l 0))
  17185. (goto-char (match-beginning 0))
  17186. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17187. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17188. (set-buffer-modified-p bmp)))
  17189. (defun org-clock-display (&optional total-only)
  17190. "Show subtree times in the entire buffer.
  17191. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17192. in the echo area."
  17193. (interactive)
  17194. (org-remove-clock-overlays)
  17195. (let (time h m p)
  17196. (org-clock-sum)
  17197. (unless total-only
  17198. (save-excursion
  17199. (goto-char (point-min))
  17200. (while (or (and (equal (setq p (point)) (point-min))
  17201. (get-text-property p :org-clock-minutes))
  17202. (setq p (next-single-property-change
  17203. (point) :org-clock-minutes)))
  17204. (goto-char p)
  17205. (when (setq time (get-text-property p :org-clock-minutes))
  17206. (org-put-clock-overlay time (funcall outline-level))))
  17207. (setq h (/ org-clock-file-total-minutes 60)
  17208. m (- org-clock-file-total-minutes (* 60 h)))
  17209. ;; Arrange to remove the overlays upon next change.
  17210. (when org-remove-highlights-with-change
  17211. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17212. nil 'local))))
  17213. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17214. (defvar org-clock-overlays nil)
  17215. (make-variable-buffer-local 'org-clock-overlays)
  17216. (defun org-put-clock-overlay (time &optional level)
  17217. "Put an overlays on the current line, displaying TIME.
  17218. If LEVEL is given, prefix time with a corresponding number of stars.
  17219. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17220. will be easy to remove."
  17221. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17222. (l (if level (org-get-legal-level level 0) 0))
  17223. (off 0)
  17224. ov tx)
  17225. (move-to-column c)
  17226. (unless (eolp) (skip-chars-backward "^ \t"))
  17227. (skip-chars-backward " \t")
  17228. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17229. tx (concat (buffer-substring (1- (point)) (point))
  17230. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17231. (org-add-props (format "%s %2d:%02d%s"
  17232. (make-string l ?*) h m
  17233. (make-string (- 10 l) ?\ ))
  17234. '(face secondary-selection))
  17235. ""))
  17236. (if (not (featurep 'xemacs))
  17237. (org-overlay-put ov 'display tx)
  17238. (org-overlay-put ov 'invisible t)
  17239. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17240. (push ov org-clock-overlays)))
  17241. (defun org-remove-clock-overlays (&optional beg end noremove)
  17242. "Remove the occur highlights from the buffer.
  17243. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17244. from the `before-change-functions' in the current buffer."
  17245. (interactive)
  17246. (unless org-inhibit-highlight-removal
  17247. (mapc 'org-delete-overlay org-clock-overlays)
  17248. (setq org-clock-overlays nil)
  17249. (unless noremove
  17250. (remove-hook 'before-change-functions
  17251. 'org-remove-clock-overlays 'local))))
  17252. (defun org-clock-out-if-current ()
  17253. "Clock out if the current entry contains the running clock.
  17254. This is used to stop the clock after a TODO entry is marked DONE,
  17255. and is only done if the variable `org-clock-out-when-done' is not nil."
  17256. (when (and org-clock-out-when-done
  17257. (member state org-done-keywords)
  17258. (equal (marker-buffer org-clock-marker) (current-buffer))
  17259. (< (point) org-clock-marker)
  17260. (> (save-excursion (outline-next-heading) (point))
  17261. org-clock-marker))
  17262. ;; Clock out, but don't accept a logging message for this.
  17263. (let ((org-log-done (if (and (listp org-log-done)
  17264. (member 'clock-out org-log-done))
  17265. '(done)
  17266. org-log-done)))
  17267. (org-clock-out))))
  17268. (add-hook 'org-after-todo-state-change-hook
  17269. 'org-clock-out-if-current)
  17270. (defun org-check-running-clock ()
  17271. "Check if the current buffer contains the running clock.
  17272. If yes, offer to stop it and to save the buffer with the changes."
  17273. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17274. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17275. (buffer-name))))
  17276. (org-clock-out)
  17277. (when (y-or-n-p "Save changed buffer?")
  17278. (save-buffer))))
  17279. (defun org-clock-report (&optional arg)
  17280. "Create a table containing a report about clocked time.
  17281. If the cursor is inside an existing clocktable block, then the table
  17282. will be updated. If not, a new clocktable will be inserted.
  17283. When called with a prefix argument, move to the first clock table in the
  17284. buffer and update it."
  17285. (interactive "P")
  17286. (org-remove-clock-overlays)
  17287. (when arg
  17288. (org-find-dblock "clocktable")
  17289. (org-show-entry))
  17290. (if (org-in-clocktable-p)
  17291. (goto-char (org-in-clocktable-p))
  17292. (org-create-dblock (list :name "clocktable"
  17293. :maxlevel 2 :scope 'file)))
  17294. (org-update-dblock))
  17295. (defun org-in-clocktable-p ()
  17296. "Check if the cursor is in a clocktable."
  17297. (let ((pos (point)) start)
  17298. (save-excursion
  17299. (end-of-line 1)
  17300. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17301. (setq start (match-beginning 0))
  17302. (re-search-forward "^#\\+END:.*" nil t)
  17303. (>= (match-end 0) pos)
  17304. start))))
  17305. (defun org-clock-update-time-maybe ()
  17306. "If this is a CLOCK line, update it and return t.
  17307. Otherwise, return nil."
  17308. (interactive)
  17309. (save-excursion
  17310. (beginning-of-line 1)
  17311. (skip-chars-forward " \t")
  17312. (when (looking-at org-clock-string)
  17313. (let ((re (concat "[ \t]*" org-clock-string
  17314. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17315. "\\([ \t]*=>.*\\)?"))
  17316. ts te h m s)
  17317. (if (not (looking-at re))
  17318. nil
  17319. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17320. (end-of-line 1)
  17321. (setq ts (match-string 1)
  17322. te (match-string 2))
  17323. (setq s (- (time-to-seconds
  17324. (apply 'encode-time (org-parse-time-string te)))
  17325. (time-to-seconds
  17326. (apply 'encode-time (org-parse-time-string ts))))
  17327. h (floor (/ s 3600))
  17328. s (- s (* 3600 h))
  17329. m (floor (/ s 60))
  17330. s (- s (* 60 s)))
  17331. (insert " => " (format "%2d:%02d" h m))
  17332. t)))))
  17333. (defun org-clock-special-range (key &optional time as-strings)
  17334. "Return two times bordering a special time range.
  17335. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17336. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17337. A week starts Monday 0:00 and ends Sunday 24:00.
  17338. The range is determined relative to TIME. TIME defaults to the current time.
  17339. The return value is a cons cell with two internal times like the ones
  17340. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17341. the returned times will be formatted strings."
  17342. (let* ((tm (decode-time (or time (current-time))))
  17343. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17344. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17345. (dow (nth 6 tm))
  17346. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17347. (cond
  17348. ((eq key 'today)
  17349. (setq h 0 m 0 h1 24 m1 0))
  17350. ((eq key 'yesterday)
  17351. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17352. ((eq key 'thisweek)
  17353. (setq diff (if (= dow 0) 6 (1- dow))
  17354. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17355. ((eq key 'lastweek)
  17356. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17357. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17358. ((eq key 'thismonth)
  17359. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17360. ((eq key 'lastmonth)
  17361. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17362. ((eq key 'thisyear)
  17363. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17364. ((eq key 'lastyear)
  17365. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17366. (t (error "No such time block %s" key)))
  17367. (setq ts (encode-time s m h d month y)
  17368. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17369. (or d1 d) (or month1 month) (or y1 y)))
  17370. (setq fm (cdr org-time-stamp-formats))
  17371. (if as-strings
  17372. (cons (format-time-string fm ts) (format-time-string fm te))
  17373. (cons ts te))))
  17374. (defun org-dblock-write:clocktable (params)
  17375. "Write the standard clocktable."
  17376. (catch 'exit
  17377. (let* ((hlchars '((1 . "*") (2 . "/")))
  17378. (ins (make-marker))
  17379. (total-time nil)
  17380. (scope (plist-get params :scope))
  17381. (tostring (plist-get params :tostring))
  17382. (multifile (plist-get params :multifile))
  17383. (header (plist-get params :header))
  17384. (maxlevel (or (plist-get params :maxlevel) 3))
  17385. (step (plist-get params :step))
  17386. (emph (plist-get params :emphasize))
  17387. (ts (plist-get params :tstart))
  17388. (te (plist-get params :tend))
  17389. (block (plist-get params :block))
  17390. ipos time h m p level hlc hdl
  17391. cc beg end pos tbl)
  17392. (when step
  17393. (org-clocktable-steps params)
  17394. (throw 'exit nil))
  17395. (when block
  17396. (setq cc (org-clock-special-range block nil t)
  17397. ts (car cc) te (cdr cc)))
  17398. (if ts (setq ts (time-to-seconds
  17399. (apply 'encode-time (org-parse-time-string ts)))))
  17400. (if te (setq te (time-to-seconds
  17401. (apply 'encode-time (org-parse-time-string te)))))
  17402. (move-marker ins (point))
  17403. (setq ipos (point))
  17404. ;; Get the right scope
  17405. (setq pos (point))
  17406. (save-restriction
  17407. (cond
  17408. ((not scope))
  17409. ((eq scope 'file) (widen))
  17410. ((eq scope 'subtree) (org-narrow-to-subtree))
  17411. ((eq scope 'tree)
  17412. (while (org-up-heading-safe))
  17413. (org-narrow-to-subtree))
  17414. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17415. (symbol-name scope)))
  17416. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17417. (catch 'exit
  17418. (while (org-up-heading-safe)
  17419. (looking-at outline-regexp)
  17420. (if (<= (org-reduced-level (funcall outline-level)) level)
  17421. (throw 'exit nil))))
  17422. (org-narrow-to-subtree))
  17423. ((or (listp scope) (eq scope 'agenda))
  17424. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17425. (scope 'agenda)
  17426. (p1 (copy-sequence params))
  17427. file)
  17428. (plist-put p1 :tostring t)
  17429. (plist-put p1 :multifile t)
  17430. (plist-put p1 :scope 'file)
  17431. (org-prepare-agenda-buffers files)
  17432. (while (setq file (pop files))
  17433. (with-current-buffer (find-buffer-visiting file)
  17434. (push (org-clocktable-add-file
  17435. file (org-dblock-write:clocktable p1)) tbl)
  17436. (setq total-time (+ (or total-time 0)
  17437. org-clock-file-total-minutes)))))))
  17438. (goto-char pos)
  17439. (unless (eq scope 'agenda)
  17440. (org-clock-sum ts te)
  17441. (goto-char (point-min))
  17442. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17443. (goto-char p)
  17444. (when (setq time (get-text-property p :org-clock-minutes))
  17445. (save-excursion
  17446. (beginning-of-line 1)
  17447. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17448. (setq level (org-reduced-level
  17449. (- (match-end 1) (match-beginning 1))))
  17450. (<= level maxlevel))
  17451. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17452. hdl (match-string 2)
  17453. h (/ time 60)
  17454. m (- time (* 60 h)))
  17455. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17456. (push (concat
  17457. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17458. (make-string (1- level) ?|)
  17459. hlc (format "%d:%02d" h m) hlc
  17460. " |") tbl))))))
  17461. (setq tbl (nreverse tbl))
  17462. (if tostring
  17463. (if tbl (mapconcat 'identity tbl "\n") nil)
  17464. (goto-char ins)
  17465. (insert-before-markers
  17466. (or header
  17467. (concat
  17468. "Clock summary at ["
  17469. (substring
  17470. (format-time-string (cdr org-time-stamp-formats))
  17471. 1 -1)
  17472. "]."
  17473. (if block
  17474. (format " Considered range is /%s/." block)
  17475. "")
  17476. "\n\n"))
  17477. (if (eq scope 'agenda) "|File" "")
  17478. "|L|Headline|Time|\n")
  17479. (setq total-time (or total-time org-clock-file-total-minutes)
  17480. h (/ total-time 60)
  17481. m (- total-time (* 60 h)))
  17482. (insert-before-markers
  17483. "|-\n|"
  17484. (if (eq scope 'agenda) "|" "")
  17485. "|"
  17486. "*Total time*| "
  17487. (format "*%d:%02d*" h m)
  17488. "|\n|-\n")
  17489. (setq tbl (delq nil tbl))
  17490. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17491. (equal (substring (car tbl) 0 2) "|-"))
  17492. (pop tbl))
  17493. (insert-before-markers (mapconcat
  17494. 'identity (delq nil tbl)
  17495. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17496. (backward-delete-char 1)
  17497. (goto-char ipos)
  17498. (skip-chars-forward "^|")
  17499. (org-table-align))))))
  17500. (defun org-clocktable-steps (params)
  17501. (let* ((p1 (copy-sequence params))
  17502. (ts (plist-get p1 :tstart))
  17503. (te (plist-get p1 :tend))
  17504. (step0 (plist-get p1 :step))
  17505. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17506. (block (plist-get p1 :block))
  17507. cc)
  17508. (when block
  17509. (setq cc (org-clock-special-range block nil t)
  17510. ts (car cc) te (cdr cc)))
  17511. (if ts (setq ts (time-to-seconds
  17512. (apply 'encode-time (org-parse-time-string ts)))))
  17513. (if te (setq te (time-to-seconds
  17514. (apply 'encode-time (org-parse-time-string te)))))
  17515. (plist-put p1 :header "")
  17516. (plist-put p1 :step nil)
  17517. (plist-put p1 :block nil)
  17518. (while (< ts te)
  17519. (or (bolp) (insert "\n"))
  17520. (plist-put p1 :tstart (format-time-string
  17521. (car org-time-stamp-formats)
  17522. (seconds-to-time ts)))
  17523. (plist-put p1 :tend (format-time-string
  17524. (car org-time-stamp-formats)
  17525. (seconds-to-time (setq ts (+ ts step)))))
  17526. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17527. (plist-get p1 :tstart) "\n")
  17528. (org-dblock-write:clocktable p1)
  17529. (re-search-forward "#\\+END:")
  17530. (end-of-line 0))))
  17531. (defun org-clocktable-add-file (file table)
  17532. (if table
  17533. (let ((lines (org-split-string table "\n"))
  17534. (ff (file-name-nondirectory file)))
  17535. (mapconcat 'identity
  17536. (mapcar (lambda (x)
  17537. (if (string-match org-table-dataline-regexp x)
  17538. (concat "|" ff x)
  17539. x))
  17540. lines)
  17541. "\n"))))
  17542. ;; FIXME: I don't think anybody uses this, ask David
  17543. (defun org-collect-clock-time-entries ()
  17544. "Return an internal list with clocking information.
  17545. This list has one entry for each CLOCK interval.
  17546. FIXME: describe the elements."
  17547. (interactive)
  17548. (let ((re (concat "^[ \t]*" org-clock-string
  17549. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17550. rtn beg end next cont level title total closedp leafp
  17551. clockpos titlepos h m donep)
  17552. (save-excursion
  17553. (org-clock-sum)
  17554. (goto-char (point-min))
  17555. (while (re-search-forward re nil t)
  17556. (setq clockpos (match-beginning 0)
  17557. beg (match-string 1) end (match-string 2)
  17558. cont (match-end 0))
  17559. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17560. end (apply 'encode-time (org-parse-time-string end)))
  17561. (org-back-to-heading t)
  17562. (setq donep (org-entry-is-done-p))
  17563. (setq titlepos (point)
  17564. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17565. h (/ total 60) m (- total (* 60 h))
  17566. total (cons h m))
  17567. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17568. (setq level (- (match-end 1) (match-beginning 1))
  17569. title (org-match-string-no-properties 2))
  17570. (save-excursion (outline-next-heading) (setq next (point)))
  17571. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17572. (goto-char next)
  17573. (setq leafp (and (looking-at "^\\*+ ")
  17574. (<= (- (match-end 0) (point)) level)))
  17575. (push (list beg end clockpos closedp donep
  17576. total title titlepos level leafp)
  17577. rtn)
  17578. (goto-char cont)))
  17579. (nreverse rtn)))
  17580. ;;;; Agenda, and Diary Integration
  17581. ;;; Define the Org-agenda-mode
  17582. (defvar org-agenda-mode-map (make-sparse-keymap)
  17583. "Keymap for `org-agenda-mode'.")
  17584. (defvar org-agenda-menu) ; defined later in this file.
  17585. (defvar org-agenda-follow-mode nil)
  17586. (defvar org-agenda-show-log nil)
  17587. (defvar org-agenda-redo-command nil)
  17588. (defvar org-agenda-mode-hook nil)
  17589. (defvar org-agenda-type nil)
  17590. (defvar org-agenda-force-single-file nil)
  17591. (defun org-agenda-mode ()
  17592. "Mode for time-sorted view on action items in Org-mode files.
  17593. The following commands are available:
  17594. \\{org-agenda-mode-map}"
  17595. (interactive)
  17596. (kill-all-local-variables)
  17597. (setq org-agenda-undo-list nil
  17598. org-agenda-pending-undo-list nil)
  17599. (setq major-mode 'org-agenda-mode)
  17600. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17601. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17602. (setq mode-name "Org-Agenda")
  17603. (use-local-map org-agenda-mode-map)
  17604. (easy-menu-add org-agenda-menu)
  17605. (if org-startup-truncated (setq truncate-lines t))
  17606. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17607. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17608. ;; Make sure properties are removed when copying text
  17609. (when (boundp 'buffer-substring-filters)
  17610. (org-set-local 'buffer-substring-filters
  17611. (cons (lambda (x)
  17612. (set-text-properties 0 (length x) nil x) x)
  17613. buffer-substring-filters)))
  17614. (unless org-agenda-keep-modes
  17615. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17616. org-agenda-show-log nil))
  17617. (easy-menu-change
  17618. '("Agenda") "Agenda Files"
  17619. (append
  17620. (list
  17621. (vector
  17622. (if (get 'org-agenda-files 'org-restrict)
  17623. "Restricted to single file"
  17624. "Edit File List")
  17625. '(org-edit-agenda-file-list)
  17626. (not (get 'org-agenda-files 'org-restrict)))
  17627. "--")
  17628. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17629. (org-agenda-set-mode-name)
  17630. (apply
  17631. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17632. (list 'org-agenda-mode-hook)))
  17633. (substitute-key-definition 'undo 'org-agenda-undo
  17634. org-agenda-mode-map global-map)
  17635. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17636. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17637. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17638. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17639. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17640. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17641. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17642. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17643. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17644. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17645. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17646. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17647. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17648. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17649. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17650. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17651. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17652. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17653. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17654. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17655. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17656. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17657. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17658. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17659. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17660. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17661. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17662. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17663. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17664. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17665. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17666. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17667. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17668. (while l (org-defkey org-agenda-mode-map
  17669. (int-to-string (pop l)) 'digit-argument)))
  17670. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17671. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17672. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17673. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17674. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17675. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17676. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17677. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17678. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17679. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17680. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17681. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17682. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17683. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17684. (org-defkey org-agenda-mode-map "n" 'next-line)
  17685. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17686. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17687. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17688. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17689. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17690. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17691. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17692. (eval-after-load "calendar"
  17693. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17694. 'org-calendar-goto-agenda))
  17695. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17696. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17697. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17698. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17699. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17700. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17701. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17702. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17703. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17704. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17705. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17706. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17707. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17708. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17709. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17710. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17711. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17712. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17713. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17714. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17715. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17716. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17717. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17718. "Local keymap for agenda entries from Org-mode.")
  17719. (org-defkey org-agenda-keymap
  17720. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17721. (org-defkey org-agenda-keymap
  17722. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17723. (when org-agenda-mouse-1-follows-link
  17724. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17725. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17726. '("Agenda"
  17727. ("Agenda Files")
  17728. "--"
  17729. ["Show" org-agenda-show t]
  17730. ["Go To (other window)" org-agenda-goto t]
  17731. ["Go To (this window)" org-agenda-switch-to t]
  17732. ["Follow Mode" org-agenda-follow-mode
  17733. :style toggle :selected org-agenda-follow-mode :active t]
  17734. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17735. "--"
  17736. ["Cycle TODO" org-agenda-todo t]
  17737. ["Archive subtree" org-agenda-archive t]
  17738. ["Delete subtree" org-agenda-kill t]
  17739. "--"
  17740. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17741. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17742. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17743. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17744. "--"
  17745. ("Tags and Properties"
  17746. ["Show all Tags" org-agenda-show-tags t]
  17747. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17748. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17749. "--"
  17750. ["Column View" org-columns t])
  17751. ("Date/Schedule"
  17752. ["Schedule" org-agenda-schedule t]
  17753. ["Set Deadline" org-agenda-deadline t]
  17754. "--"
  17755. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17756. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17757. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17758. ("Clock"
  17759. ["Clock in" org-agenda-clock-in t]
  17760. ["Clock out" org-agenda-clock-out t]
  17761. ["Clock cancel" org-agenda-clock-cancel t]
  17762. ["Goto running clock" org-clock-goto t])
  17763. ("Priority"
  17764. ["Set Priority" org-agenda-priority t]
  17765. ["Increase Priority" org-agenda-priority-up t]
  17766. ["Decrease Priority" org-agenda-priority-down t]
  17767. ["Show Priority" org-agenda-show-priority t])
  17768. ("Calendar/Diary"
  17769. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17770. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17771. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17772. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17773. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17774. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17775. "--"
  17776. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17777. "--"
  17778. ("View"
  17779. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17780. :style radio :selected (equal org-agenda-ndays 1)]
  17781. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17782. :style radio :selected (equal org-agenda-ndays 7)]
  17783. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17784. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17785. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17786. :style radio :selected (member org-agenda-ndays '(365 366))]
  17787. "--"
  17788. ["Show Logbook entries" org-agenda-log-mode
  17789. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17790. ["Include Diary" org-agenda-toggle-diary
  17791. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17792. ["Use Time Grid" org-agenda-toggle-time-grid
  17793. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17794. ["Write view to file" org-write-agenda t]
  17795. ["Rebuild buffer" org-agenda-redo t]
  17796. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17797. "--"
  17798. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17799. "--"
  17800. ["Quit" org-agenda-quit t]
  17801. ["Exit and Release Buffers" org-agenda-exit t]
  17802. ))
  17803. ;;; Agenda undo
  17804. (defvar org-agenda-allow-remote-undo t
  17805. "Non-nil means, allow remote undo from the agenda buffer.")
  17806. (defvar org-agenda-undo-list nil
  17807. "List of undoable operations in the agenda since last refresh.")
  17808. (defvar org-agenda-undo-has-started-in nil
  17809. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17810. (defvar org-agenda-pending-undo-list nil
  17811. "In a series of undo commands, this is the list of remaning undo items.")
  17812. (defmacro org-if-unprotected (&rest body)
  17813. "Execute BODY if there is no `org-protected' text property at point."
  17814. (declare (debug t))
  17815. `(unless (get-text-property (point) 'org-protected)
  17816. ,@body))
  17817. (defmacro org-with-remote-undo (_buffer &rest _body)
  17818. "Execute BODY while recording undo information in two buffers."
  17819. (declare (indent 1) (debug t))
  17820. `(let ((_cline (org-current-line))
  17821. (_cmd this-command)
  17822. (_buf1 (current-buffer))
  17823. (_buf2 ,_buffer)
  17824. (_undo1 buffer-undo-list)
  17825. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17826. _c1 _c2)
  17827. ,@_body
  17828. (when org-agenda-allow-remote-undo
  17829. (setq _c1 (org-verify-change-for-undo
  17830. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17831. _c2 (org-verify-change-for-undo
  17832. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17833. (when (or _c1 _c2)
  17834. ;; make sure there are undo boundaries
  17835. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17836. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17837. ;; remember which buffer to undo
  17838. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17839. org-agenda-undo-list)))))
  17840. (defun org-agenda-undo ()
  17841. "Undo a remote editing step in the agenda.
  17842. This undoes changes both in the agenda buffer and in the remote buffer
  17843. that have been changed along."
  17844. (interactive)
  17845. (or org-agenda-allow-remote-undo
  17846. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17847. (if (not (eq this-command last-command))
  17848. (setq org-agenda-undo-has-started-in nil
  17849. org-agenda-pending-undo-list org-agenda-undo-list))
  17850. (if (not org-agenda-pending-undo-list)
  17851. (error "No further undo information"))
  17852. (let* ((entry (pop org-agenda-pending-undo-list))
  17853. buf line cmd rembuf)
  17854. (setq cmd (pop entry) line (pop entry))
  17855. (setq rembuf (nth 2 entry))
  17856. (org-with-remote-undo rembuf
  17857. (while (bufferp (setq buf (pop entry)))
  17858. (if (pop entry)
  17859. (with-current-buffer buf
  17860. (let ((last-undo-buffer buf)
  17861. (inhibit-read-only t))
  17862. (unless (memq buf org-agenda-undo-has-started-in)
  17863. (push buf org-agenda-undo-has-started-in)
  17864. (make-local-variable 'pending-undo-list)
  17865. (undo-start))
  17866. (while (and pending-undo-list
  17867. (listp pending-undo-list)
  17868. (not (car pending-undo-list)))
  17869. (pop pending-undo-list))
  17870. (undo-more 1))))))
  17871. (goto-line line)
  17872. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17873. (defun org-verify-change-for-undo (l1 l2)
  17874. "Verify that a real change occurred between the undo lists L1 and L2."
  17875. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17876. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17877. (not (eq l1 l2)))
  17878. ;;; Agenda dispatch
  17879. (defvar org-agenda-restrict nil)
  17880. (defvar org-agenda-restrict-begin (make-marker))
  17881. (defvar org-agenda-restrict-end (make-marker))
  17882. (defvar org-agenda-last-dispatch-buffer nil)
  17883. (defvar org-agenda-overriding-restriction nil)
  17884. ;;;###autoload
  17885. (defun org-agenda (arg &optional keys restriction)
  17886. "Dispatch agenda commands to collect entries to the agenda buffer.
  17887. Prompts for a command to execute. Any prefix arg will be passed
  17888. on to the selected command. The default selections are:
  17889. a Call `org-agenda-list' to display the agenda for current day or week.
  17890. t Call `org-todo-list' to display the global todo list.
  17891. T Call `org-todo-list' to display the global todo list, select only
  17892. entries with a specific TODO keyword (the user gets a prompt).
  17893. m Call `org-tags-view' to display headlines with tags matching
  17894. a condition (the user is prompted for the condition).
  17895. M Like `m', but select only TODO entries, no ordinary headlines.
  17896. L Create a timeline for the current buffer.
  17897. e Export views to associated files.
  17898. More commands can be added by configuring the variable
  17899. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17900. searches can be pre-defined in this way.
  17901. If the current buffer is in Org-mode and visiting a file, you can also
  17902. first press `<' once to indicate that the agenda should be temporarily
  17903. \(until the next use of \\[org-agenda]) restricted to the current file.
  17904. Pressing `<' twice means to restrict to the current subtree or region
  17905. \(if active)."
  17906. (interactive "P")
  17907. (catch 'exit
  17908. (let* ((prefix-descriptions nil)
  17909. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17910. (org-agenda-custom-commands
  17911. ;; normalize different versions
  17912. (delq nil
  17913. (mapcar
  17914. (lambda (x)
  17915. (cond ((stringp (cdr x))
  17916. (push x prefix-descriptions)
  17917. nil)
  17918. ((stringp (nth 1 x)) x)
  17919. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17920. (t (cons (car x) (cons "" (cdr x))))))
  17921. org-agenda-custom-commands)))
  17922. (buf (current-buffer))
  17923. (bfn (buffer-file-name (buffer-base-buffer)))
  17924. entry key type match lprops ans)
  17925. ;; Turn off restriction unless there is an overriding one
  17926. (unless org-agenda-overriding-restriction
  17927. (put 'org-agenda-files 'org-restrict nil)
  17928. (setq org-agenda-restrict nil)
  17929. (move-marker org-agenda-restrict-begin nil)
  17930. (move-marker org-agenda-restrict-end nil))
  17931. ;; Delete old local properties
  17932. (put 'org-agenda-redo-command 'org-lprops nil)
  17933. ;; Remember where this call originated
  17934. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17935. (unless keys
  17936. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17937. keys (car ans)
  17938. restriction (cdr ans)))
  17939. ;; Estabish the restriction, if any
  17940. (when (and (not org-agenda-overriding-restriction) restriction)
  17941. (put 'org-agenda-files 'org-restrict (list bfn))
  17942. (cond
  17943. ((eq restriction 'region)
  17944. (setq org-agenda-restrict t)
  17945. (move-marker org-agenda-restrict-begin (region-beginning))
  17946. (move-marker org-agenda-restrict-end (region-end)))
  17947. ((eq restriction 'subtree)
  17948. (save-excursion
  17949. (setq org-agenda-restrict t)
  17950. (org-back-to-heading t)
  17951. (move-marker org-agenda-restrict-begin (point))
  17952. (move-marker org-agenda-restrict-end
  17953. (progn (org-end-of-subtree t)))))))
  17954. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17955. ;; For example the todo list should not need it (but does...)
  17956. (cond
  17957. ((setq entry (assoc keys org-agenda-custom-commands))
  17958. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17959. (progn
  17960. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17961. (put 'org-agenda-redo-command 'org-lprops lprops)
  17962. (cond
  17963. ((eq type 'agenda)
  17964. (org-let lprops '(org-agenda-list current-prefix-arg)))
  17965. ((eq type 'alltodo)
  17966. (org-let lprops '(org-todo-list current-prefix-arg)))
  17967. ((eq type 'stuck)
  17968. (org-let lprops '(org-agenda-list-stuck-projects
  17969. current-prefix-arg)))
  17970. ((eq type 'tags)
  17971. (org-let lprops '(org-tags-view current-prefix-arg match)))
  17972. ((eq type 'tags-todo)
  17973. (org-let lprops '(org-tags-view '(4) match)))
  17974. ((eq type 'todo)
  17975. (org-let lprops '(org-todo-list match)))
  17976. ((eq type 'tags-tree)
  17977. (org-check-for-org-mode)
  17978. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  17979. ((eq type 'todo-tree)
  17980. (org-check-for-org-mode)
  17981. (org-let lprops
  17982. '(org-occur (concat "^" outline-regexp "[ \t]*"
  17983. (regexp-quote match) "\\>"))))
  17984. ((eq type 'occur-tree)
  17985. (org-check-for-org-mode)
  17986. (org-let lprops '(org-occur match)))
  17987. ((functionp type)
  17988. (org-let lprops '(funcall type match)))
  17989. ((fboundp type)
  17990. (org-let lprops '(funcall type match)))
  17991. (t (error "Invalid custom agenda command type %s" type))))
  17992. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  17993. ((equal keys "C")
  17994. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  17995. (customize-variable 'org-agenda-custom-commands))
  17996. ((equal keys "a") (call-interactively 'org-agenda-list))
  17997. ((equal keys "t") (call-interactively 'org-todo-list))
  17998. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  17999. ((equal keys "m") (call-interactively 'org-tags-view))
  18000. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18001. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18002. ((equal keys "L")
  18003. (unless (org-mode-p)
  18004. (error "This is not an Org-mode file"))
  18005. (unless restriction
  18006. (put 'org-agenda-files 'org-restrict (list bfn))
  18007. (org-call-with-arg 'org-timeline arg)))
  18008. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18009. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18010. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18011. (t (error "Invalid agenda key"))))))
  18012. (defun org-agenda-normalize-custom-commands (cmds)
  18013. (delq nil
  18014. (mapcar
  18015. (lambda (x)
  18016. (cond ((stringp (cdr x)) nil)
  18017. ((stringp (nth 1 x)) x)
  18018. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18019. (t (cons (car x) (cons "" (cdr x))))))
  18020. cmds)))
  18021. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18022. "The user interface for selecting an agenda command."
  18023. (catch 'exit
  18024. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18025. (restrict-ok (and bfn (org-mode-p)))
  18026. (region-p (org-region-active-p))
  18027. (custom org-agenda-custom-commands)
  18028. (selstring "")
  18029. restriction second-time
  18030. c entry key type match prefixes rmheader header-end custom1 desc)
  18031. (save-window-excursion
  18032. (delete-other-windows)
  18033. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18034. (erase-buffer)
  18035. (insert (eval-when-compile
  18036. (let ((header
  18037. "
  18038. Press key for an agenda command: < Buffer,subtree/region restriction
  18039. -------------------------------- > Remove restriction
  18040. a Agenda for current week or day e Export agenda views
  18041. t List of all TODO entries T Entries with special TODO kwd
  18042. m Match a TAGS query M Like m, but only TODO entries
  18043. L Timeline for current buffer # List stuck projects (!=configure)
  18044. / Multi-occur C Configure custom agenda commands
  18045. ")
  18046. (start 0))
  18047. (while (string-match
  18048. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18049. header start)
  18050. (setq start (match-end 0))
  18051. (add-text-properties (match-beginning 2) (match-end 2)
  18052. '(face bold) header))
  18053. header)))
  18054. (setq header-end (move-marker (make-marker) (point)))
  18055. (while t
  18056. (setq custom1 custom)
  18057. (when (eq rmheader t)
  18058. (goto-line 1)
  18059. (re-search-forward ":" nil t)
  18060. (delete-region (match-end 0) (point-at-eol))
  18061. (forward-char 1)
  18062. (looking-at "-+")
  18063. (delete-region (match-end 0) (point-at-eol))
  18064. (move-marker header-end (match-end 0)))
  18065. (goto-char header-end)
  18066. (delete-region (point) (point-max))
  18067. (while (setq entry (pop custom1))
  18068. (setq key (car entry) desc (nth 1 entry)
  18069. type (nth 2 entry) match (nth 3 entry))
  18070. (if (> (length key) 1)
  18071. (add-to-list 'prefixes (string-to-char key))
  18072. (insert
  18073. (format
  18074. "\n%-4s%-14s: %s"
  18075. (org-add-props (copy-sequence key)
  18076. '(face bold))
  18077. (cond
  18078. ((string-match "\\S-" desc) desc)
  18079. ((eq type 'agenda) "Agenda for current week or day")
  18080. ((eq type 'alltodo) "List of all TODO entries")
  18081. ((eq type 'stuck) "List of stuck projects")
  18082. ((eq type 'todo) "TODO keyword")
  18083. ((eq type 'tags) "Tags query")
  18084. ((eq type 'tags-todo) "Tags (TODO)")
  18085. ((eq type 'tags-tree) "Tags tree")
  18086. ((eq type 'todo-tree) "TODO kwd tree")
  18087. ((eq type 'occur-tree) "Occur tree")
  18088. ((functionp type) (if (symbolp type)
  18089. (symbol-name type)
  18090. "Lambda expression"))
  18091. (t "???"))
  18092. (cond
  18093. ((stringp match)
  18094. (org-add-props match nil 'face 'org-warning))
  18095. (match
  18096. (format "set of %d commands" (length match)))
  18097. (t ""))))))
  18098. (when prefixes
  18099. (mapc (lambda (x)
  18100. (insert
  18101. (format "\n%s %s"
  18102. (org-add-props (char-to-string x)
  18103. nil 'face 'bold)
  18104. (or (cdr (assoc (concat selstring (char-to-string x))
  18105. prefix-descriptions))
  18106. "Prefix key"))))
  18107. prefixes))
  18108. (goto-char (point-min))
  18109. (when (fboundp 'fit-window-to-buffer)
  18110. (if second-time
  18111. (if (not (pos-visible-in-window-p (point-max)))
  18112. (fit-window-to-buffer))
  18113. (setq second-time t)
  18114. (fit-window-to-buffer)))
  18115. (message "Press key for agenda command%s:"
  18116. (if (or restrict-ok org-agenda-overriding-restriction)
  18117. (if org-agenda-overriding-restriction
  18118. " (restriction lock active)"
  18119. (if restriction
  18120. (format " (restricted to %s)" restriction)
  18121. " (unrestricted)"))
  18122. ""))
  18123. (setq c (read-char-exclusive))
  18124. (message "")
  18125. (cond
  18126. ((assoc (char-to-string c) custom)
  18127. (setq selstring (concat selstring (char-to-string c)))
  18128. (throw 'exit (cons selstring restriction)))
  18129. ((memq c prefixes)
  18130. (setq selstring (concat selstring (char-to-string c))
  18131. prefixes nil
  18132. rmheader (or rmheader t)
  18133. custom (delq nil (mapcar
  18134. (lambda (x)
  18135. (if (or (= (length (car x)) 1)
  18136. (/= (string-to-char (car x)) c))
  18137. nil
  18138. (cons (substring (car x) 1) (cdr x))))
  18139. custom))))
  18140. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18141. (message "Restriction is only possible in Org-mode buffers")
  18142. (ding) (sit-for 1))
  18143. ((eq c ?1)
  18144. (org-agenda-remove-restriction-lock 'noupdate)
  18145. (setq restriction 'buffer))
  18146. ((eq c ?0)
  18147. (org-agenda-remove-restriction-lock 'noupdate)
  18148. (setq restriction (if region-p 'region 'subtree)))
  18149. ((eq c ?<)
  18150. (org-agenda-remove-restriction-lock 'noupdate)
  18151. (setq restriction
  18152. (cond
  18153. ((eq restriction 'buffer)
  18154. (if region-p 'region 'subtree))
  18155. ((memq restriction '(subtree region))
  18156. nil)
  18157. (t 'buffer))))
  18158. ((eq c ?>)
  18159. (org-agenda-remove-restriction-lock 'noupdate)
  18160. (setq restriction nil))
  18161. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18162. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18163. ((and (> (length selstring) 0) (eq c ?\d))
  18164. (delete-window)
  18165. (org-agenda-get-restriction-and-command prefix-descriptions))
  18166. ((equal c ?q) (error "Abort"))
  18167. (t (error "Invalid key %c" c))))))))
  18168. (defun org-run-agenda-series (name series)
  18169. (org-prepare-agenda name)
  18170. (let* ((org-agenda-multi t)
  18171. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18172. (cmds (car series))
  18173. (gprops (nth 1 series))
  18174. match ;; The byte compiler incorrectly complains about this. Keep it!
  18175. cmd type lprops)
  18176. (while (setq cmd (pop cmds))
  18177. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18178. (cond
  18179. ((eq type 'agenda)
  18180. (org-let2 gprops lprops
  18181. '(call-interactively 'org-agenda-list)))
  18182. ((eq type 'alltodo)
  18183. (org-let2 gprops lprops
  18184. '(call-interactively 'org-todo-list)))
  18185. ((eq type 'stuck)
  18186. (org-let2 gprops lprops
  18187. '(call-interactively 'org-agenda-list-stuck-projects)))
  18188. ((eq type 'tags)
  18189. (org-let2 gprops lprops
  18190. '(org-tags-view current-prefix-arg match)))
  18191. ((eq type 'tags-todo)
  18192. (org-let2 gprops lprops
  18193. '(org-tags-view '(4) match)))
  18194. ((eq type 'todo)
  18195. (org-let2 gprops lprops
  18196. '(org-todo-list match)))
  18197. ((fboundp type)
  18198. (org-let2 gprops lprops
  18199. '(funcall type match)))
  18200. (t (error "Invalid type in command series"))))
  18201. (widen)
  18202. (setq org-agenda-redo-command redo)
  18203. (goto-char (point-min)))
  18204. (org-finalize-agenda))
  18205. ;;;###autoload
  18206. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18207. "Run an agenda command in batch mode and send the result to STDOUT.
  18208. If CMD-KEY is a string of length 1, it is used as a key in
  18209. `org-agenda-custom-commands' and triggers this command. If it is a
  18210. longer string it is used as a tags/todo match string.
  18211. Paramters are alternating variable names and values that will be bound
  18212. before running the agenda command."
  18213. (let (pars)
  18214. (while parameters
  18215. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18216. (if (> (length cmd-key) 2)
  18217. (eval (list 'let (nreverse pars)
  18218. (list 'org-tags-view nil cmd-key)))
  18219. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18220. (set-buffer org-agenda-buffer-name)
  18221. (princ (org-encode-for-stdout (buffer-string)))))
  18222. (defun org-encode-for-stdout (string)
  18223. (if (fboundp 'encode-coding-string)
  18224. (encode-coding-string string buffer-file-coding-system)
  18225. string))
  18226. (defvar org-agenda-info nil)
  18227. ;;;###autoload
  18228. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18229. "Run an agenda command in batch mode and send the result to STDOUT.
  18230. If CMD-KEY is a string of length 1, it is used as a key in
  18231. `org-agenda-custom-commands' and triggers this command. If it is a
  18232. longer string it is used as a tags/todo match string.
  18233. Paramters are alternating variable names and values that will be bound
  18234. before running the agenda command.
  18235. The output gives a line for each selected agenda item. Each
  18236. item is a list of comma-separated values, like this:
  18237. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18238. category The category of the item
  18239. head The headline, without TODO kwd, TAGS and PRIORITY
  18240. type The type of the agenda entry, can be
  18241. todo selected in TODO match
  18242. tagsmatch selected in tags match
  18243. diary imported from diary
  18244. deadline a deadline on given date
  18245. scheduled scheduled on given date
  18246. timestamp entry has timestamp on given date
  18247. closed entry was closed on given date
  18248. upcoming-deadline warning about deadline
  18249. past-scheduled forwarded scheduled item
  18250. block entry has date block including g. date
  18251. todo The todo keyword, if any
  18252. tags All tags including inherited ones, separated by colons
  18253. date The relevant date, like 2007-2-14
  18254. time The time, like 15:00-16:50
  18255. extra Sting with extra planning info
  18256. priority-l The priority letter if any was given
  18257. priority-n The computed numerical priority
  18258. agenda-day The day in the agenda where this is listed"
  18259. (let (pars)
  18260. (while parameters
  18261. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18262. (push (list 'org-agenda-remove-tags t) pars)
  18263. (if (> (length cmd-key) 2)
  18264. (eval (list 'let (nreverse pars)
  18265. (list 'org-tags-view nil cmd-key)))
  18266. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18267. (set-buffer org-agenda-buffer-name)
  18268. (let* ((lines (org-split-string (buffer-string) "\n"))
  18269. line)
  18270. (while (setq line (pop lines))
  18271. (catch 'next
  18272. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18273. (setq org-agenda-info
  18274. (org-fix-agenda-info (text-properties-at 0 line)))
  18275. (princ
  18276. (org-encode-for-stdout
  18277. (mapconcat 'org-agenda-export-csv-mapper
  18278. '(org-category txt type todo tags date time-of-day extra
  18279. priority-letter priority agenda-day)
  18280. ",")))
  18281. (princ "\n"))))))
  18282. (defun org-fix-agenda-info (props)
  18283. "Make sure all properties on an agenda item have a canonical form,
  18284. so the export commands can easily use it."
  18285. (let (tmp re)
  18286. (when (setq tmp (plist-get props 'tags))
  18287. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18288. (when (setq tmp (plist-get props 'date))
  18289. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18290. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18291. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18292. (setq tmp (calendar-date-string tmp)))
  18293. (setq props (plist-put props 'date tmp)))
  18294. (when (setq tmp (plist-get props 'day))
  18295. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18296. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18297. (setq tmp (calendar-date-string tmp)))
  18298. (setq props (plist-put props 'day tmp))
  18299. (setq props (plist-put props 'agenda-day tmp)))
  18300. (when (setq tmp (plist-get props 'txt))
  18301. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18302. (plist-put props 'priority-letter (match-string 1 tmp))
  18303. (setq tmp (replace-match "" t t tmp)))
  18304. (when (and (setq re (plist-get props 'org-todo-regexp))
  18305. (setq re (concat "\\`\\.*" re " ?"))
  18306. (string-match re tmp))
  18307. (plist-put props 'todo (match-string 1 tmp))
  18308. (setq tmp (replace-match "" t t tmp)))
  18309. (plist-put props 'txt tmp)))
  18310. props)
  18311. (defun org-agenda-export-csv-mapper (prop)
  18312. (let ((res (plist-get org-agenda-info prop)))
  18313. (setq res
  18314. (cond
  18315. ((not res) "")
  18316. ((stringp res) res)
  18317. (t (prin1-to-string res))))
  18318. (while (string-match "," res)
  18319. (setq res (replace-match ";" t t res)))
  18320. (org-trim res)))
  18321. ;;;###autoload
  18322. (defun org-store-agenda-views (&rest parameters)
  18323. (interactive)
  18324. (eval (list 'org-batch-store-agenda-views)))
  18325. ;; FIXME, why is this a macro?????
  18326. ;;;###autoload
  18327. (defmacro org-batch-store-agenda-views (&rest parameters)
  18328. "Run all custom agenda commands that have a file argument."
  18329. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18330. (pop-up-frames nil)
  18331. (dir default-directory)
  18332. pars cmd thiscmdkey files opts)
  18333. (while parameters
  18334. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18335. (setq pars (reverse pars))
  18336. (save-window-excursion
  18337. (while cmds
  18338. (setq cmd (pop cmds)
  18339. thiscmdkey (car cmd)
  18340. opts (nth 4 cmd)
  18341. files (nth 5 cmd))
  18342. (if (stringp files) (setq files (list files)))
  18343. (when files
  18344. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18345. (list 'org-agenda nil thiscmdkey)))
  18346. (set-buffer org-agenda-buffer-name)
  18347. (while files
  18348. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18349. (list 'org-write-agenda
  18350. (expand-file-name (pop files) dir) t))))
  18351. (and (get-buffer org-agenda-buffer-name)
  18352. (kill-buffer org-agenda-buffer-name)))))))
  18353. (defun org-write-agenda (file &optional nosettings)
  18354. "Write the current buffer (an agenda view) as a file.
  18355. Depending on the extension of the file name, plain text (.txt),
  18356. HTML (.html or .htm) or Postscript (.ps) is produced.
  18357. If NOSETTINGS is given, do not scope the settings of
  18358. `org-agenda-exporter-settings' into the export commands. This is used when
  18359. the settings have already been scoped and we do not wish to overrule other,
  18360. higher priority settings."
  18361. (interactive "FWrite agenda to file: ")
  18362. (if (not (file-writable-p file))
  18363. (error "Cannot write agenda to file %s" file))
  18364. (cond
  18365. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18366. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18367. (org-let (if nosettings nil org-agenda-exporter-settings)
  18368. '(save-excursion
  18369. (save-window-excursion
  18370. (cond
  18371. ((string-match "\\.html?\\'" file)
  18372. (set-buffer (htmlize-buffer (current-buffer)))
  18373. (when (and org-agenda-export-html-style
  18374. (string-match "<style>" org-agenda-export-html-style))
  18375. ;; replace <style> section with org-agenda-export-html-style
  18376. (goto-char (point-min))
  18377. (kill-region (- (search-forward "<style") 6)
  18378. (search-forward "</style>"))
  18379. (insert org-agenda-export-html-style))
  18380. (write-file file)
  18381. (kill-buffer (current-buffer))
  18382. (message "HTML written to %s" file))
  18383. ((string-match "\\.ps\\'" file)
  18384. (ps-print-buffer-with-faces file)
  18385. (message "Postscript written to %s" file))
  18386. (t
  18387. (let ((bs (buffer-string)))
  18388. (find-file file)
  18389. (insert bs)
  18390. (save-buffer 0)
  18391. (kill-buffer (current-buffer))
  18392. (message "Plain text written to %s" file))))))
  18393. (set-buffer org-agenda-buffer-name)))
  18394. (defmacro org-no-read-only (&rest body)
  18395. "Inhibit read-only for BODY."
  18396. `(let ((inhibit-read-only t)) ,@body))
  18397. (defun org-check-for-org-mode ()
  18398. "Make sure current buffer is in org-mode. Error if not."
  18399. (or (org-mode-p)
  18400. (error "Cannot execute org-mode agenda command on buffer in %s."
  18401. major-mode)))
  18402. (defun org-fit-agenda-window ()
  18403. "Fit the window to the buffer size."
  18404. (and (memq org-agenda-window-setup '(reorganize-frame))
  18405. (fboundp 'fit-window-to-buffer)
  18406. (fit-window-to-buffer
  18407. nil
  18408. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18409. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18410. ;;; Agenda file list
  18411. (defun org-agenda-files (&optional unrestricted)
  18412. "Get the list of agenda files.
  18413. Optional UNRESTRICTED means return the full list even if a restriction
  18414. is currently in place."
  18415. (let ((files
  18416. (cond
  18417. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18418. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18419. ((listp org-agenda-files) org-agenda-files)
  18420. (t (error "Invalid value of `org-agenda-files'")))))
  18421. (setq files (apply 'append
  18422. (mapcar (lambda (f)
  18423. (if (file-directory-p f)
  18424. (directory-files f t
  18425. org-agenda-file-regexp)
  18426. (list f)))
  18427. files)))
  18428. (if org-agenda-skip-unavailable-files
  18429. (delq nil
  18430. (mapcar (function
  18431. (lambda (file)
  18432. (and (file-readable-p file) file)))
  18433. files))
  18434. files))) ; `org-check-agenda-file' will remove them from the list
  18435. (defun org-edit-agenda-file-list ()
  18436. "Edit the list of agenda files.
  18437. Depending on setup, this either uses customize to edit the variable
  18438. `org-agenda-files', or it visits the file that is holding the list. In the
  18439. latter case, the buffer is set up in a way that saving it automatically kills
  18440. the buffer and restores the previous window configuration."
  18441. (interactive)
  18442. (if (stringp org-agenda-files)
  18443. (let ((cw (current-window-configuration)))
  18444. (find-file org-agenda-files)
  18445. (org-set-local 'org-window-configuration cw)
  18446. (org-add-hook 'after-save-hook
  18447. (lambda ()
  18448. (set-window-configuration
  18449. (prog1 org-window-configuration
  18450. (kill-buffer (current-buffer))))
  18451. (org-install-agenda-files-menu)
  18452. (message "New agenda file list installed"))
  18453. nil 'local)
  18454. (message "%s" (substitute-command-keys
  18455. "Edit list and finish with \\[save-buffer]")))
  18456. (customize-variable 'org-agenda-files)))
  18457. (defun org-store-new-agenda-file-list (list)
  18458. "Set new value for the agenda file list and save it correcly."
  18459. (if (stringp org-agenda-files)
  18460. (let ((f org-agenda-files) b)
  18461. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18462. (with-temp-file f
  18463. (insert (mapconcat 'identity list "\n") "\n")))
  18464. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18465. (setq org-agenda-files list)
  18466. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18467. (defun org-read-agenda-file-list ()
  18468. "Read the list of agenda files from a file."
  18469. (when (stringp org-agenda-files)
  18470. (with-temp-buffer
  18471. (insert-file-contents org-agenda-files)
  18472. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18473. ;;;###autoload
  18474. (defun org-cycle-agenda-files ()
  18475. "Cycle through the files in `org-agenda-files'.
  18476. If the current buffer visits an agenda file, find the next one in the list.
  18477. If the current buffer does not, find the first agenda file."
  18478. (interactive)
  18479. (let* ((fs (org-agenda-files t))
  18480. (files (append fs (list (car fs))))
  18481. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18482. file)
  18483. (unless files (error "No agenda files"))
  18484. (catch 'exit
  18485. (while (setq file (pop files))
  18486. (if (equal (file-truename file) tcf)
  18487. (when (car files)
  18488. (find-file (car files))
  18489. (throw 'exit t))))
  18490. (find-file (car fs)))
  18491. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18492. (defun org-agenda-file-to-front (&optional to-end)
  18493. "Move/add the current file to the top of the agenda file list.
  18494. If the file is not present in the list, it is added to the front. If it is
  18495. present, it is moved there. With optional argument TO-END, add/move to the
  18496. end of the list."
  18497. (interactive "P")
  18498. (let ((org-agenda-skip-unavailable-files nil)
  18499. (file-alist (mapcar (lambda (x)
  18500. (cons (file-truename x) x))
  18501. (org-agenda-files t)))
  18502. (ctf (file-truename buffer-file-name))
  18503. x had)
  18504. (setq x (assoc ctf file-alist) had x)
  18505. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18506. (if to-end
  18507. (setq file-alist (append (delq x file-alist) (list x)))
  18508. (setq file-alist (cons x (delq x file-alist))))
  18509. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18510. (org-install-agenda-files-menu)
  18511. (message "File %s to %s of agenda file list"
  18512. (if had "moved" "added") (if to-end "end" "front"))))
  18513. (defun org-remove-file (&optional file)
  18514. "Remove current file from the list of files in variable `org-agenda-files'.
  18515. These are the files which are being checked for agenda entries.
  18516. Optional argument FILE means, use this file instead of the current."
  18517. (interactive)
  18518. (let* ((org-agenda-skip-unavailable-files nil)
  18519. (file (or file buffer-file-name))
  18520. (true-file (file-truename file))
  18521. (afile (abbreviate-file-name file))
  18522. (files (delq nil (mapcar
  18523. (lambda (x)
  18524. (if (equal true-file
  18525. (file-truename x))
  18526. nil x))
  18527. (org-agenda-files t)))))
  18528. (if (not (= (length files) (length (org-agenda-files t))))
  18529. (progn
  18530. (org-store-new-agenda-file-list files)
  18531. (org-install-agenda-files-menu)
  18532. (message "Removed file: %s" afile))
  18533. (message "File was not in list: %s (not removed)" afile))))
  18534. (defun org-file-menu-entry (file)
  18535. (vector file (list 'find-file file) t))
  18536. (defun org-check-agenda-file (file)
  18537. "Make sure FILE exists. If not, ask user what to do."
  18538. (when (not (file-exists-p file))
  18539. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18540. (abbreviate-file-name file))
  18541. (let ((r (downcase (read-char-exclusive))))
  18542. (cond
  18543. ((equal r ?r)
  18544. (org-remove-file file)
  18545. (throw 'nextfile t))
  18546. (t (error "Abort"))))))
  18547. ;;; Agenda prepare and finalize
  18548. (defvar org-agenda-multi nil) ; dynammically scoped
  18549. (defvar org-agenda-buffer-name "*Org Agenda*")
  18550. (defvar org-pre-agenda-window-conf nil)
  18551. (defvar org-agenda-name nil)
  18552. (defun org-prepare-agenda (&optional name)
  18553. (setq org-todo-keywords-for-agenda nil)
  18554. (setq org-done-keywords-for-agenda nil)
  18555. (if org-agenda-multi
  18556. (progn
  18557. (setq buffer-read-only nil)
  18558. (goto-char (point-max))
  18559. (unless (or (bobp) org-agenda-compact-blocks)
  18560. (insert "\n" (make-string (window-width) ?=) "\n"))
  18561. (narrow-to-region (point) (point-max)))
  18562. (org-agenda-maybe-reset-markers 'force)
  18563. (org-prepare-agenda-buffers (org-agenda-files))
  18564. (setq org-todo-keywords-for-agenda
  18565. (org-uniquify org-todo-keywords-for-agenda))
  18566. (setq org-done-keywords-for-agenda
  18567. (org-uniquify org-done-keywords-for-agenda))
  18568. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18569. (awin (get-buffer-window abuf)))
  18570. (cond
  18571. ((equal (current-buffer) abuf) nil)
  18572. (awin (select-window awin))
  18573. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18574. ((equal org-agenda-window-setup 'current-window)
  18575. (switch-to-buffer abuf))
  18576. ((equal org-agenda-window-setup 'other-window)
  18577. (org-switch-to-buffer-other-window abuf))
  18578. ((equal org-agenda-window-setup 'other-frame)
  18579. (switch-to-buffer-other-frame abuf))
  18580. ((equal org-agenda-window-setup 'reorganize-frame)
  18581. (delete-other-windows)
  18582. (org-switch-to-buffer-other-window abuf))))
  18583. (setq buffer-read-only nil)
  18584. (erase-buffer)
  18585. (org-agenda-mode)
  18586. (and name (not org-agenda-name)
  18587. (org-set-local 'org-agenda-name name)))
  18588. (setq buffer-read-only nil))
  18589. (defun org-finalize-agenda ()
  18590. "Finishing touch for the agenda buffer, called just before displaying it."
  18591. (unless org-agenda-multi
  18592. (save-excursion
  18593. (let ((inhibit-read-only t))
  18594. (goto-char (point-min))
  18595. (while (org-activate-bracket-links (point-max))
  18596. (add-text-properties (match-beginning 0) (match-end 0)
  18597. '(face org-link)))
  18598. (org-agenda-align-tags)
  18599. (unless org-agenda-with-colors
  18600. (remove-text-properties (point-min) (point-max) '(face nil))))
  18601. (if (and (boundp 'org-overriding-columns-format)
  18602. org-overriding-columns-format)
  18603. (org-set-local 'org-overriding-columns-format
  18604. org-overriding-columns-format))
  18605. (if (and (boundp 'org-agenda-view-columns-initially)
  18606. org-agenda-view-columns-initially)
  18607. (org-agenda-columns))
  18608. (when org-agenda-fontify-priorities
  18609. (org-fontify-priorities))
  18610. (run-hooks 'org-finalize-agenda-hook)
  18611. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18612. )))
  18613. (defun org-fontify-priorities ()
  18614. "Make highest priority lines bold, and lowest italic."
  18615. (interactive)
  18616. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18617. (org-delete-overlay o)))
  18618. (org-overlays-in (point-min) (point-max)))
  18619. (save-excursion
  18620. (let ((inhibit-read-only t)
  18621. b e p ov h l)
  18622. (goto-char (point-min))
  18623. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18624. (setq h (or (get-char-property (point) 'org-highest-priority)
  18625. org-highest-priority)
  18626. l (or (get-char-property (point) 'org-lowest-priority)
  18627. org-lowest-priority)
  18628. p (string-to-char (match-string 1))
  18629. b (match-beginning 0) e (point-at-eol)
  18630. ov (org-make-overlay b e))
  18631. (org-overlay-put
  18632. ov 'face
  18633. (cond ((listp org-agenda-fontify-priorities)
  18634. (cdr (assoc p org-agenda-fontify-priorities)))
  18635. ((equal p l) 'italic)
  18636. ((equal p h) 'bold)))
  18637. (org-overlay-put ov 'org-type 'org-priority)))))
  18638. (defun org-prepare-agenda-buffers (files)
  18639. "Create buffers for all agenda files, protect archived trees and comments."
  18640. (interactive)
  18641. (let ((pa '(:org-archived t))
  18642. (pc '(:org-comment t))
  18643. (pall '(:org-archived t :org-comment t))
  18644. (inhibit-read-only t)
  18645. (rea (concat ":" org-archive-tag ":"))
  18646. bmp file re)
  18647. (save-excursion
  18648. (save-restriction
  18649. (while (setq file (pop files))
  18650. (if (bufferp file)
  18651. (set-buffer file)
  18652. (org-check-agenda-file file)
  18653. (set-buffer (org-get-agenda-file-buffer file)))
  18654. (widen)
  18655. (setq bmp (buffer-modified-p))
  18656. (org-refresh-category-properties)
  18657. (setq org-todo-keywords-for-agenda
  18658. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18659. (setq org-done-keywords-for-agenda
  18660. (append org-done-keywords-for-agenda org-done-keywords))
  18661. (save-excursion
  18662. (remove-text-properties (point-min) (point-max) pall)
  18663. (when org-agenda-skip-archived-trees
  18664. (goto-char (point-min))
  18665. (while (re-search-forward rea nil t)
  18666. (if (org-on-heading-p t)
  18667. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18668. (goto-char (point-min))
  18669. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18670. (while (re-search-forward re nil t)
  18671. (add-text-properties
  18672. (match-beginning 0) (org-end-of-subtree t) pc)))
  18673. (set-buffer-modified-p bmp))))))
  18674. (defvar org-agenda-skip-function nil
  18675. "Function to be called at each match during agenda construction.
  18676. If this function returns nil, the current match should not be skipped.
  18677. Otherwise, the function must return a position from where the search
  18678. should be continued.
  18679. This may also be a Lisp form, it will be evaluated.
  18680. Never set this variable using `setq' or so, because then it will apply
  18681. to all future agenda commands. Instead, bind it with `let' to scope
  18682. it dynamically into the agenda-constructing command. A good way to set
  18683. it is through options in org-agenda-custom-commands.")
  18684. (defun org-agenda-skip ()
  18685. "Throw to `:skip' in places that should be skipped.
  18686. Also moves point to the end of the skipped region, so that search can
  18687. continue from there."
  18688. (let ((p (point-at-bol)) to fp)
  18689. (and org-agenda-skip-archived-trees
  18690. (get-text-property p :org-archived)
  18691. (org-end-of-subtree t)
  18692. (throw :skip t))
  18693. (and (get-text-property p :org-comment)
  18694. (org-end-of-subtree t)
  18695. (throw :skip t))
  18696. (if (equal (char-after p) ?#) (throw :skip t))
  18697. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18698. (consp org-agenda-skip-function))
  18699. (setq to (save-excursion
  18700. (save-match-data
  18701. (if fp
  18702. (funcall org-agenda-skip-function)
  18703. (eval org-agenda-skip-function))))))
  18704. (goto-char to)
  18705. (throw :skip t))))
  18706. (defvar org-agenda-markers nil
  18707. "List of all currently active markers created by `org-agenda'.")
  18708. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18709. "Creation time of the last agenda marker.")
  18710. (defun org-agenda-new-marker (&optional pos)
  18711. "Return a new agenda marker.
  18712. Org-mode keeps a list of these markers and resets them when they are
  18713. no longer in use."
  18714. (let ((m (copy-marker (or pos (point)))))
  18715. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18716. (push m org-agenda-markers)
  18717. m))
  18718. (defun org-agenda-maybe-reset-markers (&optional force)
  18719. "Reset markers created by `org-agenda'. But only if they are old enough."
  18720. (if (or (and force (not org-agenda-multi))
  18721. (> (- (time-to-seconds (current-time))
  18722. org-agenda-last-marker-time)
  18723. 5))
  18724. (while org-agenda-markers
  18725. (move-marker (pop org-agenda-markers) nil))))
  18726. (defun org-get-agenda-file-buffer (file)
  18727. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18728. it to the list of buffers which might be released later."
  18729. (let ((buf (org-find-base-buffer-visiting file)))
  18730. (if buf
  18731. buf ; just return it
  18732. ;; Make a new buffer and remember it
  18733. (setq buf (find-file-noselect file))
  18734. (if buf (push buf org-agenda-new-buffers))
  18735. buf)))
  18736. (defun org-release-buffers (blist)
  18737. "Release all buffers in list, asking the user for confirmation when needed.
  18738. When a buffer is unmodified, it is just killed. When modified, it is saved
  18739. \(if the user agrees) and then killed."
  18740. (let (buf file)
  18741. (while (setq buf (pop blist))
  18742. (setq file (buffer-file-name buf))
  18743. (when (and (buffer-modified-p buf)
  18744. file
  18745. (y-or-n-p (format "Save file %s? " file)))
  18746. (with-current-buffer buf (save-buffer)))
  18747. (kill-buffer buf))))
  18748. (defun org-get-category (&optional pos)
  18749. "Get the category applying to position POS."
  18750. (get-text-property (or pos (point)) 'org-category))
  18751. ;;; Agenda timeline
  18752. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18753. (defun org-timeline (&optional include-all)
  18754. "Show a time-sorted view of the entries in the current org file.
  18755. Only entries with a time stamp of today or later will be listed. With
  18756. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18757. under the current date.
  18758. If the buffer contains an active region, only check the region for
  18759. dates."
  18760. (interactive "P")
  18761. (require 'calendar)
  18762. (org-compile-prefix-format 'timeline)
  18763. (org-set-sorting-strategy 'timeline)
  18764. (let* ((dopast t)
  18765. (dotodo include-all)
  18766. (doclosed org-agenda-show-log)
  18767. (entry buffer-file-name)
  18768. (date (calendar-current-date))
  18769. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18770. (end (if (org-region-active-p) (region-end) (point-max)))
  18771. (day-numbers (org-get-all-dates beg end 'no-ranges
  18772. t doclosed ; always include today
  18773. org-timeline-show-empty-dates))
  18774. (org-deadline-warning-days 0)
  18775. (org-agenda-only-exact-dates t)
  18776. (today (time-to-days (current-time)))
  18777. (past t)
  18778. args
  18779. s e rtn d emptyp)
  18780. (setq org-agenda-redo-command
  18781. (list 'progn
  18782. (list 'org-switch-to-buffer-other-window (current-buffer))
  18783. (list 'org-timeline (list 'quote include-all))))
  18784. (if (not dopast)
  18785. ;; Remove past dates from the list of dates.
  18786. (setq day-numbers (delq nil (mapcar (lambda(x)
  18787. (if (>= x today) x nil))
  18788. day-numbers))))
  18789. (org-prepare-agenda (concat "Timeline "
  18790. (file-name-nondirectory buffer-file-name)))
  18791. (if doclosed (push :closed args))
  18792. (push :timestamp args)
  18793. (push :deadline args)
  18794. (push :scheduled args)
  18795. (push :sexp args)
  18796. (if dotodo (push :todo args))
  18797. (while (setq d (pop day-numbers))
  18798. (if (and (listp d) (eq (car d) :omitted))
  18799. (progn
  18800. (setq s (point))
  18801. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18802. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18803. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18804. (if (and (>= d today)
  18805. dopast
  18806. past)
  18807. (progn
  18808. (setq past nil)
  18809. (insert (make-string 79 ?-) "\n")))
  18810. (setq date (calendar-gregorian-from-absolute d))
  18811. (setq s (point))
  18812. (setq rtn (and (not emptyp)
  18813. (apply 'org-agenda-get-day-entries entry
  18814. date args)))
  18815. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18816. (progn
  18817. (insert
  18818. (if (stringp org-agenda-format-date)
  18819. (format-time-string org-agenda-format-date
  18820. (org-time-from-absolute date))
  18821. (funcall org-agenda-format-date date))
  18822. "\n")
  18823. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18824. (put-text-property s (1- (point)) 'org-date-line t)
  18825. (if (equal d today)
  18826. (put-text-property s (1- (point)) 'org-today t))
  18827. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18828. (put-text-property s (1- (point)) 'day d)))))
  18829. (goto-char (point-min))
  18830. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18831. (point-min)))
  18832. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18833. (org-finalize-agenda)
  18834. (setq buffer-read-only t)))
  18835. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18836. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18837. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18838. not every single day in the range. If FORCE-TODAY is non-nil, make
  18839. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18840. inactive time stamps (those in square brackets) are included.
  18841. When EMPTY is non-nil, also include days without any entries."
  18842. (let ((re (concat
  18843. (if pre-re pre-re "")
  18844. (if inactive org-ts-regexp-both org-ts-regexp)))
  18845. dates dates1 date day day1 day2 ts1 ts2)
  18846. (if force-today
  18847. (setq dates (list (time-to-days (current-time)))))
  18848. (save-excursion
  18849. (goto-char beg)
  18850. (while (re-search-forward re end t)
  18851. (setq day (time-to-days (org-time-string-to-time
  18852. (substring (match-string 1) 0 10))))
  18853. (or (memq day dates) (push day dates)))
  18854. (unless no-ranges
  18855. (goto-char beg)
  18856. (while (re-search-forward org-tr-regexp end t)
  18857. (setq ts1 (substring (match-string 1) 0 10)
  18858. ts2 (substring (match-string 2) 0 10)
  18859. day1 (time-to-days (org-time-string-to-time ts1))
  18860. day2 (time-to-days (org-time-string-to-time ts2)))
  18861. (while (< (setq day1 (1+ day1)) day2)
  18862. (or (memq day1 dates) (push day1 dates)))))
  18863. (setq dates (sort dates '<))
  18864. (when empty
  18865. (while (setq day (pop dates))
  18866. (setq day2 (car dates))
  18867. (push day dates1)
  18868. (when (and day2 empty)
  18869. (if (or (eq empty t)
  18870. (and (numberp empty) (<= (- day2 day) empty)))
  18871. (while (< (setq day (1+ day)) day2)
  18872. (push (list day) dates1))
  18873. (push (cons :omitted (- day2 day)) dates1))))
  18874. (setq dates (nreverse dates1)))
  18875. dates)))
  18876. ;;; Agenda Daily/Weekly
  18877. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18878. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18879. (defvar org-agenda-last-arguments nil
  18880. "The arguments of the previous call to org-agenda")
  18881. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18882. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18883. (defvar org-include-all-loc nil) ; local variable
  18884. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18885. ;;;###autoload
  18886. (defun org-agenda-list (&optional include-all start-day ndays)
  18887. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18888. The view will be for the current day or week, but from the overview buffer
  18889. you will be able to go to other days/weeks.
  18890. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18891. all unfinished TODO items will also be shown, before the agenda.
  18892. This feature is considered obsolete, please use the TODO list or a block
  18893. agenda instead.
  18894. With a numeric prefix argument in an interactive call, the agenda will
  18895. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18896. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18897. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18898. given in `org-agenda-start-on-weekday'."
  18899. (interactive "P")
  18900. (if (and (integerp include-all) (> include-all 0))
  18901. (setq ndays include-all include-all nil))
  18902. (setq ndays (or ndays org-agenda-ndays)
  18903. start-day (or start-day org-agenda-start-day))
  18904. (if org-agenda-overriding-arguments
  18905. (setq include-all (car org-agenda-overriding-arguments)
  18906. start-day (nth 1 org-agenda-overriding-arguments)
  18907. ndays (nth 2 org-agenda-overriding-arguments)))
  18908. (if (stringp start-day)
  18909. ;; Convert to an absolute day number
  18910. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18911. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18912. (org-compile-prefix-format 'agenda)
  18913. (org-set-sorting-strategy 'agenda)
  18914. (require 'calendar)
  18915. (let* ((org-agenda-start-on-weekday
  18916. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18917. org-agenda-start-on-weekday nil))
  18918. (thefiles (org-agenda-files))
  18919. (files thefiles)
  18920. (today (time-to-days
  18921. (time-subtract (current-time)
  18922. (list 0 (* 3600 org-extend-today-until) 0))))
  18923. (sd (or start-day today))
  18924. (start (if (or (null org-agenda-start-on-weekday)
  18925. (< org-agenda-ndays 7))
  18926. sd
  18927. (let* ((nt (calendar-day-of-week
  18928. (calendar-gregorian-from-absolute sd)))
  18929. (n1 org-agenda-start-on-weekday)
  18930. (d (- nt n1)))
  18931. (- sd (+ (if (< d 0) 7 0) d)))))
  18932. (day-numbers (list start))
  18933. (day-cnt 0)
  18934. (inhibit-redisplay (not debug-on-error))
  18935. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18936. (setq org-agenda-redo-command
  18937. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18938. ;; Make the list of days
  18939. (setq ndays (or ndays org-agenda-ndays)
  18940. nd ndays)
  18941. (while (> ndays 1)
  18942. (push (1+ (car day-numbers)) day-numbers)
  18943. (setq ndays (1- ndays)))
  18944. (setq day-numbers (nreverse day-numbers))
  18945. (org-prepare-agenda "Day/Week")
  18946. (org-set-local 'org-starting-day (car day-numbers))
  18947. (org-set-local 'org-include-all-loc include-all)
  18948. (org-set-local 'org-agenda-span
  18949. (org-agenda-ndays-to-span nd))
  18950. (when (and (or include-all org-agenda-include-all-todo)
  18951. (member today day-numbers))
  18952. (setq files thefiles
  18953. rtnall nil)
  18954. (while (setq file (pop files))
  18955. (catch 'nextfile
  18956. (org-check-agenda-file file)
  18957. (setq date (calendar-gregorian-from-absolute today)
  18958. rtn (org-agenda-get-day-entries
  18959. file date :todo))
  18960. (setq rtnall (append rtnall rtn))))
  18961. (when rtnall
  18962. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18963. (add-text-properties (point-min) (1- (point))
  18964. (list 'face 'org-agenda-structure))
  18965. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18966. (unless org-agenda-compact-blocks
  18967. (setq s (point))
  18968. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  18969. "-agenda:\n")
  18970. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  18971. 'org-date-line t)))
  18972. (while (setq d (pop day-numbers))
  18973. (setq date (calendar-gregorian-from-absolute d)
  18974. s (point))
  18975. (if (or (setq todayp (= d today))
  18976. (and (not start-pos) (= d sd)))
  18977. (setq start-pos (point))
  18978. (if (and start-pos (not end-pos))
  18979. (setq end-pos (point))))
  18980. (setq files thefiles
  18981. rtnall nil)
  18982. (while (setq file (pop files))
  18983. (catch 'nextfile
  18984. (org-check-agenda-file file)
  18985. (if org-agenda-show-log
  18986. (setq rtn (org-agenda-get-day-entries
  18987. file date
  18988. :deadline :scheduled :timestamp :sexp :closed))
  18989. (setq rtn (org-agenda-get-day-entries
  18990. file date
  18991. :deadline :scheduled :sexp :timestamp)))
  18992. (setq rtnall (append rtnall rtn))))
  18993. (if org-agenda-include-diary
  18994. (progn
  18995. (require 'diary-lib)
  18996. (setq rtn (org-get-entries-from-diary date))
  18997. (setq rtnall (append rtnall rtn))))
  18998. (if (or rtnall org-agenda-show-all-dates)
  18999. (progn
  19000. (setq day-cnt (1+ day-cnt))
  19001. (insert
  19002. (if (stringp org-agenda-format-date)
  19003. (format-time-string org-agenda-format-date
  19004. (org-time-from-absolute date))
  19005. (funcall org-agenda-format-date date))
  19006. "\n")
  19007. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19008. (put-text-property s (1- (point)) 'org-date-line t)
  19009. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19010. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19011. (if rtnall (insert
  19012. (org-finalize-agenda-entries
  19013. (org-agenda-add-time-grid-maybe
  19014. rtnall nd todayp))
  19015. "\n"))
  19016. (put-text-property s (1- (point)) 'day d)
  19017. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19018. (goto-char (point-min))
  19019. (org-fit-agenda-window)
  19020. (unless (and (pos-visible-in-window-p (point-min))
  19021. (pos-visible-in-window-p (point-max)))
  19022. (goto-char (1- (point-max)))
  19023. (recenter -1)
  19024. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19025. (progn
  19026. (goto-char (or start-pos 1))
  19027. (recenter 1))))
  19028. (goto-char (or start-pos 1))
  19029. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19030. (org-finalize-agenda)
  19031. (setq buffer-read-only t)
  19032. (message "")))
  19033. (defun org-agenda-ndays-to-span (n)
  19034. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19035. ;;; Agenda TODO list
  19036. (defvar org-select-this-todo-keyword nil)
  19037. (defvar org-last-arg nil)
  19038. ;;;###autoload
  19039. (defun org-todo-list (arg)
  19040. "Show all TODO entries from all agenda file in a single list.
  19041. The prefix arg can be used to select a specific TODO keyword and limit
  19042. the list to these. When using \\[universal-argument], you will be prompted
  19043. for a keyword. A numeric prefix directly selects the Nth keyword in
  19044. `org-todo-keywords-1'."
  19045. (interactive "P")
  19046. (require 'calendar)
  19047. (org-compile-prefix-format 'todo)
  19048. (org-set-sorting-strategy 'todo)
  19049. (org-prepare-agenda "TODO")
  19050. (let* ((today (time-to-days (current-time)))
  19051. (date (calendar-gregorian-from-absolute today))
  19052. (kwds org-todo-keywords-for-agenda)
  19053. (completion-ignore-case t)
  19054. (org-select-this-todo-keyword
  19055. (if (stringp arg) arg
  19056. (and arg (integerp arg) (> arg 0)
  19057. (nth (1- arg) kwds))))
  19058. rtn rtnall files file pos)
  19059. (when (equal arg '(4))
  19060. (setq org-select-this-todo-keyword
  19061. (completing-read "Keyword (or KWD1|K2D2|...): "
  19062. (mapcar 'list kwds) nil nil)))
  19063. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19064. (org-set-local 'org-last-arg arg)
  19065. (setq org-agenda-redo-command
  19066. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19067. (setq files (org-agenda-files)
  19068. rtnall nil)
  19069. (while (setq file (pop files))
  19070. (catch 'nextfile
  19071. (org-check-agenda-file file)
  19072. (setq rtn (org-agenda-get-day-entries file date :todo))
  19073. (setq rtnall (append rtnall rtn))))
  19074. (if org-agenda-overriding-header
  19075. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19076. nil 'face 'org-agenda-structure) "\n")
  19077. (insert "Global list of TODO items of type: ")
  19078. (add-text-properties (point-min) (1- (point))
  19079. (list 'face 'org-agenda-structure))
  19080. (setq pos (point))
  19081. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19082. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19083. (setq pos (point))
  19084. (unless org-agenda-multi
  19085. (insert "Available with `N r': (0)ALL")
  19086. (let ((n 0) s)
  19087. (mapc (lambda (x)
  19088. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19089. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19090. (insert "\n "))
  19091. (insert " " s))
  19092. kwds))
  19093. (insert "\n"))
  19094. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19095. (when rtnall
  19096. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19097. (goto-char (point-min))
  19098. (org-fit-agenda-window)
  19099. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19100. (org-finalize-agenda)
  19101. (setq buffer-read-only t)))
  19102. ;;; Agenda tags match
  19103. ;;;###autoload
  19104. (defun org-tags-view (&optional todo-only match)
  19105. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19106. The prefix arg TODO-ONLY limits the search to TODO entries."
  19107. (interactive "P")
  19108. (org-compile-prefix-format 'tags)
  19109. (org-set-sorting-strategy 'tags)
  19110. (let* ((org-tags-match-list-sublevels
  19111. (if todo-only t org-tags-match-list-sublevels))
  19112. (completion-ignore-case t)
  19113. rtn rtnall files file pos matcher
  19114. buffer)
  19115. (setq matcher (org-make-tags-matcher match)
  19116. match (car matcher) matcher (cdr matcher))
  19117. (org-prepare-agenda (concat "TAGS " match))
  19118. (setq org-agenda-redo-command
  19119. (list 'org-tags-view (list 'quote todo-only)
  19120. (list 'if 'current-prefix-arg nil match)))
  19121. (setq files (org-agenda-files)
  19122. rtnall nil)
  19123. (while (setq file (pop files))
  19124. (catch 'nextfile
  19125. (org-check-agenda-file file)
  19126. (setq buffer (if (file-exists-p file)
  19127. (org-get-agenda-file-buffer file)
  19128. (error "No such file %s" file)))
  19129. (if (not buffer)
  19130. ;; If file does not exist, merror message to agenda
  19131. (setq rtn (list
  19132. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19133. rtnall (append rtnall rtn))
  19134. (with-current-buffer buffer
  19135. (unless (org-mode-p)
  19136. (error "Agenda file %s is not in `org-mode'" file))
  19137. (save-excursion
  19138. (save-restriction
  19139. (if org-agenda-restrict
  19140. (narrow-to-region org-agenda-restrict-begin
  19141. org-agenda-restrict-end)
  19142. (widen))
  19143. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19144. (setq rtnall (append rtnall rtn))))))))
  19145. (if org-agenda-overriding-header
  19146. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19147. nil 'face 'org-agenda-structure) "\n")
  19148. (insert "Headlines with TAGS match: ")
  19149. (add-text-properties (point-min) (1- (point))
  19150. (list 'face 'org-agenda-structure))
  19151. (setq pos (point))
  19152. (insert match "\n")
  19153. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19154. (setq pos (point))
  19155. (unless org-agenda-multi
  19156. (insert "Press `C-u r' to search again with new search string\n"))
  19157. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19158. (when rtnall
  19159. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19160. (goto-char (point-min))
  19161. (org-fit-agenda-window)
  19162. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19163. (org-finalize-agenda)
  19164. (setq buffer-read-only t)))
  19165. ;;; Agenda Finding stuck projects
  19166. (defvar org-agenda-skip-regexp nil
  19167. "Regular expression used in skipping subtrees for the agenda.
  19168. This is basically a temporary global variable that can be set and then
  19169. used by user-defined selections using `org-agenda-skip-function'.")
  19170. (defvar org-agenda-overriding-header nil
  19171. "When this is set during todo and tags searches, will replace header.")
  19172. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19173. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19174. If yes, it returns the end position of this tree, causing agenda commands
  19175. to skip this subtree. This is a function that can be put into
  19176. `org-agenda-skip-function' for the duration of a command."
  19177. (let ((end (save-excursion (org-end-of-subtree t)))
  19178. skip)
  19179. (save-excursion
  19180. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19181. (and skip end)))
  19182. (defun org-agenda-skip-entry-if (&rest conditions)
  19183. "Skip entry if any of CONDITIONS is true.
  19184. See `org-agenda-skip-if' for details."
  19185. (org-agenda-skip-if nil conditions))
  19186. (defun org-agenda-skip-subtree-if (&rest conditions)
  19187. "Skip entry if any of CONDITIONS is true.
  19188. See `org-agenda-skip-if' for details."
  19189. (org-agenda-skip-if t conditions))
  19190. (defun org-agenda-skip-if (subtree conditions)
  19191. "Checks current entity for CONDITIONS.
  19192. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19193. the entry, i.e. the text before the next heading is checked.
  19194. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19195. from different tests. Valid conditions are:
  19196. scheduled Check if there is a scheduled cookie
  19197. notscheduled Check if there is no scheduled cookie
  19198. deadline Check if there is a deadline
  19199. notdeadline Check if there is no deadline
  19200. regexp Check if regexp matches
  19201. notregexp Check if regexp does not match.
  19202. The regexp is taken from the conditions list, it must come right after
  19203. the `regexp' or `notregexp' element.
  19204. If any of these conditions is met, this function returns the end point of
  19205. the entity, causing the search to continue from there. This is a function
  19206. that can be put into `org-agenda-skip-function' for the duration of a command."
  19207. (let (beg end m)
  19208. (org-back-to-heading t)
  19209. (setq beg (point)
  19210. end (if subtree
  19211. (progn (org-end-of-subtree t) (point))
  19212. (progn (outline-next-heading) (1- (point)))))
  19213. (goto-char beg)
  19214. (and
  19215. (or
  19216. (and (memq 'scheduled conditions)
  19217. (re-search-forward org-scheduled-time-regexp end t))
  19218. (and (memq 'notscheduled conditions)
  19219. (not (re-search-forward org-scheduled-time-regexp end t)))
  19220. (and (memq 'deadline conditions)
  19221. (re-search-forward org-deadline-time-regexp end t))
  19222. (and (memq 'notdeadline conditions)
  19223. (not (re-search-forward org-deadline-time-regexp end t)))
  19224. (and (setq m (memq 'regexp conditions))
  19225. (stringp (nth 1 m))
  19226. (re-search-forward (nth 1 m) end t))
  19227. (and (setq m (memq 'notregexp conditions))
  19228. (stringp (nth 1 m))
  19229. (not (re-search-forward (nth 1 m) end t))))
  19230. end)))
  19231. ;;;###autoload
  19232. (defun org-agenda-list-stuck-projects (&rest ignore)
  19233. "Create agenda view for projects that are stuck.
  19234. Stuck projects are project that have no next actions. For the definitions
  19235. of what a project is and how to check if it stuck, customize the variable
  19236. `org-stuck-projects'.
  19237. MATCH is being ignored."
  19238. (interactive)
  19239. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19240. ;; FIXME: we could have used org-agenda-skip-if here.
  19241. (org-agenda-overriding-header "List of stuck projects: ")
  19242. (matcher (nth 0 org-stuck-projects))
  19243. (todo (nth 1 org-stuck-projects))
  19244. (todo-wds (if (member "*" todo)
  19245. (progn
  19246. (org-prepare-agenda-buffers (org-agenda-files))
  19247. (org-delete-all
  19248. org-done-keywords-for-agenda
  19249. (copy-sequence org-todo-keywords-for-agenda)))
  19250. todo))
  19251. (todo-re (concat "^\\*+[ \t]+\\("
  19252. (mapconcat 'identity todo-wds "\\|")
  19253. "\\)\\>"))
  19254. (tags (nth 2 org-stuck-projects))
  19255. (tags-re (if (member "*" tags)
  19256. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19257. (concat "^\\*+ .*:\\("
  19258. (mapconcat 'identity tags "\\|")
  19259. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19260. (gen-re (nth 3 org-stuck-projects))
  19261. (re-list
  19262. (delq nil
  19263. (list
  19264. (if todo todo-re)
  19265. (if tags tags-re)
  19266. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19267. gen-re)))))
  19268. (setq org-agenda-skip-regexp
  19269. (if re-list
  19270. (mapconcat 'identity re-list "\\|")
  19271. (error "No information how to identify unstuck projects")))
  19272. (org-tags-view nil matcher)
  19273. (with-current-buffer org-agenda-buffer-name
  19274. (setq org-agenda-redo-command
  19275. '(org-agenda-list-stuck-projects
  19276. (or current-prefix-arg org-last-arg))))))
  19277. ;;; Diary integration
  19278. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19279. (defun org-get-entries-from-diary (date)
  19280. "Get the (Emacs Calendar) diary entries for DATE."
  19281. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19282. (diary-display-hook '(fancy-diary-display))
  19283. (pop-up-frames nil)
  19284. (list-diary-entries-hook
  19285. (cons 'org-diary-default-entry list-diary-entries-hook))
  19286. (diary-file-name-prefix-function nil) ; turn this feature off
  19287. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19288. entries
  19289. (org-disable-agenda-to-diary t))
  19290. (save-excursion
  19291. (save-window-excursion
  19292. (funcall (if (fboundp 'diary-list-entries)
  19293. 'diary-list-entries 'list-diary-entries)
  19294. date 1)))
  19295. (if (not (get-buffer fancy-diary-buffer))
  19296. (setq entries nil)
  19297. (with-current-buffer fancy-diary-buffer
  19298. (setq buffer-read-only nil)
  19299. (if (zerop (buffer-size))
  19300. ;; No entries
  19301. (setq entries nil)
  19302. ;; Omit the date and other unnecessary stuff
  19303. (org-agenda-cleanup-fancy-diary)
  19304. ;; Add prefix to each line and extend the text properties
  19305. (if (zerop (buffer-size))
  19306. (setq entries nil)
  19307. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19308. (set-buffer-modified-p nil)
  19309. (kill-buffer fancy-diary-buffer)))
  19310. (when entries
  19311. (setq entries (org-split-string entries "\n"))
  19312. (setq entries
  19313. (mapcar
  19314. (lambda (x)
  19315. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19316. ;; Extend the text properties to the beginning of the line
  19317. (org-add-props x (text-properties-at (1- (length x)) x)
  19318. 'type "diary" 'date date))
  19319. entries)))))
  19320. (defun org-agenda-cleanup-fancy-diary ()
  19321. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19322. This gets rid of the date, the underline under the date, and
  19323. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19324. date. It also removes lines that contain only whitespace."
  19325. (goto-char (point-min))
  19326. (if (looking-at ".*?:[ \t]*")
  19327. (progn
  19328. (replace-match "")
  19329. (re-search-forward "\n=+$" nil t)
  19330. (replace-match "")
  19331. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19332. (re-search-forward "\n=+$" nil t)
  19333. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19334. (goto-char (point-min))
  19335. (while (re-search-forward "^ +\n" nil t)
  19336. (replace-match ""))
  19337. (goto-char (point-min))
  19338. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19339. (replace-match "")))
  19340. ;; Make sure entries from the diary have the right text properties.
  19341. (eval-after-load "diary-lib"
  19342. '(if (boundp 'diary-modify-entry-list-string-function)
  19343. ;; We can rely on the hook, nothing to do
  19344. nil
  19345. ;; Hook not avaiable, must use advice to make this work
  19346. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19347. "Make the position visible."
  19348. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19349. (stringp string)
  19350. buffer-file-name)
  19351. (setq string (org-modify-diary-entry-string string))))))
  19352. (defun org-modify-diary-entry-string (string)
  19353. "Add text properties to string, allowing org-mode to act on it."
  19354. (org-add-props string nil
  19355. 'mouse-face 'highlight
  19356. 'keymap org-agenda-keymap
  19357. 'help-echo (if buffer-file-name
  19358. (format "mouse-2 or RET jump to diary file %s"
  19359. (abbreviate-file-name buffer-file-name))
  19360. "")
  19361. 'org-agenda-diary-link t
  19362. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19363. (defun org-diary-default-entry ()
  19364. "Add a dummy entry to the diary.
  19365. Needed to avoid empty dates which mess up holiday display."
  19366. ;; Catch the error if dealing with the new add-to-diary-alist
  19367. (when org-disable-agenda-to-diary
  19368. (condition-case nil
  19369. (add-to-diary-list original-date "Org-mode dummy" "")
  19370. (error
  19371. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19372. ;;;###autoload
  19373. (defun org-diary (&rest args)
  19374. "Return diary information from org-files.
  19375. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19376. It accesses org files and extracts information from those files to be
  19377. listed in the diary. The function accepts arguments specifying what
  19378. items should be listed. The following arguments are allowed:
  19379. :timestamp List the headlines of items containing a date stamp or
  19380. date range matching the selected date. Deadlines will
  19381. also be listed, on the expiration day.
  19382. :sexp List entries resulting from diary-like sexps.
  19383. :deadline List any deadlines past due, or due within
  19384. `org-deadline-warning-days'. The listing occurs only
  19385. in the diary for *today*, not at any other date. If
  19386. an entry is marked DONE, it is no longer listed.
  19387. :scheduled List all items which are scheduled for the given date.
  19388. The diary for *today* also contains items which were
  19389. scheduled earlier and are not yet marked DONE.
  19390. :todo List all TODO items from the org-file. This may be a
  19391. long list - so this is not turned on by default.
  19392. Like deadlines, these entries only show up in the
  19393. diary for *today*, not at any other date.
  19394. The call in the diary file should look like this:
  19395. &%%(org-diary) ~/path/to/some/orgfile.org
  19396. Use a separate line for each org file to check. Or, if you omit the file name,
  19397. all files listed in `org-agenda-files' will be checked automatically:
  19398. &%%(org-diary)
  19399. If you don't give any arguments (as in the example above), the default
  19400. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19401. So the example above may also be written as
  19402. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19403. The function expects the lisp variables `entry' and `date' to be provided
  19404. by the caller, because this is how the calendar works. Don't use this
  19405. function from a program - use `org-agenda-get-day-entries' instead."
  19406. (org-agenda-maybe-reset-markers)
  19407. (org-compile-prefix-format 'agenda)
  19408. (org-set-sorting-strategy 'agenda)
  19409. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19410. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19411. (list entry)
  19412. (org-agenda-files t)))
  19413. file rtn results)
  19414. (org-prepare-agenda-buffers files)
  19415. ;; If this is called during org-agenda, don't return any entries to
  19416. ;; the calendar. Org Agenda will list these entries itself.
  19417. (if org-disable-agenda-to-diary (setq files nil))
  19418. (while (setq file (pop files))
  19419. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19420. (setq results (append results rtn)))
  19421. (if results
  19422. (concat (org-finalize-agenda-entries results) "\n"))))
  19423. ;;; Agenda entry finders
  19424. (defun org-agenda-get-day-entries (file date &rest args)
  19425. "Does the work for `org-diary' and `org-agenda'.
  19426. FILE is the path to a file to be checked for entries. DATE is date like
  19427. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19428. which kind of entries should be extracted. For details about these, see
  19429. the documentation of `org-diary'."
  19430. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19431. (let* ((org-startup-folded nil)
  19432. (org-startup-align-all-tables nil)
  19433. (buffer (if (file-exists-p file)
  19434. (org-get-agenda-file-buffer file)
  19435. (error "No such file %s" file)))
  19436. arg results rtn)
  19437. (if (not buffer)
  19438. ;; If file does not exist, make sure an error message ends up in diary
  19439. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19440. (with-current-buffer buffer
  19441. (unless (org-mode-p)
  19442. (error "Agenda file %s is not in `org-mode'" file))
  19443. (let ((case-fold-search nil))
  19444. (save-excursion
  19445. (save-restriction
  19446. (if org-agenda-restrict
  19447. (narrow-to-region org-agenda-restrict-begin
  19448. org-agenda-restrict-end)
  19449. (widen))
  19450. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19451. (while (setq arg (pop args))
  19452. (cond
  19453. ((and (eq arg :todo)
  19454. (equal date (calendar-current-date)))
  19455. (setq rtn (org-agenda-get-todos))
  19456. (setq results (append results rtn)))
  19457. ((eq arg :timestamp)
  19458. (setq rtn (org-agenda-get-blocks))
  19459. (setq results (append results rtn))
  19460. (setq rtn (org-agenda-get-timestamps))
  19461. (setq results (append results rtn)))
  19462. ((eq arg :sexp)
  19463. (setq rtn (org-agenda-get-sexps))
  19464. (setq results (append results rtn)))
  19465. ((eq arg :scheduled)
  19466. (setq rtn (org-agenda-get-scheduled))
  19467. (setq results (append results rtn)))
  19468. ((eq arg :closed)
  19469. (setq rtn (org-agenda-get-closed))
  19470. (setq results (append results rtn)))
  19471. ((eq arg :deadline)
  19472. (setq rtn (org-agenda-get-deadlines))
  19473. (setq results (append results rtn))))))))
  19474. results))))
  19475. (defun org-entry-is-todo-p ()
  19476. (member (org-get-todo-state) org-not-done-keywords))
  19477. (defun org-entry-is-done-p ()
  19478. (member (org-get-todo-state) org-done-keywords))
  19479. (defun org-get-todo-state ()
  19480. (save-excursion
  19481. (org-back-to-heading t)
  19482. (and (looking-at org-todo-line-regexp)
  19483. (match-end 2)
  19484. (match-string 2))))
  19485. (defun org-at-date-range-p (&optional inactive-ok)
  19486. "Is the cursor inside a date range?"
  19487. (interactive)
  19488. (save-excursion
  19489. (catch 'exit
  19490. (let ((pos (point)))
  19491. (skip-chars-backward "^[<\r\n")
  19492. (skip-chars-backward "<[")
  19493. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19494. (>= (match-end 0) pos)
  19495. (throw 'exit t))
  19496. (skip-chars-backward "^<[\r\n")
  19497. (skip-chars-backward "<[")
  19498. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19499. (>= (match-end 0) pos)
  19500. (throw 'exit t)))
  19501. nil)))
  19502. (defun org-agenda-get-todos ()
  19503. "Return the TODO information for agenda display."
  19504. (let* ((props (list 'face nil
  19505. 'done-face 'org-done
  19506. 'org-not-done-regexp org-not-done-regexp
  19507. 'org-todo-regexp org-todo-regexp
  19508. 'mouse-face 'highlight
  19509. 'keymap org-agenda-keymap
  19510. 'help-echo
  19511. (format "mouse-2 or RET jump to org file %s"
  19512. (abbreviate-file-name buffer-file-name))))
  19513. ;; FIXME: get rid of the \n at some point but watch out
  19514. (regexp (concat "^\\*+[ \t]+\\("
  19515. (if org-select-this-todo-keyword
  19516. (if (equal org-select-this-todo-keyword "*")
  19517. org-todo-regexp
  19518. (concat "\\<\\("
  19519. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19520. "\\)\\>"))
  19521. org-not-done-regexp)
  19522. "[^\n\r]*\\)"))
  19523. marker priority category tags
  19524. ee txt beg end)
  19525. (goto-char (point-min))
  19526. (while (re-search-forward regexp nil t)
  19527. (catch :skip
  19528. (save-match-data
  19529. (beginning-of-line)
  19530. (setq beg (point) end (progn (outline-next-heading) (point)))
  19531. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19532. (re-search-forward org-ts-regexp end t))
  19533. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19534. (re-search-forward org-scheduled-time-regexp end t))
  19535. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19536. (re-search-forward org-deadline-time-regexp end t)
  19537. (org-deadline-close (match-string 1))))
  19538. (goto-char (1+ beg))
  19539. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19540. (throw :skip nil)))
  19541. (goto-char beg)
  19542. (org-agenda-skip)
  19543. (goto-char (match-beginning 1))
  19544. (setq marker (org-agenda-new-marker (match-beginning 0))
  19545. category (org-get-category)
  19546. tags (org-get-tags-at (point))
  19547. txt (org-format-agenda-item "" (match-string 1) category tags)
  19548. priority (1+ (org-get-priority txt)))
  19549. (org-add-props txt props
  19550. 'org-marker marker 'org-hd-marker marker
  19551. 'priority priority 'org-category category
  19552. 'type "todo")
  19553. (push txt ee)
  19554. (if org-agenda-todo-list-sublevels
  19555. (goto-char (match-end 1))
  19556. (org-end-of-subtree 'invisible))))
  19557. (nreverse ee)))
  19558. (defconst org-agenda-no-heading-message
  19559. "No heading for this item in buffer or region.")
  19560. (defun org-agenda-get-timestamps ()
  19561. "Return the date stamp information for agenda display."
  19562. (let* ((props (list 'face nil
  19563. 'org-not-done-regexp org-not-done-regexp
  19564. 'org-todo-regexp org-todo-regexp
  19565. 'mouse-face 'highlight
  19566. 'keymap org-agenda-keymap
  19567. 'help-echo
  19568. (format "mouse-2 or RET jump to org file %s"
  19569. (abbreviate-file-name buffer-file-name))))
  19570. (d1 (calendar-absolute-from-gregorian date))
  19571. (remove-re
  19572. (concat
  19573. (regexp-quote
  19574. (format-time-string
  19575. "<%Y-%m-%d"
  19576. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19577. ".*?>"))
  19578. (regexp
  19579. (concat
  19580. (regexp-quote
  19581. (substring
  19582. (format-time-string
  19583. (car org-time-stamp-formats)
  19584. (apply 'encode-time ; DATE bound by calendar
  19585. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19586. 0 11))
  19587. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19588. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19589. marker hdmarker deadlinep scheduledp donep tmp priority category
  19590. ee txt timestr tags b0 b3 e3 head)
  19591. (goto-char (point-min))
  19592. (while (re-search-forward regexp nil t)
  19593. (setq b0 (match-beginning 0)
  19594. b3 (match-beginning 3) e3 (match-end 3))
  19595. (catch :skip
  19596. (and (org-at-date-range-p) (throw :skip nil))
  19597. (org-agenda-skip)
  19598. (if (and (match-end 1)
  19599. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19600. (throw :skip nil))
  19601. (if (and e3
  19602. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19603. (throw :skip nil))
  19604. (setq marker (org-agenda-new-marker b0)
  19605. category (org-get-category b0)
  19606. tmp (buffer-substring (max (point-min)
  19607. (- b0 org-ds-keyword-length))
  19608. b0)
  19609. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19610. deadlinep (string-match org-deadline-regexp tmp)
  19611. scheduledp (string-match org-scheduled-regexp tmp)
  19612. donep (org-entry-is-done-p))
  19613. (if (or scheduledp deadlinep) (throw :skip t))
  19614. (if (string-match ">" timestr)
  19615. ;; substring should only run to end of time stamp
  19616. (setq timestr (substring timestr 0 (match-end 0))))
  19617. (save-excursion
  19618. (if (re-search-backward "^\\*+ " nil t)
  19619. (progn
  19620. (goto-char (match-beginning 0))
  19621. (setq hdmarker (org-agenda-new-marker)
  19622. tags (org-get-tags-at))
  19623. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19624. (setq head (match-string 1))
  19625. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19626. (setq txt (org-format-agenda-item
  19627. nil head category tags timestr nil
  19628. remove-re)))
  19629. (setq txt org-agenda-no-heading-message))
  19630. (setq priority (org-get-priority txt))
  19631. (org-add-props txt props
  19632. 'org-marker marker 'org-hd-marker hdmarker)
  19633. (org-add-props txt nil 'priority priority
  19634. 'org-category category 'date date
  19635. 'type "timestamp")
  19636. (push txt ee))
  19637. (outline-next-heading)))
  19638. (nreverse ee)))
  19639. (defun org-agenda-get-sexps ()
  19640. "Return the sexp information for agenda display."
  19641. (require 'diary-lib)
  19642. (let* ((props (list 'face nil
  19643. 'mouse-face 'highlight
  19644. 'keymap org-agenda-keymap
  19645. 'help-echo
  19646. (format "mouse-2 or RET jump to org file %s"
  19647. (abbreviate-file-name buffer-file-name))))
  19648. (regexp "^&?%%(")
  19649. marker category ee txt tags entry result beg b sexp sexp-entry)
  19650. (goto-char (point-min))
  19651. (while (re-search-forward regexp nil t)
  19652. (catch :skip
  19653. (org-agenda-skip)
  19654. (setq beg (match-beginning 0))
  19655. (goto-char (1- (match-end 0)))
  19656. (setq b (point))
  19657. (forward-sexp 1)
  19658. (setq sexp (buffer-substring b (point)))
  19659. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19660. (org-trim (match-string 1))
  19661. ""))
  19662. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19663. (when result
  19664. (setq marker (org-agenda-new-marker beg)
  19665. category (org-get-category beg))
  19666. (if (string-match "\\S-" result)
  19667. (setq txt result)
  19668. (setq txt "SEXP entry returned empty string"))
  19669. (setq txt (org-format-agenda-item
  19670. "" txt category tags 'time))
  19671. (org-add-props txt props 'org-marker marker)
  19672. (org-add-props txt nil
  19673. 'org-category category 'date date
  19674. 'type "sexp")
  19675. (push txt ee))))
  19676. (nreverse ee)))
  19677. (defun org-agenda-get-closed ()
  19678. "Return the logged TODO entries for agenda display."
  19679. (let* ((props (list 'mouse-face 'highlight
  19680. 'org-not-done-regexp org-not-done-regexp
  19681. 'org-todo-regexp org-todo-regexp
  19682. 'keymap org-agenda-keymap
  19683. 'help-echo
  19684. (format "mouse-2 or RET jump to org file %s"
  19685. (abbreviate-file-name buffer-file-name))))
  19686. (regexp (concat
  19687. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19688. (regexp-quote
  19689. (substring
  19690. (format-time-string
  19691. (car org-time-stamp-formats)
  19692. (apply 'encode-time ; DATE bound by calendar
  19693. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19694. 1 11))))
  19695. marker hdmarker priority category tags closedp
  19696. ee txt timestr)
  19697. (goto-char (point-min))
  19698. (while (re-search-forward regexp nil t)
  19699. (catch :skip
  19700. (org-agenda-skip)
  19701. (setq marker (org-agenda-new-marker (match-beginning 0))
  19702. closedp (equal (match-string 1) org-closed-string)
  19703. category (org-get-category (match-beginning 0))
  19704. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19705. ;; donep (org-entry-is-done-p)
  19706. )
  19707. (if (string-match "\\]" timestr)
  19708. ;; substring should only run to end of time stamp
  19709. (setq timestr (substring timestr 0 (match-end 0))))
  19710. (save-excursion
  19711. (if (re-search-backward "^\\*+ " nil t)
  19712. (progn
  19713. (goto-char (match-beginning 0))
  19714. (setq hdmarker (org-agenda-new-marker)
  19715. tags (org-get-tags-at))
  19716. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19717. (setq txt (org-format-agenda-item
  19718. (if closedp "Closed: " "Clocked: ")
  19719. (match-string 1) category tags timestr)))
  19720. (setq txt org-agenda-no-heading-message))
  19721. (setq priority 100000)
  19722. (org-add-props txt props
  19723. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19724. 'priority priority 'org-category category
  19725. 'type "closed" 'date date
  19726. 'undone-face 'org-warning 'done-face 'org-done)
  19727. (push txt ee))
  19728. (goto-char (point-at-eol))))
  19729. (nreverse ee)))
  19730. (defun org-agenda-get-deadlines ()
  19731. "Return the deadline information for agenda display."
  19732. (let* ((props (list 'mouse-face 'highlight
  19733. 'org-not-done-regexp org-not-done-regexp
  19734. 'org-todo-regexp org-todo-regexp
  19735. 'keymap org-agenda-keymap
  19736. 'help-echo
  19737. (format "mouse-2 or RET jump to org file %s"
  19738. (abbreviate-file-name buffer-file-name))))
  19739. (regexp org-deadline-time-regexp)
  19740. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19741. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19742. d2 diff dfrac wdays pos pos1 category tags
  19743. ee txt head face s upcomingp donep timestr)
  19744. (goto-char (point-min))
  19745. (while (re-search-forward regexp nil t)
  19746. (catch :skip
  19747. (org-agenda-skip)
  19748. (setq s (match-string 1)
  19749. pos (1- (match-beginning 1))
  19750. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19751. diff (- d2 d1)
  19752. wdays (org-get-wdays s)
  19753. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19754. upcomingp (and todayp (> diff 0)))
  19755. ;; When to show a deadline in the calendar:
  19756. ;; If the expiration is within wdays warning time.
  19757. ;; Past-due deadlines are only shown on the current date
  19758. (if (or (and (<= diff wdays)
  19759. (and todayp (not org-agenda-only-exact-dates)))
  19760. (= diff 0))
  19761. (save-excursion
  19762. (setq category (org-get-category))
  19763. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19764. (progn
  19765. (goto-char (match-end 0))
  19766. (setq pos1 (match-beginning 0))
  19767. (setq tags (org-get-tags-at pos1))
  19768. (setq head (buffer-substring-no-properties
  19769. (point)
  19770. (progn (skip-chars-forward "^\r\n")
  19771. (point))))
  19772. (setq donep (string-match org-looking-at-done-regexp head))
  19773. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19774. (setq timestr
  19775. (concat (substring s (match-beginning 1)) " "))
  19776. (setq timestr 'time))
  19777. (if (and donep
  19778. (or org-agenda-skip-deadline-if-done
  19779. (not (= diff 0))))
  19780. (setq txt nil)
  19781. (setq txt (org-format-agenda-item
  19782. (if (= diff 0)
  19783. (car org-agenda-deadline-leaders)
  19784. (format (nth 1 org-agenda-deadline-leaders)
  19785. diff))
  19786. head category tags timestr))))
  19787. (setq txt org-agenda-no-heading-message))
  19788. (when txt
  19789. (setq face (org-agenda-deadline-face dfrac))
  19790. (org-add-props txt props
  19791. 'org-marker (org-agenda-new-marker pos)
  19792. 'org-hd-marker (org-agenda-new-marker pos1)
  19793. 'priority (+ (- diff)
  19794. (org-get-priority txt))
  19795. 'org-category category
  19796. 'type (if upcomingp "upcoming-deadline" "deadline")
  19797. 'date (if upcomingp date d2)
  19798. 'face (if donep 'org-done face)
  19799. 'undone-face face 'done-face 'org-done)
  19800. (push txt ee))))))
  19801. (nreverse ee)))
  19802. (defun org-agenda-deadline-face (fraction)
  19803. "Return the face to displaying a deadline item.
  19804. FRACTION is what fraction of the head-warning time has passed."
  19805. (let ((faces org-agenda-deadline-faces) f)
  19806. (catch 'exit
  19807. (while (setq f (pop faces))
  19808. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19809. (defun org-agenda-get-scheduled ()
  19810. "Return the scheduled information for agenda display."
  19811. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19812. 'org-todo-regexp org-todo-regexp
  19813. 'done-face 'org-done
  19814. 'mouse-face 'highlight
  19815. 'keymap org-agenda-keymap
  19816. 'help-echo
  19817. (format "mouse-2 or RET jump to org file %s"
  19818. (abbreviate-file-name buffer-file-name))))
  19819. (regexp org-scheduled-time-regexp)
  19820. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19821. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19822. d2 diff pos pos1 category tags
  19823. ee txt head pastschedp donep face timestr s)
  19824. (goto-char (point-min))
  19825. (while (re-search-forward regexp nil t)
  19826. (catch :skip
  19827. (org-agenda-skip)
  19828. (setq s (match-string 1)
  19829. pos (1- (match-beginning 1))
  19830. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19831. ;;; is this right?
  19832. ;;; do we need to do this for deadleine too????
  19833. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19834. diff (- d2 d1))
  19835. (setq pastschedp (and todayp (< diff 0)))
  19836. ;; When to show a scheduled item in the calendar:
  19837. ;; If it is on or past the date.
  19838. (if (or (and (< diff 0)
  19839. (and todayp (not org-agenda-only-exact-dates)))
  19840. (= diff 0))
  19841. (save-excursion
  19842. (setq category (org-get-category))
  19843. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19844. (progn
  19845. (goto-char (match-end 0))
  19846. (setq pos1 (match-beginning 0))
  19847. (setq tags (org-get-tags-at))
  19848. (setq head (buffer-substring-no-properties
  19849. (point)
  19850. (progn (skip-chars-forward "^\r\n") (point))))
  19851. (setq donep (string-match org-looking-at-done-regexp head))
  19852. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19853. (setq timestr
  19854. (concat (substring s (match-beginning 1)) " "))
  19855. (setq timestr 'time))
  19856. (if (and donep
  19857. (or org-agenda-skip-scheduled-if-done
  19858. (not (= diff 0))))
  19859. (setq txt nil)
  19860. (setq txt (org-format-agenda-item
  19861. (if (= diff 0)
  19862. (car org-agenda-scheduled-leaders)
  19863. (format (nth 1 org-agenda-scheduled-leaders)
  19864. (- 1 diff)))
  19865. head category tags timestr))))
  19866. (setq txt org-agenda-no-heading-message))
  19867. (when txt
  19868. (setq face (if pastschedp
  19869. 'org-scheduled-previously
  19870. 'org-scheduled-today))
  19871. (org-add-props txt props
  19872. 'undone-face face
  19873. 'face (if donep 'org-done face)
  19874. 'org-marker (org-agenda-new-marker pos)
  19875. 'org-hd-marker (org-agenda-new-marker pos1)
  19876. 'type (if pastschedp "past-scheduled" "scheduled")
  19877. 'date (if pastschedp d2 date)
  19878. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19879. 'org-category category)
  19880. (push txt ee))))))
  19881. (nreverse ee)))
  19882. (defun org-agenda-get-blocks ()
  19883. "Return the date-range information for agenda display."
  19884. (let* ((props (list 'face nil
  19885. 'org-not-done-regexp org-not-done-regexp
  19886. 'org-todo-regexp org-todo-regexp
  19887. 'mouse-face 'highlight
  19888. 'keymap org-agenda-keymap
  19889. 'help-echo
  19890. (format "mouse-2 or RET jump to org file %s"
  19891. (abbreviate-file-name buffer-file-name))))
  19892. (regexp org-tr-regexp)
  19893. (d0 (calendar-absolute-from-gregorian date))
  19894. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19895. donep head)
  19896. (goto-char (point-min))
  19897. (while (re-search-forward regexp nil t)
  19898. (catch :skip
  19899. (org-agenda-skip)
  19900. (setq pos (point))
  19901. (setq timestr (match-string 0)
  19902. s1 (match-string 1)
  19903. s2 (match-string 2)
  19904. d1 (time-to-days (org-time-string-to-time s1))
  19905. d2 (time-to-days (org-time-string-to-time s2)))
  19906. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19907. ;; Only allow days between the limits, because the normal
  19908. ;; date stamps will catch the limits.
  19909. (save-excursion
  19910. (setq marker (org-agenda-new-marker (point)))
  19911. (setq category (org-get-category))
  19912. (if (re-search-backward "^\\*+ " nil t)
  19913. (progn
  19914. (goto-char (match-beginning 0))
  19915. (setq hdmarker (org-agenda-new-marker (point)))
  19916. (setq tags (org-get-tags-at))
  19917. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19918. (setq head (match-string 1))
  19919. (and org-agenda-skip-timestamp-if-done
  19920. (org-entry-is-done-p)
  19921. (throw :skip t))
  19922. (setq txt (org-format-agenda-item
  19923. (format (if (= d1 d2) "" "(%d/%d): ")
  19924. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19925. head category tags
  19926. (if (= d0 d1) timestr))))
  19927. (setq txt org-agenda-no-heading-message))
  19928. (org-add-props txt props
  19929. 'org-marker marker 'org-hd-marker hdmarker
  19930. 'type "block" 'date date
  19931. 'priority (org-get-priority txt) 'org-category category)
  19932. (push txt ee)))
  19933. (goto-char pos)))
  19934. ;; Sort the entries by expiration date.
  19935. (nreverse ee)))
  19936. ;;; Agenda presentation and sorting
  19937. (defconst org-plain-time-of-day-regexp
  19938. (concat
  19939. "\\(\\<[012]?[0-9]"
  19940. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19941. "\\(--?"
  19942. "\\(\\<[012]?[0-9]"
  19943. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19944. "\\)?")
  19945. "Regular expression to match a plain time or time range.
  19946. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19947. groups carry important information:
  19948. 0 the full match
  19949. 1 the first time, range or not
  19950. 8 the second time, if it is a range.")
  19951. (defconst org-plain-time-extension-regexp
  19952. (concat
  19953. "\\(\\<[012]?[0-9]"
  19954. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19955. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19956. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19957. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19958. groups carry important information:
  19959. 0 the full match
  19960. 7 hours of duration
  19961. 9 minutes of duration")
  19962. (defconst org-stamp-time-of-day-regexp
  19963. (concat
  19964. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  19965. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  19966. "\\(--?"
  19967. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  19968. "Regular expression to match a timestamp time or time range.
  19969. After a match, the following groups carry important information:
  19970. 0 the full match
  19971. 1 date plus weekday, for backreferencing to make sure both times on same day
  19972. 2 the first time, range or not
  19973. 4 the second time, if it is a range.")
  19974. (defvar org-prefix-has-time nil
  19975. "A flag, set by `org-compile-prefix-format'.
  19976. The flag is set if the currently compiled format contains a `%t'.")
  19977. (defvar org-prefix-has-tag nil
  19978. "A flag, set by `org-compile-prefix-format'.
  19979. The flag is set if the currently compiled format contains a `%T'.")
  19980. (defun org-format-agenda-item (extra txt &optional category tags dotime
  19981. noprefix remove-re)
  19982. "Format TXT to be inserted into the agenda buffer.
  19983. In particular, it adds the prefix and corresponding text properties. EXTRA
  19984. must be a string and replaces the `%s' specifier in the prefix format.
  19985. CATEGORY (string, symbol or nil) may be used to overrule the default
  19986. category taken from local variable or file name. It will replace the `%c'
  19987. specifier in the format. DOTIME, when non-nil, indicates that a
  19988. time-of-day should be extracted from TXT for sorting of this entry, and for
  19989. the `%t' specifier in the format. When DOTIME is a string, this string is
  19990. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  19991. only the correctly processes TXT should be returned - this is used by
  19992. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  19993. Any match of REMOVE-RE will be removed from TXT."
  19994. (save-match-data
  19995. ;; Diary entries sometimes have extra whitespace at the beginning
  19996. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  19997. (let* ((category (or category
  19998. org-category
  19999. (if buffer-file-name
  20000. (file-name-sans-extension
  20001. (file-name-nondirectory buffer-file-name))
  20002. "")))
  20003. (tag (if tags (nth (1- (length tags)) tags) ""))
  20004. time ; time and tag are needed for the eval of the prefix format
  20005. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20006. (time-of-day (and dotime (org-get-time-of-day ts)))
  20007. stamp plain s0 s1 s2 rtn srp)
  20008. (when (and dotime time-of-day org-prefix-has-time)
  20009. ;; Extract starting and ending time and move them to prefix
  20010. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20011. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20012. (setq s0 (match-string 0 ts)
  20013. srp (and stamp (match-end 3))
  20014. s1 (match-string (if plain 1 2) ts)
  20015. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20016. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20017. ;; them, we might want to remove them there to avoid duplication.
  20018. ;; The user can turn this off with a variable.
  20019. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20020. (string-match (concat (regexp-quote s0) " *") txt)
  20021. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20022. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20023. (= (match-beginning 0) 0)
  20024. t))
  20025. (setq txt (replace-match "" nil nil txt))))
  20026. ;; Normalize the time(s) to 24 hour
  20027. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20028. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20029. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20030. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20031. (let ((m (+ (string-to-number (match-string 2 s1))
  20032. (* 60 (string-to-number (match-string 1 s1)))
  20033. org-agenda-default-appointment-duration))
  20034. h)
  20035. (setq h (/ m 60) m (- m (* h 60)))
  20036. (setq s2 (format "%02d:%02d" h m))))
  20037. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20038. txt)
  20039. ;; Tags are in the string
  20040. (if (or (eq org-agenda-remove-tags t)
  20041. (and org-agenda-remove-tags
  20042. org-prefix-has-tag))
  20043. (setq txt (replace-match "" t t txt))
  20044. (setq txt (replace-match
  20045. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20046. (match-string 2 txt))
  20047. t t txt))))
  20048. (when remove-re
  20049. (while (string-match remove-re txt)
  20050. (setq txt (replace-match "" t t txt))))
  20051. ;; Create the final string
  20052. (if noprefix
  20053. (setq rtn txt)
  20054. ;; Prepare the variables needed in the eval of the compiled format
  20055. (setq time (cond (s2 (concat s1 "-" s2))
  20056. (s1 (concat s1 "......"))
  20057. (t ""))
  20058. extra (or extra "")
  20059. category (if (symbolp category) (symbol-name category) category))
  20060. ;; Evaluate the compiled format
  20061. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20062. ;; And finally add the text properties
  20063. (org-add-props rtn nil
  20064. 'org-category (downcase category) 'tags tags
  20065. 'org-highest-priority org-highest-priority
  20066. 'org-lowest-priority org-lowest-priority
  20067. 'prefix-length (- (length rtn) (length txt))
  20068. 'time-of-day time-of-day
  20069. 'txt txt
  20070. 'time time
  20071. 'extra extra
  20072. 'dotime dotime))))
  20073. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20074. (defvar org-agenda-sorting-strategy-selected nil)
  20075. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20076. (catch 'exit
  20077. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20078. ((and todayp (member 'today (car org-agenda-time-grid))))
  20079. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20080. ((member 'weekly (car org-agenda-time-grid)))
  20081. (t (throw 'exit list)))
  20082. (let* ((have (delq nil (mapcar
  20083. (lambda (x) (get-text-property 1 'time-of-day x))
  20084. list)))
  20085. (string (nth 1 org-agenda-time-grid))
  20086. (gridtimes (nth 2 org-agenda-time-grid))
  20087. (req (car org-agenda-time-grid))
  20088. (remove (member 'remove-match req))
  20089. new time)
  20090. (if (and (member 'require-timed req) (not have))
  20091. ;; don't show empty grid
  20092. (throw 'exit list))
  20093. (while (setq time (pop gridtimes))
  20094. (unless (and remove (member time have))
  20095. (setq time (int-to-string time))
  20096. (push (org-format-agenda-item
  20097. nil string "" nil
  20098. (concat (substring time 0 -2) ":" (substring time -2)))
  20099. new)
  20100. (put-text-property
  20101. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20102. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20103. (append new list)
  20104. (append list new)))))
  20105. (defun org-compile-prefix-format (key)
  20106. "Compile the prefix format into a Lisp form that can be evaluated.
  20107. The resulting form is returned and stored in the variable
  20108. `org-prefix-format-compiled'."
  20109. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20110. (let ((s (cond
  20111. ((stringp org-agenda-prefix-format)
  20112. org-agenda-prefix-format)
  20113. ((assq key org-agenda-prefix-format)
  20114. (cdr (assq key org-agenda-prefix-format)))
  20115. (t " %-12:c%?-12t% s")))
  20116. (start 0)
  20117. varform vars var e c f opt)
  20118. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20119. s start)
  20120. (setq var (cdr (assoc (match-string 4 s)
  20121. '(("c" . category) ("t" . time) ("s" . extra)
  20122. ("T" . tag))))
  20123. c (or (match-string 3 s) "")
  20124. opt (match-beginning 1)
  20125. start (1+ (match-beginning 0)))
  20126. (if (equal var 'time) (setq org-prefix-has-time t))
  20127. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20128. (setq f (concat "%" (match-string 2 s) "s"))
  20129. (if opt
  20130. (setq varform
  20131. `(if (equal "" ,var)
  20132. ""
  20133. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20134. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20135. (setq s (replace-match "%s" t nil s))
  20136. (push varform vars))
  20137. (setq vars (nreverse vars))
  20138. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20139. (defun org-set-sorting-strategy (key)
  20140. (if (symbolp (car org-agenda-sorting-strategy))
  20141. ;; the old format
  20142. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20143. (setq org-agenda-sorting-strategy-selected
  20144. (or (cdr (assq key org-agenda-sorting-strategy))
  20145. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20146. '(time-up category-keep priority-down)))))
  20147. (defun org-get-time-of-day (s &optional string mod24)
  20148. "Check string S for a time of day.
  20149. If found, return it as a military time number between 0 and 2400.
  20150. If not found, return nil.
  20151. The optional STRING argument forces conversion into a 5 character wide string
  20152. HH:MM."
  20153. (save-match-data
  20154. (when
  20155. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20156. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20157. (let* ((h (string-to-number (match-string 1 s)))
  20158. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20159. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20160. (am-p (equal ampm "am"))
  20161. (h1 (cond ((not ampm) h)
  20162. ((= h 12) (if am-p 0 12))
  20163. (t (+ h (if am-p 0 12)))))
  20164. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20165. (mod h1 24) h1))
  20166. (t0 (+ (* 100 h2) m))
  20167. (t1 (concat (if (>= h1 24) "+" " ")
  20168. (if (< t0 100) "0" "")
  20169. (if (< t0 10) "0" "")
  20170. (int-to-string t0))))
  20171. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20172. (defun org-finalize-agenda-entries (list &optional nosort)
  20173. "Sort and concatenate the agenda items."
  20174. (setq list (mapcar 'org-agenda-highlight-todo list))
  20175. (if nosort
  20176. list
  20177. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20178. (defun org-agenda-highlight-todo (x)
  20179. (let (re pl)
  20180. (if (eq x 'line)
  20181. (save-excursion
  20182. (beginning-of-line 1)
  20183. (setq re (get-text-property (point) 'org-todo-regexp))
  20184. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20185. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20186. (add-text-properties (match-beginning 0) (match-end 0)
  20187. (list 'face (org-get-todo-face 0)))
  20188. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20189. (delete-region (match-beginning 1) (1- (match-end 0)))
  20190. (goto-char (match-beginning 1))
  20191. (insert (format org-agenda-todo-keyword-format s)))))
  20192. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20193. pl (get-text-property 0 'prefix-length x))
  20194. ; (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  20195. ; (add-text-properties
  20196. ; (or (match-end 1) (match-end 0)) (match-end 0)
  20197. ; (list 'face (org-get-todo-face (match-string 2 x)))
  20198. ; x))
  20199. (when (and re
  20200. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20201. x (or pl 0)) pl))
  20202. (add-text-properties
  20203. (or (match-end 1) (match-end 0)) (match-end 0)
  20204. (list 'face (org-get-todo-face (match-string 2 x)))
  20205. x)
  20206. (setq x (concat (substring x 0 (match-end 1))
  20207. (format org-agenda-todo-keyword-format
  20208. (match-string 2 x))
  20209. " "
  20210. (substring x (match-end 3)))))
  20211. x)))
  20212. (defsubst org-cmp-priority (a b)
  20213. "Compare the priorities of string A and B."
  20214. (let ((pa (or (get-text-property 1 'priority a) 0))
  20215. (pb (or (get-text-property 1 'priority b) 0)))
  20216. (cond ((> pa pb) +1)
  20217. ((< pa pb) -1)
  20218. (t nil))))
  20219. (defsubst org-cmp-category (a b)
  20220. "Compare the string values of categories of strings A and B."
  20221. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20222. (cb (or (get-text-property 1 'org-category b) "")))
  20223. (cond ((string-lessp ca cb) -1)
  20224. ((string-lessp cb ca) +1)
  20225. (t nil))))
  20226. (defsubst org-cmp-tag (a b)
  20227. "Compare the string values of categories of strings A and B."
  20228. (let ((ta (car (last (get-text-property 1 'tags a))))
  20229. (tb (car (last (get-text-property 1 'tags b)))))
  20230. (cond ((not ta) +1)
  20231. ((not tb) -1)
  20232. ((string-lessp ta tb) -1)
  20233. ((string-lessp tb ta) +1)
  20234. (t nil))))
  20235. (defsubst org-cmp-time (a b)
  20236. "Compare the time-of-day values of strings A and B."
  20237. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20238. (ta (or (get-text-property 1 'time-of-day a) def))
  20239. (tb (or (get-text-property 1 'time-of-day b) def)))
  20240. (cond ((< ta tb) -1)
  20241. ((< tb ta) +1)
  20242. (t nil))))
  20243. (defun org-entries-lessp (a b)
  20244. "Predicate for sorting agenda entries."
  20245. ;; The following variables will be used when the form is evaluated.
  20246. ;; So even though the compiler complains, keep them.
  20247. (let* ((time-up (org-cmp-time a b))
  20248. (time-down (if time-up (- time-up) nil))
  20249. (priority-up (org-cmp-priority a b))
  20250. (priority-down (if priority-up (- priority-up) nil))
  20251. (category-up (org-cmp-category a b))
  20252. (category-down (if category-up (- category-up) nil))
  20253. (category-keep (if category-up +1 nil))
  20254. (tag-up (org-cmp-tag a b))
  20255. (tag-down (if tag-up (- tag-up) nil)))
  20256. (cdr (assoc
  20257. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20258. '((-1 . t) (1 . nil) (nil . nil))))))
  20259. ;;; Agenda restriction lock
  20260. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20261. "Overlay to mark the headline to which arenda commands are restricted.")
  20262. (org-overlay-put org-agenda-restriction-lock-overlay
  20263. 'face 'org-agenda-restriction-lock)
  20264. (org-overlay-put org-agenda-restriction-lock-overlay
  20265. 'help-echo "Agendas are currently limited to this subtree.")
  20266. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20267. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20268. "Overlay marking the agenda restriction line in speedbar.")
  20269. (org-overlay-put org-speedbar-restriction-lock-overlay
  20270. 'face 'org-agenda-restriction-lock)
  20271. (org-overlay-put org-speedbar-restriction-lock-overlay
  20272. 'help-echo "Agendas are currently limited to this item.")
  20273. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20274. (defun org-agenda-set-restriction-lock (&optional type)
  20275. "Set restriction lock for agenda, to current subtree or file.
  20276. Restriction will be the file if TYPE is `file', or if type is the
  20277. universal prefix '(4), or if the cursor is before the first headline
  20278. in the file. Otherwise, restriction will be to the current subtree."
  20279. (interactive "P")
  20280. (and (equal type '(4)) (setq type 'file))
  20281. (setq type (cond
  20282. (type type)
  20283. ((org-at-heading-p) 'subtree)
  20284. ((condition-case nil (org-back-to-heading t) (error nil))
  20285. 'subtree)
  20286. (t 'file)))
  20287. (if (eq type 'subtree)
  20288. (progn
  20289. (setq org-agenda-restrict t)
  20290. (setq org-agenda-overriding-restriction 'subtree)
  20291. (put 'org-agenda-files 'org-restrict
  20292. (list (buffer-file-name (buffer-base-buffer))))
  20293. (org-back-to-heading t)
  20294. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20295. (move-marker org-agenda-restrict-begin (point))
  20296. (move-marker org-agenda-restrict-end
  20297. (save-excursion (org-end-of-subtree t)))
  20298. (message "Locking agenda restriction to subtree"))
  20299. (put 'org-agenda-files 'org-restrict
  20300. (list (buffer-file-name (buffer-base-buffer))))
  20301. (setq org-agenda-restrict nil)
  20302. (setq org-agenda-overriding-restriction 'file)
  20303. (move-marker org-agenda-restrict-begin nil)
  20304. (move-marker org-agenda-restrict-end nil)
  20305. (message "Locking agenda restriction to file"))
  20306. (setq current-prefix-arg nil)
  20307. (org-agenda-maybe-redo))
  20308. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20309. "Remove the agenda restriction lock."
  20310. (interactive "P")
  20311. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20312. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20313. (setq org-agenda-overriding-restriction nil)
  20314. (setq org-agenda-restrict nil)
  20315. (put 'org-agenda-files 'org-restrict nil)
  20316. (move-marker org-agenda-restrict-begin nil)
  20317. (move-marker org-agenda-restrict-end nil)
  20318. (setq current-prefix-arg nil)
  20319. (message "Agenda restriction lock removed")
  20320. (or noupdate (org-agenda-maybe-redo)))
  20321. (defun org-agenda-maybe-redo ()
  20322. "If there is any window showing the agenda view, update it."
  20323. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20324. (w0 (selected-window)))
  20325. (when w
  20326. (select-window w)
  20327. (org-agenda-redo)
  20328. (select-window w0)
  20329. (if org-agenda-overriding-restriction
  20330. (message "Agenda view shifted to new %s restriction"
  20331. org-agenda-overriding-restriction)
  20332. (message "Agenda restriction lock removed")))))
  20333. ;;; Agenda commands
  20334. (defun org-agenda-check-type (error &rest types)
  20335. "Check if agenda buffer is of allowed type.
  20336. If ERROR is non-nil, throw an error, otherwise just return nil."
  20337. (if (memq org-agenda-type types)
  20338. t
  20339. (if error
  20340. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20341. nil)))
  20342. (defun org-agenda-quit ()
  20343. "Exit agenda by removing the window or the buffer."
  20344. (interactive)
  20345. (let ((buf (current-buffer)))
  20346. (if (not (one-window-p)) (delete-window))
  20347. (kill-buffer buf)
  20348. (org-agenda-maybe-reset-markers 'force)
  20349. (org-columns-remove-overlays))
  20350. ;; Maybe restore the pre-agenda window configuration.
  20351. (and org-agenda-restore-windows-after-quit
  20352. (not (eq org-agenda-window-setup 'other-frame))
  20353. org-pre-agenda-window-conf
  20354. (set-window-configuration org-pre-agenda-window-conf)))
  20355. (defun org-agenda-exit ()
  20356. "Exit agenda by removing the window or the buffer.
  20357. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20358. Org-mode buffers visited directly by the user will not be touched."
  20359. (interactive)
  20360. (org-release-buffers org-agenda-new-buffers)
  20361. (setq org-agenda-new-buffers nil)
  20362. (org-agenda-quit))
  20363. (defun org-agenda-execute (arg)
  20364. "Execute another agenda command, keeping same window.\\<global-map>
  20365. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20366. (interactive "P")
  20367. (let ((org-agenda-window-setup 'current-window))
  20368. (org-agenda arg)))
  20369. (defun org-save-all-org-buffers ()
  20370. "Save all Org-mode buffers without user confirmation."
  20371. (interactive)
  20372. (message "Saving all Org-mode buffers...")
  20373. (save-some-buffers t 'org-mode-p)
  20374. (message "Saving all Org-mode buffers... done"))
  20375. (defun org-agenda-redo ()
  20376. "Rebuild Agenda.
  20377. When this is the global TODO list, a prefix argument will be interpreted."
  20378. (interactive)
  20379. (let* ((org-agenda-keep-modes t)
  20380. (line (org-current-line))
  20381. (window-line (- line (org-current-line (window-start))))
  20382. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20383. (message "Rebuilding agenda buffer...")
  20384. (org-let lprops '(eval org-agenda-redo-command))
  20385. (setq org-agenda-undo-list nil
  20386. org-agenda-pending-undo-list nil)
  20387. (message "Rebuilding agenda buffer...done")
  20388. (goto-line line)
  20389. (recenter window-line)))
  20390. (defun org-agenda-goto-date (date)
  20391. "Jump to DATE in agenda."
  20392. (interactive (list (org-read-date)))
  20393. (org-agenda-list nil date))
  20394. (defun org-agenda-goto-today ()
  20395. "Go to today."
  20396. (interactive)
  20397. (org-agenda-check-type t 'timeline 'agenda)
  20398. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20399. (cond
  20400. (tdpos (goto-char tdpos))
  20401. ((eq org-agenda-type 'agenda)
  20402. (let* ((sd (time-to-days
  20403. (time-subtract (current-time)
  20404. (list 0 (* 3600 org-extend-today-until) 0))))
  20405. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20406. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20407. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20408. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20409. (org-agenda-redo)
  20410. (org-agenda-find-same-or-today-or-agenda)))
  20411. (t (error "Cannot find today")))))
  20412. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20413. (goto-char
  20414. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20415. (text-property-any (point-min) (point-max) 'org-today t)
  20416. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20417. (point-min))))
  20418. (defun org-agenda-later (arg)
  20419. "Go forward in time by thee current span.
  20420. With prefix ARG, go forward that many times the current span."
  20421. (interactive "p")
  20422. (org-agenda-check-type t 'agenda)
  20423. (let* ((span org-agenda-span)
  20424. (sd org-starting-day)
  20425. (greg (calendar-gregorian-from-absolute sd))
  20426. (cnt (get-text-property (point) 'org-day-cnt))
  20427. greg2 nd)
  20428. (cond
  20429. ((eq span 'day)
  20430. (setq sd (+ arg sd) nd 1))
  20431. ((eq span 'week)
  20432. (setq sd (+ (* 7 arg) sd) nd 7))
  20433. ((eq span 'month)
  20434. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20435. sd (calendar-absolute-from-gregorian greg2))
  20436. (setcar greg2 (1+ (car greg2)))
  20437. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20438. ((eq span 'year)
  20439. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20440. sd (calendar-absolute-from-gregorian greg2))
  20441. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20442. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20443. (let ((org-agenda-overriding-arguments
  20444. (list (car org-agenda-last-arguments) sd nd t)))
  20445. (org-agenda-redo)
  20446. (org-agenda-find-same-or-today-or-agenda cnt))))
  20447. (defun org-agenda-earlier (arg)
  20448. "Go backward in time by the current span.
  20449. With prefix ARG, go backward that many times the current span."
  20450. (interactive "p")
  20451. (org-agenda-later (- arg)))
  20452. (defun org-agenda-day-view ()
  20453. "Switch to daily view for agenda."
  20454. (interactive)
  20455. (setq org-agenda-ndays 1)
  20456. (org-agenda-change-time-span 'day))
  20457. (defun org-agenda-week-view ()
  20458. "Switch to daily view for agenda."
  20459. (interactive)
  20460. (setq org-agenda-ndays 7)
  20461. (org-agenda-change-time-span 'week))
  20462. (defun org-agenda-month-view ()
  20463. "Switch to daily view for agenda."
  20464. (interactive)
  20465. (org-agenda-change-time-span 'month))
  20466. (defun org-agenda-year-view ()
  20467. "Switch to daily view for agenda."
  20468. (interactive)
  20469. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20470. (org-agenda-change-time-span 'year)
  20471. (error "Abort")))
  20472. (defun org-agenda-change-time-span (span)
  20473. "Change the agenda view to SPAN.
  20474. SPAN may be `day', `week', `month', `year'."
  20475. (org-agenda-check-type t 'agenda)
  20476. (if (equal org-agenda-span span)
  20477. (error "Viewing span is already \"%s\"" span))
  20478. (let* ((sd (or (get-text-property (point) 'day)
  20479. org-starting-day))
  20480. (computed (org-agenda-compute-time-span sd span))
  20481. (org-agenda-overriding-arguments
  20482. (list (car org-agenda-last-arguments)
  20483. (car computed) (cdr computed) t)))
  20484. (org-agenda-redo)
  20485. (org-agenda-find-same-or-today-or-agenda))
  20486. (org-agenda-set-mode-name)
  20487. (message "Switched to %s view" span))
  20488. (defun org-agenda-compute-time-span (sd span)
  20489. "Compute starting date and number of days for agenda.
  20490. SPAN may be `day', `week', `month', `year'. The return value
  20491. is a cons cell with the starting date and the number of days,
  20492. so that the date SD will be in that range."
  20493. (let* ((greg (calendar-gregorian-from-absolute sd))
  20494. nd)
  20495. (cond
  20496. ((eq span 'day)
  20497. (setq nd 1))
  20498. ((eq span 'week)
  20499. (let* ((nt (calendar-day-of-week
  20500. (calendar-gregorian-from-absolute sd)))
  20501. (d (if org-agenda-start-on-weekday
  20502. (- nt org-agenda-start-on-weekday)
  20503. 0)))
  20504. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20505. (setq nd 7)))
  20506. ((eq span 'month)
  20507. (setq sd (calendar-absolute-from-gregorian
  20508. (list (car greg) 1 (nth 2 greg)))
  20509. nd (- (calendar-absolute-from-gregorian
  20510. (list (1+ (car greg)) 1 (nth 2 greg)))
  20511. sd)))
  20512. ((eq span 'year)
  20513. (setq sd (calendar-absolute-from-gregorian
  20514. (list 1 1 (nth 2 greg)))
  20515. nd (- (calendar-absolute-from-gregorian
  20516. (list 1 1 (1+ (nth 2 greg))))
  20517. sd))))
  20518. (cons sd nd)))
  20519. ;; FIXME: does not work if user makes date format that starts with a blank
  20520. (defun org-agenda-next-date-line (&optional arg)
  20521. "Jump to the next line indicating a date in agenda buffer."
  20522. (interactive "p")
  20523. (org-agenda-check-type t 'agenda 'timeline)
  20524. (beginning-of-line 1)
  20525. (if (looking-at "^\\S-") (forward-char 1))
  20526. (if (not (re-search-forward "^\\S-" nil t arg))
  20527. (progn
  20528. (backward-char 1)
  20529. (error "No next date after this line in this buffer")))
  20530. (goto-char (match-beginning 0)))
  20531. (defun org-agenda-previous-date-line (&optional arg)
  20532. "Jump to the previous line indicating a date in agenda buffer."
  20533. (interactive "p")
  20534. (org-agenda-check-type t 'agenda 'timeline)
  20535. (beginning-of-line 1)
  20536. (if (not (re-search-backward "^\\S-" nil t arg))
  20537. (error "No previous date before this line in this buffer")))
  20538. ;; Initialize the highlight
  20539. (defvar org-hl (org-make-overlay 1 1))
  20540. (org-overlay-put org-hl 'face 'highlight)
  20541. (defun org-highlight (begin end &optional buffer)
  20542. "Highlight a region with overlay."
  20543. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20544. org-hl begin end (or buffer (current-buffer))))
  20545. (defun org-unhighlight ()
  20546. "Detach overlay INDEX."
  20547. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20548. ;; FIXME this is currently not used.
  20549. (defun org-highlight-until-next-command (beg end &optional buffer)
  20550. (org-highlight beg end buffer)
  20551. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20552. (defun org-unhighlight-once ()
  20553. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20554. (org-unhighlight))
  20555. (defun org-agenda-follow-mode ()
  20556. "Toggle follow mode in an agenda buffer."
  20557. (interactive)
  20558. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20559. (org-agenda-set-mode-name)
  20560. (message "Follow mode is %s"
  20561. (if org-agenda-follow-mode "on" "off")))
  20562. (defun org-agenda-log-mode ()
  20563. "Toggle log mode in an agenda buffer."
  20564. (interactive)
  20565. (org-agenda-check-type t 'agenda 'timeline)
  20566. (setq org-agenda-show-log (not org-agenda-show-log))
  20567. (org-agenda-set-mode-name)
  20568. (org-agenda-redo)
  20569. (message "Log mode is %s"
  20570. (if org-agenda-show-log "on" "off")))
  20571. (defun org-agenda-toggle-diary ()
  20572. "Toggle diary inclusion in an agenda buffer."
  20573. (interactive)
  20574. (org-agenda-check-type t 'agenda)
  20575. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20576. (org-agenda-redo)
  20577. (org-agenda-set-mode-name)
  20578. (message "Diary inclusion turned %s"
  20579. (if org-agenda-include-diary "on" "off")))
  20580. (defun org-agenda-toggle-time-grid ()
  20581. "Toggle time grid in an agenda buffer."
  20582. (interactive)
  20583. (org-agenda-check-type t 'agenda)
  20584. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20585. (org-agenda-redo)
  20586. (org-agenda-set-mode-name)
  20587. (message "Time-grid turned %s"
  20588. (if org-agenda-use-time-grid "on" "off")))
  20589. (defun org-agenda-set-mode-name ()
  20590. "Set the mode name to indicate all the small mode settings."
  20591. (setq mode-name
  20592. (concat "Org-Agenda"
  20593. (if (equal org-agenda-ndays 1) " Day" "")
  20594. (if (equal org-agenda-ndays 7) " Week" "")
  20595. (if org-agenda-follow-mode " Follow" "")
  20596. (if org-agenda-include-diary " Diary" "")
  20597. (if org-agenda-use-time-grid " Grid" "")
  20598. (if org-agenda-show-log " Log" "")))
  20599. (force-mode-line-update))
  20600. (defun org-agenda-post-command-hook ()
  20601. (and (eolp) (not (bolp)) (backward-char 1))
  20602. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20603. (if (and org-agenda-follow-mode
  20604. (get-text-property (point) 'org-marker))
  20605. (org-agenda-show)))
  20606. (defun org-agenda-show-priority ()
  20607. "Show the priority of the current item.
  20608. This priority is composed of the main priority given with the [#A] cookies,
  20609. and by additional input from the age of a schedules or deadline entry."
  20610. (interactive)
  20611. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20612. (message "Priority is %d" (if pri pri -1000))))
  20613. (defun org-agenda-show-tags ()
  20614. "Show the tags applicable to the current item."
  20615. (interactive)
  20616. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20617. (if tags
  20618. (message "Tags are :%s:"
  20619. (org-no-properties (mapconcat 'identity tags ":")))
  20620. (message "No tags associated with this line"))))
  20621. (defun org-agenda-goto (&optional highlight)
  20622. "Go to the Org-mode file which contains the item at point."
  20623. (interactive)
  20624. (let* ((marker (or (get-text-property (point) 'org-marker)
  20625. (org-agenda-error)))
  20626. (buffer (marker-buffer marker))
  20627. (pos (marker-position marker)))
  20628. (switch-to-buffer-other-window buffer)
  20629. (widen)
  20630. (goto-char pos)
  20631. (when (org-mode-p)
  20632. (org-show-context 'agenda)
  20633. (save-excursion
  20634. (and (outline-next-heading)
  20635. (org-flag-heading nil)))) ; show the next heading
  20636. (recenter (/ (window-height) 2))
  20637. (run-hooks 'org-agenda-after-show-hook)
  20638. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20639. (defvar org-agenda-after-show-hook nil
  20640. "Normal hook run after an item has been shown from the agenda.
  20641. Point is in the buffer where the item originated.")
  20642. (defun org-agenda-kill ()
  20643. "Kill the entry or subtree belonging to the current agenda entry."
  20644. (interactive)
  20645. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20646. (let* ((marker (or (get-text-property (point) 'org-marker)
  20647. (org-agenda-error)))
  20648. (buffer (marker-buffer marker))
  20649. (pos (marker-position marker))
  20650. (type (get-text-property (point) 'type))
  20651. dbeg dend (n 0) conf)
  20652. (org-with-remote-undo buffer
  20653. (with-current-buffer buffer
  20654. (save-excursion
  20655. (goto-char pos)
  20656. (if (and (org-mode-p) (not (member type '("sexp"))))
  20657. (setq dbeg (progn (org-back-to-heading t) (point))
  20658. dend (org-end-of-subtree t t))
  20659. (setq dbeg (point-at-bol)
  20660. dend (min (point-max) (1+ (point-at-eol)))))
  20661. (goto-char dbeg)
  20662. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20663. (setq conf (or (eq t org-agenda-confirm-kill)
  20664. (and (numberp org-agenda-confirm-kill)
  20665. (> n org-agenda-confirm-kill))))
  20666. (and conf
  20667. (not (y-or-n-p
  20668. (format "Delete entry with %d lines in buffer \"%s\"? "
  20669. n (buffer-name buffer))))
  20670. (error "Abort"))
  20671. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20672. (with-current-buffer buffer (delete-region dbeg dend))
  20673. (message "Agenda item and source killed"))))
  20674. (defun org-agenda-archive ()
  20675. "Kill the entry or subtree belonging to the current agenda entry."
  20676. (interactive)
  20677. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20678. (let* ((marker (or (get-text-property (point) 'org-marker)
  20679. (org-agenda-error)))
  20680. (buffer (marker-buffer marker))
  20681. (pos (marker-position marker)))
  20682. (org-with-remote-undo buffer
  20683. (with-current-buffer buffer
  20684. (if (org-mode-p)
  20685. (save-excursion
  20686. (goto-char pos)
  20687. (org-remove-subtree-entries-from-agenda)
  20688. (org-back-to-heading t)
  20689. (org-archive-subtree))
  20690. (error "Archiving works only in Org-mode files"))))))
  20691. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20692. "Remove all lines in the agenda that correspond to a given subtree.
  20693. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20694. If this information is not given, the function uses the tree at point."
  20695. (let ((buf (or buf (current-buffer))) m p)
  20696. (save-excursion
  20697. (unless (and beg end)
  20698. (org-back-to-heading t)
  20699. (setq beg (point))
  20700. (org-end-of-subtree t)
  20701. (setq end (point)))
  20702. (set-buffer (get-buffer org-agenda-buffer-name))
  20703. (save-excursion
  20704. (goto-char (point-max))
  20705. (beginning-of-line 1)
  20706. (while (not (bobp))
  20707. (when (and (setq m (get-text-property (point) 'org-marker))
  20708. (equal buf (marker-buffer m))
  20709. (setq p (marker-position m))
  20710. (>= p beg)
  20711. (<= p end))
  20712. (let ((inhibit-read-only t))
  20713. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20714. (beginning-of-line 0))))))
  20715. (defun org-agenda-open-link ()
  20716. "Follow the link in the current line, if any."
  20717. (interactive)
  20718. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20719. (save-excursion
  20720. (save-restriction
  20721. (narrow-to-region (point-at-bol) (point-at-eol))
  20722. (org-open-at-point))))
  20723. (defun org-agenda-copy-local-variable (var)
  20724. "Get a variable from a referenced buffer and install it here."
  20725. (let ((m (get-text-property (point) 'org-marker)))
  20726. (when (and m (buffer-live-p (marker-buffer m)))
  20727. (org-set-local var (with-current-buffer (marker-buffer m)
  20728. (symbol-value var))))))
  20729. (defun org-agenda-switch-to (&optional delete-other-windows)
  20730. "Go to the Org-mode file which contains the item at point."
  20731. (interactive)
  20732. (let* ((marker (or (get-text-property (point) 'org-marker)
  20733. (org-agenda-error)))
  20734. (buffer (marker-buffer marker))
  20735. (pos (marker-position marker)))
  20736. (switch-to-buffer buffer)
  20737. (and delete-other-windows (delete-other-windows))
  20738. (widen)
  20739. (goto-char pos)
  20740. (when (org-mode-p)
  20741. (org-show-context 'agenda)
  20742. (save-excursion
  20743. (and (outline-next-heading)
  20744. (org-flag-heading nil)))))) ; show the next heading
  20745. (defun org-agenda-goto-mouse (ev)
  20746. "Go to the Org-mode file which contains the item at the mouse click."
  20747. (interactive "e")
  20748. (mouse-set-point ev)
  20749. (org-agenda-goto))
  20750. (defun org-agenda-show ()
  20751. "Display the Org-mode file which contains the item at point."
  20752. (interactive)
  20753. (let ((win (selected-window)))
  20754. (org-agenda-goto t)
  20755. (select-window win)))
  20756. (defun org-agenda-recenter (arg)
  20757. "Display the Org-mode file which contains the item at point and recenter."
  20758. (interactive "P")
  20759. (let ((win (selected-window)))
  20760. (org-agenda-goto t)
  20761. (recenter arg)
  20762. (select-window win)))
  20763. (defun org-agenda-show-mouse (ev)
  20764. "Display the Org-mode file which contains the item at the mouse click."
  20765. (interactive "e")
  20766. (mouse-set-point ev)
  20767. (org-agenda-show))
  20768. (defun org-agenda-check-no-diary ()
  20769. "Check if the entry is a diary link and abort if yes."
  20770. (if (get-text-property (point) 'org-agenda-diary-link)
  20771. (org-agenda-error)))
  20772. (defun org-agenda-error ()
  20773. (error "Command not allowed in this line"))
  20774. (defun org-agenda-tree-to-indirect-buffer ()
  20775. "Show the subtree corresponding to the current entry in an indirect buffer.
  20776. This calls the command `org-tree-to-indirect-buffer' from the original
  20777. Org-mode buffer.
  20778. With numerical prefix arg ARG, go up to this level and then take that tree.
  20779. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20780. dedicated frame)."
  20781. (interactive)
  20782. (org-agenda-check-no-diary)
  20783. (let* ((marker (or (get-text-property (point) 'org-marker)
  20784. (org-agenda-error)))
  20785. (buffer (marker-buffer marker))
  20786. (pos (marker-position marker)))
  20787. (with-current-buffer buffer
  20788. (save-excursion
  20789. (goto-char pos)
  20790. (call-interactively 'org-tree-to-indirect-buffer)))))
  20791. (defvar org-last-heading-marker (make-marker)
  20792. "Marker pointing to the headline that last changed its TODO state
  20793. by a remote command from the agenda.")
  20794. (defun org-agenda-todo-nextset ()
  20795. "Switch TODO entry to next sequence."
  20796. (interactive)
  20797. (org-agenda-todo 'nextset))
  20798. (defun org-agenda-todo-previousset ()
  20799. "Switch TODO entry to previous sequence."
  20800. (interactive)
  20801. (org-agenda-todo 'previousset))
  20802. (defun org-agenda-todo (&optional arg)
  20803. "Cycle TODO state of line at point, also in Org-mode file.
  20804. This changes the line at point, all other lines in the agenda referring to
  20805. the same tree node, and the headline of the tree node in the Org-mode file."
  20806. (interactive "P")
  20807. (org-agenda-check-no-diary)
  20808. (let* ((col (current-column))
  20809. (marker (or (get-text-property (point) 'org-marker)
  20810. (org-agenda-error)))
  20811. (buffer (marker-buffer marker))
  20812. (pos (marker-position marker))
  20813. (hdmarker (get-text-property (point) 'org-hd-marker))
  20814. (inhibit-read-only t)
  20815. newhead)
  20816. (org-with-remote-undo buffer
  20817. (with-current-buffer buffer
  20818. (widen)
  20819. (goto-char pos)
  20820. (org-show-context 'agenda)
  20821. (save-excursion
  20822. (and (outline-next-heading)
  20823. (org-flag-heading nil))) ; show the next heading
  20824. (org-todo arg)
  20825. (and (bolp) (forward-char 1))
  20826. (setq newhead (org-get-heading))
  20827. (save-excursion
  20828. (org-back-to-heading)
  20829. (move-marker org-last-heading-marker (point))))
  20830. (beginning-of-line 1)
  20831. (save-excursion
  20832. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20833. (move-to-column col))))
  20834. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20835. "Change all lines in the agenda buffer which match HDMARKER.
  20836. The new content of the line will be NEWHEAD (as modified by
  20837. `org-format-agenda-item'). HDMARKER is checked with
  20838. `equal' against all `org-hd-marker' text properties in the file.
  20839. If FIXFACE is non-nil, the face of each item is modified acording to
  20840. the new TODO state."
  20841. (let* ((inhibit-read-only t)
  20842. props m pl undone-face done-face finish new dotime cat tags)
  20843. (save-excursion
  20844. (goto-char (point-max))
  20845. (beginning-of-line 1)
  20846. (while (not finish)
  20847. (setq finish (bobp))
  20848. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20849. (equal m hdmarker))
  20850. (setq props (text-properties-at (point))
  20851. dotime (get-text-property (point) 'dotime)
  20852. cat (get-text-property (point) 'org-category)
  20853. tags (get-text-property (point) 'tags)
  20854. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20855. pl (get-text-property (point) 'prefix-length)
  20856. undone-face (get-text-property (point) 'undone-face)
  20857. done-face (get-text-property (point) 'done-face))
  20858. (move-to-column pl)
  20859. (cond
  20860. ((equal new "")
  20861. (beginning-of-line 1)
  20862. (and (looking-at ".*\n?") (replace-match "")))
  20863. ((looking-at ".*")
  20864. (replace-match new t t)
  20865. (beginning-of-line 1)
  20866. (add-text-properties (point-at-bol) (point-at-eol) props)
  20867. (when fixface
  20868. (add-text-properties
  20869. (point-at-bol) (point-at-eol)
  20870. (list 'face
  20871. (if org-last-todo-state-is-todo
  20872. undone-face done-face))))
  20873. (org-agenda-highlight-todo 'line)
  20874. (beginning-of-line 1))
  20875. (t (error "Line update did not work"))))
  20876. (beginning-of-line 0)))
  20877. (org-finalize-agenda)))
  20878. (defun org-agenda-align-tags (&optional line)
  20879. "Align all tags in agenda items to `org-agenda-tags-column'."
  20880. (let ((inhibit-read-only t) l c)
  20881. (save-excursion
  20882. (goto-char (if line (point-at-bol) (point-min)))
  20883. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20884. (if line (point-at-eol) nil) t)
  20885. (add-text-properties
  20886. (match-beginning 2) (match-end 2)
  20887. (list 'face (list 'org-tag (get-text-property
  20888. (match-beginning 2) 'face))))
  20889. (setq l (- (match-end 2) (match-beginning 2))
  20890. c (if (< org-agenda-tags-column 0)
  20891. (- (abs org-agenda-tags-column) l)
  20892. org-agenda-tags-column))
  20893. (delete-region (match-beginning 1) (match-end 1))
  20894. (goto-char (match-beginning 1))
  20895. (insert (org-add-props
  20896. (make-string (max 1 (- c (current-column))) ?\ )
  20897. (text-properties-at (point))))))))
  20898. (defun org-agenda-priority-up ()
  20899. "Increase the priority of line at point, also in Org-mode file."
  20900. (interactive)
  20901. (org-agenda-priority 'up))
  20902. (defun org-agenda-priority-down ()
  20903. "Decrease the priority of line at point, also in Org-mode file."
  20904. (interactive)
  20905. (org-agenda-priority 'down))
  20906. (defun org-agenda-priority (&optional force-direction)
  20907. "Set the priority of line at point, also in Org-mode file.
  20908. This changes the line at point, all other lines in the agenda referring to
  20909. the same tree node, and the headline of the tree node in the Org-mode file."
  20910. (interactive)
  20911. (org-agenda-check-no-diary)
  20912. (let* ((marker (or (get-text-property (point) 'org-marker)
  20913. (org-agenda-error)))
  20914. (hdmarker (get-text-property (point) 'org-hd-marker))
  20915. (buffer (marker-buffer hdmarker))
  20916. (pos (marker-position hdmarker))
  20917. (inhibit-read-only t)
  20918. newhead)
  20919. (org-with-remote-undo buffer
  20920. (with-current-buffer buffer
  20921. (widen)
  20922. (goto-char pos)
  20923. (org-show-context 'agenda)
  20924. (save-excursion
  20925. (and (outline-next-heading)
  20926. (org-flag-heading nil))) ; show the next heading
  20927. (funcall 'org-priority force-direction)
  20928. (end-of-line 1)
  20929. (setq newhead (org-get-heading)))
  20930. (org-agenda-change-all-lines newhead hdmarker)
  20931. (beginning-of-line 1))))
  20932. (defun org-get-tags-at (&optional pos)
  20933. "Get a list of all headline tags applicable at POS.
  20934. POS defaults to point. If tags are inherited, the list contains
  20935. the targets in the same sequence as the headlines appear, i.e.
  20936. the tags of the current headline come last."
  20937. (interactive)
  20938. (let (tags lastpos)
  20939. (save-excursion
  20940. (save-restriction
  20941. (widen)
  20942. (goto-char (or pos (point)))
  20943. (save-match-data
  20944. (org-back-to-heading t)
  20945. (condition-case nil
  20946. (while (not (equal lastpos (point)))
  20947. (setq lastpos (point))
  20948. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  20949. (setq tags (append (org-split-string
  20950. (org-match-string-no-properties 1) ":")
  20951. tags)))
  20952. (or org-use-tag-inheritance (error ""))
  20953. (org-up-heading-all 1))
  20954. (error nil))))
  20955. tags)))
  20956. ;; FIXME: should fix the tags property of the agenda line.
  20957. (defun org-agenda-set-tags ()
  20958. "Set tags for the current headline."
  20959. (interactive)
  20960. (org-agenda-check-no-diary)
  20961. (if (and (org-region-active-p) (interactive-p))
  20962. (call-interactively 'org-change-tag-in-region)
  20963. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20964. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20965. (org-agenda-error)))
  20966. (buffer (marker-buffer hdmarker))
  20967. (pos (marker-position hdmarker))
  20968. (inhibit-read-only t)
  20969. newhead)
  20970. (org-with-remote-undo buffer
  20971. (with-current-buffer buffer
  20972. (widen)
  20973. (goto-char pos)
  20974. (save-excursion
  20975. (org-show-context 'agenda))
  20976. (save-excursion
  20977. (and (outline-next-heading)
  20978. (org-flag-heading nil))) ; show the next heading
  20979. (goto-char pos)
  20980. (call-interactively 'org-set-tags)
  20981. (end-of-line 1)
  20982. (setq newhead (org-get-heading)))
  20983. (org-agenda-change-all-lines newhead hdmarker)
  20984. (beginning-of-line 1)))))
  20985. (defun org-agenda-toggle-archive-tag ()
  20986. "Toggle the archive tag for the current entry."
  20987. (interactive)
  20988. (org-agenda-check-no-diary)
  20989. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20990. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20991. (org-agenda-error)))
  20992. (buffer (marker-buffer hdmarker))
  20993. (pos (marker-position hdmarker))
  20994. (inhibit-read-only t)
  20995. newhead)
  20996. (org-with-remote-undo buffer
  20997. (with-current-buffer buffer
  20998. (widen)
  20999. (goto-char pos)
  21000. (org-show-context 'agenda)
  21001. (save-excursion
  21002. (and (outline-next-heading)
  21003. (org-flag-heading nil))) ; show the next heading
  21004. (call-interactively 'org-toggle-archive-tag)
  21005. (end-of-line 1)
  21006. (setq newhead (org-get-heading)))
  21007. (org-agenda-change-all-lines newhead hdmarker)
  21008. (beginning-of-line 1))))
  21009. (defun org-agenda-date-later (arg &optional what)
  21010. "Change the date of this item to one day later."
  21011. (interactive "p")
  21012. (org-agenda-check-type t 'agenda 'timeline)
  21013. (org-agenda-check-no-diary)
  21014. (let* ((marker (or (get-text-property (point) 'org-marker)
  21015. (org-agenda-error)))
  21016. (buffer (marker-buffer marker))
  21017. (pos (marker-position marker)))
  21018. (org-with-remote-undo buffer
  21019. (with-current-buffer buffer
  21020. (widen)
  21021. (goto-char pos)
  21022. (if (not (org-at-timestamp-p))
  21023. (error "Cannot find time stamp"))
  21024. (org-timestamp-change arg (or what 'day)))
  21025. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21026. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21027. (defun org-agenda-date-earlier (arg &optional what)
  21028. "Change the date of this item to one day earlier."
  21029. (interactive "p")
  21030. (org-agenda-date-later (- arg) what))
  21031. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21032. "Show new date stamp via text properties."
  21033. ;; We use text properties to make this undoable
  21034. (let ((inhibit-read-only t))
  21035. (setq stamp (concat " " prefix " => " stamp))
  21036. (save-excursion
  21037. (goto-char (point-max))
  21038. (while (not (bobp))
  21039. (when (equal marker (get-text-property (point) 'org-marker))
  21040. (move-to-column (- (window-width) (length stamp)) t)
  21041. (if (featurep 'xemacs)
  21042. ;; Use `duplicable' property to trigger undo recording
  21043. (let ((ex (make-extent nil nil))
  21044. (gl (make-glyph stamp)))
  21045. (set-glyph-face gl 'secondary-selection)
  21046. (set-extent-properties
  21047. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21048. (insert-extent ex (1- (point)) (point-at-eol)))
  21049. (add-text-properties
  21050. (1- (point)) (point-at-eol)
  21051. (list 'display (org-add-props stamp nil
  21052. 'face 'secondary-selection))))
  21053. (beginning-of-line 1))
  21054. (beginning-of-line 0)))))
  21055. (defun org-agenda-date-prompt (arg)
  21056. "Change the date of this item. Date is prompted for, with default today.
  21057. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21058. be used to request time specification in the time stamp."
  21059. (interactive "P")
  21060. (org-agenda-check-type t 'agenda 'timeline)
  21061. (org-agenda-check-no-diary)
  21062. (let* ((marker (or (get-text-property (point) 'org-marker)
  21063. (org-agenda-error)))
  21064. (buffer (marker-buffer marker))
  21065. (pos (marker-position marker)))
  21066. (org-with-remote-undo buffer
  21067. (with-current-buffer buffer
  21068. (widen)
  21069. (goto-char pos)
  21070. (if (not (org-at-timestamp-p))
  21071. (error "Cannot find time stamp"))
  21072. (org-time-stamp arg)
  21073. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21074. (defun org-agenda-schedule (arg)
  21075. "Schedule the item at point."
  21076. (interactive "P")
  21077. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21078. (org-agenda-check-no-diary)
  21079. (let* ((marker (or (get-text-property (point) 'org-marker)
  21080. (org-agenda-error)))
  21081. (buffer (marker-buffer marker))
  21082. (pos (marker-position marker))
  21083. (org-insert-labeled-timestamps-at-point nil)
  21084. ts)
  21085. (org-with-remote-undo buffer
  21086. (with-current-buffer buffer
  21087. (widen)
  21088. (goto-char pos)
  21089. (setq ts (org-schedule arg)))
  21090. (org-agenda-show-new-time marker ts "S"))
  21091. (message "Item scheduled for %s" ts)))
  21092. (defun org-agenda-deadline (arg)
  21093. "Schedule the item at point."
  21094. (interactive "P")
  21095. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21096. (org-agenda-check-no-diary)
  21097. (let* ((marker (or (get-text-property (point) 'org-marker)
  21098. (org-agenda-error)))
  21099. (buffer (marker-buffer marker))
  21100. (pos (marker-position marker))
  21101. (org-insert-labeled-timestamps-at-point nil)
  21102. ts)
  21103. (org-with-remote-undo buffer
  21104. (with-current-buffer buffer
  21105. (widen)
  21106. (goto-char pos)
  21107. (setq ts (org-deadline arg)))
  21108. (org-agenda-show-new-time marker ts "S"))
  21109. (message "Deadline for this item set to %s" ts)))
  21110. (defun org-get-heading (&optional no-tags)
  21111. "Return the heading of the current entry, without the stars."
  21112. (save-excursion
  21113. (org-back-to-heading t)
  21114. (if (looking-at
  21115. (if no-tags
  21116. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21117. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21118. (match-string 1) "")))
  21119. (defun org-agenda-clock-in (&optional arg)
  21120. "Start the clock on the currently selected item."
  21121. (interactive "P")
  21122. (org-agenda-check-no-diary)
  21123. (let* ((marker (or (get-text-property (point) 'org-marker)
  21124. (org-agenda-error)))
  21125. (pos (marker-position marker)))
  21126. (org-with-remote-undo (marker-buffer marker)
  21127. (with-current-buffer (marker-buffer marker)
  21128. (widen)
  21129. (goto-char pos)
  21130. (org-clock-in)))))
  21131. (defun org-agenda-clock-out (&optional arg)
  21132. "Stop the currently running clock."
  21133. (interactive "P")
  21134. (unless (marker-buffer org-clock-marker)
  21135. (error "No running clock"))
  21136. (org-with-remote-undo (marker-buffer org-clock-marker)
  21137. (org-clock-out)))
  21138. (defun org-agenda-clock-cancel (&optional arg)
  21139. "Cancel the currently running clock."
  21140. (interactive "P")
  21141. (unless (marker-buffer org-clock-marker)
  21142. (error "No running clock"))
  21143. (org-with-remote-undo (marker-buffer org-clock-marker)
  21144. (org-clock-cancel)))
  21145. (defun org-agenda-diary-entry ()
  21146. "Make a diary entry, like the `i' command from the calendar.
  21147. All the standard commands work: block, weekly etc."
  21148. (interactive)
  21149. (org-agenda-check-type t 'agenda 'timeline)
  21150. (require 'diary-lib)
  21151. (let* ((char (progn
  21152. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21153. (read-char-exclusive)))
  21154. (cmd (cdr (assoc char
  21155. '((?d . insert-diary-entry)
  21156. (?w . insert-weekly-diary-entry)
  21157. (?m . insert-monthly-diary-entry)
  21158. (?y . insert-yearly-diary-entry)
  21159. (?a . insert-anniversary-diary-entry)
  21160. (?b . insert-block-diary-entry)
  21161. (?c . insert-cyclic-diary-entry)))))
  21162. (oldf (symbol-function 'calendar-cursor-to-date))
  21163. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21164. (point (point))
  21165. (mark (or (mark t) (point))))
  21166. (unless cmd
  21167. (error "No command associated with <%c>" char))
  21168. (unless (and (get-text-property point 'day)
  21169. (or (not (equal ?b char))
  21170. (get-text-property mark 'day)))
  21171. (error "Don't know which date to use for diary entry"))
  21172. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21173. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21174. (let ((calendar-mark-ring
  21175. (list (calendar-gregorian-from-absolute
  21176. (or (get-text-property mark 'day)
  21177. (get-text-property point 'day))))))
  21178. (unwind-protect
  21179. (progn
  21180. (fset 'calendar-cursor-to-date
  21181. (lambda (&optional error)
  21182. (calendar-gregorian-from-absolute
  21183. (get-text-property point 'day))))
  21184. (call-interactively cmd))
  21185. (fset 'calendar-cursor-to-date oldf)))))
  21186. (defun org-agenda-execute-calendar-command (cmd)
  21187. "Execute a calendar command from the agenda, with the date associated to
  21188. the cursor position."
  21189. (org-agenda-check-type t 'agenda 'timeline)
  21190. (require 'diary-lib)
  21191. (unless (get-text-property (point) 'day)
  21192. (error "Don't know which date to use for calendar command"))
  21193. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21194. (point (point))
  21195. (date (calendar-gregorian-from-absolute
  21196. (get-text-property point 'day)))
  21197. ;; the following 3 vars are needed in the calendar
  21198. (displayed-day (extract-calendar-day date))
  21199. (displayed-month (extract-calendar-month date))
  21200. (displayed-year (extract-calendar-year date)))
  21201. (unwind-protect
  21202. (progn
  21203. (fset 'calendar-cursor-to-date
  21204. (lambda (&optional error)
  21205. (calendar-gregorian-from-absolute
  21206. (get-text-property point 'day))))
  21207. (call-interactively cmd))
  21208. (fset 'calendar-cursor-to-date oldf))))
  21209. (defun org-agenda-phases-of-moon ()
  21210. "Display the phases of the moon for the 3 months around the cursor date."
  21211. (interactive)
  21212. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21213. (defun org-agenda-holidays ()
  21214. "Display the holidays for the 3 months around the cursor date."
  21215. (interactive)
  21216. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21217. (defun org-agenda-sunrise-sunset (arg)
  21218. "Display sunrise and sunset for the cursor date.
  21219. Latitude and longitude can be specified with the variables
  21220. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21221. argument, latitude and longitude will be prompted for."
  21222. (interactive "P")
  21223. (let ((calendar-longitude (if arg nil calendar-longitude))
  21224. (calendar-latitude (if arg nil calendar-latitude))
  21225. (calendar-location-name
  21226. (if arg "the given coordinates" calendar-location-name)))
  21227. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21228. (defun org-agenda-goto-calendar ()
  21229. "Open the Emacs calendar with the date at the cursor."
  21230. (interactive)
  21231. (org-agenda-check-type t 'agenda 'timeline)
  21232. (let* ((day (or (get-text-property (point) 'day)
  21233. (error "Don't know which date to open in calendar")))
  21234. (date (calendar-gregorian-from-absolute day))
  21235. (calendar-move-hook nil)
  21236. (view-calendar-holidays-initially nil)
  21237. (view-diary-entries-initially nil))
  21238. (calendar)
  21239. (calendar-goto-date date)))
  21240. (defun org-calendar-goto-agenda ()
  21241. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21242. This is a command that has to be installed in `calendar-mode-map'."
  21243. (interactive)
  21244. (org-agenda-list nil (calendar-absolute-from-gregorian
  21245. (calendar-cursor-to-date))
  21246. nil))
  21247. (defun org-agenda-convert-date ()
  21248. (interactive)
  21249. (org-agenda-check-type t 'agenda 'timeline)
  21250. (let ((day (get-text-property (point) 'day))
  21251. date s)
  21252. (unless day
  21253. (error "Don't know which date to convert"))
  21254. (setq date (calendar-gregorian-from-absolute day))
  21255. (setq s (concat
  21256. "Gregorian: " (calendar-date-string date) "\n"
  21257. "ISO: " (calendar-iso-date-string date) "\n"
  21258. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21259. "Julian: " (calendar-julian-date-string date) "\n"
  21260. "Astron. JD: " (calendar-astro-date-string date)
  21261. " (Julian date number at noon UTC)\n"
  21262. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21263. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21264. "French: " (calendar-french-date-string date) "\n"
  21265. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21266. "Mayan: " (calendar-mayan-date-string date) "\n"
  21267. "Coptic: " (calendar-coptic-date-string date) "\n"
  21268. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21269. "Persian: " (calendar-persian-date-string date) "\n"
  21270. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21271. (with-output-to-temp-buffer "*Dates*"
  21272. (princ s))
  21273. (if (fboundp 'fit-window-to-buffer)
  21274. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21275. ;;;; Embedded LaTeX
  21276. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21277. "Keymap for the minor `org-cdlatex-mode'.")
  21278. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21279. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21280. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21281. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21282. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21283. (defvar org-cdlatex-texmathp-advice-is-done nil
  21284. "Flag remembering if we have applied the advice to texmathp already.")
  21285. (define-minor-mode org-cdlatex-mode
  21286. "Toggle the minor `org-cdlatex-mode'.
  21287. This mode supports entering LaTeX environment and math in LaTeX fragments
  21288. in Org-mode.
  21289. \\{org-cdlatex-mode-map}"
  21290. nil " OCDL" nil
  21291. (when org-cdlatex-mode (require 'cdlatex))
  21292. (unless org-cdlatex-texmathp-advice-is-done
  21293. (setq org-cdlatex-texmathp-advice-is-done t)
  21294. (defadvice texmathp (around org-math-always-on activate)
  21295. "Always return t in org-mode buffers.
  21296. This is because we want to insert math symbols without dollars even outside
  21297. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21298. en embedded LaTeX fragement, let texmathp do its job.
  21299. \\[org-cdlatex-mode-map]"
  21300. (interactive)
  21301. (let (p)
  21302. (cond
  21303. ((not (org-mode-p)) ad-do-it)
  21304. ((eq this-command 'cdlatex-math-symbol)
  21305. (setq ad-return-value t
  21306. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21307. (t
  21308. (let ((p (org-inside-LaTeX-fragment-p)))
  21309. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21310. (setq ad-return-value t
  21311. texmathp-why '("Org-mode embedded math" . 0))
  21312. (if p ad-do-it)))))))))
  21313. (defun turn-on-org-cdlatex ()
  21314. "Unconditionally turn on `org-cdlatex-mode'."
  21315. (org-cdlatex-mode 1))
  21316. (defun org-inside-LaTeX-fragment-p ()
  21317. "Test if point is inside a LaTeX fragment.
  21318. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21319. sequence appearing also before point.
  21320. Even though the matchers for math are configurable, this function assumes
  21321. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21322. delimiters are skipped when they have been removed by customization.
  21323. The return value is nil, or a cons cell with the delimiter and
  21324. and the position of this delimiter.
  21325. This function does a reasonably good job, but can locally be fooled by
  21326. for example currency specifications. For example it will assume being in
  21327. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21328. fragments that are properly closed, but during editing, we have to live
  21329. with the uncertainty caused by missing closing delimiters. This function
  21330. looks only before point, not after."
  21331. (catch 'exit
  21332. (let ((pos (point))
  21333. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21334. (lim (progn
  21335. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21336. (point)))
  21337. dd-on str (start 0) m re)
  21338. (goto-char pos)
  21339. (when dodollar
  21340. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21341. re (nth 1 (assoc "$" org-latex-regexps)))
  21342. (while (string-match re str start)
  21343. (cond
  21344. ((= (match-end 0) (length str))
  21345. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21346. ((= (match-end 0) (- (length str) 5))
  21347. (throw 'exit nil))
  21348. (t (setq start (match-end 0))))))
  21349. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21350. (goto-char pos)
  21351. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21352. (and (match-beginning 2) (throw 'exit nil))
  21353. ;; count $$
  21354. (while (re-search-backward "\\$\\$" lim t)
  21355. (setq dd-on (not dd-on)))
  21356. (goto-char pos)
  21357. (if dd-on (cons "$$" m))))))
  21358. (defun org-try-cdlatex-tab ()
  21359. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21360. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21361. - inside a LaTeX fragment, or
  21362. - after the first word in a line, where an abbreviation expansion could
  21363. insert a LaTeX environment."
  21364. (when org-cdlatex-mode
  21365. (cond
  21366. ((save-excursion
  21367. (skip-chars-backward "a-zA-Z0-9*")
  21368. (skip-chars-backward " \t")
  21369. (bolp))
  21370. (cdlatex-tab) t)
  21371. ((org-inside-LaTeX-fragment-p)
  21372. (cdlatex-tab) t)
  21373. (t nil))))
  21374. (defun org-cdlatex-underscore-caret (&optional arg)
  21375. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21376. Revert to the normal definition outside of these fragments."
  21377. (interactive "P")
  21378. (if (org-inside-LaTeX-fragment-p)
  21379. (call-interactively 'cdlatex-sub-superscript)
  21380. (let (org-cdlatex-mode)
  21381. (call-interactively (key-binding (vector last-input-event))))))
  21382. (defun org-cdlatex-math-modify (&optional arg)
  21383. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21384. Revert to the normal definition outside of these fragments."
  21385. (interactive "P")
  21386. (if (org-inside-LaTeX-fragment-p)
  21387. (call-interactively 'cdlatex-math-modify)
  21388. (let (org-cdlatex-mode)
  21389. (call-interactively (key-binding (vector last-input-event))))))
  21390. (defvar org-latex-fragment-image-overlays nil
  21391. "List of overlays carrying the images of latex fragments.")
  21392. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21393. (defun org-remove-latex-fragment-image-overlays ()
  21394. "Remove all overlays with LaTeX fragment images in current buffer."
  21395. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21396. (setq org-latex-fragment-image-overlays nil))
  21397. (defun org-preview-latex-fragment (&optional subtree)
  21398. "Preview the LaTeX fragment at point, or all locally or globally.
  21399. If the cursor is in a LaTeX fragment, create the image and overlay
  21400. it over the source code. If there is no fragment at point, display
  21401. all fragments in the current text, from one headline to the next. With
  21402. prefix SUBTREE, display all fragments in the current subtree. With a
  21403. double prefix `C-u C-u', or when the cursor is before the first headline,
  21404. display all fragments in the buffer.
  21405. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21406. (interactive "P")
  21407. (org-remove-latex-fragment-image-overlays)
  21408. (save-excursion
  21409. (save-restriction
  21410. (let (beg end at msg)
  21411. (cond
  21412. ((or (equal subtree '(16))
  21413. (not (save-excursion
  21414. (re-search-backward (concat "^" outline-regexp) nil t))))
  21415. (setq beg (point-min) end (point-max)
  21416. msg "Creating images for buffer...%s"))
  21417. ((equal subtree '(4))
  21418. (org-back-to-heading)
  21419. (setq beg (point) end (org-end-of-subtree t)
  21420. msg "Creating images for subtree...%s"))
  21421. (t
  21422. (if (setq at (org-inside-LaTeX-fragment-p))
  21423. (goto-char (max (point-min) (- (cdr at) 2)))
  21424. (org-back-to-heading))
  21425. (setq beg (point) end (progn (outline-next-heading) (point))
  21426. msg (if at "Creating image...%s"
  21427. "Creating images for entry...%s"))))
  21428. (message msg "")
  21429. (narrow-to-region beg end)
  21430. (goto-char beg)
  21431. (org-format-latex
  21432. (concat "ltxpng/" (file-name-sans-extension
  21433. (file-name-nondirectory
  21434. buffer-file-name)))
  21435. default-directory 'overlays msg at 'forbuffer)
  21436. (message msg "done. Use `C-c C-c' to remove images.")))))
  21437. (defvar org-latex-regexps
  21438. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21439. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21440. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21441. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21442. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21443. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21444. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21445. "Regular expressions for matching embedded LaTeX.")
  21446. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21447. "Replace LaTeX fragments with links to an image, and produce images."
  21448. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21449. (let* ((prefixnodir (file-name-nondirectory prefix))
  21450. (absprefix (expand-file-name prefix dir))
  21451. (todir (file-name-directory absprefix))
  21452. (opt org-format-latex-options)
  21453. (matchers (plist-get opt :matchers))
  21454. (re-list org-latex-regexps)
  21455. (cnt 0) txt link beg end re e checkdir
  21456. m n block linkfile movefile ov)
  21457. ;; Check if there are old images files with this prefix, and remove them
  21458. (when (file-directory-p todir)
  21459. (mapc 'delete-file
  21460. (directory-files
  21461. todir 'full
  21462. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21463. ;; Check the different regular expressions
  21464. (while (setq e (pop re-list))
  21465. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21466. block (if (nth 3 e) "\n\n" ""))
  21467. (when (member m matchers)
  21468. (goto-char (point-min))
  21469. (while (re-search-forward re nil t)
  21470. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21471. (setq txt (match-string n)
  21472. beg (match-beginning n) end (match-end n)
  21473. cnt (1+ cnt)
  21474. linkfile (format "%s_%04d.png" prefix cnt)
  21475. movefile (format "%s_%04d.png" absprefix cnt)
  21476. link (concat block "[[file:" linkfile "]]" block))
  21477. (if msg (message msg cnt))
  21478. (goto-char beg)
  21479. (unless checkdir ; make sure the directory exists
  21480. (setq checkdir t)
  21481. (or (file-directory-p todir) (make-directory todir)))
  21482. (org-create-formula-image
  21483. txt movefile opt forbuffer)
  21484. (if overlays
  21485. (progn
  21486. (setq ov (org-make-overlay beg end))
  21487. (if (featurep 'xemacs)
  21488. (progn
  21489. (org-overlay-put ov 'invisible t)
  21490. (org-overlay-put
  21491. ov 'end-glyph
  21492. (make-glyph (vector 'png :file movefile))))
  21493. (org-overlay-put
  21494. ov 'display
  21495. (list 'image :type 'png :file movefile :ascent 'center)))
  21496. (push ov org-latex-fragment-image-overlays)
  21497. (goto-char end))
  21498. (delete-region beg end)
  21499. (insert link))))))))
  21500. ;; This function borrows from Ganesh Swami's latex2png.el
  21501. (defun org-create-formula-image (string tofile options buffer)
  21502. (let* ((tmpdir (if (featurep 'xemacs)
  21503. (temp-directory)
  21504. temporary-file-directory))
  21505. (texfilebase (make-temp-name
  21506. (expand-file-name "orgtex" tmpdir)))
  21507. (texfile (concat texfilebase ".tex"))
  21508. (dvifile (concat texfilebase ".dvi"))
  21509. (pngfile (concat texfilebase ".png"))
  21510. (fnh (face-attribute 'default :height nil))
  21511. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21512. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21513. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21514. "Black"))
  21515. (bg (or (plist-get options (if buffer :background :html-background))
  21516. "Transparent")))
  21517. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21518. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21519. (with-temp-file texfile
  21520. (insert org-format-latex-header
  21521. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21522. (let ((dir default-directory))
  21523. (condition-case nil
  21524. (progn
  21525. (cd tmpdir)
  21526. (call-process "latex" nil nil nil texfile))
  21527. (error nil))
  21528. (cd dir))
  21529. (if (not (file-exists-p dvifile))
  21530. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21531. (call-process "dvipng" nil nil nil
  21532. "-E" "-fg" fg "-bg" bg
  21533. "-D" dpi
  21534. ;;"-x" scale "-y" scale
  21535. "-T" "tight"
  21536. "-o" pngfile
  21537. dvifile)
  21538. (if (not (file-exists-p pngfile))
  21539. (progn (message "Failed to create png file from %s" texfile) nil)
  21540. ;; Use the requested file name and clean up
  21541. (copy-file pngfile tofile 'replace)
  21542. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21543. (delete-file (concat texfilebase e)))
  21544. pngfile))))
  21545. (defun org-dvipng-color (attr)
  21546. "Return an rgb color specification for dvipng."
  21547. (apply 'format "rgb %s %s %s"
  21548. (mapcar 'org-normalize-color
  21549. (color-values (face-attribute 'default attr nil)))))
  21550. (defun org-normalize-color (value)
  21551. "Return string to be used as color value for an RGB component."
  21552. (format "%g" (/ value 65535.0)))
  21553. ;;;; Exporting
  21554. ;;; Variables, constants, and parameter plists
  21555. (defconst org-level-max 20)
  21556. (defvar org-export-html-preamble nil
  21557. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21558. (defvar org-export-html-postamble nil
  21559. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21560. (defvar org-export-html-auto-preamble t
  21561. "Should default preamble be inserted? Set by publishing functions.")
  21562. (defvar org-export-html-auto-postamble t
  21563. "Should default postamble be inserted? Set by publishing functions.")
  21564. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21565. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21566. (defconst org-export-plist-vars
  21567. '((:language . org-export-default-language)
  21568. (:customtime . org-display-custom-times)
  21569. (:headline-levels . org-export-headline-levels)
  21570. (:section-numbers . org-export-with-section-numbers)
  21571. (:table-of-contents . org-export-with-toc)
  21572. (:preserve-breaks . org-export-preserve-breaks)
  21573. (:archived-trees . org-export-with-archived-trees)
  21574. (:emphasize . org-export-with-emphasize)
  21575. (:sub-superscript . org-export-with-sub-superscripts)
  21576. (:special-strings . org-export-with-special-strings)
  21577. (:footnotes . org-export-with-footnotes)
  21578. (:drawers . org-export-with-drawers)
  21579. (:tags . org-export-with-tags)
  21580. (:TeX-macros . org-export-with-TeX-macros)
  21581. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21582. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21583. (:fixed-width . org-export-with-fixed-width)
  21584. (:timestamps . org-export-with-timestamps)
  21585. (:author-info . org-export-author-info)
  21586. (:time-stamp-file . org-export-time-stamp-file)
  21587. (:tables . org-export-with-tables)
  21588. (:table-auto-headline . org-export-highlight-first-table-line)
  21589. (:style . org-export-html-style)
  21590. (:agenda-style . org-agenda-export-html-style)
  21591. (:convert-org-links . org-export-html-link-org-files-as-html)
  21592. (:inline-images . org-export-html-inline-images)
  21593. (:html-extension . org-export-html-extension)
  21594. (:html-table-tag . org-export-html-table-tag)
  21595. (:expand-quoted-html . org-export-html-expand)
  21596. (:timestamp . org-export-html-with-timestamp)
  21597. (:publishing-directory . org-export-publishing-directory)
  21598. (:preamble . org-export-html-preamble)
  21599. (:postamble . org-export-html-postamble)
  21600. (:auto-preamble . org-export-html-auto-preamble)
  21601. (:auto-postamble . org-export-html-auto-postamble)
  21602. (:author . user-full-name)
  21603. (:email . user-mail-address)))
  21604. (defun org-default-export-plist ()
  21605. "Return the property list with default settings for the export variables."
  21606. (let ((l org-export-plist-vars) rtn e)
  21607. (while (setq e (pop l))
  21608. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21609. rtn))
  21610. (defun org-infile-export-plist ()
  21611. "Return the property list with file-local settings for export."
  21612. (save-excursion
  21613. (save-restriction
  21614. (widen)
  21615. (goto-char 0)
  21616. (let ((re (org-make-options-regexp
  21617. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21618. p key val text options)
  21619. (while (re-search-forward re nil t)
  21620. (setq key (org-match-string-no-properties 1)
  21621. val (org-match-string-no-properties 2))
  21622. (cond
  21623. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21624. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21625. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21626. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21627. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21628. ((string-equal key "TEXT")
  21629. (setq text (if text (concat text "\n" val) val)))
  21630. ((string-equal key "OPTIONS") (setq options val))))
  21631. (setq p (plist-put p :text text))
  21632. (when options
  21633. (let ((op '(("H" . :headline-levels)
  21634. ("num" . :section-numbers)
  21635. ("toc" . :table-of-contents)
  21636. ("\\n" . :preserve-breaks)
  21637. ("@" . :expand-quoted-html)
  21638. (":" . :fixed-width)
  21639. ("|" . :tables)
  21640. ("^" . :sub-superscript)
  21641. ("-" . :special-strings)
  21642. ("f" . :footnotes)
  21643. ("d" . :drawers)
  21644. ("tags" . :tags)
  21645. ("*" . :emphasize)
  21646. ("TeX" . :TeX-macros)
  21647. ("LaTeX" . :LaTeX-fragments)
  21648. ("skip" . :skip-before-1st-heading)
  21649. ("author" . :author-info)
  21650. ("timestamp" . :time-stamp-file)))
  21651. o)
  21652. (while (setq o (pop op))
  21653. (if (string-match (concat (regexp-quote (car o))
  21654. ":\\([^ \t\n\r;,.]*\\)")
  21655. options)
  21656. (setq p (plist-put p (cdr o)
  21657. (car (read-from-string
  21658. (match-string 1 options)))))))))
  21659. p))))
  21660. (defun org-export-directory (type plist)
  21661. (let* ((val (plist-get plist :publishing-directory))
  21662. (dir (if (listp val)
  21663. (or (cdr (assoc type val)) ".")
  21664. val)))
  21665. dir))
  21666. (defun org-skip-comments (lines)
  21667. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21668. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21669. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21670. (case-fold-search nil)
  21671. rtn line level)
  21672. (while (setq line (pop lines))
  21673. (cond
  21674. ((and (string-match re1 line)
  21675. (setq level (- (match-end 1) (match-beginning 1))))
  21676. ;; Beginning of a COMMENT subtree. Skip it.
  21677. (while (and (setq line (pop lines))
  21678. (or (not (string-match re2 line))
  21679. (> (- (match-end 1) (match-beginning 1)) level))))
  21680. (setq lines (cons line lines)))
  21681. ((string-match "^#" line)
  21682. ;; an ordinary comment line
  21683. )
  21684. ((and org-export-table-remove-special-lines
  21685. (string-match "^[ \t]*|" line)
  21686. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21687. (and (string-match "| *<[0-9]+> *|" line)
  21688. (not (string-match "| *[^ <|]" line)))))
  21689. ;; a special table line that should be removed
  21690. )
  21691. (t (setq rtn (cons line rtn)))))
  21692. (nreverse rtn)))
  21693. (defun org-export (&optional arg)
  21694. (interactive)
  21695. (let ((help "[t] insert the export option template
  21696. \[v] limit export to visible part of outline tree
  21697. \[a] export as ASCII
  21698. \[h] export as HTML
  21699. \[H] export as HTML to temporary buffer
  21700. \[R] export region as HTML
  21701. \[b] export as HTML and browse immediately
  21702. \[x] export as XOXO
  21703. \[l] export as LaTeX
  21704. \[L] export as LaTeX to temporary buffer
  21705. \[i] export current file as iCalendar file
  21706. \[I] export all agenda files as iCalendar files
  21707. \[c] export agenda files into combined iCalendar file
  21708. \[F] publish current file
  21709. \[P] publish current project
  21710. \[X] publish... (project will be prompted for)
  21711. \[A] publish all projects")
  21712. (cmds
  21713. '((?t . org-insert-export-options-template)
  21714. (?v . org-export-visible)
  21715. (?a . org-export-as-ascii)
  21716. (?h . org-export-as-html)
  21717. (?b . org-export-as-html-and-open)
  21718. (?H . org-export-as-html-to-buffer)
  21719. (?R . org-export-region-as-html)
  21720. (?x . org-export-as-xoxo)
  21721. (?l . org-export-as-latex)
  21722. (?L . org-export-as-latex-to-buffer)
  21723. (?i . org-export-icalendar-this-file)
  21724. (?I . org-export-icalendar-all-agenda-files)
  21725. (?c . org-export-icalendar-combine-agenda-files)
  21726. (?F . org-publish-current-file)
  21727. (?P . org-publish-current-project)
  21728. (?X . org-publish)
  21729. (?A . org-publish-all)))
  21730. r1 r2 ass)
  21731. (save-window-excursion
  21732. (delete-other-windows)
  21733. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21734. (princ help))
  21735. (message "Select command: ")
  21736. (setq r1 (read-char-exclusive)))
  21737. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21738. (if (setq ass (assq r2 cmds))
  21739. (call-interactively (cdr ass))
  21740. (error "No command associated with key %c" r1))))
  21741. (defconst org-html-entities
  21742. '(("nbsp")
  21743. ("iexcl")
  21744. ("cent")
  21745. ("pound")
  21746. ("curren")
  21747. ("yen")
  21748. ("brvbar")
  21749. ("vert" . "&#124;")
  21750. ("sect")
  21751. ("uml")
  21752. ("copy")
  21753. ("ordf")
  21754. ("laquo")
  21755. ("not")
  21756. ("shy")
  21757. ("reg")
  21758. ("macr")
  21759. ("deg")
  21760. ("plusmn")
  21761. ("sup2")
  21762. ("sup3")
  21763. ("acute")
  21764. ("micro")
  21765. ("para")
  21766. ("middot")
  21767. ("odot"."o")
  21768. ("star"."*")
  21769. ("cedil")
  21770. ("sup1")
  21771. ("ordm")
  21772. ("raquo")
  21773. ("frac14")
  21774. ("frac12")
  21775. ("frac34")
  21776. ("iquest")
  21777. ("Agrave")
  21778. ("Aacute")
  21779. ("Acirc")
  21780. ("Atilde")
  21781. ("Auml")
  21782. ("Aring") ("AA"."&Aring;")
  21783. ("AElig")
  21784. ("Ccedil")
  21785. ("Egrave")
  21786. ("Eacute")
  21787. ("Ecirc")
  21788. ("Euml")
  21789. ("Igrave")
  21790. ("Iacute")
  21791. ("Icirc")
  21792. ("Iuml")
  21793. ("ETH")
  21794. ("Ntilde")
  21795. ("Ograve")
  21796. ("Oacute")
  21797. ("Ocirc")
  21798. ("Otilde")
  21799. ("Ouml")
  21800. ("times")
  21801. ("Oslash")
  21802. ("Ugrave")
  21803. ("Uacute")
  21804. ("Ucirc")
  21805. ("Uuml")
  21806. ("Yacute")
  21807. ("THORN")
  21808. ("szlig")
  21809. ("agrave")
  21810. ("aacute")
  21811. ("acirc")
  21812. ("atilde")
  21813. ("auml")
  21814. ("aring")
  21815. ("aelig")
  21816. ("ccedil")
  21817. ("egrave")
  21818. ("eacute")
  21819. ("ecirc")
  21820. ("euml")
  21821. ("igrave")
  21822. ("iacute")
  21823. ("icirc")
  21824. ("iuml")
  21825. ("eth")
  21826. ("ntilde")
  21827. ("ograve")
  21828. ("oacute")
  21829. ("ocirc")
  21830. ("otilde")
  21831. ("ouml")
  21832. ("divide")
  21833. ("oslash")
  21834. ("ugrave")
  21835. ("uacute")
  21836. ("ucirc")
  21837. ("uuml")
  21838. ("yacute")
  21839. ("thorn")
  21840. ("yuml")
  21841. ("fnof")
  21842. ("Alpha")
  21843. ("Beta")
  21844. ("Gamma")
  21845. ("Delta")
  21846. ("Epsilon")
  21847. ("Zeta")
  21848. ("Eta")
  21849. ("Theta")
  21850. ("Iota")
  21851. ("Kappa")
  21852. ("Lambda")
  21853. ("Mu")
  21854. ("Nu")
  21855. ("Xi")
  21856. ("Omicron")
  21857. ("Pi")
  21858. ("Rho")
  21859. ("Sigma")
  21860. ("Tau")
  21861. ("Upsilon")
  21862. ("Phi")
  21863. ("Chi")
  21864. ("Psi")
  21865. ("Omega")
  21866. ("alpha")
  21867. ("beta")
  21868. ("gamma")
  21869. ("delta")
  21870. ("epsilon")
  21871. ("varepsilon"."&epsilon;")
  21872. ("zeta")
  21873. ("eta")
  21874. ("theta")
  21875. ("iota")
  21876. ("kappa")
  21877. ("lambda")
  21878. ("mu")
  21879. ("nu")
  21880. ("xi")
  21881. ("omicron")
  21882. ("pi")
  21883. ("rho")
  21884. ("sigmaf") ("varsigma"."&sigmaf;")
  21885. ("sigma")
  21886. ("tau")
  21887. ("upsilon")
  21888. ("phi")
  21889. ("chi")
  21890. ("psi")
  21891. ("omega")
  21892. ("thetasym") ("vartheta"."&thetasym;")
  21893. ("upsih")
  21894. ("piv")
  21895. ("bull") ("bullet"."&bull;")
  21896. ("hellip") ("dots"."&hellip;")
  21897. ("prime")
  21898. ("Prime")
  21899. ("oline")
  21900. ("frasl")
  21901. ("weierp")
  21902. ("image")
  21903. ("real")
  21904. ("trade")
  21905. ("alefsym")
  21906. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21907. ("uarr") ("uparrow"."&uarr;")
  21908. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21909. ("darr")("downarrow"."&darr;")
  21910. ("harr") ("leftrightarrow"."&harr;")
  21911. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21912. ("lArr") ("Leftarrow"."&lArr;")
  21913. ("uArr") ("Uparrow"."&uArr;")
  21914. ("rArr") ("Rightarrow"."&rArr;")
  21915. ("dArr") ("Downarrow"."&dArr;")
  21916. ("hArr") ("Leftrightarrow"."&hArr;")
  21917. ("forall")
  21918. ("part") ("partial"."&part;")
  21919. ("exist") ("exists"."&exist;")
  21920. ("empty") ("emptyset"."&empty;")
  21921. ("nabla")
  21922. ("isin") ("in"."&isin;")
  21923. ("notin")
  21924. ("ni")
  21925. ("prod")
  21926. ("sum")
  21927. ("minus")
  21928. ("lowast") ("ast"."&lowast;")
  21929. ("radic")
  21930. ("prop") ("proptp"."&prop;")
  21931. ("infin") ("infty"."&infin;")
  21932. ("ang") ("angle"."&ang;")
  21933. ("and") ("wedge"."&and;")
  21934. ("or") ("vee"."&or;")
  21935. ("cap")
  21936. ("cup")
  21937. ("int")
  21938. ("there4")
  21939. ("sim")
  21940. ("cong") ("simeq"."&cong;")
  21941. ("asymp")("approx"."&asymp;")
  21942. ("ne") ("neq"."&ne;")
  21943. ("equiv")
  21944. ("le")
  21945. ("ge")
  21946. ("sub") ("subset"."&sub;")
  21947. ("sup") ("supset"."&sup;")
  21948. ("nsub")
  21949. ("sube")
  21950. ("supe")
  21951. ("oplus")
  21952. ("otimes")
  21953. ("perp")
  21954. ("sdot") ("cdot"."&sdot;")
  21955. ("lceil")
  21956. ("rceil")
  21957. ("lfloor")
  21958. ("rfloor")
  21959. ("lang")
  21960. ("rang")
  21961. ("loz") ("Diamond"."&loz;")
  21962. ("spades") ("spadesuit"."&spades;")
  21963. ("clubs") ("clubsuit"."&clubs;")
  21964. ("hearts") ("diamondsuit"."&hearts;")
  21965. ("diams") ("diamondsuit"."&diams;")
  21966. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  21967. ("quot")
  21968. ("amp")
  21969. ("lt")
  21970. ("gt")
  21971. ("OElig")
  21972. ("oelig")
  21973. ("Scaron")
  21974. ("scaron")
  21975. ("Yuml")
  21976. ("circ")
  21977. ("tilde")
  21978. ("ensp")
  21979. ("emsp")
  21980. ("thinsp")
  21981. ("zwnj")
  21982. ("zwj")
  21983. ("lrm")
  21984. ("rlm")
  21985. ("ndash")
  21986. ("mdash")
  21987. ("lsquo")
  21988. ("rsquo")
  21989. ("sbquo")
  21990. ("ldquo")
  21991. ("rdquo")
  21992. ("bdquo")
  21993. ("dagger")
  21994. ("Dagger")
  21995. ("permil")
  21996. ("lsaquo")
  21997. ("rsaquo")
  21998. ("euro")
  21999. ("arccos"."arccos")
  22000. ("arcsin"."arcsin")
  22001. ("arctan"."arctan")
  22002. ("arg"."arg")
  22003. ("cos"."cos")
  22004. ("cosh"."cosh")
  22005. ("cot"."cot")
  22006. ("coth"."coth")
  22007. ("csc"."csc")
  22008. ("deg"."deg")
  22009. ("det"."det")
  22010. ("dim"."dim")
  22011. ("exp"."exp")
  22012. ("gcd"."gcd")
  22013. ("hom"."hom")
  22014. ("inf"."inf")
  22015. ("ker"."ker")
  22016. ("lg"."lg")
  22017. ("lim"."lim")
  22018. ("liminf"."liminf")
  22019. ("limsup"."limsup")
  22020. ("ln"."ln")
  22021. ("log"."log")
  22022. ("max"."max")
  22023. ("min"."min")
  22024. ("Pr"."Pr")
  22025. ("sec"."sec")
  22026. ("sin"."sin")
  22027. ("sinh"."sinh")
  22028. ("sup"."sup")
  22029. ("tan"."tan")
  22030. ("tanh"."tanh")
  22031. )
  22032. "Entities for TeX->HTML translation.
  22033. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22034. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22035. In that case, \"\\ent\" will be translated to \"&other;\".
  22036. The list contains HTML entities for Latin-1, Greek and other symbols.
  22037. It is supplemented by a number of commonly used TeX macros with appropriate
  22038. translations. There is currently no way for users to extend this.")
  22039. ;;; General functions for all backends
  22040. (defun org-cleaned-string-for-export (string &rest parameters)
  22041. "Cleanup a buffer STRING so that links can be created safely."
  22042. (interactive)
  22043. (let* ((re-radio (and org-target-link-regexp
  22044. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22045. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22046. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22047. (re-archive (concat ":" org-archive-tag ":"))
  22048. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22049. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22050. (htmlp (plist-get parameters :for-html))
  22051. (asciip (plist-get parameters :for-ascii))
  22052. (latexp (plist-get parameters :for-LaTeX))
  22053. (commentsp (plist-get parameters :comments))
  22054. (archived-trees (plist-get parameters :archived-trees))
  22055. (inhibit-read-only t)
  22056. (drawers org-drawers)
  22057. (exp-drawers (plist-get parameters :drawers))
  22058. (outline-regexp "\\*+ ")
  22059. a b xx
  22060. rtn p)
  22061. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22062. (erase-buffer)
  22063. (insert string)
  22064. ;; Remove license-to-kill stuff
  22065. (while (setq p (text-property-any (point-min) (point-max)
  22066. :org-license-to-kill t))
  22067. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22068. (let ((org-inhibit-startup t)) (org-mode))
  22069. (untabify (point-min) (point-max))
  22070. ;; Get the correct stuff before the first headline
  22071. (when (plist-get parameters :skip-before-1st-heading)
  22072. (goto-char (point-min))
  22073. (when (re-search-forward "^\\*+[ \t]" nil t)
  22074. (delete-region (point-min) (match-beginning 0))
  22075. (goto-char (point-min))
  22076. (insert "\n")))
  22077. (when (plist-get parameters :add-text)
  22078. (goto-char (point-min))
  22079. (insert (plist-get parameters :add-text) "\n"))
  22080. ;; Get rid of archived trees
  22081. (when (not (eq archived-trees t))
  22082. (goto-char (point-min))
  22083. (while (re-search-forward re-archive nil t)
  22084. (if (not (org-on-heading-p t))
  22085. (org-end-of-subtree t)
  22086. (beginning-of-line 1)
  22087. (setq a (if archived-trees
  22088. (1+ (point-at-eol)) (point))
  22089. b (org-end-of-subtree t))
  22090. (if (> b a) (delete-region a b)))))
  22091. ;; Get rid of drawers
  22092. (unless (eq t exp-drawers)
  22093. (goto-char (point-min))
  22094. (let ((re (concat "^[ \t]*:\\("
  22095. (mapconcat
  22096. 'identity
  22097. (org-delete-all exp-drawers
  22098. (copy-sequence drawers))
  22099. "\\|")
  22100. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22101. (while (re-search-forward re nil t)
  22102. (replace-match ""))))
  22103. ;; Find targets in comments and move them out of comments,
  22104. ;; but mark them as targets that should be invisible
  22105. (goto-char (point-min))
  22106. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22107. (replace-match "\\1(INVISIBLE)"))
  22108. ;; Protect backend specific stuff, throw away the others.
  22109. (let ((formatters
  22110. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22111. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22112. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22113. fmt)
  22114. (goto-char (point-min))
  22115. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22116. (goto-char (match-end 0))
  22117. (while (not (looking-at "#\\+END_EXAMPLE"))
  22118. (insert ": ")
  22119. (beginning-of-line 2)))
  22120. (goto-char (point-min))
  22121. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22122. (add-text-properties (match-beginning 0) (match-end 0)
  22123. '(org-protected t)))
  22124. (while formatters
  22125. (setq fmt (pop formatters))
  22126. (when (car fmt)
  22127. (goto-char (point-min))
  22128. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22129. ":[ \t]*\\(.*\\)") nil t)
  22130. (replace-match "\\1" t)
  22131. (add-text-properties
  22132. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22133. '(org-protected t))))
  22134. (goto-char (point-min))
  22135. (while (re-search-forward
  22136. (concat "^#\\+"
  22137. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22138. (cadddr fmt) "\\>.*\n?") nil t)
  22139. (if (car fmt)
  22140. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22141. '(org-protected t))
  22142. (delete-region (match-beginning 0) (match-end 0))))))
  22143. ;; Protect quoted subtrees
  22144. (goto-char (point-min))
  22145. (while (re-search-forward re-quote nil t)
  22146. (goto-char (match-beginning 0))
  22147. (end-of-line 1)
  22148. (add-text-properties (point) (org-end-of-subtree t)
  22149. '(org-protected t)))
  22150. ;; Protect verbatim elements
  22151. (goto-char (point-min))
  22152. (while (re-search-forward org-verbatim-re nil t)
  22153. (add-text-properties (match-beginning 4) (match-end 4)
  22154. '(org-protected t))
  22155. (goto-char (1+ (match-end 4))))
  22156. ;; Remove subtrees that are commented
  22157. (goto-char (point-min))
  22158. (while (re-search-forward re-commented nil t)
  22159. (goto-char (match-beginning 0))
  22160. (delete-region (point) (org-end-of-subtree t)))
  22161. ;; Remove special table lines
  22162. (when org-export-table-remove-special-lines
  22163. (goto-char (point-min))
  22164. (while (re-search-forward "^[ \t]*|" nil t)
  22165. (beginning-of-line 1)
  22166. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22167. (and (looking-at ".*?| *<[0-9]+> *|")
  22168. (not (looking-at ".*?| *[^ <|]"))))
  22169. (delete-region (max (point-min) (1- (point-at-bol)))
  22170. (point-at-eol))
  22171. (end-of-line 1))))
  22172. ;; Specific LaTeX stuff
  22173. (when latexp
  22174. (require 'org-export-latex nil)
  22175. (org-export-latex-cleaned-string))
  22176. (when asciip
  22177. (org-export-ascii-clean-string))
  22178. ;; Specific HTML stuff
  22179. (when htmlp
  22180. ;; Convert LaTeX fragments to images
  22181. (when (plist-get parameters :LaTeX-fragments)
  22182. (org-format-latex
  22183. (concat "ltxpng/" (file-name-sans-extension
  22184. (file-name-nondirectory
  22185. org-current-export-file)))
  22186. org-current-export-dir nil "Creating LaTeX image %s"))
  22187. (message "Exporting..."))
  22188. ;; Remove or replace comments
  22189. (goto-char (point-min))
  22190. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22191. (if commentsp
  22192. (progn (add-text-properties
  22193. (match-beginning 0) (match-end 0) '(org-protected t))
  22194. (replace-match (format commentsp (match-string 1)) t t))
  22195. (replace-match "")))
  22196. ;; Find matches for radio targets and turn them into internal links
  22197. (goto-char (point-min))
  22198. (when re-radio
  22199. (while (re-search-forward re-radio nil t)
  22200. (org-if-unprotected
  22201. (replace-match "\\1[[\\2]]"))))
  22202. ;; Find all links that contain a newline and put them into a single line
  22203. (goto-char (point-min))
  22204. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22205. (org-if-unprotected
  22206. (replace-match "\\1 \\3")
  22207. (goto-char (match-beginning 0))))
  22208. ;; Normalize links: Convert angle and plain links into bracket links
  22209. ;; Expand link abbreviations
  22210. (goto-char (point-min))
  22211. (while (re-search-forward re-plain-link nil t)
  22212. (goto-char (1- (match-end 0)))
  22213. (org-if-unprotected
  22214. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22215. ":" (match-string 3) "]]")))
  22216. ;; added 'org-link face to links
  22217. (put-text-property 0 (length s) 'face 'org-link s)
  22218. (replace-match s t t))))
  22219. (goto-char (point-min))
  22220. (while (re-search-forward re-angle-link nil t)
  22221. (goto-char (1- (match-end 0)))
  22222. (org-if-unprotected
  22223. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22224. ":" (match-string 3) "]]")))
  22225. (put-text-property 0 (length s) 'face 'org-link s)
  22226. (replace-match s t t))))
  22227. (goto-char (point-min))
  22228. (while (re-search-forward org-bracket-link-regexp nil t)
  22229. (org-if-unprotected
  22230. (let* ((s (concat "[[" (setq xx (save-match-data
  22231. (org-link-expand-abbrev (match-string 1))))
  22232. "]"
  22233. (if (match-end 3)
  22234. (match-string 2)
  22235. (concat "[" xx "]"))
  22236. "]")))
  22237. (put-text-property 0 (length s) 'face 'org-link s)
  22238. (replace-match s t t))))
  22239. ;; Find multiline emphasis and put them into single line
  22240. (when (plist-get parameters :emph-multiline)
  22241. (goto-char (point-min))
  22242. (while (re-search-forward org-emph-re nil t)
  22243. (if (not (= (char-after (match-beginning 3))
  22244. (char-after (match-beginning 4))))
  22245. (org-if-unprotected
  22246. (subst-char-in-region (match-beginning 0) (match-end 0)
  22247. ?\n ?\ t)
  22248. (goto-char (1- (match-end 0))))
  22249. (goto-char (1+ (match-beginning 0))))))
  22250. (setq rtn (buffer-string)))
  22251. (kill-buffer " org-mode-tmp")
  22252. rtn))
  22253. (defun org-export-grab-title-from-buffer ()
  22254. "Get a title for the current document, from looking at the buffer."
  22255. (let ((inhibit-read-only t))
  22256. (save-excursion
  22257. (goto-char (point-min))
  22258. (let ((end (save-excursion (outline-next-heading) (point))))
  22259. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22260. ;; Mark the line so that it will not be exported as normal text.
  22261. (org-unmodified
  22262. (add-text-properties (match-beginning 0) (match-end 0)
  22263. (list :org-license-to-kill t)))
  22264. ;; Return the title string
  22265. (org-trim (match-string 0)))))))
  22266. (defun org-export-get-title-from-subtree ()
  22267. "Return subtree title and exclude it from export."
  22268. (let (title (m (mark)))
  22269. (save-excursion
  22270. (goto-char (region-beginning))
  22271. (when (and (org-at-heading-p)
  22272. (>= (org-end-of-subtree t t) (region-end)))
  22273. ;; This is a subtree, we take the title from the first heading
  22274. (goto-char (region-beginning))
  22275. (looking-at org-todo-line-regexp)
  22276. (setq title (match-string 3))
  22277. (org-unmodified
  22278. (add-text-properties (point) (1+ (point-at-eol))
  22279. (list :org-license-to-kill t)))))
  22280. title))
  22281. (defun org-solidify-link-text (s &optional alist)
  22282. "Take link text and make a safe target out of it."
  22283. (save-match-data
  22284. (let* ((rtn
  22285. (mapconcat
  22286. 'identity
  22287. (org-split-string s "[ \t\r\n]+") "--"))
  22288. (a (assoc rtn alist)))
  22289. (or (cdr a) rtn))))
  22290. (defun org-get-min-level (lines)
  22291. "Get the minimum level in LINES."
  22292. (let ((re "^\\(\\*+\\) ") l min)
  22293. (catch 'exit
  22294. (while (setq l (pop lines))
  22295. (if (string-match re l)
  22296. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22297. 1)))
  22298. ;; Variable holding the vector with section numbers
  22299. (defvar org-section-numbers (make-vector org-level-max 0))
  22300. (defun org-init-section-numbers ()
  22301. "Initialize the vector for the section numbers."
  22302. (let* ((level -1)
  22303. (numbers (nreverse (org-split-string "" "\\.")))
  22304. (depth (1- (length org-section-numbers)))
  22305. (i depth) number-string)
  22306. (while (>= i 0)
  22307. (if (> i level)
  22308. (aset org-section-numbers i 0)
  22309. (setq number-string (or (car numbers) "0"))
  22310. (if (string-match "\\`[A-Z]\\'" number-string)
  22311. (aset org-section-numbers i
  22312. (- (string-to-char number-string) ?A -1))
  22313. (aset org-section-numbers i (string-to-number number-string)))
  22314. (pop numbers))
  22315. (setq i (1- i)))))
  22316. (defun org-section-number (&optional level)
  22317. "Return a string with the current section number.
  22318. When LEVEL is non-nil, increase section numbers on that level."
  22319. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22320. (when level
  22321. (when (> level -1)
  22322. (aset org-section-numbers
  22323. level (1+ (aref org-section-numbers level))))
  22324. (setq idx (1+ level))
  22325. (while (<= idx depth)
  22326. (if (not (= idx 1))
  22327. (aset org-section-numbers idx 0))
  22328. (setq idx (1+ idx))))
  22329. (setq idx 0)
  22330. (while (<= idx depth)
  22331. (setq n (aref org-section-numbers idx))
  22332. (setq string (concat string (if (not (string= string "")) "." "")
  22333. (int-to-string n)))
  22334. (setq idx (1+ idx)))
  22335. (save-match-data
  22336. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22337. (setq string (replace-match "" t nil string)))
  22338. (if (string-match "\\(\\.0\\)+\\'" string)
  22339. (setq string (replace-match "" t nil string))))
  22340. string))
  22341. ;;; ASCII export
  22342. (defvar org-last-level nil) ; dynamically scoped variable
  22343. (defvar org-min-level nil) ; dynamically scoped variable
  22344. (defvar org-levels-open nil) ; dynamically scoped parameter
  22345. (defvar org-ascii-current-indentation nil) ; For communication
  22346. (defun org-export-as-ascii (arg)
  22347. "Export the outline as a pretty ASCII file.
  22348. If there is an active region, export only the region.
  22349. The prefix ARG specifies how many levels of the outline should become
  22350. underlined headlines. The default is 3."
  22351. (interactive "P")
  22352. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22353. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22354. (org-infile-export-plist)))
  22355. (region-p (org-region-active-p))
  22356. (subtree-p
  22357. (when region-p
  22358. (save-excursion
  22359. (goto-char (region-beginning))
  22360. (and (org-at-heading-p)
  22361. (>= (org-end-of-subtree t t) (region-end))))))
  22362. (custom-times org-display-custom-times)
  22363. (org-ascii-current-indentation '(0 . 0))
  22364. (level 0) line txt
  22365. (umax nil)
  22366. (umax-toc nil)
  22367. (case-fold-search nil)
  22368. (filename (concat (file-name-as-directory
  22369. (org-export-directory :ascii opt-plist))
  22370. (file-name-sans-extension
  22371. (or (and subtree-p
  22372. (org-entry-get (region-beginning)
  22373. "EXPORT_FILE_NAME" t))
  22374. (file-name-nondirectory buffer-file-name)))
  22375. ".txt"))
  22376. (filename (if (equal (file-truename filename)
  22377. (file-truename buffer-file-name))
  22378. (concat filename ".txt")
  22379. filename))
  22380. (buffer (find-file-noselect filename))
  22381. (org-levels-open (make-vector org-level-max nil))
  22382. (odd org-odd-levels-only)
  22383. (date (plist-get opt-plist :date))
  22384. (author (plist-get opt-plist :author))
  22385. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22386. (plist-get opt-plist :title)
  22387. (and (not
  22388. (plist-get opt-plist :skip-before-1st-heading))
  22389. (org-export-grab-title-from-buffer))
  22390. (file-name-sans-extension
  22391. (file-name-nondirectory buffer-file-name))))
  22392. (email (plist-get opt-plist :email))
  22393. (language (plist-get opt-plist :language))
  22394. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22395. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22396. (todo nil)
  22397. (lang-words nil)
  22398. (region
  22399. (buffer-substring
  22400. (if (org-region-active-p) (region-beginning) (point-min))
  22401. (if (org-region-active-p) (region-end) (point-max))))
  22402. (lines (org-split-string
  22403. (org-cleaned-string-for-export
  22404. region
  22405. :for-ascii t
  22406. :skip-before-1st-heading
  22407. (plist-get opt-plist :skip-before-1st-heading)
  22408. :drawers (plist-get opt-plist :drawers)
  22409. :verbatim-multiline t
  22410. :archived-trees
  22411. (plist-get opt-plist :archived-trees)
  22412. :add-text (plist-get opt-plist :text))
  22413. "\n"))
  22414. thetoc have-headings first-heading-pos
  22415. table-open table-buffer)
  22416. (let ((inhibit-read-only t))
  22417. (org-unmodified
  22418. (remove-text-properties (point-min) (point-max)
  22419. '(:org-license-to-kill t))))
  22420. (setq org-min-level (org-get-min-level lines))
  22421. (setq org-last-level org-min-level)
  22422. (org-init-section-numbers)
  22423. (find-file-noselect filename)
  22424. (setq lang-words (or (assoc language org-export-language-setup)
  22425. (assoc "en" org-export-language-setup)))
  22426. (switch-to-buffer-other-window buffer)
  22427. (erase-buffer)
  22428. (fundamental-mode)
  22429. ;; create local variables for all options, to make sure all called
  22430. ;; functions get the correct information
  22431. (mapc (lambda (x)
  22432. (set (make-local-variable (cdr x))
  22433. (plist-get opt-plist (car x))))
  22434. org-export-plist-vars)
  22435. (org-set-local 'org-odd-levels-only odd)
  22436. (setq umax (if arg (prefix-numeric-value arg)
  22437. org-export-headline-levels))
  22438. (setq umax-toc (if (integerp org-export-with-toc)
  22439. (min org-export-with-toc umax)
  22440. umax))
  22441. ;; File header
  22442. (if title (org-insert-centered title ?=))
  22443. (insert "\n")
  22444. (if (and (or author email)
  22445. org-export-author-info)
  22446. (insert (concat (nth 1 lang-words) ": " (or author "")
  22447. (if email (concat " <" email ">") "")
  22448. "\n")))
  22449. (cond
  22450. ((and date (string-match "%" date))
  22451. (setq date (format-time-string date (current-time))))
  22452. (date)
  22453. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22454. (if (and date org-export-time-stamp-file)
  22455. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22456. (insert "\n\n")
  22457. (if org-export-with-toc
  22458. (progn
  22459. (push (concat (nth 3 lang-words) "\n") thetoc)
  22460. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22461. (mapc '(lambda (line)
  22462. (if (string-match org-todo-line-regexp
  22463. line)
  22464. ;; This is a headline
  22465. (progn
  22466. (setq have-headings t)
  22467. (setq level (- (match-end 1) (match-beginning 1))
  22468. level (org-tr-level level)
  22469. txt (match-string 3 line)
  22470. todo
  22471. (or (and org-export-mark-todo-in-toc
  22472. (match-beginning 2)
  22473. (not (member (match-string 2 line)
  22474. org-done-keywords)))
  22475. ; TODO, not DONE
  22476. (and org-export-mark-todo-in-toc
  22477. (= level umax-toc)
  22478. (org-search-todo-below
  22479. line lines level))))
  22480. (setq txt (org-html-expand-for-ascii txt))
  22481. (while (string-match org-bracket-link-regexp txt)
  22482. (setq txt
  22483. (replace-match
  22484. (match-string (if (match-end 2) 3 1) txt)
  22485. t t txt)))
  22486. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22487. (string-match
  22488. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22489. txt))
  22490. (setq txt (replace-match "" t t txt)))
  22491. (if (string-match quote-re0 txt)
  22492. (setq txt (replace-match "" t t txt)))
  22493. (if org-export-with-section-numbers
  22494. (setq txt (concat (org-section-number level)
  22495. " " txt)))
  22496. (if (<= level umax-toc)
  22497. (progn
  22498. (push
  22499. (concat
  22500. (make-string
  22501. (* (max 0 (- level org-min-level)) 4) ?\ )
  22502. (format (if todo "%s (*)\n" "%s\n") txt))
  22503. thetoc)
  22504. (setq org-last-level level))
  22505. ))))
  22506. lines)
  22507. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22508. (org-init-section-numbers)
  22509. (while (setq line (pop lines))
  22510. ;; Remove the quoted HTML tags.
  22511. (setq line (org-html-expand-for-ascii line))
  22512. ;; Remove targets
  22513. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22514. (setq line (replace-match "" t t line)))
  22515. ;; Replace internal links
  22516. (while (string-match org-bracket-link-regexp line)
  22517. (setq line (replace-match
  22518. (if (match-end 3) "[\\3]" "[\\1]")
  22519. t nil line)))
  22520. (when custom-times
  22521. (setq line (org-translate-time line)))
  22522. (cond
  22523. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22524. ;; a Headline
  22525. (setq first-heading-pos (or first-heading-pos (point)))
  22526. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22527. txt (match-string 2 line))
  22528. (org-ascii-level-start level txt umax lines))
  22529. ((and org-export-with-tables
  22530. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22531. (if (not table-open)
  22532. ;; New table starts
  22533. (setq table-open t table-buffer nil))
  22534. ;; Accumulate lines
  22535. (setq table-buffer (cons line table-buffer))
  22536. (when (or (not lines)
  22537. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22538. (car lines))))
  22539. (setq table-open nil
  22540. table-buffer (nreverse table-buffer))
  22541. (insert (mapconcat
  22542. (lambda (x)
  22543. (org-fix-indentation x org-ascii-current-indentation))
  22544. (org-format-table-ascii table-buffer)
  22545. "\n") "\n")))
  22546. (t
  22547. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22548. (if (and org-export-with-fixed-width
  22549. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22550. (setq line (replace-match "\\1" nil nil line)))
  22551. (insert line "\n"))))
  22552. (normal-mode)
  22553. ;; insert the table of contents
  22554. (when thetoc
  22555. (goto-char (point-min))
  22556. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22557. (progn
  22558. (goto-char (match-beginning 0))
  22559. (replace-match ""))
  22560. (goto-char first-heading-pos))
  22561. (mapc 'insert thetoc)
  22562. (or (looking-at "[ \t]*\n[ \t]*\n")
  22563. (insert "\n\n")))
  22564. ;; Convert whitespace place holders
  22565. (goto-char (point-min))
  22566. (let (beg end)
  22567. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22568. (setq end (next-single-property-change beg 'org-whitespace))
  22569. (goto-char beg)
  22570. (delete-region beg end)
  22571. (insert (make-string (- end beg) ?\ ))))
  22572. (save-buffer)
  22573. ;; remove display and invisible chars
  22574. (let (beg end)
  22575. (goto-char (point-min))
  22576. (while (setq beg (next-single-property-change (point) 'display))
  22577. (setq end (next-single-property-change beg 'display))
  22578. (delete-region beg end)
  22579. (goto-char beg)
  22580. (insert "=>"))
  22581. (goto-char (point-min))
  22582. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22583. (setq end (next-single-property-change beg 'org-cwidth))
  22584. (delete-region beg end)
  22585. (goto-char beg)))
  22586. (goto-char (point-min))))
  22587. (defun org-export-ascii-clean-string ()
  22588. "Do extra work for ASCII export"
  22589. (goto-char (point-min))
  22590. (while (re-search-forward org-verbatim-re nil t)
  22591. (goto-char (match-end 2))
  22592. (backward-delete-char 1) (insert "'")
  22593. (goto-char (match-beginning 2))
  22594. (delete-char 1) (insert "`")
  22595. (goto-char (match-end 2))))
  22596. (defun org-search-todo-below (line lines level)
  22597. "Search the subtree below LINE for any TODO entries."
  22598. (let ((rest (cdr (memq line lines)))
  22599. (re org-todo-line-regexp)
  22600. line lv todo)
  22601. (catch 'exit
  22602. (while (setq line (pop rest))
  22603. (if (string-match re line)
  22604. (progn
  22605. (setq lv (- (match-end 1) (match-beginning 1))
  22606. todo (and (match-beginning 2)
  22607. (not (member (match-string 2 line)
  22608. org-done-keywords))))
  22609. ; TODO, not DONE
  22610. (if (<= lv level) (throw 'exit nil))
  22611. (if todo (throw 'exit t))))))))
  22612. (defun org-html-expand-for-ascii (line)
  22613. "Handle quoted HTML for ASCII export."
  22614. (if org-export-html-expand
  22615. (while (string-match "@<[^<>\n]*>" line)
  22616. ;; We just remove the tags for now.
  22617. (setq line (replace-match "" nil nil line))))
  22618. line)
  22619. (defun org-insert-centered (s &optional underline)
  22620. "Insert the string S centered and underline it with character UNDERLINE."
  22621. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22622. (insert (make-string ind ?\ ) s "\n")
  22623. (if underline
  22624. (insert (make-string ind ?\ )
  22625. (make-string (string-width s) underline)
  22626. "\n"))))
  22627. (defun org-ascii-level-start (level title umax &optional lines)
  22628. "Insert a new level in ASCII export."
  22629. (let (char (n (- level umax 1)) (ind 0))
  22630. (if (> level umax)
  22631. (progn
  22632. (insert (make-string (* 2 n) ?\ )
  22633. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22634. org-export-ascii-bullets))
  22635. " " title "\n")
  22636. ;; find the indentation of the next non-empty line
  22637. (catch 'stop
  22638. (while lines
  22639. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22640. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22641. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22642. (pop lines)))
  22643. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22644. (if (or (not (equal (char-before) ?\n))
  22645. (not (equal (char-before (1- (point))) ?\n)))
  22646. (insert "\n"))
  22647. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22648. (unless org-export-with-tags
  22649. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22650. (setq title (replace-match "" t t title))))
  22651. (if org-export-with-section-numbers
  22652. (setq title (concat (org-section-number level) " " title)))
  22653. (insert title "\n" (make-string (string-width title) char) "\n")
  22654. (setq org-ascii-current-indentation '(0 . 0)))))
  22655. (defun org-export-visible (type arg)
  22656. "Create a copy of the visible part of the current buffer, and export it.
  22657. The copy is created in a temporary buffer and removed after use.
  22658. TYPE is the final key (as a string) that also select the export command in
  22659. the `C-c C-e' export dispatcher.
  22660. As a special case, if the you type SPC at the prompt, the temporary
  22661. org-mode file will not be removed but presented to you so that you can
  22662. continue to use it. The prefix arg ARG is passed through to the exporting
  22663. command."
  22664. (interactive
  22665. (list (progn
  22666. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22667. (read-char-exclusive))
  22668. current-prefix-arg))
  22669. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22670. (error "Invalid export key"))
  22671. (let* ((binding (cdr (assoc type
  22672. '((?a . org-export-as-ascii)
  22673. (?\C-a . org-export-as-ascii)
  22674. (?b . org-export-as-html-and-open)
  22675. (?\C-b . org-export-as-html-and-open)
  22676. (?h . org-export-as-html)
  22677. (?H . org-export-as-html-to-buffer)
  22678. (?R . org-export-region-as-html)
  22679. (?x . org-export-as-xoxo)))))
  22680. (keepp (equal type ?\ ))
  22681. (file buffer-file-name)
  22682. (buffer (get-buffer-create "*Org Export Visible*"))
  22683. s e)
  22684. ;; Need to hack the drawers here.
  22685. (save-excursion
  22686. (goto-char (point-min))
  22687. (while (re-search-forward org-drawer-regexp nil t)
  22688. (goto-char (match-beginning 1))
  22689. (or (org-invisible-p) (org-flag-drawer nil))))
  22690. (with-current-buffer buffer (erase-buffer))
  22691. (save-excursion
  22692. (setq s (goto-char (point-min)))
  22693. (while (not (= (point) (point-max)))
  22694. (goto-char (org-find-invisible))
  22695. (append-to-buffer buffer s (point))
  22696. (setq s (goto-char (org-find-visible))))
  22697. (org-cycle-hide-drawers 'all)
  22698. (goto-char (point-min))
  22699. (unless keepp
  22700. ;; Copy all comment lines to the end, to make sure #+ settings are
  22701. ;; still available for the second export step. Kind of a hack, but
  22702. ;; does do the trick.
  22703. (if (looking-at "#[^\r\n]*")
  22704. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22705. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22706. (append-to-buffer buffer (1+ (match-beginning 0))
  22707. (min (point-max) (1+ (match-end 0))))))
  22708. (set-buffer buffer)
  22709. (let ((buffer-file-name file)
  22710. (org-inhibit-startup t))
  22711. (org-mode)
  22712. (show-all)
  22713. (unless keepp (funcall binding arg))))
  22714. (if (not keepp)
  22715. (kill-buffer buffer)
  22716. (switch-to-buffer-other-window buffer)
  22717. (goto-char (point-min)))))
  22718. (defun org-find-visible ()
  22719. (let ((s (point)))
  22720. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22721. (get-char-property s 'invisible)))
  22722. s))
  22723. (defun org-find-invisible ()
  22724. (let ((s (point)))
  22725. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22726. (not (get-char-property s 'invisible))))
  22727. s))
  22728. ;;; HTML export
  22729. (defun org-get-current-options ()
  22730. "Return a string with current options as keyword options.
  22731. Does include HTML export options as well as TODO and CATEGORY stuff."
  22732. (format
  22733. "#+TITLE: %s
  22734. #+AUTHOR: %s
  22735. #+EMAIL: %s
  22736. #+LANGUAGE: %s
  22737. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22738. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
  22739. #+CATEGORY: %s
  22740. #+SEQ_TODO: %s
  22741. #+TYP_TODO: %s
  22742. #+PRIORITIES: %c %c %c
  22743. #+DRAWERS: %s
  22744. #+STARTUP: %s %s %s %s %s
  22745. #+TAGS: %s
  22746. #+ARCHIVE: %s
  22747. #+LINK: %s
  22748. "
  22749. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22750. org-export-headline-levels
  22751. org-export-with-section-numbers
  22752. org-export-with-toc
  22753. org-export-preserve-breaks
  22754. org-export-html-expand
  22755. org-export-with-fixed-width
  22756. org-export-with-tables
  22757. org-export-with-sub-superscripts
  22758. org-export-with-special-strings
  22759. org-export-with-footnotes
  22760. org-export-with-emphasize
  22761. org-export-with-TeX-macros
  22762. org-export-with-LaTeX-fragments
  22763. org-export-skip-text-before-1st-heading
  22764. org-export-with-drawers
  22765. org-export-with-tags
  22766. (file-name-nondirectory buffer-file-name)
  22767. "TODO FEEDBACK VERIFY DONE"
  22768. "Me Jason Marie DONE"
  22769. org-highest-priority org-lowest-priority org-default-priority
  22770. (mapconcat 'identity org-drawers " ")
  22771. (cdr (assoc org-startup-folded
  22772. '((nil . "showall") (t . "overview") (content . "content"))))
  22773. (if org-odd-levels-only "odd" "oddeven")
  22774. (if org-hide-leading-stars "hidestars" "showstars")
  22775. (if org-startup-align-all-tables "align" "noalign")
  22776. (cond ((eq t org-log-done) "logdone")
  22777. ((not org-log-done) "nologging")
  22778. ((listp org-log-done)
  22779. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  22780. org-log-done " ")))
  22781. (or (mapconcat (lambda (x)
  22782. (cond
  22783. ((equal '(:startgroup) x) "{")
  22784. ((equal '(:endgroup) x) "}")
  22785. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22786. (t (car x))))
  22787. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22788. org-archive-location
  22789. "org file:~/org/%s.org"
  22790. ))
  22791. (defun org-insert-export-options-template ()
  22792. "Insert into the buffer a template with information for exporting."
  22793. (interactive)
  22794. (if (not (bolp)) (newline))
  22795. (let ((s (org-get-current-options)))
  22796. (and (string-match "#\\+CATEGORY" s)
  22797. (setq s (substring s 0 (match-beginning 0))))
  22798. (insert s)))
  22799. (defun org-toggle-fixed-width-section (arg)
  22800. "Toggle the fixed-width export.
  22801. If there is no active region, the QUOTE keyword at the current headline is
  22802. inserted or removed. When present, it causes the text between this headline
  22803. and the next to be exported as fixed-width text, and unmodified.
  22804. If there is an active region, this command adds or removes a colon as the
  22805. first character of this line. If the first character of a line is a colon,
  22806. this line is also exported in fixed-width font."
  22807. (interactive "P")
  22808. (let* ((cc 0)
  22809. (regionp (org-region-active-p))
  22810. (beg (if regionp (region-beginning) (point)))
  22811. (end (if regionp (region-end)))
  22812. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22813. (case-fold-search nil)
  22814. (re "[ \t]*\\(:\\)")
  22815. off)
  22816. (if regionp
  22817. (save-excursion
  22818. (goto-char beg)
  22819. (setq cc (current-column))
  22820. (beginning-of-line 1)
  22821. (setq off (looking-at re))
  22822. (while (> nlines 0)
  22823. (setq nlines (1- nlines))
  22824. (beginning-of-line 1)
  22825. (cond
  22826. (arg
  22827. (move-to-column cc t)
  22828. (insert ":\n")
  22829. (forward-line -1))
  22830. ((and off (looking-at re))
  22831. (replace-match "" t t nil 1))
  22832. ((not off) (move-to-column cc t) (insert ":")))
  22833. (forward-line 1)))
  22834. (save-excursion
  22835. (org-back-to-heading)
  22836. (if (looking-at (concat outline-regexp
  22837. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22838. (replace-match "" t t nil 1)
  22839. (if (looking-at outline-regexp)
  22840. (progn
  22841. (goto-char (match-end 0))
  22842. (insert org-quote-string " "))))))))
  22843. (defun org-export-as-html-and-open (arg)
  22844. "Export the outline as HTML and immediately open it with a browser.
  22845. If there is an active region, export only the region.
  22846. The prefix ARG specifies how many levels of the outline should become
  22847. headlines. The default is 3. Lower levels will become bulleted lists."
  22848. (interactive "P")
  22849. (org-export-as-html arg 'hidden)
  22850. (org-open-file buffer-file-name))
  22851. (defun org-export-as-html-batch ()
  22852. "Call `org-export-as-html', may be used in batch processing as
  22853. emacs --batch
  22854. --load=$HOME/lib/emacs/org.el
  22855. --eval \"(setq org-export-headline-levels 2)\"
  22856. --visit=MyFile --funcall org-export-as-html-batch"
  22857. (org-export-as-html org-export-headline-levels 'hidden))
  22858. (defun org-export-as-html-to-buffer (arg)
  22859. "Call `org-exort-as-html` with output to a temporary buffer.
  22860. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22861. (interactive "P")
  22862. (org-export-as-html arg nil nil "*Org HTML Export*")
  22863. (switch-to-buffer-other-window "*Org HTML Export*"))
  22864. (defun org-replace-region-by-html (beg end)
  22865. "Assume the current region has org-mode syntax, and convert it to HTML.
  22866. This can be used in any buffer. For example, you could write an
  22867. itemized list in org-mode syntax in an HTML buffer and then use this
  22868. command to convert it."
  22869. (interactive "r")
  22870. (let (reg html buf pop-up-frames)
  22871. (save-window-excursion
  22872. (if (org-mode-p)
  22873. (setq html (org-export-region-as-html
  22874. beg end t 'string))
  22875. (setq reg (buffer-substring beg end)
  22876. buf (get-buffer-create "*Org tmp*"))
  22877. (with-current-buffer buf
  22878. (erase-buffer)
  22879. (insert reg)
  22880. (org-mode)
  22881. (setq html (org-export-region-as-html
  22882. (point-min) (point-max) t 'string)))
  22883. (kill-buffer buf)))
  22884. (delete-region beg end)
  22885. (insert html)))
  22886. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22887. "Convert region from BEG to END in org-mode buffer to HTML.
  22888. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22889. contents, and only produce the region of converted text, useful for
  22890. cut-and-paste operations.
  22891. If BUFFER is a buffer or a string, use/create that buffer as a target
  22892. of the converted HTML. If BUFFER is the symbol `string', return the
  22893. produced HTML as a string and leave not buffer behind. For example,
  22894. a Lisp program could call this function in the following way:
  22895. (setq html (org-export-region-as-html beg end t 'string))
  22896. When called interactively, the output buffer is selected, and shown
  22897. in a window. A non-interactive call will only retunr the buffer."
  22898. (interactive "r\nP")
  22899. (when (interactive-p)
  22900. (setq buffer "*Org HTML Export*"))
  22901. (let ((transient-mark-mode t) (zmacs-regions t)
  22902. rtn)
  22903. (goto-char end)
  22904. (set-mark (point)) ;; to activate the region
  22905. (goto-char beg)
  22906. (setq rtn (org-export-as-html
  22907. nil nil nil
  22908. buffer body-only))
  22909. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22910. (if (and (interactive-p) (bufferp rtn))
  22911. (switch-to-buffer-other-window rtn)
  22912. rtn)))
  22913. (defvar html-table-tag nil) ; dynamically scoped into this.
  22914. (defun org-export-as-html (arg &optional hidden ext-plist
  22915. to-buffer body-only)
  22916. "Export the outline as a pretty HTML file.
  22917. If there is an active region, export only the region. The prefix
  22918. ARG specifies how many levels of the outline should become
  22919. headlines. The default is 3. Lower levels will become bulleted
  22920. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22921. EXT-PLIST is a property list with external parameters overriding
  22922. org-mode's default settings, but still inferior to file-local
  22923. settings. When TO-BUFFER is non-nil, create a buffer with that
  22924. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22925. don't leave any buffer behind but just return the resulting HTML as
  22926. a string. When BODY-ONLY is set, don't produce the file header and footer,
  22927. simply return the content of <body>...</body>, without even
  22928. the body tags themselves."
  22929. (interactive "P")
  22930. ;; Make sure we have a file name when we need it.
  22931. (when (and (not (or to-buffer body-only))
  22932. (not buffer-file-name))
  22933. (if (buffer-base-buffer)
  22934. (org-set-local 'buffer-file-name
  22935. (with-current-buffer (buffer-base-buffer)
  22936. buffer-file-name))
  22937. (error "Need a file name to be able to export.")))
  22938. (message "Exporting...")
  22939. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22940. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  22941. (setq-default org-done-keywords org-done-keywords)
  22942. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  22943. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22944. ext-plist
  22945. (org-infile-export-plist)))
  22946. (style (plist-get opt-plist :style))
  22947. (link-validate (plist-get opt-plist :link-validation-function))
  22948. valid thetoc have-headings first-heading-pos
  22949. (odd org-odd-levels-only)
  22950. (region-p (org-region-active-p))
  22951. (subtree-p
  22952. (when region-p
  22953. (save-excursion
  22954. (goto-char (region-beginning))
  22955. (and (org-at-heading-p)
  22956. (>= (org-end-of-subtree t t) (region-end))))))
  22957. ;; The following two are dynamically scoped into other
  22958. ;; routines below.
  22959. (org-current-export-dir (org-export-directory :html opt-plist))
  22960. (org-current-export-file buffer-file-name)
  22961. (level 0) (line "") (origline "") txt todo
  22962. (umax nil)
  22963. (umax-toc nil)
  22964. (filename (if to-buffer nil
  22965. (expand-file-name
  22966. (concat
  22967. (file-name-sans-extension
  22968. (or (and subtree-p
  22969. (org-entry-get (region-beginning)
  22970. "EXPORT_FILE_NAME" t))
  22971. (file-name-nondirectory buffer-file-name)))
  22972. "." org-export-html-extension)
  22973. (file-name-as-directory
  22974. (org-export-directory :html opt-plist)))))
  22975. (current-dir (if buffer-file-name
  22976. (file-name-directory buffer-file-name)
  22977. default-directory))
  22978. (buffer (if to-buffer
  22979. (cond
  22980. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  22981. (t (get-buffer-create to-buffer)))
  22982. (find-file-noselect filename)))
  22983. (org-levels-open (make-vector org-level-max nil))
  22984. (date (plist-get opt-plist :date))
  22985. (author (plist-get opt-plist :author))
  22986. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22987. (plist-get opt-plist :title)
  22988. (and (not
  22989. (plist-get opt-plist :skip-before-1st-heading))
  22990. (org-export-grab-title-from-buffer))
  22991. (and buffer-file-name
  22992. (file-name-sans-extension
  22993. (file-name-nondirectory buffer-file-name)))
  22994. "UNTITLED"))
  22995. (html-table-tag (plist-get opt-plist :html-table-tag))
  22996. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22997. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  22998. (inquote nil)
  22999. (infixed nil)
  23000. (in-local-list nil)
  23001. (local-list-num nil)
  23002. (local-list-indent nil)
  23003. (llt org-plain-list-ordered-item-terminator)
  23004. (email (plist-get opt-plist :email))
  23005. (language (plist-get opt-plist :language))
  23006. (lang-words nil)
  23007. (target-alist nil) tg
  23008. (head-count 0) cnt
  23009. (start 0)
  23010. (coding-system (and (boundp 'buffer-file-coding-system)
  23011. buffer-file-coding-system))
  23012. (coding-system-for-write (or org-export-html-coding-system
  23013. coding-system))
  23014. (save-buffer-coding-system (or org-export-html-coding-system
  23015. coding-system))
  23016. (charset (and coding-system-for-write
  23017. (fboundp 'coding-system-get)
  23018. (coding-system-get coding-system-for-write
  23019. 'mime-charset)))
  23020. (region
  23021. (buffer-substring
  23022. (if region-p (region-beginning) (point-min))
  23023. (if region-p (region-end) (point-max))))
  23024. (lines
  23025. (org-split-string
  23026. (org-cleaned-string-for-export
  23027. region
  23028. :emph-multiline t
  23029. :for-html t
  23030. :skip-before-1st-heading
  23031. (plist-get opt-plist :skip-before-1st-heading)
  23032. :drawers (plist-get opt-plist :drawers)
  23033. :archived-trees
  23034. (plist-get opt-plist :archived-trees)
  23035. :add-text
  23036. (plist-get opt-plist :text)
  23037. :LaTeX-fragments
  23038. (plist-get opt-plist :LaTeX-fragments))
  23039. "[\r\n]"))
  23040. table-open type
  23041. table-buffer table-orig-buffer
  23042. ind start-is-num starter didclose
  23043. rpl path desc descp desc1 desc2 link
  23044. )
  23045. (let ((inhibit-read-only t))
  23046. (org-unmodified
  23047. (remove-text-properties (point-min) (point-max)
  23048. '(:org-license-to-kill t))))
  23049. (message "Exporting...")
  23050. (setq org-min-level (org-get-min-level lines))
  23051. (setq org-last-level org-min-level)
  23052. (org-init-section-numbers)
  23053. (cond
  23054. ((and date (string-match "%" date))
  23055. (setq date (format-time-string date (current-time))))
  23056. (date)
  23057. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23058. ;; Get the language-dependent settings
  23059. (setq lang-words (or (assoc language org-export-language-setup)
  23060. (assoc "en" org-export-language-setup)))
  23061. ;; Switch to the output buffer
  23062. (set-buffer buffer)
  23063. (let ((inhibit-read-only t)) (erase-buffer))
  23064. (fundamental-mode)
  23065. (and (fboundp 'set-buffer-file-coding-system)
  23066. (set-buffer-file-coding-system coding-system-for-write))
  23067. (let ((case-fold-search nil)
  23068. (org-odd-levels-only odd))
  23069. ;; create local variables for all options, to make sure all called
  23070. ;; functions get the correct information
  23071. (mapc (lambda (x)
  23072. (set (make-local-variable (cdr x))
  23073. (plist-get opt-plist (car x))))
  23074. org-export-plist-vars)
  23075. (setq umax (if arg (prefix-numeric-value arg)
  23076. org-export-headline-levels))
  23077. (setq umax-toc (if (integerp org-export-with-toc)
  23078. (min org-export-with-toc umax)
  23079. umax))
  23080. (unless body-only
  23081. ;; File header
  23082. (insert (format
  23083. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23084. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23085. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23086. lang=\"%s\" xml:lang=\"%s\">
  23087. <head>
  23088. <title>%s</title>
  23089. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23090. <meta name=\"generator\" content=\"Org-mode\"/>
  23091. <meta name=\"generated\" content=\"%s\"/>
  23092. <meta name=\"author\" content=\"%s\"/>
  23093. %s
  23094. </head><body>
  23095. "
  23096. language language (org-html-expand title)
  23097. (or charset "iso-8859-1") date author style))
  23098. (insert (or (plist-get opt-plist :preamble) ""))
  23099. (when (plist-get opt-plist :auto-preamble)
  23100. (if title (insert (format org-export-html-title-format
  23101. (org-html-expand title))))))
  23102. (if (and org-export-with-toc (not body-only))
  23103. (progn
  23104. (push (format "<h%d>%s</h%d>\n"
  23105. org-export-html-toplevel-hlevel
  23106. (nth 3 lang-words)
  23107. org-export-html-toplevel-hlevel)
  23108. thetoc)
  23109. (push "<ul>\n<li>" thetoc)
  23110. (setq lines
  23111. (mapcar '(lambda (line)
  23112. (if (string-match org-todo-line-regexp line)
  23113. ;; This is a headline
  23114. (progn
  23115. (setq have-headings t)
  23116. (setq level (- (match-end 1) (match-beginning 1))
  23117. level (org-tr-level level)
  23118. txt (save-match-data
  23119. (org-html-expand
  23120. (org-export-cleanup-toc-line
  23121. (match-string 3 line))))
  23122. todo
  23123. (or (and org-export-mark-todo-in-toc
  23124. (match-beginning 2)
  23125. (not (member (match-string 2 line)
  23126. org-done-keywords)))
  23127. ; TODO, not DONE
  23128. (and org-export-mark-todo-in-toc
  23129. (= level umax-toc)
  23130. (org-search-todo-below
  23131. line lines level))))
  23132. (if (string-match
  23133. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23134. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23135. (if (string-match quote-re0 txt)
  23136. (setq txt (replace-match "" t t txt)))
  23137. (if org-export-with-section-numbers
  23138. (setq txt (concat (org-section-number level)
  23139. " " txt)))
  23140. (if (<= level (max umax umax-toc))
  23141. (setq head-count (+ head-count 1)))
  23142. (if (<= level umax-toc)
  23143. (progn
  23144. (if (> level org-last-level)
  23145. (progn
  23146. (setq cnt (- level org-last-level))
  23147. (while (>= (setq cnt (1- cnt)) 0)
  23148. (push "\n<ul>\n<li>" thetoc))
  23149. (push "\n" thetoc)))
  23150. (if (< level org-last-level)
  23151. (progn
  23152. (setq cnt (- org-last-level level))
  23153. (while (>= (setq cnt (1- cnt)) 0)
  23154. (push "</li>\n</ul>" thetoc))
  23155. (push "\n" thetoc)))
  23156. ;; Check for targets
  23157. (while (string-match org-target-regexp line)
  23158. (setq tg (match-string 1 line)
  23159. line (replace-match
  23160. (concat "@<span class=\"target\">" tg "@</span> ")
  23161. t t line))
  23162. (push (cons (org-solidify-link-text tg)
  23163. (format "sec-%d" head-count))
  23164. target-alist))
  23165. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23166. (setq txt (replace-match "" t t txt)))
  23167. (push
  23168. (format
  23169. (if todo
  23170. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23171. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23172. head-count txt) thetoc)
  23173. (setq org-last-level level))
  23174. )))
  23175. line)
  23176. lines))
  23177. (while (> org-last-level (1- org-min-level))
  23178. (setq org-last-level (1- org-last-level))
  23179. (push "</li>\n</ul>\n" thetoc))
  23180. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23181. (setq head-count 0)
  23182. (org-init-section-numbers)
  23183. (while (setq line (pop lines) origline line)
  23184. (catch 'nextline
  23185. ;; end of quote section?
  23186. (when (and inquote (string-match "^\\*+ " line))
  23187. (insert "</pre>\n")
  23188. (setq inquote nil))
  23189. ;; inside a quote section?
  23190. (when inquote
  23191. (insert (org-html-protect line) "\n")
  23192. (throw 'nextline nil))
  23193. ;; verbatim lines
  23194. (when (and org-export-with-fixed-width
  23195. (string-match "^[ \t]*:\\(.*\\)" line))
  23196. (when (not infixed)
  23197. (setq infixed t)
  23198. (insert "<pre>\n"))
  23199. (insert (org-html-protect (match-string 1 line)) "\n")
  23200. (when (and lines
  23201. (not (string-match "^[ \t]*\\(:.*\\)"
  23202. (car lines))))
  23203. (setq infixed nil)
  23204. (insert "</pre>\n"))
  23205. (throw 'nextline nil))
  23206. ;; Protected HTML
  23207. (when (get-text-property 0 'org-protected line)
  23208. (let (par)
  23209. (when (re-search-backward
  23210. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23211. (setq par (match-string 1))
  23212. (replace-match "\\2\n"))
  23213. (insert line "\n")
  23214. (while (and lines
  23215. (or (= (length (car lines)) 0)
  23216. (get-text-property 0 'org-protected (car lines))))
  23217. (insert (pop lines) "\n"))
  23218. (and par (insert "<p>\n")))
  23219. (throw 'nextline nil))
  23220. ;; Horizontal line
  23221. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23222. (insert "\n<hr/>\n")
  23223. (throw 'nextline nil))
  23224. ;; make targets to anchors
  23225. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23226. (cond
  23227. ((match-end 2)
  23228. (setq line (replace-match
  23229. (concat "@<a name=\""
  23230. (org-solidify-link-text (match-string 1 line))
  23231. "\">\\nbsp@</a>")
  23232. t t line)))
  23233. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23234. (setq line (replace-match
  23235. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23236. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23237. t t line)))
  23238. (t
  23239. (setq line (replace-match
  23240. (concat "@<a name=\""
  23241. (org-solidify-link-text (match-string 1 line))
  23242. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23243. t t line)))))
  23244. (setq line (org-html-handle-time-stamps line))
  23245. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23246. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23247. ;; Also handle sub_superscripts and checkboxes
  23248. (or (string-match org-table-hline-regexp line)
  23249. (setq line (org-html-expand line)))
  23250. ;; Format the links
  23251. (setq start 0)
  23252. (while (string-match org-bracket-link-analytic-regexp line start)
  23253. (setq start (match-beginning 0))
  23254. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23255. (setq path (match-string 3 line))
  23256. (setq desc1 (if (match-end 5) (match-string 5 line))
  23257. desc2 (if (match-end 2) (concat type ":" path) path)
  23258. descp (and desc1 (not (equal desc1 desc2)))
  23259. desc (or desc1 desc2))
  23260. ;; Make an image out of the description if that is so wanted
  23261. (when (and descp (org-file-image-p desc))
  23262. (save-match-data
  23263. (if (string-match "^file:" desc)
  23264. (setq desc (substring desc (match-end 0)))))
  23265. (setq desc (concat "<img src=\"" desc "\"/>")))
  23266. ;; FIXME: do we need to unescape here somewhere?
  23267. (cond
  23268. ((equal type "internal")
  23269. (setq rpl
  23270. (concat
  23271. "<a href=\"#"
  23272. (org-solidify-link-text
  23273. (save-match-data (org-link-unescape path)) target-alist)
  23274. "\">" desc "</a>")))
  23275. ((member type '("http" "https"))
  23276. ;; standard URL, just check if we need to inline an image
  23277. (if (and (or (eq t org-export-html-inline-images)
  23278. (and org-export-html-inline-images (not descp)))
  23279. (org-file-image-p path))
  23280. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23281. (setq link (concat type ":" path))
  23282. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23283. ((member type '("ftp" "mailto" "news"))
  23284. ;; standard URL
  23285. (setq link (concat type ":" path))
  23286. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23287. ((string= type "file")
  23288. ;; FILE link
  23289. (let* ((filename path)
  23290. (abs-p (file-name-absolute-p filename))
  23291. thefile file-is-image-p search)
  23292. (save-match-data
  23293. (if (string-match "::\\(.*\\)" filename)
  23294. (setq search (match-string 1 filename)
  23295. filename (replace-match "" t nil filename)))
  23296. (setq valid
  23297. (if (functionp link-validate)
  23298. (funcall link-validate filename current-dir)
  23299. t))
  23300. (setq file-is-image-p (org-file-image-p filename))
  23301. (setq thefile (if abs-p (expand-file-name filename) filename))
  23302. (when (and org-export-html-link-org-files-as-html
  23303. (string-match "\\.org$" thefile))
  23304. (setq thefile (concat (substring thefile 0
  23305. (match-beginning 0))
  23306. "." org-export-html-extension))
  23307. (if (and search
  23308. ;; make sure this is can be used as target search
  23309. (not (string-match "^[0-9]*$" search))
  23310. (not (string-match "^\\*" search))
  23311. (not (string-match "^/.*/$" search)))
  23312. (setq thefile (concat thefile "#"
  23313. (org-solidify-link-text
  23314. (org-link-unescape search)))))
  23315. (when (string-match "^file:" desc)
  23316. (setq desc (replace-match "" t t desc))
  23317. (if (string-match "\\.org$" desc)
  23318. (setq desc (replace-match "" t t desc))))))
  23319. (setq rpl (if (and file-is-image-p
  23320. (or (eq t org-export-html-inline-images)
  23321. (and org-export-html-inline-images
  23322. (not descp))))
  23323. (concat "<img src=\"" thefile "\"/>")
  23324. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23325. (if (not valid) (setq rpl desc))))
  23326. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23327. (setq rpl (concat "<i>&lt;" type ":"
  23328. (save-match-data (org-link-unescape path))
  23329. "&gt;</i>"))))
  23330. (setq line (replace-match rpl t t line)
  23331. start (+ start (length rpl))))
  23332. ;; TODO items
  23333. (if (and (string-match org-todo-line-regexp line)
  23334. (match-beginning 2))
  23335. (setq line
  23336. (concat (substring line 0 (match-beginning 2))
  23337. "<span class=\""
  23338. (if (member (match-string 2 line)
  23339. org-done-keywords)
  23340. "done" "todo")
  23341. "\">" (match-string 2 line)
  23342. "</span>" (substring line (match-end 2)))))
  23343. ;; Does this contain a reference to a footnote?
  23344. (when org-export-with-footnotes
  23345. (setq start 0)
  23346. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23347. (if (get-text-property (match-beginning 2) 'org-protected line)
  23348. (setq start (match-end 2))
  23349. (let ((n (match-string 2 line)))
  23350. (setq line
  23351. (replace-match
  23352. (format
  23353. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23354. (match-string 1 line) n n n)
  23355. t t line))))))
  23356. (cond
  23357. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23358. ;; This is a headline
  23359. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23360. txt (match-string 2 line))
  23361. (if (string-match quote-re0 txt)
  23362. (setq txt (replace-match "" t t txt)))
  23363. (if (<= level (max umax umax-toc))
  23364. (setq head-count (+ head-count 1)))
  23365. (when in-local-list
  23366. ;; Close any local lists before inserting a new header line
  23367. (while local-list-num
  23368. (org-close-li)
  23369. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23370. (pop local-list-num))
  23371. (setq local-list-indent nil
  23372. in-local-list nil))
  23373. (setq first-heading-pos (or first-heading-pos (point)))
  23374. (org-html-level-start level txt umax
  23375. (and org-export-with-toc (<= level umax))
  23376. head-count)
  23377. ;; QUOTES
  23378. (when (string-match quote-re line)
  23379. (insert "<pre>")
  23380. (setq inquote t)))
  23381. ((and org-export-with-tables
  23382. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23383. (if (not table-open)
  23384. ;; New table starts
  23385. (setq table-open t table-buffer nil table-orig-buffer nil))
  23386. ;; Accumulate lines
  23387. (setq table-buffer (cons line table-buffer)
  23388. table-orig-buffer (cons origline table-orig-buffer))
  23389. (when (or (not lines)
  23390. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23391. (car lines))))
  23392. (setq table-open nil
  23393. table-buffer (nreverse table-buffer)
  23394. table-orig-buffer (nreverse table-orig-buffer))
  23395. (org-close-par-maybe)
  23396. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23397. (t
  23398. ;; Normal lines
  23399. (when (string-match
  23400. (cond
  23401. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23402. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23403. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23404. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23405. line)
  23406. (setq ind (org-get-string-indentation line)
  23407. start-is-num (match-beginning 4)
  23408. starter (if (match-beginning 2)
  23409. (substring (match-string 2 line) 0 -1))
  23410. line (substring line (match-beginning 5)))
  23411. (unless (string-match "[^ \t]" line)
  23412. ;; empty line. Pretend indentation is large.
  23413. (setq ind (if org-empty-line-terminates-plain-lists
  23414. 0
  23415. (1+ (or (car local-list-indent) 1)))))
  23416. (setq didclose nil)
  23417. (while (and in-local-list
  23418. (or (and (= ind (car local-list-indent))
  23419. (not starter))
  23420. (< ind (car local-list-indent))))
  23421. (setq didclose t)
  23422. (org-close-li)
  23423. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23424. (pop local-list-num) (pop local-list-indent)
  23425. (setq in-local-list local-list-indent))
  23426. (cond
  23427. ((and starter
  23428. (or (not in-local-list)
  23429. (> ind (car local-list-indent))))
  23430. ;; Start new (level of) list
  23431. (org-close-par-maybe)
  23432. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23433. (push start-is-num local-list-num)
  23434. (push ind local-list-indent)
  23435. (setq in-local-list t))
  23436. (starter
  23437. ;; continue current list
  23438. (org-close-li)
  23439. (insert "<li>\n"))
  23440. (didclose
  23441. ;; we did close a list, normal text follows: need <p>
  23442. (org-open-par)))
  23443. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23444. (setq line
  23445. (replace-match
  23446. (if (equal (match-string 1 line) "X")
  23447. "<b>[X]</b>"
  23448. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23449. t t line))))
  23450. ;; Empty lines start a new paragraph. If hand-formatted lists
  23451. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23452. ;; also start a new paragraph.
  23453. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23454. ;; Is this the start of a footnote?
  23455. (when org-export-with-footnotes
  23456. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23457. (org-close-par-maybe)
  23458. (let ((n (match-string 1 line)))
  23459. (setq line (replace-match
  23460. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23461. ;; Check if the line break needs to be conserved
  23462. (cond
  23463. ((string-match "\\\\\\\\[ \t]*$" line)
  23464. (setq line (replace-match "<br/>" t t line)))
  23465. (org-export-preserve-breaks
  23466. (setq line (concat line "<br/>"))))
  23467. (insert line "\n")))))
  23468. ;; Properly close all local lists and other lists
  23469. (when inquote (insert "</pre>\n"))
  23470. (when in-local-list
  23471. ;; Close any local lists before inserting a new header line
  23472. (while local-list-num
  23473. (org-close-li)
  23474. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23475. (pop local-list-num))
  23476. (setq local-list-indent nil
  23477. in-local-list nil))
  23478. (org-html-level-start 1 nil umax
  23479. (and org-export-with-toc (<= level umax))
  23480. head-count)
  23481. (unless body-only
  23482. (when (plist-get opt-plist :auto-postamble)
  23483. (insert "<div id=\"postamble\">")
  23484. (when (and org-export-author-info author)
  23485. (insert "<p class=\"author\"> "
  23486. (nth 1 lang-words) ": " author "\n")
  23487. (when email
  23488. (if (listp (split-string email ",+ *"))
  23489. (mapc (lambda(e)
  23490. (insert "<a href=\"mailto:" e "\">&lt;"
  23491. e "&gt;</a>\n"))
  23492. (split-string email ",+ *"))
  23493. (insert "<a href=\"mailto:" email "\">&lt;"
  23494. email "&gt;</a>\n")))
  23495. (insert "</p>\n"))
  23496. (when (and date org-export-time-stamp-file)
  23497. (insert "<p class=\"date\"> "
  23498. (nth 2 lang-words) ": "
  23499. date "</p>\n"))
  23500. (insert "</div>"))
  23501. (if org-export-html-with-timestamp
  23502. (insert org-export-html-html-helper-timestamp))
  23503. (insert (or (plist-get opt-plist :postamble) ""))
  23504. (insert "</body>\n</html>\n"))
  23505. (normal-mode)
  23506. (if (eq major-mode default-major-mode) (html-mode))
  23507. ;; insert the table of contents
  23508. (goto-char (point-min))
  23509. (when thetoc
  23510. (if (or (re-search-forward
  23511. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23512. (re-search-forward
  23513. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23514. (progn
  23515. (goto-char (match-beginning 0))
  23516. (replace-match ""))
  23517. (goto-char first-heading-pos)
  23518. (when (looking-at "\\s-*</p>")
  23519. (goto-char (match-end 0))
  23520. (insert "\n")))
  23521. (insert "<div id=\"table-of-contents\">\n")
  23522. (mapc 'insert thetoc)
  23523. (insert "</div>\n"))
  23524. ;; remove empty paragraphs and lists
  23525. (goto-char (point-min))
  23526. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23527. (replace-match ""))
  23528. (goto-char (point-min))
  23529. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23530. (replace-match ""))
  23531. ;; Convert whitespace place holders
  23532. (goto-char (point-min))
  23533. (let (beg end n)
  23534. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23535. (setq n (get-text-property beg 'org-whitespace)
  23536. end (next-single-property-change beg 'org-whitespace))
  23537. (goto-char beg)
  23538. (delete-region beg end)
  23539. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23540. (make-string n ?x)))))
  23541. (or to-buffer (save-buffer))
  23542. (goto-char (point-min))
  23543. (message "Exporting... done")
  23544. (if (eq to-buffer 'string)
  23545. (prog1 (buffer-substring (point-min) (point-max))
  23546. (kill-buffer (current-buffer)))
  23547. (current-buffer)))))
  23548. (defvar org-table-colgroup-info nil)
  23549. (defun org-format-table-ascii (lines)
  23550. "Format a table for ascii export."
  23551. (if (stringp lines)
  23552. (setq lines (org-split-string lines "\n")))
  23553. (if (not (string-match "^[ \t]*|" (car lines)))
  23554. ;; Table made by table.el - test for spanning
  23555. lines
  23556. ;; A normal org table
  23557. ;; Get rid of hlines at beginning and end
  23558. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23559. (setq lines (nreverse lines))
  23560. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23561. (setq lines (nreverse lines))
  23562. (when org-export-table-remove-special-lines
  23563. ;; Check if the table has a marking column. If yes remove the
  23564. ;; column and the special lines
  23565. (setq lines (org-table-clean-before-export lines)))
  23566. ;; Get rid of the vertical lines except for grouping
  23567. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23568. rtn line vl1 start)
  23569. (while (setq line (pop lines))
  23570. (if (string-match org-table-hline-regexp line)
  23571. (and (string-match "|\\(.*\\)|" line)
  23572. (setq line (replace-match " \\1" t nil line)))
  23573. (setq start 0 vl1 vl)
  23574. (while (string-match "|" line start)
  23575. (setq start (match-end 0))
  23576. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23577. (push line rtn))
  23578. (nreverse rtn))))
  23579. (defun org-colgroup-info-to-vline-list (info)
  23580. (let (vl new last)
  23581. (while info
  23582. (setq last new new (pop info))
  23583. (if (or (memq last '(:end :startend))
  23584. (memq new '(:start :startend)))
  23585. (push t vl)
  23586. (push nil vl)))
  23587. (setq vl (nreverse vl))
  23588. (and vl (setcar vl nil))
  23589. vl))
  23590. (defun org-format-table-html (lines olines)
  23591. "Find out which HTML converter to use and return the HTML code."
  23592. (if (stringp lines)
  23593. (setq lines (org-split-string lines "\n")))
  23594. (if (string-match "^[ \t]*|" (car lines))
  23595. ;; A normal org table
  23596. (org-format-org-table-html lines)
  23597. ;; Table made by table.el - test for spanning
  23598. (let* ((hlines (delq nil (mapcar
  23599. (lambda (x)
  23600. (if (string-match "^[ \t]*\\+-" x) x
  23601. nil))
  23602. lines)))
  23603. (first (car hlines))
  23604. (ll (and (string-match "\\S-+" first)
  23605. (match-string 0 first)))
  23606. (re (concat "^[ \t]*" (regexp-quote ll)))
  23607. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23608. hlines))))
  23609. (if (and (not spanning)
  23610. (not org-export-prefer-native-exporter-for-tables))
  23611. ;; We can use my own converter with HTML conversions
  23612. (org-format-table-table-html lines)
  23613. ;; Need to use the code generator in table.el, with the original text.
  23614. (org-format-table-table-html-using-table-generate-source olines)))))
  23615. (defun org-format-org-table-html (lines &optional splice)
  23616. "Format a table into HTML."
  23617. ;; Get rid of hlines at beginning and end
  23618. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23619. (setq lines (nreverse lines))
  23620. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23621. (setq lines (nreverse lines))
  23622. (when org-export-table-remove-special-lines
  23623. ;; Check if the table has a marking column. If yes remove the
  23624. ;; column and the special lines
  23625. (setq lines (org-table-clean-before-export lines)))
  23626. (let ((head (and org-export-highlight-first-table-line
  23627. (delq nil (mapcar
  23628. (lambda (x) (string-match "^[ \t]*|-" x))
  23629. (cdr lines)))))
  23630. (nlines 0) fnum i
  23631. tbopen line fields html gr colgropen)
  23632. (if splice (setq head nil))
  23633. (unless splice (push (if head "<thead>" "<tbody>") html))
  23634. (setq tbopen t)
  23635. (while (setq line (pop lines))
  23636. (catch 'next-line
  23637. (if (string-match "^[ \t]*|-" line)
  23638. (progn
  23639. (unless splice
  23640. (push (if head "</thead>" "</tbody>") html)
  23641. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23642. (setq head nil) ;; head ends here, first time around
  23643. ;; ignore this line
  23644. (throw 'next-line t)))
  23645. ;; Break the line into fields
  23646. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23647. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23648. (setq nlines (1+ nlines) i -1)
  23649. (push (concat "<tr>"
  23650. (mapconcat
  23651. (lambda (x)
  23652. (setq i (1+ i))
  23653. (if (and (< i nlines)
  23654. (string-match org-table-number-regexp x))
  23655. (incf (aref fnum i)))
  23656. (if head
  23657. (concat (car org-export-table-header-tags) x
  23658. (cdr org-export-table-header-tags))
  23659. (concat (car org-export-table-data-tags) x
  23660. (cdr org-export-table-data-tags))))
  23661. fields "")
  23662. "</tr>")
  23663. html)))
  23664. (unless splice (if tbopen (push "</tbody>" html)))
  23665. (unless splice (push "</table>\n" html))
  23666. (setq html (nreverse html))
  23667. (unless splice
  23668. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23669. (push (mapconcat
  23670. (lambda (x)
  23671. (setq gr (pop org-table-colgroup-info))
  23672. (format "%s<col align=\"%s\"></col>%s"
  23673. (if (memq gr '(:start :startend))
  23674. (prog1
  23675. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23676. (setq colgropen t))
  23677. "")
  23678. (if (> (/ (float x) nlines) org-table-number-fraction)
  23679. "right" "left")
  23680. (if (memq gr '(:end :startend))
  23681. (progn (setq colgropen nil) "</colgroup>")
  23682. "")))
  23683. fnum "")
  23684. html)
  23685. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23686. (push html-table-tag html))
  23687. (concat (mapconcat 'identity html "\n") "\n")))
  23688. (defun org-table-clean-before-export (lines)
  23689. "Check if the table has a marking column.
  23690. If yes remove the column and the special lines."
  23691. (setq org-table-colgroup-info nil)
  23692. (if (memq nil
  23693. (mapcar
  23694. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23695. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23696. lines))
  23697. (progn
  23698. (setq org-table-clean-did-remove-column nil)
  23699. (delq nil
  23700. (mapcar
  23701. (lambda (x)
  23702. (cond
  23703. ((string-match "^[ \t]*| */ *|" x)
  23704. (setq org-table-colgroup-info
  23705. (mapcar (lambda (x)
  23706. (cond ((member x '("<" "&lt;")) :start)
  23707. ((member x '(">" "&gt;")) :end)
  23708. ((member x '("<>" "&lt;&gt;")) :startend)
  23709. (t nil)))
  23710. (org-split-string x "[ \t]*|[ \t]*")))
  23711. nil)
  23712. (t x)))
  23713. lines)))
  23714. (setq org-table-clean-did-remove-column t)
  23715. (delq nil
  23716. (mapcar
  23717. (lambda (x)
  23718. (cond
  23719. ((string-match "^[ \t]*| */ *|" x)
  23720. (setq org-table-colgroup-info
  23721. (mapcar (lambda (x)
  23722. (cond ((member x '("<" "&lt;")) :start)
  23723. ((member x '(">" "&gt;")) :end)
  23724. ((member x '("<>" "&lt;&gt;")) :startend)
  23725. (t nil)))
  23726. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23727. nil)
  23728. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23729. nil) ; ignore this line
  23730. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23731. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23732. ;; remove the first column
  23733. (replace-match "\\1|" t nil x))))
  23734. lines))))
  23735. (defun org-format-table-table-html (lines)
  23736. "Format a table generated by table.el into HTML.
  23737. This conversion does *not* use `table-generate-source' from table.el.
  23738. This has the advantage that Org-mode's HTML conversions can be used.
  23739. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23740. (let (line field-buffer
  23741. (head org-export-highlight-first-table-line)
  23742. fields html empty)
  23743. (setq html (concat html-table-tag "\n"))
  23744. (while (setq line (pop lines))
  23745. (setq empty "&nbsp;")
  23746. (catch 'next-line
  23747. (if (string-match "^[ \t]*\\+-" line)
  23748. (progn
  23749. (if field-buffer
  23750. (progn
  23751. (setq
  23752. html
  23753. (concat
  23754. html
  23755. "<tr>"
  23756. (mapconcat
  23757. (lambda (x)
  23758. (if (equal x "") (setq x empty))
  23759. (if head
  23760. (concat (car org-export-table-header-tags) x
  23761. (cdr org-export-table-header-tags))
  23762. (concat (car org-export-table-data-tags) x
  23763. (cdr org-export-table-data-tags))))
  23764. field-buffer "\n")
  23765. "</tr>\n"))
  23766. (setq head nil)
  23767. (setq field-buffer nil)))
  23768. ;; Ignore this line
  23769. (throw 'next-line t)))
  23770. ;; Break the line into fields and store the fields
  23771. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23772. (if field-buffer
  23773. (setq field-buffer (mapcar
  23774. (lambda (x)
  23775. (concat x "<br/>" (pop fields)))
  23776. field-buffer))
  23777. (setq field-buffer fields))))
  23778. (setq html (concat html "</table>\n"))
  23779. html))
  23780. (defun org-format-table-table-html-using-table-generate-source (lines)
  23781. "Format a table into html, using `table-generate-source' from table.el.
  23782. This has the advantage that cell- or row-spanning is allowed.
  23783. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23784. (require 'table)
  23785. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23786. (erase-buffer)
  23787. (insert (mapconcat 'identity lines "\n"))
  23788. (goto-char (point-min))
  23789. (if (not (re-search-forward "|[^+]" nil t))
  23790. (error "Error processing table"))
  23791. (table-recognize-table)
  23792. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23793. (table-generate-source 'html " org-tmp2 ")
  23794. (set-buffer " org-tmp2 ")
  23795. (buffer-substring (point-min) (point-max))))
  23796. (defun org-html-handle-time-stamps (s)
  23797. "Format time stamps in string S, or remove them."
  23798. (catch 'exit
  23799. (let (r b)
  23800. (while (string-match org-maybe-keyword-time-regexp s)
  23801. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23802. ;; never export CLOCK
  23803. (throw 'exit ""))
  23804. (or b (setq b (substring s 0 (match-beginning 0))))
  23805. (if (not org-export-with-timestamps)
  23806. (setq r (concat r (substring s 0 (match-beginning 0)))
  23807. s (substring s (match-end 0)))
  23808. (setq r (concat
  23809. r (substring s 0 (match-beginning 0))
  23810. (if (match-end 1)
  23811. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23812. (match-string 1 s)))
  23813. (format " @<span class=\"timestamp\">%s@</span>"
  23814. (substring
  23815. (org-translate-time (match-string 3 s)) 1 -1)))
  23816. s (substring s (match-end 0)))))
  23817. ;; Line break if line started and ended with time stamp stuff
  23818. (if (not r)
  23819. s
  23820. (setq r (concat r s))
  23821. (unless (string-match "\\S-" (concat b s))
  23822. (setq r (concat r "@<br/>")))
  23823. r))))
  23824. (defun org-html-protect (s)
  23825. ;; convert & to &amp;, < to &lt; and > to &gt;
  23826. (let ((start 0))
  23827. (while (string-match "&" s start)
  23828. (setq s (replace-match "&amp;" t t s)
  23829. start (1+ (match-beginning 0))))
  23830. (while (string-match "<" s)
  23831. (setq s (replace-match "&lt;" t t s)))
  23832. (while (string-match ">" s)
  23833. (setq s (replace-match "&gt;" t t s))))
  23834. s)
  23835. (defun org-export-cleanup-toc-line (s)
  23836. "Remove tags and time staps from lines going into the toc."
  23837. (when (memq org-export-with-tags '(not-in-toc nil))
  23838. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23839. (setq s (replace-match "" t t s))))
  23840. (when org-export-remove-timestamps-from-toc
  23841. (while (string-match org-maybe-keyword-time-regexp s)
  23842. (setq s (replace-match "" t t s))))
  23843. (while (string-match org-bracket-link-regexp s)
  23844. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23845. t t s)))
  23846. s)
  23847. (defun org-html-expand (string)
  23848. "Prepare STRING for HTML export. Applies all active conversions.
  23849. If there are links in the string, don't modify these."
  23850. (let* ((re (concat org-bracket-link-regexp "\\|"
  23851. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23852. m s l res)
  23853. (while (setq m (string-match re string))
  23854. (setq s (substring string 0 m)
  23855. l (match-string 0 string)
  23856. string (substring string (match-end 0)))
  23857. (push (org-html-do-expand s) res)
  23858. (push l res))
  23859. (push (org-html-do-expand string) res)
  23860. (apply 'concat (nreverse res))))
  23861. (defun org-html-do-expand (s)
  23862. "Apply all active conversions to translate special ASCII to HTML."
  23863. (setq s (org-html-protect s))
  23864. (if org-export-html-expand
  23865. (let ((start 0))
  23866. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23867. (setq s (replace-match "<\\1>" t nil s)))))
  23868. (if org-export-with-emphasize
  23869. (setq s (org-export-html-convert-emphasize s)))
  23870. (if org-export-with-special-strings
  23871. (setq s (org-export-html-convert-special-strings s)))
  23872. (if org-export-with-sub-superscripts
  23873. (setq s (org-export-html-convert-sub-super s)))
  23874. (if org-export-with-TeX-macros
  23875. (let ((start 0) wd ass)
  23876. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23877. (if (get-text-property (match-beginning 0) 'org-protected s)
  23878. (setq start (match-end 0))
  23879. (setq wd (match-string 1 s))
  23880. (if (setq ass (assoc wd org-html-entities))
  23881. (setq s (replace-match (or (cdr ass)
  23882. (concat "&" (car ass) ";"))
  23883. t t s))
  23884. (setq start (+ start (length wd))))))))
  23885. s)
  23886. (defun org-create-multibrace-regexp (left right n)
  23887. "Create a regular expression which will match a balanced sexp.
  23888. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23889. as single character strings.
  23890. The regexp returned will match the entire expression including the
  23891. delimiters. It will also define a single group which contains the
  23892. match except for the outermost delimiters. The maximum depth of
  23893. stacked delimiters is N. Escaping delimiters is not possible."
  23894. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23895. (or "\\|")
  23896. (re nothing)
  23897. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23898. (while (> n 1)
  23899. (setq n (1- n)
  23900. re (concat re or next)
  23901. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23902. (concat left "\\(" re "\\)" right)))
  23903. (defvar org-match-substring-regexp
  23904. (concat
  23905. "\\([^\\]\\)\\([_^]\\)\\("
  23906. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23907. "\\|"
  23908. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23909. "\\|"
  23910. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23911. "The regular expression matching a sub- or superscript.")
  23912. (defvar org-match-substring-with-braces-regexp
  23913. (concat
  23914. "\\([^\\]\\)\\([_^]\\)\\("
  23915. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23916. "\\)")
  23917. "The regular expression matching a sub- or superscript, forcing braces.")
  23918. (defconst org-export-html-special-string-regexps
  23919. '(("\\\\-" . "&shy;")
  23920. ("---\\([^-]\\)" . "&mdash;\\1")
  23921. ("--\\([^-]\\)" . "&ndash;\\1")
  23922. ("\\.\\.\\." . "&hellip;"))
  23923. "Regular expressions for special string conversion.")
  23924. (defun org-export-html-convert-special-strings (string)
  23925. "Convert special characters in STRING to HTML."
  23926. (let ((all org-export-html-special-string-regexps)
  23927. e a re rpl start)
  23928. (while (setq a (pop all))
  23929. (setq re (car a) rpl (cdr a) start 0)
  23930. (while (string-match re string start)
  23931. (if (get-text-property (match-beginning 0) 'org-protected string)
  23932. (setq start (match-end 0))
  23933. (setq string (replace-match rpl t nil string)))))
  23934. string))
  23935. (defun org-export-html-convert-sub-super (string)
  23936. "Convert sub- and superscripts in STRING to HTML."
  23937. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  23938. (while (string-match org-match-substring-regexp string s)
  23939. (cond
  23940. ((and requireb (match-end 8)) (setq s (match-end 2)))
  23941. ((get-text-property (match-beginning 2) 'org-protected string)
  23942. (setq s (match-end 2)))
  23943. (t
  23944. (setq s (match-end 1)
  23945. key (if (string= (match-string 2 string) "_") "sub" "sup")
  23946. c (or (match-string 8 string)
  23947. (match-string 6 string)
  23948. (match-string 5 string))
  23949. string (replace-match
  23950. (concat (match-string 1 string)
  23951. "<" key ">" c "</" key ">")
  23952. t t string)))))
  23953. (while (string-match "\\\\\\([_^]\\)" string)
  23954. (setq string (replace-match (match-string 1 string) t t string)))
  23955. string))
  23956. (defun org-export-html-convert-emphasize (string)
  23957. "Apply emphasis."
  23958. (let ((s 0) rpl)
  23959. (while (string-match org-emph-re string s)
  23960. (if (not (equal
  23961. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  23962. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  23963. (setq s (match-beginning 0)
  23964. rpl
  23965. (concat
  23966. (match-string 1 string)
  23967. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  23968. (match-string 4 string)
  23969. (nth 3 (assoc (match-string 3 string)
  23970. org-emphasis-alist))
  23971. (match-string 5 string))
  23972. string (replace-match rpl t t string)
  23973. s (+ s (- (length rpl) 2)))
  23974. (setq s (1+ s))))
  23975. string))
  23976. (defvar org-par-open nil)
  23977. (defun org-open-par ()
  23978. "Insert <p>, but first close previous paragraph if any."
  23979. (org-close-par-maybe)
  23980. (insert "\n<p>")
  23981. (setq org-par-open t))
  23982. (defun org-close-par-maybe ()
  23983. "Close paragraph if there is one open."
  23984. (when org-par-open
  23985. (insert "</p>")
  23986. (setq org-par-open nil)))
  23987. (defun org-close-li ()
  23988. "Close <li> if necessary."
  23989. (org-close-par-maybe)
  23990. (insert "</li>\n"))
  23991. (defvar body-only) ; dynamically scoped into this.
  23992. (defun org-html-level-start (level title umax with-toc head-count)
  23993. "Insert a new level in HTML export.
  23994. When TITLE is nil, just close all open levels."
  23995. (org-close-par-maybe)
  23996. (let ((l org-level-max))
  23997. (while (>= l level)
  23998. (if (aref org-levels-open (1- l))
  23999. (progn
  24000. (org-html-level-close l umax)
  24001. (aset org-levels-open (1- l) nil)))
  24002. (setq l (1- l)))
  24003. (when title
  24004. ;; If title is nil, this means this function is called to close
  24005. ;; all levels, so the rest is done only if title is given
  24006. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24007. (setq title (replace-match
  24008. (if org-export-with-tags
  24009. (save-match-data
  24010. (concat
  24011. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24012. (mapconcat 'identity (org-split-string
  24013. (match-string 1 title) ":")
  24014. "&nbsp;")
  24015. "</span>"))
  24016. "")
  24017. t t title)))
  24018. (if (> level umax)
  24019. (progn
  24020. (if (aref org-levels-open (1- level))
  24021. (progn
  24022. (org-close-li)
  24023. (insert "<li>" title "<br/>\n"))
  24024. (aset org-levels-open (1- level) t)
  24025. (org-close-par-maybe)
  24026. (insert "<ul>\n<li>" title "<br/>\n")))
  24027. (aset org-levels-open (1- level) t)
  24028. (if (and org-export-with-section-numbers (not body-only))
  24029. (setq title (concat (org-section-number level) " " title)))
  24030. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24031. (if with-toc
  24032. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24033. level level head-count title level))
  24034. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24035. (org-open-par)))))
  24036. (defun org-html-level-close (level max-outline-level)
  24037. "Terminate one level in HTML export."
  24038. (if (<= level max-outline-level)
  24039. (insert "</div>\n")
  24040. (org-close-li)
  24041. (insert "</ul>\n")))
  24042. ;;; iCalendar export
  24043. ;;;###autoload
  24044. (defun org-export-icalendar-this-file ()
  24045. "Export current file as an iCalendar file.
  24046. The iCalendar file will be located in the same directory as the Org-mode
  24047. file, but with extension `.ics'."
  24048. (interactive)
  24049. (org-export-icalendar nil buffer-file-name))
  24050. ;;;###autoload
  24051. (defun org-export-icalendar-all-agenda-files ()
  24052. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24053. Each iCalendar file will be located in the same directory as the Org-mode
  24054. file, but with extension `.ics'."
  24055. (interactive)
  24056. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24057. ;;;###autoload
  24058. (defun org-export-icalendar-combine-agenda-files ()
  24059. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24060. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24061. (interactive)
  24062. (apply 'org-export-icalendar t (org-agenda-files t)))
  24063. (defun org-export-icalendar (combine &rest files)
  24064. "Create iCalendar files for all elements of FILES.
  24065. If COMBINE is non-nil, combine all calendar entries into a single large
  24066. file and store it under the name `org-combined-agenda-icalendar-file'."
  24067. (save-excursion
  24068. (org-prepare-agenda-buffers files)
  24069. (let* ((dir (org-export-directory
  24070. :ical (list :publishing-directory
  24071. org-export-publishing-directory)))
  24072. file ical-file ical-buffer category started org-agenda-new-buffers)
  24073. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24074. (when combine
  24075. (setq ical-file
  24076. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24077. org-combined-agenda-icalendar-file
  24078. (expand-file-name org-combined-agenda-icalendar-file dir))
  24079. ical-buffer (org-get-agenda-file-buffer ical-file))
  24080. (set-buffer ical-buffer) (erase-buffer))
  24081. (while (setq file (pop files))
  24082. (catch 'nextfile
  24083. (org-check-agenda-file file)
  24084. (set-buffer (org-get-agenda-file-buffer file))
  24085. (unless combine
  24086. (setq ical-file (concat (file-name-as-directory dir)
  24087. (file-name-sans-extension
  24088. (file-name-nondirectory buffer-file-name))
  24089. ".ics"))
  24090. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24091. (with-current-buffer ical-buffer (erase-buffer)))
  24092. (setq category (or org-category
  24093. (file-name-sans-extension
  24094. (file-name-nondirectory buffer-file-name))))
  24095. (if (symbolp category) (setq category (symbol-name category)))
  24096. (let ((standard-output ical-buffer))
  24097. (if combine
  24098. (and (not started) (setq started t)
  24099. (org-start-icalendar-file org-icalendar-combined-name))
  24100. (org-start-icalendar-file category))
  24101. (org-print-icalendar-entries combine)
  24102. (when (or (and combine (not files)) (not combine))
  24103. (org-finish-icalendar-file)
  24104. (set-buffer ical-buffer)
  24105. (save-buffer)
  24106. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24107. (org-release-buffers org-agenda-new-buffers))))
  24108. (defvar org-after-save-iCalendar-file-hook nil
  24109. "Hook run after an iCalendar file has been saved.
  24110. The iCalendar buffer is still current when this hook is run.
  24111. A good way to use this is to tell a desktop calenndar application to re-read
  24112. the iCalendar file.")
  24113. (defun org-print-icalendar-entries (&optional combine)
  24114. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24115. When COMBINE is non nil, add the category to each line."
  24116. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24117. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24118. (dts (org-ical-ts-to-string
  24119. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24120. "DTSTART"))
  24121. hd ts ts2 state status (inc t) pos b sexp rrule
  24122. scheduledp deadlinep tmp pri category entry location summary desc
  24123. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24124. (org-refresh-category-properties)
  24125. (save-excursion
  24126. (goto-char (point-min))
  24127. (while (re-search-forward re1 nil t)
  24128. (catch :skip
  24129. (org-agenda-skip)
  24130. (setq pos (match-beginning 0)
  24131. ts (match-string 0)
  24132. inc t
  24133. hd (org-get-heading)
  24134. summary (org-icalendar-cleanup-string
  24135. (org-entry-get nil "SUMMARY"))
  24136. desc (org-icalendar-cleanup-string
  24137. (or (org-entry-get nil "DESCRIPTION")
  24138. (and org-icalendar-include-body (org-get-entry)))
  24139. t org-icalendar-include-body)
  24140. location (org-icalendar-cleanup-string
  24141. (org-entry-get nil "LOCATION"))
  24142. category (org-get-category))
  24143. (if (looking-at re2)
  24144. (progn
  24145. (goto-char (match-end 0))
  24146. (setq ts2 (match-string 1) inc nil))
  24147. (setq tmp (buffer-substring (max (point-min)
  24148. (- pos org-ds-keyword-length))
  24149. pos)
  24150. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24151. (progn
  24152. (setq inc nil)
  24153. (replace-match "\\1" t nil ts))
  24154. ts)
  24155. deadlinep (string-match org-deadline-regexp tmp)
  24156. scheduledp (string-match org-scheduled-regexp tmp)
  24157. ;; donep (org-entry-is-done-p)
  24158. ))
  24159. (if (or (string-match org-tr-regexp hd)
  24160. (string-match org-ts-regexp hd))
  24161. (setq hd (replace-match "" t t hd)))
  24162. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24163. (setq rrule
  24164. (concat "\nRRULE:FREQ="
  24165. (cdr (assoc
  24166. (match-string 2 ts)
  24167. '(("d" . "DAILY")("w" . "WEEKLY")
  24168. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24169. ";INTERVAL=" (match-string 1 ts)))
  24170. (setq rrule ""))
  24171. (setq summary (or summary hd))
  24172. (if (string-match org-bracket-link-regexp summary)
  24173. (setq summary
  24174. (replace-match (if (match-end 3)
  24175. (match-string 3 summary)
  24176. (match-string 1 summary))
  24177. t t summary)))
  24178. (if deadlinep (setq summary (concat "DL: " summary)))
  24179. (if scheduledp (setq summary (concat "S: " summary)))
  24180. (if (string-match "\\`<%%" ts)
  24181. (with-current-buffer sexp-buffer
  24182. (insert (substring ts 1 -1) " " summary "\n"))
  24183. (princ (format "BEGIN:VEVENT
  24184. %s
  24185. %s%s
  24186. SUMMARY:%s%s%s
  24187. CATEGORIES:%s
  24188. END:VEVENT\n"
  24189. (org-ical-ts-to-string ts "DTSTART")
  24190. (org-ical-ts-to-string ts2 "DTEND" inc)
  24191. rrule summary
  24192. (if (and desc (string-match "\\S-" desc))
  24193. (concat "\nDESCRIPTION: " desc) "")
  24194. (if (and location (string-match "\\S-" location))
  24195. (concat "\nLOCATION: " location) "")
  24196. category)))))
  24197. (when (and org-icalendar-include-sexps
  24198. (condition-case nil (require 'icalendar) (error nil))
  24199. (fboundp 'icalendar-export-region))
  24200. ;; Get all the literal sexps
  24201. (goto-char (point-min))
  24202. (while (re-search-forward "^&?%%(" nil t)
  24203. (catch :skip
  24204. (org-agenda-skip)
  24205. (setq b (match-beginning 0))
  24206. (goto-char (1- (match-end 0)))
  24207. (forward-sexp 1)
  24208. (end-of-line 1)
  24209. (setq sexp (buffer-substring b (point)))
  24210. (with-current-buffer sexp-buffer
  24211. (insert sexp "\n"))
  24212. (princ (org-diary-to-ical-string sexp-buffer)))))
  24213. (when org-icalendar-include-todo
  24214. (goto-char (point-min))
  24215. (while (re-search-forward org-todo-line-regexp nil t)
  24216. (catch :skip
  24217. (org-agenda-skip)
  24218. (setq state (match-string 2))
  24219. (setq status (if (member state org-done-keywords)
  24220. "COMPLETED" "NEEDS-ACTION"))
  24221. (when (and state
  24222. (or (not (member state org-done-keywords))
  24223. (eq org-icalendar-include-todo 'all))
  24224. (not (member org-archive-tag (org-get-tags-at)))
  24225. )
  24226. (setq hd (match-string 3)
  24227. summary (org-icalendar-cleanup-string
  24228. (org-entry-get nil "SUMMARY"))
  24229. desc (org-icalendar-cleanup-string
  24230. (or (org-entry-get nil "DESCRIPTION")
  24231. (and org-icalendar-include-body (org-get-entry)))
  24232. t org-icalendar-include-body)
  24233. location (org-icalendar-cleanup-string
  24234. (org-entry-get nil "LOCATION")))
  24235. (if (string-match org-bracket-link-regexp hd)
  24236. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24237. (match-string 1 hd))
  24238. t t hd)))
  24239. (if (string-match org-priority-regexp hd)
  24240. (setq pri (string-to-char (match-string 2 hd))
  24241. hd (concat (substring hd 0 (match-beginning 1))
  24242. (substring hd (match-end 1))))
  24243. (setq pri org-default-priority))
  24244. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24245. (- org-lowest-priority org-highest-priority))))))
  24246. (princ (format "BEGIN:VTODO
  24247. %s
  24248. SUMMARY:%s%s%s
  24249. CATEGORIES:%s
  24250. SEQUENCE:1
  24251. PRIORITY:%d
  24252. STATUS:%s
  24253. END:VTODO\n"
  24254. dts
  24255. (or summary hd)
  24256. (if (and location (string-match "\\S-" location))
  24257. (concat "\nLOCATION: " location) "")
  24258. (if (and desc (string-match "\\S-" desc))
  24259. (concat "\nDESCRIPTION: " desc) "")
  24260. category pri status)))))))))
  24261. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24262. "Take out stuff and quote what needs to be quoted.
  24263. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24264. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24265. characters."
  24266. (if (not s)
  24267. nil
  24268. (when is-body
  24269. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24270. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24271. (while (string-match re s) (setq s (replace-match "" t t s)))
  24272. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24273. (let ((start 0))
  24274. (while (string-match "\\([,;\\]\\)" s start)
  24275. (setq start (+ (match-beginning 0) 2)
  24276. s (replace-match "\\\\\\1" nil nil s))))
  24277. (when is-body
  24278. (while (string-match "[ \t]*\n[ \t]*" s)
  24279. (setq s (replace-match "\\n" t t s))))
  24280. (setq s (org-trim s))
  24281. (if is-body
  24282. (if maxlength
  24283. (if (and (numberp maxlength)
  24284. (> (length s) maxlength))
  24285. (setq s (substring s 0 maxlength)))))
  24286. s))
  24287. (defun org-get-entry ()
  24288. "Clean-up description string."
  24289. (save-excursion
  24290. (org-back-to-heading t)
  24291. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24292. (defun org-start-icalendar-file (name)
  24293. "Start an iCalendar file by inserting the header."
  24294. (let ((user user-full-name)
  24295. (name (or name "unknown"))
  24296. (timezone (cadr (current-time-zone))))
  24297. (princ
  24298. (format "BEGIN:VCALENDAR
  24299. VERSION:2.0
  24300. X-WR-CALNAME:%s
  24301. PRODID:-//%s//Emacs with Org-mode//EN
  24302. X-WR-TIMEZONE:%s
  24303. CALSCALE:GREGORIAN\n" name user timezone))))
  24304. (defun org-finish-icalendar-file ()
  24305. "Finish an iCalendar file by inserting the END statement."
  24306. (princ "END:VCALENDAR\n"))
  24307. (defun org-ical-ts-to-string (s keyword &optional inc)
  24308. "Take a time string S and convert it to iCalendar format.
  24309. KEYWORD is added in front, to make a complete line like DTSTART....
  24310. When INC is non-nil, increase the hour by two (if time string contains
  24311. a time), or the day by one (if it does not contain a time)."
  24312. (let ((t1 (org-parse-time-string s 'nodefault))
  24313. t2 fmt have-time time)
  24314. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24315. (setq t2 t1 have-time t)
  24316. (setq t2 (org-parse-time-string s)))
  24317. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24318. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24319. (when inc
  24320. (if have-time
  24321. (if org-agenda-default-appointment-duration
  24322. (setq mi (+ org-agenda-default-appointment-duration mi))
  24323. (setq h (+ 2 h)))
  24324. (setq d (1+ d))))
  24325. (setq time (encode-time s mi h d m y)))
  24326. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24327. (concat keyword (format-time-string fmt time))))
  24328. ;;; XOXO export
  24329. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24330. (with-current-buffer buffer
  24331. (apply 'insert output)))
  24332. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24333. (defun org-export-as-xoxo (&optional buffer)
  24334. "Export the org buffer as XOXO.
  24335. The XOXO buffer is named *xoxo-<source buffer name>*"
  24336. (interactive (list (current-buffer)))
  24337. ;; A quickie abstraction
  24338. ;; Output everything as XOXO
  24339. (with-current-buffer (get-buffer buffer)
  24340. (let* ((pos (point))
  24341. (opt-plist (org-combine-plists (org-default-export-plist)
  24342. (org-infile-export-plist)))
  24343. (filename (concat (file-name-as-directory
  24344. (org-export-directory :xoxo opt-plist))
  24345. (file-name-sans-extension
  24346. (file-name-nondirectory buffer-file-name))
  24347. ".html"))
  24348. (out (find-file-noselect filename))
  24349. (last-level 1)
  24350. (hanging-li nil))
  24351. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24352. ;; Check the output buffer is empty.
  24353. (with-current-buffer out (erase-buffer))
  24354. ;; Kick off the output
  24355. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24356. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24357. (let* ((hd (match-string-no-properties 1))
  24358. (level (length hd))
  24359. (text (concat
  24360. (match-string-no-properties 2)
  24361. (save-excursion
  24362. (goto-char (match-end 0))
  24363. (let ((str ""))
  24364. (catch 'loop
  24365. (while 't
  24366. (forward-line)
  24367. (if (looking-at "^[ \t]\\(.*\\)")
  24368. (setq str (concat str (match-string-no-properties 1)))
  24369. (throw 'loop str)))))))))
  24370. ;; Handle level rendering
  24371. (cond
  24372. ((> level last-level)
  24373. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24374. ((< level last-level)
  24375. (dotimes (- (- last-level level) 1)
  24376. (if hanging-li
  24377. (org-export-as-xoxo-insert-into out "</li>\n"))
  24378. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24379. (when hanging-li
  24380. (org-export-as-xoxo-insert-into out "</li>\n")
  24381. (setq hanging-li nil)))
  24382. ((equal level last-level)
  24383. (if hanging-li
  24384. (org-export-as-xoxo-insert-into out "</li>\n")))
  24385. )
  24386. (setq last-level level)
  24387. ;; And output the new li
  24388. (setq hanging-li 't)
  24389. (if (equal ?+ (elt text 0))
  24390. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24391. (org-export-as-xoxo-insert-into out "<li>" text))))
  24392. ;; Finally finish off the ol
  24393. (dotimes (- last-level 1)
  24394. (if hanging-li
  24395. (org-export-as-xoxo-insert-into out "</li>\n"))
  24396. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24397. (goto-char pos)
  24398. ;; Finish the buffer off and clean it up.
  24399. (switch-to-buffer-other-window out)
  24400. (indent-region (point-min) (point-max) nil)
  24401. (save-buffer)
  24402. (goto-char (point-min))
  24403. )))
  24404. ;;;; Key bindings
  24405. ;; Make `C-c C-x' a prefix key
  24406. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24407. ;; TAB key with modifiers
  24408. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24409. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24410. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24411. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24412. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24413. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24414. ;; The following line is necessary under Suse GNU/Linux
  24415. (unless (featurep 'xemacs)
  24416. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24417. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24418. (define-key org-mode-map [backtab] 'org-shifttab)
  24419. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24420. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24421. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24422. ;; Cursor keys with modifiers
  24423. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24424. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24425. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24426. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24427. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24428. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24429. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24430. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24431. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24432. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24433. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24434. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24435. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24436. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24437. ;;; Extra keys for tty access.
  24438. ;; We only set them when really needed because otherwise the
  24439. ;; menus don't show the simple keys
  24440. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24441. (not window-system))
  24442. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24443. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24444. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24445. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24446. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24447. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24448. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24449. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24450. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24451. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24452. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24453. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24454. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24455. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24456. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24457. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24458. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24459. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24460. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24461. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24462. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24463. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24464. ;; All the other keys
  24465. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24466. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24467. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24468. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24469. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24470. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24471. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24472. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24473. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24474. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24475. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24476. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24477. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24478. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24479. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24480. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24481. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24482. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24483. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24484. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24485. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24486. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24487. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24488. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24489. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24490. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24491. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24492. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24493. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24494. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24495. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24496. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24497. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24498. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24499. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24500. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24501. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24502. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24503. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24504. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24505. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24506. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24507. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24508. (org-defkey org-mode-map "\C-m" 'org-return)
  24509. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24510. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24511. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24512. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24513. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24514. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24515. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24516. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24517. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24518. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24519. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24520. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24521. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24522. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24523. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24524. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24525. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24526. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24527. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24528. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24529. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24530. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24531. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24532. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24533. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24534. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24535. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24536. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24537. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24538. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24539. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24540. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24541. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24542. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24543. (when (featurep 'xemacs)
  24544. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24545. (defsubst org-table-p () (org-at-table-p))
  24546. (defun org-self-insert-command (N)
  24547. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24548. If the cursor is in a table looking at whitespace, the whitespace is
  24549. overwritten, and the table is not marked as requiring realignment."
  24550. (interactive "p")
  24551. (if (and (org-table-p)
  24552. (progn
  24553. ;; check if we blank the field, and if that triggers align
  24554. (and org-table-auto-blank-field
  24555. (member last-command
  24556. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24557. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24558. ;; got extra space, this field does not determine column width
  24559. (let (org-table-may-need-update) (org-table-blank-field))
  24560. ;; no extra space, this field may determine column width
  24561. (org-table-blank-field)))
  24562. t)
  24563. (eq N 1)
  24564. (looking-at "[^|\n]* |"))
  24565. (let (org-table-may-need-update)
  24566. (goto-char (1- (match-end 0)))
  24567. (delete-backward-char 1)
  24568. (goto-char (match-beginning 0))
  24569. (self-insert-command N))
  24570. (setq org-table-may-need-update t)
  24571. (self-insert-command N)
  24572. (org-fix-tags-on-the-fly)))
  24573. (defun org-fix-tags-on-the-fly ()
  24574. (when (and (equal (char-after (point-at-bol)) ?*)
  24575. (org-on-heading-p))
  24576. (org-align-tags-here org-tags-column)))
  24577. (defun org-delete-backward-char (N)
  24578. "Like `delete-backward-char', insert whitespace at field end in tables.
  24579. When deleting backwards, in tables this function will insert whitespace in
  24580. front of the next \"|\" separator, to keep the table aligned. The table will
  24581. still be marked for re-alignment if the field did fill the entire column,
  24582. because, in this case the deletion might narrow the column."
  24583. (interactive "p")
  24584. (if (and (org-table-p)
  24585. (eq N 1)
  24586. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24587. (looking-at ".*?|"))
  24588. (let ((pos (point))
  24589. (noalign (looking-at "[^|\n\r]* |"))
  24590. (c org-table-may-need-update))
  24591. (backward-delete-char N)
  24592. (skip-chars-forward "^|")
  24593. (insert " ")
  24594. (goto-char (1- pos))
  24595. ;; noalign: if there were two spaces at the end, this field
  24596. ;; does not determine the width of the column.
  24597. (if noalign (setq org-table-may-need-update c)))
  24598. (backward-delete-char N)
  24599. (org-fix-tags-on-the-fly)))
  24600. (defun org-delete-char (N)
  24601. "Like `delete-char', but insert whitespace at field end in tables.
  24602. When deleting characters, in tables this function will insert whitespace in
  24603. front of the next \"|\" separator, to keep the table aligned. The table will
  24604. still be marked for re-alignment if the field did fill the entire column,
  24605. because, in this case the deletion might narrow the column."
  24606. (interactive "p")
  24607. (if (and (org-table-p)
  24608. (not (bolp))
  24609. (not (= (char-after) ?|))
  24610. (eq N 1))
  24611. (if (looking-at ".*?|")
  24612. (let ((pos (point))
  24613. (noalign (looking-at "[^|\n\r]* |"))
  24614. (c org-table-may-need-update))
  24615. (replace-match (concat
  24616. (substring (match-string 0) 1 -1)
  24617. " |"))
  24618. (goto-char pos)
  24619. ;; noalign: if there were two spaces at the end, this field
  24620. ;; does not determine the width of the column.
  24621. (if noalign (setq org-table-may-need-update c)))
  24622. (delete-char N))
  24623. (delete-char N)
  24624. (org-fix-tags-on-the-fly)))
  24625. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24626. (put 'org-self-insert-command 'delete-selection t)
  24627. (put 'orgtbl-self-insert-command 'delete-selection t)
  24628. (put 'org-delete-char 'delete-selection 'supersede)
  24629. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24630. ;; Make `flyspell-mode' delay after some commands
  24631. (put 'org-self-insert-command 'flyspell-delayed t)
  24632. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24633. (put 'org-delete-char 'flyspell-delayed t)
  24634. (put 'org-delete-backward-char 'flyspell-delayed t)
  24635. ;; Make pabbrev-mode expand after org-mode commands
  24636. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24637. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24638. ;; How to do this: Measure non-white length of current string
  24639. ;; If equal to column width, we should realign.
  24640. (defun org-remap (map &rest commands)
  24641. "In MAP, remap the functions given in COMMANDS.
  24642. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24643. (let (new old)
  24644. (while commands
  24645. (setq old (pop commands) new (pop commands))
  24646. (if (fboundp 'command-remapping)
  24647. (org-defkey map (vector 'remap old) new)
  24648. (substitute-key-definition old new map global-map)))))
  24649. (when (eq org-enable-table-editor 'optimized)
  24650. ;; If the user wants maximum table support, we need to hijack
  24651. ;; some standard editing functions
  24652. (org-remap org-mode-map
  24653. 'self-insert-command 'org-self-insert-command
  24654. 'delete-char 'org-delete-char
  24655. 'delete-backward-char 'org-delete-backward-char)
  24656. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24657. (defun org-shiftcursor-error ()
  24658. "Throw an error because Shift-Cursor command was applied in wrong context."
  24659. (error "This command is active in special context like tables, headlines or timestamps"))
  24660. (defun org-shifttab (&optional arg)
  24661. "Global visibility cycling or move to previous table field.
  24662. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24663. on context.
  24664. See the individual commands for more information."
  24665. (interactive "P")
  24666. (cond
  24667. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24668. (arg (message "Content view to level: ")
  24669. (org-content (prefix-numeric-value arg))
  24670. (setq org-cycle-global-status 'overview))
  24671. (t (call-interactively 'org-global-cycle))))
  24672. (defun org-shiftmetaleft ()
  24673. "Promote subtree or delete table column.
  24674. Calls `org-promote-subtree', `org-outdent-item',
  24675. or `org-table-delete-column', depending on context.
  24676. See the individual commands for more information."
  24677. (interactive)
  24678. (cond
  24679. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24680. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24681. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24682. (t (org-shiftcursor-error))))
  24683. (defun org-shiftmetaright ()
  24684. "Demote subtree or insert table column.
  24685. Calls `org-demote-subtree', `org-indent-item',
  24686. or `org-table-insert-column', depending on context.
  24687. See the individual commands for more information."
  24688. (interactive)
  24689. (cond
  24690. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24691. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24692. ((org-at-item-p) (call-interactively 'org-indent-item))
  24693. (t (org-shiftcursor-error))))
  24694. (defun org-shiftmetaup (&optional arg)
  24695. "Move subtree up or kill table row.
  24696. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24697. `org-move-item-up' depending on context. See the individual commands
  24698. for more information."
  24699. (interactive "P")
  24700. (cond
  24701. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24702. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24703. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24704. (t (org-shiftcursor-error))))
  24705. (defun org-shiftmetadown (&optional arg)
  24706. "Move subtree down or insert table row.
  24707. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24708. `org-move-item-down', depending on context. See the individual
  24709. commands for more information."
  24710. (interactive "P")
  24711. (cond
  24712. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24713. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24714. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24715. (t (org-shiftcursor-error))))
  24716. (defun org-metaleft (&optional arg)
  24717. "Promote heading or move table column to left.
  24718. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24719. With no specific context, calls the Emacs default `backward-word'.
  24720. See the individual commands for more information."
  24721. (interactive "P")
  24722. (cond
  24723. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24724. ((or (org-on-heading-p) (org-region-active-p))
  24725. (call-interactively 'org-do-promote))
  24726. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24727. (t (call-interactively 'backward-word))))
  24728. (defun org-metaright (&optional arg)
  24729. "Demote subtree or move table column to right.
  24730. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24731. With no specific context, calls the Emacs default `forward-word'.
  24732. See the individual commands for more information."
  24733. (interactive "P")
  24734. (cond
  24735. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24736. ((or (org-on-heading-p) (org-region-active-p))
  24737. (call-interactively 'org-do-demote))
  24738. ((org-at-item-p) (call-interactively 'org-indent-item))
  24739. (t (call-interactively 'forward-word))))
  24740. (defun org-metaup (&optional arg)
  24741. "Move subtree up or move table row up.
  24742. Calls `org-move-subtree-up' or `org-table-move-row' or
  24743. `org-move-item-up', depending on context. See the individual commands
  24744. for more information."
  24745. (interactive "P")
  24746. (cond
  24747. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24748. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24749. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24750. (t (transpose-lines 1) (beginning-of-line -1))))
  24751. (defun org-metadown (&optional arg)
  24752. "Move subtree down or move table row down.
  24753. Calls `org-move-subtree-down' or `org-table-move-row' or
  24754. `org-move-item-down', depending on context. See the individual
  24755. commands for more information."
  24756. (interactive "P")
  24757. (cond
  24758. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24759. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24760. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24761. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24762. (defun org-shiftup (&optional arg)
  24763. "Increase item in timestamp or increase priority of current headline.
  24764. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24765. depending on context. See the individual commands for more information."
  24766. (interactive "P")
  24767. (cond
  24768. ((org-at-timestamp-p t)
  24769. (call-interactively (if org-edit-timestamp-down-means-later
  24770. 'org-timestamp-down 'org-timestamp-up)))
  24771. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24772. ((org-at-item-p) (call-interactively 'org-previous-item))
  24773. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24774. (defun org-shiftdown (&optional arg)
  24775. "Decrease item in timestamp or decrease priority of current headline.
  24776. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24777. depending on context. See the individual commands for more information."
  24778. (interactive "P")
  24779. (cond
  24780. ((org-at-timestamp-p t)
  24781. (call-interactively (if org-edit-timestamp-down-means-later
  24782. 'org-timestamp-up 'org-timestamp-down)))
  24783. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24784. (t (call-interactively 'org-next-item))))
  24785. (defun org-shiftright ()
  24786. "Next TODO keyword or timestamp one day later, depending on context."
  24787. (interactive)
  24788. (cond
  24789. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24790. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24791. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24792. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24793. (t (org-shiftcursor-error))))
  24794. (defun org-shiftleft ()
  24795. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24796. (interactive)
  24797. (cond
  24798. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24799. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24800. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24801. ((org-at-property-p)
  24802. (call-interactively 'org-property-previous-allowed-value))
  24803. (t (org-shiftcursor-error))))
  24804. (defun org-shiftcontrolright ()
  24805. "Switch to next TODO set."
  24806. (interactive)
  24807. (cond
  24808. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24809. (t (org-shiftcursor-error))))
  24810. (defun org-shiftcontrolleft ()
  24811. "Switch to previous TODO set."
  24812. (interactive)
  24813. (cond
  24814. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24815. (t (org-shiftcursor-error))))
  24816. (defun org-ctrl-c-ret ()
  24817. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24818. (interactive)
  24819. (cond
  24820. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24821. (t (call-interactively 'org-insert-heading))))
  24822. (defun org-copy-special ()
  24823. "Copy region in table or copy current subtree.
  24824. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24825. See the individual commands for more information."
  24826. (interactive)
  24827. (call-interactively
  24828. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24829. (defun org-cut-special ()
  24830. "Cut region in table or cut current subtree.
  24831. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24832. See the individual commands for more information."
  24833. (interactive)
  24834. (call-interactively
  24835. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24836. (defun org-paste-special (arg)
  24837. "Paste rectangular region into table, or past subtree relative to level.
  24838. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24839. See the individual commands for more information."
  24840. (interactive "P")
  24841. (if (org-at-table-p)
  24842. (org-table-paste-rectangle)
  24843. (org-paste-subtree arg)))
  24844. (defun org-ctrl-c-ctrl-c (&optional arg)
  24845. "Set tags in headline, or update according to changed information at point.
  24846. This command does many different things, depending on context:
  24847. - If the cursor is in a headline, prompt for tags and insert them
  24848. into the current line, aligned to `org-tags-column'. When called
  24849. with prefix arg, realign all tags in the current buffer.
  24850. - If the cursor is in one of the special #+KEYWORD lines, this
  24851. triggers scanning the buffer for these lines and updating the
  24852. information.
  24853. - If the cursor is inside a table, realign the table. This command
  24854. works even if the automatic table editor has been turned off.
  24855. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24856. the entire table.
  24857. - If the cursor is a the beginning of a dynamic block, update it.
  24858. - If the cursor is inside a table created by the table.el package,
  24859. activate that table.
  24860. - If the current buffer is a remember buffer, close note and file it.
  24861. with a prefix argument, file it without further interaction to the default
  24862. location.
  24863. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24864. links in this buffer.
  24865. - If the cursor is on a numbered item in a plain list, renumber the
  24866. ordered list.
  24867. - If the cursor is on a checkbox, toggle it."
  24868. (interactive "P")
  24869. (let ((org-enable-table-editor t))
  24870. (cond
  24871. ((or org-clock-overlays
  24872. org-occur-highlights
  24873. org-latex-fragment-image-overlays)
  24874. (org-remove-clock-overlays)
  24875. (org-remove-occur-highlights)
  24876. (org-remove-latex-fragment-image-overlays)
  24877. (message "Temporary highlights/overlays removed from current buffer"))
  24878. ((and (local-variable-p 'org-finish-function (current-buffer))
  24879. (fboundp org-finish-function))
  24880. (funcall org-finish-function))
  24881. ((org-at-property-p)
  24882. (call-interactively 'org-property-action))
  24883. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24884. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24885. ((org-at-table.el-p)
  24886. (require 'table)
  24887. (beginning-of-line 1)
  24888. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24889. (call-interactively 'table-recognize-table))
  24890. ((org-at-table-p)
  24891. (org-table-maybe-eval-formula)
  24892. (if arg
  24893. (call-interactively 'org-table-recalculate)
  24894. (org-table-maybe-recalculate-line))
  24895. (call-interactively 'org-table-align))
  24896. ((org-at-item-checkbox-p)
  24897. (call-interactively 'org-toggle-checkbox))
  24898. ((org-at-item-p)
  24899. (call-interactively 'org-maybe-renumber-ordered-list))
  24900. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24901. ;; Dynamic block
  24902. (beginning-of-line 1)
  24903. (org-update-dblock))
  24904. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24905. (cond
  24906. ((equal (match-string 1) "TBLFM")
  24907. ;; Recalculate the table before this line
  24908. (save-excursion
  24909. (beginning-of-line 1)
  24910. (skip-chars-backward " \r\n\t")
  24911. (if (org-at-table-p)
  24912. (org-call-with-arg 'org-table-recalculate t))))
  24913. (t
  24914. (call-interactively 'org-mode-restart))))
  24915. (t (error "C-c C-c can do nothing useful at this location.")))))
  24916. (defun org-mode-restart ()
  24917. "Restart Org-mode, to scan again for special lines.
  24918. Also updates the keyword regular expressions."
  24919. (interactive)
  24920. (let ((org-inhibit-startup t)) (org-mode))
  24921. (message "Org-mode restarted to refresh keyword and special line setup"))
  24922. (defun org-kill-note-or-show-branches ()
  24923. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  24924. (interactive)
  24925. (if (not org-finish-function)
  24926. (call-interactively 'show-branches)
  24927. (let ((org-note-abort t))
  24928. (funcall org-finish-function))))
  24929. (defun org-return (&optional indent)
  24930. "Goto next table row or insert a newline.
  24931. Calls `org-table-next-row' or `newline', depending on context.
  24932. See the individual commands for more information."
  24933. (interactive)
  24934. (cond
  24935. ((bobp) (if indent (newline-and-indent) (newline)))
  24936. ((and (org-at-heading-p)
  24937. (looking-at
  24938. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  24939. (org-show-entry)
  24940. (end-of-line 1)
  24941. (newline))
  24942. ((org-at-table-p)
  24943. (org-table-justify-field-maybe)
  24944. (call-interactively 'org-table-next-row))
  24945. (t (if indent (newline-and-indent) (newline)))))
  24946. (defun org-return-indent ()
  24947. (interactive)
  24948. "Goto next table row or insert a newline and indent.
  24949. Calls `org-table-next-row' or `newline-and-indent', depending on
  24950. context. See the individual commands for more information."
  24951. (org-return t))
  24952. (defun org-ctrl-c-minus ()
  24953. "Insert separator line in table or modify bullet type in list.
  24954. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  24955. depending on context."
  24956. (interactive)
  24957. (cond
  24958. ((org-at-table-p)
  24959. (call-interactively 'org-table-insert-hline))
  24960. ((org-on-heading-p)
  24961. ;; Convert to item
  24962. (save-excursion
  24963. (beginning-of-line 1)
  24964. (if (looking-at "\\*+ ")
  24965. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  24966. ((org-in-item-p)
  24967. (call-interactively 'org-cycle-list-bullet))
  24968. (t (error "`C-c -' does have no function here."))))
  24969. (defun org-meta-return (&optional arg)
  24970. "Insert a new heading or wrap a region in a table.
  24971. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  24972. See the individual commands for more information."
  24973. (interactive "P")
  24974. (cond
  24975. ((org-at-table-p)
  24976. (call-interactively 'org-table-wrap-region))
  24977. (t (call-interactively 'org-insert-heading))))
  24978. ;;; Menu entries
  24979. ;; Define the Org-mode menus
  24980. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  24981. '("Tbl"
  24982. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  24983. ["Next Field" org-cycle (org-at-table-p)]
  24984. ["Previous Field" org-shifttab (org-at-table-p)]
  24985. ["Next Row" org-return (org-at-table-p)]
  24986. "--"
  24987. ["Blank Field" org-table-blank-field (org-at-table-p)]
  24988. ["Edit Field" org-table-edit-field (org-at-table-p)]
  24989. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  24990. "--"
  24991. ("Column"
  24992. ["Move Column Left" org-metaleft (org-at-table-p)]
  24993. ["Move Column Right" org-metaright (org-at-table-p)]
  24994. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  24995. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  24996. ("Row"
  24997. ["Move Row Up" org-metaup (org-at-table-p)]
  24998. ["Move Row Down" org-metadown (org-at-table-p)]
  24999. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25000. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25001. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25002. "--"
  25003. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25004. ("Rectangle"
  25005. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25006. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25007. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25008. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25009. "--"
  25010. ("Calculate"
  25011. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25012. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25013. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25014. "--"
  25015. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25016. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25017. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25018. "--"
  25019. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25020. "--"
  25021. ["Sum Column/Rectangle" org-table-sum
  25022. (or (org-at-table-p) (org-region-active-p))]
  25023. ["Which Column?" org-table-current-column (org-at-table-p)])
  25024. ["Debug Formulas"
  25025. org-table-toggle-formula-debugger
  25026. :style toggle :selected org-table-formula-debug]
  25027. ["Show Col/Row Numbers"
  25028. org-table-toggle-coordinate-overlays
  25029. :style toggle :selected org-table-overlay-coordinates]
  25030. "--"
  25031. ["Create" org-table-create (and (not (org-at-table-p))
  25032. org-enable-table-editor)]
  25033. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25034. ["Import from File" org-table-import (not (org-at-table-p))]
  25035. ["Export to File" org-table-export (org-at-table-p)]
  25036. "--"
  25037. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25038. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25039. '("Org"
  25040. ("Show/Hide"
  25041. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25042. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25043. ["Sparse Tree" org-occur t]
  25044. ["Reveal Context" org-reveal t]
  25045. ["Show All" show-all t]
  25046. "--"
  25047. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25048. "--"
  25049. ["New Heading" org-insert-heading t]
  25050. ("Navigate Headings"
  25051. ["Up" outline-up-heading t]
  25052. ["Next" outline-next-visible-heading t]
  25053. ["Previous" outline-previous-visible-heading t]
  25054. ["Next Same Level" outline-forward-same-level t]
  25055. ["Previous Same Level" outline-backward-same-level t]
  25056. "--"
  25057. ["Jump" org-goto t])
  25058. ("Edit Structure"
  25059. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25060. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25061. "--"
  25062. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25063. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25064. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25065. "--"
  25066. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25067. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25068. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25069. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25070. "--"
  25071. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25072. "--"
  25073. ["Convert to odd levels" org-convert-to-odd-levels t]
  25074. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25075. ("Editing"
  25076. ["Emphasis..." org-emphasize t])
  25077. ("Archive"
  25078. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25079. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25080. ; :active t :keys "C-u C-c C-x C-a"]
  25081. ["Sparse trees open ARCHIVE trees"
  25082. (setq org-sparse-tree-open-archived-trees
  25083. (not org-sparse-tree-open-archived-trees))
  25084. :style toggle :selected org-sparse-tree-open-archived-trees]
  25085. ["Cycling opens ARCHIVE trees"
  25086. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25087. :style toggle :selected org-cycle-open-archived-trees]
  25088. ["Agenda includes ARCHIVE trees"
  25089. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25090. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25091. "--"
  25092. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25093. ; ["Check and Move Children" (org-archive-subtree '(4))
  25094. ; :active t :keys "C-u C-c C-x C-s"]
  25095. )
  25096. "--"
  25097. ("TODO Lists"
  25098. ["TODO/DONE/-" org-todo t]
  25099. ("Select keyword"
  25100. ["Next keyword" org-shiftright (org-on-heading-p)]
  25101. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25102. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25103. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25104. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25105. ["Show TODO Tree" org-show-todo-tree t]
  25106. ["Global TODO list" org-todo-list t]
  25107. "--"
  25108. ["Set Priority" org-priority t]
  25109. ["Priority Up" org-shiftup t]
  25110. ["Priority Down" org-shiftdown t])
  25111. ("TAGS and Properties"
  25112. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25113. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25114. "--"
  25115. ["Set property" 'org-set-property t]
  25116. ["Column view of properties" org-columns t]
  25117. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25118. ("Dates and Scheduling"
  25119. ["Timestamp" org-time-stamp t]
  25120. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25121. ("Change Date"
  25122. ["1 Day Later" org-shiftright t]
  25123. ["1 Day Earlier" org-shiftleft t]
  25124. ["1 ... Later" org-shiftup t]
  25125. ["1 ... Earlier" org-shiftdown t])
  25126. ["Compute Time Range" org-evaluate-time-range t]
  25127. ["Schedule Item" org-schedule t]
  25128. ["Deadline" org-deadline t]
  25129. "--"
  25130. ["Custom time format" org-toggle-time-stamp-overlays
  25131. :style radio :selected org-display-custom-times]
  25132. "--"
  25133. ["Goto Calendar" org-goto-calendar t]
  25134. ["Date from Calendar" org-date-from-calendar t])
  25135. ("Logging work"
  25136. ["Clock in" org-clock-in t]
  25137. ["Clock out" org-clock-out t]
  25138. ["Clock cancel" org-clock-cancel t]
  25139. ["Goto running clock" org-clock-goto t]
  25140. ["Display times" org-clock-display t]
  25141. ["Create clock table" org-clock-report t]
  25142. "--"
  25143. ["Record DONE time"
  25144. (progn (setq org-log-done (not org-log-done))
  25145. (message "Switching to %s will %s record a timestamp"
  25146. (car org-done-keywords)
  25147. (if org-log-done "automatically" "not")))
  25148. :style toggle :selected org-log-done])
  25149. "--"
  25150. ["Agenda Command..." org-agenda t]
  25151. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25152. ("File List for Agenda")
  25153. ("Special views current file"
  25154. ["TODO Tree" org-show-todo-tree t]
  25155. ["Check Deadlines" org-check-deadlines t]
  25156. ["Timeline" org-timeline t]
  25157. ["Tags Tree" org-tags-sparse-tree t])
  25158. "--"
  25159. ("Hyperlinks"
  25160. ["Store Link (Global)" org-store-link t]
  25161. ["Insert Link" org-insert-link t]
  25162. ["Follow Link" org-open-at-point t]
  25163. "--"
  25164. ["Next link" org-next-link t]
  25165. ["Previous link" org-previous-link t]
  25166. "--"
  25167. ["Descriptive Links"
  25168. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25169. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25170. ["Literal Links"
  25171. (progn
  25172. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25173. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25174. "--"
  25175. ["Export/Publish..." org-export t]
  25176. ("LaTeX"
  25177. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25178. :selected org-cdlatex-mode]
  25179. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25180. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25181. ["Modify math symbol" org-cdlatex-math-modify
  25182. (org-inside-LaTeX-fragment-p)]
  25183. ["Export LaTeX fragments as images"
  25184. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25185. :style toggle :selected org-export-with-LaTeX-fragments])
  25186. "--"
  25187. ("Documentation"
  25188. ["Show Version" org-version t]
  25189. ["Info Documentation" org-info t])
  25190. ("Customize"
  25191. ["Browse Org Group" org-customize t]
  25192. "--"
  25193. ["Expand This Menu" org-create-customize-menu
  25194. (fboundp 'customize-menu-create)])
  25195. "--"
  25196. ["Refresh setup" org-mode-restart t]
  25197. ))
  25198. (defun org-info (&optional node)
  25199. "Read documentation for Org-mode in the info system.
  25200. With optional NODE, go directly to that node."
  25201. (interactive)
  25202. (require 'info)
  25203. (Info-goto-node (format "(org)%s" (or node ""))))
  25204. (defun org-install-agenda-files-menu ()
  25205. (let ((bl (buffer-list)))
  25206. (save-excursion
  25207. (while bl
  25208. (set-buffer (pop bl))
  25209. (if (org-mode-p) (setq bl nil)))
  25210. (when (org-mode-p)
  25211. (easy-menu-change
  25212. '("Org") "File List for Agenda"
  25213. (append
  25214. (list
  25215. ["Edit File List" (org-edit-agenda-file-list) t]
  25216. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25217. ["Remove Current File from List" org-remove-file t]
  25218. ["Cycle through agenda files" org-cycle-agenda-files t]
  25219. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25220. "--")
  25221. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25222. ;;;; Documentation
  25223. (defun org-customize ()
  25224. "Call the customize function with org as argument."
  25225. (interactive)
  25226. (customize-browse 'org))
  25227. (defun org-create-customize-menu ()
  25228. "Create a full customization menu for Org-mode, insert it into the menu."
  25229. (interactive)
  25230. (if (fboundp 'customize-menu-create)
  25231. (progn
  25232. (easy-menu-change
  25233. '("Org") "Customize"
  25234. `(["Browse Org group" org-customize t]
  25235. "--"
  25236. ,(customize-menu-create 'org)
  25237. ["Set" Custom-set t]
  25238. ["Save" Custom-save t]
  25239. ["Reset to Current" Custom-reset-current t]
  25240. ["Reset to Saved" Custom-reset-saved t]
  25241. ["Reset to Standard Settings" Custom-reset-standard t]))
  25242. (message "\"Org\"-menu now contains full customization menu"))
  25243. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25244. ;;;; Miscellaneous stuff
  25245. ;;; Generally useful functions
  25246. (defun org-context ()
  25247. "Return a list of contexts of the current cursor position.
  25248. If several contexts apply, all are returned.
  25249. Each context entry is a list with a symbol naming the context, and
  25250. two positions indicating start and end of the context. Possible
  25251. contexts are:
  25252. :headline anywhere in a headline
  25253. :headline-stars on the leading stars in a headline
  25254. :todo-keyword on a TODO keyword (including DONE) in a headline
  25255. :tags on the TAGS in a headline
  25256. :priority on the priority cookie in a headline
  25257. :item on the first line of a plain list item
  25258. :item-bullet on the bullet/number of a plain list item
  25259. :checkbox on the checkbox in a plain list item
  25260. :table in an org-mode table
  25261. :table-special on a special filed in a table
  25262. :table-table in a table.el table
  25263. :link on a hyperlink
  25264. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25265. :target on a <<target>>
  25266. :radio-target on a <<<radio-target>>>
  25267. :latex-fragment on a LaTeX fragment
  25268. :latex-preview on a LaTeX fragment with overlayed preview image
  25269. This function expects the position to be visible because it uses font-lock
  25270. faces as a help to recognize the following contexts: :table-special, :link,
  25271. and :keyword."
  25272. (let* ((f (get-text-property (point) 'face))
  25273. (faces (if (listp f) f (list f)))
  25274. (p (point)) clist o)
  25275. ;; First the large context
  25276. (cond
  25277. ((org-on-heading-p t)
  25278. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25279. (when (progn
  25280. (beginning-of-line 1)
  25281. (looking-at org-todo-line-tags-regexp))
  25282. (push (org-point-in-group p 1 :headline-stars) clist)
  25283. (push (org-point-in-group p 2 :todo-keyword) clist)
  25284. (push (org-point-in-group p 4 :tags) clist))
  25285. (goto-char p)
  25286. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25287. (if (looking-at "\\[#[A-Z0-9]\\]")
  25288. (push (org-point-in-group p 0 :priority) clist)))
  25289. ((org-at-item-p)
  25290. (push (org-point-in-group p 2 :item-bullet) clist)
  25291. (push (list :item (point-at-bol)
  25292. (save-excursion (org-end-of-item) (point)))
  25293. clist)
  25294. (and (org-at-item-checkbox-p)
  25295. (push (org-point-in-group p 0 :checkbox) clist)))
  25296. ((org-at-table-p)
  25297. (push (list :table (org-table-begin) (org-table-end)) clist)
  25298. (if (memq 'org-formula faces)
  25299. (push (list :table-special
  25300. (previous-single-property-change p 'face)
  25301. (next-single-property-change p 'face)) clist)))
  25302. ((org-at-table-p 'any)
  25303. (push (list :table-table) clist)))
  25304. (goto-char p)
  25305. ;; Now the small context
  25306. (cond
  25307. ((org-at-timestamp-p)
  25308. (push (org-point-in-group p 0 :timestamp) clist))
  25309. ((memq 'org-link faces)
  25310. (push (list :link
  25311. (previous-single-property-change p 'face)
  25312. (next-single-property-change p 'face)) clist))
  25313. ((memq 'org-special-keyword faces)
  25314. (push (list :keyword
  25315. (previous-single-property-change p 'face)
  25316. (next-single-property-change p 'face)) clist))
  25317. ((org-on-target-p)
  25318. (push (org-point-in-group p 0 :target) clist)
  25319. (goto-char (1- (match-beginning 0)))
  25320. (if (looking-at org-radio-target-regexp)
  25321. (push (org-point-in-group p 0 :radio-target) clist))
  25322. (goto-char p))
  25323. ((setq o (car (delq nil
  25324. (mapcar
  25325. (lambda (x)
  25326. (if (memq x org-latex-fragment-image-overlays) x))
  25327. (org-overlays-at (point))))))
  25328. (push (list :latex-fragment
  25329. (org-overlay-start o) (org-overlay-end o)) clist)
  25330. (push (list :latex-preview
  25331. (org-overlay-start o) (org-overlay-end o)) clist))
  25332. ((org-inside-LaTeX-fragment-p)
  25333. ;; FIXME: positions wrong.
  25334. (push (list :latex-fragment (point) (point)) clist)))
  25335. (setq clist (nreverse (delq nil clist)))
  25336. clist))
  25337. ;; FIXME: Compare with at-regexp-p Do we need both?
  25338. (defun org-in-regexp (re &optional nlines visually)
  25339. "Check if point is inside a match of regexp.
  25340. Normally only the current line is checked, but you can include NLINES extra
  25341. lines both before and after point into the search.
  25342. If VISUALLY is set, require that the cursor is not after the match but
  25343. really on, so that the block visually is on the match."
  25344. (catch 'exit
  25345. (let ((pos (point))
  25346. (eol (point-at-eol (+ 1 (or nlines 0))))
  25347. (inc (if visually 1 0)))
  25348. (save-excursion
  25349. (beginning-of-line (- 1 (or nlines 0)))
  25350. (while (re-search-forward re eol t)
  25351. (if (and (<= (match-beginning 0) pos)
  25352. (>= (+ inc (match-end 0)) pos))
  25353. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25354. (defun org-at-regexp-p (regexp)
  25355. "Is point inside a match of REGEXP in the current line?"
  25356. (catch 'exit
  25357. (save-excursion
  25358. (let ((pos (point)) (end (point-at-eol)))
  25359. (beginning-of-line 1)
  25360. (while (re-search-forward regexp end t)
  25361. (if (and (<= (match-beginning 0) pos)
  25362. (>= (match-end 0) pos))
  25363. (throw 'exit t)))
  25364. nil))))
  25365. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25366. "Call `multi-occur' with buffers for all agenda files."
  25367. (interactive "sOrg-files matching: \np")
  25368. (let* ((files (org-agenda-files))
  25369. (tnames (mapcar 'file-truename files))
  25370. (extra org-agenda-multi-occur-extra-files)
  25371. f)
  25372. (while (setq f (pop extra))
  25373. (unless (member (file-truename f) tnames)
  25374. (add-to-list 'files f 'append)
  25375. (add-to-list 'tnames (file-truename f) 'append)))
  25376. (multi-occur
  25377. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25378. regexp)))
  25379. (if (boundp 'occur-mode-find-occurrence-hook)
  25380. ;; Emacs 23
  25381. (add-hook 'occur-mode-find-occurrence-hook
  25382. (lambda ()
  25383. (when (org-mode-p)
  25384. (org-reveal))))
  25385. ;; Emacs 22
  25386. (defadvice occur-mode-goto-occurrence
  25387. (after org-occur-reveal activate)
  25388. (and (org-mode-p) (org-reveal)))
  25389. (defadvice occur-mode-goto-occurrence-other-window
  25390. (after org-occur-reveal activate)
  25391. (and (org-mode-p) (org-reveal)))
  25392. (defadvice occur-mode-display-occurrence
  25393. (after org-occur-reveal activate)
  25394. (when (org-mode-p)
  25395. (let ((pos (occur-mode-find-occurrence)))
  25396. (with-current-buffer (marker-buffer pos)
  25397. (save-excursion
  25398. (goto-char pos)
  25399. (org-reveal)))))))
  25400. (defun org-uniquify (list)
  25401. "Remove duplicate elements from LIST."
  25402. (let (res)
  25403. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25404. res))
  25405. (defun org-delete-all (elts list)
  25406. "Remove all elements in ELTS from LIST."
  25407. (while elts
  25408. (setq list (delete (pop elts) list)))
  25409. list)
  25410. (defun org-back-over-empty-lines ()
  25411. "Move backwards over witespace, to the beginning of the first empty line.
  25412. Returns the number o empty lines passed."
  25413. (let ((pos (point)))
  25414. (skip-chars-backward " \t\n\r")
  25415. (beginning-of-line 2)
  25416. (goto-char (min (point) pos))
  25417. (count-lines (point) pos)))
  25418. (defun org-skip-whitespace ()
  25419. (skip-chars-forward " \t\n\r"))
  25420. (defun org-point-in-group (point group &optional context)
  25421. "Check if POINT is in match-group GROUP.
  25422. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25423. match. If the match group does ot exist or point is not inside it,
  25424. return nil."
  25425. (and (match-beginning group)
  25426. (>= point (match-beginning group))
  25427. (<= point (match-end group))
  25428. (if context
  25429. (list context (match-beginning group) (match-end group))
  25430. t)))
  25431. (defun org-switch-to-buffer-other-window (&rest args)
  25432. "Switch to buffer in a second window on the current frame.
  25433. In particular, do not allow pop-up frames."
  25434. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25435. special-display-function)
  25436. (apply 'switch-to-buffer-other-window args)))
  25437. (defun org-combine-plists (&rest plists)
  25438. "Create a single property list from all plists in PLISTS.
  25439. The process starts by copying the first list, and then setting properties
  25440. from the other lists. Settings in the last list are the most significant
  25441. ones and overrule settings in the other lists."
  25442. (let ((rtn (copy-sequence (pop plists)))
  25443. p v ls)
  25444. (while plists
  25445. (setq ls (pop plists))
  25446. (while ls
  25447. (setq p (pop ls) v (pop ls))
  25448. (setq rtn (plist-put rtn p v))))
  25449. rtn))
  25450. (defun org-move-line-down (arg)
  25451. "Move the current line down. With prefix argument, move it past ARG lines."
  25452. (interactive "p")
  25453. (let ((col (current-column))
  25454. beg end pos)
  25455. (beginning-of-line 1) (setq beg (point))
  25456. (beginning-of-line 2) (setq end (point))
  25457. (beginning-of-line (+ 1 arg))
  25458. (setq pos (move-marker (make-marker) (point)))
  25459. (insert (delete-and-extract-region beg end))
  25460. (goto-char pos)
  25461. (move-to-column col)))
  25462. (defun org-move-line-up (arg)
  25463. "Move the current line up. With prefix argument, move it past ARG lines."
  25464. (interactive "p")
  25465. (let ((col (current-column))
  25466. beg end pos)
  25467. (beginning-of-line 1) (setq beg (point))
  25468. (beginning-of-line 2) (setq end (point))
  25469. (beginning-of-line (- arg))
  25470. (setq pos (move-marker (make-marker) (point)))
  25471. (insert (delete-and-extract-region beg end))
  25472. (goto-char pos)
  25473. (move-to-column col)))
  25474. (defun org-replace-escapes (string table)
  25475. "Replace %-escapes in STRING with values in TABLE.
  25476. TABLE is an association list with keys like \"%a\" and string values.
  25477. The sequences in STRING may contain normal field width and padding information,
  25478. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25479. so values can contain further %-escapes if they are define later in TABLE."
  25480. (let ((case-fold-search nil)
  25481. e re rpl)
  25482. (while (setq e (pop table))
  25483. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25484. (while (string-match re string)
  25485. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25486. (cdr e)))
  25487. (setq string (replace-match rpl t t string))))
  25488. string))
  25489. (defun org-sublist (list start end)
  25490. "Return a section of LIST, from START to END.
  25491. Counting starts at 1."
  25492. (let (rtn (c start))
  25493. (setq list (nthcdr (1- start) list))
  25494. (while (and list (<= c end))
  25495. (push (pop list) rtn)
  25496. (setq c (1+ c)))
  25497. (nreverse rtn)))
  25498. (defun org-find-base-buffer-visiting (file)
  25499. "Like `find-buffer-visiting' but alway return the base buffer and
  25500. not an indirect buffer"
  25501. (let ((buf (find-buffer-visiting file)))
  25502. (if buf
  25503. (or (buffer-base-buffer buf) buf)
  25504. nil)))
  25505. (defun org-image-file-name-regexp ()
  25506. "Return regexp matching the file names of images."
  25507. (if (fboundp 'image-file-name-regexp)
  25508. (image-file-name-regexp)
  25509. (let ((image-file-name-extensions
  25510. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25511. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25512. (concat "\\."
  25513. (regexp-opt (nconc (mapcar 'upcase
  25514. image-file-name-extensions)
  25515. image-file-name-extensions)
  25516. t)
  25517. "\\'"))))
  25518. (defun org-file-image-p (file)
  25519. "Return non-nil if FILE is an image."
  25520. (save-match-data
  25521. (string-match (org-image-file-name-regexp) file)))
  25522. ;;; Paragraph filling stuff.
  25523. ;; We want this to be just right, so use the full arsenal.
  25524. (defun org-indent-line-function ()
  25525. "Indent line like previous, but further if previous was headline or item."
  25526. (interactive)
  25527. (let* ((pos (point))
  25528. (itemp (org-at-item-p))
  25529. column bpos bcol tpos tcol bullet btype bullet-type)
  25530. ;; Find the previous relevant line
  25531. (beginning-of-line 1)
  25532. (cond
  25533. ((looking-at "#") (setq column 0))
  25534. ((looking-at "\\*+ ") (setq column 0))
  25535. (t
  25536. (beginning-of-line 0)
  25537. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25538. (beginning-of-line 0))
  25539. (cond
  25540. ((looking-at "\\*+[ \t]+")
  25541. (goto-char (match-end 0))
  25542. (setq column (current-column)))
  25543. ((org-in-item-p)
  25544. (org-beginning-of-item)
  25545. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25546. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25547. (setq bpos (match-beginning 1) tpos (match-end 0)
  25548. bcol (progn (goto-char bpos) (current-column))
  25549. tcol (progn (goto-char tpos) (current-column))
  25550. bullet (match-string 1)
  25551. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25552. (if (not itemp)
  25553. (setq column tcol)
  25554. (goto-char pos)
  25555. (beginning-of-line 1)
  25556. (if (looking-at "\\S-")
  25557. (progn
  25558. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25559. (setq bullet (match-string 1)
  25560. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25561. (setq column (if (equal btype bullet-type) bcol tcol)))
  25562. (setq column (org-get-indentation)))))
  25563. (t (setq column (org-get-indentation))))))
  25564. (goto-char pos)
  25565. (if (<= (current-column) (current-indentation))
  25566. (indent-line-to column)
  25567. (save-excursion (indent-line-to column)))
  25568. (setq column (current-column))
  25569. (beginning-of-line 1)
  25570. (if (looking-at
  25571. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25572. (replace-match (concat "\\1" (format org-property-format
  25573. (match-string 2) (match-string 3)))
  25574. t nil))
  25575. (move-to-column column)))
  25576. (defun org-set-autofill-regexps ()
  25577. (interactive)
  25578. ;; In the paragraph separator we include headlines, because filling
  25579. ;; text in a line directly attached to a headline would otherwise
  25580. ;; fill the headline as well.
  25581. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25582. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25583. ;; The paragraph starter includes hand-formatted lists.
  25584. (org-set-local 'paragraph-start
  25585. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25586. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25587. ;; But only if the user has not turned off tables or fixed-width regions
  25588. (org-set-local
  25589. 'auto-fill-inhibit-regexp
  25590. (concat "\\*+ \\|#\\+"
  25591. "\\|[ \t]*" org-keyword-time-regexp
  25592. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25593. (concat
  25594. "\\|[ \t]*["
  25595. (if org-enable-table-editor "|" "")
  25596. (if org-enable-fixed-width-editor ":" "")
  25597. "]"))))
  25598. ;; We use our own fill-paragraph function, to make sure that tables
  25599. ;; and fixed-width regions are not wrapped. That function will pass
  25600. ;; through to `fill-paragraph' when appropriate.
  25601. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25602. ; Adaptive filling: To get full control, first make sure that
  25603. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25604. (org-set-local 'adaptive-fill-regexp "\000")
  25605. (org-set-local 'adaptive-fill-function
  25606. 'org-adaptive-fill-function)
  25607. (org-set-local
  25608. 'align-mode-rules-list
  25609. '((org-in-buffer-settings
  25610. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25611. (modes . '(org-mode))))))
  25612. (defun org-fill-paragraph (&optional justify)
  25613. "Re-align a table, pass through to fill-paragraph if no table."
  25614. (let ((table-p (org-at-table-p))
  25615. (table.el-p (org-at-table.el-p)))
  25616. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25617. (save-excursion (goto-char (point-at-bol))
  25618. (looking-at outline-regexp)))
  25619. t) ; skip headlines
  25620. (table.el-p t) ; skip table.el tables
  25621. (table-p (org-table-align) t) ; align org-mode tables
  25622. (t nil)))) ; call paragraph-fill
  25623. ;; For reference, this is the default value of adaptive-fill-regexp
  25624. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25625. (defun org-adaptive-fill-function ()
  25626. "Return a fill prefix for org-mode files.
  25627. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25628. work correctly."
  25629. (cond ((looking-at "#[ \t]+")
  25630. (match-string 0))
  25631. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25632. (save-excursion
  25633. (goto-char (match-end 0))
  25634. (make-string (current-column) ?\ )))
  25635. (t nil)))
  25636. ;;;; Functions extending outline functionality
  25637. (defun org-beginning-of-line (&optional arg)
  25638. "Go to the beginning of the current line. If that is invisible, continue
  25639. to a visible line beginning. This makes the function of C-a more intuitive.
  25640. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25641. first attempt, and only move to after the tags when the cursor is already
  25642. beyond the end of the headline."
  25643. (interactive "P")
  25644. (let ((pos (point)))
  25645. (beginning-of-line 1)
  25646. (if (bobp)
  25647. nil
  25648. (backward-char 1)
  25649. (if (org-invisible-p)
  25650. (while (and (not (bobp)) (org-invisible-p))
  25651. (backward-char 1)
  25652. (beginning-of-line 1))
  25653. (forward-char 1)))
  25654. (when org-special-ctrl-a/e
  25655. (cond
  25656. ((and (looking-at org-todo-line-regexp)
  25657. (= (char-after (match-end 1)) ?\ ))
  25658. (goto-char
  25659. (if (eq org-special-ctrl-a/e t)
  25660. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25661. ((= pos (point)) (match-beginning 3))
  25662. (t (point)))
  25663. (cond ((> pos (point)) (point))
  25664. ((not (eq last-command this-command)) (point))
  25665. (t (match-beginning 3))))))
  25666. ((org-at-item-p)
  25667. (goto-char
  25668. (if (eq org-special-ctrl-a/e t)
  25669. (cond ((> pos (match-end 4)) (match-end 4))
  25670. ((= pos (point)) (match-end 4))
  25671. (t (point)))
  25672. (cond ((> pos (point)) (point))
  25673. ((not (eq last-command this-command)) (point))
  25674. (t (match-end 4))))))))))
  25675. (defun org-end-of-line (&optional arg)
  25676. "Go to the end of the line.
  25677. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25678. first attempt, and only move to after the tags when the cursor is already
  25679. beyond the end of the headline."
  25680. (interactive "P")
  25681. (if (or (not org-special-ctrl-a/e)
  25682. (not (org-on-heading-p)))
  25683. (end-of-line arg)
  25684. (let ((pos (point)))
  25685. (beginning-of-line 1)
  25686. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25687. (if (eq org-special-ctrl-a/e t)
  25688. (if (or (< pos (match-beginning 1))
  25689. (= pos (match-end 0)))
  25690. (goto-char (match-beginning 1))
  25691. (goto-char (match-end 0)))
  25692. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25693. (goto-char (match-end 0))
  25694. (goto-char (match-beginning 1))))
  25695. (end-of-line arg)))))
  25696. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25697. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25698. (defun org-kill-line (&optional arg)
  25699. "Kill line, to tags or end of line."
  25700. (interactive "P")
  25701. (cond
  25702. ((or (not org-special-ctrl-k)
  25703. (bolp)
  25704. (not (org-on-heading-p)))
  25705. (call-interactively 'kill-line))
  25706. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25707. (kill-region (point) (match-beginning 1))
  25708. (org-set-tags nil t))
  25709. (t (kill-region (point) (point-at-eol)))))
  25710. (define-key org-mode-map "\C-k" 'org-kill-line)
  25711. (defun org-invisible-p ()
  25712. "Check if point is at a character currently not visible."
  25713. ;; Early versions of noutline don't have `outline-invisible-p'.
  25714. (if (fboundp 'outline-invisible-p)
  25715. (outline-invisible-p)
  25716. (get-char-property (point) 'invisible)))
  25717. (defun org-invisible-p2 ()
  25718. "Check if point is at a character currently not visible."
  25719. (save-excursion
  25720. (if (and (eolp) (not (bobp))) (backward-char 1))
  25721. ;; Early versions of noutline don't have `outline-invisible-p'.
  25722. (if (fboundp 'outline-invisible-p)
  25723. (outline-invisible-p)
  25724. (get-char-property (point) 'invisible))))
  25725. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25726. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25727. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25728. (defun org-at-heading-or-item-p ()
  25729. (or (org-on-heading-p) (org-at-item-p)))
  25730. (defun org-on-target-p ()
  25731. (or (org-in-regexp org-radio-target-regexp)
  25732. (org-in-regexp org-target-regexp)))
  25733. (defun org-up-heading-all (arg)
  25734. "Move to the heading line of which the present line is a subheading.
  25735. This function considers both visible and invisible heading lines.
  25736. With argument, move up ARG levels."
  25737. (if (fboundp 'outline-up-heading-all)
  25738. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25739. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25740. (defun org-up-heading-safe ()
  25741. "Move to the heading line of which the present line is a subheading.
  25742. This version will not throw an error. It will return the level of the
  25743. headline found, or nil if no higher level is found."
  25744. (let ((pos (point)) start-level level
  25745. (re (concat "^" outline-regexp)))
  25746. (catch 'exit
  25747. (outline-back-to-heading t)
  25748. (setq start-level (funcall outline-level))
  25749. (if (equal start-level 1) (throw 'exit nil))
  25750. (while (re-search-backward re nil t)
  25751. (setq level (funcall outline-level))
  25752. (if (< level start-level) (throw 'exit level)))
  25753. nil)))
  25754. (defun org-first-sibling-p ()
  25755. "Is this heading the first child of its parents?"
  25756. (interactive)
  25757. (let ((re (concat "^" outline-regexp))
  25758. level l)
  25759. (unless (org-at-heading-p t)
  25760. (error "Not at a heading"))
  25761. (setq level (funcall outline-level))
  25762. (save-excursion
  25763. (if (not (re-search-backward re nil t))
  25764. t
  25765. (setq l (funcall outline-level))
  25766. (< l level)))))
  25767. (defun org-goto-sibling (&optional previous)
  25768. "Goto the next sibling, even if it is invisible.
  25769. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25770. when a sibling was found. When none is found, return nil and don't
  25771. move point."
  25772. (let ((fun (if previous 're-search-backward 're-search-forward))
  25773. (pos (point))
  25774. (re (concat "^" outline-regexp))
  25775. level l)
  25776. (when (condition-case nil (org-back-to-heading t) (error nil))
  25777. (setq level (funcall outline-level))
  25778. (catch 'exit
  25779. (or previous (forward-char 1))
  25780. (while (funcall fun re nil t)
  25781. (setq l (funcall outline-level))
  25782. (when (< l level) (goto-char pos) (throw 'exit nil))
  25783. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25784. (goto-char pos)
  25785. nil))))
  25786. (defun org-show-siblings ()
  25787. "Show all siblings of the current headline."
  25788. (save-excursion
  25789. (while (org-goto-sibling) (org-flag-heading nil)))
  25790. (save-excursion
  25791. (while (org-goto-sibling 'previous)
  25792. (org-flag-heading nil))))
  25793. (defun org-show-hidden-entry ()
  25794. "Show an entry where even the heading is hidden."
  25795. (save-excursion
  25796. (org-show-entry)))
  25797. (defun org-flag-heading (flag &optional entry)
  25798. "Flag the current heading. FLAG non-nil means make invisible.
  25799. When ENTRY is non-nil, show the entire entry."
  25800. (save-excursion
  25801. (org-back-to-heading t)
  25802. ;; Check if we should show the entire entry
  25803. (if entry
  25804. (progn
  25805. (org-show-entry)
  25806. (save-excursion
  25807. (and (outline-next-heading)
  25808. (org-flag-heading nil))))
  25809. (outline-flag-region (max (point-min) (1- (point)))
  25810. (save-excursion (outline-end-of-heading) (point))
  25811. flag))))
  25812. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25813. ;; This is an exact copy of the original function, but it uses
  25814. ;; `org-back-to-heading', to make it work also in invisible
  25815. ;; trees. And is uses an invisible-OK argument.
  25816. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25817. (org-back-to-heading invisible-OK)
  25818. (let ((first t)
  25819. (level (funcall outline-level)))
  25820. (while (and (not (eobp))
  25821. (or first (> (funcall outline-level) level)))
  25822. (setq first nil)
  25823. (outline-next-heading))
  25824. (unless to-heading
  25825. (if (memq (preceding-char) '(?\n ?\^M))
  25826. (progn
  25827. ;; Go to end of line before heading
  25828. (forward-char -1)
  25829. (if (memq (preceding-char) '(?\n ?\^M))
  25830. ;; leave blank line before heading
  25831. (forward-char -1))))))
  25832. (point))
  25833. (defun org-show-subtree ()
  25834. "Show everything after this heading at deeper levels."
  25835. (outline-flag-region
  25836. (point)
  25837. (save-excursion
  25838. (outline-end-of-subtree) (outline-next-heading) (point))
  25839. nil))
  25840. (defun org-show-entry ()
  25841. "Show the body directly following this heading.
  25842. Show the heading too, if it is currently invisible."
  25843. (interactive)
  25844. (save-excursion
  25845. (condition-case nil
  25846. (progn
  25847. (org-back-to-heading t)
  25848. (outline-flag-region
  25849. (max (point-min) (1- (point)))
  25850. (save-excursion
  25851. (re-search-forward
  25852. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25853. (or (match-beginning 1) (point-max)))
  25854. nil))
  25855. (error nil))))
  25856. (defun org-make-options-regexp (kwds)
  25857. "Make a regular expression for keyword lines."
  25858. (concat
  25859. "^"
  25860. "#?[ \t]*\\+\\("
  25861. (mapconcat 'regexp-quote kwds "\\|")
  25862. "\\):[ \t]*"
  25863. "\\(.+\\)"))
  25864. ;; Make isearch reveal the necessary context
  25865. (defun org-isearch-end ()
  25866. "Reveal context after isearch exits."
  25867. (when isearch-success ; only if search was successful
  25868. (if (featurep 'xemacs)
  25869. ;; Under XEmacs, the hook is run in the correct place,
  25870. ;; we directly show the context.
  25871. (org-show-context 'isearch)
  25872. ;; In Emacs the hook runs *before* restoring the overlays.
  25873. ;; So we have to use a one-time post-command-hook to do this.
  25874. ;; (Emacs 22 has a special variable, see function `org-mode')
  25875. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25876. isearch-mode-end-hook-quit)
  25877. ;; Only when the isearch was not quitted.
  25878. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25879. 'append 'local)))))
  25880. (defun org-isearch-post-command ()
  25881. "Remove self from hook, and show context."
  25882. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25883. (org-show-context 'isearch))
  25884. ;;;; Integration with and fixes for other packages
  25885. ;;; Imenu support
  25886. (defvar org-imenu-markers nil
  25887. "All markers currently used by Imenu.")
  25888. (make-variable-buffer-local 'org-imenu-markers)
  25889. (defun org-imenu-new-marker (&optional pos)
  25890. "Return a new marker for use by Imenu, and remember the marker."
  25891. (let ((m (make-marker)))
  25892. (move-marker m (or pos (point)))
  25893. (push m org-imenu-markers)
  25894. m))
  25895. (defun org-imenu-get-tree ()
  25896. "Produce the index for Imenu."
  25897. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25898. (setq org-imenu-markers nil)
  25899. (let* ((n org-imenu-depth)
  25900. (re (concat "^" outline-regexp))
  25901. (subs (make-vector (1+ n) nil))
  25902. (last-level 0)
  25903. m tree level head)
  25904. (save-excursion
  25905. (save-restriction
  25906. (widen)
  25907. (goto-char (point-max))
  25908. (while (re-search-backward re nil t)
  25909. (setq level (org-reduced-level (funcall outline-level)))
  25910. (when (<= level n)
  25911. (looking-at org-complex-heading-regexp)
  25912. (setq head (org-match-string-no-properties 4)
  25913. m (org-imenu-new-marker))
  25914. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25915. (if (>= level last-level)
  25916. (push (cons head m) (aref subs level))
  25917. (push (cons head (aref subs (1+ level))) (aref subs level))
  25918. (loop for i from (1+ level) to n do (aset subs i nil)))
  25919. (setq last-level level)))))
  25920. (aref subs 1)))
  25921. (eval-after-load "imenu"
  25922. '(progn
  25923. (add-hook 'imenu-after-jump-hook
  25924. (lambda () (org-show-context 'org-goto)))))
  25925. ;; Speedbar support
  25926. (defun org-speedbar-set-agenda-restriction ()
  25927. "Restrict future agenda commands to the location at point in speedbar.
  25928. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  25929. (interactive)
  25930. (let (p m tp np dir txt w)
  25931. (cond
  25932. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25933. 'org-imenu t))
  25934. (setq m (get-text-property p 'org-imenu-marker))
  25935. (save-excursion
  25936. (save-restriction
  25937. (set-buffer (marker-buffer m))
  25938. (goto-char m)
  25939. (org-agenda-set-restriction-lock 'subtree))))
  25940. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25941. 'speedbar-function 'speedbar-find-file))
  25942. (setq tp (previous-single-property-change
  25943. (1+ p) 'speedbar-function)
  25944. np (next-single-property-change
  25945. tp 'speedbar-function)
  25946. dir (speedbar-line-directory)
  25947. txt (buffer-substring-no-properties (or tp (point-min))
  25948. (or np (point-max))))
  25949. (save-excursion
  25950. (save-restriction
  25951. (set-buffer (find-file-noselect
  25952. (let ((default-directory dir))
  25953. (expand-file-name txt))))
  25954. (unless (org-mode-p)
  25955. (error "Cannot restrict to non-Org-mode file"))
  25956. (org-agenda-set-restriction-lock 'file))))
  25957. (t (error "Don't know how to restrict Org-mode's agenda")))
  25958. (org-move-overlay org-speedbar-restriction-lock-overlay
  25959. (point-at-bol) (point-at-eol))
  25960. (setq current-prefix-arg nil)
  25961. (org-agenda-maybe-redo)))
  25962. (eval-after-load "speedbar"
  25963. '(progn
  25964. (speedbar-add-supported-extension ".org")
  25965. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  25966. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  25967. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  25968. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  25969. (add-hook 'speedbar-visiting-tag-hook
  25970. (lambda () (org-show-context 'org-goto)))))
  25971. ;;; Fixes and Hacks
  25972. ;; Make flyspell not check words in links, to not mess up our keymap
  25973. (defun org-mode-flyspell-verify ()
  25974. "Don't let flyspell put overlays at active buttons."
  25975. (not (get-text-property (point) 'keymap)))
  25976. ;; Make `bookmark-jump' show the jump location if it was hidden.
  25977. (eval-after-load "bookmark"
  25978. '(if (boundp 'bookmark-after-jump-hook)
  25979. ;; We can use the hook
  25980. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  25981. ;; Hook not available, use advice
  25982. (defadvice bookmark-jump (after org-make-visible activate)
  25983. "Make the position visible."
  25984. (org-bookmark-jump-unhide))))
  25985. (defun org-bookmark-jump-unhide ()
  25986. "Unhide the current position, to show the bookmark location."
  25987. (and (org-mode-p)
  25988. (or (org-invisible-p)
  25989. (save-excursion (goto-char (max (point-min) (1- (point))))
  25990. (org-invisible-p)))
  25991. (org-show-context 'bookmark-jump)))
  25992. ;; Fix a bug in htmlize where there are text properties (face nil)
  25993. (eval-after-load "htmlize"
  25994. '(progn
  25995. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  25996. "Make sure there are no nil faces"
  25997. (setq ad-return-value (delq nil ad-return-value)))))
  25998. ;; Make session.el ignore our circular variable
  25999. (eval-after-load "session"
  26000. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26001. ;;;; Experimental code
  26002. (defun org-closed-in-range ()
  26003. "Sparse tree of items closed in a certain time range.
  26004. Still experimental, may disappear in the future."
  26005. (interactive)
  26006. ;; Get the time interval from the user.
  26007. (let* ((time1 (time-to-seconds
  26008. (org-read-date nil 'to-time nil "Starting date: ")))
  26009. (time2 (time-to-seconds
  26010. (org-read-date nil 'to-time nil "End date:")))
  26011. ;; callback function
  26012. (callback (lambda ()
  26013. (let ((time
  26014. (time-to-seconds
  26015. (apply 'encode-time
  26016. (org-parse-time-string
  26017. (match-string 1))))))
  26018. ;; check if time in interval
  26019. (and (>= time time1) (<= time time2))))))
  26020. ;; make tree, check each match with the callback
  26021. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26022. ;;;; Finish up
  26023. (provide 'org)
  26024. (run-hooks 'org-load-hook)
  26025. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26026. ;;; org.el ends here