org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759
  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.23a++
  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.23a++"
  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. (defcustom org-load-hook nil
  138. "Hook that is run after org.el has been loaded."
  139. :group 'org
  140. :type 'hook)
  141. (defvar org-modules) ; defined below
  142. (defvar org-modules-loaded nil
  143. "Have the modules been loaded already?")
  144. (defun org-load-modules-maybe (&optional force)
  145. "Load all extensions listed in `org-default-extensions'."
  146. (when (or force (not org-modules-loaded))
  147. (mapc (lambda (ext)
  148. (condition-case nil (require ext)
  149. (error (message "Problems while trying to load feature `%s'" ext))))
  150. org-modules)
  151. (setq org-modules-loaded t)))
  152. (defun org-set-modules (var value)
  153. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  154. (set var value)
  155. (when (featurep 'org)
  156. (org-load-modules-maybe 'force)))
  157. (defcustom org-modules '(org-bbdb org-gnus org-info org-irc org-mhe org-rmail org-vm org-wl)
  158. "Modules that should always be loaded together with org.el.
  159. If the description starts with <A>, this means the extension
  160. will be autoloaded when needed, preloading is not necessary.
  161. If a description starts with <C>, the file is not part of emacs
  162. and loading it will require that you have downloaded and properly installed
  163. the org-mode distribution."
  164. :group 'org
  165. :set 'org-set-modules
  166. :type
  167. '(set :greedy t
  168. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  169. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  170. (const :tag " info: Links to Info nodes" org-info)
  171. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  172. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  173. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  174. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  175. (const :tag " vm: Links to VM folders/messages" org-vm)
  176. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  177. (const :tag " mouse: Additional mouse support" org-mouse)
  178. ; (const :tag "A export-latex: LaTeX export" org-export-latex)
  179. ; (const :tag "A publish: Publishing" org-publish)
  180. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  181. (const :tag "C bibtex: Org links to BibTeX entries" org-bibtex)
  182. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  183. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  184. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  185. (const :tag "C id: Global id's for identifying entries" org-id)
  186. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  187. (const :tag "C iswitchb: Use iswitchb to select Org buffer" org-iswitchb)
  188. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  189. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  190. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  191. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  192. (const :tag "C registry: A registry for Org links" org-registry)
  193. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  194. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  195. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)))
  196. ;; FIXME: Needs a separate group...
  197. (defcustom org-completion-fallback-command 'hippie-expand
  198. "The expansion command called by \\[org-complete] in normal context.
  199. Normal means, no org-mode-specific context."
  200. :group 'org
  201. :type 'function)
  202. (defgroup org-startup nil
  203. "Options concerning startup of Org-mode."
  204. :tag "Org Startup"
  205. :group 'org)
  206. (defcustom org-startup-folded t
  207. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  208. This can also be configured on a per-file basis by adding one of
  209. the following lines anywhere in the buffer:
  210. #+STARTUP: fold
  211. #+STARTUP: nofold
  212. #+STARTUP: content"
  213. :group 'org-startup
  214. :type '(choice
  215. (const :tag "nofold: show all" nil)
  216. (const :tag "fold: overview" t)
  217. (const :tag "content: all headlines" content)))
  218. (defcustom org-startup-truncated t
  219. "Non-nil means, entering Org-mode will set `truncate-lines'.
  220. This is useful since some lines containing links can be very long and
  221. uninteresting. Also tables look terrible when wrapped."
  222. :group 'org-startup
  223. :type 'boolean)
  224. (defcustom org-startup-align-all-tables nil
  225. "Non-nil means, align all tables when visiting a file.
  226. This is useful when the column width in tables is forced with <N> cookies
  227. in table fields. Such tables will look correct only after the first re-align.
  228. This can also be configured on a per-file basis by adding one of
  229. the following lines anywhere in the buffer:
  230. #+STARTUP: align
  231. #+STARTUP: noalign"
  232. :group 'org-startup
  233. :type 'boolean)
  234. (defcustom org-insert-mode-line-in-empty-file nil
  235. "Non-nil means insert the first line setting Org-mode in empty files.
  236. When the function `org-mode' is called interactively in an empty file, this
  237. normally means that the file name does not automatically trigger Org-mode.
  238. To ensure that the file will always be in Org-mode in the future, a
  239. line enforcing Org-mode will be inserted into the buffer, if this option
  240. has been set."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-replace-disputed-keys nil
  244. "Non-nil means use alternative key bindings for some keys.
  245. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  246. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  247. If you want to use Org-mode together with one of these other modes,
  248. or more generally if you would like to move some Org-mode commands to
  249. other keys, set this variable and configure the keys with the variable
  250. `org-disputed-keys'.
  251. This option is only relevant at load-time of Org-mode, and must be set
  252. *before* org.el is loaded. Changing it requires a restart of Emacs to
  253. become effective."
  254. :group 'org-startup
  255. :type 'boolean)
  256. (if (fboundp 'defvaralias)
  257. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  258. (defcustom org-disputed-keys
  259. '(([(shift up)] . [(meta p)])
  260. ([(shift down)] . [(meta n)])
  261. ([(shift left)] . [(meta -)])
  262. ([(shift right)] . [(meta +)])
  263. ([(control shift right)] . [(meta shift +)])
  264. ([(control shift left)] . [(meta shift -)]))
  265. "Keys for which Org-mode and other modes compete.
  266. This is an alist, cars are the default keys, second element specifies
  267. the alternative to use when `org-replace-disputed-keys' is t.
  268. Keys can be specified in any syntax supported by `define-key'.
  269. The value of this option takes effect only at Org-mode's startup,
  270. therefore you'll have to restart Emacs to apply it after changing."
  271. :group 'org-startup
  272. :type 'alist)
  273. (defun org-key (key)
  274. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  275. Or return the original if not disputed."
  276. (if org-replace-disputed-keys
  277. (let* ((nkey (key-description key))
  278. (x (org-find-if (lambda (x)
  279. (equal (key-description (car x)) nkey))
  280. org-disputed-keys)))
  281. (if x (cdr x) key))
  282. key))
  283. (defun org-find-if (predicate seq)
  284. (catch 'exit
  285. (while seq
  286. (if (funcall predicate (car seq))
  287. (throw 'exit (car seq))
  288. (pop seq)))))
  289. (defun org-defkey (keymap key def)
  290. "Define a key, possibly translated, as returned by `org-key'."
  291. (define-key keymap (org-key key) def))
  292. (defcustom org-ellipsis nil
  293. "The ellipsis to use in the Org-mode outline.
  294. When nil, just use the standard three dots. When a string, use that instead,
  295. When a face, use the standart 3 dots, but with the specified face.
  296. The change affects only Org-mode (which will then use its own display table).
  297. Changing this requires executing `M-x org-mode' in a buffer to become
  298. effective."
  299. :group 'org-startup
  300. :type '(choice (const :tag "Default" nil)
  301. (face :tag "Face" :value org-warning)
  302. (string :tag "String" :value "...#")))
  303. (defvar org-display-table nil
  304. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  305. (defgroup org-keywords nil
  306. "Keywords in Org-mode."
  307. :tag "Org Keywords"
  308. :group 'org)
  309. (defcustom org-deadline-string "DEADLINE:"
  310. "String to mark deadline entries.
  311. A deadline is this string, followed by a time stamp. Should be a word,
  312. terminated by a colon. You can insert a schedule keyword and
  313. a timestamp with \\[org-deadline].
  314. Changes become only effective after restarting Emacs."
  315. :group 'org-keywords
  316. :type 'string)
  317. (defcustom org-scheduled-string "SCHEDULED:"
  318. "String to mark scheduled TODO entries.
  319. A schedule is this string, followed by a time stamp. Should be a word,
  320. terminated by a colon. You can insert a schedule keyword and
  321. a timestamp with \\[org-schedule].
  322. Changes become only effective after restarting Emacs."
  323. :group 'org-keywords
  324. :type 'string)
  325. (defcustom org-closed-string "CLOSED:"
  326. "String used as the prefix for timestamps logging closing a TODO entry."
  327. :group 'org-keywords
  328. :type 'string)
  329. (defcustom org-clock-string "CLOCK:"
  330. "String used as prefix for timestamps clocking work hours on an item."
  331. :group 'org-keywords
  332. :type 'string)
  333. (defcustom org-comment-string "COMMENT"
  334. "Entries starting with this keyword will never be exported.
  335. An entry can be toggled between COMMENT and normal with
  336. \\[org-toggle-comment].
  337. Changes become only effective after restarting Emacs."
  338. :group 'org-keywords
  339. :type 'string)
  340. (defcustom org-quote-string "QUOTE"
  341. "Entries starting with this keyword will be exported in fixed-width font.
  342. Quoting applies only to the text in the entry following the headline, and does
  343. not extend beyond the next headline, even if that is lower level.
  344. An entry can be toggled between QUOTE and normal with
  345. \\[org-toggle-fixed-width-section]."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defconst org-repeat-re
  349. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  350. "Regular expression for specifying repeated events.
  351. After a match, group 1 contains the repeat expression.")
  352. (defgroup org-structure nil
  353. "Options concerning the general structure of Org-mode files."
  354. :tag "Org Structure"
  355. :group 'org)
  356. (defgroup org-reveal-location nil
  357. "Options about how to make context of a location visible."
  358. :tag "Org Reveal Location"
  359. :group 'org-structure)
  360. (defconst org-context-choice
  361. '(choice
  362. (const :tag "Always" t)
  363. (const :tag "Never" nil)
  364. (repeat :greedy t :tag "Individual contexts"
  365. (cons
  366. (choice :tag "Context"
  367. (const agenda)
  368. (const org-goto)
  369. (const occur-tree)
  370. (const tags-tree)
  371. (const link-search)
  372. (const mark-goto)
  373. (const bookmark-jump)
  374. (const isearch)
  375. (const default))
  376. (boolean))))
  377. "Contexts for the reveal options.")
  378. (defcustom org-show-hierarchy-above '((default . t))
  379. "Non-nil means, show full hierarchy when revealing a location.
  380. Org-mode often shows locations in an org-mode file which might have
  381. been invisible before. When this is set, the hierarchy of headings
  382. above the exposed location is shown.
  383. Turning this off for example for sparse trees makes them very compact.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. Valid contexts are
  386. agenda when exposing an entry from the agenda
  387. org-goto when using the command `org-goto' on key C-c C-j
  388. occur-tree when using the command `org-occur' on key C-c /
  389. tags-tree when constructing a sparse tree based on tags matches
  390. link-search when exposing search matches associated with a link
  391. mark-goto when exposing the jump goal of a mark
  392. bookmark-jump when exposing a bookmark location
  393. isearch when exiting from an incremental search
  394. default default for all contexts not set explicitly"
  395. :group 'org-reveal-location
  396. :type org-context-choice)
  397. (defcustom org-show-following-heading '((default . nil))
  398. "Non-nil means, show following heading when revealing a location.
  399. Org-mode often shows locations in an org-mode file which might have
  400. been invisible before. When this is set, the heading following the
  401. match is shown.
  402. Turning this off for example for sparse trees makes them very compact,
  403. but makes it harder to edit the location of the match. In such a case,
  404. use the command \\[org-reveal] to show more context.
  405. Instead of t, this can also be an alist specifying this option for different
  406. contexts. See `org-show-hierarchy-above' for valid contexts."
  407. :group 'org-reveal-location
  408. :type org-context-choice)
  409. (defcustom org-show-siblings '((default . nil) (isearch t))
  410. "Non-nil means, show all sibling heading when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the sibling of the current entry
  413. heading are all made visible. If `org-show-hierarchy-above' is t,
  414. the same happens on each level of the hierarchy above the current entry.
  415. By default this is on for the isearch context, off for all other contexts.
  416. Turning this off for example for sparse trees makes them very compact,
  417. but makes it harder to edit the location of the match. In such a case,
  418. use the command \\[org-reveal] to show more context.
  419. Instead of t, this can also be an alist specifying this option for different
  420. contexts. See `org-show-hierarchy-above' for valid contexts."
  421. :group 'org-reveal-location
  422. :type org-context-choice)
  423. (defcustom org-show-entry-below '((default . nil))
  424. "Non-nil means, show the entry below a headline when revealing a location.
  425. Org-mode often shows locations in an org-mode file which might have
  426. been invisible before. When this is set, the text below the headline that is
  427. exposed is also shown.
  428. By default this is off for all contexts.
  429. Instead of t, this can also be an alist specifying this option for different
  430. contexts. See `org-show-hierarchy-above' for valid contexts."
  431. :group 'org-reveal-location
  432. :type org-context-choice)
  433. (defgroup org-cycle nil
  434. "Options concerning visibility cycling in Org-mode."
  435. :tag "Org Cycle"
  436. :group 'org-structure)
  437. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  438. "Names of drawers. Drawers are not opened by cycling on the headline above.
  439. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  440. this:
  441. :DRAWERNAME:
  442. .....
  443. :END:
  444. The drawer \"PROPERTIES\" is special for capturing properties through
  445. the property API.
  446. Drawers can be defined on the per-file basis with a line like:
  447. #+DRAWERS: HIDDEN STATE PROPERTIES"
  448. :group 'org-structure
  449. :type '(repeat (string :tag "Drawer Name")))
  450. (defcustom org-cycle-global-at-bob nil
  451. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  452. This makes it possible to do global cycling without having to use S-TAB or
  453. C-u TAB. For this special case to work, the first line of the buffer
  454. must not be a headline - it may be empty ot some other text. When used in
  455. this way, `org-cycle-hook' is disables temporarily, to make sure the
  456. cursor stays at the beginning of the buffer.
  457. When this option is nil, don't do anything special at the beginning
  458. of the buffer."
  459. :group 'org-cycle
  460. :type 'boolean)
  461. (defcustom org-cycle-emulate-tab t
  462. "Where should `org-cycle' emulate TAB.
  463. nil Never
  464. white Only in completely white lines
  465. whitestart Only at the beginning of lines, before the first non-white char
  466. t Everywhere except in headlines
  467. exc-hl-bol Everywhere except at the start of a headline
  468. If TAB is used in a place where it does not emulate TAB, the current subtree
  469. visibility is cycled."
  470. :group 'org-cycle
  471. :type '(choice (const :tag "Never" nil)
  472. (const :tag "Only in completely white lines" white)
  473. (const :tag "Before first char in a line" whitestart)
  474. (const :tag "Everywhere except in headlines" t)
  475. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  476. ))
  477. (defcustom org-cycle-separator-lines 2
  478. "Number of empty lines needed to keep an empty line between collapsed trees.
  479. If you leave an empty line between the end of a subtree and the following
  480. headline, this empty line is hidden when the subtree is folded.
  481. Org-mode will leave (exactly) one empty line visible if the number of
  482. empty lines is equal or larger to the number given in this variable.
  483. So the default 2 means, at least 2 empty lines after the end of a subtree
  484. are needed to produce free space between a collapsed subtree and the
  485. following headline.
  486. Special case: when 0, never leave empty lines in collapsed view."
  487. :group 'org-cycle
  488. :type 'integer)
  489. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  490. org-cycle-hide-drawers
  491. org-cycle-show-empty-lines
  492. org-optimize-window-after-visibility-change)
  493. "Hook that is run after `org-cycle' has changed the buffer visibility.
  494. The function(s) in this hook must accept a single argument which indicates
  495. the new state that was set by the most recent `org-cycle' command. The
  496. argument is a symbol. After a global state change, it can have the values
  497. `overview', `content', or `all'. After a local state change, it can have
  498. the values `folded', `children', or `subtree'."
  499. :group 'org-cycle
  500. :type 'hook)
  501. (defgroup org-edit-structure nil
  502. "Options concerning structure editing in Org-mode."
  503. :tag "Org Edit Structure"
  504. :group 'org-structure)
  505. (defcustom org-odd-levels-only nil
  506. "Non-nil means, skip even levels and only use odd levels for the outline.
  507. This has the effect that two stars are being added/taken away in
  508. promotion/demotion commands. It also influences how levels are
  509. handled by the exporters.
  510. Changing it requires restart of `font-lock-mode' to become effective
  511. for fontification also in regions already fontified.
  512. You may also set this on a per-file basis by adding one of the following
  513. lines to the buffer:
  514. #+STARTUP: odd
  515. #+STARTUP: oddeven"
  516. :group 'org-edit-structure
  517. :group 'org-font-lock
  518. :type 'boolean)
  519. (defcustom org-adapt-indentation t
  520. "Non-nil means, adapt indentation when promoting and demoting.
  521. When this is set and the *entire* text in an entry is indented, the
  522. indentation is increased by one space in a demotion command, and
  523. decreased by one in a promotion command. If any line in the entry
  524. body starts at column 0, indentation is not changed at all."
  525. :group 'org-edit-structure
  526. :type 'boolean)
  527. (defcustom org-special-ctrl-a/e nil
  528. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  529. When t, `C-a' will bring back the cursor to the beginning of the
  530. headline text, i.e. after the stars and after a possible TODO keyword.
  531. In an item, this will be the position after the bullet.
  532. When the cursor is already at that position, another `C-a' will bring
  533. it to the beginning of the line.
  534. `C-e' will jump to the end of the headline, ignoring the presence of tags
  535. in the headline. A second `C-e' will then jump to the true end of the
  536. line, after any tags.
  537. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  538. and only a directly following, identical keypress will bring the cursor
  539. to the special positions."
  540. :group 'org-edit-structure
  541. :type '(choice
  542. (const :tag "off" nil)
  543. (const :tag "after bullet first" t)
  544. (const :tag "border first" reversed)))
  545. (if (fboundp 'defvaralias)
  546. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  547. (defcustom org-special-ctrl-k nil
  548. "Non-nil means `C-k' will behave specially in headlines.
  549. When nil, `C-k' will call the default `kill-line' command.
  550. When t, the following will happen while the cursor is in the headline:
  551. - When the cursor is at the beginning of a headline, kill the entire
  552. line and possible the folded subtree below the line.
  553. - When in the middle of the headline text, kill the headline up to the tags.
  554. - When after the headline text, kill the tags."
  555. :group 'org-edit-structure
  556. :type 'boolean)
  557. (defcustom org-M-RET-may-split-line '((default . t))
  558. "Non-nil means, M-RET will split the line at the cursor position.
  559. When nil, it will go to the end of the line before making a
  560. new line.
  561. You may also set this option in a different way for different
  562. contexts. Valid contexts are:
  563. headline when creating a new headline
  564. item when creating a new item
  565. table in a table field
  566. default the value to be used for all contexts not explicitly
  567. customized"
  568. :group 'org-structure
  569. :group 'org-table
  570. :type '(choice
  571. (const :tag "Always" t)
  572. (const :tag "Never" nil)
  573. (repeat :greedy t :tag "Individual contexts"
  574. (cons
  575. (choice :tag "Context"
  576. (const headline)
  577. (const item)
  578. (const table)
  579. (const default))
  580. (boolean)))))
  581. (defcustom org-blank-before-new-entry '((heading . nil)
  582. (plain-list-item . nil))
  583. "Should `org-insert-heading' leave a blank line before new heading/item?
  584. The value is an alist, with `heading' and `plain-list-item' as car,
  585. and a boolean flag as cdr."
  586. :group 'org-edit-structure
  587. :type '(list
  588. (cons (const heading) (boolean))
  589. (cons (const plain-list-item) (boolean))))
  590. (defcustom org-insert-heading-hook nil
  591. "Hook being run after inserting a new heading."
  592. :group 'org-edit-structure
  593. :type 'hook)
  594. (defcustom org-enable-fixed-width-editor t
  595. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  596. This currently only means, they are never auto-wrapped.
  597. When nil, such lines will be treated like ordinary lines.
  598. See also the QUOTE keyword."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-goto-auto-isearch t
  602. "Non-nil means, typing characters in org-goto starts incremental search."
  603. :group 'org-edit-structure
  604. :type 'boolean)
  605. (defgroup org-sparse-trees nil
  606. "Options concerning sparse trees in Org-mode."
  607. :tag "Org Sparse Trees"
  608. :group 'org-structure)
  609. (defcustom org-highlight-sparse-tree-matches t
  610. "Non-nil means, highlight all matches that define a sparse tree.
  611. The highlights will automatically disappear the next time the buffer is
  612. changed by an edit command."
  613. :group 'org-sparse-trees
  614. :type 'boolean)
  615. (defcustom org-remove-highlights-with-change t
  616. "Non-nil means, any change to the buffer will remove temporary highlights.
  617. Such highlights are created by `org-occur' and `org-clock-display'.
  618. When nil, `C-c C-c needs to be used to get rid of the highlights.
  619. The highlights created by `org-preview-latex-fragment' always need
  620. `C-c C-c' to be removed."
  621. :group 'org-sparse-trees
  622. :group 'org-time
  623. :type 'boolean)
  624. (defcustom org-occur-hook '(org-first-headline-recenter)
  625. "Hook that is run after `org-occur' has constructed a sparse tree.
  626. This can be used to recenter the window to show as much of the structure
  627. as possible."
  628. :group 'org-sparse-trees
  629. :type 'hook)
  630. (defgroup org-plain-lists nil
  631. "Options concerning plain lists in Org-mode."
  632. :tag "Org Plain lists"
  633. :group 'org-structure)
  634. (defcustom org-cycle-include-plain-lists nil
  635. "Non-nil means, include plain lists into visibility cycling.
  636. This means that during cycling, plain list items will *temporarily* be
  637. interpreted as outline headlines with a level given by 1000+i where i is the
  638. indentation of the bullet. In all other operations, plain list items are
  639. not seen as headlines. For example, you cannot assign a TODO keyword to
  640. such an item."
  641. :group 'org-plain-lists
  642. :type 'boolean)
  643. (defcustom org-plain-list-ordered-item-terminator t
  644. "The character that makes a line with leading number an ordered list item.
  645. Valid values are ?. and ?\). To get both terminators, use t. While
  646. ?. may look nicer, it creates the danger that a line with leading
  647. number may be incorrectly interpreted as an item. ?\) therefore is
  648. the safe choice."
  649. :group 'org-plain-lists
  650. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  651. (const :tag "paren like in \"2)\"" ?\))
  652. (const :tab "both" t)))
  653. (defcustom org-auto-renumber-ordered-lists t
  654. "Non-nil means, automatically renumber ordered plain lists.
  655. Renumbering happens when the sequence have been changed with
  656. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  657. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  658. :group 'org-plain-lists
  659. :type 'boolean)
  660. (defcustom org-provide-checkbox-statistics t
  661. "Non-nil means, update checkbox statistics after insert and toggle.
  662. When this is set, checkbox statistics is updated each time you either insert
  663. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  664. with \\[org-ctrl-c-ctrl-c\\]."
  665. :group 'org-plain-lists
  666. :type 'boolean)
  667. (defgroup org-archive nil
  668. "Options concerning archiving in Org-mode."
  669. :tag "Org Archive"
  670. :group 'org-structure)
  671. (defcustom org-archive-tag "ARCHIVE"
  672. "The tag that marks a subtree as archived.
  673. An archived subtree does not open during visibility cycling, and does
  674. not contribute to the agenda listings.
  675. After changing this, font-lock must be restarted in the relevant buffers to
  676. get the proper fontification."
  677. :group 'org-archive
  678. :group 'org-keywords
  679. :type 'string)
  680. (defcustom org-agenda-skip-archived-trees t
  681. "Non-nil means, the agenda will skip any items located in archived trees.
  682. An archived tree is a tree marked with the tag ARCHIVE."
  683. :group 'org-archive
  684. :group 'org-agenda-skip
  685. :type 'boolean)
  686. (defcustom org-cycle-open-archived-trees nil
  687. "Non-nil means, `org-cycle' will open archived trees.
  688. An archived tree is a tree marked with the tag ARCHIVE.
  689. When nil, archived trees will stay folded. You can still open them with
  690. normal outline commands like `show-all', but not with the cycling commands."
  691. :group 'org-archive
  692. :group 'org-cycle
  693. :type 'boolean)
  694. (defcustom org-sparse-tree-open-archived-trees nil
  695. "Non-nil means sparse tree construction shows matches in archived trees.
  696. When nil, matches in these trees are highlighted, but the trees are kept in
  697. collapsed state."
  698. :group 'org-archive
  699. :group 'org-sparse-trees
  700. :type 'boolean)
  701. (defcustom org-archive-location "%s_archive::"
  702. "The location where subtrees should be archived.
  703. This string consists of two parts, separated by a double-colon.
  704. The first part is a file name - when omitted, archiving happens in the same
  705. file. %s will be replaced by the current file name (without directory part).
  706. Archiving to a different file is useful to keep archived entries from
  707. contributing to the Org-mode Agenda.
  708. The part after the double colon is a headline. The archived entries will be
  709. filed under that headline. When omitted, the subtrees are simply filed away
  710. at the end of the file, as top-level entries.
  711. Here are a few examples:
  712. \"%s_archive::\"
  713. If the current file is Projects.org, archive in file
  714. Projects.org_archive, as top-level trees. This is the default.
  715. \"::* Archived Tasks\"
  716. Archive in the current file, under the top-level headline
  717. \"* Archived Tasks\".
  718. \"~/org/archive.org::\"
  719. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  720. \"basement::** Finished Tasks\"
  721. Archive in file ./basement (relative path), as level 3 trees
  722. below the level 2 heading \"** Finished Tasks\".
  723. You may set this option on a per-file basis by adding to the buffer a
  724. line like
  725. #+ARCHIVE: basement::** Finished Tasks"
  726. :group 'org-archive
  727. :type 'string)
  728. (defcustom org-archive-mark-done t
  729. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  730. This can be a string to set the keyword to use. When t, Org-mode will
  731. use the first keyword in its list that means done."
  732. :group 'org-archive
  733. :type '(choice
  734. (const :tag "No" nil)
  735. (const :tag "Yes" t)
  736. (string :tag "Use this keyword")))
  737. (defcustom org-archive-stamp-time t
  738. "Non-nil means, add a time stamp to entries moved to an archive file.
  739. This variable is obsolete and has no effect anymore, instead add ot remove
  740. `time' from the variablle `org-archive-save-context-info'."
  741. :group 'org-archive
  742. :type 'boolean)
  743. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  744. "Parts of context info that should be stored as properties when archiving.
  745. When a subtree is moved to an archive file, it looses information given by
  746. context, like inherited tags, the category, and possibly also the TODO
  747. state (depending on the variable `org-archive-mark-done').
  748. This variable can be a list of any of the following symbols:
  749. time The time of archiving.
  750. file The file where the entry originates.
  751. itags The local tags, in the headline of the subtree.
  752. ltags The tags the subtree inherits from further up the hierarchy.
  753. todo The pre-archive TODO state.
  754. category The category, taken from file name or #+CATEGORY lines.
  755. olpath The outline path to the item. These are all headlines above
  756. the current item, separated by /, like a file path.
  757. For each symbol present in the list, a property will be created in
  758. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  759. information."
  760. :group 'org-archive
  761. :type '(set :greedy t
  762. (const :tag "Time" time)
  763. (const :tag "File" file)
  764. (const :tag "Category" category)
  765. (const :tag "TODO state" todo)
  766. (const :tag "TODO state" priority)
  767. (const :tag "Inherited tags" itags)
  768. (const :tag "Outline path" olpath)
  769. (const :tag "Local tags" ltags)))
  770. (defgroup org-imenu-and-speedbar nil
  771. "Options concerning imenu and speedbar in Org-mode."
  772. :tag "Org Imenu and Speedbar"
  773. :group 'org-structure)
  774. (defcustom org-imenu-depth 2
  775. "The maximum level for Imenu access to Org-mode headlines.
  776. This also applied for speedbar access."
  777. :group 'org-imenu-and-speedbar
  778. :type 'number)
  779. (defgroup org-table nil
  780. "Options concerning tables in Org-mode."
  781. :tag "Org Table"
  782. :group 'org)
  783. (defcustom org-enable-table-editor 'optimized
  784. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  785. When nil, such lines will be treated like ordinary lines.
  786. When equal to the symbol `optimized', the table editor will be optimized to
  787. do the following:
  788. - Automatic overwrite mode in front of whitespace in table fields.
  789. This makes the structure of the table stay in tact as long as the edited
  790. field does not exceed the column width.
  791. - Minimize the number of realigns. Normally, the table is aligned each time
  792. TAB or RET are pressed to move to another field. With optimization this
  793. happens only if changes to a field might have changed the column width.
  794. Optimization requires replacing the functions `self-insert-command',
  795. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  796. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  797. very good at guessing when a re-align will be necessary, but you can always
  798. force one with \\[org-ctrl-c-ctrl-c].
  799. If you would like to use the optimized version in Org-mode, but the
  800. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  801. This variable can be used to turn on and off the table editor during a session,
  802. but in order to toggle optimization, a restart is required.
  803. See also the variable `org-table-auto-blank-field'."
  804. :group 'org-table
  805. :type '(choice
  806. (const :tag "off" nil)
  807. (const :tag "on" t)
  808. (const :tag "on, optimized" optimized)))
  809. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  810. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  811. In the optimized version, the table editor takes over all simple keys that
  812. normally just insert a character. In tables, the characters are inserted
  813. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  814. for empty fields). Outside tables, the correct binding of the keys is
  815. restored.
  816. The default for this option is t if the optimized version is also used in
  817. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  818. this variable requires a restart of Emacs to become effective."
  819. :group 'org-table
  820. :type 'boolean)
  821. (defcustom orgtbl-radio-table-templates
  822. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  823. % END RECEIVE ORGTBL %n
  824. \\begin{comment}
  825. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  826. | | |
  827. \\end{comment}\n")
  828. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  829. @c END RECEIVE ORGTBL %n
  830. @ignore
  831. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  832. | | |
  833. @end ignore\n")
  834. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  835. <!-- END RECEIVE ORGTBL %n -->
  836. <!--
  837. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  838. | | |
  839. -->\n"))
  840. "Templates for radio tables in different major modes.
  841. All occurrences of %n in a template will be replaced with the name of the
  842. table, obtained by prompting the user."
  843. :group 'org-table
  844. :type '(repeat
  845. (list (symbol :tag "Major mode")
  846. (string :tag "Format"))))
  847. (defgroup org-table-settings nil
  848. "Settings for tables in Org-mode."
  849. :tag "Org Table Settings"
  850. :group 'org-table)
  851. (defcustom org-table-default-size "5x2"
  852. "The default size for newly created tables, Columns x Rows."
  853. :group 'org-table-settings
  854. :type 'string)
  855. (defcustom org-table-number-regexp
  856. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  857. "Regular expression for recognizing numbers in table columns.
  858. If a table column contains mostly numbers, it will be aligned to the
  859. right. If not, it will be aligned to the left.
  860. The default value of this option is a regular expression which allows
  861. anything which looks remotely like a number as used in scientific
  862. context. For example, all of the following will be considered a
  863. number:
  864. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  865. Other options offered by the customize interface are more restrictive."
  866. :group 'org-table-settings
  867. :type '(choice
  868. (const :tag "Positive Integers"
  869. "^[0-9]+$")
  870. (const :tag "Integers"
  871. "^[-+]?[0-9]+$")
  872. (const :tag "Floating Point Numbers"
  873. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  874. (const :tag "Floating Point Number or Integer"
  875. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  876. (const :tag "Exponential, Floating point, Integer"
  877. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  878. (const :tag "Very General Number-Like, including hex"
  879. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  880. (string :tag "Regexp:")))
  881. (defcustom org-table-number-fraction 0.5
  882. "Fraction of numbers in a column required to make the column align right.
  883. In a column all non-white fields are considered. If at least this
  884. fraction of fields is matched by `org-table-number-fraction',
  885. alignment to the right border applies."
  886. :group 'org-table-settings
  887. :type 'number)
  888. (defgroup org-table-editing nil
  889. "Behavior of tables during editing in Org-mode."
  890. :tag "Org Table Editing"
  891. :group 'org-table)
  892. (defcustom org-table-automatic-realign t
  893. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  894. When nil, aligning is only done with \\[org-table-align], or after column
  895. removal/insertion."
  896. :group 'org-table-editing
  897. :type 'boolean)
  898. (defcustom org-table-auto-blank-field t
  899. "Non-nil means, automatically blank table field when starting to type into it.
  900. This only happens when typing immediately after a field motion
  901. command (TAB, S-TAB or RET).
  902. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  903. :group 'org-table-editing
  904. :type 'boolean)
  905. (defcustom org-table-tab-jumps-over-hlines t
  906. "Non-nil means, tab in the last column of a table with jump over a hline.
  907. If a horizontal separator line is following the current line,
  908. `org-table-next-field' can either create a new row before that line, or jump
  909. over the line. When this option is nil, a new line will be created before
  910. this line."
  911. :group 'org-table-editing
  912. :type 'boolean)
  913. (defcustom org-table-tab-recognizes-table.el t
  914. "Non-nil means, TAB will automatically notice a table.el table.
  915. When it sees such a table, it moves point into it and - if necessary -
  916. calls `table-recognize-table'."
  917. :group 'org-table-editing
  918. :type 'boolean)
  919. (defgroup org-table-calculation nil
  920. "Options concerning tables in Org-mode."
  921. :tag "Org Table Calculation"
  922. :group 'org-table)
  923. (defcustom org-table-use-standard-references t
  924. "Should org-mode work with table refrences like B3 instead of @3$2?
  925. Possible values are:
  926. nil never use them
  927. from accept as input, do not present for editing
  928. t: accept as input and present for editing"
  929. :group 'org-table-calculation
  930. :type '(choice
  931. (const :tag "Never, don't even check unser input for them" nil)
  932. (const :tag "Always, both as user input, and when editing" t)
  933. (const :tag "Convert user input, don't offer during editing" 'from)))
  934. (defcustom org-table-copy-increment t
  935. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  936. :group 'org-table-calculation
  937. :type 'boolean)
  938. (defcustom org-calc-default-modes
  939. '(calc-internal-prec 12
  940. calc-float-format (float 5)
  941. calc-angle-mode deg
  942. calc-prefer-frac nil
  943. calc-symbolic-mode nil
  944. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  945. calc-display-working-message t
  946. )
  947. "List with Calc mode settings for use in calc-eval for table formulas.
  948. The list must contain alternating symbols (Calc modes variables and values).
  949. Don't remove any of the default settings, just change the values. Org-mode
  950. relies on the variables to be present in the list."
  951. :group 'org-table-calculation
  952. :type 'plist)
  953. (defcustom org-table-formula-evaluate-inline t
  954. "Non-nil means, TAB and RET evaluate a formula in current table field.
  955. If the current field starts with an equal sign, it is assumed to be a formula
  956. which should be evaluated as described in the manual and in the documentation
  957. string of the command `org-table-eval-formula'. This feature requires the
  958. Emacs calc package.
  959. When this variable is nil, formula calculation is only available through
  960. the command \\[org-table-eval-formula]."
  961. :group 'org-table-calculation
  962. :type 'boolean)
  963. (defcustom org-table-formula-use-constants t
  964. "Non-nil means, interpret constants in formulas in tables.
  965. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  966. by the value given in `org-table-formula-constants', or by a value obtained
  967. from the `constants.el' package."
  968. :group 'org-table-calculation
  969. :type 'boolean)
  970. (defcustom org-table-formula-constants nil
  971. "Alist with constant names and values, for use in table formulas.
  972. The car of each element is a name of a constant, without the `$' before it.
  973. The cdr is the value as a string. For example, if you'd like to use the
  974. speed of light in a formula, you would configure
  975. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  976. and then use it in an equation like `$1*$c'.
  977. Constants can also be defined on a per-file basis using a line like
  978. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  979. :group 'org-table-calculation
  980. :type '(repeat
  981. (cons (string :tag "name")
  982. (string :tag "value"))))
  983. (defvar org-table-formula-constants-local nil
  984. "Local version of `org-table-formula-constants'.")
  985. (make-variable-buffer-local 'org-table-formula-constants-local)
  986. (defcustom org-table-allow-automatic-line-recalculation t
  987. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  988. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  989. :group 'org-table-calculation
  990. :type 'boolean)
  991. (defgroup org-link nil
  992. "Options concerning links in Org-mode."
  993. :tag "Org Link"
  994. :group 'org)
  995. (defvar org-link-abbrev-alist-local nil
  996. "Buffer-local version of `org-link-abbrev-alist', which see.
  997. The value of this is taken from the #+LINK lines.")
  998. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  999. (defcustom org-link-abbrev-alist nil
  1000. "Alist of link abbreviations.
  1001. The car of each element is a string, to be replaced at the start of a link.
  1002. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1003. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1004. [[linkkey:tag][description]]
  1005. If REPLACE is a string, the tag will simply be appended to create the link.
  1006. If the string contains \"%s\", the tag will be inserted there.
  1007. REPLACE may also be a function that will be called with the tag as the
  1008. only argument to create the link, which should be returned as a string.
  1009. See the manual for examples."
  1010. :group 'org-link
  1011. :type 'alist)
  1012. (defcustom org-descriptive-links t
  1013. "Non-nil means, hide link part and only show description of bracket links.
  1014. Bracket links are like [[link][descritpion]]. This variable sets the initial
  1015. state in new org-mode buffers. The setting can then be toggled on a
  1016. per-buffer basis from the Org->Hyperlinks menu."
  1017. :group 'org-link
  1018. :type 'boolean)
  1019. (defcustom org-link-file-path-type 'adaptive
  1020. "How the path name in file links should be stored.
  1021. Valid values are:
  1022. relative Relative to the current directory, i.e. the directory of the file
  1023. into which the link is being inserted.
  1024. absolute Absolute path, if possible with ~ for home directory.
  1025. noabbrev Absolute path, no abbreviation of home directory.
  1026. adaptive Use relative path for files in the current directory and sub-
  1027. directories of it. For other files, use an absolute path."
  1028. :group 'org-link
  1029. :type '(choice
  1030. (const relative)
  1031. (const absolute)
  1032. (const noabbrev)
  1033. (const adaptive)))
  1034. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1035. "Types of links that should be activated in Org-mode files.
  1036. This is a list of symbols, each leading to the activation of a certain link
  1037. type. In principle, it does not hurt to turn on most link types - there may
  1038. be a small gain when turning off unused link types. The types are:
  1039. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1040. angular Links in angular brackes that may contain whitespace like
  1041. <bbdb:Carsten Dominik>.
  1042. plain Plain links in normal text, no whitespace, like http://google.com.
  1043. radio Text that is matched by a radio target, see manual for details.
  1044. tag Tag settings in a headline (link to tag search).
  1045. date Time stamps (link to calendar).
  1046. Changing this variable requires a restart of Emacs to become effective."
  1047. :group 'org-link
  1048. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1049. (const :tag "Angular bracket links (old style)" angular)
  1050. (const :tag "Plain text links" plain)
  1051. (const :tag "Radio target matches" radio)
  1052. (const :tag "Tags" tag)
  1053. (const :tag "Timestamps" date)))
  1054. (defgroup org-link-store nil
  1055. "Options concerning storing links in Org-mode."
  1056. :tag "Org Store Link"
  1057. :group 'org-link)
  1058. (defcustom org-email-link-description-format "Email %c: %.30s"
  1059. "Format of the description part of a link to an email or usenet message.
  1060. The following %-excapes will be replaced by corresponding information:
  1061. %F full \"From\" field
  1062. %f name, taken from \"From\" field, address if no name
  1063. %T full \"To\" field
  1064. %t first name in \"To\" field, address if no name
  1065. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1066. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1067. %s subject
  1068. %m message-id.
  1069. You may use normal field width specification between the % and the letter.
  1070. This is for example useful to limit the length of the subject.
  1071. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1072. :group 'org-link-store
  1073. :type 'string)
  1074. (defcustom org-from-is-user-regexp
  1075. (let (r1 r2)
  1076. (when (and user-mail-address (not (string= user-mail-address "")))
  1077. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1078. (when (and user-full-name (not (string= user-full-name "")))
  1079. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1080. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1081. "Regexp mached against the \"From:\" header of an email or usenet message.
  1082. It should match if the message is from the user him/herself."
  1083. :group 'org-link-store
  1084. :type 'regexp)
  1085. (defcustom org-context-in-file-links t
  1086. "Non-nil means, file links from `org-store-link' contain context.
  1087. A search string will be added to the file name with :: as separator and
  1088. used to find the context when the link is activated by the command
  1089. `org-open-at-point'.
  1090. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1091. negates this setting for the duration of the command."
  1092. :group 'org-link-store
  1093. :type 'boolean)
  1094. (defcustom org-keep-stored-link-after-insertion nil
  1095. "Non-nil means, keep link in list for entire session.
  1096. The command `org-store-link' adds a link pointing to the current
  1097. location to an internal list. These links accumulate during a session.
  1098. The command `org-insert-link' can be used to insert links into any
  1099. Org-mode file (offering completion for all stored links). When this
  1100. option is nil, every link which has been inserted once using \\[org-insert-link]
  1101. will be removed from the list, to make completing the unused links
  1102. more efficient."
  1103. :group 'org-link-store
  1104. :type 'boolean)
  1105. (defgroup org-link-follow nil
  1106. "Options concerning following links in Org-mode."
  1107. :tag "Org Follow Link"
  1108. :group 'org-link)
  1109. (defcustom org-follow-link-hook nil
  1110. "Hook that is run after a link has been followed."
  1111. :group 'org-link-follow
  1112. :type 'hook)
  1113. (defcustom org-tab-follows-link nil
  1114. "Non-nil means, on links TAB will follow the link.
  1115. Needs to be set before org.el is loaded."
  1116. :group 'org-link-follow
  1117. :type 'boolean)
  1118. (defcustom org-return-follows-link nil
  1119. "Non-nil means, on links RET will follow the link.
  1120. Needs to be set before org.el is loaded."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-mouse-1-follows-link
  1124. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1125. "Non-nil means, mouse-1 on a link will follow the link.
  1126. A longer mouse click will still set point. Does not work on XEmacs.
  1127. Needs to be set before org.el is loaded."
  1128. :group 'org-link-follow
  1129. :type 'boolean)
  1130. (defcustom org-mark-ring-length 4
  1131. "Number of different positions to be recorded in the ring
  1132. Changing this requires a restart of Emacs to work correctly."
  1133. :group 'org-link-follow
  1134. :type 'interger)
  1135. (defcustom org-link-frame-setup
  1136. '((vm . vm-visit-folder-other-frame)
  1137. (gnus . gnus-other-frame)
  1138. (file . find-file-other-window))
  1139. "Setup the frame configuration for following links.
  1140. When following a link with Emacs, it may often be useful to display
  1141. this link in another window or frame. This variable can be used to
  1142. set this up for the different types of links.
  1143. For VM, use any of
  1144. `vm-visit-folder'
  1145. `vm-visit-folder-other-frame'
  1146. For Gnus, use any of
  1147. `gnus'
  1148. `gnus-other-frame'
  1149. For FILE, use any of
  1150. `find-file'
  1151. `find-file-other-window'
  1152. `find-file-other-frame'
  1153. For the calendar, use the variable `calendar-setup'.
  1154. For BBDB, it is currently only possible to display the matches in
  1155. another window."
  1156. :group 'org-link-follow
  1157. :type '(list
  1158. (cons (const vm)
  1159. (choice
  1160. (const vm-visit-folder)
  1161. (const vm-visit-folder-other-window)
  1162. (const vm-visit-folder-other-frame)))
  1163. (cons (const gnus)
  1164. (choice
  1165. (const gnus)
  1166. (const gnus-other-frame)))
  1167. (cons (const file)
  1168. (choice
  1169. (const find-file)
  1170. (const find-file-other-window)
  1171. (const find-file-other-frame)))))
  1172. (defcustom org-display-internal-link-with-indirect-buffer nil
  1173. "Non-nil means, use indirect buffer to display infile links.
  1174. Activating internal links (from one location in a file to another location
  1175. in the same file) normally just jumps to the location. When the link is
  1176. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1177. another window. When this option is set, the other window actually displays
  1178. an indirect buffer clone of the current buffer, to avoid any visibility
  1179. changes to the current buffer."
  1180. :group 'org-link-follow
  1181. :type 'boolean)
  1182. (defcustom org-open-non-existing-files nil
  1183. "Non-nil means, `org-open-file' will open non-existing files.
  1184. When nil, an error will be generated."
  1185. :group 'org-link-follow
  1186. :type 'boolean)
  1187. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1188. "Function and arguments to call for following mailto links.
  1189. This is a list with the first element being a lisp function, and the
  1190. remaining elements being arguments to the function. In string arguments,
  1191. %a will be replaced by the address, and %s will be replaced by the subject
  1192. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1193. :group 'org-link-follow
  1194. :type '(choice
  1195. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1196. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1197. (const :tag "message-mail" (message-mail "%a" "%s"))
  1198. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1199. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1200. "Non-nil means, ask for confirmation before executing shell links.
  1201. Shell links can be dangerous: just think about a link
  1202. [[shell:rm -rf ~/*][Google Search]]
  1203. This link would show up in your Org-mode document as \"Google Search\",
  1204. but really it would remove your entire home directory.
  1205. Therefore we advise against setting this variable to nil.
  1206. Just change it to `y-or-n-p' of you want to confirm with a
  1207. single keystroke rather than having to type \"yes\"."
  1208. :group 'org-link-follow
  1209. :type '(choice
  1210. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1211. (const :tag "with y-or-n (faster)" y-or-n-p)
  1212. (const :tag "no confirmation (dangerous)" nil)))
  1213. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1214. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1215. Elisp links can be dangerous: just think about a link
  1216. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1217. This link would show up in your Org-mode document as \"Google Search\",
  1218. but really it would remove your entire home directory.
  1219. Therefore we advise against setting this variable to nil.
  1220. Just change it to `y-or-n-p' of you want to confirm with a
  1221. single keystroke rather than having to type \"yes\"."
  1222. :group 'org-link-follow
  1223. :type '(choice
  1224. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1225. (const :tag "with y-or-n (faster)" y-or-n-p)
  1226. (const :tag "no confirmation (dangerous)" nil)))
  1227. (defconst org-file-apps-defaults-gnu
  1228. '((remote . emacs)
  1229. (t . mailcap))
  1230. "Default file applications on a UNIX or GNU/Linux system.
  1231. See `org-file-apps'.")
  1232. (defconst org-file-apps-defaults-macosx
  1233. '((remote . emacs)
  1234. (t . "open %s")
  1235. ("ps" . "gv %s")
  1236. ("ps.gz" . "gv %s")
  1237. ("eps" . "gv %s")
  1238. ("eps.gz" . "gv %s")
  1239. ("dvi" . "xdvi %s")
  1240. ("fig" . "xfig %s"))
  1241. "Default file applications on a MacOS X system.
  1242. The system \"open\" is known as a default, but we use X11 applications
  1243. for some files for which the OS does not have a good default.
  1244. See `org-file-apps'.")
  1245. (defconst org-file-apps-defaults-windowsnt
  1246. (list
  1247. '(remote . emacs)
  1248. (cons t
  1249. (list (if (featurep 'xemacs)
  1250. 'mswindows-shell-execute
  1251. 'w32-shell-execute)
  1252. "open" 'file)))
  1253. "Default file applications on a Windows NT system.
  1254. The system \"open\" is used for most files.
  1255. See `org-file-apps'.")
  1256. (defcustom org-file-apps
  1257. '(
  1258. ("txt" . emacs)
  1259. ("tex" . emacs)
  1260. ("ltx" . emacs)
  1261. ("org" . emacs)
  1262. ("el" . emacs)
  1263. ("bib" . emacs)
  1264. )
  1265. "External applications for opening `file:path' items in a document.
  1266. Org-mode uses system defaults for different file types, but
  1267. you can use this variable to set the application for a given file
  1268. extension. The entries in this list are cons cells where the car identifies
  1269. files and the cdr the corresponding command. Possible values for the
  1270. file identifier are
  1271. \"ext\" A string identifying an extension
  1272. `directory' Matches a directory
  1273. `remote' Matches a remote file, accessible through tramp or efs.
  1274. Remote files most likely should be visited through Emacs
  1275. because external applications cannot handle such paths.
  1276. t Default for all remaining files
  1277. Possible values for the command are:
  1278. `emacs' The file will be visited by the current Emacs process.
  1279. `default' Use the default application for this file type.
  1280. string A command to be executed by a shell; %s will be replaced
  1281. by the path to the file.
  1282. sexp A Lisp form which will be evaluated. The file path will
  1283. be available in the Lisp variable `file'.
  1284. For more examples, see the system specific constants
  1285. `org-file-apps-defaults-macosx'
  1286. `org-file-apps-defaults-windowsnt'
  1287. `org-file-apps-defaults-gnu'."
  1288. :group 'org-link-follow
  1289. :type '(repeat
  1290. (cons (choice :value ""
  1291. (string :tag "Extension")
  1292. (const :tag "Default for unrecognized files" t)
  1293. (const :tag "Remote file" remote)
  1294. (const :tag "Links to a directory" directory))
  1295. (choice :value ""
  1296. (const :tag "Visit with Emacs" emacs)
  1297. (const :tag "Use system default" default)
  1298. (string :tag "Command")
  1299. (sexp :tag "Lisp form")))))
  1300. (defgroup org-remember nil
  1301. "Options concerning interaction with remember.el."
  1302. :tag "Org Remember"
  1303. :group 'org)
  1304. (defcustom org-directory "~/org"
  1305. "Directory with org files.
  1306. This directory will be used as default to prompt for org files.
  1307. Used by the hooks for remember.el."
  1308. :group 'org-remember
  1309. :type 'directory)
  1310. (defcustom org-default-notes-file "~/.notes"
  1311. "Default target for storing notes.
  1312. Used by the hooks for remember.el. This can be a string, or nil to mean
  1313. the value of `remember-data-file'.
  1314. You can set this on a per-template basis with the variable
  1315. `org-remember-templates'."
  1316. :group 'org-remember
  1317. :type '(choice
  1318. (const :tag "Default from remember-data-file" nil)
  1319. file))
  1320. (defcustom org-remember-store-without-prompt t
  1321. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1322. In this case, you need `C-u C-c C-c' to get the prompts for
  1323. note file and headline.
  1324. When this variable is nil, `C-c C-c' give you the prompts, and
  1325. `C-u C-c C-c' trigger the fasttrack."
  1326. :group 'org-remember
  1327. :type 'boolean)
  1328. (defcustom org-remember-interactive-interface 'refile
  1329. "The interface to be used for interactive filing of remember notes.
  1330. This is only used when the interactive mode for selecting a filing
  1331. location is used (see the variable `org-remember-store-without-prompt').
  1332. Allowed vaues are:
  1333. outline The interface shows an outline of the relevant file
  1334. and the correct heading is found by moving through
  1335. the outline or by searching with incremental search.
  1336. outline-path-completion Headlines in the current buffer are offered via
  1337. completion.
  1338. refile Use the refile interface, and offer headlines,
  1339. possibly from different buffers."
  1340. :group 'org-remember
  1341. :type '(choice
  1342. (const :tag "Refile" refile)
  1343. (const :tag "Outline" outline)
  1344. (const :tag "Outline-path-completion" outline-path-completion)))
  1345. (defcustom org-goto-interface 'outline
  1346. "The default interface to be used for `org-goto'.
  1347. Allowed vaues are:
  1348. outline The interface shows an outline of the relevant file
  1349. and the correct heading is found by moving through
  1350. the outline or by searching with incremental search.
  1351. outline-path-completion Headlines in the current buffer are offered via
  1352. completion."
  1353. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1354. :type '(choice
  1355. (const :tag "Outline" outline)
  1356. (const :tag "Outline-path-completion" outline-path-completion)))
  1357. (defcustom org-remember-default-headline ""
  1358. "The headline that should be the default location in the notes file.
  1359. When filing remember notes, the cursor will start at that position.
  1360. You can set this on a per-template basis with the variable
  1361. `org-remember-templates'."
  1362. :group 'org-remember
  1363. :type 'string)
  1364. (defcustom org-remember-templates nil
  1365. "Templates for the creation of remember buffers.
  1366. When nil, just let remember make the buffer.
  1367. When not nil, this is a list of 5-element lists. In each entry, the first
  1368. element is the name of the template, which should be a single short word.
  1369. The second element is a character, a unique key to select this template.
  1370. The third element is the template. The fourth element is optional and can
  1371. specify a destination file for remember items created with this template.
  1372. The default file is given by `org-default-notes-file'. An optional fifth
  1373. element can specify the headline in that file that should be offered
  1374. first when the user is asked to file the entry. The default headline is
  1375. given in the variable `org-remember-default-headline'.
  1376. An optional sixth element specifies the contexts in which the user can
  1377. select the template. This element can be either a list of major modes
  1378. or a function. `org-remember' will first check whether the function
  1379. returns `t' or if we are in any of the listed major modes, and select
  1380. the template accordingly.
  1381. The template specifies the structure of the remember buffer. It should have
  1382. a first line starting with a star, to act as the org-mode headline.
  1383. Furthermore, the following %-escapes will be replaced with content:
  1384. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1385. A default value and a completion table ca be specified like this:
  1386. %^{prompt|default|completion2|completion3|...}
  1387. %t time stamp, date only
  1388. %T time stamp with date and time
  1389. %u, %U like the above, but inactive time stamps
  1390. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1391. You may define a prompt like %^{Please specify birthday}t
  1392. %n user name (taken from `user-full-name')
  1393. %a annotation, normally the link created with org-store-link
  1394. %i initial content, the region active. If %i is indented,
  1395. the entire inserted text will be indented as well.
  1396. %c content of the clipboard, or current kill ring head
  1397. %^g prompt for tags, with completion on tags in target file
  1398. %^G prompt for tags, with completion all tags in all agenda files
  1399. %:keyword specific information for certain link types, see below
  1400. %[pathname] insert the contents of the file given by `pathname'
  1401. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1402. %! Store this note immediately after filling the template
  1403. %? After completing the template, position cursor here.
  1404. Apart from these general escapes, you can access information specific to the
  1405. link type that is created. For example, calling `remember' in emails or gnus
  1406. will record the author and the subject of the message, which you can access
  1407. with %:author and %:subject, respectively. Here is a complete list of what
  1408. is recorded for each link type.
  1409. Link type | Available information
  1410. -------------------+------------------------------------------------------
  1411. bbdb | %:type %:name %:company
  1412. vm, wl, mh, rmail | %:type %:subject %:message-id
  1413. | %:from %:fromname %:fromaddress
  1414. | %:to %:toname %:toaddress
  1415. | %:fromto (either \"to NAME\" or \"from NAME\")
  1416. gnus | %:group, for messages also all email fields
  1417. w3, w3m | %:type %:url
  1418. info | %:type %:file %:node
  1419. calendar | %:type %:date"
  1420. :group 'org-remember
  1421. :get (lambda (var) ; Make sure all entries have at least 5 elements
  1422. (mapcar (lambda (x)
  1423. (if (not (stringp (car x))) (setq x (cons "" x)))
  1424. (cond ((= (length x) 4) (append x '("")))
  1425. ((= (length x) 3) (append x '("" "")))
  1426. (t x)))
  1427. (default-value var)))
  1428. :type '(repeat
  1429. :tag "enabled"
  1430. (list :value ("" ?a "\n" nil nil nil)
  1431. (string :tag "Name")
  1432. (character :tag "Selection Key")
  1433. (string :tag "Template")
  1434. (choice
  1435. (file :tag "Destination file")
  1436. (const :tag "Prompt for file" nil))
  1437. (choice
  1438. (string :tag "Destination headline")
  1439. (const :tag "Selection interface for heading"))
  1440. (choice
  1441. (const :tag "Use by default" nil)
  1442. (const :tag "Use in all contexts" t)
  1443. (repeat :tag "Use only if in major mode"
  1444. (symbol :tag "Major mode"))
  1445. (function :tag "Perform a check against function")))))
  1446. (defcustom org-reverse-note-order nil
  1447. "Non-nil means, store new notes at the beginning of a file or entry.
  1448. When nil, new notes will be filed to the end of a file or entry.
  1449. This can also be a list with cons cells of regular expressions that
  1450. are matched against file names, and values."
  1451. :group 'org-remember
  1452. :type '(choice
  1453. (const :tag "Reverse always" t)
  1454. (const :tag "Reverse never" nil)
  1455. (repeat :tag "By file name regexp"
  1456. (cons regexp boolean))))
  1457. (defcustom org-refile-targets nil
  1458. "Targets for refiling entries with \\[org-refile].
  1459. This is list of cons cells. Each cell contains:
  1460. - a specification of the files to be considered, either a list of files,
  1461. or a symbol whose function or value fields will be used to retrieve
  1462. a file name or a list of file names. Nil means, refile to a different
  1463. heading in the current buffer.
  1464. - A specification of how to find candidate refile targets. This may be
  1465. any of
  1466. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1467. This tag has to be present in all target headlines, inheritance will
  1468. not be considered.
  1469. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1470. todo keyword.
  1471. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1472. headlines that are refiling targets.
  1473. - a cons cell (:level . N). Any headline of level N is considered a target.
  1474. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1475. ;; FIXME: what if there are a var and func with same name???
  1476. :group 'org-remember
  1477. :type '(repeat
  1478. (cons
  1479. (choice :value org-agenda-files
  1480. (const :tag "All agenda files" org-agenda-files)
  1481. (const :tag "Current buffer" nil)
  1482. (function) (variable) (file))
  1483. (choice :tag "Identify target headline by"
  1484. (cons :tag "Specific tag" (const :tag) (string))
  1485. (cons :tag "TODO keyword" (const :todo) (string))
  1486. (cons :tag "Regular expression" (const :regexp) (regexp))
  1487. (cons :tag "Level number" (const :level) (integer))
  1488. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1489. (defcustom org-refile-use-outline-path nil
  1490. "Non-nil means, provide refile targets as paths.
  1491. So a level 3 headline will be available as level1/level2/level3.
  1492. When the value is `file', also include the file name (without directory)
  1493. into the path. When `full-file-path', include the full file path."
  1494. :group 'org-remember
  1495. :type '(choice
  1496. (const :tag "Not" nil)
  1497. (const :tag "Yes" t)
  1498. (const :tag "Start with file name" file)
  1499. (const :tag "Start with full file path" full-file-path)))
  1500. (defgroup org-todo nil
  1501. "Options concerning TODO items in Org-mode."
  1502. :tag "Org TODO"
  1503. :group 'org)
  1504. (defgroup org-progress nil
  1505. "Options concerning Progress logging in Org-mode."
  1506. :tag "Org Progress"
  1507. :group 'org-time)
  1508. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1509. "List of TODO entry keyword sequences and their interpretation.
  1510. \\<org-mode-map>This is a list of sequences.
  1511. Each sequence starts with a symbol, either `sequence' or `type',
  1512. indicating if the keywords should be interpreted as a sequence of
  1513. action steps, or as different types of TODO items. The first
  1514. keywords are states requiring action - these states will select a headline
  1515. for inclusion into the global TODO list Org-mode produces. If one of
  1516. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1517. signify that no further action is necessary. If \"|\" is not found,
  1518. the last keyword is treated as the only DONE state of the sequence.
  1519. The command \\[org-todo] cycles an entry through these states, and one
  1520. additional state where no keyword is present. For details about this
  1521. cycling, see the manual.
  1522. TODO keywords and interpretation can also be set on a per-file basis with
  1523. the special #+SEQ_TODO and #+TYP_TODO lines.
  1524. Each keyword can optionally specify a character for fast state selection
  1525. \(in combination with the variable `org-use-fast-todo-selection')
  1526. and specifiers for state change logging, using the same syntax
  1527. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1528. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1529. indicates to record a time stamp each time this state is selected.
  1530. Each keyword may also specify if a timestamp or a note should be
  1531. recorded when entering or leaving the state, by adding additional
  1532. characters in the parenthesis after the keyword. This looks like this:
  1533. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1534. record only the time of the state change. With X and Y being either
  1535. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1536. Y when leaving the state if and only if the *target* state does not
  1537. define X. You may omit any of the fast-selection key or X or /Y,
  1538. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1539. For backward compatibility, this variable may also be just a list
  1540. of keywords - in this case the interptetation (sequence or type) will be
  1541. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1542. :group 'org-todo
  1543. :group 'org-keywords
  1544. :type '(choice
  1545. (repeat :tag "Old syntax, just keywords"
  1546. (string :tag "Keyword"))
  1547. (repeat :tag "New syntax"
  1548. (cons
  1549. (choice
  1550. :tag "Interpretation"
  1551. (const :tag "Sequence (cycling hits every state)" sequence)
  1552. (const :tag "Type (cycling directly to DONE)" type))
  1553. (repeat
  1554. (string :tag "Keyword"))))))
  1555. (defvar org-todo-keywords-1 nil
  1556. "All TODO and DONE keywords active in a buffer.")
  1557. (make-variable-buffer-local 'org-todo-keywords-1)
  1558. (defvar org-todo-keywords-for-agenda nil)
  1559. (defvar org-done-keywords-for-agenda nil)
  1560. (defvar org-not-done-keywords nil)
  1561. (make-variable-buffer-local 'org-not-done-keywords)
  1562. (defvar org-done-keywords nil)
  1563. (make-variable-buffer-local 'org-done-keywords)
  1564. (defvar org-todo-heads nil)
  1565. (make-variable-buffer-local 'org-todo-heads)
  1566. (defvar org-todo-sets nil)
  1567. (make-variable-buffer-local 'org-todo-sets)
  1568. (defvar org-todo-log-states nil)
  1569. (make-variable-buffer-local 'org-todo-log-states)
  1570. (defvar org-todo-kwd-alist nil)
  1571. (make-variable-buffer-local 'org-todo-kwd-alist)
  1572. (defvar org-todo-key-alist nil)
  1573. (make-variable-buffer-local 'org-todo-key-alist)
  1574. (defvar org-todo-key-trigger nil)
  1575. (make-variable-buffer-local 'org-todo-key-trigger)
  1576. (defcustom org-todo-interpretation 'sequence
  1577. "Controls how TODO keywords are interpreted.
  1578. This variable is in principle obsolete and is only used for
  1579. backward compatibility, if the interpretation of todo keywords is
  1580. not given already in `org-todo-keywords'. See that variable for
  1581. more information."
  1582. :group 'org-todo
  1583. :group 'org-keywords
  1584. :type '(choice (const sequence)
  1585. (const type)))
  1586. (defcustom org-use-fast-todo-selection 'prefix
  1587. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1588. This variable describes if and under what circumstances the cycling
  1589. mechanism for TODO keywords will be replaced by a single-key, direct
  1590. selection scheme.
  1591. When nil, fast selection is never used.
  1592. When the symbol `prefix', it will be used when `org-todo' is called with
  1593. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1594. in an agenda buffer.
  1595. When t, fast selection is used by default. In this case, the prefix
  1596. argument forces cycling instead.
  1597. In all cases, the special interface is only used if access keys have actually
  1598. been assigned by the user, i.e. if keywords in the configuration are followed
  1599. by a letter in parenthesis, like TODO(t)."
  1600. :group 'org-todo
  1601. :type '(choice
  1602. (const :tag "Never" nil)
  1603. (const :tag "By default" t)
  1604. (const :tag "Only with C-u C-c C-t" prefix)))
  1605. (defcustom org-after-todo-state-change-hook nil
  1606. "Hook which is run after the state of a TODO item was changed.
  1607. The new state (a string with a TODO keyword, or nil) is available in the
  1608. Lisp variable `state'."
  1609. :group 'org-todo
  1610. :type 'hook)
  1611. (defcustom org-log-done nil
  1612. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1613. When equal to the list (done), also prompt for a closing note.
  1614. This can also be configured on a per-file basis by adding one of
  1615. the following lines anywhere in the buffer:
  1616. #+STARTUP: logdone
  1617. #+STARTUP: lognotedone
  1618. #+STARTUP: nologdone"
  1619. :group 'org-todo
  1620. :group 'org-progress
  1621. :type '(choice
  1622. (const :tag "No logging" nil)
  1623. (const :tag "Record CLOSED timestamp" time)
  1624. (const :tag "Record CLOSED timestamp with closing note." note)))
  1625. ;; Normalize old uses of org-log-done.
  1626. (cond
  1627. ((eq org-log-done t) (setq org-log-done 'time))
  1628. ((and (listp org-log-done) (memq 'done org-log-done))
  1629. (setq org-log-done 'note)))
  1630. ;; FIXME: document
  1631. (defcustom org-log-note-clock-out nil
  1632. "Non-nil means, recored a note when clocking out of an item.
  1633. This can also be configured on a per-file basis by adding one of
  1634. the following lines anywhere in the buffer:
  1635. #+STARTUP: lognoteclock-out
  1636. #+STARTUP: nolognoteclock-out"
  1637. :group 'org-todo
  1638. :group 'org-progress
  1639. :type 'boolean)
  1640. (defcustom org-log-done-with-time t
  1641. "Non-nil means, the CLOSED time stamp will contain date and time.
  1642. When nil, only the date will be recorded."
  1643. :group 'org-progress
  1644. :type 'boolean)
  1645. (defcustom org-log-note-headings
  1646. '((done . "CLOSING NOTE %t")
  1647. (state . "State %-12s %t")
  1648. (clock-out . ""))
  1649. "Headings for notes added when clocking out or closing TODO items.
  1650. The value is an alist, with the car being a symbol indicating the note
  1651. context, and the cdr is the heading to be used. The heading may also be the
  1652. empty string.
  1653. %t in the heading will be replaced by a time stamp.
  1654. %s will be replaced by the new TODO state, in double quotes.
  1655. %u will be replaced by the user name.
  1656. %U will be replaced by the full user name."
  1657. :group 'org-todo
  1658. :group 'org-progress
  1659. :type '(list :greedy t
  1660. (cons (const :tag "Heading when closing an item" done) string)
  1661. (cons (const :tag
  1662. "Heading when changing todo state (todo sequence only)"
  1663. state) string)
  1664. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1665. (defcustom org-log-states-order-reversed t
  1666. "Non-nil means, the latest state change note will be directly after heading.
  1667. When nil, the notes will be orderer according to time."
  1668. :group 'org-todo
  1669. :group 'org-progress
  1670. :type 'boolean)
  1671. (defcustom org-log-repeat 'time
  1672. "Non-nil means, record moving through the DONE state when triggering repeat.
  1673. An auto-repeating tasks is immediately switched back to TODO when marked
  1674. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1675. the TODO keyword definition, or recording a cloing note by setting
  1676. `org-log-done', there will be no record of the task moving trhough DONE.
  1677. This variable forces taking a note anyway. Possible values are:
  1678. nil Don't force a record
  1679. time Record a time stamp
  1680. note Record a note
  1681. This option can also be set with on a per-file-basis with
  1682. #+STARTUP: logrepeat
  1683. #+STARTUP: lognoterepeat
  1684. #+STARTUP: nologrepeat
  1685. You can have local logging settings for a subtree by setting the LOGGING
  1686. property to one or more of these keywords."
  1687. :group 'org-todo
  1688. :group 'org-progress
  1689. :type '(choice
  1690. (const :tag "Don't force a record" nil)
  1691. (const :tag "Force recording the DONE state" time)
  1692. (const :tag "Force recording a note with the DONE state" note)))
  1693. (defcustom org-clock-into-drawer 2
  1694. "Should clocking info be wrapped into a drawer?
  1695. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1696. If necessary, the drawer will be created.
  1697. When nil, the drawer will not be created, but used when present.
  1698. When an integer and the number of clocking entries in an item
  1699. reaches or exceeds this number, a drawer will be created."
  1700. :group 'org-todo
  1701. :group 'org-progress
  1702. :type '(choice
  1703. (const :tag "Always" t)
  1704. (const :tag "Only when drawer exists" nil)
  1705. (integer :tag "When at least N clock entries")))
  1706. (defcustom org-clock-out-when-done t
  1707. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1708. Nil means, clock will keep running until stopped explicitly with
  1709. `C-c C-x C-o', or until the clock is started in a different item."
  1710. :group 'org-progress
  1711. :type 'boolean)
  1712. (defcustom org-clock-in-switch-to-state nil
  1713. "Set task to a special todo state while clocking it.
  1714. The value should be the state to which the entry should be switched."
  1715. :group 'org-progress
  1716. :group 'org-todo
  1717. :type '(choice
  1718. (const :tag "Don't force a state" nil)
  1719. (string :tag "State")))
  1720. (defgroup org-priorities nil
  1721. "Priorities in Org-mode."
  1722. :tag "Org Priorities"
  1723. :group 'org-todo)
  1724. (defcustom org-highest-priority ?A
  1725. "The highest priority of TODO items. A character like ?A, ?B etc.
  1726. Must have a smaller ASCII number than `org-lowest-priority'."
  1727. :group 'org-priorities
  1728. :type 'character)
  1729. (defcustom org-lowest-priority ?C
  1730. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1731. Must have a larger ASCII number than `org-highest-priority'."
  1732. :group 'org-priorities
  1733. :type 'character)
  1734. (defcustom org-default-priority ?B
  1735. "The default priority of TODO items.
  1736. This is the priority an item get if no explicit priority is given."
  1737. :group 'org-priorities
  1738. :type 'character)
  1739. (defcustom org-priority-start-cycle-with-default t
  1740. "Non-nil means, start with default priority when starting to cycle.
  1741. When this is nil, the first step in the cycle will be (depending on the
  1742. command used) one higher or lower that the default priority."
  1743. :group 'org-priorities
  1744. :type 'boolean)
  1745. (defgroup org-time nil
  1746. "Options concerning time stamps and deadlines in Org-mode."
  1747. :tag "Org Time"
  1748. :group 'org)
  1749. (defcustom org-insert-labeled-timestamps-at-point nil
  1750. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1751. When nil, these labeled time stamps are forces into the second line of an
  1752. entry, just after the headline. When scheduling from the global TODO list,
  1753. the time stamp will always be forced into the second line."
  1754. :group 'org-time
  1755. :type 'boolean)
  1756. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1757. "Formats for `format-time-string' which are used for time stamps.
  1758. It is not recommended to change this constant.")
  1759. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1760. "Number of minutes to round time stamps to.
  1761. These are two values, the first applies when first creating a time stamp.
  1762. The second applies when changing it with the commands `S-up' and `S-down'.
  1763. When changing the time stamp, this means that it will change in steps
  1764. of N minutes, as given by the second value.
  1765. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1766. numbers should be factors of 60, so for example 5, 10, 15.
  1767. When this is larger than 1, you can still force an exact time-stamp by using
  1768. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1769. and by using a prefix arg to `S-up/down' to specify the exact number
  1770. of minutes to shift."
  1771. :group 'org-time
  1772. :get '(lambda (var) ; Make sure all entries have 5 elements
  1773. (if (integerp (default-value var))
  1774. (list (default-value var) 5)
  1775. (default-value var)))
  1776. :type '(list
  1777. (integer :tag "when inserting times")
  1778. (integer :tag "when modifying times")))
  1779. ;; Make sure old customizations of this variable don't lead to problems.
  1780. (when (integerp org-time-stamp-rounding-minutes)
  1781. (setq org-time-stamp-rounding-minutes
  1782. (list org-time-stamp-rounding-minutes
  1783. org-time-stamp-rounding-minutes)))
  1784. (defcustom org-display-custom-times nil
  1785. "Non-nil means, overlay custom formats over all time stamps.
  1786. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1787. To turn this on on a per-file basis, insert anywhere in the file:
  1788. #+STARTUP: customtime"
  1789. :group 'org-time
  1790. :set 'set-default
  1791. :type 'sexp)
  1792. (make-variable-buffer-local 'org-display-custom-times)
  1793. (defcustom org-time-stamp-custom-formats
  1794. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1795. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1796. These are overlayed over the default ISO format if the variable
  1797. `org-display-custom-times' is set. Time like %H:%M should be at the
  1798. end of the second format."
  1799. :group 'org-time
  1800. :type 'sexp)
  1801. (defun org-time-stamp-format (&optional long inactive)
  1802. "Get the right format for a time string."
  1803. (let ((f (if long (cdr org-time-stamp-formats)
  1804. (car org-time-stamp-formats))))
  1805. (if inactive
  1806. (concat "[" (substring f 1 -1) "]")
  1807. f)))
  1808. (defcustom org-read-date-prefer-future t
  1809. "Non-nil means, assume future for incomplete date input from user.
  1810. This affects the following situations:
  1811. 1. The user gives a day, but no month.
  1812. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1813. read this as the third of *next* month. However, if you enter \"17\",
  1814. it will be considered as *this* month.
  1815. 2. The user gives a month but not a year.
  1816. For example, if it is april and you enter \"feb 2\", this will be read
  1817. as feb 2, *next* year. \"May 5\", however, will be this year.
  1818. Currently this does not work for ISO week specifications.
  1819. When this option is nil, the current month and year will always be used
  1820. as defaults."
  1821. :group 'org-time
  1822. :type 'boolean)
  1823. (defcustom org-read-date-display-live t
  1824. "Non-nil means, display current interpretation of date prompt live.
  1825. This display will be in an overlay, in the minibuffer."
  1826. :group 'org-time
  1827. :type 'boolean)
  1828. (defcustom org-read-date-popup-calendar t
  1829. "Non-nil means, pop up a calendar when prompting for a date.
  1830. In the calendar, the date can be selected with mouse-1. However, the
  1831. minibuffer will also be active, and you can simply enter the date as well.
  1832. When nil, only the minibuffer will be available."
  1833. :group 'org-time
  1834. :type 'boolean)
  1835. (if (fboundp 'defvaralias)
  1836. (defvaralias 'org-popup-calendar-for-date-prompt
  1837. 'org-read-date-popup-calendar))
  1838. (defcustom org-extend-today-until 0
  1839. "The hour when your day really ends.
  1840. This has influence for the following applications:
  1841. - When switching the agenda to \"today\". It it is still earlier than
  1842. the time given here, the day recognized as TODAY is actually yesterday.
  1843. - When a date is read from the user and it is still before the time given
  1844. here, the current date and time will be assumed to be yesterday, 23:59.
  1845. FIXME:
  1846. IMPORTANT: This is still a very experimental feature, it may disappear
  1847. again or it may be extended to mean more things."
  1848. :group 'org-time
  1849. :type 'number)
  1850. (defcustom org-edit-timestamp-down-means-later nil
  1851. "Non-nil means, S-down will increase the time in a time stamp.
  1852. When nil, S-up will increase."
  1853. :group 'org-time
  1854. :type 'boolean)
  1855. (defcustom org-calendar-follow-timestamp-change t
  1856. "Non-nil means, make the calendar window follow timestamp changes.
  1857. When a timestamp is modified and the calendar window is visible, it will be
  1858. moved to the new date."
  1859. :group 'org-time
  1860. :type 'boolean)
  1861. (defcustom org-clock-heading-function nil
  1862. "When non-nil, should be a function to create `org-clock-heading'.
  1863. This is the string shown in the mode line when a clock is running.
  1864. The function is called with point at the beginning of the headline."
  1865. :group 'org-time ; FIXME: Should we have a separate group????
  1866. :type 'function)
  1867. (defgroup org-tags nil
  1868. "Options concerning tags in Org-mode."
  1869. :tag "Org Tags"
  1870. :group 'org)
  1871. (defcustom org-tag-alist nil
  1872. "List of tags allowed in Org-mode files.
  1873. When this list is nil, Org-mode will base TAG input on what is already in the
  1874. buffer.
  1875. The value of this variable is an alist, the car of each entry must be a
  1876. keyword as a string, the cdr may be a character that is used to select
  1877. that tag through the fast-tag-selection interface.
  1878. See the manual for details."
  1879. :group 'org-tags
  1880. :type '(repeat
  1881. (choice
  1882. (cons (string :tag "Tag name")
  1883. (character :tag "Access char"))
  1884. (const :tag "Start radio group" (:startgroup))
  1885. (const :tag "End radio group" (:endgroup)))))
  1886. (defcustom org-use-fast-tag-selection 'auto
  1887. "Non-nil means, use fast tag selection scheme.
  1888. This is a special interface to select and deselect tags with single keys.
  1889. When nil, fast selection is never used.
  1890. When the symbol `auto', fast selection is used if and only if selection
  1891. characters for tags have been configured, either through the variable
  1892. `org-tag-alist' or through a #+TAGS line in the buffer.
  1893. When t, fast selection is always used and selection keys are assigned
  1894. automatically if necessary."
  1895. :group 'org-tags
  1896. :type '(choice
  1897. (const :tag "Always" t)
  1898. (const :tag "Never" nil)
  1899. (const :tag "When selection characters are configured" 'auto)))
  1900. (defcustom org-fast-tag-selection-single-key nil
  1901. "Non-nil means, fast tag selection exits after first change.
  1902. When nil, you have to press RET to exit it.
  1903. During fast tag selection, you can toggle this flag with `C-c'.
  1904. This variable can also have the value `expert'. In this case, the window
  1905. displaying the tags menu is not even shown, until you press C-c again."
  1906. :group 'org-tags
  1907. :type '(choice
  1908. (const :tag "No" nil)
  1909. (const :tag "Yes" t)
  1910. (const :tag "Expert" expert)))
  1911. (defvar org-fast-tag-selection-include-todo nil
  1912. "Non-nil means, fast tags selection interface will also offer TODO states.
  1913. This is an undocumented feature, you should not rely on it.")
  1914. (defcustom org-tags-column -80
  1915. "The column to which tags should be indented in a headline.
  1916. If this number is positive, it specifies the column. If it is negative,
  1917. it means that the tags should be flushright to that column. For example,
  1918. -80 works well for a normal 80 character screen."
  1919. :group 'org-tags
  1920. :type 'integer)
  1921. (defcustom org-auto-align-tags t
  1922. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1923. These operations change the length of a headline and therefore shift
  1924. the tags around. With this options turned on, after each such operation
  1925. the tags are again aligned to `org-tags-column'."
  1926. :group 'org-tags
  1927. :type 'boolean)
  1928. (defcustom org-use-tag-inheritance t
  1929. "Non-nil means, tags in levels apply also for sublevels.
  1930. When nil, only the tags directly given in a specific line apply there.
  1931. If you turn off this option, you very likely want to turn on the
  1932. companion option `org-tags-match-list-sublevels'."
  1933. :group 'org-tags
  1934. :type 'boolean)
  1935. (defcustom org-tags-match-list-sublevels nil
  1936. "Non-nil means list also sublevels of headlines matching tag search.
  1937. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1938. the sublevels of a headline matching a tag search often also match
  1939. the same search. Listing all of them can create very long lists.
  1940. Setting this variable to nil causes subtrees of a match to be skipped.
  1941. This option is off by default, because inheritance in on. If you turn
  1942. inheritance off, you very likely want to turn this option on.
  1943. As a special case, if the tag search is restricted to TODO items, the
  1944. value of this variable is ignored and sublevels are always checked, to
  1945. make sure all corresponding TODO items find their way into the list."
  1946. :group 'org-tags
  1947. :type 'boolean)
  1948. (defvar org-tags-history nil
  1949. "History of minibuffer reads for tags.")
  1950. (defvar org-last-tags-completion-table nil
  1951. "The last used completion table for tags.")
  1952. (defvar org-after-tags-change-hook nil
  1953. "Hook that is run after the tags in a line have changed.")
  1954. (defgroup org-properties nil
  1955. "Options concerning properties in Org-mode."
  1956. :tag "Org Properties"
  1957. :group 'org)
  1958. (defcustom org-property-format "%-10s %s"
  1959. "How property key/value pairs should be formatted by `indent-line'.
  1960. When `indent-line' hits a property definition, it will format the line
  1961. according to this format, mainly to make sure that the values are
  1962. lined-up with respect to each other."
  1963. :group 'org-properties
  1964. :type 'string)
  1965. (defcustom org-use-property-inheritance nil
  1966. "Non-nil means, properties apply also for sublevels.
  1967. This setting is only relevant during property searches, not when querying
  1968. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1969. you need to call `org-entry-get' with the inheritance flag.
  1970. Turning this on can cause significant overhead when doing a search, so
  1971. this is turned off by default.
  1972. When nil, only the properties directly given in the current entry count.
  1973. The value may also be a list of properties that shouldhave inheritance.
  1974. However, note that some special properties use inheritance under special
  1975. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1976. and the properties ending in \"_ALL\" when they are used as descriptor
  1977. for valid values of a property."
  1978. :group 'org-properties
  1979. :type '(choice
  1980. (const :tag "Not" nil)
  1981. (const :tag "Always" nil)
  1982. (repeat :tag "Specific properties" (string :tag "Property"))))
  1983. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1984. "The default column format, if no other format has been defined.
  1985. This variable can be set on the per-file basis by inserting a line
  1986. #+COLUMNS: %25ITEM ....."
  1987. :group 'org-properties
  1988. :type 'string)
  1989. (defcustom org-global-properties nil
  1990. "List of property/value pairs that can be inherited by any entry.
  1991. You can set buffer-local values for this by adding lines like
  1992. #+PROPERTY: NAME VALUE"
  1993. :group 'org-properties
  1994. :type '(repeat
  1995. (cons (string :tag "Property")
  1996. (string :tag "Value"))))
  1997. (defvar org-local-properties nil
  1998. "List of property/value pairs that can be inherited by any entry.
  1999. Valid for the current buffer.
  2000. This variable is populated from #+PROPERTY lines.")
  2001. (defgroup org-agenda nil
  2002. "Options concerning agenda views in Org-mode."
  2003. :tag "Org Agenda"
  2004. :group 'org)
  2005. (defvar org-category nil
  2006. "Variable used by org files to set a category for agenda display.
  2007. Such files should use a file variable to set it, for example
  2008. # -*- mode: org; org-category: \"ELisp\"
  2009. or contain a special line
  2010. #+CATEGORY: ELisp
  2011. If the file does not specify a category, then file's base name
  2012. is used instead.")
  2013. (make-variable-buffer-local 'org-category)
  2014. (defcustom org-agenda-files nil
  2015. "The files to be used for agenda display.
  2016. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2017. \\[org-remove-file]. You can also use customize to edit the list.
  2018. If an entry is a directory, all files in that directory that are matched by
  2019. `org-agenda-file-regexp' will be part of the file list.
  2020. If the value of the variable is not a list but a single file name, then
  2021. the list of agenda files is actually stored and maintained in that file, one
  2022. agenda file per line."
  2023. :group 'org-agenda
  2024. :type '(choice
  2025. (repeat :tag "List of files and directories" file)
  2026. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2027. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2028. "Regular expression to match files for `org-agenda-files'.
  2029. If any element in the list in that variable contains a directory instead
  2030. of a normal file, all files in that directory that are matched by this
  2031. regular expression will be included."
  2032. :group 'org-agenda
  2033. :type 'regexp)
  2034. (defcustom org-agenda-skip-unavailable-files nil
  2035. "t means to just skip non-reachable files in `org-agenda-files'.
  2036. Nil means to remove them, after a query, from the list."
  2037. :group 'org-agenda
  2038. :type 'boolean)
  2039. (defcustom org-agenda-text-search-extra-files nil
  2040. "List of extra files to be searched by text search commands.
  2041. These files will be search in addition to the agenda files bu the
  2042. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2043. Note that these files will only be searched for text search commands,
  2044. not for the other agenda views like todo lists, tag earches or the weekly
  2045. agenda. This variable is intended to list notes and possibly archive files
  2046. that should also be searched by these two commands."
  2047. :group 'org-agenda
  2048. :type '(repeat file))
  2049. (if (fboundp 'defvaralias)
  2050. (defvaralias 'org-agenda-multi-occur-extra-files
  2051. 'org-agenda-text-search-extra-files))
  2052. (defcustom org-agenda-confirm-kill 1
  2053. "When set, remote killing from the agenda buffer needs confirmation.
  2054. When t, a confirmation is always needed. When a number N, confirmation is
  2055. only needed when the text to be killed contains more than N non-white lines."
  2056. :group 'org-agenda
  2057. :type '(choice
  2058. (const :tag "Never" nil)
  2059. (const :tag "Always" t)
  2060. (number :tag "When more than N lines")))
  2061. (defcustom org-calendar-to-agenda-key [?c]
  2062. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2063. The command `org-calendar-goto-agenda' will be bound to this key. The
  2064. default is the character `c' because then `c' can be used to switch back and
  2065. forth between agenda and calendar."
  2066. :group 'org-agenda
  2067. :type 'sexp)
  2068. (defcustom org-agenda-compact-blocks nil
  2069. "Non-nil means, make the block agenda more compact.
  2070. This is done by leaving out unnecessary lines."
  2071. :group 'org-agenda
  2072. :type nil)
  2073. (defgroup org-agenda-export nil
  2074. "Options concerning exporting agenda views in Org-mode."
  2075. :tag "Org Agenda Export"
  2076. :group 'org-agenda)
  2077. (defcustom org-agenda-with-colors t
  2078. "Non-nil means, use colors in agenda views."
  2079. :group 'org-agenda-export
  2080. :type 'boolean)
  2081. (defcustom org-agenda-exporter-settings nil
  2082. "Alist of variable/value pairs that should be active during agenda export.
  2083. This is a good place to set uptions for ps-print and for htmlize."
  2084. :group 'org-agenda-export
  2085. :type '(repeat
  2086. (list
  2087. (variable)
  2088. (sexp :tag "Value"))))
  2089. (defcustom org-agenda-export-html-style ""
  2090. "The style specification for exported HTML Agenda files.
  2091. If this variable contains a string, it will replace the default <style>
  2092. section as produced by `htmlize'.
  2093. Since there are different ways of setting style information, this variable
  2094. needs to contain the full HTML structure to provide a style, including the
  2095. surrounding HTML tags. The style specifications should include definitions
  2096. the fonts used by the agenda, here is an example:
  2097. <style type=\"text/css\">
  2098. p { font-weight: normal; color: gray; }
  2099. .org-agenda-structure {
  2100. font-size: 110%;
  2101. color: #003399;
  2102. font-weight: 600;
  2103. }
  2104. .org-todo {
  2105. color: #cc6666;
  2106. font-weight: bold;
  2107. }
  2108. .org-done {
  2109. color: #339933;
  2110. }
  2111. .title { text-align: center; }
  2112. .todo, .deadline { color: red; }
  2113. .done { color: green; }
  2114. </style>
  2115. or, if you want to keep the style in a file,
  2116. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2117. As the value of this option simply gets inserted into the HTML <head> header,
  2118. you can \"misuse\" it to also add other text to the header. However,
  2119. <style>...</style> is required, if not present the variable will be ignored."
  2120. :group 'org-agenda-export
  2121. :group 'org-export-html
  2122. :type 'string)
  2123. (defgroup org-agenda-custom-commands nil
  2124. "Options concerning agenda views in Org-mode."
  2125. :tag "Org Agenda Custom Commands"
  2126. :group 'org-agenda)
  2127. (defconst org-sorting-choice
  2128. '(choice
  2129. (const time-up) (const time-down)
  2130. (const category-keep) (const category-up) (const category-down)
  2131. (const tag-down) (const tag-up)
  2132. (const priority-up) (const priority-down))
  2133. "Sorting choices.")
  2134. (defconst org-agenda-custom-commands-local-options
  2135. `(repeat :tag "Local settings for this command. Remember to quote values"
  2136. (choice :tag "Setting"
  2137. (list :tag "Any variable"
  2138. (variable :tag "Variable")
  2139. (sexp :tag "Value"))
  2140. (list :tag "Files to be searched"
  2141. (const org-agenda-files)
  2142. (list
  2143. (const :format "" quote)
  2144. (repeat
  2145. (file))))
  2146. (list :tag "Sorting strategy"
  2147. (const org-agenda-sorting-strategy)
  2148. (list
  2149. (const :format "" quote)
  2150. (repeat
  2151. ,org-sorting-choice)))
  2152. (list :tag "Prefix format"
  2153. (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
  2154. (string))
  2155. (list :tag "Number of days in agenda"
  2156. (const org-agenda-ndays)
  2157. (integer :value 1))
  2158. (list :tag "Fixed starting date"
  2159. (const org-agenda-start-day)
  2160. (string :value "2007-11-01"))
  2161. (list :tag "Start on day of week"
  2162. (const org-agenda-start-on-weekday)
  2163. (choice :value 1
  2164. (const :tag "Today" nil)
  2165. (number :tag "Weekday No.")))
  2166. (list :tag "Include data from diary"
  2167. (const org-agenda-include-diary)
  2168. (boolean))
  2169. (list :tag "Deadline Warning days"
  2170. (const org-deadline-warning-days)
  2171. (integer :value 1))
  2172. (list :tag "Standard skipping condition"
  2173. :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
  2174. (const org-agenda-skip-function)
  2175. (list
  2176. (const :format "" quote)
  2177. (list
  2178. (choice
  2179. :tag "Skiping range"
  2180. (const :tag "Skip entry" org-agenda-skip-entry-if)
  2181. (const :tag "Skip subtree" org-agenda-skip-subtree-if))
  2182. (repeat :inline t :tag "Conditions for skipping"
  2183. (choice
  2184. :tag "Condition type"
  2185. (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
  2186. (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
  2187. (const :tag "scheduled" 'scheduled)
  2188. (const :tag "not scheduled" 'notscheduled)
  2189. (const :tag "deadline" 'deadline)
  2190. (const :tag "no deadline" 'notdeadline))))))
  2191. (list :tag "Non-standard skipping condition"
  2192. :value (org-agenda-skip-function)
  2193. (list
  2194. (const org-agenda-skip-function)
  2195. (sexp :tag "Function or form (quoted!)")))))
  2196. "Selection of examples for agenda command settings.
  2197. This will be spliced into the custom type of
  2198. `org-agenda-custom-commands'.")
  2199. (defcustom org-agenda-custom-commands nil
  2200. "Custom commands for the agenda.
  2201. These commands will be offered on the splash screen displayed by the
  2202. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2203. (key desc type match settings files)
  2204. key The key (one or more characters as a string) to be associated
  2205. with the command.
  2206. desc A description of the command, when omitted or nil, a default
  2207. description is built using MATCH.
  2208. type The command type, any of the following symbols:
  2209. agenda The daily/weekly agenda.
  2210. todo Entries with a specific TODO keyword, in all agenda files.
  2211. search Entries containing search words entry or headline.
  2212. tags Tags/Property/TODO match in all agenda files.
  2213. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2214. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2215. tags-tree Sparse tree with all tags matches in *current* file.
  2216. occur-tree Occur sparse tree for *current* file.
  2217. ... A user-defined function.
  2218. match What to search for:
  2219. - a single keyword for TODO keyword searches
  2220. - a tags match expression for tags searches
  2221. - a word search expression for text searches.
  2222. - a regular expression for occur searches
  2223. For all other commands, this should be the empty string.
  2224. settings A list of option settings, similar to that in a let form, so like
  2225. this: ((opt1 val1) (opt2 val2) ...). The values will be
  2226. evaluated at the moment of execution, so quote them when needed.
  2227. files A list of files file to write the produced agenda buffer to
  2228. with the command `org-store-agenda-views'.
  2229. If a file name ends in \".html\", an HTML version of the buffer
  2230. is written out. If it ends in \".ps\", a postscript version is
  2231. produced. Otherwide, only the plain text is written to the file.
  2232. You can also define a set of commands, to create a composite agenda buffer.
  2233. In this case, an entry looks like this:
  2234. (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
  2235. where
  2236. desc A description string to be displayed in the dispatcher menu.
  2237. cmd An agenda command, similar to the above. However, tree commands
  2238. are no allowed, but instead you can get agenda and global todo list.
  2239. So valid commands for a set are:
  2240. (agenda \"\" settings)
  2241. (alltodo \"\" settings)
  2242. (stuck \"\" settings)
  2243. (todo \"match\" settings files)
  2244. (search \"match\" settings files)
  2245. (tags \"match\" settings files)
  2246. (tags-todo \"match\" settings files)
  2247. Each command can carry a list of options, and another set of options can be
  2248. given for the whole set of commands. Individual command options take
  2249. precedence over the general options.
  2250. When using several characters as key to a command, the first characters
  2251. are prefix commands. For the dispatcher to display useful information, you
  2252. should provide a description for the prefix, like
  2253. (setq org-agenda-custom-commands
  2254. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2255. (\"hl\" tags \"+HOME+Lisa\")
  2256. (\"hp\" tags \"+HOME+Peter\")
  2257. (\"hk\" tags \"+HOME+Kim\")))"
  2258. :group 'org-agenda-custom-commands
  2259. :type `(repeat
  2260. (choice :value ("x" "Describe command here" tags "" nil)
  2261. (list :tag "Single command"
  2262. (string :tag "Access Key(s) ")
  2263. (option (string :tag "Description"))
  2264. (choice
  2265. (const :tag "Agenda" agenda)
  2266. (const :tag "TODO list" alltodo)
  2267. (const :tag "Search words" search)
  2268. (const :tag "Stuck projects" stuck)
  2269. (const :tag "Tags search (all agenda files)" tags)
  2270. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2271. (const :tag "TODO keyword search (all agenda files)" todo)
  2272. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2273. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2274. (const :tag "Occur tree (current buffer)" occur-tree)
  2275. (sexp :tag "Other, user-defined function"))
  2276. (string :tag "Match (only for some commands)")
  2277. ,org-agenda-custom-commands-local-options
  2278. (option (repeat :tag "Export" (file :tag "Export to"))))
  2279. (list :tag "Command series, all agenda files"
  2280. (string :tag "Access Key(s)")
  2281. (string :tag "Description ")
  2282. (repeat :tag "Component"
  2283. (choice
  2284. (list :tag "Agenda"
  2285. (const :format "" agenda)
  2286. (const :tag "" :format "" "")
  2287. ,org-agenda-custom-commands-local-options)
  2288. (list :tag "TODO list (all keywords)"
  2289. (const :format "" alltodo)
  2290. (const :tag "" :format "" "")
  2291. ,org-agenda-custom-commands-local-options)
  2292. (list :tag "Search words"
  2293. (const :format "" search)
  2294. (string :tag "Match")
  2295. ,org-agenda-custom-commands-local-options)
  2296. (list :tag "Stuck projects"
  2297. (const :format "" stuck)
  2298. (const :tag "" :format "" "")
  2299. ,org-agenda-custom-commands-local-options)
  2300. (list :tag "Tags search"
  2301. (const :format "" tags)
  2302. (string :tag "Match")
  2303. ,org-agenda-custom-commands-local-options)
  2304. (list :tag "Tags search, TODO entries only"
  2305. (const :format "" tags-todo)
  2306. (string :tag "Match")
  2307. ,org-agenda-custom-commands-local-options)
  2308. (list :tag "TODO keyword search"
  2309. (const :format "" todo)
  2310. (string :tag "Match")
  2311. ,org-agenda-custom-commands-local-options)
  2312. (list :tag "Other, user-defined function"
  2313. (symbol :tag "function")
  2314. (string :tag "Match")
  2315. ,org-agenda-custom-commands-local-options)))
  2316. (repeat :tag "Settings for entire command set"
  2317. (list (variable :tag "Any variable")
  2318. (sexp :tag "Value")))
  2319. (option (repeat :tag "Export" (file :tag "Export to"))))
  2320. (cons :tag "Prefix key documentation"
  2321. (string :tag "Access Key(s)")
  2322. (string :tag "Description ")))))
  2323. (defcustom org-agenda-query-register ?o
  2324. "The register holding the current query string.
  2325. The prupose of this is that if you construct a query string interactively,
  2326. you can then use it to define a custom command."
  2327. :group 'org-agenda-custom-commands
  2328. :type 'character)
  2329. (defcustom org-stuck-projects
  2330. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2331. "How to identify stuck projects.
  2332. This is a list of four items:
  2333. 1. A tags/todo matcher string that is used to identify a project.
  2334. The entire tree below a headline matched by this is considered one project.
  2335. 2. A list of TODO keywords identifying non-stuck projects.
  2336. If the project subtree contains any headline with one of these todo
  2337. keywords, the project is considered to be not stuck. If you specify
  2338. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2339. 3. A list of tags identifying non-stuck projects.
  2340. If the project subtree contains any headline with one of these tags,
  2341. the project is considered to be not stuck. If you specify \"*\" as
  2342. a tag, any tag will mark the project unstuck.
  2343. 4. An arbitrary regular expression matching non-stuck projects.
  2344. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2345. or `C-c a #' to produce the list."
  2346. :group 'org-agenda-custom-commands
  2347. :type '(list
  2348. (string :tag "Tags/TODO match to identify a project")
  2349. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2350. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2351. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2352. (defgroup org-agenda-skip nil
  2353. "Options concerning skipping parts of agenda files."
  2354. :tag "Org Agenda Skip"
  2355. :group 'org-agenda)
  2356. (defcustom org-agenda-todo-list-sublevels t
  2357. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2358. When nil, the sublevels of a TODO entry are not checked, resulting in
  2359. potentially much shorter TODO lists."
  2360. :group 'org-agenda-skip
  2361. :group 'org-todo
  2362. :type 'boolean)
  2363. (defcustom org-agenda-todo-ignore-with-date nil
  2364. "Non-nil means, don't show entries with a date in the global todo list.
  2365. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2366. but don't want them to show up in the TODO list.
  2367. When this is set, it also covers deadlines and scheduled items, the settings
  2368. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2369. will be ignored."
  2370. :group 'org-agenda-skip
  2371. :group 'org-todo
  2372. :type 'boolean)
  2373. (defcustom org-agenda-todo-ignore-scheduled nil
  2374. "Non-nil means, don't show scheduled entries in the global todo list.
  2375. The idea behind this is that by scheduling it, you have already taken care
  2376. of this item.
  2377. See also `org-agenda-todo-ignore-with-date'."
  2378. :group 'org-agenda-skip
  2379. :group 'org-todo
  2380. :type 'boolean)
  2381. (defcustom org-agenda-todo-ignore-deadlines nil
  2382. "Non-nil means, don't show near deadline entries in the global todo list.
  2383. Near means closer than `org-deadline-warning-days' days.
  2384. The idea behind this is that such items will appear in the agenda anyway.
  2385. See also `org-agenda-todo-ignore-with-date'."
  2386. :group 'org-agenda-skip
  2387. :group 'org-todo
  2388. :type 'boolean)
  2389. (defcustom org-agenda-skip-scheduled-if-done nil
  2390. "Non-nil means don't show scheduled items in agenda when they are done.
  2391. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2392. it applies only to the actual date of the scheduling. Warnings about
  2393. an item with a past scheduling dates are always turned off when the item
  2394. is DONE."
  2395. :group 'org-agenda-skip
  2396. :type 'boolean)
  2397. (defcustom org-agenda-skip-deadline-if-done nil
  2398. "Non-nil means don't show deadines when the corresponding item is done.
  2399. When nil, the deadline is still shown and should give you a happy feeling.
  2400. This is relevant for the daily/weekly agenda. And it applied only to the
  2401. actualy date of the deadline. Warnings about approching and past-due
  2402. deadlines are always turned off when the item is DONE."
  2403. :group 'org-agenda-skip
  2404. :type 'boolean)
  2405. (defcustom org-agenda-skip-timestamp-if-done nil
  2406. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2407. :group 'org-agenda-skip
  2408. :type 'boolean)
  2409. (defcustom org-timeline-show-empty-dates 3
  2410. "Non-nil means, `org-timeline' also shows dates without an entry.
  2411. When nil, only the days which actually have entries are shown.
  2412. When t, all days between the first and the last date are shown.
  2413. When an integer, show also empty dates, but if there is a gap of more than
  2414. N days, just insert a special line indicating the size of the gap."
  2415. :group 'org-agenda-skip
  2416. :type '(choice
  2417. (const :tag "None" nil)
  2418. (const :tag "All" t)
  2419. (number :tag "at most")))
  2420. (defgroup org-agenda-startup nil
  2421. "Options concerning initial settings in the Agenda in Org Mode."
  2422. :tag "Org Agenda Startup"
  2423. :group 'org-agenda)
  2424. (defcustom org-finalize-agenda-hook nil
  2425. "Hook run just before displaying an agenda buffer."
  2426. :group 'org-agenda-startup
  2427. :type 'hook)
  2428. (defcustom org-agenda-mouse-1-follows-link nil
  2429. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2430. A longer mouse click will still set point. Does not work on XEmacs.
  2431. Needs to be set before org.el is loaded."
  2432. :group 'org-agenda-startup
  2433. :type 'boolean)
  2434. (defcustom org-agenda-start-with-follow-mode nil
  2435. "The initial value of follow-mode in a newly created agenda window."
  2436. :group 'org-agenda-startup
  2437. :type 'boolean)
  2438. (defgroup org-agenda-windows nil
  2439. "Options concerning the windows used by the Agenda in Org Mode."
  2440. :tag "Org Agenda Windows"
  2441. :group 'org-agenda)
  2442. (defcustom org-agenda-window-setup 'reorganize-frame
  2443. "How the agenda buffer should be displayed.
  2444. Possible values for this option are:
  2445. current-window Show agenda in the current window, keeping all other windows.
  2446. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2447. other-window Use `switch-to-buffer-other-window' to display agenda.
  2448. reorganize-frame Show only two windows on the current frame, the current
  2449. window and the agenda.
  2450. See also the variable `org-agenda-restore-windows-after-quit'."
  2451. :group 'org-agenda-windows
  2452. :type '(choice
  2453. (const current-window)
  2454. (const other-frame)
  2455. (const other-window)
  2456. (const reorganize-frame)))
  2457. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2458. "The min and max height of the agenda window as a fraction of frame height.
  2459. The value of the variable is a cons cell with two numbers between 0 and 1.
  2460. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2461. :group 'org-agenda-windows
  2462. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2463. (defcustom org-agenda-restore-windows-after-quit nil
  2464. "Non-nil means, restore window configuration open exiting agenda.
  2465. Before the window configuration is changed for displaying the agenda,
  2466. the current status is recorded. When the agenda is exited with
  2467. `q' or `x' and this option is set, the old state is restored. If
  2468. `org-agenda-window-setup' is `other-frame', the value of this
  2469. option will be ignored.."
  2470. :group 'org-agenda-windows
  2471. :type 'boolean)
  2472. (defcustom org-indirect-buffer-display 'other-window
  2473. "How should indirect tree buffers be displayed?
  2474. This applies to indirect buffers created with the commands
  2475. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2476. Valid values are:
  2477. current-window Display in the current window
  2478. other-window Just display in another window.
  2479. dedicated-frame Create one new frame, and re-use it each time.
  2480. new-frame Make a new frame each time. Note that in this case
  2481. previously-made indirect buffers are kept, and you need to
  2482. kill these buffers yourself."
  2483. :group 'org-structure
  2484. :group 'org-agenda-windows
  2485. :type '(choice
  2486. (const :tag "In current window" current-window)
  2487. (const :tag "In current frame, other window" other-window)
  2488. (const :tag "Each time a new frame" new-frame)
  2489. (const :tag "One dedicated frame" dedicated-frame)))
  2490. (defgroup org-agenda-daily/weekly nil
  2491. "Options concerning the daily/weekly agenda."
  2492. :tag "Org Agenda Daily/Weekly"
  2493. :group 'org-agenda)
  2494. (defcustom org-agenda-ndays 7
  2495. "Number of days to include in overview display.
  2496. Should be 1 or 7."
  2497. :group 'org-agenda-daily/weekly
  2498. :type 'number)
  2499. (defcustom org-agenda-start-on-weekday 1
  2500. "Non-nil means, start the overview always on the specified weekday.
  2501. 0 denotes Sunday, 1 denotes Monday etc.
  2502. When nil, always start on the current day."
  2503. :group 'org-agenda-daily/weekly
  2504. :type '(choice (const :tag "Today" nil)
  2505. (number :tag "Weekday No.")))
  2506. (defcustom org-agenda-show-all-dates t
  2507. "Non-nil means, `org-agenda' shows every day in the selected range.
  2508. When nil, only the days which actually have entries are shown."
  2509. :group 'org-agenda-daily/weekly
  2510. :type 'boolean)
  2511. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2512. "Format string for displaying dates in the agenda.
  2513. Used by the daily/weekly agenda and by the timeline. This should be
  2514. a format string understood by `format-time-string', or a function returning
  2515. the formatted date as a string. The function must take a single argument,
  2516. a calendar-style date list like (month day year)."
  2517. :group 'org-agenda-daily/weekly
  2518. :type '(choice
  2519. (string :tag "Format string")
  2520. (function :tag "Function")))
  2521. (defun org-agenda-format-date-aligned (date)
  2522. "Format a date string for display in the daily/weekly agenda, or timeline.
  2523. This function makes sure that dates are aligned for easy reading."
  2524. (require 'cal-iso)
  2525. (let* ((dayname (calendar-day-name date))
  2526. (day (extract-calendar-day date))
  2527. (day-of-week (calendar-day-of-week date))
  2528. (month (extract-calendar-month date))
  2529. (monthname (calendar-month-name month))
  2530. (year (extract-calendar-year date))
  2531. (iso-week (org-days-to-iso-week
  2532. (calendar-absolute-from-gregorian date)))
  2533. (weekyear (cond ((and (= month 1) (>= iso-week 52))
  2534. (1- year))
  2535. ((and (= month 12) (<= iso-week 1))
  2536. (1+ year))
  2537. (t year)))
  2538. (weekstring (if (= day-of-week 1)
  2539. (format " W%02d" iso-week)
  2540. "")))
  2541. (format "%-9s %2d %s %4d%s"
  2542. dayname day monthname year weekstring)))
  2543. (defcustom org-agenda-include-diary nil
  2544. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2545. :group 'org-agenda-daily/weekly
  2546. :type 'boolean)
  2547. (defcustom org-agenda-include-all-todo nil
  2548. "Set means weekly/daily agenda will always contain all TODO entries.
  2549. The TODO entries will be listed at the top of the agenda, before
  2550. the entries for specific days."
  2551. :group 'org-agenda-daily/weekly
  2552. :type 'boolean)
  2553. (defcustom org-agenda-repeating-timestamp-show-all t
  2554. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2555. When nil, only one occurence is shown, either today or the
  2556. nearest into the future."
  2557. :group 'org-agenda-daily/weekly
  2558. :type 'boolean)
  2559. (defcustom org-deadline-warning-days 14
  2560. "No. of days before expiration during which a deadline becomes active.
  2561. This variable governs the display in sparse trees and in the agenda.
  2562. When 0 or negative, it means use this number (the absolute value of it)
  2563. even if a deadline has a different individual lead time specified."
  2564. :group 'org-time
  2565. :group 'org-agenda-daily/weekly
  2566. :type 'number)
  2567. (defcustom org-scheduled-past-days 10000
  2568. "No. of days to continue listing scheduled items that are not marked DONE.
  2569. When an item is scheduled on a date, it shows up in the agenda on this
  2570. day and will be listed until it is marked done for the number of days
  2571. given here."
  2572. :group 'org-agenda-daily/weekly
  2573. :type 'number)
  2574. (defgroup org-agenda-time-grid nil
  2575. "Options concerning the time grid in the Org-mode Agenda."
  2576. :tag "Org Agenda Time Grid"
  2577. :group 'org-agenda)
  2578. (defcustom org-agenda-use-time-grid t
  2579. "Non-nil means, show a time grid in the agenda schedule.
  2580. A time grid is a set of lines for specific times (like every two hours between
  2581. 8:00 and 20:00). The items scheduled for a day at specific times are
  2582. sorted in between these lines.
  2583. For details about when the grid will be shown, and what it will look like, see
  2584. the variable `org-agenda-time-grid'."
  2585. :group 'org-agenda-time-grid
  2586. :type 'boolean)
  2587. (defcustom org-agenda-time-grid
  2588. '((daily today require-timed)
  2589. "----------------"
  2590. (800 1000 1200 1400 1600 1800 2000))
  2591. "The settings for time grid for agenda display.
  2592. This is a list of three items. The first item is again a list. It contains
  2593. symbols specifying conditions when the grid should be displayed:
  2594. daily if the agenda shows a single day
  2595. weekly if the agenda shows an entire week
  2596. today show grid on current date, independent of daily/weekly display
  2597. require-timed show grid only if at least one item has a time specification
  2598. The second item is a string which will be places behing the grid time.
  2599. The third item is a list of integers, indicating the times that should have
  2600. a grid line."
  2601. :group 'org-agenda-time-grid
  2602. :type
  2603. '(list
  2604. (set :greedy t :tag "Grid Display Options"
  2605. (const :tag "Show grid in single day agenda display" daily)
  2606. (const :tag "Show grid in weekly agenda display" weekly)
  2607. (const :tag "Always show grid for today" today)
  2608. (const :tag "Show grid only if any timed entries are present"
  2609. require-timed)
  2610. (const :tag "Skip grid times already present in an entry"
  2611. remove-match))
  2612. (string :tag "Grid String")
  2613. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2614. (defgroup org-agenda-sorting nil
  2615. "Options concerning sorting in the Org-mode Agenda."
  2616. :tag "Org Agenda Sorting"
  2617. :group 'org-agenda)
  2618. (defcustom org-agenda-sorting-strategy
  2619. '((agenda time-up category-keep priority-down)
  2620. (todo category-keep priority-down)
  2621. (tags category-keep priority-down)
  2622. (search category-keep))
  2623. "Sorting structure for the agenda items of a single day.
  2624. This is a list of symbols which will be used in sequence to determine
  2625. if an entry should be listed before another entry. The following
  2626. symbols are recognized:
  2627. time-up Put entries with time-of-day indications first, early first
  2628. time-down Put entries with time-of-day indications first, late first
  2629. category-keep Keep the default order of categories, corresponding to the
  2630. sequence in `org-agenda-files'.
  2631. category-up Sort alphabetically by category, A-Z.
  2632. category-down Sort alphabetically by category, Z-A.
  2633. tag-up Sort alphabetically by last tag, A-Z.
  2634. tag-down Sort alphabetically by last tag, Z-A.
  2635. priority-up Sort numerically by priority, high priority last.
  2636. priority-down Sort numerically by priority, high priority first.
  2637. The different possibilities will be tried in sequence, and testing stops
  2638. if one comparison returns a \"not-equal\". For example, the default
  2639. '(time-up category-keep priority-down)
  2640. means: Pull out all entries having a specified time of day and sort them,
  2641. in order to make a time schedule for the current day the first thing in the
  2642. agenda listing for the day. Of the entries without a time indication, keep
  2643. the grouped in categories, don't sort the categories, but keep them in
  2644. the sequence given in `org-agenda-files'. Within each category sort by
  2645. priority.
  2646. Leaving out `category-keep' would mean that items will be sorted across
  2647. categories by priority.
  2648. Instead of a single list, this can also be a set of list for specific
  2649. contents, with a context symbol in the car of the list, any of
  2650. `agenda', `todo', `tags' for the corresponding agenda views."
  2651. :group 'org-agenda-sorting
  2652. :type `(choice
  2653. (repeat :tag "General" ,org-sorting-choice)
  2654. (list :tag "Individually"
  2655. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2656. (repeat ,org-sorting-choice))
  2657. (cons (const :tag "Strategy for TODO lists" todo)
  2658. (repeat ,org-sorting-choice))
  2659. (cons (const :tag "Strategy for Tags matches" tags)
  2660. (repeat ,org-sorting-choice)))))
  2661. (defcustom org-sort-agenda-notime-is-late t
  2662. "Non-nil means, items without time are considered late.
  2663. This is only relevant for sorting. When t, items which have no explicit
  2664. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2665. do have a time. When nil, the default time is before 0:00. You can use this
  2666. option to decide if the schedule for today should come before or after timeless
  2667. agenda entries."
  2668. :group 'org-agenda-sorting
  2669. :type 'boolean)
  2670. (defgroup org-agenda-line-format nil
  2671. "Options concerning the entry prefix in the Org-mode agenda display."
  2672. :tag "Org Agenda Line Format"
  2673. :group 'org-agenda)
  2674. (defcustom org-agenda-prefix-format
  2675. '((agenda . " %-12:c%?-12t% s")
  2676. (timeline . " % s")
  2677. (todo . " %-12:c")
  2678. (tags . " %-12:c")
  2679. (search . " %-12:c"))
  2680. "Format specifications for the prefix of items in the agenda views.
  2681. An alist with four entries, for the different agenda types. The keys to the
  2682. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2683. are format strings.
  2684. This format works similar to a printf format, with the following meaning:
  2685. %c the category of the item, \"Diary\" for entries from the diary, or
  2686. as given by the CATEGORY keyword or derived from the file name.
  2687. %T the *last* tag of the item. Last because inherited tags come
  2688. first in the list.
  2689. %t the time-of-day specification if one applies to the entry, in the
  2690. format HH:MM
  2691. %s Scheduling/Deadline information, a short string
  2692. All specifiers work basically like the standard `%s' of printf, but may
  2693. contain two additional characters: A question mark just after the `%' and
  2694. a whitespace/punctuation character just before the final letter.
  2695. If the first character after `%' is a question mark, the entire field
  2696. will only be included if the corresponding value applies to the
  2697. current entry. This is useful for fields which should have fixed
  2698. width when present, but zero width when absent. For example,
  2699. \"%?-12t\" will result in a 12 character time field if a time of the
  2700. day is specified, but will completely disappear in entries which do
  2701. not contain a time.
  2702. If there is punctuation or whitespace character just before the final
  2703. format letter, this character will be appended to the field value if
  2704. the value is not empty. For example, the format \"%-12:c\" leads to
  2705. \"Diary: \" if the category is \"Diary\". If the category were be
  2706. empty, no additional colon would be interted.
  2707. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2708. - Indent the line with two space characters
  2709. - Give the category in a 12 chars wide field, padded with whitespace on
  2710. the right (because of `-'). Append a colon if there is a category
  2711. (because of `:').
  2712. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2713. time, don't put in an empty field, just skip it (because of '?').
  2714. - Finally, put the scheduling information and append a whitespace.
  2715. As another example, if you don't want the time-of-day of entries in
  2716. the prefix, you could use:
  2717. (setq org-agenda-prefix-format \" %-11:c% s\")
  2718. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2719. `org-agenda-remove-tags'."
  2720. :type '(choice
  2721. (string :tag "General format")
  2722. (list :greedy t :tag "View dependent"
  2723. (cons (const agenda) (string :tag "Format"))
  2724. (cons (const timeline) (string :tag "Format"))
  2725. (cons (const todo) (string :tag "Format"))
  2726. (cons (const tags) (string :tag "Format"))
  2727. (cons (const search) (string :tag "Format"))))
  2728. :group 'org-agenda-line-format)
  2729. (defvar org-prefix-format-compiled nil
  2730. "The compiled version of the most recently used prefix format.
  2731. See the variable `org-agenda-prefix-format'.")
  2732. (defcustom org-agenda-todo-keyword-format "%-1s"
  2733. "Format for the TODO keyword in agenda lines.
  2734. Set this to something like \"%-12s\" if you want all TODO keywords
  2735. to occupy a fixed space in the agenda display."
  2736. :group 'org-agenda-line-format
  2737. :type 'string)
  2738. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2739. "Text preceeding scheduled items in the agenda view.
  2740. This is a list with two strings. The first applies when the item is
  2741. scheduled on the current day. The second applies when it has been scheduled
  2742. previously, it may contain a %d to capture how many days ago the item was
  2743. scheduled."
  2744. :group 'org-agenda-line-format
  2745. :type '(list
  2746. (string :tag "Scheduled today ")
  2747. (string :tag "Scheduled previously")))
  2748. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2749. "Text preceeding deadline items in the agenda view.
  2750. This is a list with two strings. The first applies when the item has its
  2751. deadline on the current day. The second applies when it is in the past or
  2752. in the future, it may contain %d to capture how many days away the deadline
  2753. is (was)."
  2754. :group 'org-agenda-line-format
  2755. :type '(list
  2756. (string :tag "Deadline today ")
  2757. (string :tag "Deadline relative")))
  2758. (defcustom org-agenda-remove-times-when-in-prefix t
  2759. "Non-nil means, remove duplicate time specifications in agenda items.
  2760. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2761. time-of-day specification in a headline or diary entry is extracted and
  2762. placed into the prefix. If this option is non-nil, the original specification
  2763. \(a timestamp or -range, or just a plain time(range) specification like
  2764. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2765. cluttered.
  2766. The option can be t or nil. It may also be the symbol `beg', indicating
  2767. that the time should only be removed what it is located at the beginning of
  2768. the headline/diary entry."
  2769. :group 'org-agenda-line-format
  2770. :type '(choice
  2771. (const :tag "Always" t)
  2772. (const :tag "Never" nil)
  2773. (const :tag "When at beginning of entry" beg)))
  2774. (defcustom org-agenda-default-appointment-duration nil
  2775. "Default duration for appointments that only have a starting time.
  2776. When nil, no duration is specified in such cases.
  2777. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2778. :group 'org-agenda-line-format
  2779. :type '(choice
  2780. (integer :tag "Minutes")
  2781. (const :tag "No default duration")))
  2782. (defcustom org-agenda-remove-tags nil
  2783. "Non-nil means, remove the tags from the headline copy in the agenda.
  2784. When this is the symbol `prefix', only remove tags when
  2785. `org-agenda-prefix-format' contains a `%T' specifier."
  2786. :group 'org-agenda-line-format
  2787. :type '(choice
  2788. (const :tag "Always" t)
  2789. (const :tag "Never" nil)
  2790. (const :tag "When prefix format contains %T" prefix)))
  2791. (if (fboundp 'defvaralias)
  2792. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2793. 'org-agenda-remove-tags))
  2794. (defcustom org-agenda-tags-column -80
  2795. "Shift tags in agenda items to this column.
  2796. If this number is positive, it specifies the column. If it is negative,
  2797. it means that the tags should be flushright to that column. For example,
  2798. -80 works well for a normal 80 character screen."
  2799. :group 'org-agenda-line-format
  2800. :type 'integer)
  2801. (if (fboundp 'defvaralias)
  2802. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2803. (defcustom org-agenda-fontify-priorities t
  2804. "Non-nil means, highlight low and high priorities in agenda.
  2805. When t, the highest priority entries are bold, lowest priority italic.
  2806. This may also be an association list of priority faces. The face may be
  2807. a names face, or a list like `(:background \"Red\")'."
  2808. :group 'org-agenda-line-format
  2809. :type '(choice
  2810. (const :tag "Never" nil)
  2811. (const :tag "Defaults" t)
  2812. (repeat :tag "Specify"
  2813. (list (character :tag "Priority" :value ?A)
  2814. (sexp :tag "face")))))
  2815. (defgroup org-latex nil
  2816. "Options for embedding LaTeX code into Org-mode."
  2817. :tag "Org LaTeX"
  2818. :group 'org)
  2819. (defcustom org-format-latex-options
  2820. '(:foreground default :background default :scale 1.0
  2821. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2822. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2823. "Options for creating images from LaTeX fragments.
  2824. This is a property list with the following properties:
  2825. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2826. `default' means use the forground of the default face.
  2827. :background the background color, or \"Transparent\".
  2828. `default' means use the background of the default face.
  2829. :scale a scaling factor for the size of the images
  2830. :html-foreground, :html-background, :html-scale
  2831. The same numbers for HTML export.
  2832. :matchers a list indicating which matchers should be used to
  2833. find LaTeX fragments. Valid members of this list are:
  2834. \"begin\" find environments
  2835. \"$\" find math expressions surrounded by $...$
  2836. \"$$\" find math expressions surrounded by $$....$$
  2837. \"\\(\" find math expressions surrounded by \\(...\\)
  2838. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2839. :group 'org-latex
  2840. :type 'plist)
  2841. (defcustom org-format-latex-header "\\documentclass{article}
  2842. \\usepackage{fullpage} % do not remove
  2843. \\usepackage{amssymb}
  2844. \\usepackage[usenames]{color}
  2845. \\usepackage{amsmath}
  2846. \\usepackage{latexsym}
  2847. \\usepackage[mathscr]{eucal}
  2848. \\pagestyle{empty} % do not remove"
  2849. "The document header used for processing LaTeX fragments."
  2850. :group 'org-latex
  2851. :type 'string)
  2852. (defgroup org-export nil
  2853. "Options for exporting org-listings."
  2854. :tag "Org Export"
  2855. :group 'org)
  2856. (defgroup org-export-general nil
  2857. "General options for exporting Org-mode files."
  2858. :tag "Org Export General"
  2859. :group 'org-export)
  2860. ;; FIXME
  2861. (defvar org-export-publishing-directory nil)
  2862. (defcustom org-export-with-special-strings t
  2863. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2864. When this option is turned on, these strings will be exported as:
  2865. Org HTML LaTeX
  2866. -----+----------+--------
  2867. \\- &shy; \\-
  2868. -- &ndash; --
  2869. --- &mdash; ---
  2870. ... &hellip; \ldots
  2871. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2872. :group 'org-export-translation
  2873. :type 'boolean)
  2874. (defcustom org-export-language-setup
  2875. '(("en" "Author" "Date" "Table of Contents")
  2876. ("cs" "Autor" "Datum" "Obsah")
  2877. ("da" "Ophavsmand" "Dato" "Indhold")
  2878. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2879. ("es" "Autor" "Fecha" "\xcdndice")
  2880. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2881. ("it" "Autore" "Data" "Indice")
  2882. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2883. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2884. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2885. "Terms used in export text, translated to different languages.
  2886. Use the variable `org-export-default-language' to set the language,
  2887. or use the +OPTION lines for a per-file setting."
  2888. :group 'org-export-general
  2889. :type '(repeat
  2890. (list
  2891. (string :tag "HTML language tag")
  2892. (string :tag "Author")
  2893. (string :tag "Date")
  2894. (string :tag "Table of Contents"))))
  2895. (defcustom org-export-default-language "en"
  2896. "The default language of HTML export, as a string.
  2897. This should have an association in `org-export-language-setup'."
  2898. :group 'org-export-general
  2899. :type 'string)
  2900. (defcustom org-export-skip-text-before-1st-heading t
  2901. "Non-nil means, skip all text before the first headline when exporting.
  2902. When nil, that text is exported as well."
  2903. :group 'org-export-general
  2904. :type 'boolean)
  2905. (defcustom org-export-headline-levels 3
  2906. "The last level which is still exported as a headline.
  2907. Inferior levels will produce itemize lists when exported.
  2908. Note that a numeric prefix argument to an exporter function overrides
  2909. this setting.
  2910. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2911. :group 'org-export-general
  2912. :type 'number)
  2913. (defcustom org-export-with-section-numbers t
  2914. "Non-nil means, add section numbers to headlines when exporting.
  2915. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2916. :group 'org-export-general
  2917. :type 'boolean)
  2918. (defcustom org-export-with-toc t
  2919. "Non-nil means, create a table of contents in exported files.
  2920. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2921. When an integer, include levels up to N in the toc, this may then be
  2922. different from `org-export-headline-levels', but it will not be allowed
  2923. to be larger than the number of headline levels.
  2924. When nil, no table of contents is made.
  2925. Headlines which contain any TODO items will be marked with \"(*)\" in
  2926. ASCII export, and with red color in HTML output, if the option
  2927. `org-export-mark-todo-in-toc' is set.
  2928. In HTML output, the TOC will be clickable.
  2929. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2930. or \"toc:3\"."
  2931. :group 'org-export-general
  2932. :type '(choice
  2933. (const :tag "No Table of Contents" nil)
  2934. (const :tag "Full Table of Contents" t)
  2935. (integer :tag "TOC to level")))
  2936. (defcustom org-export-mark-todo-in-toc nil
  2937. "Non-nil means, mark TOC lines that contain any open TODO items."
  2938. :group 'org-export-general
  2939. :type 'boolean)
  2940. (defcustom org-export-preserve-breaks nil
  2941. "Non-nil means, preserve all line breaks when exporting.
  2942. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2943. export, line breaks will always be preserved, regardless of this variable.
  2944. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2945. :group 'org-export-general
  2946. :type 'boolean)
  2947. (defcustom org-export-with-archived-trees 'headline
  2948. "Whether subtrees with the ARCHIVE tag should be exported.
  2949. This can have three different values
  2950. nil Do not export, pretend this tree is not present
  2951. t Do export the entire tree
  2952. headline Only export the headline, but skip the tree below it."
  2953. :group 'org-export-general
  2954. :group 'org-archive
  2955. :type '(choice
  2956. (const :tag "not at all" nil)
  2957. (const :tag "headline only" 'headline)
  2958. (const :tag "entirely" t)))
  2959. (defcustom org-export-author-info t
  2960. "Non-nil means, insert author name and email into the exported file.
  2961. This option can also be set with the +OPTIONS line,
  2962. e.g. \"author-info:nil\"."
  2963. :group 'org-export-general
  2964. :type 'boolean)
  2965. (defcustom org-export-time-stamp-file t
  2966. "Non-nil means, insert a time stamp into the exported file.
  2967. The time stamp shows when the file was created.
  2968. This option can also be set with the +OPTIONS line,
  2969. e.g. \"timestamp:nil\"."
  2970. :group 'org-export-general
  2971. :type 'boolean)
  2972. (defcustom org-export-with-timestamps t
  2973. "If nil, do not export time stamps and associated keywords."
  2974. :group 'org-export-general
  2975. :type 'boolean)
  2976. (defcustom org-export-remove-timestamps-from-toc t
  2977. "If nil, remove timestamps from the table of contents entries."
  2978. :group 'org-export-general
  2979. :type 'boolean)
  2980. (defcustom org-export-with-tags 'not-in-toc
  2981. "If nil, do not export tags, just remove them from headlines.
  2982. If this is the symbol `not-in-toc', tags will be removed from table of
  2983. contents entries, but still be shown in the headlines of the document.
  2984. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2985. :group 'org-export-general
  2986. :type '(choice
  2987. (const :tag "Off" nil)
  2988. (const :tag "Not in TOC" not-in-toc)
  2989. (const :tag "On" t)))
  2990. (defcustom org-export-with-drawers nil
  2991. "Non-nil means, export with drawers like the property drawer.
  2992. When t, all drawers are exported. This may also be a list of
  2993. drawer names to export."
  2994. :group 'org-export-general
  2995. :type '(choice
  2996. (const :tag "All drawers" t)
  2997. (const :tag "None" nil)
  2998. (repeat :tag "Selected drawers"
  2999. (string :tag "Drawer name"))))
  3000. (defgroup org-export-translation nil
  3001. "Options for translating special ascii sequences for the export backends."
  3002. :tag "Org Export Translation"
  3003. :group 'org-export)
  3004. (defcustom org-export-with-emphasize t
  3005. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  3006. If the export target supports emphasizing text, the word will be
  3007. typeset in bold, italic, or underlined, respectively. Works only for
  3008. single words, but you can say: I *really* *mean* *this*.
  3009. Not all export backends support this.
  3010. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  3011. :group 'org-export-translation
  3012. :type 'boolean)
  3013. (defcustom org-export-with-footnotes t
  3014. "If nil, export [1] as a footnote marker.
  3015. Lines starting with [1] will be formatted as footnotes.
  3016. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  3017. :group 'org-export-translation
  3018. :type 'boolean)
  3019. (defcustom org-export-with-sub-superscripts t
  3020. "Non-nil means, interpret \"_\" and \"^\" for export.
  3021. When this option is turned on, you can use TeX-like syntax for sub- and
  3022. superscripts. Several characters after \"_\" or \"^\" will be
  3023. considered as a single item - so grouping with {} is normally not
  3024. needed. For example, the following things will be parsed as single
  3025. sub- or superscripts.
  3026. 10^24 or 10^tau several digits will be considered 1 item.
  3027. 10^-12 or 10^-tau a leading sign with digits or a word
  3028. x^2-y^3 will be read as x^2 - y^3, because items are
  3029. terminated by almost any nonword/nondigit char.
  3030. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  3031. Still, ambiguity is possible - so when in doubt use {} to enclose the
  3032. sub/superscript. If you set this variable to the symbol `{}',
  3033. the braces are *required* in order to trigger interpretations as
  3034. sub/superscript. This can be helpful in documents that need \"_\"
  3035. frequently in plain text.
  3036. Not all export backends support this, but HTML does.
  3037. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  3038. :group 'org-export-translation
  3039. :type '(choice
  3040. (const :tag "Always interpret" t)
  3041. (const :tag "Only with braces" {})
  3042. (const :tag "Never interpret" nil)))
  3043. (defcustom org-export-with-special-strings t
  3044. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  3045. When this option is turned on, these strings will be exported as:
  3046. \\- : &shy;
  3047. -- : &ndash;
  3048. --- : &mdash;
  3049. Not all export backends support this, but HTML does.
  3050. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  3051. :group 'org-export-translation
  3052. :type 'boolean)
  3053. (defcustom org-export-with-TeX-macros t
  3054. "Non-nil means, interpret simple TeX-like macros when exporting.
  3055. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  3056. No only real TeX macros will work here, but the standard HTML entities
  3057. for math can be used as macro names as well. For a list of supported
  3058. names in HTML export, see the constant `org-html-entities'.
  3059. Not all export backends support this.
  3060. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  3061. :group 'org-export-translation
  3062. :group 'org-export-latex
  3063. :type 'boolean)
  3064. (defcustom org-export-with-LaTeX-fragments nil
  3065. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  3066. When set, the exporter will find LaTeX environments if the \\begin line is
  3067. the first non-white thing on a line. It will also find the math delimiters
  3068. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  3069. display math.
  3070. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  3071. :group 'org-export-translation
  3072. :group 'org-export-latex
  3073. :type 'boolean)
  3074. (defcustom org-export-with-fixed-width t
  3075. "Non-nil means, lines starting with \":\" will be in fixed width font.
  3076. This can be used to have pre-formatted text, fragments of code etc. For
  3077. example:
  3078. : ;; Some Lisp examples
  3079. : (while (defc cnt)
  3080. : (ding))
  3081. will be looking just like this in also HTML. See also the QUOTE keyword.
  3082. Not all export backends support this.
  3083. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  3084. :group 'org-export-translation
  3085. :type 'boolean)
  3086. (defcustom org-match-sexp-depth 3
  3087. "Number of stacked braces for sub/superscript matching.
  3088. This has to be set before loading org.el to be effective."
  3089. :group 'org-export-translation
  3090. :type 'integer)
  3091. (defgroup org-export-tables nil
  3092. "Options for exporting tables in Org-mode."
  3093. :tag "Org Export Tables"
  3094. :group 'org-export)
  3095. (defcustom org-export-with-tables t
  3096. "If non-nil, lines starting with \"|\" define a table.
  3097. For example:
  3098. | Name | Address | Birthday |
  3099. |-------------+----------+-----------|
  3100. | Arthur Dent | England | 29.2.2100 |
  3101. Not all export backends support this.
  3102. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  3103. :group 'org-export-tables
  3104. :type 'boolean)
  3105. (defcustom org-export-highlight-first-table-line t
  3106. "Non-nil means, highlight the first table line.
  3107. In HTML export, this means use <th> instead of <td>.
  3108. In tables created with table.el, this applies to the first table line.
  3109. In Org-mode tables, all lines before the first horizontal separator
  3110. line will be formatted with <th> tags."
  3111. :group 'org-export-tables
  3112. :type 'boolean)
  3113. (defcustom org-export-table-remove-special-lines t
  3114. "Remove special lines and marking characters in calculating tables.
  3115. This removes the special marking character column from tables that are set
  3116. up for spreadsheet calculations. It also removes the entire lines
  3117. marked with `!', `_', or `^'. The lines with `$' are kept, because
  3118. the values of constants may be useful to have."
  3119. :group 'org-export-tables
  3120. :type 'boolean)
  3121. (defcustom org-export-prefer-native-exporter-for-tables nil
  3122. "Non-nil means, always export tables created with table.el natively.
  3123. Natively means, use the HTML code generator in table.el.
  3124. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  3125. the table does not use row- or column-spanning). This has the
  3126. advantage, that the automatic HTML conversions for math symbols and
  3127. sub/superscripts can be applied. Org-mode's HTML generator is also
  3128. much faster."
  3129. :group 'org-export-tables
  3130. :type 'boolean)
  3131. (defgroup org-export-ascii nil
  3132. "Options specific for ASCII export of Org-mode files."
  3133. :tag "Org Export ASCII"
  3134. :group 'org-export)
  3135. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  3136. "Characters for underlining headings in ASCII export.
  3137. In the given sequence, these characters will be used for level 1, 2, ..."
  3138. :group 'org-export-ascii
  3139. :type '(repeat character))
  3140. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  3141. "Bullet characters for headlines converted to lists in ASCII export.
  3142. The first character is used for the first lest level generated in this
  3143. way, and so on. If there are more levels than characters given here,
  3144. the list will be repeated.
  3145. Note that plain lists will keep the same bullets as the have in the
  3146. Org-mode file."
  3147. :group 'org-export-ascii
  3148. :type '(repeat character))
  3149. (defgroup org-export-xml nil
  3150. "Options specific for XML export of Org-mode files."
  3151. :tag "Org Export XML"
  3152. :group 'org-export)
  3153. (defgroup org-export-html nil
  3154. "Options specific for HTML export of Org-mode files."
  3155. :tag "Org Export HTML"
  3156. :group 'org-export)
  3157. (defcustom org-export-html-coding-system nil
  3158. ""
  3159. :group 'org-export-html
  3160. :type 'coding-system)
  3161. (defcustom org-export-html-extension "html"
  3162. "The extension for exported HTML files."
  3163. :group 'org-export-html
  3164. :type 'string)
  3165. (defcustom org-export-html-style
  3166. "<style type=\"text/css\">
  3167. html {
  3168. font-family: Times, serif;
  3169. font-size: 12pt;
  3170. }
  3171. .title { text-align: center; }
  3172. .todo { color: red; }
  3173. .done { color: green; }
  3174. .timestamp { color: grey }
  3175. .timestamp-kwd { color: CadetBlue }
  3176. .tag { background-color:lightblue; font-weight:normal }
  3177. .target { background-color: lavender; }
  3178. pre {
  3179. border: 1pt solid #AEBDCC;
  3180. background-color: #F3F5F7;
  3181. padding: 5pt;
  3182. font-family: courier, monospace;
  3183. }
  3184. table { border-collapse: collapse; }
  3185. td, th {
  3186. vertical-align: top;
  3187. <!--border: 1pt solid #ADB9CC;-->
  3188. }
  3189. </style>"
  3190. "The default style specification for exported HTML files.
  3191. Since there are different ways of setting style information, this variable
  3192. needs to contain the full HTML structure to provide a style, including the
  3193. surrounding HTML tags. The style specifications should include definitions
  3194. for new classes todo, done, title, and deadline. For example, valid values
  3195. would be:
  3196. <style type=\"text/css\">
  3197. p { font-weight: normal; color: gray; }
  3198. h1 { color: black; }
  3199. .title { text-align: center; }
  3200. .todo, .deadline { color: red; }
  3201. .done { color: green; }
  3202. </style>
  3203. or, if you want to keep the style in a file,
  3204. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3205. As the value of this option simply gets inserted into the HTML <head> header,
  3206. you can \"misuse\" it to add arbitrary text to the header."
  3207. :group 'org-export-html
  3208. :type 'string)
  3209. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3210. "Format for typesetting the document title in HTML export."
  3211. :group 'org-export-html
  3212. :type 'string)
  3213. (defcustom org-export-html-toplevel-hlevel 2
  3214. "The <H> level for level 1 headings in HTML export."
  3215. :group 'org-export-html
  3216. :type 'string)
  3217. (defcustom org-export-html-link-org-files-as-html t
  3218. "Non-nil means, make file links to `file.org' point to `file.html'.
  3219. When org-mode is exporting an org-mode file to HTML, links to
  3220. non-html files are directly put into a href tag in HTML.
  3221. However, links to other Org-mode files (recognized by the
  3222. extension `.org.) should become links to the corresponding html
  3223. file, assuming that the linked org-mode file will also be
  3224. converted to HTML.
  3225. When nil, the links still point to the plain `.org' file."
  3226. :group 'org-export-html
  3227. :type 'boolean)
  3228. (defcustom org-export-html-inline-images 'maybe
  3229. "Non-nil means, inline images into exported HTML pages.
  3230. This is done using an <img> tag. When nil, an anchor with href is used to
  3231. link to the image. If this option is `maybe', then images in links with
  3232. an empty description will be inlined, while images with a description will
  3233. be linked only."
  3234. :group 'org-export-html
  3235. :type '(choice (const :tag "Never" nil)
  3236. (const :tag "Always" t)
  3237. (const :tag "When there is no description" maybe)))
  3238. ;; FIXME: rename
  3239. (defcustom org-export-html-expand t
  3240. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3241. When nil, these tags will be exported as plain text and therefore
  3242. not be interpreted by a browser.
  3243. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3244. :group 'org-export-html
  3245. :type 'boolean)
  3246. (defcustom org-export-html-table-tag
  3247. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3248. "The HTML tag that is used to start a table.
  3249. This must be a <table> tag, but you may change the options like
  3250. borders and spacing."
  3251. :group 'org-export-html
  3252. :type 'string)
  3253. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3254. "The opening tag for table header fields.
  3255. This is customizable so that alignment options can be specified."
  3256. :group 'org-export-tables
  3257. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3258. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3259. "The opening tag for table data fields.
  3260. This is customizable so that alignment options can be specified."
  3261. :group 'org-export-tables
  3262. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3263. (defcustom org-export-html-with-timestamp nil
  3264. "If non-nil, write `org-export-html-html-helper-timestamp'
  3265. into the exported HTML text. Otherwise, the buffer will just be saved
  3266. to a file."
  3267. :group 'org-export-html
  3268. :type 'boolean)
  3269. (defcustom org-export-html-html-helper-timestamp
  3270. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3271. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3272. :group 'org-export-html
  3273. :type 'string)
  3274. (defgroup org-export-icalendar nil
  3275. "Options specific for iCalendar export of Org-mode files."
  3276. :tag "Org Export iCalendar"
  3277. :group 'org-export)
  3278. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3279. "The file name for the iCalendar file covering all agenda files.
  3280. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3281. The file name should be absolute, the file will be overwritten without warning."
  3282. :group 'org-export-icalendar
  3283. :type 'file)
  3284. (defcustom org-icalendar-include-todo nil
  3285. "Non-nil means, export to iCalendar files should also cover TODO items."
  3286. :group 'org-export-icalendar
  3287. :type '(choice
  3288. (const :tag "None" nil)
  3289. (const :tag "Unfinished" t)
  3290. (const :tag "All" all)))
  3291. (defcustom org-icalendar-include-sexps t
  3292. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3293. These are entries like in the diary, but directly in an Org-mode file."
  3294. :group 'org-export-icalendar
  3295. :type 'boolean)
  3296. (defcustom org-icalendar-include-body 100
  3297. "Amount of text below headline to be included in iCalendar export.
  3298. This is a number of characters that should maximally be included.
  3299. Properties, scheduling and clocking lines will always be removed.
  3300. The text will be inserted into the DESCRIPTION field."
  3301. :group 'org-export-icalendar
  3302. :type '(choice
  3303. (const :tag "Nothing" nil)
  3304. (const :tag "Everything" t)
  3305. (integer :tag "Max characters")))
  3306. (defcustom org-icalendar-combined-name "OrgMode"
  3307. "Calendar name for the combined iCalendar representing all agenda files."
  3308. :group 'org-export-icalendar
  3309. :type 'string)
  3310. (defgroup org-font-lock nil
  3311. "Font-lock settings for highlighting in Org-mode."
  3312. :tag "Org Font Lock"
  3313. :group 'org)
  3314. (defcustom org-level-color-stars-only nil
  3315. "Non-nil means fontify only the stars in each headline.
  3316. When nil, the entire headline is fontified.
  3317. Changing it requires restart of `font-lock-mode' to become effective
  3318. also in regions already fontified."
  3319. :group 'org-font-lock
  3320. :type 'boolean)
  3321. (defcustom org-hide-leading-stars nil
  3322. "Non-nil means, hide the first N-1 stars in a headline.
  3323. This works by using the face `org-hide' for these stars. This
  3324. face is white for a light background, and black for a dark
  3325. background. You may have to customize the face `org-hide' to
  3326. make this work.
  3327. Changing it requires restart of `font-lock-mode' to become effective
  3328. also in regions already fontified.
  3329. You may also set this on a per-file basis by adding one of the following
  3330. lines to the buffer:
  3331. #+STARTUP: hidestars
  3332. #+STARTUP: showstars"
  3333. :group 'org-font-lock
  3334. :type 'boolean)
  3335. (defcustom org-fontify-done-headline nil
  3336. "Non-nil means, change the face of a headline if it is marked DONE.
  3337. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3338. When this is non-nil, the headline after the keyword is set to the
  3339. `org-headline-done' as an additional indication."
  3340. :group 'org-font-lock
  3341. :type 'boolean)
  3342. (defcustom org-fontify-emphasized-text t
  3343. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3344. Changing this variable requires a restart of Emacs to take effect."
  3345. :group 'org-font-lock
  3346. :type 'boolean)
  3347. (defcustom org-highlight-latex-fragments-and-specials nil
  3348. "Non-nil means, fontify what is treated specially by the exporters."
  3349. :group 'org-font-lock
  3350. :type 'boolean)
  3351. (defcustom org-hide-emphasis-markers nil
  3352. "Non-nil mean font-lock should hide the emphasis marker characters."
  3353. :group 'org-font-lock
  3354. :type 'boolean)
  3355. (defvar org-emph-re nil
  3356. "Regular expression for matching emphasis.")
  3357. (defvar org-verbatim-re nil
  3358. "Regular expression for matching verbatim text.")
  3359. (defvar org-emphasis-regexp-components) ; defined just below
  3360. (defvar org-emphasis-alist) ; defined just below
  3361. (defun org-set-emph-re (var val)
  3362. "Set variable and compute the emphasis regular expression."
  3363. (set var val)
  3364. (when (and (boundp 'org-emphasis-alist)
  3365. (boundp 'org-emphasis-regexp-components)
  3366. org-emphasis-alist org-emphasis-regexp-components)
  3367. (let* ((e org-emphasis-regexp-components)
  3368. (pre (car e))
  3369. (post (nth 1 e))
  3370. (border (nth 2 e))
  3371. (body (nth 3 e))
  3372. (nl (nth 4 e))
  3373. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3374. (body1 (concat body "*?"))
  3375. (markers (mapconcat 'car org-emphasis-alist ""))
  3376. (vmarkers (mapconcat
  3377. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3378. org-emphasis-alist "")))
  3379. ;; make sure special characters appear at the right position in the class
  3380. (if (string-match "\\^" markers)
  3381. (setq markers (concat (replace-match "" t t markers) "^")))
  3382. (if (string-match "-" markers)
  3383. (setq markers (concat (replace-match "" t t markers) "-")))
  3384. (if (string-match "\\^" vmarkers)
  3385. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3386. (if (string-match "-" vmarkers)
  3387. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3388. (if (> nl 0)
  3389. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3390. (int-to-string nl) "\\}")))
  3391. ;; Make the regexp
  3392. (setq org-emph-re
  3393. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3394. "\\("
  3395. "\\([" markers "]\\)"
  3396. "\\("
  3397. "[^" border "]\\|"
  3398. "[^" border (if (and nil stacked) markers) "]"
  3399. body1
  3400. "[^" border (if (and nil stacked) markers) "]"
  3401. "\\)"
  3402. "\\3\\)"
  3403. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3404. (setq org-verbatim-re
  3405. (concat "\\([" pre "]\\|^\\)"
  3406. "\\("
  3407. "\\([" vmarkers "]\\)"
  3408. "\\("
  3409. "[^" border "]\\|"
  3410. "[^" border "]"
  3411. body1
  3412. "[^" border "]"
  3413. "\\)"
  3414. "\\3\\)"
  3415. "\\([" post "]\\|$\\)")))))
  3416. (defcustom org-emphasis-regexp-components
  3417. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3418. "Components used to build the regular expression for emphasis.
  3419. This is a list with 6 entries. Terminology: In an emphasis string
  3420. like \" *strong word* \", we call the initial space PREMATCH, the final
  3421. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3422. and \"trong wor\" is the body. The different components in this variable
  3423. specify what is allowed/forbidden in each part:
  3424. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3425. post Chars allowed as postmatch. End of line will be allowed too.
  3426. border The chars *forbidden* as border characters.
  3427. body-regexp A regexp like \".\" to match a body character. Don't use
  3428. non-shy groups here, and don't allow newline here.
  3429. newline The maximum number of newlines allowed in an emphasis exp.
  3430. Use customize to modify this, or restart Emacs after changing it."
  3431. :group 'org-font-lock
  3432. :set 'org-set-emph-re
  3433. :type '(list
  3434. (sexp :tag "Allowed chars in pre ")
  3435. (sexp :tag "Allowed chars in post ")
  3436. (sexp :tag "Forbidden chars in border ")
  3437. (sexp :tag "Regexp for body ")
  3438. (integer :tag "number of newlines allowed")
  3439. (option (boolean :tag "Stacking (DISABLED) "))))
  3440. (defcustom org-emphasis-alist
  3441. '(("*" bold "<b>" "</b>")
  3442. ("/" italic "<i>" "</i>")
  3443. ("_" underline "<u>" "</u>")
  3444. ("=" org-code "<code>" "</code>" verbatim)
  3445. ("~" org-verbatim "" "" verbatim)
  3446. ("+" (:strike-through t) "<del>" "</del>")
  3447. )
  3448. "Special syntax for emphasized text.
  3449. Text starting and ending with a special character will be emphasized, for
  3450. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3451. characters, the face to be used by font-lock for highlighting in Org-mode
  3452. Emacs buffers, and the HTML tags to be used for this.
  3453. Use customize to modify this, or restart Emacs after changing it."
  3454. :group 'org-font-lock
  3455. :set 'org-set-emph-re
  3456. :type '(repeat
  3457. (list
  3458. (string :tag "Marker character")
  3459. (choice
  3460. (face :tag "Font-lock-face")
  3461. (plist :tag "Face property list"))
  3462. (string :tag "HTML start tag")
  3463. (string :tag "HTML end tag")
  3464. (option (const verbatim)))))
  3465. ;;; The faces
  3466. (defgroup org-faces nil
  3467. "Faces in Org-mode."
  3468. :tag "Org Faces"
  3469. :group 'org-font-lock)
  3470. (defun org-compatible-face (inherits specs)
  3471. "Make a compatible face specification.
  3472. If INHERITS is an existing face and if the Emacs version supports it,
  3473. just inherit the face. If not, use SPECS to define the face.
  3474. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3475. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3476. to the top of the list. The `min-colors' attribute will be removed from
  3477. any other entries, and any resulting duplicates will be removed entirely."
  3478. (cond
  3479. ((and inherits (facep inherits)
  3480. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3481. ;; In Emacs 23, we use inheritance where possible.
  3482. ;; We only do this in Emacs 23, because only there the outline
  3483. ;; faces have been changed to the original org-mode-level-faces.
  3484. (list (list t :inherit inherits)))
  3485. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3486. ;; These do not understand the `min-colors' attribute.
  3487. (let (r e a)
  3488. (while (setq e (pop specs))
  3489. (cond
  3490. ((memq (car e) '(t default)) (push e r))
  3491. ((setq a (member '(min-colors 8) (car e)))
  3492. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3493. (cdr e)))))
  3494. ((setq a (assq 'min-colors (car e)))
  3495. (setq e (cons (delq a (car e)) (cdr e)))
  3496. (or (assoc (car e) r) (push e r)))
  3497. (t (or (assoc (car e) r) (push e r)))))
  3498. (nreverse r)))
  3499. (t specs)))
  3500. (put 'org-compatible-face 'lisp-indent-function 1)
  3501. (defface org-hide
  3502. '((((background light)) (:foreground "white"))
  3503. (((background dark)) (:foreground "black")))
  3504. "Face used to hide leading stars in headlines.
  3505. The forground color of this face should be equal to the background
  3506. color of the frame."
  3507. :group 'org-faces)
  3508. (defface org-level-1 ;; font-lock-function-name-face
  3509. (org-compatible-face 'outline-1
  3510. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3511. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3512. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3513. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3514. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3515. (t (:bold t))))
  3516. "Face used for level 1 headlines."
  3517. :group 'org-faces)
  3518. (defface org-level-2 ;; font-lock-variable-name-face
  3519. (org-compatible-face 'outline-2
  3520. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3521. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3522. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3523. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3524. (t (:bold t))))
  3525. "Face used for level 2 headlines."
  3526. :group 'org-faces)
  3527. (defface org-level-3 ;; font-lock-keyword-face
  3528. (org-compatible-face 'outline-3
  3529. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3530. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3531. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3532. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3533. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3534. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3535. (t (:bold t))))
  3536. "Face used for level 3 headlines."
  3537. :group 'org-faces)
  3538. (defface org-level-4 ;; font-lock-comment-face
  3539. (org-compatible-face 'outline-4
  3540. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3541. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3542. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3543. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3544. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3545. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3546. (t (:bold t))))
  3547. "Face used for level 4 headlines."
  3548. :group 'org-faces)
  3549. (defface org-level-5 ;; font-lock-type-face
  3550. (org-compatible-face 'outline-5
  3551. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3552. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3553. (((class color) (min-colors 8)) (:foreground "green"))))
  3554. "Face used for level 5 headlines."
  3555. :group 'org-faces)
  3556. (defface org-level-6 ;; font-lock-constant-face
  3557. (org-compatible-face 'outline-6
  3558. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3559. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3560. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3561. "Face used for level 6 headlines."
  3562. :group 'org-faces)
  3563. (defface org-level-7 ;; font-lock-builtin-face
  3564. (org-compatible-face 'outline-7
  3565. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3566. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3567. (((class color) (min-colors 8)) (:foreground "blue"))))
  3568. "Face used for level 7 headlines."
  3569. :group 'org-faces)
  3570. (defface org-level-8 ;; font-lock-string-face
  3571. (org-compatible-face 'outline-8
  3572. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3573. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3574. (((class color) (min-colors 8)) (:foreground "green"))))
  3575. "Face used for level 8 headlines."
  3576. :group 'org-faces)
  3577. (defface org-special-keyword ;; font-lock-string-face
  3578. (org-compatible-face nil
  3579. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3580. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3581. (t (:italic t))))
  3582. "Face used for special keywords."
  3583. :group 'org-faces)
  3584. (defface org-drawer ;; font-lock-function-name-face
  3585. (org-compatible-face nil
  3586. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3587. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3588. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3589. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3590. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3591. (t (:bold t))))
  3592. "Face used for drawers."
  3593. :group 'org-faces)
  3594. (defface org-property-value nil
  3595. "Face used for the value of a property."
  3596. :group 'org-faces)
  3597. (defface org-column
  3598. (org-compatible-face nil
  3599. '((((class color) (min-colors 16) (background light))
  3600. (:background "grey90"))
  3601. (((class color) (min-colors 16) (background dark))
  3602. (:background "grey30"))
  3603. (((class color) (min-colors 8))
  3604. (:background "cyan" :foreground "black"))
  3605. (t (:inverse-video t))))
  3606. "Face for column display of entry properties."
  3607. :group 'org-faces)
  3608. (when (fboundp 'set-face-attribute)
  3609. ;; Make sure that a fixed-width face is used when we have a column table.
  3610. (set-face-attribute 'org-column nil
  3611. :height (face-attribute 'default :height)
  3612. :family (face-attribute 'default :family)))
  3613. (defface org-warning
  3614. (org-compatible-face 'font-lock-warning-face
  3615. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3616. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3617. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3618. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3619. (t (:bold t))))
  3620. "Face for deadlines and TODO keywords."
  3621. :group 'org-faces)
  3622. (defface org-archived ; similar to shadow
  3623. (org-compatible-face 'shadow
  3624. '((((class color grayscale) (min-colors 88) (background light))
  3625. (:foreground "grey50"))
  3626. (((class color grayscale) (min-colors 88) (background dark))
  3627. (:foreground "grey70"))
  3628. (((class color) (min-colors 8) (background light))
  3629. (:foreground "green"))
  3630. (((class color) (min-colors 8) (background dark))
  3631. (:foreground "yellow"))))
  3632. "Face for headline with the ARCHIVE tag."
  3633. :group 'org-faces)
  3634. (defface org-link
  3635. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3636. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3637. (t (:underline t)))
  3638. "Face for links."
  3639. :group 'org-faces)
  3640. (defface org-ellipsis
  3641. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3642. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3643. (t (:strike-through t)))
  3644. "Face for the ellipsis in folded text."
  3645. :group 'org-faces)
  3646. (defface org-target
  3647. '((((class color) (background light)) (:underline t))
  3648. (((class color) (background dark)) (:underline t))
  3649. (t (:underline t)))
  3650. "Face for links."
  3651. :group 'org-faces)
  3652. (defface org-date
  3653. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3654. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3655. (t (:underline t)))
  3656. "Face for links."
  3657. :group 'org-faces)
  3658. (defface org-sexp-date
  3659. '((((class color) (background light)) (:foreground "Purple"))
  3660. (((class color) (background dark)) (:foreground "Cyan"))
  3661. (t (:underline t)))
  3662. "Face for links."
  3663. :group 'org-faces)
  3664. (defface org-tag
  3665. '((t (:bold t)))
  3666. "Face for tags."
  3667. :group 'org-faces)
  3668. (defface org-todo ; font-lock-warning-face
  3669. (org-compatible-face nil
  3670. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3671. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3672. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3673. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3674. (t (:inverse-video t :bold t))))
  3675. "Face for TODO keywords."
  3676. :group 'org-faces)
  3677. (defface org-done ;; font-lock-type-face
  3678. (org-compatible-face nil
  3679. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3680. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3681. (((class color) (min-colors 8)) (:foreground "green"))
  3682. (t (:bold t))))
  3683. "Face used for todo keywords that indicate DONE items."
  3684. :group 'org-faces)
  3685. (defface org-headline-done ;; font-lock-string-face
  3686. (org-compatible-face nil
  3687. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3688. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3689. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3690. "Face used to indicate that a headline is DONE.
  3691. This face is only used if `org-fontify-done-headline' is set. If applies
  3692. to the part of the headline after the DONE keyword."
  3693. :group 'org-faces)
  3694. (defcustom org-todo-keyword-faces nil
  3695. "Faces for specific TODO keywords.
  3696. This is a list of cons cells, with TODO keywords in the car
  3697. and faces in the cdr. The face can be a symbol, or a property
  3698. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3699. :group 'org-faces
  3700. :group 'org-todo
  3701. :type '(repeat
  3702. (cons
  3703. (string :tag "keyword")
  3704. (sexp :tag "face"))))
  3705. (defface org-table ;; font-lock-function-name-face
  3706. (org-compatible-face nil
  3707. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3708. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3709. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3710. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3711. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3712. (((class color) (min-colors 8) (background dark)))))
  3713. "Face used for tables."
  3714. :group 'org-faces)
  3715. (defface org-formula
  3716. (org-compatible-face nil
  3717. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3718. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3719. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3720. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3721. (t (:bold t :italic t))))
  3722. "Face for formulas."
  3723. :group 'org-faces)
  3724. (defface org-code
  3725. (org-compatible-face nil
  3726. '((((class color grayscale) (min-colors 88) (background light))
  3727. (:foreground "grey50"))
  3728. (((class color grayscale) (min-colors 88) (background dark))
  3729. (:foreground "grey70"))
  3730. (((class color) (min-colors 8) (background light))
  3731. (:foreground "green"))
  3732. (((class color) (min-colors 8) (background dark))
  3733. (:foreground "yellow"))))
  3734. "Face for fixed-with text like code snippets."
  3735. :group 'org-faces
  3736. :version "22.1")
  3737. (defface org-verbatim
  3738. (org-compatible-face nil
  3739. '((((class color grayscale) (min-colors 88) (background light))
  3740. (:foreground "grey50" :underline t))
  3741. (((class color grayscale) (min-colors 88) (background dark))
  3742. (:foreground "grey70" :underline t))
  3743. (((class color) (min-colors 8) (background light))
  3744. (:foreground "green" :underline t))
  3745. (((class color) (min-colors 8) (background dark))
  3746. (:foreground "yellow" :underline t))))
  3747. "Face for fixed-with text like code snippets."
  3748. :group 'org-faces
  3749. :version "22.1")
  3750. (defface org-agenda-structure ;; font-lock-function-name-face
  3751. (org-compatible-face nil
  3752. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3753. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3754. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3755. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3756. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3757. (t (:bold t))))
  3758. "Face used in agenda for captions and dates."
  3759. :group 'org-faces)
  3760. (defface org-scheduled-today
  3761. (org-compatible-face nil
  3762. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3763. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3764. (((class color) (min-colors 8)) (:foreground "green"))
  3765. (t (:bold t :italic t))))
  3766. "Face for items scheduled for a certain day."
  3767. :group 'org-faces)
  3768. (defface org-scheduled-previously
  3769. (org-compatible-face nil
  3770. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3771. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3772. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3773. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3774. (t (:bold t))))
  3775. "Face for items scheduled previously, and not yet done."
  3776. :group 'org-faces)
  3777. (defface org-upcoming-deadline
  3778. (org-compatible-face nil
  3779. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3780. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3781. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3782. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3783. (t (:bold t))))
  3784. "Face for items scheduled previously, and not yet done."
  3785. :group 'org-faces)
  3786. (defcustom org-agenda-deadline-faces
  3787. '((1.0 . org-warning)
  3788. (0.5 . org-upcoming-deadline)
  3789. (0.0 . default))
  3790. "Faces for showing deadlines in the agenda.
  3791. This is a list of cons cells. The cdr of each cell is a face to be used,
  3792. and it can also just be like '(:foreground \"yellow\").
  3793. Each car is a fraction of the head-warning time that must have passed for
  3794. this the face in the cdr to be used for display. The numbers must be
  3795. given in descending order. The head-warning time is normally taken
  3796. from `org-deadline-warning-days', but can also be specified in the deadline
  3797. timestamp itself, like this:
  3798. DEADLINE: <2007-08-13 Mon -8d>
  3799. You may use d for days, w for weeks, m for months and y for years. Months
  3800. and years will only be treated in an approximate fashion (30.4 days for a
  3801. month and 365.24 days for a year)."
  3802. :group 'org-faces
  3803. :group 'org-agenda-daily/weekly
  3804. :type '(repeat
  3805. (cons
  3806. (number :tag "Fraction of head-warning time passed")
  3807. (sexp :tag "Face"))))
  3808. ;; FIXME: this is not a good face yet.
  3809. (defface org-agenda-restriction-lock
  3810. (org-compatible-face nil
  3811. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3812. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3813. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3814. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3815. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3816. (t (:inverse-video t))))
  3817. "Face for showing the agenda restriction lock."
  3818. :group 'org-faces)
  3819. (defface org-time-grid ;; font-lock-variable-name-face
  3820. (org-compatible-face nil
  3821. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3822. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3823. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3824. "Face used for time grids."
  3825. :group 'org-faces)
  3826. (defconst org-level-faces
  3827. '(org-level-1 org-level-2 org-level-3 org-level-4
  3828. org-level-5 org-level-6 org-level-7 org-level-8
  3829. ))
  3830. (defcustom org-n-level-faces (length org-level-faces)
  3831. "The number of different faces to be used for headlines.
  3832. Org-mode defines 8 different headline faces, so this can be at most 8.
  3833. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3834. :type 'number
  3835. :group 'org-faces)
  3836. ;;; Functions and variables from ther packages
  3837. ;; Declared here to avoid compiler warnings
  3838. (eval-and-compile
  3839. (unless (fboundp 'declare-function)
  3840. (defmacro declare-function (fn file &optional arglist fileonly))))
  3841. ;; XEmacs only
  3842. (defvar outline-mode-menu-heading)
  3843. (defvar outline-mode-menu-show)
  3844. (defvar outline-mode-menu-hide)
  3845. (defvar zmacs-regions) ; XEmacs regions
  3846. ;; Emacs only
  3847. (defvar mark-active)
  3848. ;; Various packages
  3849. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3850. (declare-function add-to-diary-list "diary-lib"
  3851. (date string specifier &optional marker globcolor literal))
  3852. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3853. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3854. (declare-function bibtex-generate-autokey "bibtex" ())
  3855. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3856. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3857. (defvar calc-embedded-close-formula)
  3858. (defvar calc-embedded-open-formula)
  3859. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3860. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3861. (declare-function calendar-check-holidays "holidays" (date))
  3862. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3863. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3864. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3865. (declare-function calendar-forward-day "cal-move" (arg))
  3866. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3867. (declare-function calendar-goto-date "cal-move" (date))
  3868. (declare-function calendar-goto-today "cal-move" ())
  3869. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3870. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3871. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3872. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  3873. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  3874. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3875. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3876. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3877. (defvar calendar-mode-map)
  3878. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3879. (declare-function cdlatex-tab "ext:cdlatex" ())
  3880. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3881. (defvar font-lock-unfontify-region-function)
  3882. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3883. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3884. (declare-function parse-time-string "parse-time" (string))
  3885. (declare-function remember "remember" (&optional initial))
  3886. (declare-function remember-buffer-desc "remember" ())
  3887. (declare-function remember-finalize "remember" ())
  3888. (defvar remember-save-after-remembering)
  3889. (defvar remember-data-file)
  3890. (defvar remember-register)
  3891. (defvar remember-buffer)
  3892. (defvar remember-handler-functions)
  3893. (defvar remember-annotation-functions)
  3894. (defvar texmathp-why)
  3895. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3896. (defvar w3m-current-url)
  3897. (defvar w3m-current-title)
  3898. (defvar org-latex-regexps)
  3899. (defvar constants-unit-system)
  3900. ;;; Variables for pre-computed regular expressions, all buffer local
  3901. (defvar org-drawer-regexp nil
  3902. "Matches first line of a hidden block.")
  3903. (make-variable-buffer-local 'org-drawer-regexp)
  3904. (defvar org-todo-regexp nil
  3905. "Matches any of the TODO state keywords.")
  3906. (make-variable-buffer-local 'org-todo-regexp)
  3907. (defvar org-not-done-regexp nil
  3908. "Matches any of the TODO state keywords except the last one.")
  3909. (make-variable-buffer-local 'org-not-done-regexp)
  3910. (defvar org-todo-line-regexp nil
  3911. "Matches a headline and puts TODO state into group 2 if present.")
  3912. (make-variable-buffer-local 'org-todo-line-regexp)
  3913. (defvar org-complex-heading-regexp nil
  3914. "Matches a headline and puts everything into groups:
  3915. group 1: the stars
  3916. group 2: The todo keyword, maybe
  3917. group 3: Priority cookie
  3918. group 4: True headline
  3919. group 5: Tags")
  3920. (make-variable-buffer-local 'org-complex-heading-regexp)
  3921. (defvar org-todo-line-tags-regexp nil
  3922. "Matches a headline and puts TODO state into group 2 if present.
  3923. Also put tags into group 4 if tags are present.")
  3924. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3925. (defvar org-nl-done-regexp nil
  3926. "Matches newline followed by a headline with the DONE keyword.")
  3927. (make-variable-buffer-local 'org-nl-done-regexp)
  3928. (defvar org-looking-at-done-regexp nil
  3929. "Matches the DONE keyword a point.")
  3930. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3931. (defvar org-ds-keyword-length 12
  3932. "Maximum length of the Deadline and SCHEDULED keywords.")
  3933. (make-variable-buffer-local 'org-ds-keyword-length)
  3934. (defvar org-deadline-regexp nil
  3935. "Matches the DEADLINE keyword.")
  3936. (make-variable-buffer-local 'org-deadline-regexp)
  3937. (defvar org-deadline-time-regexp nil
  3938. "Matches the DEADLINE keyword together with a time stamp.")
  3939. (make-variable-buffer-local 'org-deadline-time-regexp)
  3940. (defvar org-deadline-line-regexp nil
  3941. "Matches the DEADLINE keyword and the rest of the line.")
  3942. (make-variable-buffer-local 'org-deadline-line-regexp)
  3943. (defvar org-scheduled-regexp nil
  3944. "Matches the SCHEDULED keyword.")
  3945. (make-variable-buffer-local 'org-scheduled-regexp)
  3946. (defvar org-scheduled-time-regexp nil
  3947. "Matches the SCHEDULED keyword together with a time stamp.")
  3948. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3949. (defvar org-closed-time-regexp nil
  3950. "Matches the CLOSED keyword together with a time stamp.")
  3951. (make-variable-buffer-local 'org-closed-time-regexp)
  3952. (defvar org-keyword-time-regexp nil
  3953. "Matches any of the 4 keywords, together with the time stamp.")
  3954. (make-variable-buffer-local 'org-keyword-time-regexp)
  3955. (defvar org-keyword-time-not-clock-regexp nil
  3956. "Matches any of the 3 keywords, together with the time stamp.")
  3957. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3958. (defvar org-maybe-keyword-time-regexp nil
  3959. "Matches a timestamp, possibly preceeded by a keyword.")
  3960. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3961. (defvar org-planning-or-clock-line-re nil
  3962. "Matches a line with planning or clock info.")
  3963. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3964. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3965. rear-nonsticky t mouse-map t fontified t)
  3966. "Properties to remove when a string without properties is wanted.")
  3967. (defsubst org-match-string-no-properties (num &optional string)
  3968. (if (featurep 'xemacs)
  3969. (let ((s (match-string num string)))
  3970. (remove-text-properties 0 (length s) org-rm-props s)
  3971. s)
  3972. (match-string-no-properties num string)))
  3973. (defsubst org-no-properties (s)
  3974. (if (fboundp 'set-text-properties)
  3975. (set-text-properties 0 (length s) nil s)
  3976. (remove-text-properties 0 (length s) org-rm-props s))
  3977. s)
  3978. (defsubst org-get-alist-option (option key)
  3979. (cond ((eq key t) t)
  3980. ((eq option t) t)
  3981. ((assoc key option) (cdr (assoc key option)))
  3982. (t (cdr (assq 'default option)))))
  3983. (defsubst org-inhibit-invisibility ()
  3984. "Modified `buffer-invisibility-spec' for Emacs 21.
  3985. Some ops with invisible text do not work correctly on Emacs 21. For these
  3986. we turn off invisibility temporarily. Use this in a `let' form."
  3987. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3988. (defsubst org-set-local (var value)
  3989. "Make VAR local in current buffer and set it to VALUE."
  3990. (set (make-variable-buffer-local var) value))
  3991. (defsubst org-mode-p ()
  3992. "Check if the current buffer is in Org-mode."
  3993. (eq major-mode 'org-mode))
  3994. (defsubst org-last (list)
  3995. "Return the last element of LIST."
  3996. (car (last list)))
  3997. (defun org-let (list &rest body)
  3998. (eval (cons 'let (cons list body))))
  3999. (put 'org-let 'lisp-indent-function 1)
  4000. (defun org-let2 (list1 list2 &rest body)
  4001. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  4002. (put 'org-let2 'lisp-indent-function 2)
  4003. (defconst org-startup-options
  4004. '(("fold" org-startup-folded t)
  4005. ("overview" org-startup-folded t)
  4006. ("nofold" org-startup-folded nil)
  4007. ("showall" org-startup-folded nil)
  4008. ("content" org-startup-folded content)
  4009. ("hidestars" org-hide-leading-stars t)
  4010. ("showstars" org-hide-leading-stars nil)
  4011. ("odd" org-odd-levels-only t)
  4012. ("oddeven" org-odd-levels-only nil)
  4013. ("align" org-startup-align-all-tables t)
  4014. ("noalign" org-startup-align-all-tables nil)
  4015. ("customtime" org-display-custom-times t)
  4016. ("logdone" org-log-done time)
  4017. ("lognotedone" org-log-done note)
  4018. ("nologdone" org-log-done nil)
  4019. ("lognoteclock-out" org-log-note-clock-out t)
  4020. ("nolognoteclock-out" org-log-note-clock-out nil)
  4021. ("logrepeat" org-log-repeat state)
  4022. ("lognoterepeat" org-log-repeat note)
  4023. ("nologrepeat" org-log-repeat nil)
  4024. ("constcgs" constants-unit-system cgs)
  4025. ("constSI" constants-unit-system SI))
  4026. "Variable associated with STARTUP options for org-mode.
  4027. Each element is a list of three items: The startup options as written
  4028. in the #+STARTUP line, the corresponding variable, and the value to
  4029. set this variable to if the option is found. An optional forth element PUSH
  4030. means to push this value onto the list in the variable.")
  4031. (defun org-set-regexps-and-options ()
  4032. "Precompute regular expressions for current buffer."
  4033. (when (org-mode-p)
  4034. (org-set-local 'org-todo-kwd-alist nil)
  4035. (org-set-local 'org-todo-key-alist nil)
  4036. (org-set-local 'org-todo-key-trigger nil)
  4037. (org-set-local 'org-todo-keywords-1 nil)
  4038. (org-set-local 'org-done-keywords nil)
  4039. (org-set-local 'org-todo-heads nil)
  4040. (org-set-local 'org-todo-sets nil)
  4041. (org-set-local 'org-todo-log-states nil)
  4042. (let ((re (org-make-options-regexp
  4043. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  4044. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  4045. "CONSTANTS" "PROPERTY" "DRAWERS")))
  4046. (splitre "[ \t]+")
  4047. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4048. tail sep kws1 prio props drawers)
  4049. (save-excursion
  4050. (save-restriction
  4051. (widen)
  4052. (goto-char (point-min))
  4053. (while (re-search-forward re nil t)
  4054. (setq key (match-string 1) value (org-match-string-no-properties 2))
  4055. (cond
  4056. ((equal key "CATEGORY")
  4057. (if (string-match "[ \t]+$" value)
  4058. (setq value (replace-match "" t t value)))
  4059. (setq cat value))
  4060. ((member key '("SEQ_TODO" "TODO"))
  4061. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4062. ((equal key "TYP_TODO")
  4063. (push (cons 'type (org-split-string value splitre)) kwds))
  4064. ((equal key "TAGS")
  4065. (setq tags (append tags (org-split-string value splitre))))
  4066. ((equal key "COLUMNS")
  4067. (org-set-local 'org-columns-default-format value))
  4068. ((equal key "LINK")
  4069. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4070. (push (cons (match-string 1 value)
  4071. (org-trim (match-string 2 value)))
  4072. links)))
  4073. ((equal key "PRIORITIES")
  4074. (setq prio (org-split-string value " +")))
  4075. ((equal key "PROPERTY")
  4076. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4077. (push (cons (match-string 1 value) (match-string 2 value))
  4078. props)))
  4079. ((equal key "DRAWERS")
  4080. (setq drawers (org-split-string value splitre)))
  4081. ((equal key "CONSTANTS")
  4082. (setq const (append const (org-split-string value splitre))))
  4083. ((equal key "STARTUP")
  4084. (let ((opts (org-split-string value splitre))
  4085. l var val)
  4086. (while (setq l (pop opts))
  4087. (when (setq l (assoc l org-startup-options))
  4088. (setq var (nth 1 l) val (nth 2 l))
  4089. (if (not (nth 3 l))
  4090. (set (make-local-variable var) val)
  4091. (if (not (listp (symbol-value var)))
  4092. (set (make-local-variable var) nil))
  4093. (set (make-local-variable var) (symbol-value var))
  4094. (add-to-list var val))))))
  4095. ((equal key "ARCHIVE")
  4096. (string-match " *$" value)
  4097. (setq arch (replace-match "" t t value))
  4098. (remove-text-properties 0 (length arch)
  4099. '(face t fontified t) arch)))
  4100. )))
  4101. (when cat
  4102. (org-set-local 'org-category (intern cat))
  4103. (push (cons "CATEGORY" cat) props))
  4104. (when prio
  4105. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4106. (setq prio (mapcar 'string-to-char prio))
  4107. (org-set-local 'org-highest-priority (nth 0 prio))
  4108. (org-set-local 'org-lowest-priority (nth 1 prio))
  4109. (org-set-local 'org-default-priority (nth 2 prio)))
  4110. (and props (org-set-local 'org-local-properties (nreverse props)))
  4111. (and drawers (org-set-local 'org-drawers drawers))
  4112. (and arch (org-set-local 'org-archive-location arch))
  4113. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4114. ;; Process the TODO keywords
  4115. (unless kwds
  4116. ;; Use the global values as if they had been given locally.
  4117. (setq kwds (default-value 'org-todo-keywords))
  4118. (if (stringp (car kwds))
  4119. (setq kwds (list (cons org-todo-interpretation
  4120. (default-value 'org-todo-keywords)))))
  4121. (setq kwds (reverse kwds)))
  4122. (setq kwds (nreverse kwds))
  4123. (let (inter kws kw)
  4124. (while (setq kws (pop kwds))
  4125. (setq inter (pop kws) sep (member "|" kws)
  4126. kws0 (delete "|" (copy-sequence kws))
  4127. kwsa nil
  4128. kws1 (mapcar
  4129. (lambda (x)
  4130. ;; 1 2
  4131. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4132. (progn
  4133. (setq kw (match-string 1 x)
  4134. key (and (match-end 2) (match-string 2 x))
  4135. log (org-extract-log-state-settings x))
  4136. (push (cons kw (and key (string-to-char key))) kwsa)
  4137. (and log (push log org-todo-log-states))
  4138. kw)
  4139. (error "Invalid TODO keyword %s" x)))
  4140. kws0)
  4141. kwsa (if kwsa (append '((:startgroup))
  4142. (nreverse kwsa)
  4143. '((:endgroup))))
  4144. hw (car kws1)
  4145. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4146. tail (list inter hw (car dws) (org-last dws)))
  4147. (add-to-list 'org-todo-heads hw 'append)
  4148. (push kws1 org-todo-sets)
  4149. (setq org-done-keywords (append org-done-keywords dws nil))
  4150. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4151. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4152. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4153. (setq org-todo-sets (nreverse org-todo-sets)
  4154. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4155. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4156. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4157. ;; Process the constants
  4158. (when const
  4159. (let (e cst)
  4160. (while (setq e (pop const))
  4161. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4162. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4163. (setq org-table-formula-constants-local cst)))
  4164. ;; Process the tags.
  4165. (when tags
  4166. (let (e tgs)
  4167. (while (setq e (pop tags))
  4168. (cond
  4169. ((equal e "{") (push '(:startgroup) tgs))
  4170. ((equal e "}") (push '(:endgroup) tgs))
  4171. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4172. (push (cons (match-string 1 e)
  4173. (string-to-char (match-string 2 e)))
  4174. tgs))
  4175. (t (push (list e) tgs))))
  4176. (org-set-local 'org-tag-alist nil)
  4177. (while (setq e (pop tgs))
  4178. (or (and (stringp (car e))
  4179. (assoc (car e) org-tag-alist))
  4180. (push e org-tag-alist))))))
  4181. ;; Compute the regular expressions and other local variables
  4182. (if (not org-done-keywords)
  4183. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4184. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4185. (length org-scheduled-string)))
  4186. org-drawer-regexp
  4187. (concat "^[ \t]*:\\("
  4188. (mapconcat 'regexp-quote org-drawers "\\|")
  4189. "\\):[ \t]*$")
  4190. org-not-done-keywords
  4191. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4192. org-todo-regexp
  4193. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4194. "\\|") "\\)\\>")
  4195. org-not-done-regexp
  4196. (concat "\\<\\("
  4197. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4198. "\\)\\>")
  4199. org-todo-line-regexp
  4200. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4201. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4202. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4203. org-complex-heading-regexp
  4204. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4205. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4206. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4207. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4208. org-nl-done-regexp
  4209. (concat "\n\\*+[ \t]+"
  4210. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4211. "\\)" "\\>")
  4212. org-todo-line-tags-regexp
  4213. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4214. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4215. (org-re
  4216. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4217. org-looking-at-done-regexp
  4218. (concat "^" "\\(?:"
  4219. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4220. "\\>")
  4221. org-deadline-regexp (concat "\\<" org-deadline-string)
  4222. org-deadline-time-regexp
  4223. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4224. org-deadline-line-regexp
  4225. (concat "\\<\\(" org-deadline-string "\\).*")
  4226. org-scheduled-regexp
  4227. (concat "\\<" org-scheduled-string)
  4228. org-scheduled-time-regexp
  4229. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4230. org-closed-time-regexp
  4231. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4232. org-keyword-time-regexp
  4233. (concat "\\<\\(" org-scheduled-string
  4234. "\\|" org-deadline-string
  4235. "\\|" org-closed-string
  4236. "\\|" org-clock-string "\\)"
  4237. " *[[<]\\([^]>]+\\)[]>]")
  4238. org-keyword-time-not-clock-regexp
  4239. (concat "\\<\\(" org-scheduled-string
  4240. "\\|" org-deadline-string
  4241. "\\|" org-closed-string
  4242. "\\)"
  4243. " *[[<]\\([^]>]+\\)[]>]")
  4244. org-maybe-keyword-time-regexp
  4245. (concat "\\(\\<\\(" org-scheduled-string
  4246. "\\|" org-deadline-string
  4247. "\\|" org-closed-string
  4248. "\\|" org-clock-string "\\)\\)?"
  4249. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4250. org-planning-or-clock-line-re
  4251. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4252. "\\|" org-deadline-string
  4253. "\\|" org-closed-string "\\|" org-clock-string
  4254. "\\)\\>\\)")
  4255. )
  4256. (org-compute-latex-and-specials-regexp)
  4257. (org-set-font-lock-defaults)))
  4258. (defun org-extract-log-state-settings (x)
  4259. "Extract the log state setting from a TODO keyword string.
  4260. This will extract info from a string like \"WAIT(w@/!)\"."
  4261. (let (kw key log1 log2)
  4262. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4263. (setq kw (match-string 1 x)
  4264. key (and (match-end 2) (match-string 2 x))
  4265. log1 (and (match-end 3) (match-string 3 x))
  4266. log2 (and (match-end 4) (match-string 4 x)))
  4267. (and (or log1 log2)
  4268. (list kw
  4269. (and log1 (if (equal log1 "!") 'time 'note))
  4270. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4271. (defun org-remove-keyword-keys (list)
  4272. "Remove a pair of parenthesis at the end of each string in LIST."
  4273. (mapcar (lambda (x)
  4274. (if (string-match "(.*)$" x)
  4275. (substring x 0 (match-beginning 0))
  4276. x))
  4277. list))
  4278. ;; FIXME: this could be done much better, using second characters etc.
  4279. (defun org-assign-fast-keys (alist)
  4280. "Assign fast keys to a keyword-key alist.
  4281. Respect keys that are already there."
  4282. (let (new e k c c1 c2 (char ?a))
  4283. (while (setq e (pop alist))
  4284. (cond
  4285. ((equal e '(:startgroup)) (push e new))
  4286. ((equal e '(:endgroup)) (push e new))
  4287. (t
  4288. (setq k (car e) c2 nil)
  4289. (if (cdr e)
  4290. (setq c (cdr e))
  4291. ;; automatically assign a character.
  4292. (setq c1 (string-to-char
  4293. (downcase (substring
  4294. k (if (= (string-to-char k) ?@) 1 0)))))
  4295. (if (or (rassoc c1 new) (rassoc c1 alist))
  4296. (while (or (rassoc char new) (rassoc char alist))
  4297. (setq char (1+ char)))
  4298. (setq c2 c1))
  4299. (setq c (or c2 char)))
  4300. (push (cons k c) new))))
  4301. (nreverse new)))
  4302. ;;; Some variables ujsed in various places
  4303. (defvar org-window-configuration nil
  4304. "Used in various places to store a window configuration.")
  4305. (defvar org-finish-function nil
  4306. "Function to be called when `C-c C-c' is used.
  4307. This is for getting out of special buffers like remember.")
  4308. ;; FIXME: Occasionally check by commenting these, to make sure
  4309. ;; no other functions uses these, forgetting to let-bind them.
  4310. (defvar entry)
  4311. (defvar state)
  4312. (defvar last-state)
  4313. (defvar date)
  4314. (defvar description)
  4315. ;; Defined somewhere in this file, but used before definition.
  4316. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4317. (defvar org-agenda-buffer-name)
  4318. (defvar org-agenda-undo-list)
  4319. (defvar org-agenda-pending-undo-list)
  4320. (defvar org-agenda-overriding-header)
  4321. (defvar orgtbl-mode)
  4322. (defvar org-html-entities)
  4323. (defvar org-struct-menu)
  4324. (defvar org-org-menu)
  4325. (defvar org-tbl-menu)
  4326. (defvar org-agenda-keymap)
  4327. ;;;; Emacs/XEmacs compatibility
  4328. ;; Overlay compatibility functions
  4329. (defun org-make-overlay (beg end &optional buffer)
  4330. (if (featurep 'xemacs)
  4331. (make-extent beg end buffer)
  4332. (make-overlay beg end buffer)))
  4333. (defun org-delete-overlay (ovl)
  4334. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4335. (defun org-detach-overlay (ovl)
  4336. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4337. (defun org-move-overlay (ovl beg end &optional buffer)
  4338. (if (featurep 'xemacs)
  4339. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4340. (move-overlay ovl beg end buffer)))
  4341. (defun org-overlay-put (ovl prop value)
  4342. (if (featurep 'xemacs)
  4343. (set-extent-property ovl prop value)
  4344. (overlay-put ovl prop value)))
  4345. (defun org-overlay-display (ovl text &optional face evap)
  4346. "Make overlay OVL display TEXT with face FACE."
  4347. (if (featurep 'xemacs)
  4348. (let ((gl (make-glyph text)))
  4349. (and face (set-glyph-face gl face))
  4350. (set-extent-property ovl 'invisible t)
  4351. (set-extent-property ovl 'end-glyph gl))
  4352. (overlay-put ovl 'display text)
  4353. (if face (overlay-put ovl 'face face))
  4354. (if evap (overlay-put ovl 'evaporate t))))
  4355. (defun org-overlay-before-string (ovl text &optional face evap)
  4356. "Make overlay OVL display TEXT with face FACE."
  4357. (if (featurep 'xemacs)
  4358. (let ((gl (make-glyph text)))
  4359. (and face (set-glyph-face gl face))
  4360. (set-extent-property ovl 'begin-glyph gl))
  4361. (if face (org-add-props text nil 'face face))
  4362. (overlay-put ovl 'before-string text)
  4363. (if evap (overlay-put ovl 'evaporate t))))
  4364. (defun org-overlay-get (ovl prop)
  4365. (if (featurep 'xemacs)
  4366. (extent-property ovl prop)
  4367. (overlay-get ovl prop)))
  4368. (defun org-overlays-at (pos)
  4369. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4370. (defun org-overlays-in (&optional start end)
  4371. (if (featurep 'xemacs)
  4372. (extent-list nil start end)
  4373. (overlays-in start end)))
  4374. (defun org-overlay-start (o)
  4375. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4376. (defun org-overlay-end (o)
  4377. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4378. (defun org-find-overlays (prop &optional pos delete)
  4379. "Find all overlays specifying PROP at POS or point.
  4380. If DELETE is non-nil, delete all those overlays."
  4381. (let ((overlays (org-overlays-at (or pos (point))))
  4382. ov found)
  4383. (while (setq ov (pop overlays))
  4384. (if (org-overlay-get ov prop)
  4385. (if delete (org-delete-overlay ov) (push ov found))))
  4386. found))
  4387. ;; Region compatibility
  4388. (defun org-add-hook (hook function &optional append local)
  4389. "Add-hook, compatible with both Emacsen."
  4390. (if (and local (featurep 'xemacs))
  4391. (add-local-hook hook function append)
  4392. (add-hook hook function append local)))
  4393. (defvar org-ignore-region nil
  4394. "To temporarily disable the active region.")
  4395. (defun org-region-active-p ()
  4396. "Is `transient-mark-mode' on and the region active?
  4397. Works on both Emacs and XEmacs."
  4398. (if org-ignore-region
  4399. nil
  4400. (if (featurep 'xemacs)
  4401. (and zmacs-regions (region-active-p))
  4402. (if (fboundp 'use-region-p)
  4403. (use-region-p)
  4404. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4405. ;; Invisibility compatibility
  4406. (defun org-add-to-invisibility-spec (arg)
  4407. "Add elements to `buffer-invisibility-spec'.
  4408. See documentation for `buffer-invisibility-spec' for the kind of elements
  4409. that can be added."
  4410. (cond
  4411. ((fboundp 'add-to-invisibility-spec)
  4412. (add-to-invisibility-spec arg))
  4413. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4414. (setq buffer-invisibility-spec (list arg)))
  4415. (t
  4416. (setq buffer-invisibility-spec
  4417. (cons arg buffer-invisibility-spec)))))
  4418. (defun org-remove-from-invisibility-spec (arg)
  4419. "Remove elements from `buffer-invisibility-spec'."
  4420. (if (fboundp 'remove-from-invisibility-spec)
  4421. (remove-from-invisibility-spec arg)
  4422. (if (consp buffer-invisibility-spec)
  4423. (setq buffer-invisibility-spec
  4424. (delete arg buffer-invisibility-spec)))))
  4425. (defun org-in-invisibility-spec-p (arg)
  4426. "Is ARG a member of `buffer-invisibility-spec'?"
  4427. (if (consp buffer-invisibility-spec)
  4428. (member arg buffer-invisibility-spec)
  4429. nil))
  4430. ;;;; Define the Org-mode
  4431. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4432. (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."))
  4433. ;; We use a before-change function to check if a table might need
  4434. ;; an update.
  4435. (defvar org-table-may-need-update t
  4436. "Indicates that a table might need an update.
  4437. This variable is set by `org-before-change-function'.
  4438. `org-table-align' sets it back to nil.")
  4439. (defvar org-mode-map)
  4440. (defvar org-mode-hook nil)
  4441. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4442. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4443. (defvar org-table-buffer-is-an nil)
  4444. (defconst org-outline-regexp "\\*+ ")
  4445. ;;;###autoload
  4446. (define-derived-mode org-mode outline-mode "Org"
  4447. "Outline-based notes management and organizer, alias
  4448. \"Carsten's outline-mode for keeping track of everything.\"
  4449. Org-mode develops organizational tasks around a NOTES file which
  4450. contains information about projects as plain text. Org-mode is
  4451. implemented on top of outline-mode, which is ideal to keep the content
  4452. of large files well structured. It supports ToDo items, deadlines and
  4453. time stamps, which magically appear in the diary listing of the Emacs
  4454. calendar. Tables are easily created with a built-in table editor.
  4455. Plain text URL-like links connect to websites, emails (VM), Usenet
  4456. messages (Gnus), BBDB entries, and any files related to the project.
  4457. For printing and sharing of notes, an Org-mode file (or a part of it)
  4458. can be exported as a structured ASCII or HTML file.
  4459. The following commands are available:
  4460. \\{org-mode-map}"
  4461. ;; Get rid of Outline menus, they are not needed
  4462. ;; Need to do this here because define-derived-mode sets up
  4463. ;; the keymap so late. Still, it is a waste to call this each time
  4464. ;; we switch another buffer into org-mode.
  4465. (if (featurep 'xemacs)
  4466. (when (boundp 'outline-mode-menu-heading)
  4467. ;; Assume this is Greg's port, it used easymenu
  4468. (easy-menu-remove outline-mode-menu-heading)
  4469. (easy-menu-remove outline-mode-menu-show)
  4470. (easy-menu-remove outline-mode-menu-hide))
  4471. (define-key org-mode-map [menu-bar headings] 'undefined)
  4472. (define-key org-mode-map [menu-bar hide] 'undefined)
  4473. (define-key org-mode-map [menu-bar show] 'undefined))
  4474. (org-load-modules-maybe)
  4475. (easy-menu-add org-org-menu)
  4476. (easy-menu-add org-tbl-menu)
  4477. (org-install-agenda-files-menu)
  4478. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4479. (org-add-to-invisibility-spec '(org-cwidth))
  4480. (when (featurep 'xemacs)
  4481. (org-set-local 'line-move-ignore-invisible t))
  4482. (org-set-local 'outline-regexp org-outline-regexp)
  4483. (org-set-local 'outline-level 'org-outline-level)
  4484. (when (and org-ellipsis
  4485. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4486. (fboundp 'make-glyph-code))
  4487. (unless org-display-table
  4488. (setq org-display-table (make-display-table)))
  4489. (set-display-table-slot
  4490. org-display-table 4
  4491. (vconcat (mapcar
  4492. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4493. org-ellipsis)))
  4494. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4495. (setq buffer-display-table org-display-table))
  4496. (org-set-regexps-and-options)
  4497. ;; Calc embedded
  4498. (org-set-local 'calc-embedded-open-mode "# ")
  4499. (modify-syntax-entry ?# "<")
  4500. (modify-syntax-entry ?@ "w")
  4501. (if org-startup-truncated (setq truncate-lines t))
  4502. (org-set-local 'font-lock-unfontify-region-function
  4503. 'org-unfontify-region)
  4504. ;; Activate before-change-function
  4505. (org-set-local 'org-table-may-need-update t)
  4506. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4507. 'local)
  4508. ;; Check for running clock before killing a buffer
  4509. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4510. ;; Paragraphs and auto-filling
  4511. (org-set-autofill-regexps)
  4512. (setq indent-line-function 'org-indent-line-function)
  4513. (org-update-radio-target-regexp)
  4514. ;; Comment characters
  4515. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4516. (org-set-local 'comment-padding " ")
  4517. ;; Align options lines
  4518. (org-set-local
  4519. 'align-mode-rules-list
  4520. '((org-in-buffer-settings
  4521. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4522. (modes . '(org-mode)))))
  4523. ;; Imenu
  4524. (org-set-local 'imenu-create-index-function
  4525. 'org-imenu-get-tree)
  4526. ;; Make isearch reveal context
  4527. (if (or (featurep 'xemacs)
  4528. (not (boundp 'outline-isearch-open-invisible-function)))
  4529. ;; Emacs 21 and XEmacs make use of the hook
  4530. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4531. ;; Emacs 22 deals with this through a special variable
  4532. (org-set-local 'outline-isearch-open-invisible-function
  4533. (lambda (&rest ignore) (org-show-context 'isearch))))
  4534. ;; If empty file that did not turn on org-mode automatically, make it to.
  4535. (if (and org-insert-mode-line-in-empty-file
  4536. (interactive-p)
  4537. (= (point-min) (point-max)))
  4538. (insert "# -*- mode: org -*-\n\n"))
  4539. (unless org-inhibit-startup
  4540. (when org-startup-align-all-tables
  4541. (let ((bmp (buffer-modified-p)))
  4542. (org-table-map-tables 'org-table-align)
  4543. (set-buffer-modified-p bmp)))
  4544. (org-cycle-hide-drawers 'all)
  4545. (cond
  4546. ((eq org-startup-folded t)
  4547. (org-cycle '(4)))
  4548. ((eq org-startup-folded 'content)
  4549. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4550. (org-cycle '(4)) (org-cycle '(4)))))))
  4551. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4552. (defsubst org-call-with-arg (command arg)
  4553. "Call COMMAND interactively, but pretend prefix are was ARG."
  4554. (let ((current-prefix-arg arg)) (call-interactively command)))
  4555. (defsubst org-current-line (&optional pos)
  4556. (save-excursion
  4557. (and pos (goto-char pos))
  4558. ;; works also in narrowed buffer, because we start at 1, not point-min
  4559. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4560. (defun org-current-time ()
  4561. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4562. (if (> (car org-time-stamp-rounding-minutes) 1)
  4563. (let ((r (car org-time-stamp-rounding-minutes))
  4564. (time (decode-time)))
  4565. (apply 'encode-time
  4566. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4567. (nthcdr 2 time))))
  4568. (current-time)))
  4569. (defun org-add-props (string plist &rest props)
  4570. "Add text properties to entire string, from beginning to end.
  4571. PLIST may be a list of properties, PROPS are individual properties and values
  4572. that will be added to PLIST. Returns the string that was modified."
  4573. (add-text-properties
  4574. 0 (length string) (if props (append plist props) plist) string)
  4575. string)
  4576. (put 'org-add-props 'lisp-indent-function 2)
  4577. ;;;; Font-Lock stuff, including the activators
  4578. (defvar org-mouse-map (make-sparse-keymap))
  4579. (org-defkey org-mouse-map
  4580. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4581. (org-defkey org-mouse-map
  4582. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4583. (when org-mouse-1-follows-link
  4584. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4585. (when org-tab-follows-link
  4586. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4587. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4588. (when org-return-follows-link
  4589. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4590. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4591. (require 'font-lock)
  4592. (defconst org-non-link-chars "]\t\n\r<>")
  4593. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4594. "shell" "elisp"))
  4595. (defvar org-link-re-with-space nil
  4596. "Matches a link with spaces, optional angular brackets around it.")
  4597. (defvar org-link-re-with-space2 nil
  4598. "Matches a link with spaces, optional angular brackets around it.")
  4599. (defvar org-angle-link-re nil
  4600. "Matches link with angular brackets, spaces are allowed.")
  4601. (defvar org-plain-link-re nil
  4602. "Matches plain link, without spaces.")
  4603. (defvar org-bracket-link-regexp nil
  4604. "Matches a link in double brackets.")
  4605. (defvar org-bracket-link-analytic-regexp nil
  4606. "Regular expression used to analyze links.
  4607. Here is what the match groups contain after a match:
  4608. 1: http:
  4609. 2: http
  4610. 3: path
  4611. 4: [desc]
  4612. 5: desc")
  4613. (defvar org-any-link-re nil
  4614. "Regular expression matching any link.")
  4615. (defun org-make-link-regexps ()
  4616. "Update the link regular expressions.
  4617. This should be called after the variable `org-link-types' has changed."
  4618. (setq org-link-re-with-space
  4619. (concat
  4620. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4621. "\\([^" org-non-link-chars " ]"
  4622. "[^" org-non-link-chars "]*"
  4623. "[^" org-non-link-chars " ]\\)>?")
  4624. org-link-re-with-space2
  4625. (concat
  4626. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4627. "\\([^" org-non-link-chars " ]"
  4628. "[^]\t\n\r]*"
  4629. "[^" org-non-link-chars " ]\\)>?")
  4630. org-angle-link-re
  4631. (concat
  4632. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4633. "\\([^" org-non-link-chars " ]"
  4634. "[^" org-non-link-chars "]*"
  4635. "\\)>")
  4636. org-plain-link-re
  4637. (concat
  4638. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4639. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4640. org-bracket-link-regexp
  4641. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4642. org-bracket-link-analytic-regexp
  4643. (concat
  4644. "\\[\\["
  4645. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4646. "\\([^]]+\\)"
  4647. "\\]"
  4648. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4649. "\\]")
  4650. org-any-link-re
  4651. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4652. org-angle-link-re "\\)\\|\\("
  4653. org-plain-link-re "\\)")))
  4654. (org-make-link-regexps)
  4655. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4656. "Regular expression for fast time stamp matching.")
  4657. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4658. "Regular expression for fast time stamp matching.")
  4659. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4660. "Regular expression matching time strings for analysis.
  4661. This one does not require the space after the date, so it can be used
  4662. on a string that terminates immediately after the date.")
  4663. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4664. "Regular expression matching time strings for analysis.")
  4665. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4666. "Regular expression matching time stamps, with groups.")
  4667. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4668. "Regular expression matching time stamps (also [..]), with groups.")
  4669. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4670. "Regular expression matching a time stamp range.")
  4671. (defconst org-tr-regexp-both
  4672. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4673. "Regular expression matching a time stamp range.")
  4674. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4675. org-ts-regexp "\\)?")
  4676. "Regular expression matching a time stamp or time stamp range.")
  4677. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4678. org-ts-regexp-both "\\)?")
  4679. "Regular expression matching a time stamp or time stamp range.
  4680. The time stamps may be either active or inactive.")
  4681. (defvar org-emph-face nil)
  4682. (defun org-do-emphasis-faces (limit)
  4683. "Run through the buffer and add overlays to links."
  4684. (let (rtn)
  4685. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4686. (if (not (= (char-after (match-beginning 3))
  4687. (char-after (match-beginning 4))))
  4688. (progn
  4689. (setq rtn t)
  4690. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4691. 'face
  4692. (nth 1 (assoc (match-string 3)
  4693. org-emphasis-alist)))
  4694. (add-text-properties (match-beginning 2) (match-end 2)
  4695. '(font-lock-multiline t))
  4696. (when org-hide-emphasis-markers
  4697. (add-text-properties (match-end 4) (match-beginning 5)
  4698. '(invisible org-link))
  4699. (add-text-properties (match-beginning 3) (match-end 3)
  4700. '(invisible org-link)))))
  4701. (backward-char 1))
  4702. rtn))
  4703. (defun org-emphasize (&optional char)
  4704. "Insert or change an emphasis, i.e. a font like bold or italic.
  4705. If there is an active region, change that region to a new emphasis.
  4706. If there is no region, just insert the marker characters and position
  4707. the cursor between them.
  4708. CHAR should be either the marker character, or the first character of the
  4709. HTML tag associated with that emphasis. If CHAR is a space, the means
  4710. to remove the emphasis of the selected region.
  4711. If char is not given (for example in an interactive call) it
  4712. will be prompted for."
  4713. (interactive)
  4714. (let ((eal org-emphasis-alist) e det
  4715. (erc org-emphasis-regexp-components)
  4716. (prompt "")
  4717. (string "") beg end move tag c s)
  4718. (if (org-region-active-p)
  4719. (setq beg (region-beginning) end (region-end)
  4720. string (buffer-substring beg end))
  4721. (setq move t))
  4722. (while (setq e (pop eal))
  4723. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4724. c (aref tag 0))
  4725. (push (cons c (string-to-char (car e))) det)
  4726. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4727. (substring tag 1)))))
  4728. (unless char
  4729. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4730. (setq char (read-char-exclusive)))
  4731. (setq char (or (cdr (assoc char det)) char))
  4732. (if (equal char ?\ )
  4733. (setq s "" move nil)
  4734. (unless (assoc (char-to-string char) org-emphasis-alist)
  4735. (error "No such emphasis marker: \"%c\"" char))
  4736. (setq s (char-to-string char)))
  4737. (while (and (> (length string) 1)
  4738. (equal (substring string 0 1) (substring string -1))
  4739. (assoc (substring string 0 1) org-emphasis-alist))
  4740. (setq string (substring string 1 -1)))
  4741. (setq string (concat s string s))
  4742. (if beg (delete-region beg end))
  4743. (unless (or (bolp)
  4744. (string-match (concat "[" (nth 0 erc) "\n]")
  4745. (char-to-string (char-before (point)))))
  4746. (insert " "))
  4747. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4748. (char-to-string (char-after (point))))
  4749. (insert " ") (backward-char 1))
  4750. (insert string)
  4751. (and move (backward-char 1))))
  4752. (defconst org-nonsticky-props
  4753. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4754. (defun org-activate-plain-links (limit)
  4755. "Run through the buffer and add overlays to links."
  4756. (catch 'exit
  4757. (let (f)
  4758. (while (re-search-forward org-plain-link-re limit t)
  4759. (setq f (get-text-property (match-beginning 0) 'face))
  4760. (if (or (eq f 'org-tag)
  4761. (and (listp f) (memq 'org-tag f)))
  4762. nil
  4763. (add-text-properties (match-beginning 0) (match-end 0)
  4764. (list 'mouse-face 'highlight
  4765. 'rear-nonsticky org-nonsticky-props
  4766. 'keymap org-mouse-map
  4767. ))
  4768. (throw 'exit t))))))
  4769. (defun org-activate-code (limit)
  4770. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4771. (unless (get-text-property (match-beginning 1) 'face)
  4772. (remove-text-properties (match-beginning 0) (match-end 0)
  4773. '(display t invisible t intangible t))
  4774. t)))
  4775. (defun org-activate-angle-links (limit)
  4776. "Run through the buffer and add overlays to links."
  4777. (if (re-search-forward org-angle-link-re limit t)
  4778. (progn
  4779. (add-text-properties (match-beginning 0) (match-end 0)
  4780. (list 'mouse-face 'highlight
  4781. 'rear-nonsticky org-nonsticky-props
  4782. 'keymap org-mouse-map
  4783. ))
  4784. t)))
  4785. (defmacro org-maybe-intangible (props)
  4786. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4787. In emacs 21, invisible text is not avoided by the command loop, so the
  4788. intangible property is needed to make sure point skips this text.
  4789. In Emacs 22, this is not necessary. The intangible text property has
  4790. led to problems with flyspell. These problems are fixed in flyspell.el,
  4791. but we still avoid setting the property in Emacs 22 and later.
  4792. We use a macro so that the test can happen at compilation time."
  4793. (if (< emacs-major-version 22)
  4794. `(append '(intangible t) ,props)
  4795. props))
  4796. (defun org-activate-bracket-links (limit)
  4797. "Run through the buffer and add overlays to bracketed links."
  4798. (if (re-search-forward org-bracket-link-regexp limit t)
  4799. (let* ((help (concat "LINK: "
  4800. (org-match-string-no-properties 1)))
  4801. ;; FIXME: above we should remove the escapes.
  4802. ;; but that requires another match, protecting match data,
  4803. ;; a lot of overhead for font-lock.
  4804. (ip (org-maybe-intangible
  4805. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4806. 'keymap org-mouse-map 'mouse-face 'highlight
  4807. 'font-lock-multiline t 'help-echo help)))
  4808. (vp (list 'rear-nonsticky org-nonsticky-props
  4809. 'keymap org-mouse-map 'mouse-face 'highlight
  4810. ' font-lock-multiline t 'help-echo help)))
  4811. ;; We need to remove the invisible property here. Table narrowing
  4812. ;; may have made some of this invisible.
  4813. (remove-text-properties (match-beginning 0) (match-end 0)
  4814. '(invisible nil))
  4815. (if (match-end 3)
  4816. (progn
  4817. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4818. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4819. (add-text-properties (match-end 3) (match-end 0) ip))
  4820. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4821. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4822. (add-text-properties (match-end 1) (match-end 0) ip))
  4823. t)))
  4824. (defun org-activate-dates (limit)
  4825. "Run through the buffer and add overlays to dates."
  4826. (if (re-search-forward org-tsr-regexp-both limit t)
  4827. (progn
  4828. (add-text-properties (match-beginning 0) (match-end 0)
  4829. (list 'mouse-face 'highlight
  4830. 'rear-nonsticky org-nonsticky-props
  4831. 'keymap org-mouse-map))
  4832. (when org-display-custom-times
  4833. (if (match-end 3)
  4834. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4835. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4836. t)))
  4837. (defvar org-target-link-regexp nil
  4838. "Regular expression matching radio targets in plain text.")
  4839. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4840. "Regular expression matching a link target.")
  4841. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4842. "Regular expression matching a radio target.")
  4843. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4844. "Regular expression matching any target.")
  4845. (defun org-activate-target-links (limit)
  4846. "Run through the buffer and add overlays to target matches."
  4847. (when org-target-link-regexp
  4848. (let ((case-fold-search t))
  4849. (if (re-search-forward org-target-link-regexp limit t)
  4850. (progn
  4851. (add-text-properties (match-beginning 0) (match-end 0)
  4852. (list 'mouse-face 'highlight
  4853. 'rear-nonsticky org-nonsticky-props
  4854. 'keymap org-mouse-map
  4855. 'help-echo "Radio target link"
  4856. 'org-linked-text t))
  4857. t)))))
  4858. (defun org-update-radio-target-regexp ()
  4859. "Find all radio targets in this file and update the regular expression."
  4860. (interactive)
  4861. (when (memq 'radio org-activate-links)
  4862. (setq org-target-link-regexp
  4863. (org-make-target-link-regexp (org-all-targets 'radio)))
  4864. (org-restart-font-lock)))
  4865. (defun org-hide-wide-columns (limit)
  4866. (let (s e)
  4867. (setq s (text-property-any (point) (or limit (point-max))
  4868. 'org-cwidth t))
  4869. (when s
  4870. (setq e (next-single-property-change s 'org-cwidth))
  4871. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4872. (goto-char e)
  4873. t)))
  4874. (defvar org-latex-and-specials-regexp nil
  4875. "Regular expression for highlighting export special stuff.")
  4876. (defvar org-match-substring-regexp)
  4877. (defvar org-match-substring-with-braces-regexp)
  4878. (defvar org-export-html-special-string-regexps)
  4879. (defun org-compute-latex-and-specials-regexp ()
  4880. "Compute regular expression for stuff treated specially by exporters."
  4881. (if (not org-highlight-latex-fragments-and-specials)
  4882. (org-set-local 'org-latex-and-specials-regexp nil)
  4883. (let*
  4884. ((matchers (plist-get org-format-latex-options :matchers))
  4885. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4886. org-latex-regexps)))
  4887. (options (org-combine-plists (org-default-export-plist)
  4888. (org-infile-export-plist)))
  4889. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4890. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4891. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4892. (org-export-html-expand (plist-get options :expand-quoted-html))
  4893. (org-export-with-special-strings (plist-get options :special-strings))
  4894. (re-sub
  4895. (cond
  4896. ((equal org-export-with-sub-superscripts '{})
  4897. (list org-match-substring-with-braces-regexp))
  4898. (org-export-with-sub-superscripts
  4899. (list org-match-substring-regexp))
  4900. (t nil)))
  4901. (re-latex
  4902. (if org-export-with-LaTeX-fragments
  4903. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4904. (re-macros
  4905. (if org-export-with-TeX-macros
  4906. (list (concat "\\\\"
  4907. (regexp-opt
  4908. (append (mapcar 'car org-html-entities)
  4909. (if (boundp 'org-latex-entities)
  4910. org-latex-entities nil))
  4911. 'words))) ; FIXME
  4912. ))
  4913. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4914. (re-special (if org-export-with-special-strings
  4915. (mapcar (lambda (x) (car x))
  4916. org-export-html-special-string-regexps)))
  4917. (re-rest
  4918. (delq nil
  4919. (list
  4920. (if org-export-html-expand "@<[^>\n]+>")
  4921. ))))
  4922. (org-set-local
  4923. 'org-latex-and-specials-regexp
  4924. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4925. re-rest) "\\|")))))
  4926. (defface org-latex-and-export-specials
  4927. (let ((font (cond ((assq :inherit custom-face-attributes)
  4928. '(:inherit underline))
  4929. (t '(:underline t)))))
  4930. `((((class grayscale) (background light))
  4931. (:foreground "DimGray" ,@font))
  4932. (((class grayscale) (background dark))
  4933. (:foreground "LightGray" ,@font))
  4934. (((class color) (background light))
  4935. (:foreground "SaddleBrown"))
  4936. (((class color) (background dark))
  4937. (:foreground "burlywood"))
  4938. (t (,@font))))
  4939. "Face used to highlight math latex and other special exporter stuff."
  4940. :group 'org-faces)
  4941. (defun org-do-latex-and-special-faces (limit)
  4942. "Run through the buffer and add overlays to links."
  4943. (when org-latex-and-specials-regexp
  4944. (let (rtn d)
  4945. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4946. limit t))
  4947. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4948. 'face))
  4949. '(org-code org-verbatim underline)))
  4950. (progn
  4951. (setq rtn t
  4952. d (cond ((member (char-after (1+ (match-beginning 0)))
  4953. '(?_ ?^)) 1)
  4954. (t 0)))
  4955. (font-lock-prepend-text-property
  4956. (+ d (match-beginning 0)) (match-end 0)
  4957. 'face 'org-latex-and-export-specials)
  4958. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4959. '(font-lock-multiline t)))))
  4960. rtn)))
  4961. (defun org-restart-font-lock ()
  4962. "Restart font-lock-mode, to force refontification."
  4963. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4964. (font-lock-mode -1)
  4965. (font-lock-mode 1)))
  4966. (defun org-all-targets (&optional radio)
  4967. "Return a list of all targets in this file.
  4968. With optional argument RADIO, only find radio targets."
  4969. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4970. rtn)
  4971. (save-excursion
  4972. (goto-char (point-min))
  4973. (while (re-search-forward re nil t)
  4974. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4975. rtn)))
  4976. (defun org-make-target-link-regexp (targets)
  4977. "Make regular expression matching all strings in TARGETS.
  4978. The regular expression finds the targets also if there is a line break
  4979. between words."
  4980. (and targets
  4981. (concat
  4982. "\\<\\("
  4983. (mapconcat
  4984. (lambda (x)
  4985. (while (string-match " +" x)
  4986. (setq x (replace-match "\\s-+" t t x)))
  4987. x)
  4988. targets
  4989. "\\|")
  4990. "\\)\\>")))
  4991. (defun org-activate-tags (limit)
  4992. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4993. (progn
  4994. (add-text-properties (match-beginning 1) (match-end 1)
  4995. (list 'mouse-face 'highlight
  4996. 'rear-nonsticky org-nonsticky-props
  4997. 'keymap org-mouse-map))
  4998. t)))
  4999. (defun org-outline-level ()
  5000. (save-excursion
  5001. (looking-at outline-regexp)
  5002. (if (match-beginning 1)
  5003. (+ (org-get-string-indentation (match-string 1)) 1000)
  5004. (1- (- (match-end 0) (match-beginning 0))))))
  5005. (defvar org-font-lock-keywords nil)
  5006. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  5007. "Regular expression matching a property line.")
  5008. (defun org-set-font-lock-defaults ()
  5009. (let* ((em org-fontify-emphasized-text)
  5010. (lk org-activate-links)
  5011. (org-font-lock-extra-keywords
  5012. (list
  5013. ;; Headlines
  5014. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  5015. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  5016. ;; Table lines
  5017. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5018. (1 'org-table t))
  5019. ;; Table internals
  5020. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5021. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5022. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5023. ;; Drawers
  5024. (list org-drawer-regexp '(0 'org-special-keyword t))
  5025. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5026. ;; Properties
  5027. (list org-property-re
  5028. '(1 'org-special-keyword t)
  5029. '(3 'org-property-value t))
  5030. (if org-format-transports-properties-p
  5031. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  5032. ;; Links
  5033. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5034. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5035. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5036. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5037. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5038. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5039. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5040. '(org-hide-wide-columns (0 nil append))
  5041. ;; TODO lines
  5042. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  5043. '(1 (org-get-todo-face 1) t))
  5044. ;; DONE
  5045. (if org-fontify-done-headline
  5046. (list (concat "^[*]+ +\\<\\("
  5047. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5048. "\\)\\(.*\\)")
  5049. '(2 'org-headline-done t))
  5050. nil)
  5051. ;; Priorities
  5052. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  5053. ;; Special keywords
  5054. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5055. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5056. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5057. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5058. ;; Emphasis
  5059. (if em
  5060. (if (featurep 'xemacs)
  5061. '(org-do-emphasis-faces (0 nil append))
  5062. '(org-do-emphasis-faces)))
  5063. ;; Checkboxes
  5064. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  5065. 2 'bold prepend)
  5066. (if org-provide-checkbox-statistics
  5067. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5068. (0 (org-get-checkbox-statistics-face) t)))
  5069. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  5070. '(1 'org-archived prepend))
  5071. ;; Specials
  5072. '(org-do-latex-and-special-faces)
  5073. ;; Code
  5074. '(org-activate-code (1 'org-code t))
  5075. ;; COMMENT
  5076. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5077. "\\|" org-quote-string "\\)\\>")
  5078. '(1 'org-special-keyword t))
  5079. '("^#.*" (0 'font-lock-comment-face t))
  5080. )))
  5081. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5082. ;; Now set the full font-lock-keywords
  5083. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5084. (org-set-local 'font-lock-defaults
  5085. '(org-font-lock-keywords t nil nil backward-paragraph))
  5086. (kill-local-variable 'font-lock-keywords) nil))
  5087. (defvar org-m nil)
  5088. (defvar org-l nil)
  5089. (defvar org-f nil)
  5090. (defun org-get-level-face (n)
  5091. "Get the right face for match N in font-lock matching of healdines."
  5092. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5093. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5094. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5095. (cond
  5096. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5097. ((eq n 2) org-f)
  5098. (t (if org-level-color-stars-only nil org-f))))
  5099. (defun org-get-todo-face (kwd)
  5100. "Get the right face for a TODO keyword KWD.
  5101. If KWD is a number, get the corresponding match group."
  5102. (if (numberp kwd) (setq kwd (match-string kwd)))
  5103. (or (cdr (assoc kwd org-todo-keyword-faces))
  5104. (and (member kwd org-done-keywords) 'org-done)
  5105. 'org-todo))
  5106. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5107. "Remove fontification and activation overlays from links."
  5108. (font-lock-default-unfontify-region beg end)
  5109. (let* ((buffer-undo-list t)
  5110. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5111. (inhibit-modification-hooks t)
  5112. deactivate-mark buffer-file-name buffer-file-truename)
  5113. (remove-text-properties beg end
  5114. '(mouse-face t keymap t org-linked-text t
  5115. invisible t intangible t))))
  5116. ;;;; Visibility cycling, including org-goto and indirect buffer
  5117. ;;; Cycling
  5118. (defvar org-cycle-global-status nil)
  5119. (make-variable-buffer-local 'org-cycle-global-status)
  5120. (defvar org-cycle-subtree-status nil)
  5121. (make-variable-buffer-local 'org-cycle-subtree-status)
  5122. ;;;###autoload
  5123. (defun org-cycle (&optional arg)
  5124. "Visibility cycling for Org-mode.
  5125. - When this function is called with a prefix argument, rotate the entire
  5126. buffer through 3 states (global cycling)
  5127. 1. OVERVIEW: Show only top-level headlines.
  5128. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5129. 3. SHOW ALL: Show everything.
  5130. - When point is at the beginning of a headline, rotate the subtree started
  5131. by this line through 3 different states (local cycling)
  5132. 1. FOLDED: Only the main headline is shown.
  5133. 2. CHILDREN: The main headline and the direct children are shown.
  5134. From this state, you can move to one of the children
  5135. and zoom in further.
  5136. 3. SUBTREE: Show the entire subtree, including body text.
  5137. - When there is a numeric prefix, go up to a heading with level ARG, do
  5138. a `show-subtree' and return to the previous cursor position. If ARG
  5139. is negative, go up that many levels.
  5140. - When point is not at the beginning of a headline, execute
  5141. `indent-relative', like TAB normally does. See the option
  5142. `org-cycle-emulate-tab' for details.
  5143. - Special case: if point is at the beginning of the buffer and there is
  5144. no headline in line 1, this function will act as if called with prefix arg.
  5145. But only if also the variable `org-cycle-global-at-bob' is t."
  5146. (interactive "P")
  5147. (org-load-modules-maybe)
  5148. (let* ((outline-regexp
  5149. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5150. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5151. outline-regexp))
  5152. (bob-special (and org-cycle-global-at-bob (bobp)
  5153. (not (looking-at outline-regexp))))
  5154. (org-cycle-hook
  5155. (if bob-special
  5156. (delq 'org-optimize-window-after-visibility-change
  5157. (copy-sequence org-cycle-hook))
  5158. org-cycle-hook))
  5159. (pos (point)))
  5160. (if (or bob-special (equal arg '(4)))
  5161. ;; special case: use global cycling
  5162. (setq arg t))
  5163. (cond
  5164. ((org-at-table-p 'any)
  5165. ;; Enter the table or move to the next field in the table
  5166. (or (org-table-recognize-table.el)
  5167. (progn
  5168. (if arg (org-table-edit-field t)
  5169. (org-table-justify-field-maybe)
  5170. (call-interactively 'org-table-next-field)))))
  5171. ((eq arg t) ;; Global cycling
  5172. (cond
  5173. ((and (eq last-command this-command)
  5174. (eq org-cycle-global-status 'overview))
  5175. ;; We just created the overview - now do table of contents
  5176. ;; This can be slow in very large buffers, so indicate action
  5177. (message "CONTENTS...")
  5178. (org-content)
  5179. (message "CONTENTS...done")
  5180. (setq org-cycle-global-status 'contents)
  5181. (run-hook-with-args 'org-cycle-hook 'contents))
  5182. ((and (eq last-command this-command)
  5183. (eq org-cycle-global-status 'contents))
  5184. ;; We just showed the table of contents - now show everything
  5185. (show-all)
  5186. (message "SHOW ALL")
  5187. (setq org-cycle-global-status 'all)
  5188. (run-hook-with-args 'org-cycle-hook 'all))
  5189. (t
  5190. ;; Default action: go to overview
  5191. (org-overview)
  5192. (message "OVERVIEW")
  5193. (setq org-cycle-global-status 'overview)
  5194. (run-hook-with-args 'org-cycle-hook 'overview))))
  5195. ((and org-drawers org-drawer-regexp
  5196. (save-excursion
  5197. (beginning-of-line 1)
  5198. (looking-at org-drawer-regexp)))
  5199. ;; Toggle block visibility
  5200. (org-flag-drawer
  5201. (not (get-char-property (match-end 0) 'invisible))))
  5202. ((integerp arg)
  5203. ;; Show-subtree, ARG levels up from here.
  5204. (save-excursion
  5205. (org-back-to-heading)
  5206. (outline-up-heading (if (< arg 0) (- arg)
  5207. (- (funcall outline-level) arg)))
  5208. (org-show-subtree)))
  5209. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5210. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5211. ;; At a heading: rotate between three different views
  5212. (org-back-to-heading)
  5213. (let ((goal-column 0) eoh eol eos)
  5214. ;; First, some boundaries
  5215. (save-excursion
  5216. (org-back-to-heading)
  5217. (save-excursion
  5218. (beginning-of-line 2)
  5219. (while (and (not (eobp)) ;; this is like `next-line'
  5220. (get-char-property (1- (point)) 'invisible))
  5221. (beginning-of-line 2)) (setq eol (point)))
  5222. (outline-end-of-heading) (setq eoh (point))
  5223. (org-end-of-subtree t)
  5224. (unless (eobp)
  5225. (skip-chars-forward " \t\n")
  5226. (beginning-of-line 1) ; in case this is an item
  5227. )
  5228. (setq eos (1- (point))))
  5229. ;; Find out what to do next and set `this-command'
  5230. (cond
  5231. ((= eos eoh)
  5232. ;; Nothing is hidden behind this heading
  5233. (message "EMPTY ENTRY")
  5234. (setq org-cycle-subtree-status nil)
  5235. (save-excursion
  5236. (goto-char eos)
  5237. (outline-next-heading)
  5238. (if (org-invisible-p) (org-flag-heading nil))))
  5239. ((or (>= eol eos)
  5240. (not (string-match "\\S-" (buffer-substring eol eos))))
  5241. ;; Entire subtree is hidden in one line: open it
  5242. (org-show-entry)
  5243. (show-children)
  5244. (message "CHILDREN")
  5245. (save-excursion
  5246. (goto-char eos)
  5247. (outline-next-heading)
  5248. (if (org-invisible-p) (org-flag-heading nil)))
  5249. (setq org-cycle-subtree-status 'children)
  5250. (run-hook-with-args 'org-cycle-hook 'children))
  5251. ((and (eq last-command this-command)
  5252. (eq org-cycle-subtree-status 'children))
  5253. ;; We just showed the children, now show everything.
  5254. (org-show-subtree)
  5255. (message "SUBTREE")
  5256. (setq org-cycle-subtree-status 'subtree)
  5257. (run-hook-with-args 'org-cycle-hook 'subtree))
  5258. (t
  5259. ;; Default action: hide the subtree.
  5260. (hide-subtree)
  5261. (message "FOLDED")
  5262. (setq org-cycle-subtree-status 'folded)
  5263. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5264. ;; TAB emulation
  5265. (buffer-read-only (org-back-to-heading))
  5266. ((org-try-cdlatex-tab))
  5267. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5268. (or (not (bolp))
  5269. (not (looking-at outline-regexp))))
  5270. (call-interactively (global-key-binding "\t")))
  5271. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5272. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5273. (or (and (eq org-cycle-emulate-tab 'white)
  5274. (= (match-end 0) (point-at-eol)))
  5275. (and (eq org-cycle-emulate-tab 'whitestart)
  5276. (>= (match-end 0) pos))))
  5277. t
  5278. (eq org-cycle-emulate-tab t))
  5279. ; (if (and (looking-at "[ \n\r\t]")
  5280. ; (string-match "^[ \t]*$" (buffer-substring
  5281. ; (point-at-bol) (point))))
  5282. ; (progn
  5283. ; (beginning-of-line 1)
  5284. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5285. (call-interactively (global-key-binding "\t")))
  5286. (t (save-excursion
  5287. (org-back-to-heading)
  5288. (org-cycle))))))
  5289. ;;;###autoload
  5290. (defun org-global-cycle (&optional arg)
  5291. "Cycle the global visibility. For details see `org-cycle'."
  5292. (interactive "P")
  5293. (let ((org-cycle-include-plain-lists
  5294. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5295. (if (integerp arg)
  5296. (progn
  5297. (show-all)
  5298. (hide-sublevels arg)
  5299. (setq org-cycle-global-status 'contents))
  5300. (org-cycle '(4)))))
  5301. (defun org-overview ()
  5302. "Switch to overview mode, shoing only top-level headlines.
  5303. Really, this shows all headlines with level equal or greater than the level
  5304. of the first headline in the buffer. This is important, because if the
  5305. first headline is not level one, then (hide-sublevels 1) gives confusing
  5306. results."
  5307. (interactive)
  5308. (let ((level (save-excursion
  5309. (goto-char (point-min))
  5310. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5311. (progn
  5312. (goto-char (match-beginning 0))
  5313. (funcall outline-level))))))
  5314. (and level (hide-sublevels level))))
  5315. (defun org-content (&optional arg)
  5316. "Show all headlines in the buffer, like a table of contents.
  5317. With numerical argument N, show content up to level N."
  5318. (interactive "P")
  5319. (save-excursion
  5320. ;; Visit all headings and show their offspring
  5321. (and (integerp arg) (org-overview))
  5322. (goto-char (point-max))
  5323. (catch 'exit
  5324. (while (and (progn (condition-case nil
  5325. (outline-previous-visible-heading 1)
  5326. (error (goto-char (point-min))))
  5327. t)
  5328. (looking-at outline-regexp))
  5329. (if (integerp arg)
  5330. (show-children (1- arg))
  5331. (show-branches))
  5332. (if (bobp) (throw 'exit nil))))))
  5333. (defun org-optimize-window-after-visibility-change (state)
  5334. "Adjust the window after a change in outline visibility.
  5335. This function is the default value of the hook `org-cycle-hook'."
  5336. (when (get-buffer-window (current-buffer))
  5337. (cond
  5338. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5339. ; ((eq state 'overview) (org-beginning-of-line))
  5340. ((eq state 'content) nil)
  5341. ((eq state 'all) nil)
  5342. ((eq state 'folded) nil)
  5343. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5344. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5345. (defun org-compact-display-after-subtree-move ()
  5346. (let (beg end)
  5347. (save-excursion
  5348. (if (org-up-heading-safe)
  5349. (progn
  5350. (hide-subtree)
  5351. (show-entry)
  5352. (show-children)
  5353. (org-cycle-show-empty-lines 'children)
  5354. (org-cycle-hide-drawers 'children))
  5355. (org-overview)))))
  5356. (defun org-cycle-show-empty-lines (state)
  5357. "Show empty lines above all visible headlines.
  5358. The region to be covered depends on STATE when called through
  5359. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5360. entire buffer covered. Note that an empty line is only shown if there
  5361. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5362. (when (> org-cycle-separator-lines 0)
  5363. (save-excursion
  5364. (let* ((n org-cycle-separator-lines)
  5365. (re (cond
  5366. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5367. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5368. (t (let ((ns (number-to-string (- n 2))))
  5369. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5370. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5371. beg end)
  5372. (cond
  5373. ((memq state '(overview contents t))
  5374. (setq beg (point-min) end (point-max)))
  5375. ((memq state '(children folded))
  5376. (setq beg (point) end (progn (org-end-of-subtree t t)
  5377. (beginning-of-line 2)
  5378. (point)))))
  5379. (when beg
  5380. (goto-char beg)
  5381. (while (re-search-forward re end t)
  5382. (if (not (get-char-property (match-end 1) 'invisible))
  5383. (outline-flag-region
  5384. (match-beginning 1) (match-end 1) nil)))))))
  5385. ;; Never hide empty lines at the end of the file.
  5386. (save-excursion
  5387. (goto-char (point-max))
  5388. (outline-previous-heading)
  5389. (outline-end-of-heading)
  5390. (if (and (looking-at "[ \t\n]+")
  5391. (= (match-end 0) (point-max)))
  5392. (outline-flag-region (point) (match-end 0) nil))))
  5393. (defun org-subtree-end-visible-p ()
  5394. "Is the end of the current subtree visible?"
  5395. (pos-visible-in-window-p
  5396. (save-excursion (org-end-of-subtree t) (point))))
  5397. (defun org-first-headline-recenter (&optional N)
  5398. "Move cursor to the first headline and recenter the headline.
  5399. Optional argument N means, put the headline into the Nth line of the window."
  5400. (goto-char (point-min))
  5401. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5402. (beginning-of-line)
  5403. (recenter (prefix-numeric-value N))))
  5404. ;;; Org-goto
  5405. (defvar org-goto-window-configuration nil)
  5406. (defvar org-goto-marker nil)
  5407. (defvar org-goto-map
  5408. (let ((map (make-sparse-keymap)))
  5409. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5410. (while (setq cmd (pop cmds))
  5411. (substitute-key-definition cmd cmd map global-map)))
  5412. (suppress-keymap map)
  5413. (org-defkey map "\C-m" 'org-goto-ret)
  5414. (org-defkey map [(return)] 'org-goto-ret)
  5415. (org-defkey map [(left)] 'org-goto-left)
  5416. (org-defkey map [(right)] 'org-goto-right)
  5417. (org-defkey map [(control ?g)] 'org-goto-quit)
  5418. (org-defkey map "\C-i" 'org-cycle)
  5419. (org-defkey map [(tab)] 'org-cycle)
  5420. (org-defkey map [(down)] 'outline-next-visible-heading)
  5421. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5422. (if org-goto-auto-isearch
  5423. (if (fboundp 'define-key-after)
  5424. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5425. nil)
  5426. (org-defkey map "q" 'org-goto-quit)
  5427. (org-defkey map "n" 'outline-next-visible-heading)
  5428. (org-defkey map "p" 'outline-previous-visible-heading)
  5429. (org-defkey map "f" 'outline-forward-same-level)
  5430. (org-defkey map "b" 'outline-backward-same-level)
  5431. (org-defkey map "u" 'outline-up-heading))
  5432. (org-defkey map "/" 'org-occur)
  5433. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5434. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5435. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5436. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5437. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5438. map))
  5439. (defconst org-goto-help
  5440. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5441. RET=jump to location [Q]uit and return to previous location
  5442. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5443. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5444. (defun org-goto (&optional alternative-interface)
  5445. "Look up a different location in the current file, keeping current visibility.
  5446. When you want look-up or go to a different location in a document, the
  5447. fastest way is often to fold the entire buffer and then dive into the tree.
  5448. This method has the disadvantage, that the previous location will be folded,
  5449. which may not be what you want.
  5450. This command works around this by showing a copy of the current buffer
  5451. in an indirect buffer, in overview mode. You can dive into the tree in
  5452. that copy, use org-occur and incremental search to find a location.
  5453. When pressing RET or `Q', the command returns to the original buffer in
  5454. which the visibility is still unchanged. After RET is will also jump to
  5455. the location selected in the indirect buffer and expose the
  5456. the headline hierarchy above."
  5457. (interactive "P")
  5458. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5459. (org-refile-use-outline-path t)
  5460. (interface
  5461. (if (not alternative-interface)
  5462. org-goto-interface
  5463. (if (eq org-goto-interface 'outline)
  5464. 'outline-path-completion
  5465. 'outline)))
  5466. (org-goto-start-pos (point))
  5467. (selected-point
  5468. (if (eq interface 'outline)
  5469. (car (org-get-location (current-buffer) org-goto-help))
  5470. (nth 3 (org-refile-get-location "Goto: ")))))
  5471. (if selected-point
  5472. (progn
  5473. (org-mark-ring-push org-goto-start-pos)
  5474. (goto-char selected-point)
  5475. (if (or (org-invisible-p) (org-invisible-p2))
  5476. (org-show-context 'org-goto)))
  5477. (message "Quit"))))
  5478. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5479. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5480. (defvar org-goto-local-auto-isearch-map) ; defined below
  5481. (defun org-get-location (buf help)
  5482. "Let the user select a location in the Org-mode buffer BUF.
  5483. This function uses a recursive edit. It returns the selected position
  5484. or nil."
  5485. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5486. (isearch-hide-immediately nil)
  5487. (isearch-search-fun-function
  5488. (lambda () 'org-goto-local-search-forward-headings))
  5489. (org-goto-selected-point org-goto-exit-command))
  5490. (save-excursion
  5491. (save-window-excursion
  5492. (delete-other-windows)
  5493. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5494. (switch-to-buffer
  5495. (condition-case nil
  5496. (make-indirect-buffer (current-buffer) "*org-goto*")
  5497. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5498. (with-output-to-temp-buffer "*Help*"
  5499. (princ help))
  5500. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5501. (setq buffer-read-only nil)
  5502. (let ((org-startup-truncated t)
  5503. (org-startup-folded nil)
  5504. (org-startup-align-all-tables nil))
  5505. (org-mode)
  5506. (org-overview))
  5507. (setq buffer-read-only t)
  5508. (if (and (boundp 'org-goto-start-pos)
  5509. (integer-or-marker-p org-goto-start-pos))
  5510. (let ((org-show-hierarchy-above t)
  5511. (org-show-siblings t)
  5512. (org-show-following-heading t))
  5513. (goto-char org-goto-start-pos)
  5514. (and (org-invisible-p) (org-show-context)))
  5515. (goto-char (point-min)))
  5516. (org-beginning-of-line)
  5517. (message "Select location and press RET")
  5518. (use-local-map org-goto-map)
  5519. (recursive-edit)
  5520. ))
  5521. (kill-buffer "*org-goto*")
  5522. (cons org-goto-selected-point org-goto-exit-command)))
  5523. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5524. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5525. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5526. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5527. (defun org-goto-local-search-forward-headings (string bound noerror)
  5528. "Search and make sure that anu matches are in headlines."
  5529. (catch 'return
  5530. (while (search-forward string bound noerror)
  5531. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5532. (and (member :headline context)
  5533. (not (member :tags context))))
  5534. (throw 'return (point))))))
  5535. (defun org-goto-local-auto-isearch ()
  5536. "Start isearch."
  5537. (interactive)
  5538. (goto-char (point-min))
  5539. (let ((keys (this-command-keys)))
  5540. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5541. (isearch-mode t)
  5542. (isearch-process-search-char (string-to-char keys)))))
  5543. (defun org-goto-ret (&optional arg)
  5544. "Finish `org-goto' by going to the new location."
  5545. (interactive "P")
  5546. (setq org-goto-selected-point (point)
  5547. org-goto-exit-command 'return)
  5548. (throw 'exit nil))
  5549. (defun org-goto-left ()
  5550. "Finish `org-goto' by going to the new location."
  5551. (interactive)
  5552. (if (org-on-heading-p)
  5553. (progn
  5554. (beginning-of-line 1)
  5555. (setq org-goto-selected-point (point)
  5556. org-goto-exit-command 'left)
  5557. (throw 'exit nil))
  5558. (error "Not on a heading")))
  5559. (defun org-goto-right ()
  5560. "Finish `org-goto' by going to the new location."
  5561. (interactive)
  5562. (if (org-on-heading-p)
  5563. (progn
  5564. (setq org-goto-selected-point (point)
  5565. org-goto-exit-command 'right)
  5566. (throw 'exit nil))
  5567. (error "Not on a heading")))
  5568. (defun org-goto-quit ()
  5569. "Finish `org-goto' without cursor motion."
  5570. (interactive)
  5571. (setq org-goto-selected-point nil)
  5572. (setq org-goto-exit-command 'quit)
  5573. (throw 'exit nil))
  5574. ;;; Indirect buffer display of subtrees
  5575. (defvar org-indirect-dedicated-frame nil
  5576. "This is the frame being used for indirect tree display.")
  5577. (defvar org-last-indirect-buffer nil)
  5578. (defun org-tree-to-indirect-buffer (&optional arg)
  5579. "Create indirect buffer and narrow it to current subtree.
  5580. With numerical prefix ARG, go up to this level and then take that tree.
  5581. If ARG is negative, go up that many levels.
  5582. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5583. indirect buffer previously made with this command, to avoid proliferation of
  5584. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5585. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5586. is kept so that you can work with several indirect buffers at the same time.
  5587. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5588. requests that a new frame be made for the new buffer, so that the dedicated
  5589. frame is not changed."
  5590. (interactive "P")
  5591. (let ((cbuf (current-buffer))
  5592. (cwin (selected-window))
  5593. (pos (point))
  5594. beg end level heading ibuf)
  5595. (save-excursion
  5596. (org-back-to-heading t)
  5597. (when (numberp arg)
  5598. (setq level (org-outline-level))
  5599. (if (< arg 0) (setq arg (+ level arg)))
  5600. (while (> (setq level (org-outline-level)) arg)
  5601. (outline-up-heading 1 t)))
  5602. (setq beg (point)
  5603. heading (org-get-heading))
  5604. (org-end-of-subtree t) (setq end (point)))
  5605. (if (and (buffer-live-p org-last-indirect-buffer)
  5606. (not (eq org-indirect-buffer-display 'new-frame))
  5607. (not arg))
  5608. (kill-buffer org-last-indirect-buffer))
  5609. (setq ibuf (org-get-indirect-buffer cbuf)
  5610. org-last-indirect-buffer ibuf)
  5611. (cond
  5612. ((or (eq org-indirect-buffer-display 'new-frame)
  5613. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5614. (select-frame (make-frame))
  5615. (delete-other-windows)
  5616. (switch-to-buffer ibuf)
  5617. (org-set-frame-title heading))
  5618. ((eq org-indirect-buffer-display 'dedicated-frame)
  5619. (raise-frame
  5620. (select-frame (or (and org-indirect-dedicated-frame
  5621. (frame-live-p org-indirect-dedicated-frame)
  5622. org-indirect-dedicated-frame)
  5623. (setq org-indirect-dedicated-frame (make-frame)))))
  5624. (delete-other-windows)
  5625. (switch-to-buffer ibuf)
  5626. (org-set-frame-title (concat "Indirect: " heading)))
  5627. ((eq org-indirect-buffer-display 'current-window)
  5628. (switch-to-buffer ibuf))
  5629. ((eq org-indirect-buffer-display 'other-window)
  5630. (pop-to-buffer ibuf))
  5631. (t (error "Invalid value.")))
  5632. (if (featurep 'xemacs)
  5633. (save-excursion (org-mode) (turn-on-font-lock)))
  5634. (narrow-to-region beg end)
  5635. (show-all)
  5636. (goto-char pos)
  5637. (and (window-live-p cwin) (select-window cwin))))
  5638. (defun org-get-indirect-buffer (&optional buffer)
  5639. (setq buffer (or buffer (current-buffer)))
  5640. (let ((n 1) (base (buffer-name buffer)) bname)
  5641. (while (buffer-live-p
  5642. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5643. (setq n (1+ n)))
  5644. (condition-case nil
  5645. (make-indirect-buffer buffer bname 'clone)
  5646. (error (make-indirect-buffer buffer bname)))))
  5647. (defun org-set-frame-title (title)
  5648. "Set the title of the current frame to the string TITLE."
  5649. ;; FIXME: how to name a single frame in XEmacs???
  5650. (unless (featurep 'xemacs)
  5651. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5652. ;;;; Structure editing
  5653. ;;; Inserting headlines
  5654. (defun org-insert-heading (&optional force-heading)
  5655. "Insert a new heading or item with same depth at point.
  5656. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5657. If point is at the beginning of a headline, insert a sibling before the
  5658. current headline. If point is not at the beginning, do not split the line,
  5659. but create the new hedline after the current line."
  5660. (interactive "P")
  5661. (if (= (buffer-size) 0)
  5662. (insert "\n* ")
  5663. (when (or force-heading (not (org-insert-item)))
  5664. (let* ((head (save-excursion
  5665. (condition-case nil
  5666. (progn
  5667. (org-back-to-heading)
  5668. (match-string 0))
  5669. (error "*"))))
  5670. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5671. pos)
  5672. (cond
  5673. ((and (org-on-heading-p) (bolp)
  5674. (or (bobp)
  5675. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5676. ;; insert before the current line
  5677. (open-line (if blank 2 1)))
  5678. ((and (bolp)
  5679. (or (bobp)
  5680. (save-excursion
  5681. (backward-char 1) (not (org-invisible-p)))))
  5682. ;; insert right here
  5683. nil)
  5684. (t
  5685. ; ;; in the middle of the line
  5686. ; (org-show-entry)
  5687. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5688. ; (if (and
  5689. ; (org-on-heading-p)
  5690. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5691. ; ;; protect the tags
  5692. ;; (let ((tags (match-string 2)) pos)
  5693. ; (delete-region (match-beginning 1) (match-end 1))
  5694. ; (setq pos (point-at-bol))
  5695. ; (newline (if blank 2 1))
  5696. ; (save-excursion
  5697. ; (goto-char pos)
  5698. ; (end-of-line 1)
  5699. ; (insert " " tags)
  5700. ; (org-set-tags nil 'align)))
  5701. ; (newline (if blank 2 1)))
  5702. ; (newline (if blank 2 1))))
  5703. ;; in the middle of the line
  5704. (org-show-entry)
  5705. (let ((split
  5706. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5707. tags pos)
  5708. (if (org-on-heading-p)
  5709. (progn
  5710. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5711. (setq tags (and (match-end 2) (match-string 2)))
  5712. (and (match-end 1)
  5713. (delete-region (match-beginning 1) (match-end 1)))
  5714. (setq pos (point-at-bol))
  5715. (or split (end-of-line 1))
  5716. (delete-horizontal-space)
  5717. (newline (if blank 2 1))
  5718. (when tags
  5719. (save-excursion
  5720. (goto-char pos)
  5721. (end-of-line 1)
  5722. (insert " " tags)
  5723. (org-set-tags nil 'align))))
  5724. (or split (end-of-line 1))
  5725. (newline (if blank 2 1))))))
  5726. (insert head) (just-one-space)
  5727. (setq pos (point))
  5728. (end-of-line 1)
  5729. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5730. (run-hooks 'org-insert-heading-hook)))))
  5731. (defun org-insert-heading-after-current ()
  5732. "Insert a new heading with same level as current, after current subtree."
  5733. (interactive)
  5734. (org-back-to-heading)
  5735. (org-insert-heading)
  5736. (org-move-subtree-down)
  5737. (end-of-line 1))
  5738. (defun org-insert-todo-heading (arg)
  5739. "Insert a new heading with the same level and TODO state as current heading.
  5740. If the heading has no TODO state, or if the state is DONE, use the first
  5741. state (TODO by default). Also with prefix arg, force first state."
  5742. (interactive "P")
  5743. (when (not (org-insert-item 'checkbox))
  5744. (org-insert-heading)
  5745. (save-excursion
  5746. (org-back-to-heading)
  5747. (outline-previous-heading)
  5748. (looking-at org-todo-line-regexp))
  5749. (if (or arg
  5750. (not (match-beginning 2))
  5751. (member (match-string 2) org-done-keywords))
  5752. (insert (car org-todo-keywords-1) " ")
  5753. (insert (match-string 2) " "))))
  5754. (defun org-insert-subheading (arg)
  5755. "Insert a new subheading and demote it.
  5756. Works for outline headings and for plain lists alike."
  5757. (interactive "P")
  5758. (org-insert-heading arg)
  5759. (cond
  5760. ((org-on-heading-p) (org-do-demote))
  5761. ((org-at-item-p) (org-indent-item 1))))
  5762. (defun org-insert-todo-subheading (arg)
  5763. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5764. Works for outline headings and for plain lists alike."
  5765. (interactive "P")
  5766. (org-insert-todo-heading arg)
  5767. (cond
  5768. ((org-on-heading-p) (org-do-demote))
  5769. ((org-at-item-p) (org-indent-item 1))))
  5770. ;;; Promotion and Demotion
  5771. (defun org-promote-subtree ()
  5772. "Promote the entire subtree.
  5773. See also `org-promote'."
  5774. (interactive)
  5775. (save-excursion
  5776. (org-map-tree 'org-promote))
  5777. (org-fix-position-after-promote))
  5778. (defun org-demote-subtree ()
  5779. "Demote the entire subtree. See `org-demote'.
  5780. See also `org-promote'."
  5781. (interactive)
  5782. (save-excursion
  5783. (org-map-tree 'org-demote))
  5784. (org-fix-position-after-promote))
  5785. (defun org-do-promote ()
  5786. "Promote the current heading higher up the tree.
  5787. If the region is active in `transient-mark-mode', promote all headings
  5788. in the region."
  5789. (interactive)
  5790. (save-excursion
  5791. (if (org-region-active-p)
  5792. (org-map-region 'org-promote (region-beginning) (region-end))
  5793. (org-promote)))
  5794. (org-fix-position-after-promote))
  5795. (defun org-do-demote ()
  5796. "Demote the current heading lower down the tree.
  5797. If the region is active in `transient-mark-mode', demote all headings
  5798. in the region."
  5799. (interactive)
  5800. (save-excursion
  5801. (if (org-region-active-p)
  5802. (org-map-region 'org-demote (region-beginning) (region-end))
  5803. (org-demote)))
  5804. (org-fix-position-after-promote))
  5805. (defun org-fix-position-after-promote ()
  5806. "Make sure that after pro/demotion cursor position is right."
  5807. (let ((pos (point)))
  5808. (when (save-excursion
  5809. (beginning-of-line 1)
  5810. (looking-at org-todo-line-regexp)
  5811. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5812. (cond ((eobp) (insert " "))
  5813. ((eolp) (insert " "))
  5814. ((equal (char-after) ?\ ) (forward-char 1))))))
  5815. (defun org-reduced-level (l)
  5816. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5817. (defun org-get-valid-level (level &optional change)
  5818. "Rectify a level change under the influence of `org-odd-levels-only'
  5819. LEVEL is a current level, CHANGE is by how much the level should be
  5820. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5821. even level numbers will become the next higher odd number."
  5822. (if org-odd-levels-only
  5823. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5824. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5825. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5826. (max 1 (+ level change))))
  5827. (if (featurep 'xemacs)
  5828. (define-obsolete-function-alias 'org-get-legal-level
  5829. 'org-get-valid-level)
  5830. (define-obsolete-function-alias 'org-get-legal-level
  5831. 'org-get-valid-level "23.1"))
  5832. (defun org-promote ()
  5833. "Promote the current heading higher up the tree.
  5834. If the region is active in `transient-mark-mode', promote all headings
  5835. in the region."
  5836. (org-back-to-heading t)
  5837. (let* ((level (save-match-data (funcall outline-level)))
  5838. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5839. (diff (abs (- level (length up-head) -1))))
  5840. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5841. (replace-match up-head nil t)
  5842. ;; Fixup tag positioning
  5843. (and org-auto-align-tags (org-set-tags nil t))
  5844. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5845. (defun org-demote ()
  5846. "Demote the current heading lower down the tree.
  5847. If the region is active in `transient-mark-mode', demote all headings
  5848. in the region."
  5849. (org-back-to-heading t)
  5850. (let* ((level (save-match-data (funcall outline-level)))
  5851. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5852. (diff (abs (- level (length down-head) -1))))
  5853. (replace-match down-head nil t)
  5854. ;; Fixup tag positioning
  5855. (and org-auto-align-tags (org-set-tags nil t))
  5856. (if org-adapt-indentation (org-fixup-indentation diff))))
  5857. (defun org-map-tree (fun)
  5858. "Call FUN for every heading underneath the current one."
  5859. (org-back-to-heading)
  5860. (let ((level (funcall outline-level)))
  5861. (save-excursion
  5862. (funcall fun)
  5863. (while (and (progn
  5864. (outline-next-heading)
  5865. (> (funcall outline-level) level))
  5866. (not (eobp)))
  5867. (funcall fun)))))
  5868. (defun org-map-region (fun beg end)
  5869. "Call FUN for every heading between BEG and END."
  5870. (let ((org-ignore-region t))
  5871. (save-excursion
  5872. (setq end (copy-marker end))
  5873. (goto-char beg)
  5874. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5875. (< (point) end))
  5876. (funcall fun))
  5877. (while (and (progn
  5878. (outline-next-heading)
  5879. (< (point) end))
  5880. (not (eobp)))
  5881. (funcall fun)))))
  5882. (defun org-fixup-indentation (diff)
  5883. "Change the indentation in the current entry by DIFF
  5884. However, if any line in the current entry has no indentation, or if it
  5885. would end up with no indentation after the change, nothing at all is done."
  5886. (save-excursion
  5887. (let ((end (save-excursion (outline-next-heading)
  5888. (point-marker)))
  5889. (prohibit (if (> diff 0)
  5890. "^\\S-"
  5891. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5892. col)
  5893. (unless (save-excursion (end-of-line 1)
  5894. (re-search-forward prohibit end t))
  5895. (while (and (< (point) end)
  5896. (re-search-forward "^[ \t]+" end t))
  5897. (goto-char (match-end 0))
  5898. (setq col (current-column))
  5899. (if (< diff 0) (replace-match ""))
  5900. (indent-to (+ diff col))))
  5901. (move-marker end nil))))
  5902. (defun org-convert-to-odd-levels ()
  5903. "Convert an org-mode file with all levels allowed to one with odd levels.
  5904. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5905. level 5 etc."
  5906. (interactive)
  5907. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5908. (let ((org-odd-levels-only nil) n)
  5909. (save-excursion
  5910. (goto-char (point-min))
  5911. (while (re-search-forward "^\\*\\*+ " nil t)
  5912. (setq n (- (length (match-string 0)) 2))
  5913. (while (>= (setq n (1- n)) 0)
  5914. (org-demote))
  5915. (end-of-line 1))))))
  5916. (defun org-convert-to-oddeven-levels ()
  5917. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5918. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5919. section with an even level, conversion would destroy the structure of the file. An error
  5920. is signaled in this case."
  5921. (interactive)
  5922. (goto-char (point-min))
  5923. ;; First check if there are no even levels
  5924. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5925. (org-show-context t)
  5926. (error "Not all levels are odd in this file. Conversion not possible."))
  5927. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5928. (let ((org-odd-levels-only nil) n)
  5929. (save-excursion
  5930. (goto-char (point-min))
  5931. (while (re-search-forward "^\\*\\*+ " nil t)
  5932. (setq n (/ (1- (length (match-string 0))) 2))
  5933. (while (>= (setq n (1- n)) 0)
  5934. (org-promote))
  5935. (end-of-line 1))))))
  5936. (defun org-tr-level (n)
  5937. "Make N odd if required."
  5938. (if org-odd-levels-only (1+ (/ n 2)) n))
  5939. ;;; Vertical tree motion, cutting and pasting of subtrees
  5940. (defun org-move-subtree-up (&optional arg)
  5941. "Move the current subtree up past ARG headlines of the same level."
  5942. (interactive "p")
  5943. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5944. (defun org-move-subtree-down (&optional arg)
  5945. "Move the current subtree down past ARG headlines of the same level."
  5946. (interactive "p")
  5947. (setq arg (prefix-numeric-value arg))
  5948. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5949. 'outline-get-last-sibling))
  5950. (ins-point (make-marker))
  5951. (cnt (abs arg))
  5952. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5953. ;; Select the tree
  5954. (org-back-to-heading)
  5955. (setq beg0 (point))
  5956. (save-excursion
  5957. (setq ne-beg (org-back-over-empty-lines))
  5958. (setq beg (point)))
  5959. (save-match-data
  5960. (save-excursion (outline-end-of-heading)
  5961. (setq folded (org-invisible-p)))
  5962. (outline-end-of-subtree))
  5963. (outline-next-heading)
  5964. (setq ne-end (org-back-over-empty-lines))
  5965. (setq end (point))
  5966. (goto-char beg0)
  5967. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5968. ;; include less whitespace
  5969. (save-excursion
  5970. (goto-char beg)
  5971. (forward-line (- ne-beg ne-end))
  5972. (setq beg (point))))
  5973. ;; Find insertion point, with error handling
  5974. (while (> cnt 0)
  5975. (or (and (funcall movfunc) (looking-at outline-regexp))
  5976. (progn (goto-char beg0)
  5977. (error "Cannot move past superior level or buffer limit")))
  5978. (setq cnt (1- cnt)))
  5979. (if (> arg 0)
  5980. ;; Moving forward - still need to move over subtree
  5981. (progn (org-end-of-subtree t t)
  5982. (save-excursion
  5983. (org-back-over-empty-lines)
  5984. (or (bolp) (newline)))))
  5985. (setq ne-ins (org-back-over-empty-lines))
  5986. (move-marker ins-point (point))
  5987. (setq txt (buffer-substring beg end))
  5988. (delete-region beg end)
  5989. (outline-flag-region (1- beg) beg nil)
  5990. (outline-flag-region (1- (point)) (point) nil)
  5991. (insert txt)
  5992. (or (bolp) (insert "\n"))
  5993. (setq ins-end (point))
  5994. (goto-char ins-point)
  5995. (org-skip-whitespace)
  5996. (when (and (< arg 0)
  5997. (org-first-sibling-p)
  5998. (> ne-ins ne-beg))
  5999. ;; Move whitespace back to beginning
  6000. (save-excursion
  6001. (goto-char ins-end)
  6002. (let ((kill-whole-line t))
  6003. (kill-line (- ne-ins ne-beg)) (point)))
  6004. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6005. (move-marker ins-point nil)
  6006. (org-compact-display-after-subtree-move)
  6007. (unless folded
  6008. (org-show-entry)
  6009. (show-children)
  6010. (org-cycle-hide-drawers 'children))))
  6011. (defvar org-subtree-clip ""
  6012. "Clipboard for cut and paste of subtrees.
  6013. This is actually only a copy of the kill, because we use the normal kill
  6014. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6015. (defvar org-subtree-clip-folded nil
  6016. "Was the last copied subtree folded?
  6017. This is used to fold the tree back after pasting.")
  6018. (defun org-cut-subtree (&optional n)
  6019. "Cut the current subtree into the clipboard.
  6020. With prefix arg N, cut this many sequential subtrees.
  6021. This is a short-hand for marking the subtree and then cutting it."
  6022. (interactive "p")
  6023. (org-copy-subtree n 'cut))
  6024. (defun org-copy-subtree (&optional n cut)
  6025. "Cut the current subtree into the clipboard.
  6026. With prefix arg N, cut this many sequential subtrees.
  6027. This is a short-hand for marking the subtree and then copying it.
  6028. If CUT is non-nil, actually cut the subtree."
  6029. (interactive "p")
  6030. (let (beg end folded (beg0 (point)))
  6031. (if (interactive-p)
  6032. (org-back-to-heading nil) ; take what looks like a subtree
  6033. (org-back-to-heading t)) ; take what is really there
  6034. (org-back-over-empty-lines)
  6035. (setq beg (point))
  6036. (skip-chars-forward " \t\r\n")
  6037. (save-match-data
  6038. (save-excursion (outline-end-of-heading)
  6039. (setq folded (org-invisible-p)))
  6040. (condition-case nil
  6041. (outline-forward-same-level (1- n))
  6042. (error nil))
  6043. (org-end-of-subtree t t))
  6044. (org-back-over-empty-lines)
  6045. (setq end (point))
  6046. (goto-char beg0)
  6047. (when (> end beg)
  6048. (setq org-subtree-clip-folded folded)
  6049. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6050. (setq org-subtree-clip (current-kill 0))
  6051. (message "%s: Subtree(s) with %d characters"
  6052. (if cut "Cut" "Copied")
  6053. (length org-subtree-clip)))))
  6054. (defun org-paste-subtree (&optional level tree)
  6055. "Paste the clipboard as a subtree, with modification of headline level.
  6056. The entire subtree is promoted or demoted in order to match a new headline
  6057. level. By default, the new level is derived from the visible headings
  6058. before and after the insertion point, and taken to be the inferior headline
  6059. level of the two. So if the previous visible heading is level 3 and the
  6060. next is level 4 (or vice versa), level 4 will be used for insertion.
  6061. This makes sure that the subtree remains an independent subtree and does
  6062. not swallow low level entries.
  6063. You can also force a different level, either by using a numeric prefix
  6064. argument, or by inserting the heading marker by hand. For example, if the
  6065. cursor is after \"*****\", then the tree will be shifted to level 5.
  6066. If you want to insert the tree as is, just use \\[yank].
  6067. If optional TREE is given, use this text instead of the kill ring."
  6068. (interactive "P")
  6069. (unless (org-kill-is-subtree-p tree)
  6070. (error "%s"
  6071. (substitute-command-keys
  6072. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6073. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  6074. (^re (concat "^\\(" outline-regexp "\\)"))
  6075. (re (concat "\\(" outline-regexp "\\)"))
  6076. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6077. (old-level (if (string-match ^re txt)
  6078. (- (match-end 0) (match-beginning 0) 1)
  6079. -1))
  6080. (force-level (cond (level (prefix-numeric-value level))
  6081. ((string-match
  6082. ^re_ (buffer-substring (point-at-bol) (point)))
  6083. (- (match-end 1) (match-beginning 1)))
  6084. (t nil)))
  6085. (previous-level (save-excursion
  6086. (condition-case nil
  6087. (progn
  6088. (outline-previous-visible-heading 1)
  6089. (if (looking-at re)
  6090. (- (match-end 0) (match-beginning 0) 1)
  6091. 1))
  6092. (error 1))))
  6093. (next-level (save-excursion
  6094. (condition-case nil
  6095. (progn
  6096. (or (looking-at outline-regexp)
  6097. (outline-next-visible-heading 1))
  6098. (if (looking-at re)
  6099. (- (match-end 0) (match-beginning 0) 1)
  6100. 1))
  6101. (error 1))))
  6102. (new-level (or force-level (max previous-level next-level)))
  6103. (shift (if (or (= old-level -1)
  6104. (= new-level -1)
  6105. (= old-level new-level))
  6106. 0
  6107. (- new-level old-level)))
  6108. (delta (if (> shift 0) -1 1))
  6109. (func (if (> shift 0) 'org-demote 'org-promote))
  6110. (org-odd-levels-only nil)
  6111. beg end)
  6112. ;; Remove the forced level indicator
  6113. (if force-level
  6114. (delete-region (point-at-bol) (point)))
  6115. ;; Paste
  6116. (beginning-of-line 1)
  6117. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6118. (setq beg (point))
  6119. (insert-before-markers txt) ;; FIXME: correct fix????
  6120. (unless (string-match "\n\\'" txt) (insert "\n"))
  6121. (setq end (point))
  6122. (goto-char beg)
  6123. (skip-chars-forward " \t\n\r")
  6124. (setq beg (point))
  6125. ;; Shift if necessary
  6126. (unless (= shift 0)
  6127. (save-restriction
  6128. (narrow-to-region beg end)
  6129. (while (not (= shift 0))
  6130. (org-map-region func (point-min) (point-max))
  6131. (setq shift (+ delta shift)))
  6132. (goto-char (point-min))))
  6133. (when (interactive-p)
  6134. (message "Clipboard pasted as level %d subtree" new-level))
  6135. (if (and kill-ring
  6136. (eq org-subtree-clip (current-kill 0))
  6137. org-subtree-clip-folded)
  6138. ;; The tree was folded before it was killed/copied
  6139. (hide-subtree))))
  6140. (defun org-kill-is-subtree-p (&optional txt)
  6141. "Check if the current kill is an outline subtree, or a set of trees.
  6142. Returns nil if kill does not start with a headline, or if the first
  6143. headline level is not the largest headline level in the tree.
  6144. So this will actually accept several entries of equal levels as well,
  6145. which is OK for `org-paste-subtree'.
  6146. If optional TXT is given, check this string instead of the current kill."
  6147. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6148. (start-level (and kill
  6149. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6150. org-outline-regexp "\\)")
  6151. kill)
  6152. (- (match-end 2) (match-beginning 2) 1)))
  6153. (re (concat "^" org-outline-regexp))
  6154. (start (1+ (match-beginning 2))))
  6155. (if (not start-level)
  6156. (progn
  6157. nil) ;; does not even start with a heading
  6158. (catch 'exit
  6159. (while (setq start (string-match re kill (1+ start)))
  6160. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6161. (throw 'exit nil)))
  6162. t))))
  6163. (defun org-narrow-to-subtree ()
  6164. "Narrow buffer to the current subtree."
  6165. (interactive)
  6166. (save-excursion
  6167. (save-match-data
  6168. (narrow-to-region
  6169. (progn (org-back-to-heading) (point))
  6170. (progn (org-end-of-subtree t t) (point))))))
  6171. ;;; Outline Sorting
  6172. (defun org-sort (with-case)
  6173. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6174. Optional argument WITH-CASE means sort case-sensitively."
  6175. (interactive "P")
  6176. (if (org-at-table-p)
  6177. (org-call-with-arg 'org-table-sort-lines with-case)
  6178. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6179. (defvar org-priority-regexp) ; defined later in the file
  6180. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6181. "Sort entries on a certain level of an outline tree.
  6182. If there is an active region, the entries in the region are sorted.
  6183. Else, if the cursor is before the first entry, sort the top-level items.
  6184. Else, the children of the entry at point are sorted.
  6185. Sorting can be alphabetically, numerically, and by date/time as given by
  6186. the first time stamp in the entry. The command prompts for the sorting
  6187. type unless it has been given to the function through the SORTING-TYPE
  6188. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6189. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6190. called with point at the beginning of the record. It must return either
  6191. a string or a number that should serve as the sorting key for that record.
  6192. Comparing entries ignores case by default. However, with an optional argument
  6193. WITH-CASE, the sorting considers case as well."
  6194. (interactive "P")
  6195. (let ((case-func (if with-case 'identity 'downcase))
  6196. start beg end stars re re2
  6197. txt what tmp plain-list-p)
  6198. ;; Find beginning and end of region to sort
  6199. (cond
  6200. ((org-region-active-p)
  6201. ;; we will sort the region
  6202. (setq end (region-end)
  6203. what "region")
  6204. (goto-char (region-beginning))
  6205. (if (not (org-on-heading-p)) (outline-next-heading))
  6206. (setq start (point)))
  6207. ((org-at-item-p)
  6208. ;; we will sort this plain list
  6209. (org-beginning-of-item-list) (setq start (point))
  6210. (org-end-of-item-list) (setq end (point))
  6211. (goto-char start)
  6212. (setq plain-list-p t
  6213. what "plain list"))
  6214. ((or (org-on-heading-p)
  6215. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6216. ;; we will sort the children of the current headline
  6217. (org-back-to-heading)
  6218. (setq start (point)
  6219. end (progn (org-end-of-subtree t t)
  6220. (org-back-over-empty-lines)
  6221. (point))
  6222. what "children")
  6223. (goto-char start)
  6224. (show-subtree)
  6225. (outline-next-heading))
  6226. (t
  6227. ;; we will sort the top-level entries in this file
  6228. (goto-char (point-min))
  6229. (or (org-on-heading-p) (outline-next-heading))
  6230. (setq start (point) end (point-max) what "top-level")
  6231. (goto-char start)
  6232. (show-all)))
  6233. (setq beg (point))
  6234. (if (>= beg end) (error "Nothing to sort"))
  6235. (unless plain-list-p
  6236. (looking-at "\\(\\*+\\)")
  6237. (setq stars (match-string 1)
  6238. re (concat "^" (regexp-quote stars) " +")
  6239. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6240. txt (buffer-substring beg end))
  6241. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6242. (if (and (not (equal stars "*")) (string-match re2 txt))
  6243. (error "Region to sort contains a level above the first entry")))
  6244. (unless sorting-type
  6245. (message
  6246. (if plain-list-p
  6247. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6248. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6249. what)
  6250. (setq sorting-type (read-char-exclusive))
  6251. (and (= (downcase sorting-type) ?f)
  6252. (setq getkey-func
  6253. (completing-read "Sort using function: "
  6254. obarray 'fboundp t nil nil))
  6255. (setq getkey-func (intern getkey-func)))
  6256. (and (= (downcase sorting-type) ?r)
  6257. (setq property
  6258. (completing-read "Property: "
  6259. (mapcar 'list (org-buffer-property-keys t))
  6260. nil t))))
  6261. (message "Sorting entries...")
  6262. (save-restriction
  6263. (narrow-to-region start end)
  6264. (let ((dcst (downcase sorting-type))
  6265. (now (current-time)))
  6266. (sort-subr
  6267. (/= dcst sorting-type)
  6268. ;; This function moves to the beginning character of the "record" to
  6269. ;; be sorted.
  6270. (if plain-list-p
  6271. (lambda nil
  6272. (if (org-at-item-p) t (goto-char (point-max))))
  6273. (lambda nil
  6274. (if (re-search-forward re nil t)
  6275. (goto-char (match-beginning 0))
  6276. (goto-char (point-max)))))
  6277. ;; This function moves to the last character of the "record" being
  6278. ;; sorted.
  6279. (if plain-list-p
  6280. 'org-end-of-item
  6281. (lambda nil
  6282. (save-match-data
  6283. (condition-case nil
  6284. (outline-forward-same-level 1)
  6285. (error
  6286. (goto-char (point-max)))))))
  6287. ;; This function returns the value that gets sorted against.
  6288. (if plain-list-p
  6289. (lambda nil
  6290. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6291. (cond
  6292. ((= dcst ?n)
  6293. (string-to-number (buffer-substring (match-end 0)
  6294. (point-at-eol))))
  6295. ((= dcst ?a)
  6296. (buffer-substring (match-end 0) (point-at-eol)))
  6297. ((= dcst ?t)
  6298. (if (re-search-forward org-ts-regexp
  6299. (point-at-eol) t)
  6300. (org-time-string-to-time (match-string 0))
  6301. now))
  6302. ((= dcst ?f)
  6303. (if getkey-func
  6304. (progn
  6305. (setq tmp (funcall getkey-func))
  6306. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6307. tmp)
  6308. (error "Invalid key function `%s'" getkey-func)))
  6309. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6310. (lambda nil
  6311. (cond
  6312. ((= dcst ?n)
  6313. (if (looking-at outline-regexp)
  6314. (string-to-number (buffer-substring (match-end 0)
  6315. (point-at-eol)))
  6316. nil))
  6317. ((= dcst ?a)
  6318. (funcall case-func (buffer-substring (point-at-bol)
  6319. (point-at-eol))))
  6320. ((= dcst ?t)
  6321. (if (re-search-forward org-ts-regexp
  6322. (save-excursion
  6323. (forward-line 2)
  6324. (point)) t)
  6325. (org-time-string-to-time (match-string 0))
  6326. now))
  6327. ((= dcst ?p)
  6328. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6329. (string-to-char (match-string 2))
  6330. org-default-priority))
  6331. ((= dcst ?r)
  6332. (or (org-entry-get nil property) ""))
  6333. ((= dcst ?f)
  6334. (if getkey-func
  6335. (progn
  6336. (setq tmp (funcall getkey-func))
  6337. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6338. tmp)
  6339. (error "Invalid key function `%s'" getkey-func)))
  6340. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6341. nil
  6342. (cond
  6343. ((= dcst ?a) 'string<)
  6344. ((= dcst ?t) 'time-less-p)
  6345. (t nil)))))
  6346. (message "Sorting entries...done")))
  6347. (defun org-do-sort (table what &optional with-case sorting-type)
  6348. "Sort TABLE of WHAT according to SORTING-TYPE.
  6349. The user will be prompted for the SORTING-TYPE if the call to this
  6350. function does not specify it. WHAT is only for the prompt, to indicate
  6351. what is being sorted. The sorting key will be extracted from
  6352. the car of the elements of the table.
  6353. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6354. (unless sorting-type
  6355. (message
  6356. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6357. what)
  6358. (setq sorting-type (read-char-exclusive)))
  6359. (let ((dcst (downcase sorting-type))
  6360. extractfun comparefun)
  6361. ;; Define the appropriate functions
  6362. (cond
  6363. ((= dcst ?n)
  6364. (setq extractfun 'string-to-number
  6365. comparefun (if (= dcst sorting-type) '< '>)))
  6366. ((= dcst ?a)
  6367. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6368. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6369. comparefun (if (= dcst sorting-type)
  6370. 'string<
  6371. (lambda (a b) (and (not (string< a b))
  6372. (not (string= a b)))))))
  6373. ((= dcst ?t)
  6374. (setq extractfun
  6375. (lambda (x)
  6376. (if (string-match org-ts-regexp x)
  6377. (time-to-seconds
  6378. (org-time-string-to-time (match-string 0 x)))
  6379. 0))
  6380. comparefun (if (= dcst sorting-type) '< '>)))
  6381. (t (error "Invalid sorting type `%c'" sorting-type)))
  6382. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6383. table)
  6384. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6385. ;;;; Plain list items, including checkboxes
  6386. ;;; Plain list items
  6387. (defun org-at-item-p ()
  6388. "Is point in a line starting a hand-formatted item?"
  6389. (let ((llt org-plain-list-ordered-item-terminator))
  6390. (save-excursion
  6391. (goto-char (point-at-bol))
  6392. (looking-at
  6393. (cond
  6394. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6395. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6396. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6397. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6398. (defun org-in-item-p ()
  6399. "It the cursor inside a plain list item.
  6400. Does not have to be the first line."
  6401. (save-excursion
  6402. (condition-case nil
  6403. (progn
  6404. (org-beginning-of-item)
  6405. (org-at-item-p)
  6406. t)
  6407. (error nil))))
  6408. (defun org-insert-item (&optional checkbox)
  6409. "Insert a new item at the current level.
  6410. Return t when things worked, nil when we are not in an item."
  6411. (when (save-excursion
  6412. (condition-case nil
  6413. (progn
  6414. (org-beginning-of-item)
  6415. (org-at-item-p)
  6416. (if (org-invisible-p) (error "Invisible item"))
  6417. t)
  6418. (error nil)))
  6419. (let* ((bul (match-string 0))
  6420. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6421. (match-end 0)))
  6422. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6423. pos)
  6424. (cond
  6425. ((and (org-at-item-p) (<= (point) eow))
  6426. ;; before the bullet
  6427. (beginning-of-line 1)
  6428. (open-line (if blank 2 1)))
  6429. ((<= (point) eow)
  6430. (beginning-of-line 1))
  6431. (t
  6432. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6433. (end-of-line 1)
  6434. (delete-horizontal-space))
  6435. (newline (if blank 2 1))))
  6436. (insert bul (if checkbox "[ ]" ""))
  6437. (just-one-space)
  6438. (setq pos (point))
  6439. (end-of-line 1)
  6440. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6441. (org-maybe-renumber-ordered-list)
  6442. (and checkbox (org-update-checkbox-count-maybe))
  6443. t))
  6444. ;;; Checkboxes
  6445. (defun org-at-item-checkbox-p ()
  6446. "Is point at a line starting a plain-list item with a checklet?"
  6447. (and (org-at-item-p)
  6448. (save-excursion
  6449. (goto-char (match-end 0))
  6450. (skip-chars-forward " \t")
  6451. (looking-at "\\[[- X]\\]"))))
  6452. (defun org-toggle-checkbox (&optional arg)
  6453. "Toggle the checkbox in the current line."
  6454. (interactive "P")
  6455. (catch 'exit
  6456. (let (beg end status (firstnew 'unknown))
  6457. (cond
  6458. ((org-region-active-p)
  6459. (setq beg (region-beginning) end (region-end)))
  6460. ((org-on-heading-p)
  6461. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6462. ((org-at-item-checkbox-p)
  6463. (let ((pos (point)))
  6464. (replace-match
  6465. (cond (arg "[-]")
  6466. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6467. (t "[ ]"))
  6468. t t)
  6469. (goto-char pos))
  6470. (throw 'exit t))
  6471. (t (error "Not at a checkbox or heading, and no active region")))
  6472. (save-excursion
  6473. (goto-char beg)
  6474. (while (< (point) end)
  6475. (when (org-at-item-checkbox-p)
  6476. (setq status (equal (match-string 0) "[X]"))
  6477. (when (eq firstnew 'unknown)
  6478. (setq firstnew (not status)))
  6479. (replace-match
  6480. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6481. (beginning-of-line 2)))))
  6482. (org-update-checkbox-count-maybe))
  6483. (defun org-update-checkbox-count-maybe ()
  6484. "Update checkbox statistics unless turned off by user."
  6485. (when org-provide-checkbox-statistics
  6486. (org-update-checkbox-count)))
  6487. (defun org-update-checkbox-count (&optional all)
  6488. "Update the checkbox statistics in the current section.
  6489. This will find all statistic cookies like [57%] and [6/12] and update them
  6490. with the current numbers. With optional prefix argument ALL, do this for
  6491. the whole buffer."
  6492. (interactive "P")
  6493. (save-excursion
  6494. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6495. (beg (condition-case nil
  6496. (progn (outline-back-to-heading) (point))
  6497. (error (point-min))))
  6498. (end (move-marker (make-marker)
  6499. (progn (outline-next-heading) (point))))
  6500. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6501. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6502. (re-find (concat re "\\|" re-box))
  6503. beg-cookie end-cookie is-percent c-on c-off lim
  6504. eline curr-ind next-ind continue-from startsearch
  6505. (cstat 0)
  6506. )
  6507. (when all
  6508. (goto-char (point-min))
  6509. (outline-next-heading)
  6510. (setq beg (point) end (point-max)))
  6511. (goto-char end)
  6512. ;; find each statistic cookie
  6513. (while (re-search-backward re-find beg t)
  6514. (setq beg-cookie (match-beginning 1)
  6515. end-cookie (match-end 1)
  6516. cstat (+ cstat (if end-cookie 1 0))
  6517. startsearch (point-at-eol)
  6518. continue-from (point-at-bol)
  6519. is-percent (match-beginning 2)
  6520. lim (cond
  6521. ((org-on-heading-p) (outline-next-heading) (point))
  6522. ((org-at-item-p) (org-end-of-item) (point))
  6523. (t nil))
  6524. c-on 0
  6525. c-off 0)
  6526. (when lim
  6527. ;; find first checkbox for this cookie and gather
  6528. ;; statistics from all that are at this indentation level
  6529. (goto-char startsearch)
  6530. (if (re-search-forward re-box lim t)
  6531. (progn
  6532. (org-beginning-of-item)
  6533. (setq curr-ind (org-get-indentation))
  6534. (setq next-ind curr-ind)
  6535. (while (= curr-ind next-ind)
  6536. (save-excursion (end-of-line) (setq eline (point)))
  6537. (if (re-search-forward re-box eline t)
  6538. (if (member (match-string 2) '("[ ]" "[-]"))
  6539. (setq c-off (1+ c-off))
  6540. (setq c-on (1+ c-on))
  6541. )
  6542. )
  6543. (org-end-of-item)
  6544. (setq next-ind (org-get-indentation))
  6545. )))
  6546. (goto-char continue-from)
  6547. ;; update cookie
  6548. (when end-cookie
  6549. (delete-region beg-cookie end-cookie)
  6550. (goto-char beg-cookie)
  6551. (insert
  6552. (if is-percent
  6553. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6554. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6555. ;; update items checkbox if it has one
  6556. (when (org-at-item-p)
  6557. (org-beginning-of-item)
  6558. (when (and (> (+ c-on c-off) 0)
  6559. (re-search-forward re-box (point-at-eol) t))
  6560. (setq beg-cookie (match-beginning 2)
  6561. end-cookie (match-end 2))
  6562. (delete-region beg-cookie end-cookie)
  6563. (goto-char beg-cookie)
  6564. (cond ((= c-off 0) (insert "[X]"))
  6565. ((= c-on 0) (insert "[ ]"))
  6566. (t (insert "[-]")))
  6567. )))
  6568. (goto-char continue-from))
  6569. (when (interactive-p)
  6570. (message "Checkbox satistics updated %s (%d places)"
  6571. (if all "in entire file" "in current outline entry") cstat)))))
  6572. (defun org-get-checkbox-statistics-face ()
  6573. "Select the face for checkbox statistics.
  6574. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6575. it will be `org-todo'."
  6576. (if (match-end 1)
  6577. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6578. (if (and (> (match-end 2) (match-beginning 2))
  6579. (equal (match-string 2) (match-string 3)))
  6580. 'org-done
  6581. 'org-todo)))
  6582. (defun org-get-indentation (&optional line)
  6583. "Get the indentation of the current line, interpreting tabs.
  6584. When LINE is given, assume it represents a line and compute its indentation."
  6585. (if line
  6586. (if (string-match "^ *" (org-remove-tabs line))
  6587. (match-end 0))
  6588. (save-excursion
  6589. (beginning-of-line 1)
  6590. (skip-chars-forward " \t")
  6591. (current-column))))
  6592. (defun org-remove-tabs (s &optional width)
  6593. "Replace tabulators in S with spaces.
  6594. Assumes that s is a single line, starting in column 0."
  6595. (setq width (or width tab-width))
  6596. (while (string-match "\t" s)
  6597. (setq s (replace-match
  6598. (make-string
  6599. (- (* width (/ (+ (match-beginning 0) width) width))
  6600. (match-beginning 0)) ?\ )
  6601. t t s)))
  6602. s)
  6603. (defun org-fix-indentation (line ind)
  6604. "Fix indentation in LINE.
  6605. IND is a cons cell with target and minimum indentation.
  6606. If the current indenation in LINE is smaller than the minimum,
  6607. leave it alone. If it is larger than ind, set it to the target."
  6608. (let* ((l (org-remove-tabs line))
  6609. (i (org-get-indentation l))
  6610. (i1 (car ind)) (i2 (cdr ind)))
  6611. (if (>= i i2) (setq l (substring line i2)))
  6612. (if (> i1 0)
  6613. (concat (make-string i1 ?\ ) l)
  6614. l)))
  6615. (defcustom org-empty-line-terminates-plain-lists nil
  6616. "Non-nil means, an empty line ends all plain list levels.
  6617. When nil, empty lines are part of the preceeding item."
  6618. :group 'org-plain-lists
  6619. :type 'boolean)
  6620. (defun org-beginning-of-item ()
  6621. "Go to the beginning of the current hand-formatted item.
  6622. If the cursor is not in an item, throw an error."
  6623. (interactive)
  6624. (let ((pos (point))
  6625. (limit (save-excursion
  6626. (condition-case nil
  6627. (progn
  6628. (org-back-to-heading)
  6629. (beginning-of-line 2) (point))
  6630. (error (point-min)))))
  6631. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6632. ind ind1)
  6633. (if (org-at-item-p)
  6634. (beginning-of-line 1)
  6635. (beginning-of-line 1)
  6636. (skip-chars-forward " \t")
  6637. (setq ind (current-column))
  6638. (if (catch 'exit
  6639. (while t
  6640. (beginning-of-line 0)
  6641. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6642. (if (looking-at "[ \t]*$")
  6643. (setq ind1 ind-empty)
  6644. (skip-chars-forward " \t")
  6645. (setq ind1 (current-column)))
  6646. (if (< ind1 ind)
  6647. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6648. nil
  6649. (goto-char pos)
  6650. (error "Not in an item")))))
  6651. (defun org-end-of-item ()
  6652. "Go to the end of the current hand-formatted item.
  6653. If the cursor is not in an item, throw an error."
  6654. (interactive)
  6655. (let* ((pos (point))
  6656. ind1
  6657. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6658. (limit (save-excursion (outline-next-heading) (point)))
  6659. (ind (save-excursion
  6660. (org-beginning-of-item)
  6661. (skip-chars-forward " \t")
  6662. (current-column)))
  6663. (end (catch 'exit
  6664. (while t
  6665. (beginning-of-line 2)
  6666. (if (eobp) (throw 'exit (point)))
  6667. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6668. (if (looking-at "[ \t]*$")
  6669. (setq ind1 ind-empty)
  6670. (skip-chars-forward " \t")
  6671. (setq ind1 (current-column)))
  6672. (if (<= ind1 ind)
  6673. (throw 'exit (point-at-bol)))))))
  6674. (if end
  6675. (goto-char end)
  6676. (goto-char pos)
  6677. (error "Not in an item"))))
  6678. (defun org-next-item ()
  6679. "Move to the beginning of the next item in the current plain list.
  6680. Error if not at a plain list, or if this is the last item in the list."
  6681. (interactive)
  6682. (let (ind ind1 (pos (point)))
  6683. (org-beginning-of-item)
  6684. (setq ind (org-get-indentation))
  6685. (org-end-of-item)
  6686. (setq ind1 (org-get-indentation))
  6687. (unless (and (org-at-item-p) (= ind ind1))
  6688. (goto-char pos)
  6689. (error "On last item"))))
  6690. (defun org-previous-item ()
  6691. "Move to the beginning of the previous item in the current plain list.
  6692. Error if not at a plain list, or if this is the first item in the list."
  6693. (interactive)
  6694. (let (beg ind ind1 (pos (point)))
  6695. (org-beginning-of-item)
  6696. (setq beg (point))
  6697. (setq ind (org-get-indentation))
  6698. (goto-char beg)
  6699. (catch 'exit
  6700. (while t
  6701. (beginning-of-line 0)
  6702. (if (looking-at "[ \t]*$")
  6703. nil
  6704. (if (<= (setq ind1 (org-get-indentation)) ind)
  6705. (throw 'exit t)))))
  6706. (condition-case nil
  6707. (if (or (not (org-at-item-p))
  6708. (< ind1 (1- ind)))
  6709. (error "")
  6710. (org-beginning-of-item))
  6711. (error (goto-char pos)
  6712. (error "On first item")))))
  6713. (defun org-first-list-item-p ()
  6714. "Is this heading the item in a plain list?"
  6715. (unless (org-at-item-p)
  6716. (error "Not at a plain list item"))
  6717. (org-beginning-of-item)
  6718. (= (point) (save-excursion (org-beginning-of-item-list))))
  6719. (defun org-move-item-down ()
  6720. "Move the plain list item at point down, i.e. swap with following item.
  6721. Subitems (items with larger indentation) are considered part of the item,
  6722. so this really moves item trees."
  6723. (interactive)
  6724. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6725. (org-beginning-of-item)
  6726. (setq beg0 (point))
  6727. (save-excursion
  6728. (setq ne-beg (org-back-over-empty-lines))
  6729. (setq beg (point)))
  6730. (goto-char beg0)
  6731. (setq ind (org-get-indentation))
  6732. (org-end-of-item)
  6733. (setq end0 (point))
  6734. (setq ind1 (org-get-indentation))
  6735. (setq ne-end (org-back-over-empty-lines))
  6736. (setq end (point))
  6737. (goto-char beg0)
  6738. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6739. ;; include less whitespace
  6740. (save-excursion
  6741. (goto-char beg)
  6742. (forward-line (- ne-beg ne-end))
  6743. (setq beg (point))))
  6744. (goto-char end0)
  6745. (if (and (org-at-item-p) (= ind ind1))
  6746. (progn
  6747. (org-end-of-item)
  6748. (org-back-over-empty-lines)
  6749. (setq txt (buffer-substring beg end))
  6750. (save-excursion
  6751. (delete-region beg end))
  6752. (setq pos (point))
  6753. (insert txt)
  6754. (goto-char pos) (org-skip-whitespace)
  6755. (org-maybe-renumber-ordered-list))
  6756. (goto-char pos)
  6757. (error "Cannot move this item further down"))))
  6758. (defun org-move-item-up (arg)
  6759. "Move the plain list item at point up, i.e. swap with previous item.
  6760. Subitems (items with larger indentation) are considered part of the item,
  6761. so this really moves item trees."
  6762. (interactive "p")
  6763. (let (beg beg0 end ind ind1 (pos (point)) txt
  6764. ne-beg ne-ins ins-end)
  6765. (org-beginning-of-item)
  6766. (setq beg0 (point))
  6767. (setq ind (org-get-indentation))
  6768. (save-excursion
  6769. (setq ne-beg (org-back-over-empty-lines))
  6770. (setq beg (point)))
  6771. (goto-char beg0)
  6772. (org-end-of-item)
  6773. (setq end (point))
  6774. (goto-char beg0)
  6775. (catch 'exit
  6776. (while t
  6777. (beginning-of-line 0)
  6778. (if (looking-at "[ \t]*$")
  6779. (if org-empty-line-terminates-plain-lists
  6780. (progn
  6781. (goto-char pos)
  6782. (error "Cannot move this item further up"))
  6783. nil)
  6784. (if (<= (setq ind1 (org-get-indentation)) ind)
  6785. (throw 'exit t)))))
  6786. (condition-case nil
  6787. (org-beginning-of-item)
  6788. (error (goto-char beg)
  6789. (error "Cannot move this item further up")))
  6790. (setq ind1 (org-get-indentation))
  6791. (if (and (org-at-item-p) (= ind ind1))
  6792. (progn
  6793. (setq ne-ins (org-back-over-empty-lines))
  6794. (setq txt (buffer-substring beg end))
  6795. (save-excursion
  6796. (delete-region beg end))
  6797. (setq pos (point))
  6798. (insert txt)
  6799. (setq ins-end (point))
  6800. (goto-char pos) (org-skip-whitespace)
  6801. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6802. ;; Move whitespace back to beginning
  6803. (save-excursion
  6804. (goto-char ins-end)
  6805. (let ((kill-whole-line t))
  6806. (kill-line (- ne-ins ne-beg)) (point)))
  6807. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6808. (org-maybe-renumber-ordered-list))
  6809. (goto-char pos)
  6810. (error "Cannot move this item further up"))))
  6811. (defun org-maybe-renumber-ordered-list ()
  6812. "Renumber the ordered list at point if setup allows it.
  6813. This tests the user option `org-auto-renumber-ordered-lists' before
  6814. doing the renumbering."
  6815. (interactive)
  6816. (when (and org-auto-renumber-ordered-lists
  6817. (org-at-item-p))
  6818. (if (match-beginning 3)
  6819. (org-renumber-ordered-list 1)
  6820. (org-fix-bullet-type))))
  6821. (defun org-maybe-renumber-ordered-list-safe ()
  6822. (condition-case nil
  6823. (save-excursion
  6824. (org-maybe-renumber-ordered-list))
  6825. (error nil)))
  6826. (defun org-cycle-list-bullet (&optional which)
  6827. "Cycle through the different itemize/enumerate bullets.
  6828. This cycle the entire list level through the sequence:
  6829. `-' -> `+' -> `*' -> `1.' -> `1)'
  6830. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6831. 0 meand `-', 1 means `+' etc."
  6832. (interactive "P")
  6833. (org-preserve-lc
  6834. (org-beginning-of-item-list)
  6835. (org-at-item-p)
  6836. (beginning-of-line 1)
  6837. (let ((current (match-string 0))
  6838. (prevp (eq which 'previous))
  6839. new)
  6840. (setq new (cond
  6841. ((and (numberp which)
  6842. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6843. ((string-match "-" current) (if prevp "1)" "+"))
  6844. ((string-match "\\+" current)
  6845. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6846. ((string-match "\\*" current) (if prevp "+" "1."))
  6847. ((string-match "\\." current) (if prevp "*" "1)"))
  6848. ((string-match ")" current) (if prevp "1." "-"))
  6849. (t (error "This should not happen"))))
  6850. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6851. (org-fix-bullet-type)
  6852. (org-maybe-renumber-ordered-list))))
  6853. (defun org-get-string-indentation (s)
  6854. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6855. (let ((n -1) (i 0) (w tab-width) c)
  6856. (catch 'exit
  6857. (while (< (setq n (1+ n)) (length s))
  6858. (setq c (aref s n))
  6859. (cond ((= c ?\ ) (setq i (1+ i)))
  6860. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6861. (t (throw 'exit t)))))
  6862. i))
  6863. (defun org-renumber-ordered-list (arg)
  6864. "Renumber an ordered plain list.
  6865. Cursor needs to be in the first line of an item, the line that starts
  6866. with something like \"1.\" or \"2)\"."
  6867. (interactive "p")
  6868. (unless (and (org-at-item-p)
  6869. (match-beginning 3))
  6870. (error "This is not an ordered list"))
  6871. (let ((line (org-current-line))
  6872. (col (current-column))
  6873. (ind (org-get-string-indentation
  6874. (buffer-substring (point-at-bol) (match-beginning 3))))
  6875. ;; (term (substring (match-string 3) -1))
  6876. ind1 (n (1- arg))
  6877. fmt)
  6878. ;; find where this list begins
  6879. (org-beginning-of-item-list)
  6880. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6881. (setq fmt (concat "%d" (match-string 1)))
  6882. (beginning-of-line 0)
  6883. ;; walk forward and replace these numbers
  6884. (catch 'exit
  6885. (while t
  6886. (catch 'next
  6887. (beginning-of-line 2)
  6888. (if (eobp) (throw 'exit nil))
  6889. (if (looking-at "[ \t]*$") (throw 'next nil))
  6890. (skip-chars-forward " \t") (setq ind1 (current-column))
  6891. (if (> ind1 ind) (throw 'next t))
  6892. (if (< ind1 ind) (throw 'exit t))
  6893. (if (not (org-at-item-p)) (throw 'exit nil))
  6894. (delete-region (match-beginning 2) (match-end 2))
  6895. (goto-char (match-beginning 2))
  6896. (insert (format fmt (setq n (1+ n)))))))
  6897. (goto-line line)
  6898. (move-to-column col)))
  6899. (defun org-fix-bullet-type ()
  6900. "Make sure all items in this list have the same bullet as the firsst item."
  6901. (interactive)
  6902. (unless (org-at-item-p) (error "This is not a list"))
  6903. (let ((line (org-current-line))
  6904. (col (current-column))
  6905. (ind (current-indentation))
  6906. ind1 bullet)
  6907. ;; find where this list begins
  6908. (org-beginning-of-item-list)
  6909. (beginning-of-line 1)
  6910. ;; find out what the bullet type is
  6911. (looking-at "[ \t]*\\(\\S-+\\)")
  6912. (setq bullet (match-string 1))
  6913. ;; walk forward and replace these numbers
  6914. (beginning-of-line 0)
  6915. (catch 'exit
  6916. (while t
  6917. (catch 'next
  6918. (beginning-of-line 2)
  6919. (if (eobp) (throw 'exit nil))
  6920. (if (looking-at "[ \t]*$") (throw 'next nil))
  6921. (skip-chars-forward " \t") (setq ind1 (current-column))
  6922. (if (> ind1 ind) (throw 'next t))
  6923. (if (< ind1 ind) (throw 'exit t))
  6924. (if (not (org-at-item-p)) (throw 'exit nil))
  6925. (skip-chars-forward " \t")
  6926. (looking-at "\\S-+")
  6927. (replace-match bullet))))
  6928. (goto-line line)
  6929. (move-to-column col)
  6930. (if (string-match "[0-9]" bullet)
  6931. (org-renumber-ordered-list 1))))
  6932. (defun org-beginning-of-item-list ()
  6933. "Go to the beginning of the current item list.
  6934. I.e. to the first item in this list."
  6935. (interactive)
  6936. (org-beginning-of-item)
  6937. (let ((pos (point-at-bol))
  6938. (ind (org-get-indentation))
  6939. ind1)
  6940. ;; find where this list begins
  6941. (catch 'exit
  6942. (while t
  6943. (catch 'next
  6944. (beginning-of-line 0)
  6945. (if (looking-at "[ \t]*$")
  6946. (throw (if (bobp) 'exit 'next) t))
  6947. (skip-chars-forward " \t") (setq ind1 (current-column))
  6948. (if (or (< ind1 ind)
  6949. (and (= ind1 ind)
  6950. (not (org-at-item-p)))
  6951. (bobp))
  6952. (throw 'exit t)
  6953. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6954. (goto-char pos)))
  6955. (defun org-end-of-item-list ()
  6956. "Go to the end of the current item list.
  6957. I.e. to the text after the last item."
  6958. (interactive)
  6959. (org-beginning-of-item)
  6960. (let ((pos (point-at-bol))
  6961. (ind (org-get-indentation))
  6962. ind1)
  6963. ;; find where this list begins
  6964. (catch 'exit
  6965. (while t
  6966. (catch 'next
  6967. (beginning-of-line 2)
  6968. (if (looking-at "[ \t]*$")
  6969. (throw (if (eobp) 'exit 'next) t))
  6970. (skip-chars-forward " \t") (setq ind1 (current-column))
  6971. (if (or (< ind1 ind)
  6972. (and (= ind1 ind)
  6973. (not (org-at-item-p)))
  6974. (eobp))
  6975. (progn
  6976. (setq pos (point-at-bol))
  6977. (throw 'exit t))))))
  6978. (goto-char pos)))
  6979. (defvar org-last-indent-begin-marker (make-marker))
  6980. (defvar org-last-indent-end-marker (make-marker))
  6981. (defun org-outdent-item (arg)
  6982. "Outdent a local list item."
  6983. (interactive "p")
  6984. (org-indent-item (- arg)))
  6985. (defun org-indent-item (arg)
  6986. "Indent a local list item."
  6987. (interactive "p")
  6988. (unless (org-at-item-p)
  6989. (error "Not on an item"))
  6990. (save-excursion
  6991. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6992. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6993. (setq beg org-last-indent-begin-marker
  6994. end org-last-indent-end-marker)
  6995. (org-beginning-of-item)
  6996. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6997. (org-end-of-item)
  6998. (setq end (move-marker org-last-indent-end-marker (point))))
  6999. (goto-char beg)
  7000. (setq tmp (org-item-indent-positions)
  7001. ind (car tmp)
  7002. ind-down (nth 2 tmp)
  7003. ind-up (nth 1 tmp)
  7004. delta (if (> arg 0)
  7005. (if ind-down (- ind-down ind) 2)
  7006. (if ind-up (- ind-up ind) -2)))
  7007. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  7008. (while (< (point) end)
  7009. (beginning-of-line 1)
  7010. (skip-chars-forward " \t") (setq ind1 (current-column))
  7011. (delete-region (point-at-bol) (point))
  7012. (or (eolp) (indent-to-column (+ ind1 delta)))
  7013. (beginning-of-line 2))))
  7014. (org-fix-bullet-type)
  7015. (org-maybe-renumber-ordered-list-safe)
  7016. (save-excursion
  7017. (beginning-of-line 0)
  7018. (condition-case nil (org-beginning-of-item) (error nil))
  7019. (org-maybe-renumber-ordered-list-safe)))
  7020. (defun org-item-indent-positions ()
  7021. "Return indentation for plain list items.
  7022. This returns a list with three values: The current indentation, the
  7023. parent indentation and the indentation a child should habe.
  7024. Assumes cursor in item line."
  7025. (let* ((bolpos (point-at-bol))
  7026. (ind (org-get-indentation))
  7027. ind-down ind-up pos)
  7028. (save-excursion
  7029. (org-beginning-of-item-list)
  7030. (skip-chars-backward "\n\r \t")
  7031. (when (org-in-item-p)
  7032. (org-beginning-of-item)
  7033. (setq ind-up (org-get-indentation))))
  7034. (setq pos (point))
  7035. (save-excursion
  7036. (cond
  7037. ((and (condition-case nil (progn (org-previous-item) t)
  7038. (error nil))
  7039. (or (forward-char 1) t)
  7040. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  7041. (setq ind-down (org-get-indentation)))
  7042. ((and (goto-char pos)
  7043. (org-at-item-p))
  7044. (goto-char (match-end 0))
  7045. (skip-chars-forward " \t")
  7046. (setq ind-down (current-column)))))
  7047. (list ind ind-up ind-down)))
  7048. ;;; The orgstruct minor mode
  7049. ;; Define a minor mode which can be used in other modes in order to
  7050. ;; integrate the org-mode structure editing commands.
  7051. ;; This is really a hack, because the org-mode structure commands use
  7052. ;; keys which normally belong to the major mode. Here is how it
  7053. ;; works: The minor mode defines all the keys necessary to operate the
  7054. ;; structure commands, but wraps the commands into a function which
  7055. ;; tests if the cursor is currently at a headline or a plain list
  7056. ;; item. If that is the case, the structure command is used,
  7057. ;; temporarily setting many Org-mode variables like regular
  7058. ;; expressions for filling etc. However, when any of those keys is
  7059. ;; used at a different location, function uses `key-binding' to look
  7060. ;; up if the key has an associated command in another currently active
  7061. ;; keymap (minor modes, major mode, global), and executes that
  7062. ;; command. There might be problems if any of the keys is otherwise
  7063. ;; used as a prefix key.
  7064. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7065. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7066. ;; addresses this by checking explicitly for both bindings.
  7067. (defvar orgstruct-mode-map (make-sparse-keymap)
  7068. "Keymap for the minor `orgstruct-mode'.")
  7069. (defvar org-local-vars nil
  7070. "List of local variables, for use by `orgstruct-mode'")
  7071. ;;;###autoload
  7072. (define-minor-mode orgstruct-mode
  7073. "Toggle the minor more `orgstruct-mode'.
  7074. This mode is for using Org-mode structure commands in other modes.
  7075. The following key behave as if Org-mode was active, if the cursor
  7076. is on a headline, or on a plain list item (both in the definition
  7077. of Org-mode).
  7078. M-up Move entry/item up
  7079. M-down Move entry/item down
  7080. M-left Promote
  7081. M-right Demote
  7082. M-S-up Move entry/item up
  7083. M-S-down Move entry/item down
  7084. M-S-left Promote subtree
  7085. M-S-right Demote subtree
  7086. M-q Fill paragraph and items like in Org-mode
  7087. C-c ^ Sort entries
  7088. C-c - Cycle list bullet
  7089. TAB Cycle item visibility
  7090. M-RET Insert new heading/item
  7091. S-M-RET Insert new TODO heading / Chekbox item
  7092. C-c C-c Set tags / toggle checkbox"
  7093. nil " OrgStruct" nil
  7094. (org-load-modules-maybe)
  7095. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7096. ;;;###autoload
  7097. (defun turn-on-orgstruct ()
  7098. "Unconditionally turn on `orgstruct-mode'."
  7099. (orgstruct-mode 1))
  7100. ;;;###autoload
  7101. (defun turn-on-orgstruct++ ()
  7102. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7103. In addition to setting orgstruct-mode, this also exports all indentation and
  7104. autofilling variables from org-mode into the buffer. Note that turning
  7105. off orgstruct-mode will *not* remove these additional settings."
  7106. (orgstruct-mode 1)
  7107. (let (var val)
  7108. (mapc
  7109. (lambda (x)
  7110. (when (string-match
  7111. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7112. (symbol-name (car x)))
  7113. (setq var (car x) val (nth 1 x))
  7114. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7115. org-local-vars)))
  7116. (defun orgstruct-error ()
  7117. "Error when there is no default binding for a structure key."
  7118. (interactive)
  7119. (error "This key has no function outside structure elements"))
  7120. (defun orgstruct-setup ()
  7121. "Setup orgstruct keymaps."
  7122. (let ((nfunc 0)
  7123. (bindings
  7124. (list
  7125. '([(meta up)] org-metaup)
  7126. '([(meta down)] org-metadown)
  7127. '([(meta left)] org-metaleft)
  7128. '([(meta right)] org-metaright)
  7129. '([(meta shift up)] org-shiftmetaup)
  7130. '([(meta shift down)] org-shiftmetadown)
  7131. '([(meta shift left)] org-shiftmetaleft)
  7132. '([(meta shift right)] org-shiftmetaright)
  7133. '([(shift up)] org-shiftup)
  7134. '([(shift down)] org-shiftdown)
  7135. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7136. '("\M-q" fill-paragraph)
  7137. '("\C-c^" org-sort)
  7138. '("\C-c-" org-cycle-list-bullet)))
  7139. elt key fun cmd)
  7140. (while (setq elt (pop bindings))
  7141. (setq nfunc (1+ nfunc))
  7142. (setq key (org-key (car elt))
  7143. fun (nth 1 elt)
  7144. cmd (orgstruct-make-binding fun nfunc key))
  7145. (org-defkey orgstruct-mode-map key cmd))
  7146. ;; Special treatment needed for TAB and RET
  7147. (org-defkey orgstruct-mode-map [(tab)]
  7148. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7149. (org-defkey orgstruct-mode-map "\C-i"
  7150. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7151. (org-defkey orgstruct-mode-map "\M-\C-m"
  7152. (orgstruct-make-binding 'org-insert-heading 105
  7153. "\M-\C-m" [(meta return)]))
  7154. (org-defkey orgstruct-mode-map [(meta return)]
  7155. (orgstruct-make-binding 'org-insert-heading 106
  7156. [(meta return)] "\M-\C-m"))
  7157. (org-defkey orgstruct-mode-map [(shift meta return)]
  7158. (orgstruct-make-binding 'org-insert-todo-heading 107
  7159. [(meta return)] "\M-\C-m"))
  7160. (unless org-local-vars
  7161. (setq org-local-vars (org-get-local-variables)))
  7162. t))
  7163. (defun orgstruct-make-binding (fun n &rest keys)
  7164. "Create a function for binding in the structure minor mode.
  7165. FUN is the command to call inside a table. N is used to create a unique
  7166. command name. KEYS are keys that should be checked in for a command
  7167. to execute outside of tables."
  7168. (eval
  7169. (list 'defun
  7170. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7171. '(arg)
  7172. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7173. "Outside of structure, run the binding of `"
  7174. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7175. "'.")
  7176. '(interactive "p")
  7177. (list 'if
  7178. '(org-context-p 'headline 'item)
  7179. (list 'org-run-like-in-org-mode (list 'quote fun))
  7180. (list 'let '(orgstruct-mode)
  7181. (list 'call-interactively
  7182. (append '(or)
  7183. (mapcar (lambda (k)
  7184. (list 'key-binding k))
  7185. keys)
  7186. '('orgstruct-error))))))))
  7187. (defun org-context-p (&rest contexts)
  7188. "Check if local context is and of CONTEXTS.
  7189. Possible values in the list of contexts are `table', `headline', and `item'."
  7190. (let ((pos (point)))
  7191. (goto-char (point-at-bol))
  7192. (prog1 (or (and (memq 'table contexts)
  7193. (looking-at "[ \t]*|"))
  7194. (and (memq 'headline contexts)
  7195. (looking-at "\\*+"))
  7196. (and (memq 'item contexts)
  7197. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7198. (goto-char pos))))
  7199. (defun org-get-local-variables ()
  7200. "Return a list of all local variables in an org-mode buffer."
  7201. (let (varlist)
  7202. (with-current-buffer (get-buffer-create "*Org tmp*")
  7203. (erase-buffer)
  7204. (org-mode)
  7205. (setq varlist (buffer-local-variables)))
  7206. (kill-buffer "*Org tmp*")
  7207. (delq nil
  7208. (mapcar
  7209. (lambda (x)
  7210. (setq x
  7211. (if (symbolp x)
  7212. (list x)
  7213. (list (car x) (list 'quote (cdr x)))))
  7214. (if (string-match
  7215. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7216. (symbol-name (car x)))
  7217. x nil))
  7218. varlist))))
  7219. ;;;###autoload
  7220. (defun org-run-like-in-org-mode (cmd)
  7221. (org-load-modules-maybe)
  7222. (unless org-local-vars
  7223. (setq org-local-vars (org-get-local-variables)))
  7224. (eval (list 'let org-local-vars
  7225. (list 'call-interactively (list 'quote cmd)))))
  7226. ;;;; Archiving
  7227. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7228. (defun org-archive-subtree (&optional find-done)
  7229. "Move the current subtree to the archive.
  7230. The archive can be a certain top-level heading in the current file, or in
  7231. a different file. The tree will be moved to that location, the subtree
  7232. heading be marked DONE, and the current time will be added.
  7233. When called with prefix argument FIND-DONE, find whole trees without any
  7234. open TODO items and archive them (after getting confirmation from the user).
  7235. If the cursor is not at a headline when this comand is called, try all level
  7236. 1 trees. If the cursor is on a headline, only try the direct children of
  7237. this heading."
  7238. (interactive "P")
  7239. (if find-done
  7240. (org-archive-all-done)
  7241. ;; Save all relevant TODO keyword-relatex variables
  7242. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7243. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7244. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7245. (tr-org-done-keywords org-done-keywords)
  7246. (tr-org-todo-regexp org-todo-regexp)
  7247. (tr-org-todo-line-regexp org-todo-line-regexp)
  7248. (tr-org-odd-levels-only org-odd-levels-only)
  7249. (this-buffer (current-buffer))
  7250. (org-archive-location org-archive-location)
  7251. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7252. ;; start of variables that will be used for saving context
  7253. ;; The compiler complains about them - keep them anyway!
  7254. (file (abbreviate-file-name (buffer-file-name)))
  7255. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7256. (time (format-time-string
  7257. (substring (cdr org-time-stamp-formats) 1 -1)
  7258. (current-time)))
  7259. afile heading buffer level newfile-p
  7260. category todo priority
  7261. ;; start of variables that will be used for savind context
  7262. ltags itags prop)
  7263. ;; Try to find a local archive location
  7264. (save-excursion
  7265. (save-restriction
  7266. (widen)
  7267. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7268. (if (and prop (string-match "\\S-" prop))
  7269. (setq org-archive-location prop)
  7270. (if (or (re-search-backward re nil t)
  7271. (re-search-forward re nil t))
  7272. (setq org-archive-location (match-string 1))))))
  7273. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7274. (progn
  7275. (setq afile (format (match-string 1 org-archive-location)
  7276. (file-name-nondirectory buffer-file-name))
  7277. heading (match-string 2 org-archive-location)))
  7278. (error "Invalid `org-archive-location'"))
  7279. (if (> (length afile) 0)
  7280. (setq newfile-p (not (file-exists-p afile))
  7281. buffer (find-file-noselect afile))
  7282. (setq buffer (current-buffer)))
  7283. (unless buffer
  7284. (error "Cannot access file \"%s\"" afile))
  7285. (if (and (> (length heading) 0)
  7286. (string-match "^\\*+" heading))
  7287. (setq level (match-end 0))
  7288. (setq heading nil level 0))
  7289. (save-excursion
  7290. (org-back-to-heading t)
  7291. ;; Get context information that will be lost by moving the tree
  7292. (org-refresh-category-properties)
  7293. (setq category (org-get-category)
  7294. todo (and (looking-at org-todo-line-regexp)
  7295. (match-string 2))
  7296. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7297. ltags (org-get-tags)
  7298. itags (org-delete-all ltags (org-get-tags-at)))
  7299. (setq ltags (mapconcat 'identity ltags " ")
  7300. itags (mapconcat 'identity itags " "))
  7301. ;; We first only copy, in case something goes wrong
  7302. ;; we need to protect this-command, to avoid kill-region sets it,
  7303. ;; which would lead to duplication of subtrees
  7304. (let (this-command) (org-copy-subtree))
  7305. (set-buffer buffer)
  7306. ;; Enforce org-mode for the archive buffer
  7307. (if (not (org-mode-p))
  7308. ;; Force the mode for future visits.
  7309. (let ((org-insert-mode-line-in-empty-file t)
  7310. (org-inhibit-startup t))
  7311. (call-interactively 'org-mode)))
  7312. (when newfile-p
  7313. (goto-char (point-max))
  7314. (insert (format "\nArchived entries from file %s\n\n"
  7315. (buffer-file-name this-buffer))))
  7316. ;; Force the TODO keywords of the original buffer
  7317. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7318. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7319. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7320. (org-done-keywords tr-org-done-keywords)
  7321. (org-todo-regexp tr-org-todo-regexp)
  7322. (org-todo-line-regexp tr-org-todo-line-regexp)
  7323. (org-odd-levels-only
  7324. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7325. org-odd-levels-only
  7326. tr-org-odd-levels-only)))
  7327. (goto-char (point-min))
  7328. (show-all)
  7329. (if heading
  7330. (progn
  7331. (if (re-search-forward
  7332. (concat "^" (regexp-quote heading)
  7333. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7334. nil t)
  7335. (goto-char (match-end 0))
  7336. ;; Heading not found, just insert it at the end
  7337. (goto-char (point-max))
  7338. (or (bolp) (insert "\n"))
  7339. (insert "\n" heading "\n")
  7340. (end-of-line 0))
  7341. ;; Make the subtree visible
  7342. (show-subtree)
  7343. (org-end-of-subtree t)
  7344. (skip-chars-backward " \t\r\n")
  7345. (and (looking-at "[ \t\r\n]*")
  7346. (replace-match "\n\n")))
  7347. ;; No specific heading, just go to end of file.
  7348. (goto-char (point-max)) (insert "\n"))
  7349. ;; Paste
  7350. (org-paste-subtree (org-get-valid-level level 1))
  7351. ;; Mark the entry as done
  7352. (when (and org-archive-mark-done
  7353. (looking-at org-todo-line-regexp)
  7354. (or (not (match-end 2))
  7355. (not (member (match-string 2) org-done-keywords))))
  7356. (let (org-log-done org-todo-log-states)
  7357. (org-todo
  7358. (car (or (member org-archive-mark-done org-done-keywords)
  7359. org-done-keywords)))))
  7360. ;; Add the context info
  7361. (when org-archive-save-context-info
  7362. (let ((l org-archive-save-context-info) e n v)
  7363. (while (setq e (pop l))
  7364. (when (and (setq v (symbol-value e))
  7365. (stringp v) (string-match "\\S-" v))
  7366. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7367. (org-entry-put (point) n v)))))
  7368. ;; Save and kill the buffer, if it is not the same buffer.
  7369. (if (not (eq this-buffer buffer))
  7370. (progn (save-buffer) (kill-buffer buffer)))))
  7371. ;; Here we are back in the original buffer. Everything seems to have
  7372. ;; worked. So now cut the tree and finish up.
  7373. (let (this-command) (org-cut-subtree))
  7374. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7375. (message "Subtree archived %s"
  7376. (if (eq this-buffer buffer)
  7377. (concat "under heading: " heading)
  7378. (concat "in file: " (abbreviate-file-name afile)))))))
  7379. (defun org-refresh-category-properties ()
  7380. "Refresh category text properties in teh buffer."
  7381. (let ((def-cat (cond
  7382. ((null org-category)
  7383. (if buffer-file-name
  7384. (file-name-sans-extension
  7385. (file-name-nondirectory buffer-file-name))
  7386. "???"))
  7387. ((symbolp org-category) (symbol-name org-category))
  7388. (t org-category)))
  7389. beg end cat pos optionp)
  7390. (org-unmodified
  7391. (save-excursion
  7392. (save-restriction
  7393. (widen)
  7394. (goto-char (point-min))
  7395. (put-text-property (point) (point-max) 'org-category def-cat)
  7396. (while (re-search-forward
  7397. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7398. (setq pos (match-end 0)
  7399. optionp (equal (char-after (match-beginning 0)) ?#)
  7400. cat (org-trim (match-string 2)))
  7401. (if optionp
  7402. (setq beg (point-at-bol) end (point-max))
  7403. (org-back-to-heading t)
  7404. (setq beg (point) end (org-end-of-subtree t t)))
  7405. (put-text-property beg end 'org-category cat)
  7406. (goto-char pos)))))))
  7407. (defun org-archive-all-done (&optional tag)
  7408. "Archive sublevels of the current tree without open TODO items.
  7409. If the cursor is not on a headline, try all level 1 trees. If
  7410. it is on a headline, try all direct children.
  7411. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7412. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7413. (rea (concat ".*:" org-archive-tag ":"))
  7414. (begm (make-marker))
  7415. (endm (make-marker))
  7416. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7417. "Move subtree to archive (no open TODO items)? "))
  7418. beg end (cntarch 0))
  7419. (if (org-on-heading-p)
  7420. (progn
  7421. (setq re1 (concat "^" (regexp-quote
  7422. (make-string
  7423. (1+ (- (match-end 0) (match-beginning 0) 1))
  7424. ?*))
  7425. " "))
  7426. (move-marker begm (point))
  7427. (move-marker endm (org-end-of-subtree t)))
  7428. (setq re1 "^* ")
  7429. (move-marker begm (point-min))
  7430. (move-marker endm (point-max)))
  7431. (save-excursion
  7432. (goto-char begm)
  7433. (while (re-search-forward re1 endm t)
  7434. (setq beg (match-beginning 0)
  7435. end (save-excursion (org-end-of-subtree t) (point)))
  7436. (goto-char beg)
  7437. (if (re-search-forward re end t)
  7438. (goto-char end)
  7439. (goto-char beg)
  7440. (if (and (or (not tag) (not (looking-at rea)))
  7441. (y-or-n-p question))
  7442. (progn
  7443. (if tag
  7444. (org-toggle-tag org-archive-tag 'on)
  7445. (org-archive-subtree))
  7446. (setq cntarch (1+ cntarch)))
  7447. (goto-char end)))))
  7448. (message "%d trees archived" cntarch)))
  7449. (defun org-cycle-hide-drawers (state)
  7450. "Re-hide all drawers after a visibility state change."
  7451. (when (and (org-mode-p)
  7452. (not (memq state '(overview folded))))
  7453. (save-excursion
  7454. (let* ((globalp (memq state '(contents all)))
  7455. (beg (if globalp (point-min) (point)))
  7456. (end (if globalp (point-max) (org-end-of-subtree t))))
  7457. (goto-char beg)
  7458. (while (re-search-forward org-drawer-regexp end t)
  7459. (org-flag-drawer t))))))
  7460. (defun org-flag-drawer (flag)
  7461. (save-excursion
  7462. (beginning-of-line 1)
  7463. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7464. (let ((b (match-end 0))
  7465. (outline-regexp org-outline-regexp))
  7466. (if (re-search-forward
  7467. "^[ \t]*:END:"
  7468. (save-excursion (outline-next-heading) (point)) t)
  7469. (outline-flag-region b (point-at-eol) flag)
  7470. (error ":END: line missing"))))))
  7471. (defun org-cycle-hide-archived-subtrees (state)
  7472. "Re-hide all archived subtrees after a visibility state change."
  7473. (when (and (not org-cycle-open-archived-trees)
  7474. (not (memq state '(overview folded))))
  7475. (save-excursion
  7476. (let* ((globalp (memq state '(contents all)))
  7477. (beg (if globalp (point-min) (point)))
  7478. (end (if globalp (point-max) (org-end-of-subtree t))))
  7479. (org-hide-archived-subtrees beg end)
  7480. (goto-char beg)
  7481. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7482. (message "%s" (substitute-command-keys
  7483. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7484. (defun org-force-cycle-archived ()
  7485. "Cycle subtree even if it is archived."
  7486. (interactive)
  7487. (setq this-command 'org-cycle)
  7488. (let ((org-cycle-open-archived-trees t))
  7489. (call-interactively 'org-cycle)))
  7490. (defun org-hide-archived-subtrees (beg end)
  7491. "Re-hide all archived subtrees after a visibility state change."
  7492. (save-excursion
  7493. (let* ((re (concat ":" org-archive-tag ":")))
  7494. (goto-char beg)
  7495. (while (re-search-forward re end t)
  7496. (and (org-on-heading-p) (hide-subtree))
  7497. (org-end-of-subtree t)))))
  7498. (defun org-toggle-tag (tag &optional onoff)
  7499. "Toggle the tag TAG for the current line.
  7500. If ONOFF is `on' or `off', don't toggle but set to this state."
  7501. (unless (org-on-heading-p t) (error "Not on headling"))
  7502. (let (res current)
  7503. (save-excursion
  7504. (beginning-of-line)
  7505. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7506. (point-at-eol) t)
  7507. (progn
  7508. (setq current (match-string 1))
  7509. (replace-match ""))
  7510. (setq current ""))
  7511. (setq current (nreverse (org-split-string current ":")))
  7512. (cond
  7513. ((eq onoff 'on)
  7514. (setq res t)
  7515. (or (member tag current) (push tag current)))
  7516. ((eq onoff 'off)
  7517. (or (not (member tag current)) (setq current (delete tag current))))
  7518. (t (if (member tag current)
  7519. (setq current (delete tag current))
  7520. (setq res t)
  7521. (push tag current))))
  7522. (end-of-line 1)
  7523. (if current
  7524. (progn
  7525. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7526. (org-set-tags nil t))
  7527. (delete-horizontal-space))
  7528. (run-hooks 'org-after-tags-change-hook))
  7529. res))
  7530. (defun org-toggle-archive-tag (&optional arg)
  7531. "Toggle the archive tag for the current headline.
  7532. With prefix ARG, check all children of current headline and offer tagging
  7533. the children that do not contain any open TODO items."
  7534. (interactive "P")
  7535. (if arg
  7536. (org-archive-all-done 'tag)
  7537. (let (set)
  7538. (save-excursion
  7539. (org-back-to-heading t)
  7540. (setq set (org-toggle-tag org-archive-tag))
  7541. (when set (hide-subtree)))
  7542. (and set (beginning-of-line 1))
  7543. (message "Subtree %s" (if set "archived" "unarchived")))))
  7544. ;;;; Tables
  7545. ;;; The table editor
  7546. ;; Watch out: Here we are talking about two different kind of tables.
  7547. ;; Most of the code is for the tables created with the Org-mode table editor.
  7548. ;; Sometimes, we talk about tables created and edited with the table.el
  7549. ;; Emacs package. We call the former org-type tables, and the latter
  7550. ;; table.el-type tables.
  7551. (defun org-before-change-function (beg end)
  7552. "Every change indicates that a table might need an update."
  7553. (setq org-table-may-need-update t))
  7554. (defconst org-table-line-regexp "^[ \t]*|"
  7555. "Detects an org-type table line.")
  7556. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7557. "Detects an org-type table line.")
  7558. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7559. "Detects a table line marked for automatic recalculation.")
  7560. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7561. "Detects a table line marked for automatic recalculation.")
  7562. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7563. "Detects a table line marked for automatic recalculation.")
  7564. (defconst org-table-hline-regexp "^[ \t]*|-"
  7565. "Detects an org-type table hline.")
  7566. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7567. "Detects a table-type table hline.")
  7568. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7569. "Detects an org-type or table-type table.")
  7570. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7571. "Searching from within a table (any type) this finds the first line
  7572. outside the table.")
  7573. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7574. "Searching from within a table (any type) this finds the first line
  7575. outside the table.")
  7576. (defvar org-table-last-highlighted-reference nil)
  7577. (defvar org-table-formula-history nil)
  7578. (defvar org-table-column-names nil
  7579. "Alist with column names, derived from the `!' line.")
  7580. (defvar org-table-column-name-regexp nil
  7581. "Regular expression matching the current column names.")
  7582. (defvar org-table-local-parameters nil
  7583. "Alist with parameter names, derived from the `$' line.")
  7584. (defvar org-table-named-field-locations nil
  7585. "Alist with locations of named fields.")
  7586. (defvar org-table-current-line-types nil
  7587. "Table row types, non-nil only for the duration of a comand.")
  7588. (defvar org-table-current-begin-line nil
  7589. "Table begin line, non-nil only for the duration of a comand.")
  7590. (defvar org-table-current-begin-pos nil
  7591. "Table begin position, non-nil only for the duration of a comand.")
  7592. (defvar org-table-dlines nil
  7593. "Vector of data line line numbers in the current table.")
  7594. (defvar org-table-hlines nil
  7595. "Vector of hline line numbers in the current table.")
  7596. (defconst org-table-range-regexp
  7597. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7598. ;; 1 2 3 4 5
  7599. "Regular expression for matching ranges in formulas.")
  7600. (defconst org-table-range-regexp2
  7601. (concat
  7602. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7603. "\\.\\."
  7604. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7605. "Match a range for reference display.")
  7606. (defconst org-table-translate-regexp
  7607. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7608. "Match a reference that needs translation, for reference display.")
  7609. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7610. (defun org-table-create-with-table.el ()
  7611. "Use the table.el package to insert a new table.
  7612. If there is already a table at point, convert between Org-mode tables
  7613. and table.el tables."
  7614. (interactive)
  7615. (require 'table)
  7616. (cond
  7617. ((org-at-table.el-p)
  7618. (if (y-or-n-p "Convert table to Org-mode table? ")
  7619. (org-table-convert)))
  7620. ((org-at-table-p)
  7621. (if (y-or-n-p "Convert table to table.el table? ")
  7622. (org-table-convert)))
  7623. (t (call-interactively 'table-insert))))
  7624. (defun org-table-create-or-convert-from-region (arg)
  7625. "Convert region to table, or create an empty table.
  7626. If there is an active region, convert it to a table, using the function
  7627. `org-table-convert-region'. See the documentation of that function
  7628. to learn how the prefix argument is interpreted to determine the field
  7629. separator.
  7630. If there is no such region, create an empty table with `org-table-create'."
  7631. (interactive "P")
  7632. (if (org-region-active-p)
  7633. (org-table-convert-region (region-beginning) (region-end) arg)
  7634. (org-table-create arg)))
  7635. (defun org-table-create (&optional size)
  7636. "Query for a size and insert a table skeleton.
  7637. SIZE is a string Columns x Rows like for example \"3x2\"."
  7638. (interactive "P")
  7639. (unless size
  7640. (setq size (read-string
  7641. (concat "Table size Columns x Rows [e.g. "
  7642. org-table-default-size "]: ")
  7643. "" nil org-table-default-size)))
  7644. (let* ((pos (point))
  7645. (indent (make-string (current-column) ?\ ))
  7646. (split (org-split-string size " *x *"))
  7647. (rows (string-to-number (nth 1 split)))
  7648. (columns (string-to-number (car split)))
  7649. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7650. "\n")))
  7651. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7652. (point-at-bol) (point)))
  7653. (beginning-of-line 1)
  7654. (newline))
  7655. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7656. (dotimes (i rows) (insert line))
  7657. (goto-char pos)
  7658. (if (> rows 1)
  7659. ;; Insert a hline after the first row.
  7660. (progn
  7661. (end-of-line 1)
  7662. (insert "\n|-")
  7663. (goto-char pos)))
  7664. (org-table-align)))
  7665. (defun org-table-convert-region (beg0 end0 &optional separator)
  7666. "Convert region to a table.
  7667. The region goes from BEG0 to END0, but these borders will be moved
  7668. slightly, to make sure a beginning of line in the first line is included.
  7669. SEPARATOR specifies the field separator in the lines. It can have the
  7670. following values:
  7671. '(4) Use the comma as a field separator
  7672. '(16) Use a TAB as field separator
  7673. integer When a number, use that many spaces as field separator
  7674. nil When nil, the command tries to be smart and figure out the
  7675. separator in the following way:
  7676. - when each line contains a TAB, assume TAB-separated material
  7677. - when each line contains a comme, assume CSV material
  7678. - else, assume one or more SPACE charcters as separator."
  7679. (interactive "rP")
  7680. (let* ((beg (min beg0 end0))
  7681. (end (max beg0 end0))
  7682. re)
  7683. (goto-char beg)
  7684. (beginning-of-line 1)
  7685. (setq beg (move-marker (make-marker) (point)))
  7686. (goto-char end)
  7687. (if (bolp) (backward-char 1) (end-of-line 1))
  7688. (setq end (move-marker (make-marker) (point)))
  7689. ;; Get the right field separator
  7690. (unless separator
  7691. (goto-char beg)
  7692. (setq separator
  7693. (cond
  7694. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7695. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7696. (t 1))))
  7697. (setq re (cond
  7698. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7699. ((equal separator '(16)) "^\\|\t")
  7700. ((integerp separator)
  7701. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7702. (t (error "This should not happen"))))
  7703. (goto-char beg)
  7704. (while (re-search-forward re end t)
  7705. (replace-match "| " t t))
  7706. (goto-char beg)
  7707. (insert " ")
  7708. (org-table-align)))
  7709. (defun org-table-import (file arg)
  7710. "Import FILE as a table.
  7711. The file is assumed to be tab-separated. Such files can be produced by most
  7712. spreadsheet and database applications. If no tabs (at least one per line)
  7713. are found, lines will be split on whitespace into fields."
  7714. (interactive "f\nP")
  7715. (or (bolp) (newline))
  7716. (let ((beg (point))
  7717. (pm (point-max)))
  7718. (insert-file-contents file)
  7719. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7720. (defun org-table-export ()
  7721. "Export table as a tab-separated file.
  7722. Such a file can be imported into a spreadsheet program like Excel."
  7723. (interactive)
  7724. (let* ((beg (org-table-begin))
  7725. (end (org-table-end))
  7726. (table (buffer-substring beg end))
  7727. (file (read-file-name "Export table to: "))
  7728. buf)
  7729. (unless (or (not (file-exists-p file))
  7730. (y-or-n-p (format "Overwrite file %s? " file)))
  7731. (error "Abort"))
  7732. (with-current-buffer (find-file-noselect file)
  7733. (setq buf (current-buffer))
  7734. (erase-buffer)
  7735. (fundamental-mode)
  7736. (insert table)
  7737. (goto-char (point-min))
  7738. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7739. (replace-match "" t t)
  7740. (end-of-line 1))
  7741. (goto-char (point-min))
  7742. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7743. (replace-match "" t t)
  7744. (goto-char (min (1+ (point)) (point-max))))
  7745. (goto-char (point-min))
  7746. (while (re-search-forward "^-[-+]*$" nil t)
  7747. (replace-match "")
  7748. (if (looking-at "\n")
  7749. (delete-char 1)))
  7750. (goto-char (point-min))
  7751. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7752. (replace-match "\t" t t))
  7753. (save-buffer))
  7754. (kill-buffer buf)))
  7755. (defvar org-table-aligned-begin-marker (make-marker)
  7756. "Marker at the beginning of the table last aligned.
  7757. Used to check if cursor still is in that table, to minimize realignment.")
  7758. (defvar org-table-aligned-end-marker (make-marker)
  7759. "Marker at the end of the table last aligned.
  7760. Used to check if cursor still is in that table, to minimize realignment.")
  7761. (defvar org-table-last-alignment nil
  7762. "List of flags for flushright alignment, from the last re-alignment.
  7763. This is being used to correctly align a single field after TAB or RET.")
  7764. (defvar org-table-last-column-widths nil
  7765. "List of max width of fields in each column.
  7766. This is being used to correctly align a single field after TAB or RET.")
  7767. (defvar org-table-overlay-coordinates nil
  7768. "Overlay coordinates after each align of a table.")
  7769. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7770. (defvar org-last-recalc-line nil)
  7771. (defconst org-narrow-column-arrow "=>"
  7772. "Used as display property in narrowed table columns.")
  7773. (defun org-table-align ()
  7774. "Align the table at point by aligning all vertical bars."
  7775. (interactive)
  7776. (let* (
  7777. ;; Limits of table
  7778. (beg (org-table-begin))
  7779. (end (org-table-end))
  7780. ;; Current cursor position
  7781. (linepos (org-current-line))
  7782. (colpos (org-table-current-column))
  7783. (winstart (window-start))
  7784. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7785. lines (new "") lengths l typenums ty fields maxfields i
  7786. column
  7787. (indent "") cnt frac
  7788. rfmt hfmt
  7789. (spaces '(1 . 1))
  7790. (sp1 (car spaces))
  7791. (sp2 (cdr spaces))
  7792. (rfmt1 (concat
  7793. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7794. (hfmt1 (concat
  7795. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7796. emptystrings links dates emph narrow fmax f1 len c e)
  7797. (untabify beg end)
  7798. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7799. ;; Check if we have links or dates
  7800. (goto-char beg)
  7801. (setq links (re-search-forward org-bracket-link-regexp end t))
  7802. (goto-char beg)
  7803. (setq emph (and org-hide-emphasis-markers
  7804. (re-search-forward org-emph-re end t)))
  7805. (goto-char beg)
  7806. (setq dates (and org-display-custom-times
  7807. (re-search-forward org-ts-regexp-both end t)))
  7808. ;; Make sure the link properties are right
  7809. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7810. ;; Make sure the date properties are right
  7811. (when dates (goto-char beg) (while (org-activate-dates end)))
  7812. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7813. ;; Check if we are narrowing any columns
  7814. (goto-char beg)
  7815. (setq narrow (and org-format-transports-properties-p
  7816. (re-search-forward "<[0-9]+>" end t)))
  7817. ;; Get the rows
  7818. (setq lines (org-split-string
  7819. (buffer-substring beg end) "\n"))
  7820. ;; Store the indentation of the first line
  7821. (if (string-match "^ *" (car lines))
  7822. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7823. ;; Mark the hlines by setting the corresponding element to nil
  7824. ;; At the same time, we remove trailing space.
  7825. (setq lines (mapcar (lambda (l)
  7826. (if (string-match "^ *|-" l)
  7827. nil
  7828. (if (string-match "[ \t]+$" l)
  7829. (substring l 0 (match-beginning 0))
  7830. l)))
  7831. lines))
  7832. ;; Get the data fields by splitting the lines.
  7833. (setq fields (mapcar
  7834. (lambda (l)
  7835. (org-split-string l " *| *"))
  7836. (delq nil (copy-sequence lines))))
  7837. ;; How many fields in the longest line?
  7838. (condition-case nil
  7839. (setq maxfields (apply 'max (mapcar 'length fields)))
  7840. (error
  7841. (kill-region beg end)
  7842. (org-table-create org-table-default-size)
  7843. (error "Empty table - created default table")))
  7844. ;; A list of empty strings to fill any short rows on output
  7845. (setq emptystrings (make-list maxfields ""))
  7846. ;; Check for special formatting.
  7847. (setq i -1)
  7848. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7849. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7850. ;; Check if there is an explicit width specified
  7851. (when narrow
  7852. (setq c column fmax nil)
  7853. (while c
  7854. (setq e (pop c))
  7855. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7856. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7857. ;; Find fields that are wider than fmax, and shorten them
  7858. (when fmax
  7859. (loop for xx in column do
  7860. (when (and (stringp xx)
  7861. (> (org-string-width xx) fmax))
  7862. (org-add-props xx nil
  7863. 'help-echo
  7864. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7865. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7866. (unless (> f1 1)
  7867. (error "Cannot narrow field starting with wide link \"%s\""
  7868. (match-string 0 xx)))
  7869. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7870. (add-text-properties (- f1 2) f1
  7871. (list 'display org-narrow-column-arrow)
  7872. xx)))))
  7873. ;; Get the maximum width for each column
  7874. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7875. ;; Get the fraction of numbers, to decide about alignment of the column
  7876. (setq cnt 0 frac 0.0)
  7877. (loop for x in column do
  7878. (if (equal x "")
  7879. nil
  7880. (setq frac ( / (+ (* frac cnt)
  7881. (if (string-match org-table-number-regexp x) 1 0))
  7882. (setq cnt (1+ cnt))))))
  7883. (push (>= frac org-table-number-fraction) typenums))
  7884. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7885. ;; Store the alignment of this table, for later editing of single fields
  7886. (setq org-table-last-alignment typenums
  7887. org-table-last-column-widths lengths)
  7888. ;; With invisible characters, `format' does not get the field width right
  7889. ;; So we need to make these fields wide by hand.
  7890. (when (or links emph)
  7891. (loop for i from 0 upto (1- maxfields) do
  7892. (setq len (nth i lengths))
  7893. (loop for j from 0 upto (1- (length fields)) do
  7894. (setq c (nthcdr i (car (nthcdr j fields))))
  7895. (if (and (stringp (car c))
  7896. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7897. ; (string-match org-bracket-link-regexp (car c))
  7898. (< (org-string-width (car c)) len))
  7899. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7900. ;; Compute the formats needed for output of the table
  7901. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7902. (while (setq l (pop lengths))
  7903. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7904. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7905. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7906. (setq rfmt (concat rfmt "\n")
  7907. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7908. (setq new (mapconcat
  7909. (lambda (l)
  7910. (if l (apply 'format rfmt
  7911. (append (pop fields) emptystrings))
  7912. hfmt))
  7913. lines ""))
  7914. ;; Replace the old one
  7915. (delete-region beg end)
  7916. (move-marker end nil)
  7917. (move-marker org-table-aligned-begin-marker (point))
  7918. (insert new)
  7919. (move-marker org-table-aligned-end-marker (point))
  7920. (when (and orgtbl-mode (not (org-mode-p)))
  7921. (goto-char org-table-aligned-begin-marker)
  7922. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7923. ;; Try to move to the old location
  7924. (goto-line winstartline)
  7925. (setq winstart (point-at-bol))
  7926. (goto-line linepos)
  7927. (set-window-start (selected-window) winstart 'noforce)
  7928. (org-table-goto-column colpos)
  7929. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7930. (setq org-table-may-need-update nil)
  7931. ))
  7932. (defun org-string-width (s)
  7933. "Compute width of string, ignoring invisible characters.
  7934. This ignores character with invisibility property `org-link', and also
  7935. characters with property `org-cwidth', because these will become invisible
  7936. upon the next fontification round."
  7937. (let (b l)
  7938. (when (or (eq t buffer-invisibility-spec)
  7939. (assq 'org-link buffer-invisibility-spec))
  7940. (while (setq b (text-property-any 0 (length s)
  7941. 'invisible 'org-link s))
  7942. (setq s (concat (substring s 0 b)
  7943. (substring s (or (next-single-property-change
  7944. b 'invisible s) (length s)))))))
  7945. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7946. (setq s (concat (substring s 0 b)
  7947. (substring s (or (next-single-property-change
  7948. b 'org-cwidth s) (length s))))))
  7949. (setq l (string-width s) b -1)
  7950. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7951. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7952. l))
  7953. (defun org-table-begin (&optional table-type)
  7954. "Find the beginning of the table and return its position.
  7955. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7956. (save-excursion
  7957. (if (not (re-search-backward
  7958. (if table-type org-table-any-border-regexp
  7959. org-table-border-regexp)
  7960. nil t))
  7961. (progn (goto-char (point-min)) (point))
  7962. (goto-char (match-beginning 0))
  7963. (beginning-of-line 2)
  7964. (point))))
  7965. (defun org-table-end (&optional table-type)
  7966. "Find the end of the table and return its position.
  7967. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7968. (save-excursion
  7969. (if (not (re-search-forward
  7970. (if table-type org-table-any-border-regexp
  7971. org-table-border-regexp)
  7972. nil t))
  7973. (goto-char (point-max))
  7974. (goto-char (match-beginning 0)))
  7975. (point-marker)))
  7976. (defun org-table-justify-field-maybe (&optional new)
  7977. "Justify the current field, text to left, number to right.
  7978. Optional argument NEW may specify text to replace the current field content."
  7979. (cond
  7980. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7981. ((org-at-table-hline-p))
  7982. ((and (not new)
  7983. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7984. (current-buffer)))
  7985. (< (point) org-table-aligned-begin-marker)
  7986. (>= (point) org-table-aligned-end-marker)))
  7987. ;; This is not the same table, force a full re-align
  7988. (setq org-table-may-need-update t))
  7989. (t ;; realign the current field, based on previous full realign
  7990. (let* ((pos (point)) s
  7991. (col (org-table-current-column))
  7992. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7993. l f n o e)
  7994. (when (> col 0)
  7995. (skip-chars-backward "^|\n")
  7996. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7997. (progn
  7998. (setq s (match-string 1)
  7999. o (match-string 0)
  8000. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  8001. e (not (= (match-beginning 2) (match-end 2))))
  8002. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  8003. l (if e "|" (setq org-table-may-need-update t) ""))
  8004. n (format f s))
  8005. (if new
  8006. (if (<= (length new) l) ;; FIXME: length -> str-width?
  8007. (setq n (format f new))
  8008. (setq n (concat new "|") org-table-may-need-update t)))
  8009. (or (equal n o)
  8010. (let (org-table-may-need-update)
  8011. (replace-match n t t))))
  8012. (setq org-table-may-need-update t))
  8013. (goto-char pos))))))
  8014. (defun org-table-next-field ()
  8015. "Go to the next field in the current table, creating new lines as needed.
  8016. Before doing so, re-align the table if necessary."
  8017. (interactive)
  8018. (org-table-maybe-eval-formula)
  8019. (org-table-maybe-recalculate-line)
  8020. (if (and org-table-automatic-realign
  8021. org-table-may-need-update)
  8022. (org-table-align))
  8023. (let ((end (org-table-end)))
  8024. (if (org-at-table-hline-p)
  8025. (end-of-line 1))
  8026. (condition-case nil
  8027. (progn
  8028. (re-search-forward "|" end)
  8029. (if (looking-at "[ \t]*$")
  8030. (re-search-forward "|" end))
  8031. (if (and (looking-at "-")
  8032. org-table-tab-jumps-over-hlines
  8033. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  8034. (goto-char (match-beginning 1)))
  8035. (if (looking-at "-")
  8036. (progn
  8037. (beginning-of-line 0)
  8038. (org-table-insert-row 'below))
  8039. (if (looking-at " ") (forward-char 1))))
  8040. (error
  8041. (org-table-insert-row 'below)))))
  8042. (defun org-table-previous-field ()
  8043. "Go to the previous field in the table.
  8044. Before doing so, re-align the table if necessary."
  8045. (interactive)
  8046. (org-table-justify-field-maybe)
  8047. (org-table-maybe-recalculate-line)
  8048. (if (and org-table-automatic-realign
  8049. org-table-may-need-update)
  8050. (org-table-align))
  8051. (if (org-at-table-hline-p)
  8052. (end-of-line 1))
  8053. (re-search-backward "|" (org-table-begin))
  8054. (re-search-backward "|" (org-table-begin))
  8055. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  8056. (re-search-backward "|" (org-table-begin)))
  8057. (if (looking-at "| ?")
  8058. (goto-char (match-end 0))))
  8059. (defun org-table-next-row ()
  8060. "Go to the next row (same column) in the current table.
  8061. Before doing so, re-align the table if necessary."
  8062. (interactive)
  8063. (org-table-maybe-eval-formula)
  8064. (org-table-maybe-recalculate-line)
  8065. (if (or (looking-at "[ \t]*$")
  8066. (save-excursion (skip-chars-backward " \t") (bolp)))
  8067. (newline)
  8068. (if (and org-table-automatic-realign
  8069. org-table-may-need-update)
  8070. (org-table-align))
  8071. (let ((col (org-table-current-column)))
  8072. (beginning-of-line 2)
  8073. (if (or (not (org-at-table-p))
  8074. (org-at-table-hline-p))
  8075. (progn
  8076. (beginning-of-line 0)
  8077. (org-table-insert-row 'below)))
  8078. (org-table-goto-column col)
  8079. (skip-chars-backward "^|\n\r")
  8080. (if (looking-at " ") (forward-char 1)))))
  8081. (defun org-table-copy-down (n)
  8082. "Copy a field down in the current column.
  8083. If the field at the cursor is empty, copy into it the content of the nearest
  8084. non-empty field above. With argument N, use the Nth non-empty field.
  8085. If the current field is not empty, it is copied down to the next row, and
  8086. the cursor is moved with it. Therefore, repeating this command causes the
  8087. column to be filled row-by-row.
  8088. If the variable `org-table-copy-increment' is non-nil and the field is an
  8089. integer or a timestamp, it will be incremented while copying. In the case of
  8090. a timestamp, if the cursor is on the year, change the year. If it is on the
  8091. month or the day, change that. Point will stay on the current date field
  8092. in order to easily repeat the interval."
  8093. (interactive "p")
  8094. (let* ((colpos (org-table-current-column))
  8095. (col (current-column))
  8096. (field (org-table-get-field))
  8097. (non-empty (string-match "[^ \t]" field))
  8098. (beg (org-table-begin))
  8099. txt)
  8100. (org-table-check-inside-data-field)
  8101. (if non-empty
  8102. (progn
  8103. (setq txt (org-trim field))
  8104. (org-table-next-row)
  8105. (org-table-blank-field))
  8106. (save-excursion
  8107. (setq txt
  8108. (catch 'exit
  8109. (while (progn (beginning-of-line 1)
  8110. (re-search-backward org-table-dataline-regexp
  8111. beg t))
  8112. (org-table-goto-column colpos t)
  8113. (if (and (looking-at
  8114. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8115. (= (setq n (1- n)) 0))
  8116. (throw 'exit (match-string 1))))))))
  8117. (if txt
  8118. (progn
  8119. (if (and org-table-copy-increment
  8120. (string-match "^[0-9]+$" txt))
  8121. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8122. (insert txt)
  8123. (move-to-column col)
  8124. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8125. (org-timestamp-up 1)
  8126. (org-table-maybe-recalculate-line))
  8127. (org-table-align)
  8128. (move-to-column col))
  8129. (error "No non-empty field found"))))
  8130. (defun org-table-check-inside-data-field ()
  8131. "Is point inside a table data field?
  8132. I.e. not on a hline or before the first or after the last column?
  8133. This actually throws an error, so it aborts the current command."
  8134. (if (or (not (org-at-table-p))
  8135. (= (org-table-current-column) 0)
  8136. (org-at-table-hline-p)
  8137. (looking-at "[ \t]*$"))
  8138. (error "Not in table data field")))
  8139. (defvar org-table-clip nil
  8140. "Clipboard for table regions.")
  8141. (defun org-table-blank-field ()
  8142. "Blank the current table field or active region."
  8143. (interactive)
  8144. (org-table-check-inside-data-field)
  8145. (if (and (interactive-p) (org-region-active-p))
  8146. (let (org-table-clip)
  8147. (org-table-cut-region (region-beginning) (region-end)))
  8148. (skip-chars-backward "^|")
  8149. (backward-char 1)
  8150. (if (looking-at "|[^|\n]+")
  8151. (let* ((pos (match-beginning 0))
  8152. (match (match-string 0))
  8153. (len (org-string-width match)))
  8154. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8155. (goto-char (+ 2 pos))
  8156. (substring match 1)))))
  8157. (defun org-table-get-field (&optional n replace)
  8158. "Return the value of the field in column N of current row.
  8159. N defaults to current field.
  8160. If REPLACE is a string, replace field with this value. The return value
  8161. is always the old value."
  8162. (and n (org-table-goto-column n))
  8163. (skip-chars-backward "^|\n")
  8164. (backward-char 1)
  8165. (if (looking-at "|[^|\r\n]*")
  8166. (let* ((pos (match-beginning 0))
  8167. (val (buffer-substring (1+ pos) (match-end 0))))
  8168. (if replace
  8169. (replace-match (concat "|" replace) t t))
  8170. (goto-char (min (point-at-eol) (+ 2 pos)))
  8171. val)
  8172. (forward-char 1) ""))
  8173. (defun org-table-field-info (arg)
  8174. "Show info about the current field, and highlight any reference at point."
  8175. (interactive "P")
  8176. (org-table-get-specials)
  8177. (save-excursion
  8178. (let* ((pos (point))
  8179. (col (org-table-current-column))
  8180. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8181. (name (car (rassoc (list (org-current-line) col)
  8182. org-table-named-field-locations)))
  8183. (eql (org-table-get-stored-formulas))
  8184. (dline (org-table-current-dline))
  8185. (ref (format "@%d$%d" dline col))
  8186. (ref1 (org-table-convert-refs-to-an ref))
  8187. (fequation (or (assoc name eql) (assoc ref eql)))
  8188. (cequation (assoc (int-to-string col) eql))
  8189. (eqn (or fequation cequation)))
  8190. (goto-char pos)
  8191. (condition-case nil
  8192. (org-table-show-reference 'local)
  8193. (error nil))
  8194. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8195. dline col
  8196. (if cname (concat " or $" cname) "")
  8197. dline col ref1
  8198. (if name (concat " or $" name) "")
  8199. ;; FIXME: formula info not correct if special table line
  8200. (if eqn
  8201. (concat ", formula: "
  8202. (org-table-formula-to-user
  8203. (concat
  8204. (if (string-match "^[$@]"(car eqn)) "" "$")
  8205. (car eqn) "=" (cdr eqn))))
  8206. "")))))
  8207. (defun org-table-current-column ()
  8208. "Find out which column we are in.
  8209. When called interactively, column is also displayed in echo area."
  8210. (interactive)
  8211. (if (interactive-p) (org-table-check-inside-data-field))
  8212. (save-excursion
  8213. (let ((cnt 0) (pos (point)))
  8214. (beginning-of-line 1)
  8215. (while (search-forward "|" pos t)
  8216. (setq cnt (1+ cnt)))
  8217. (if (interactive-p) (message "This is table column %d" cnt))
  8218. cnt)))
  8219. (defun org-table-current-dline ()
  8220. "Find out what table data line we are in.
  8221. Only datalins count for this."
  8222. (interactive)
  8223. (if (interactive-p) (org-table-check-inside-data-field))
  8224. (save-excursion
  8225. (let ((cnt 0) (pos (point)))
  8226. (goto-char (org-table-begin))
  8227. (while (<= (point) pos)
  8228. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8229. (beginning-of-line 2))
  8230. (if (interactive-p) (message "This is table line %d" cnt))
  8231. cnt)))
  8232. (defun org-table-goto-column (n &optional on-delim force)
  8233. "Move the cursor to the Nth column in the current table line.
  8234. With optional argument ON-DELIM, stop with point before the left delimiter
  8235. of the field.
  8236. If there are less than N fields, just go to after the last delimiter.
  8237. However, when FORCE is non-nil, create new columns if necessary."
  8238. (interactive "p")
  8239. (let ((pos (point-at-eol)))
  8240. (beginning-of-line 1)
  8241. (when (> n 0)
  8242. (while (and (> (setq n (1- n)) -1)
  8243. (or (search-forward "|" pos t)
  8244. (and force
  8245. (progn (end-of-line 1)
  8246. (skip-chars-backward "^|")
  8247. (insert " | "))))))
  8248. ; (backward-char 2) t)))))
  8249. (when (and force (not (looking-at ".*|")))
  8250. (save-excursion (end-of-line 1) (insert " | ")))
  8251. (if on-delim
  8252. (backward-char 1)
  8253. (if (looking-at " ") (forward-char 1))))))
  8254. (defun org-at-table-p (&optional table-type)
  8255. "Return t if the cursor is inside an org-type table.
  8256. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8257. (if org-enable-table-editor
  8258. (save-excursion
  8259. (beginning-of-line 1)
  8260. (looking-at (if table-type org-table-any-line-regexp
  8261. org-table-line-regexp)))
  8262. nil))
  8263. (defun org-at-table.el-p ()
  8264. "Return t if and only if we are at a table.el table."
  8265. (and (org-at-table-p 'any)
  8266. (save-excursion
  8267. (goto-char (org-table-begin 'any))
  8268. (looking-at org-table1-hline-regexp))))
  8269. (defun org-table-recognize-table.el ()
  8270. "If there is a table.el table nearby, recognize it and move into it."
  8271. (if org-table-tab-recognizes-table.el
  8272. (if (org-at-table.el-p)
  8273. (progn
  8274. (beginning-of-line 1)
  8275. (if (looking-at org-table-dataline-regexp)
  8276. nil
  8277. (if (looking-at org-table1-hline-regexp)
  8278. (progn
  8279. (beginning-of-line 2)
  8280. (if (looking-at org-table-any-border-regexp)
  8281. (beginning-of-line -1)))))
  8282. (if (re-search-forward "|" (org-table-end t) t)
  8283. (progn
  8284. (require 'table)
  8285. (if (table--at-cell-p (point))
  8286. t
  8287. (message "recognizing table.el table...")
  8288. (table-recognize-table)
  8289. (message "recognizing table.el table...done")))
  8290. (error "This should not happen..."))
  8291. t)
  8292. nil)
  8293. nil))
  8294. (defun org-at-table-hline-p ()
  8295. "Return t if the cursor is inside a hline in a table."
  8296. (if org-enable-table-editor
  8297. (save-excursion
  8298. (beginning-of-line 1)
  8299. (looking-at org-table-hline-regexp))
  8300. nil))
  8301. (defun org-table-insert-column ()
  8302. "Insert a new column into the table."
  8303. (interactive)
  8304. (if (not (org-at-table-p))
  8305. (error "Not at a table"))
  8306. (org-table-find-dataline)
  8307. (let* ((col (max 1 (org-table-current-column)))
  8308. (beg (org-table-begin))
  8309. (end (org-table-end))
  8310. ;; Current cursor position
  8311. (linepos (org-current-line))
  8312. (colpos col))
  8313. (goto-char beg)
  8314. (while (< (point) end)
  8315. (if (org-at-table-hline-p)
  8316. nil
  8317. (org-table-goto-column col t)
  8318. (insert "| "))
  8319. (beginning-of-line 2))
  8320. (move-marker end nil)
  8321. (goto-line linepos)
  8322. (org-table-goto-column colpos)
  8323. (org-table-align)
  8324. (org-table-fix-formulas "$" nil (1- col) 1)))
  8325. (defun org-table-find-dataline ()
  8326. "Find a dataline in the current table, which is needed for column commands."
  8327. (if (and (org-at-table-p)
  8328. (not (org-at-table-hline-p)))
  8329. t
  8330. (let ((col (current-column))
  8331. (end (org-table-end)))
  8332. (move-to-column col)
  8333. (while (and (< (point) end)
  8334. (or (not (= (current-column) col))
  8335. (org-at-table-hline-p)))
  8336. (beginning-of-line 2)
  8337. (move-to-column col))
  8338. (if (and (org-at-table-p)
  8339. (not (org-at-table-hline-p)))
  8340. t
  8341. (error
  8342. "Please position cursor in a data line for column operations")))))
  8343. (defun org-table-delete-column ()
  8344. "Delete a column from the table."
  8345. (interactive)
  8346. (if (not (org-at-table-p))
  8347. (error "Not at a table"))
  8348. (org-table-find-dataline)
  8349. (org-table-check-inside-data-field)
  8350. (let* ((col (org-table-current-column))
  8351. (beg (org-table-begin))
  8352. (end (org-table-end))
  8353. ;; Current cursor position
  8354. (linepos (org-current-line))
  8355. (colpos col))
  8356. (goto-char beg)
  8357. (while (< (point) end)
  8358. (if (org-at-table-hline-p)
  8359. nil
  8360. (org-table-goto-column col t)
  8361. (and (looking-at "|[^|\n]+|")
  8362. (replace-match "|")))
  8363. (beginning-of-line 2))
  8364. (move-marker end nil)
  8365. (goto-line linepos)
  8366. (org-table-goto-column colpos)
  8367. (org-table-align)
  8368. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8369. col -1 col)))
  8370. (defun org-table-move-column-right ()
  8371. "Move column to the right."
  8372. (interactive)
  8373. (org-table-move-column nil))
  8374. (defun org-table-move-column-left ()
  8375. "Move column to the left."
  8376. (interactive)
  8377. (org-table-move-column 'left))
  8378. (defun org-table-move-column (&optional left)
  8379. "Move the current column to the right. With arg LEFT, move to the left."
  8380. (interactive "P")
  8381. (if (not (org-at-table-p))
  8382. (error "Not at a table"))
  8383. (org-table-find-dataline)
  8384. (org-table-check-inside-data-field)
  8385. (let* ((col (org-table-current-column))
  8386. (col1 (if left (1- col) col))
  8387. (beg (org-table-begin))
  8388. (end (org-table-end))
  8389. ;; Current cursor position
  8390. (linepos (org-current-line))
  8391. (colpos (if left (1- col) (1+ col))))
  8392. (if (and left (= col 1))
  8393. (error "Cannot move column further left"))
  8394. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8395. (error "Cannot move column further right"))
  8396. (goto-char beg)
  8397. (while (< (point) end)
  8398. (if (org-at-table-hline-p)
  8399. nil
  8400. (org-table-goto-column col1 t)
  8401. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8402. (replace-match "|\\2|\\1|")))
  8403. (beginning-of-line 2))
  8404. (move-marker end nil)
  8405. (goto-line linepos)
  8406. (org-table-goto-column colpos)
  8407. (org-table-align)
  8408. (org-table-fix-formulas
  8409. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8410. (cons (number-to-string colpos) (number-to-string col))))))
  8411. (defun org-table-move-row-down ()
  8412. "Move table row down."
  8413. (interactive)
  8414. (org-table-move-row nil))
  8415. (defun org-table-move-row-up ()
  8416. "Move table row up."
  8417. (interactive)
  8418. (org-table-move-row 'up))
  8419. (defun org-table-move-row (&optional up)
  8420. "Move the current table line down. With arg UP, move it up."
  8421. (interactive "P")
  8422. (let* ((col (current-column))
  8423. (pos (point))
  8424. (hline1p (save-excursion (beginning-of-line 1)
  8425. (looking-at org-table-hline-regexp)))
  8426. (dline1 (org-table-current-dline))
  8427. (dline2 (+ dline1 (if up -1 1)))
  8428. (tonew (if up 0 2))
  8429. txt hline2p)
  8430. (beginning-of-line tonew)
  8431. (unless (org-at-table-p)
  8432. (goto-char pos)
  8433. (error "Cannot move row further"))
  8434. (setq hline2p (looking-at org-table-hline-regexp))
  8435. (goto-char pos)
  8436. (beginning-of-line 1)
  8437. (setq pos (point))
  8438. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8439. (delete-region (point) (1+ (point-at-eol)))
  8440. (beginning-of-line tonew)
  8441. (insert txt)
  8442. (beginning-of-line 0)
  8443. (move-to-column col)
  8444. (unless (or hline1p hline2p)
  8445. (org-table-fix-formulas
  8446. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8447. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8448. (defun org-table-insert-row (&optional arg)
  8449. "Insert a new row above the current line into the table.
  8450. With prefix ARG, insert below the current line."
  8451. (interactive "P")
  8452. (if (not (org-at-table-p))
  8453. (error "Not at a table"))
  8454. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8455. (new (org-table-clean-line line)))
  8456. ;; Fix the first field if necessary
  8457. (if (string-match "^[ \t]*| *[#$] *|" line)
  8458. (setq new (replace-match (match-string 0 line) t t new)))
  8459. (beginning-of-line (if arg 2 1))
  8460. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8461. (beginning-of-line 0)
  8462. (re-search-forward "| ?" (point-at-eol) t)
  8463. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8464. (org-table-align))
  8465. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8466. (defun org-table-insert-hline (&optional above)
  8467. "Insert a horizontal-line below the current line into the table.
  8468. With prefix ABOVE, insert above the current line."
  8469. (interactive "P")
  8470. (if (not (org-at-table-p))
  8471. (error "Not at a table"))
  8472. (let ((line (org-table-clean-line
  8473. (buffer-substring (point-at-bol) (point-at-eol))))
  8474. (col (current-column)))
  8475. (while (string-match "|\\( +\\)|" line)
  8476. (setq line (replace-match
  8477. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8478. ?-) "|") t t line)))
  8479. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8480. (beginning-of-line (if above 1 2))
  8481. (insert line "\n")
  8482. (beginning-of-line (if above 1 -1))
  8483. (move-to-column col)
  8484. (and org-table-overlay-coordinates (org-table-align))))
  8485. (defun org-table-hline-and-move (&optional same-column)
  8486. "Insert a hline and move to the row below that line."
  8487. (interactive "P")
  8488. (let ((col (org-table-current-column)))
  8489. (org-table-maybe-eval-formula)
  8490. (org-table-maybe-recalculate-line)
  8491. (org-table-insert-hline)
  8492. (end-of-line 2)
  8493. (if (looking-at "\n[ \t]*|-")
  8494. (progn (insert "\n|") (org-table-align))
  8495. (org-table-next-field))
  8496. (if same-column (org-table-goto-column col))))
  8497. (defun org-table-clean-line (s)
  8498. "Convert a table line S into a string with only \"|\" and space.
  8499. In particular, this does handle wide and invisible characters."
  8500. (if (string-match "^[ \t]*|-" s)
  8501. ;; It's a hline, just map the characters
  8502. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8503. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8504. (setq s (replace-match
  8505. (concat "|" (make-string (org-string-width (match-string 1 s))
  8506. ?\ ) "|")
  8507. t t s)))
  8508. s))
  8509. (defun org-table-kill-row ()
  8510. "Delete the current row or horizontal line from the table."
  8511. (interactive)
  8512. (if (not (org-at-table-p))
  8513. (error "Not at a table"))
  8514. (let ((col (current-column))
  8515. (dline (org-table-current-dline)))
  8516. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8517. (if (not (org-at-table-p)) (beginning-of-line 0))
  8518. (move-to-column col)
  8519. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8520. dline -1 dline)))
  8521. (defun org-table-sort-lines (with-case &optional sorting-type)
  8522. "Sort table lines according to the column at point.
  8523. The position of point indicates the column to be used for
  8524. sorting, and the range of lines is the range between the nearest
  8525. horizontal separator lines, or the entire table of no such lines
  8526. exist. If point is before the first column, you will be prompted
  8527. for the sorting column. If there is an active region, the mark
  8528. specifies the first line and the sorting column, while point
  8529. should be in the last line to be included into the sorting.
  8530. The command then prompts for the sorting type which can be
  8531. alphabetically, numerically, or by time (as given in a time stamp
  8532. in the field). Sorting in reverse order is also possible.
  8533. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8534. If SORTING-TYPE is specified when this function is called from a Lisp
  8535. program, no prompting will take place. SORTING-TYPE must be a character,
  8536. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8537. should be done in reverse order."
  8538. (interactive "P")
  8539. (let* ((thisline (org-current-line))
  8540. (thiscol (org-table-current-column))
  8541. beg end bcol ecol tend tbeg column lns pos)
  8542. (when (equal thiscol 0)
  8543. (if (interactive-p)
  8544. (setq thiscol
  8545. (string-to-number
  8546. (read-string "Use column N for sorting: ")))
  8547. (setq thiscol 1))
  8548. (org-table-goto-column thiscol))
  8549. (org-table-check-inside-data-field)
  8550. (if (org-region-active-p)
  8551. (progn
  8552. (setq beg (region-beginning) end (region-end))
  8553. (goto-char beg)
  8554. (setq column (org-table-current-column)
  8555. beg (point-at-bol))
  8556. (goto-char end)
  8557. (setq end (point-at-bol 2)))
  8558. (setq column (org-table-current-column)
  8559. pos (point)
  8560. tbeg (org-table-begin)
  8561. tend (org-table-end))
  8562. (if (re-search-backward org-table-hline-regexp tbeg t)
  8563. (setq beg (point-at-bol 2))
  8564. (goto-char tbeg)
  8565. (setq beg (point-at-bol 1)))
  8566. (goto-char pos)
  8567. (if (re-search-forward org-table-hline-regexp tend t)
  8568. (setq end (point-at-bol 1))
  8569. (goto-char tend)
  8570. (setq end (point-at-bol))))
  8571. (setq beg (move-marker (make-marker) beg)
  8572. end (move-marker (make-marker) end))
  8573. (untabify beg end)
  8574. (goto-char beg)
  8575. (org-table-goto-column column)
  8576. (skip-chars-backward "^|")
  8577. (setq bcol (current-column))
  8578. (org-table-goto-column (1+ column))
  8579. (skip-chars-backward "^|")
  8580. (setq ecol (1- (current-column)))
  8581. (org-table-goto-column column)
  8582. (setq lns (mapcar (lambda(x) (cons
  8583. (org-sort-remove-invisible
  8584. (nth (1- column)
  8585. (org-split-string x "[ \t]*|[ \t]*")))
  8586. x))
  8587. (org-split-string (buffer-substring beg end) "\n")))
  8588. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8589. (delete-region beg end)
  8590. (move-marker beg nil)
  8591. (move-marker end nil)
  8592. (insert (mapconcat 'cdr lns "\n") "\n")
  8593. (goto-line thisline)
  8594. (org-table-goto-column thiscol)
  8595. (message "%d lines sorted, based on column %d" (length lns) column)))
  8596. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8597. (defun org-sort-remove-invisible (s)
  8598. (remove-text-properties 0 (length s) org-rm-props s)
  8599. (while (string-match org-bracket-link-regexp s)
  8600. (setq s (replace-match (if (match-end 2)
  8601. (match-string 3 s)
  8602. (match-string 1 s)) t t s)))
  8603. s)
  8604. (defun org-table-cut-region (beg end)
  8605. "Copy region in table to the clipboard and blank all relevant fields."
  8606. (interactive "r")
  8607. (org-table-copy-region beg end 'cut))
  8608. (defun org-table-copy-region (beg end &optional cut)
  8609. "Copy rectangular region in table to clipboard.
  8610. A special clipboard is used which can only be accessed
  8611. with `org-table-paste-rectangle'."
  8612. (interactive "rP")
  8613. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8614. region cols
  8615. (rpl (if cut " " nil)))
  8616. (goto-char beg)
  8617. (org-table-check-inside-data-field)
  8618. (setq l01 (org-current-line)
  8619. c01 (org-table-current-column))
  8620. (goto-char end)
  8621. (org-table-check-inside-data-field)
  8622. (setq l02 (org-current-line)
  8623. c02 (org-table-current-column))
  8624. (setq l1 (min l01 l02) l2 (max l01 l02)
  8625. c1 (min c01 c02) c2 (max c01 c02))
  8626. (catch 'exit
  8627. (while t
  8628. (catch 'nextline
  8629. (if (> l1 l2) (throw 'exit t))
  8630. (goto-line l1)
  8631. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8632. (setq cols nil ic1 c1 ic2 c2)
  8633. (while (< ic1 (1+ ic2))
  8634. (push (org-table-get-field ic1 rpl) cols)
  8635. (setq ic1 (1+ ic1)))
  8636. (push (nreverse cols) region)
  8637. (setq l1 (1+ l1)))))
  8638. (setq org-table-clip (nreverse region))
  8639. (if cut (org-table-align))
  8640. org-table-clip))
  8641. (defun org-table-paste-rectangle ()
  8642. "Paste a rectangular region into a table.
  8643. The upper right corner ends up in the current field. All involved fields
  8644. will be overwritten. If the rectangle does not fit into the present table,
  8645. the table is enlarged as needed. The process ignores horizontal separator
  8646. lines."
  8647. (interactive)
  8648. (unless (and org-table-clip (listp org-table-clip))
  8649. (error "First cut/copy a region to paste!"))
  8650. (org-table-check-inside-data-field)
  8651. (let* ((clip org-table-clip)
  8652. (line (org-current-line))
  8653. (col (org-table-current-column))
  8654. (org-enable-table-editor t)
  8655. (org-table-automatic-realign nil)
  8656. c cols field)
  8657. (while (setq cols (pop clip))
  8658. (while (org-at-table-hline-p) (beginning-of-line 2))
  8659. (if (not (org-at-table-p))
  8660. (progn (end-of-line 0) (org-table-next-field)))
  8661. (setq c col)
  8662. (while (setq field (pop cols))
  8663. (org-table-goto-column c nil 'force)
  8664. (org-table-get-field nil field)
  8665. (setq c (1+ c)))
  8666. (beginning-of-line 2))
  8667. (goto-line line)
  8668. (org-table-goto-column col)
  8669. (org-table-align)))
  8670. (defun org-table-convert ()
  8671. "Convert from `org-mode' table to table.el and back.
  8672. Obviously, this only works within limits. When an Org-mode table is
  8673. converted to table.el, all horizontal separator lines get lost, because
  8674. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8675. A table.el table can be converted to an Org-mode table only if it does not
  8676. do row or column spanning. Multiline cells will become multiple cells.
  8677. Beware, Org-mode does not test if the table can be successfully converted - it
  8678. blindly applies a recipe that works for simple tables."
  8679. (interactive)
  8680. (require 'table)
  8681. (if (org-at-table.el-p)
  8682. ;; convert to Org-mode table
  8683. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8684. (end (move-marker (make-marker) (org-table-end t))))
  8685. (table-unrecognize-region beg end)
  8686. (goto-char beg)
  8687. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8688. (replace-match ""))
  8689. (goto-char beg))
  8690. (if (org-at-table-p)
  8691. ;; convert to table.el table
  8692. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8693. (end (move-marker (make-marker) (org-table-end))))
  8694. ;; first, get rid of all horizontal lines
  8695. (goto-char beg)
  8696. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8697. (replace-match ""))
  8698. ;; insert a hline before first
  8699. (goto-char beg)
  8700. (org-table-insert-hline 'above)
  8701. (beginning-of-line -1)
  8702. ;; insert a hline after each line
  8703. (while (progn (beginning-of-line 3) (< (point) end))
  8704. (org-table-insert-hline))
  8705. (goto-char beg)
  8706. (setq end (move-marker end (org-table-end)))
  8707. ;; replace "+" at beginning and ending of hlines
  8708. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8709. (replace-match "\\1+-"))
  8710. (goto-char beg)
  8711. (while (re-search-forward "-|[ \t]*$" end t)
  8712. (replace-match "-+"))
  8713. (goto-char beg)))))
  8714. (defun org-table-wrap-region (arg)
  8715. "Wrap several fields in a column like a paragraph.
  8716. This is useful if you'd like to spread the contents of a field over several
  8717. lines, in order to keep the table compact.
  8718. If there is an active region, and both point and mark are in the same column,
  8719. the text in the column is wrapped to minimum width for the given number of
  8720. lines. Generally, this makes the table more compact. A prefix ARG may be
  8721. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8722. formats the selected text to two lines. If the region was longer than two
  8723. lines, the remaining lines remain empty. A negative prefix argument reduces
  8724. the current number of lines by that amount. The wrapped text is pasted back
  8725. into the table. If you formatted it to more lines than it was before, fields
  8726. further down in the table get overwritten - so you might need to make space in
  8727. the table first.
  8728. If there is no region, the current field is split at the cursor position and
  8729. the text fragment to the right of the cursor is prepended to the field one
  8730. line down.
  8731. If there is no region, but you specify a prefix ARG, the current field gets
  8732. blank, and the content is appended to the field above."
  8733. (interactive "P")
  8734. (org-table-check-inside-data-field)
  8735. (if (org-region-active-p)
  8736. ;; There is a region: fill as a paragraph
  8737. (let* ((beg (region-beginning))
  8738. (cline (save-excursion (goto-char beg) (org-current-line)))
  8739. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8740. nlines)
  8741. (org-table-cut-region (region-beginning) (region-end))
  8742. (if (> (length (car org-table-clip)) 1)
  8743. (error "Region must be limited to single column"))
  8744. (setq nlines (if arg
  8745. (if (< arg 1)
  8746. (+ (length org-table-clip) arg)
  8747. arg)
  8748. (length org-table-clip)))
  8749. (setq org-table-clip
  8750. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8751. nil nlines)))
  8752. (goto-line cline)
  8753. (org-table-goto-column ccol)
  8754. (org-table-paste-rectangle))
  8755. ;; No region, split the current field at point
  8756. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8757. (skip-chars-forward "^\r\n|"))
  8758. (if arg
  8759. ;; combine with field above
  8760. (let ((s (org-table-blank-field))
  8761. (col (org-table-current-column)))
  8762. (beginning-of-line 0)
  8763. (while (org-at-table-hline-p) (beginning-of-line 0))
  8764. (org-table-goto-column col)
  8765. (skip-chars-forward "^|")
  8766. (skip-chars-backward " ")
  8767. (insert " " (org-trim s))
  8768. (org-table-align))
  8769. ;; split field
  8770. (if (looking-at "\\([^|]+\\)+|")
  8771. (let ((s (match-string 1)))
  8772. (replace-match " |")
  8773. (goto-char (match-beginning 0))
  8774. (org-table-next-row)
  8775. (insert (org-trim s) " ")
  8776. (org-table-align))
  8777. (org-table-next-row)))))
  8778. (defvar org-field-marker nil)
  8779. (defun org-table-edit-field (arg)
  8780. "Edit table field in a different window.
  8781. This is mainly useful for fields that contain hidden parts.
  8782. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8783. it can be edited in place."
  8784. (interactive "P")
  8785. (if arg
  8786. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8787. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8788. (remove-text-properties b e '(org-cwidth t invisible t
  8789. display t intangible t))
  8790. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8791. (font-lock-fontify-block)))
  8792. (let ((pos (move-marker (make-marker) (point)))
  8793. (field (org-table-get-field))
  8794. (cw (current-window-configuration))
  8795. p)
  8796. (org-switch-to-buffer-other-window "*Org tmp*")
  8797. (erase-buffer)
  8798. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8799. (let ((org-inhibit-startup t)) (org-mode))
  8800. (goto-char (setq p (point-max)))
  8801. (insert (org-trim field))
  8802. (remove-text-properties p (point-max)
  8803. '(invisible t org-cwidth t display t
  8804. intangible t))
  8805. (goto-char p)
  8806. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8807. (org-set-local 'org-window-configuration cw)
  8808. (org-set-local 'org-field-marker pos)
  8809. (message "Edit and finish with C-c C-c"))))
  8810. (defun org-table-finish-edit-field ()
  8811. "Finish editing a table data field.
  8812. Remove all newline characters, insert the result into the table, realign
  8813. the table and kill the editing buffer."
  8814. (let ((pos org-field-marker)
  8815. (cw org-window-configuration)
  8816. (cb (current-buffer))
  8817. text)
  8818. (goto-char (point-min))
  8819. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8820. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8821. (replace-match " "))
  8822. (setq text (org-trim (buffer-string)))
  8823. (set-window-configuration cw)
  8824. (kill-buffer cb)
  8825. (select-window (get-buffer-window (marker-buffer pos)))
  8826. (goto-char pos)
  8827. (move-marker pos nil)
  8828. (org-table-check-inside-data-field)
  8829. (org-table-get-field nil text)
  8830. (org-table-align)
  8831. (message "New field value inserted")))
  8832. (defun org-trim (s)
  8833. "Remove whitespace at beginning and end of string."
  8834. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8835. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8836. s)
  8837. (defun org-wrap (string &optional width lines)
  8838. "Wrap string to either a number of lines, or a width in characters.
  8839. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8840. that costs. If there is a word longer than WIDTH, the text is actually
  8841. wrapped to the length of that word.
  8842. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8843. many lines, whatever width that takes.
  8844. The return value is a list of lines, without newlines at the end."
  8845. (let* ((words (org-split-string string "[ \t\n]+"))
  8846. (maxword (apply 'max (mapcar 'org-string-width words)))
  8847. w ll)
  8848. (cond (width
  8849. (org-do-wrap words (max maxword width)))
  8850. (lines
  8851. (setq w maxword)
  8852. (setq ll (org-do-wrap words maxword))
  8853. (if (<= (length ll) lines)
  8854. ll
  8855. (setq ll words)
  8856. (while (> (length ll) lines)
  8857. (setq w (1+ w))
  8858. (setq ll (org-do-wrap words w)))
  8859. ll))
  8860. (t (error "Cannot wrap this")))))
  8861. (defun org-do-wrap (words width)
  8862. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8863. (let (lines line)
  8864. (while words
  8865. (setq line (pop words))
  8866. (while (and words (< (+ (length line) (length (car words))) width))
  8867. (setq line (concat line " " (pop words))))
  8868. (setq lines (push line lines)))
  8869. (nreverse lines)))
  8870. (defun org-split-string (string &optional separators)
  8871. "Splits STRING into substrings at SEPARATORS.
  8872. No empty strings are returned if there are matches at the beginning
  8873. and end of string."
  8874. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8875. (start 0)
  8876. notfirst
  8877. (list nil))
  8878. (while (and (string-match rexp string
  8879. (if (and notfirst
  8880. (= start (match-beginning 0))
  8881. (< start (length string)))
  8882. (1+ start) start))
  8883. (< (match-beginning 0) (length string)))
  8884. (setq notfirst t)
  8885. (or (eq (match-beginning 0) 0)
  8886. (and (eq (match-beginning 0) (match-end 0))
  8887. (eq (match-beginning 0) start))
  8888. (setq list
  8889. (cons (substring string start (match-beginning 0))
  8890. list)))
  8891. (setq start (match-end 0)))
  8892. (or (eq start (length string))
  8893. (setq list
  8894. (cons (substring string start)
  8895. list)))
  8896. (nreverse list)))
  8897. (defun org-table-map-tables (function)
  8898. "Apply FUNCTION to the start of all tables in the buffer."
  8899. (save-excursion
  8900. (save-restriction
  8901. (widen)
  8902. (goto-char (point-min))
  8903. (while (re-search-forward org-table-any-line-regexp nil t)
  8904. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8905. (beginning-of-line 1)
  8906. (if (looking-at org-table-line-regexp)
  8907. (save-excursion (funcall function)))
  8908. (re-search-forward org-table-any-border-regexp nil 1))))
  8909. (message "Mapping tables: done"))
  8910. (defvar org-timecnt) ; dynamically scoped parameter
  8911. (defun org-table-sum (&optional beg end nlast)
  8912. "Sum numbers in region of current table column.
  8913. The result will be displayed in the echo area, and will be available
  8914. as kill to be inserted with \\[yank].
  8915. If there is an active region, it is interpreted as a rectangle and all
  8916. numbers in that rectangle will be summed. If there is no active
  8917. region and point is located in a table column, sum all numbers in that
  8918. column.
  8919. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8920. numbers are assumed to be times as well (in decimal hours) and the
  8921. numbers are added as such.
  8922. If NLAST is a number, only the NLAST fields will actually be summed."
  8923. (interactive)
  8924. (save-excursion
  8925. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8926. (cond
  8927. ((and beg end)) ; beg and end given explicitly
  8928. ((org-region-active-p)
  8929. (setq beg (region-beginning) end (region-end)))
  8930. (t
  8931. (setq col (org-table-current-column))
  8932. (goto-char (org-table-begin))
  8933. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8934. (error "No table data"))
  8935. (org-table-goto-column col)
  8936. (setq beg (point))
  8937. (goto-char (org-table-end))
  8938. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8939. (error "No table data"))
  8940. (org-table-goto-column col)
  8941. (setq end (point))))
  8942. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8943. (items1 (cond ((not nlast) items)
  8944. ((>= nlast (length items)) items)
  8945. (t (setq items (reverse items))
  8946. (setcdr (nthcdr (1- nlast) items) nil)
  8947. (nreverse items))))
  8948. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8949. items1)))
  8950. (res (apply '+ numbers))
  8951. (sres (if (= org-timecnt 0)
  8952. (format "%g" res)
  8953. (setq diff (* 3600 res)
  8954. h (floor (/ diff 3600)) diff (mod diff 3600)
  8955. m (floor (/ diff 60)) diff (mod diff 60)
  8956. s diff)
  8957. (format "%d:%02d:%02d" h m s))))
  8958. (kill-new sres)
  8959. (if (interactive-p)
  8960. (message "%s"
  8961. (substitute-command-keys
  8962. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8963. (length numbers) sres))))
  8964. sres))))
  8965. (defun org-table-get-number-for-summing (s)
  8966. (let (n)
  8967. (if (string-match "^ *|? *" s)
  8968. (setq s (replace-match "" nil nil s)))
  8969. (if (string-match " *|? *$" s)
  8970. (setq s (replace-match "" nil nil s)))
  8971. (setq n (string-to-number s))
  8972. (cond
  8973. ((and (string-match "0" s)
  8974. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8975. ((string-match "\\`[ \t]+\\'" s) nil)
  8976. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8977. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8978. (m (string-to-number (or (match-string 2 s) "0")))
  8979. (s (string-to-number (or (match-string 4 s) "0"))))
  8980. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8981. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8982. ((equal n 0) nil)
  8983. (t n))))
  8984. (defun org-table-current-field-formula (&optional key noerror)
  8985. "Return the formula active for the current field.
  8986. Assumes that specials are in place.
  8987. If KEY is given, return the key to this formula.
  8988. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8989. (let* ((name (car (rassoc (list (org-current-line)
  8990. (org-table-current-column))
  8991. org-table-named-field-locations)))
  8992. (col (org-table-current-column))
  8993. (scol (int-to-string col))
  8994. (ref (format "@%d$%d" (org-table-current-dline) col))
  8995. (stored-list (org-table-get-stored-formulas noerror))
  8996. (ass (or (assoc name stored-list)
  8997. (assoc ref stored-list)
  8998. (assoc scol stored-list))))
  8999. (if key
  9000. (car ass)
  9001. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  9002. (cdr ass))))))
  9003. (defun org-table-get-formula (&optional equation named)
  9004. "Read a formula from the minibuffer, offer stored formula as default.
  9005. When NAMED is non-nil, look for a named equation."
  9006. (let* ((stored-list (org-table-get-stored-formulas))
  9007. (name (car (rassoc (list (org-current-line)
  9008. (org-table-current-column))
  9009. org-table-named-field-locations)))
  9010. (ref (format "@%d$%d" (org-table-current-dline)
  9011. (org-table-current-column)))
  9012. (refass (assoc ref stored-list))
  9013. (scol (if named
  9014. (if name name ref)
  9015. (int-to-string (org-table-current-column))))
  9016. (dummy (and (or name refass) (not named)
  9017. (not (y-or-n-p "Replace field formula with column formula? " ))
  9018. (error "Abort")))
  9019. (name (or name ref))
  9020. (org-table-may-need-update nil)
  9021. (stored (cdr (assoc scol stored-list)))
  9022. (eq (cond
  9023. ((and stored equation (string-match "^ *=? *$" equation))
  9024. stored)
  9025. ((stringp equation)
  9026. equation)
  9027. (t (org-table-formula-from-user
  9028. (read-string
  9029. (org-table-formula-to-user
  9030. (format "%s formula %s%s="
  9031. (if named "Field" "Column")
  9032. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  9033. scol))
  9034. (if stored (org-table-formula-to-user stored) "")
  9035. 'org-table-formula-history
  9036. )))))
  9037. mustsave)
  9038. (when (not (string-match "\\S-" eq))
  9039. ;; remove formula
  9040. (setq stored-list (delq (assoc scol stored-list) stored-list))
  9041. (org-table-store-formulas stored-list)
  9042. (error "Formula removed"))
  9043. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  9044. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  9045. (if (and name (not named))
  9046. ;; We set the column equation, delete the named one.
  9047. (setq stored-list (delq (assoc name stored-list) stored-list)
  9048. mustsave t))
  9049. (if stored
  9050. (setcdr (assoc scol stored-list) eq)
  9051. (setq stored-list (cons (cons scol eq) stored-list)))
  9052. (if (or mustsave (not (equal stored eq)))
  9053. (org-table-store-formulas stored-list))
  9054. eq))
  9055. (defun org-table-store-formulas (alist)
  9056. "Store the list of formulas below the current table."
  9057. (setq alist (sort alist 'org-table-formula-less-p))
  9058. (save-excursion
  9059. (goto-char (org-table-end))
  9060. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  9061. (progn
  9062. ;; don't overwrite TBLFM, we might use text properties to store stuff
  9063. (goto-char (match-beginning 2))
  9064. (delete-region (match-beginning 2) (match-end 0)))
  9065. (insert "#+TBLFM:"))
  9066. (insert " "
  9067. (mapconcat (lambda (x)
  9068. (concat
  9069. (if (equal (string-to-char (car x)) ?@) "" "$")
  9070. (car x) "=" (cdr x)))
  9071. alist "::")
  9072. "\n")))
  9073. (defsubst org-table-formula-make-cmp-string (a)
  9074. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  9075. (concat
  9076. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  9077. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  9078. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  9079. (defun org-table-formula-less-p (a b)
  9080. "Compare two formulas for sorting."
  9081. (let ((as (org-table-formula-make-cmp-string (car a)))
  9082. (bs (org-table-formula-make-cmp-string (car b))))
  9083. (and as bs (string< as bs))))
  9084. (defun org-table-get-stored-formulas (&optional noerror)
  9085. "Return an alist with the stored formulas directly after current table."
  9086. (interactive)
  9087. (let (scol eq eq-alist strings string seen)
  9088. (save-excursion
  9089. (goto-char (org-table-end))
  9090. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9091. (setq strings (org-split-string (match-string 2) " *:: *"))
  9092. (while (setq string (pop strings))
  9093. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9094. (setq scol (if (match-end 2)
  9095. (match-string 2 string)
  9096. (match-string 1 string))
  9097. eq (match-string 3 string)
  9098. eq-alist (cons (cons scol eq) eq-alist))
  9099. (if (member scol seen)
  9100. (if noerror
  9101. (progn
  9102. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9103. (ding)
  9104. (sit-for 2))
  9105. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9106. (push scol seen))))))
  9107. (nreverse eq-alist)))
  9108. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9109. "Modify the equations after the table structure has been edited.
  9110. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9111. For all numbers larger than LIMIT, shift them by DELTA."
  9112. (save-excursion
  9113. (goto-char (org-table-end))
  9114. (when (looking-at "#\\+TBLFM:")
  9115. (let ((re (concat key "\\([0-9]+\\)"))
  9116. (re2
  9117. (when remove
  9118. (if (equal key "$")
  9119. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9120. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9121. s n a)
  9122. (when remove
  9123. (while (re-search-forward re2 (point-at-eol) t)
  9124. (replace-match "")))
  9125. (while (re-search-forward re (point-at-eol) t)
  9126. (setq s (match-string 1) n (string-to-number s))
  9127. (cond
  9128. ((setq a (assoc s replace))
  9129. (replace-match (concat key (cdr a)) t t))
  9130. ((and limit (> n limit))
  9131. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9132. (defun org-table-get-specials ()
  9133. "Get the column names and local parameters for this table."
  9134. (save-excursion
  9135. (let ((beg (org-table-begin)) (end (org-table-end))
  9136. names name fields fields1 field cnt
  9137. c v l line col types dlines hlines)
  9138. (setq org-table-column-names nil
  9139. org-table-local-parameters nil
  9140. org-table-named-field-locations nil
  9141. org-table-current-begin-line nil
  9142. org-table-current-begin-pos nil
  9143. org-table-current-line-types nil)
  9144. (goto-char beg)
  9145. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9146. (setq names (org-split-string (match-string 1) " *| *")
  9147. cnt 1)
  9148. (while (setq name (pop names))
  9149. (setq cnt (1+ cnt))
  9150. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9151. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9152. (setq org-table-column-names (nreverse org-table-column-names))
  9153. (setq org-table-column-name-regexp
  9154. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9155. (goto-char beg)
  9156. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9157. (setq fields (org-split-string (match-string 1) " *| *"))
  9158. (while (setq field (pop fields))
  9159. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9160. (push (cons (match-string 1 field) (match-string 2 field))
  9161. org-table-local-parameters))))
  9162. (goto-char beg)
  9163. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9164. (setq c (match-string 1)
  9165. fields (org-split-string (match-string 2) " *| *"))
  9166. (save-excursion
  9167. (beginning-of-line (if (equal c "_") 2 0))
  9168. (setq line (org-current-line) col 1)
  9169. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9170. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9171. (while (and fields1 (setq field (pop fields)))
  9172. (setq v (pop fields1) col (1+ col))
  9173. (when (and (stringp field) (stringp v)
  9174. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9175. (push (cons field v) org-table-local-parameters)
  9176. (push (list field line col) org-table-named-field-locations))))
  9177. ;; Analyse the line types
  9178. (goto-char beg)
  9179. (setq org-table-current-begin-line (org-current-line)
  9180. org-table-current-begin-pos (point)
  9181. l org-table-current-begin-line)
  9182. (while (looking-at "[ \t]*|\\(-\\)?")
  9183. (push (if (match-end 1) 'hline 'dline) types)
  9184. (if (match-end 1) (push l hlines) (push l dlines))
  9185. (beginning-of-line 2)
  9186. (setq l (1+ l)))
  9187. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9188. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9189. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9190. (defun org-table-maybe-eval-formula ()
  9191. "Check if the current field starts with \"=\" or \":=\".
  9192. If yes, store the formula and apply it."
  9193. ;; We already know we are in a table. Get field will only return a formula
  9194. ;; when appropriate. It might return a separator line, but no problem.
  9195. (when org-table-formula-evaluate-inline
  9196. (let* ((field (org-trim (or (org-table-get-field) "")))
  9197. named eq)
  9198. (when (string-match "^:?=\\(.*\\)" field)
  9199. (setq named (equal (string-to-char field) ?:)
  9200. eq (match-string 1 field))
  9201. (if (or (fboundp 'calc-eval)
  9202. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9203. (org-table-eval-formula (if named '(4) nil)
  9204. (org-table-formula-from-user eq))
  9205. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9206. (defvar org-recalc-commands nil
  9207. "List of commands triggering the recalculation of a line.
  9208. Will be filled automatically during use.")
  9209. (defvar org-recalc-marks
  9210. '((" " . "Unmarked: no special line, no automatic recalculation")
  9211. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9212. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9213. ("!" . "Column name definition line. Reference in formula as $name.")
  9214. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9215. ("_" . "Names for values in row below this one.")
  9216. ("^" . "Names for values in row above this one.")))
  9217. (defun org-table-rotate-recalc-marks (&optional newchar)
  9218. "Rotate the recalculation mark in the first column.
  9219. If in any row, the first field is not consistent with a mark,
  9220. insert a new column for the markers.
  9221. When there is an active region, change all the lines in the region,
  9222. after prompting for the marking character.
  9223. After each change, a message will be displayed indicating the meaning
  9224. of the new mark."
  9225. (interactive)
  9226. (unless (org-at-table-p) (error "Not at a table"))
  9227. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9228. (beg (org-table-begin))
  9229. (end (org-table-end))
  9230. (l (org-current-line))
  9231. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9232. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9233. (have-col
  9234. (save-excursion
  9235. (goto-char beg)
  9236. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9237. (col (org-table-current-column))
  9238. (forcenew (car (assoc newchar org-recalc-marks)))
  9239. epos new)
  9240. (when l1
  9241. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9242. (setq newchar (char-to-string (read-char-exclusive))
  9243. forcenew (car (assoc newchar org-recalc-marks))))
  9244. (if (and newchar (not forcenew))
  9245. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9246. newchar))
  9247. (if l1 (goto-line l1))
  9248. (save-excursion
  9249. (beginning-of-line 1)
  9250. (unless (looking-at org-table-dataline-regexp)
  9251. (error "Not at a table data line")))
  9252. (unless have-col
  9253. (org-table-goto-column 1)
  9254. (org-table-insert-column)
  9255. (org-table-goto-column (1+ col)))
  9256. (setq epos (point-at-eol))
  9257. (save-excursion
  9258. (beginning-of-line 1)
  9259. (org-table-get-field
  9260. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9261. (concat " "
  9262. (setq new (or forcenew
  9263. (cadr (member (match-string 1) marks))))
  9264. " ")
  9265. " # ")))
  9266. (if (and l1 l2)
  9267. (progn
  9268. (goto-line l1)
  9269. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9270. (and (looking-at org-table-dataline-regexp)
  9271. (org-table-get-field 1 (concat " " new " "))))
  9272. (goto-line l1)))
  9273. (if (not (= epos (point-at-eol))) (org-table-align))
  9274. (goto-line l)
  9275. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9276. (defun org-table-maybe-recalculate-line ()
  9277. "Recompute the current line if marked for it, and if we haven't just done it."
  9278. (interactive)
  9279. (and org-table-allow-automatic-line-recalculation
  9280. (not (and (memq last-command org-recalc-commands)
  9281. (equal org-last-recalc-line (org-current-line))))
  9282. (save-excursion (beginning-of-line 1)
  9283. (looking-at org-table-auto-recalculate-regexp))
  9284. (org-table-recalculate) t))
  9285. (defvar org-table-formula-debug nil
  9286. "Non-nil means, debug table formulas.
  9287. When nil, simply write \"#ERROR\" in corrupted fields.")
  9288. (make-variable-buffer-local 'org-table-formula-debug)
  9289. (defvar modes)
  9290. (defsubst org-set-calc-mode (var &optional value)
  9291. (if (stringp var)
  9292. (setq var (assoc var '(("D" calc-angle-mode deg)
  9293. ("R" calc-angle-mode rad)
  9294. ("F" calc-prefer-frac t)
  9295. ("S" calc-symbolic-mode t)))
  9296. value (nth 2 var) var (nth 1 var)))
  9297. (if (memq var modes)
  9298. (setcar (cdr (memq var modes)) value)
  9299. (cons var (cons value modes)))
  9300. modes)
  9301. (defun org-table-eval-formula (&optional arg equation
  9302. suppress-align suppress-const
  9303. suppress-store suppress-analysis)
  9304. "Replace the table field value at the cursor by the result of a calculation.
  9305. This function makes use of Dave Gillespie's Calc package, in my view the
  9306. most exciting program ever written for GNU Emacs. So you need to have Calc
  9307. installed in order to use this function.
  9308. In a table, this command replaces the value in the current field with the
  9309. result of a formula. It also installs the formula as the \"current\" column
  9310. formula, by storing it in a special line below the table. When called
  9311. with a `C-u' prefix, the current field must ba a named field, and the
  9312. formula is installed as valid in only this specific field.
  9313. When called with two `C-u' prefixes, insert the active equation
  9314. for the field back into the current field, so that it can be
  9315. edited there. This is useful in order to use \\[org-table-show-reference]
  9316. to check the referenced fields.
  9317. When called, the command first prompts for a formula, which is read in
  9318. the minibuffer. Previously entered formulas are available through the
  9319. history list, and the last used formula is offered as a default.
  9320. These stored formulas are adapted correctly when moving, inserting, or
  9321. deleting columns with the corresponding commands.
  9322. The formula can be any algebraic expression understood by the Calc package.
  9323. For details, see the Org-mode manual.
  9324. This function can also be called from Lisp programs and offers
  9325. additional arguments: EQUATION can be the formula to apply. If this
  9326. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9327. used to speed-up recursive calls by by-passing unnecessary aligns.
  9328. SUPPRESS-CONST suppresses the interpretation of constants in the
  9329. formula, assuming that this has been done already outside the function.
  9330. SUPPRESS-STORE means the formula should not be stored, either because
  9331. it is already stored, or because it is a modified equation that should
  9332. not overwrite the stored one."
  9333. (interactive "P")
  9334. (org-table-check-inside-data-field)
  9335. (or suppress-analysis (org-table-get-specials))
  9336. (if (equal arg '(16))
  9337. (let ((eq (org-table-current-field-formula)))
  9338. (or eq (error "No equation active for current field"))
  9339. (org-table-get-field nil eq)
  9340. (org-table-align)
  9341. (setq org-table-may-need-update t))
  9342. (let* (fields
  9343. (ndown (if (integerp arg) arg 1))
  9344. (org-table-automatic-realign nil)
  9345. (case-fold-search nil)
  9346. (down (> ndown 1))
  9347. (formula (if (and equation suppress-store)
  9348. equation
  9349. (org-table-get-formula equation (equal arg '(4)))))
  9350. (n0 (org-table-current-column))
  9351. (modes (copy-sequence org-calc-default-modes))
  9352. (numbers nil) ; was a variable, now fixed default
  9353. (keep-empty nil)
  9354. n form form0 bw fmt x ev orig c lispp literal)
  9355. ;; Parse the format string. Since we have a lot of modes, this is
  9356. ;; a lot of work. However, I think calc still uses most of the time.
  9357. (if (string-match ";" formula)
  9358. (let ((tmp (org-split-string formula ";")))
  9359. (setq formula (car tmp)
  9360. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9361. (nth 1 tmp)))
  9362. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9363. (setq c (string-to-char (match-string 1 fmt))
  9364. n (string-to-number (match-string 2 fmt)))
  9365. (if (= c ?p)
  9366. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9367. (setq modes (org-set-calc-mode
  9368. 'calc-float-format
  9369. (list (cdr (assoc c '((?n . float) (?f . fix)
  9370. (?s . sci) (?e . eng))))
  9371. n))))
  9372. (setq fmt (replace-match "" t t fmt)))
  9373. (if (string-match "[NT]" fmt)
  9374. (setq numbers (equal (match-string 0 fmt) "N")
  9375. fmt (replace-match "" t t fmt)))
  9376. (if (string-match "L" fmt)
  9377. (setq literal t
  9378. fmt (replace-match "" t t fmt)))
  9379. (if (string-match "E" fmt)
  9380. (setq keep-empty t
  9381. fmt (replace-match "" t t fmt)))
  9382. (while (string-match "[DRFS]" fmt)
  9383. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9384. (setq fmt (replace-match "" t t fmt)))
  9385. (unless (string-match "\\S-" fmt)
  9386. (setq fmt nil))))
  9387. (if (and (not suppress-const) org-table-formula-use-constants)
  9388. (setq formula (org-table-formula-substitute-names formula)))
  9389. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9390. (while (> ndown 0)
  9391. (setq fields (org-split-string
  9392. (org-no-properties
  9393. (buffer-substring (point-at-bol) (point-at-eol)))
  9394. " *| *"))
  9395. (if (eq numbers t)
  9396. (setq fields (mapcar
  9397. (lambda (x) (number-to-string (string-to-number x)))
  9398. fields)))
  9399. (setq ndown (1- ndown))
  9400. (setq form (copy-sequence formula)
  9401. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9402. (if (and lispp literal) (setq lispp 'literal))
  9403. ;; Check for old vertical references
  9404. (setq form (org-rewrite-old-row-references form))
  9405. ;; Insert complex ranges
  9406. (while (string-match org-table-range-regexp form)
  9407. (setq form
  9408. (replace-match
  9409. (save-match-data
  9410. (org-table-make-reference
  9411. (org-table-get-range (match-string 0 form) nil n0)
  9412. keep-empty numbers lispp))
  9413. t t form)))
  9414. ;; Insert simple ranges
  9415. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9416. (setq form
  9417. (replace-match
  9418. (save-match-data
  9419. (org-table-make-reference
  9420. (org-sublist
  9421. fields (string-to-number (match-string 1 form))
  9422. (string-to-number (match-string 2 form)))
  9423. keep-empty numbers lispp))
  9424. t t form)))
  9425. (setq form0 form)
  9426. ;; Insert the references to fields in same row
  9427. (while (string-match "\\$\\([0-9]+\\)" form)
  9428. (setq n (string-to-number (match-string 1 form))
  9429. x (nth (1- (if (= n 0) n0 n)) fields))
  9430. (unless x (error "Invalid field specifier \"%s\""
  9431. (match-string 0 form)))
  9432. (setq form (replace-match
  9433. (save-match-data
  9434. (org-table-make-reference x nil numbers lispp))
  9435. t t form)))
  9436. (if lispp
  9437. (setq ev (condition-case nil
  9438. (eval (eval (read form)))
  9439. (error "#ERROR"))
  9440. ev (if (numberp ev) (number-to-string ev) ev))
  9441. (or (fboundp 'calc-eval)
  9442. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9443. (setq ev (calc-eval (cons form modes)
  9444. (if numbers 'num))))
  9445. (when org-table-formula-debug
  9446. (with-output-to-temp-buffer "*Substitution History*"
  9447. (princ (format "Substitution history of formula
  9448. Orig: %s
  9449. $xyz-> %s
  9450. @r$c-> %s
  9451. $1-> %s\n" orig formula form0 form))
  9452. (if (listp ev)
  9453. (princ (format " %s^\nError: %s"
  9454. (make-string (car ev) ?\-) (nth 1 ev)))
  9455. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9456. ev (or fmt "NONE")
  9457. (if fmt (format fmt (string-to-number ev)) ev)))))
  9458. (setq bw (get-buffer-window "*Substitution History*"))
  9459. (shrink-window-if-larger-than-buffer bw)
  9460. (unless (and (interactive-p) (not ndown))
  9461. (unless (let (inhibit-redisplay)
  9462. (y-or-n-p "Debugging Formula. Continue to next? "))
  9463. (org-table-align)
  9464. (error "Abort"))
  9465. (delete-window bw)
  9466. (message "")))
  9467. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9468. (org-table-justify-field-maybe
  9469. (if fmt (format fmt (string-to-number ev)) ev))
  9470. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9471. (call-interactively 'org-return)
  9472. (setq ndown 0)))
  9473. (and down (org-table-maybe-recalculate-line))
  9474. (or suppress-align (and org-table-may-need-update
  9475. (org-table-align))))))
  9476. (defun org-table-put-field-property (prop value)
  9477. (save-excursion
  9478. (put-text-property (progn (skip-chars-backward "^|") (point))
  9479. (progn (skip-chars-forward "^|") (point))
  9480. prop value)))
  9481. (defun org-table-get-range (desc &optional tbeg col highlight)
  9482. "Get a calc vector from a column, accorting to descriptor DESC.
  9483. Optional arguments TBEG and COL can give the beginning of the table and
  9484. the current column, to avoid unnecessary parsing.
  9485. HIGHLIGHT means, just highlight the range."
  9486. (if (not (equal (string-to-char desc) ?@))
  9487. (setq desc (concat "@" desc)))
  9488. (save-excursion
  9489. (or tbeg (setq tbeg (org-table-begin)))
  9490. (or col (setq col (org-table-current-column)))
  9491. (let ((thisline (org-current-line))
  9492. beg end c1 c2 r1 r2 rangep tmp)
  9493. (unless (string-match org-table-range-regexp desc)
  9494. (error "Invalid table range specifier `%s'" desc))
  9495. (setq rangep (match-end 3)
  9496. r1 (and (match-end 1) (match-string 1 desc))
  9497. r2 (and (match-end 4) (match-string 4 desc))
  9498. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9499. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9500. (and c1 (setq c1 (+ (string-to-number c1)
  9501. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9502. (and c2 (setq c2 (+ (string-to-number c2)
  9503. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9504. (if (equal r1 "") (setq r1 nil))
  9505. (if (equal r2 "") (setq r2 nil))
  9506. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9507. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9508. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9509. (if (not r1) (setq r1 thisline))
  9510. (if (not r2) (setq r2 thisline))
  9511. (if (not c1) (setq c1 col))
  9512. (if (not c2) (setq c2 col))
  9513. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9514. ;; just one field
  9515. (progn
  9516. (goto-line r1)
  9517. (while (not (looking-at org-table-dataline-regexp))
  9518. (beginning-of-line 2))
  9519. (prog1 (org-trim (org-table-get-field c1))
  9520. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9521. ;; A range, return a vector
  9522. ;; First sort the numbers to get a regular ractangle
  9523. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9524. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9525. (goto-line r1)
  9526. (while (not (looking-at org-table-dataline-regexp))
  9527. (beginning-of-line 2))
  9528. (org-table-goto-column c1)
  9529. (setq beg (point))
  9530. (goto-line r2)
  9531. (while (not (looking-at org-table-dataline-regexp))
  9532. (beginning-of-line 0))
  9533. (org-table-goto-column c2)
  9534. (setq end (point))
  9535. (if highlight
  9536. (org-table-highlight-rectangle
  9537. beg (progn (skip-chars-forward "^|\n") (point))))
  9538. ;; return string representation of calc vector
  9539. (mapcar 'org-trim
  9540. (apply 'append (org-table-copy-region beg end)))))))
  9541. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9542. "Analyze descriptor DESC and retrieve the corresponding line number.
  9543. The cursor is currently in line CLINE, the table begins in line BLINE,
  9544. and TABLE is a vector with line types."
  9545. (if (string-match "^[0-9]+$" desc)
  9546. (aref org-table-dlines (string-to-number desc))
  9547. (setq cline (or cline (org-current-line))
  9548. bline (or bline org-table-current-begin-line)
  9549. table (or table org-table-current-line-types))
  9550. (if (or
  9551. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9552. ;; 1 2 3 4 5 6
  9553. (and (not (match-end 3)) (not (match-end 6)))
  9554. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9555. (error "invalid row descriptor `%s'" desc))
  9556. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9557. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9558. (odir (and (match-end 5) (match-string 5 desc)))
  9559. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9560. (i (- cline bline))
  9561. (rel (and (match-end 6)
  9562. (or (and (match-end 1) (not (match-end 3)))
  9563. (match-end 5)))))
  9564. (if (and hn (not hdir))
  9565. (progn
  9566. (setq i 0 hdir "+")
  9567. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9568. (if (and (not hn) on (not odir))
  9569. (error "should never happen");;(aref org-table-dlines on)
  9570. (if (and hn (> hn 0))
  9571. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9572. (if on
  9573. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9574. (+ bline i)))))
  9575. (defun org-find-row-type (table i type backwards relative n)
  9576. (let ((l (length table)))
  9577. (while (> n 0)
  9578. (while (and (setq i (+ i (if backwards -1 1)))
  9579. (>= i 0) (< i l)
  9580. (not (eq (aref table i) type))
  9581. (if (and relative (eq (aref table i) 'hline))
  9582. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9583. t)))
  9584. (setq n (1- n)))
  9585. (if (or (< i 0) (>= i l))
  9586. (error "Row descriptor leads outside table")
  9587. i)))
  9588. (defun org-rewrite-old-row-references (s)
  9589. (if (string-match "&[-+0-9I]" s)
  9590. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9591. s))
  9592. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9593. "Convert list ELEMENTS to something appropriate to insert into formula.
  9594. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9595. NUMBERS indicates that everything should be converted to numbers.
  9596. LISPP means to return something appropriate for a Lisp list."
  9597. (if (stringp elements) ; just a single val
  9598. (if lispp
  9599. (if (eq lispp 'literal)
  9600. elements
  9601. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9602. (if (equal elements "") (setq elements "0"))
  9603. (if numbers (number-to-string (string-to-number elements)) elements))
  9604. (unless keep-empty
  9605. (setq elements
  9606. (delq nil
  9607. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9608. elements))))
  9609. (setq elements (or elements '("0")))
  9610. (if lispp
  9611. (mapconcat
  9612. (lambda (x)
  9613. (if (eq lispp 'literal)
  9614. x
  9615. (prin1-to-string (if numbers (string-to-number x) x))))
  9616. elements " ")
  9617. (concat "[" (mapconcat
  9618. (lambda (x)
  9619. (if numbers (number-to-string (string-to-number x)) x))
  9620. elements
  9621. ",") "]"))))
  9622. (defun org-table-recalculate (&optional all noalign)
  9623. "Recalculate the current table line by applying all stored formulas.
  9624. With prefix arg ALL, do this for all lines in the table."
  9625. (interactive "P")
  9626. (or (memq this-command org-recalc-commands)
  9627. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9628. (unless (org-at-table-p) (error "Not at a table"))
  9629. (if (equal all '(16))
  9630. (org-table-iterate)
  9631. (org-table-get-specials)
  9632. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9633. (lambda (a b) (string< (car a) (car b)))))
  9634. (inhibit-redisplay (not debug-on-error))
  9635. (line-re org-table-dataline-regexp)
  9636. (thisline (org-current-line))
  9637. (thiscol (org-table-current-column))
  9638. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9639. ;; Insert constants in all formulas
  9640. (setq eqlist
  9641. (mapcar (lambda (x)
  9642. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9643. x)
  9644. eqlist))
  9645. ;; Split the equation list
  9646. (while (setq eq (pop eqlist))
  9647. (if (<= (string-to-char (car eq)) ?9)
  9648. (push eq eqlnum)
  9649. (push eq eqlname)))
  9650. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9651. (if all
  9652. (progn
  9653. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9654. (goto-char (setq beg (org-table-begin)))
  9655. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9656. ;; This is a table with marked lines, compute selected lines
  9657. (setq line-re org-table-recalculate-regexp)
  9658. ;; Move forward to the first non-header line
  9659. (if (and (re-search-forward org-table-dataline-regexp end t)
  9660. (re-search-forward org-table-hline-regexp end t)
  9661. (re-search-forward org-table-dataline-regexp end t))
  9662. (setq beg (match-beginning 0))
  9663. nil))) ;; just leave beg where it is
  9664. (setq beg (point-at-bol)
  9665. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9666. (goto-char beg)
  9667. (and all (message "Re-applying formulas to full table..."))
  9668. ;; First find the named fields, and mark them untouchanble
  9669. (remove-text-properties beg end '(org-untouchable t))
  9670. (while (setq eq (pop eqlname))
  9671. (setq name (car eq)
  9672. a (assoc name org-table-named-field-locations))
  9673. (and (not a)
  9674. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9675. (setq a (list name
  9676. (aref org-table-dlines
  9677. (string-to-number (match-string 1 name)))
  9678. (string-to-number (match-string 2 name)))))
  9679. (when (and a (or all (equal (nth 1 a) thisline)))
  9680. (message "Re-applying formula to field: %s" name)
  9681. (goto-line (nth 1 a))
  9682. (org-table-goto-column (nth 2 a))
  9683. (push (append a (list (cdr eq))) eqlname1)
  9684. (org-table-put-field-property :org-untouchable t)))
  9685. ;; Now evauluate the column formulas, but skip fields covered by
  9686. ;; field formulas
  9687. (goto-char beg)
  9688. (while (re-search-forward line-re end t)
  9689. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9690. ;; Unprotected line, recalculate
  9691. (and all (message "Re-applying formulas to full table...(line %d)"
  9692. (setq cnt (1+ cnt))))
  9693. (setq org-last-recalc-line (org-current-line))
  9694. (setq eql eqlnum)
  9695. (while (setq entry (pop eql))
  9696. (goto-line org-last-recalc-line)
  9697. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9698. (unless (get-text-property (point) :org-untouchable)
  9699. (org-table-eval-formula nil (cdr entry)
  9700. 'noalign 'nocst 'nostore 'noanalysis)))))
  9701. ;; Now evaluate the field formulas
  9702. (while (setq eq (pop eqlname1))
  9703. (message "Re-applying formula to field: %s" (car eq))
  9704. (goto-line (nth 1 eq))
  9705. (org-table-goto-column (nth 2 eq))
  9706. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9707. 'nostore 'noanalysis))
  9708. (goto-line thisline)
  9709. (org-table-goto-column thiscol)
  9710. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9711. (or noalign (and org-table-may-need-update (org-table-align))
  9712. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9713. ;; back to initial position
  9714. (message "Re-applying formulas...done")
  9715. (goto-line thisline)
  9716. (org-table-goto-column thiscol)
  9717. (or noalign (and org-table-may-need-update (org-table-align))
  9718. (and all (message "Re-applying formulas...done"))))))
  9719. (defun org-table-iterate (&optional arg)
  9720. "Recalculate the table until it does not change anymore."
  9721. (interactive "P")
  9722. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9723. (i 0)
  9724. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9725. thistbl)
  9726. (catch 'exit
  9727. (while (< i imax)
  9728. (setq i (1+ i))
  9729. (org-table-recalculate 'all)
  9730. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9731. (if (not (string= lasttbl thistbl))
  9732. (setq lasttbl thistbl)
  9733. (if (> i 1)
  9734. (message "Convergence after %d iterations" i)
  9735. (message "Table was already stable"))
  9736. (throw 'exit t)))
  9737. (error "No convergence after %d iterations" i))))
  9738. (defun org-table-formula-substitute-names (f)
  9739. "Replace $const with values in string F."
  9740. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9741. ;; First, check for column names
  9742. (while (setq start (string-match org-table-column-name-regexp f start))
  9743. (setq start (1+ start))
  9744. (setq a (assoc (match-string 1 f) org-table-column-names))
  9745. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9746. ;; Parameters and constants
  9747. (setq start 0)
  9748. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9749. (setq start (1+ start))
  9750. (if (setq a (save-match-data
  9751. (org-table-get-constant (match-string 1 f))))
  9752. (setq f (replace-match
  9753. (concat (if pp "(") a (if pp ")")) t t f))))
  9754. (if org-table-formula-debug
  9755. (put-text-property 0 (length f) :orig-formula f1 f))
  9756. f))
  9757. (defun org-table-get-constant (const)
  9758. "Find the value for a parameter or constant in a formula.
  9759. Parameters get priority."
  9760. (or (cdr (assoc const org-table-local-parameters))
  9761. (cdr (assoc const org-table-formula-constants-local))
  9762. (cdr (assoc const org-table-formula-constants))
  9763. (and (fboundp 'constants-get) (constants-get const))
  9764. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9765. (org-entry-get nil (substring const 5) 'inherit))
  9766. "#UNDEFINED_NAME"))
  9767. (defvar org-table-fedit-map
  9768. (let ((map (make-sparse-keymap)))
  9769. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9770. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9771. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9772. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9773. (org-defkey map "\C-c?" 'org-table-show-reference)
  9774. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9775. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9776. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9777. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9778. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9779. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9780. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9781. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9782. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9783. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9784. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9785. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9786. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9787. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9788. map))
  9789. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9790. '("Edit-Formulas"
  9791. ["Finish and Install" org-table-fedit-finish t]
  9792. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9793. ["Abort" org-table-fedit-abort t]
  9794. "--"
  9795. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9796. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9797. "--"
  9798. "Shift Reference at Point"
  9799. ["Up" org-table-fedit-ref-up t]
  9800. ["Down" org-table-fedit-ref-down t]
  9801. ["Left" org-table-fedit-ref-left t]
  9802. ["Right" org-table-fedit-ref-right t]
  9803. "-"
  9804. "Change Test Row for Column Formulas"
  9805. ["Up" org-table-fedit-line-up t]
  9806. ["Down" org-table-fedit-line-down t]
  9807. "--"
  9808. ["Scroll Table Window" org-table-fedit-scroll t]
  9809. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9810. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9811. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9812. org-table-overlay-coordinates)]
  9813. "--"
  9814. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9815. :style toggle :selected org-table-buffer-is-an]))
  9816. (defvar org-pos)
  9817. (defun org-table-edit-formulas ()
  9818. "Edit the formulas of the current table in a separate buffer."
  9819. (interactive)
  9820. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9821. (beginning-of-line 0))
  9822. (unless (org-at-table-p) (error "Not at a table"))
  9823. (org-table-get-specials)
  9824. (let ((key (org-table-current-field-formula 'key 'noerror))
  9825. (eql (sort (org-table-get-stored-formulas 'noerror)
  9826. 'org-table-formula-less-p))
  9827. (pos (move-marker (make-marker) (point)))
  9828. (startline 1)
  9829. (wc (current-window-configuration))
  9830. (titles '((column . "# Column Formulas\n")
  9831. (field . "# Field Formulas\n")
  9832. (named . "# Named Field Formulas\n")))
  9833. entry s type title)
  9834. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9835. (erase-buffer)
  9836. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9837. (let ((font-lock-global-modes '(not fundamental-mode)))
  9838. (fundamental-mode))
  9839. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9840. (org-set-local 'org-pos pos)
  9841. (org-set-local 'org-window-configuration wc)
  9842. (use-local-map org-table-fedit-map)
  9843. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9844. (easy-menu-add org-table-fedit-menu)
  9845. (setq startline (org-current-line))
  9846. (while (setq entry (pop eql))
  9847. (setq type (cond
  9848. ((equal (string-to-char (car entry)) ?@) 'field)
  9849. ((string-match "^[0-9]" (car entry)) 'column)
  9850. (t 'named)))
  9851. (when (setq title (assq type titles))
  9852. (or (bobp) (insert "\n"))
  9853. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9854. (setq titles (delq title titles)))
  9855. (if (equal key (car entry)) (setq startline (org-current-line)))
  9856. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9857. (car entry) " = " (cdr entry) "\n"))
  9858. (remove-text-properties 0 (length s) '(face nil) s)
  9859. (insert s))
  9860. (if (eq org-table-use-standard-references t)
  9861. (org-table-fedit-toggle-ref-type))
  9862. (goto-line startline)
  9863. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9864. (defun org-table-fedit-post-command ()
  9865. (when (not (memq this-command '(lisp-complete-symbol)))
  9866. (let ((win (selected-window)))
  9867. (save-excursion
  9868. (condition-case nil
  9869. (org-table-show-reference)
  9870. (error nil))
  9871. (select-window win)))))
  9872. (defun org-table-formula-to-user (s)
  9873. "Convert a formula from internal to user representation."
  9874. (if (eq org-table-use-standard-references t)
  9875. (org-table-convert-refs-to-an s)
  9876. s))
  9877. (defun org-table-formula-from-user (s)
  9878. "Convert a formula from user to internal representation."
  9879. (if org-table-use-standard-references
  9880. (org-table-convert-refs-to-rc s)
  9881. s))
  9882. (defun org-table-convert-refs-to-rc (s)
  9883. "Convert spreadsheet references from AB7 to @7$28.
  9884. Works for single references, but also for entire formulas and even the
  9885. full TBLFM line."
  9886. (let ((start 0))
  9887. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9888. (cond
  9889. ((match-end 3)
  9890. ;; format match, just advance
  9891. (setq start (match-end 0)))
  9892. ((and (> (match-beginning 0) 0)
  9893. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9894. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9895. ;; 3.e5 or something like this.
  9896. (setq start (match-end 0)))
  9897. (t
  9898. (setq start (match-beginning 0)
  9899. s (replace-match
  9900. (if (equal (match-string 2 s) "&")
  9901. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9902. (format "@%d$%d"
  9903. (string-to-number (match-string 2 s))
  9904. (org-letters-to-number (match-string 1 s))))
  9905. t t s)))))
  9906. s))
  9907. (defun org-table-convert-refs-to-an (s)
  9908. "Convert spreadsheet references from to @7$28 to AB7.
  9909. Works for single references, but also for entire formulas and even the
  9910. full TBLFM line."
  9911. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9912. (setq s (replace-match
  9913. (format "%s%d"
  9914. (org-number-to-letters
  9915. (string-to-number (match-string 2 s)))
  9916. (string-to-number (match-string 1 s)))
  9917. t t s)))
  9918. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9919. (setq s (replace-match (concat "\\1"
  9920. (org-number-to-letters
  9921. (string-to-number (match-string 2 s))) "&")
  9922. t nil s)))
  9923. s)
  9924. (defun org-letters-to-number (s)
  9925. "Convert a base 26 number represented by letters into an integer.
  9926. For example: AB -> 28."
  9927. (let ((n 0))
  9928. (setq s (upcase s))
  9929. (while (> (length s) 0)
  9930. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9931. s (substring s 1)))
  9932. n))
  9933. (defun org-number-to-letters (n)
  9934. "Convert an integer into a base 26 number represented by letters.
  9935. For example: 28 -> AB."
  9936. (let ((s ""))
  9937. (while (> n 0)
  9938. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9939. n (/ (1- n) 26)))
  9940. s))
  9941. (defun org-table-fedit-convert-buffer (function)
  9942. "Convert all references in this buffer, using FUNTION."
  9943. (let ((line (org-current-line)))
  9944. (goto-char (point-min))
  9945. (while (not (eobp))
  9946. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9947. (delete-region (point) (point-at-eol))
  9948. (or (eobp) (forward-char 1)))
  9949. (goto-line line)))
  9950. (defun org-table-fedit-toggle-ref-type ()
  9951. "Convert all references in the buffer from B3 to @3$2 and back."
  9952. (interactive)
  9953. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9954. (org-table-fedit-convert-buffer
  9955. (if org-table-buffer-is-an
  9956. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9957. (message "Reference type switched to %s"
  9958. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9959. (defun org-table-fedit-ref-up ()
  9960. "Shift the reference at point one row/hline up."
  9961. (interactive)
  9962. (org-table-fedit-shift-reference 'up))
  9963. (defun org-table-fedit-ref-down ()
  9964. "Shift the reference at point one row/hline down."
  9965. (interactive)
  9966. (org-table-fedit-shift-reference 'down))
  9967. (defun org-table-fedit-ref-left ()
  9968. "Shift the reference at point one field to the left."
  9969. (interactive)
  9970. (org-table-fedit-shift-reference 'left))
  9971. (defun org-table-fedit-ref-right ()
  9972. "Shift the reference at point one field to the right."
  9973. (interactive)
  9974. (org-table-fedit-shift-reference 'right))
  9975. (defun org-table-fedit-shift-reference (dir)
  9976. (cond
  9977. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9978. (if (memq dir '(left right))
  9979. (org-rematch-and-replace 1 (eq dir 'left))
  9980. (error "Cannot shift reference in this direction")))
  9981. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9982. ;; A B3-like reference
  9983. (if (memq dir '(up down))
  9984. (org-rematch-and-replace 2 (eq dir 'up))
  9985. (org-rematch-and-replace 1 (eq dir 'left))))
  9986. ((org-at-regexp-p
  9987. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9988. ;; An internal reference
  9989. (if (memq dir '(up down))
  9990. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9991. (org-rematch-and-replace 5 (eq dir 'left))))))
  9992. (defun org-rematch-and-replace (n &optional decr hline)
  9993. "Re-match the group N, and replace it with the shifted refrence."
  9994. (or (match-end n) (error "Cannot shift reference in this direction"))
  9995. (goto-char (match-beginning n))
  9996. (and (looking-at (regexp-quote (match-string n)))
  9997. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9998. t t)))
  9999. (defun org-shift-refpart (ref &optional decr hline)
  10000. "Shift a refrence part REF.
  10001. If DECR is set, decrease the references row/column, else increase.
  10002. If HLINE is set, this may be a hline reference, it certainly is not
  10003. a translation reference."
  10004. (save-match-data
  10005. (let* ((sign (string-match "^[-+]" ref)) n)
  10006. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  10007. (cond
  10008. ((and hline (string-match "^I+" ref))
  10009. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  10010. (setq n (+ n (if decr -1 1)))
  10011. (if (= n 0) (setq n (+ n (if decr -1 1))))
  10012. (if sign
  10013. (setq sign (if (< n 0) "-" "+") n (abs n))
  10014. (setq n (max 1 n)))
  10015. (concat sign (make-string n ?I)))
  10016. ((string-match "^[0-9]+" ref)
  10017. (setq n (string-to-number (concat sign ref)))
  10018. (setq n (+ n (if decr -1 1)))
  10019. (if sign
  10020. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  10021. (number-to-string (max 1 n))))
  10022. ((string-match "^[a-zA-Z]+" ref)
  10023. (org-number-to-letters
  10024. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  10025. (t (error "Cannot shift reference"))))))
  10026. (defun org-table-fedit-toggle-coordinates ()
  10027. "Toggle the display of coordinates in the refrenced table."
  10028. (interactive)
  10029. (let ((pos (marker-position org-pos)))
  10030. (with-current-buffer (marker-buffer org-pos)
  10031. (save-excursion
  10032. (goto-char pos)
  10033. (org-table-toggle-coordinate-overlays)))))
  10034. (defun org-table-fedit-finish (&optional arg)
  10035. "Parse the buffer for formula definitions and install them.
  10036. With prefix ARG, apply the new formulas to the table."
  10037. (interactive "P")
  10038. (org-table-remove-rectangle-highlight)
  10039. (if org-table-use-standard-references
  10040. (progn
  10041. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  10042. (setq org-table-buffer-is-an nil)))
  10043. (let ((pos org-pos) eql var form)
  10044. (goto-char (point-min))
  10045. (while (re-search-forward
  10046. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  10047. nil t)
  10048. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  10049. form (match-string 3))
  10050. (setq form (org-trim form))
  10051. (when (not (equal form ""))
  10052. (while (string-match "[ \t]*\n[ \t]*" form)
  10053. (setq form (replace-match " " t t form)))
  10054. (when (assoc var eql)
  10055. (error "Double formulas for %s" var))
  10056. (push (cons var form) eql)))
  10057. (setq org-pos nil)
  10058. (set-window-configuration org-window-configuration)
  10059. (select-window (get-buffer-window (marker-buffer pos)))
  10060. (goto-char pos)
  10061. (unless (org-at-table-p)
  10062. (error "Lost table position - cannot install formulae"))
  10063. (org-table-store-formulas eql)
  10064. (move-marker pos nil)
  10065. (kill-buffer "*Edit Formulas*")
  10066. (if arg
  10067. (org-table-recalculate 'all)
  10068. (message "New formulas installed - press C-u C-c C-c to apply."))))
  10069. (defun org-table-fedit-abort ()
  10070. "Abort editing formulas, without installing the changes."
  10071. (interactive)
  10072. (org-table-remove-rectangle-highlight)
  10073. (let ((pos org-pos))
  10074. (set-window-configuration org-window-configuration)
  10075. (select-window (get-buffer-window (marker-buffer pos)))
  10076. (goto-char pos)
  10077. (move-marker pos nil)
  10078. (message "Formula editing aborted without installing changes")))
  10079. (defun org-table-fedit-lisp-indent ()
  10080. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  10081. (interactive)
  10082. (let ((pos (point)) beg end ind)
  10083. (beginning-of-line 1)
  10084. (cond
  10085. ((looking-at "[ \t]")
  10086. (goto-char pos)
  10087. (call-interactively 'lisp-indent-line))
  10088. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  10089. ((not (fboundp 'pp-buffer))
  10090. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  10091. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  10092. (goto-char (- (match-end 0) 2))
  10093. (setq beg (point))
  10094. (setq ind (make-string (current-column) ?\ ))
  10095. (condition-case nil (forward-sexp 1)
  10096. (error
  10097. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10098. (setq end (point))
  10099. (save-restriction
  10100. (narrow-to-region beg end)
  10101. (if (eq last-command this-command)
  10102. (progn
  10103. (goto-char (point-min))
  10104. (setq this-command nil)
  10105. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10106. (replace-match " ")))
  10107. (pp-buffer)
  10108. (untabify (point-min) (point-max))
  10109. (goto-char (1+ (point-min)))
  10110. (while (re-search-forward "^." nil t)
  10111. (beginning-of-line 1)
  10112. (insert ind))
  10113. (goto-char (point-max))
  10114. (backward-delete-char 1)))
  10115. (goto-char beg))
  10116. (t nil))))
  10117. (defvar org-show-positions nil)
  10118. (defun org-table-show-reference (&optional local)
  10119. "Show the location/value of the $ expression at point."
  10120. (interactive)
  10121. (org-table-remove-rectangle-highlight)
  10122. (catch 'exit
  10123. (let ((pos (if local (point) org-pos))
  10124. (face2 'highlight)
  10125. (org-inhibit-highlight-removal t)
  10126. (win (selected-window))
  10127. (org-show-positions nil)
  10128. var name e what match dest)
  10129. (if local (org-table-get-specials))
  10130. (setq what (cond
  10131. ((or (org-at-regexp-p org-table-range-regexp2)
  10132. (org-at-regexp-p org-table-translate-regexp)
  10133. (org-at-regexp-p org-table-range-regexp))
  10134. (setq match
  10135. (save-match-data
  10136. (org-table-convert-refs-to-rc (match-string 0))))
  10137. 'range)
  10138. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10139. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10140. ((not local) nil)
  10141. (t (error "No reference at point")))
  10142. match (and what (or match (match-string 0))))
  10143. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10144. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10145. 'secondary-selection))
  10146. (org-add-hook 'before-change-functions
  10147. 'org-table-remove-rectangle-highlight)
  10148. (if (eq what 'name) (setq var (substring match 1)))
  10149. (when (eq what 'range)
  10150. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10151. (setq match (org-table-formula-substitute-names match)))
  10152. (unless local
  10153. (save-excursion
  10154. (end-of-line 1)
  10155. (re-search-backward "^\\S-" nil t)
  10156. (beginning-of-line 1)
  10157. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10158. (setq dest
  10159. (save-match-data
  10160. (org-table-convert-refs-to-rc (match-string 1))))
  10161. (org-table-add-rectangle-overlay
  10162. (match-beginning 1) (match-end 1) face2))))
  10163. (if (and (markerp pos) (marker-buffer pos))
  10164. (if (get-buffer-window (marker-buffer pos))
  10165. (select-window (get-buffer-window (marker-buffer pos)))
  10166. (org-switch-to-buffer-other-window (get-buffer-window
  10167. (marker-buffer pos)))))
  10168. (goto-char pos)
  10169. (org-table-force-dataline)
  10170. (when dest
  10171. (setq name (substring dest 1))
  10172. (cond
  10173. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10174. (setq e (assoc name org-table-named-field-locations))
  10175. (goto-line (nth 1 e))
  10176. (org-table-goto-column (nth 2 e)))
  10177. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10178. (let ((l (string-to-number (match-string 1 dest)))
  10179. (c (string-to-number (match-string 2 dest))))
  10180. (goto-line (aref org-table-dlines l))
  10181. (org-table-goto-column c)))
  10182. (t (org-table-goto-column (string-to-number name))))
  10183. (move-marker pos (point))
  10184. (org-table-highlight-rectangle nil nil face2))
  10185. (cond
  10186. ((equal dest match))
  10187. ((not match))
  10188. ((eq what 'range)
  10189. (condition-case nil
  10190. (save-excursion
  10191. (org-table-get-range match nil nil 'highlight))
  10192. (error nil)))
  10193. ((setq e (assoc var org-table-named-field-locations))
  10194. (goto-line (nth 1 e))
  10195. (org-table-goto-column (nth 2 e))
  10196. (org-table-highlight-rectangle (point) (point))
  10197. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10198. ((setq e (assoc var org-table-column-names))
  10199. (org-table-goto-column (string-to-number (cdr e)))
  10200. (org-table-highlight-rectangle (point) (point))
  10201. (goto-char (org-table-begin))
  10202. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10203. (org-table-end) t)
  10204. (progn
  10205. (goto-char (match-beginning 1))
  10206. (org-table-highlight-rectangle)
  10207. (message "Named column (column %s)" (cdr e)))
  10208. (error "Column name not found")))
  10209. ((eq what 'column)
  10210. ;; column number
  10211. (org-table-goto-column (string-to-number (substring match 1)))
  10212. (org-table-highlight-rectangle (point) (point))
  10213. (message "Column %s" (substring match 1)))
  10214. ((setq e (assoc var org-table-local-parameters))
  10215. (goto-char (org-table-begin))
  10216. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10217. (progn
  10218. (goto-char (match-beginning 1))
  10219. (org-table-highlight-rectangle)
  10220. (message "Local parameter."))
  10221. (error "Parameter not found")))
  10222. (t
  10223. (cond
  10224. ((not var) (error "No reference at point"))
  10225. ((setq e (assoc var org-table-formula-constants-local))
  10226. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10227. var (cdr e)))
  10228. ((setq e (assoc var org-table-formula-constants))
  10229. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10230. var (cdr e)))
  10231. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10232. (message "Constant: $%s=%s, from `constants.el'%s."
  10233. var e (format " (%s units)" constants-unit-system)))
  10234. (t (error "Undefined name $%s" var)))))
  10235. (goto-char pos)
  10236. (when (and org-show-positions
  10237. (not (memq this-command '(org-table-fedit-scroll
  10238. org-table-fedit-scroll-down))))
  10239. (push pos org-show-positions)
  10240. (push org-table-current-begin-pos org-show-positions)
  10241. (let ((min (apply 'min org-show-positions))
  10242. (max (apply 'max org-show-positions)))
  10243. (goto-char min) (recenter 0)
  10244. (goto-char max)
  10245. (or (pos-visible-in-window-p max) (recenter -1))))
  10246. (select-window win))))
  10247. (defun org-table-force-dataline ()
  10248. "Make sure the cursor is in a dataline in a table."
  10249. (unless (save-excursion
  10250. (beginning-of-line 1)
  10251. (looking-at org-table-dataline-regexp))
  10252. (let* ((re org-table-dataline-regexp)
  10253. (p1 (save-excursion (re-search-forward re nil 'move)))
  10254. (p2 (save-excursion (re-search-backward re nil 'move))))
  10255. (cond ((and p1 p2)
  10256. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10257. p1 p2)))
  10258. ((or p1 p2) (goto-char (or p1 p2)))
  10259. (t (error "No table dataline around here"))))))
  10260. (defun org-table-fedit-line-up ()
  10261. "Move cursor one line up in the window showing the table."
  10262. (interactive)
  10263. (org-table-fedit-move 'previous-line))
  10264. (defun org-table-fedit-line-down ()
  10265. "Move cursor one line down in the window showing the table."
  10266. (interactive)
  10267. (org-table-fedit-move 'next-line))
  10268. (defun org-table-fedit-move (command)
  10269. "Move the cursor in the window shoinw the table.
  10270. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10271. (let ((org-table-allow-automatic-line-recalculation nil)
  10272. (pos org-pos) (win (selected-window)) p)
  10273. (select-window (get-buffer-window (marker-buffer org-pos)))
  10274. (setq p (point))
  10275. (call-interactively command)
  10276. (while (and (org-at-table-p)
  10277. (org-at-table-hline-p))
  10278. (call-interactively command))
  10279. (or (org-at-table-p) (goto-char p))
  10280. (move-marker pos (point))
  10281. (select-window win)))
  10282. (defun org-table-fedit-scroll (N)
  10283. (interactive "p")
  10284. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10285. (scroll-other-window N)))
  10286. (defun org-table-fedit-scroll-down (N)
  10287. (interactive "p")
  10288. (org-table-fedit-scroll (- N)))
  10289. (defvar org-table-rectangle-overlays nil)
  10290. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10291. "Add a new overlay."
  10292. (let ((ov (org-make-overlay beg end)))
  10293. (org-overlay-put ov 'face (or face 'secondary-selection))
  10294. (push ov org-table-rectangle-overlays)))
  10295. (defun org-table-highlight-rectangle (&optional beg end face)
  10296. "Highlight rectangular region in a table."
  10297. (setq beg (or beg (point)) end (or end (point)))
  10298. (let ((b (min beg end))
  10299. (e (max beg end))
  10300. l1 c1 l2 c2 tmp)
  10301. (and (boundp 'org-show-positions)
  10302. (setq org-show-positions (cons b (cons e org-show-positions))))
  10303. (goto-char (min beg end))
  10304. (setq l1 (org-current-line)
  10305. c1 (org-table-current-column))
  10306. (goto-char (max beg end))
  10307. (setq l2 (org-current-line)
  10308. c2 (org-table-current-column))
  10309. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10310. (goto-line l1)
  10311. (beginning-of-line 1)
  10312. (loop for line from l1 to l2 do
  10313. (when (looking-at org-table-dataline-regexp)
  10314. (org-table-goto-column c1)
  10315. (skip-chars-backward "^|\n") (setq beg (point))
  10316. (org-table-goto-column c2)
  10317. (skip-chars-forward "^|\n") (setq end (point))
  10318. (org-table-add-rectangle-overlay beg end face))
  10319. (beginning-of-line 2))
  10320. (goto-char b))
  10321. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10322. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10323. "Remove the rectangle overlays."
  10324. (unless org-inhibit-highlight-removal
  10325. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10326. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10327. (setq org-table-rectangle-overlays nil)))
  10328. (defvar org-table-coordinate-overlays nil
  10329. "Collects the cooordinate grid overlays, so that they can be removed.")
  10330. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10331. (defun org-table-overlay-coordinates ()
  10332. "Add overlays to the table at point, to show row/column coordinates."
  10333. (interactive)
  10334. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10335. (setq org-table-coordinate-overlays nil)
  10336. (save-excursion
  10337. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10338. (goto-char (org-table-begin))
  10339. (while (org-at-table-p)
  10340. (setq eol (point-at-eol))
  10341. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10342. (push ov org-table-coordinate-overlays)
  10343. (setq hline (looking-at org-table-hline-regexp))
  10344. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10345. (format "%4d" (setq id (1+ id)))))
  10346. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10347. (when hline
  10348. (setq ic 0)
  10349. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10350. (setq beg (1+ (match-beginning 0))
  10351. ic (1+ ic)
  10352. s1 (concat "$" (int-to-string ic))
  10353. s2 (org-number-to-letters ic)
  10354. str (if (eq org-table-use-standard-references t) s2 s1))
  10355. (setq ov (org-make-overlay beg (+ beg (length str))))
  10356. (push ov org-table-coordinate-overlays)
  10357. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10358. (beginning-of-line 2)))))
  10359. (defun org-table-toggle-coordinate-overlays ()
  10360. "Toggle the display of Row/Column numbers in tables."
  10361. (interactive)
  10362. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10363. (message "Row/Column number display turned %s"
  10364. (if org-table-overlay-coordinates "on" "off"))
  10365. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10366. (org-table-align))
  10367. (unless org-table-overlay-coordinates
  10368. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10369. (setq org-table-coordinate-overlays nil)))
  10370. (defun org-table-toggle-formula-debugger ()
  10371. "Toggle the formula debugger in tables."
  10372. (interactive)
  10373. (setq org-table-formula-debug (not org-table-formula-debug))
  10374. (message "Formula debugging has been turned %s"
  10375. (if org-table-formula-debug "on" "off")))
  10376. ;;; The orgtbl minor mode
  10377. ;; Define a minor mode which can be used in other modes in order to
  10378. ;; integrate the org-mode table editor.
  10379. ;; This is really a hack, because the org-mode table editor uses several
  10380. ;; keys which normally belong to the major mode, for example the TAB and
  10381. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10382. ;; necessary to operate the table editor, but wraps the commands into a
  10383. ;; function which tests if the cursor is currently inside a table. If that
  10384. ;; is the case, the table editor command is executed. However, when any of
  10385. ;; those keys is used outside a table, the function uses `key-binding' to
  10386. ;; look up if the key has an associated command in another currently active
  10387. ;; keymap (minor modes, major mode, global), and executes that command.
  10388. ;; There might be problems if any of the keys used by the table editor is
  10389. ;; otherwise used as a prefix key.
  10390. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10391. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10392. ;; addresses this by checking explicitly for both bindings.
  10393. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10394. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10395. ;; Some modes bind other commands to simple characters, for example
  10396. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10397. ;; active, this binding is ignored inside tables and replaced with a
  10398. ;; modified self-insert.
  10399. (defvar orgtbl-mode nil
  10400. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10401. table editor in arbitrary modes.")
  10402. (make-variable-buffer-local 'orgtbl-mode)
  10403. (defvar orgtbl-mode-map (make-keymap)
  10404. "Keymap for `orgtbl-mode'.")
  10405. ;;;###autoload
  10406. (defun turn-on-orgtbl ()
  10407. "Unconditionally turn on `orgtbl-mode'."
  10408. (orgtbl-mode 1))
  10409. (defvar org-old-auto-fill-inhibit-regexp nil
  10410. "Local variable used by `orgtbl-mode'")
  10411. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10412. "Matches a line belonging to an orgtbl.")
  10413. (defconst orgtbl-extra-font-lock-keywords
  10414. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10415. 0 (quote 'org-table) 'prepend))
  10416. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10417. ;;;###autoload
  10418. (defun orgtbl-mode (&optional arg)
  10419. "The `org-mode' table editor as a minor mode for use in other modes."
  10420. (interactive)
  10421. (org-load-modules-maybe)
  10422. (if (org-mode-p)
  10423. ;; Exit without error, in case some hook functions calls this
  10424. ;; by accident in org-mode.
  10425. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10426. (setq orgtbl-mode
  10427. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10428. (if orgtbl-mode
  10429. (progn
  10430. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10431. ;; Make sure we are first in minor-mode-map-alist
  10432. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10433. (and c (setq minor-mode-map-alist
  10434. (cons c (delq c minor-mode-map-alist)))))
  10435. (org-set-local (quote org-table-may-need-update) t)
  10436. (org-add-hook 'before-change-functions 'org-before-change-function
  10437. nil 'local)
  10438. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10439. auto-fill-inhibit-regexp)
  10440. (org-set-local 'auto-fill-inhibit-regexp
  10441. (if auto-fill-inhibit-regexp
  10442. (concat orgtbl-line-start-regexp "\\|"
  10443. auto-fill-inhibit-regexp)
  10444. orgtbl-line-start-regexp))
  10445. (org-add-to-invisibility-spec '(org-cwidth))
  10446. (when (fboundp 'font-lock-add-keywords)
  10447. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10448. (org-restart-font-lock))
  10449. (easy-menu-add orgtbl-mode-menu)
  10450. (run-hooks 'orgtbl-mode-hook))
  10451. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10452. (org-cleanup-narrow-column-properties)
  10453. (org-remove-from-invisibility-spec '(org-cwidth))
  10454. (remove-hook 'before-change-functions 'org-before-change-function t)
  10455. (when (fboundp 'font-lock-remove-keywords)
  10456. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10457. (org-restart-font-lock))
  10458. (easy-menu-remove orgtbl-mode-menu)
  10459. (force-mode-line-update 'all))))
  10460. (defun org-cleanup-narrow-column-properties ()
  10461. "Remove all properties related to narrow-column invisibility."
  10462. (let ((s 1))
  10463. (while (setq s (text-property-any s (point-max)
  10464. 'display org-narrow-column-arrow))
  10465. (remove-text-properties s (1+ s) '(display t)))
  10466. (setq s 1)
  10467. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10468. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10469. (setq s 1)
  10470. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10471. (remove-text-properties s (1+ s) '(invisible t)))))
  10472. ;; Install it as a minor mode.
  10473. (put 'orgtbl-mode :included t)
  10474. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10475. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10476. (defun orgtbl-make-binding (fun n &rest keys)
  10477. "Create a function for binding in the table minor mode.
  10478. FUN is the command to call inside a table. N is used to create a unique
  10479. command name. KEYS are keys that should be checked in for a command
  10480. to execute outside of tables."
  10481. (eval
  10482. (list 'defun
  10483. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10484. '(arg)
  10485. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10486. "Outside of tables, run the binding of `"
  10487. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10488. "'.")
  10489. '(interactive "p")
  10490. (list 'if
  10491. '(org-at-table-p)
  10492. (list 'call-interactively (list 'quote fun))
  10493. (list 'let '(orgtbl-mode)
  10494. (list 'call-interactively
  10495. (append '(or)
  10496. (mapcar (lambda (k)
  10497. (list 'key-binding k))
  10498. keys)
  10499. '('orgtbl-error))))))))
  10500. (defun orgtbl-error ()
  10501. "Error when there is no default binding for a table key."
  10502. (interactive)
  10503. (error "This key has no function outside tables"))
  10504. (defun orgtbl-setup ()
  10505. "Setup orgtbl keymaps."
  10506. (let ((nfunc 0)
  10507. (bindings
  10508. (list
  10509. '([(meta shift left)] org-table-delete-column)
  10510. '([(meta left)] org-table-move-column-left)
  10511. '([(meta right)] org-table-move-column-right)
  10512. '([(meta shift right)] org-table-insert-column)
  10513. '([(meta shift up)] org-table-kill-row)
  10514. '([(meta shift down)] org-table-insert-row)
  10515. '([(meta up)] org-table-move-row-up)
  10516. '([(meta down)] org-table-move-row-down)
  10517. '("\C-c\C-w" org-table-cut-region)
  10518. '("\C-c\M-w" org-table-copy-region)
  10519. '("\C-c\C-y" org-table-paste-rectangle)
  10520. '("\C-c-" org-table-insert-hline)
  10521. '("\C-c}" org-table-toggle-coordinate-overlays)
  10522. '("\C-c{" org-table-toggle-formula-debugger)
  10523. '("\C-m" org-table-next-row)
  10524. '([(shift return)] org-table-copy-down)
  10525. '("\C-c\C-q" org-table-wrap-region)
  10526. '("\C-c?" org-table-field-info)
  10527. '("\C-c " org-table-blank-field)
  10528. '("\C-c+" org-table-sum)
  10529. '("\C-c=" org-table-eval-formula)
  10530. '("\C-c'" org-table-edit-formulas)
  10531. '("\C-c`" org-table-edit-field)
  10532. '("\C-c*" org-table-recalculate)
  10533. '("\C-c|" org-table-create-or-convert-from-region)
  10534. '("\C-c^" org-table-sort-lines)
  10535. '([(control ?#)] org-table-rotate-recalc-marks)))
  10536. elt key fun cmd)
  10537. (while (setq elt (pop bindings))
  10538. (setq nfunc (1+ nfunc))
  10539. (setq key (org-key (car elt))
  10540. fun (nth 1 elt)
  10541. cmd (orgtbl-make-binding fun nfunc key))
  10542. (org-defkey orgtbl-mode-map key cmd))
  10543. ;; Special treatment needed for TAB and RET
  10544. (org-defkey orgtbl-mode-map [(return)]
  10545. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10546. (org-defkey orgtbl-mode-map "\C-m"
  10547. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10548. (org-defkey orgtbl-mode-map [(tab)]
  10549. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10550. (org-defkey orgtbl-mode-map "\C-i"
  10551. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10552. (org-defkey orgtbl-mode-map [(shift tab)]
  10553. (orgtbl-make-binding 'org-table-previous-field 104
  10554. [(shift tab)] [(tab)] "\C-i"))
  10555. (org-defkey orgtbl-mode-map "\M-\C-m"
  10556. (orgtbl-make-binding 'org-table-wrap-region 105
  10557. "\M-\C-m" [(meta return)]))
  10558. (org-defkey orgtbl-mode-map [(meta return)]
  10559. (orgtbl-make-binding 'org-table-wrap-region 106
  10560. [(meta return)] "\M-\C-m"))
  10561. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10562. (when orgtbl-optimized
  10563. ;; If the user wants maximum table support, we need to hijack
  10564. ;; some standard editing functions
  10565. (org-remap orgtbl-mode-map
  10566. 'self-insert-command 'orgtbl-self-insert-command
  10567. 'delete-char 'org-delete-char
  10568. 'delete-backward-char 'org-delete-backward-char)
  10569. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10570. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10571. '("OrgTbl"
  10572. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10573. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10574. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10575. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10576. "--"
  10577. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10578. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10579. ["Copy Field from Above"
  10580. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10581. "--"
  10582. ("Column"
  10583. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10584. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10585. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10586. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10587. ("Row"
  10588. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10589. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10590. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10591. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10592. ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
  10593. "--"
  10594. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10595. ("Rectangle"
  10596. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10597. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10598. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10599. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10600. "--"
  10601. ("Radio tables"
  10602. ["Insert table template" orgtbl-insert-radio-table
  10603. (assq major-mode orgtbl-radio-table-templates)]
  10604. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10605. "--"
  10606. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10607. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10608. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10609. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10610. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10611. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10612. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10613. ["Sum Column/Rectangle" org-table-sum
  10614. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10615. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10616. ["Debug Formulas"
  10617. org-table-toggle-formula-debugger :active (org-at-table-p)
  10618. :keys "C-c {"
  10619. :style toggle :selected org-table-formula-debug]
  10620. ["Show Col/Row Numbers"
  10621. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10622. :keys "C-c }"
  10623. :style toggle :selected org-table-overlay-coordinates]
  10624. ))
  10625. t))
  10626. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10627. "If the cursor is inside a table, realign the table.
  10628. It it is a table to be sent away to a receiver, do it.
  10629. With prefix arg, also recompute table."
  10630. (interactive "P")
  10631. (let ((pos (point)) action)
  10632. (save-excursion
  10633. (beginning-of-line 1)
  10634. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10635. ((looking-at "[ \t]*|") pos)
  10636. ((looking-at "#\\+TBLFM:") 'recalc))))
  10637. (cond
  10638. ((integerp action)
  10639. (goto-char action)
  10640. (org-table-maybe-eval-formula)
  10641. (if arg
  10642. (call-interactively 'org-table-recalculate)
  10643. (org-table-maybe-recalculate-line))
  10644. (call-interactively 'org-table-align)
  10645. (orgtbl-send-table 'maybe))
  10646. ((eq action 'recalc)
  10647. (save-excursion
  10648. (beginning-of-line 1)
  10649. (skip-chars-backward " \r\n\t")
  10650. (if (org-at-table-p)
  10651. (org-call-with-arg 'org-table-recalculate t))))
  10652. (t (let (orgtbl-mode)
  10653. (call-interactively (key-binding "\C-c\C-c")))))))
  10654. (defun orgtbl-tab (arg)
  10655. "Justification and field motion for `orgtbl-mode'."
  10656. (interactive "P")
  10657. (if arg (org-table-edit-field t)
  10658. (org-table-justify-field-maybe)
  10659. (org-table-next-field)))
  10660. (defun orgtbl-ret ()
  10661. "Justification and field motion for `orgtbl-mode'."
  10662. (interactive)
  10663. (org-table-justify-field-maybe)
  10664. (org-table-next-row))
  10665. (defun orgtbl-self-insert-command (N)
  10666. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10667. If the cursor is in a table looking at whitespace, the whitespace is
  10668. overwritten, and the table is not marked as requiring realignment."
  10669. (interactive "p")
  10670. (if (and (org-at-table-p)
  10671. (or
  10672. (and org-table-auto-blank-field
  10673. (member last-command
  10674. '(orgtbl-hijacker-command-100
  10675. orgtbl-hijacker-command-101
  10676. orgtbl-hijacker-command-102
  10677. orgtbl-hijacker-command-103
  10678. orgtbl-hijacker-command-104
  10679. orgtbl-hijacker-command-105))
  10680. (org-table-blank-field))
  10681. t)
  10682. (eq N 1)
  10683. (looking-at "[^|\n]* +|"))
  10684. (let (org-table-may-need-update)
  10685. (goto-char (1- (match-end 0)))
  10686. (delete-backward-char 1)
  10687. (goto-char (match-beginning 0))
  10688. (self-insert-command N))
  10689. (setq org-table-may-need-update t)
  10690. (let (orgtbl-mode)
  10691. (call-interactively (key-binding (vector last-input-event))))))
  10692. (defun org-force-self-insert (N)
  10693. "Needed to enforce self-insert under remapping."
  10694. (interactive "p")
  10695. (self-insert-command N))
  10696. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10697. "Regula expression matching exponentials as produced by calc.")
  10698. (defvar org-table-clean-did-remove-column nil)
  10699. (defun orgtbl-export (table target)
  10700. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10701. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10702. org-table-last-alignment org-table-last-column-widths
  10703. maxcol column)
  10704. (if (not (fboundp func))
  10705. (error "Cannot export orgtbl table to %s" target))
  10706. (setq lines (org-table-clean-before-export lines))
  10707. (setq table
  10708. (mapcar
  10709. (lambda (x)
  10710. (if (string-match org-table-hline-regexp x)
  10711. 'hline
  10712. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10713. lines))
  10714. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10715. table)))
  10716. (loop for i from (1- maxcol) downto 0 do
  10717. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10718. (setq column (delq nil column))
  10719. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10720. (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))
  10721. (funcall func table nil)))
  10722. (defun orgtbl-send-table (&optional maybe)
  10723. "Send a tranformed version of this table to the receiver position.
  10724. With argument MAYBE, fail quietly if no transformation is defined for
  10725. this table."
  10726. (interactive)
  10727. (catch 'exit
  10728. (unless (org-at-table-p) (error "Not at a table"))
  10729. ;; when non-interactive, we assume align has just happened.
  10730. (when (interactive-p) (org-table-align))
  10731. (save-excursion
  10732. (goto-char (org-table-begin))
  10733. (beginning-of-line 0)
  10734. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10735. (if maybe
  10736. (throw 'exit nil)
  10737. (error "Don't know how to transform this table."))))
  10738. (let* ((name (match-string 1))
  10739. beg
  10740. (transform (intern (match-string 2)))
  10741. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10742. (skip (plist-get params :skip))
  10743. (skipcols (plist-get params :skipcols))
  10744. (txt (buffer-substring-no-properties
  10745. (org-table-begin) (org-table-end)))
  10746. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10747. (lines (org-table-clean-before-export lines))
  10748. (i0 (if org-table-clean-did-remove-column 2 1))
  10749. (table (mapcar
  10750. (lambda (x)
  10751. (if (string-match org-table-hline-regexp x)
  10752. 'hline
  10753. (org-remove-by-index
  10754. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10755. skipcols i0)))
  10756. lines))
  10757. (fun (if (= i0 2) 'cdr 'identity))
  10758. (org-table-last-alignment
  10759. (org-remove-by-index (funcall fun org-table-last-alignment)
  10760. skipcols i0))
  10761. (org-table-last-column-widths
  10762. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10763. skipcols i0)))
  10764. (unless (fboundp transform)
  10765. (error "No such transformation function %s" transform))
  10766. (setq txt (funcall transform table params))
  10767. ;; Find the insertion place
  10768. (save-excursion
  10769. (goto-char (point-min))
  10770. (unless (re-search-forward
  10771. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10772. (error "Don't know where to insert translated table"))
  10773. (goto-char (match-beginning 0))
  10774. (beginning-of-line 2)
  10775. (setq beg (point))
  10776. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10777. (error "Cannot find end of insertion region"))
  10778. (beginning-of-line 1)
  10779. (delete-region beg (point))
  10780. (goto-char beg)
  10781. (insert txt "\n"))
  10782. (message "Table converted and installed at receiver location"))))
  10783. (defun org-remove-by-index (list indices &optional i0)
  10784. "Remove the elements in LIST with indices in INDICES.
  10785. First element has index 0, or I0 if given."
  10786. (if (not indices)
  10787. list
  10788. (if (integerp indices) (setq indices (list indices)))
  10789. (setq i0 (1- (or i0 0)))
  10790. (delq :rm (mapcar (lambda (x)
  10791. (setq i0 (1+ i0))
  10792. (if (memq i0 indices) :rm x))
  10793. list))))
  10794. (defun orgtbl-toggle-comment ()
  10795. "Comment or uncomment the orgtbl at point."
  10796. (interactive)
  10797. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10798. (re2 (concat "^" orgtbl-line-start-regexp))
  10799. (commented (save-excursion (beginning-of-line 1)
  10800. (cond ((looking-at re1) t)
  10801. ((looking-at re2) nil)
  10802. (t (error "Not at an org table")))))
  10803. (re (if commented re1 re2))
  10804. beg end)
  10805. (save-excursion
  10806. (beginning-of-line 1)
  10807. (while (looking-at re) (beginning-of-line 0))
  10808. (beginning-of-line 2)
  10809. (setq beg (point))
  10810. (while (looking-at re) (beginning-of-line 2))
  10811. (setq end (point)))
  10812. (comment-region beg end (if commented '(4) nil))))
  10813. (defun orgtbl-insert-radio-table ()
  10814. "Insert a radio table template appropriate for this major mode."
  10815. (interactive)
  10816. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10817. (txt (nth 1 e))
  10818. name pos)
  10819. (unless e (error "No radio table setup defined for %s" major-mode))
  10820. (setq name (read-string "Table name: "))
  10821. (while (string-match "%n" txt)
  10822. (setq txt (replace-match name t t txt)))
  10823. (or (bolp) (insert "\n"))
  10824. (setq pos (point))
  10825. (insert txt)
  10826. (goto-char pos)))
  10827. (defun org-get-param (params header i sym &optional hsym)
  10828. "Get parameter value for symbol SYM.
  10829. If this is a header line, actually get the value for the symbol with an
  10830. additional \"h\" inserted after the colon.
  10831. If the value is a protperty list, get the element for the current column.
  10832. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10833. (let ((val (plist-get params sym)))
  10834. (and hsym header (setq val (or (plist-get params hsym) val)))
  10835. (if (consp val) (plist-get val i) val)))
  10836. (defun orgtbl-to-generic (table params)
  10837. "Convert the orgtbl-mode TABLE to some other format.
  10838. This generic routine can be used for many standard cases.
  10839. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10840. separator line, or a list of fields for that line.
  10841. PARAMS is a property list of parameters that can influence the conversion.
  10842. For the generic converter, some parameters are obligatory: You need to
  10843. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10844. :splice, you must have :tstart and :tend.
  10845. Valid parameters are
  10846. :tstart String to start the table. Ignored when :splice is t.
  10847. :tend String to end the table. Ignored when :splice is t.
  10848. :splice When set to t, return only table body lines, don't wrap
  10849. them into :tstart and :tend. Default is nil.
  10850. :hline String to be inserted on horizontal separation lines.
  10851. May be nil to ignore hlines.
  10852. :lstart String to start a new table line.
  10853. :lend String to end a table line
  10854. :sep Separator between two fields
  10855. :lfmt Format for entire line, with enough %s to capture all fields.
  10856. If this is present, :lstart, :lend, and :sep are ignored.
  10857. :fmt A format to be used to wrap the field, should contain
  10858. %s for the original field value. For example, to wrap
  10859. everything in dollars, you could use :fmt \"$%s$\".
  10860. This may also be a property list with column numbers and
  10861. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10862. :hlstart :hlend :hlsep :hlfmt :hfmt
  10863. Same as above, specific for the header lines in the table.
  10864. All lines before the first hline are treated as header.
  10865. If any of these is not present, the data line value is used.
  10866. :efmt Use this format to print numbers with exponentials.
  10867. The format should have %s twice for inserting mantissa
  10868. and exponent, for example \"%s\\\\times10^{%s}\". This
  10869. may also be a property list with column numbers and
  10870. formats. :fmt will still be applied after :efmt.
  10871. In addition to this, the parameters :skip and :skipcols are always handled
  10872. directly by `orgtbl-send-table'. See manual."
  10873. (interactive)
  10874. (let* ((p params)
  10875. (splicep (plist-get p :splice))
  10876. (hline (plist-get p :hline))
  10877. rtn line i fm efm lfmt h)
  10878. ;; Do we have a header?
  10879. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10880. (setq h t))
  10881. ;; Put header
  10882. (unless splicep
  10883. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10884. ;; Now loop over all lines
  10885. (while (setq line (pop table))
  10886. (if (eq line 'hline)
  10887. ;; A horizontal separator line
  10888. (progn (if hline (push hline rtn))
  10889. (setq h nil)) ; no longer in header
  10890. ;; A normal line. Convert the fields, push line onto the result list
  10891. (setq i 0)
  10892. (setq line
  10893. (mapcar
  10894. (lambda (f)
  10895. (setq i (1+ i)
  10896. fm (org-get-param p h i :fmt :hfmt)
  10897. efm (org-get-param p h i :efmt))
  10898. (if (and efm (string-match orgtbl-exp-regexp f))
  10899. (setq f (format
  10900. efm (match-string 1 f) (match-string 2 f))))
  10901. (if fm (setq f (format fm f)))
  10902. f)
  10903. line))
  10904. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10905. (push (apply 'format lfmt line) rtn)
  10906. (push (concat
  10907. (org-get-param p h i :lstart :hlstart)
  10908. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10909. (org-get-param p h i :lend :hlend))
  10910. rtn))))
  10911. (unless splicep
  10912. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10913. (mapconcat 'identity (nreverse rtn) "\n")))
  10914. (defun orgtbl-to-latex (table params)
  10915. "Convert the orgtbl-mode TABLE to LaTeX.
  10916. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10917. separator line, or a list of fields for that line.
  10918. PARAMS is a property list of parameters that can influence the conversion.
  10919. Supports all parameters from `orgtbl-to-generic'. Most important for
  10920. LaTeX are:
  10921. :splice When set to t, return only table body lines, don't wrap
  10922. them into a tabular environment. Default is nil.
  10923. :fmt A format to be used to wrap the field, should contain %s for the
  10924. original field value. For example, to wrap everything in dollars,
  10925. use :fmt \"$%s$\". This may also be a property list with column
  10926. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10927. :efmt Format for transforming numbers with exponentials. The format
  10928. should have %s twice for inserting mantissa and exponent, for
  10929. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10930. This may also be a property list with column numbers and formats.
  10931. The general parameters :skip and :skipcols have already been applied when
  10932. this function is called."
  10933. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10934. org-table-last-alignment ""))
  10935. (params2
  10936. (list
  10937. :tstart (concat "\\begin{tabular}{" alignment "}")
  10938. :tend "\\end{tabular}"
  10939. :lstart "" :lend " \\\\" :sep " & "
  10940. :efmt "%s\\,(%s)" :hline "\\hline")))
  10941. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10942. (defun orgtbl-to-html (table params)
  10943. "Convert the orgtbl-mode TABLE to LaTeX.
  10944. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10945. separator line, or a list of fields for that line.
  10946. PARAMS is a property list of parameters that can influence the conversion.
  10947. Currently this function recognizes the following parameters:
  10948. :splice When set to t, return only table body lines, don't wrap
  10949. them into a <table> environment. Default is nil.
  10950. The general parameters :skip and :skipcols have already been applied when
  10951. this function is called. The function does *not* use `orgtbl-to-generic',
  10952. so you cannot specify parameters for it."
  10953. (let* ((splicep (plist-get params :splice))
  10954. html)
  10955. ;; Just call the formatter we already have
  10956. ;; We need to make text lines for it, so put the fields back together.
  10957. (setq html (org-format-org-table-html
  10958. (mapcar
  10959. (lambda (x)
  10960. (if (eq x 'hline)
  10961. "|----+----|"
  10962. (concat "| " (mapconcat 'identity x " | ") " |")))
  10963. table)
  10964. splicep))
  10965. (if (string-match "\n+\\'" html)
  10966. (setq html (replace-match "" t t html)))
  10967. html))
  10968. (defun orgtbl-to-texinfo (table params)
  10969. "Convert the orgtbl-mode TABLE to TeXInfo.
  10970. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10971. separator line, or a list of fields for that line.
  10972. PARAMS is a property list of parameters that can influence the conversion.
  10973. Supports all parameters from `orgtbl-to-generic'. Most important for
  10974. TeXInfo are:
  10975. :splice nil/t When set to t, return only table body lines, don't wrap
  10976. them into a multitable environment. Default is nil.
  10977. :fmt fmt A format to be used to wrap the field, should contain
  10978. %s for the original field value. For example, to wrap
  10979. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10980. This may also be a property list with column numbers and
  10981. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10982. :cf \"f1 f2..\" The column fractions for the table. By default these
  10983. are computed automatically from the width of the columns
  10984. under org-mode.
  10985. The general parameters :skip and :skipcols have already been applied when
  10986. this function is called."
  10987. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10988. (colfrac (or (plist-get params :cf)
  10989. (mapconcat
  10990. (lambda (x) (format "%.3f" (/ (float x) total)))
  10991. org-table-last-column-widths " ")))
  10992. (params2
  10993. (list
  10994. :tstart (concat "@multitable @columnfractions " colfrac)
  10995. :tend "@end multitable"
  10996. :lstart "@item " :lend "" :sep " @tab "
  10997. :hlstart "@headitem ")))
  10998. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10999. ;;;; Link Stuff
  11000. ;;; Link abbreviations
  11001. (defun org-link-expand-abbrev (link)
  11002. "Apply replacements as defined in `org-link-abbrev-alist."
  11003. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  11004. (let* ((key (match-string 1 link))
  11005. (as (or (assoc key org-link-abbrev-alist-local)
  11006. (assoc key org-link-abbrev-alist)))
  11007. (tag (and (match-end 2) (match-string 3 link)))
  11008. rpl)
  11009. (if (not as)
  11010. link
  11011. (setq rpl (cdr as))
  11012. (cond
  11013. ((symbolp rpl) (funcall rpl tag))
  11014. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  11015. (t (concat rpl tag)))))
  11016. link))
  11017. ;;; Storing and inserting links
  11018. (defvar org-insert-link-history nil
  11019. "Minibuffer history for links inserted with `org-insert-link'.")
  11020. (defvar org-stored-links nil
  11021. "Contains the links stored with `org-store-link'.")
  11022. (defvar org-store-link-plist nil
  11023. "Plist with info about the most recently link created with `org-store-link'.")
  11024. (defvar org-link-protocols nil
  11025. "Link protocols added to Org-mode using `org-add-link-type'.")
  11026. (defvar org-store-link-functions nil
  11027. "List of functions that are called to create and store a link.
  11028. Each function will be called in turn until one returns a non-nil
  11029. value. Each function should check if it is responsible for creating
  11030. this link (for example by looking at the major mode).
  11031. If not, it must exit and return nil.
  11032. If yes, it should return a non-nil value after a calling
  11033. `org-store-link-props' with a list of properties and values.
  11034. Special properties are:
  11035. :type The link prefix. like \"http\". This must be given.
  11036. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  11037. This is obligatory as well.
  11038. :description Optional default description for the second pair
  11039. of brackets in an Org-mode link. The user can still change
  11040. this when inserting this link into an Org-mode buffer.
  11041. In addition to these, any additional properties can be specified
  11042. and then used in remember templates.")
  11043. (defun org-add-link-type (type &optional follow export)
  11044. "Add TYPE to the list of `org-link-types'.
  11045. Re-compute all regular expressions depending on `org-link-types'
  11046. FOLLOW and EXPORT are two functions.
  11047. FOLLOW should take the link path as the single argument and do whatever
  11048. is necessary to follow the link, for example find a file or display
  11049. a mail message.
  11050. EXPORT should format the link path for export to one of the export formats.
  11051. It should be a function accepting three arguments:
  11052. path the path of the link, the text after the prefix (like \"http:\")
  11053. desc the description of the link, if any, nil if there was no descripton
  11054. format the export format, a symbol like `html' or `latex'.
  11055. The function may use the FORMAT information to return different values
  11056. depending on the format. The return value will be put literally into
  11057. the exported file.
  11058. Org-mode has a built-in default for exporting links. If you are happy with
  11059. this default, there is no need to define an export function for the link
  11060. type. For a simple example of an export function, see `org-bbdb.el'."
  11061. (add-to-list 'org-link-types type t)
  11062. (org-make-link-regexps)
  11063. (if (assoc type org-link-protocols)
  11064. (setcdr (assoc type org-link-protocols) (list follow export))
  11065. (push (list type follow export) org-link-protocols)))
  11066. (defun org-add-agenda-custom-command (entry)
  11067. "Replace or add a command in `org-agenda-custom-commands'.
  11068. This is mostly for hacking and trying a new command - once the command
  11069. works you probably want to add it to `org-agenda-custom-commands' for good."
  11070. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  11071. (if ass
  11072. (setcdr ass (cdr entry))
  11073. (push entry org-agenda-custom-commands))))
  11074. ;;;###autoload
  11075. (defun org-store-link (arg)
  11076. "\\<org-mode-map>Store an org-link to the current location.
  11077. This link is added to `org-stored-links' and can later be inserted
  11078. into an org-buffer with \\[org-insert-link].
  11079. For some link types, a prefix arg is interpreted:
  11080. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  11081. For file links, arg negates `org-context-in-file-links'."
  11082. (interactive "P")
  11083. (org-load-modules-maybe)
  11084. (setq org-store-link-plist nil) ; reset
  11085. (let (link cpltxt desc description search txt)
  11086. (cond
  11087. ((run-hook-with-args-until-success 'org-store-link-functions)
  11088. (setq link (plist-get org-store-link-plist :link)
  11089. desc (or (plist-get org-store-link-plist :description) link)))
  11090. ((eq major-mode 'calendar-mode)
  11091. (let ((cd (calendar-cursor-to-date)))
  11092. (setq link
  11093. (format-time-string
  11094. (car org-time-stamp-formats)
  11095. (apply 'encode-time
  11096. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11097. nil nil nil))))
  11098. (org-store-link-props :type "calendar" :date cd)))
  11099. ((eq major-mode 'w3-mode)
  11100. (setq cpltxt (url-view-url t)
  11101. link (org-make-link cpltxt))
  11102. (org-store-link-props :type "w3" :url (url-view-url t)))
  11103. ((eq major-mode 'w3m-mode)
  11104. (setq cpltxt (or w3m-current-title w3m-current-url)
  11105. link (org-make-link w3m-current-url))
  11106. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11107. ((setq search (run-hook-with-args-until-success
  11108. 'org-create-file-search-functions))
  11109. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11110. "::" search))
  11111. (setq cpltxt (or description link)))
  11112. ((eq major-mode 'image-mode)
  11113. (setq cpltxt (concat "file:"
  11114. (abbreviate-file-name buffer-file-name))
  11115. link (org-make-link cpltxt))
  11116. (org-store-link-props :type "image" :file buffer-file-name))
  11117. ((eq major-mode 'dired-mode)
  11118. ;; link to the file in the current line
  11119. (setq cpltxt (concat "file:"
  11120. (abbreviate-file-name
  11121. (expand-file-name
  11122. (dired-get-filename nil t))))
  11123. link (org-make-link cpltxt)))
  11124. ((and buffer-file-name (org-mode-p))
  11125. ;; Just link to current headline
  11126. (setq cpltxt (concat "file:"
  11127. (abbreviate-file-name buffer-file-name)))
  11128. ;; Add a context search string
  11129. (when (org-xor org-context-in-file-links arg)
  11130. ;; Check if we are on a target
  11131. (if (org-in-regexp "<<\\(.*?\\)>>")
  11132. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11133. (setq txt (cond
  11134. ((org-on-heading-p) nil)
  11135. ((org-region-active-p)
  11136. (buffer-substring (region-beginning) (region-end)))
  11137. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11138. (when (or (null txt) (string-match "\\S-" txt))
  11139. (setq cpltxt
  11140. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11141. desc "NONE"))))
  11142. (if (string-match "::\\'" cpltxt)
  11143. (setq cpltxt (substring cpltxt 0 -2)))
  11144. (setq link (org-make-link cpltxt)))
  11145. ((buffer-file-name (buffer-base-buffer))
  11146. ;; Just link to this file here.
  11147. (setq cpltxt (concat "file:"
  11148. (abbreviate-file-name
  11149. (buffer-file-name (buffer-base-buffer)))))
  11150. ;; Add a context string
  11151. (when (org-xor org-context-in-file-links arg)
  11152. (setq txt (if (org-region-active-p)
  11153. (buffer-substring (region-beginning) (region-end))
  11154. (buffer-substring (point-at-bol) (point-at-eol))))
  11155. ;; Only use search option if there is some text.
  11156. (when (string-match "\\S-" txt)
  11157. (setq cpltxt
  11158. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11159. desc "NONE")))
  11160. (setq link (org-make-link cpltxt)))
  11161. ((interactive-p)
  11162. (error "Cannot link to a buffer which is not visiting a file"))
  11163. (t (setq link nil)))
  11164. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11165. (setq link (or link cpltxt)
  11166. desc (or desc cpltxt))
  11167. (if (equal desc "NONE") (setq desc nil))
  11168. (if (and (interactive-p) link)
  11169. (progn
  11170. (setq org-stored-links
  11171. (cons (list link desc) org-stored-links))
  11172. (message "Stored: %s" (or desc link)))
  11173. (and link (org-make-link-string link desc)))))
  11174. (defun org-store-link-props (&rest plist)
  11175. "Store link properties, extract names and addresses."
  11176. (let (x adr)
  11177. (when (setq x (plist-get plist :from))
  11178. (setq adr (mail-extract-address-components x))
  11179. (plist-put plist :fromname (car adr))
  11180. (plist-put plist :fromaddress (nth 1 adr)))
  11181. (when (setq x (plist-get plist :to))
  11182. (setq adr (mail-extract-address-components x))
  11183. (plist-put plist :toname (car adr))
  11184. (plist-put plist :toaddress (nth 1 adr))))
  11185. (let ((from (plist-get plist :from))
  11186. (to (plist-get plist :to)))
  11187. (when (and from to org-from-is-user-regexp)
  11188. (plist-put plist :fromto
  11189. (if (string-match org-from-is-user-regexp from)
  11190. (concat "to %t")
  11191. (concat "from %f")))))
  11192. (setq org-store-link-plist plist))
  11193. (defun org-add-link-props (&rest plist)
  11194. "Add these properties to the link property list."
  11195. (let (key value)
  11196. (while plist
  11197. (setq key (pop plist) value (pop plist))
  11198. (setq org-store-link-plist
  11199. (plist-put org-store-link-plist key value)))))
  11200. (defun org-email-link-description (&optional fmt)
  11201. "Return the description part of an email link.
  11202. This takes information from `org-store-link-plist' and formats it
  11203. according to FMT (default from `org-email-link-description-format')."
  11204. (setq fmt (or fmt org-email-link-description-format))
  11205. (let* ((p org-store-link-plist)
  11206. (to (plist-get p :toaddress))
  11207. (from (plist-get p :fromaddress))
  11208. (table
  11209. (list
  11210. (cons "%c" (plist-get p :fromto))
  11211. (cons "%F" (plist-get p :from))
  11212. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11213. (cons "%T" (plist-get p :to))
  11214. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11215. (cons "%s" (plist-get p :subject))
  11216. (cons "%m" (plist-get p :message-id)))))
  11217. (when (string-match "%c" fmt)
  11218. ;; Check if the user wrote this message
  11219. (if (and org-from-is-user-regexp from to
  11220. (save-match-data (string-match org-from-is-user-regexp from)))
  11221. (setq fmt (replace-match "to %t" t t fmt))
  11222. (setq fmt (replace-match "from %f" t t fmt))))
  11223. (org-replace-escapes fmt table)))
  11224. (defun org-make-org-heading-search-string (&optional string heading)
  11225. "Make search string for STRING or current headline."
  11226. (interactive)
  11227. (let ((s (or string (org-get-heading))))
  11228. (unless (and string (not heading))
  11229. ;; We are using a headline, clean up garbage in there.
  11230. (if (string-match org-todo-regexp s)
  11231. (setq s (replace-match "" t t s)))
  11232. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11233. (setq s (replace-match "" t t s)))
  11234. (setq s (org-trim s))
  11235. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11236. org-comment-string "\\)") s)
  11237. (setq s (replace-match "" t t s)))
  11238. (while (string-match org-ts-regexp s)
  11239. (setq s (replace-match "" t t s))))
  11240. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11241. (setq s (replace-match " " t t s)))
  11242. (or string (setq s (concat "*" s))) ; Add * for headlines
  11243. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11244. (defun org-make-link (&rest strings)
  11245. "Concatenate STRINGS."
  11246. (apply 'concat strings))
  11247. (defun org-make-link-string (link &optional description)
  11248. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11249. (unless (string-match "\\S-" link)
  11250. (error "Empty link"))
  11251. (when (stringp description)
  11252. ;; Remove brackets from the description, they are fatal.
  11253. (while (string-match "\\[" description)
  11254. (setq description (replace-match "{" t t description)))
  11255. (while (string-match "\\]" description)
  11256. (setq description (replace-match "}" t t description))))
  11257. (when (equal (org-link-escape link) description)
  11258. ;; No description needed, it is identical
  11259. (setq description nil))
  11260. (when (and (not description)
  11261. (not (equal link (org-link-escape link))))
  11262. (setq description link))
  11263. (concat "[[" (org-link-escape link) "]"
  11264. (if description (concat "[" description "]") "")
  11265. "]"))
  11266. (defconst org-link-escape-chars
  11267. '((?\ . "%20")
  11268. (?\[ . "%5B")
  11269. (?\] . "%5D")
  11270. (?\340 . "%E0") ; `a
  11271. (?\342 . "%E2") ; ^a
  11272. (?\347 . "%E7") ; ,c
  11273. (?\350 . "%E8") ; `e
  11274. (?\351 . "%E9") ; 'e
  11275. (?\352 . "%EA") ; ^e
  11276. (?\356 . "%EE") ; ^i
  11277. (?\364 . "%F4") ; ^o
  11278. (?\371 . "%F9") ; `u
  11279. (?\373 . "%FB") ; ^u
  11280. (?\; . "%3B")
  11281. (?? . "%3F")
  11282. (?= . "%3D")
  11283. (?+ . "%2B")
  11284. )
  11285. "Association list of escapes for some characters problematic in links.
  11286. This is the list that is used for internal purposes.")
  11287. (defconst org-link-escape-chars-browser
  11288. '((?\ . "%20")) ; 32 for the SPC char
  11289. "Association list of escapes for some characters problematic in links.
  11290. This is the list that is used before handing over to the browser.")
  11291. (defun org-link-escape (text &optional table)
  11292. "Escape charaters in TEXT that are problematic for links."
  11293. (setq table (or table org-link-escape-chars))
  11294. (when text
  11295. (let ((re (mapconcat (lambda (x) (regexp-quote
  11296. (char-to-string (car x))))
  11297. table "\\|")))
  11298. (while (string-match re text)
  11299. (setq text
  11300. (replace-match
  11301. (cdr (assoc (string-to-char (match-string 0 text))
  11302. table))
  11303. t t text)))
  11304. text)))
  11305. (defun org-link-unescape (text &optional table)
  11306. "Reverse the action of `org-link-escape'."
  11307. (setq table (or table org-link-escape-chars))
  11308. (when text
  11309. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11310. table "\\|")))
  11311. (while (string-match re text)
  11312. (setq text
  11313. (replace-match
  11314. (char-to-string (car (rassoc (match-string 0 text) table)))
  11315. t t text)))
  11316. text)))
  11317. (defun org-xor (a b)
  11318. "Exclusive or."
  11319. (if a (not b) b))
  11320. (defun org-get-header (header)
  11321. "Find a header field in the current buffer."
  11322. (save-excursion
  11323. (goto-char (point-min))
  11324. (let ((case-fold-search t) s)
  11325. (cond
  11326. ((eq header 'from)
  11327. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11328. (setq s (match-string 1)))
  11329. (while (string-match "\"" s)
  11330. (setq s (replace-match "" t t s)))
  11331. (if (string-match "[<(].*" s)
  11332. (setq s (replace-match "" t t s))))
  11333. ((eq header 'message-id)
  11334. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11335. (setq s (match-string 1))))
  11336. ((eq header 'subject)
  11337. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11338. (setq s (match-string 1)))))
  11339. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11340. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11341. s)))
  11342. (defun org-fixup-message-id-for-http (s)
  11343. "Replace special characters in a message id, so it can be used in an http query."
  11344. (while (string-match "<" s)
  11345. (setq s (replace-match "%3C" t t s)))
  11346. (while (string-match ">" s)
  11347. (setq s (replace-match "%3E" t t s)))
  11348. (while (string-match "@" s)
  11349. (setq s (replace-match "%40" t t s)))
  11350. s)
  11351. ;;;###autoload
  11352. (defun org-insert-link-global ()
  11353. "Insert a link like Org-mode does.
  11354. This command can be called in any mode to insert a link in Org-mode syntax."
  11355. (interactive)
  11356. (org-load-modules-maybe)
  11357. (org-run-like-in-org-mode 'org-insert-link))
  11358. (defun org-insert-link (&optional complete-file)
  11359. "Insert a link. At the prompt, enter the link.
  11360. Completion can be used to select a link previously stored with
  11361. `org-store-link'. When the empty string is entered (i.e. if you just
  11362. press RET at the prompt), the link defaults to the most recently
  11363. stored link. As SPC triggers completion in the minibuffer, you need to
  11364. use M-SPC or C-q SPC to force the insertion of a space character.
  11365. You will also be prompted for a description, and if one is given, it will
  11366. be displayed in the buffer instead of the link.
  11367. If there is already a link at point, this command will allow you to edit link
  11368. and description parts.
  11369. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11370. selected using completion. The path to the file will be relative to
  11371. the current directory if the file is in the current directory or a
  11372. subdirectory. Otherwise, the link will be the absolute path as
  11373. completed in the minibuffer (i.e. normally ~/path/to/file).
  11374. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11375. is in the current directory or below.
  11376. With three \\[universal-argument] prefixes, negate the meaning of
  11377. `org-keep-stored-link-after-insertion'."
  11378. (interactive "P")
  11379. (let* ((wcf (current-window-configuration))
  11380. (region (if (org-region-active-p)
  11381. (buffer-substring (region-beginning) (region-end))))
  11382. (remove (and region (list (region-beginning) (region-end))))
  11383. (desc region)
  11384. tmphist ; byte-compile incorrectly complains about this
  11385. link entry file)
  11386. (cond
  11387. ((org-in-regexp org-bracket-link-regexp 1)
  11388. ;; We do have a link at point, and we are going to edit it.
  11389. (setq remove (list (match-beginning 0) (match-end 0)))
  11390. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11391. (setq link (read-string "Link: "
  11392. (org-link-unescape
  11393. (org-match-string-no-properties 1)))))
  11394. ((or (org-in-regexp org-angle-link-re)
  11395. (org-in-regexp org-plain-link-re))
  11396. ;; Convert to bracket link
  11397. (setq remove (list (match-beginning 0) (match-end 0))
  11398. link (read-string "Link: "
  11399. (org-remove-angle-brackets (match-string 0)))))
  11400. ((equal complete-file '(4))
  11401. ;; Completing read for file names.
  11402. (setq file (read-file-name "File: "))
  11403. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11404. (pwd1 (file-name-as-directory (abbreviate-file-name
  11405. (expand-file-name ".")))))
  11406. (cond
  11407. ((equal complete-file '(16))
  11408. (setq link (org-make-link
  11409. "file:"
  11410. (abbreviate-file-name (expand-file-name file)))))
  11411. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11412. (setq link (org-make-link "file:" (match-string 1 file))))
  11413. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11414. (expand-file-name file))
  11415. (setq link (org-make-link
  11416. "file:" (match-string 1 (expand-file-name file)))))
  11417. (t (setq link (org-make-link "file:" file))))))
  11418. (t
  11419. ;; Read link, with completion for stored links.
  11420. (with-output-to-temp-buffer "*Org Links*"
  11421. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11422. (when org-stored-links
  11423. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11424. (princ (mapconcat
  11425. (lambda (x)
  11426. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11427. (reverse org-stored-links) "\n"))))
  11428. (let ((cw (selected-window)))
  11429. (select-window (get-buffer-window "*Org Links*"))
  11430. (shrink-window-if-larger-than-buffer)
  11431. (setq truncate-lines t)
  11432. (select-window cw))
  11433. ;; Fake a link history, containing the stored links.
  11434. (setq tmphist (append (mapcar 'car org-stored-links)
  11435. org-insert-link-history))
  11436. (unwind-protect
  11437. (setq link (org-completing-read
  11438. "Link: "
  11439. (append
  11440. (mapcar (lambda (x) (list (concat (car x) ":")))
  11441. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11442. (mapcar (lambda (x) (list (concat x ":")))
  11443. org-link-types))
  11444. nil nil nil
  11445. 'tmphist
  11446. (or (car (car org-stored-links)))))
  11447. (set-window-configuration wcf)
  11448. (kill-buffer "*Org Links*"))
  11449. (setq entry (assoc link org-stored-links))
  11450. (or entry (push link org-insert-link-history))
  11451. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11452. (not org-keep-stored-link-after-insertion))
  11453. (setq org-stored-links (delq (assoc link org-stored-links)
  11454. org-stored-links)))
  11455. (setq desc (or desc (nth 1 entry)))))
  11456. (if (string-match org-plain-link-re link)
  11457. ;; URL-like link, normalize the use of angular brackets.
  11458. (setq link (org-make-link (org-remove-angle-brackets link))))
  11459. ;; Check if we are linking to the current file with a search option
  11460. ;; If yes, simplify the link by using only the search option.
  11461. (when (and buffer-file-name
  11462. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11463. (let* ((path (match-string 1 link))
  11464. (case-fold-search nil)
  11465. (search (match-string 2 link)))
  11466. (save-match-data
  11467. (if (equal (file-truename buffer-file-name) (file-truename path))
  11468. ;; We are linking to this same file, with a search option
  11469. (setq link search)))))
  11470. ;; Check if we can/should use a relative path. If yes, simplify the link
  11471. (when (string-match "\\<file:\\(.*\\)" link)
  11472. (let* ((path (match-string 1 link))
  11473. (origpath path)
  11474. (case-fold-search nil))
  11475. (cond
  11476. ((eq org-link-file-path-type 'absolute)
  11477. (setq path (abbreviate-file-name (expand-file-name path))))
  11478. ((eq org-link-file-path-type 'noabbrev)
  11479. (setq path (expand-file-name path)))
  11480. ((eq org-link-file-path-type 'relative)
  11481. (setq path (file-relative-name path)))
  11482. (t
  11483. (save-match-data
  11484. (if (string-match (concat "^" (regexp-quote
  11485. (file-name-as-directory
  11486. (expand-file-name "."))))
  11487. (expand-file-name path))
  11488. ;; We are linking a file with relative path name.
  11489. (setq path (substring (expand-file-name path)
  11490. (match-end 0)))))))
  11491. (setq link (concat "file:" path))
  11492. (if (equal desc origpath)
  11493. (setq desc path))))
  11494. (setq desc (read-string "Description: " desc))
  11495. (unless (string-match "\\S-" desc) (setq desc nil))
  11496. (if remove (apply 'delete-region remove))
  11497. (insert (org-make-link-string link desc))))
  11498. (defun org-completing-read (&rest args)
  11499. (let ((minibuffer-local-completion-map
  11500. (copy-keymap minibuffer-local-completion-map)))
  11501. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11502. (apply 'completing-read args)))
  11503. ;;; Opening/following a link
  11504. (defvar org-link-search-failed nil)
  11505. (defun org-next-link ()
  11506. "Move forward to the next link.
  11507. If the link is in hidden text, expose it."
  11508. (interactive)
  11509. (when (and org-link-search-failed (eq this-command last-command))
  11510. (goto-char (point-min))
  11511. (message "Link search wrapped back to beginning of buffer"))
  11512. (setq org-link-search-failed nil)
  11513. (let* ((pos (point))
  11514. (ct (org-context))
  11515. (a (assoc :link ct)))
  11516. (if a (goto-char (nth 2 a)))
  11517. (if (re-search-forward org-any-link-re nil t)
  11518. (progn
  11519. (goto-char (match-beginning 0))
  11520. (if (org-invisible-p) (org-show-context)))
  11521. (goto-char pos)
  11522. (setq org-link-search-failed t)
  11523. (error "No further link found"))))
  11524. (defun org-previous-link ()
  11525. "Move backward to the previous link.
  11526. If the link is in hidden text, expose it."
  11527. (interactive)
  11528. (when (and org-link-search-failed (eq this-command last-command))
  11529. (goto-char (point-max))
  11530. (message "Link search wrapped back to end of buffer"))
  11531. (setq org-link-search-failed nil)
  11532. (let* ((pos (point))
  11533. (ct (org-context))
  11534. (a (assoc :link ct)))
  11535. (if a (goto-char (nth 1 a)))
  11536. (if (re-search-backward org-any-link-re nil t)
  11537. (progn
  11538. (goto-char (match-beginning 0))
  11539. (if (org-invisible-p) (org-show-context)))
  11540. (goto-char pos)
  11541. (setq org-link-search-failed t)
  11542. (error "No further link found"))))
  11543. (defun org-find-file-at-mouse (ev)
  11544. "Open file link or URL at mouse."
  11545. (interactive "e")
  11546. (mouse-set-point ev)
  11547. (org-open-at-point 'in-emacs))
  11548. (defun org-open-at-mouse (ev)
  11549. "Open file link or URL at mouse."
  11550. (interactive "e")
  11551. (mouse-set-point ev)
  11552. (org-open-at-point))
  11553. (defvar org-window-config-before-follow-link nil
  11554. "The window configuration before following a link.
  11555. This is saved in case the need arises to restore it.")
  11556. (defvar org-open-link-marker (make-marker)
  11557. "Marker pointing to the location where `org-open-at-point; was called.")
  11558. ;;;###autoload
  11559. (defun org-open-at-point-global ()
  11560. "Follow a link like Org-mode does.
  11561. This command can be called in any mode to follow a link that has
  11562. Org-mode syntax."
  11563. (interactive)
  11564. (org-run-like-in-org-mode 'org-open-at-point))
  11565. (defun org-open-at-point (&optional in-emacs)
  11566. "Open link at or after point.
  11567. If there is no link at point, this function will search forward up to
  11568. the end of the current subtree.
  11569. Normally, files will be opened by an appropriate application. If the
  11570. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11571. (interactive "P")
  11572. (org-load-modules-maybe)
  11573. (move-marker org-open-link-marker (point))
  11574. (setq org-window-config-before-follow-link (current-window-configuration))
  11575. (org-remove-occur-highlights nil nil t)
  11576. (if (org-at-timestamp-p t)
  11577. (org-follow-timestamp-link)
  11578. (let (type path link line search (pos (point)))
  11579. (catch 'match
  11580. (save-excursion
  11581. (skip-chars-forward "^]\n\r")
  11582. (when (org-in-regexp org-bracket-link-regexp)
  11583. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11584. (while (string-match " *\n *" link)
  11585. (setq link (replace-match " " t t link)))
  11586. (setq link (org-link-expand-abbrev link))
  11587. (if (string-match org-link-re-with-space2 link)
  11588. (setq type (match-string 1 link) path (match-string 2 link))
  11589. (setq type "thisfile" path link))
  11590. (throw 'match t)))
  11591. (when (get-text-property (point) 'org-linked-text)
  11592. (setq type "thisfile"
  11593. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11594. (1+ (point)) (point))
  11595. path (buffer-substring
  11596. (previous-single-property-change pos 'org-linked-text)
  11597. (next-single-property-change pos 'org-linked-text)))
  11598. (throw 'match t))
  11599. (save-excursion
  11600. (when (or (org-in-regexp org-angle-link-re)
  11601. (org-in-regexp org-plain-link-re))
  11602. (setq type (match-string 1) path (match-string 2))
  11603. (throw 'match t)))
  11604. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11605. (setq type "tree-match"
  11606. path (match-string 1))
  11607. (throw 'match t))
  11608. (save-excursion
  11609. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11610. (setq type "tags"
  11611. path (match-string 1))
  11612. (while (string-match ":" path)
  11613. (setq path (replace-match "+" t t path)))
  11614. (throw 'match t))))
  11615. (unless path
  11616. (error "No link found"))
  11617. ;; Remove any trailing spaces in path
  11618. (if (string-match " +\\'" path)
  11619. (setq path (replace-match "" t t path)))
  11620. (cond
  11621. ((assoc type org-link-protocols)
  11622. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11623. ((equal type "mailto")
  11624. (let ((cmd (car org-link-mailto-program))
  11625. (args (cdr org-link-mailto-program)) args1
  11626. (address path) (subject "") a)
  11627. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11628. (setq address (match-string 1 path)
  11629. subject (org-link-escape (match-string 2 path))))
  11630. (while args
  11631. (cond
  11632. ((not (stringp (car args))) (push (pop args) args1))
  11633. (t (setq a (pop args))
  11634. (if (string-match "%a" a)
  11635. (setq a (replace-match address t t a)))
  11636. (if (string-match "%s" a)
  11637. (setq a (replace-match subject t t a)))
  11638. (push a args1))))
  11639. (apply cmd (nreverse args1))))
  11640. ((member type '("http" "https" "ftp" "news"))
  11641. (browse-url (concat type ":" (org-link-escape
  11642. path org-link-escape-chars-browser))))
  11643. ((member type '("message"))
  11644. (browse-url (concat type ":" path)))
  11645. ((string= type "tags")
  11646. (org-tags-view in-emacs path))
  11647. ((string= type "thisfile")
  11648. (if in-emacs
  11649. (switch-to-buffer-other-window
  11650. (org-get-buffer-for-internal-link (current-buffer)))
  11651. (org-mark-ring-push))
  11652. (let ((cmd `(org-link-search
  11653. ,path
  11654. ,(cond ((equal in-emacs '(4)) 'occur)
  11655. ((equal in-emacs '(16)) 'org-occur)
  11656. (t nil))
  11657. ,pos)))
  11658. (condition-case nil (eval cmd)
  11659. (error (progn (widen) (eval cmd))))))
  11660. ((string= type "tree-match")
  11661. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11662. ((string= type "file")
  11663. (if (string-match "::\\([0-9]+\\)\\'" path)
  11664. (setq line (string-to-number (match-string 1 path))
  11665. path (substring path 0 (match-beginning 0)))
  11666. (if (string-match "::\\(.+\\)\\'" path)
  11667. (setq search (match-string 1 path)
  11668. path (substring path 0 (match-beginning 0)))))
  11669. (if (string-match "[*?{]" (file-name-nondirectory path))
  11670. (dired path)
  11671. (org-open-file path in-emacs line search)))
  11672. ((string= type "news")
  11673. (require 'org-gnus)
  11674. (org-gnus-follow-link path))
  11675. ((string= type "shell")
  11676. (let ((cmd path))
  11677. (if (or (not org-confirm-shell-link-function)
  11678. (funcall org-confirm-shell-link-function
  11679. (format "Execute \"%s\" in shell? "
  11680. (org-add-props cmd nil
  11681. 'face 'org-warning))))
  11682. (progn
  11683. (message "Executing %s" cmd)
  11684. (shell-command cmd))
  11685. (error "Abort"))))
  11686. ((string= type "elisp")
  11687. (let ((cmd path))
  11688. (if (or (not org-confirm-elisp-link-function)
  11689. (funcall org-confirm-elisp-link-function
  11690. (format "Execute \"%s\" as elisp? "
  11691. (org-add-props cmd nil
  11692. 'face 'org-warning))))
  11693. (message "%s => %s" cmd (eval (read cmd)))
  11694. (error "Abort"))))
  11695. (t
  11696. (browse-url-at-point)))))
  11697. (move-marker org-open-link-marker nil)
  11698. (run-hook-with-args 'org-follow-link-hook))
  11699. ;;; File search
  11700. (defvar org-create-file-search-functions nil
  11701. "List of functions to construct the right search string for a file link.
  11702. These functions are called in turn with point at the location to
  11703. which the link should point.
  11704. A function in the hook should first test if it would like to
  11705. handle this file type, for example by checking the major-mode or
  11706. the file extension. If it decides not to handle this file, it
  11707. should just return nil to give other functions a chance. If it
  11708. does handle the file, it must return the search string to be used
  11709. when following the link. The search string will be part of the
  11710. file link, given after a double colon, and `org-open-at-point'
  11711. will automatically search for it. If special measures must be
  11712. taken to make the search successful, another function should be
  11713. added to the companion hook `org-execute-file-search-functions',
  11714. which see.
  11715. A function in this hook may also use `setq' to set the variable
  11716. `description' to provide a suggestion for the descriptive text to
  11717. be used for this link when it gets inserted into an Org-mode
  11718. buffer with \\[org-insert-link].")
  11719. (defvar org-execute-file-search-functions nil
  11720. "List of functions to execute a file search triggered by a link.
  11721. Functions added to this hook must accept a single argument, the
  11722. search string that was part of the file link, the part after the
  11723. double colon. The function must first check if it would like to
  11724. handle this search, for example by checking the major-mode or the
  11725. file extension. If it decides not to handle this search, it
  11726. should just return nil to give other functions a chance. If it
  11727. does handle the search, it must return a non-nil value to keep
  11728. other functions from trying.
  11729. Each function can access the current prefix argument through the
  11730. variable `current-prefix-argument'. Note that a single prefix is
  11731. used to force opening a link in Emacs, so it may be good to only
  11732. use a numeric or double prefix to guide the search function.
  11733. In case this is needed, a function in this hook can also restore
  11734. the window configuration before `org-open-at-point' was called using:
  11735. (set-window-configuration org-window-config-before-follow-link)")
  11736. (defun org-link-search (s &optional type avoid-pos)
  11737. "Search for a link search option.
  11738. If S is surrounded by forward slashes, it is interpreted as a
  11739. regular expression. In org-mode files, this will create an `org-occur'
  11740. sparse tree. In ordinary files, `occur' will be used to list matches.
  11741. If the current buffer is in `dired-mode', grep will be used to search
  11742. in all files. If AVOID-POS is given, ignore matches near that position."
  11743. (let ((case-fold-search t)
  11744. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11745. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11746. (append '(("") (" ") ("\t") ("\n"))
  11747. org-emphasis-alist)
  11748. "\\|") "\\)"))
  11749. (pos (point))
  11750. (pre "") (post "")
  11751. words re0 re1 re2 re3 re4 re5 re2a reall)
  11752. (cond
  11753. ;; First check if there are any special
  11754. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11755. ;; Now try the builtin stuff
  11756. ((save-excursion
  11757. (goto-char (point-min))
  11758. (and
  11759. (re-search-forward
  11760. (concat "<<" (regexp-quote s0) ">>") nil t)
  11761. (setq pos (match-beginning 0))))
  11762. ;; There is an exact target for this
  11763. (goto-char pos))
  11764. ((string-match "^/\\(.*\\)/$" s)
  11765. ;; A regular expression
  11766. (cond
  11767. ((org-mode-p)
  11768. (org-occur (match-string 1 s)))
  11769. ;;((eq major-mode 'dired-mode)
  11770. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11771. (t (org-do-occur (match-string 1 s)))))
  11772. (t
  11773. ;; A normal search strings
  11774. (when (equal (string-to-char s) ?*)
  11775. ;; Anchor on headlines, post may include tags.
  11776. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11777. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11778. s (substring s 1)))
  11779. (remove-text-properties
  11780. 0 (length s)
  11781. '(face nil mouse-face nil keymap nil fontified nil) s)
  11782. ;; Make a series of regular expressions to find a match
  11783. (setq words (org-split-string s "[ \n\r\t]+")
  11784. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11785. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11786. "\\)" markers)
  11787. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11788. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11789. re1 (concat pre re2 post)
  11790. re3 (concat pre re4 post)
  11791. re5 (concat pre ".*" re4)
  11792. re2 (concat pre re2)
  11793. re2a (concat pre re2a)
  11794. re4 (concat pre re4)
  11795. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11796. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11797. re5 "\\)"
  11798. ))
  11799. (cond
  11800. ((eq type 'org-occur) (org-occur reall))
  11801. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11802. (t (goto-char (point-min))
  11803. (if (or (org-search-not-self 1 re0 nil t)
  11804. (org-search-not-self 1 re1 nil t)
  11805. (org-search-not-self 1 re2 nil t)
  11806. (org-search-not-self 1 re2a nil t)
  11807. (org-search-not-self 1 re3 nil t)
  11808. (org-search-not-self 1 re4 nil t)
  11809. (org-search-not-self 1 re5 nil t)
  11810. )
  11811. (goto-char (match-beginning 1))
  11812. (goto-char pos)
  11813. (error "No match")))))
  11814. (t
  11815. ;; Normal string-search
  11816. (goto-char (point-min))
  11817. (if (search-forward s nil t)
  11818. (goto-char (match-beginning 0))
  11819. (error "No match"))))
  11820. (and (org-mode-p) (org-show-context 'link-search))))
  11821. (defun org-search-not-self (group &rest args)
  11822. "Execute `re-search-forward', but only accept matches that do not
  11823. enclose the position of `org-open-link-marker'."
  11824. (let ((m org-open-link-marker))
  11825. (catch 'exit
  11826. (while (apply 're-search-forward args)
  11827. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11828. (goto-char (match-end group))
  11829. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11830. (> (match-beginning 0) (marker-position m))
  11831. (< (match-end 0) (marker-position m)))
  11832. (save-match-data
  11833. (or (not (org-in-regexp
  11834. org-bracket-link-analytic-regexp 1))
  11835. (not (match-end 4)) ; no description
  11836. (and (<= (match-beginning 4) (point))
  11837. (>= (match-end 4) (point))))))
  11838. (throw 'exit (point))))))))
  11839. (defun org-get-buffer-for-internal-link (buffer)
  11840. "Return a buffer to be used for displaying the link target of internal links."
  11841. (cond
  11842. ((not org-display-internal-link-with-indirect-buffer)
  11843. buffer)
  11844. ((string-match "(Clone)$" (buffer-name buffer))
  11845. (message "Buffer is already a clone, not making another one")
  11846. ;; we also do not modify visibility in this case
  11847. buffer)
  11848. (t ; make a new indirect buffer for displaying the link
  11849. (let* ((bn (buffer-name buffer))
  11850. (ibn (concat bn "(Clone)"))
  11851. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11852. (with-current-buffer ib (org-overview))
  11853. ib))))
  11854. (defun org-do-occur (regexp &optional cleanup)
  11855. "Call the Emacs command `occur'.
  11856. If CLEANUP is non-nil, remove the printout of the regular expression
  11857. in the *Occur* buffer. This is useful if the regex is long and not useful
  11858. to read."
  11859. (occur regexp)
  11860. (when cleanup
  11861. (let ((cwin (selected-window)) win beg end)
  11862. (when (setq win (get-buffer-window "*Occur*"))
  11863. (select-window win))
  11864. (goto-char (point-min))
  11865. (when (re-search-forward "match[a-z]+" nil t)
  11866. (setq beg (match-end 0))
  11867. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11868. (setq end (1- (match-beginning 0)))))
  11869. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11870. (goto-char (point-min))
  11871. (select-window cwin))))
  11872. ;;; The mark ring for links jumps
  11873. (defvar org-mark-ring nil
  11874. "Mark ring for positions before jumps in Org-mode.")
  11875. (defvar org-mark-ring-last-goto nil
  11876. "Last position in the mark ring used to go back.")
  11877. ;; Fill and close the ring
  11878. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11879. (loop for i from 1 to org-mark-ring-length do
  11880. (push (make-marker) org-mark-ring))
  11881. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11882. org-mark-ring)
  11883. (defun org-mark-ring-push (&optional pos buffer)
  11884. "Put the current position or POS into the mark ring and rotate it."
  11885. (interactive)
  11886. (setq pos (or pos (point)))
  11887. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11888. (move-marker (car org-mark-ring)
  11889. (or pos (point))
  11890. (or buffer (current-buffer)))
  11891. (message "%s"
  11892. (substitute-command-keys
  11893. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11894. (defun org-mark-ring-goto (&optional n)
  11895. "Jump to the previous position in the mark ring.
  11896. With prefix arg N, jump back that many stored positions. When
  11897. called several times in succession, walk through the entire ring.
  11898. Org-mode commands jumping to a different position in the current file,
  11899. or to another Org-mode file, automatically push the old position
  11900. onto the ring."
  11901. (interactive "p")
  11902. (let (p m)
  11903. (if (eq last-command this-command)
  11904. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11905. (setq p org-mark-ring))
  11906. (setq org-mark-ring-last-goto p)
  11907. (setq m (car p))
  11908. (switch-to-buffer (marker-buffer m))
  11909. (goto-char m)
  11910. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11911. (defun org-remove-angle-brackets (s)
  11912. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11913. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11914. s)
  11915. (defun org-add-angle-brackets (s)
  11916. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11917. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11918. s)
  11919. ;;; Following specific links
  11920. (defun org-follow-timestamp-link ()
  11921. (cond
  11922. ((org-at-date-range-p t)
  11923. (let ((org-agenda-start-on-weekday)
  11924. (t1 (match-string 1))
  11925. (t2 (match-string 2)))
  11926. (setq t1 (time-to-days (org-time-string-to-time t1))
  11927. t2 (time-to-days (org-time-string-to-time t2)))
  11928. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11929. ((org-at-timestamp-p t)
  11930. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11931. (substring (match-string 1) 0 10)))
  11932. 1))
  11933. (t (error "This should not happen"))))
  11934. ;;; BibTeX links
  11935. ;; Use the custom search meachnism to construct and use search strings for
  11936. ;; file links to BibTeX database entries.
  11937. (defun org-create-file-search-in-bibtex ()
  11938. "Create the search string and description for a BibTeX database entry."
  11939. (when (eq major-mode 'bibtex-mode)
  11940. ;; yes, we want to construct this search string.
  11941. ;; Make a good description for this entry, using names, year and the title
  11942. ;; Put it into the `description' variable which is dynamically scoped.
  11943. (let ((bibtex-autokey-names 1)
  11944. (bibtex-autokey-names-stretch 1)
  11945. (bibtex-autokey-name-case-convert-function 'identity)
  11946. (bibtex-autokey-name-separator " & ")
  11947. (bibtex-autokey-additional-names " et al.")
  11948. (bibtex-autokey-year-length 4)
  11949. (bibtex-autokey-name-year-separator " ")
  11950. (bibtex-autokey-titlewords 3)
  11951. (bibtex-autokey-titleword-separator " ")
  11952. (bibtex-autokey-titleword-case-convert-function 'identity)
  11953. (bibtex-autokey-titleword-length 'infty)
  11954. (bibtex-autokey-year-title-separator ": "))
  11955. (setq description (bibtex-generate-autokey)))
  11956. ;; Now parse the entry, get the key and return it.
  11957. (save-excursion
  11958. (bibtex-beginning-of-entry)
  11959. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11960. (defun org-execute-file-search-in-bibtex (s)
  11961. "Find the link search string S as a key for a database entry."
  11962. (when (eq major-mode 'bibtex-mode)
  11963. ;; Yes, we want to do the search in this file.
  11964. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11965. (goto-char (point-min))
  11966. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11967. (regexp-quote s) "[ \t\n]*,") nil t)
  11968. (goto-char (match-beginning 0)))
  11969. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11970. ;; Use double prefix to indicate that any web link should be browsed
  11971. (let ((b (current-buffer)) (p (point)))
  11972. ;; Restore the window configuration because we just use the web link
  11973. (set-window-configuration org-window-config-before-follow-link)
  11974. (save-excursion (set-buffer b) (goto-char p)
  11975. (bibtex-url)))
  11976. (recenter 0)) ; Move entry start to beginning of window
  11977. ;; return t to indicate that the search is done.
  11978. t))
  11979. ;; Finally add the functions to the right hooks.
  11980. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11981. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11982. ;; end of Bibtex link setup
  11983. ;;; Following file links
  11984. (defun org-open-file (path &optional in-emacs line search)
  11985. "Open the file at PATH.
  11986. First, this expands any special file name abbreviations. Then the
  11987. configuration variable `org-file-apps' is checked if it contains an
  11988. entry for this file type, and if yes, the corresponding command is launched.
  11989. If no application is found, Emacs simply visits the file.
  11990. With optional argument IN-EMACS, Emacs will visit the file.
  11991. Optional LINE specifies a line to go to, optional SEARCH a string to
  11992. search for. If LINE or SEARCH is given, the file will always be
  11993. opened in Emacs.
  11994. If the file does not exist, an error is thrown."
  11995. (setq in-emacs (or in-emacs line search))
  11996. (let* ((file (if (equal path "")
  11997. buffer-file-name
  11998. (substitute-in-file-name (expand-file-name path))))
  11999. (apps (append org-file-apps (org-default-apps)))
  12000. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12001. (dirp (if remp nil (file-directory-p file)))
  12002. (dfile (downcase file))
  12003. (old-buffer (current-buffer))
  12004. (old-pos (point))
  12005. (old-mode major-mode)
  12006. ext cmd)
  12007. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12008. (setq ext (match-string 1 dfile))
  12009. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12010. (setq ext (match-string 1 dfile))))
  12011. (if in-emacs
  12012. (setq cmd 'emacs)
  12013. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12014. (and dirp (cdr (assoc 'directory apps)))
  12015. (cdr (assoc ext apps))
  12016. (cdr (assoc t apps)))))
  12017. (when (eq cmd 'mailcap)
  12018. (require 'mailcap)
  12019. (mailcap-parse-mailcaps)
  12020. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12021. (command (mailcap-mime-info mime-type)))
  12022. (if (stringp command)
  12023. (setq cmd command)
  12024. (setq cmd 'emacs))))
  12025. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12026. (not (file-exists-p file))
  12027. (not org-open-non-existing-files))
  12028. (error "No such file: %s" file))
  12029. (cond
  12030. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12031. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12032. (while (string-match "['\"]%s['\"]" cmd)
  12033. (setq cmd (replace-match "%s" t t cmd)))
  12034. (while (string-match "%s" cmd)
  12035. (setq cmd (replace-match
  12036. (save-match-data (shell-quote-argument file))
  12037. t t cmd)))
  12038. (save-window-excursion
  12039. (start-process-shell-command cmd nil cmd)))
  12040. ((or (stringp cmd)
  12041. (eq cmd 'emacs))
  12042. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12043. (widen)
  12044. (if line (goto-line line)
  12045. (if search (org-link-search search))))
  12046. ((consp cmd)
  12047. (eval cmd))
  12048. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12049. (and (org-mode-p) (eq old-mode 'org-mode)
  12050. (or (not (equal old-buffer (current-buffer)))
  12051. (not (equal old-pos (point))))
  12052. (org-mark-ring-push old-pos old-buffer))))
  12053. (defun org-default-apps ()
  12054. "Return the default applications for this operating system."
  12055. (cond
  12056. ((eq system-type 'darwin)
  12057. org-file-apps-defaults-macosx)
  12058. ((eq system-type 'windows-nt)
  12059. org-file-apps-defaults-windowsnt)
  12060. (t org-file-apps-defaults-gnu)))
  12061. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12062. (defun org-file-remote-p (file)
  12063. "Test whether FILE specifies a location on a remote system.
  12064. Return non-nil if the location is indeed remote.
  12065. For example, the filename \"/user@host:/foo\" specifies a location
  12066. on the system \"/user@host:\"."
  12067. (cond ((fboundp 'file-remote-p)
  12068. (file-remote-p file))
  12069. ((fboundp 'tramp-handle-file-remote-p)
  12070. (tramp-handle-file-remote-p file))
  12071. ((and (boundp 'ange-ftp-name-format)
  12072. (string-match (car ange-ftp-name-format) file))
  12073. t)
  12074. (t nil)))
  12075. ;;;; Hooks for remember.el, and refiling
  12076. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12077. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12078. ;;;###autoload
  12079. (defun org-remember-insinuate ()
  12080. "Setup remember.el for use wiht Org-mode."
  12081. (require 'remember)
  12082. (setq remember-annotation-functions '(org-remember-annotation))
  12083. (setq remember-handler-functions '(org-remember-handler))
  12084. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12085. ;;;###autoload
  12086. (defun org-remember-annotation ()
  12087. "Return a link to the current location as an annotation for remember.el.
  12088. If you are using Org-mode files as target for data storage with
  12089. remember.el, then the annotations should include a link compatible with the
  12090. conventions in Org-mode. This function returns such a link."
  12091. (org-store-link nil))
  12092. (defconst org-remember-help
  12093. "Select a destination location for the note.
  12094. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12095. RET on headline -> Store as sublevel entry to current headline
  12096. RET at beg-of-buf -> Append to file as level 2 headline
  12097. <left>/<right> -> before/after current headline, same headings level")
  12098. (defvar org-remember-previous-location nil)
  12099. (defvar org-force-remember-template-char) ;; dynamically scoped
  12100. ;; Save the major mode of the buffer we called remember from
  12101. (defvar org-select-template-temp-major-mode nil)
  12102. ;; Temporary store the buffer where remember was called from
  12103. (defvar org-select-template-original-buffer nil)
  12104. (defun org-select-remember-template (&optional use-char)
  12105. (when org-remember-templates
  12106. (let* ((pre-selected-templates
  12107. (mapcar
  12108. (lambda (tpl)
  12109. (let ((ctxt (nth 5 tpl))
  12110. (mode org-select-template-temp-major-mode)
  12111. (buf org-select-template-original-buffer))
  12112. (and (or (not ctxt) (eq ctxt t)
  12113. (and (listp ctxt) (memq mode ctxt))
  12114. (and (functionp ctxt)
  12115. (with-current-buffer buf
  12116. ;; Protect the user-defined function from error
  12117. (condition-case nil (funcall ctxt) (error nil)))))
  12118. tpl)))
  12119. org-remember-templates))
  12120. ;; If no template at this point, add the default templates:
  12121. (pre-selected-templates1
  12122. (if (not (delq nil pre-selected-templates))
  12123. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  12124. org-remember-templates)
  12125. pre-selected-templates))
  12126. ;; Then unconditionnally add template for any contexts
  12127. (pre-selected-templates2
  12128. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  12129. org-remember-templates)
  12130. (delq nil pre-selected-templates1)))
  12131. (templates (mapcar (lambda (x)
  12132. (if (stringp (car x))
  12133. (append (list (nth 1 x) (car x)) (cddr x))
  12134. (append (list (car x) "") (cdr x))))
  12135. (delq nil pre-selected-templates2)))
  12136. (char (or use-char
  12137. (cond
  12138. ((= (length templates) 1)
  12139. (caar templates))
  12140. ((and (boundp 'org-force-remember-template-char)
  12141. org-force-remember-template-char)
  12142. (if (stringp org-force-remember-template-char)
  12143. (string-to-char org-force-remember-template-char)
  12144. org-force-remember-template-char))
  12145. (t
  12146. (message "Select template: %s"
  12147. (mapconcat
  12148. (lambda (x)
  12149. (cond
  12150. ((not (string-match "\\S-" (nth 1 x)))
  12151. (format "[%c]" (car x)))
  12152. ((equal (downcase (car x))
  12153. (downcase (aref (nth 1 x) 0)))
  12154. (format "[%c]%s" (car x)
  12155. (substring (nth 1 x) 1)))
  12156. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12157. templates " "))
  12158. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12159. (when (equal char0 ?\C-g)
  12160. (jump-to-register remember-register)
  12161. (kill-buffer remember-buffer))
  12162. char0))))))
  12163. (cddr (assoc char templates)))))
  12164. (defvar x-last-selected-text)
  12165. (defvar x-last-selected-text-primary)
  12166. ;;;###autoload
  12167. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12168. "Initialize *remember* buffer with template, invoke `org-mode'.
  12169. This function should be placed into `remember-mode-hook' and in fact requires
  12170. to be run from that hook to function properly."
  12171. (if org-remember-templates
  12172. (let* ((entry (org-select-remember-template use-char))
  12173. (tpl (car entry))
  12174. (plist-p (if org-store-link-plist t nil))
  12175. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12176. (string-match "\\S-" (nth 1 entry)))
  12177. (nth 1 entry)
  12178. org-default-notes-file))
  12179. (headline (nth 2 entry))
  12180. (v-c (or (and (eq window-system 'x)
  12181. (fboundp 'x-cut-buffer-or-selection-value)
  12182. (x-cut-buffer-or-selection-value))
  12183. (org-bound-and-true-p x-last-selected-text)
  12184. (org-bound-and-true-p x-last-selected-text-primary)
  12185. (and (> (length kill-ring) 0) (current-kill 0))))
  12186. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12187. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12188. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12189. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12190. ;; `initial' and `annotation' are bound in `remember'
  12191. (v-i (if (boundp 'initial) initial))
  12192. (v-a (if (and (boundp 'annotation) annotation)
  12193. (if (equal annotation "[[]]") "" annotation)
  12194. ""))
  12195. (v-A (if (and v-a
  12196. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12197. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12198. v-a))
  12199. (v-n user-full-name)
  12200. (org-startup-folded nil)
  12201. org-time-was-given org-end-time-was-given x
  12202. prompt completions char time pos default histvar)
  12203. (setq org-store-link-plist
  12204. (append (list :annotation v-a :initial v-i)
  12205. org-store-link-plist))
  12206. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12207. (erase-buffer)
  12208. (insert (substitute-command-keys
  12209. (format
  12210. "## Filing location: Select interactively, default, or last used:
  12211. ## %s to select file and header location interactively.
  12212. ## %s \"%s\" -> \"* %s\"
  12213. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12214. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12215. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12216. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12217. (abbreviate-file-name (or file org-default-notes-file))
  12218. (or headline "")
  12219. (or (car org-remember-previous-location) "???")
  12220. (or (cdr org-remember-previous-location) "???"))))
  12221. (insert tpl) (goto-char (point-min))
  12222. ;; Simple %-escapes
  12223. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12224. (when (and initial (equal (match-string 0) "%i"))
  12225. (save-match-data
  12226. (let* ((lead (buffer-substring
  12227. (point-at-bol) (match-beginning 0))))
  12228. (setq v-i (mapconcat 'identity
  12229. (org-split-string initial "\n")
  12230. (concat "\n" lead))))))
  12231. (replace-match
  12232. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12233. t t))
  12234. ;; %[] Insert contents of a file.
  12235. (goto-char (point-min))
  12236. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12237. (let ((start (match-beginning 0))
  12238. (end (match-end 0))
  12239. (filename (expand-file-name (match-string 1))))
  12240. (goto-char start)
  12241. (delete-region start end)
  12242. (condition-case error
  12243. (insert-file-contents filename)
  12244. (error (insert (format "%%![Couldn't insert %s: %s]"
  12245. filename error))))))
  12246. ;; %() embedded elisp
  12247. (goto-char (point-min))
  12248. (while (re-search-forward "%\\((.+)\\)" nil t)
  12249. (goto-char (match-beginning 0))
  12250. (let ((template-start (point)))
  12251. (forward-char 1)
  12252. (let ((result
  12253. (condition-case error
  12254. (eval (read (current-buffer)))
  12255. (error (format "%%![Error: %s]" error)))))
  12256. (delete-region template-start (point))
  12257. (insert result))))
  12258. ;; From the property list
  12259. (when plist-p
  12260. (goto-char (point-min))
  12261. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12262. (and (setq x (or (plist-get org-store-link-plist
  12263. (intern (match-string 1))) ""))
  12264. (replace-match x t t))))
  12265. ;; Turn on org-mode in the remember buffer, set local variables
  12266. (org-mode)
  12267. (org-set-local 'org-finish-function 'org-remember-finalize)
  12268. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12269. (org-set-local 'org-default-notes-file file))
  12270. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12271. (org-set-local 'org-remember-default-headline headline))
  12272. ;; Interactive template entries
  12273. (goto-char (point-min))
  12274. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12275. (setq char (if (match-end 3) (match-string 3))
  12276. prompt (if (match-end 2) (match-string 2)))
  12277. (goto-char (match-beginning 0))
  12278. (replace-match "")
  12279. (setq completions nil default nil)
  12280. (when prompt
  12281. (setq completions (org-split-string prompt "|")
  12282. prompt (pop completions)
  12283. default (car completions)
  12284. histvar (intern (concat
  12285. "org-remember-template-prompt-history::"
  12286. (or prompt "")))
  12287. completions (mapcar 'list completions)))
  12288. (cond
  12289. ((member char '("G" "g"))
  12290. (let* ((org-last-tags-completion-table
  12291. (org-global-tags-completion-table
  12292. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12293. (org-add-colon-after-tag-completion t)
  12294. (ins (completing-read
  12295. (if prompt (concat prompt ": ") "Tags: ")
  12296. 'org-tags-completion-function nil nil nil
  12297. 'org-tags-history)))
  12298. (setq ins (mapconcat 'identity
  12299. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12300. ":"))
  12301. (when (string-match "\\S-" ins)
  12302. (or (equal (char-before) ?:) (insert ":"))
  12303. (insert ins)
  12304. (or (equal (char-after) ?:) (insert ":")))))
  12305. (char
  12306. (setq org-time-was-given (equal (upcase char) char))
  12307. (setq time (org-read-date (equal (upcase char) "U") t nil
  12308. prompt))
  12309. (org-insert-time-stamp time org-time-was-given
  12310. (member char '("u" "U"))
  12311. nil nil (list org-end-time-was-given)))
  12312. (t
  12313. (insert (org-completing-read
  12314. (concat (if prompt prompt "Enter string")
  12315. (if default (concat " [" default "]"))
  12316. ": ")
  12317. completions nil nil nil histvar default)))))
  12318. (goto-char (point-min))
  12319. (if (re-search-forward "%\\?" nil t)
  12320. (replace-match "")
  12321. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12322. (org-mode)
  12323. (org-set-local 'org-finish-function 'org-remember-finalize))
  12324. (when (save-excursion
  12325. (goto-char (point-min))
  12326. (re-search-forward "%!" nil t))
  12327. (replace-match "")
  12328. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12329. (defun org-remember-finish-immediately ()
  12330. "File remember note immediately.
  12331. This should be run in `post-command-hook' and will remove itself
  12332. from that hook."
  12333. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12334. (when org-finish-function
  12335. (funcall org-finish-function)))
  12336. (defvar org-clock-marker) ; Defined below
  12337. (defun org-remember-finalize ()
  12338. "Finalize the remember process."
  12339. (unless (fboundp 'remember-finalize)
  12340. (defalias 'remember-finalize 'remember-buffer))
  12341. (when (and org-clock-marker
  12342. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12343. ;; FIXME: test this, this is w/o notetaking!
  12344. (let (org-log-note-clock-out) (org-clock-out)))
  12345. (when buffer-file-name
  12346. (save-buffer)
  12347. (setq buffer-file-name nil))
  12348. (remember-finalize))
  12349. ;;;###autoload
  12350. (defun org-remember (&optional goto org-force-remember-template-char)
  12351. "Call `remember'. If this is already a remember buffer, re-apply template.
  12352. If there is an active region, make sure remember uses it as initial content
  12353. of the remember buffer.
  12354. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12355. anything, just go to the file/headline where the selected template usually
  12356. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12357. note stored by remember.
  12358. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12359. associated with a template in `org-remember-templates'."
  12360. (interactive "P")
  12361. (cond
  12362. ((equal goto '(4)) (org-go-to-remember-target))
  12363. ((equal goto '(16)) (org-remember-goto-last-stored))
  12364. (t
  12365. ;; set temporary variables that will be needed in
  12366. ;; `org-select-remember-template'
  12367. (setq org-select-template-temp-major-mode major-mode)
  12368. (setq org-select-template-original-buffer (current-buffer))
  12369. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12370. (progn
  12371. (when (< (length org-remember-templates) 2)
  12372. (error "No other template available"))
  12373. (erase-buffer)
  12374. (let ((annotation (plist-get org-store-link-plist :annotation))
  12375. (initial (plist-get org-store-link-plist :initial)))
  12376. (org-remember-apply-template))
  12377. (message "Press C-c C-c to remember data"))
  12378. (if (org-region-active-p)
  12379. (remember (buffer-substring (point) (mark)))
  12380. (call-interactively 'remember))))))
  12381. (defun org-remember-goto-last-stored ()
  12382. "Go to the location where the last remember note was stored."
  12383. (interactive)
  12384. (bookmark-jump "org-remember-last-stored")
  12385. (message "This is the last note stored by remember"))
  12386. (defun org-go-to-remember-target (&optional template-key)
  12387. "Go to the target location of a remember template.
  12388. The user is queried for the template."
  12389. (interactive)
  12390. (let* (org-select-template-temp-major-mode
  12391. (entry (org-select-remember-template template-key))
  12392. (file (nth 1 entry))
  12393. (heading (nth 2 entry))
  12394. visiting)
  12395. (unless (and file (stringp file) (string-match "\\S-" file))
  12396. (setq file org-default-notes-file))
  12397. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12398. (setq heading org-remember-default-headline))
  12399. (setq visiting (org-find-base-buffer-visiting file))
  12400. (if (not visiting) (find-file-noselect file))
  12401. (switch-to-buffer (or visiting (get-file-buffer file)))
  12402. (widen)
  12403. (goto-char (point-min))
  12404. (if (re-search-forward
  12405. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12406. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12407. nil t)
  12408. (goto-char (match-beginning 0))
  12409. (error "Target headline not found: %s" heading))))
  12410. (defvar org-note-abort nil) ; dynamically scoped
  12411. ;;;###autoload
  12412. (defun org-remember-handler ()
  12413. "Store stuff from remember.el into an org file.
  12414. First prompts for an org file. If the user just presses return, the value
  12415. of `org-default-notes-file' is used.
  12416. Then the command offers the headings tree of the selected file in order to
  12417. file the text at a specific location.
  12418. You can either immediately press RET to get the note appended to the
  12419. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12420. find a better place. Then press RET or <left> or <right> in insert the note.
  12421. Key Cursor position Note gets inserted
  12422. -----------------------------------------------------------------------------
  12423. RET buffer-start as level 1 heading at end of file
  12424. RET on headline as sublevel of the heading at cursor
  12425. RET no heading at cursor position, level taken from context.
  12426. Or use prefix arg to specify level manually.
  12427. <left> on headline as same level, before current heading
  12428. <right> on headline as same level, after current heading
  12429. So the fastest way to store the note is to press RET RET to append it to
  12430. the default file. This way your current train of thought is not
  12431. interrupted, in accordance with the principles of remember.el.
  12432. You can also get the fast execution without prompting by using
  12433. C-u C-c C-c to exit the remember buffer. See also the variable
  12434. `org-remember-store-without-prompt'.
  12435. Before being stored away, the function ensures that the text has a
  12436. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12437. is constructed from the current date and some additional data.
  12438. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12439. also indented so that it starts in the same column as the headline
  12440. \(i.e. after the stars).
  12441. See also the variable `org-reverse-note-order'."
  12442. (goto-char (point-min))
  12443. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12444. (replace-match ""))
  12445. (goto-char (point-max))
  12446. (beginning-of-line 1)
  12447. (while (looking-at "[ \t]*$\\|##.*")
  12448. (delete-region (1- (point)) (point-max))
  12449. (beginning-of-line 1))
  12450. (catch 'quit
  12451. (if org-note-abort (throw 'quit nil))
  12452. (let* ((txt (buffer-substring (point-min) (point-max)))
  12453. (fastp (org-xor (equal current-prefix-arg '(4))
  12454. org-remember-store-without-prompt))
  12455. (file (cond
  12456. (fastp org-default-notes-file)
  12457. ((and (eq org-remember-interactive-interface 'refile)
  12458. org-refile-targets)
  12459. org-default-notes-file)
  12460. ((not (and (equal current-prefix-arg '(16))
  12461. org-remember-previous-location))
  12462. (org-get-org-file))))
  12463. (heading org-remember-default-headline)
  12464. (visiting (and file (org-find-base-buffer-visiting file)))
  12465. (org-startup-folded nil)
  12466. (org-startup-align-all-tables nil)
  12467. (org-goto-start-pos 1)
  12468. spos exitcmd level indent reversed)
  12469. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12470. (setq file (car org-remember-previous-location)
  12471. heading (cdr org-remember-previous-location)
  12472. fastp t))
  12473. (setq current-prefix-arg nil)
  12474. (if (string-match "[ \t\n]+\\'" txt)
  12475. (setq txt (replace-match "" t t txt)))
  12476. ;; Modify text so that it becomes a nice subtree which can be inserted
  12477. ;; into an org tree.
  12478. (let* ((lines (split-string txt "\n"))
  12479. first)
  12480. (setq first (car lines) lines (cdr lines))
  12481. (if (string-match "^\\*+ " first)
  12482. ;; Is already a headline
  12483. (setq indent nil)
  12484. ;; We need to add a headline: Use time and first buffer line
  12485. (setq lines (cons first lines)
  12486. first (concat "* " (current-time-string)
  12487. " (" (remember-buffer-desc) ")")
  12488. indent " "))
  12489. (if (and org-adapt-indentation indent)
  12490. (setq lines (mapcar
  12491. (lambda (x)
  12492. (if (string-match "\\S-" x)
  12493. (concat indent x) x))
  12494. lines)))
  12495. (setq txt (concat first "\n"
  12496. (mapconcat 'identity lines "\n"))))
  12497. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12498. (setq txt (replace-match "\n\n" t t txt))
  12499. (if (string-match "[ \t\n]*\\'" txt)
  12500. (setq txt (replace-match "\n" t t txt))))
  12501. ;; Put the modified text back into the remember buffer, for refile.
  12502. (erase-buffer)
  12503. (insert txt)
  12504. (goto-char (point-min))
  12505. (when (and (eq org-remember-interactive-interface 'refile)
  12506. (not fastp))
  12507. (org-refile nil (or visiting (find-file-noselect file)))
  12508. (throw 'quit t))
  12509. ;; Find the file
  12510. (if (not visiting) (find-file-noselect file))
  12511. (with-current-buffer (or visiting (get-file-buffer file))
  12512. (unless (org-mode-p)
  12513. (error "Target files for remember notes must be in Org-mode"))
  12514. (save-excursion
  12515. (save-restriction
  12516. (widen)
  12517. (and (goto-char (point-min))
  12518. (not (re-search-forward "^\\* " nil t))
  12519. (insert "\n* " (or heading "Notes") "\n"))
  12520. (setq reversed (org-notes-order-reversed-p))
  12521. ;; Find the default location
  12522. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12523. (goto-char (point-min))
  12524. (if (re-search-forward
  12525. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12526. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12527. nil t)
  12528. (setq org-goto-start-pos (match-beginning 0))
  12529. (when fastp
  12530. (goto-char (point-max))
  12531. (unless (bolp) (newline))
  12532. (insert "* " heading "\n")
  12533. (setq org-goto-start-pos (point-at-bol 0)))))
  12534. ;; Ask the User for a location, using the appropriate interface
  12535. (cond
  12536. (fastp (setq spos org-goto-start-pos
  12537. exitcmd 'return))
  12538. ((eq org-remember-interactive-interface 'outline)
  12539. (setq spos (org-get-location (current-buffer)
  12540. org-remember-help)
  12541. exitcmd (cdr spos)
  12542. spos (car spos)))
  12543. ((eq org-remember-interactive-interface 'outline-path-completion)
  12544. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12545. (org-refile-use-outline-path t))
  12546. (setq spos (org-refile-get-location "Heading: ")
  12547. exitcmd 'return
  12548. spos (nth 3 spos))))
  12549. (t (error "this should not hapen")))
  12550. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12551. ; not handle this note
  12552. (goto-char spos)
  12553. (cond ((org-on-heading-p t)
  12554. (org-back-to-heading t)
  12555. (setq level (funcall outline-level))
  12556. (cond
  12557. ((eq exitcmd 'return)
  12558. ;; sublevel of current
  12559. (setq org-remember-previous-location
  12560. (cons (abbreviate-file-name file)
  12561. (org-get-heading 'notags)))
  12562. (if reversed
  12563. (outline-next-heading)
  12564. (org-end-of-subtree t)
  12565. (if (not (bolp))
  12566. (if (looking-at "[ \t]*\n")
  12567. (beginning-of-line 2)
  12568. (end-of-line 1)
  12569. (insert "\n"))))
  12570. (bookmark-set "org-remember-last-stored")
  12571. (org-paste-subtree (org-get-valid-level level 1) txt))
  12572. ((eq exitcmd 'left)
  12573. ;; before current
  12574. (bookmark-set "org-remember-last-stored")
  12575. (org-paste-subtree level txt))
  12576. ((eq exitcmd 'right)
  12577. ;; after current
  12578. (org-end-of-subtree t)
  12579. (bookmark-set "org-remember-last-stored")
  12580. (org-paste-subtree level txt))
  12581. (t (error "This should not happen"))))
  12582. ((and (bobp) (not reversed))
  12583. ;; Put it at the end, one level below level 1
  12584. (save-restriction
  12585. (widen)
  12586. (goto-char (point-max))
  12587. (if (not (bolp)) (newline))
  12588. (bookmark-set "org-remember-last-stored")
  12589. (org-paste-subtree (org-get-valid-level 1 1) txt)))
  12590. ((and (bobp) reversed)
  12591. ;; Put it at the start, as level 1
  12592. (save-restriction
  12593. (widen)
  12594. (goto-char (point-min))
  12595. (re-search-forward "^\\*+ " nil t)
  12596. (beginning-of-line 1)
  12597. (bookmark-set "org-remember-last-stored")
  12598. (org-paste-subtree 1 txt)))
  12599. (t
  12600. ;; Put it right there, with automatic level determined by
  12601. ;; org-paste-subtree or from prefix arg
  12602. (bookmark-set "org-remember-last-stored")
  12603. (org-paste-subtree
  12604. (if (numberp current-prefix-arg) current-prefix-arg)
  12605. txt)))
  12606. (when remember-save-after-remembering
  12607. (save-buffer)
  12608. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12609. t) ;; return t to indicate that we took care of this note.
  12610. (defun org-get-org-file ()
  12611. "Read a filename, with default directory `org-directory'."
  12612. (let ((default (or org-default-notes-file remember-data-file)))
  12613. (read-file-name (format "File name [%s]: " default)
  12614. (file-name-as-directory org-directory)
  12615. default)))
  12616. (defun org-notes-order-reversed-p ()
  12617. "Check if the current file should receive notes in reversed order."
  12618. (cond
  12619. ((not org-reverse-note-order) nil)
  12620. ((eq t org-reverse-note-order) t)
  12621. ((not (listp org-reverse-note-order)) nil)
  12622. (t (catch 'exit
  12623. (let ((all org-reverse-note-order)
  12624. entry)
  12625. (while (setq entry (pop all))
  12626. (if (string-match (car entry) buffer-file-name)
  12627. (throw 'exit (cdr entry))))
  12628. nil)))))
  12629. ;;; Refiling
  12630. (defvar org-refile-target-table nil
  12631. "The list of refile targets, created by `org-refile'.")
  12632. (defvar org-agenda-new-buffers nil
  12633. "Buffers created to visit agenda files.")
  12634. (defun org-get-refile-targets (&optional default-buffer)
  12635. "Produce a table with refile targets."
  12636. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12637. targets txt re files f desc descre)
  12638. (with-current-buffer (or default-buffer (current-buffer))
  12639. (while (setq entry (pop entries))
  12640. (setq files (car entry) desc (cdr entry))
  12641. (cond
  12642. ((null files) (setq files (list (current-buffer))))
  12643. ((eq files 'org-agenda-files)
  12644. (setq files (org-agenda-files 'unrestricted)))
  12645. ((and (symbolp files) (fboundp files))
  12646. (setq files (funcall files)))
  12647. ((and (symbolp files) (boundp files))
  12648. (setq files (symbol-value files))))
  12649. (if (stringp files) (setq files (list files)))
  12650. (cond
  12651. ((eq (car desc) :tag)
  12652. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12653. ((eq (car desc) :todo)
  12654. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12655. ((eq (car desc) :regexp)
  12656. (setq descre (cdr desc)))
  12657. ((eq (car desc) :level)
  12658. (setq descre (concat "^\\*\\{" (number-to-string
  12659. (if org-odd-levels-only
  12660. (1- (* 2 (cdr desc)))
  12661. (cdr desc)))
  12662. "\\}[ \t]")))
  12663. ((eq (car desc) :maxlevel)
  12664. (setq descre (concat "^\\*\\{1," (number-to-string
  12665. (if org-odd-levels-only
  12666. (1- (* 2 (cdr desc)))
  12667. (cdr desc)))
  12668. "\\}[ \t]")))
  12669. (t (error "Bad refiling target description %s" desc)))
  12670. (while (setq f (pop files))
  12671. (save-excursion
  12672. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12673. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12674. (save-excursion
  12675. (save-restriction
  12676. (widen)
  12677. (goto-char (point-min))
  12678. (while (re-search-forward descre nil t)
  12679. (goto-char (point-at-bol))
  12680. (when (looking-at org-complex-heading-regexp)
  12681. (setq txt (match-string 4)
  12682. re (concat "^" (regexp-quote
  12683. (buffer-substring (match-beginning 1)
  12684. (match-end 4)))))
  12685. (if (match-end 5) (setq re (concat re "[ \t]+"
  12686. (regexp-quote
  12687. (match-string 5)))))
  12688. (setq re (concat re "[ \t]*$"))
  12689. (when org-refile-use-outline-path
  12690. (setq txt (mapconcat 'identity
  12691. (append
  12692. (if (eq org-refile-use-outline-path 'file)
  12693. (list (file-name-nondirectory
  12694. (buffer-file-name (buffer-base-buffer))))
  12695. (if (eq org-refile-use-outline-path 'full-file-path)
  12696. (list (buffer-file-name (buffer-base-buffer)))))
  12697. (org-get-outline-path)
  12698. (list txt))
  12699. "/")))
  12700. (push (list txt f re (point)) targets))
  12701. (goto-char (point-at-eol))))))))
  12702. (nreverse targets))))
  12703. (defun org-get-outline-path ()
  12704. "Return the outline path to the current entry, as a list."
  12705. (let (rtn)
  12706. (save-excursion
  12707. (while (org-up-heading-safe)
  12708. (when (looking-at org-complex-heading-regexp)
  12709. (push (org-match-string-no-properties 4) rtn)))
  12710. rtn)))
  12711. (defvar org-refile-history nil
  12712. "History for refiling operations.")
  12713. (defun org-refile (&optional goto default-buffer)
  12714. "Move the entry at point to another heading.
  12715. The list of target headings is compiled using the information in
  12716. `org-refile-targets', which see. This list is created upon first use, and
  12717. you can update it by calling this command with a double prefix (`C-u C-u').
  12718. FIXME: Can we find a better way of updating?
  12719. At the target location, the entry is filed as a subitem of the target heading.
  12720. Depending on `org-reverse-note-order', the new subitem will either be the
  12721. first of the last subitem.
  12722. With prefix arg GOTO, the command will only visit the target location,
  12723. not actually move anything.
  12724. With a double prefix `C-c C-c', go to the location where the last refiling
  12725. operation has put the subtree.
  12726. With a double prefix argument, the command can be used to jump to any
  12727. heading in the current buffer."
  12728. (interactive "P")
  12729. (let* ((cbuf (current-buffer))
  12730. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12731. pos it nbuf file re level reversed)
  12732. (if (equal goto '(16))
  12733. (org-refile-goto-last-stored)
  12734. (when (setq it (org-refile-get-location
  12735. (if goto "Goto: " "Refile to: ") default-buffer))
  12736. (setq file (nth 1 it)
  12737. re (nth 2 it)
  12738. pos (nth 3 it))
  12739. (setq nbuf (or (find-buffer-visiting file)
  12740. (find-file-noselect file)))
  12741. (if goto
  12742. (progn
  12743. (switch-to-buffer nbuf)
  12744. (goto-char pos)
  12745. (org-show-context 'org-goto))
  12746. (org-copy-special)
  12747. (save-excursion
  12748. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12749. (find-file-noselect file))))
  12750. (setq reversed (org-notes-order-reversed-p))
  12751. (save-excursion
  12752. (save-restriction
  12753. (widen)
  12754. (goto-char pos)
  12755. (looking-at outline-regexp)
  12756. (setq level (org-get-valid-level (funcall outline-level) 1))
  12757. (goto-char
  12758. (if reversed
  12759. (outline-next-heading)
  12760. (or (save-excursion (outline-get-next-sibling))
  12761. (org-end-of-subtree t t)
  12762. (point-max))))
  12763. (bookmark-set "org-refile-last-stored")
  12764. (org-paste-subtree level))))
  12765. (org-cut-special)
  12766. (message "Entry refiled to \"%s\"" (car it)))))))
  12767. (defun org-refile-goto-last-stored ()
  12768. "Go to the location where the last refile was stored."
  12769. (interactive)
  12770. (bookmark-jump "org-refile-last-stored")
  12771. (message "This is the location of the last refile"))
  12772. (defun org-refile-get-location (&optional prompt default-buffer)
  12773. "Prompt the user for a refile location, using PROMPT."
  12774. (let ((org-refile-targets org-refile-targets)
  12775. (org-refile-use-outline-path org-refile-use-outline-path))
  12776. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12777. (unless org-refile-target-table
  12778. (error "No refile targets"))
  12779. (let* ((cbuf (current-buffer))
  12780. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12781. (fname (and filename (file-truename filename)))
  12782. (tbl (mapcar
  12783. (lambda (x)
  12784. (if (not (equal fname (file-truename (nth 1 x))))
  12785. (cons (concat (car x) " (" (file-name-nondirectory
  12786. (nth 1 x)) ")")
  12787. (cdr x))
  12788. x))
  12789. org-refile-target-table))
  12790. (completion-ignore-case t))
  12791. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12792. tbl)))
  12793. ;;;; Dynamic blocks
  12794. (defun org-find-dblock (name)
  12795. "Find the first dynamic block with name NAME in the buffer.
  12796. If not found, stay at current position and return nil."
  12797. (let (pos)
  12798. (save-excursion
  12799. (goto-char (point-min))
  12800. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12801. nil t)
  12802. (match-beginning 0))))
  12803. (if pos (goto-char pos))
  12804. pos))
  12805. (defconst org-dblock-start-re
  12806. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12807. "Matches the startline of a dynamic block, with parameters.")
  12808. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12809. "Matches the end of a dyhamic block.")
  12810. (defun org-create-dblock (plist)
  12811. "Create a dynamic block section, with parameters taken from PLIST.
  12812. PLIST must containe a :name entry which is used as name of the block."
  12813. (unless (bolp) (newline))
  12814. (let ((name (plist-get plist :name)))
  12815. (insert "#+BEGIN: " name)
  12816. (while plist
  12817. (if (eq (car plist) :name)
  12818. (setq plist (cddr plist))
  12819. (insert " " (prin1-to-string (pop plist)))))
  12820. (insert "\n\n#+END:\n")
  12821. (beginning-of-line -2)))
  12822. (defun org-prepare-dblock ()
  12823. "Prepare dynamic block for refresh.
  12824. This empties the block, puts the cursor at the insert position and returns
  12825. the property list including an extra property :name with the block name."
  12826. (unless (looking-at org-dblock-start-re)
  12827. (error "Not at a dynamic block"))
  12828. (let* ((begdel (1+ (match-end 0)))
  12829. (name (org-no-properties (match-string 1)))
  12830. (params (append (list :name name)
  12831. (read (concat "(" (match-string 3) ")")))))
  12832. (unless (re-search-forward org-dblock-end-re nil t)
  12833. (error "Dynamic block not terminated"))
  12834. (setq params
  12835. (append params
  12836. (list :content (buffer-substring
  12837. begdel (match-beginning 0)))))
  12838. (delete-region begdel (match-beginning 0))
  12839. (goto-char begdel)
  12840. (open-line 1)
  12841. params))
  12842. (defun org-map-dblocks (&optional command)
  12843. "Apply COMMAND to all dynamic blocks in the current buffer.
  12844. If COMMAND is not given, use `org-update-dblock'."
  12845. (let ((cmd (or command 'org-update-dblock))
  12846. pos)
  12847. (save-excursion
  12848. (goto-char (point-min))
  12849. (while (re-search-forward org-dblock-start-re nil t)
  12850. (goto-char (setq pos (match-beginning 0)))
  12851. (condition-case nil
  12852. (funcall cmd)
  12853. (error (message "Error during update of dynamic block")))
  12854. (goto-char pos)
  12855. (unless (re-search-forward org-dblock-end-re nil t)
  12856. (error "Dynamic block not terminated"))))))
  12857. (defun org-dblock-update (&optional arg)
  12858. "User command for updating dynamic blocks.
  12859. Update the dynamic block at point. With prefix ARG, update all dynamic
  12860. blocks in the buffer."
  12861. (interactive "P")
  12862. (if arg
  12863. (org-update-all-dblocks)
  12864. (or (looking-at org-dblock-start-re)
  12865. (org-beginning-of-dblock))
  12866. (org-update-dblock)))
  12867. (defun org-update-dblock ()
  12868. "Update the dynamic block at point
  12869. This means to empty the block, parse for parameters and then call
  12870. the correct writing function."
  12871. (save-window-excursion
  12872. (let* ((pos (point))
  12873. (line (org-current-line))
  12874. (params (org-prepare-dblock))
  12875. (name (plist-get params :name))
  12876. (cmd (intern (concat "org-dblock-write:" name))))
  12877. (message "Updating dynamic block `%s' at line %d..." name line)
  12878. (funcall cmd params)
  12879. (message "Updating dynamic block `%s' at line %d...done" name line)
  12880. (goto-char pos))))
  12881. (defun org-beginning-of-dblock ()
  12882. "Find the beginning of the dynamic block at point.
  12883. Error if there is no scuh block at point."
  12884. (let ((pos (point))
  12885. beg)
  12886. (end-of-line 1)
  12887. (if (and (re-search-backward org-dblock-start-re nil t)
  12888. (setq beg (match-beginning 0))
  12889. (re-search-forward org-dblock-end-re nil t)
  12890. (> (match-end 0) pos))
  12891. (goto-char beg)
  12892. (goto-char pos)
  12893. (error "Not in a dynamic block"))))
  12894. (defun org-update-all-dblocks ()
  12895. "Update all dynamic blocks in the buffer.
  12896. This function can be used in a hook."
  12897. (when (org-mode-p)
  12898. (org-map-dblocks 'org-update-dblock)))
  12899. ;;;; Completion
  12900. (defconst org-additional-option-like-keywords
  12901. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12902. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  12903. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12904. (defun org-complete (&optional arg)
  12905. "Perform completion on word at point.
  12906. At the beginning of a headline, this completes TODO keywords as given in
  12907. `org-todo-keywords'.
  12908. If the current word is preceded by a backslash, completes the TeX symbols
  12909. that are supported for HTML support.
  12910. If the current word is preceded by \"#+\", completes special words for
  12911. setting file options.
  12912. In the line after \"#+STARTUP:, complete valid keywords.\"
  12913. At all other locations, this simply calls the value of
  12914. `org-completion-fallback-command'."
  12915. (interactive "P")
  12916. (org-without-partial-completion
  12917. (catch 'exit
  12918. (let* ((end (point))
  12919. (beg1 (save-excursion
  12920. (skip-chars-backward (org-re "[:alnum:]_@"))
  12921. (point)))
  12922. (beg (save-excursion
  12923. (skip-chars-backward "a-zA-Z0-9_:$")
  12924. (point)))
  12925. (confirm (lambda (x) (stringp (car x))))
  12926. (searchhead (equal (char-before beg) ?*))
  12927. (tag (and (equal (char-before beg1) ?:)
  12928. (equal (char-after (point-at-bol)) ?*)))
  12929. (prop (and (equal (char-before beg1) ?:)
  12930. (not (equal (char-after (point-at-bol)) ?*))))
  12931. (texp (equal (char-before beg) ?\\))
  12932. (link (equal (char-before beg) ?\[))
  12933. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12934. beg)
  12935. "#+"))
  12936. (startup (string-match "^#\\+STARTUP:.*"
  12937. (buffer-substring (point-at-bol) (point))))
  12938. (completion-ignore-case opt)
  12939. (type nil)
  12940. (tbl nil)
  12941. (table (cond
  12942. (opt
  12943. (setq type :opt)
  12944. (append
  12945. (mapcar
  12946. (lambda (x)
  12947. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12948. (cons (match-string 2 x) (match-string 1 x)))
  12949. (org-split-string (org-get-current-options) "\n"))
  12950. (mapcar 'list org-additional-option-like-keywords)))
  12951. (startup
  12952. (setq type :startup)
  12953. org-startup-options)
  12954. (link (append org-link-abbrev-alist-local
  12955. org-link-abbrev-alist))
  12956. (texp
  12957. (setq type :tex)
  12958. org-html-entities)
  12959. ((string-match "\\`\\*+[ \t]+\\'"
  12960. (buffer-substring (point-at-bol) beg))
  12961. (setq type :todo)
  12962. (mapcar 'list org-todo-keywords-1))
  12963. (searchhead
  12964. (setq type :searchhead)
  12965. (save-excursion
  12966. (goto-char (point-min))
  12967. (while (re-search-forward org-todo-line-regexp nil t)
  12968. (push (list
  12969. (org-make-org-heading-search-string
  12970. (match-string 3) t))
  12971. tbl)))
  12972. tbl)
  12973. (tag (setq type :tag beg beg1)
  12974. (or org-tag-alist (org-get-buffer-tags)))
  12975. (prop (setq type :prop beg beg1)
  12976. (mapcar 'list (org-buffer-property-keys nil t t)))
  12977. (t (progn
  12978. (call-interactively org-completion-fallback-command)
  12979. (throw 'exit nil)))))
  12980. (pattern (buffer-substring-no-properties beg end))
  12981. (completion (try-completion pattern table confirm)))
  12982. (cond ((eq completion t)
  12983. (if (not (assoc (upcase pattern) table))
  12984. (message "Already complete")
  12985. (if (and (equal type :opt)
  12986. (not (member (car (assoc (upcase pattern) table))
  12987. org-additional-option-like-keywords)))
  12988. (insert (substring (cdr (assoc (upcase pattern) table))
  12989. (length pattern)))
  12990. (if (memq type '(:tag :prop)) (insert ":")))))
  12991. ((null completion)
  12992. (message "Can't find completion for \"%s\"" pattern)
  12993. (ding))
  12994. ((not (string= pattern completion))
  12995. (delete-region beg end)
  12996. (if (string-match " +$" completion)
  12997. (setq completion (replace-match "" t t completion)))
  12998. (insert completion)
  12999. (if (get-buffer-window "*Completions*")
  13000. (delete-window (get-buffer-window "*Completions*")))
  13001. (if (assoc completion table)
  13002. (if (eq type :todo) (insert " ")
  13003. (if (memq type '(:tag :prop)) (insert ":"))))
  13004. (if (and (equal type :opt) (assoc completion table))
  13005. (message "%s" (substitute-command-keys
  13006. "Press \\[org-complete] again to insert example settings"))))
  13007. (t
  13008. (message "Making completion list...")
  13009. (let ((list (sort (all-completions pattern table confirm)
  13010. 'string<)))
  13011. (with-output-to-temp-buffer "*Completions*"
  13012. (condition-case nil
  13013. ;; Protection needed for XEmacs and emacs 21
  13014. (display-completion-list list pattern)
  13015. (error (display-completion-list list)))))
  13016. (message "Making completion list...%s" "done")))))))
  13017. ;;;; TODO, DEADLINE, Comments
  13018. (defun org-toggle-comment ()
  13019. "Change the COMMENT state of an entry."
  13020. (interactive)
  13021. (save-excursion
  13022. (org-back-to-heading)
  13023. (let (case-fold-search)
  13024. (if (looking-at (concat outline-regexp
  13025. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13026. (replace-match "" t t nil 1)
  13027. (if (looking-at outline-regexp)
  13028. (progn
  13029. (goto-char (match-end 0))
  13030. (insert org-comment-string " ")))))))
  13031. (defvar org-last-todo-state-is-todo nil
  13032. "This is non-nil when the last TODO state change led to a TODO state.
  13033. If the last change removed the TODO tag or switched to DONE, then
  13034. this is nil.")
  13035. (defvar org-setting-tags nil) ; dynamically skiped
  13036. ;; FIXME: better place
  13037. (defun org-property-or-variable-value (var &optional inherit)
  13038. "Check if there is a property fixing the value of VAR.
  13039. If yes, return this value. If not, return the current value of the variable."
  13040. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13041. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13042. (read prop)
  13043. (symbol-value var))))
  13044. (defun org-parse-local-options (string var)
  13045. "Parse STRING for startup setting relevant for variable VAR."
  13046. (let ((rtn (symbol-value var))
  13047. e opts)
  13048. (save-match-data
  13049. (if (or (not string) (not (string-match "\\S-" string)))
  13050. rtn
  13051. (setq opts (delq nil (mapcar (lambda (x)
  13052. (setq e (assoc x org-startup-options))
  13053. (if (eq (nth 1 e) var) e nil))
  13054. (org-split-string string "[ \t]+"))))
  13055. (if (not opts)
  13056. rtn
  13057. (setq rtn nil)
  13058. (while (setq e (pop opts))
  13059. (if (not (nth 3 e))
  13060. (setq rtn (nth 2 e))
  13061. (if (not (listp rtn)) (setq rtn nil))
  13062. (push (nth 2 e) rtn)))
  13063. rtn)))))
  13064. (defvar org-blocker-hook nil
  13065. "Hook for functions that are allowed to block a state change.
  13066. Each function gets as its single argument a property list, see
  13067. `org-trigger-hook' for more information about this list.
  13068. If any of the functions in this hook returns nil, the state change
  13069. is blocked.")
  13070. (defvar org-trigger-hook nil
  13071. "Hook for functions that are triggered by a state change.
  13072. Each function gets as its single argument a property list with at least
  13073. the following elements:
  13074. (:type type-of-change :position pos-at-entry-start
  13075. :from old-state :to new-state)
  13076. Depending on the type, more properties may be present.
  13077. This mechanism is currently implemented for:
  13078. TODO state changes
  13079. ------------------
  13080. :type todo-state-change
  13081. :from previous state (keyword as a string), or nil
  13082. :to new state (keyword as a string), or nil")
  13083. (defun org-todo (&optional arg)
  13084. "Change the TODO state of an item.
  13085. The state of an item is given by a keyword at the start of the heading,
  13086. like
  13087. *** TODO Write paper
  13088. *** DONE Call mom
  13089. The different keywords are specified in the variable `org-todo-keywords'.
  13090. By default the available states are \"TODO\" and \"DONE\".
  13091. So for this example: when the item starts with TODO, it is changed to DONE.
  13092. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13093. DONE are present, add TODO at the beginning of the heading.
  13094. With C-u prefix arg, use completion to determine the new state.
  13095. With numeric prefix arg, switch to that state.
  13096. For calling through lisp, arg is also interpreted in the following way:
  13097. 'none -> empty state
  13098. \"\"(empty string) -> switch to empty state
  13099. 'done -> switch to DONE
  13100. 'nextset -> switch to the next set of keywords
  13101. 'previousset -> switch to the previous set of keywords
  13102. \"WAITING\" -> switch to the specified keyword, but only if it
  13103. really is a member of `org-todo-keywords'."
  13104. (interactive "P")
  13105. (save-excursion
  13106. (catch 'exit
  13107. (org-back-to-heading)
  13108. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13109. (or (looking-at (concat " +" org-todo-regexp " *"))
  13110. (looking-at " *"))
  13111. (let* ((match-data (match-data))
  13112. (startpos (point-at-bol))
  13113. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13114. (org-log-done org-log-done)
  13115. (org-log-repeat org-log-repeat)
  13116. (org-todo-log-states org-todo-log-states)
  13117. (this (match-string 1))
  13118. (hl-pos (match-beginning 0))
  13119. (head (org-get-todo-sequence-head this))
  13120. (ass (assoc head org-todo-kwd-alist))
  13121. (interpret (nth 1 ass))
  13122. (done-word (nth 3 ass))
  13123. (final-done-word (nth 4 ass))
  13124. (last-state (or this ""))
  13125. (completion-ignore-case t)
  13126. (member (member this org-todo-keywords-1))
  13127. (tail (cdr member))
  13128. (state (cond
  13129. ((and org-todo-key-trigger
  13130. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13131. (and (not arg) org-use-fast-todo-selection
  13132. (not (eq org-use-fast-todo-selection 'prefix)))))
  13133. ;; Use fast selection
  13134. (org-fast-todo-selection))
  13135. ((and (equal arg '(4))
  13136. (or (not org-use-fast-todo-selection)
  13137. (not org-todo-key-trigger)))
  13138. ;; Read a state with completion
  13139. (completing-read "State: " (mapcar (lambda(x) (list x))
  13140. org-todo-keywords-1)
  13141. nil t))
  13142. ((eq arg 'right)
  13143. (if this
  13144. (if tail (car tail) nil)
  13145. (car org-todo-keywords-1)))
  13146. ((eq arg 'left)
  13147. (if (equal member org-todo-keywords-1)
  13148. nil
  13149. (if this
  13150. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13151. org-todo-keywords-1)
  13152. (org-last org-todo-keywords-1))))
  13153. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13154. (setq arg nil))) ; hack to fall back to cycling
  13155. (arg
  13156. ;; user or caller requests a specific state
  13157. (cond
  13158. ((equal arg "") nil)
  13159. ((eq arg 'none) nil)
  13160. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13161. ((eq arg 'nextset)
  13162. (or (car (cdr (member head org-todo-heads)))
  13163. (car org-todo-heads)))
  13164. ((eq arg 'previousset)
  13165. (let ((org-todo-heads (reverse org-todo-heads)))
  13166. (or (car (cdr (member head org-todo-heads)))
  13167. (car org-todo-heads))))
  13168. ((car (member arg org-todo-keywords-1)))
  13169. ((nth (1- (prefix-numeric-value arg))
  13170. org-todo-keywords-1))))
  13171. ((null member) (or head (car org-todo-keywords-1)))
  13172. ((equal this final-done-word) nil) ;; -> make empty
  13173. ((null tail) nil) ;; -> first entry
  13174. ((eq interpret 'sequence)
  13175. (car tail))
  13176. ((memq interpret '(type priority))
  13177. (if (eq this-command last-command)
  13178. (car tail)
  13179. (if (> (length tail) 0)
  13180. (or done-word (car org-done-keywords))
  13181. nil)))
  13182. (t nil)))
  13183. (next (if state (concat " " state " ") " "))
  13184. (change-plist (list :type 'todo-state-change :from this :to state
  13185. :position startpos))
  13186. dolog now-done-p)
  13187. (when org-blocker-hook
  13188. (unless (save-excursion
  13189. (save-match-data
  13190. (run-hook-with-args-until-failure
  13191. 'org-blocker-hook change-plist)))
  13192. (if (interactive-p)
  13193. (error "TODO state change from %s to %s blocked" this state)
  13194. ;; fail silently
  13195. (message "TODO state change from %s to %s blocked" this state)
  13196. (throw 'exit nil))))
  13197. (store-match-data match-data)
  13198. (replace-match next t t)
  13199. (unless (pos-visible-in-window-p hl-pos)
  13200. (message "TODO state changed to %s" (org-trim next)))
  13201. (unless head
  13202. (setq head (org-get-todo-sequence-head state)
  13203. ass (assoc head org-todo-kwd-alist)
  13204. interpret (nth 1 ass)
  13205. done-word (nth 3 ass)
  13206. final-done-word (nth 4 ass)))
  13207. (when (memq arg '(nextset previousset))
  13208. (message "Keyword-Set %d/%d: %s"
  13209. (- (length org-todo-sets) -1
  13210. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13211. (length org-todo-sets)
  13212. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13213. (setq org-last-todo-state-is-todo
  13214. (not (member state org-done-keywords)))
  13215. (setq now-done-p (and (member state org-done-keywords)
  13216. (not (member this org-done-keywords))))
  13217. (and logging (org-local-logging logging))
  13218. (when (and (or org-todo-log-states org-log-done)
  13219. (not (memq arg '(nextset previousset))))
  13220. ;; we need to look at recording a time and note
  13221. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13222. (nth 2 (assoc this org-todo-log-states))))
  13223. (when (and state
  13224. (member state org-not-done-keywords)
  13225. (not (member this org-not-done-keywords)))
  13226. ;; This is now a todo state and was not one before
  13227. ;; If there was a CLOSED time stamp, get rid of it.
  13228. (org-add-planning-info nil nil 'closed))
  13229. (when (and now-done-p org-log-done)
  13230. ;; It is now done, and it was not done before
  13231. (org-add-planning-info 'closed (org-current-time))
  13232. (if (and (not dolog) (eq 'note org-log-done))
  13233. (org-add-log-maybe 'done state 'findpos 'note)))
  13234. (when (and state dolog)
  13235. ;; This is a non-nil state, and we need to log it
  13236. (org-add-log-maybe 'state state 'findpos dolog)))
  13237. ;; Fixup tag positioning
  13238. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13239. (run-hooks 'org-after-todo-state-change-hook)
  13240. (if (and arg (not (member state org-done-keywords)))
  13241. (setq head (org-get-todo-sequence-head state)))
  13242. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13243. ;; Do we need to trigger a repeat?
  13244. (when now-done-p (org-auto-repeat-maybe state))
  13245. ;; Fixup cursor location if close to the keyword
  13246. (if (and (outline-on-heading-p)
  13247. (not (bolp))
  13248. (save-excursion (beginning-of-line 1)
  13249. (looking-at org-todo-line-regexp))
  13250. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13251. (progn
  13252. (goto-char (or (match-end 2) (match-end 1)))
  13253. (just-one-space)))
  13254. (when org-trigger-hook
  13255. (save-excursion
  13256. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13257. (defun org-local-logging (value)
  13258. "Get logging settings from a property VALUE."
  13259. (let* (words w a)
  13260. ;; directly set the variables, they are already local.
  13261. (setq org-log-done nil
  13262. org-log-repeat nil
  13263. org-todo-log-states nil)
  13264. (setq words (org-split-string value))
  13265. (while (setq w (pop words))
  13266. (cond
  13267. ((setq a (assoc w org-startup-options))
  13268. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13269. (set (nth 1 a) (nth 2 a))))
  13270. ((setq a (org-extract-log-state-settings w))
  13271. (and (member (car a) org-todo-keywords-1)
  13272. (push a org-todo-log-states)))))))
  13273. (defun org-get-todo-sequence-head (kwd)
  13274. "Return the head of the TODO sequence to which KWD belongs.
  13275. If KWD is not set, check if there is a text property remembering the
  13276. right sequence."
  13277. (let (p)
  13278. (cond
  13279. ((not kwd)
  13280. (or (get-text-property (point-at-bol) 'org-todo-head)
  13281. (progn
  13282. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13283. nil (point-at-eol)))
  13284. (get-text-property p 'org-todo-head))))
  13285. ((not (member kwd org-todo-keywords-1))
  13286. (car org-todo-keywords-1))
  13287. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13288. (defun org-fast-todo-selection ()
  13289. "Fast TODO keyword selection with single keys.
  13290. Returns the new TODO keyword, or nil if no state change should occur."
  13291. (let* ((fulltable org-todo-key-alist)
  13292. (done-keywords org-done-keywords) ;; needed for the faces.
  13293. (maxlen (apply 'max (mapcar
  13294. (lambda (x)
  13295. (if (stringp (car x)) (string-width (car x)) 0))
  13296. fulltable)))
  13297. (expert nil)
  13298. (fwidth (+ maxlen 3 1 3))
  13299. (ncol (/ (- (window-width) 4) fwidth))
  13300. tg cnt e c tbl
  13301. groups ingroup)
  13302. (save-window-excursion
  13303. (if expert
  13304. (set-buffer (get-buffer-create " *Org todo*"))
  13305. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13306. (erase-buffer)
  13307. (org-set-local 'org-done-keywords done-keywords)
  13308. (setq tbl fulltable cnt 0)
  13309. (while (setq e (pop tbl))
  13310. (cond
  13311. ((equal e '(:startgroup))
  13312. (push '() groups) (setq ingroup t)
  13313. (when (not (= cnt 0))
  13314. (setq cnt 0)
  13315. (insert "\n"))
  13316. (insert "{ "))
  13317. ((equal e '(:endgroup))
  13318. (setq ingroup nil cnt 0)
  13319. (insert "}\n"))
  13320. (t
  13321. (setq tg (car e) c (cdr e))
  13322. (if ingroup (push tg (car groups)))
  13323. (setq tg (org-add-props tg nil 'face
  13324. (org-get-todo-face tg)))
  13325. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13326. (insert "[" c "] " tg (make-string
  13327. (- fwidth 4 (length tg)) ?\ ))
  13328. (when (= (setq cnt (1+ cnt)) ncol)
  13329. (insert "\n")
  13330. (if ingroup (insert " "))
  13331. (setq cnt 0)))))
  13332. (insert "\n")
  13333. (goto-char (point-min))
  13334. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13335. (fit-window-to-buffer))
  13336. (message "[a-z..]:Set [SPC]:clear")
  13337. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13338. (cond
  13339. ((or (= c ?\C-g)
  13340. (and (= c ?q) (not (rassoc c fulltable))))
  13341. (setq quit-flag t))
  13342. ((= c ?\ ) nil)
  13343. ((setq e (rassoc c fulltable) tg (car e))
  13344. tg)
  13345. (t (setq quit-flag t))))))
  13346. (defun org-get-repeat ()
  13347. "Check if tere is a deadline/schedule with repeater in this entry."
  13348. (save-match-data
  13349. (save-excursion
  13350. (org-back-to-heading t)
  13351. (if (re-search-forward
  13352. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13353. (match-string 1)))))
  13354. (defvar org-last-changed-timestamp)
  13355. (defvar org-log-post-message)
  13356. (defvar org-log-note-purpose)
  13357. (defun org-auto-repeat-maybe (done-word)
  13358. "Check if the current headline contains a repeated deadline/schedule.
  13359. If yes, set TODO state back to what it was and change the base date
  13360. of repeating deadline/scheduled time stamps to new date.
  13361. This function is run automatically after each state change to a DONE state."
  13362. ;; last-state is dynamically scoped into this function
  13363. (let* ((repeat (org-get-repeat))
  13364. (aa (assoc last-state org-todo-kwd-alist))
  13365. (interpret (nth 1 aa))
  13366. (head (nth 2 aa))
  13367. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13368. (msg "Entry repeats: ")
  13369. (org-log-done nil)
  13370. (org-todo-log-states nil)
  13371. (nshiftmax 10) (nshift 0)
  13372. re type n what ts mb0 time)
  13373. (when repeat
  13374. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13375. (org-todo (if (eq interpret 'type) last-state head))
  13376. (when (and org-log-repeat
  13377. (or (not (memq 'org-add-log-note
  13378. (default-value 'post-command-hook)))
  13379. (eq org-log-note-purpose 'done)))
  13380. ;; Make sure a note is taken;
  13381. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13382. 'findpos org-log-repeat))
  13383. (org-back-to-heading t)
  13384. (org-add-planning-info nil nil 'closed)
  13385. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13386. org-deadline-time-regexp "\\)\\|\\("
  13387. org-ts-regexp "\\)"))
  13388. (while (re-search-forward
  13389. re (save-excursion (outline-next-heading) (point)) t)
  13390. (setq type (if (match-end 1) org-scheduled-string
  13391. (if (match-end 3) org-deadline-string "Plain:"))
  13392. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  13393. mb0 (match-beginning 0))
  13394. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  13395. (setq n (string-to-number (match-string 2 ts))
  13396. what (match-string 3 ts))
  13397. (if (equal what "w") (setq n (* n 7) what "d"))
  13398. ;; Preparation, see if we need to modify the start date for the change
  13399. (when (match-end 1)
  13400. (setq time (save-match-data (org-time-string-to-time ts)))
  13401. (cond
  13402. ((equal (match-string 1 ts) ".")
  13403. ;; Shift starting date to today
  13404. (org-timestamp-change
  13405. (- (time-to-days (current-time)) (time-to-days time))
  13406. 'day))
  13407. ((equal (match-string 1 ts) "+")
  13408. (while (< (time-to-days time) (time-to-days (current-time)))
  13409. (when (= (incf nshift) nshiftmax)
  13410. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  13411. (error "Abort")))
  13412. (org-timestamp-change n (cdr (assoc what whata)))
  13413. (sit-for .0001) ;; so we can watch the date shifting
  13414. (org-at-timestamp-p t)
  13415. (setq ts (match-string 1))
  13416. (setq time (save-match-data (org-time-string-to-time ts))))
  13417. (org-timestamp-change (- n) (cdr (assoc what whata)))
  13418. ;; rematch, so that we have everything in place for the real shift
  13419. (org-at-timestamp-p t)
  13420. (setq ts (match-string 1))
  13421. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  13422. (org-timestamp-change n (cdr (assoc what whata)))
  13423. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13424. (setq org-log-post-message msg)
  13425. (message "%s" msg))))
  13426. (defun org-show-todo-tree (arg)
  13427. "Make a compact tree which shows all headlines marked with TODO.
  13428. The tree will show the lines where the regexp matches, and all higher
  13429. headlines above the match.
  13430. With a \\[universal-argument] prefix, also show the DONE entries.
  13431. With a numeric prefix N, construct a sparse tree for the Nth element
  13432. of `org-todo-keywords-1'."
  13433. (interactive "P")
  13434. (let ((case-fold-search nil)
  13435. (kwd-re
  13436. (cond ((null arg) org-not-done-regexp)
  13437. ((equal arg '(4))
  13438. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13439. (mapcar 'list org-todo-keywords-1))))
  13440. (concat "\\("
  13441. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13442. "\\)\\>")))
  13443. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13444. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13445. org-todo-keywords-1)))
  13446. (t (error "Invalid prefix argument: %s" arg)))))
  13447. (message "%d TODO entries found"
  13448. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13449. (defun org-deadline (&optional remove)
  13450. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13451. With argument REMOVE, remove any deadline from the item."
  13452. (interactive "P")
  13453. (if remove
  13454. (progn
  13455. (org-remove-timestamp-with-keyword org-deadline-string)
  13456. (message "Item no longer has a deadline."))
  13457. (org-add-planning-info 'deadline nil 'closed)))
  13458. (defun org-schedule (&optional remove)
  13459. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13460. With argument REMOVE, remove any scheduling date from the item."
  13461. (interactive "P")
  13462. (if remove
  13463. (progn
  13464. (org-remove-timestamp-with-keyword org-scheduled-string)
  13465. (message "Item is no longer scheduled."))
  13466. (org-add-planning-info 'scheduled nil 'closed)))
  13467. (defun org-remove-timestamp-with-keyword (keyword)
  13468. "Remove all time stamps with KEYWORD in the current entry."
  13469. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13470. beg)
  13471. (save-excursion
  13472. (org-back-to-heading t)
  13473. (setq beg (point))
  13474. (org-end-of-subtree t t)
  13475. (while (re-search-backward re beg t)
  13476. (replace-match "")
  13477. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13478. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13479. (defun org-add-planning-info (what &optional time &rest remove)
  13480. "Insert new timestamp with keyword in the line directly after the headline.
  13481. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13482. If non is given, the user is prompted for a date.
  13483. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13484. be removed."
  13485. (interactive)
  13486. (let (org-time-was-given org-end-time-was-given ts
  13487. end default-time default-input)
  13488. (when (and (not time) (memq what '(scheduled deadline)))
  13489. ;; Try to get a default date/time from existing timestamp
  13490. (save-excursion
  13491. (org-back-to-heading t)
  13492. (setq end (save-excursion (outline-next-heading) (point)))
  13493. (when (re-search-forward (if (eq what 'scheduled)
  13494. org-scheduled-time-regexp
  13495. org-deadline-time-regexp)
  13496. end t)
  13497. (setq ts (match-string 1)
  13498. default-time
  13499. (apply 'encode-time (org-parse-time-string ts))
  13500. default-input (and ts (org-get-compact-tod ts))))))
  13501. (when what
  13502. ;; If necessary, get the time from the user
  13503. (setq time (or time (org-read-date nil 'to-time nil nil
  13504. default-time default-input))))
  13505. (when (and org-insert-labeled-timestamps-at-point
  13506. (member what '(scheduled deadline)))
  13507. (insert
  13508. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13509. (org-insert-time-stamp time org-time-was-given
  13510. nil nil nil (list org-end-time-was-given))
  13511. (setq what nil))
  13512. (save-excursion
  13513. (save-restriction
  13514. (let (col list elt ts buffer-invisibility-spec)
  13515. (org-back-to-heading t)
  13516. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13517. (goto-char (match-end 1))
  13518. (setq col (current-column))
  13519. (goto-char (match-end 0))
  13520. (if (eobp) (insert "\n") (forward-char 1))
  13521. (if (and (not (looking-at outline-regexp))
  13522. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13523. "[^\r\n]*"))
  13524. (not (equal (match-string 1) org-clock-string)))
  13525. (narrow-to-region (match-beginning 0) (match-end 0))
  13526. (insert-before-markers "\n")
  13527. (backward-char 1)
  13528. (narrow-to-region (point) (point))
  13529. (indent-to-column col))
  13530. ;; Check if we have to remove something.
  13531. (setq list (cons what remove))
  13532. (while list
  13533. (setq elt (pop list))
  13534. (goto-char (point-min))
  13535. (when (or (and (eq elt 'scheduled)
  13536. (re-search-forward org-scheduled-time-regexp nil t))
  13537. (and (eq elt 'deadline)
  13538. (re-search-forward org-deadline-time-regexp nil t))
  13539. (and (eq elt 'closed)
  13540. (re-search-forward org-closed-time-regexp nil t)))
  13541. (replace-match "")
  13542. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13543. (if (looking-at " +") (replace-match ""))))
  13544. (goto-char (point-max))
  13545. (when what
  13546. (insert
  13547. (if (not (equal (char-before) ?\ )) " " "")
  13548. (cond ((eq what 'scheduled) org-scheduled-string)
  13549. ((eq what 'deadline) org-deadline-string)
  13550. ((eq what 'closed) org-closed-string))
  13551. " ")
  13552. (setq ts (org-insert-time-stamp
  13553. time
  13554. (or org-time-was-given
  13555. (and (eq what 'closed) org-log-done-with-time))
  13556. (eq what 'closed)
  13557. nil nil (list org-end-time-was-given)))
  13558. (end-of-line 1))
  13559. (goto-char (point-min))
  13560. (widen)
  13561. (if (looking-at "[ \t]+\r?\n")
  13562. (replace-match ""))
  13563. ts)))))
  13564. (defvar org-log-note-marker (make-marker))
  13565. (defvar org-log-note-purpose nil)
  13566. (defvar org-log-note-state nil)
  13567. (defvar org-log-note-how nil)
  13568. (defvar org-log-note-window-configuration nil)
  13569. (defvar org-log-note-return-to (make-marker))
  13570. (defvar org-log-post-message nil
  13571. "Message to be displayed after a log note has been stored.
  13572. The auto-repeater uses this.")
  13573. (defun org-add-log-maybe (&optional purpose state findpos how)
  13574. "Set up the post command hook to take a note.
  13575. If this is about to TODO state change, the new state is expected in STATE.
  13576. When FINDPOS is non-nil, find the correct position for the note in
  13577. the current entry. If not, assume that it can be inserted at point."
  13578. (save-excursion
  13579. (when findpos
  13580. (org-back-to-heading t)
  13581. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13582. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13583. "[^\r\n]*\\)?"))
  13584. (goto-char (match-end 0))
  13585. (unless org-log-states-order-reversed
  13586. (and (= (char-after) ?\n) (forward-char 1))
  13587. (org-skip-over-state-notes)
  13588. (skip-chars-backward " \t\n\r")))
  13589. (move-marker org-log-note-marker (point))
  13590. (setq org-log-note-purpose purpose
  13591. org-log-note-state state
  13592. org-log-note-how how)
  13593. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13594. (defun org-skip-over-state-notes ()
  13595. "Skip past the list of State notes in an entry."
  13596. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13597. (while (looking-at "[ \t]*- State")
  13598. (condition-case nil
  13599. (org-next-item)
  13600. (error (org-end-of-item)))))
  13601. (defun org-add-log-note (&optional purpose)
  13602. "Pop up a window for taking a note, and add this note later at point."
  13603. (remove-hook 'post-command-hook 'org-add-log-note)
  13604. (setq org-log-note-window-configuration (current-window-configuration))
  13605. (delete-other-windows)
  13606. (move-marker org-log-note-return-to (point))
  13607. (switch-to-buffer (marker-buffer org-log-note-marker))
  13608. (goto-char org-log-note-marker)
  13609. (org-switch-to-buffer-other-window "*Org Note*")
  13610. (erase-buffer)
  13611. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13612. (org-store-log-note)
  13613. (let ((org-inhibit-startup t)) (org-mode))
  13614. (insert (format "# Insert note for %s.
  13615. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13616. (cond
  13617. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13618. ((eq org-log-note-purpose 'done) "closed todo item")
  13619. ((eq org-log-note-purpose 'state)
  13620. (format "state change to \"%s\"" org-log-note-state))
  13621. (t (error "This should not happen")))))
  13622. (org-set-local 'org-finish-function 'org-store-log-note)))
  13623. (defun org-store-log-note ()
  13624. "Finish taking a log note, and insert it to where it belongs."
  13625. (let ((txt (buffer-string))
  13626. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13627. lines ind)
  13628. (kill-buffer (current-buffer))
  13629. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13630. (setq txt (replace-match "" t t txt)))
  13631. (if (string-match "\\s-+\\'" txt)
  13632. (setq txt (replace-match "" t t txt)))
  13633. (setq lines (org-split-string txt "\n"))
  13634. (when (and note (string-match "\\S-" note))
  13635. (setq note
  13636. (org-replace-escapes
  13637. note
  13638. (list (cons "%u" (user-login-name))
  13639. (cons "%U" user-full-name)
  13640. (cons "%t" (format-time-string
  13641. (org-time-stamp-format 'long 'inactive)
  13642. (current-time)))
  13643. (cons "%s" (if org-log-note-state
  13644. (concat "\"" org-log-note-state "\"")
  13645. "")))))
  13646. (if lines (setq note (concat note " \\\\")))
  13647. (push note lines))
  13648. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13649. (when lines
  13650. (save-excursion
  13651. (set-buffer (marker-buffer org-log-note-marker))
  13652. (save-excursion
  13653. (goto-char org-log-note-marker)
  13654. (move-marker org-log-note-marker nil)
  13655. (end-of-line 1)
  13656. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13657. (indent-relative nil)
  13658. (insert "- " (pop lines))
  13659. (org-indent-line-function)
  13660. (beginning-of-line 1)
  13661. (looking-at "[ \t]*")
  13662. (setq ind (concat (match-string 0) " "))
  13663. (end-of-line 1)
  13664. (while lines (insert "\n" ind (pop lines)))))))
  13665. (set-window-configuration org-log-note-window-configuration)
  13666. (with-current-buffer (marker-buffer org-log-note-return-to)
  13667. (goto-char org-log-note-return-to))
  13668. (move-marker org-log-note-return-to nil)
  13669. (and org-log-post-message (message "%s" org-log-post-message)))
  13670. ;; FIXME: what else would be useful?
  13671. ;; - priority
  13672. ;; - date
  13673. (defun org-sparse-tree (&optional arg)
  13674. "Create a sparse tree, prompt for the details.
  13675. This command can create sparse trees. You first need to select the type
  13676. of match used to create the tree:
  13677. t Show entries with a specific TODO keyword.
  13678. T Show entries selected by a tags match.
  13679. p Enter a property name and its value (both with completion on existing
  13680. names/values) and show entries with that property.
  13681. r Show entries matching a regular expression
  13682. d Show deadlines due within `org-deadline-warning-days'."
  13683. (interactive "P")
  13684. (let (ans kwd value)
  13685. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13686. (setq ans (read-char-exclusive))
  13687. (cond
  13688. ((equal ans ?d)
  13689. (call-interactively 'org-check-deadlines))
  13690. ((equal ans ?b)
  13691. (call-interactively 'org-check-before-date))
  13692. ((equal ans ?t)
  13693. (org-show-todo-tree '(4)))
  13694. ((equal ans ?T)
  13695. (call-interactively 'org-tags-sparse-tree))
  13696. ((member ans '(?p ?P))
  13697. (setq kwd (completing-read "Property: "
  13698. (mapcar 'list (org-buffer-property-keys))))
  13699. (setq value (completing-read "Value: "
  13700. (mapcar 'list (org-property-values kwd))))
  13701. (unless (string-match "\\`{.*}\\'" value)
  13702. (setq value (concat "\"" value "\"")))
  13703. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13704. ((member ans '(?r ?R ?/))
  13705. (call-interactively 'org-occur))
  13706. (t (error "No such sparse tree command \"%c\"" ans)))))
  13707. (defvar org-occur-highlights nil
  13708. "List of overlays used for occur matches.")
  13709. (make-variable-buffer-local 'org-occur-highlights)
  13710. (defvar org-occur-parameters nil
  13711. "Parameters of the active org-occur calls.
  13712. This is a list, each call to org-occur pushes as cons cell,
  13713. containing the regular expression and the callback, onto the list.
  13714. The list can contain several entries if `org-occur' has been called
  13715. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  13716. will only contain one set of parameters. When the highlights are
  13717. removed (for example with `C-c C-c', or with the next edit (depending
  13718. on `org-remove-highlights-with-change'), this variable is emptied
  13719. as well.")
  13720. (make-variable-buffer-local 'org-occur-parameters)
  13721. (defun org-occur (regexp &optional keep-previous callback)
  13722. "Make a compact tree which shows all matches of REGEXP.
  13723. The tree will show the lines where the regexp matches, and all higher
  13724. headlines above the match. It will also show the heading after the match,
  13725. to make sure editing the matching entry is easy.
  13726. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13727. call to `org-occur' will be kept, to allow stacking of calls to this
  13728. command.
  13729. If CALLBACK is non-nil, it is a function which is called to confirm
  13730. that the match should indeed be shown."
  13731. (interactive "sRegexp: \nP")
  13732. (unless keep-previous
  13733. (org-remove-occur-highlights nil nil t))
  13734. (push (cons regexp callback) org-occur-parameters)
  13735. (let ((cnt 0))
  13736. (save-excursion
  13737. (goto-char (point-min))
  13738. (if (or (not keep-previous) ; do not want to keep
  13739. (not org-occur-highlights)) ; no previous matches
  13740. ;; hide everything
  13741. (org-overview))
  13742. (while (re-search-forward regexp nil t)
  13743. (when (or (not callback)
  13744. (save-match-data (funcall callback)))
  13745. (setq cnt (1+ cnt))
  13746. (when org-highlight-sparse-tree-matches
  13747. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13748. (org-show-context 'occur-tree))))
  13749. (when org-remove-highlights-with-change
  13750. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13751. nil 'local))
  13752. (unless org-sparse-tree-open-archived-trees
  13753. (org-hide-archived-subtrees (point-min) (point-max)))
  13754. (run-hooks 'org-occur-hook)
  13755. (if (interactive-p)
  13756. (message "%d match(es) for regexp %s" cnt regexp))
  13757. cnt))
  13758. (defun org-show-context (&optional key)
  13759. "Make sure point and context and visible.
  13760. How much context is shown depends upon the variables
  13761. `org-show-hierarchy-above', `org-show-following-heading'. and
  13762. `org-show-siblings'."
  13763. (let ((heading-p (org-on-heading-p t))
  13764. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13765. (following-p (org-get-alist-option org-show-following-heading key))
  13766. (entry-p (org-get-alist-option org-show-entry-below key))
  13767. (siblings-p (org-get-alist-option org-show-siblings key)))
  13768. (catch 'exit
  13769. ;; Show heading or entry text
  13770. (if (and heading-p (not entry-p))
  13771. (org-flag-heading nil) ; only show the heading
  13772. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13773. (org-show-hidden-entry))) ; show entire entry
  13774. (when following-p
  13775. ;; Show next sibling, or heading below text
  13776. (save-excursion
  13777. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13778. (org-flag-heading nil))))
  13779. (when siblings-p (org-show-siblings))
  13780. (when hierarchy-p
  13781. ;; show all higher headings, possibly with siblings
  13782. (save-excursion
  13783. (while (and (condition-case nil
  13784. (progn (org-up-heading-all 1) t)
  13785. (error nil))
  13786. (not (bobp)))
  13787. (org-flag-heading nil)
  13788. (when siblings-p (org-show-siblings))))))))
  13789. (defun org-reveal (&optional siblings)
  13790. "Show current entry, hierarchy above it, and the following headline.
  13791. This can be used to show a consistent set of context around locations
  13792. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13793. not t for the search context.
  13794. With optional argument SIBLINGS, on each level of the hierarchy all
  13795. siblings are shown. This repairs the tree structure to what it would
  13796. look like when opened with hierarchical calls to `org-cycle'."
  13797. (interactive "P")
  13798. (let ((org-show-hierarchy-above t)
  13799. (org-show-following-heading t)
  13800. (org-show-siblings (if siblings t org-show-siblings)))
  13801. (org-show-context nil)))
  13802. (defun org-highlight-new-match (beg end)
  13803. "Highlight from BEG to END and mark the highlight is an occur headline."
  13804. (let ((ov (org-make-overlay beg end)))
  13805. (org-overlay-put ov 'face 'secondary-selection)
  13806. (push ov org-occur-highlights)))
  13807. (defun org-remove-occur-highlights (&optional beg end noremove)
  13808. "Remove the occur highlights from the buffer.
  13809. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13810. from the `before-change-functions' in the current buffer."
  13811. (interactive)
  13812. (unless org-inhibit-highlight-removal
  13813. (mapc 'org-delete-overlay org-occur-highlights)
  13814. (setq org-occur-highlights nil)
  13815. (setq org-occur-parameters nil)
  13816. (unless noremove
  13817. (remove-hook 'before-change-functions
  13818. 'org-remove-occur-highlights 'local))))
  13819. ;;;; Priorities
  13820. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13821. "Regular expression matching the priority indicator.")
  13822. (defvar org-remove-priority-next-time nil)
  13823. (defun org-priority-up ()
  13824. "Increase the priority of the current item."
  13825. (interactive)
  13826. (org-priority 'up))
  13827. (defun org-priority-down ()
  13828. "Decrease the priority of the current item."
  13829. (interactive)
  13830. (org-priority 'down))
  13831. (defun org-priority (&optional action)
  13832. "Change the priority of an item by ARG.
  13833. ACTION can be `set', `up', `down', or a character."
  13834. (interactive)
  13835. (setq action (or action 'set))
  13836. (let (current new news have remove)
  13837. (save-excursion
  13838. (org-back-to-heading)
  13839. (if (looking-at org-priority-regexp)
  13840. (setq current (string-to-char (match-string 2))
  13841. have t)
  13842. (setq current org-default-priority))
  13843. (cond
  13844. ((or (eq action 'set) (integerp action))
  13845. (if (integerp action)
  13846. (setq new action)
  13847. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13848. (setq new (read-char-exclusive)))
  13849. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13850. (= (upcase org-lowest-priority) org-lowest-priority))
  13851. (setq new (upcase new)))
  13852. (cond ((equal new ?\ ) (setq remove t))
  13853. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13854. (error "Priority must be between `%c' and `%c'"
  13855. org-highest-priority org-lowest-priority))))
  13856. ((eq action 'up)
  13857. (if (and (not have) (eq last-command this-command))
  13858. (setq new org-lowest-priority)
  13859. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13860. org-default-priority (1- current)))))
  13861. ((eq action 'down)
  13862. (if (and (not have) (eq last-command this-command))
  13863. (setq new org-highest-priority)
  13864. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13865. org-default-priority (1+ current)))))
  13866. (t (error "Invalid action")))
  13867. (if (or (< (upcase new) org-highest-priority)
  13868. (> (upcase new) org-lowest-priority))
  13869. (setq remove t))
  13870. (setq news (format "%c" new))
  13871. (if have
  13872. (if remove
  13873. (replace-match "" t t nil 1)
  13874. (replace-match news t t nil 2))
  13875. (if remove
  13876. (error "No priority cookie found in line")
  13877. (looking-at org-todo-line-regexp)
  13878. (if (match-end 2)
  13879. (progn
  13880. (goto-char (match-end 2))
  13881. (insert " [#" news "]"))
  13882. (goto-char (match-beginning 3))
  13883. (insert "[#" news "] ")))))
  13884. (org-preserve-lc (org-set-tags nil 'align))
  13885. (if remove
  13886. (message "Priority removed")
  13887. (message "Priority of current item set to %s" news))))
  13888. (defun org-get-priority (s)
  13889. "Find priority cookie and return priority."
  13890. (save-match-data
  13891. (if (not (string-match org-priority-regexp s))
  13892. (* 1000 (- org-lowest-priority org-default-priority))
  13893. (* 1000 (- org-lowest-priority
  13894. (string-to-char (match-string 2 s)))))))
  13895. ;;;; Tags
  13896. (defun org-scan-tags (action matcher &optional todo-only)
  13897. "Scan headline tags with inheritance and produce output ACTION.
  13898. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13899. evaluated, testing if a given set of tags qualifies a headline for
  13900. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13901. are included in the output."
  13902. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13903. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13904. (org-re
  13905. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13906. (props (list 'face nil
  13907. 'done-face 'org-done
  13908. 'undone-face nil
  13909. 'mouse-face 'highlight
  13910. 'org-not-done-regexp org-not-done-regexp
  13911. 'org-todo-regexp org-todo-regexp
  13912. 'keymap org-agenda-keymap
  13913. 'help-echo
  13914. (format "mouse-2 or RET jump to org file %s"
  13915. (abbreviate-file-name
  13916. (or (buffer-file-name (buffer-base-buffer))
  13917. (buffer-name (buffer-base-buffer)))))))
  13918. (case-fold-search nil)
  13919. lspos
  13920. tags tags-list tags-alist (llast 0) rtn level category i txt
  13921. todo marker entry priority)
  13922. (save-excursion
  13923. (goto-char (point-min))
  13924. (when (eq action 'sparse-tree)
  13925. (org-overview)
  13926. (org-remove-occur-highlights))
  13927. (while (re-search-forward re nil t)
  13928. (catch :skip
  13929. (setq todo (if (match-end 1) (match-string 2))
  13930. tags (if (match-end 4) (match-string 4)))
  13931. (goto-char (setq lspos (1+ (match-beginning 0))))
  13932. (setq level (org-reduced-level (funcall outline-level))
  13933. category (org-get-category))
  13934. (setq i llast llast level)
  13935. ;; remove tag lists from same and sublevels
  13936. (while (>= i level)
  13937. (when (setq entry (assoc i tags-alist))
  13938. (setq tags-alist (delete entry tags-alist)))
  13939. (setq i (1- i)))
  13940. ;; add the nex tags
  13941. (when tags
  13942. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13943. tags-alist
  13944. (cons (cons level tags) tags-alist)))
  13945. ;; compile tags for current headline
  13946. (setq tags-list
  13947. (if org-use-tag-inheritance
  13948. (apply 'append (mapcar 'cdr tags-alist))
  13949. tags))
  13950. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13951. (eval matcher)
  13952. (or (not org-agenda-skip-archived-trees)
  13953. (not (member org-archive-tag tags-list))))
  13954. (and (eq action 'agenda) (org-agenda-skip))
  13955. ;; list this headline
  13956. (if (eq action 'sparse-tree)
  13957. (progn
  13958. (and org-highlight-sparse-tree-matches
  13959. (org-get-heading) (match-end 0)
  13960. (org-highlight-new-match
  13961. (match-beginning 0) (match-beginning 1)))
  13962. (org-show-context 'tags-tree))
  13963. (setq txt (org-format-agenda-item
  13964. ""
  13965. (concat
  13966. (if org-tags-match-list-sublevels
  13967. (make-string (1- level) ?.) "")
  13968. (org-get-heading))
  13969. category tags-list)
  13970. priority (org-get-priority txt))
  13971. (goto-char lspos)
  13972. (setq marker (org-agenda-new-marker))
  13973. (org-add-props txt props
  13974. 'org-marker marker 'org-hd-marker marker 'org-category category
  13975. 'priority priority 'type "tagsmatch")
  13976. (push txt rtn))
  13977. ;; if we are to skip sublevels, jump to end of subtree
  13978. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13979. (when (and (eq action 'sparse-tree)
  13980. (not org-sparse-tree-open-archived-trees))
  13981. (org-hide-archived-subtrees (point-min) (point-max)))
  13982. (nreverse rtn)))
  13983. (defvar todo-only) ;; dynamically scoped
  13984. (defun org-tags-sparse-tree (&optional todo-only match)
  13985. "Create a sparse tree according to tags string MATCH.
  13986. MATCH can contain positive and negative selection of tags, like
  13987. \"+WORK+URGENT-WITHBOSS\".
  13988. If optional argument TODO_ONLY is non-nil, only select lines that are
  13989. also TODO lines."
  13990. (interactive "P")
  13991. (org-prepare-agenda-buffers (list (current-buffer)))
  13992. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13993. (defvar org-cached-props nil)
  13994. (defun org-cached-entry-get (pom property)
  13995. (if (or (eq t org-use-property-inheritance)
  13996. (member property org-use-property-inheritance))
  13997. ;; Caching is not possible, check it directly
  13998. (org-entry-get pom property 'inherit)
  13999. ;; Get all properties, so that we can do complicated checks easily
  14000. (cdr (assoc property (or org-cached-props
  14001. (setq org-cached-props
  14002. (org-entry-properties pom)))))))
  14003. (defun org-global-tags-completion-table (&optional files)
  14004. "Return the list of all tags in all agenda buffer/files."
  14005. (save-excursion
  14006. (org-uniquify
  14007. (delq nil
  14008. (apply 'append
  14009. (mapcar
  14010. (lambda (file)
  14011. (set-buffer (find-file-noselect file))
  14012. (append (org-get-buffer-tags)
  14013. (mapcar (lambda (x) (if (stringp (car-safe x))
  14014. (list (car-safe x)) nil))
  14015. org-tag-alist)))
  14016. (if (and files (car files))
  14017. files
  14018. (org-agenda-files))))))))
  14019. (defun org-make-tags-matcher (match)
  14020. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14021. ;; todo-only is scoped dynamically into this function, and the function
  14022. ;; may change it it the matcher asksk for it.
  14023. (unless match
  14024. ;; Get a new match request, with completion
  14025. (let ((org-last-tags-completion-table
  14026. (org-global-tags-completion-table)))
  14027. (setq match (completing-read
  14028. "Match: " 'org-tags-completion-function nil nil nil
  14029. 'org-tags-history))))
  14030. ;; Parse the string and create a lisp form
  14031. (let ((match0 match)
  14032. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14033. minus tag mm
  14034. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14035. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14036. (if (string-match "/+" match)
  14037. ;; match contains also a todo-matching request
  14038. (progn
  14039. (setq tagsmatch (substring match 0 (match-beginning 0))
  14040. todomatch (substring match (match-end 0)))
  14041. (if (string-match "^!" todomatch)
  14042. (setq todo-only t todomatch (substring todomatch 1)))
  14043. (if (string-match "^\\s-*$" todomatch)
  14044. (setq todomatch nil)))
  14045. ;; only matching tags
  14046. (setq tagsmatch match todomatch nil))
  14047. ;; Make the tags matcher
  14048. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14049. (setq tagsmatcher t)
  14050. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14051. (while (setq term (pop orterms))
  14052. (while (and (equal (substring term -1) "\\") orterms)
  14053. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14054. (while (string-match re term)
  14055. (setq minus (and (match-end 1)
  14056. (equal (match-string 1 term) "-"))
  14057. tag (match-string 2 term)
  14058. re-p (equal (string-to-char tag) ?{)
  14059. level-p (match-end 3)
  14060. prop-p (match-end 4)
  14061. mm (cond
  14062. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14063. (level-p `(= level ,(string-to-number
  14064. (match-string 3 term))))
  14065. (prop-p
  14066. (setq pn (match-string 4 term)
  14067. pv (match-string 5 term)
  14068. cat-p (equal pn "CATEGORY")
  14069. re-p (equal (string-to-char pv) ?{)
  14070. pv (substring pv 1 -1))
  14071. (if (equal pn "CATEGORY")
  14072. (setq gv '(get-text-property (point) 'org-category))
  14073. (setq gv `(org-cached-entry-get nil ,pn)))
  14074. (if re-p
  14075. `(string-match ,pv (or ,gv ""))
  14076. `(equal ,pv (or ,gv ""))))
  14077. (t `(member ,(downcase tag) tags-list)))
  14078. mm (if minus (list 'not mm) mm)
  14079. term (substring term (match-end 0)))
  14080. (push mm tagsmatcher))
  14081. (push (if (> (length tagsmatcher) 1)
  14082. (cons 'and tagsmatcher)
  14083. (car tagsmatcher))
  14084. orlist)
  14085. (setq tagsmatcher nil))
  14086. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14087. (setq tagsmatcher
  14088. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14089. ;; Make the todo matcher
  14090. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14091. (setq todomatcher t)
  14092. (setq orterms (org-split-string todomatch "|") orlist nil)
  14093. (while (setq term (pop orterms))
  14094. (while (string-match re term)
  14095. (setq minus (and (match-end 1)
  14096. (equal (match-string 1 term) "-"))
  14097. kwd (match-string 2 term)
  14098. re-p (equal (string-to-char kwd) ?{)
  14099. term (substring term (match-end 0))
  14100. mm (if re-p
  14101. `(string-match ,(substring kwd 1 -1) todo)
  14102. (list 'equal 'todo kwd))
  14103. mm (if minus (list 'not mm) mm))
  14104. (push mm todomatcher))
  14105. (push (if (> (length todomatcher) 1)
  14106. (cons 'and todomatcher)
  14107. (car todomatcher))
  14108. orlist)
  14109. (setq todomatcher nil))
  14110. (setq todomatcher (if (> (length orlist) 1)
  14111. (cons 'or orlist) (car orlist))))
  14112. ;; Return the string and lisp forms of the matcher
  14113. (setq matcher (if todomatcher
  14114. (list 'and tagsmatcher todomatcher)
  14115. tagsmatcher))
  14116. (cons match0 matcher)))
  14117. (defun org-match-any-p (re list)
  14118. "Does re match any element of list?"
  14119. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14120. (delq nil list))
  14121. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14122. (defvar org-tags-overlay (org-make-overlay 1 1))
  14123. (org-detach-overlay org-tags-overlay)
  14124. (defun org-align-tags-here (to-col)
  14125. ;; Assumes that this is a headline
  14126. (let ((pos (point)) (col (current-column)) tags)
  14127. (beginning-of-line 1)
  14128. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14129. (< pos (match-beginning 2)))
  14130. (progn
  14131. (setq tags (match-string 2))
  14132. (goto-char (match-beginning 1))
  14133. (insert " ")
  14134. (delete-region (point) (1+ (match-end 0)))
  14135. (backward-char 1)
  14136. (move-to-column
  14137. (max (1+ (current-column))
  14138. (1+ col)
  14139. (if (> to-col 0)
  14140. to-col
  14141. (- (abs to-col) (length tags))))
  14142. t)
  14143. (insert tags)
  14144. (move-to-column (min (current-column) col) t))
  14145. (goto-char pos))))
  14146. (defun org-set-tags (&optional arg just-align)
  14147. "Set the tags for the current headline.
  14148. With prefix ARG, realign all tags in headings in the current buffer."
  14149. (interactive "P")
  14150. (let* ((re (concat "^" outline-regexp))
  14151. (current (org-get-tags-string))
  14152. (col (current-column))
  14153. (org-setting-tags t)
  14154. table current-tags inherited-tags ; computed below when needed
  14155. tags p0 c0 c1 rpl)
  14156. (if arg
  14157. (save-excursion
  14158. (goto-char (point-min))
  14159. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14160. (while (re-search-forward re nil t)
  14161. (org-set-tags nil t)
  14162. (end-of-line 1)))
  14163. (message "All tags realigned to column %d" org-tags-column))
  14164. (if just-align
  14165. (setq tags current)
  14166. ;; Get a new set of tags from the user
  14167. (save-excursion
  14168. (setq table (or org-tag-alist (org-get-buffer-tags))
  14169. org-last-tags-completion-table table
  14170. current-tags (org-split-string current ":")
  14171. inherited-tags (nreverse
  14172. (nthcdr (length current-tags)
  14173. (nreverse (org-get-tags-at))))
  14174. tags
  14175. (if (or (eq t org-use-fast-tag-selection)
  14176. (and org-use-fast-tag-selection
  14177. (delq nil (mapcar 'cdr table))))
  14178. (org-fast-tag-selection
  14179. current-tags inherited-tags table
  14180. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14181. (let ((org-add-colon-after-tag-completion t))
  14182. (org-trim
  14183. (org-without-partial-completion
  14184. (completing-read "Tags: " 'org-tags-completion-function
  14185. nil nil current 'org-tags-history)))))))
  14186. (while (string-match "[-+&]+" tags)
  14187. ;; No boolean logic, just a list
  14188. (setq tags (replace-match ":" t t tags))))
  14189. (if (string-match "\\`[\t ]*\\'" tags)
  14190. (setq tags "")
  14191. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14192. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14193. ;; Insert new tags at the correct column
  14194. (beginning-of-line 1)
  14195. (cond
  14196. ((and (equal current "") (equal tags "")))
  14197. ((re-search-forward
  14198. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14199. (point-at-eol) t)
  14200. (if (equal tags "")
  14201. (setq rpl "")
  14202. (goto-char (match-beginning 0))
  14203. (setq c0 (current-column) p0 (point)
  14204. c1 (max (1+ c0) (if (> org-tags-column 0)
  14205. org-tags-column
  14206. (- (- org-tags-column) (length tags))))
  14207. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14208. (replace-match rpl t t)
  14209. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14210. tags)
  14211. (t (error "Tags alignment failed")))
  14212. (move-to-column col)
  14213. (unless just-align
  14214. (run-hooks 'org-after-tags-change-hook)))))
  14215. (defun org-change-tag-in-region (beg end tag off)
  14216. "Add or remove TAG for each entry in the region.
  14217. This works in the agenda, and also in an org-mode buffer."
  14218. (interactive
  14219. (list (region-beginning) (region-end)
  14220. (let ((org-last-tags-completion-table
  14221. (if (org-mode-p)
  14222. (org-get-buffer-tags)
  14223. (org-global-tags-completion-table))))
  14224. (completing-read
  14225. "Tag: " 'org-tags-completion-function nil nil nil
  14226. 'org-tags-history))
  14227. (progn
  14228. (message "[s]et or [r]emove? ")
  14229. (equal (read-char-exclusive) ?r))))
  14230. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14231. (let ((agendap (equal major-mode 'org-agenda-mode))
  14232. l1 l2 m buf pos newhead (cnt 0))
  14233. (goto-char end)
  14234. (setq l2 (1- (org-current-line)))
  14235. (goto-char beg)
  14236. (setq l1 (org-current-line))
  14237. (loop for l from l1 to l2 do
  14238. (goto-line l)
  14239. (setq m (get-text-property (point) 'org-hd-marker))
  14240. (when (or (and (org-mode-p) (org-on-heading-p))
  14241. (and agendap m))
  14242. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14243. pos (if agendap m (point)))
  14244. (with-current-buffer buf
  14245. (save-excursion
  14246. (save-restriction
  14247. (goto-char pos)
  14248. (setq cnt (1+ cnt))
  14249. (org-toggle-tag tag (if off 'off 'on))
  14250. (setq newhead (org-get-heading)))))
  14251. (and agendap (org-agenda-change-all-lines newhead m))))
  14252. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14253. (defun org-tags-completion-function (string predicate &optional flag)
  14254. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14255. (confirm (lambda (x) (stringp (car x)))))
  14256. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14257. (setq s1 (match-string 1 string)
  14258. s2 (match-string 2 string))
  14259. (setq s1 "" s2 string))
  14260. (cond
  14261. ((eq flag nil)
  14262. ;; try completion
  14263. (setq rtn (try-completion s2 ctable confirm))
  14264. (if (stringp rtn)
  14265. (setq rtn
  14266. (concat s1 s2 (substring rtn (length s2))
  14267. (if (and org-add-colon-after-tag-completion
  14268. (assoc rtn ctable))
  14269. ":" ""))))
  14270. rtn)
  14271. ((eq flag t)
  14272. ;; all-completions
  14273. (all-completions s2 ctable confirm)
  14274. )
  14275. ((eq flag 'lambda)
  14276. ;; exact match?
  14277. (assoc s2 ctable)))
  14278. ))
  14279. (defun org-fast-tag-insert (kwd tags face &optional end)
  14280. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14281. (insert (format "%-12s" (concat kwd ":"))
  14282. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14283. (or end "")))
  14284. (defun org-fast-tag-show-exit (flag)
  14285. (save-excursion
  14286. (goto-line 3)
  14287. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14288. (replace-match ""))
  14289. (when flag
  14290. (end-of-line 1)
  14291. (move-to-column (- (window-width) 19) t)
  14292. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14293. (defun org-set-current-tags-overlay (current prefix)
  14294. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14295. (if (featurep 'xemacs)
  14296. (org-overlay-display org-tags-overlay (concat prefix s)
  14297. 'secondary-selection)
  14298. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14299. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14300. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14301. "Fast tag selection with single keys.
  14302. CURRENT is the current list of tags in the headline, INHERITED is the
  14303. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14304. possibly with grouping information. TODO-TABLE is a similar table with
  14305. TODO keywords, should these have keys assigned to them.
  14306. If the keys are nil, a-z are automatically assigned.
  14307. Returns the new tags string, or nil to not change the current settings."
  14308. (let* ((fulltable (append table todo-table))
  14309. (maxlen (apply 'max (mapcar
  14310. (lambda (x)
  14311. (if (stringp (car x)) (string-width (car x)) 0))
  14312. fulltable)))
  14313. (buf (current-buffer))
  14314. (expert (eq org-fast-tag-selection-single-key 'expert))
  14315. (buffer-tags nil)
  14316. (fwidth (+ maxlen 3 1 3))
  14317. (ncol (/ (- (window-width) 4) fwidth))
  14318. (i-face 'org-done)
  14319. (c-face 'org-todo)
  14320. tg cnt e c char c1 c2 ntable tbl rtn
  14321. ov-start ov-end ov-prefix
  14322. (exit-after-next org-fast-tag-selection-single-key)
  14323. (done-keywords org-done-keywords)
  14324. groups ingroup)
  14325. (save-excursion
  14326. (beginning-of-line 1)
  14327. (if (looking-at
  14328. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14329. (setq ov-start (match-beginning 1)
  14330. ov-end (match-end 1)
  14331. ov-prefix "")
  14332. (setq ov-start (1- (point-at-eol))
  14333. ov-end (1+ ov-start))
  14334. (skip-chars-forward "^\n\r")
  14335. (setq ov-prefix
  14336. (concat
  14337. (buffer-substring (1- (point)) (point))
  14338. (if (> (current-column) org-tags-column)
  14339. " "
  14340. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14341. (org-move-overlay org-tags-overlay ov-start ov-end)
  14342. (save-window-excursion
  14343. (if expert
  14344. (set-buffer (get-buffer-create " *Org tags*"))
  14345. (delete-other-windows)
  14346. (split-window-vertically)
  14347. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14348. (erase-buffer)
  14349. (org-set-local 'org-done-keywords done-keywords)
  14350. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14351. (org-fast-tag-insert "Current" current c-face "\n\n")
  14352. (org-fast-tag-show-exit exit-after-next)
  14353. (org-set-current-tags-overlay current ov-prefix)
  14354. (setq tbl fulltable char ?a cnt 0)
  14355. (while (setq e (pop tbl))
  14356. (cond
  14357. ((equal e '(:startgroup))
  14358. (push '() groups) (setq ingroup t)
  14359. (when (not (= cnt 0))
  14360. (setq cnt 0)
  14361. (insert "\n"))
  14362. (insert "{ "))
  14363. ((equal e '(:endgroup))
  14364. (setq ingroup nil cnt 0)
  14365. (insert "}\n"))
  14366. (t
  14367. (setq tg (car e) c2 nil)
  14368. (if (cdr e)
  14369. (setq c (cdr e))
  14370. ;; automatically assign a character.
  14371. (setq c1 (string-to-char
  14372. (downcase (substring
  14373. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14374. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14375. (while (or (rassoc char ntable) (rassoc char table))
  14376. (setq char (1+ char)))
  14377. (setq c2 c1))
  14378. (setq c (or c2 char)))
  14379. (if ingroup (push tg (car groups)))
  14380. (setq tg (org-add-props tg nil 'face
  14381. (cond
  14382. ((not (assoc tg table))
  14383. (org-get-todo-face tg))
  14384. ((member tg current) c-face)
  14385. ((member tg inherited) i-face)
  14386. (t nil))))
  14387. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14388. (insert "[" c "] " tg (make-string
  14389. (- fwidth 4 (length tg)) ?\ ))
  14390. (push (cons tg c) ntable)
  14391. (when (= (setq cnt (1+ cnt)) ncol)
  14392. (insert "\n")
  14393. (if ingroup (insert " "))
  14394. (setq cnt 0)))))
  14395. (setq ntable (nreverse ntable))
  14396. (insert "\n")
  14397. (goto-char (point-min))
  14398. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14399. (fit-window-to-buffer))
  14400. (setq rtn
  14401. (catch 'exit
  14402. (while t
  14403. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14404. (if groups " [!] no groups" " [!]groups")
  14405. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14406. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14407. (cond
  14408. ((= c ?\r) (throw 'exit t))
  14409. ((= c ?!)
  14410. (setq groups (not groups))
  14411. (goto-char (point-min))
  14412. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14413. ((= c ?\C-c)
  14414. (if (not expert)
  14415. (org-fast-tag-show-exit
  14416. (setq exit-after-next (not exit-after-next)))
  14417. (setq expert nil)
  14418. (delete-other-windows)
  14419. (split-window-vertically)
  14420. (org-switch-to-buffer-other-window " *Org tags*")
  14421. (and (fboundp 'fit-window-to-buffer)
  14422. (fit-window-to-buffer))))
  14423. ((or (= c ?\C-g)
  14424. (and (= c ?q) (not (rassoc c ntable))))
  14425. (org-detach-overlay org-tags-overlay)
  14426. (setq quit-flag t))
  14427. ((= c ?\ )
  14428. (setq current nil)
  14429. (if exit-after-next (setq exit-after-next 'now)))
  14430. ((= c ?\t)
  14431. (condition-case nil
  14432. (setq tg (completing-read
  14433. "Tag: "
  14434. (or buffer-tags
  14435. (with-current-buffer buf
  14436. (org-get-buffer-tags)))))
  14437. (quit (setq tg "")))
  14438. (when (string-match "\\S-" tg)
  14439. (add-to-list 'buffer-tags (list tg))
  14440. (if (member tg current)
  14441. (setq current (delete tg current))
  14442. (push tg current)))
  14443. (if exit-after-next (setq exit-after-next 'now)))
  14444. ((setq e (rassoc c todo-table) tg (car e))
  14445. (with-current-buffer buf
  14446. (save-excursion (org-todo tg)))
  14447. (if exit-after-next (setq exit-after-next 'now)))
  14448. ((setq e (rassoc c ntable) tg (car e))
  14449. (if (member tg current)
  14450. (setq current (delete tg current))
  14451. (loop for g in groups do
  14452. (if (member tg g)
  14453. (mapc (lambda (x)
  14454. (setq current (delete x current)))
  14455. g)))
  14456. (push tg current))
  14457. (if exit-after-next (setq exit-after-next 'now))))
  14458. ;; Create a sorted list
  14459. (setq current
  14460. (sort current
  14461. (lambda (a b)
  14462. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14463. (if (eq exit-after-next 'now) (throw 'exit t))
  14464. (goto-char (point-min))
  14465. (beginning-of-line 2)
  14466. (delete-region (point) (point-at-eol))
  14467. (org-fast-tag-insert "Current" current c-face)
  14468. (org-set-current-tags-overlay current ov-prefix)
  14469. (while (re-search-forward
  14470. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14471. (setq tg (match-string 1))
  14472. (add-text-properties
  14473. (match-beginning 1) (match-end 1)
  14474. (list 'face
  14475. (cond
  14476. ((member tg current) c-face)
  14477. ((member tg inherited) i-face)
  14478. (t (get-text-property (match-beginning 1) 'face))))))
  14479. (goto-char (point-min)))))
  14480. (org-detach-overlay org-tags-overlay)
  14481. (if rtn
  14482. (mapconcat 'identity current ":")
  14483. nil))))
  14484. (defun org-get-tags-string ()
  14485. "Get the TAGS string in the current headline."
  14486. (unless (org-on-heading-p t)
  14487. (error "Not on a heading"))
  14488. (save-excursion
  14489. (beginning-of-line 1)
  14490. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14491. (org-match-string-no-properties 1)
  14492. "")))
  14493. (defun org-get-tags ()
  14494. "Get the list of tags specified in the current headline."
  14495. (org-split-string (org-get-tags-string) ":"))
  14496. (defun org-get-buffer-tags ()
  14497. "Get a table of all tags used in the buffer, for completion."
  14498. (let (tags)
  14499. (save-excursion
  14500. (goto-char (point-min))
  14501. (while (re-search-forward
  14502. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14503. (when (equal (char-after (point-at-bol 0)) ?*)
  14504. (mapc (lambda (x) (add-to-list 'tags x))
  14505. (org-split-string (org-match-string-no-properties 1) ":")))))
  14506. (mapcar 'list tags)))
  14507. ;;;; Properties
  14508. ;;; Setting and retrieving properties
  14509. (defconst org-special-properties
  14510. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14511. "TIMESTAMP" "TIMESTAMP_IA")
  14512. "The special properties valid in Org-mode.
  14513. These are properties that are not defined in the property drawer,
  14514. but in some other way.")
  14515. (defconst org-default-properties
  14516. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14517. "LOCATION" "LOGGING" "COLUMNS")
  14518. "Some properties that are used by Org-mode for various purposes.
  14519. Being in this list makes sure that they are offered for completion.")
  14520. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14521. "Regular expression matching the first line of a property drawer.")
  14522. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14523. "Regular expression matching the first line of a property drawer.")
  14524. (defun org-property-action ()
  14525. "Do an action on properties."
  14526. (interactive)
  14527. (let (c)
  14528. (org-at-property-p)
  14529. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14530. (setq c (read-char-exclusive))
  14531. (cond
  14532. ((equal c ?s)
  14533. (call-interactively 'org-set-property))
  14534. ((equal c ?d)
  14535. (call-interactively 'org-delete-property))
  14536. ((equal c ?D)
  14537. (call-interactively 'org-delete-property-globally))
  14538. ((equal c ?c)
  14539. (call-interactively 'org-compute-property-at-point))
  14540. (t (error "No such property action %c" c)))))
  14541. (defun org-at-property-p ()
  14542. "Is the cursor in a property line?"
  14543. ;; FIXME: Does not check if we are actually in the drawer.
  14544. ;; FIXME: also returns true on any drawers.....
  14545. ;; This is used by C-c C-c for property action.
  14546. (save-excursion
  14547. (beginning-of-line 1)
  14548. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14549. (defmacro org-with-point-at (pom &rest body)
  14550. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14551. (declare (indent 1) (debug t))
  14552. `(save-excursion
  14553. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14554. (save-excursion
  14555. (goto-char (or pom (point)))
  14556. ,@body)))
  14557. (defun org-get-property-block (&optional beg end force)
  14558. "Return the (beg . end) range of the body of the property drawer.
  14559. BEG and END can be beginning and end of subtree, if not given
  14560. they will be found.
  14561. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14562. (catch 'exit
  14563. (save-excursion
  14564. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14565. (end (or end (progn (outline-next-heading) (point)))))
  14566. (goto-char beg)
  14567. (if (re-search-forward org-property-start-re end t)
  14568. (setq beg (1+ (match-end 0)))
  14569. (if force
  14570. (save-excursion
  14571. (org-insert-property-drawer)
  14572. (setq end (progn (outline-next-heading) (point))))
  14573. (throw 'exit nil))
  14574. (goto-char beg)
  14575. (if (re-search-forward org-property-start-re end t)
  14576. (setq beg (1+ (match-end 0)))))
  14577. (if (re-search-forward org-property-end-re end t)
  14578. (setq end (match-beginning 0))
  14579. (or force (throw 'exit nil))
  14580. (goto-char beg)
  14581. (setq end beg)
  14582. (org-indent-line-function)
  14583. (insert ":END:\n"))
  14584. (cons beg end)))))
  14585. (defun org-entry-properties (&optional pom which)
  14586. "Get all properties of the entry at point-or-marker POM.
  14587. This includes the TODO keyword, the tags, time strings for deadline,
  14588. scheduled, and clocking, and any additional properties defined in the
  14589. entry. The return value is an alist, keys may occur multiple times
  14590. if the property key was used several times.
  14591. POM may also be nil, in which case the current entry is used.
  14592. If WHICH is nil or `all', get all properties. If WHICH is
  14593. `special' or `standard', only get that subclass."
  14594. (setq which (or which 'all))
  14595. (org-with-point-at pom
  14596. (let ((clockstr (substring org-clock-string 0 -1))
  14597. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14598. beg end range props sum-props key value string clocksum)
  14599. (save-excursion
  14600. (when (condition-case nil (org-back-to-heading t) (error nil))
  14601. (setq beg (point))
  14602. (setq sum-props (get-text-property (point) 'org-summaries))
  14603. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14604. (outline-next-heading)
  14605. (setq end (point))
  14606. (when (memq which '(all special))
  14607. ;; Get the special properties, like TODO and tags
  14608. (goto-char beg)
  14609. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14610. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14611. (when (looking-at org-priority-regexp)
  14612. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14613. (when (and (setq value (org-get-tags-string))
  14614. (string-match "\\S-" value))
  14615. (push (cons "TAGS" value) props))
  14616. (when (setq value (org-get-tags-at))
  14617. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14618. props))
  14619. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14620. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14621. string (if (equal key clockstr)
  14622. (org-no-properties
  14623. (org-trim
  14624. (buffer-substring
  14625. (match-beginning 3) (goto-char (point-at-eol)))))
  14626. (substring (org-match-string-no-properties 3) 1 -1)))
  14627. (unless key
  14628. (if (= (char-after (match-beginning 3)) ?\[)
  14629. (setq key "TIMESTAMP_IA")
  14630. (setq key "TIMESTAMP")))
  14631. (when (or (equal key clockstr) (not (assoc key props)))
  14632. (push (cons key string) props)))
  14633. )
  14634. (when (memq which '(all standard))
  14635. ;; Get the standard properties, like :PORP: ...
  14636. (setq range (org-get-property-block beg end))
  14637. (when range
  14638. (goto-char (car range))
  14639. (while (re-search-forward
  14640. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14641. (cdr range) t)
  14642. (setq key (org-match-string-no-properties 1)
  14643. value (org-trim (or (org-match-string-no-properties 2) "")))
  14644. (unless (member key excluded)
  14645. (push (cons key (or value "")) props)))))
  14646. (if clocksum
  14647. (push (cons "CLOCKSUM"
  14648. (org-column-number-to-string (/ (float clocksum) 60.)
  14649. 'add_times))
  14650. props))
  14651. (append sum-props (nreverse props)))))))
  14652. (defun org-entry-get (pom property &optional inherit)
  14653. "Get value of PROPERTY for entry at point-or-marker POM.
  14654. If INHERIT is non-nil and the entry does not have the property,
  14655. then also check higher levels of the hierarchy.
  14656. If the property is present but empty, the return value is the empty string.
  14657. If the property is not present at all, nil is returned."
  14658. (org-with-point-at pom
  14659. (if inherit
  14660. (org-entry-get-with-inheritance property)
  14661. (if (member property org-special-properties)
  14662. ;; We need a special property. Use brute force, get all properties.
  14663. (cdr (assoc property (org-entry-properties nil 'special)))
  14664. (let ((range (org-get-property-block)))
  14665. (if (and range
  14666. (goto-char (car range))
  14667. (re-search-forward
  14668. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14669. (cdr range) t))
  14670. ;; Found the property, return it.
  14671. (if (match-end 1)
  14672. (org-match-string-no-properties 1)
  14673. "")))))))
  14674. (defun org-entry-delete (pom property)
  14675. "Delete the property PROPERTY from entry at point-or-marker POM."
  14676. (org-with-point-at pom
  14677. (if (member property org-special-properties)
  14678. nil ; cannot delete these properties.
  14679. (let ((range (org-get-property-block)))
  14680. (if (and range
  14681. (goto-char (car range))
  14682. (re-search-forward
  14683. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14684. (cdr range) t))
  14685. (progn
  14686. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14687. t)
  14688. nil)))))
  14689. ;; Multi-values properties are properties that contain multiple values
  14690. ;; These values are assumed to be single words, separated by whitespace.
  14691. (defun org-entry-add-to-multivalued-property (pom property value)
  14692. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14693. (let* ((old (org-entry-get pom property))
  14694. (values (and old (org-split-string old "[ \t]"))))
  14695. (unless (member value values)
  14696. (setq values (cons value values))
  14697. (org-entry-put pom property
  14698. (mapconcat 'identity values " ")))))
  14699. (defun org-entry-remove-from-multivalued-property (pom property value)
  14700. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14701. (let* ((old (org-entry-get pom property))
  14702. (values (and old (org-split-string old "[ \t]"))))
  14703. (when (member value values)
  14704. (setq values (delete value values))
  14705. (org-entry-put pom property
  14706. (mapconcat 'identity values " ")))))
  14707. (defun org-entry-member-in-multivalued-property (pom property value)
  14708. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14709. (let* ((old (org-entry-get pom property))
  14710. (values (and old (org-split-string old "[ \t]"))))
  14711. (member value values)))
  14712. (defvar org-entry-property-inherited-from (make-marker))
  14713. (defun org-entry-get-with-inheritance (property)
  14714. "Get entry property, and search higher levels if not present."
  14715. (let (tmp)
  14716. (save-excursion
  14717. (save-restriction
  14718. (widen)
  14719. (catch 'ex
  14720. (while t
  14721. (when (setq tmp (org-entry-get nil property))
  14722. (org-back-to-heading t)
  14723. (move-marker org-entry-property-inherited-from (point))
  14724. (throw 'ex tmp))
  14725. (or (org-up-heading-safe) (throw 'ex nil)))))
  14726. (or tmp (cdr (assoc property org-local-properties))
  14727. (cdr (assoc property org-global-properties))))))
  14728. (defun org-entry-put (pom property value)
  14729. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14730. (org-with-point-at pom
  14731. (org-back-to-heading t)
  14732. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14733. range)
  14734. (cond
  14735. ((equal property "TODO")
  14736. (when (and (stringp value) (string-match "\\S-" value)
  14737. (not (member value org-todo-keywords-1)))
  14738. (error "\"%s\" is not a valid TODO state" value))
  14739. (if (or (not value)
  14740. (not (string-match "\\S-" value)))
  14741. (setq value 'none))
  14742. (org-todo value)
  14743. (org-set-tags nil 'align))
  14744. ((equal property "PRIORITY")
  14745. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14746. (string-to-char value) ?\ ))
  14747. (org-set-tags nil 'align))
  14748. ((equal property "SCHEDULED")
  14749. (if (re-search-forward org-scheduled-time-regexp end t)
  14750. (cond
  14751. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14752. ((eq value 'later) (org-timestamp-change 1 'day))
  14753. (t (call-interactively 'org-schedule)))
  14754. (call-interactively 'org-schedule)))
  14755. ((equal property "DEADLINE")
  14756. (if (re-search-forward org-deadline-time-regexp end t)
  14757. (cond
  14758. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14759. ((eq value 'later) (org-timestamp-change 1 'day))
  14760. (t (call-interactively 'org-deadline)))
  14761. (call-interactively 'org-deadline)))
  14762. ((member property org-special-properties)
  14763. (error "The %s property can not yet be set with `org-entry-put'"
  14764. property))
  14765. (t ; a non-special property
  14766. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14767. (setq range (org-get-property-block beg end 'force))
  14768. (goto-char (car range))
  14769. (if (re-search-forward
  14770. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14771. (progn
  14772. (delete-region (match-beginning 1) (match-end 1))
  14773. (goto-char (match-beginning 1)))
  14774. (goto-char (cdr range))
  14775. (insert "\n")
  14776. (backward-char 1)
  14777. (org-indent-line-function)
  14778. (insert ":" property ":"))
  14779. (and value (insert " " value))
  14780. (org-indent-line-function)))))))
  14781. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14782. "Get all property keys in the current buffer.
  14783. With INCLUDE-SPECIALS, also list the special properties that relect things
  14784. like tags and TODO state.
  14785. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14786. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14787. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14788. formats in the current buffer."
  14789. (let (rtn range cfmt cols s p)
  14790. (save-excursion
  14791. (save-restriction
  14792. (widen)
  14793. (goto-char (point-min))
  14794. (while (re-search-forward org-property-start-re nil t)
  14795. (setq range (org-get-property-block))
  14796. (goto-char (car range))
  14797. (while (re-search-forward
  14798. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14799. (cdr range) t)
  14800. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14801. (outline-next-heading))))
  14802. (when include-specials
  14803. (setq rtn (append org-special-properties rtn)))
  14804. (when include-defaults
  14805. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14806. (when include-columns
  14807. (save-excursion
  14808. (save-restriction
  14809. (widen)
  14810. (goto-char (point-min))
  14811. (while (re-search-forward
  14812. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14813. nil t)
  14814. (setq cfmt (match-string 2) s 0)
  14815. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14816. cfmt s)
  14817. (setq s (match-end 0)
  14818. p (match-string 1 cfmt))
  14819. (unless (or (equal p "ITEM")
  14820. (member p org-special-properties))
  14821. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14822. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14823. (defun org-property-values (key)
  14824. "Return a list of all values of property KEY."
  14825. (save-excursion
  14826. (save-restriction
  14827. (widen)
  14828. (goto-char (point-min))
  14829. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14830. values)
  14831. (while (re-search-forward re nil t)
  14832. (add-to-list 'values (org-trim (match-string 1))))
  14833. (delete "" values)))))
  14834. (defun org-insert-property-drawer ()
  14835. "Insert a property drawer into the current entry."
  14836. (interactive)
  14837. (org-back-to-heading t)
  14838. (looking-at outline-regexp)
  14839. (let ((indent (- (match-end 0)(match-beginning 0)))
  14840. (beg (point))
  14841. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14842. end hiddenp)
  14843. (outline-next-heading)
  14844. (setq end (point))
  14845. (goto-char beg)
  14846. (while (re-search-forward re end t))
  14847. (setq hiddenp (org-invisible-p))
  14848. (end-of-line 1)
  14849. (and (equal (char-after) ?\n) (forward-char 1))
  14850. (org-skip-over-state-notes)
  14851. (skip-chars-backward " \t\n\r")
  14852. (if (eq (char-before) ?*) (forward-char 1))
  14853. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14854. (beginning-of-line 0)
  14855. (indent-to-column indent)
  14856. (beginning-of-line 2)
  14857. (indent-to-column indent)
  14858. (beginning-of-line 0)
  14859. (if hiddenp
  14860. (save-excursion
  14861. (org-back-to-heading t)
  14862. (hide-entry))
  14863. (org-flag-drawer t))))
  14864. (defun org-set-property (property value)
  14865. "In the current entry, set PROPERTY to VALUE.
  14866. When called interactively, this will prompt for a property name, offering
  14867. completion on existing and default properties. And then it will prompt
  14868. for a value, offering competion either on allowed values (via an inherited
  14869. xxx_ALL property) or on existing values in other instances of this property
  14870. in the current file."
  14871. (interactive
  14872. (let* ((prop (completing-read
  14873. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  14874. (cur (org-entry-get nil prop))
  14875. (allowed (org-property-get-allowed-values nil prop 'table))
  14876. (existing (mapcar 'list (org-property-values prop)))
  14877. (val (if allowed
  14878. (completing-read "Value: " allowed nil 'req-match)
  14879. (completing-read
  14880. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14881. (concat "[" cur "]") "")
  14882. ": ")
  14883. existing nil nil "" nil cur))))
  14884. (list prop (if (equal val "") cur val))))
  14885. (unless (equal (org-entry-get nil property) value)
  14886. (org-entry-put nil property value)))
  14887. (defun org-delete-property (property)
  14888. "In the current entry, delete PROPERTY."
  14889. (interactive
  14890. (let* ((prop (completing-read
  14891. "Property: " (org-entry-properties nil 'standard))))
  14892. (list prop)))
  14893. (message "Property %s %s" property
  14894. (if (org-entry-delete nil property)
  14895. "deleted"
  14896. "was not present in the entry")))
  14897. (defun org-delete-property-globally (property)
  14898. "Remove PROPERTY globally, from all entries."
  14899. (interactive
  14900. (let* ((prop (completing-read
  14901. "Globally remove property: "
  14902. (mapcar 'list (org-buffer-property-keys)))))
  14903. (list prop)))
  14904. (save-excursion
  14905. (save-restriction
  14906. (widen)
  14907. (goto-char (point-min))
  14908. (let ((cnt 0))
  14909. (while (re-search-forward
  14910. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14911. nil t)
  14912. (setq cnt (1+ cnt))
  14913. (replace-match ""))
  14914. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14915. (defvar org-columns-current-fmt-compiled) ; defined below
  14916. (defun org-compute-property-at-point ()
  14917. "Compute the property at point.
  14918. This looks for an enclosing column format, extracts the operator and
  14919. then applies it to the proerty in the column format's scope."
  14920. (interactive)
  14921. (unless (org-at-property-p)
  14922. (error "Not at a property"))
  14923. (let ((prop (org-match-string-no-properties 2)))
  14924. (org-columns-get-format-and-top-level)
  14925. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14926. (error "No operator defined for property %s" prop))
  14927. (org-columns-compute prop)))
  14928. (defun org-property-get-allowed-values (pom property &optional table)
  14929. "Get allowed values for the property PROPERTY.
  14930. When TABLE is non-nil, return an alist that can directly be used for
  14931. completion."
  14932. (let (vals)
  14933. (cond
  14934. ((equal property "TODO")
  14935. (setq vals (org-with-point-at pom
  14936. (append org-todo-keywords-1 '("")))))
  14937. ((equal property "PRIORITY")
  14938. (let ((n org-lowest-priority))
  14939. (while (>= n org-highest-priority)
  14940. (push (char-to-string n) vals)
  14941. (setq n (1- n)))))
  14942. ((member property org-special-properties))
  14943. (t
  14944. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14945. (when (and vals (string-match "\\S-" vals))
  14946. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14947. (setq vals (mapcar (lambda (x)
  14948. (cond ((stringp x) x)
  14949. ((numberp x) (number-to-string x))
  14950. ((symbolp x) (symbol-name x))
  14951. (t "???")))
  14952. vals)))))
  14953. (if table (mapcar 'list vals) vals)))
  14954. (defun org-property-previous-allowed-value (&optional previous)
  14955. "Switch to the next allowed value for this property."
  14956. (interactive)
  14957. (org-property-next-allowed-value t))
  14958. (defun org-property-next-allowed-value (&optional previous)
  14959. "Switch to the next allowed value for this property."
  14960. (interactive)
  14961. (unless (org-at-property-p)
  14962. (error "Not at a property"))
  14963. (let* ((key (match-string 2))
  14964. (value (match-string 3))
  14965. (allowed (or (org-property-get-allowed-values (point) key)
  14966. (and (member value '("[ ]" "[-]" "[X]"))
  14967. '("[ ]" "[X]"))))
  14968. nval)
  14969. (unless allowed
  14970. (error "Allowed values for this property have not been defined"))
  14971. (if previous (setq allowed (reverse allowed)))
  14972. (if (member value allowed)
  14973. (setq nval (car (cdr (member value allowed)))))
  14974. (setq nval (or nval (car allowed)))
  14975. (if (equal nval value)
  14976. (error "Only one allowed value for this property"))
  14977. (org-at-property-p)
  14978. (replace-match (concat " :" key ": " nval) t t)
  14979. (org-indent-line-function)
  14980. (beginning-of-line 1)
  14981. (skip-chars-forward " \t")))
  14982. (defun org-find-entry-with-id (ident)
  14983. "Locate the entry that contains the ID property with exact value IDENT.
  14984. IDENT can be a string, a symbol or a number, this function will search for
  14985. the string representation of it.
  14986. Return the position where this entry starts, or nil if there is no such entry."
  14987. (let ((id (cond
  14988. ((stringp ident) ident)
  14989. ((symbol-name ident) (symbol-name ident))
  14990. ((numberp ident) (number-to-string ident))
  14991. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14992. (case-fold-search nil))
  14993. (save-excursion
  14994. (save-restriction
  14995. (widen)
  14996. (goto-char (point-min))
  14997. (when (re-search-forward
  14998. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14999. nil t)
  15000. (org-back-to-heading)
  15001. (point))))))
  15002. ;;; Column View
  15003. (defvar org-columns-overlays nil
  15004. "Holds the list of current column overlays.")
  15005. (defvar org-columns-current-fmt nil
  15006. "Local variable, holds the currently active column format.")
  15007. (defvar org-columns-current-fmt-compiled nil
  15008. "Local variable, holds the currently active column format.
  15009. This is the compiled version of the format.")
  15010. (defvar org-columns-current-widths nil
  15011. "Loval variable, holds the currently widths of fields.")
  15012. (defvar org-columns-current-maxwidths nil
  15013. "Loval variable, holds the currently active maximum column widths.")
  15014. (defvar org-columns-begin-marker (make-marker)
  15015. "Points to the position where last a column creation command was called.")
  15016. (defvar org-columns-top-level-marker (make-marker)
  15017. "Points to the position where current columns region starts.")
  15018. (defvar org-columns-map (make-sparse-keymap)
  15019. "The keymap valid in column display.")
  15020. (defun org-columns-content ()
  15021. "Switch to contents view while in columns view."
  15022. (interactive)
  15023. (org-overview)
  15024. (org-content))
  15025. (org-defkey org-columns-map "c" 'org-columns-content)
  15026. (org-defkey org-columns-map "o" 'org-overview)
  15027. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15028. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15029. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15030. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15031. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15032. (org-defkey org-columns-map "q" 'org-columns-quit)
  15033. (org-defkey org-columns-map "r" 'org-columns-redo)
  15034. (org-defkey org-columns-map "g" 'org-columns-redo)
  15035. (org-defkey org-columns-map [left] 'backward-char)
  15036. (org-defkey org-columns-map "\M-b" 'backward-char)
  15037. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15038. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15039. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15040. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15041. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15042. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15043. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15044. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15045. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15046. (org-defkey org-columns-map ">" 'org-columns-widen)
  15047. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15048. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15049. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15050. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15051. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15052. '("Column"
  15053. ["Edit property" org-columns-edit-value t]
  15054. ["Next allowed value" org-columns-next-allowed-value t]
  15055. ["Previous allowed value" org-columns-previous-allowed-value t]
  15056. ["Show full value" org-columns-show-value t]
  15057. ["Edit allowed values" org-columns-edit-allowed t]
  15058. "--"
  15059. ["Edit column attributes" org-columns-edit-attributes t]
  15060. ["Increase column width" org-columns-widen t]
  15061. ["Decrease column width" org-columns-narrow t]
  15062. "--"
  15063. ["Move column right" org-columns-move-right t]
  15064. ["Move column left" org-columns-move-left t]
  15065. ["Add column" org-columns-new t]
  15066. ["Delete column" org-columns-delete t]
  15067. "--"
  15068. ["CONTENTS" org-columns-content t]
  15069. ["OVERVIEW" org-overview t]
  15070. ["Refresh columns display" org-columns-redo t]
  15071. "--"
  15072. ["Open link" org-columns-open-link t]
  15073. "--"
  15074. ["Quit" org-columns-quit t]))
  15075. (defun org-columns-new-overlay (beg end &optional string face)
  15076. "Create a new column overlay and add it to the list."
  15077. (let ((ov (org-make-overlay beg end)))
  15078. (org-overlay-put ov 'face (or face 'secondary-selection))
  15079. (org-overlay-display ov string face)
  15080. (push ov org-columns-overlays)
  15081. ov))
  15082. (defun org-columns-display-here (&optional props)
  15083. "Overlay the current line with column display."
  15084. (interactive)
  15085. (let* ((fmt org-columns-current-fmt-compiled)
  15086. (beg (point-at-bol))
  15087. (level-face (save-excursion
  15088. (beginning-of-line 1)
  15089. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15090. (org-get-level-face 2))))
  15091. (color (list :foreground
  15092. (face-attribute (or level-face 'default) :foreground)))
  15093. props pom property ass width f string ov column val modval)
  15094. ;; Check if the entry is in another buffer.
  15095. (unless props
  15096. (if (eq major-mode 'org-agenda-mode)
  15097. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15098. (get-text-property (point) 'org-marker))
  15099. props (if pom (org-entry-properties pom) nil))
  15100. (setq props (org-entry-properties nil))))
  15101. ;; Walk the format
  15102. (while (setq column (pop fmt))
  15103. (setq property (car column)
  15104. ass (if (equal property "ITEM")
  15105. (cons "ITEM"
  15106. (save-match-data
  15107. (org-no-properties
  15108. (org-remove-tabs
  15109. (buffer-substring-no-properties
  15110. (point-at-bol) (point-at-eol))))))
  15111. (assoc property props))
  15112. width (or (cdr (assoc property org-columns-current-maxwidths))
  15113. (nth 2 column)
  15114. (length property))
  15115. f (format "%%-%d.%ds | " width width)
  15116. val (or (cdr ass) "")
  15117. modval (if (equal property "ITEM")
  15118. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15119. string (format f (or modval val)))
  15120. ;; Create the overlay
  15121. (org-unmodified
  15122. (setq ov (org-columns-new-overlay
  15123. beg (setq beg (1+ beg)) string
  15124. (list color 'org-column)))
  15125. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15126. (org-overlay-put ov 'keymap org-columns-map)
  15127. (org-overlay-put ov 'org-columns-key property)
  15128. (org-overlay-put ov 'org-columns-value (cdr ass))
  15129. (org-overlay-put ov 'org-columns-value-modified modval)
  15130. (org-overlay-put ov 'org-columns-pom pom)
  15131. (org-overlay-put ov 'org-columns-format f))
  15132. (if (or (not (char-after beg))
  15133. (equal (char-after beg) ?\n))
  15134. (let ((inhibit-read-only t))
  15135. (save-excursion
  15136. (goto-char beg)
  15137. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15138. ;; Make the rest of the line disappear.
  15139. (org-unmodified
  15140. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15141. (org-overlay-put ov 'invisible t)
  15142. (org-overlay-put ov 'keymap org-columns-map)
  15143. (org-overlay-put ov 'intangible t)
  15144. (push ov org-columns-overlays)
  15145. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15146. (org-overlay-put ov 'keymap org-columns-map)
  15147. (push ov org-columns-overlays)
  15148. (let ((inhibit-read-only t))
  15149. (put-text-property (max (point-min) (1- (point-at-bol)))
  15150. (min (point-max) (1+ (point-at-eol)))
  15151. 'read-only "Type `e' to edit property")))))
  15152. (defvar org-previous-header-line-format nil
  15153. "The header line format before column view was turned on.")
  15154. (defvar org-columns-inhibit-recalculation nil
  15155. "Inhibit recomputing of columns on column view startup.")
  15156. (defvar header-line-format)
  15157. (defun org-columns-display-here-title ()
  15158. "Overlay the newline before the current line with the table title."
  15159. (interactive)
  15160. (let ((fmt org-columns-current-fmt-compiled)
  15161. string (title "")
  15162. property width f column str widths)
  15163. (while (setq column (pop fmt))
  15164. (setq property (car column)
  15165. str (or (nth 1 column) property)
  15166. width (or (cdr (assoc property org-columns-current-maxwidths))
  15167. (nth 2 column)
  15168. (length str))
  15169. widths (push width widths)
  15170. f (format "%%-%d.%ds | " width width)
  15171. string (format f str)
  15172. title (concat title string)))
  15173. (setq title (concat
  15174. (org-add-props " " nil 'display '(space :align-to 0))
  15175. (org-add-props title nil 'face '(:weight bold :underline t))))
  15176. (org-set-local 'org-previous-header-line-format header-line-format)
  15177. (org-set-local 'org-columns-current-widths (nreverse widths))
  15178. (setq header-line-format title)))
  15179. (defun org-columns-remove-overlays ()
  15180. "Remove all currently active column overlays."
  15181. (interactive)
  15182. (when (marker-buffer org-columns-begin-marker)
  15183. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15184. (when (local-variable-p 'org-previous-header-line-format)
  15185. (setq header-line-format org-previous-header-line-format)
  15186. (kill-local-variable 'org-previous-header-line-format))
  15187. (move-marker org-columns-begin-marker nil)
  15188. (move-marker org-columns-top-level-marker nil)
  15189. (org-unmodified
  15190. (mapc 'org-delete-overlay org-columns-overlays)
  15191. (setq org-columns-overlays nil)
  15192. (let ((inhibit-read-only t))
  15193. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15194. (defun org-columns-cleanup-item (item fmt)
  15195. "Remove from ITEM what is a column in the format FMT."
  15196. (if (not org-complex-heading-regexp)
  15197. item
  15198. (when (string-match org-complex-heading-regexp item)
  15199. (concat
  15200. (org-add-props (concat (match-string 1 item) " ") nil
  15201. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15202. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15203. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15204. " " (match-string 4 item)
  15205. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15206. (defun org-columns-show-value ()
  15207. "Show the full value of the property."
  15208. (interactive)
  15209. (let ((value (get-char-property (point) 'org-columns-value)))
  15210. (message "Value is: %s" (or value ""))))
  15211. (defun org-columns-quit ()
  15212. "Remove the column overlays and in this way exit column editing."
  15213. (interactive)
  15214. (org-unmodified
  15215. (org-columns-remove-overlays)
  15216. (let ((inhibit-read-only t))
  15217. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15218. (when (eq major-mode 'org-agenda-mode)
  15219. (message
  15220. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15221. (defun org-columns-check-computed ()
  15222. "Check if this column value is computed.
  15223. If yes, throw an error indicating that changing it does not make sense."
  15224. (let ((val (get-char-property (point) 'org-columns-value)))
  15225. (when (and (stringp val)
  15226. (get-char-property 0 'org-computed val))
  15227. (error "This value is computed from the entry's children"))))
  15228. (defun org-columns-todo (&optional arg)
  15229. "Change the TODO state during column view."
  15230. (interactive "P")
  15231. (org-columns-edit-value "TODO"))
  15232. (defun org-columns-set-tags-or-toggle (&optional arg)
  15233. "Toggle checkbox at point, or set tags for current headline."
  15234. (interactive "P")
  15235. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15236. (get-char-property (point) 'org-columns-value))
  15237. (org-columns-next-allowed-value)
  15238. (org-columns-edit-value "TAGS")))
  15239. (defun org-columns-edit-value (&optional key)
  15240. "Edit the value of the property at point in column view.
  15241. Where possible, use the standard interface for changing this line."
  15242. (interactive)
  15243. (org-columns-check-computed)
  15244. (let* ((external-key key)
  15245. (col (current-column))
  15246. (key (or key (get-char-property (point) 'org-columns-key)))
  15247. (value (get-char-property (point) 'org-columns-value))
  15248. (bol (point-at-bol)) (eol (point-at-eol))
  15249. (pom (or (get-text-property bol 'org-hd-marker)
  15250. (point))) ; keep despite of compiler waring
  15251. (line-overlays
  15252. (delq nil (mapcar (lambda (x)
  15253. (and (eq (overlay-buffer x) (current-buffer))
  15254. (>= (overlay-start x) bol)
  15255. (<= (overlay-start x) eol)
  15256. x))
  15257. org-columns-overlays)))
  15258. nval eval allowed)
  15259. (cond
  15260. ((equal key "CLOCKSUM")
  15261. (error "This special column cannot be edited"))
  15262. ((equal key "ITEM")
  15263. (setq eval '(org-with-point-at pom
  15264. (org-edit-headline))))
  15265. ((equal key "TODO")
  15266. (setq eval '(org-with-point-at pom
  15267. (let ((current-prefix-arg
  15268. (if external-key current-prefix-arg '(4))))
  15269. (call-interactively 'org-todo)))))
  15270. ((equal key "PRIORITY")
  15271. (setq eval '(org-with-point-at pom
  15272. (call-interactively 'org-priority))))
  15273. ((equal key "TAGS")
  15274. (setq eval '(org-with-point-at pom
  15275. (let ((org-fast-tag-selection-single-key
  15276. (if (eq org-fast-tag-selection-single-key 'expert)
  15277. t org-fast-tag-selection-single-key)))
  15278. (call-interactively 'org-set-tags)))))
  15279. ((equal key "DEADLINE")
  15280. (setq eval '(org-with-point-at pom
  15281. (call-interactively 'org-deadline))))
  15282. ((equal key "SCHEDULED")
  15283. (setq eval '(org-with-point-at pom
  15284. (call-interactively 'org-schedule))))
  15285. (t
  15286. (setq allowed (org-property-get-allowed-values pom key 'table))
  15287. (if allowed
  15288. (setq nval (completing-read "Value: " allowed nil t))
  15289. (setq nval (read-string "Edit: " value)))
  15290. (setq nval (org-trim nval))
  15291. (when (not (equal nval value))
  15292. (setq eval '(org-entry-put pom key nval)))))
  15293. (when eval
  15294. (let ((inhibit-read-only t))
  15295. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15296. (unwind-protect
  15297. (progn
  15298. (setq org-columns-overlays
  15299. (org-delete-all line-overlays org-columns-overlays))
  15300. (mapc 'org-delete-overlay line-overlays)
  15301. (org-columns-eval eval))
  15302. (org-columns-display-here))))
  15303. (move-to-column col)
  15304. (if (and (org-mode-p)
  15305. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15306. (org-columns-update key))))
  15307. (defun org-edit-headline () ; FIXME: this is not columns specific
  15308. "Edit the current headline, the part without TODO keyword, TAGS."
  15309. (org-back-to-heading)
  15310. (when (looking-at org-todo-line-regexp)
  15311. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15312. (txt (match-string 3))
  15313. (post "")
  15314. txt2)
  15315. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15316. (setq post (match-string 0 txt)
  15317. txt (substring txt 0 (match-beginning 0))))
  15318. (setq txt2 (read-string "Edit: " txt))
  15319. (when (not (equal txt txt2))
  15320. (beginning-of-line 1)
  15321. (insert pre txt2 post)
  15322. (delete-region (point) (point-at-eol))
  15323. (org-set-tags nil t)))))
  15324. (defun org-columns-edit-allowed ()
  15325. "Edit the list of allowed values for the current property."
  15326. (interactive)
  15327. (let* ((key (get-char-property (point) 'org-columns-key))
  15328. (key1 (concat key "_ALL"))
  15329. (allowed (org-entry-get (point) key1 t))
  15330. nval)
  15331. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15332. (setq nval (read-string "Allowed: " allowed))
  15333. (org-entry-put
  15334. (cond ((marker-position org-entry-property-inherited-from)
  15335. org-entry-property-inherited-from)
  15336. ((marker-position org-columns-top-level-marker)
  15337. org-columns-top-level-marker))
  15338. key1 nval)))
  15339. (defmacro org-no-warnings (&rest body)
  15340. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15341. (defun org-columns-eval (form)
  15342. (let (hidep)
  15343. (save-excursion
  15344. (beginning-of-line 1)
  15345. ;; `next-line' is needed here, because it skips invisible line.
  15346. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15347. (setq hidep (org-on-heading-p 1)))
  15348. (eval form)
  15349. (and hidep (hide-entry))))
  15350. (defun org-columns-previous-allowed-value ()
  15351. "Switch to the previous allowed value for this column."
  15352. (interactive)
  15353. (org-columns-next-allowed-value t))
  15354. (defun org-columns-next-allowed-value (&optional previous)
  15355. "Switch to the next allowed value for this column."
  15356. (interactive)
  15357. (org-columns-check-computed)
  15358. (let* ((col (current-column))
  15359. (key (get-char-property (point) 'org-columns-key))
  15360. (value (get-char-property (point) 'org-columns-value))
  15361. (bol (point-at-bol)) (eol (point-at-eol))
  15362. (pom (or (get-text-property bol 'org-hd-marker)
  15363. (point))) ; keep despite of compiler waring
  15364. (line-overlays
  15365. (delq nil (mapcar (lambda (x)
  15366. (and (eq (overlay-buffer x) (current-buffer))
  15367. (>= (overlay-start x) bol)
  15368. (<= (overlay-start x) eol)
  15369. x))
  15370. org-columns-overlays)))
  15371. (allowed (or (org-property-get-allowed-values pom key)
  15372. (and (memq
  15373. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15374. '(checkbox checkbox-n-of-m checkbox-percent))
  15375. '("[ ]" "[X]"))))
  15376. nval)
  15377. (when (equal key "ITEM")
  15378. (error "Cannot edit item headline from here"))
  15379. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15380. (error "Allowed values for this property have not been defined"))
  15381. (if (member key '("SCHEDULED" "DEADLINE"))
  15382. (setq nval (if previous 'earlier 'later))
  15383. (if previous (setq allowed (reverse allowed)))
  15384. (if (member value allowed)
  15385. (setq nval (car (cdr (member value allowed)))))
  15386. (setq nval (or nval (car allowed)))
  15387. (if (equal nval value)
  15388. (error "Only one allowed value for this property")))
  15389. (let ((inhibit-read-only t))
  15390. (remove-text-properties (1- bol) eol '(read-only t))
  15391. (unwind-protect
  15392. (progn
  15393. (setq org-columns-overlays
  15394. (org-delete-all line-overlays org-columns-overlays))
  15395. (mapc 'org-delete-overlay line-overlays)
  15396. (org-columns-eval '(org-entry-put pom key nval)))
  15397. (org-columns-display-here)))
  15398. (move-to-column col)
  15399. (if (and (org-mode-p)
  15400. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15401. (org-columns-update key))))
  15402. (defun org-verify-version (task)
  15403. (cond
  15404. ((eq task 'columns)
  15405. (if (or (featurep 'xemacs)
  15406. (< emacs-major-version 22))
  15407. (error "Emacs 22 is required for the columns feature")))))
  15408. (defun org-columns-open-link (&optional arg)
  15409. (interactive "P")
  15410. (let ((value (get-char-property (point) 'org-columns-value)))
  15411. (org-open-link-from-string value arg)))
  15412. (defun org-open-link-from-string (s &optional arg)
  15413. "Open a link in the string S, as if it was in Org-mode."
  15414. (interactive)
  15415. (with-temp-buffer
  15416. (let ((org-inhibit-startup t))
  15417. (org-mode)
  15418. (insert s)
  15419. (goto-char (point-min))
  15420. (org-open-at-point arg))))
  15421. (defun org-columns-get-format-and-top-level ()
  15422. (let (fmt)
  15423. (when (condition-case nil (org-back-to-heading) (error nil))
  15424. (move-marker org-entry-property-inherited-from nil)
  15425. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15426. (setq fmt (or fmt org-columns-default-format))
  15427. (org-set-local 'org-columns-current-fmt fmt)
  15428. (org-columns-compile-format fmt)
  15429. (if (marker-position org-entry-property-inherited-from)
  15430. (move-marker org-columns-top-level-marker
  15431. org-entry-property-inherited-from)
  15432. (move-marker org-columns-top-level-marker (point)))
  15433. fmt))
  15434. (defun org-columns ()
  15435. "Turn on column view on an org-mode file."
  15436. (interactive)
  15437. (org-verify-version 'columns)
  15438. (org-columns-remove-overlays)
  15439. (move-marker org-columns-begin-marker (point))
  15440. (let (beg end fmt cache maxwidths)
  15441. (setq fmt (org-columns-get-format-and-top-level))
  15442. (save-excursion
  15443. (goto-char org-columns-top-level-marker)
  15444. (setq beg (point))
  15445. (unless org-columns-inhibit-recalculation
  15446. (org-columns-compute-all))
  15447. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15448. (point-max)))
  15449. ;; Get and cache the properties
  15450. (goto-char beg)
  15451. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15452. (save-excursion
  15453. (save-restriction
  15454. (narrow-to-region beg end)
  15455. (org-clock-sum))))
  15456. (while (re-search-forward (concat "^" outline-regexp) end t)
  15457. (push (cons (org-current-line) (org-entry-properties)) cache))
  15458. (when cache
  15459. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15460. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15461. (org-columns-display-here-title)
  15462. (mapc (lambda (x)
  15463. (goto-line (car x))
  15464. (org-columns-display-here (cdr x)))
  15465. cache)))))
  15466. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15467. "Insert a new column, to the left of the current column."
  15468. (interactive)
  15469. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15470. cell)
  15471. (setq prop (completing-read
  15472. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15473. nil nil prop))
  15474. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15475. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15476. (if (string-match "\\S-" width)
  15477. (setq width (string-to-number width))
  15478. (setq width nil))
  15479. (setq fmt (completing-read "Summary [none]: "
  15480. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15481. nil t))
  15482. (if (string-match "\\S-" fmt)
  15483. (setq fmt (intern fmt))
  15484. (setq fmt nil))
  15485. (if (eq fmt 'none) (setq fmt nil))
  15486. (if editp
  15487. (progn
  15488. (setcar editp prop)
  15489. (setcdr editp (list title width nil fmt)))
  15490. (setq cell (nthcdr (1- (current-column))
  15491. org-columns-current-fmt-compiled))
  15492. (setcdr cell (cons (list prop title width nil fmt)
  15493. (cdr cell))))
  15494. (org-columns-store-format)
  15495. (org-columns-redo)))
  15496. (defun org-columns-delete ()
  15497. "Delete the column at point from columns view."
  15498. (interactive)
  15499. (let* ((n (current-column))
  15500. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15501. (when (y-or-n-p
  15502. (format "Are you sure you want to remove column \"%s\"? " title))
  15503. (setq org-columns-current-fmt-compiled
  15504. (delq (nth n org-columns-current-fmt-compiled)
  15505. org-columns-current-fmt-compiled))
  15506. (org-columns-store-format)
  15507. (org-columns-redo)
  15508. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15509. (backward-char 1)))))
  15510. (defun org-columns-edit-attributes ()
  15511. "Edit the attributes of the current column."
  15512. (interactive)
  15513. (let* ((n (current-column))
  15514. (info (nth n org-columns-current-fmt-compiled)))
  15515. (apply 'org-columns-new info)))
  15516. (defun org-columns-widen (arg)
  15517. "Make the column wider by ARG characters."
  15518. (interactive "p")
  15519. (let* ((n (current-column))
  15520. (entry (nth n org-columns-current-fmt-compiled))
  15521. (width (or (nth 2 entry)
  15522. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15523. (setq width (max 1 (+ width arg)))
  15524. (setcar (nthcdr 2 entry) width)
  15525. (org-columns-store-format)
  15526. (org-columns-redo)))
  15527. (defun org-columns-narrow (arg)
  15528. "Make the column nrrower by ARG characters."
  15529. (interactive "p")
  15530. (org-columns-widen (- arg)))
  15531. (defun org-columns-move-right ()
  15532. "Swap this column with the one to the right."
  15533. (interactive)
  15534. (let* ((n (current-column))
  15535. (cell (nthcdr n org-columns-current-fmt-compiled))
  15536. e)
  15537. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15538. (error "Cannot shift this column further to the right"))
  15539. (setq e (car cell))
  15540. (setcar cell (car (cdr cell)))
  15541. (setcdr cell (cons e (cdr (cdr cell))))
  15542. (org-columns-store-format)
  15543. (org-columns-redo)
  15544. (forward-char 1)))
  15545. (defun org-columns-move-left ()
  15546. "Swap this column with the one to the left."
  15547. (interactive)
  15548. (let* ((n (current-column)))
  15549. (when (= n 0)
  15550. (error "Cannot shift this column further to the left"))
  15551. (backward-char 1)
  15552. (org-columns-move-right)
  15553. (backward-char 1)))
  15554. (defun org-columns-store-format ()
  15555. "Store the text version of the current columns format in appropriate place.
  15556. This is either in the COLUMNS property of the node starting the current column
  15557. display, or in the #+COLUMNS line of the current buffer."
  15558. (let (fmt (cnt 0))
  15559. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15560. (org-set-local 'org-columns-current-fmt fmt)
  15561. (if (marker-position org-columns-top-level-marker)
  15562. (save-excursion
  15563. (goto-char org-columns-top-level-marker)
  15564. (if (and (org-at-heading-p)
  15565. (org-entry-get nil "COLUMNS"))
  15566. (org-entry-put nil "COLUMNS" fmt)
  15567. (goto-char (point-min))
  15568. ;; Overwrite all #+COLUMNS lines....
  15569. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15570. (setq cnt (1+ cnt))
  15571. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15572. (unless (> cnt 0)
  15573. (goto-char (point-min))
  15574. (or (org-on-heading-p t) (outline-next-heading))
  15575. (let ((inhibit-read-only t))
  15576. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15577. (org-set-local 'org-columns-default-format fmt))))))
  15578. (defvar org-overriding-columns-format nil
  15579. "When set, overrides any other definition.")
  15580. (defvar org-agenda-view-columns-initially nil
  15581. "When set, switch to columns view immediately after creating the agenda.")
  15582. (defun org-agenda-columns ()
  15583. "Turn on column view in the agenda."
  15584. (interactive)
  15585. (org-verify-version 'columns)
  15586. (org-columns-remove-overlays)
  15587. (move-marker org-columns-begin-marker (point))
  15588. (let (fmt cache maxwidths m)
  15589. (cond
  15590. ((and (local-variable-p 'org-overriding-columns-format)
  15591. org-overriding-columns-format)
  15592. (setq fmt org-overriding-columns-format))
  15593. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15594. (setq fmt (org-entry-get m "COLUMNS" t)))
  15595. ((and (boundp 'org-columns-current-fmt)
  15596. (local-variable-p 'org-columns-current-fmt)
  15597. org-columns-current-fmt)
  15598. (setq fmt org-columns-current-fmt))
  15599. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15600. (setq m (get-text-property m 'org-hd-marker))
  15601. (setq fmt (org-entry-get m "COLUMNS" t))))
  15602. (setq fmt (or fmt org-columns-default-format))
  15603. (org-set-local 'org-columns-current-fmt fmt)
  15604. (org-columns-compile-format fmt)
  15605. (save-excursion
  15606. ;; Get and cache the properties
  15607. (goto-char (point-min))
  15608. (while (not (eobp))
  15609. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15610. (get-text-property (point) 'org-marker)))
  15611. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15612. (beginning-of-line 2))
  15613. (when cache
  15614. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15615. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15616. (org-columns-display-here-title)
  15617. (mapc (lambda (x)
  15618. (goto-line (car x))
  15619. (org-columns-display-here (cdr x)))
  15620. cache)))))
  15621. (defun org-columns-get-autowidth-alist (s cache)
  15622. "Derive the maximum column widths from the format and the cache."
  15623. (let ((start 0) rtn)
  15624. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15625. (push (cons (match-string 1 s) 1) rtn)
  15626. (setq start (match-end 0)))
  15627. (mapc (lambda (x)
  15628. (setcdr x (apply 'max
  15629. (mapcar
  15630. (lambda (y)
  15631. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15632. cache))))
  15633. rtn)
  15634. rtn))
  15635. (defun org-columns-compute-all ()
  15636. "Compute all columns that have operators defined."
  15637. (org-unmodified
  15638. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15639. (let ((columns org-columns-current-fmt-compiled) col)
  15640. (while (setq col (pop columns))
  15641. (when (nth 3 col)
  15642. (save-excursion
  15643. (org-columns-compute (car col)))))))
  15644. (defun org-columns-update (property)
  15645. "Recompute PROPERTY, and update the columns display for it."
  15646. (org-columns-compute property)
  15647. (let (fmt val pos)
  15648. (save-excursion
  15649. (mapc (lambda (ov)
  15650. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15651. (setq pos (org-overlay-start ov))
  15652. (goto-char pos)
  15653. (when (setq val (cdr (assoc property
  15654. (get-text-property
  15655. (point-at-bol) 'org-summaries))))
  15656. (setq fmt (org-overlay-get ov 'org-columns-format))
  15657. (org-overlay-put ov 'org-columns-value val)
  15658. (org-overlay-put ov 'display (format fmt val)))))
  15659. org-columns-overlays))))
  15660. (defun org-columns-compute (property)
  15661. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15662. (interactive)
  15663. (let* ((re (concat "^" outline-regexp))
  15664. (lmax 30) ; Does anyone use deeper levels???
  15665. (lsum (make-vector lmax 0))
  15666. (lflag (make-vector lmax nil))
  15667. (level 0)
  15668. (ass (assoc property org-columns-current-fmt-compiled))
  15669. (format (nth 4 ass))
  15670. (printf (nth 5 ass))
  15671. (beg org-columns-top-level-marker)
  15672. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15673. (save-excursion
  15674. ;; Find the region to compute
  15675. (goto-char beg)
  15676. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15677. (goto-char end)
  15678. ;; Walk the tree from the back and do the computations
  15679. (while (re-search-backward re beg t)
  15680. (setq sumpos (match-beginning 0)
  15681. last-level level
  15682. level (org-outline-level)
  15683. val (org-entry-get nil property)
  15684. valflag (and val (string-match "\\S-" val)))
  15685. (cond
  15686. ((< level last-level)
  15687. ;; put the sum of lower levels here as a property
  15688. (setq sum (aref lsum last-level) ; current sum
  15689. flag (aref lflag last-level) ; any valid entries from children?
  15690. str (org-column-number-to-string sum format printf)
  15691. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15692. useval (if flag str1 (if valflag val ""))
  15693. sum-alist (get-text-property sumpos 'org-summaries))
  15694. (if (assoc property sum-alist)
  15695. (setcdr (assoc property sum-alist) useval)
  15696. (push (cons property useval) sum-alist)
  15697. (org-unmodified
  15698. (add-text-properties sumpos (1+ sumpos)
  15699. (list 'org-summaries sum-alist))))
  15700. (when val
  15701. (org-entry-put nil property (if flag str val)))
  15702. ;; add current to current level accumulator
  15703. (when (or flag valflag)
  15704. (aset lsum level (+ (aref lsum level)
  15705. (if flag sum (org-column-string-to-number
  15706. (if flag str val) format))))
  15707. (aset lflag level t))
  15708. ;; clear accumulators for deeper levels
  15709. (loop for l from (1+ level) to (1- lmax) do
  15710. (aset lsum l 0)
  15711. (aset lflag l nil)))
  15712. ((>= level last-level)
  15713. ;; add what we have here to the accumulator for this level
  15714. (aset lsum level (+ (aref lsum level)
  15715. (org-column-string-to-number (or val "0") format)))
  15716. (and valflag (aset lflag level t)))
  15717. (t (error "This should not happen")))))))
  15718. (defun org-columns-redo ()
  15719. "Construct the column display again."
  15720. (interactive)
  15721. (message "Recomputing columns...")
  15722. (save-excursion
  15723. (if (marker-position org-columns-begin-marker)
  15724. (goto-char org-columns-begin-marker))
  15725. (org-columns-remove-overlays)
  15726. (if (org-mode-p)
  15727. (call-interactively 'org-columns)
  15728. (call-interactively 'org-agenda-columns)))
  15729. (message "Recomputing columns...done"))
  15730. (defun org-columns-not-in-agenda ()
  15731. (if (eq major-mode 'org-agenda-mode)
  15732. (error "This command is only allowed in Org-mode buffers")))
  15733. (defun org-string-to-number (s)
  15734. "Convert string to number, and interpret hh:mm:ss."
  15735. (if (not (string-match ":" s))
  15736. (string-to-number s)
  15737. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15738. (while l
  15739. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15740. sum)))
  15741. (defun org-column-number-to-string (n fmt &optional printf)
  15742. "Convert a computed column number to a string value, according to FMT."
  15743. (cond
  15744. ((eq fmt 'add_times)
  15745. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15746. (format "%d:%02d" h m)))
  15747. ((eq fmt 'checkbox)
  15748. (cond ((= n (floor n)) "[X]")
  15749. ((> n 1.) "[-]")
  15750. (t "[ ]")))
  15751. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15752. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15753. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15754. (printf (format printf n))
  15755. ((eq fmt 'currency)
  15756. (format "%.2f" n))
  15757. (t (number-to-string n))))
  15758. (defun org-nofm-to-completion (n m &optional percent)
  15759. (if (not percent)
  15760. (format "[%d/%d]" n m)
  15761. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15762. (defun org-column-string-to-number (s fmt)
  15763. "Convert a column value to a number that can be used for column computing."
  15764. (cond
  15765. ((string-match ":" s)
  15766. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15767. (while l
  15768. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15769. sum))
  15770. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15771. (if (equal s "[X]") 1. 0.000001))
  15772. (t (string-to-number s))))
  15773. (defun org-columns-uncompile-format (cfmt)
  15774. "Turn the compiled columns format back into a string representation."
  15775. (let ((rtn "") e s prop title op width fmt printf)
  15776. (while (setq e (pop cfmt))
  15777. (setq prop (car e)
  15778. title (nth 1 e)
  15779. width (nth 2 e)
  15780. op (nth 3 e)
  15781. fmt (nth 4 e)
  15782. printf (nth 5 e))
  15783. (cond
  15784. ((eq fmt 'add_times) (setq op ":"))
  15785. ((eq fmt 'checkbox) (setq op "X"))
  15786. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15787. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15788. ((eq fmt 'add_numbers) (setq op "+"))
  15789. ((eq fmt 'currency) (setq op "$")))
  15790. (if (and op printf) (setq op (concat op ";" printf)))
  15791. (if (equal title prop) (setq title nil))
  15792. (setq s (concat "%" (if width (number-to-string width))
  15793. prop
  15794. (if title (concat "(" title ")"))
  15795. (if op (concat "{" op "}"))))
  15796. (setq rtn (concat rtn " " s)))
  15797. (org-trim rtn)))
  15798. (defun org-columns-compile-format (fmt)
  15799. "Turn a column format string into an alist of specifications.
  15800. The alist has one entry for each column in the format. The elements of
  15801. that list are:
  15802. property the property
  15803. title the title field for the columns
  15804. width the column width in characters, can be nil for automatic
  15805. operator the operator if any
  15806. format the output format for computed results, derived from operator
  15807. printf a printf format for computed values"
  15808. (let ((start 0) width prop title op f printf)
  15809. (setq org-columns-current-fmt-compiled nil)
  15810. (while (string-match
  15811. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15812. fmt start)
  15813. (setq start (match-end 0)
  15814. width (match-string 1 fmt)
  15815. prop (match-string 2 fmt)
  15816. title (or (match-string 3 fmt) prop)
  15817. op (match-string 4 fmt)
  15818. f nil
  15819. printf nil)
  15820. (if width (setq width (string-to-number width)))
  15821. (when (and op (string-match ";" op))
  15822. (setq printf (substring op (match-end 0))
  15823. op (substring op 0 (match-beginning 0))))
  15824. (cond
  15825. ((equal op "+") (setq f 'add_numbers))
  15826. ((equal op "$") (setq f 'currency))
  15827. ((equal op ":") (setq f 'add_times))
  15828. ((equal op "X") (setq f 'checkbox))
  15829. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15830. ((equal op "X%") (setq f 'checkbox-percent))
  15831. )
  15832. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15833. (setq org-columns-current-fmt-compiled
  15834. (nreverse org-columns-current-fmt-compiled))))
  15835. ;;; Dynamic block for Column view
  15836. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  15837. "Get the column view of the current buffer or subtree.
  15838. The first optional argument MAXLEVEL sets the level limit. A
  15839. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  15840. empty rows, an empty row being one where all the column view
  15841. specifiers except ITEM are empty. This function returns a list
  15842. containing the title row and all other rows. Each row is a list
  15843. of fields."
  15844. (save-excursion
  15845. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15846. (n (length title)) row tbl)
  15847. (goto-char (point-min))
  15848. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  15849. (or (null maxlevel)
  15850. (>= maxlevel
  15851. (if org-odd-levels-only
  15852. (/ (1+ (length (match-string 1))) 2)
  15853. (length (match-string 1))))))
  15854. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15855. (setq row nil)
  15856. (loop for i from 0 to (1- n) do
  15857. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15858. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15859. "")
  15860. row))
  15861. (setq row (nreverse row))
  15862. (unless (and skip-empty-rows
  15863. (eq 1 (length (delete "" (delete-dups row)))))
  15864. (push row tbl))))
  15865. (append (list title 'hline) (nreverse tbl)))))
  15866. (defun org-dblock-write:columnview (params)
  15867. "Write the column view table.
  15868. PARAMS is a property list of parameters:
  15869. :width enforce same column widths with <N> specifiers.
  15870. :id the :ID: property of the entry where the columns view
  15871. should be built, as a string. When `local', call locally.
  15872. When `global' call column view with the cursor at the beginning
  15873. of the buffer (usually this means that the whole buffer switches
  15874. to column view).
  15875. :hlines When t, insert a hline before each item. When a number, insert
  15876. a hline before each level <= that number.
  15877. :vlines When t, make each column a colgroup to enforce vertical lines.
  15878. :maxlevel When set to a number, don't capture headlines below this level.
  15879. :skip-empty-rows
  15880. When t, skip rows where all specifiers other than ITEM are empty."
  15881. (let ((pos (move-marker (make-marker) (point)))
  15882. (hlines (plist-get params :hlines))
  15883. (vlines (plist-get params :vlines))
  15884. (maxlevel (plist-get params :maxlevel))
  15885. (skip-empty-rows (plist-get params :skip-empty-rows))
  15886. tbl id idpos nfields tmp)
  15887. (save-excursion
  15888. (save-restriction
  15889. (when (setq id (plist-get params :id))
  15890. (cond ((not id) nil)
  15891. ((eq id 'global) (goto-char (point-min)))
  15892. ((eq id 'local) nil)
  15893. ((setq idpos (org-find-entry-with-id id))
  15894. (goto-char idpos))
  15895. (t (error "Cannot find entry with :ID: %s" id))))
  15896. (org-columns)
  15897. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  15898. (setq nfields (length (car tbl)))
  15899. (org-columns-quit)))
  15900. (goto-char pos)
  15901. (move-marker pos nil)
  15902. (when tbl
  15903. (when (plist-get params :hlines)
  15904. (setq tmp nil)
  15905. (while tbl
  15906. (if (eq (car tbl) 'hline)
  15907. (push (pop tbl) tmp)
  15908. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15909. (if (and (not (eq (car tmp) 'hline))
  15910. (or (eq hlines t)
  15911. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15912. (push 'hline tmp)))
  15913. (push (pop tbl) tmp)))
  15914. (setq tbl (nreverse tmp)))
  15915. (when vlines
  15916. (setq tbl (mapcar (lambda (x)
  15917. (if (eq 'hline x) x (cons "" x)))
  15918. tbl))
  15919. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15920. (setq pos (point))
  15921. (insert (org-listtable-to-string tbl))
  15922. (when (plist-get params :width)
  15923. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15924. org-columns-current-widths "|")))
  15925. (goto-char pos)
  15926. (org-table-align))))
  15927. (defun org-listtable-to-string (tbl)
  15928. "Convert a listtable TBL to a string that contains the Org-mode table.
  15929. The table still need to be alligned. The resulting string has no leading
  15930. and tailing newline characters."
  15931. (mapconcat
  15932. (lambda (x)
  15933. (cond
  15934. ((listp x)
  15935. (concat "|" (mapconcat 'identity x "|") "|"))
  15936. ((eq x 'hline) "|-|")
  15937. (t (error "Garbage in listtable: %s" x))))
  15938. tbl "\n"))
  15939. (defun org-insert-columns-dblock ()
  15940. "Create a dynamic block capturing a column view table."
  15941. (interactive)
  15942. (let ((defaults '(:name "columnview" :hlines 1))
  15943. (id (completing-read
  15944. "Capture columns (local, global, entry with :ID: property) [local]: "
  15945. (append '(("global") ("local"))
  15946. (mapcar 'list (org-property-values "ID"))))))
  15947. (if (equal id "") (setq id 'local))
  15948. (if (equal id "global") (setq id 'global))
  15949. (setq defaults (append defaults (list :id id)))
  15950. (org-create-dblock defaults)
  15951. (org-update-dblock)))
  15952. ;;;; Timestamps
  15953. (defvar org-last-changed-timestamp nil)
  15954. (defvar org-time-was-given) ; dynamically scoped parameter
  15955. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15956. (defvar org-ts-what) ; dynamically scoped parameter
  15957. (defun org-time-stamp (arg)
  15958. "Prompt for a date/time and insert a time stamp.
  15959. If the user specifies a time like HH:MM, or if this command is called
  15960. with a prefix argument, the time stamp will contain date and time.
  15961. Otherwise, only the date will be included. All parts of a date not
  15962. specified by the user will be filled in from the current date/time.
  15963. So if you press just return without typing anything, the time stamp
  15964. will represent the current date/time. If there is already a timestamp
  15965. at the cursor, it will be modified."
  15966. (interactive "P")
  15967. (let* ((ts nil)
  15968. (default-time
  15969. ;; Default time is either today, or, when entering a range,
  15970. ;; the range start.
  15971. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15972. (save-excursion
  15973. (re-search-backward
  15974. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15975. (- (point) 20) t)))
  15976. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15977. (current-time)))
  15978. (default-input (and ts (org-get-compact-tod ts)))
  15979. org-time-was-given org-end-time-was-given time)
  15980. (cond
  15981. ((and (org-at-timestamp-p)
  15982. (eq last-command 'org-time-stamp)
  15983. (eq this-command 'org-time-stamp))
  15984. (insert "--")
  15985. (setq time (let ((this-command this-command))
  15986. (org-read-date arg 'totime nil nil default-time default-input)))
  15987. (org-insert-time-stamp time (or org-time-was-given arg)))
  15988. ((org-at-timestamp-p)
  15989. (setq time (let ((this-command this-command))
  15990. (org-read-date arg 'totime nil nil default-time default-input)))
  15991. (when (org-at-timestamp-p) ; just to get the match data
  15992. (replace-match "")
  15993. (setq org-last-changed-timestamp
  15994. (org-insert-time-stamp
  15995. time (or org-time-was-given arg)
  15996. nil nil nil (list org-end-time-was-given))))
  15997. (message "Timestamp updated"))
  15998. (t
  15999. (setq time (let ((this-command this-command))
  16000. (org-read-date arg 'totime nil nil default-time default-input)))
  16001. (org-insert-time-stamp time (or org-time-was-given arg)
  16002. nil nil nil (list org-end-time-was-given))))))
  16003. ;; FIXME: can we use this for something else????
  16004. ;; like computing time differences?????
  16005. (defun org-get-compact-tod (s)
  16006. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16007. (let* ((t1 (match-string 1 s))
  16008. (h1 (string-to-number (match-string 2 s)))
  16009. (m1 (string-to-number (match-string 3 s)))
  16010. (t2 (and (match-end 4) (match-string 5 s)))
  16011. (h2 (and t2 (string-to-number (match-string 6 s))))
  16012. (m2 (and t2 (string-to-number (match-string 7 s))))
  16013. dh dm)
  16014. (if (not t2)
  16015. t1
  16016. (setq dh (- h2 h1) dm (- m2 m1))
  16017. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16018. (concat t1 "+" (number-to-string dh)
  16019. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16020. (defun org-time-stamp-inactive (&optional arg)
  16021. "Insert an inactive time stamp.
  16022. An inactive time stamp is enclosed in square brackets instead of angle
  16023. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16024. does not link to the calendar and cannot be changed with the S-cursor keys.
  16025. So these are more for recording a certain time/date."
  16026. (interactive "P")
  16027. (let (org-time-was-given org-end-time-was-given time)
  16028. (setq time (org-read-date arg 'totime))
  16029. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16030. nil nil (list org-end-time-was-given))))
  16031. (defvar org-date-ovl (org-make-overlay 1 1))
  16032. (org-overlay-put org-date-ovl 'face 'org-warning)
  16033. (org-detach-overlay org-date-ovl)
  16034. (defvar org-ans1) ; dynamically scoped parameter
  16035. (defvar org-ans2) ; dynamically scoped parameter
  16036. (defvar org-plain-time-of-day-regexp) ; defined below
  16037. (defvar org-read-date-overlay nil)
  16038. (defvar org-dcst nil) ; dynamically scoped
  16039. (defun org-read-date (&optional with-time to-time from-string prompt
  16040. default-time default-input)
  16041. "Read a date, possibly a time, and make things smooth for the user.
  16042. The prompt will suggest to enter an ISO date, but you can also enter anything
  16043. which will at least partially be understood by `parse-time-string'.
  16044. Unrecognized parts of the date will default to the current day, month, year,
  16045. hour and minute. If this command is called to replace a timestamp at point,
  16046. of to enter the second timestamp of a range, the default time is taken from the
  16047. existing stamp. For example,
  16048. 3-2-5 --> 2003-02-05
  16049. feb 15 --> currentyear-02-15
  16050. sep 12 9 --> 2009-09-12
  16051. 12:45 --> today 12:45
  16052. 22 sept 0:34 --> currentyear-09-22 0:34
  16053. 12 --> currentyear-currentmonth-12
  16054. Fri --> nearest Friday (today or later)
  16055. etc.
  16056. Furthermore you can specify a relative date by giving, as the *first* thing
  16057. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16058. change in days weeks, months, years.
  16059. With a single plus or minus, the date is relative to today. With a double
  16060. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16061. +4d --> four days from today
  16062. +4 --> same as above
  16063. +2w --> two weeks from today
  16064. ++5 --> five days from default date
  16065. The function understands only English month and weekday abbreviations,
  16066. but this can be configured with the variables `parse-time-months' and
  16067. `parse-time-weekdays'.
  16068. While prompting, a calendar is popped up - you can also select the
  16069. date with the mouse (button 1). The calendar shows a period of three
  16070. months. To scroll it to other months, use the keys `>' and `<'.
  16071. If you don't like the calendar, turn it off with
  16072. \(setq org-read-date-popup-calendar nil)
  16073. With optional argument TO-TIME, the date will immediately be converted
  16074. to an internal time.
  16075. With an optional argument WITH-TIME, the prompt will suggest to also
  16076. insert a time. Note that when WITH-TIME is not set, you can still
  16077. enter a time, and this function will inform the calling routine about
  16078. this change. The calling routine may then choose to change the format
  16079. used to insert the time stamp into the buffer to include the time.
  16080. With optional argument FROM-STRING, read from this string instead from
  16081. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16082. the time/date that is used for everything that is not specified by the
  16083. user."
  16084. (require 'parse-time)
  16085. (let* ((org-time-stamp-rounding-minutes
  16086. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  16087. (org-dcst org-display-custom-times)
  16088. (ct (org-current-time))
  16089. (def (or default-time ct))
  16090. (defdecode (decode-time def))
  16091. (dummy (progn
  16092. (when (< (nth 2 defdecode) org-extend-today-until)
  16093. (setcar (nthcdr 2 defdecode) -1)
  16094. (setcar (nthcdr 1 defdecode) 59)
  16095. (setq def (apply 'encode-time defdecode)
  16096. defdecode (decode-time def)))))
  16097. (calendar-move-hook nil)
  16098. (view-diary-entries-initially nil)
  16099. (view-calendar-holidays-initially nil)
  16100. (timestr (format-time-string
  16101. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16102. (prompt (concat (if prompt (concat prompt " ") "")
  16103. (format "Date+time [%s]: " timestr)))
  16104. ans (org-ans0 "") org-ans1 org-ans2 final)
  16105. (cond
  16106. (from-string (setq ans from-string))
  16107. (org-read-date-popup-calendar
  16108. (save-excursion
  16109. (save-window-excursion
  16110. (calendar)
  16111. (calendar-forward-day (- (time-to-days def)
  16112. (calendar-absolute-from-gregorian
  16113. (calendar-current-date))))
  16114. (org-eval-in-calendar nil t)
  16115. (let* ((old-map (current-local-map))
  16116. (map (copy-keymap calendar-mode-map))
  16117. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16118. (org-defkey map (kbd "RET") 'org-calendar-select)
  16119. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16120. 'org-calendar-select-mouse)
  16121. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16122. 'org-calendar-select-mouse)
  16123. (org-defkey minibuffer-local-map [(meta shift left)]
  16124. (lambda () (interactive)
  16125. (org-eval-in-calendar '(calendar-backward-month 1))))
  16126. (org-defkey minibuffer-local-map [(meta shift right)]
  16127. (lambda () (interactive)
  16128. (org-eval-in-calendar '(calendar-forward-month 1))))
  16129. (org-defkey minibuffer-local-map [(meta shift up)]
  16130. (lambda () (interactive)
  16131. (org-eval-in-calendar '(calendar-backward-year 1))))
  16132. (org-defkey minibuffer-local-map [(meta shift down)]
  16133. (lambda () (interactive)
  16134. (org-eval-in-calendar '(calendar-forward-year 1))))
  16135. (org-defkey minibuffer-local-map [(shift up)]
  16136. (lambda () (interactive)
  16137. (org-eval-in-calendar '(calendar-backward-week 1))))
  16138. (org-defkey minibuffer-local-map [(shift down)]
  16139. (lambda () (interactive)
  16140. (org-eval-in-calendar '(calendar-forward-week 1))))
  16141. (org-defkey minibuffer-local-map [(shift left)]
  16142. (lambda () (interactive)
  16143. (org-eval-in-calendar '(calendar-backward-day 1))))
  16144. (org-defkey minibuffer-local-map [(shift right)]
  16145. (lambda () (interactive)
  16146. (org-eval-in-calendar '(calendar-forward-day 1))))
  16147. (org-defkey minibuffer-local-map ">"
  16148. (lambda () (interactive)
  16149. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16150. (org-defkey minibuffer-local-map "<"
  16151. (lambda () (interactive)
  16152. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16153. (unwind-protect
  16154. (progn
  16155. (use-local-map map)
  16156. (add-hook 'post-command-hook 'org-read-date-display)
  16157. (setq org-ans0 (read-string prompt default-input nil nil))
  16158. ;; org-ans0: from prompt
  16159. ;; org-ans1: from mouse click
  16160. ;; org-ans2: from calendar motion
  16161. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16162. (remove-hook 'post-command-hook 'org-read-date-display)
  16163. (use-local-map old-map)
  16164. (when org-read-date-overlay
  16165. (org-delete-overlay org-read-date-overlay)
  16166. (setq org-read-date-overlay nil)))))))
  16167. (t ; Naked prompt only
  16168. (unwind-protect
  16169. (setq ans (read-string prompt default-input nil timestr))
  16170. (when org-read-date-overlay
  16171. (org-delete-overlay org-read-date-overlay)
  16172. (setq org-read-date-overlay nil)))))
  16173. (setq final (org-read-date-analyze ans def defdecode))
  16174. (if to-time
  16175. (apply 'encode-time final)
  16176. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16177. (format "%04d-%02d-%02d %02d:%02d"
  16178. (nth 5 final) (nth 4 final) (nth 3 final)
  16179. (nth 2 final) (nth 1 final))
  16180. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16181. (defvar def)
  16182. (defvar defdecode)
  16183. (defvar with-time)
  16184. (defun org-read-date-display ()
  16185. "Display the currrent date prompt interpretation in the minibuffer."
  16186. (when org-read-date-display-live
  16187. (when org-read-date-overlay
  16188. (org-delete-overlay org-read-date-overlay))
  16189. (let ((p (point)))
  16190. (end-of-line 1)
  16191. (while (not (equal (buffer-substring
  16192. (max (point-min) (- (point) 4)) (point))
  16193. " "))
  16194. (insert " "))
  16195. (goto-char p))
  16196. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16197. " " (or org-ans1 org-ans2)))
  16198. (org-end-time-was-given nil)
  16199. (f (org-read-date-analyze ans def defdecode))
  16200. (fmts (if org-dcst
  16201. org-time-stamp-custom-formats
  16202. org-time-stamp-formats))
  16203. (fmt (if (or with-time
  16204. (and (boundp 'org-time-was-given) org-time-was-given))
  16205. (cdr fmts)
  16206. (car fmts)))
  16207. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16208. (when (and org-end-time-was-given
  16209. (string-match org-plain-time-of-day-regexp txt))
  16210. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16211. org-end-time-was-given
  16212. (substring txt (match-end 0)))))
  16213. (setq org-read-date-overlay
  16214. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16215. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16216. (defun org-read-date-analyze (ans def defdecode)
  16217. "Analyze the combined answer of the date prompt."
  16218. ;; FIXME: cleanup and comment
  16219. (let (delta deltan deltaw deltadef year month day
  16220. hour minute second wday pm h2 m2 tl wday1
  16221. iso-year iso-weekday iso-week iso-year iso-date)
  16222. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16223. (setq ans (replace-match "" t t ans)
  16224. deltan (car delta)
  16225. deltaw (nth 1 delta)
  16226. deltadef (nth 2 delta)))
  16227. ;; Check if there is an iso week date in there
  16228. ;; If yes, sore the info and ostpone interpreting it until the rest
  16229. ;; of the parsing is done
  16230. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  16231. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  16232. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  16233. iso-week (string-to-number (match-string 2 ans)))
  16234. (setq ans (replace-match "" t t ans)))
  16235. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16236. (when (string-match
  16237. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16238. (setq year (if (match-end 2)
  16239. (string-to-number (match-string 2 ans))
  16240. (string-to-number (format-time-string "%Y")))
  16241. month (string-to-number (match-string 3 ans))
  16242. day (string-to-number (match-string 4 ans)))
  16243. (if (< year 100) (setq year (+ 2000 year)))
  16244. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16245. t nil ans)))
  16246. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16247. ;; If there is a time with am/pm, and *no* time without it, we convert
  16248. ;; so that matching will be successful.
  16249. (loop for i from 1 to 2 do ; twice, for end time as well
  16250. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16251. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16252. (setq hour (string-to-number (match-string 1 ans))
  16253. minute (if (match-end 3)
  16254. (string-to-number (match-string 3 ans))
  16255. 0)
  16256. pm (equal ?p
  16257. (string-to-char (downcase (match-string 4 ans)))))
  16258. (if (and (= hour 12) (not pm))
  16259. (setq hour 0)
  16260. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16261. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16262. t t ans))))
  16263. ;; Check if a time range is given as a duration
  16264. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16265. (setq hour (string-to-number (match-string 1 ans))
  16266. h2 (+ hour (string-to-number (match-string 3 ans)))
  16267. minute (string-to-number (match-string 2 ans))
  16268. m2 (+ minute (if (match-end 5) (string-to-number
  16269. (match-string 5 ans))0)))
  16270. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16271. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  16272. t t ans)))
  16273. ;; Check if there is a time range
  16274. (when (boundp 'org-end-time-was-given)
  16275. (setq org-time-was-given nil)
  16276. (when (and (string-match org-plain-time-of-day-regexp ans)
  16277. (match-end 8))
  16278. (setq org-end-time-was-given (match-string 8 ans))
  16279. (setq ans (concat (substring ans 0 (match-beginning 7))
  16280. (substring ans (match-end 7))))))
  16281. (setq tl (parse-time-string ans)
  16282. day (or (nth 3 tl) (nth 3 defdecode))
  16283. month (or (nth 4 tl)
  16284. (if (and org-read-date-prefer-future
  16285. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16286. (1+ (nth 4 defdecode))
  16287. (nth 4 defdecode)))
  16288. year (or (nth 5 tl)
  16289. (if (and org-read-date-prefer-future
  16290. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16291. (1+ (nth 5 defdecode))
  16292. (nth 5 defdecode)))
  16293. hour (or (nth 2 tl) (nth 2 defdecode))
  16294. minute (or (nth 1 tl) (nth 1 defdecode))
  16295. second (or (nth 0 tl) 0)
  16296. wday (nth 6 tl))
  16297. ;; Special date definitions below
  16298. (cond
  16299. (iso-week
  16300. ;; There was an iso week
  16301. (setq year (or iso-year year)
  16302. day (or iso-weekday wday 1)
  16303. wday nil ; to make sure that the trigger below does not match
  16304. iso-date (calendar-gregorian-from-absolute
  16305. (calendar-absolute-from-iso
  16306. (list iso-week day year))))
  16307. ; FIXME: Should we also push ISO weeks into the future?
  16308. ; (when (and org-read-date-prefer-future
  16309. ; (not iso-year)
  16310. ; (< (calendar-absolute-from-gregorian iso-date)
  16311. ; (time-to-days (current-time))))
  16312. ; (setq year (1+ year)
  16313. ; iso-date (calendar-gregorian-from-absolute
  16314. ; (calendar-absolute-from-iso
  16315. ; (list iso-week day year)))))
  16316. (setq month (car iso-date)
  16317. year (nth 2 iso-date)
  16318. day (nth 1 iso-date)))
  16319. (deltan
  16320. (unless deltadef
  16321. (let ((now (decode-time (current-time))))
  16322. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16323. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16324. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16325. ((equal deltaw "m") (setq month (+ month deltan)))
  16326. ((equal deltaw "y") (setq year (+ year deltan)))))
  16327. ((and wday (not (nth 3 tl)))
  16328. ;; Weekday was given, but no day, so pick that day in the week
  16329. ;; on or after the derived date.
  16330. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16331. (unless (equal wday wday1)
  16332. (setq day (+ day (% (- wday wday1 -7) 7))))))
  16333. (if (and (boundp 'org-time-was-given)
  16334. (nth 2 tl))
  16335. (setq org-time-was-given t))
  16336. (if (< year 100) (setq year (+ 2000 year)))
  16337. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16338. (list second minute hour day month year)))
  16339. (defvar parse-time-weekdays)
  16340. (defun org-read-date-get-relative (s today default)
  16341. "Check string S for special relative date string.
  16342. TODAY and DEFAULT are internal times, for today and for a default.
  16343. Return shift list (N what def-flag)
  16344. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16345. N is the number of WHATs to shift.
  16346. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16347. the DEFAULT date rather than TODAY."
  16348. (when (string-match
  16349. (concat
  16350. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16351. "\\([0-9]+\\)?"
  16352. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16353. "\\([ \t]\\|$\\)") s)
  16354. (let* ((dir (if (match-end 1)
  16355. (string-to-char (substring (match-string 1 s) -1))
  16356. ?+))
  16357. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16358. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16359. (what (if (match-end 3) (match-string 3 s) "d"))
  16360. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16361. (date (if rel default today))
  16362. (wday (nth 6 (decode-time date)))
  16363. delta)
  16364. (if wday1
  16365. (progn
  16366. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16367. (if (= dir ?-) (setq delta (- delta 7)))
  16368. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16369. (list delta "d" rel))
  16370. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16371. (defun org-eval-in-calendar (form &optional keepdate)
  16372. "Eval FORM in the calendar window and return to current window.
  16373. Also, store the cursor date in variable org-ans2."
  16374. (let ((sw (selected-window)))
  16375. (select-window (get-buffer-window "*Calendar*"))
  16376. (eval form)
  16377. (when (and (not keepdate) (calendar-cursor-to-date))
  16378. (let* ((date (calendar-cursor-to-date))
  16379. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16380. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16381. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16382. (select-window sw)))
  16383. ; ;; Update the prompt to show new default date
  16384. ; (save-excursion
  16385. ; (goto-char (point-min))
  16386. ; (when (and org-ans2
  16387. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16388. ; (get-text-property (match-end 0) 'field))
  16389. ; (let ((inhibit-read-only t))
  16390. ; (replace-match (concat "[" org-ans2 "]") t t)
  16391. ; (add-text-properties (point-min) (1+ (match-end 0))
  16392. ; (text-properties-at (1+ (point-min)))))))))
  16393. (defun org-calendar-select ()
  16394. "Return to `org-read-date' with the date currently selected.
  16395. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16396. (interactive)
  16397. (when (calendar-cursor-to-date)
  16398. (let* ((date (calendar-cursor-to-date))
  16399. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16400. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16401. (if (active-minibuffer-window) (exit-minibuffer))))
  16402. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16403. "Insert a date stamp for the date given by the internal TIME.
  16404. WITH-HM means, use the stamp format that includes the time of the day.
  16405. INACTIVE means use square brackets instead of angular ones, so that the
  16406. stamp will not contribute to the agenda.
  16407. PRE and POST are optional strings to be inserted before and after the
  16408. stamp.
  16409. The command returns the inserted time stamp."
  16410. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16411. stamp)
  16412. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16413. (insert-before-markers (or pre ""))
  16414. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16415. (when (listp extra)
  16416. (setq extra (car extra))
  16417. (if (and (stringp extra)
  16418. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16419. (setq extra (format "-%02d:%02d"
  16420. (string-to-number (match-string 1 extra))
  16421. (string-to-number (match-string 2 extra))))
  16422. (setq extra nil)))
  16423. (when extra
  16424. (backward-char 1)
  16425. (insert-before-markers extra)
  16426. (forward-char 1))
  16427. (insert-before-markers (or post ""))
  16428. stamp))
  16429. (defun org-toggle-time-stamp-overlays ()
  16430. "Toggle the use of custom time stamp formats."
  16431. (interactive)
  16432. (setq org-display-custom-times (not org-display-custom-times))
  16433. (unless org-display-custom-times
  16434. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16435. (while (setq p (next-single-property-change p 'display))
  16436. (if (and (get-text-property p 'display)
  16437. (eq (get-text-property p 'face) 'org-date))
  16438. (remove-text-properties
  16439. p (setq p (next-single-property-change p 'display))
  16440. '(display t))))
  16441. (set-buffer-modified-p bmp)))
  16442. (if (featurep 'xemacs)
  16443. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16444. (org-restart-font-lock)
  16445. (setq org-table-may-need-update t)
  16446. (if org-display-custom-times
  16447. (message "Time stamps are overlayed with custom format")
  16448. (message "Time stamp overlays removed")))
  16449. (defun org-display-custom-time (beg end)
  16450. "Overlay modified time stamp format over timestamp between BED and END."
  16451. (let* ((ts (buffer-substring beg end))
  16452. t1 w1 with-hm tf time str w2 (off 0))
  16453. (save-match-data
  16454. (setq t1 (org-parse-time-string ts t))
  16455. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  16456. (setq off (- (match-end 0) (match-beginning 0)))))
  16457. (setq end (- end off))
  16458. (setq w1 (- end beg)
  16459. with-hm (and (nth 1 t1) (nth 2 t1))
  16460. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16461. time (org-fix-decoded-time t1)
  16462. str (org-add-props
  16463. (format-time-string
  16464. (substring tf 1 -1) (apply 'encode-time time))
  16465. nil 'mouse-face 'highlight)
  16466. w2 (length str))
  16467. (if (not (= w2 w1))
  16468. (add-text-properties (1+ beg) (+ 2 beg)
  16469. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16470. (if (featurep 'xemacs)
  16471. (progn
  16472. (put-text-property beg end 'invisible t)
  16473. (put-text-property beg end 'end-glyph (make-glyph str)))
  16474. (put-text-property beg end 'display str))))
  16475. (defun org-translate-time (string)
  16476. "Translate all timestamps in STRING to custom format.
  16477. But do this only if the variable `org-display-custom-times' is set."
  16478. (when org-display-custom-times
  16479. (save-match-data
  16480. (let* ((start 0)
  16481. (re org-ts-regexp-both)
  16482. t1 with-hm inactive tf time str beg end)
  16483. (while (setq start (string-match re string start))
  16484. (setq beg (match-beginning 0)
  16485. end (match-end 0)
  16486. t1 (save-match-data
  16487. (org-parse-time-string (substring string beg end) t))
  16488. with-hm (and (nth 1 t1) (nth 2 t1))
  16489. inactive (equal (substring string beg (1+ beg)) "[")
  16490. tf (funcall (if with-hm 'cdr 'car)
  16491. org-time-stamp-custom-formats)
  16492. time (org-fix-decoded-time t1)
  16493. str (format-time-string
  16494. (concat
  16495. (if inactive "[" "<") (substring tf 1 -1)
  16496. (if inactive "]" ">"))
  16497. (apply 'encode-time time))
  16498. string (replace-match str t t string)
  16499. start (+ start (length str)))))))
  16500. string)
  16501. (defun org-fix-decoded-time (time)
  16502. "Set 0 instead of nil for the first 6 elements of time.
  16503. Don't touch the rest."
  16504. (let ((n 0))
  16505. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16506. (defun org-days-to-time (timestamp-string)
  16507. "Difference between TIMESTAMP-STRING and now in days."
  16508. (- (time-to-days (org-time-string-to-time timestamp-string))
  16509. (time-to-days (current-time))))
  16510. (defun org-deadline-close (timestamp-string &optional ndays)
  16511. "Is the time in TIMESTAMP-STRING close to the current date?"
  16512. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16513. (and (< (org-days-to-time timestamp-string) ndays)
  16514. (not (org-entry-is-done-p))))
  16515. (defun org-get-wdays (ts)
  16516. "Get the deadline lead time appropriate for timestring TS."
  16517. (cond
  16518. ((<= org-deadline-warning-days 0)
  16519. ;; 0 or negative, enforce this value no matter what
  16520. (- org-deadline-warning-days))
  16521. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16522. ;; lead time is specified.
  16523. (floor (* (string-to-number (match-string 1 ts))
  16524. (cdr (assoc (match-string 2 ts)
  16525. '(("d" . 1) ("w" . 7)
  16526. ("m" . 30.4) ("y" . 365.25)))))))
  16527. ;; go for the default.
  16528. (t org-deadline-warning-days)))
  16529. (defun org-calendar-select-mouse (ev)
  16530. "Return to `org-read-date' with the date currently selected.
  16531. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16532. (interactive "e")
  16533. (mouse-set-point ev)
  16534. (when (calendar-cursor-to-date)
  16535. (let* ((date (calendar-cursor-to-date))
  16536. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16537. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16538. (if (active-minibuffer-window) (exit-minibuffer))))
  16539. (defun org-check-deadlines (ndays)
  16540. "Check if there are any deadlines due or past due.
  16541. A deadline is considered due if it happens within `org-deadline-warning-days'
  16542. days from today's date. If the deadline appears in an entry marked DONE,
  16543. it is not shown. The prefix arg NDAYS can be used to test that many
  16544. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16545. (interactive "P")
  16546. (let* ((org-warn-days
  16547. (cond
  16548. ((equal ndays '(4)) 100000)
  16549. (ndays (prefix-numeric-value ndays))
  16550. (t (abs org-deadline-warning-days))))
  16551. (case-fold-search nil)
  16552. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16553. (callback
  16554. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16555. (message "%d deadlines past-due or due within %d days"
  16556. (org-occur regexp nil callback)
  16557. org-warn-days)))
  16558. (defun org-check-before-date (date)
  16559. "Check if there are deadlines or scheduled entries before DATE."
  16560. (interactive (list (org-read-date)))
  16561. (let ((case-fold-search nil)
  16562. (regexp (concat "\\<\\(" org-deadline-string
  16563. "\\|" org-scheduled-string
  16564. "\\) *<\\([^>]+\\)>"))
  16565. (callback
  16566. (lambda () (time-less-p
  16567. (org-time-string-to-time (match-string 2))
  16568. (org-time-string-to-time date)))))
  16569. (message "%d entries before %s"
  16570. (org-occur regexp nil callback) date)))
  16571. (defun org-evaluate-time-range (&optional to-buffer)
  16572. "Evaluate a time range by computing the difference between start and end.
  16573. Normally the result is just printed in the echo area, but with prefix arg
  16574. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16575. If the time range is actually in a table, the result is inserted into the
  16576. next column.
  16577. For time difference computation, a year is assumed to be exactly 365
  16578. days in order to avoid rounding problems."
  16579. (interactive "P")
  16580. (or
  16581. (org-clock-update-time-maybe)
  16582. (save-excursion
  16583. (unless (org-at-date-range-p t)
  16584. (goto-char (point-at-bol))
  16585. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16586. (if (not (org-at-date-range-p t))
  16587. (error "Not at a time-stamp range, and none found in current line")))
  16588. (let* ((ts1 (match-string 1))
  16589. (ts2 (match-string 2))
  16590. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16591. (match-end (match-end 0))
  16592. (time1 (org-time-string-to-time ts1))
  16593. (time2 (org-time-string-to-time ts2))
  16594. (t1 (time-to-seconds time1))
  16595. (t2 (time-to-seconds time2))
  16596. (diff (abs (- t2 t1)))
  16597. (negative (< (- t2 t1) 0))
  16598. ;; (ys (floor (* 365 24 60 60)))
  16599. (ds (* 24 60 60))
  16600. (hs (* 60 60))
  16601. (fy "%dy %dd %02d:%02d")
  16602. (fy1 "%dy %dd")
  16603. (fd "%dd %02d:%02d")
  16604. (fd1 "%dd")
  16605. (fh "%02d:%02d")
  16606. y d h m align)
  16607. (if havetime
  16608. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16609. y 0
  16610. d (floor (/ diff ds)) diff (mod diff ds)
  16611. h (floor (/ diff hs)) diff (mod diff hs)
  16612. m (floor (/ diff 60)))
  16613. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16614. y 0
  16615. d (floor (+ (/ diff ds) 0.5))
  16616. h 0 m 0))
  16617. (if (not to-buffer)
  16618. (message "%s" (org-make-tdiff-string y d h m))
  16619. (if (org-at-table-p)
  16620. (progn
  16621. (goto-char match-end)
  16622. (setq align t)
  16623. (and (looking-at " *|") (goto-char (match-end 0))))
  16624. (goto-char match-end))
  16625. (if (looking-at
  16626. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16627. (replace-match ""))
  16628. (if negative (insert " -"))
  16629. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16630. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16631. (insert " " (format fh h m))))
  16632. (if align (org-table-align))
  16633. (message "Time difference inserted")))))
  16634. (defun org-make-tdiff-string (y d h m)
  16635. (let ((fmt "")
  16636. (l nil))
  16637. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16638. l (push y l)))
  16639. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16640. l (push d l)))
  16641. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16642. l (push h l)))
  16643. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16644. l (push m l)))
  16645. (apply 'format fmt (nreverse l))))
  16646. (defun org-time-string-to-time (s)
  16647. (apply 'encode-time (org-parse-time-string s)))
  16648. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16649. "Convert a time stamp to an absolute day number.
  16650. If there is a specifyer for a cyclic time stamp, get the closest date to
  16651. DAYNR."
  16652. (cond
  16653. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16654. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16655. daynr
  16656. (+ daynr 1000)))
  16657. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16658. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16659. (time-to-days (current-time))) (match-string 0 s)
  16660. prefer))
  16661. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16662. (defun org-time-from-absolute (d)
  16663. "Return the time corresponding to date D.
  16664. D may be an absolute day number, or a calendar-type list (month day year)."
  16665. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16666. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16667. (defun org-calendar-holiday ()
  16668. "List of holidays, for Diary display in Org-mode."
  16669. (require 'holidays)
  16670. (let ((hl (funcall
  16671. (if (fboundp 'calendar-check-holidays)
  16672. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16673. (if hl (mapconcat 'identity hl "; "))))
  16674. (defun org-diary-sexp-entry (sexp entry date)
  16675. "Process a SEXP diary ENTRY for DATE."
  16676. (require 'diary-lib)
  16677. (let ((result (if calendar-debug-sexp
  16678. (let ((stack-trace-on-error t))
  16679. (eval (car (read-from-string sexp))))
  16680. (condition-case nil
  16681. (eval (car (read-from-string sexp)))
  16682. (error
  16683. (beep)
  16684. (message "Bad sexp at line %d in %s: %s"
  16685. (org-current-line)
  16686. (buffer-file-name) sexp)
  16687. (sleep-for 2))))))
  16688. (cond ((stringp result) result)
  16689. ((and (consp result)
  16690. (stringp (cdr result))) (cdr result))
  16691. (result entry)
  16692. (t nil))))
  16693. (defun org-diary-to-ical-string (frombuf)
  16694. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16695. This uses the icalendar.el library."
  16696. (let* ((tmpdir (if (featurep 'xemacs)
  16697. (temp-directory)
  16698. temporary-file-directory))
  16699. (tmpfile (make-temp-name
  16700. (expand-file-name "orgics" tmpdir)))
  16701. buf rtn b e)
  16702. (save-excursion
  16703. (set-buffer frombuf)
  16704. (icalendar-export-region (point-min) (point-max) tmpfile)
  16705. (setq buf (find-buffer-visiting tmpfile))
  16706. (set-buffer buf)
  16707. (goto-char (point-min))
  16708. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16709. (setq b (match-beginning 0)))
  16710. (goto-char (point-max))
  16711. (if (re-search-backward "^END:VEVENT" nil t)
  16712. (setq e (match-end 0)))
  16713. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16714. (kill-buffer buf)
  16715. (kill-buffer frombuf)
  16716. (delete-file tmpfile)
  16717. rtn))
  16718. (defun org-closest-date (start current change prefer)
  16719. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16720. When PREFER is `past' return a date that is either CURRENT or past.
  16721. When PREFER is `future', return a date that is either CURRENT or future."
  16722. ;; Make the proper lists from the dates
  16723. (catch 'exit
  16724. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16725. dn dw sday cday n1 n2
  16726. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16727. (setq start (org-date-to-gregorian start)
  16728. current (org-date-to-gregorian
  16729. (if org-agenda-repeating-timestamp-show-all
  16730. current
  16731. (time-to-days (current-time))))
  16732. sday (calendar-absolute-from-gregorian start)
  16733. cday (calendar-absolute-from-gregorian current))
  16734. (if (<= cday sday) (throw 'exit sday))
  16735. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16736. (setq dn (string-to-number (match-string 1 change))
  16737. dw (cdr (assoc (match-string 2 change) a1)))
  16738. (error "Invalid change specifyer: %s" change))
  16739. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16740. (cond
  16741. ((eq dw 'day)
  16742. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16743. n2 (+ n1 dn)))
  16744. ((eq dw 'year)
  16745. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16746. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16747. (setq date1 (list m d y1)
  16748. n1 (calendar-absolute-from-gregorian date1)
  16749. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16750. n2 (calendar-absolute-from-gregorian date2)))
  16751. ((eq dw 'month)
  16752. ;; approx number of month between the tow dates
  16753. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16754. ;; How often does dn fit in there?
  16755. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16756. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16757. m (+ m nm)
  16758. ny (floor (/ m 12))
  16759. y (+ y ny)
  16760. m (- m (* ny 12)))
  16761. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16762. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16763. (setq m2 (+ m dn) y2 y)
  16764. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16765. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16766. (while (< n2 cday)
  16767. (setq n1 n2 m m2 y y2)
  16768. (setq m2 (+ m dn) y2 y)
  16769. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16770. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16771. (if org-agenda-repeating-timestamp-show-all
  16772. (cond
  16773. ((eq prefer 'past) n1)
  16774. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16775. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16776. (cond
  16777. ((eq prefer 'past) n1)
  16778. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16779. (t (if (= cday n1) n1 n2)))))))
  16780. (defun org-date-to-gregorian (date)
  16781. "Turn any specification of DATE into a gregorian date for the calendar."
  16782. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16783. ((and (listp date) (= (length date) 3)) date)
  16784. ((stringp date)
  16785. (setq date (org-parse-time-string date))
  16786. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16787. ((listp date)
  16788. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16789. (defun org-parse-time-string (s &optional nodefault)
  16790. "Parse the standard Org-mode time string.
  16791. This should be a lot faster than the normal `parse-time-string'.
  16792. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16793. hour and minute fields will be nil if not given."
  16794. (if (string-match org-ts-regexp0 s)
  16795. (list 0
  16796. (if (or (match-beginning 8) (not nodefault))
  16797. (string-to-number (or (match-string 8 s) "0")))
  16798. (if (or (match-beginning 7) (not nodefault))
  16799. (string-to-number (or (match-string 7 s) "0")))
  16800. (string-to-number (match-string 4 s))
  16801. (string-to-number (match-string 3 s))
  16802. (string-to-number (match-string 2 s))
  16803. nil nil nil)
  16804. (make-list 9 0)))
  16805. (defun org-timestamp-up (&optional arg)
  16806. "Increase the date item at the cursor by one.
  16807. If the cursor is on the year, change the year. If it is on the month or
  16808. the day, change that.
  16809. With prefix ARG, change by that many units."
  16810. (interactive "p")
  16811. (org-timestamp-change (prefix-numeric-value arg)))
  16812. (defun org-timestamp-down (&optional arg)
  16813. "Decrease the date item at the cursor by one.
  16814. If the cursor is on the year, change the year. If it is on the month or
  16815. the day, change that.
  16816. With prefix ARG, change by that many units."
  16817. (interactive "p")
  16818. (org-timestamp-change (- (prefix-numeric-value arg))))
  16819. (defun org-timestamp-up-day (&optional arg)
  16820. "Increase the date in the time stamp by one day.
  16821. With prefix ARG, change that many days."
  16822. (interactive "p")
  16823. (if (and (not (org-at-timestamp-p t))
  16824. (org-on-heading-p))
  16825. (org-todo 'up)
  16826. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16827. (defun org-timestamp-down-day (&optional arg)
  16828. "Decrease the date in the time stamp by one day.
  16829. With prefix ARG, change that many days."
  16830. (interactive "p")
  16831. (if (and (not (org-at-timestamp-p t))
  16832. (org-on-heading-p))
  16833. (org-todo 'down)
  16834. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16835. (defsubst org-pos-in-match-range (pos n)
  16836. (and (match-beginning n)
  16837. (<= (match-beginning n) pos)
  16838. (>= (match-end n) pos)))
  16839. (defun org-at-timestamp-p (&optional inactive-ok)
  16840. "Determine if the cursor is in or at a timestamp."
  16841. (interactive)
  16842. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16843. (pos (point))
  16844. (ans (or (looking-at tsr)
  16845. (save-excursion
  16846. (skip-chars-backward "^[<\n\r\t")
  16847. (if (> (point) (point-min)) (backward-char 1))
  16848. (and (looking-at tsr)
  16849. (> (- (match-end 0) pos) -1))))))
  16850. (and ans
  16851. (boundp 'org-ts-what)
  16852. (setq org-ts-what
  16853. (cond
  16854. ((= pos (match-beginning 0)) 'bracket)
  16855. ((= pos (1- (match-end 0))) 'bracket)
  16856. ((org-pos-in-match-range pos 2) 'year)
  16857. ((org-pos-in-match-range pos 3) 'month)
  16858. ((org-pos-in-match-range pos 7) 'hour)
  16859. ((org-pos-in-match-range pos 8) 'minute)
  16860. ((or (org-pos-in-match-range pos 4)
  16861. (org-pos-in-match-range pos 5)) 'day)
  16862. ((and (> pos (or (match-end 8) (match-end 5)))
  16863. (< pos (match-end 0)))
  16864. (- pos (or (match-end 8) (match-end 5))))
  16865. (t 'day))))
  16866. ans))
  16867. (defun org-toggle-timestamp-type ()
  16868. "Toggle the type (<active> or [inactive]) of a time stamp."
  16869. (interactive)
  16870. (when (org-at-timestamp-p t)
  16871. (save-excursion
  16872. (goto-char (match-beginning 0))
  16873. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16874. (goto-char (1- (match-end 0)))
  16875. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16876. (message "Timestamp is now %sactive"
  16877. (if (equal (char-before) ?>) "in" ""))))
  16878. (defun org-timestamp-change (n &optional what)
  16879. "Change the date in the time stamp at point.
  16880. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16881. `year', `minute', `second'. If WHAT is not given, the cursor position
  16882. in the timestamp determines what will be changed."
  16883. (let ((pos (point))
  16884. with-hm inactive
  16885. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16886. org-ts-what
  16887. extra rem
  16888. ts time time0)
  16889. (if (not (org-at-timestamp-p t))
  16890. (error "Not at a timestamp"))
  16891. (if (and (not what) (eq org-ts-what 'bracket))
  16892. (org-toggle-timestamp-type)
  16893. (if (and (not what) (not (eq org-ts-what 'day))
  16894. org-display-custom-times
  16895. (get-text-property (point) 'display)
  16896. (not (get-text-property (1- (point)) 'display)))
  16897. (setq org-ts-what 'day))
  16898. (setq org-ts-what (or what org-ts-what)
  16899. inactive (= (char-after (match-beginning 0)) ?\[)
  16900. ts (match-string 0))
  16901. (replace-match "")
  16902. (if (string-match
  16903. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  16904. ts)
  16905. (setq extra (match-string 1 ts)))
  16906. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16907. (setq with-hm t))
  16908. (setq time0 (org-parse-time-string ts))
  16909. (when (and (eq org-ts-what 'minute)
  16910. (eq current-prefix-arg nil))
  16911. (setq n (* dm (org-no-warnings (signum n))))
  16912. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16913. (setcar (cdr time0) (+ (nth 1 time0)
  16914. (if (> n 0) (- rem) (- dm rem))))))
  16915. (setq time
  16916. (encode-time (or (car time0) 0)
  16917. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16918. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16919. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16920. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16921. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16922. (nthcdr 6 time0)))
  16923. (when (integerp org-ts-what)
  16924. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16925. (if (eq what 'calendar)
  16926. (let ((cal-date (org-get-date-from-calendar)))
  16927. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16928. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16929. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16930. (setcar time0 (or (car time0) 0))
  16931. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16932. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16933. (setq time (apply 'encode-time time0))))
  16934. (setq org-last-changed-timestamp
  16935. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16936. (org-clock-update-time-maybe)
  16937. (goto-char pos)
  16938. ;; Try to recenter the calendar window, if any
  16939. (if (and org-calendar-follow-timestamp-change
  16940. (get-buffer-window "*Calendar*" t)
  16941. (memq org-ts-what '(day month year)))
  16942. (org-recenter-calendar (time-to-days time))))))
  16943. ;; FIXME: does not yet work for lead times
  16944. (defun org-modify-ts-extra (s pos n dm)
  16945. "Change the different parts of the lead-time and repeat fields in timestamp."
  16946. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16947. ng h m new rem)
  16948. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16949. (cond
  16950. ((or (org-pos-in-match-range pos 2)
  16951. (org-pos-in-match-range pos 3))
  16952. (setq m (string-to-number (match-string 3 s))
  16953. h (string-to-number (match-string 2 s)))
  16954. (if (org-pos-in-match-range pos 2)
  16955. (setq h (+ h n))
  16956. (setq n (* dm (org-no-warnings (signum n))))
  16957. (when (not (= 0 (setq rem (% m dm))))
  16958. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16959. (setq m (+ m n)))
  16960. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16961. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16962. (setq h (min 24 (max 0 h)))
  16963. (setq ng 1 new (format "-%02d:%02d" h m)))
  16964. ((org-pos-in-match-range pos 6)
  16965. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16966. ((org-pos-in-match-range pos 5)
  16967. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16968. ((org-pos-in-match-range pos 9)
  16969. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16970. ((org-pos-in-match-range pos 8)
  16971. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16972. (when ng
  16973. (setq s (concat
  16974. (substring s 0 (match-beginning ng))
  16975. new
  16976. (substring s (match-end ng))))))
  16977. s))
  16978. (defun org-recenter-calendar (date)
  16979. "If the calendar is visible, recenter it to DATE."
  16980. (let* ((win (selected-window))
  16981. (cwin (get-buffer-window "*Calendar*" t))
  16982. (calendar-move-hook nil))
  16983. (when cwin
  16984. (select-window cwin)
  16985. (calendar-goto-date (if (listp date) date
  16986. (calendar-gregorian-from-absolute date)))
  16987. (select-window win))))
  16988. (defun org-goto-calendar (&optional arg)
  16989. "Go to the Emacs calendar at the current date.
  16990. If there is a time stamp in the current line, go to that date.
  16991. A prefix ARG can be used to force the current date."
  16992. (interactive "P")
  16993. (let ((tsr org-ts-regexp) diff
  16994. (calendar-move-hook nil)
  16995. (view-calendar-holidays-initially nil)
  16996. (view-diary-entries-initially nil))
  16997. (if (or (org-at-timestamp-p)
  16998. (save-excursion
  16999. (beginning-of-line 1)
  17000. (looking-at (concat ".*" tsr))))
  17001. (let ((d1 (time-to-days (current-time)))
  17002. (d2 (time-to-days
  17003. (org-time-string-to-time (match-string 1)))))
  17004. (setq diff (- d2 d1))))
  17005. (calendar)
  17006. (calendar-goto-today)
  17007. (if (and diff (not arg)) (calendar-forward-day diff))))
  17008. (defun org-get-date-from-calendar ()
  17009. "Return a list (month day year) of date at point in calendar."
  17010. (with-current-buffer "*Calendar*"
  17011. (save-match-data
  17012. (calendar-cursor-to-date))))
  17013. (defun org-date-from-calendar ()
  17014. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17015. If there is already a time stamp at the cursor position, update it."
  17016. (interactive)
  17017. (if (org-at-timestamp-p t)
  17018. (org-timestamp-change 0 'calendar)
  17019. (let ((cal-date (org-get-date-from-calendar)))
  17020. (org-insert-time-stamp
  17021. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17022. (defvar appt-time-msg-list)
  17023. ;;;###autoload
  17024. (defun org-agenda-to-appt (&optional refresh filter)
  17025. "Activate appointments found in `org-agenda-files'.
  17026. With a \\[universal-argument] prefix, refresh the list of
  17027. appointements.
  17028. If FILTER is t, interactively prompt the user for a regular
  17029. expression, and filter out entries that don't match it.
  17030. If FILTER is a string, use this string as a regular expression
  17031. for filtering entries out.
  17032. FILTER can also be an alist with the car of each cell being
  17033. either 'headline or 'category. For example:
  17034. '((headline \"IMPORTANT\")
  17035. (category \"Work\"))
  17036. will only add headlines containing IMPORTANT or headlines
  17037. belonging to the \"Work\" category."
  17038. (interactive "P")
  17039. (require 'calendar)
  17040. (if refresh (setq appt-time-msg-list nil))
  17041. (if (eq filter t)
  17042. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17043. (let* ((cnt 0) ; count added events
  17044. (org-agenda-new-buffers nil)
  17045. (org-deadline-warning-days 0)
  17046. (today (org-date-to-gregorian
  17047. (time-to-days (current-time))))
  17048. (files (org-agenda-files)) entries file)
  17049. ;; Get all entries which may contain an appt
  17050. (while (setq file (pop files))
  17051. (setq entries
  17052. (append entries
  17053. (org-agenda-get-day-entries
  17054. file today :timestamp :scheduled :deadline))))
  17055. (setq entries (delq nil entries))
  17056. ;; Map thru entries and find if we should filter them out
  17057. (mapc
  17058. (lambda(x)
  17059. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17060. (cat (get-text-property 1 'org-category x))
  17061. (tod (get-text-property 1 'time-of-day x))
  17062. (ok (or (null filter)
  17063. (and (stringp filter) (string-match filter evt))
  17064. (and (listp filter)
  17065. (or (string-match
  17066. (cadr (assoc 'category filter)) cat)
  17067. (string-match
  17068. (cadr (assoc 'headline filter)) evt))))))
  17069. ;; FIXME: Shall we remove text-properties for the appt text?
  17070. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17071. (when (and ok tod)
  17072. (setq tod (number-to-string tod)
  17073. tod (when (string-match
  17074. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17075. (concat (match-string 1 tod) ":"
  17076. (match-string 2 tod))))
  17077. (appt-add tod evt)
  17078. (setq cnt (1+ cnt))))) entries)
  17079. (org-release-buffers org-agenda-new-buffers)
  17080. (if (eq cnt 0)
  17081. (message "No event to add")
  17082. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17083. ;;; The clock for measuring work time.
  17084. (defvar org-mode-line-string "")
  17085. (put 'org-mode-line-string 'risky-local-variable t)
  17086. (defvar org-mode-line-timer nil)
  17087. (defvar org-clock-heading "")
  17088. (defvar org-clock-start-time "")
  17089. (defun org-update-mode-line ()
  17090. (let* ((delta (- (time-to-seconds (current-time))
  17091. (time-to-seconds org-clock-start-time)))
  17092. (h (floor delta 3600))
  17093. (m (floor (- delta (* 3600 h)) 60)))
  17094. (setq org-mode-line-string
  17095. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17096. 'help-echo "Org-mode clock is running"))
  17097. (force-mode-line-update)))
  17098. (defvar org-clock-marker (make-marker)
  17099. "Marker recording the last clock-in.")
  17100. (defvar org-clock-mode-line-entry nil
  17101. "Information for the modeline about the running clock.")
  17102. (defun org-clock-in ()
  17103. "Start the clock on the current item.
  17104. If necessary, clock-out of the currently active clock."
  17105. (interactive)
  17106. (org-clock-out t)
  17107. (let (ts)
  17108. (save-excursion
  17109. (org-back-to-heading t)
  17110. (when (and org-clock-in-switch-to-state
  17111. (not (looking-at (concat outline-regexp "[ \t]*"
  17112. org-clock-in-switch-to-state
  17113. "\\>"))))
  17114. (org-todo org-clock-in-switch-to-state))
  17115. (if (and org-clock-heading-function
  17116. (functionp org-clock-heading-function))
  17117. (setq org-clock-heading (funcall org-clock-heading-function))
  17118. (if (looking-at org-complex-heading-regexp)
  17119. (setq org-clock-heading (match-string 4))
  17120. (setq org-clock-heading "???")))
  17121. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17122. (org-clock-find-position)
  17123. (insert "\n") (backward-char 1)
  17124. (indent-relative)
  17125. (insert org-clock-string " ")
  17126. (setq org-clock-start-time (current-time))
  17127. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17128. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17129. (or global-mode-string (setq global-mode-string '("")))
  17130. (or (memq 'org-mode-line-string global-mode-string)
  17131. (setq global-mode-string
  17132. (append global-mode-string '(org-mode-line-string))))
  17133. (org-update-mode-line)
  17134. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17135. (message "Clock started at %s" ts))))
  17136. (defun org-clock-find-position ()
  17137. "Find the location where the next clock line should be inserted."
  17138. (org-back-to-heading t)
  17139. (catch 'exit
  17140. (let ((beg (save-excursion
  17141. (beginning-of-line 2)
  17142. (or (bolp) (newline))
  17143. (point)))
  17144. (end (progn (outline-next-heading) (point)))
  17145. (re (concat "^[ \t]*" org-clock-string))
  17146. (cnt 0)
  17147. first last)
  17148. (goto-char beg)
  17149. (when (eobp) (newline) (setq end (max (point) end)))
  17150. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17151. ;; we seem to have a CLOCK drawer, so go there.
  17152. (beginning-of-line 2)
  17153. (throw 'exit t))
  17154. ;; Lets count the CLOCK lines
  17155. (goto-char beg)
  17156. (while (re-search-forward re end t)
  17157. (setq first (or first (match-beginning 0))
  17158. last (match-beginning 0)
  17159. cnt (1+ cnt)))
  17160. (when (and (integerp org-clock-into-drawer)
  17161. (>= (1+ cnt) org-clock-into-drawer))
  17162. ;; Wrap current entries into a new drawer
  17163. (goto-char last)
  17164. (beginning-of-line 2)
  17165. (if (org-at-item-p) (org-end-of-item))
  17166. (insert ":END:\n")
  17167. (beginning-of-line 0)
  17168. (org-indent-line-function)
  17169. (goto-char first)
  17170. (insert ":CLOCK:\n")
  17171. (beginning-of-line 0)
  17172. (org-indent-line-function)
  17173. (org-flag-drawer t)
  17174. (beginning-of-line 2)
  17175. (throw 'exit nil))
  17176. (goto-char beg)
  17177. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17178. (not (equal (match-string 1) org-clock-string)))
  17179. ;; Planning info, skip to after it
  17180. (beginning-of-line 2)
  17181. (or (bolp) (newline)))
  17182. (when (eq t org-clock-into-drawer)
  17183. (insert ":CLOCK:\n:END:\n")
  17184. (beginning-of-line -1)
  17185. (org-indent-line-function)
  17186. (org-flag-drawer t)
  17187. (beginning-of-line 2)
  17188. (org-indent-line-function)))))
  17189. (defun org-clock-out (&optional fail-quietly)
  17190. "Stop the currently running clock.
  17191. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17192. (interactive)
  17193. (catch 'exit
  17194. (if (not (marker-buffer org-clock-marker))
  17195. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17196. (let (ts te s h m)
  17197. (save-excursion
  17198. (set-buffer (marker-buffer org-clock-marker))
  17199. (goto-char org-clock-marker)
  17200. (beginning-of-line 1)
  17201. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17202. (equal (match-string 1) org-clock-string))
  17203. (setq ts (match-string 2))
  17204. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17205. (goto-char (match-end 0))
  17206. (delete-region (point) (point-at-eol))
  17207. (insert "--")
  17208. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17209. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17210. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17211. h (floor (/ s 3600))
  17212. s (- s (* 3600 h))
  17213. m (floor (/ s 60))
  17214. s (- s (* 60 s)))
  17215. (insert " => " (format "%2d:%02d" h m))
  17216. (move-marker org-clock-marker nil)
  17217. (when org-log-note-clock-out
  17218. (org-add-log-maybe 'clock-out))
  17219. (when org-mode-line-timer
  17220. (cancel-timer org-mode-line-timer)
  17221. (setq org-mode-line-timer nil))
  17222. (setq global-mode-string
  17223. (delq 'org-mode-line-string global-mode-string))
  17224. (force-mode-line-update)
  17225. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17226. (defun org-clock-cancel ()
  17227. "Cancel the running clock be removing the start timestamp."
  17228. (interactive)
  17229. (if (not (marker-buffer org-clock-marker))
  17230. (error "No active clock"))
  17231. (save-excursion
  17232. (set-buffer (marker-buffer org-clock-marker))
  17233. (goto-char org-clock-marker)
  17234. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17235. (setq global-mode-string
  17236. (delq 'org-mode-line-string global-mode-string))
  17237. (force-mode-line-update)
  17238. (message "Clock canceled"))
  17239. (defun org-clock-goto (&optional delete-windows)
  17240. "Go to the currently clocked-in entry."
  17241. (interactive "P")
  17242. (if (not (marker-buffer org-clock-marker))
  17243. (error "No active clock"))
  17244. (switch-to-buffer-other-window
  17245. (marker-buffer org-clock-marker))
  17246. (if delete-windows (delete-other-windows))
  17247. (goto-char org-clock-marker)
  17248. (org-show-entry)
  17249. (org-back-to-heading)
  17250. (recenter))
  17251. (defvar org-clock-file-total-minutes nil
  17252. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17253. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17254. (defun org-clock-sum (&optional tstart tend)
  17255. "Sum the times for each subtree.
  17256. Puts the resulting times in minutes as a text property on each headline."
  17257. (interactive)
  17258. (let* ((bmp (buffer-modified-p))
  17259. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17260. org-clock-string
  17261. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17262. (lmax 30)
  17263. (ltimes (make-vector lmax 0))
  17264. (t1 0)
  17265. (level 0)
  17266. ts te dt
  17267. time)
  17268. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17269. (save-excursion
  17270. (goto-char (point-max))
  17271. (while (re-search-backward re nil t)
  17272. (cond
  17273. ((match-end 2)
  17274. ;; Two time stamps
  17275. (setq ts (match-string 2)
  17276. te (match-string 3)
  17277. ts (time-to-seconds
  17278. (apply 'encode-time (org-parse-time-string ts)))
  17279. te (time-to-seconds
  17280. (apply 'encode-time (org-parse-time-string te)))
  17281. ts (if tstart (max ts tstart) ts)
  17282. te (if tend (min te tend) te)
  17283. dt (- te ts)
  17284. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17285. ((match-end 4)
  17286. ;; A naket time
  17287. (setq t1 (+ t1 (string-to-number (match-string 5))
  17288. (* 60 (string-to-number (match-string 4))))))
  17289. (t ;; A headline
  17290. (setq level (- (match-end 1) (match-beginning 1)))
  17291. (when (or (> t1 0) (> (aref ltimes level) 0))
  17292. (loop for l from 0 to level do
  17293. (aset ltimes l (+ (aref ltimes l) t1)))
  17294. (setq t1 0 time (aref ltimes level))
  17295. (loop for l from level to (1- lmax) do
  17296. (aset ltimes l 0))
  17297. (goto-char (match-beginning 0))
  17298. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17299. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17300. (set-buffer-modified-p bmp)))
  17301. (defun org-clock-display (&optional total-only)
  17302. "Show subtree times in the entire buffer.
  17303. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17304. in the echo area."
  17305. (interactive)
  17306. (org-remove-clock-overlays)
  17307. (let (time h m p)
  17308. (org-clock-sum)
  17309. (unless total-only
  17310. (save-excursion
  17311. (goto-char (point-min))
  17312. (while (or (and (equal (setq p (point)) (point-min))
  17313. (get-text-property p :org-clock-minutes))
  17314. (setq p (next-single-property-change
  17315. (point) :org-clock-minutes)))
  17316. (goto-char p)
  17317. (when (setq time (get-text-property p :org-clock-minutes))
  17318. (org-put-clock-overlay time (funcall outline-level))))
  17319. (setq h (/ org-clock-file-total-minutes 60)
  17320. m (- org-clock-file-total-minutes (* 60 h)))
  17321. ;; Arrange to remove the overlays upon next change.
  17322. (when org-remove-highlights-with-change
  17323. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17324. nil 'local))))
  17325. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17326. (defvar org-clock-overlays nil)
  17327. (make-variable-buffer-local 'org-clock-overlays)
  17328. (defun org-put-clock-overlay (time &optional level)
  17329. "Put an overlays on the current line, displaying TIME.
  17330. If LEVEL is given, prefix time with a corresponding number of stars.
  17331. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17332. will be easy to remove."
  17333. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17334. (l (if level (org-get-valid-level level 0) 0))
  17335. (off 0)
  17336. ov tx)
  17337. (move-to-column c)
  17338. (unless (eolp) (skip-chars-backward "^ \t"))
  17339. (skip-chars-backward " \t")
  17340. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17341. tx (concat (buffer-substring (1- (point)) (point))
  17342. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17343. (org-add-props (format "%s %2d:%02d%s"
  17344. (make-string l ?*) h m
  17345. (make-string (- 16 l) ?\ ))
  17346. '(face secondary-selection))
  17347. ""))
  17348. (if (not (featurep 'xemacs))
  17349. (org-overlay-put ov 'display tx)
  17350. (org-overlay-put ov 'invisible t)
  17351. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17352. (push ov org-clock-overlays)))
  17353. (defun org-remove-clock-overlays (&optional beg end noremove)
  17354. "Remove the occur highlights from the buffer.
  17355. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17356. from the `before-change-functions' in the current buffer."
  17357. (interactive)
  17358. (unless org-inhibit-highlight-removal
  17359. (mapc 'org-delete-overlay org-clock-overlays)
  17360. (setq org-clock-overlays nil)
  17361. (unless noremove
  17362. (remove-hook 'before-change-functions
  17363. 'org-remove-clock-overlays 'local))))
  17364. (defun org-clock-out-if-current ()
  17365. "Clock out if the current entry contains the running clock.
  17366. This is used to stop the clock after a TODO entry is marked DONE,
  17367. and is only done if the variable `org-clock-out-when-done' is not nil."
  17368. (when (and org-clock-out-when-done
  17369. (member state org-done-keywords)
  17370. (equal (marker-buffer org-clock-marker) (current-buffer))
  17371. (< (point) org-clock-marker)
  17372. (> (save-excursion (outline-next-heading) (point))
  17373. org-clock-marker))
  17374. ;; Clock out, but don't accept a logging message for this.
  17375. (let ((org-log-note-clock-out nil))
  17376. (org-clock-out))))
  17377. (add-hook 'org-after-todo-state-change-hook
  17378. 'org-clock-out-if-current)
  17379. (defun org-check-running-clock ()
  17380. "Check if the current buffer contains the running clock.
  17381. If yes, offer to stop it and to save the buffer with the changes."
  17382. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17383. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17384. (buffer-name))))
  17385. (org-clock-out)
  17386. (when (y-or-n-p "Save changed buffer?")
  17387. (save-buffer))))
  17388. (defun org-clock-report (&optional arg)
  17389. "Create a table containing a report about clocked time.
  17390. If the cursor is inside an existing clocktable block, then the table
  17391. will be updated. If not, a new clocktable will be inserted.
  17392. When called with a prefix argument, move to the first clock table in the
  17393. buffer and update it."
  17394. (interactive "P")
  17395. (org-remove-clock-overlays)
  17396. (when arg
  17397. (org-find-dblock "clocktable")
  17398. (org-show-entry))
  17399. (if (org-in-clocktable-p)
  17400. (goto-char (org-in-clocktable-p))
  17401. (org-create-dblock (list :name "clocktable"
  17402. :maxlevel 2 :scope 'file)))
  17403. (org-update-dblock))
  17404. (defun org-in-clocktable-p ()
  17405. "Check if the cursor is in a clocktable."
  17406. (let ((pos (point)) start)
  17407. (save-excursion
  17408. (end-of-line 1)
  17409. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17410. (setq start (match-beginning 0))
  17411. (re-search-forward "^#\\+END:.*" nil t)
  17412. (>= (match-end 0) pos)
  17413. start))))
  17414. (defun org-clock-update-time-maybe ()
  17415. "If this is a CLOCK line, update it and return t.
  17416. Otherwise, return nil."
  17417. (interactive)
  17418. (save-excursion
  17419. (beginning-of-line 1)
  17420. (skip-chars-forward " \t")
  17421. (when (looking-at org-clock-string)
  17422. (let ((re (concat "[ \t]*" org-clock-string
  17423. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17424. "\\([ \t]*=>.*\\)?"))
  17425. ts te h m s)
  17426. (if (not (looking-at re))
  17427. nil
  17428. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17429. (end-of-line 1)
  17430. (setq ts (match-string 1)
  17431. te (match-string 2))
  17432. (setq s (- (time-to-seconds
  17433. (apply 'encode-time (org-parse-time-string te)))
  17434. (time-to-seconds
  17435. (apply 'encode-time (org-parse-time-string ts))))
  17436. h (floor (/ s 3600))
  17437. s (- s (* 3600 h))
  17438. m (floor (/ s 60))
  17439. s (- s (* 60 s)))
  17440. (insert " => " (format "%2d:%02d" h m))
  17441. t)))))
  17442. (defun org-clock-special-range (key &optional time as-strings)
  17443. "Return two times bordering a special time range.
  17444. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17445. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17446. A week starts Monday 0:00 and ends Sunday 24:00.
  17447. The range is determined relative to TIME. TIME defaults to the current time.
  17448. The return value is a cons cell with two internal times like the ones
  17449. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17450. the returned times will be formatted strings."
  17451. (let* ((tm (decode-time (or time (current-time))))
  17452. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17453. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17454. (dow (nth 6 tm))
  17455. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17456. (cond
  17457. ((eq key 'today)
  17458. (setq h 0 m 0 h1 24 m1 0))
  17459. ((eq key 'yesterday)
  17460. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17461. ((eq key 'thisweek)
  17462. (setq diff (if (= dow 0) 6 (1- dow))
  17463. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17464. ((eq key 'lastweek)
  17465. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17466. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17467. ((eq key 'thismonth)
  17468. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17469. ((eq key 'lastmonth)
  17470. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17471. ((eq key 'thisyear)
  17472. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17473. ((eq key 'lastyear)
  17474. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17475. (t (error "No such time block %s" key)))
  17476. (setq ts (encode-time s m h d month y)
  17477. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17478. (or d1 d) (or month1 month) (or y1 y)))
  17479. (setq fm (cdr org-time-stamp-formats))
  17480. (if as-strings
  17481. (cons (format-time-string fm ts) (format-time-string fm te))
  17482. (cons ts te))))
  17483. (defun org-dblock-write:clocktable (params)
  17484. "Write the standard clocktable."
  17485. (catch 'exit
  17486. (let* ((hlchars '((1 . "*") (2 . "/")))
  17487. (ins (make-marker))
  17488. (total-time nil)
  17489. (scope (plist-get params :scope))
  17490. (tostring (plist-get params :tostring))
  17491. (multifile (plist-get params :multifile))
  17492. (header (plist-get params :header))
  17493. (maxlevel (or (plist-get params :maxlevel) 3))
  17494. (step (plist-get params :step))
  17495. (emph (plist-get params :emphasize))
  17496. (ts (plist-get params :tstart))
  17497. (te (plist-get params :tend))
  17498. (block (plist-get params :block))
  17499. (link (plist-get params :link))
  17500. ipos time h m p level hlc hdl
  17501. cc beg end pos tbl)
  17502. (when step
  17503. (org-clocktable-steps params)
  17504. (throw 'exit nil))
  17505. (when block
  17506. (setq cc (org-clock-special-range block nil t)
  17507. ts (car cc) te (cdr cc)))
  17508. (if ts (setq ts (time-to-seconds
  17509. (apply 'encode-time (org-parse-time-string ts)))))
  17510. (if te (setq te (time-to-seconds
  17511. (apply 'encode-time (org-parse-time-string te)))))
  17512. (move-marker ins (point))
  17513. (setq ipos (point))
  17514. ;; Get the right scope
  17515. (setq pos (point))
  17516. (save-restriction
  17517. (cond
  17518. ((not scope))
  17519. ((eq scope 'file) (widen))
  17520. ((eq scope 'subtree) (org-narrow-to-subtree))
  17521. ((eq scope 'tree)
  17522. (while (org-up-heading-safe))
  17523. (org-narrow-to-subtree))
  17524. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17525. (symbol-name scope)))
  17526. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17527. (catch 'exit
  17528. (while (org-up-heading-safe)
  17529. (looking-at outline-regexp)
  17530. (if (<= (org-reduced-level (funcall outline-level)) level)
  17531. (throw 'exit nil))))
  17532. (org-narrow-to-subtree))
  17533. ((or (listp scope) (eq scope 'agenda))
  17534. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17535. (scope 'agenda)
  17536. (p1 (copy-sequence params))
  17537. file)
  17538. (plist-put p1 :tostring t)
  17539. (plist-put p1 :multifile t)
  17540. (plist-put p1 :scope 'file)
  17541. (org-prepare-agenda-buffers files)
  17542. (while (setq file (pop files))
  17543. (with-current-buffer (find-buffer-visiting file)
  17544. (push (org-clocktable-add-file
  17545. file (org-dblock-write:clocktable p1)) tbl)
  17546. (setq total-time (+ (or total-time 0)
  17547. org-clock-file-total-minutes)))))))
  17548. (goto-char pos)
  17549. (unless (eq scope 'agenda)
  17550. (org-clock-sum ts te)
  17551. (goto-char (point-min))
  17552. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17553. (goto-char p)
  17554. (when (setq time (get-text-property p :org-clock-minutes))
  17555. (save-excursion
  17556. (beginning-of-line 1)
  17557. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17558. (setq level (org-reduced-level
  17559. (- (match-end 1) (match-beginning 1))))
  17560. (<= level maxlevel))
  17561. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17562. hdl (if (not link)
  17563. (match-string 2)
  17564. (org-make-link-string
  17565. (format "file:%s::%s"
  17566. (buffer-file-name)
  17567. (save-match-data
  17568. (org-make-org-heading-search-string
  17569. (match-string 2))))
  17570. (match-string 2)))
  17571. h (/ time 60)
  17572. m (- time (* 60 h)))
  17573. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17574. (push (concat
  17575. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17576. (make-string (1- level) ?|)
  17577. hlc (format "%d:%02d" h m) hlc
  17578. " |") tbl))))))
  17579. (setq tbl (nreverse tbl))
  17580. (if tostring
  17581. (if tbl (mapconcat 'identity tbl "\n") nil)
  17582. (goto-char ins)
  17583. (insert-before-markers
  17584. (or header
  17585. (concat
  17586. "Clock summary at ["
  17587. (substring
  17588. (format-time-string (cdr org-time-stamp-formats))
  17589. 1 -1)
  17590. "]."
  17591. (if block
  17592. (format " Considered range is /%s/." block)
  17593. "")
  17594. "\n\n"))
  17595. (if (eq scope 'agenda) "|File" "")
  17596. "|L|Headline|Time|\n")
  17597. (setq total-time (or total-time org-clock-file-total-minutes)
  17598. h (/ total-time 60)
  17599. m (- total-time (* 60 h)))
  17600. (insert-before-markers
  17601. "|-\n|"
  17602. (if (eq scope 'agenda) "|" "")
  17603. "|"
  17604. "*Total time*| "
  17605. (format "*%d:%02d*" h m)
  17606. "|\n|-\n")
  17607. (setq tbl (delq nil tbl))
  17608. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17609. (equal (substring (car tbl) 0 2) "|-"))
  17610. (pop tbl))
  17611. (insert-before-markers (mapconcat
  17612. 'identity (delq nil tbl)
  17613. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17614. (backward-delete-char 1)
  17615. (goto-char ipos)
  17616. (skip-chars-forward "^|")
  17617. (org-table-align))))))
  17618. (defun org-clocktable-steps (params)
  17619. (let* ((p1 (copy-sequence params))
  17620. (ts (plist-get p1 :tstart))
  17621. (te (plist-get p1 :tend))
  17622. (step0 (plist-get p1 :step))
  17623. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17624. (block (plist-get p1 :block))
  17625. cc)
  17626. (when block
  17627. (setq cc (org-clock-special-range block nil t)
  17628. ts (car cc) te (cdr cc)))
  17629. (if ts (setq ts (time-to-seconds
  17630. (apply 'encode-time (org-parse-time-string ts)))))
  17631. (if te (setq te (time-to-seconds
  17632. (apply 'encode-time (org-parse-time-string te)))))
  17633. (plist-put p1 :header "")
  17634. (plist-put p1 :step nil)
  17635. (plist-put p1 :block nil)
  17636. (while (< ts te)
  17637. (or (bolp) (insert "\n"))
  17638. (plist-put p1 :tstart (format-time-string
  17639. (car org-time-stamp-formats)
  17640. (seconds-to-time ts)))
  17641. (plist-put p1 :tend (format-time-string
  17642. (car org-time-stamp-formats)
  17643. (seconds-to-time (setq ts (+ ts step)))))
  17644. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17645. (plist-get p1 :tstart) "\n")
  17646. (org-dblock-write:clocktable p1)
  17647. (re-search-forward "#\\+END:")
  17648. (end-of-line 0))))
  17649. (defun org-clocktable-add-file (file table)
  17650. (if table
  17651. (let ((lines (org-split-string table "\n"))
  17652. (ff (file-name-nondirectory file)))
  17653. (mapconcat 'identity
  17654. (mapcar (lambda (x)
  17655. (if (string-match org-table-dataline-regexp x)
  17656. (concat "|" ff x)
  17657. x))
  17658. lines)
  17659. "\n"))))
  17660. ;; FIXME: I don't think anybody uses this, ask David
  17661. (defun org-collect-clock-time-entries ()
  17662. "Return an internal list with clocking information.
  17663. This list has one entry for each CLOCK interval.
  17664. FIXME: describe the elements."
  17665. (interactive)
  17666. (let ((re (concat "^[ \t]*" org-clock-string
  17667. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17668. rtn beg end next cont level title total closedp leafp
  17669. clockpos titlepos h m donep)
  17670. (save-excursion
  17671. (org-clock-sum)
  17672. (goto-char (point-min))
  17673. (while (re-search-forward re nil t)
  17674. (setq clockpos (match-beginning 0)
  17675. beg (match-string 1) end (match-string 2)
  17676. cont (match-end 0))
  17677. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17678. end (apply 'encode-time (org-parse-time-string end)))
  17679. (org-back-to-heading t)
  17680. (setq donep (org-entry-is-done-p))
  17681. (setq titlepos (point)
  17682. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17683. h (/ total 60) m (- total (* 60 h))
  17684. total (cons h m))
  17685. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17686. (setq level (- (match-end 1) (match-beginning 1))
  17687. title (org-match-string-no-properties 2))
  17688. (save-excursion (outline-next-heading) (setq next (point)))
  17689. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17690. (goto-char next)
  17691. (setq leafp (and (looking-at "^\\*+ ")
  17692. (<= (- (match-end 0) (point)) level)))
  17693. (push (list beg end clockpos closedp donep
  17694. total title titlepos level leafp)
  17695. rtn)
  17696. (goto-char cont)))
  17697. (nreverse rtn)))
  17698. ;;;; Agenda, and Diary Integration
  17699. ;;; Define the Org-agenda-mode
  17700. (defvar org-agenda-mode-map (make-sparse-keymap)
  17701. "Keymap for `org-agenda-mode'.")
  17702. (defvar org-agenda-menu) ; defined later in this file.
  17703. (defvar org-agenda-follow-mode nil)
  17704. (defvar org-agenda-show-log nil)
  17705. (defvar org-agenda-redo-command nil)
  17706. (defvar org-agenda-query-string nil)
  17707. (defvar org-agenda-mode-hook nil)
  17708. (defvar org-agenda-type nil)
  17709. (defvar org-agenda-force-single-file nil)
  17710. (defun org-agenda-mode ()
  17711. "Mode for time-sorted view on action items in Org-mode files.
  17712. The following commands are available:
  17713. \\{org-agenda-mode-map}"
  17714. (interactive)
  17715. (kill-all-local-variables)
  17716. (setq org-agenda-undo-list nil
  17717. org-agenda-pending-undo-list nil)
  17718. (setq major-mode 'org-agenda-mode)
  17719. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17720. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17721. (setq mode-name "Org-Agenda")
  17722. (use-local-map org-agenda-mode-map)
  17723. (easy-menu-add org-agenda-menu)
  17724. (if org-startup-truncated (setq truncate-lines t))
  17725. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17726. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17727. ;; Make sure properties are removed when copying text
  17728. (when (boundp 'buffer-substring-filters)
  17729. (org-set-local 'buffer-substring-filters
  17730. (cons (lambda (x)
  17731. (set-text-properties 0 (length x) nil x) x)
  17732. buffer-substring-filters)))
  17733. (unless org-agenda-keep-modes
  17734. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17735. org-agenda-show-log nil))
  17736. (easy-menu-change
  17737. '("Agenda") "Agenda Files"
  17738. (append
  17739. (list
  17740. (vector
  17741. (if (get 'org-agenda-files 'org-restrict)
  17742. "Restricted to single file"
  17743. "Edit File List")
  17744. '(org-edit-agenda-file-list)
  17745. (not (get 'org-agenda-files 'org-restrict)))
  17746. "--")
  17747. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17748. (org-agenda-set-mode-name)
  17749. (apply
  17750. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17751. (list 'org-agenda-mode-hook)))
  17752. (substitute-key-definition 'undo 'org-agenda-undo
  17753. org-agenda-mode-map global-map)
  17754. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17755. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17756. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17757. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17758. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17759. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17760. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17761. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17762. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17763. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17764. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17765. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17766. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17767. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17768. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17769. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17770. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17771. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17772. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17773. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17774. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17775. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17776. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17777. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17778. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17779. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17780. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17781. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17782. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17783. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17784. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17785. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17786. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17787. (while l (org-defkey org-agenda-mode-map
  17788. (int-to-string (pop l)) 'digit-argument)))
  17789. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17790. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17791. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17792. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17793. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17794. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17795. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17796. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17797. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17798. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17799. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17800. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17801. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17802. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17803. (org-defkey org-agenda-mode-map "n" 'next-line)
  17804. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17805. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17806. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17807. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17808. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17809. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17810. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17811. (eval-after-load "calendar"
  17812. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17813. 'org-calendar-goto-agenda))
  17814. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17815. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17816. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17817. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17818. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17819. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17820. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17821. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17822. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17823. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17824. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17825. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17826. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17827. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17828. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17829. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17830. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17831. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17832. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17833. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17834. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17835. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17836. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  17837. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  17838. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  17839. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  17840. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17841. "Local keymap for agenda entries from Org-mode.")
  17842. (org-defkey org-agenda-keymap
  17843. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17844. (org-defkey org-agenda-keymap
  17845. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17846. (when org-agenda-mouse-1-follows-link
  17847. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17848. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17849. '("Agenda"
  17850. ("Agenda Files")
  17851. "--"
  17852. ["Show" org-agenda-show t]
  17853. ["Go To (other window)" org-agenda-goto t]
  17854. ["Go To (this window)" org-agenda-switch-to t]
  17855. ["Follow Mode" org-agenda-follow-mode
  17856. :style toggle :selected org-agenda-follow-mode :active t]
  17857. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17858. "--"
  17859. ["Cycle TODO" org-agenda-todo t]
  17860. ["Archive subtree" org-agenda-archive t]
  17861. ["Delete subtree" org-agenda-kill t]
  17862. "--"
  17863. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17864. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17865. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17866. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17867. "--"
  17868. ("Tags and Properties"
  17869. ["Show all Tags" org-agenda-show-tags t]
  17870. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17871. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17872. "--"
  17873. ["Column View" org-columns t])
  17874. ("Date/Schedule"
  17875. ["Schedule" org-agenda-schedule t]
  17876. ["Set Deadline" org-agenda-deadline t]
  17877. "--"
  17878. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17879. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17880. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17881. ("Clock"
  17882. ["Clock in" org-agenda-clock-in t]
  17883. ["Clock out" org-agenda-clock-out t]
  17884. ["Clock cancel" org-agenda-clock-cancel t]
  17885. ["Goto running clock" org-clock-goto t])
  17886. ("Priority"
  17887. ["Set Priority" org-agenda-priority t]
  17888. ["Increase Priority" org-agenda-priority-up t]
  17889. ["Decrease Priority" org-agenda-priority-down t]
  17890. ["Show Priority" org-agenda-show-priority t])
  17891. ("Calendar/Diary"
  17892. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17893. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17894. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17895. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17896. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17897. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17898. "--"
  17899. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17900. "--"
  17901. ("View"
  17902. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17903. :style radio :selected (equal org-agenda-ndays 1)]
  17904. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17905. :style radio :selected (equal org-agenda-ndays 7)]
  17906. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17907. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17908. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17909. :style radio :selected (member org-agenda-ndays '(365 366))]
  17910. "--"
  17911. ["Show Logbook entries" org-agenda-log-mode
  17912. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17913. ["Include Diary" org-agenda-toggle-diary
  17914. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17915. ["Use Time Grid" org-agenda-toggle-time-grid
  17916. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17917. ["Write view to file" org-write-agenda t]
  17918. ["Rebuild buffer" org-agenda-redo t]
  17919. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17920. "--"
  17921. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17922. "--"
  17923. ["Quit" org-agenda-quit t]
  17924. ["Exit and Release Buffers" org-agenda-exit t]
  17925. ))
  17926. ;;; Agenda undo
  17927. (defvar org-agenda-allow-remote-undo t
  17928. "Non-nil means, allow remote undo from the agenda buffer.")
  17929. (defvar org-agenda-undo-list nil
  17930. "List of undoable operations in the agenda since last refresh.")
  17931. (defvar org-agenda-undo-has-started-in nil
  17932. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17933. (defvar org-agenda-pending-undo-list nil
  17934. "In a series of undo commands, this is the list of remaning undo items.")
  17935. (defmacro org-if-unprotected (&rest body)
  17936. "Execute BODY if there is no `org-protected' text property at point."
  17937. (declare (debug t))
  17938. `(unless (get-text-property (point) 'org-protected)
  17939. ,@body))
  17940. (defmacro org-with-remote-undo (_buffer &rest _body)
  17941. "Execute BODY while recording undo information in two buffers."
  17942. (declare (indent 1) (debug t))
  17943. `(let ((_cline (org-current-line))
  17944. (_cmd this-command)
  17945. (_buf1 (current-buffer))
  17946. (_buf2 ,_buffer)
  17947. (_undo1 buffer-undo-list)
  17948. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17949. _c1 _c2)
  17950. ,@_body
  17951. (when org-agenda-allow-remote-undo
  17952. (setq _c1 (org-verify-change-for-undo
  17953. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17954. _c2 (org-verify-change-for-undo
  17955. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17956. (when (or _c1 _c2)
  17957. ;; make sure there are undo boundaries
  17958. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17959. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17960. ;; remember which buffer to undo
  17961. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17962. org-agenda-undo-list)))))
  17963. (defun org-agenda-undo ()
  17964. "Undo a remote editing step in the agenda.
  17965. This undoes changes both in the agenda buffer and in the remote buffer
  17966. that have been changed along."
  17967. (interactive)
  17968. (or org-agenda-allow-remote-undo
  17969. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17970. (if (not (eq this-command last-command))
  17971. (setq org-agenda-undo-has-started-in nil
  17972. org-agenda-pending-undo-list org-agenda-undo-list))
  17973. (if (not org-agenda-pending-undo-list)
  17974. (error "No further undo information"))
  17975. (let* ((entry (pop org-agenda-pending-undo-list))
  17976. buf line cmd rembuf)
  17977. (setq cmd (pop entry) line (pop entry))
  17978. (setq rembuf (nth 2 entry))
  17979. (org-with-remote-undo rembuf
  17980. (while (bufferp (setq buf (pop entry)))
  17981. (if (pop entry)
  17982. (with-current-buffer buf
  17983. (let ((last-undo-buffer buf)
  17984. (inhibit-read-only t))
  17985. (unless (memq buf org-agenda-undo-has-started-in)
  17986. (push buf org-agenda-undo-has-started-in)
  17987. (make-local-variable 'pending-undo-list)
  17988. (undo-start))
  17989. (while (and pending-undo-list
  17990. (listp pending-undo-list)
  17991. (not (car pending-undo-list)))
  17992. (pop pending-undo-list))
  17993. (undo-more 1))))))
  17994. (goto-line line)
  17995. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17996. (defun org-verify-change-for-undo (l1 l2)
  17997. "Verify that a real change occurred between the undo lists L1 and L2."
  17998. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17999. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18000. (not (eq l1 l2)))
  18001. ;;; Agenda dispatch
  18002. (defvar org-agenda-restrict nil)
  18003. (defvar org-agenda-restrict-begin (make-marker))
  18004. (defvar org-agenda-restrict-end (make-marker))
  18005. (defvar org-agenda-last-dispatch-buffer nil)
  18006. (defvar org-agenda-overriding-restriction nil)
  18007. ;;;###autoload
  18008. (defun org-agenda (arg &optional keys restriction)
  18009. "Dispatch agenda commands to collect entries to the agenda buffer.
  18010. Prompts for a command to execute. Any prefix arg will be passed
  18011. on to the selected command. The default selections are:
  18012. a Call `org-agenda-list' to display the agenda for current day or week.
  18013. t Call `org-todo-list' to display the global todo list.
  18014. T Call `org-todo-list' to display the global todo list, select only
  18015. entries with a specific TODO keyword (the user gets a prompt).
  18016. m Call `org-tags-view' to display headlines with tags matching
  18017. a condition (the user is prompted for the condition).
  18018. M Like `m', but select only TODO entries, no ordinary headlines.
  18019. L Create a timeline for the current buffer.
  18020. e Export views to associated files.
  18021. More commands can be added by configuring the variable
  18022. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18023. searches can be pre-defined in this way.
  18024. If the current buffer is in Org-mode and visiting a file, you can also
  18025. first press `<' once to indicate that the agenda should be temporarily
  18026. \(until the next use of \\[org-agenda]) restricted to the current file.
  18027. Pressing `<' twice means to restrict to the current subtree or region
  18028. \(if active)."
  18029. (interactive "P")
  18030. (catch 'exit
  18031. (let* ((prefix-descriptions nil)
  18032. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18033. (org-agenda-custom-commands
  18034. ;; normalize different versions
  18035. (delq nil
  18036. (mapcar
  18037. (lambda (x)
  18038. (cond ((stringp (cdr x))
  18039. (push x prefix-descriptions)
  18040. nil)
  18041. ((stringp (nth 1 x)) x)
  18042. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18043. (t (cons (car x) (cons "" (cdr x))))))
  18044. org-agenda-custom-commands)))
  18045. (buf (current-buffer))
  18046. (bfn (buffer-file-name (buffer-base-buffer)))
  18047. entry key type match lprops ans)
  18048. ;; Turn off restriction unless there is an overriding one
  18049. (unless org-agenda-overriding-restriction
  18050. (put 'org-agenda-files 'org-restrict nil)
  18051. (setq org-agenda-restrict nil)
  18052. (move-marker org-agenda-restrict-begin nil)
  18053. (move-marker org-agenda-restrict-end nil))
  18054. ;; Delete old local properties
  18055. (put 'org-agenda-redo-command 'org-lprops nil)
  18056. ;; Remember where this call originated
  18057. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18058. (unless keys
  18059. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18060. keys (car ans)
  18061. restriction (cdr ans)))
  18062. ;; Estabish the restriction, if any
  18063. (when (and (not org-agenda-overriding-restriction) restriction)
  18064. (put 'org-agenda-files 'org-restrict (list bfn))
  18065. (cond
  18066. ((eq restriction 'region)
  18067. (setq org-agenda-restrict t)
  18068. (move-marker org-agenda-restrict-begin (region-beginning))
  18069. (move-marker org-agenda-restrict-end (region-end)))
  18070. ((eq restriction 'subtree)
  18071. (save-excursion
  18072. (setq org-agenda-restrict t)
  18073. (org-back-to-heading t)
  18074. (move-marker org-agenda-restrict-begin (point))
  18075. (move-marker org-agenda-restrict-end
  18076. (progn (org-end-of-subtree t)))))))
  18077. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18078. ;; For example the todo list should not need it (but does...)
  18079. (cond
  18080. ((setq entry (assoc keys org-agenda-custom-commands))
  18081. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18082. (progn
  18083. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18084. (put 'org-agenda-redo-command 'org-lprops lprops)
  18085. (cond
  18086. ((eq type 'agenda)
  18087. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18088. ((eq type 'alltodo)
  18089. (org-let lprops '(org-todo-list current-prefix-arg)))
  18090. ((eq type 'search)
  18091. (org-let lprops '(org-search-view current-prefix-arg match nil)))
  18092. ((eq type 'stuck)
  18093. (org-let lprops '(org-agenda-list-stuck-projects
  18094. current-prefix-arg)))
  18095. ((eq type 'tags)
  18096. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18097. ((eq type 'tags-todo)
  18098. (org-let lprops '(org-tags-view '(4) match)))
  18099. ((eq type 'todo)
  18100. (org-let lprops '(org-todo-list match)))
  18101. ((eq type 'tags-tree)
  18102. (org-check-for-org-mode)
  18103. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18104. ((eq type 'todo-tree)
  18105. (org-check-for-org-mode)
  18106. (org-let lprops
  18107. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18108. (regexp-quote match) "\\>"))))
  18109. ((eq type 'occur-tree)
  18110. (org-check-for-org-mode)
  18111. (org-let lprops '(org-occur match)))
  18112. ((functionp type)
  18113. (org-let lprops '(funcall type match)))
  18114. ((fboundp type)
  18115. (org-let lprops '(funcall type match)))
  18116. (t (error "Invalid custom agenda command type %s" type))))
  18117. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18118. ((equal keys "C")
  18119. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18120. (customize-variable 'org-agenda-custom-commands))
  18121. ((equal keys "a") (call-interactively 'org-agenda-list))
  18122. ((equal keys "s") (call-interactively 'org-search-view))
  18123. ((equal keys "t") (call-interactively 'org-todo-list))
  18124. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18125. ((equal keys "m") (call-interactively 'org-tags-view))
  18126. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18127. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18128. ((equal keys "L")
  18129. (unless (org-mode-p)
  18130. (error "This is not an Org-mode file"))
  18131. (unless restriction
  18132. (put 'org-agenda-files 'org-restrict (list bfn))
  18133. (org-call-with-arg 'org-timeline arg)))
  18134. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18135. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18136. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18137. (t (error "Invalid agenda key"))))))
  18138. (defun org-agenda-normalize-custom-commands (cmds)
  18139. (delq nil
  18140. (mapcar
  18141. (lambda (x)
  18142. (cond ((stringp (cdr x)) nil)
  18143. ((stringp (nth 1 x)) x)
  18144. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18145. (t (cons (car x) (cons "" (cdr x))))))
  18146. cmds)))
  18147. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18148. "The user interface for selecting an agenda command."
  18149. (catch 'exit
  18150. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18151. (restrict-ok (and bfn (org-mode-p)))
  18152. (region-p (org-region-active-p))
  18153. (custom org-agenda-custom-commands)
  18154. (selstring "")
  18155. restriction second-time
  18156. c entry key type match prefixes rmheader header-end custom1 desc)
  18157. (save-window-excursion
  18158. (delete-other-windows)
  18159. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18160. (erase-buffer)
  18161. (insert (eval-when-compile
  18162. (let ((header
  18163. "
  18164. Press key for an agenda command: < Buffer,subtree/region restriction
  18165. -------------------------------- > Remove restriction
  18166. a Agenda for current week or day e Export agenda views
  18167. t List of all TODO entries T Entries with special TODO kwd
  18168. m Match a TAGS query M Like m, but only TODO entries
  18169. L Timeline for current buffer # List stuck projects (!=configure)
  18170. s Search for keywords C Configure custom agenda commands
  18171. / Multi-occur
  18172. ")
  18173. (start 0))
  18174. (while (string-match
  18175. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18176. header start)
  18177. (setq start (match-end 0))
  18178. (add-text-properties (match-beginning 2) (match-end 2)
  18179. '(face bold) header))
  18180. header)))
  18181. (setq header-end (move-marker (make-marker) (point)))
  18182. (while t
  18183. (setq custom1 custom)
  18184. (when (eq rmheader t)
  18185. (goto-line 1)
  18186. (re-search-forward ":" nil t)
  18187. (delete-region (match-end 0) (point-at-eol))
  18188. (forward-char 1)
  18189. (looking-at "-+")
  18190. (delete-region (match-end 0) (point-at-eol))
  18191. (move-marker header-end (match-end 0)))
  18192. (goto-char header-end)
  18193. (delete-region (point) (point-max))
  18194. (while (setq entry (pop custom1))
  18195. (setq key (car entry) desc (nth 1 entry)
  18196. type (nth 2 entry) match (nth 3 entry))
  18197. (if (> (length key) 1)
  18198. (add-to-list 'prefixes (string-to-char key))
  18199. (insert
  18200. (format
  18201. "\n%-4s%-14s: %s"
  18202. (org-add-props (copy-sequence key)
  18203. '(face bold))
  18204. (cond
  18205. ((string-match "\\S-" desc) desc)
  18206. ((eq type 'agenda) "Agenda for current week or day")
  18207. ((eq type 'alltodo) "List of all TODO entries")
  18208. ((eq type 'search) "Word search")
  18209. ((eq type 'stuck) "List of stuck projects")
  18210. ((eq type 'todo) "TODO keyword")
  18211. ((eq type 'tags) "Tags query")
  18212. ((eq type 'tags-todo) "Tags (TODO)")
  18213. ((eq type 'tags-tree) "Tags tree")
  18214. ((eq type 'todo-tree) "TODO kwd tree")
  18215. ((eq type 'occur-tree) "Occur tree")
  18216. ((functionp type) (if (symbolp type)
  18217. (symbol-name type)
  18218. "Lambda expression"))
  18219. (t "???"))
  18220. (cond
  18221. ((stringp match)
  18222. (org-add-props match nil 'face 'org-warning))
  18223. (match
  18224. (format "set of %d commands" (length match)))
  18225. (t ""))))))
  18226. (when prefixes
  18227. (mapc (lambda (x)
  18228. (insert
  18229. (format "\n%s %s"
  18230. (org-add-props (char-to-string x)
  18231. nil 'face 'bold)
  18232. (or (cdr (assoc (concat selstring (char-to-string x))
  18233. prefix-descriptions))
  18234. "Prefix key"))))
  18235. prefixes))
  18236. (goto-char (point-min))
  18237. (when (fboundp 'fit-window-to-buffer)
  18238. (if second-time
  18239. (if (not (pos-visible-in-window-p (point-max)))
  18240. (fit-window-to-buffer))
  18241. (setq second-time t)
  18242. (fit-window-to-buffer)))
  18243. (message "Press key for agenda command%s:"
  18244. (if (or restrict-ok org-agenda-overriding-restriction)
  18245. (if org-agenda-overriding-restriction
  18246. " (restriction lock active)"
  18247. (if restriction
  18248. (format " (restricted to %s)" restriction)
  18249. " (unrestricted)"))
  18250. ""))
  18251. (setq c (read-char-exclusive))
  18252. (message "")
  18253. (cond
  18254. ((assoc (char-to-string c) custom)
  18255. (setq selstring (concat selstring (char-to-string c)))
  18256. (throw 'exit (cons selstring restriction)))
  18257. ((memq c prefixes)
  18258. (setq selstring (concat selstring (char-to-string c))
  18259. prefixes nil
  18260. rmheader (or rmheader t)
  18261. custom (delq nil (mapcar
  18262. (lambda (x)
  18263. (if (or (= (length (car x)) 1)
  18264. (/= (string-to-char (car x)) c))
  18265. nil
  18266. (cons (substring (car x) 1) (cdr x))))
  18267. custom))))
  18268. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18269. (message "Restriction is only possible in Org-mode buffers")
  18270. (ding) (sit-for 1))
  18271. ((eq c ?1)
  18272. (org-agenda-remove-restriction-lock 'noupdate)
  18273. (setq restriction 'buffer))
  18274. ((eq c ?0)
  18275. (org-agenda-remove-restriction-lock 'noupdate)
  18276. (setq restriction (if region-p 'region 'subtree)))
  18277. ((eq c ?<)
  18278. (org-agenda-remove-restriction-lock 'noupdate)
  18279. (setq restriction
  18280. (cond
  18281. ((eq restriction 'buffer)
  18282. (if region-p 'region 'subtree))
  18283. ((memq restriction '(subtree region))
  18284. nil)
  18285. (t 'buffer))))
  18286. ((eq c ?>)
  18287. (org-agenda-remove-restriction-lock 'noupdate)
  18288. (setq restriction nil))
  18289. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18290. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18291. ((and (> (length selstring) 0) (eq c ?\d))
  18292. (delete-window)
  18293. (org-agenda-get-restriction-and-command prefix-descriptions))
  18294. ((equal c ?q) (error "Abort"))
  18295. (t (error "Invalid key %c" c))))))))
  18296. (defun org-run-agenda-series (name series)
  18297. (org-prepare-agenda name)
  18298. (let* ((org-agenda-multi t)
  18299. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18300. (cmds (car series))
  18301. (gprops (nth 1 series))
  18302. match ;; The byte compiler incorrectly complains about this. Keep it!
  18303. cmd type lprops)
  18304. (while (setq cmd (pop cmds))
  18305. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18306. (cond
  18307. ((eq type 'agenda)
  18308. (org-let2 gprops lprops
  18309. '(call-interactively 'org-agenda-list)))
  18310. ((eq type 'alltodo)
  18311. (org-let2 gprops lprops
  18312. '(call-interactively 'org-todo-list)))
  18313. ((eq type 'search)
  18314. (org-let2 gprops lprops
  18315. '(org-search-view current-prefix-arg match nil)))
  18316. ((eq type 'stuck)
  18317. (org-let2 gprops lprops
  18318. '(call-interactively 'org-agenda-list-stuck-projects)))
  18319. ((eq type 'tags)
  18320. (org-let2 gprops lprops
  18321. '(org-tags-view current-prefix-arg match)))
  18322. ((eq type 'tags-todo)
  18323. (org-let2 gprops lprops
  18324. '(org-tags-view '(4) match)))
  18325. ((eq type 'todo)
  18326. (org-let2 gprops lprops
  18327. '(org-todo-list match)))
  18328. ((fboundp type)
  18329. (org-let2 gprops lprops
  18330. '(funcall type match)))
  18331. (t (error "Invalid type in command series"))))
  18332. (widen)
  18333. (setq org-agenda-redo-command redo)
  18334. (goto-char (point-min)))
  18335. (org-finalize-agenda))
  18336. ;;;###autoload
  18337. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18338. "Run an agenda command in batch mode and send the result to STDOUT.
  18339. If CMD-KEY is a string of length 1, it is used as a key in
  18340. `org-agenda-custom-commands' and triggers this command. If it is a
  18341. longer string it is used as a tags/todo match string.
  18342. Paramters are alternating variable names and values that will be bound
  18343. before running the agenda command."
  18344. (let (pars)
  18345. (while parameters
  18346. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18347. (if (> (length cmd-key) 2)
  18348. (eval (list 'let (nreverse pars)
  18349. (list 'org-tags-view nil cmd-key)))
  18350. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18351. (set-buffer org-agenda-buffer-name)
  18352. (princ (org-encode-for-stdout (buffer-string)))))
  18353. (defun org-encode-for-stdout (string)
  18354. (if (fboundp 'encode-coding-string)
  18355. (encode-coding-string string buffer-file-coding-system)
  18356. string))
  18357. (defvar org-agenda-info nil)
  18358. ;;;###autoload
  18359. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18360. "Run an agenda command in batch mode and send the result to STDOUT.
  18361. If CMD-KEY is a string of length 1, it is used as a key in
  18362. `org-agenda-custom-commands' and triggers this command. If it is a
  18363. longer string it is used as a tags/todo match string.
  18364. Paramters are alternating variable names and values that will be bound
  18365. before running the agenda command.
  18366. The output gives a line for each selected agenda item. Each
  18367. item is a list of comma-separated values, like this:
  18368. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18369. category The category of the item
  18370. head The headline, without TODO kwd, TAGS and PRIORITY
  18371. type The type of the agenda entry, can be
  18372. todo selected in TODO match
  18373. tagsmatch selected in tags match
  18374. diary imported from diary
  18375. deadline a deadline on given date
  18376. scheduled scheduled on given date
  18377. timestamp entry has timestamp on given date
  18378. closed entry was closed on given date
  18379. upcoming-deadline warning about deadline
  18380. past-scheduled forwarded scheduled item
  18381. block entry has date block including g. date
  18382. todo The todo keyword, if any
  18383. tags All tags including inherited ones, separated by colons
  18384. date The relevant date, like 2007-2-14
  18385. time The time, like 15:00-16:50
  18386. extra Sting with extra planning info
  18387. priority-l The priority letter if any was given
  18388. priority-n The computed numerical priority
  18389. agenda-day The day in the agenda where this is listed"
  18390. (let (pars)
  18391. (while parameters
  18392. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18393. (push (list 'org-agenda-remove-tags t) pars)
  18394. (if (> (length cmd-key) 2)
  18395. (eval (list 'let (nreverse pars)
  18396. (list 'org-tags-view nil cmd-key)))
  18397. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18398. (set-buffer org-agenda-buffer-name)
  18399. (let* ((lines (org-split-string (buffer-string) "\n"))
  18400. line)
  18401. (while (setq line (pop lines))
  18402. (catch 'next
  18403. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18404. (setq org-agenda-info
  18405. (org-fix-agenda-info (text-properties-at 0 line)))
  18406. (princ
  18407. (org-encode-for-stdout
  18408. (mapconcat 'org-agenda-export-csv-mapper
  18409. '(org-category txt type todo tags date time-of-day extra
  18410. priority-letter priority agenda-day)
  18411. ",")))
  18412. (princ "\n"))))))
  18413. (defun org-fix-agenda-info (props)
  18414. "Make sure all properties on an agenda item have a canonical form,
  18415. so the export commands can easily use it."
  18416. (let (tmp re)
  18417. (when (setq tmp (plist-get props 'tags))
  18418. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18419. (when (setq tmp (plist-get props 'date))
  18420. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18421. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18422. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18423. (setq tmp (calendar-date-string tmp)))
  18424. (setq props (plist-put props 'date tmp)))
  18425. (when (setq tmp (plist-get props 'day))
  18426. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18427. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18428. (setq tmp (calendar-date-string tmp)))
  18429. (setq props (plist-put props 'day tmp))
  18430. (setq props (plist-put props 'agenda-day tmp)))
  18431. (when (setq tmp (plist-get props 'txt))
  18432. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18433. (plist-put props 'priority-letter (match-string 1 tmp))
  18434. (setq tmp (replace-match "" t t tmp)))
  18435. (when (and (setq re (plist-get props 'org-todo-regexp))
  18436. (setq re (concat "\\`\\.*" re " ?"))
  18437. (string-match re tmp))
  18438. (plist-put props 'todo (match-string 1 tmp))
  18439. (setq tmp (replace-match "" t t tmp)))
  18440. (plist-put props 'txt tmp)))
  18441. props)
  18442. (defun org-agenda-export-csv-mapper (prop)
  18443. (let ((res (plist-get org-agenda-info prop)))
  18444. (setq res
  18445. (cond
  18446. ((not res) "")
  18447. ((stringp res) res)
  18448. (t (prin1-to-string res))))
  18449. (while (string-match "," res)
  18450. (setq res (replace-match ";" t t res)))
  18451. (org-trim res)))
  18452. ;;;###autoload
  18453. (defun org-store-agenda-views (&rest parameters)
  18454. (interactive)
  18455. (eval (list 'org-batch-store-agenda-views)))
  18456. ;; FIXME, why is this a macro?????
  18457. ;;;###autoload
  18458. (defmacro org-batch-store-agenda-views (&rest parameters)
  18459. "Run all custom agenda commands that have a file argument."
  18460. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18461. (pop-up-frames nil)
  18462. (dir default-directory)
  18463. pars cmd thiscmdkey files opts)
  18464. (while parameters
  18465. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18466. (setq pars (reverse pars))
  18467. (save-window-excursion
  18468. (while cmds
  18469. (setq cmd (pop cmds)
  18470. thiscmdkey (car cmd)
  18471. opts (nth 4 cmd)
  18472. files (nth 5 cmd))
  18473. (if (stringp files) (setq files (list files)))
  18474. (when files
  18475. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18476. (list 'org-agenda nil thiscmdkey)))
  18477. (set-buffer org-agenda-buffer-name)
  18478. (while files
  18479. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18480. (list 'org-write-agenda
  18481. (expand-file-name (pop files) dir) t))))
  18482. (and (get-buffer org-agenda-buffer-name)
  18483. (kill-buffer org-agenda-buffer-name)))))))
  18484. (defun org-write-agenda (file &optional nosettings)
  18485. "Write the current buffer (an agenda view) as a file.
  18486. Depending on the extension of the file name, plain text (.txt),
  18487. HTML (.html or .htm) or Postscript (.ps) is produced.
  18488. If the extension is .ics, run icalendar export over all files used
  18489. to construct the agenda and limit the export to entries listed in the
  18490. agenda now.
  18491. If NOSETTINGS is given, do not scope the settings of
  18492. `org-agenda-exporter-settings' into the export commands. This is used when
  18493. the settings have already been scoped and we do not wish to overrule other,
  18494. higher priority settings."
  18495. (interactive "FWrite agenda to file: ")
  18496. (if (not (file-writable-p file))
  18497. (error "Cannot write agenda to file %s" file))
  18498. (cond
  18499. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18500. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18501. (org-let (if nosettings nil org-agenda-exporter-settings)
  18502. '(save-excursion
  18503. (save-window-excursion
  18504. (cond
  18505. ((string-match "\\.html?\\'" file)
  18506. (set-buffer (htmlize-buffer (current-buffer)))
  18507. (when (and org-agenda-export-html-style
  18508. (string-match "<style>" org-agenda-export-html-style))
  18509. ;; replace <style> section with org-agenda-export-html-style
  18510. (goto-char (point-min))
  18511. (kill-region (- (search-forward "<style") 6)
  18512. (search-forward "</style>"))
  18513. (insert org-agenda-export-html-style))
  18514. (write-file file)
  18515. (kill-buffer (current-buffer))
  18516. (message "HTML written to %s" file))
  18517. ((string-match "\\.ps\\'" file)
  18518. (ps-print-buffer-with-faces file)
  18519. (message "Postscript written to %s" file))
  18520. ((string-match "\\.ics\\'" file)
  18521. (let ((org-agenda-marker-table
  18522. (org-create-marker-find-array
  18523. (org-agenda-collect-markers)))
  18524. (org-icalendar-verify-function 'org-check-agenda-marker-table)
  18525. (org-combined-agenda-icalendar-file file))
  18526. (apply 'org-export-icalendar 'combine (org-agenda-files))))
  18527. (t
  18528. (let ((bs (buffer-string)))
  18529. (find-file file)
  18530. (insert bs)
  18531. (save-buffer 0)
  18532. (kill-buffer (current-buffer))
  18533. (message "Plain text written to %s" file))))))
  18534. (set-buffer org-agenda-buffer-name)))
  18535. (defun org-agenda-collect-markers ()
  18536. "Collect the markers pointing to entries in the agenda buffer."
  18537. (let (m markers)
  18538. (save-excursion
  18539. (goto-char (point-min))
  18540. (while (not (eobp))
  18541. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  18542. (get-text-property (point) 'org-marker)))
  18543. (push m markers))
  18544. (beginning-of-line 2)))
  18545. (nreverse markers)))
  18546. (defun org-create-marker-find-array (marker-list)
  18547. "Create a alist of files names with all marker positions in that file."
  18548. (let (f tbl m a p)
  18549. (while (setq m (pop marker-list))
  18550. (setq p (marker-position m)
  18551. f (buffer-file-name (or (buffer-base-buffer
  18552. (marker-buffer m))
  18553. (marker-buffer m))))
  18554. (if (setq a (assoc f tbl))
  18555. (push (marker-position m) (cdr a))
  18556. (push (list f p) tbl)))
  18557. (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
  18558. tbl)))
  18559. (defvar org-agenda-marker-table nil) ; dyamically scoped parameter
  18560. (defun org-check-agenda-marker-table ()
  18561. "Check of the current entry is on the marker list."
  18562. (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  18563. a)
  18564. (and (setq a (assoc file org-agenda-marker-table))
  18565. (save-match-data
  18566. (save-excursion
  18567. (org-back-to-heading t)
  18568. (member (point) (cdr a)))))))
  18569. (defmacro org-no-read-only (&rest body)
  18570. "Inhibit read-only for BODY."
  18571. `(let ((inhibit-read-only t)) ,@body))
  18572. (defun org-check-for-org-mode ()
  18573. "Make sure current buffer is in org-mode. Error if not."
  18574. (or (org-mode-p)
  18575. (error "Cannot execute org-mode agenda command on buffer in %s."
  18576. major-mode)))
  18577. (defun org-fit-agenda-window ()
  18578. "Fit the window to the buffer size."
  18579. (and (memq org-agenda-window-setup '(reorganize-frame))
  18580. (fboundp 'fit-window-to-buffer)
  18581. (fit-window-to-buffer
  18582. nil
  18583. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18584. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18585. ;;; Agenda file list
  18586. (defun org-agenda-files (&optional unrestricted)
  18587. "Get the list of agenda files.
  18588. Optional UNRESTRICTED means return the full list even if a restriction
  18589. is currently in place."
  18590. (let ((files
  18591. (cond
  18592. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18593. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18594. ((listp org-agenda-files) org-agenda-files)
  18595. (t (error "Invalid value of `org-agenda-files'")))))
  18596. (setq files (apply 'append
  18597. (mapcar (lambda (f)
  18598. (if (file-directory-p f)
  18599. (directory-files f t
  18600. org-agenda-file-regexp)
  18601. (list f)))
  18602. files)))
  18603. (if org-agenda-skip-unavailable-files
  18604. (delq nil
  18605. (mapcar (function
  18606. (lambda (file)
  18607. (and (file-readable-p file) file)))
  18608. files))
  18609. files))) ; `org-check-agenda-file' will remove them from the list
  18610. (defun org-edit-agenda-file-list ()
  18611. "Edit the list of agenda files.
  18612. Depending on setup, this either uses customize to edit the variable
  18613. `org-agenda-files', or it visits the file that is holding the list. In the
  18614. latter case, the buffer is set up in a way that saving it automatically kills
  18615. the buffer and restores the previous window configuration."
  18616. (interactive)
  18617. (if (stringp org-agenda-files)
  18618. (let ((cw (current-window-configuration)))
  18619. (find-file org-agenda-files)
  18620. (org-set-local 'org-window-configuration cw)
  18621. (org-add-hook 'after-save-hook
  18622. (lambda ()
  18623. (set-window-configuration
  18624. (prog1 org-window-configuration
  18625. (kill-buffer (current-buffer))))
  18626. (org-install-agenda-files-menu)
  18627. (message "New agenda file list installed"))
  18628. nil 'local)
  18629. (message "%s" (substitute-command-keys
  18630. "Edit list and finish with \\[save-buffer]")))
  18631. (customize-variable 'org-agenda-files)))
  18632. (defun org-store-new-agenda-file-list (list)
  18633. "Set new value for the agenda file list and save it correcly."
  18634. (if (stringp org-agenda-files)
  18635. (let ((f org-agenda-files) b)
  18636. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18637. (with-temp-file f
  18638. (insert (mapconcat 'identity list "\n") "\n")))
  18639. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18640. (setq org-agenda-files list)
  18641. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18642. (defun org-read-agenda-file-list ()
  18643. "Read the list of agenda files from a file."
  18644. (when (stringp org-agenda-files)
  18645. (with-temp-buffer
  18646. (insert-file-contents org-agenda-files)
  18647. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18648. ;;;###autoload
  18649. (defun org-cycle-agenda-files ()
  18650. "Cycle through the files in `org-agenda-files'.
  18651. If the current buffer visits an agenda file, find the next one in the list.
  18652. If the current buffer does not, find the first agenda file."
  18653. (interactive)
  18654. (let* ((fs (org-agenda-files t))
  18655. (files (append fs (list (car fs))))
  18656. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18657. file)
  18658. (unless files (error "No agenda files"))
  18659. (catch 'exit
  18660. (while (setq file (pop files))
  18661. (if (equal (file-truename file) tcf)
  18662. (when (car files)
  18663. (find-file (car files))
  18664. (throw 'exit t))))
  18665. (find-file (car fs)))
  18666. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18667. (defun org-agenda-file-to-front (&optional to-end)
  18668. "Move/add the current file to the top of the agenda file list.
  18669. If the file is not present in the list, it is added to the front. If it is
  18670. present, it is moved there. With optional argument TO-END, add/move to the
  18671. end of the list."
  18672. (interactive "P")
  18673. (let ((org-agenda-skip-unavailable-files nil)
  18674. (file-alist (mapcar (lambda (x)
  18675. (cons (file-truename x) x))
  18676. (org-agenda-files t)))
  18677. (ctf (file-truename buffer-file-name))
  18678. x had)
  18679. (setq x (assoc ctf file-alist) had x)
  18680. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18681. (if to-end
  18682. (setq file-alist (append (delq x file-alist) (list x)))
  18683. (setq file-alist (cons x (delq x file-alist))))
  18684. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18685. (org-install-agenda-files-menu)
  18686. (message "File %s to %s of agenda file list"
  18687. (if had "moved" "added") (if to-end "end" "front"))))
  18688. (defun org-remove-file (&optional file)
  18689. "Remove current file from the list of files in variable `org-agenda-files'.
  18690. These are the files which are being checked for agenda entries.
  18691. Optional argument FILE means, use this file instead of the current."
  18692. (interactive)
  18693. (let* ((org-agenda-skip-unavailable-files nil)
  18694. (file (or file buffer-file-name))
  18695. (true-file (file-truename file))
  18696. (afile (abbreviate-file-name file))
  18697. (files (delq nil (mapcar
  18698. (lambda (x)
  18699. (if (equal true-file
  18700. (file-truename x))
  18701. nil x))
  18702. (org-agenda-files t)))))
  18703. (if (not (= (length files) (length (org-agenda-files t))))
  18704. (progn
  18705. (org-store-new-agenda-file-list files)
  18706. (org-install-agenda-files-menu)
  18707. (message "Removed file: %s" afile))
  18708. (message "File was not in list: %s (not removed)" afile))))
  18709. (defun org-file-menu-entry (file)
  18710. (vector file (list 'find-file file) t))
  18711. (defun org-check-agenda-file (file)
  18712. "Make sure FILE exists. If not, ask user what to do."
  18713. (when (not (file-exists-p file))
  18714. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18715. (abbreviate-file-name file))
  18716. (let ((r (downcase (read-char-exclusive))))
  18717. (cond
  18718. ((equal r ?r)
  18719. (org-remove-file file)
  18720. (throw 'nextfile t))
  18721. (t (error "Abort"))))))
  18722. ;;; Agenda prepare and finalize
  18723. (defvar org-agenda-multi nil) ; dynammically scoped
  18724. (defvar org-agenda-buffer-name "*Org Agenda*")
  18725. (defvar org-pre-agenda-window-conf nil)
  18726. (defvar org-agenda-name nil)
  18727. (defun org-prepare-agenda (&optional name)
  18728. (setq org-todo-keywords-for-agenda nil)
  18729. (setq org-done-keywords-for-agenda nil)
  18730. (if org-agenda-multi
  18731. (progn
  18732. (setq buffer-read-only nil)
  18733. (goto-char (point-max))
  18734. (unless (or (bobp) org-agenda-compact-blocks)
  18735. (insert "\n" (make-string (window-width) ?=) "\n"))
  18736. (narrow-to-region (point) (point-max)))
  18737. (org-agenda-reset-markers)
  18738. (org-prepare-agenda-buffers (org-agenda-files))
  18739. (setq org-todo-keywords-for-agenda
  18740. (org-uniquify org-todo-keywords-for-agenda))
  18741. (setq org-done-keywords-for-agenda
  18742. (org-uniquify org-done-keywords-for-agenda))
  18743. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18744. (awin (get-buffer-window abuf)))
  18745. (cond
  18746. ((equal (current-buffer) abuf) nil)
  18747. (awin (select-window awin))
  18748. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18749. ((equal org-agenda-window-setup 'current-window)
  18750. (switch-to-buffer abuf))
  18751. ((equal org-agenda-window-setup 'other-window)
  18752. (org-switch-to-buffer-other-window abuf))
  18753. ((equal org-agenda-window-setup 'other-frame)
  18754. (switch-to-buffer-other-frame abuf))
  18755. ((equal org-agenda-window-setup 'reorganize-frame)
  18756. (delete-other-windows)
  18757. (org-switch-to-buffer-other-window abuf))))
  18758. (setq buffer-read-only nil)
  18759. (erase-buffer)
  18760. (org-agenda-mode)
  18761. (and name (not org-agenda-name)
  18762. (org-set-local 'org-agenda-name name)))
  18763. (setq buffer-read-only nil))
  18764. (defun org-finalize-agenda ()
  18765. "Finishing touch for the agenda buffer, called just before displaying it."
  18766. (unless org-agenda-multi
  18767. (save-excursion
  18768. (let ((inhibit-read-only t))
  18769. (goto-char (point-min))
  18770. (while (org-activate-bracket-links (point-max))
  18771. (add-text-properties (match-beginning 0) (match-end 0)
  18772. '(face org-link)))
  18773. (org-agenda-align-tags)
  18774. (unless org-agenda-with-colors
  18775. (remove-text-properties (point-min) (point-max) '(face nil))))
  18776. (if (and (boundp 'org-overriding-columns-format)
  18777. org-overriding-columns-format)
  18778. (org-set-local 'org-overriding-columns-format
  18779. org-overriding-columns-format))
  18780. (if (and (boundp 'org-agenda-view-columns-initially)
  18781. org-agenda-view-columns-initially)
  18782. (org-agenda-columns))
  18783. (when org-agenda-fontify-priorities
  18784. (org-fontify-priorities))
  18785. (run-hooks 'org-finalize-agenda-hook)
  18786. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18787. )))
  18788. (defun org-fontify-priorities ()
  18789. "Make highest priority lines bold, and lowest italic."
  18790. (interactive)
  18791. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18792. (org-delete-overlay o)))
  18793. (org-overlays-in (point-min) (point-max)))
  18794. (save-excursion
  18795. (let ((inhibit-read-only t)
  18796. b e p ov h l)
  18797. (goto-char (point-min))
  18798. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18799. (setq h (or (get-char-property (point) 'org-highest-priority)
  18800. org-highest-priority)
  18801. l (or (get-char-property (point) 'org-lowest-priority)
  18802. org-lowest-priority)
  18803. p (string-to-char (match-string 1))
  18804. b (match-beginning 0) e (point-at-eol)
  18805. ov (org-make-overlay b e))
  18806. (org-overlay-put
  18807. ov 'face
  18808. (cond ((listp org-agenda-fontify-priorities)
  18809. (cdr (assoc p org-agenda-fontify-priorities)))
  18810. ((equal p l) 'italic)
  18811. ((equal p h) 'bold)))
  18812. (org-overlay-put ov 'org-type 'org-priority)))))
  18813. (defun org-prepare-agenda-buffers (files)
  18814. "Create buffers for all agenda files, protect archived trees and comments."
  18815. (interactive)
  18816. (let ((pa '(:org-archived t))
  18817. (pc '(:org-comment t))
  18818. (pall '(:org-archived t :org-comment t))
  18819. (inhibit-read-only t)
  18820. (rea (concat ":" org-archive-tag ":"))
  18821. bmp file re)
  18822. (save-excursion
  18823. (save-restriction
  18824. (while (setq file (pop files))
  18825. (if (bufferp file)
  18826. (set-buffer file)
  18827. (org-check-agenda-file file)
  18828. (set-buffer (org-get-agenda-file-buffer file)))
  18829. (widen)
  18830. (setq bmp (buffer-modified-p))
  18831. (org-refresh-category-properties)
  18832. (setq org-todo-keywords-for-agenda
  18833. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18834. (setq org-done-keywords-for-agenda
  18835. (append org-done-keywords-for-agenda org-done-keywords))
  18836. (save-excursion
  18837. (remove-text-properties (point-min) (point-max) pall)
  18838. (when org-agenda-skip-archived-trees
  18839. (goto-char (point-min))
  18840. (while (re-search-forward rea nil t)
  18841. (if (org-on-heading-p t)
  18842. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18843. (goto-char (point-min))
  18844. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18845. (while (re-search-forward re nil t)
  18846. (add-text-properties
  18847. (match-beginning 0) (org-end-of-subtree t) pc)))
  18848. (set-buffer-modified-p bmp))))))
  18849. (defvar org-agenda-skip-function nil
  18850. "Function to be called at each match during agenda construction.
  18851. If this function returns nil, the current match should not be skipped.
  18852. Otherwise, the function must return a position from where the search
  18853. should be continued.
  18854. This may also be a Lisp form, it will be evaluated.
  18855. Never set this variable using `setq' or so, because then it will apply
  18856. to all future agenda commands. Instead, bind it with `let' to scope
  18857. it dynamically into the agenda-constructing command. A good way to set
  18858. it is through options in org-agenda-custom-commands.")
  18859. (defun org-agenda-skip ()
  18860. "Throw to `:skip' in places that should be skipped.
  18861. Also moves point to the end of the skipped region, so that search can
  18862. continue from there."
  18863. (let ((p (point-at-bol)) to fp)
  18864. (and org-agenda-skip-archived-trees
  18865. (get-text-property p :org-archived)
  18866. (org-end-of-subtree t)
  18867. (throw :skip t))
  18868. (and (get-text-property p :org-comment)
  18869. (org-end-of-subtree t)
  18870. (throw :skip t))
  18871. (if (equal (char-after p) ?#) (throw :skip t))
  18872. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18873. (consp org-agenda-skip-function))
  18874. (setq to (save-excursion
  18875. (save-match-data
  18876. (if fp
  18877. (funcall org-agenda-skip-function)
  18878. (eval org-agenda-skip-function))))))
  18879. (goto-char to)
  18880. (throw :skip t))))
  18881. (defvar org-agenda-markers nil
  18882. "List of all currently active markers created by `org-agenda'.")
  18883. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18884. "Creation time of the last agenda marker.")
  18885. (defun org-agenda-new-marker (&optional pos)
  18886. "Return a new agenda marker.
  18887. Org-mode keeps a list of these markers and resets them when they are
  18888. no longer in use."
  18889. (let ((m (copy-marker (or pos (point)))))
  18890. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18891. (push m org-agenda-markers)
  18892. m))
  18893. (defun org-agenda-reset-markers ()
  18894. "Reset markers created by `org-agenda'."
  18895. (while org-agenda-markers
  18896. (move-marker (pop org-agenda-markers) nil)))
  18897. (defun org-get-agenda-file-buffer (file)
  18898. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18899. it to the list of buffers which might be released later."
  18900. (let ((buf (org-find-base-buffer-visiting file)))
  18901. (if buf
  18902. buf ; just return it
  18903. ;; Make a new buffer and remember it
  18904. (setq buf (find-file-noselect file))
  18905. (if buf (push buf org-agenda-new-buffers))
  18906. buf)))
  18907. (defun org-release-buffers (blist)
  18908. "Release all buffers in list, asking the user for confirmation when needed.
  18909. When a buffer is unmodified, it is just killed. When modified, it is saved
  18910. \(if the user agrees) and then killed."
  18911. (let (buf file)
  18912. (while (setq buf (pop blist))
  18913. (setq file (buffer-file-name buf))
  18914. (when (and (buffer-modified-p buf)
  18915. file
  18916. (y-or-n-p (format "Save file %s? " file)))
  18917. (with-current-buffer buf (save-buffer)))
  18918. (kill-buffer buf))))
  18919. (defun org-get-category (&optional pos)
  18920. "Get the category applying to position POS."
  18921. (get-text-property (or pos (point)) 'org-category))
  18922. ;;; Agenda timeline
  18923. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18924. (defun org-timeline (&optional include-all)
  18925. "Show a time-sorted view of the entries in the current org file.
  18926. Only entries with a time stamp of today or later will be listed. With
  18927. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18928. under the current date.
  18929. If the buffer contains an active region, only check the region for
  18930. dates."
  18931. (interactive "P")
  18932. (require 'calendar)
  18933. (org-compile-prefix-format 'timeline)
  18934. (org-set-sorting-strategy 'timeline)
  18935. (let* ((dopast t)
  18936. (dotodo include-all)
  18937. (doclosed org-agenda-show-log)
  18938. (entry buffer-file-name)
  18939. (date (calendar-current-date))
  18940. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18941. (end (if (org-region-active-p) (region-end) (point-max)))
  18942. (day-numbers (org-get-all-dates beg end 'no-ranges
  18943. t doclosed ; always include today
  18944. org-timeline-show-empty-dates))
  18945. (org-deadline-warning-days 0)
  18946. (org-agenda-only-exact-dates t)
  18947. (today (time-to-days (current-time)))
  18948. (past t)
  18949. args
  18950. s e rtn d emptyp)
  18951. (setq org-agenda-redo-command
  18952. (list 'progn
  18953. (list 'org-switch-to-buffer-other-window (current-buffer))
  18954. (list 'org-timeline (list 'quote include-all))))
  18955. (if (not dopast)
  18956. ;; Remove past dates from the list of dates.
  18957. (setq day-numbers (delq nil (mapcar (lambda(x)
  18958. (if (>= x today) x nil))
  18959. day-numbers))))
  18960. (org-prepare-agenda (concat "Timeline "
  18961. (file-name-nondirectory buffer-file-name)))
  18962. (if doclosed (push :closed args))
  18963. (push :timestamp args)
  18964. (push :deadline args)
  18965. (push :scheduled args)
  18966. (push :sexp args)
  18967. (if dotodo (push :todo args))
  18968. (while (setq d (pop day-numbers))
  18969. (if (and (listp d) (eq (car d) :omitted))
  18970. (progn
  18971. (setq s (point))
  18972. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18973. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18974. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18975. (if (and (>= d today)
  18976. dopast
  18977. past)
  18978. (progn
  18979. (setq past nil)
  18980. (insert (make-string 79 ?-) "\n")))
  18981. (setq date (calendar-gregorian-from-absolute d))
  18982. (setq s (point))
  18983. (setq rtn (and (not emptyp)
  18984. (apply 'org-agenda-get-day-entries entry
  18985. date args)))
  18986. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18987. (progn
  18988. (insert
  18989. (if (stringp org-agenda-format-date)
  18990. (format-time-string org-agenda-format-date
  18991. (org-time-from-absolute date))
  18992. (funcall org-agenda-format-date date))
  18993. "\n")
  18994. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18995. (put-text-property s (1- (point)) 'org-date-line t)
  18996. (if (equal d today)
  18997. (put-text-property s (1- (point)) 'org-today t))
  18998. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18999. (put-text-property s (1- (point)) 'day d)))))
  19000. (goto-char (point-min))
  19001. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19002. (point-min)))
  19003. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19004. (org-finalize-agenda)
  19005. (setq buffer-read-only t)))
  19006. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19007. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19008. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19009. not every single day in the range. If FORCE-TODAY is non-nil, make
  19010. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19011. inactive time stamps (those in square brackets) are included.
  19012. When EMPTY is non-nil, also include days without any entries."
  19013. (let ((re (concat
  19014. (if pre-re pre-re "")
  19015. (if inactive org-ts-regexp-both org-ts-regexp)))
  19016. dates dates1 date day day1 day2 ts1 ts2)
  19017. (if force-today
  19018. (setq dates (list (time-to-days (current-time)))))
  19019. (save-excursion
  19020. (goto-char beg)
  19021. (while (re-search-forward re end t)
  19022. (setq day (time-to-days (org-time-string-to-time
  19023. (substring (match-string 1) 0 10))))
  19024. (or (memq day dates) (push day dates)))
  19025. (unless no-ranges
  19026. (goto-char beg)
  19027. (while (re-search-forward org-tr-regexp end t)
  19028. (setq ts1 (substring (match-string 1) 0 10)
  19029. ts2 (substring (match-string 2) 0 10)
  19030. day1 (time-to-days (org-time-string-to-time ts1))
  19031. day2 (time-to-days (org-time-string-to-time ts2)))
  19032. (while (< (setq day1 (1+ day1)) day2)
  19033. (or (memq day1 dates) (push day1 dates)))))
  19034. (setq dates (sort dates '<))
  19035. (when empty
  19036. (while (setq day (pop dates))
  19037. (setq day2 (car dates))
  19038. (push day dates1)
  19039. (when (and day2 empty)
  19040. (if (or (eq empty t)
  19041. (and (numberp empty) (<= (- day2 day) empty)))
  19042. (while (< (setq day (1+ day)) day2)
  19043. (push (list day) dates1))
  19044. (push (cons :omitted (- day2 day)) dates1))))
  19045. (setq dates (nreverse dates1)))
  19046. dates)))
  19047. ;;; Agenda Daily/Weekly
  19048. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19049. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19050. (defvar org-agenda-last-arguments nil
  19051. "The arguments of the previous call to org-agenda")
  19052. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19053. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19054. (defvar org-include-all-loc nil) ; local variable
  19055. (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
  19056. ;;;###autoload
  19057. (defun org-agenda-list (&optional include-all start-day ndays)
  19058. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19059. The view will be for the current day or week, but from the overview buffer
  19060. you will be able to go to other days/weeks.
  19061. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19062. all unfinished TODO items will also be shown, before the agenda.
  19063. This feature is considered obsolete, please use the TODO list or a block
  19064. agenda instead.
  19065. With a numeric prefix argument in an interactive call, the agenda will
  19066. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19067. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19068. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19069. given in `org-agenda-start-on-weekday'."
  19070. (interactive "P")
  19071. (if (and (integerp include-all) (> include-all 0))
  19072. (setq ndays include-all include-all nil))
  19073. (setq ndays (or ndays org-agenda-ndays)
  19074. start-day (or start-day org-agenda-start-day))
  19075. (if org-agenda-overriding-arguments
  19076. (setq include-all (car org-agenda-overriding-arguments)
  19077. start-day (nth 1 org-agenda-overriding-arguments)
  19078. ndays (nth 2 org-agenda-overriding-arguments)))
  19079. (if (stringp start-day)
  19080. ;; Convert to an absolute day number
  19081. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19082. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19083. (org-compile-prefix-format 'agenda)
  19084. (org-set-sorting-strategy 'agenda)
  19085. (require 'calendar)
  19086. (let* ((org-agenda-start-on-weekday
  19087. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19088. org-agenda-start-on-weekday nil))
  19089. (thefiles (org-agenda-files))
  19090. (files thefiles)
  19091. (today (time-to-days
  19092. (time-subtract (current-time)
  19093. (list 0 (* 3600 org-extend-today-until) 0))))
  19094. (sd (or start-day today))
  19095. (start (if (or (null org-agenda-start-on-weekday)
  19096. (< org-agenda-ndays 7))
  19097. sd
  19098. (let* ((nt (calendar-day-of-week
  19099. (calendar-gregorian-from-absolute sd)))
  19100. (n1 org-agenda-start-on-weekday)
  19101. (d (- nt n1)))
  19102. (- sd (+ (if (< d 0) 7 0) d)))))
  19103. (day-numbers (list start))
  19104. (day-cnt 0)
  19105. (inhibit-redisplay (not debug-on-error))
  19106. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19107. (setq org-agenda-redo-command
  19108. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19109. ;; Make the list of days
  19110. (setq ndays (or ndays org-agenda-ndays)
  19111. nd ndays)
  19112. (while (> ndays 1)
  19113. (push (1+ (car day-numbers)) day-numbers)
  19114. (setq ndays (1- ndays)))
  19115. (setq day-numbers (nreverse day-numbers))
  19116. (org-prepare-agenda "Day/Week")
  19117. (org-set-local 'org-starting-day (car day-numbers))
  19118. (org-set-local 'org-include-all-loc include-all)
  19119. (org-set-local 'org-agenda-span
  19120. (org-agenda-ndays-to-span nd))
  19121. (when (and (or include-all org-agenda-include-all-todo)
  19122. (member today day-numbers))
  19123. (setq files thefiles
  19124. rtnall nil)
  19125. (while (setq file (pop files))
  19126. (catch 'nextfile
  19127. (org-check-agenda-file file)
  19128. (setq date (calendar-gregorian-from-absolute today)
  19129. rtn (org-agenda-get-day-entries
  19130. file date :todo))
  19131. (setq rtnall (append rtnall rtn))))
  19132. (when rtnall
  19133. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19134. (add-text-properties (point-min) (1- (point))
  19135. (list 'face 'org-agenda-structure))
  19136. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19137. (unless org-agenda-compact-blocks
  19138. (let* ((d1 (car day-numbers))
  19139. (d2 (org-last day-numbers))
  19140. (w1 (org-days-to-iso-week d1))
  19141. (w2 (org-days-to-iso-week d2)))
  19142. (setq s (point))
  19143. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19144. "-agenda"
  19145. (if (< (- d2 d1) 350)
  19146. (if (= w1 w2)
  19147. (format " (W%02d)" w1)
  19148. (format " (W%02d-W%02d)" w1 w2))
  19149. "")
  19150. ":\n"))
  19151. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19152. 'org-date-line t)))
  19153. (while (setq d (pop day-numbers))
  19154. (setq date (calendar-gregorian-from-absolute d)
  19155. s (point))
  19156. (if (or (setq todayp (= d today))
  19157. (and (not start-pos) (= d sd)))
  19158. (setq start-pos (point))
  19159. (if (and start-pos (not end-pos))
  19160. (setq end-pos (point))))
  19161. (setq files thefiles
  19162. rtnall nil)
  19163. (while (setq file (pop files))
  19164. (catch 'nextfile
  19165. (org-check-agenda-file file)
  19166. (if org-agenda-show-log
  19167. (setq rtn (org-agenda-get-day-entries
  19168. file date
  19169. :deadline :scheduled :timestamp :sexp :closed))
  19170. (setq rtn (org-agenda-get-day-entries
  19171. file date
  19172. :deadline :scheduled :sexp :timestamp)))
  19173. (setq rtnall (append rtnall rtn))))
  19174. (if org-agenda-include-diary
  19175. (progn
  19176. (require 'diary-lib)
  19177. (setq rtn (org-get-entries-from-diary date))
  19178. (setq rtnall (append rtnall rtn))))
  19179. (if (or rtnall org-agenda-show-all-dates)
  19180. (progn
  19181. (setq day-cnt (1+ day-cnt))
  19182. (insert
  19183. (if (stringp org-agenda-format-date)
  19184. (format-time-string org-agenda-format-date
  19185. (org-time-from-absolute date))
  19186. (funcall org-agenda-format-date date))
  19187. "\n")
  19188. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19189. (put-text-property s (1- (point)) 'org-date-line t)
  19190. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19191. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19192. (if rtnall (insert
  19193. (org-finalize-agenda-entries
  19194. (org-agenda-add-time-grid-maybe
  19195. rtnall nd todayp))
  19196. "\n"))
  19197. (put-text-property s (1- (point)) 'day d)
  19198. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19199. (goto-char (point-min))
  19200. (org-fit-agenda-window)
  19201. (unless (and (pos-visible-in-window-p (point-min))
  19202. (pos-visible-in-window-p (point-max)))
  19203. (goto-char (1- (point-max)))
  19204. (recenter -1)
  19205. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19206. (progn
  19207. (goto-char (or start-pos 1))
  19208. (recenter 1))))
  19209. (goto-char (or start-pos 1))
  19210. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19211. (org-finalize-agenda)
  19212. (setq buffer-read-only t)
  19213. (message "")))
  19214. (defun org-agenda-ndays-to-span (n)
  19215. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19216. ;;; Agenda word search
  19217. (defvar org-agenda-search-history nil)
  19218. (defvar org-todo-only nil)
  19219. (defvar org-search-syntax-table nil
  19220. "Special syntax table for org-mode search.
  19221. In this table, we have single quotes not as word constituents, to
  19222. that when \"+Ameli\" is searchd as a work, it will also match \"Ameli's\"")
  19223. (defun org-search-syntax-table ()
  19224. (unless org-search-syntax-table
  19225. (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
  19226. (modify-syntax-entry ?' "." org-search-syntax-table)
  19227. (modify-syntax-entry ?` "." org-search-syntax-table))
  19228. org-search-syntax-table)
  19229. ;;;###autoload
  19230. (defun org-search-view (&optional todo-only string edit-at)
  19231. "Show all entries that contain words or regular expressions.
  19232. If the first character of the search string is an asterisks,
  19233. search only the headlines.
  19234. With optional prefix argument TODO-ONLY, only consider entries that are
  19235. TODO entries. The argument STRING can be used to pass a default search
  19236. string into this function. If EDIT-AT is non-nil, it means that the
  19237. user should get a chance to edit this string, with cursor at position
  19238. EDIT-AT.
  19239. The search string is broken into \"words\" by splitting at whitespace.
  19240. The individual words are then interpreted as a boolean expression with
  19241. logical AND. Words prefixed with a minus must not occur in the entry.
  19242. Words without a prefix or prefixed with a plus must occur in the entry.
  19243. Matching is case-insensitive and the words are enclosed by word delimiters.
  19244. Words enclosed by curly braces are interpreted as regular expressions
  19245. that must or must not match in the entry.
  19246. If the search string starts with an asterisk, search only in headlines.
  19247. If (possibly after the leading star) the search string starts with an
  19248. exclamation mark, this also means to look at TODO entries only, an effect
  19249. that can also be achieved with a prefix argument.
  19250. This command searches the agenda files, and in addition the files listed
  19251. in `org-agenda-text-search-extra-files'."
  19252. (interactive "P")
  19253. (org-compile-prefix-format 'search)
  19254. (org-set-sorting-strategy 'search)
  19255. (org-prepare-agenda "SEARCH")
  19256. (let* ((props (list 'face nil
  19257. 'done-face 'org-done
  19258. 'org-not-done-regexp org-not-done-regexp
  19259. 'org-todo-regexp org-todo-regexp
  19260. 'mouse-face 'highlight
  19261. 'keymap org-agenda-keymap
  19262. 'help-echo (format "mouse-2 or RET jump to location")))
  19263. regexp rtn rtnall files file pos
  19264. marker priority category tags c neg re
  19265. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19266. (unless (and (not edit-at)
  19267. (stringp string)
  19268. (string-match "\\S-" string))
  19269. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19270. (cond
  19271. ((integerp edit-at) (cons string edit-at))
  19272. (edit-at string))
  19273. 'org-agenda-search-history)))
  19274. (org-set-local 'org-todo-only todo-only)
  19275. (setq org-agenda-redo-command
  19276. (list 'org-search-view (if todo-only t nil) string
  19277. '(if current-prefix-arg 1 nil)))
  19278. (setq org-agenda-query-string string)
  19279. (if (equal (string-to-char string) ?*)
  19280. (setq hdl-only t
  19281. words (substring string 1))
  19282. (setq words string))
  19283. (when (equal (string-to-char words) ?!)
  19284. (setq todo-only t
  19285. words (substring words 1)))
  19286. (setq words (org-split-string words))
  19287. (mapc (lambda (w)
  19288. (setq c (string-to-char w))
  19289. (if (equal c ?-)
  19290. (setq neg t w (substring w 1))
  19291. (if (equal c ?+)
  19292. (setq neg nil w (substring w 1))
  19293. (setq neg nil)))
  19294. (if (string-match "\\`{.*}\\'" w)
  19295. (setq re (substring w 1 -1))
  19296. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19297. (if neg (push re regexps-) (push re regexps+)))
  19298. words)
  19299. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19300. (if (not regexps+)
  19301. (setq regexp (concat "^" org-outline-regexp))
  19302. (setq regexp (pop regexps+))
  19303. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19304. regexp))))
  19305. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19306. rtnall nil)
  19307. (while (setq file (pop files))
  19308. (setq ee nil)
  19309. (catch 'nextfile
  19310. (org-check-agenda-file file)
  19311. (setq buffer (if (file-exists-p file)
  19312. (org-get-agenda-file-buffer file)
  19313. (error "No such file %s" file)))
  19314. (if (not buffer)
  19315. ;; If file does not exist, make sure an error message is sent
  19316. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19317. file))))
  19318. (with-current-buffer buffer
  19319. (with-syntax-table (org-search-syntax-table)
  19320. (unless (org-mode-p)
  19321. (error "Agenda file %s is not in `org-mode'" file))
  19322. (let ((case-fold-search t))
  19323. (save-excursion
  19324. (save-restriction
  19325. (if org-agenda-restrict
  19326. (narrow-to-region org-agenda-restrict-begin
  19327. org-agenda-restrict-end)
  19328. (widen))
  19329. (goto-char (point-min))
  19330. (unless (or (org-on-heading-p)
  19331. (outline-next-heading))
  19332. (throw 'nextfile t))
  19333. (goto-char (max (point-min) (1- (point))))
  19334. (while (re-search-forward regexp nil t)
  19335. (org-back-to-heading t)
  19336. (skip-chars-forward "* ")
  19337. (setq beg (point-at-bol)
  19338. beg1 (point)
  19339. end (progn (outline-next-heading) (point)))
  19340. (catch :skip
  19341. (goto-char beg)
  19342. (org-agenda-skip)
  19343. (setq str (buffer-substring-no-properties
  19344. (point-at-bol)
  19345. (if hdl-only (point-at-eol) end)))
  19346. (mapc (lambda (wr) (when (string-match wr str)
  19347. (goto-char (1- end))
  19348. (throw :skip t)))
  19349. regexps-)
  19350. (mapc (lambda (wr) (unless (string-match wr str)
  19351. (goto-char (1- end))
  19352. (throw :skip t)))
  19353. (if todo-only
  19354. (cons (concat "^\*+[ \t]+" org-not-done-regexp)
  19355. regexps+)
  19356. regexps+))
  19357. (goto-char beg)
  19358. (setq marker (org-agenda-new-marker (point))
  19359. category (org-get-category)
  19360. tags (org-get-tags-at (point))
  19361. txt (org-format-agenda-item
  19362. ""
  19363. (buffer-substring-no-properties
  19364. beg1 (point-at-eol))
  19365. category tags))
  19366. (org-add-props txt props
  19367. 'org-marker marker 'org-hd-marker marker
  19368. 'org-todo-regexp org-todo-regexp
  19369. 'priority 1000 'org-category category
  19370. 'type "search")
  19371. (push txt ee)
  19372. (goto-char (1- end))))))))))
  19373. (setq rtn (nreverse ee))
  19374. (setq rtnall (append rtnall rtn)))
  19375. (if org-agenda-overriding-header
  19376. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19377. nil 'face 'org-agenda-structure) "\n")
  19378. (insert "Search words: ")
  19379. (add-text-properties (point-min) (1- (point))
  19380. (list 'face 'org-agenda-structure))
  19381. (setq pos (point))
  19382. (insert string "\n")
  19383. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19384. (setq pos (point))
  19385. (unless org-agenda-multi
  19386. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19387. (add-text-properties pos (1- (point))
  19388. (list 'face 'org-agenda-structure))))
  19389. (when rtnall
  19390. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19391. (goto-char (point-min))
  19392. (org-fit-agenda-window)
  19393. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19394. (org-finalize-agenda)
  19395. (setq buffer-read-only t)))
  19396. ;;; Agenda TODO list
  19397. (defvar org-select-this-todo-keyword nil)
  19398. (defvar org-last-arg nil)
  19399. ;;;###autoload
  19400. (defun org-todo-list (arg)
  19401. "Show all TODO entries from all agenda file in a single list.
  19402. The prefix arg can be used to select a specific TODO keyword and limit
  19403. the list to these. When using \\[universal-argument], you will be prompted
  19404. for a keyword. A numeric prefix directly selects the Nth keyword in
  19405. `org-todo-keywords-1'."
  19406. (interactive "P")
  19407. (require 'calendar)
  19408. (org-compile-prefix-format 'todo)
  19409. (org-set-sorting-strategy 'todo)
  19410. (org-prepare-agenda "TODO")
  19411. (let* ((today (time-to-days (current-time)))
  19412. (date (calendar-gregorian-from-absolute today))
  19413. (kwds org-todo-keywords-for-agenda)
  19414. (completion-ignore-case t)
  19415. (org-select-this-todo-keyword
  19416. (if (stringp arg) arg
  19417. (and arg (integerp arg) (> arg 0)
  19418. (nth (1- arg) kwds))))
  19419. rtn rtnall files file pos)
  19420. (when (equal arg '(4))
  19421. (setq org-select-this-todo-keyword
  19422. (completing-read "Keyword (or KWD1|K2D2|...): "
  19423. (mapcar 'list kwds) nil nil)))
  19424. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19425. (org-set-local 'org-last-arg arg)
  19426. (setq org-agenda-redo-command
  19427. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19428. (setq files (org-agenda-files)
  19429. rtnall nil)
  19430. (while (setq file (pop files))
  19431. (catch 'nextfile
  19432. (org-check-agenda-file file)
  19433. (setq rtn (org-agenda-get-day-entries file date :todo))
  19434. (setq rtnall (append rtnall rtn))))
  19435. (if org-agenda-overriding-header
  19436. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19437. nil 'face 'org-agenda-structure) "\n")
  19438. (insert "Global list of TODO items of type: ")
  19439. (add-text-properties (point-min) (1- (point))
  19440. (list 'face 'org-agenda-structure))
  19441. (setq pos (point))
  19442. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19443. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19444. (setq pos (point))
  19445. (unless org-agenda-multi
  19446. (insert "Available with `N r': (0)ALL")
  19447. (let ((n 0) s)
  19448. (mapc (lambda (x)
  19449. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19450. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19451. (insert "\n "))
  19452. (insert " " s))
  19453. kwds))
  19454. (insert "\n"))
  19455. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19456. (when rtnall
  19457. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19458. (goto-char (point-min))
  19459. (org-fit-agenda-window)
  19460. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19461. (org-finalize-agenda)
  19462. (setq buffer-read-only t)))
  19463. ;;; Agenda tags match
  19464. ;;;###autoload
  19465. (defun org-tags-view (&optional todo-only match)
  19466. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19467. The prefix arg TODO-ONLY limits the search to TODO entries."
  19468. (interactive "P")
  19469. (org-compile-prefix-format 'tags)
  19470. (org-set-sorting-strategy 'tags)
  19471. (let* ((org-tags-match-list-sublevels
  19472. (if todo-only t org-tags-match-list-sublevels))
  19473. (completion-ignore-case t)
  19474. rtn rtnall files file pos matcher
  19475. buffer)
  19476. (setq matcher (org-make-tags-matcher match)
  19477. match (car matcher) matcher (cdr matcher))
  19478. (org-prepare-agenda (concat "TAGS " match))
  19479. (setq org-agenda-query-string match)
  19480. (setq org-agenda-redo-command
  19481. (list 'org-tags-view (list 'quote todo-only)
  19482. (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
  19483. (setq files (org-agenda-files)
  19484. rtnall nil)
  19485. (while (setq file (pop files))
  19486. (catch 'nextfile
  19487. (org-check-agenda-file file)
  19488. (setq buffer (if (file-exists-p file)
  19489. (org-get-agenda-file-buffer file)
  19490. (error "No such file %s" file)))
  19491. (if (not buffer)
  19492. ;; If file does not exist, merror message to agenda
  19493. (setq rtn (list
  19494. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19495. rtnall (append rtnall rtn))
  19496. (with-current-buffer buffer
  19497. (unless (org-mode-p)
  19498. (error "Agenda file %s is not in `org-mode'" file))
  19499. (save-excursion
  19500. (save-restriction
  19501. (if org-agenda-restrict
  19502. (narrow-to-region org-agenda-restrict-begin
  19503. org-agenda-restrict-end)
  19504. (widen))
  19505. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19506. (setq rtnall (append rtnall rtn))))))))
  19507. (if org-agenda-overriding-header
  19508. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19509. nil 'face 'org-agenda-structure) "\n")
  19510. (insert "Headlines with TAGS match: ")
  19511. (add-text-properties (point-min) (1- (point))
  19512. (list 'face 'org-agenda-structure))
  19513. (setq pos (point))
  19514. (insert match "\n")
  19515. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19516. (setq pos (point))
  19517. (unless org-agenda-multi
  19518. (insert "Press `C-u r' to search again with new search string\n"))
  19519. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19520. (when rtnall
  19521. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19522. (goto-char (point-min))
  19523. (org-fit-agenda-window)
  19524. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19525. (org-finalize-agenda)
  19526. (setq buffer-read-only t)))
  19527. ;;; Agenda Finding stuck projects
  19528. (defvar org-agenda-skip-regexp nil
  19529. "Regular expression used in skipping subtrees for the agenda.
  19530. This is basically a temporary global variable that can be set and then
  19531. used by user-defined selections using `org-agenda-skip-function'.")
  19532. (defvar org-agenda-overriding-header nil
  19533. "When this is set during todo and tags searches, will replace header.")
  19534. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19535. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19536. If yes, it returns the end position of this tree, causing agenda commands
  19537. to skip this subtree. This is a function that can be put into
  19538. `org-agenda-skip-function' for the duration of a command."
  19539. (let ((end (save-excursion (org-end-of-subtree t)))
  19540. skip)
  19541. (save-excursion
  19542. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19543. (and skip end)))
  19544. (defun org-agenda-skip-entry-if (&rest conditions)
  19545. "Skip entry if any of CONDITIONS is true.
  19546. See `org-agenda-skip-if' for details."
  19547. (org-agenda-skip-if nil conditions))
  19548. (defun org-agenda-skip-subtree-if (&rest conditions)
  19549. "Skip entry if any of CONDITIONS is true.
  19550. See `org-agenda-skip-if' for details."
  19551. (org-agenda-skip-if t conditions))
  19552. (defun org-agenda-skip-if (subtree conditions)
  19553. "Checks current entity for CONDITIONS.
  19554. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19555. the entry, i.e. the text before the next heading is checked.
  19556. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19557. from different tests. Valid conditions are:
  19558. scheduled Check if there is a scheduled cookie
  19559. notscheduled Check if there is no scheduled cookie
  19560. deadline Check if there is a deadline
  19561. notdeadline Check if there is no deadline
  19562. regexp Check if regexp matches
  19563. notregexp Check if regexp does not match.
  19564. The regexp is taken from the conditions list, it must come right after
  19565. the `regexp' or `notregexp' element.
  19566. If any of these conditions is met, this function returns the end point of
  19567. the entity, causing the search to continue from there. This is a function
  19568. that can be put into `org-agenda-skip-function' for the duration of a command."
  19569. (let (beg end m)
  19570. (org-back-to-heading t)
  19571. (setq beg (point)
  19572. end (if subtree
  19573. (progn (org-end-of-subtree t) (point))
  19574. (progn (outline-next-heading) (1- (point)))))
  19575. (goto-char beg)
  19576. (and
  19577. (or
  19578. (and (memq 'scheduled conditions)
  19579. (re-search-forward org-scheduled-time-regexp end t))
  19580. (and (memq 'notscheduled conditions)
  19581. (not (re-search-forward org-scheduled-time-regexp end t)))
  19582. (and (memq 'deadline conditions)
  19583. (re-search-forward org-deadline-time-regexp end t))
  19584. (and (memq 'notdeadline conditions)
  19585. (not (re-search-forward org-deadline-time-regexp end t)))
  19586. (and (setq m (memq 'regexp conditions))
  19587. (stringp (nth 1 m))
  19588. (re-search-forward (nth 1 m) end t))
  19589. (and (setq m (memq 'notregexp conditions))
  19590. (stringp (nth 1 m))
  19591. (not (re-search-forward (nth 1 m) end t))))
  19592. end)))
  19593. ;;;###autoload
  19594. (defun org-agenda-list-stuck-projects (&rest ignore)
  19595. "Create agenda view for projects that are stuck.
  19596. Stuck projects are project that have no next actions. For the definitions
  19597. of what a project is and how to check if it stuck, customize the variable
  19598. `org-stuck-projects'.
  19599. MATCH is being ignored."
  19600. (interactive)
  19601. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19602. ;; FIXME: we could have used org-agenda-skip-if here.
  19603. (org-agenda-overriding-header "List of stuck projects: ")
  19604. (matcher (nth 0 org-stuck-projects))
  19605. (todo (nth 1 org-stuck-projects))
  19606. (todo-wds (if (member "*" todo)
  19607. (progn
  19608. (org-prepare-agenda-buffers (org-agenda-files))
  19609. (org-delete-all
  19610. org-done-keywords-for-agenda
  19611. (copy-sequence org-todo-keywords-for-agenda)))
  19612. todo))
  19613. (todo-re (concat "^\\*+[ \t]+\\("
  19614. (mapconcat 'identity todo-wds "\\|")
  19615. "\\)\\>"))
  19616. (tags (nth 2 org-stuck-projects))
  19617. (tags-re (if (member "*" tags)
  19618. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19619. (concat "^\\*+ .*:\\("
  19620. (mapconcat 'identity tags "\\|")
  19621. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19622. (gen-re (nth 3 org-stuck-projects))
  19623. (re-list
  19624. (delq nil
  19625. (list
  19626. (if todo todo-re)
  19627. (if tags tags-re)
  19628. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19629. gen-re)))))
  19630. (setq org-agenda-skip-regexp
  19631. (if re-list
  19632. (mapconcat 'identity re-list "\\|")
  19633. (error "No information how to identify unstuck projects")))
  19634. (org-tags-view nil matcher)
  19635. (with-current-buffer org-agenda-buffer-name
  19636. (setq org-agenda-redo-command
  19637. '(org-agenda-list-stuck-projects
  19638. (or current-prefix-arg org-last-arg))))))
  19639. ;;; Diary integration
  19640. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19641. (defvar list-diary-entries-hook)
  19642. (defun org-get-entries-from-diary (date)
  19643. "Get the (Emacs Calendar) diary entries for DATE."
  19644. (require 'diary-lib)
  19645. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19646. (diary-display-hook '(fancy-diary-display))
  19647. (pop-up-frames nil)
  19648. (list-diary-entries-hook
  19649. (cons 'org-diary-default-entry list-diary-entries-hook))
  19650. (diary-file-name-prefix-function nil) ; turn this feature off
  19651. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19652. entries
  19653. (org-disable-agenda-to-diary t))
  19654. (save-excursion
  19655. (save-window-excursion
  19656. (funcall (if (fboundp 'diary-list-entries)
  19657. 'diary-list-entries 'list-diary-entries)
  19658. date 1)))
  19659. (if (not (get-buffer fancy-diary-buffer))
  19660. (setq entries nil)
  19661. (with-current-buffer fancy-diary-buffer
  19662. (setq buffer-read-only nil)
  19663. (if (zerop (buffer-size))
  19664. ;; No entries
  19665. (setq entries nil)
  19666. ;; Omit the date and other unnecessary stuff
  19667. (org-agenda-cleanup-fancy-diary)
  19668. ;; Add prefix to each line and extend the text properties
  19669. (if (zerop (buffer-size))
  19670. (setq entries nil)
  19671. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19672. (set-buffer-modified-p nil)
  19673. (kill-buffer fancy-diary-buffer)))
  19674. (when entries
  19675. (setq entries (org-split-string entries "\n"))
  19676. (setq entries
  19677. (mapcar
  19678. (lambda (x)
  19679. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19680. ;; Extend the text properties to the beginning of the line
  19681. (org-add-props x (text-properties-at (1- (length x)) x)
  19682. 'type "diary" 'date date))
  19683. entries)))))
  19684. (defun org-agenda-cleanup-fancy-diary ()
  19685. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19686. This gets rid of the date, the underline under the date, and
  19687. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19688. date. It also removes lines that contain only whitespace."
  19689. (goto-char (point-min))
  19690. (if (looking-at ".*?:[ \t]*")
  19691. (progn
  19692. (replace-match "")
  19693. (re-search-forward "\n=+$" nil t)
  19694. (replace-match "")
  19695. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19696. (re-search-forward "\n=+$" nil t)
  19697. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19698. (goto-char (point-min))
  19699. (while (re-search-forward "^ +\n" nil t)
  19700. (replace-match ""))
  19701. (goto-char (point-min))
  19702. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19703. (replace-match "")))
  19704. ;; Make sure entries from the diary have the right text properties.
  19705. (eval-after-load "diary-lib"
  19706. '(if (boundp 'diary-modify-entry-list-string-function)
  19707. ;; We can rely on the hook, nothing to do
  19708. nil
  19709. ;; Hook not avaiable, must use advice to make this work
  19710. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19711. "Make the position visible."
  19712. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19713. (stringp string)
  19714. buffer-file-name)
  19715. (setq string (org-modify-diary-entry-string string))))))
  19716. (defun org-modify-diary-entry-string (string)
  19717. "Add text properties to string, allowing org-mode to act on it."
  19718. (org-add-props string nil
  19719. 'mouse-face 'highlight
  19720. 'keymap org-agenda-keymap
  19721. 'help-echo (if buffer-file-name
  19722. (format "mouse-2 or RET jump to diary file %s"
  19723. (abbreviate-file-name buffer-file-name))
  19724. "")
  19725. 'org-agenda-diary-link t
  19726. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19727. (defun org-diary-default-entry ()
  19728. "Add a dummy entry to the diary.
  19729. Needed to avoid empty dates which mess up holiday display."
  19730. ;; Catch the error if dealing with the new add-to-diary-alist
  19731. (when org-disable-agenda-to-diary
  19732. (condition-case nil
  19733. (add-to-diary-list original-date "Org-mode dummy" "")
  19734. (error
  19735. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19736. ;;;###autoload
  19737. (defun org-diary (&rest args)
  19738. "Return diary information from org-files.
  19739. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19740. It accesses org files and extracts information from those files to be
  19741. listed in the diary. The function accepts arguments specifying what
  19742. items should be listed. The following arguments are allowed:
  19743. :timestamp List the headlines of items containing a date stamp or
  19744. date range matching the selected date. Deadlines will
  19745. also be listed, on the expiration day.
  19746. :sexp List entries resulting from diary-like sexps.
  19747. :deadline List any deadlines past due, or due within
  19748. `org-deadline-warning-days'. The listing occurs only
  19749. in the diary for *today*, not at any other date. If
  19750. an entry is marked DONE, it is no longer listed.
  19751. :scheduled List all items which are scheduled for the given date.
  19752. The diary for *today* also contains items which were
  19753. scheduled earlier and are not yet marked DONE.
  19754. :todo List all TODO items from the org-file. This may be a
  19755. long list - so this is not turned on by default.
  19756. Like deadlines, these entries only show up in the
  19757. diary for *today*, not at any other date.
  19758. The call in the diary file should look like this:
  19759. &%%(org-diary) ~/path/to/some/orgfile.org
  19760. Use a separate line for each org file to check. Or, if you omit the file name,
  19761. all files listed in `org-agenda-files' will be checked automatically:
  19762. &%%(org-diary)
  19763. If you don't give any arguments (as in the example above), the default
  19764. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19765. So the example above may also be written as
  19766. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19767. The function expects the lisp variables `entry' and `date' to be provided
  19768. by the caller, because this is how the calendar works. Don't use this
  19769. function from a program - use `org-agenda-get-day-entries' instead."
  19770. (when (> (- (time-to-seconds (current-time))
  19771. org-agenda-last-marker-time)
  19772. 5)
  19773. (org-agenda-reset-markers))
  19774. (org-compile-prefix-format 'agenda)
  19775. (org-set-sorting-strategy 'agenda)
  19776. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19777. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19778. (list entry)
  19779. (org-agenda-files t)))
  19780. file rtn results)
  19781. (org-prepare-agenda-buffers files)
  19782. ;; If this is called during org-agenda, don't return any entries to
  19783. ;; the calendar. Org Agenda will list these entries itself.
  19784. (if org-disable-agenda-to-diary (setq files nil))
  19785. (while (setq file (pop files))
  19786. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19787. (setq results (append results rtn)))
  19788. (if results
  19789. (concat (org-finalize-agenda-entries results) "\n"))))
  19790. ;;; Agenda entry finders
  19791. (defun org-agenda-get-day-entries (file date &rest args)
  19792. "Does the work for `org-diary' and `org-agenda'.
  19793. FILE is the path to a file to be checked for entries. DATE is date like
  19794. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19795. which kind of entries should be extracted. For details about these, see
  19796. the documentation of `org-diary'."
  19797. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19798. (let* ((org-startup-folded nil)
  19799. (org-startup-align-all-tables nil)
  19800. (buffer (if (file-exists-p file)
  19801. (org-get-agenda-file-buffer file)
  19802. (error "No such file %s" file)))
  19803. arg results rtn)
  19804. (if (not buffer)
  19805. ;; If file does not exist, make sure an error message ends up in diary
  19806. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19807. (with-current-buffer buffer
  19808. (unless (org-mode-p)
  19809. (error "Agenda file %s is not in `org-mode'" file))
  19810. (let ((case-fold-search nil))
  19811. (save-excursion
  19812. (save-restriction
  19813. (if org-agenda-restrict
  19814. (narrow-to-region org-agenda-restrict-begin
  19815. org-agenda-restrict-end)
  19816. (widen))
  19817. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19818. (while (setq arg (pop args))
  19819. (cond
  19820. ((and (eq arg :todo)
  19821. (equal date (calendar-current-date)))
  19822. (setq rtn (org-agenda-get-todos))
  19823. (setq results (append results rtn)))
  19824. ((eq arg :timestamp)
  19825. (setq rtn (org-agenda-get-blocks))
  19826. (setq results (append results rtn))
  19827. (setq rtn (org-agenda-get-timestamps))
  19828. (setq results (append results rtn)))
  19829. ((eq arg :sexp)
  19830. (setq rtn (org-agenda-get-sexps))
  19831. (setq results (append results rtn)))
  19832. ((eq arg :scheduled)
  19833. (setq rtn (org-agenda-get-scheduled))
  19834. (setq results (append results rtn)))
  19835. ((eq arg :closed)
  19836. (setq rtn (org-agenda-get-closed))
  19837. (setq results (append results rtn)))
  19838. ((eq arg :deadline)
  19839. (setq rtn (org-agenda-get-deadlines))
  19840. (setq results (append results rtn))))))))
  19841. results))))
  19842. (defun org-entry-is-todo-p ()
  19843. (member (org-get-todo-state) org-not-done-keywords))
  19844. (defun org-entry-is-done-p ()
  19845. (member (org-get-todo-state) org-done-keywords))
  19846. (defun org-get-todo-state ()
  19847. (save-excursion
  19848. (org-back-to-heading t)
  19849. (and (looking-at org-todo-line-regexp)
  19850. (match-end 2)
  19851. (match-string 2))))
  19852. (defun org-at-date-range-p (&optional inactive-ok)
  19853. "Is the cursor inside a date range?"
  19854. (interactive)
  19855. (save-excursion
  19856. (catch 'exit
  19857. (let ((pos (point)))
  19858. (skip-chars-backward "^[<\r\n")
  19859. (skip-chars-backward "<[")
  19860. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19861. (>= (match-end 0) pos)
  19862. (throw 'exit t))
  19863. (skip-chars-backward "^<[\r\n")
  19864. (skip-chars-backward "<[")
  19865. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19866. (>= (match-end 0) pos)
  19867. (throw 'exit t)))
  19868. nil)))
  19869. (defun org-agenda-get-todos ()
  19870. "Return the TODO information for agenda display."
  19871. (let* ((props (list 'face nil
  19872. 'done-face 'org-done
  19873. 'org-not-done-regexp org-not-done-regexp
  19874. 'org-todo-regexp org-todo-regexp
  19875. 'mouse-face 'highlight
  19876. 'keymap org-agenda-keymap
  19877. 'help-echo
  19878. (format "mouse-2 or RET jump to org file %s"
  19879. (abbreviate-file-name buffer-file-name))))
  19880. ;; FIXME: get rid of the \n at some point but watch out
  19881. (regexp (concat "^\\*+[ \t]+\\("
  19882. (if org-select-this-todo-keyword
  19883. (if (equal org-select-this-todo-keyword "*")
  19884. org-todo-regexp
  19885. (concat "\\<\\("
  19886. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19887. "\\)\\>"))
  19888. org-not-done-regexp)
  19889. "[^\n\r]*\\)"))
  19890. marker priority category tags
  19891. ee txt beg end)
  19892. (goto-char (point-min))
  19893. (while (re-search-forward regexp nil t)
  19894. (catch :skip
  19895. (save-match-data
  19896. (beginning-of-line)
  19897. (setq beg (point) end (progn (outline-next-heading) (point)))
  19898. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19899. (re-search-forward org-ts-regexp end t))
  19900. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19901. (re-search-forward org-scheduled-time-regexp end t))
  19902. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19903. (re-search-forward org-deadline-time-regexp end t)
  19904. (org-deadline-close (match-string 1))))
  19905. (goto-char (1+ beg))
  19906. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19907. (throw :skip nil)))
  19908. (goto-char beg)
  19909. (org-agenda-skip)
  19910. (goto-char (match-beginning 1))
  19911. (setq marker (org-agenda-new-marker (match-beginning 0))
  19912. category (org-get-category)
  19913. tags (org-get-tags-at (point))
  19914. txt (org-format-agenda-item "" (match-string 1) category tags)
  19915. priority (1+ (org-get-priority txt)))
  19916. (org-add-props txt props
  19917. 'org-marker marker 'org-hd-marker marker
  19918. 'priority priority 'org-category category
  19919. 'type "todo")
  19920. (push txt ee)
  19921. (if org-agenda-todo-list-sublevels
  19922. (goto-char (match-end 1))
  19923. (org-end-of-subtree 'invisible))))
  19924. (nreverse ee)))
  19925. (defconst org-agenda-no-heading-message
  19926. "No heading for this item in buffer or region.")
  19927. (defun org-agenda-get-timestamps ()
  19928. "Return the date stamp information for agenda display."
  19929. (let* ((props (list 'face nil
  19930. 'org-not-done-regexp org-not-done-regexp
  19931. 'org-todo-regexp org-todo-regexp
  19932. 'mouse-face 'highlight
  19933. 'keymap org-agenda-keymap
  19934. 'help-echo
  19935. (format "mouse-2 or RET jump to org file %s"
  19936. (abbreviate-file-name buffer-file-name))))
  19937. (d1 (calendar-absolute-from-gregorian date))
  19938. (remove-re
  19939. (concat
  19940. (regexp-quote
  19941. (format-time-string
  19942. "<%Y-%m-%d"
  19943. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19944. ".*?>"))
  19945. (regexp
  19946. (concat
  19947. (regexp-quote
  19948. (substring
  19949. (format-time-string
  19950. (car org-time-stamp-formats)
  19951. (apply 'encode-time ; DATE bound by calendar
  19952. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19953. 0 11))
  19954. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19955. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19956. marker hdmarker deadlinep scheduledp donep tmp priority category
  19957. ee txt timestr tags b0 b3 e3 head)
  19958. (goto-char (point-min))
  19959. (while (re-search-forward regexp nil t)
  19960. (setq b0 (match-beginning 0)
  19961. b3 (match-beginning 3) e3 (match-end 3))
  19962. (catch :skip
  19963. (and (org-at-date-range-p) (throw :skip nil))
  19964. (org-agenda-skip)
  19965. (if (and (match-end 1)
  19966. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19967. (throw :skip nil))
  19968. (if (and e3
  19969. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19970. (throw :skip nil))
  19971. (setq marker (org-agenda-new-marker b0)
  19972. category (org-get-category b0)
  19973. tmp (buffer-substring (max (point-min)
  19974. (- b0 org-ds-keyword-length))
  19975. b0)
  19976. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19977. deadlinep (string-match org-deadline-regexp tmp)
  19978. scheduledp (string-match org-scheduled-regexp tmp)
  19979. donep (org-entry-is-done-p))
  19980. (if (or scheduledp deadlinep) (throw :skip t))
  19981. (if (string-match ">" timestr)
  19982. ;; substring should only run to end of time stamp
  19983. (setq timestr (substring timestr 0 (match-end 0))))
  19984. (save-excursion
  19985. (if (re-search-backward "^\\*+ " nil t)
  19986. (progn
  19987. (goto-char (match-beginning 0))
  19988. (setq hdmarker (org-agenda-new-marker)
  19989. tags (org-get-tags-at))
  19990. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19991. (setq head (match-string 1))
  19992. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19993. (setq txt (org-format-agenda-item
  19994. nil head category tags timestr nil
  19995. remove-re)))
  19996. (setq txt org-agenda-no-heading-message))
  19997. (setq priority (org-get-priority txt))
  19998. (org-add-props txt props
  19999. 'org-marker marker 'org-hd-marker hdmarker)
  20000. (org-add-props txt nil 'priority priority
  20001. 'org-category category 'date date
  20002. 'type "timestamp")
  20003. (push txt ee))
  20004. (outline-next-heading)))
  20005. (nreverse ee)))
  20006. (defun org-agenda-get-sexps ()
  20007. "Return the sexp information for agenda display."
  20008. (require 'diary-lib)
  20009. (let* ((props (list 'face nil
  20010. 'mouse-face 'highlight
  20011. 'keymap org-agenda-keymap
  20012. 'help-echo
  20013. (format "mouse-2 or RET jump to org file %s"
  20014. (abbreviate-file-name buffer-file-name))))
  20015. (regexp "^&?%%(")
  20016. marker category ee txt tags entry result beg b sexp sexp-entry)
  20017. (goto-char (point-min))
  20018. (while (re-search-forward regexp nil t)
  20019. (catch :skip
  20020. (org-agenda-skip)
  20021. (setq beg (match-beginning 0))
  20022. (goto-char (1- (match-end 0)))
  20023. (setq b (point))
  20024. (forward-sexp 1)
  20025. (setq sexp (buffer-substring b (point)))
  20026. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  20027. (org-trim (match-string 1))
  20028. ""))
  20029. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  20030. (when result
  20031. (setq marker (org-agenda-new-marker beg)
  20032. category (org-get-category beg))
  20033. (if (string-match "\\S-" result)
  20034. (setq txt result)
  20035. (setq txt "SEXP entry returned empty string"))
  20036. (setq txt (org-format-agenda-item
  20037. "" txt category tags 'time))
  20038. (org-add-props txt props 'org-marker marker)
  20039. (org-add-props txt nil
  20040. 'org-category category 'date date
  20041. 'type "sexp")
  20042. (push txt ee))))
  20043. (nreverse ee)))
  20044. (defun org-agenda-get-closed ()
  20045. "Return the logged TODO entries for agenda display."
  20046. (let* ((props (list 'mouse-face 'highlight
  20047. 'org-not-done-regexp org-not-done-regexp
  20048. 'org-todo-regexp org-todo-regexp
  20049. 'keymap org-agenda-keymap
  20050. 'help-echo
  20051. (format "mouse-2 or RET jump to org file %s"
  20052. (abbreviate-file-name buffer-file-name))))
  20053. (regexp (concat
  20054. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  20055. (regexp-quote
  20056. (substring
  20057. (format-time-string
  20058. (car org-time-stamp-formats)
  20059. (apply 'encode-time ; DATE bound by calendar
  20060. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20061. 1 11))))
  20062. marker hdmarker priority category tags closedp
  20063. ee txt timestr)
  20064. (goto-char (point-min))
  20065. (while (re-search-forward regexp nil t)
  20066. (catch :skip
  20067. (org-agenda-skip)
  20068. (setq marker (org-agenda-new-marker (match-beginning 0))
  20069. closedp (equal (match-string 1) org-closed-string)
  20070. category (org-get-category (match-beginning 0))
  20071. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20072. ;; donep (org-entry-is-done-p)
  20073. )
  20074. (if (string-match "\\]" timestr)
  20075. ;; substring should only run to end of time stamp
  20076. (setq timestr (substring timestr 0 (match-end 0))))
  20077. (save-excursion
  20078. (if (re-search-backward "^\\*+ " nil t)
  20079. (progn
  20080. (goto-char (match-beginning 0))
  20081. (setq hdmarker (org-agenda-new-marker)
  20082. tags (org-get-tags-at))
  20083. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20084. (setq txt (org-format-agenda-item
  20085. (if closedp "Closed: " "Clocked: ")
  20086. (match-string 1) category tags timestr)))
  20087. (setq txt org-agenda-no-heading-message))
  20088. (setq priority 100000)
  20089. (org-add-props txt props
  20090. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20091. 'priority priority 'org-category category
  20092. 'type "closed" 'date date
  20093. 'undone-face 'org-warning 'done-face 'org-done)
  20094. (push txt ee))
  20095. (goto-char (point-at-eol))))
  20096. (nreverse ee)))
  20097. (defun org-agenda-get-deadlines ()
  20098. "Return the deadline information for agenda display."
  20099. (let* ((props (list 'mouse-face 'highlight
  20100. 'org-not-done-regexp org-not-done-regexp
  20101. 'org-todo-regexp org-todo-regexp
  20102. 'keymap org-agenda-keymap
  20103. 'help-echo
  20104. (format "mouse-2 or RET jump to org file %s"
  20105. (abbreviate-file-name buffer-file-name))))
  20106. (regexp org-deadline-time-regexp)
  20107. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20108. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20109. d2 diff dfrac wdays pos pos1 category tags
  20110. ee txt head face s upcomingp donep timestr)
  20111. (goto-char (point-min))
  20112. (while (re-search-forward regexp nil t)
  20113. (catch :skip
  20114. (org-agenda-skip)
  20115. (setq s (match-string 1)
  20116. pos (1- (match-beginning 1))
  20117. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20118. diff (- d2 d1)
  20119. wdays (org-get-wdays s)
  20120. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20121. upcomingp (and todayp (> diff 0)))
  20122. ;; When to show a deadline in the calendar:
  20123. ;; If the expiration is within wdays warning time.
  20124. ;; Past-due deadlines are only shown on the current date
  20125. (if (or (and (<= diff wdays)
  20126. (and todayp (not org-agenda-only-exact-dates)))
  20127. (= diff 0))
  20128. (save-excursion
  20129. (setq category (org-get-category))
  20130. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20131. (progn
  20132. (goto-char (match-end 0))
  20133. (setq pos1 (match-beginning 0))
  20134. (setq tags (org-get-tags-at pos1))
  20135. (setq head (buffer-substring-no-properties
  20136. (point)
  20137. (progn (skip-chars-forward "^\r\n")
  20138. (point))))
  20139. (setq donep (string-match org-looking-at-done-regexp head))
  20140. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20141. (setq timestr
  20142. (concat (substring s (match-beginning 1)) " "))
  20143. (setq timestr 'time))
  20144. (if (and donep
  20145. (or org-agenda-skip-deadline-if-done
  20146. (not (= diff 0))))
  20147. (setq txt nil)
  20148. (setq txt (org-format-agenda-item
  20149. (if (= diff 0)
  20150. (car org-agenda-deadline-leaders)
  20151. (format (nth 1 org-agenda-deadline-leaders)
  20152. diff))
  20153. head category tags timestr))))
  20154. (setq txt org-agenda-no-heading-message))
  20155. (when txt
  20156. (setq face (org-agenda-deadline-face dfrac wdays))
  20157. (org-add-props txt props
  20158. 'org-marker (org-agenda-new-marker pos)
  20159. 'org-hd-marker (org-agenda-new-marker pos1)
  20160. 'priority (+ (- diff)
  20161. (org-get-priority txt))
  20162. 'org-category category
  20163. 'type (if upcomingp "upcoming-deadline" "deadline")
  20164. 'date (if upcomingp date d2)
  20165. 'face (if donep 'org-done face)
  20166. 'undone-face face 'done-face 'org-done)
  20167. (push txt ee))))))
  20168. (nreverse ee)))
  20169. (defun org-agenda-deadline-face (fraction &optional wdays)
  20170. "Return the face to displaying a deadline item.
  20171. FRACTION is what fraction of the head-warning time has passed."
  20172. (if (equal wdays 0) (setq fraction 1.))
  20173. (let ((faces org-agenda-deadline-faces) f)
  20174. (catch 'exit
  20175. (while (setq f (pop faces))
  20176. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20177. (defun org-agenda-get-scheduled ()
  20178. "Return the scheduled information for agenda display."
  20179. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20180. 'org-todo-regexp org-todo-regexp
  20181. 'done-face 'org-done
  20182. 'mouse-face 'highlight
  20183. 'keymap org-agenda-keymap
  20184. 'help-echo
  20185. (format "mouse-2 or RET jump to org file %s"
  20186. (abbreviate-file-name buffer-file-name))))
  20187. (regexp org-scheduled-time-regexp)
  20188. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20189. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20190. d2 diff pos pos1 category tags
  20191. ee txt head pastschedp donep face timestr s)
  20192. (goto-char (point-min))
  20193. (while (re-search-forward regexp nil t)
  20194. (catch :skip
  20195. (org-agenda-skip)
  20196. (setq s (match-string 1)
  20197. pos (1- (match-beginning 1))
  20198. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20199. ;;; is this right?
  20200. ;;; do we need to do this for deadleine too????
  20201. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20202. diff (- d2 d1))
  20203. (setq pastschedp (and todayp (< diff 0)))
  20204. ;; When to show a scheduled item in the calendar:
  20205. ;; If it is on or past the date.
  20206. (if (or (and (< diff 0)
  20207. (< (abs diff) org-scheduled-past-days)
  20208. (and todayp (not org-agenda-only-exact-dates)))
  20209. (= diff 0))
  20210. (save-excursion
  20211. (setq category (org-get-category))
  20212. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20213. (progn
  20214. (goto-char (match-end 0))
  20215. (setq pos1 (match-beginning 0))
  20216. (setq tags (org-get-tags-at))
  20217. (setq head (buffer-substring-no-properties
  20218. (point)
  20219. (progn (skip-chars-forward "^\r\n") (point))))
  20220. (setq donep (string-match org-looking-at-done-regexp head))
  20221. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20222. (setq timestr
  20223. (concat (substring s (match-beginning 1)) " "))
  20224. (setq timestr 'time))
  20225. (if (and donep
  20226. (or org-agenda-skip-scheduled-if-done
  20227. (not (= diff 0))))
  20228. (setq txt nil)
  20229. (setq txt (org-format-agenda-item
  20230. (if (= diff 0)
  20231. (car org-agenda-scheduled-leaders)
  20232. (format (nth 1 org-agenda-scheduled-leaders)
  20233. (- 1 diff)))
  20234. head category tags timestr))))
  20235. (setq txt org-agenda-no-heading-message))
  20236. (when txt
  20237. (setq face (if pastschedp
  20238. 'org-scheduled-previously
  20239. 'org-scheduled-today))
  20240. (org-add-props txt props
  20241. 'undone-face face
  20242. 'face (if donep 'org-done face)
  20243. 'org-marker (org-agenda-new-marker pos)
  20244. 'org-hd-marker (org-agenda-new-marker pos1)
  20245. 'type (if pastschedp "past-scheduled" "scheduled")
  20246. 'date (if pastschedp d2 date)
  20247. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20248. 'org-category category)
  20249. (push txt ee))))))
  20250. (nreverse ee)))
  20251. (defun org-agenda-get-blocks ()
  20252. "Return the date-range information for agenda display."
  20253. (let* ((props (list 'face nil
  20254. 'org-not-done-regexp org-not-done-regexp
  20255. 'org-todo-regexp org-todo-regexp
  20256. 'mouse-face 'highlight
  20257. 'keymap org-agenda-keymap
  20258. 'help-echo
  20259. (format "mouse-2 or RET jump to org file %s"
  20260. (abbreviate-file-name buffer-file-name))))
  20261. (regexp org-tr-regexp)
  20262. (d0 (calendar-absolute-from-gregorian date))
  20263. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20264. donep head)
  20265. (goto-char (point-min))
  20266. (while (re-search-forward regexp nil t)
  20267. (catch :skip
  20268. (org-agenda-skip)
  20269. (setq pos (point))
  20270. (setq timestr (match-string 0)
  20271. s1 (match-string 1)
  20272. s2 (match-string 2)
  20273. d1 (time-to-days (org-time-string-to-time s1))
  20274. d2 (time-to-days (org-time-string-to-time s2)))
  20275. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20276. ;; Only allow days between the limits, because the normal
  20277. ;; date stamps will catch the limits.
  20278. (save-excursion
  20279. (setq marker (org-agenda-new-marker (point)))
  20280. (setq category (org-get-category))
  20281. (if (re-search-backward "^\\*+ " nil t)
  20282. (progn
  20283. (goto-char (match-beginning 0))
  20284. (setq hdmarker (org-agenda-new-marker (point)))
  20285. (setq tags (org-get-tags-at))
  20286. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20287. (setq head (match-string 1))
  20288. (and org-agenda-skip-timestamp-if-done
  20289. (org-entry-is-done-p)
  20290. (throw :skip t))
  20291. (setq txt (org-format-agenda-item
  20292. (format (if (= d1 d2) "" "(%d/%d): ")
  20293. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20294. head category tags
  20295. (if (= d0 d1) timestr))))
  20296. (setq txt org-agenda-no-heading-message))
  20297. (org-add-props txt props
  20298. 'org-marker marker 'org-hd-marker hdmarker
  20299. 'type "block" 'date date
  20300. 'priority (org-get-priority txt) 'org-category category)
  20301. (push txt ee)))
  20302. (goto-char pos)))
  20303. ;; Sort the entries by expiration date.
  20304. (nreverse ee)))
  20305. ;;; Agenda presentation and sorting
  20306. (defconst org-plain-time-of-day-regexp
  20307. (concat
  20308. "\\(\\<[012]?[0-9]"
  20309. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20310. "\\(--?"
  20311. "\\(\\<[012]?[0-9]"
  20312. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20313. "\\)?")
  20314. "Regular expression to match a plain time or time range.
  20315. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20316. groups carry important information:
  20317. 0 the full match
  20318. 1 the first time, range or not
  20319. 8 the second time, if it is a range.")
  20320. (defconst org-plain-time-extension-regexp
  20321. (concat
  20322. "\\(\\<[012]?[0-9]"
  20323. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20324. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20325. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20326. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20327. groups carry important information:
  20328. 0 the full match
  20329. 7 hours of duration
  20330. 9 minutes of duration")
  20331. (defconst org-stamp-time-of-day-regexp
  20332. (concat
  20333. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20334. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20335. "\\(--?"
  20336. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20337. "Regular expression to match a timestamp time or time range.
  20338. After a match, the following groups carry important information:
  20339. 0 the full match
  20340. 1 date plus weekday, for backreferencing to make sure both times on same day
  20341. 2 the first time, range or not
  20342. 4 the second time, if it is a range.")
  20343. (defvar org-prefix-has-time nil
  20344. "A flag, set by `org-compile-prefix-format'.
  20345. The flag is set if the currently compiled format contains a `%t'.")
  20346. (defvar org-prefix-has-tag nil
  20347. "A flag, set by `org-compile-prefix-format'.
  20348. The flag is set if the currently compiled format contains a `%T'.")
  20349. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20350. noprefix remove-re)
  20351. "Format TXT to be inserted into the agenda buffer.
  20352. In particular, it adds the prefix and corresponding text properties. EXTRA
  20353. must be a string and replaces the `%s' specifier in the prefix format.
  20354. CATEGORY (string, symbol or nil) may be used to overrule the default
  20355. category taken from local variable or file name. It will replace the `%c'
  20356. specifier in the format. DOTIME, when non-nil, indicates that a
  20357. time-of-day should be extracted from TXT for sorting of this entry, and for
  20358. the `%t' specifier in the format. When DOTIME is a string, this string is
  20359. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20360. only the correctly processes TXT should be returned - this is used by
  20361. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20362. Any match of REMOVE-RE will be removed from TXT."
  20363. (save-match-data
  20364. ;; Diary entries sometimes have extra whitespace at the beginning
  20365. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20366. (let* ((category (or category
  20367. org-category
  20368. (if buffer-file-name
  20369. (file-name-sans-extension
  20370. (file-name-nondirectory buffer-file-name))
  20371. "")))
  20372. (tag (if tags (nth (1- (length tags)) tags) ""))
  20373. time ; time and tag are needed for the eval of the prefix format
  20374. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20375. (time-of-day (and dotime (org-get-time-of-day ts)))
  20376. stamp plain s0 s1 s2 rtn srp)
  20377. (when (and dotime time-of-day org-prefix-has-time)
  20378. ;; Extract starting and ending time and move them to prefix
  20379. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20380. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20381. (setq s0 (match-string 0 ts)
  20382. srp (and stamp (match-end 3))
  20383. s1 (match-string (if plain 1 2) ts)
  20384. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20385. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20386. ;; them, we might want to remove them there to avoid duplication.
  20387. ;; The user can turn this off with a variable.
  20388. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20389. (string-match (concat (regexp-quote s0) " *") txt)
  20390. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20391. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20392. (= (match-beginning 0) 0)
  20393. t))
  20394. (setq txt (replace-match "" nil nil txt))))
  20395. ;; Normalize the time(s) to 24 hour
  20396. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20397. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20398. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20399. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20400. (let ((m (+ (string-to-number (match-string 2 s1))
  20401. (* 60 (string-to-number (match-string 1 s1)))
  20402. org-agenda-default-appointment-duration))
  20403. h)
  20404. (setq h (/ m 60) m (- m (* h 60)))
  20405. (setq s2 (format "%02d:%02d" h m))))
  20406. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20407. txt)
  20408. ;; Tags are in the string
  20409. (if (or (eq org-agenda-remove-tags t)
  20410. (and org-agenda-remove-tags
  20411. org-prefix-has-tag))
  20412. (setq txt (replace-match "" t t txt))
  20413. (setq txt (replace-match
  20414. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20415. (match-string 2 txt))
  20416. t t txt))))
  20417. (when remove-re
  20418. (while (string-match remove-re txt)
  20419. (setq txt (replace-match "" t t txt))))
  20420. ;; Create the final string
  20421. (if noprefix
  20422. (setq rtn txt)
  20423. ;; Prepare the variables needed in the eval of the compiled format
  20424. (setq time (cond (s2 (concat s1 "-" s2))
  20425. (s1 (concat s1 "......"))
  20426. (t ""))
  20427. extra (or extra "")
  20428. category (if (symbolp category) (symbol-name category) category))
  20429. ;; Evaluate the compiled format
  20430. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20431. ;; And finally add the text properties
  20432. (org-add-props rtn nil
  20433. 'org-category (downcase category) 'tags tags
  20434. 'org-highest-priority org-highest-priority
  20435. 'org-lowest-priority org-lowest-priority
  20436. 'prefix-length (- (length rtn) (length txt))
  20437. 'time-of-day time-of-day
  20438. 'txt txt
  20439. 'time time
  20440. 'extra extra
  20441. 'dotime dotime))))
  20442. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20443. (defvar org-agenda-sorting-strategy-selected nil)
  20444. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20445. (catch 'exit
  20446. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20447. ((and todayp (member 'today (car org-agenda-time-grid))))
  20448. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20449. ((member 'weekly (car org-agenda-time-grid)))
  20450. (t (throw 'exit list)))
  20451. (let* ((have (delq nil (mapcar
  20452. (lambda (x) (get-text-property 1 'time-of-day x))
  20453. list)))
  20454. (string (nth 1 org-agenda-time-grid))
  20455. (gridtimes (nth 2 org-agenda-time-grid))
  20456. (req (car org-agenda-time-grid))
  20457. (remove (member 'remove-match req))
  20458. new time)
  20459. (if (and (member 'require-timed req) (not have))
  20460. ;; don't show empty grid
  20461. (throw 'exit list))
  20462. (while (setq time (pop gridtimes))
  20463. (unless (and remove (member time have))
  20464. (setq time (int-to-string time))
  20465. (push (org-format-agenda-item
  20466. nil string "" nil
  20467. (concat (substring time 0 -2) ":" (substring time -2)))
  20468. new)
  20469. (put-text-property
  20470. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20471. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20472. (append new list)
  20473. (append list new)))))
  20474. (defun org-compile-prefix-format (key)
  20475. "Compile the prefix format into a Lisp form that can be evaluated.
  20476. The resulting form is returned and stored in the variable
  20477. `org-prefix-format-compiled'."
  20478. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20479. (let ((s (cond
  20480. ((stringp org-agenda-prefix-format)
  20481. org-agenda-prefix-format)
  20482. ((assq key org-agenda-prefix-format)
  20483. (cdr (assq key org-agenda-prefix-format)))
  20484. (t " %-12:c%?-12t% s")))
  20485. (start 0)
  20486. varform vars var e c f opt)
  20487. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20488. s start)
  20489. (setq var (cdr (assoc (match-string 4 s)
  20490. '(("c" . category) ("t" . time) ("s" . extra)
  20491. ("T" . tag))))
  20492. c (or (match-string 3 s) "")
  20493. opt (match-beginning 1)
  20494. start (1+ (match-beginning 0)))
  20495. (if (equal var 'time) (setq org-prefix-has-time t))
  20496. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20497. (setq f (concat "%" (match-string 2 s) "s"))
  20498. (if opt
  20499. (setq varform
  20500. `(if (equal "" ,var)
  20501. ""
  20502. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20503. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20504. (setq s (replace-match "%s" t nil s))
  20505. (push varform vars))
  20506. (setq vars (nreverse vars))
  20507. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20508. (defun org-set-sorting-strategy (key)
  20509. (if (symbolp (car org-agenda-sorting-strategy))
  20510. ;; the old format
  20511. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20512. (setq org-agenda-sorting-strategy-selected
  20513. (or (cdr (assq key org-agenda-sorting-strategy))
  20514. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20515. '(time-up category-keep priority-down)))))
  20516. (defun org-get-time-of-day (s &optional string mod24)
  20517. "Check string S for a time of day.
  20518. If found, return it as a military time number between 0 and 2400.
  20519. If not found, return nil.
  20520. The optional STRING argument forces conversion into a 5 character wide string
  20521. HH:MM."
  20522. (save-match-data
  20523. (when
  20524. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20525. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20526. (let* ((h (string-to-number (match-string 1 s)))
  20527. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20528. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20529. (am-p (equal ampm "am"))
  20530. (h1 (cond ((not ampm) h)
  20531. ((= h 12) (if am-p 0 12))
  20532. (t (+ h (if am-p 0 12)))))
  20533. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20534. (mod h1 24) h1))
  20535. (t0 (+ (* 100 h2) m))
  20536. (t1 (concat (if (>= h1 24) "+" " ")
  20537. (if (< t0 100) "0" "")
  20538. (if (< t0 10) "0" "")
  20539. (int-to-string t0))))
  20540. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20541. (defun org-finalize-agenda-entries (list &optional nosort)
  20542. "Sort and concatenate the agenda items."
  20543. (setq list (mapcar 'org-agenda-highlight-todo list))
  20544. (if nosort
  20545. list
  20546. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20547. (defun org-agenda-highlight-todo (x)
  20548. (let (re pl)
  20549. (if (eq x 'line)
  20550. (save-excursion
  20551. (beginning-of-line 1)
  20552. (setq re (get-text-property (point) 'org-todo-regexp))
  20553. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20554. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20555. (add-text-properties (match-beginning 0) (match-end 0)
  20556. (list 'face (org-get-todo-face 0)))
  20557. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20558. (delete-region (match-beginning 1) (1- (match-end 0)))
  20559. (goto-char (match-beginning 1))
  20560. (insert (format org-agenda-todo-keyword-format s)))))
  20561. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20562. pl (get-text-property 0 'prefix-length x))
  20563. (when (and re
  20564. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20565. x (or pl 0)) pl))
  20566. (add-text-properties
  20567. (or (match-end 1) (match-end 0)) (match-end 0)
  20568. (list 'face (org-get-todo-face (match-string 2 x)))
  20569. x)
  20570. (setq x (concat (substring x 0 (match-end 1))
  20571. (format org-agenda-todo-keyword-format
  20572. (match-string 2 x))
  20573. " "
  20574. (substring x (match-end 3)))))
  20575. x)))
  20576. (defsubst org-cmp-priority (a b)
  20577. "Compare the priorities of string A and B."
  20578. (let ((pa (or (get-text-property 1 'priority a) 0))
  20579. (pb (or (get-text-property 1 'priority b) 0)))
  20580. (cond ((> pa pb) +1)
  20581. ((< pa pb) -1)
  20582. (t nil))))
  20583. (defsubst org-cmp-category (a b)
  20584. "Compare the string values of categories of strings A and B."
  20585. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20586. (cb (or (get-text-property 1 'org-category b) "")))
  20587. (cond ((string-lessp ca cb) -1)
  20588. ((string-lessp cb ca) +1)
  20589. (t nil))))
  20590. (defsubst org-cmp-tag (a b)
  20591. "Compare the string values of categories of strings A and B."
  20592. (let ((ta (car (last (get-text-property 1 'tags a))))
  20593. (tb (car (last (get-text-property 1 'tags b)))))
  20594. (cond ((not ta) +1)
  20595. ((not tb) -1)
  20596. ((string-lessp ta tb) -1)
  20597. ((string-lessp tb ta) +1)
  20598. (t nil))))
  20599. (defsubst org-cmp-time (a b)
  20600. "Compare the time-of-day values of strings A and B."
  20601. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20602. (ta (or (get-text-property 1 'time-of-day a) def))
  20603. (tb (or (get-text-property 1 'time-of-day b) def)))
  20604. (cond ((< ta tb) -1)
  20605. ((< tb ta) +1)
  20606. (t nil))))
  20607. (defun org-entries-lessp (a b)
  20608. "Predicate for sorting agenda entries."
  20609. ;; The following variables will be used when the form is evaluated.
  20610. ;; So even though the compiler complains, keep them.
  20611. (let* ((time-up (org-cmp-time a b))
  20612. (time-down (if time-up (- time-up) nil))
  20613. (priority-up (org-cmp-priority a b))
  20614. (priority-down (if priority-up (- priority-up) nil))
  20615. (category-up (org-cmp-category a b))
  20616. (category-down (if category-up (- category-up) nil))
  20617. (category-keep (if category-up +1 nil))
  20618. (tag-up (org-cmp-tag a b))
  20619. (tag-down (if tag-up (- tag-up) nil)))
  20620. (cdr (assoc
  20621. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20622. '((-1 . t) (1 . nil) (nil . nil))))))
  20623. ;;; Agenda restriction lock
  20624. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20625. "Overlay to mark the headline to which arenda commands are restricted.")
  20626. (org-overlay-put org-agenda-restriction-lock-overlay
  20627. 'face 'org-agenda-restriction-lock)
  20628. (org-overlay-put org-agenda-restriction-lock-overlay
  20629. 'help-echo "Agendas are currently limited to this subtree.")
  20630. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20631. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20632. "Overlay marking the agenda restriction line in speedbar.")
  20633. (org-overlay-put org-speedbar-restriction-lock-overlay
  20634. 'face 'org-agenda-restriction-lock)
  20635. (org-overlay-put org-speedbar-restriction-lock-overlay
  20636. 'help-echo "Agendas are currently limited to this item.")
  20637. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20638. (defun org-agenda-set-restriction-lock (&optional type)
  20639. "Set restriction lock for agenda, to current subtree or file.
  20640. Restriction will be the file if TYPE is `file', or if type is the
  20641. universal prefix '(4), or if the cursor is before the first headline
  20642. in the file. Otherwise, restriction will be to the current subtree."
  20643. (interactive "P")
  20644. (and (equal type '(4)) (setq type 'file))
  20645. (setq type (cond
  20646. (type type)
  20647. ((org-at-heading-p) 'subtree)
  20648. ((condition-case nil (org-back-to-heading t) (error nil))
  20649. 'subtree)
  20650. (t 'file)))
  20651. (if (eq type 'subtree)
  20652. (progn
  20653. (setq org-agenda-restrict t)
  20654. (setq org-agenda-overriding-restriction 'subtree)
  20655. (put 'org-agenda-files 'org-restrict
  20656. (list (buffer-file-name (buffer-base-buffer))))
  20657. (org-back-to-heading t)
  20658. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20659. (move-marker org-agenda-restrict-begin (point))
  20660. (move-marker org-agenda-restrict-end
  20661. (save-excursion (org-end-of-subtree t)))
  20662. (message "Locking agenda restriction to subtree"))
  20663. (put 'org-agenda-files 'org-restrict
  20664. (list (buffer-file-name (buffer-base-buffer))))
  20665. (setq org-agenda-restrict nil)
  20666. (setq org-agenda-overriding-restriction 'file)
  20667. (move-marker org-agenda-restrict-begin nil)
  20668. (move-marker org-agenda-restrict-end nil)
  20669. (message "Locking agenda restriction to file"))
  20670. (setq current-prefix-arg nil)
  20671. (org-agenda-maybe-redo))
  20672. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20673. "Remove the agenda restriction lock."
  20674. (interactive "P")
  20675. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20676. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20677. (setq org-agenda-overriding-restriction nil)
  20678. (setq org-agenda-restrict nil)
  20679. (put 'org-agenda-files 'org-restrict nil)
  20680. (move-marker org-agenda-restrict-begin nil)
  20681. (move-marker org-agenda-restrict-end nil)
  20682. (setq current-prefix-arg nil)
  20683. (message "Agenda restriction lock removed")
  20684. (or noupdate (org-agenda-maybe-redo)))
  20685. (defun org-agenda-maybe-redo ()
  20686. "If there is any window showing the agenda view, update it."
  20687. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20688. (w0 (selected-window)))
  20689. (when w
  20690. (select-window w)
  20691. (org-agenda-redo)
  20692. (select-window w0)
  20693. (if org-agenda-overriding-restriction
  20694. (message "Agenda view shifted to new %s restriction"
  20695. org-agenda-overriding-restriction)
  20696. (message "Agenda restriction lock removed")))))
  20697. ;;; Agenda commands
  20698. (defun org-agenda-check-type (error &rest types)
  20699. "Check if agenda buffer is of allowed type.
  20700. If ERROR is non-nil, throw an error, otherwise just return nil."
  20701. (if (memq org-agenda-type types)
  20702. t
  20703. (if error
  20704. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20705. nil)))
  20706. (defun org-agenda-quit ()
  20707. "Exit agenda by removing the window or the buffer."
  20708. (interactive)
  20709. (let ((buf (current-buffer)))
  20710. (if (not (one-window-p)) (delete-window))
  20711. (kill-buffer buf)
  20712. (org-agenda-reset-markers)
  20713. (org-columns-remove-overlays))
  20714. ;; Maybe restore the pre-agenda window configuration.
  20715. (and org-agenda-restore-windows-after-quit
  20716. (not (eq org-agenda-window-setup 'other-frame))
  20717. org-pre-agenda-window-conf
  20718. (set-window-configuration org-pre-agenda-window-conf)))
  20719. (defun org-agenda-exit ()
  20720. "Exit agenda by removing the window or the buffer.
  20721. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20722. Org-mode buffers visited directly by the user will not be touched."
  20723. (interactive)
  20724. (org-release-buffers org-agenda-new-buffers)
  20725. (setq org-agenda-new-buffers nil)
  20726. (org-agenda-quit))
  20727. (defun org-agenda-execute (arg)
  20728. "Execute another agenda command, keeping same window.\\<global-map>
  20729. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20730. (interactive "P")
  20731. (let ((org-agenda-window-setup 'current-window))
  20732. (org-agenda arg)))
  20733. (defun org-save-all-org-buffers ()
  20734. "Save all Org-mode buffers without user confirmation."
  20735. (interactive)
  20736. (message "Saving all Org-mode buffers...")
  20737. (save-some-buffers t 'org-mode-p)
  20738. (message "Saving all Org-mode buffers... done"))
  20739. (defun org-agenda-redo ()
  20740. "Rebuild Agenda.
  20741. When this is the global TODO list, a prefix argument will be interpreted."
  20742. (interactive)
  20743. (let* ((org-agenda-keep-modes t)
  20744. (line (org-current-line))
  20745. (window-line (- line (org-current-line (window-start))))
  20746. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20747. (message "Rebuilding agenda buffer...")
  20748. (org-let lprops '(eval org-agenda-redo-command))
  20749. (setq org-agenda-undo-list nil
  20750. org-agenda-pending-undo-list nil)
  20751. (message "Rebuilding agenda buffer...done")
  20752. (goto-line line)
  20753. (recenter window-line)))
  20754. (defun org-agenda-manipulate-query-add ()
  20755. "Manipulate the query by adding a search term with positive selection.
  20756. Positive selection means, the term must be matched for selection of an entry."
  20757. (interactive)
  20758. (org-agenda-manipulate-query ?\[))
  20759. (defun org-agenda-manipulate-query-subtract ()
  20760. "Manipulate the query by adding a search term with negative selection.
  20761. Negative selection means, term must not be matched for selection of an entry."
  20762. (interactive)
  20763. (org-agenda-manipulate-query ?\]))
  20764. (defun org-agenda-manipulate-query-add-re ()
  20765. "Manipulate the query by adding a search regexp with positive selection.
  20766. Positive selection means, the regexp must match for selection of an entry."
  20767. (interactive)
  20768. (org-agenda-manipulate-query ?\{))
  20769. (defun org-agenda-manipulate-query-subtract-re ()
  20770. "Manipulate the query by adding a search regexp with negative selection.
  20771. Negative selection means, regexp must not match for selection of an entry."
  20772. (interactive)
  20773. (org-agenda-manipulate-query ?\}))
  20774. (defun org-agenda-manipulate-query (char)
  20775. (cond
  20776. ((eq org-agenda-type 'search)
  20777. (org-add-to-string
  20778. 'org-agenda-query-string
  20779. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20780. (?\{ . " +{}") (?\} . " -{}")))))
  20781. (setq org-agenda-redo-command
  20782. (list 'org-search-view
  20783. org-todo-only
  20784. org-agenda-query-string
  20785. (+ (length org-agenda-query-string)
  20786. (if (member char '(?\{ ?\})) 0 1))))
  20787. (set-register org-agenda-query-register org-agenda-query-string)
  20788. (org-agenda-redo))
  20789. (t (error "Canot manipulate query for %s-type agenda buffers"
  20790. org-agenda-type))))
  20791. (defun org-add-to-string (var string)
  20792. (set var (concat (symbol-value var) string)))
  20793. (defun org-agenda-goto-date (date)
  20794. "Jump to DATE in agenda."
  20795. (interactive (list (org-read-date)))
  20796. (org-agenda-list nil date))
  20797. (defun org-agenda-goto-today ()
  20798. "Go to today."
  20799. (interactive)
  20800. (org-agenda-check-type t 'timeline 'agenda)
  20801. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20802. (cond
  20803. (tdpos (goto-char tdpos))
  20804. ((eq org-agenda-type 'agenda)
  20805. (let* ((sd (time-to-days
  20806. (time-subtract (current-time)
  20807. (list 0 (* 3600 org-extend-today-until) 0))))
  20808. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20809. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20810. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20811. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20812. (org-agenda-redo)
  20813. (org-agenda-find-same-or-today-or-agenda)))
  20814. (t (error "Cannot find today")))))
  20815. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20816. (goto-char
  20817. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20818. (text-property-any (point-min) (point-max) 'org-today t)
  20819. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20820. (point-min))))
  20821. (defun org-agenda-later (arg)
  20822. "Go forward in time by thee current span.
  20823. With prefix ARG, go forward that many times the current span."
  20824. (interactive "p")
  20825. (org-agenda-check-type t 'agenda)
  20826. (let* ((span org-agenda-span)
  20827. (sd org-starting-day)
  20828. (greg (calendar-gregorian-from-absolute sd))
  20829. (cnt (get-text-property (point) 'org-day-cnt))
  20830. greg2 nd)
  20831. (cond
  20832. ((eq span 'day)
  20833. (setq sd (+ arg sd) nd 1))
  20834. ((eq span 'week)
  20835. (setq sd (+ (* 7 arg) sd) nd 7))
  20836. ((eq span 'month)
  20837. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20838. sd (calendar-absolute-from-gregorian greg2))
  20839. (setcar greg2 (1+ (car greg2)))
  20840. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20841. ((eq span 'year)
  20842. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20843. sd (calendar-absolute-from-gregorian greg2))
  20844. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20845. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20846. (let ((org-agenda-overriding-arguments
  20847. (list (car org-agenda-last-arguments) sd nd t)))
  20848. (org-agenda-redo)
  20849. (org-agenda-find-same-or-today-or-agenda cnt))))
  20850. (defun org-agenda-earlier (arg)
  20851. "Go backward in time by the current span.
  20852. With prefix ARG, go backward that many times the current span."
  20853. (interactive "p")
  20854. (org-agenda-later (- arg)))
  20855. (defun org-agenda-day-view (&optional day-of-year)
  20856. "Switch to daily view for agenda.
  20857. With argument DAY-OF-YEAR, switch to that day of the year."
  20858. (interactive "P")
  20859. (setq org-agenda-ndays 1)
  20860. (org-agenda-change-time-span 'day day-of-year))
  20861. (defun org-agenda-week-view (&optional iso-week)
  20862. "Switch to daily view for agenda.
  20863. With argument ISO-WEEK, switch to the corresponding ISO week.
  20864. If ISO-WEEK has more then 2 digits, only the last two encode the
  20865. week. Any digits before this encode a year. So 200712 means
  20866. week 12 of year 2007. Years in the range 1938-2037 can also be
  20867. written as 2-digit years."
  20868. (interactive "P")
  20869. (setq org-agenda-ndays 7)
  20870. (org-agenda-change-time-span 'week iso-week))
  20871. (defun org-agenda-month-view (&optional month)
  20872. "Switch to daily view for agenda.
  20873. With argument MONTH, switch to that month."
  20874. (interactive "P")
  20875. ;; FIXME: allow month like 812 to mean 2008 december
  20876. (org-agenda-change-time-span 'month month))
  20877. (defun org-agenda-year-view (&optional year)
  20878. "Switch to daily view for agenda.
  20879. With argument YEAR, switch to that year.
  20880. If MONTH has more then 2 digits, only the last two encode the
  20881. month. Any digits before this encode a year. So 200712 means
  20882. December year 2007. Years in the range 1938-2037 can also be
  20883. written as 2-digit years."
  20884. (interactive "P")
  20885. (when year
  20886. (setq year (org-small-year-to-year year)))
  20887. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20888. (org-agenda-change-time-span 'year year)
  20889. (error "Abort")))
  20890. (defun org-agenda-change-time-span (span &optional n)
  20891. "Change the agenda view to SPAN.
  20892. SPAN may be `day', `week', `month', `year'."
  20893. (org-agenda-check-type t 'agenda)
  20894. (if (and (not n) (equal org-agenda-span span))
  20895. (error "Viewing span is already \"%s\"" span))
  20896. (let* ((sd (or (get-text-property (point) 'day)
  20897. org-starting-day))
  20898. (computed (org-agenda-compute-time-span sd span n))
  20899. (org-agenda-overriding-arguments
  20900. (list (car org-agenda-last-arguments)
  20901. (car computed) (cdr computed) t)))
  20902. (org-agenda-redo)
  20903. (org-agenda-find-same-or-today-or-agenda))
  20904. (org-agenda-set-mode-name)
  20905. (message "Switched to %s view" span))
  20906. (defun org-agenda-compute-time-span (sd span &optional n)
  20907. "Compute starting date and number of days for agenda.
  20908. SPAN may be `day', `week', `month', `year'. The return value
  20909. is a cons cell with the starting date and the number of days,
  20910. so that the date SD will be in that range."
  20911. (let* ((greg (calendar-gregorian-from-absolute sd))
  20912. (dg (nth 1 greg))
  20913. (mg (car greg))
  20914. (yg (nth 2 greg))
  20915. nd w1 y1 m1 thisweek)
  20916. (cond
  20917. ((eq span 'day)
  20918. (when n
  20919. (setq sd (+ (calendar-absolute-from-gregorian
  20920. (list mg 1 yg))
  20921. n -1)))
  20922. (setq nd 1))
  20923. ((eq span 'week)
  20924. (let* ((nt (calendar-day-of-week
  20925. (calendar-gregorian-from-absolute sd)))
  20926. (d (if org-agenda-start-on-weekday
  20927. (- nt org-agenda-start-on-weekday)
  20928. 0)))
  20929. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20930. (when n
  20931. (require 'cal-iso)
  20932. (setq thisweek (car (calendar-iso-from-absolute sd)))
  20933. (when (> n 99)
  20934. (setq y1 (org-small-year-to-year (/ n 100))
  20935. n (mod n 100)))
  20936. (setq sd
  20937. (calendar-absolute-from-iso
  20938. (list n 1
  20939. (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
  20940. (setq nd 7)))
  20941. ((eq span 'month)
  20942. (when (and n (> n 99))
  20943. (setq y1 (org-small-year-to-year (/ n 100))
  20944. n (mod n 100)))
  20945. (setq sd (calendar-absolute-from-gregorian
  20946. (list (or n mg) 1 (or y1 yg)))
  20947. nd (- (calendar-absolute-from-gregorian
  20948. (list (1+ (or n mg)) 1 (or y1 yg)))
  20949. sd)))
  20950. ((eq span 'year)
  20951. (setq sd (calendar-absolute-from-gregorian
  20952. (list 1 1 (or n yg)))
  20953. nd (- (calendar-absolute-from-gregorian
  20954. (list 1 1 (1+ (or n yg))))
  20955. sd))))
  20956. (cons sd nd)))
  20957. (defun org-days-to-iso-week (days)
  20958. "Return the iso week number."
  20959. (require 'cal-iso)
  20960. (car (calendar-iso-from-absolute days)))
  20961. (defun org-small-year-to-year (year)
  20962. "Convert 2-digit years into 4-digit years.
  20963. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  20964. The year 2000 cannot be abbreviated. Any year lager than 99
  20965. is retrned unchanged."
  20966. (if (< year 38)
  20967. (setq year (+ 2000 year))
  20968. (if (< year 100)
  20969. (setq year (+ 1900 year))))
  20970. year)
  20971. ;; FIXME: does not work if user makes date format that starts with a blank
  20972. (defun org-agenda-next-date-line (&optional arg)
  20973. "Jump to the next line indicating a date in agenda buffer."
  20974. (interactive "p")
  20975. (org-agenda-check-type t 'agenda 'timeline)
  20976. (beginning-of-line 1)
  20977. (if (looking-at "^\\S-") (forward-char 1))
  20978. (if (not (re-search-forward "^\\S-" nil t arg))
  20979. (progn
  20980. (backward-char 1)
  20981. (error "No next date after this line in this buffer")))
  20982. (goto-char (match-beginning 0)))
  20983. (defun org-agenda-previous-date-line (&optional arg)
  20984. "Jump to the previous line indicating a date in agenda buffer."
  20985. (interactive "p")
  20986. (org-agenda-check-type t 'agenda 'timeline)
  20987. (beginning-of-line 1)
  20988. (if (not (re-search-backward "^\\S-" nil t arg))
  20989. (error "No previous date before this line in this buffer")))
  20990. ;; Initialize the highlight
  20991. (defvar org-hl (org-make-overlay 1 1))
  20992. (org-overlay-put org-hl 'face 'highlight)
  20993. (defun org-highlight (begin end &optional buffer)
  20994. "Highlight a region with overlay."
  20995. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20996. org-hl begin end (or buffer (current-buffer))))
  20997. (defun org-unhighlight ()
  20998. "Detach overlay INDEX."
  20999. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  21000. ;; FIXME this is currently not used.
  21001. (defun org-highlight-until-next-command (beg end &optional buffer)
  21002. (org-highlight beg end buffer)
  21003. (add-hook 'pre-command-hook 'org-unhighlight-once))
  21004. (defun org-unhighlight-once ()
  21005. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  21006. (org-unhighlight))
  21007. (defun org-agenda-follow-mode ()
  21008. "Toggle follow mode in an agenda buffer."
  21009. (interactive)
  21010. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  21011. (org-agenda-set-mode-name)
  21012. (message "Follow mode is %s"
  21013. (if org-agenda-follow-mode "on" "off")))
  21014. (defun org-agenda-log-mode ()
  21015. "Toggle log mode in an agenda buffer."
  21016. (interactive)
  21017. (org-agenda-check-type t 'agenda 'timeline)
  21018. (setq org-agenda-show-log (not org-agenda-show-log))
  21019. (org-agenda-set-mode-name)
  21020. (org-agenda-redo)
  21021. (message "Log mode is %s"
  21022. (if org-agenda-show-log "on" "off")))
  21023. (defun org-agenda-toggle-diary ()
  21024. "Toggle diary inclusion in an agenda buffer."
  21025. (interactive)
  21026. (org-agenda-check-type t 'agenda)
  21027. (setq org-agenda-include-diary (not org-agenda-include-diary))
  21028. (org-agenda-redo)
  21029. (org-agenda-set-mode-name)
  21030. (message "Diary inclusion turned %s"
  21031. (if org-agenda-include-diary "on" "off")))
  21032. (defun org-agenda-toggle-time-grid ()
  21033. "Toggle time grid in an agenda buffer."
  21034. (interactive)
  21035. (org-agenda-check-type t 'agenda)
  21036. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  21037. (org-agenda-redo)
  21038. (org-agenda-set-mode-name)
  21039. (message "Time-grid turned %s"
  21040. (if org-agenda-use-time-grid "on" "off")))
  21041. (defun org-agenda-set-mode-name ()
  21042. "Set the mode name to indicate all the small mode settings."
  21043. (setq mode-name
  21044. (concat "Org-Agenda"
  21045. (if (equal org-agenda-ndays 1) " Day" "")
  21046. (if (equal org-agenda-ndays 7) " Week" "")
  21047. (if org-agenda-follow-mode " Follow" "")
  21048. (if org-agenda-include-diary " Diary" "")
  21049. (if org-agenda-use-time-grid " Grid" "")
  21050. (if org-agenda-show-log " Log" "")))
  21051. (force-mode-line-update))
  21052. (defun org-agenda-post-command-hook ()
  21053. (and (eolp) (not (bolp)) (backward-char 1))
  21054. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  21055. (if (and org-agenda-follow-mode
  21056. (get-text-property (point) 'org-marker))
  21057. (org-agenda-show)))
  21058. (defun org-agenda-show-priority ()
  21059. "Show the priority of the current item.
  21060. This priority is composed of the main priority given with the [#A] cookies,
  21061. and by additional input from the age of a schedules or deadline entry."
  21062. (interactive)
  21063. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  21064. (message "Priority is %d" (if pri pri -1000))))
  21065. (defun org-agenda-show-tags ()
  21066. "Show the tags applicable to the current item."
  21067. (interactive)
  21068. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  21069. (if tags
  21070. (message "Tags are :%s:"
  21071. (org-no-properties (mapconcat 'identity tags ":")))
  21072. (message "No tags associated with this line"))))
  21073. (defun org-agenda-goto (&optional highlight)
  21074. "Go to the Org-mode file which contains the item at point."
  21075. (interactive)
  21076. (let* ((marker (or (get-text-property (point) 'org-marker)
  21077. (org-agenda-error)))
  21078. (buffer (marker-buffer marker))
  21079. (pos (marker-position marker)))
  21080. (switch-to-buffer-other-window buffer)
  21081. (widen)
  21082. (goto-char pos)
  21083. (when (org-mode-p)
  21084. (org-show-context 'agenda)
  21085. (save-excursion
  21086. (and (outline-next-heading)
  21087. (org-flag-heading nil)))) ; show the next heading
  21088. (recenter (/ (window-height) 2))
  21089. (run-hooks 'org-agenda-after-show-hook)
  21090. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  21091. (defvar org-agenda-after-show-hook nil
  21092. "Normal hook run after an item has been shown from the agenda.
  21093. Point is in the buffer where the item originated.")
  21094. (defun org-agenda-kill ()
  21095. "Kill the entry or subtree belonging to the current agenda entry."
  21096. (interactive)
  21097. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21098. (let* ((marker (or (get-text-property (point) 'org-marker)
  21099. (org-agenda-error)))
  21100. (buffer (marker-buffer marker))
  21101. (pos (marker-position marker))
  21102. (type (get-text-property (point) 'type))
  21103. dbeg dend (n 0) conf)
  21104. (org-with-remote-undo buffer
  21105. (with-current-buffer buffer
  21106. (save-excursion
  21107. (goto-char pos)
  21108. (if (and (org-mode-p) (not (member type '("sexp"))))
  21109. (setq dbeg (progn (org-back-to-heading t) (point))
  21110. dend (org-end-of-subtree t t))
  21111. (setq dbeg (point-at-bol)
  21112. dend (min (point-max) (1+ (point-at-eol)))))
  21113. (goto-char dbeg)
  21114. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  21115. (setq conf (or (eq t org-agenda-confirm-kill)
  21116. (and (numberp org-agenda-confirm-kill)
  21117. (> n org-agenda-confirm-kill))))
  21118. (and conf
  21119. (not (y-or-n-p
  21120. (format "Delete entry with %d lines in buffer \"%s\"? "
  21121. n (buffer-name buffer))))
  21122. (error "Abort"))
  21123. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21124. (with-current-buffer buffer (delete-region dbeg dend))
  21125. (message "Agenda item and source killed"))))
  21126. (defun org-agenda-archive ()
  21127. "Kill the entry or subtree belonging to the current agenda entry."
  21128. (interactive)
  21129. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21130. (let* ((marker (or (get-text-property (point) 'org-marker)
  21131. (org-agenda-error)))
  21132. (buffer (marker-buffer marker))
  21133. (pos (marker-position marker)))
  21134. (org-with-remote-undo buffer
  21135. (with-current-buffer buffer
  21136. (if (org-mode-p)
  21137. (save-excursion
  21138. (goto-char pos)
  21139. (org-remove-subtree-entries-from-agenda)
  21140. (org-back-to-heading t)
  21141. (org-archive-subtree))
  21142. (error "Archiving works only in Org-mode files"))))))
  21143. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21144. "Remove all lines in the agenda that correspond to a given subtree.
  21145. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21146. If this information is not given, the function uses the tree at point."
  21147. (let ((buf (or buf (current-buffer))) m p)
  21148. (save-excursion
  21149. (unless (and beg end)
  21150. (org-back-to-heading t)
  21151. (setq beg (point))
  21152. (org-end-of-subtree t)
  21153. (setq end (point)))
  21154. (set-buffer (get-buffer org-agenda-buffer-name))
  21155. (save-excursion
  21156. (goto-char (point-max))
  21157. (beginning-of-line 1)
  21158. (while (not (bobp))
  21159. (when (and (setq m (get-text-property (point) 'org-marker))
  21160. (equal buf (marker-buffer m))
  21161. (setq p (marker-position m))
  21162. (>= p beg)
  21163. (<= p end))
  21164. (let ((inhibit-read-only t))
  21165. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21166. (beginning-of-line 0))))))
  21167. (defun org-agenda-open-link ()
  21168. "Follow the link in the current line, if any."
  21169. (interactive)
  21170. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21171. (save-excursion
  21172. (save-restriction
  21173. (narrow-to-region (point-at-bol) (point-at-eol))
  21174. (org-open-at-point))))
  21175. (defun org-agenda-copy-local-variable (var)
  21176. "Get a variable from a referenced buffer and install it here."
  21177. (let ((m (get-text-property (point) 'org-marker)))
  21178. (when (and m (buffer-live-p (marker-buffer m)))
  21179. (org-set-local var (with-current-buffer (marker-buffer m)
  21180. (symbol-value var))))))
  21181. (defun org-agenda-switch-to (&optional delete-other-windows)
  21182. "Go to the Org-mode file which contains the item at point."
  21183. (interactive)
  21184. (let* ((marker (or (get-text-property (point) 'org-marker)
  21185. (org-agenda-error)))
  21186. (buffer (marker-buffer marker))
  21187. (pos (marker-position marker)))
  21188. (switch-to-buffer buffer)
  21189. (and delete-other-windows (delete-other-windows))
  21190. (widen)
  21191. (goto-char pos)
  21192. (when (org-mode-p)
  21193. (org-show-context 'agenda)
  21194. (save-excursion
  21195. (and (outline-next-heading)
  21196. (org-flag-heading nil)))))) ; show the next heading
  21197. (defun org-agenda-goto-mouse (ev)
  21198. "Go to the Org-mode file which contains the item at the mouse click."
  21199. (interactive "e")
  21200. (mouse-set-point ev)
  21201. (org-agenda-goto))
  21202. (defun org-agenda-show ()
  21203. "Display the Org-mode file which contains the item at point."
  21204. (interactive)
  21205. (let ((win (selected-window)))
  21206. (org-agenda-goto t)
  21207. (select-window win)))
  21208. (defun org-agenda-recenter (arg)
  21209. "Display the Org-mode file which contains the item at point and recenter."
  21210. (interactive "P")
  21211. (let ((win (selected-window)))
  21212. (org-agenda-goto t)
  21213. (recenter arg)
  21214. (select-window win)))
  21215. (defun org-agenda-show-mouse (ev)
  21216. "Display the Org-mode file which contains the item at the mouse click."
  21217. (interactive "e")
  21218. (mouse-set-point ev)
  21219. (org-agenda-show))
  21220. (defun org-agenda-check-no-diary ()
  21221. "Check if the entry is a diary link and abort if yes."
  21222. (if (get-text-property (point) 'org-agenda-diary-link)
  21223. (org-agenda-error)))
  21224. (defun org-agenda-error ()
  21225. (error "Command not allowed in this line"))
  21226. (defun org-agenda-tree-to-indirect-buffer ()
  21227. "Show the subtree corresponding to the current entry in an indirect buffer.
  21228. This calls the command `org-tree-to-indirect-buffer' from the original
  21229. Org-mode buffer.
  21230. With numerical prefix arg ARG, go up to this level and then take that tree.
  21231. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21232. dedicated frame)."
  21233. (interactive)
  21234. (org-agenda-check-no-diary)
  21235. (let* ((marker (or (get-text-property (point) 'org-marker)
  21236. (org-agenda-error)))
  21237. (buffer (marker-buffer marker))
  21238. (pos (marker-position marker)))
  21239. (with-current-buffer buffer
  21240. (save-excursion
  21241. (goto-char pos)
  21242. (call-interactively 'org-tree-to-indirect-buffer)))))
  21243. (defvar org-last-heading-marker (make-marker)
  21244. "Marker pointing to the headline that last changed its TODO state
  21245. by a remote command from the agenda.")
  21246. (defun org-agenda-todo-nextset ()
  21247. "Switch TODO entry to next sequence."
  21248. (interactive)
  21249. (org-agenda-todo 'nextset))
  21250. (defun org-agenda-todo-previousset ()
  21251. "Switch TODO entry to previous sequence."
  21252. (interactive)
  21253. (org-agenda-todo 'previousset))
  21254. (defun org-agenda-todo (&optional arg)
  21255. "Cycle TODO state of line at point, also in Org-mode file.
  21256. This changes the line at point, all other lines in the agenda referring to
  21257. the same tree node, and the headline of the tree node in the Org-mode file."
  21258. (interactive "P")
  21259. (org-agenda-check-no-diary)
  21260. (let* ((col (current-column))
  21261. (marker (or (get-text-property (point) 'org-marker)
  21262. (org-agenda-error)))
  21263. (buffer (marker-buffer marker))
  21264. (pos (marker-position marker))
  21265. (hdmarker (get-text-property (point) 'org-hd-marker))
  21266. (inhibit-read-only t)
  21267. newhead)
  21268. (org-with-remote-undo buffer
  21269. (with-current-buffer buffer
  21270. (widen)
  21271. (goto-char pos)
  21272. (org-show-context 'agenda)
  21273. (save-excursion
  21274. (and (outline-next-heading)
  21275. (org-flag-heading nil))) ; show the next heading
  21276. (org-todo arg)
  21277. (and (bolp) (forward-char 1))
  21278. (setq newhead (org-get-heading))
  21279. (save-excursion
  21280. (org-back-to-heading)
  21281. (move-marker org-last-heading-marker (point))))
  21282. (beginning-of-line 1)
  21283. (save-excursion
  21284. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21285. (move-to-column col))))
  21286. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21287. "Change all lines in the agenda buffer which match HDMARKER.
  21288. The new content of the line will be NEWHEAD (as modified by
  21289. `org-format-agenda-item'). HDMARKER is checked with
  21290. `equal' against all `org-hd-marker' text properties in the file.
  21291. If FIXFACE is non-nil, the face of each item is modified acording to
  21292. the new TODO state."
  21293. (let* ((inhibit-read-only t)
  21294. props m pl undone-face done-face finish new dotime cat tags)
  21295. (save-excursion
  21296. (goto-char (point-max))
  21297. (beginning-of-line 1)
  21298. (while (not finish)
  21299. (setq finish (bobp))
  21300. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21301. (equal m hdmarker))
  21302. (setq props (text-properties-at (point))
  21303. dotime (get-text-property (point) 'dotime)
  21304. cat (get-text-property (point) 'org-category)
  21305. tags (get-text-property (point) 'tags)
  21306. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21307. pl (get-text-property (point) 'prefix-length)
  21308. undone-face (get-text-property (point) 'undone-face)
  21309. done-face (get-text-property (point) 'done-face))
  21310. (move-to-column pl)
  21311. (cond
  21312. ((equal new "")
  21313. (beginning-of-line 1)
  21314. (and (looking-at ".*\n?") (replace-match "")))
  21315. ((looking-at ".*")
  21316. (replace-match new t t)
  21317. (beginning-of-line 1)
  21318. (add-text-properties (point-at-bol) (point-at-eol) props)
  21319. (when fixface
  21320. (add-text-properties
  21321. (point-at-bol) (point-at-eol)
  21322. (list 'face
  21323. (if org-last-todo-state-is-todo
  21324. undone-face done-face))))
  21325. (org-agenda-highlight-todo 'line)
  21326. (beginning-of-line 1))
  21327. (t (error "Line update did not work"))))
  21328. (beginning-of-line 0)))
  21329. (org-finalize-agenda)))
  21330. (defun org-agenda-align-tags (&optional line)
  21331. "Align all tags in agenda items to `org-agenda-tags-column'."
  21332. (let ((inhibit-read-only t) l c)
  21333. (save-excursion
  21334. (goto-char (if line (point-at-bol) (point-min)))
  21335. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21336. (if line (point-at-eol) nil) t)
  21337. (add-text-properties
  21338. (match-beginning 2) (match-end 2)
  21339. (list 'face (delq nil (list 'org-tag (get-text-property
  21340. (match-beginning 2) 'face)))))
  21341. (setq l (- (match-end 2) (match-beginning 2))
  21342. c (if (< org-agenda-tags-column 0)
  21343. (- (abs org-agenda-tags-column) l)
  21344. org-agenda-tags-column))
  21345. (delete-region (match-beginning 1) (match-end 1))
  21346. (goto-char (match-beginning 1))
  21347. (insert (org-add-props
  21348. (make-string (max 1 (- c (current-column))) ?\ )
  21349. (text-properties-at (point))))))))
  21350. (defun org-agenda-priority-up ()
  21351. "Increase the priority of line at point, also in Org-mode file."
  21352. (interactive)
  21353. (org-agenda-priority 'up))
  21354. (defun org-agenda-priority-down ()
  21355. "Decrease the priority of line at point, also in Org-mode file."
  21356. (interactive)
  21357. (org-agenda-priority 'down))
  21358. (defun org-agenda-priority (&optional force-direction)
  21359. "Set the priority of line at point, also in Org-mode file.
  21360. This changes the line at point, all other lines in the agenda referring to
  21361. the same tree node, and the headline of the tree node in the Org-mode file."
  21362. (interactive)
  21363. (org-agenda-check-no-diary)
  21364. (let* ((marker (or (get-text-property (point) 'org-marker)
  21365. (org-agenda-error)))
  21366. (hdmarker (get-text-property (point) 'org-hd-marker))
  21367. (buffer (marker-buffer hdmarker))
  21368. (pos (marker-position hdmarker))
  21369. (inhibit-read-only t)
  21370. newhead)
  21371. (org-with-remote-undo buffer
  21372. (with-current-buffer buffer
  21373. (widen)
  21374. (goto-char pos)
  21375. (org-show-context 'agenda)
  21376. (save-excursion
  21377. (and (outline-next-heading)
  21378. (org-flag-heading nil))) ; show the next heading
  21379. (funcall 'org-priority force-direction)
  21380. (end-of-line 1)
  21381. (setq newhead (org-get-heading)))
  21382. (org-agenda-change-all-lines newhead hdmarker)
  21383. (beginning-of-line 1))))
  21384. (defun org-get-tags-at (&optional pos)
  21385. "Get a list of all headline tags applicable at POS.
  21386. POS defaults to point. If tags are inherited, the list contains
  21387. the targets in the same sequence as the headlines appear, i.e.
  21388. the tags of the current headline come last."
  21389. (interactive)
  21390. (let (tags lastpos)
  21391. (save-excursion
  21392. (save-restriction
  21393. (widen)
  21394. (goto-char (or pos (point)))
  21395. (save-match-data
  21396. (condition-case nil
  21397. (progn
  21398. (org-back-to-heading t)
  21399. (while (not (equal lastpos (point)))
  21400. (setq lastpos (point))
  21401. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21402. (setq tags (append (org-split-string
  21403. (org-match-string-no-properties 1) ":")
  21404. tags)))
  21405. (or org-use-tag-inheritance (error ""))
  21406. (org-up-heading-all 1)))
  21407. (error nil))))
  21408. tags)))
  21409. ;; FIXME: should fix the tags property of the agenda line.
  21410. (defun org-agenda-set-tags ()
  21411. "Set tags for the current headline."
  21412. (interactive)
  21413. (org-agenda-check-no-diary)
  21414. (if (and (org-region-active-p) (interactive-p))
  21415. (call-interactively 'org-change-tag-in-region)
  21416. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21417. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21418. (org-agenda-error)))
  21419. (buffer (marker-buffer hdmarker))
  21420. (pos (marker-position hdmarker))
  21421. (inhibit-read-only t)
  21422. newhead)
  21423. (org-with-remote-undo buffer
  21424. (with-current-buffer buffer
  21425. (widen)
  21426. (goto-char pos)
  21427. (save-excursion
  21428. (org-show-context 'agenda))
  21429. (save-excursion
  21430. (and (outline-next-heading)
  21431. (org-flag-heading nil))) ; show the next heading
  21432. (goto-char pos)
  21433. (call-interactively 'org-set-tags)
  21434. (end-of-line 1)
  21435. (setq newhead (org-get-heading)))
  21436. (org-agenda-change-all-lines newhead hdmarker)
  21437. (beginning-of-line 1)))))
  21438. (defun org-agenda-toggle-archive-tag ()
  21439. "Toggle the archive tag for the current entry."
  21440. (interactive)
  21441. (org-agenda-check-no-diary)
  21442. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21443. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21444. (org-agenda-error)))
  21445. (buffer (marker-buffer hdmarker))
  21446. (pos (marker-position hdmarker))
  21447. (inhibit-read-only t)
  21448. newhead)
  21449. (org-with-remote-undo buffer
  21450. (with-current-buffer buffer
  21451. (widen)
  21452. (goto-char pos)
  21453. (org-show-context 'agenda)
  21454. (save-excursion
  21455. (and (outline-next-heading)
  21456. (org-flag-heading nil))) ; show the next heading
  21457. (call-interactively 'org-toggle-archive-tag)
  21458. (end-of-line 1)
  21459. (setq newhead (org-get-heading)))
  21460. (org-agenda-change-all-lines newhead hdmarker)
  21461. (beginning-of-line 1))))
  21462. (defun org-agenda-date-later (arg &optional what)
  21463. "Change the date of this item to one day later."
  21464. (interactive "p")
  21465. (org-agenda-check-type t 'agenda 'timeline)
  21466. (org-agenda-check-no-diary)
  21467. (let* ((marker (or (get-text-property (point) 'org-marker)
  21468. (org-agenda-error)))
  21469. (buffer (marker-buffer marker))
  21470. (pos (marker-position marker)))
  21471. (org-with-remote-undo buffer
  21472. (with-current-buffer buffer
  21473. (widen)
  21474. (goto-char pos)
  21475. (if (not (org-at-timestamp-p))
  21476. (error "Cannot find time stamp"))
  21477. (org-timestamp-change arg (or what 'day)))
  21478. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21479. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21480. (defun org-agenda-date-earlier (arg &optional what)
  21481. "Change the date of this item to one day earlier."
  21482. (interactive "p")
  21483. (org-agenda-date-later (- arg) what))
  21484. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21485. "Show new date stamp via text properties."
  21486. ;; We use text properties to make this undoable
  21487. (let ((inhibit-read-only t))
  21488. (setq stamp (concat " " prefix " => " stamp))
  21489. (save-excursion
  21490. (goto-char (point-max))
  21491. (while (not (bobp))
  21492. (when (equal marker (get-text-property (point) 'org-marker))
  21493. (move-to-column (- (window-width) (length stamp)) t)
  21494. (if (featurep 'xemacs)
  21495. ;; Use `duplicable' property to trigger undo recording
  21496. (let ((ex (make-extent nil nil))
  21497. (gl (make-glyph stamp)))
  21498. (set-glyph-face gl 'secondary-selection)
  21499. (set-extent-properties
  21500. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21501. (insert-extent ex (1- (point)) (point-at-eol)))
  21502. (add-text-properties
  21503. (1- (point)) (point-at-eol)
  21504. (list 'display (org-add-props stamp nil
  21505. 'face 'secondary-selection))))
  21506. (beginning-of-line 1))
  21507. (beginning-of-line 0)))))
  21508. (defun org-agenda-date-prompt (arg)
  21509. "Change the date of this item. Date is prompted for, with default today.
  21510. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21511. be used to request time specification in the time stamp."
  21512. (interactive "P")
  21513. (org-agenda-check-type t 'agenda 'timeline)
  21514. (org-agenda-check-no-diary)
  21515. (let* ((marker (or (get-text-property (point) 'org-marker)
  21516. (org-agenda-error)))
  21517. (buffer (marker-buffer marker))
  21518. (pos (marker-position marker)))
  21519. (org-with-remote-undo buffer
  21520. (with-current-buffer buffer
  21521. (widen)
  21522. (goto-char pos)
  21523. (if (not (org-at-timestamp-p))
  21524. (error "Cannot find time stamp"))
  21525. (org-time-stamp arg)
  21526. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21527. (defun org-agenda-schedule (arg)
  21528. "Schedule the item at point."
  21529. (interactive "P")
  21530. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  21531. (org-agenda-check-no-diary)
  21532. (let* ((marker (or (get-text-property (point) 'org-marker)
  21533. (org-agenda-error)))
  21534. (type (marker-insertion-type marker))
  21535. (buffer (marker-buffer marker))
  21536. (pos (marker-position marker))
  21537. (org-insert-labeled-timestamps-at-point nil)
  21538. ts)
  21539. (when type (message "%s" type) (sit-for 3))
  21540. (set-marker-insertion-type marker t)
  21541. (org-with-remote-undo buffer
  21542. (with-current-buffer buffer
  21543. (widen)
  21544. (goto-char pos)
  21545. (setq ts (org-schedule arg)))
  21546. (org-agenda-show-new-time marker ts "S"))
  21547. (message "Item scheduled for %s" ts)))
  21548. (defun org-agenda-deadline (arg)
  21549. "Schedule the item at point."
  21550. (interactive "P")
  21551. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  21552. (org-agenda-check-no-diary)
  21553. (let* ((marker (or (get-text-property (point) 'org-marker)
  21554. (org-agenda-error)))
  21555. (buffer (marker-buffer marker))
  21556. (pos (marker-position marker))
  21557. (org-insert-labeled-timestamps-at-point nil)
  21558. ts)
  21559. (org-with-remote-undo buffer
  21560. (with-current-buffer buffer
  21561. (widen)
  21562. (goto-char pos)
  21563. (setq ts (org-deadline arg)))
  21564. (org-agenda-show-new-time marker ts "S"))
  21565. (message "Deadline for this item set to %s" ts)))
  21566. (defun org-get-heading (&optional no-tags)
  21567. "Return the heading of the current entry, without the stars."
  21568. (save-excursion
  21569. (org-back-to-heading t)
  21570. (if (looking-at
  21571. (if no-tags
  21572. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21573. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21574. (match-string 1) "")))
  21575. (defun org-agenda-clock-in (&optional arg)
  21576. "Start the clock on the currently selected item."
  21577. (interactive "P")
  21578. (org-agenda-check-no-diary)
  21579. (let* ((marker (or (get-text-property (point) 'org-marker)
  21580. (org-agenda-error)))
  21581. (pos (marker-position marker)))
  21582. (org-with-remote-undo (marker-buffer marker)
  21583. (with-current-buffer (marker-buffer marker)
  21584. (widen)
  21585. (goto-char pos)
  21586. (org-clock-in)))))
  21587. (defun org-agenda-clock-out (&optional arg)
  21588. "Stop the currently running clock."
  21589. (interactive "P")
  21590. (unless (marker-buffer org-clock-marker)
  21591. (error "No running clock"))
  21592. (org-with-remote-undo (marker-buffer org-clock-marker)
  21593. (org-clock-out)))
  21594. (defun org-agenda-clock-cancel (&optional arg)
  21595. "Cancel the currently running clock."
  21596. (interactive "P")
  21597. (unless (marker-buffer org-clock-marker)
  21598. (error "No running clock"))
  21599. (org-with-remote-undo (marker-buffer org-clock-marker)
  21600. (org-clock-cancel)))
  21601. (defun org-agenda-diary-entry ()
  21602. "Make a diary entry, like the `i' command from the calendar.
  21603. All the standard commands work: block, weekly etc."
  21604. (interactive)
  21605. (org-agenda-check-type t 'agenda 'timeline)
  21606. (require 'diary-lib)
  21607. (let* ((char (progn
  21608. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21609. (read-char-exclusive)))
  21610. (cmd (cdr (assoc char
  21611. '((?d . insert-diary-entry)
  21612. (?w . insert-weekly-diary-entry)
  21613. (?m . insert-monthly-diary-entry)
  21614. (?y . insert-yearly-diary-entry)
  21615. (?a . insert-anniversary-diary-entry)
  21616. (?b . insert-block-diary-entry)
  21617. (?c . insert-cyclic-diary-entry)))))
  21618. (oldf (symbol-function 'calendar-cursor-to-date))
  21619. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21620. (point (point))
  21621. (mark (or (mark t) (point))))
  21622. (unless cmd
  21623. (error "No command associated with <%c>" char))
  21624. (unless (and (get-text-property point 'day)
  21625. (or (not (equal ?b char))
  21626. (get-text-property mark 'day)))
  21627. (error "Don't know which date to use for diary entry"))
  21628. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21629. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21630. (let ((calendar-mark-ring
  21631. (list (calendar-gregorian-from-absolute
  21632. (or (get-text-property mark 'day)
  21633. (get-text-property point 'day))))))
  21634. (unwind-protect
  21635. (progn
  21636. (fset 'calendar-cursor-to-date
  21637. (lambda (&optional error)
  21638. (calendar-gregorian-from-absolute
  21639. (get-text-property point 'day))))
  21640. (call-interactively cmd))
  21641. (fset 'calendar-cursor-to-date oldf)))))
  21642. (defun org-agenda-execute-calendar-command (cmd)
  21643. "Execute a calendar command from the agenda, with the date associated to
  21644. the cursor position."
  21645. (org-agenda-check-type t 'agenda 'timeline)
  21646. (require 'diary-lib)
  21647. (unless (get-text-property (point) 'day)
  21648. (error "Don't know which date to use for calendar command"))
  21649. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21650. (point (point))
  21651. (date (calendar-gregorian-from-absolute
  21652. (get-text-property point 'day)))
  21653. ;; the following 3 vars are needed in the calendar
  21654. (displayed-day (extract-calendar-day date))
  21655. (displayed-month (extract-calendar-month date))
  21656. (displayed-year (extract-calendar-year date)))
  21657. (unwind-protect
  21658. (progn
  21659. (fset 'calendar-cursor-to-date
  21660. (lambda (&optional error)
  21661. (calendar-gregorian-from-absolute
  21662. (get-text-property point 'day))))
  21663. (call-interactively cmd))
  21664. (fset 'calendar-cursor-to-date oldf))))
  21665. (defun org-agenda-phases-of-moon ()
  21666. "Display the phases of the moon for the 3 months around the cursor date."
  21667. (interactive)
  21668. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21669. (defun org-agenda-holidays ()
  21670. "Display the holidays for the 3 months around the cursor date."
  21671. (interactive)
  21672. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21673. (defvar calendar-longitude)
  21674. (defvar calendar-latitude)
  21675. (defvar calendar-location-name)
  21676. (defun org-agenda-sunrise-sunset (arg)
  21677. "Display sunrise and sunset for the cursor date.
  21678. Latitude and longitude can be specified with the variables
  21679. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21680. argument, latitude and longitude will be prompted for."
  21681. (interactive "P")
  21682. (require 'solar)
  21683. (let ((calendar-longitude (if arg nil calendar-longitude))
  21684. (calendar-latitude (if arg nil calendar-latitude))
  21685. (calendar-location-name
  21686. (if arg "the given coordinates" calendar-location-name)))
  21687. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21688. (defun org-agenda-goto-calendar ()
  21689. "Open the Emacs calendar with the date at the cursor."
  21690. (interactive)
  21691. (org-agenda-check-type t 'agenda 'timeline)
  21692. (let* ((day (or (get-text-property (point) 'day)
  21693. (error "Don't know which date to open in calendar")))
  21694. (date (calendar-gregorian-from-absolute day))
  21695. (calendar-move-hook nil)
  21696. (view-calendar-holidays-initially nil)
  21697. (view-diary-entries-initially nil))
  21698. (calendar)
  21699. (calendar-goto-date date)))
  21700. (defun org-calendar-goto-agenda ()
  21701. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21702. This is a command that has to be installed in `calendar-mode-map'."
  21703. (interactive)
  21704. (org-agenda-list nil (calendar-absolute-from-gregorian
  21705. (calendar-cursor-to-date))
  21706. nil))
  21707. (defun org-agenda-convert-date ()
  21708. (interactive)
  21709. (org-agenda-check-type t 'agenda 'timeline)
  21710. (let ((day (get-text-property (point) 'day))
  21711. date s)
  21712. (unless day
  21713. (error "Don't know which date to convert"))
  21714. (setq date (calendar-gregorian-from-absolute day))
  21715. (setq s (concat
  21716. "Gregorian: " (calendar-date-string date) "\n"
  21717. "ISO: " (calendar-iso-date-string date) "\n"
  21718. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21719. "Julian: " (calendar-julian-date-string date) "\n"
  21720. "Astron. JD: " (calendar-astro-date-string date)
  21721. " (Julian date number at noon UTC)\n"
  21722. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21723. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21724. "French: " (calendar-french-date-string date) "\n"
  21725. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21726. "Mayan: " (calendar-mayan-date-string date) "\n"
  21727. "Coptic: " (calendar-coptic-date-string date) "\n"
  21728. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21729. "Persian: " (calendar-persian-date-string date) "\n"
  21730. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21731. (with-output-to-temp-buffer "*Dates*"
  21732. (princ s))
  21733. (if (fboundp 'fit-window-to-buffer)
  21734. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21735. ;;;; Embedded LaTeX
  21736. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21737. "Keymap for the minor `org-cdlatex-mode'.")
  21738. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21739. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21740. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21741. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21742. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21743. (defvar org-cdlatex-texmathp-advice-is-done nil
  21744. "Flag remembering if we have applied the advice to texmathp already.")
  21745. (define-minor-mode org-cdlatex-mode
  21746. "Toggle the minor `org-cdlatex-mode'.
  21747. This mode supports entering LaTeX environment and math in LaTeX fragments
  21748. in Org-mode.
  21749. \\{org-cdlatex-mode-map}"
  21750. nil " OCDL" nil
  21751. (when org-cdlatex-mode (require 'cdlatex))
  21752. (unless org-cdlatex-texmathp-advice-is-done
  21753. (setq org-cdlatex-texmathp-advice-is-done t)
  21754. (defadvice texmathp (around org-math-always-on activate)
  21755. "Always return t in org-mode buffers.
  21756. This is because we want to insert math symbols without dollars even outside
  21757. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21758. en embedded LaTeX fragement, let texmathp do its job.
  21759. \\[org-cdlatex-mode-map]"
  21760. (interactive)
  21761. (let (p)
  21762. (cond
  21763. ((not (org-mode-p)) ad-do-it)
  21764. ((eq this-command 'cdlatex-math-symbol)
  21765. (setq ad-return-value t
  21766. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21767. (t
  21768. (let ((p (org-inside-LaTeX-fragment-p)))
  21769. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21770. (setq ad-return-value t
  21771. texmathp-why '("Org-mode embedded math" . 0))
  21772. (if p ad-do-it)))))))))
  21773. (defun turn-on-org-cdlatex ()
  21774. "Unconditionally turn on `org-cdlatex-mode'."
  21775. (org-cdlatex-mode 1))
  21776. (defun org-inside-LaTeX-fragment-p ()
  21777. "Test if point is inside a LaTeX fragment.
  21778. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21779. sequence appearing also before point.
  21780. Even though the matchers for math are configurable, this function assumes
  21781. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21782. delimiters are skipped when they have been removed by customization.
  21783. The return value is nil, or a cons cell with the delimiter and
  21784. and the position of this delimiter.
  21785. This function does a reasonably good job, but can locally be fooled by
  21786. for example currency specifications. For example it will assume being in
  21787. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21788. fragments that are properly closed, but during editing, we have to live
  21789. with the uncertainty caused by missing closing delimiters. This function
  21790. looks only before point, not after."
  21791. (catch 'exit
  21792. (let ((pos (point))
  21793. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21794. (lim (progn
  21795. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21796. (point)))
  21797. dd-on str (start 0) m re)
  21798. (goto-char pos)
  21799. (when dodollar
  21800. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21801. re (nth 1 (assoc "$" org-latex-regexps)))
  21802. (while (string-match re str start)
  21803. (cond
  21804. ((= (match-end 0) (length str))
  21805. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21806. ((= (match-end 0) (- (length str) 5))
  21807. (throw 'exit nil))
  21808. (t (setq start (match-end 0))))))
  21809. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21810. (goto-char pos)
  21811. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21812. (and (match-beginning 2) (throw 'exit nil))
  21813. ;; count $$
  21814. (while (re-search-backward "\\$\\$" lim t)
  21815. (setq dd-on (not dd-on)))
  21816. (goto-char pos)
  21817. (if dd-on (cons "$$" m))))))
  21818. (defun org-try-cdlatex-tab ()
  21819. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21820. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21821. - inside a LaTeX fragment, or
  21822. - after the first word in a line, where an abbreviation expansion could
  21823. insert a LaTeX environment."
  21824. (when org-cdlatex-mode
  21825. (cond
  21826. ((save-excursion
  21827. (skip-chars-backward "a-zA-Z0-9*")
  21828. (skip-chars-backward " \t")
  21829. (bolp))
  21830. (cdlatex-tab) t)
  21831. ((org-inside-LaTeX-fragment-p)
  21832. (cdlatex-tab) t)
  21833. (t nil))))
  21834. (defun org-cdlatex-underscore-caret (&optional arg)
  21835. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21836. Revert to the normal definition outside of these fragments."
  21837. (interactive "P")
  21838. (if (org-inside-LaTeX-fragment-p)
  21839. (call-interactively 'cdlatex-sub-superscript)
  21840. (let (org-cdlatex-mode)
  21841. (call-interactively (key-binding (vector last-input-event))))))
  21842. (defun org-cdlatex-math-modify (&optional arg)
  21843. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21844. Revert to the normal definition outside of these fragments."
  21845. (interactive "P")
  21846. (if (org-inside-LaTeX-fragment-p)
  21847. (call-interactively 'cdlatex-math-modify)
  21848. (let (org-cdlatex-mode)
  21849. (call-interactively (key-binding (vector last-input-event))))))
  21850. (defvar org-latex-fragment-image-overlays nil
  21851. "List of overlays carrying the images of latex fragments.")
  21852. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21853. (defun org-remove-latex-fragment-image-overlays ()
  21854. "Remove all overlays with LaTeX fragment images in current buffer."
  21855. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21856. (setq org-latex-fragment-image-overlays nil))
  21857. (defun org-preview-latex-fragment (&optional subtree)
  21858. "Preview the LaTeX fragment at point, or all locally or globally.
  21859. If the cursor is in a LaTeX fragment, create the image and overlay
  21860. it over the source code. If there is no fragment at point, display
  21861. all fragments in the current text, from one headline to the next. With
  21862. prefix SUBTREE, display all fragments in the current subtree. With a
  21863. double prefix `C-u C-u', or when the cursor is before the first headline,
  21864. display all fragments in the buffer.
  21865. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21866. (interactive "P")
  21867. (org-remove-latex-fragment-image-overlays)
  21868. (save-excursion
  21869. (save-restriction
  21870. (let (beg end at msg)
  21871. (cond
  21872. ((or (equal subtree '(16))
  21873. (not (save-excursion
  21874. (re-search-backward (concat "^" outline-regexp) nil t))))
  21875. (setq beg (point-min) end (point-max)
  21876. msg "Creating images for buffer...%s"))
  21877. ((equal subtree '(4))
  21878. (org-back-to-heading)
  21879. (setq beg (point) end (org-end-of-subtree t)
  21880. msg "Creating images for subtree...%s"))
  21881. (t
  21882. (if (setq at (org-inside-LaTeX-fragment-p))
  21883. (goto-char (max (point-min) (- (cdr at) 2)))
  21884. (org-back-to-heading))
  21885. (setq beg (point) end (progn (outline-next-heading) (point))
  21886. msg (if at "Creating image...%s"
  21887. "Creating images for entry...%s"))))
  21888. (message msg "")
  21889. (narrow-to-region beg end)
  21890. (goto-char beg)
  21891. (org-format-latex
  21892. (concat "ltxpng/" (file-name-sans-extension
  21893. (file-name-nondirectory
  21894. buffer-file-name)))
  21895. default-directory 'overlays msg at 'forbuffer)
  21896. (message msg "done. Use `C-c C-c' to remove images.")))))
  21897. (defvar org-latex-regexps
  21898. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21899. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21900. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21901. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21902. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21903. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21904. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21905. "Regular expressions for matching embedded LaTeX.")
  21906. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21907. "Replace LaTeX fragments with links to an image, and produce images."
  21908. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21909. (let* ((prefixnodir (file-name-nondirectory prefix))
  21910. (absprefix (expand-file-name prefix dir))
  21911. (todir (file-name-directory absprefix))
  21912. (opt org-format-latex-options)
  21913. (matchers (plist-get opt :matchers))
  21914. (re-list org-latex-regexps)
  21915. (cnt 0) txt link beg end re e checkdir
  21916. m n block linkfile movefile ov)
  21917. ;; Check if there are old images files with this prefix, and remove them
  21918. (when (file-directory-p todir)
  21919. (mapc 'delete-file
  21920. (directory-files
  21921. todir 'full
  21922. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21923. ;; Check the different regular expressions
  21924. (while (setq e (pop re-list))
  21925. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21926. block (if (nth 3 e) "\n\n" ""))
  21927. (when (member m matchers)
  21928. (goto-char (point-min))
  21929. (while (re-search-forward re nil t)
  21930. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21931. (setq txt (match-string n)
  21932. beg (match-beginning n) end (match-end n)
  21933. cnt (1+ cnt)
  21934. linkfile (format "%s_%04d.png" prefix cnt)
  21935. movefile (format "%s_%04d.png" absprefix cnt)
  21936. link (concat block "[[file:" linkfile "]]" block))
  21937. (if msg (message msg cnt))
  21938. (goto-char beg)
  21939. (unless checkdir ; make sure the directory exists
  21940. (setq checkdir t)
  21941. (or (file-directory-p todir) (make-directory todir)))
  21942. (org-create-formula-image
  21943. txt movefile opt forbuffer)
  21944. (if overlays
  21945. (progn
  21946. (setq ov (org-make-overlay beg end))
  21947. (if (featurep 'xemacs)
  21948. (progn
  21949. (org-overlay-put ov 'invisible t)
  21950. (org-overlay-put
  21951. ov 'end-glyph
  21952. (make-glyph (vector 'png :file movefile))))
  21953. (org-overlay-put
  21954. ov 'display
  21955. (list 'image :type 'png :file movefile :ascent 'center)))
  21956. (push ov org-latex-fragment-image-overlays)
  21957. (goto-char end))
  21958. (delete-region beg end)
  21959. (insert link))))))))
  21960. ;; This function borrows from Ganesh Swami's latex2png.el
  21961. (defun org-create-formula-image (string tofile options buffer)
  21962. (let* ((tmpdir (if (featurep 'xemacs)
  21963. (temp-directory)
  21964. temporary-file-directory))
  21965. (texfilebase (make-temp-name
  21966. (expand-file-name "orgtex" tmpdir)))
  21967. (texfile (concat texfilebase ".tex"))
  21968. (dvifile (concat texfilebase ".dvi"))
  21969. (pngfile (concat texfilebase ".png"))
  21970. (fnh (face-attribute 'default :height nil))
  21971. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21972. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21973. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21974. "Black"))
  21975. (bg (or (plist-get options (if buffer :background :html-background))
  21976. "Transparent")))
  21977. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21978. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21979. (with-temp-file texfile
  21980. (insert org-format-latex-header
  21981. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21982. (let ((dir default-directory))
  21983. (condition-case nil
  21984. (progn
  21985. (cd tmpdir)
  21986. (call-process "latex" nil nil nil texfile))
  21987. (error nil))
  21988. (cd dir))
  21989. (if (not (file-exists-p dvifile))
  21990. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21991. (call-process "dvipng" nil nil nil
  21992. "-E" "-fg" fg "-bg" bg
  21993. "-D" dpi
  21994. ;;"-x" scale "-y" scale
  21995. "-T" "tight"
  21996. "-o" pngfile
  21997. dvifile)
  21998. (if (not (file-exists-p pngfile))
  21999. (progn (message "Failed to create png file from %s" texfile) nil)
  22000. ;; Use the requested file name and clean up
  22001. (copy-file pngfile tofile 'replace)
  22002. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  22003. (delete-file (concat texfilebase e)))
  22004. pngfile))))
  22005. (defun org-dvipng-color (attr)
  22006. "Return an rgb color specification for dvipng."
  22007. (apply 'format "rgb %s %s %s"
  22008. (mapcar 'org-normalize-color
  22009. (color-values (face-attribute 'default attr nil)))))
  22010. (defun org-normalize-color (value)
  22011. "Return string to be used as color value for an RGB component."
  22012. (format "%g" (/ value 65535.0)))
  22013. ;;;; Exporting
  22014. ;;; Variables, constants, and parameter plists
  22015. (defconst org-level-max 20)
  22016. (defvar org-export-html-preamble nil
  22017. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  22018. (defvar org-export-html-postamble nil
  22019. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  22020. (defvar org-export-html-auto-preamble t
  22021. "Should default preamble be inserted? Set by publishing functions.")
  22022. (defvar org-export-html-auto-postamble t
  22023. "Should default postamble be inserted? Set by publishing functions.")
  22024. (defvar org-current-export-file nil) ; dynamically scoped parameter
  22025. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  22026. (defconst org-export-plist-vars
  22027. '((:language . org-export-default-language)
  22028. (:customtime . org-display-custom-times)
  22029. (:headline-levels . org-export-headline-levels)
  22030. (:section-numbers . org-export-with-section-numbers)
  22031. (:table-of-contents . org-export-with-toc)
  22032. (:preserve-breaks . org-export-preserve-breaks)
  22033. (:archived-trees . org-export-with-archived-trees)
  22034. (:emphasize . org-export-with-emphasize)
  22035. (:sub-superscript . org-export-with-sub-superscripts)
  22036. (:special-strings . org-export-with-special-strings)
  22037. (:footnotes . org-export-with-footnotes)
  22038. (:drawers . org-export-with-drawers)
  22039. (:tags . org-export-with-tags)
  22040. (:TeX-macros . org-export-with-TeX-macros)
  22041. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  22042. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  22043. (:fixed-width . org-export-with-fixed-width)
  22044. (:timestamps . org-export-with-timestamps)
  22045. (:author-info . org-export-author-info)
  22046. (:time-stamp-file . org-export-time-stamp-file)
  22047. (:tables . org-export-with-tables)
  22048. (:table-auto-headline . org-export-highlight-first-table-line)
  22049. (:style . org-export-html-style)
  22050. (:agenda-style . org-agenda-export-html-style)
  22051. (:convert-org-links . org-export-html-link-org-files-as-html)
  22052. (:inline-images . org-export-html-inline-images)
  22053. (:html-extension . org-export-html-extension)
  22054. (:html-table-tag . org-export-html-table-tag)
  22055. (:expand-quoted-html . org-export-html-expand)
  22056. (:timestamp . org-export-html-with-timestamp)
  22057. (:publishing-directory . org-export-publishing-directory)
  22058. (:preamble . org-export-html-preamble)
  22059. (:postamble . org-export-html-postamble)
  22060. (:auto-preamble . org-export-html-auto-preamble)
  22061. (:auto-postamble . org-export-html-auto-postamble)
  22062. (:author . user-full-name)
  22063. (:email . user-mail-address)))
  22064. (defun org-default-export-plist ()
  22065. "Return the property list with default settings for the export variables."
  22066. (let ((l org-export-plist-vars) rtn e)
  22067. (while (setq e (pop l))
  22068. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  22069. rtn))
  22070. (defun org-infile-export-plist ()
  22071. "Return the property list with file-local settings for export."
  22072. (save-excursion
  22073. (save-restriction
  22074. (widen)
  22075. (goto-char 0)
  22076. (let ((re (org-make-options-regexp
  22077. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  22078. p key val text options)
  22079. (while (re-search-forward re nil t)
  22080. (setq key (org-match-string-no-properties 1)
  22081. val (org-match-string-no-properties 2))
  22082. (cond
  22083. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  22084. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  22085. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  22086. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  22087. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  22088. ((string-equal key "TEXT")
  22089. (setq text (if text (concat text "\n" val) val)))
  22090. ((string-equal key "OPTIONS") (setq options val))))
  22091. (setq p (plist-put p :text text))
  22092. (when options
  22093. (let ((op '(("H" . :headline-levels)
  22094. ("num" . :section-numbers)
  22095. ("toc" . :table-of-contents)
  22096. ("\\n" . :preserve-breaks)
  22097. ("@" . :expand-quoted-html)
  22098. (":" . :fixed-width)
  22099. ("|" . :tables)
  22100. ("^" . :sub-superscript)
  22101. ("-" . :special-strings)
  22102. ("f" . :footnotes)
  22103. ("d" . :drawers)
  22104. ("tags" . :tags)
  22105. ("*" . :emphasize)
  22106. ("TeX" . :TeX-macros)
  22107. ("LaTeX" . :LaTeX-fragments)
  22108. ("skip" . :skip-before-1st-heading)
  22109. ("author" . :author-info)
  22110. ("timestamp" . :time-stamp-file)))
  22111. o)
  22112. (while (setq o (pop op))
  22113. (if (string-match (concat (regexp-quote (car o))
  22114. ":\\([^ \t\n\r;,.]*\\)")
  22115. options)
  22116. (setq p (plist-put p (cdr o)
  22117. (car (read-from-string
  22118. (match-string 1 options)))))))))
  22119. p))))
  22120. (defun org-export-directory (type plist)
  22121. (let* ((val (plist-get plist :publishing-directory))
  22122. (dir (if (listp val)
  22123. (or (cdr (assoc type val)) ".")
  22124. val)))
  22125. dir))
  22126. (defun org-skip-comments (lines)
  22127. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22128. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22129. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22130. (case-fold-search nil)
  22131. rtn line level)
  22132. (while (setq line (pop lines))
  22133. (cond
  22134. ((and (string-match re1 line)
  22135. (setq level (- (match-end 1) (match-beginning 1))))
  22136. ;; Beginning of a COMMENT subtree. Skip it.
  22137. (while (and (setq line (pop lines))
  22138. (or (not (string-match re2 line))
  22139. (> (- (match-end 1) (match-beginning 1)) level))))
  22140. (setq lines (cons line lines)))
  22141. ((string-match "^#" line)
  22142. ;; an ordinary comment line
  22143. )
  22144. ((and org-export-table-remove-special-lines
  22145. (string-match "^[ \t]*|" line)
  22146. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22147. (and (string-match "| *<[0-9]+> *|" line)
  22148. (not (string-match "| *[^ <|]" line)))))
  22149. ;; a special table line that should be removed
  22150. )
  22151. (t (setq rtn (cons line rtn)))))
  22152. (nreverse rtn)))
  22153. (defun org-export (&optional arg)
  22154. (interactive)
  22155. (let ((help "[t] insert the export option template
  22156. \[v] limit export to visible part of outline tree
  22157. \[a] export as ASCII
  22158. \[h] export as HTML
  22159. \[H] export as HTML to temporary buffer
  22160. \[R] export region as HTML
  22161. \[b] export as HTML and browse immediately
  22162. \[x] export as XOXO
  22163. \[l] export as LaTeX
  22164. \[L] export as LaTeX to temporary buffer
  22165. \[i] export current file as iCalendar file
  22166. \[I] export all agenda files as iCalendar files
  22167. \[c] export agenda files into combined iCalendar file
  22168. \[F] publish current file
  22169. \[P] publish current project
  22170. \[X] publish... (project will be prompted for)
  22171. \[A] publish all projects")
  22172. (cmds
  22173. '((?t . org-insert-export-options-template)
  22174. (?v . org-export-visible)
  22175. (?a . org-export-as-ascii)
  22176. (?h . org-export-as-html)
  22177. (?b . org-export-as-html-and-open)
  22178. (?H . org-export-as-html-to-buffer)
  22179. (?R . org-export-region-as-html)
  22180. (?x . org-export-as-xoxo)
  22181. (?l . org-export-as-latex)
  22182. (?L . org-export-as-latex-to-buffer)
  22183. (?i . org-export-icalendar-this-file)
  22184. (?I . org-export-icalendar-all-agenda-files)
  22185. (?c . org-export-icalendar-combine-agenda-files)
  22186. (?F . org-publish-current-file)
  22187. (?P . org-publish-current-project)
  22188. (?X . org-publish)
  22189. (?A . org-publish-all)))
  22190. r1 r2 ass)
  22191. (save-window-excursion
  22192. (delete-other-windows)
  22193. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22194. (princ help))
  22195. (message "Select command: ")
  22196. (setq r1 (read-char-exclusive)))
  22197. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22198. (if (setq ass (assq r2 cmds))
  22199. (call-interactively (cdr ass))
  22200. (error "No command associated with key %c" r1))))
  22201. (defconst org-html-entities
  22202. '(("nbsp")
  22203. ("iexcl")
  22204. ("cent")
  22205. ("pound")
  22206. ("curren")
  22207. ("yen")
  22208. ("brvbar")
  22209. ("vert" . "&#124;")
  22210. ("sect")
  22211. ("uml")
  22212. ("copy")
  22213. ("ordf")
  22214. ("laquo")
  22215. ("not")
  22216. ("shy")
  22217. ("reg")
  22218. ("macr")
  22219. ("deg")
  22220. ("plusmn")
  22221. ("sup2")
  22222. ("sup3")
  22223. ("acute")
  22224. ("micro")
  22225. ("para")
  22226. ("middot")
  22227. ("odot"."o")
  22228. ("star"."*")
  22229. ("cedil")
  22230. ("sup1")
  22231. ("ordm")
  22232. ("raquo")
  22233. ("frac14")
  22234. ("frac12")
  22235. ("frac34")
  22236. ("iquest")
  22237. ("Agrave")
  22238. ("Aacute")
  22239. ("Acirc")
  22240. ("Atilde")
  22241. ("Auml")
  22242. ("Aring") ("AA"."&Aring;")
  22243. ("AElig")
  22244. ("Ccedil")
  22245. ("Egrave")
  22246. ("Eacute")
  22247. ("Ecirc")
  22248. ("Euml")
  22249. ("Igrave")
  22250. ("Iacute")
  22251. ("Icirc")
  22252. ("Iuml")
  22253. ("ETH")
  22254. ("Ntilde")
  22255. ("Ograve")
  22256. ("Oacute")
  22257. ("Ocirc")
  22258. ("Otilde")
  22259. ("Ouml")
  22260. ("times")
  22261. ("Oslash")
  22262. ("Ugrave")
  22263. ("Uacute")
  22264. ("Ucirc")
  22265. ("Uuml")
  22266. ("Yacute")
  22267. ("THORN")
  22268. ("szlig")
  22269. ("agrave")
  22270. ("aacute")
  22271. ("acirc")
  22272. ("atilde")
  22273. ("auml")
  22274. ("aring")
  22275. ("aelig")
  22276. ("ccedil")
  22277. ("egrave")
  22278. ("eacute")
  22279. ("ecirc")
  22280. ("euml")
  22281. ("igrave")
  22282. ("iacute")
  22283. ("icirc")
  22284. ("iuml")
  22285. ("eth")
  22286. ("ntilde")
  22287. ("ograve")
  22288. ("oacute")
  22289. ("ocirc")
  22290. ("otilde")
  22291. ("ouml")
  22292. ("divide")
  22293. ("oslash")
  22294. ("ugrave")
  22295. ("uacute")
  22296. ("ucirc")
  22297. ("uuml")
  22298. ("yacute")
  22299. ("thorn")
  22300. ("yuml")
  22301. ("fnof")
  22302. ("Alpha")
  22303. ("Beta")
  22304. ("Gamma")
  22305. ("Delta")
  22306. ("Epsilon")
  22307. ("Zeta")
  22308. ("Eta")
  22309. ("Theta")
  22310. ("Iota")
  22311. ("Kappa")
  22312. ("Lambda")
  22313. ("Mu")
  22314. ("Nu")
  22315. ("Xi")
  22316. ("Omicron")
  22317. ("Pi")
  22318. ("Rho")
  22319. ("Sigma")
  22320. ("Tau")
  22321. ("Upsilon")
  22322. ("Phi")
  22323. ("Chi")
  22324. ("Psi")
  22325. ("Omega")
  22326. ("alpha")
  22327. ("beta")
  22328. ("gamma")
  22329. ("delta")
  22330. ("epsilon")
  22331. ("varepsilon"."&epsilon;")
  22332. ("zeta")
  22333. ("eta")
  22334. ("theta")
  22335. ("iota")
  22336. ("kappa")
  22337. ("lambda")
  22338. ("mu")
  22339. ("nu")
  22340. ("xi")
  22341. ("omicron")
  22342. ("pi")
  22343. ("rho")
  22344. ("sigmaf") ("varsigma"."&sigmaf;")
  22345. ("sigma")
  22346. ("tau")
  22347. ("upsilon")
  22348. ("phi")
  22349. ("chi")
  22350. ("psi")
  22351. ("omega")
  22352. ("thetasym") ("vartheta"."&thetasym;")
  22353. ("upsih")
  22354. ("piv")
  22355. ("bull") ("bullet"."&bull;")
  22356. ("hellip") ("dots"."&hellip;")
  22357. ("prime")
  22358. ("Prime")
  22359. ("oline")
  22360. ("frasl")
  22361. ("weierp")
  22362. ("image")
  22363. ("real")
  22364. ("trade")
  22365. ("alefsym")
  22366. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22367. ("uarr") ("uparrow"."&uarr;")
  22368. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22369. ("darr")("downarrow"."&darr;")
  22370. ("harr") ("leftrightarrow"."&harr;")
  22371. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22372. ("lArr") ("Leftarrow"."&lArr;")
  22373. ("uArr") ("Uparrow"."&uArr;")
  22374. ("rArr") ("Rightarrow"."&rArr;")
  22375. ("dArr") ("Downarrow"."&dArr;")
  22376. ("hArr") ("Leftrightarrow"."&hArr;")
  22377. ("forall")
  22378. ("part") ("partial"."&part;")
  22379. ("exist") ("exists"."&exist;")
  22380. ("empty") ("emptyset"."&empty;")
  22381. ("nabla")
  22382. ("isin") ("in"."&isin;")
  22383. ("notin")
  22384. ("ni")
  22385. ("prod")
  22386. ("sum")
  22387. ("minus")
  22388. ("lowast") ("ast"."&lowast;")
  22389. ("radic")
  22390. ("prop") ("proptp"."&prop;")
  22391. ("infin") ("infty"."&infin;")
  22392. ("ang") ("angle"."&ang;")
  22393. ("and") ("wedge"."&and;")
  22394. ("or") ("vee"."&or;")
  22395. ("cap")
  22396. ("cup")
  22397. ("int")
  22398. ("there4")
  22399. ("sim")
  22400. ("cong") ("simeq"."&cong;")
  22401. ("asymp")("approx"."&asymp;")
  22402. ("ne") ("neq"."&ne;")
  22403. ("equiv")
  22404. ("le")
  22405. ("ge")
  22406. ("sub") ("subset"."&sub;")
  22407. ("sup") ("supset"."&sup;")
  22408. ("nsub")
  22409. ("sube")
  22410. ("supe")
  22411. ("oplus")
  22412. ("otimes")
  22413. ("perp")
  22414. ("sdot") ("cdot"."&sdot;")
  22415. ("lceil")
  22416. ("rceil")
  22417. ("lfloor")
  22418. ("rfloor")
  22419. ("lang")
  22420. ("rang")
  22421. ("loz") ("Diamond"."&loz;")
  22422. ("spades") ("spadesuit"."&spades;")
  22423. ("clubs") ("clubsuit"."&clubs;")
  22424. ("hearts") ("diamondsuit"."&hearts;")
  22425. ("diams") ("diamondsuit"."&diams;")
  22426. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22427. ("quot")
  22428. ("amp")
  22429. ("lt")
  22430. ("gt")
  22431. ("OElig")
  22432. ("oelig")
  22433. ("Scaron")
  22434. ("scaron")
  22435. ("Yuml")
  22436. ("circ")
  22437. ("tilde")
  22438. ("ensp")
  22439. ("emsp")
  22440. ("thinsp")
  22441. ("zwnj")
  22442. ("zwj")
  22443. ("lrm")
  22444. ("rlm")
  22445. ("ndash")
  22446. ("mdash")
  22447. ("lsquo")
  22448. ("rsquo")
  22449. ("sbquo")
  22450. ("ldquo")
  22451. ("rdquo")
  22452. ("bdquo")
  22453. ("dagger")
  22454. ("Dagger")
  22455. ("permil")
  22456. ("lsaquo")
  22457. ("rsaquo")
  22458. ("euro")
  22459. ("arccos"."arccos")
  22460. ("arcsin"."arcsin")
  22461. ("arctan"."arctan")
  22462. ("arg"."arg")
  22463. ("cos"."cos")
  22464. ("cosh"."cosh")
  22465. ("cot"."cot")
  22466. ("coth"."coth")
  22467. ("csc"."csc")
  22468. ("deg"."deg")
  22469. ("det"."det")
  22470. ("dim"."dim")
  22471. ("exp"."exp")
  22472. ("gcd"."gcd")
  22473. ("hom"."hom")
  22474. ("inf"."inf")
  22475. ("ker"."ker")
  22476. ("lg"."lg")
  22477. ("lim"."lim")
  22478. ("liminf"."liminf")
  22479. ("limsup"."limsup")
  22480. ("ln"."ln")
  22481. ("log"."log")
  22482. ("max"."max")
  22483. ("min"."min")
  22484. ("Pr"."Pr")
  22485. ("sec"."sec")
  22486. ("sin"."sin")
  22487. ("sinh"."sinh")
  22488. ("sup"."sup")
  22489. ("tan"."tan")
  22490. ("tanh"."tanh")
  22491. )
  22492. "Entities for TeX->HTML translation.
  22493. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22494. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22495. In that case, \"\\ent\" will be translated to \"&other;\".
  22496. The list contains HTML entities for Latin-1, Greek and other symbols.
  22497. It is supplemented by a number of commonly used TeX macros with appropriate
  22498. translations. There is currently no way for users to extend this.")
  22499. ;;; General functions for all backends
  22500. (defun org-cleaned-string-for-export (string &rest parameters)
  22501. "Cleanup a buffer STRING so that links can be created safely."
  22502. (interactive)
  22503. (let* ((re-radio (and org-target-link-regexp
  22504. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22505. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22506. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22507. (re-archive (concat ":" org-archive-tag ":"))
  22508. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22509. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22510. (htmlp (plist-get parameters :for-html))
  22511. (asciip (plist-get parameters :for-ascii))
  22512. (latexp (plist-get parameters :for-LaTeX))
  22513. (commentsp (plist-get parameters :comments))
  22514. (archived-trees (plist-get parameters :archived-trees))
  22515. (inhibit-read-only t)
  22516. (drawers org-drawers)
  22517. (exp-drawers (plist-get parameters :drawers))
  22518. (outline-regexp "\\*+ ")
  22519. a b xx
  22520. rtn p)
  22521. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22522. (erase-buffer)
  22523. (insert string)
  22524. ;; Remove license-to-kill stuff
  22525. (while (setq p (text-property-any (point-min) (point-max)
  22526. :org-license-to-kill t))
  22527. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22528. (let ((org-inhibit-startup t)) (org-mode))
  22529. (untabify (point-min) (point-max))
  22530. ;; Get rid of drawers
  22531. (unless (eq t exp-drawers)
  22532. (goto-char (point-min))
  22533. (let ((re (concat "^[ \t]*:\\("
  22534. (mapconcat
  22535. 'identity
  22536. (org-delete-all exp-drawers
  22537. (copy-sequence drawers))
  22538. "\\|")
  22539. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22540. (while (re-search-forward re nil t)
  22541. (replace-match ""))))
  22542. ;; Get the correct stuff before the first headline
  22543. (when (plist-get parameters :skip-before-1st-heading)
  22544. (goto-char (point-min))
  22545. (when (re-search-forward "^\\*+[ \t]" nil t)
  22546. (delete-region (point-min) (match-beginning 0))
  22547. (goto-char (point-min))
  22548. (insert "\n")))
  22549. (when (plist-get parameters :add-text)
  22550. (goto-char (point-min))
  22551. (insert (plist-get parameters :add-text) "\n"))
  22552. ;; Get rid of archived trees
  22553. (when (not (eq archived-trees t))
  22554. (goto-char (point-min))
  22555. (while (re-search-forward re-archive nil t)
  22556. (if (not (org-on-heading-p t))
  22557. (org-end-of-subtree t)
  22558. (beginning-of-line 1)
  22559. (setq a (if archived-trees
  22560. (1+ (point-at-eol)) (point))
  22561. b (org-end-of-subtree t))
  22562. (if (> b a) (delete-region a b)))))
  22563. ;; Find targets in comments and move them out of comments,
  22564. ;; but mark them as targets that should be invisible
  22565. (goto-char (point-min))
  22566. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22567. (replace-match "\\1(INVISIBLE)"))
  22568. ;; Protect backend specific stuff, throw away the others.
  22569. (let ((formatters
  22570. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22571. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22572. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22573. fmt)
  22574. (goto-char (point-min))
  22575. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22576. (goto-char (match-end 0))
  22577. (while (not (looking-at "#\\+END_EXAMPLE"))
  22578. (insert ": ")
  22579. (beginning-of-line 2)))
  22580. (goto-char (point-min))
  22581. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22582. (add-text-properties (match-beginning 0) (match-end 0)
  22583. '(org-protected t)))
  22584. (while formatters
  22585. (setq fmt (pop formatters))
  22586. (when (car fmt)
  22587. (goto-char (point-min))
  22588. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22589. ":[ \t]*\\(.*\\)") nil t)
  22590. (replace-match "\\1" t)
  22591. (add-text-properties
  22592. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22593. '(org-protected t))))
  22594. (goto-char (point-min))
  22595. (while (re-search-forward
  22596. (concat "^#\\+"
  22597. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22598. (cadddr fmt) "\\>.*\n?") nil t)
  22599. (if (car fmt)
  22600. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22601. '(org-protected t))
  22602. (delete-region (match-beginning 0) (match-end 0))))))
  22603. ;; Protect quoted subtrees
  22604. (goto-char (point-min))
  22605. (while (re-search-forward re-quote nil t)
  22606. (goto-char (match-beginning 0))
  22607. (end-of-line 1)
  22608. (add-text-properties (point) (org-end-of-subtree t)
  22609. '(org-protected t)))
  22610. ;; Protect verbatim elements
  22611. (goto-char (point-min))
  22612. (while (re-search-forward org-verbatim-re nil t)
  22613. (add-text-properties (match-beginning 4) (match-end 4)
  22614. '(org-protected t))
  22615. (goto-char (1+ (match-end 4))))
  22616. ;; Remove subtrees that are commented
  22617. (goto-char (point-min))
  22618. (while (re-search-forward re-commented nil t)
  22619. (goto-char (match-beginning 0))
  22620. (delete-region (point) (org-end-of-subtree t)))
  22621. ;; Remove special table lines
  22622. (when org-export-table-remove-special-lines
  22623. (goto-char (point-min))
  22624. (while (re-search-forward "^[ \t]*|" nil t)
  22625. (beginning-of-line 1)
  22626. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22627. (and (looking-at ".*?| *<[0-9]+> *|")
  22628. (not (looking-at ".*?| *[^ <|]"))))
  22629. (delete-region (max (point-min) (1- (point-at-bol)))
  22630. (point-at-eol))
  22631. (end-of-line 1))))
  22632. ;; Specific LaTeX stuff
  22633. (when latexp
  22634. (require 'org-export-latex nil)
  22635. (org-export-latex-cleaned-string))
  22636. (when asciip
  22637. (org-export-ascii-clean-string))
  22638. ;; Specific HTML stuff
  22639. (when htmlp
  22640. ;; Convert LaTeX fragments to images
  22641. (when (plist-get parameters :LaTeX-fragments)
  22642. (org-format-latex
  22643. (concat "ltxpng/" (file-name-sans-extension
  22644. (file-name-nondirectory
  22645. org-current-export-file)))
  22646. org-current-export-dir nil "Creating LaTeX image %s"))
  22647. (message "Exporting..."))
  22648. ;; Remove or replace comments
  22649. (goto-char (point-min))
  22650. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22651. (if commentsp
  22652. (progn (add-text-properties
  22653. (match-beginning 0) (match-end 0) '(org-protected t))
  22654. (replace-match (format commentsp (match-string 1)) t t))
  22655. (replace-match "")))
  22656. ;; Find matches for radio targets and turn them into internal links
  22657. (goto-char (point-min))
  22658. (when re-radio
  22659. (while (re-search-forward re-radio nil t)
  22660. (org-if-unprotected
  22661. (replace-match "\\1[[\\2]]"))))
  22662. ;; Find all links that contain a newline and put them into a single line
  22663. (goto-char (point-min))
  22664. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22665. (org-if-unprotected
  22666. (replace-match "\\1 \\3")
  22667. (goto-char (match-beginning 0))))
  22668. ;; Normalize links: Convert angle and plain links into bracket links
  22669. ;; Expand link abbreviations
  22670. (goto-char (point-min))
  22671. (while (re-search-forward re-plain-link nil t)
  22672. (goto-char (1- (match-end 0)))
  22673. (org-if-unprotected
  22674. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22675. ":" (match-string 3) "]]")))
  22676. ;; added 'org-link face to links
  22677. (put-text-property 0 (length s) 'face 'org-link s)
  22678. (replace-match s t t))))
  22679. (goto-char (point-min))
  22680. (while (re-search-forward re-angle-link nil t)
  22681. (goto-char (1- (match-end 0)))
  22682. (org-if-unprotected
  22683. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22684. ":" (match-string 3) "]]")))
  22685. (put-text-property 0 (length s) 'face 'org-link s)
  22686. (replace-match s t t))))
  22687. (goto-char (point-min))
  22688. (while (re-search-forward org-bracket-link-regexp nil t)
  22689. (org-if-unprotected
  22690. (let* ((s (concat "[[" (setq xx (save-match-data
  22691. (org-link-expand-abbrev (match-string 1))))
  22692. "]"
  22693. (if (match-end 3)
  22694. (match-string 2)
  22695. (concat "[" xx "]"))
  22696. "]")))
  22697. (put-text-property 0 (length s) 'face 'org-link s)
  22698. (replace-match s t t))))
  22699. ;; Find multiline emphasis and put them into single line
  22700. (when (plist-get parameters :emph-multiline)
  22701. (goto-char (point-min))
  22702. (while (re-search-forward org-emph-re nil t)
  22703. (if (not (= (char-after (match-beginning 3))
  22704. (char-after (match-beginning 4))))
  22705. (org-if-unprotected
  22706. (subst-char-in-region (match-beginning 0) (match-end 0)
  22707. ?\n ?\ t)
  22708. (goto-char (1- (match-end 0))))
  22709. (goto-char (1+ (match-beginning 0))))))
  22710. (setq rtn (buffer-string)))
  22711. (kill-buffer " org-mode-tmp")
  22712. rtn))
  22713. (defun org-export-grab-title-from-buffer ()
  22714. "Get a title for the current document, from looking at the buffer."
  22715. (let ((inhibit-read-only t))
  22716. (save-excursion
  22717. (goto-char (point-min))
  22718. (let ((end (save-excursion (outline-next-heading) (point))))
  22719. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22720. ;; Mark the line so that it will not be exported as normal text.
  22721. (org-unmodified
  22722. (add-text-properties (match-beginning 0) (match-end 0)
  22723. (list :org-license-to-kill t)))
  22724. ;; Return the title string
  22725. (org-trim (match-string 0)))))))
  22726. (defun org-export-get-title-from-subtree ()
  22727. "Return subtree title and exclude it from export."
  22728. (let (title (m (mark)))
  22729. (save-excursion
  22730. (goto-char (region-beginning))
  22731. (when (and (org-at-heading-p)
  22732. (>= (org-end-of-subtree t t) (region-end)))
  22733. ;; This is a subtree, we take the title from the first heading
  22734. (goto-char (region-beginning))
  22735. (looking-at org-todo-line-regexp)
  22736. (setq title (match-string 3))
  22737. (org-unmodified
  22738. (add-text-properties (point) (1+ (point-at-eol))
  22739. (list :org-license-to-kill t)))))
  22740. title))
  22741. (defun org-solidify-link-text (s &optional alist)
  22742. "Take link text and make a safe target out of it."
  22743. (save-match-data
  22744. (let* ((rtn
  22745. (mapconcat
  22746. 'identity
  22747. (org-split-string s "[ \t\r\n]+") "--"))
  22748. (a (assoc rtn alist)))
  22749. (or (cdr a) rtn))))
  22750. (defun org-get-min-level (lines)
  22751. "Get the minimum level in LINES."
  22752. (let ((re "^\\(\\*+\\) ") l min)
  22753. (catch 'exit
  22754. (while (setq l (pop lines))
  22755. (if (string-match re l)
  22756. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22757. 1)))
  22758. ;; Variable holding the vector with section numbers
  22759. (defvar org-section-numbers (make-vector org-level-max 0))
  22760. (defun org-init-section-numbers ()
  22761. "Initialize the vector for the section numbers."
  22762. (let* ((level -1)
  22763. (numbers (nreverse (org-split-string "" "\\.")))
  22764. (depth (1- (length org-section-numbers)))
  22765. (i depth) number-string)
  22766. (while (>= i 0)
  22767. (if (> i level)
  22768. (aset org-section-numbers i 0)
  22769. (setq number-string (or (car numbers) "0"))
  22770. (if (string-match "\\`[A-Z]\\'" number-string)
  22771. (aset org-section-numbers i
  22772. (- (string-to-char number-string) ?A -1))
  22773. (aset org-section-numbers i (string-to-number number-string)))
  22774. (pop numbers))
  22775. (setq i (1- i)))))
  22776. (defun org-section-number (&optional level)
  22777. "Return a string with the current section number.
  22778. When LEVEL is non-nil, increase section numbers on that level."
  22779. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22780. (when level
  22781. (when (> level -1)
  22782. (aset org-section-numbers
  22783. level (1+ (aref org-section-numbers level))))
  22784. (setq idx (1+ level))
  22785. (while (<= idx depth)
  22786. (if (not (= idx 1))
  22787. (aset org-section-numbers idx 0))
  22788. (setq idx (1+ idx))))
  22789. (setq idx 0)
  22790. (while (<= idx depth)
  22791. (setq n (aref org-section-numbers idx))
  22792. (setq string (concat string (if (not (string= string "")) "." "")
  22793. (int-to-string n)))
  22794. (setq idx (1+ idx)))
  22795. (save-match-data
  22796. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22797. (setq string (replace-match "" t nil string)))
  22798. (if (string-match "\\(\\.0\\)+\\'" string)
  22799. (setq string (replace-match "" t nil string))))
  22800. string))
  22801. ;;; ASCII export
  22802. (defvar org-last-level nil) ; dynamically scoped variable
  22803. (defvar org-min-level nil) ; dynamically scoped variable
  22804. (defvar org-levels-open nil) ; dynamically scoped parameter
  22805. (defvar org-ascii-current-indentation nil) ; For communication
  22806. (defun org-export-as-ascii (arg)
  22807. "Export the outline as a pretty ASCII file.
  22808. If there is an active region, export only the region.
  22809. The prefix ARG specifies how many levels of the outline should become
  22810. underlined headlines. The default is 3."
  22811. (interactive "P")
  22812. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22813. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22814. (org-infile-export-plist)))
  22815. (region-p (org-region-active-p))
  22816. (subtree-p
  22817. (when region-p
  22818. (save-excursion
  22819. (goto-char (region-beginning))
  22820. (and (org-at-heading-p)
  22821. (>= (org-end-of-subtree t t) (region-end))))))
  22822. (custom-times org-display-custom-times)
  22823. (org-ascii-current-indentation '(0 . 0))
  22824. (level 0) line txt
  22825. (umax nil)
  22826. (umax-toc nil)
  22827. (case-fold-search nil)
  22828. (filename (concat (file-name-as-directory
  22829. (org-export-directory :ascii opt-plist))
  22830. (file-name-sans-extension
  22831. (or (and subtree-p
  22832. (org-entry-get (region-beginning)
  22833. "EXPORT_FILE_NAME" t))
  22834. (file-name-nondirectory buffer-file-name)))
  22835. ".txt"))
  22836. (filename (if (equal (file-truename filename)
  22837. (file-truename buffer-file-name))
  22838. (concat filename ".txt")
  22839. filename))
  22840. (buffer (find-file-noselect filename))
  22841. (org-levels-open (make-vector org-level-max nil))
  22842. (odd org-odd-levels-only)
  22843. (date (plist-get opt-plist :date))
  22844. (author (plist-get opt-plist :author))
  22845. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22846. (plist-get opt-plist :title)
  22847. (and (not
  22848. (plist-get opt-plist :skip-before-1st-heading))
  22849. (org-export-grab-title-from-buffer))
  22850. (file-name-sans-extension
  22851. (file-name-nondirectory buffer-file-name))))
  22852. (email (plist-get opt-plist :email))
  22853. (language (plist-get opt-plist :language))
  22854. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22855. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22856. (todo nil)
  22857. (lang-words nil)
  22858. (region
  22859. (buffer-substring
  22860. (if (org-region-active-p) (region-beginning) (point-min))
  22861. (if (org-region-active-p) (region-end) (point-max))))
  22862. (lines (org-split-string
  22863. (org-cleaned-string-for-export
  22864. region
  22865. :for-ascii t
  22866. :skip-before-1st-heading
  22867. (plist-get opt-plist :skip-before-1st-heading)
  22868. :drawers (plist-get opt-plist :drawers)
  22869. :verbatim-multiline t
  22870. :archived-trees
  22871. (plist-get opt-plist :archived-trees)
  22872. :add-text (plist-get opt-plist :text))
  22873. "\n"))
  22874. thetoc have-headings first-heading-pos
  22875. table-open table-buffer)
  22876. (let ((inhibit-read-only t))
  22877. (org-unmodified
  22878. (remove-text-properties (point-min) (point-max)
  22879. '(:org-license-to-kill t))))
  22880. (setq org-min-level (org-get-min-level lines))
  22881. (setq org-last-level org-min-level)
  22882. (org-init-section-numbers)
  22883. (find-file-noselect filename)
  22884. (setq lang-words (or (assoc language org-export-language-setup)
  22885. (assoc "en" org-export-language-setup)))
  22886. (switch-to-buffer-other-window buffer)
  22887. (erase-buffer)
  22888. (fundamental-mode)
  22889. ;; create local variables for all options, to make sure all called
  22890. ;; functions get the correct information
  22891. (mapc (lambda (x)
  22892. (set (make-local-variable (cdr x))
  22893. (plist-get opt-plist (car x))))
  22894. org-export-plist-vars)
  22895. (org-set-local 'org-odd-levels-only odd)
  22896. (setq umax (if arg (prefix-numeric-value arg)
  22897. org-export-headline-levels))
  22898. (setq umax-toc (if (integerp org-export-with-toc)
  22899. (min org-export-with-toc umax)
  22900. umax))
  22901. ;; File header
  22902. (if title (org-insert-centered title ?=))
  22903. (insert "\n")
  22904. (if (and (or author email)
  22905. org-export-author-info)
  22906. (insert (concat (nth 1 lang-words) ": " (or author "")
  22907. (if email (concat " <" email ">") "")
  22908. "\n")))
  22909. (cond
  22910. ((and date (string-match "%" date))
  22911. (setq date (format-time-string date)))
  22912. (date)
  22913. (t (setq date (format-time-string "%Y/%m/%d %X"))))
  22914. (if (and date org-export-time-stamp-file)
  22915. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22916. (insert "\n\n")
  22917. (if org-export-with-toc
  22918. (progn
  22919. (push (concat (nth 3 lang-words) "\n") thetoc)
  22920. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22921. (mapc '(lambda (line)
  22922. (if (string-match org-todo-line-regexp
  22923. line)
  22924. ;; This is a headline
  22925. (progn
  22926. (setq have-headings t)
  22927. (setq level (- (match-end 1) (match-beginning 1))
  22928. level (org-tr-level level)
  22929. txt (match-string 3 line)
  22930. todo
  22931. (or (and org-export-mark-todo-in-toc
  22932. (match-beginning 2)
  22933. (not (member (match-string 2 line)
  22934. org-done-keywords)))
  22935. ; TODO, not DONE
  22936. (and org-export-mark-todo-in-toc
  22937. (= level umax-toc)
  22938. (org-search-todo-below
  22939. line lines level))))
  22940. (setq txt (org-html-expand-for-ascii txt))
  22941. (while (string-match org-bracket-link-regexp txt)
  22942. (setq txt
  22943. (replace-match
  22944. (match-string (if (match-end 2) 3 1) txt)
  22945. t t txt)))
  22946. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22947. (string-match
  22948. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22949. txt))
  22950. (setq txt (replace-match "" t t txt)))
  22951. (if (string-match quote-re0 txt)
  22952. (setq txt (replace-match "" t t txt)))
  22953. (if org-export-with-section-numbers
  22954. (setq txt (concat (org-section-number level)
  22955. " " txt)))
  22956. (if (<= level umax-toc)
  22957. (progn
  22958. (push
  22959. (concat
  22960. (make-string
  22961. (* (max 0 (- level org-min-level)) 4) ?\ )
  22962. (format (if todo "%s (*)\n" "%s\n") txt))
  22963. thetoc)
  22964. (setq org-last-level level))
  22965. ))))
  22966. lines)
  22967. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22968. (org-init-section-numbers)
  22969. (while (setq line (pop lines))
  22970. ;; Remove the quoted HTML tags.
  22971. (setq line (org-html-expand-for-ascii line))
  22972. ;; Remove targets
  22973. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22974. (setq line (replace-match "" t t line)))
  22975. ;; Replace internal links
  22976. (while (string-match org-bracket-link-regexp line)
  22977. (setq line (replace-match
  22978. (if (match-end 3) "[\\3]" "[\\1]")
  22979. t nil line)))
  22980. (when custom-times
  22981. (setq line (org-translate-time line)))
  22982. (cond
  22983. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22984. ;; a Headline
  22985. (setq first-heading-pos (or first-heading-pos (point)))
  22986. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22987. txt (match-string 2 line))
  22988. (org-ascii-level-start level txt umax lines))
  22989. ((and org-export-with-tables
  22990. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22991. (if (not table-open)
  22992. ;; New table starts
  22993. (setq table-open t table-buffer nil))
  22994. ;; Accumulate lines
  22995. (setq table-buffer (cons line table-buffer))
  22996. (when (or (not lines)
  22997. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22998. (car lines))))
  22999. (setq table-open nil
  23000. table-buffer (nreverse table-buffer))
  23001. (insert (mapconcat
  23002. (lambda (x)
  23003. (org-fix-indentation x org-ascii-current-indentation))
  23004. (org-format-table-ascii table-buffer)
  23005. "\n") "\n")))
  23006. (t
  23007. (setq line (org-fix-indentation line org-ascii-current-indentation))
  23008. (if (and org-export-with-fixed-width
  23009. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  23010. (setq line (replace-match "\\1" nil nil line)))
  23011. (insert line "\n"))))
  23012. (normal-mode)
  23013. ;; insert the table of contents
  23014. (when thetoc
  23015. (goto-char (point-min))
  23016. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  23017. (progn
  23018. (goto-char (match-beginning 0))
  23019. (replace-match ""))
  23020. (goto-char first-heading-pos))
  23021. (mapc 'insert thetoc)
  23022. (or (looking-at "[ \t]*\n[ \t]*\n")
  23023. (insert "\n\n")))
  23024. ;; Convert whitespace place holders
  23025. (goto-char (point-min))
  23026. (let (beg end)
  23027. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23028. (setq end (next-single-property-change beg 'org-whitespace))
  23029. (goto-char beg)
  23030. (delete-region beg end)
  23031. (insert (make-string (- end beg) ?\ ))))
  23032. (save-buffer)
  23033. ;; remove display and invisible chars
  23034. (let (beg end)
  23035. (goto-char (point-min))
  23036. (while (setq beg (next-single-property-change (point) 'display))
  23037. (setq end (next-single-property-change beg 'display))
  23038. (delete-region beg end)
  23039. (goto-char beg)
  23040. (insert "=>"))
  23041. (goto-char (point-min))
  23042. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  23043. (setq end (next-single-property-change beg 'org-cwidth))
  23044. (delete-region beg end)
  23045. (goto-char beg)))
  23046. (goto-char (point-min))))
  23047. (defun org-export-ascii-clean-string ()
  23048. "Do extra work for ASCII export"
  23049. (goto-char (point-min))
  23050. (while (re-search-forward org-verbatim-re nil t)
  23051. (goto-char (match-end 2))
  23052. (backward-delete-char 1) (insert "'")
  23053. (goto-char (match-beginning 2))
  23054. (delete-char 1) (insert "`")
  23055. (goto-char (match-end 2))))
  23056. (defun org-search-todo-below (line lines level)
  23057. "Search the subtree below LINE for any TODO entries."
  23058. (let ((rest (cdr (memq line lines)))
  23059. (re org-todo-line-regexp)
  23060. line lv todo)
  23061. (catch 'exit
  23062. (while (setq line (pop rest))
  23063. (if (string-match re line)
  23064. (progn
  23065. (setq lv (- (match-end 1) (match-beginning 1))
  23066. todo (and (match-beginning 2)
  23067. (not (member (match-string 2 line)
  23068. org-done-keywords))))
  23069. ; TODO, not DONE
  23070. (if (<= lv level) (throw 'exit nil))
  23071. (if todo (throw 'exit t))))))))
  23072. (defun org-html-expand-for-ascii (line)
  23073. "Handle quoted HTML for ASCII export."
  23074. (if org-export-html-expand
  23075. (while (string-match "@<[^<>\n]*>" line)
  23076. ;; We just remove the tags for now.
  23077. (setq line (replace-match "" nil nil line))))
  23078. line)
  23079. (defun org-insert-centered (s &optional underline)
  23080. "Insert the string S centered and underline it with character UNDERLINE."
  23081. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  23082. (insert (make-string ind ?\ ) s "\n")
  23083. (if underline
  23084. (insert (make-string ind ?\ )
  23085. (make-string (string-width s) underline)
  23086. "\n"))))
  23087. (defun org-ascii-level-start (level title umax &optional lines)
  23088. "Insert a new level in ASCII export."
  23089. (let (char (n (- level umax 1)) (ind 0))
  23090. (if (> level umax)
  23091. (progn
  23092. (insert (make-string (* 2 n) ?\ )
  23093. (char-to-string (nth (% n (length org-export-ascii-bullets))
  23094. org-export-ascii-bullets))
  23095. " " title "\n")
  23096. ;; find the indentation of the next non-empty line
  23097. (catch 'stop
  23098. (while lines
  23099. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  23100. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  23101. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  23102. (pop lines)))
  23103. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  23104. (if (or (not (equal (char-before) ?\n))
  23105. (not (equal (char-before (1- (point))) ?\n)))
  23106. (insert "\n"))
  23107. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  23108. (unless org-export-with-tags
  23109. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23110. (setq title (replace-match "" t t title))))
  23111. (if org-export-with-section-numbers
  23112. (setq title (concat (org-section-number level) " " title)))
  23113. (insert title "\n" (make-string (string-width title) char) "\n")
  23114. (setq org-ascii-current-indentation '(0 . 0)))))
  23115. (defun org-export-visible (type arg)
  23116. "Create a copy of the visible part of the current buffer, and export it.
  23117. The copy is created in a temporary buffer and removed after use.
  23118. TYPE is the final key (as a string) that also select the export command in
  23119. the `C-c C-e' export dispatcher.
  23120. As a special case, if the you type SPC at the prompt, the temporary
  23121. org-mode file will not be removed but presented to you so that you can
  23122. continue to use it. The prefix arg ARG is passed through to the exporting
  23123. command."
  23124. (interactive
  23125. (list (progn
  23126. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23127. (read-char-exclusive))
  23128. current-prefix-arg))
  23129. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23130. (error "Invalid export key"))
  23131. (let* ((binding (cdr (assoc type
  23132. '((?a . org-export-as-ascii)
  23133. (?\C-a . org-export-as-ascii)
  23134. (?b . org-export-as-html-and-open)
  23135. (?\C-b . org-export-as-html-and-open)
  23136. (?h . org-export-as-html)
  23137. (?H . org-export-as-html-to-buffer)
  23138. (?R . org-export-region-as-html)
  23139. (?x . org-export-as-xoxo)))))
  23140. (keepp (equal type ?\ ))
  23141. (file buffer-file-name)
  23142. (buffer (get-buffer-create "*Org Export Visible*"))
  23143. s e)
  23144. ;; Need to hack the drawers here.
  23145. (save-excursion
  23146. (goto-char (point-min))
  23147. (while (re-search-forward org-drawer-regexp nil t)
  23148. (goto-char (match-beginning 1))
  23149. (or (org-invisible-p) (org-flag-drawer nil))))
  23150. (with-current-buffer buffer (erase-buffer))
  23151. (save-excursion
  23152. (setq s (goto-char (point-min)))
  23153. (while (not (= (point) (point-max)))
  23154. (goto-char (org-find-invisible))
  23155. (append-to-buffer buffer s (point))
  23156. (setq s (goto-char (org-find-visible))))
  23157. (org-cycle-hide-drawers 'all)
  23158. (goto-char (point-min))
  23159. (unless keepp
  23160. ;; Copy all comment lines to the end, to make sure #+ settings are
  23161. ;; still available for the second export step. Kind of a hack, but
  23162. ;; does do the trick.
  23163. (if (looking-at "#[^\r\n]*")
  23164. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23165. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23166. (append-to-buffer buffer (1+ (match-beginning 0))
  23167. (min (point-max) (1+ (match-end 0))))))
  23168. (set-buffer buffer)
  23169. (let ((buffer-file-name file)
  23170. (org-inhibit-startup t))
  23171. (org-mode)
  23172. (show-all)
  23173. (unless keepp (funcall binding arg))))
  23174. (if (not keepp)
  23175. (kill-buffer buffer)
  23176. (switch-to-buffer-other-window buffer)
  23177. (goto-char (point-min)))))
  23178. (defun org-find-visible ()
  23179. (let ((s (point)))
  23180. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23181. (get-char-property s 'invisible)))
  23182. s))
  23183. (defun org-find-invisible ()
  23184. (let ((s (point)))
  23185. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23186. (not (get-char-property s 'invisible))))
  23187. s))
  23188. ;;; HTML export
  23189. (defun org-get-current-options ()
  23190. "Return a string with current options as keyword options.
  23191. Does include HTML export options as well as TODO and CATEGORY stuff."
  23192. (format
  23193. "#+TITLE: %s
  23194. #+AUTHOR: %s
  23195. #+EMAIL: %s
  23196. #+DATE: %s
  23197. #+LANGUAGE: %s
  23198. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23199. #+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
  23200. #+CATEGORY: %s
  23201. #+SEQ_TODO: %s
  23202. #+TYP_TODO: %s
  23203. #+PRIORITIES: %c %c %c
  23204. #+DRAWERS: %s
  23205. #+STARTUP: %s %s %s %s %s
  23206. #+TAGS: %s
  23207. #+ARCHIVE: %s
  23208. #+LINK: %s
  23209. "
  23210. (buffer-name) (user-full-name) user-mail-address
  23211. (format-time-string (car org-time-stamp-formats))
  23212. org-export-default-language
  23213. org-export-headline-levels
  23214. org-export-with-section-numbers
  23215. org-export-with-toc
  23216. org-export-preserve-breaks
  23217. org-export-html-expand
  23218. org-export-with-fixed-width
  23219. org-export-with-tables
  23220. org-export-with-sub-superscripts
  23221. org-export-with-special-strings
  23222. org-export-with-footnotes
  23223. org-export-with-emphasize
  23224. org-export-with-TeX-macros
  23225. org-export-with-LaTeX-fragments
  23226. org-export-skip-text-before-1st-heading
  23227. org-export-with-drawers
  23228. org-export-with-tags
  23229. (file-name-nondirectory buffer-file-name)
  23230. "TODO FEEDBACK VERIFY DONE"
  23231. "Me Jason Marie DONE"
  23232. org-highest-priority org-lowest-priority org-default-priority
  23233. (mapconcat 'identity org-drawers " ")
  23234. (cdr (assoc org-startup-folded
  23235. '((nil . "showall") (t . "overview") (content . "content"))))
  23236. (if org-odd-levels-only "odd" "oddeven")
  23237. (if org-hide-leading-stars "hidestars" "showstars")
  23238. (if org-startup-align-all-tables "align" "noalign")
  23239. (cond ((eq org-log-done t) "logdone")
  23240. ((equal org-log-done 'note) "lognotedone")
  23241. ((not org-log-done) "nologdone"))
  23242. (or (mapconcat (lambda (x)
  23243. (cond
  23244. ((equal '(:startgroup) x) "{")
  23245. ((equal '(:endgroup) x) "}")
  23246. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23247. (t (car x))))
  23248. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23249. org-archive-location
  23250. "org file:~/org/%s.org"
  23251. ))
  23252. (defun org-insert-export-options-template ()
  23253. "Insert into the buffer a template with information for exporting."
  23254. (interactive)
  23255. (if (not (bolp)) (newline))
  23256. (let ((s (org-get-current-options)))
  23257. (and (string-match "#\\+CATEGORY" s)
  23258. (setq s (substring s 0 (match-beginning 0))))
  23259. (insert s)))
  23260. (defun org-toggle-fixed-width-section (arg)
  23261. "Toggle the fixed-width export.
  23262. If there is no active region, the QUOTE keyword at the current headline is
  23263. inserted or removed. When present, it causes the text between this headline
  23264. and the next to be exported as fixed-width text, and unmodified.
  23265. If there is an active region, this command adds or removes a colon as the
  23266. first character of this line. If the first character of a line is a colon,
  23267. this line is also exported in fixed-width font."
  23268. (interactive "P")
  23269. (let* ((cc 0)
  23270. (regionp (org-region-active-p))
  23271. (beg (if regionp (region-beginning) (point)))
  23272. (end (if regionp (region-end)))
  23273. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23274. (case-fold-search nil)
  23275. (re "[ \t]*\\(:\\)")
  23276. off)
  23277. (if regionp
  23278. (save-excursion
  23279. (goto-char beg)
  23280. (setq cc (current-column))
  23281. (beginning-of-line 1)
  23282. (setq off (looking-at re))
  23283. (while (> nlines 0)
  23284. (setq nlines (1- nlines))
  23285. (beginning-of-line 1)
  23286. (cond
  23287. (arg
  23288. (move-to-column cc t)
  23289. (insert ":\n")
  23290. (forward-line -1))
  23291. ((and off (looking-at re))
  23292. (replace-match "" t t nil 1))
  23293. ((not off) (move-to-column cc t) (insert ":")))
  23294. (forward-line 1)))
  23295. (save-excursion
  23296. (org-back-to-heading)
  23297. (if (looking-at (concat outline-regexp
  23298. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23299. (replace-match "" t t nil 1)
  23300. (if (looking-at outline-regexp)
  23301. (progn
  23302. (goto-char (match-end 0))
  23303. (insert org-quote-string " "))))))))
  23304. (defun org-export-as-html-and-open (arg)
  23305. "Export the outline as HTML and immediately open it with a browser.
  23306. If there is an active region, export only the region.
  23307. The prefix ARG specifies how many levels of the outline should become
  23308. headlines. The default is 3. Lower levels will become bulleted lists."
  23309. (interactive "P")
  23310. (org-export-as-html arg 'hidden)
  23311. (org-open-file buffer-file-name))
  23312. (defun org-export-as-html-batch ()
  23313. "Call `org-export-as-html', may be used in batch processing as
  23314. emacs --batch
  23315. --load=$HOME/lib/emacs/org.el
  23316. --eval \"(setq org-export-headline-levels 2)\"
  23317. --visit=MyFile --funcall org-export-as-html-batch"
  23318. (org-export-as-html org-export-headline-levels 'hidden))
  23319. (defun org-export-as-html-to-buffer (arg)
  23320. "Call `org-exort-as-html` with output to a temporary buffer.
  23321. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23322. (interactive "P")
  23323. (org-export-as-html arg nil nil "*Org HTML Export*")
  23324. (switch-to-buffer-other-window "*Org HTML Export*"))
  23325. (defun org-replace-region-by-html (beg end)
  23326. "Assume the current region has org-mode syntax, and convert it to HTML.
  23327. This can be used in any buffer. For example, you could write an
  23328. itemized list in org-mode syntax in an HTML buffer and then use this
  23329. command to convert it."
  23330. (interactive "r")
  23331. (let (reg html buf pop-up-frames)
  23332. (save-window-excursion
  23333. (if (org-mode-p)
  23334. (setq html (org-export-region-as-html
  23335. beg end t 'string))
  23336. (setq reg (buffer-substring beg end)
  23337. buf (get-buffer-create "*Org tmp*"))
  23338. (with-current-buffer buf
  23339. (erase-buffer)
  23340. (insert reg)
  23341. (org-mode)
  23342. (setq html (org-export-region-as-html
  23343. (point-min) (point-max) t 'string)))
  23344. (kill-buffer buf)))
  23345. (delete-region beg end)
  23346. (insert html)))
  23347. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23348. "Convert region from BEG to END in org-mode buffer to HTML.
  23349. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23350. contents, and only produce the region of converted text, useful for
  23351. cut-and-paste operations.
  23352. If BUFFER is a buffer or a string, use/create that buffer as a target
  23353. of the converted HTML. If BUFFER is the symbol `string', return the
  23354. produced HTML as a string and leave not buffer behind. For example,
  23355. a Lisp program could call this function in the following way:
  23356. (setq html (org-export-region-as-html beg end t 'string))
  23357. When called interactively, the output buffer is selected, and shown
  23358. in a window. A non-interactive call will only retunr the buffer."
  23359. (interactive "r\nP")
  23360. (when (interactive-p)
  23361. (setq buffer "*Org HTML Export*"))
  23362. (let ((transient-mark-mode t) (zmacs-regions t)
  23363. rtn)
  23364. (goto-char end)
  23365. (set-mark (point)) ;; to activate the region
  23366. (goto-char beg)
  23367. (setq rtn (org-export-as-html
  23368. nil nil nil
  23369. buffer body-only))
  23370. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23371. (if (and (interactive-p) (bufferp rtn))
  23372. (switch-to-buffer-other-window rtn)
  23373. rtn)))
  23374. (defvar html-table-tag nil) ; dynamically scoped into this.
  23375. (defun org-export-as-html (arg &optional hidden ext-plist
  23376. to-buffer body-only pub-dir)
  23377. "Export the outline as a pretty HTML file.
  23378. If there is an active region, export only the region. The prefix
  23379. ARG specifies how many levels of the outline should become
  23380. headlines. The default is 3. Lower levels will become bulleted
  23381. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23382. EXT-PLIST is a property list with external parameters overriding
  23383. org-mode's default settings, but still inferior to file-local
  23384. settings. When TO-BUFFER is non-nil, create a buffer with that
  23385. name and export to that buffer. If TO-BUFFER is the symbol
  23386. `string', don't leave any buffer behind but just return the
  23387. resulting HTML as a string. When BODY-ONLY is set, don't produce
  23388. the file header and footer, simply return the content of
  23389. <body>...</body>, without even the body tags themselves. When
  23390. PUB-DIR is set, use this as the publishing directory."
  23391. (interactive "P")
  23392. ;; Make sure we have a file name when we need it.
  23393. (when (and (not (or to-buffer body-only))
  23394. (not buffer-file-name))
  23395. (if (buffer-base-buffer)
  23396. (org-set-local 'buffer-file-name
  23397. (with-current-buffer (buffer-base-buffer)
  23398. buffer-file-name))
  23399. (error "Need a file name to be able to export.")))
  23400. (message "Exporting...")
  23401. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23402. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23403. (setq-default org-done-keywords org-done-keywords)
  23404. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23405. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23406. ext-plist
  23407. (org-infile-export-plist)))
  23408. (style (plist-get opt-plist :style))
  23409. (html-extension (plist-get opt-plist :html-extension))
  23410. (link-validate (plist-get opt-plist :link-validation-function))
  23411. valid thetoc have-headings first-heading-pos
  23412. (odd org-odd-levels-only)
  23413. (region-p (org-region-active-p))
  23414. (subtree-p
  23415. (when region-p
  23416. (save-excursion
  23417. (goto-char (region-beginning))
  23418. (and (org-at-heading-p)
  23419. (>= (org-end-of-subtree t t) (region-end))))))
  23420. ;; The following two are dynamically scoped into other
  23421. ;; routines below.
  23422. (org-current-export-dir
  23423. (or pub-dir (org-export-directory :html opt-plist)))
  23424. (org-current-export-file buffer-file-name)
  23425. (level 0) (line "") (origline "") txt todo
  23426. (umax nil)
  23427. (umax-toc nil)
  23428. (filename (if to-buffer nil
  23429. (expand-file-name
  23430. (concat
  23431. (file-name-sans-extension
  23432. (or (and subtree-p
  23433. (org-entry-get (region-beginning)
  23434. "EXPORT_FILE_NAME" t))
  23435. (file-name-nondirectory buffer-file-name)))
  23436. "." html-extension)
  23437. (file-name-as-directory
  23438. (or pub-dir (org-export-directory :html opt-plist))))))
  23439. (current-dir (if buffer-file-name
  23440. (file-name-directory buffer-file-name)
  23441. default-directory))
  23442. (buffer (if to-buffer
  23443. (cond
  23444. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23445. (t (get-buffer-create to-buffer)))
  23446. (find-file-noselect filename)))
  23447. (org-levels-open (make-vector org-level-max nil))
  23448. (date (plist-get opt-plist :date))
  23449. (author (plist-get opt-plist :author))
  23450. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23451. (plist-get opt-plist :title)
  23452. (and (not
  23453. (plist-get opt-plist :skip-before-1st-heading))
  23454. (org-export-grab-title-from-buffer))
  23455. (and buffer-file-name
  23456. (file-name-sans-extension
  23457. (file-name-nondirectory buffer-file-name)))
  23458. "UNTITLED"))
  23459. (html-table-tag (plist-get opt-plist :html-table-tag))
  23460. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23461. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23462. (inquote nil)
  23463. (infixed nil)
  23464. (in-local-list nil)
  23465. (local-list-num nil)
  23466. (local-list-indent nil)
  23467. (llt org-plain-list-ordered-item-terminator)
  23468. (email (plist-get opt-plist :email))
  23469. (language (plist-get opt-plist :language))
  23470. (lang-words nil)
  23471. (target-alist nil) tg
  23472. (head-count 0) cnt
  23473. (start 0)
  23474. (coding-system (and (boundp 'buffer-file-coding-system)
  23475. buffer-file-coding-system))
  23476. (coding-system-for-write (or org-export-html-coding-system
  23477. coding-system))
  23478. (save-buffer-coding-system (or org-export-html-coding-system
  23479. coding-system))
  23480. (charset (and coding-system-for-write
  23481. (fboundp 'coding-system-get)
  23482. (coding-system-get coding-system-for-write
  23483. 'mime-charset)))
  23484. (region
  23485. (buffer-substring
  23486. (if region-p (region-beginning) (point-min))
  23487. (if region-p (region-end) (point-max))))
  23488. (lines
  23489. (org-split-string
  23490. (org-cleaned-string-for-export
  23491. region
  23492. :emph-multiline t
  23493. :for-html t
  23494. :skip-before-1st-heading
  23495. (plist-get opt-plist :skip-before-1st-heading)
  23496. :drawers (plist-get opt-plist :drawers)
  23497. :archived-trees
  23498. (plist-get opt-plist :archived-trees)
  23499. :add-text
  23500. (plist-get opt-plist :text)
  23501. :LaTeX-fragments
  23502. (plist-get opt-plist :LaTeX-fragments))
  23503. "[\r\n]"))
  23504. table-open type
  23505. table-buffer table-orig-buffer
  23506. ind start-is-num starter didclose
  23507. rpl path desc descp desc1 desc2 link
  23508. snumber fnc
  23509. )
  23510. (let ((inhibit-read-only t))
  23511. (org-unmodified
  23512. (remove-text-properties (point-min) (point-max)
  23513. '(:org-license-to-kill t))))
  23514. (message "Exporting...")
  23515. (setq org-min-level (org-get-min-level lines))
  23516. (setq org-last-level org-min-level)
  23517. (org-init-section-numbers)
  23518. (cond
  23519. ((and date (string-match "%" date))
  23520. (setq date (format-time-string date)))
  23521. (date)
  23522. (t (setq date (format-time-string "%Y/%m/%d %X"))))
  23523. ;; Get the language-dependent settings
  23524. (setq lang-words (or (assoc language org-export-language-setup)
  23525. (assoc "en" org-export-language-setup)))
  23526. ;; Switch to the output buffer
  23527. (set-buffer buffer)
  23528. (let ((inhibit-read-only t)) (erase-buffer))
  23529. (fundamental-mode)
  23530. (and (fboundp 'set-buffer-file-coding-system)
  23531. (set-buffer-file-coding-system coding-system-for-write))
  23532. (let ((case-fold-search nil)
  23533. (org-odd-levels-only odd))
  23534. ;; create local variables for all options, to make sure all called
  23535. ;; functions get the correct information
  23536. (mapc (lambda (x)
  23537. (set (make-local-variable (cdr x))
  23538. (plist-get opt-plist (car x))))
  23539. org-export-plist-vars)
  23540. (setq umax (if arg (prefix-numeric-value arg)
  23541. org-export-headline-levels))
  23542. (setq umax-toc (if (integerp org-export-with-toc)
  23543. (min org-export-with-toc umax)
  23544. umax))
  23545. (unless body-only
  23546. ;; File header
  23547. (insert (format
  23548. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23549. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23550. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23551. lang=\"%s\" xml:lang=\"%s\">
  23552. <head>
  23553. <title>%s</title>
  23554. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23555. <meta name=\"generator\" content=\"Org-mode\"/>
  23556. <meta name=\"generated\" content=\"%s\"/>
  23557. <meta name=\"author\" content=\"%s\"/>
  23558. %s
  23559. </head><body>
  23560. "
  23561. language language (org-html-expand title)
  23562. (or charset "iso-8859-1") date author style))
  23563. (insert (or (plist-get opt-plist :preamble) ""))
  23564. (when (plist-get opt-plist :auto-preamble)
  23565. (if title (insert (format org-export-html-title-format
  23566. (org-html-expand title))))))
  23567. (if (and org-export-with-toc (not body-only))
  23568. (progn
  23569. (push (format "<h%d>%s</h%d>\n"
  23570. org-export-html-toplevel-hlevel
  23571. (nth 3 lang-words)
  23572. org-export-html-toplevel-hlevel)
  23573. thetoc)
  23574. (push "<div id=\"text-table-of-contents\">\n" thetoc)
  23575. (push "<ul>\n<li>" thetoc)
  23576. (setq lines
  23577. (mapcar '(lambda (line)
  23578. (if (string-match org-todo-line-regexp line)
  23579. ;; This is a headline
  23580. (progn
  23581. (setq have-headings t)
  23582. (setq level (- (match-end 1) (match-beginning 1))
  23583. level (org-tr-level level)
  23584. txt (save-match-data
  23585. (org-html-expand
  23586. (org-export-cleanup-toc-line
  23587. (match-string 3 line))))
  23588. todo
  23589. (or (and org-export-mark-todo-in-toc
  23590. (match-beginning 2)
  23591. (not (member (match-string 2 line)
  23592. org-done-keywords)))
  23593. ; TODO, not DONE
  23594. (and org-export-mark-todo-in-toc
  23595. (= level umax-toc)
  23596. (org-search-todo-below
  23597. line lines level))))
  23598. (if (string-match
  23599. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23600. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23601. (if (string-match quote-re0 txt)
  23602. (setq txt (replace-match "" t t txt)))
  23603. (setq snumber (org-section-number level))
  23604. (if org-export-with-section-numbers
  23605. (setq txt (concat snumber " " txt)))
  23606. (if (<= level (max umax umax-toc))
  23607. (setq head-count (+ head-count 1)))
  23608. (if (<= level umax-toc)
  23609. (progn
  23610. (if (> level org-last-level)
  23611. (progn
  23612. (setq cnt (- level org-last-level))
  23613. (while (>= (setq cnt (1- cnt)) 0)
  23614. (push "\n<ul>\n<li>" thetoc))
  23615. (push "\n" thetoc)))
  23616. (if (< level org-last-level)
  23617. (progn
  23618. (setq cnt (- org-last-level level))
  23619. (while (>= (setq cnt (1- cnt)) 0)
  23620. (push "</li>\n</ul>" thetoc))
  23621. (push "\n" thetoc)))
  23622. ;; Check for targets
  23623. (while (string-match org-target-regexp line)
  23624. (setq tg (match-string 1 line)
  23625. line (replace-match
  23626. (concat "@<span class=\"target\">" tg "@</span> ")
  23627. t t line))
  23628. (push (cons (org-solidify-link-text tg)
  23629. (format "sec-%s" snumber))
  23630. target-alist))
  23631. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23632. (setq txt (replace-match "" t t txt)))
  23633. (push
  23634. (format
  23635. (if todo
  23636. "</li>\n<li><a href=\"#sec-%s\"><span class=\"todo\">%s</span></a>"
  23637. "</li>\n<li><a href=\"#sec-%s\">%s</a>")
  23638. snumber txt) thetoc)
  23639. (setq org-last-level level))
  23640. )))
  23641. line)
  23642. lines))
  23643. (while (> org-last-level (1- org-min-level))
  23644. (setq org-last-level (1- org-last-level))
  23645. (push "</li>\n</ul>\n" thetoc))
  23646. (push "</div>\n" thetoc)
  23647. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23648. (setq head-count 0)
  23649. (org-init-section-numbers)
  23650. (while (setq line (pop lines) origline line)
  23651. (catch 'nextline
  23652. ;; end of quote section?
  23653. (when (and inquote (string-match "^\\*+ " line))
  23654. (insert "</pre>\n")
  23655. (setq inquote nil))
  23656. ;; inside a quote section?
  23657. (when inquote
  23658. (insert (org-html-protect line) "\n")
  23659. (throw 'nextline nil))
  23660. ;; verbatim lines
  23661. (when (and org-export-with-fixed-width
  23662. (string-match "^[ \t]*:\\(.*\\)" line))
  23663. (when (not infixed)
  23664. (setq infixed t)
  23665. (insert "<pre>\n"))
  23666. (insert (org-html-protect (match-string 1 line)) "\n")
  23667. (when (and lines
  23668. (not (string-match "^[ \t]*\\(:.*\\)"
  23669. (car lines))))
  23670. (setq infixed nil)
  23671. (insert "</pre>\n"))
  23672. (throw 'nextline nil))
  23673. ;; Protected HTML
  23674. (when (get-text-property 0 'org-protected line)
  23675. (let (par)
  23676. (when (re-search-backward
  23677. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23678. (setq par (match-string 1))
  23679. (replace-match "\\2\n"))
  23680. (insert line "\n")
  23681. (while (and lines
  23682. (or (= (length (car lines)) 0)
  23683. (get-text-property 0 'org-protected (car lines))))
  23684. (insert (pop lines) "\n"))
  23685. (and par (insert "<p>\n")))
  23686. (throw 'nextline nil))
  23687. ;; Horizontal line
  23688. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23689. (insert "\n<hr/>\n")
  23690. (throw 'nextline nil))
  23691. ;; make targets to anchors
  23692. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23693. (cond
  23694. ((match-end 2)
  23695. (setq line (replace-match
  23696. (concat "@<a name=\""
  23697. (org-solidify-link-text (match-string 1 line))
  23698. "\">\\nbsp@</a>")
  23699. t t line)))
  23700. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23701. (setq line (replace-match
  23702. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23703. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23704. t t line)))
  23705. (t
  23706. (setq line (replace-match
  23707. (concat "@<a name=\""
  23708. (org-solidify-link-text (match-string 1 line))
  23709. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23710. t t line)))))
  23711. (setq line (org-html-handle-time-stamps line))
  23712. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23713. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23714. ;; Also handle sub_superscripts and checkboxes
  23715. (or (string-match org-table-hline-regexp line)
  23716. (setq line (org-html-expand line)))
  23717. ;; Format the links
  23718. (setq start 0)
  23719. (while (string-match org-bracket-link-analytic-regexp line start)
  23720. (setq start (match-beginning 0))
  23721. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23722. (setq path (match-string 3 line))
  23723. (setq desc1 (if (match-end 5) (match-string 5 line))
  23724. desc2 (if (match-end 2) (concat type ":" path) path)
  23725. descp (and desc1 (not (equal desc1 desc2)))
  23726. desc (or desc1 desc2))
  23727. ;; Make an image out of the description if that is so wanted
  23728. (when (and descp (org-file-image-p desc))
  23729. (save-match-data
  23730. (if (string-match "^file:" desc)
  23731. (setq desc (substring desc (match-end 0)))))
  23732. (setq desc (concat "<img src=\"" desc "\"/>")))
  23733. ;; FIXME: do we need to unescape here somewhere?
  23734. (cond
  23735. ((equal type "internal")
  23736. (setq rpl
  23737. (concat
  23738. "<a href=\"#"
  23739. (org-solidify-link-text
  23740. (save-match-data (org-link-unescape path)) target-alist)
  23741. "\">" desc "</a>")))
  23742. ((member type '("http" "https"))
  23743. ;; standard URL, just check if we need to inline an image
  23744. (if (and (or (eq t org-export-html-inline-images)
  23745. (and org-export-html-inline-images (not descp)))
  23746. (org-file-image-p path))
  23747. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23748. (setq link (concat type ":" path))
  23749. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23750. ((member type '("ftp" "mailto" "news"))
  23751. ;; standard URL
  23752. (setq link (concat type ":" path))
  23753. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23754. ((string= type "file")
  23755. ;; FILE link
  23756. (let* ((filename path)
  23757. (abs-p (file-name-absolute-p filename))
  23758. thefile file-is-image-p search)
  23759. (save-match-data
  23760. (if (string-match "::\\(.*\\)" filename)
  23761. (setq search (match-string 1 filename)
  23762. filename (replace-match "" t nil filename)))
  23763. (setq valid
  23764. (if (functionp link-validate)
  23765. (funcall link-validate filename current-dir)
  23766. t))
  23767. (setq file-is-image-p (org-file-image-p filename))
  23768. (setq thefile (if abs-p (expand-file-name filename) filename))
  23769. (when (and org-export-html-link-org-files-as-html
  23770. (string-match "\\.org$" thefile))
  23771. (setq thefile (concat (substring thefile 0
  23772. (match-beginning 0))
  23773. "." html-extension))
  23774. (if (and search
  23775. ;; make sure this is can be used as target search
  23776. (not (string-match "^[0-9]*$" search))
  23777. (not (string-match "^\\*" search))
  23778. (not (string-match "^/.*/$" search)))
  23779. (setq thefile (concat thefile "#"
  23780. (org-solidify-link-text
  23781. (org-link-unescape search)))))
  23782. (when (string-match "^file:" desc)
  23783. (setq desc (replace-match "" t t desc))
  23784. (if (string-match "\\.org$" desc)
  23785. (setq desc (replace-match "" t t desc))))))
  23786. (setq rpl (if (and file-is-image-p
  23787. (or (eq t org-export-html-inline-images)
  23788. (and org-export-html-inline-images
  23789. (not descp))))
  23790. (concat "<img src=\"" thefile "\"/>")
  23791. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23792. (if (not valid) (setq rpl desc))))
  23793. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  23794. (setq rpl
  23795. (save-match-data
  23796. (funcall fnc (org-link-unescape path) desc1 'html))))
  23797. (t
  23798. ;; just publish the path, as default
  23799. (setq rpl (concat "<i>&lt;" type ":"
  23800. (save-match-data (org-link-unescape path))
  23801. "&gt;</i>"))))
  23802. (setq line (replace-match rpl t t line)
  23803. start (+ start (length rpl))))
  23804. ;; TODO items
  23805. (if (and (string-match org-todo-line-regexp line)
  23806. (match-beginning 2))
  23807. (setq line
  23808. (concat (substring line 0 (match-beginning 2))
  23809. "<span class=\""
  23810. (if (member (match-string 2 line)
  23811. org-done-keywords)
  23812. "done" "todo")
  23813. "\">" (match-string 2 line)
  23814. "</span>" (substring line (match-end 2)))))
  23815. ;; Does this contain a reference to a footnote?
  23816. (when org-export-with-footnotes
  23817. (setq start 0)
  23818. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23819. (if (get-text-property (match-beginning 2) 'org-protected line)
  23820. (setq start (match-end 2))
  23821. (let ((n (match-string 2 line)))
  23822. (setq line
  23823. (replace-match
  23824. (format
  23825. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23826. (match-string 1 line) n n n)
  23827. t t line))))))
  23828. (cond
  23829. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23830. ;; This is a headline
  23831. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23832. txt (match-string 2 line))
  23833. (if (string-match quote-re0 txt)
  23834. (setq txt (replace-match "" t t txt)))
  23835. (if (<= level (max umax umax-toc))
  23836. (setq head-count (+ head-count 1)))
  23837. (when in-local-list
  23838. ;; Close any local lists before inserting a new header line
  23839. (while local-list-num
  23840. (org-close-li)
  23841. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23842. (pop local-list-num))
  23843. (setq local-list-indent nil
  23844. in-local-list nil))
  23845. (setq first-heading-pos (or first-heading-pos (point)))
  23846. (org-html-level-start level txt umax
  23847. (and org-export-with-toc (<= level umax))
  23848. head-count)
  23849. ;; QUOTES
  23850. (when (string-match quote-re line)
  23851. (insert "<pre>")
  23852. (setq inquote t)))
  23853. ((and org-export-with-tables
  23854. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23855. (if (not table-open)
  23856. ;; New table starts
  23857. (setq table-open t table-buffer nil table-orig-buffer nil))
  23858. ;; Accumulate lines
  23859. (setq table-buffer (cons line table-buffer)
  23860. table-orig-buffer (cons origline table-orig-buffer))
  23861. (when (or (not lines)
  23862. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23863. (car lines))))
  23864. (setq table-open nil
  23865. table-buffer (nreverse table-buffer)
  23866. table-orig-buffer (nreverse table-orig-buffer))
  23867. (org-close-par-maybe)
  23868. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23869. (t
  23870. ;; Normal lines
  23871. (when (string-match
  23872. (cond
  23873. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23874. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23875. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23876. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23877. line)
  23878. (setq ind (org-get-string-indentation line)
  23879. start-is-num (match-beginning 4)
  23880. starter (if (match-beginning 2)
  23881. (substring (match-string 2 line) 0 -1))
  23882. line (substring line (match-beginning 5)))
  23883. (unless (string-match "[^ \t]" line)
  23884. ;; empty line. Pretend indentation is large.
  23885. (setq ind (if org-empty-line-terminates-plain-lists
  23886. 0
  23887. (1+ (or (car local-list-indent) 1)))))
  23888. (setq didclose nil)
  23889. (while (and in-local-list
  23890. (or (and (= ind (car local-list-indent))
  23891. (not starter))
  23892. (< ind (car local-list-indent))))
  23893. (setq didclose t)
  23894. (org-close-li)
  23895. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23896. (pop local-list-num) (pop local-list-indent)
  23897. (setq in-local-list local-list-indent))
  23898. (cond
  23899. ((and starter
  23900. (or (not in-local-list)
  23901. (> ind (car local-list-indent))))
  23902. ;; Start new (level of) list
  23903. (org-close-par-maybe)
  23904. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23905. (push start-is-num local-list-num)
  23906. (push ind local-list-indent)
  23907. (setq in-local-list t))
  23908. (starter
  23909. ;; continue current list
  23910. (org-close-li)
  23911. (insert "<li>\n"))
  23912. (didclose
  23913. ;; we did close a list, normal text follows: need <p>
  23914. (org-open-par)))
  23915. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23916. (setq line
  23917. (replace-match
  23918. (if (equal (match-string 1 line) "X")
  23919. "<b>[X]</b>"
  23920. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23921. t t line))))
  23922. ;; Empty lines start a new paragraph. If hand-formatted lists
  23923. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23924. ;; also start a new paragraph.
  23925. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23926. ;; Is this the start of a footnote?
  23927. (when org-export-with-footnotes
  23928. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23929. (org-close-par-maybe)
  23930. (let ((n (match-string 1 line)))
  23931. (setq line (replace-match
  23932. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23933. ;; Check if the line break needs to be conserved
  23934. (cond
  23935. ((string-match "\\\\\\\\[ \t]*$" line)
  23936. (setq line (replace-match "<br/>" t t line)))
  23937. (org-export-preserve-breaks
  23938. (setq line (concat line "<br/>"))))
  23939. (insert line "\n")))))
  23940. ;; Properly close all local lists and other lists
  23941. (when inquote (insert "</pre>\n"))
  23942. (when in-local-list
  23943. ;; Close any local lists before inserting a new header line
  23944. (while local-list-num
  23945. (org-close-li)
  23946. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23947. (pop local-list-num))
  23948. (setq local-list-indent nil
  23949. in-local-list nil))
  23950. (org-html-level-start 1 nil umax
  23951. (and org-export-with-toc (<= level umax))
  23952. head-count)
  23953. ;; the </div> to lose the last text-... div.
  23954. (insert "</div>\n")
  23955. (unless body-only
  23956. (when (plist-get opt-plist :auto-postamble)
  23957. (insert "<div id=\"postamble\">")
  23958. (when (and org-export-author-info author)
  23959. (insert "<p class=\"author\"> "
  23960. (nth 1 lang-words) ": " author "\n")
  23961. (when email
  23962. (if (listp (split-string email ",+ *"))
  23963. (mapc (lambda(e)
  23964. (insert "<a href=\"mailto:" e "\">&lt;"
  23965. e "&gt;</a>\n"))
  23966. (split-string email ",+ *"))
  23967. (insert "<a href=\"mailto:" email "\">&lt;"
  23968. email "&gt;</a>\n")))
  23969. (insert "</p>\n"))
  23970. (when (and date org-export-time-stamp-file)
  23971. (insert "<p class=\"date\"> "
  23972. (nth 2 lang-words) ": "
  23973. date "</p>\n"))
  23974. (insert "</div>"))
  23975. (if org-export-html-with-timestamp
  23976. (insert org-export-html-html-helper-timestamp))
  23977. (insert (or (plist-get opt-plist :postamble) ""))
  23978. (insert "</body>\n</html>\n"))
  23979. (normal-mode)
  23980. (if (eq major-mode default-major-mode) (html-mode))
  23981. ;; insert the table of contents
  23982. (goto-char (point-min))
  23983. (when thetoc
  23984. (if (or (re-search-forward
  23985. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23986. (re-search-forward
  23987. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23988. (progn
  23989. (goto-char (match-beginning 0))
  23990. (replace-match ""))
  23991. (goto-char first-heading-pos)
  23992. (when (looking-at "\\s-*</p>")
  23993. (goto-char (match-end 0))
  23994. (insert "\n")))
  23995. (insert "<div id=\"table-of-contents\">\n")
  23996. (mapc 'insert thetoc)
  23997. (insert "</div>\n"))
  23998. ;; remove empty paragraphs and lists
  23999. (goto-char (point-min))
  24000. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  24001. (replace-match ""))
  24002. (goto-char (point-min))
  24003. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  24004. (replace-match ""))
  24005. (goto-char (point-min))
  24006. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  24007. (replace-match ""))
  24008. ;; Convert whitespace place holders
  24009. (goto-char (point-min))
  24010. (let (beg end n)
  24011. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  24012. (setq n (get-text-property beg 'org-whitespace)
  24013. end (next-single-property-change beg 'org-whitespace))
  24014. (goto-char beg)
  24015. (delete-region beg end)
  24016. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  24017. (make-string n ?x)))))
  24018. (or to-buffer (save-buffer))
  24019. (goto-char (point-min))
  24020. (message "Exporting... done")
  24021. (if (eq to-buffer 'string)
  24022. (prog1 (buffer-substring (point-min) (point-max))
  24023. (kill-buffer (current-buffer)))
  24024. (current-buffer)))))
  24025. (defvar org-table-colgroup-info nil)
  24026. (defun org-format-table-ascii (lines)
  24027. "Format a table for ascii export."
  24028. (if (stringp lines)
  24029. (setq lines (org-split-string lines "\n")))
  24030. (if (not (string-match "^[ \t]*|" (car lines)))
  24031. ;; Table made by table.el - test for spanning
  24032. lines
  24033. ;; A normal org table
  24034. ;; Get rid of hlines at beginning and end
  24035. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24036. (setq lines (nreverse lines))
  24037. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24038. (setq lines (nreverse lines))
  24039. (when org-export-table-remove-special-lines
  24040. ;; Check if the table has a marking column. If yes remove the
  24041. ;; column and the special lines
  24042. (setq lines (org-table-clean-before-export lines)))
  24043. ;; Get rid of the vertical lines except for grouping
  24044. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  24045. rtn line vl1 start)
  24046. (while (setq line (pop lines))
  24047. (if (string-match org-table-hline-regexp line)
  24048. (and (string-match "|\\(.*\\)|" line)
  24049. (setq line (replace-match " \\1" t nil line)))
  24050. (setq start 0 vl1 vl)
  24051. (while (string-match "|" line start)
  24052. (setq start (match-end 0))
  24053. (or (pop vl1) (setq line (replace-match " " t t line)))))
  24054. (push line rtn))
  24055. (nreverse rtn))))
  24056. (defun org-colgroup-info-to-vline-list (info)
  24057. (let (vl new last)
  24058. (while info
  24059. (setq last new new (pop info))
  24060. (if (or (memq last '(:end :startend))
  24061. (memq new '(:start :startend)))
  24062. (push t vl)
  24063. (push nil vl)))
  24064. (setq vl (nreverse vl))
  24065. (and vl (setcar vl nil))
  24066. vl))
  24067. (defun org-format-table-html (lines olines)
  24068. "Find out which HTML converter to use and return the HTML code."
  24069. (if (stringp lines)
  24070. (setq lines (org-split-string lines "\n")))
  24071. (if (string-match "^[ \t]*|" (car lines))
  24072. ;; A normal org table
  24073. (org-format-org-table-html lines)
  24074. ;; Table made by table.el - test for spanning
  24075. (let* ((hlines (delq nil (mapcar
  24076. (lambda (x)
  24077. (if (string-match "^[ \t]*\\+-" x) x
  24078. nil))
  24079. lines)))
  24080. (first (car hlines))
  24081. (ll (and (string-match "\\S-+" first)
  24082. (match-string 0 first)))
  24083. (re (concat "^[ \t]*" (regexp-quote ll)))
  24084. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  24085. hlines))))
  24086. (if (and (not spanning)
  24087. (not org-export-prefer-native-exporter-for-tables))
  24088. ;; We can use my own converter with HTML conversions
  24089. (org-format-table-table-html lines)
  24090. ;; Need to use the code generator in table.el, with the original text.
  24091. (org-format-table-table-html-using-table-generate-source olines)))))
  24092. (defun org-format-org-table-html (lines &optional splice)
  24093. "Format a table into HTML."
  24094. ;; Get rid of hlines at beginning and end
  24095. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24096. (setq lines (nreverse lines))
  24097. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24098. (setq lines (nreverse lines))
  24099. (when org-export-table-remove-special-lines
  24100. ;; Check if the table has a marking column. If yes remove the
  24101. ;; column and the special lines
  24102. (setq lines (org-table-clean-before-export lines)))
  24103. (let ((head (and org-export-highlight-first-table-line
  24104. (delq nil (mapcar
  24105. (lambda (x) (string-match "^[ \t]*|-" x))
  24106. (cdr lines)))))
  24107. (nlines 0) fnum i
  24108. tbopen line fields html gr colgropen)
  24109. (if splice (setq head nil))
  24110. (unless splice (push (if head "<thead>" "<tbody>") html))
  24111. (setq tbopen t)
  24112. (while (setq line (pop lines))
  24113. (catch 'next-line
  24114. (if (string-match "^[ \t]*|-" line)
  24115. (progn
  24116. (unless splice
  24117. (push (if head "</thead>" "</tbody>") html)
  24118. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24119. (setq head nil) ;; head ends here, first time around
  24120. ;; ignore this line
  24121. (throw 'next-line t)))
  24122. ;; Break the line into fields
  24123. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24124. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24125. (setq nlines (1+ nlines) i -1)
  24126. (push (concat "<tr>"
  24127. (mapconcat
  24128. (lambda (x)
  24129. (setq i (1+ i))
  24130. (if (and (< i nlines)
  24131. (string-match org-table-number-regexp x))
  24132. (incf (aref fnum i)))
  24133. (if head
  24134. (concat (car org-export-table-header-tags) x
  24135. (cdr org-export-table-header-tags))
  24136. (concat (car org-export-table-data-tags) x
  24137. (cdr org-export-table-data-tags))))
  24138. fields "")
  24139. "</tr>")
  24140. html)))
  24141. (unless splice (if tbopen (push "</tbody>" html)))
  24142. (unless splice (push "</table>\n" html))
  24143. (setq html (nreverse html))
  24144. (unless splice
  24145. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24146. (push (mapconcat
  24147. (lambda (x)
  24148. (setq gr (pop org-table-colgroup-info))
  24149. (format "%s<col align=\"%s\"></col>%s"
  24150. (if (memq gr '(:start :startend))
  24151. (prog1
  24152. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24153. (setq colgropen t))
  24154. "")
  24155. (if (> (/ (float x) nlines) org-table-number-fraction)
  24156. "right" "left")
  24157. (if (memq gr '(:end :startend))
  24158. (progn (setq colgropen nil) "</colgroup>")
  24159. "")))
  24160. fnum "")
  24161. html)
  24162. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24163. (push html-table-tag html))
  24164. (concat (mapconcat 'identity html "\n") "\n")))
  24165. (defun org-table-clean-before-export (lines)
  24166. "Check if the table has a marking column.
  24167. If yes remove the column and the special lines."
  24168. (setq org-table-colgroup-info nil)
  24169. (if (memq nil
  24170. (mapcar
  24171. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24172. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24173. lines))
  24174. (progn
  24175. (setq org-table-clean-did-remove-column nil)
  24176. (delq nil
  24177. (mapcar
  24178. (lambda (x)
  24179. (cond
  24180. ((string-match "^[ \t]*| */ *|" x)
  24181. (setq org-table-colgroup-info
  24182. (mapcar (lambda (x)
  24183. (cond ((member x '("<" "&lt;")) :start)
  24184. ((member x '(">" "&gt;")) :end)
  24185. ((member x '("<>" "&lt;&gt;")) :startend)
  24186. (t nil)))
  24187. (org-split-string x "[ \t]*|[ \t]*")))
  24188. nil)
  24189. (t x)))
  24190. lines)))
  24191. (setq org-table-clean-did-remove-column t)
  24192. (delq nil
  24193. (mapcar
  24194. (lambda (x)
  24195. (cond
  24196. ((string-match "^[ \t]*| */ *|" x)
  24197. (setq org-table-colgroup-info
  24198. (mapcar (lambda (x)
  24199. (cond ((member x '("<" "&lt;")) :start)
  24200. ((member x '(">" "&gt;")) :end)
  24201. ((member x '("<>" "&lt;&gt;")) :startend)
  24202. (t nil)))
  24203. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24204. nil)
  24205. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24206. nil) ; ignore this line
  24207. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24208. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24209. ;; remove the first column
  24210. (replace-match "\\1|" t nil x))))
  24211. lines))))
  24212. (defun org-format-table-table-html (lines)
  24213. "Format a table generated by table.el into HTML.
  24214. This conversion does *not* use `table-generate-source' from table.el.
  24215. This has the advantage that Org-mode's HTML conversions can be used.
  24216. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24217. (let (line field-buffer
  24218. (head org-export-highlight-first-table-line)
  24219. fields html empty)
  24220. (setq html (concat html-table-tag "\n"))
  24221. (while (setq line (pop lines))
  24222. (setq empty "&nbsp;")
  24223. (catch 'next-line
  24224. (if (string-match "^[ \t]*\\+-" line)
  24225. (progn
  24226. (if field-buffer
  24227. (progn
  24228. (setq
  24229. html
  24230. (concat
  24231. html
  24232. "<tr>"
  24233. (mapconcat
  24234. (lambda (x)
  24235. (if (equal x "") (setq x empty))
  24236. (if head
  24237. (concat (car org-export-table-header-tags) x
  24238. (cdr org-export-table-header-tags))
  24239. (concat (car org-export-table-data-tags) x
  24240. (cdr org-export-table-data-tags))))
  24241. field-buffer "\n")
  24242. "</tr>\n"))
  24243. (setq head nil)
  24244. (setq field-buffer nil)))
  24245. ;; Ignore this line
  24246. (throw 'next-line t)))
  24247. ;; Break the line into fields and store the fields
  24248. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24249. (if field-buffer
  24250. (setq field-buffer (mapcar
  24251. (lambda (x)
  24252. (concat x "<br/>" (pop fields)))
  24253. field-buffer))
  24254. (setq field-buffer fields))))
  24255. (setq html (concat html "</table>\n"))
  24256. html))
  24257. (defun org-format-table-table-html-using-table-generate-source (lines)
  24258. "Format a table into html, using `table-generate-source' from table.el.
  24259. This has the advantage that cell- or row-spanning is allowed.
  24260. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24261. (require 'table)
  24262. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24263. (erase-buffer)
  24264. (insert (mapconcat 'identity lines "\n"))
  24265. (goto-char (point-min))
  24266. (if (not (re-search-forward "|[^+]" nil t))
  24267. (error "Error processing table"))
  24268. (table-recognize-table)
  24269. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24270. (table-generate-source 'html " org-tmp2 ")
  24271. (set-buffer " org-tmp2 ")
  24272. (buffer-substring (point-min) (point-max))))
  24273. (defun org-html-handle-time-stamps (s)
  24274. "Format time stamps in string S, or remove them."
  24275. (catch 'exit
  24276. (let (r b)
  24277. (while (string-match org-maybe-keyword-time-regexp s)
  24278. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24279. ;; never export CLOCK
  24280. (throw 'exit ""))
  24281. (or b (setq b (substring s 0 (match-beginning 0))))
  24282. (if (not org-export-with-timestamps)
  24283. (setq r (concat r (substring s 0 (match-beginning 0)))
  24284. s (substring s (match-end 0)))
  24285. (setq r (concat
  24286. r (substring s 0 (match-beginning 0))
  24287. (if (match-end 1)
  24288. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24289. (match-string 1 s)))
  24290. (format " @<span class=\"timestamp\">%s@</span>"
  24291. (substring
  24292. (org-translate-time (match-string 3 s)) 1 -1)))
  24293. s (substring s (match-end 0)))))
  24294. ;; Line break if line started and ended with time stamp stuff
  24295. (if (not r)
  24296. s
  24297. (setq r (concat r s))
  24298. (unless (string-match "\\S-" (concat b s))
  24299. (setq r (concat r "@<br/>")))
  24300. r))))
  24301. (defun org-html-protect (s)
  24302. ;; convert & to &amp;, < to &lt; and > to &gt;
  24303. (let ((start 0))
  24304. (while (string-match "&" s start)
  24305. (setq s (replace-match "&amp;" t t s)
  24306. start (1+ (match-beginning 0))))
  24307. (while (string-match "<" s)
  24308. (setq s (replace-match "&lt;" t t s)))
  24309. (while (string-match ">" s)
  24310. (setq s (replace-match "&gt;" t t s))))
  24311. s)
  24312. (defun org-export-cleanup-toc-line (s)
  24313. "Remove tags and time staps from lines going into the toc."
  24314. (when (memq org-export-with-tags '(not-in-toc nil))
  24315. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24316. (setq s (replace-match "" t t s))))
  24317. (when org-export-remove-timestamps-from-toc
  24318. (while (string-match org-maybe-keyword-time-regexp s)
  24319. (setq s (replace-match "" t t s))))
  24320. (while (string-match org-bracket-link-regexp s)
  24321. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24322. t t s)))
  24323. s)
  24324. (defun org-html-expand (string)
  24325. "Prepare STRING for HTML export. Applies all active conversions.
  24326. If there are links in the string, don't modify these."
  24327. (let* ((re (concat org-bracket-link-regexp "\\|"
  24328. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24329. m s l res)
  24330. (while (setq m (string-match re string))
  24331. (setq s (substring string 0 m)
  24332. l (match-string 0 string)
  24333. string (substring string (match-end 0)))
  24334. (push (org-html-do-expand s) res)
  24335. (push l res))
  24336. (push (org-html-do-expand string) res)
  24337. (apply 'concat (nreverse res))))
  24338. (defun org-html-do-expand (s)
  24339. "Apply all active conversions to translate special ASCII to HTML."
  24340. (setq s (org-html-protect s))
  24341. (if org-export-html-expand
  24342. (let ((start 0))
  24343. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24344. (setq s (replace-match "<\\1>" t nil s)))))
  24345. (if org-export-with-emphasize
  24346. (setq s (org-export-html-convert-emphasize s)))
  24347. (if org-export-with-special-strings
  24348. (setq s (org-export-html-convert-special-strings s)))
  24349. (if org-export-with-sub-superscripts
  24350. (setq s (org-export-html-convert-sub-super s)))
  24351. (if org-export-with-TeX-macros
  24352. (let ((start 0) wd ass)
  24353. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24354. (if (get-text-property (match-beginning 0) 'org-protected s)
  24355. (setq start (match-end 0))
  24356. (setq wd (match-string 1 s))
  24357. (if (setq ass (assoc wd org-html-entities))
  24358. (setq s (replace-match (or (cdr ass)
  24359. (concat "&" (car ass) ";"))
  24360. t t s))
  24361. (setq start (+ start (length wd))))))))
  24362. s)
  24363. (defun org-create-multibrace-regexp (left right n)
  24364. "Create a regular expression which will match a balanced sexp.
  24365. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24366. as single character strings.
  24367. The regexp returned will match the entire expression including the
  24368. delimiters. It will also define a single group which contains the
  24369. match except for the outermost delimiters. The maximum depth of
  24370. stacked delimiters is N. Escaping delimiters is not possible."
  24371. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24372. (or "\\|")
  24373. (re nothing)
  24374. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24375. (while (> n 1)
  24376. (setq n (1- n)
  24377. re (concat re or next)
  24378. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24379. (concat left "\\(" re "\\)" right)))
  24380. (defvar org-match-substring-regexp
  24381. (concat
  24382. "\\([^\\]\\)\\([_^]\\)\\("
  24383. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24384. "\\|"
  24385. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24386. "\\|"
  24387. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24388. "The regular expression matching a sub- or superscript.")
  24389. (defvar org-match-substring-with-braces-regexp
  24390. (concat
  24391. "\\([^\\]\\)\\([_^]\\)\\("
  24392. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24393. "\\)")
  24394. "The regular expression matching a sub- or superscript, forcing braces.")
  24395. (defconst org-export-html-special-string-regexps
  24396. '(("\\\\-" . "&shy;")
  24397. ("---\\([^-]\\)" . "&mdash;\\1")
  24398. ("--\\([^-]\\)" . "&ndash;\\1")
  24399. ("\\.\\.\\." . "&hellip;"))
  24400. "Regular expressions for special string conversion.")
  24401. (defun org-export-html-convert-special-strings (string)
  24402. "Convert special characters in STRING to HTML."
  24403. (let ((all org-export-html-special-string-regexps)
  24404. e a re rpl start)
  24405. (while (setq a (pop all))
  24406. (setq re (car a) rpl (cdr a) start 0)
  24407. (while (string-match re string start)
  24408. (if (get-text-property (match-beginning 0) 'org-protected string)
  24409. (setq start (match-end 0))
  24410. (setq string (replace-match rpl t nil string)))))
  24411. string))
  24412. (defun org-export-html-convert-sub-super (string)
  24413. "Convert sub- and superscripts in STRING to HTML."
  24414. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24415. (while (string-match org-match-substring-regexp string s)
  24416. (cond
  24417. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24418. ((get-text-property (match-beginning 2) 'org-protected string)
  24419. (setq s (match-end 2)))
  24420. (t
  24421. (setq s (match-end 1)
  24422. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24423. c (or (match-string 8 string)
  24424. (match-string 6 string)
  24425. (match-string 5 string))
  24426. string (replace-match
  24427. (concat (match-string 1 string)
  24428. "<" key ">" c "</" key ">")
  24429. t t string)))))
  24430. (while (string-match "\\\\\\([_^]\\)" string)
  24431. (setq string (replace-match (match-string 1 string) t t string)))
  24432. string))
  24433. (defun org-export-html-convert-emphasize (string)
  24434. "Apply emphasis."
  24435. (let ((s 0) rpl)
  24436. (while (string-match org-emph-re string s)
  24437. (if (not (equal
  24438. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24439. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24440. (setq s (match-beginning 0)
  24441. rpl
  24442. (concat
  24443. (match-string 1 string)
  24444. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24445. (match-string 4 string)
  24446. (nth 3 (assoc (match-string 3 string)
  24447. org-emphasis-alist))
  24448. (match-string 5 string))
  24449. string (replace-match rpl t t string)
  24450. s (+ s (- (length rpl) 2)))
  24451. (setq s (1+ s))))
  24452. string))
  24453. (defvar org-par-open nil)
  24454. (defun org-open-par ()
  24455. "Insert <p>, but first close previous paragraph if any."
  24456. (org-close-par-maybe)
  24457. (insert "\n<p>")
  24458. (setq org-par-open t))
  24459. (defun org-close-par-maybe ()
  24460. "Close paragraph if there is one open."
  24461. (when org-par-open
  24462. (insert "</p>")
  24463. (setq org-par-open nil)))
  24464. (defun org-close-li ()
  24465. "Close <li> if necessary."
  24466. (org-close-par-maybe)
  24467. (insert "</li>\n"))
  24468. (defvar body-only) ; dynamically scoped into this.
  24469. (defun org-html-level-start (level title umax with-toc head-count)
  24470. "Insert a new level in HTML export.
  24471. When TITLE is nil, just close all open levels."
  24472. (org-close-par-maybe)
  24473. (let ((l org-level-max) snumber)
  24474. (while (>= l level)
  24475. (if (aref org-levels-open (1- l))
  24476. (progn
  24477. (org-html-level-close l umax)
  24478. (aset org-levels-open (1- l) nil)))
  24479. (setq l (1- l)))
  24480. (when title
  24481. ;; If title is nil, this means this function is called to close
  24482. ;; all levels, so the rest is done only if title is given
  24483. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24484. (setq title (replace-match
  24485. (if org-export-with-tags
  24486. (save-match-data
  24487. (concat
  24488. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24489. (mapconcat 'identity (org-split-string
  24490. (match-string 1 title) ":")
  24491. "&nbsp;")
  24492. "</span>"))
  24493. "")
  24494. t t title)))
  24495. (if (> level umax)
  24496. (progn
  24497. (if (aref org-levels-open (1- level))
  24498. (progn
  24499. (org-close-li)
  24500. (insert "<li>" title "<br/>\n"))
  24501. (aset org-levels-open (1- level) t)
  24502. (org-close-par-maybe)
  24503. (insert "<ul>\n<li>" title "<br/>\n")))
  24504. (aset org-levels-open (1- level) t)
  24505. (setq snumber (org-section-number level))
  24506. (if (and org-export-with-section-numbers (not body-only))
  24507. (setq title (concat snumber " " title)))
  24508. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24509. (unless (= head-count 1) (insert "\n</div>\n"))
  24510. (insert (format "\n<div id=\"outline-container-%s\" class=\"outline-%d\">\n<h%d id=\"sec-%s\">%s</h%d>\n<div id=\"text-%s\">\n"
  24511. snumber level level snumber title level snumber))
  24512. (org-open-par)))))
  24513. (defun org-html-level-close (level max-outline-level)
  24514. "Terminate one level in HTML export."
  24515. (if (<= level max-outline-level)
  24516. (insert "</div>\n")
  24517. (org-close-li)
  24518. (insert "</ul>\n")))
  24519. ;;; iCalendar export
  24520. ;;;###autoload
  24521. (defun org-export-icalendar-this-file ()
  24522. "Export current file as an iCalendar file.
  24523. The iCalendar file will be located in the same directory as the Org-mode
  24524. file, but with extension `.ics'."
  24525. (interactive)
  24526. (org-export-icalendar nil buffer-file-name))
  24527. ;;;###autoload
  24528. (defun org-export-icalendar-all-agenda-files ()
  24529. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24530. Each iCalendar file will be located in the same directory as the Org-mode
  24531. file, but with extension `.ics'."
  24532. (interactive)
  24533. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24534. ;;;###autoload
  24535. (defun org-export-icalendar-combine-agenda-files ()
  24536. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24537. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24538. (interactive)
  24539. (apply 'org-export-icalendar t (org-agenda-files t)))
  24540. (defun org-export-icalendar (combine &rest files)
  24541. "Create iCalendar files for all elements of FILES.
  24542. If COMBINE is non-nil, combine all calendar entries into a single large
  24543. file and store it under the name `org-combined-agenda-icalendar-file'."
  24544. (save-excursion
  24545. (org-prepare-agenda-buffers files)
  24546. (let* ((dir (org-export-directory
  24547. :ical (list :publishing-directory
  24548. org-export-publishing-directory)))
  24549. file ical-file ical-buffer category started org-agenda-new-buffers)
  24550. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24551. (when combine
  24552. (setq ical-file
  24553. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24554. org-combined-agenda-icalendar-file
  24555. (expand-file-name org-combined-agenda-icalendar-file dir))
  24556. ical-buffer (org-get-agenda-file-buffer ical-file))
  24557. (set-buffer ical-buffer) (erase-buffer))
  24558. (while (setq file (pop files))
  24559. (catch 'nextfile
  24560. (org-check-agenda-file file)
  24561. (set-buffer (org-get-agenda-file-buffer file))
  24562. (unless combine
  24563. (setq ical-file (concat (file-name-as-directory dir)
  24564. (file-name-sans-extension
  24565. (file-name-nondirectory buffer-file-name))
  24566. ".ics"))
  24567. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24568. (with-current-buffer ical-buffer (erase-buffer)))
  24569. (setq category (or org-category
  24570. (file-name-sans-extension
  24571. (file-name-nondirectory buffer-file-name))))
  24572. (if (symbolp category) (setq category (symbol-name category)))
  24573. (let ((standard-output ical-buffer))
  24574. (if combine
  24575. (and (not started) (setq started t)
  24576. (org-start-icalendar-file org-icalendar-combined-name))
  24577. (org-start-icalendar-file category))
  24578. (org-print-icalendar-entries combine)
  24579. (when (or (and combine (not files)) (not combine))
  24580. (org-finish-icalendar-file)
  24581. (set-buffer ical-buffer)
  24582. (save-buffer)
  24583. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24584. (org-release-buffers org-agenda-new-buffers))))
  24585. (defvar org-after-save-iCalendar-file-hook nil
  24586. "Hook run after an iCalendar file has been saved.
  24587. The iCalendar buffer is still current when this hook is run.
  24588. A good way to use this is to tell a desktop calenndar application to re-read
  24589. the iCalendar file.")
  24590. (defun org-print-icalendar-entries (&optional combine)
  24591. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24592. When COMBINE is non nil, add the category to each line."
  24593. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24594. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24595. (dts (org-ical-ts-to-string
  24596. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24597. "DTSTART"))
  24598. hd ts ts2 state status (inc t) pos b sexp rrule
  24599. scheduledp deadlinep tmp pri category entry location summary desc
  24600. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24601. (org-refresh-category-properties)
  24602. (save-excursion
  24603. (goto-char (point-min))
  24604. (while (re-search-forward re1 nil t)
  24605. (catch :skip
  24606. (org-agenda-skip)
  24607. (when (boundp 'org-icalendar-verify-function)
  24608. (unless (funcall org-icalendar-verify-function)
  24609. (outline-next-heading)
  24610. (backward-char 1)
  24611. (throw :skip nil)))
  24612. (setq pos (match-beginning 0)
  24613. ts (match-string 0)
  24614. inc t
  24615. hd (org-get-heading)
  24616. summary (org-icalendar-cleanup-string
  24617. (org-entry-get nil "SUMMARY"))
  24618. desc (org-icalendar-cleanup-string
  24619. (or (org-entry-get nil "DESCRIPTION")
  24620. (and org-icalendar-include-body (org-get-entry)))
  24621. t org-icalendar-include-body)
  24622. location (org-icalendar-cleanup-string
  24623. (org-entry-get nil "LOCATION"))
  24624. category (org-get-category))
  24625. (if (looking-at re2)
  24626. (progn
  24627. (goto-char (match-end 0))
  24628. (setq ts2 (match-string 1) inc nil))
  24629. (setq tmp (buffer-substring (max (point-min)
  24630. (- pos org-ds-keyword-length))
  24631. pos)
  24632. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24633. (progn
  24634. (setq inc nil)
  24635. (replace-match "\\1" t nil ts))
  24636. ts)
  24637. deadlinep (string-match org-deadline-regexp tmp)
  24638. scheduledp (string-match org-scheduled-regexp tmp)
  24639. ;; donep (org-entry-is-done-p)
  24640. ))
  24641. (if (or (string-match org-tr-regexp hd)
  24642. (string-match org-ts-regexp hd))
  24643. (setq hd (replace-match "" t t hd)))
  24644. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24645. (setq rrule
  24646. (concat "\nRRULE:FREQ="
  24647. (cdr (assoc
  24648. (match-string 2 ts)
  24649. '(("d" . "DAILY")("w" . "WEEKLY")
  24650. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24651. ";INTERVAL=" (match-string 1 ts)))
  24652. (setq rrule ""))
  24653. (setq summary (or summary hd))
  24654. (if (string-match org-bracket-link-regexp summary)
  24655. (setq summary
  24656. (replace-match (if (match-end 3)
  24657. (match-string 3 summary)
  24658. (match-string 1 summary))
  24659. t t summary)))
  24660. (if deadlinep (setq summary (concat "DL: " summary)))
  24661. (if scheduledp (setq summary (concat "S: " summary)))
  24662. (if (string-match "\\`<%%" ts)
  24663. (with-current-buffer sexp-buffer
  24664. (insert (substring ts 1 -1) " " summary "\n"))
  24665. (princ (format "BEGIN:VEVENT
  24666. %s
  24667. %s%s
  24668. SUMMARY:%s%s%s
  24669. CATEGORIES:%s
  24670. END:VEVENT\n"
  24671. (org-ical-ts-to-string ts "DTSTART")
  24672. (org-ical-ts-to-string ts2 "DTEND" inc)
  24673. rrule summary
  24674. (if (and desc (string-match "\\S-" desc))
  24675. (concat "\nDESCRIPTION: " desc) "")
  24676. (if (and location (string-match "\\S-" location))
  24677. (concat "\nLOCATION: " location) "")
  24678. category)))))
  24679. (when (and org-icalendar-include-sexps
  24680. (condition-case nil (require 'icalendar) (error nil))
  24681. (fboundp 'icalendar-export-region))
  24682. ;; Get all the literal sexps
  24683. (goto-char (point-min))
  24684. (while (re-search-forward "^&?%%(" nil t)
  24685. (catch :skip
  24686. (org-agenda-skip)
  24687. (setq b (match-beginning 0))
  24688. (goto-char (1- (match-end 0)))
  24689. (forward-sexp 1)
  24690. (end-of-line 1)
  24691. (setq sexp (buffer-substring b (point)))
  24692. (with-current-buffer sexp-buffer
  24693. (insert sexp "\n"))
  24694. (princ (org-diary-to-ical-string sexp-buffer)))))
  24695. (when org-icalendar-include-todo
  24696. (goto-char (point-min))
  24697. (while (re-search-forward org-todo-line-regexp nil t)
  24698. (catch :skip
  24699. (org-agenda-skip)
  24700. (when (boundp 'org-icalendar-verify-function)
  24701. (unless (funcall org-icalendar-verify-function)
  24702. (outline-next-heading)
  24703. (backward-char 1)
  24704. (throw :skip nil)))
  24705. (setq state (match-string 2))
  24706. (setq status (if (member state org-done-keywords)
  24707. "COMPLETED" "NEEDS-ACTION"))
  24708. (when (and state
  24709. (or (not (member state org-done-keywords))
  24710. (eq org-icalendar-include-todo 'all))
  24711. (not (member org-archive-tag (org-get-tags-at)))
  24712. )
  24713. (setq hd (match-string 3)
  24714. summary (org-icalendar-cleanup-string
  24715. (org-entry-get nil "SUMMARY"))
  24716. desc (org-icalendar-cleanup-string
  24717. (or (org-entry-get nil "DESCRIPTION")
  24718. (and org-icalendar-include-body (org-get-entry)))
  24719. t org-icalendar-include-body)
  24720. location (org-icalendar-cleanup-string
  24721. (org-entry-get nil "LOCATION")))
  24722. (if (string-match org-bracket-link-regexp hd)
  24723. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24724. (match-string 1 hd))
  24725. t t hd)))
  24726. (if (string-match org-priority-regexp hd)
  24727. (setq pri (string-to-char (match-string 2 hd))
  24728. hd (concat (substring hd 0 (match-beginning 1))
  24729. (substring hd (match-end 1))))
  24730. (setq pri org-default-priority))
  24731. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24732. (- org-lowest-priority org-highest-priority))))))
  24733. (princ (format "BEGIN:VTODO
  24734. %s
  24735. SUMMARY:%s%s%s
  24736. CATEGORIES:%s
  24737. SEQUENCE:1
  24738. PRIORITY:%d
  24739. STATUS:%s
  24740. END:VTODO\n"
  24741. dts
  24742. (or summary hd)
  24743. (if (and location (string-match "\\S-" location))
  24744. (concat "\nLOCATION: " location) "")
  24745. (if (and desc (string-match "\\S-" desc))
  24746. (concat "\nDESCRIPTION: " desc) "")
  24747. category pri status)))))))))
  24748. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24749. "Take out stuff and quote what needs to be quoted.
  24750. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24751. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24752. characters."
  24753. (if (not s)
  24754. nil
  24755. (when is-body
  24756. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24757. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24758. (while (string-match re s) (setq s (replace-match "" t t s)))
  24759. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24760. (let ((start 0))
  24761. (while (string-match "\\([,;\\]\\)" s start)
  24762. (setq start (+ (match-beginning 0) 2)
  24763. s (replace-match "\\\\\\1" nil nil s))))
  24764. (when is-body
  24765. (while (string-match "[ \t]*\n[ \t]*" s)
  24766. (setq s (replace-match "\\n" t t s))))
  24767. (setq s (org-trim s))
  24768. (if is-body
  24769. (if maxlength
  24770. (if (and (numberp maxlength)
  24771. (> (length s) maxlength))
  24772. (setq s (substring s 0 maxlength)))))
  24773. s))
  24774. (defun org-get-entry ()
  24775. "Clean-up description string."
  24776. (save-excursion
  24777. (org-back-to-heading t)
  24778. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24779. (defun org-start-icalendar-file (name)
  24780. "Start an iCalendar file by inserting the header."
  24781. (let ((user user-full-name)
  24782. (name (or name "unknown"))
  24783. (timezone (cadr (current-time-zone))))
  24784. (princ
  24785. (format "BEGIN:VCALENDAR
  24786. VERSION:2.0
  24787. X-WR-CALNAME:%s
  24788. PRODID:-//%s//Emacs with Org-mode//EN
  24789. X-WR-TIMEZONE:%s
  24790. CALSCALE:GREGORIAN\n" name user timezone))))
  24791. (defun org-finish-icalendar-file ()
  24792. "Finish an iCalendar file by inserting the END statement."
  24793. (princ "END:VCALENDAR\n"))
  24794. (defun org-ical-ts-to-string (s keyword &optional inc)
  24795. "Take a time string S and convert it to iCalendar format.
  24796. KEYWORD is added in front, to make a complete line like DTSTART....
  24797. When INC is non-nil, increase the hour by two (if time string contains
  24798. a time), or the day by one (if it does not contain a time)."
  24799. (let ((t1 (org-parse-time-string s 'nodefault))
  24800. t2 fmt have-time time)
  24801. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24802. (setq t2 t1 have-time t)
  24803. (setq t2 (org-parse-time-string s)))
  24804. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24805. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24806. (when inc
  24807. (if have-time
  24808. (if org-agenda-default-appointment-duration
  24809. (setq mi (+ org-agenda-default-appointment-duration mi))
  24810. (setq h (+ 2 h)))
  24811. (setq d (1+ d))))
  24812. (setq time (encode-time s mi h d m y)))
  24813. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24814. (concat keyword (format-time-string fmt time))))
  24815. ;;; XOXO export
  24816. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24817. (with-current-buffer buffer
  24818. (apply 'insert output)))
  24819. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24820. (defun org-export-as-xoxo (&optional buffer)
  24821. "Export the org buffer as XOXO.
  24822. The XOXO buffer is named *xoxo-<source buffer name>*"
  24823. (interactive (list (current-buffer)))
  24824. ;; A quickie abstraction
  24825. ;; Output everything as XOXO
  24826. (with-current-buffer (get-buffer buffer)
  24827. (let* ((pos (point))
  24828. (opt-plist (org-combine-plists (org-default-export-plist)
  24829. (org-infile-export-plist)))
  24830. (filename (concat (file-name-as-directory
  24831. (org-export-directory :xoxo opt-plist))
  24832. (file-name-sans-extension
  24833. (file-name-nondirectory buffer-file-name))
  24834. ".html"))
  24835. (out (find-file-noselect filename))
  24836. (last-level 1)
  24837. (hanging-li nil))
  24838. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24839. ;; Check the output buffer is empty.
  24840. (with-current-buffer out (erase-buffer))
  24841. ;; Kick off the output
  24842. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24843. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24844. (let* ((hd (match-string-no-properties 1))
  24845. (level (length hd))
  24846. (text (concat
  24847. (match-string-no-properties 2)
  24848. (save-excursion
  24849. (goto-char (match-end 0))
  24850. (let ((str ""))
  24851. (catch 'loop
  24852. (while 't
  24853. (forward-line)
  24854. (if (looking-at "^[ \t]\\(.*\\)")
  24855. (setq str (concat str (match-string-no-properties 1)))
  24856. (throw 'loop str)))))))))
  24857. ;; Handle level rendering
  24858. (cond
  24859. ((> level last-level)
  24860. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24861. ((< level last-level)
  24862. (dotimes (- (- last-level level) 1)
  24863. (if hanging-li
  24864. (org-export-as-xoxo-insert-into out "</li>\n"))
  24865. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24866. (when hanging-li
  24867. (org-export-as-xoxo-insert-into out "</li>\n")
  24868. (setq hanging-li nil)))
  24869. ((equal level last-level)
  24870. (if hanging-li
  24871. (org-export-as-xoxo-insert-into out "</li>\n")))
  24872. )
  24873. (setq last-level level)
  24874. ;; And output the new li
  24875. (setq hanging-li 't)
  24876. (if (equal ?+ (elt text 0))
  24877. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24878. (org-export-as-xoxo-insert-into out "<li>" text))))
  24879. ;; Finally finish off the ol
  24880. (dotimes (- last-level 1)
  24881. (if hanging-li
  24882. (org-export-as-xoxo-insert-into out "</li>\n"))
  24883. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24884. (goto-char pos)
  24885. ;; Finish the buffer off and clean it up.
  24886. (switch-to-buffer-other-window out)
  24887. (indent-region (point-min) (point-max) nil)
  24888. (save-buffer)
  24889. (goto-char (point-min))
  24890. )))
  24891. ;;;; Key bindings
  24892. ;; Make `C-c C-x' a prefix key
  24893. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24894. ;; TAB key with modifiers
  24895. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24896. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24897. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24898. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24899. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24900. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24901. ;; The following line is necessary under Suse GNU/Linux
  24902. (unless (featurep 'xemacs)
  24903. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24904. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24905. (define-key org-mode-map [backtab] 'org-shifttab)
  24906. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24907. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24908. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24909. ;; Cursor keys with modifiers
  24910. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24911. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24912. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24913. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24914. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24915. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24916. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24917. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24918. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24919. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24920. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24921. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24922. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24923. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24924. ;;; Extra keys for tty access.
  24925. ;; We only set them when really needed because otherwise the
  24926. ;; menus don't show the simple keys
  24927. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24928. (not window-system))
  24929. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24930. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24931. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24932. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24933. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24934. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24935. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24936. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24937. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24938. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24939. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24940. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24941. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24942. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24943. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24944. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24945. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24946. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24947. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24948. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24949. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24950. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24951. ;; All the other keys
  24952. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24953. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24954. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24955. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24956. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24957. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24958. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24959. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24960. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24961. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24962. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24963. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24964. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24965. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24966. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24967. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24968. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24969. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24970. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24971. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24972. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24973. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24974. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24975. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24976. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24977. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24978. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24979. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24980. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24981. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24982. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24983. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24984. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24985. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24986. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24987. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24988. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24989. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24990. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24991. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  24992. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24993. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24994. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24995. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24996. (org-defkey org-mode-map "\C-m" 'org-return)
  24997. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24998. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24999. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  25000. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  25001. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  25002. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  25003. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  25004. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  25005. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  25006. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  25007. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  25008. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  25009. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  25010. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  25011. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  25012. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  25013. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  25014. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  25015. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  25016. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  25017. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  25018. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  25019. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  25020. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  25021. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  25022. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  25023. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  25024. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  25025. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  25026. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  25027. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  25028. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  25029. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  25030. (when (featurep 'xemacs)
  25031. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  25032. (defsubst org-table-p () (org-at-table-p))
  25033. (defun org-self-insert-command (N)
  25034. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  25035. If the cursor is in a table looking at whitespace, the whitespace is
  25036. overwritten, and the table is not marked as requiring realignment."
  25037. (interactive "p")
  25038. (if (and (org-table-p)
  25039. (progn
  25040. ;; check if we blank the field, and if that triggers align
  25041. (and org-table-auto-blank-field
  25042. (member last-command
  25043. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  25044. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  25045. ;; got extra space, this field does not determine column width
  25046. (let (org-table-may-need-update) (org-table-blank-field))
  25047. ;; no extra space, this field may determine column width
  25048. (org-table-blank-field)))
  25049. t)
  25050. (eq N 1)
  25051. (looking-at "[^|\n]* |"))
  25052. (let (org-table-may-need-update)
  25053. (goto-char (1- (match-end 0)))
  25054. (delete-backward-char 1)
  25055. (goto-char (match-beginning 0))
  25056. (self-insert-command N))
  25057. (setq org-table-may-need-update t)
  25058. (self-insert-command N)
  25059. (org-fix-tags-on-the-fly)))
  25060. (defun org-fix-tags-on-the-fly ()
  25061. (when (and (equal (char-after (point-at-bol)) ?*)
  25062. (org-on-heading-p))
  25063. (org-align-tags-here org-tags-column)))
  25064. (defun org-delete-backward-char (N)
  25065. "Like `delete-backward-char', insert whitespace at field end in tables.
  25066. When deleting backwards, in tables this function will insert whitespace in
  25067. front of the next \"|\" separator, to keep the table aligned. The table will
  25068. still be marked for re-alignment if the field did fill the entire column,
  25069. because, in this case the deletion might narrow the column."
  25070. (interactive "p")
  25071. (if (and (org-table-p)
  25072. (eq N 1)
  25073. (string-match "|" (buffer-substring (point-at-bol) (point)))
  25074. (looking-at ".*?|"))
  25075. (let ((pos (point))
  25076. (noalign (looking-at "[^|\n\r]* |"))
  25077. (c org-table-may-need-update))
  25078. (backward-delete-char N)
  25079. (skip-chars-forward "^|")
  25080. (insert " ")
  25081. (goto-char (1- pos))
  25082. ;; noalign: if there were two spaces at the end, this field
  25083. ;; does not determine the width of the column.
  25084. (if noalign (setq org-table-may-need-update c)))
  25085. (backward-delete-char N)
  25086. (org-fix-tags-on-the-fly)))
  25087. (defun org-delete-char (N)
  25088. "Like `delete-char', but insert whitespace at field end in tables.
  25089. When deleting characters, in tables this function will insert whitespace in
  25090. front of the next \"|\" separator, to keep the table aligned. The table will
  25091. still be marked for re-alignment if the field did fill the entire column,
  25092. because, in this case the deletion might narrow the column."
  25093. (interactive "p")
  25094. (if (and (org-table-p)
  25095. (not (bolp))
  25096. (not (= (char-after) ?|))
  25097. (eq N 1))
  25098. (if (looking-at ".*?|")
  25099. (let ((pos (point))
  25100. (noalign (looking-at "[^|\n\r]* |"))
  25101. (c org-table-may-need-update))
  25102. (replace-match (concat
  25103. (substring (match-string 0) 1 -1)
  25104. " |"))
  25105. (goto-char pos)
  25106. ;; noalign: if there were two spaces at the end, this field
  25107. ;; does not determine the width of the column.
  25108. (if noalign (setq org-table-may-need-update c)))
  25109. (delete-char N))
  25110. (delete-char N)
  25111. (org-fix-tags-on-the-fly)))
  25112. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  25113. (put 'org-self-insert-command 'delete-selection t)
  25114. (put 'orgtbl-self-insert-command 'delete-selection t)
  25115. (put 'org-delete-char 'delete-selection 'supersede)
  25116. (put 'org-delete-backward-char 'delete-selection 'supersede)
  25117. ;; Make `flyspell-mode' delay after some commands
  25118. (put 'org-self-insert-command 'flyspell-delayed t)
  25119. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25120. (put 'org-delete-char 'flyspell-delayed t)
  25121. (put 'org-delete-backward-char 'flyspell-delayed t)
  25122. ;; Make pabbrev-mode expand after org-mode commands
  25123. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25124. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25125. ;; How to do this: Measure non-white length of current string
  25126. ;; If equal to column width, we should realign.
  25127. (defun org-remap (map &rest commands)
  25128. "In MAP, remap the functions given in COMMANDS.
  25129. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25130. (let (new old)
  25131. (while commands
  25132. (setq old (pop commands) new (pop commands))
  25133. (if (fboundp 'command-remapping)
  25134. (org-defkey map (vector 'remap old) new)
  25135. (substitute-key-definition old new map global-map)))))
  25136. (when (eq org-enable-table-editor 'optimized)
  25137. ;; If the user wants maximum table support, we need to hijack
  25138. ;; some standard editing functions
  25139. (org-remap org-mode-map
  25140. 'self-insert-command 'org-self-insert-command
  25141. 'delete-char 'org-delete-char
  25142. 'delete-backward-char 'org-delete-backward-char)
  25143. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25144. (defun org-shiftcursor-error ()
  25145. "Throw an error because Shift-Cursor command was applied in wrong context."
  25146. (error "This command is active in special context like tables, headlines or timestamps"))
  25147. (defun org-shifttab (&optional arg)
  25148. "Global visibility cycling or move to previous table field.
  25149. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25150. on context.
  25151. See the individual commands for more information."
  25152. (interactive "P")
  25153. (cond
  25154. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25155. (arg (message "Content view to level: ")
  25156. (org-content (prefix-numeric-value arg))
  25157. (setq org-cycle-global-status 'overview))
  25158. (t (call-interactively 'org-global-cycle))))
  25159. (defun org-shiftmetaleft ()
  25160. "Promote subtree or delete table column.
  25161. Calls `org-promote-subtree', `org-outdent-item',
  25162. or `org-table-delete-column', depending on context.
  25163. See the individual commands for more information."
  25164. (interactive)
  25165. (cond
  25166. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25167. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25168. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25169. (t (org-shiftcursor-error))))
  25170. (defun org-shiftmetaright ()
  25171. "Demote subtree or insert table column.
  25172. Calls `org-demote-subtree', `org-indent-item',
  25173. or `org-table-insert-column', depending on context.
  25174. See the individual commands for more information."
  25175. (interactive)
  25176. (cond
  25177. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25178. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25179. ((org-at-item-p) (call-interactively 'org-indent-item))
  25180. (t (org-shiftcursor-error))))
  25181. (defun org-shiftmetaup (&optional arg)
  25182. "Move subtree up or kill table row.
  25183. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25184. `org-move-item-up' depending on context. See the individual commands
  25185. for more information."
  25186. (interactive "P")
  25187. (cond
  25188. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25189. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25190. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25191. (t (org-shiftcursor-error))))
  25192. (defun org-shiftmetadown (&optional arg)
  25193. "Move subtree down or insert table row.
  25194. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25195. `org-move-item-down', depending on context. See the individual
  25196. commands for more information."
  25197. (interactive "P")
  25198. (cond
  25199. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25200. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25201. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25202. (t (org-shiftcursor-error))))
  25203. (defun org-metaleft (&optional arg)
  25204. "Promote heading or move table column to left.
  25205. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25206. With no specific context, calls the Emacs default `backward-word'.
  25207. See the individual commands for more information."
  25208. (interactive "P")
  25209. (cond
  25210. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25211. ((or (org-on-heading-p) (org-region-active-p))
  25212. (call-interactively 'org-do-promote))
  25213. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25214. (t (call-interactively 'backward-word))))
  25215. (defun org-metaright (&optional arg)
  25216. "Demote subtree or move table column to right.
  25217. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25218. With no specific context, calls the Emacs default `forward-word'.
  25219. See the individual commands for more information."
  25220. (interactive "P")
  25221. (cond
  25222. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25223. ((or (org-on-heading-p) (org-region-active-p))
  25224. (call-interactively 'org-do-demote))
  25225. ((org-at-item-p) (call-interactively 'org-indent-item))
  25226. (t (call-interactively 'forward-word))))
  25227. (defun org-metaup (&optional arg)
  25228. "Move subtree up or move table row up.
  25229. Calls `org-move-subtree-up' or `org-table-move-row' or
  25230. `org-move-item-up', depending on context. See the individual commands
  25231. for more information."
  25232. (interactive "P")
  25233. (cond
  25234. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25235. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25236. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25237. (t (transpose-lines 1) (beginning-of-line -1))))
  25238. (defun org-metadown (&optional arg)
  25239. "Move subtree down or move table row down.
  25240. Calls `org-move-subtree-down' or `org-table-move-row' or
  25241. `org-move-item-down', depending on context. See the individual
  25242. commands for more information."
  25243. (interactive "P")
  25244. (cond
  25245. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25246. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25247. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25248. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25249. (defun org-shiftup (&optional arg)
  25250. "Increase item in timestamp or increase priority of current headline.
  25251. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25252. depending on context. See the individual commands for more information."
  25253. (interactive "P")
  25254. (cond
  25255. ((org-at-timestamp-p t)
  25256. (call-interactively (if org-edit-timestamp-down-means-later
  25257. 'org-timestamp-down 'org-timestamp-up)))
  25258. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25259. ((org-at-item-p) (call-interactively 'org-previous-item))
  25260. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25261. (defun org-shiftdown (&optional arg)
  25262. "Decrease item in timestamp or decrease priority of current headline.
  25263. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25264. depending on context. See the individual commands for more information."
  25265. (interactive "P")
  25266. (cond
  25267. ((org-at-timestamp-p t)
  25268. (call-interactively (if org-edit-timestamp-down-means-later
  25269. 'org-timestamp-up 'org-timestamp-down)))
  25270. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25271. (t (call-interactively 'org-next-item))))
  25272. (defun org-shiftright ()
  25273. "Next TODO keyword or timestamp one day later, depending on context."
  25274. (interactive)
  25275. (cond
  25276. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25277. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25278. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25279. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25280. (t (org-shiftcursor-error))))
  25281. (defun org-shiftleft ()
  25282. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25283. (interactive)
  25284. (cond
  25285. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25286. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25287. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25288. ((org-at-property-p)
  25289. (call-interactively 'org-property-previous-allowed-value))
  25290. (t (org-shiftcursor-error))))
  25291. (defun org-shiftcontrolright ()
  25292. "Switch to next TODO set."
  25293. (interactive)
  25294. (cond
  25295. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25296. (t (org-shiftcursor-error))))
  25297. (defun org-shiftcontrolleft ()
  25298. "Switch to previous TODO set."
  25299. (interactive)
  25300. (cond
  25301. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25302. (t (org-shiftcursor-error))))
  25303. (defun org-ctrl-c-ret ()
  25304. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25305. (interactive)
  25306. (cond
  25307. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25308. (t (call-interactively 'org-insert-heading))))
  25309. (defun org-copy-special ()
  25310. "Copy region in table or copy current subtree.
  25311. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25312. See the individual commands for more information."
  25313. (interactive)
  25314. (call-interactively
  25315. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25316. (defun org-cut-special ()
  25317. "Cut region in table or cut current subtree.
  25318. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25319. See the individual commands for more information."
  25320. (interactive)
  25321. (call-interactively
  25322. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25323. (defun org-paste-special (arg)
  25324. "Paste rectangular region into table, or past subtree relative to level.
  25325. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25326. See the individual commands for more information."
  25327. (interactive "P")
  25328. (if (org-at-table-p)
  25329. (org-table-paste-rectangle)
  25330. (org-paste-subtree arg)))
  25331. (defun org-ctrl-c-ctrl-c (&optional arg)
  25332. "Set tags in headline, or update according to changed information at point.
  25333. This command does many different things, depending on context:
  25334. - If the cursor is in a headline, prompt for tags and insert them
  25335. into the current line, aligned to `org-tags-column'. When called
  25336. with prefix arg, realign all tags in the current buffer.
  25337. - If the cursor is in one of the special #+KEYWORD lines, this
  25338. triggers scanning the buffer for these lines and updating the
  25339. information.
  25340. - If the cursor is inside a table, realign the table. This command
  25341. works even if the automatic table editor has been turned off.
  25342. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25343. the entire table.
  25344. - If the cursor is a the beginning of a dynamic block, update it.
  25345. - If the cursor is inside a table created by the table.el package,
  25346. activate that table.
  25347. - If the current buffer is a remember buffer, close note and file it.
  25348. with a prefix argument, file it without further interaction to the default
  25349. location.
  25350. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25351. links in this buffer.
  25352. - If the cursor is on a numbered item in a plain list, renumber the
  25353. ordered list.
  25354. - If the cursor is on a checkbox, toggle it."
  25355. (interactive "P")
  25356. (let ((org-enable-table-editor t))
  25357. (cond
  25358. ((or org-clock-overlays
  25359. org-occur-highlights
  25360. org-latex-fragment-image-overlays)
  25361. (org-remove-clock-overlays)
  25362. (org-remove-occur-highlights)
  25363. (org-remove-latex-fragment-image-overlays)
  25364. (message "Temporary highlights/overlays removed from current buffer"))
  25365. ((and (local-variable-p 'org-finish-function (current-buffer))
  25366. (fboundp org-finish-function))
  25367. (funcall org-finish-function))
  25368. ((org-at-property-p)
  25369. (call-interactively 'org-property-action))
  25370. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25371. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25372. ((org-at-table.el-p)
  25373. (require 'table)
  25374. (beginning-of-line 1)
  25375. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25376. (call-interactively 'table-recognize-table))
  25377. ((org-at-table-p)
  25378. (org-table-maybe-eval-formula)
  25379. (if arg
  25380. (call-interactively 'org-table-recalculate)
  25381. (org-table-maybe-recalculate-line))
  25382. (call-interactively 'org-table-align))
  25383. ((org-at-item-checkbox-p)
  25384. (call-interactively 'org-toggle-checkbox))
  25385. ((org-at-item-p)
  25386. (call-interactively 'org-maybe-renumber-ordered-list))
  25387. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25388. ;; Dynamic block
  25389. (beginning-of-line 1)
  25390. (org-update-dblock))
  25391. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25392. (cond
  25393. ((equal (match-string 1) "TBLFM")
  25394. ;; Recalculate the table before this line
  25395. (save-excursion
  25396. (beginning-of-line 1)
  25397. (skip-chars-backward " \r\n\t")
  25398. (if (org-at-table-p)
  25399. (org-call-with-arg 'org-table-recalculate t))))
  25400. (t
  25401. (call-interactively 'org-mode-restart))))
  25402. (t (error "C-c C-c can do nothing useful at this location.")))))
  25403. (defun org-mode-restart ()
  25404. "Restart Org-mode, to scan again for special lines.
  25405. Also updates the keyword regular expressions."
  25406. (interactive)
  25407. (let ((org-inhibit-startup t)) (org-mode))
  25408. (message "Org-mode restarted to refresh keyword and special line setup"))
  25409. (defun org-kill-note-or-show-branches ()
  25410. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25411. (interactive)
  25412. (if (not org-finish-function)
  25413. (call-interactively 'show-branches)
  25414. (let ((org-note-abort t))
  25415. (funcall org-finish-function))))
  25416. (defun org-return (&optional indent)
  25417. "Goto next table row or insert a newline.
  25418. Calls `org-table-next-row' or `newline', depending on context.
  25419. See the individual commands for more information."
  25420. (interactive)
  25421. (cond
  25422. ((bobp) (if indent (newline-and-indent) (newline)))
  25423. ((and (org-at-heading-p)
  25424. (looking-at
  25425. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25426. (org-show-entry)
  25427. (end-of-line 1)
  25428. (newline))
  25429. ((org-at-table-p)
  25430. (org-table-justify-field-maybe)
  25431. (call-interactively 'org-table-next-row))
  25432. (t (if indent (newline-and-indent) (newline)))))
  25433. (defun org-return-indent ()
  25434. "Goto next table row or insert a newline and indent.
  25435. Calls `org-table-next-row' or `newline-and-indent', depending on
  25436. context. See the individual commands for more information."
  25437. (interactive)
  25438. (org-return t))
  25439. (defun org-ctrl-c-star ()
  25440. "Compute table, or change heading status of lines.
  25441. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  25442. depending on context. This will also turn a plain list item or a normal
  25443. line into a subheading."
  25444. (interactive)
  25445. (cond
  25446. ((org-at-table-p)
  25447. (call-interactively 'org-table-recalculate))
  25448. ((org-region-active-p)
  25449. ;; Convert all lines in region to list items
  25450. (call-interactively 'org-toggle-region-headings))
  25451. ((org-on-heading-p)
  25452. (org-toggle-region-headings (point-at-bol)
  25453. (min (1+ (point-at-eol)) (point-max))))
  25454. ((org-at-item-p)
  25455. ;; Convert to heading
  25456. (let ((level (save-match-data
  25457. (save-excursion
  25458. (condition-case nil
  25459. (progn
  25460. (org-back-to-heading t)
  25461. (funcall outline-level))
  25462. (error 0))))))
  25463. (replace-match
  25464. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  25465. (t (org-toggle-region-headings (point-at-bol)
  25466. (min (1+ (point-at-eol)) (point-max))))))
  25467. (defun org-ctrl-c-minus ()
  25468. "Insert separator line in table or modify bullet status of line.
  25469. Also turns a plain line or a region of lines into list items.
  25470. Calls `org-table-insert-hline', `org-toggle-region-items', or
  25471. `org-cycle-list-bullet', depending on context."
  25472. (interactive)
  25473. (cond
  25474. ((org-at-table-p)
  25475. (call-interactively 'org-table-insert-hline))
  25476. ((org-on-heading-p)
  25477. ;; Convert to item
  25478. (save-excursion
  25479. (beginning-of-line 1)
  25480. (if (looking-at "\\*+ ")
  25481. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  25482. ((org-region-active-p)
  25483. ;; Convert all lines in region to list items
  25484. (call-interactively 'org-toggle-region-items))
  25485. ((org-in-item-p)
  25486. (call-interactively 'org-cycle-list-bullet))
  25487. (t (org-toggle-region-items (point-at-bol)
  25488. (min (1+ (point-at-eol)) (point-max))))))
  25489. (defun org-toggle-region-items (beg end)
  25490. "Convert all lines in region to list items.
  25491. If the first line is already an item, convert all list items in the region
  25492. to normal lines."
  25493. (interactive "r")
  25494. (let (l2 l)
  25495. (save-excursion
  25496. (goto-char end)
  25497. (setq l2 (org-current-line))
  25498. (goto-char beg)
  25499. (beginning-of-line 1)
  25500. (setq l (1- (org-current-line)))
  25501. (if (org-at-item-p)
  25502. ;; We already have items, de-itemize
  25503. (while (< (setq l (1+ l)) l2)
  25504. (when (org-at-item-p)
  25505. (goto-char (match-beginning 2))
  25506. (delete-region (match-beginning 2) (match-end 2))
  25507. (and (looking-at "[ \t]+") (replace-match "")))
  25508. (beginning-of-line 2))
  25509. (while (< (setq l (1+ l)) l2)
  25510. (unless (org-at-item-p)
  25511. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25512. (replace-match "\\1- \\2")))
  25513. (beginning-of-line 2))))))
  25514. (defun org-toggle-region-headings (beg end)
  25515. "Convert all lines in region to list items.
  25516. If the first line is already an item, convert all list items in the region
  25517. to normal lines."
  25518. (interactive "r")
  25519. (let (l2 l)
  25520. (save-excursion
  25521. (goto-char end)
  25522. (setq l2 (org-current-line))
  25523. (goto-char beg)
  25524. (beginning-of-line 1)
  25525. (setq l (1- (org-current-line)))
  25526. (if (org-on-heading-p)
  25527. ;; We already have headlines, de-star them
  25528. (while (< (setq l (1+ l)) l2)
  25529. (when (org-on-heading-p t)
  25530. (and (looking-at outline-regexp) (replace-match "")))
  25531. (beginning-of-line 2))
  25532. (let* ((stars (save-excursion
  25533. (re-search-backward org-complex-heading-regexp nil t)
  25534. (or (match-string 1) "*")))
  25535. (add-stars (if org-odd-levels-only "**" "*"))
  25536. (rpl (concat stars add-stars " \\2")))
  25537. (while (< (setq l (1+ l)) l2)
  25538. (unless (org-on-heading-p)
  25539. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  25540. (replace-match rpl)))
  25541. (beginning-of-line 2)))))))
  25542. (defun org-meta-return (&optional arg)
  25543. "Insert a new heading or wrap a region in a table.
  25544. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25545. See the individual commands for more information."
  25546. (interactive "P")
  25547. (cond
  25548. ((org-at-table-p)
  25549. (call-interactively 'org-table-wrap-region))
  25550. (t (call-interactively 'org-insert-heading))))
  25551. ;;; Menu entries
  25552. ;; Define the Org-mode menus
  25553. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25554. '("Tbl"
  25555. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25556. ["Next Field" org-cycle (org-at-table-p)]
  25557. ["Previous Field" org-shifttab (org-at-table-p)]
  25558. ["Next Row" org-return (org-at-table-p)]
  25559. "--"
  25560. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25561. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25562. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25563. "--"
  25564. ("Column"
  25565. ["Move Column Left" org-metaleft (org-at-table-p)]
  25566. ["Move Column Right" org-metaright (org-at-table-p)]
  25567. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25568. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25569. ("Row"
  25570. ["Move Row Up" org-metaup (org-at-table-p)]
  25571. ["Move Row Down" org-metadown (org-at-table-p)]
  25572. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25573. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25574. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25575. "--"
  25576. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25577. ("Rectangle"
  25578. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25579. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25580. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25581. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25582. "--"
  25583. ("Calculate"
  25584. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25585. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25586. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25587. "--"
  25588. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25589. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25590. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25591. "--"
  25592. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25593. "--"
  25594. ["Sum Column/Rectangle" org-table-sum
  25595. (or (org-at-table-p) (org-region-active-p))]
  25596. ["Which Column?" org-table-current-column (org-at-table-p)])
  25597. ["Debug Formulas"
  25598. org-table-toggle-formula-debugger
  25599. :style toggle :selected org-table-formula-debug]
  25600. ["Show Col/Row Numbers"
  25601. org-table-toggle-coordinate-overlays
  25602. :style toggle :selected org-table-overlay-coordinates]
  25603. "--"
  25604. ["Create" org-table-create (and (not (org-at-table-p))
  25605. org-enable-table-editor)]
  25606. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25607. ["Import from File" org-table-import (not (org-at-table-p))]
  25608. ["Export to File" org-table-export (org-at-table-p)]
  25609. "--"
  25610. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25611. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25612. '("Org"
  25613. ("Show/Hide"
  25614. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25615. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25616. ["Sparse Tree" org-occur t]
  25617. ["Reveal Context" org-reveal t]
  25618. ["Show All" show-all t]
  25619. "--"
  25620. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25621. "--"
  25622. ["New Heading" org-insert-heading t]
  25623. ("Navigate Headings"
  25624. ["Up" outline-up-heading t]
  25625. ["Next" outline-next-visible-heading t]
  25626. ["Previous" outline-previous-visible-heading t]
  25627. ["Next Same Level" outline-forward-same-level t]
  25628. ["Previous Same Level" outline-backward-same-level t]
  25629. "--"
  25630. ["Jump" org-goto t])
  25631. ("Edit Structure"
  25632. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25633. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25634. "--"
  25635. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25636. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25637. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25638. "--"
  25639. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25640. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25641. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25642. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25643. "--"
  25644. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25645. "--"
  25646. ["Convert to odd levels" org-convert-to-odd-levels t]
  25647. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25648. ("Editing"
  25649. ["Emphasis..." org-emphasize t])
  25650. ("Archive"
  25651. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25652. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25653. ; :active t :keys "C-u C-c C-x C-a"]
  25654. ["Sparse trees open ARCHIVE trees"
  25655. (setq org-sparse-tree-open-archived-trees
  25656. (not org-sparse-tree-open-archived-trees))
  25657. :style toggle :selected org-sparse-tree-open-archived-trees]
  25658. ["Cycling opens ARCHIVE trees"
  25659. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25660. :style toggle :selected org-cycle-open-archived-trees]
  25661. ["Agenda includes ARCHIVE trees"
  25662. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25663. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25664. "--"
  25665. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25666. ; ["Check and Move Children" (org-archive-subtree '(4))
  25667. ; :active t :keys "C-u C-c C-x C-s"]
  25668. )
  25669. "--"
  25670. ("TODO Lists"
  25671. ["TODO/DONE/-" org-todo t]
  25672. ("Select keyword"
  25673. ["Next keyword" org-shiftright (org-on-heading-p)]
  25674. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25675. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25676. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25677. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25678. ["Show TODO Tree" org-show-todo-tree t]
  25679. ["Global TODO list" org-todo-list t]
  25680. "--"
  25681. ["Set Priority" org-priority t]
  25682. ["Priority Up" org-shiftup t]
  25683. ["Priority Down" org-shiftdown t])
  25684. ("TAGS and Properties"
  25685. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25686. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25687. "--"
  25688. ["Set property" 'org-set-property t]
  25689. ["Column view of properties" org-columns t]
  25690. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25691. ("Dates and Scheduling"
  25692. ["Timestamp" org-time-stamp t]
  25693. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25694. ("Change Date"
  25695. ["1 Day Later" org-shiftright t]
  25696. ["1 Day Earlier" org-shiftleft t]
  25697. ["1 ... Later" org-shiftup t]
  25698. ["1 ... Earlier" org-shiftdown t])
  25699. ["Compute Time Range" org-evaluate-time-range t]
  25700. ["Schedule Item" org-schedule t]
  25701. ["Deadline" org-deadline t]
  25702. "--"
  25703. ["Custom time format" org-toggle-time-stamp-overlays
  25704. :style radio :selected org-display-custom-times]
  25705. "--"
  25706. ["Goto Calendar" org-goto-calendar t]
  25707. ["Date from Calendar" org-date-from-calendar t])
  25708. ("Logging work"
  25709. ["Clock in" org-clock-in t]
  25710. ["Clock out" org-clock-out t]
  25711. ["Clock cancel" org-clock-cancel t]
  25712. ["Goto running clock" org-clock-goto t]
  25713. ["Display times" org-clock-display t]
  25714. ["Create clock table" org-clock-report t]
  25715. "--"
  25716. ["Record DONE time"
  25717. (progn (setq org-log-done (not org-log-done))
  25718. (message "Switching to %s will %s record a timestamp"
  25719. (car org-done-keywords)
  25720. (if org-log-done "automatically" "not")))
  25721. :style toggle :selected org-log-done])
  25722. "--"
  25723. ["Agenda Command..." org-agenda t]
  25724. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25725. ("File List for Agenda")
  25726. ("Special views current file"
  25727. ["TODO Tree" org-show-todo-tree t]
  25728. ["Check Deadlines" org-check-deadlines t]
  25729. ["Timeline" org-timeline t]
  25730. ["Tags Tree" org-tags-sparse-tree t])
  25731. "--"
  25732. ("Hyperlinks"
  25733. ["Store Link (Global)" org-store-link t]
  25734. ["Insert Link" org-insert-link t]
  25735. ["Follow Link" org-open-at-point t]
  25736. "--"
  25737. ["Next link" org-next-link t]
  25738. ["Previous link" org-previous-link t]
  25739. "--"
  25740. ["Descriptive Links"
  25741. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25742. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25743. ["Literal Links"
  25744. (progn
  25745. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25746. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25747. "--"
  25748. ["Export/Publish..." org-export t]
  25749. ("LaTeX"
  25750. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25751. :selected org-cdlatex-mode]
  25752. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25753. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25754. ["Modify math symbol" org-cdlatex-math-modify
  25755. (org-inside-LaTeX-fragment-p)]
  25756. ["Export LaTeX fragments as images"
  25757. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25758. :style toggle :selected org-export-with-LaTeX-fragments])
  25759. "--"
  25760. ("Documentation"
  25761. ["Show Version" org-version t]
  25762. ["Info Documentation" org-info t])
  25763. ("Customize"
  25764. ["Browse Org Group" org-customize t]
  25765. "--"
  25766. ["Expand This Menu" org-create-customize-menu
  25767. (fboundp 'customize-menu-create)])
  25768. "--"
  25769. ["Refresh setup" org-mode-restart t]
  25770. ))
  25771. (defun org-info (&optional node)
  25772. "Read documentation for Org-mode in the info system.
  25773. With optional NODE, go directly to that node."
  25774. (interactive)
  25775. (info (format "(org)%s" (or node ""))))
  25776. (defun org-install-agenda-files-menu ()
  25777. (let ((bl (buffer-list)))
  25778. (save-excursion
  25779. (while bl
  25780. (set-buffer (pop bl))
  25781. (if (org-mode-p) (setq bl nil)))
  25782. (when (org-mode-p)
  25783. (easy-menu-change
  25784. '("Org") "File List for Agenda"
  25785. (append
  25786. (list
  25787. ["Edit File List" (org-edit-agenda-file-list) t]
  25788. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25789. ["Remove Current File from List" org-remove-file t]
  25790. ["Cycle through agenda files" org-cycle-agenda-files t]
  25791. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25792. "--")
  25793. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25794. ;;;; Documentation
  25795. (defun org-customize ()
  25796. "Call the customize function with org as argument."
  25797. (interactive)
  25798. (customize-browse 'org))
  25799. (defun org-create-customize-menu ()
  25800. "Create a full customization menu for Org-mode, insert it into the menu."
  25801. (interactive)
  25802. (if (fboundp 'customize-menu-create)
  25803. (progn
  25804. (easy-menu-change
  25805. '("Org") "Customize"
  25806. `(["Browse Org group" org-customize t]
  25807. "--"
  25808. ,(customize-menu-create 'org)
  25809. ["Set" Custom-set t]
  25810. ["Save" Custom-save t]
  25811. ["Reset to Current" Custom-reset-current t]
  25812. ["Reset to Saved" Custom-reset-saved t]
  25813. ["Reset to Standard Settings" Custom-reset-standard t]))
  25814. (message "\"Org\"-menu now contains full customization menu"))
  25815. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25816. ;;;; Miscellaneous stuff
  25817. ;;; Generally useful functions
  25818. (defun org-context ()
  25819. "Return a list of contexts of the current cursor position.
  25820. If several contexts apply, all are returned.
  25821. Each context entry is a list with a symbol naming the context, and
  25822. two positions indicating start and end of the context. Possible
  25823. contexts are:
  25824. :headline anywhere in a headline
  25825. :headline-stars on the leading stars in a headline
  25826. :todo-keyword on a TODO keyword (including DONE) in a headline
  25827. :tags on the TAGS in a headline
  25828. :priority on the priority cookie in a headline
  25829. :item on the first line of a plain list item
  25830. :item-bullet on the bullet/number of a plain list item
  25831. :checkbox on the checkbox in a plain list item
  25832. :table in an org-mode table
  25833. :table-special on a special filed in a table
  25834. :table-table in a table.el table
  25835. :link on a hyperlink
  25836. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25837. :target on a <<target>>
  25838. :radio-target on a <<<radio-target>>>
  25839. :latex-fragment on a LaTeX fragment
  25840. :latex-preview on a LaTeX fragment with overlayed preview image
  25841. This function expects the position to be visible because it uses font-lock
  25842. faces as a help to recognize the following contexts: :table-special, :link,
  25843. and :keyword."
  25844. (let* ((f (get-text-property (point) 'face))
  25845. (faces (if (listp f) f (list f)))
  25846. (p (point)) clist o)
  25847. ;; First the large context
  25848. (cond
  25849. ((org-on-heading-p t)
  25850. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25851. (when (progn
  25852. (beginning-of-line 1)
  25853. (looking-at org-todo-line-tags-regexp))
  25854. (push (org-point-in-group p 1 :headline-stars) clist)
  25855. (push (org-point-in-group p 2 :todo-keyword) clist)
  25856. (push (org-point-in-group p 4 :tags) clist))
  25857. (goto-char p)
  25858. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25859. (if (looking-at "\\[#[A-Z0-9]\\]")
  25860. (push (org-point-in-group p 0 :priority) clist)))
  25861. ((org-at-item-p)
  25862. (push (org-point-in-group p 2 :item-bullet) clist)
  25863. (push (list :item (point-at-bol)
  25864. (save-excursion (org-end-of-item) (point)))
  25865. clist)
  25866. (and (org-at-item-checkbox-p)
  25867. (push (org-point-in-group p 0 :checkbox) clist)))
  25868. ((org-at-table-p)
  25869. (push (list :table (org-table-begin) (org-table-end)) clist)
  25870. (if (memq 'org-formula faces)
  25871. (push (list :table-special
  25872. (previous-single-property-change p 'face)
  25873. (next-single-property-change p 'face)) clist)))
  25874. ((org-at-table-p 'any)
  25875. (push (list :table-table) clist)))
  25876. (goto-char p)
  25877. ;; Now the small context
  25878. (cond
  25879. ((org-at-timestamp-p)
  25880. (push (org-point-in-group p 0 :timestamp) clist))
  25881. ((memq 'org-link faces)
  25882. (push (list :link
  25883. (previous-single-property-change p 'face)
  25884. (next-single-property-change p 'face)) clist))
  25885. ((memq 'org-special-keyword faces)
  25886. (push (list :keyword
  25887. (previous-single-property-change p 'face)
  25888. (next-single-property-change p 'face)) clist))
  25889. ((org-on-target-p)
  25890. (push (org-point-in-group p 0 :target) clist)
  25891. (goto-char (1- (match-beginning 0)))
  25892. (if (looking-at org-radio-target-regexp)
  25893. (push (org-point-in-group p 0 :radio-target) clist))
  25894. (goto-char p))
  25895. ((setq o (car (delq nil
  25896. (mapcar
  25897. (lambda (x)
  25898. (if (memq x org-latex-fragment-image-overlays) x))
  25899. (org-overlays-at (point))))))
  25900. (push (list :latex-fragment
  25901. (org-overlay-start o) (org-overlay-end o)) clist)
  25902. (push (list :latex-preview
  25903. (org-overlay-start o) (org-overlay-end o)) clist))
  25904. ((org-inside-LaTeX-fragment-p)
  25905. ;; FIXME: positions wrong.
  25906. (push (list :latex-fragment (point) (point)) clist)))
  25907. (setq clist (nreverse (delq nil clist)))
  25908. clist))
  25909. ;; FIXME: Compare with at-regexp-p Do we need both?
  25910. (defun org-in-regexp (re &optional nlines visually)
  25911. "Check if point is inside a match of regexp.
  25912. Normally only the current line is checked, but you can include NLINES extra
  25913. lines both before and after point into the search.
  25914. If VISUALLY is set, require that the cursor is not after the match but
  25915. really on, so that the block visually is on the match."
  25916. (catch 'exit
  25917. (let ((pos (point))
  25918. (eol (point-at-eol (+ 1 (or nlines 0))))
  25919. (inc (if visually 1 0)))
  25920. (save-excursion
  25921. (beginning-of-line (- 1 (or nlines 0)))
  25922. (while (re-search-forward re eol t)
  25923. (if (and (<= (match-beginning 0) pos)
  25924. (>= (+ inc (match-end 0)) pos))
  25925. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25926. (defun org-at-regexp-p (regexp)
  25927. "Is point inside a match of REGEXP in the current line?"
  25928. (catch 'exit
  25929. (save-excursion
  25930. (let ((pos (point)) (end (point-at-eol)))
  25931. (beginning-of-line 1)
  25932. (while (re-search-forward regexp end t)
  25933. (if (and (<= (match-beginning 0) pos)
  25934. (>= (match-end 0) pos))
  25935. (throw 'exit t)))
  25936. nil))))
  25937. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25938. "Call `multi-occur' with buffers for all agenda files."
  25939. (interactive "sOrg-files matching: \np")
  25940. (let* ((files (org-agenda-files))
  25941. (tnames (mapcar 'file-truename files))
  25942. (extra org-agenda-text-search-extra-files)
  25943. f)
  25944. (while (setq f (pop extra))
  25945. (unless (member (file-truename f) tnames)
  25946. (add-to-list 'files f 'append)
  25947. (add-to-list 'tnames (file-truename f) 'append)))
  25948. (multi-occur
  25949. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25950. regexp)))
  25951. (if (boundp 'occur-mode-find-occurrence-hook)
  25952. ;; Emacs 23
  25953. (add-hook 'occur-mode-find-occurrence-hook
  25954. (lambda ()
  25955. (when (org-mode-p)
  25956. (org-reveal))))
  25957. ;; Emacs 22
  25958. (defadvice occur-mode-goto-occurrence
  25959. (after org-occur-reveal activate)
  25960. (and (org-mode-p) (org-reveal)))
  25961. (defadvice occur-mode-goto-occurrence-other-window
  25962. (after org-occur-reveal activate)
  25963. (and (org-mode-p) (org-reveal)))
  25964. (defadvice occur-mode-display-occurrence
  25965. (after org-occur-reveal activate)
  25966. (when (org-mode-p)
  25967. (let ((pos (occur-mode-find-occurrence)))
  25968. (with-current-buffer (marker-buffer pos)
  25969. (save-excursion
  25970. (goto-char pos)
  25971. (org-reveal)))))))
  25972. (defun org-uniquify (list)
  25973. "Remove duplicate elements from LIST."
  25974. (let (res)
  25975. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25976. res))
  25977. (defun org-delete-all (elts list)
  25978. "Remove all elements in ELTS from LIST."
  25979. (while elts
  25980. (setq list (delete (pop elts) list)))
  25981. list)
  25982. (defun org-back-over-empty-lines ()
  25983. "Move backwards over witespace, to the beginning of the first empty line.
  25984. Returns the number of empty lines passed."
  25985. (let ((pos (point)))
  25986. (skip-chars-backward " \t\n\r")
  25987. (beginning-of-line 2)
  25988. (goto-char (min (point) pos))
  25989. (count-lines (point) pos)))
  25990. (defun org-skip-whitespace ()
  25991. (skip-chars-forward " \t\n\r"))
  25992. (defun org-point-in-group (point group &optional context)
  25993. "Check if POINT is in match-group GROUP.
  25994. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25995. match. If the match group does ot exist or point is not inside it,
  25996. return nil."
  25997. (and (match-beginning group)
  25998. (>= point (match-beginning group))
  25999. (<= point (match-end group))
  26000. (if context
  26001. (list context (match-beginning group) (match-end group))
  26002. t)))
  26003. (defun org-switch-to-buffer-other-window (&rest args)
  26004. "Switch to buffer in a second window on the current frame.
  26005. In particular, do not allow pop-up frames."
  26006. (let (pop-up-frames special-display-buffer-names special-display-regexps
  26007. special-display-function)
  26008. (apply 'switch-to-buffer-other-window args)))
  26009. (defun org-combine-plists (&rest plists)
  26010. "Create a single property list from all plists in PLISTS.
  26011. The process starts by copying the first list, and then setting properties
  26012. from the other lists. Settings in the last list are the most significant
  26013. ones and overrule settings in the other lists."
  26014. (let ((rtn (copy-sequence (pop plists)))
  26015. p v ls)
  26016. (while plists
  26017. (setq ls (pop plists))
  26018. (while ls
  26019. (setq p (pop ls) v (pop ls))
  26020. (setq rtn (plist-put rtn p v))))
  26021. rtn))
  26022. (defun org-move-line-down (arg)
  26023. "Move the current line down. With prefix argument, move it past ARG lines."
  26024. (interactive "p")
  26025. (let ((col (current-column))
  26026. beg end pos)
  26027. (beginning-of-line 1) (setq beg (point))
  26028. (beginning-of-line 2) (setq end (point))
  26029. (beginning-of-line (+ 1 arg))
  26030. (setq pos (move-marker (make-marker) (point)))
  26031. (insert (delete-and-extract-region beg end))
  26032. (goto-char pos)
  26033. (move-to-column col)))
  26034. (defun org-move-line-up (arg)
  26035. "Move the current line up. With prefix argument, move it past ARG lines."
  26036. (interactive "p")
  26037. (let ((col (current-column))
  26038. beg end pos)
  26039. (beginning-of-line 1) (setq beg (point))
  26040. (beginning-of-line 2) (setq end (point))
  26041. (beginning-of-line (- arg))
  26042. (setq pos (move-marker (make-marker) (point)))
  26043. (insert (delete-and-extract-region beg end))
  26044. (goto-char pos)
  26045. (move-to-column col)))
  26046. (defun org-replace-escapes (string table)
  26047. "Replace %-escapes in STRING with values in TABLE.
  26048. TABLE is an association list with keys like \"%a\" and string values.
  26049. The sequences in STRING may contain normal field width and padding information,
  26050. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  26051. so values can contain further %-escapes if they are define later in TABLE."
  26052. (let ((case-fold-search nil)
  26053. e re rpl)
  26054. (while (setq e (pop table))
  26055. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  26056. (while (string-match re string)
  26057. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  26058. (cdr e)))
  26059. (setq string (replace-match rpl t t string))))
  26060. string))
  26061. (defun org-sublist (list start end)
  26062. "Return a section of LIST, from START to END.
  26063. Counting starts at 1."
  26064. (let (rtn (c start))
  26065. (setq list (nthcdr (1- start) list))
  26066. (while (and list (<= c end))
  26067. (push (pop list) rtn)
  26068. (setq c (1+ c)))
  26069. (nreverse rtn)))
  26070. (defun org-find-base-buffer-visiting (file)
  26071. "Like `find-buffer-visiting' but alway return the base buffer and
  26072. not an indirect buffer."
  26073. (let ((buf (find-buffer-visiting file)))
  26074. (if buf
  26075. (or (buffer-base-buffer buf) buf)
  26076. nil)))
  26077. (defun org-image-file-name-regexp ()
  26078. "Return regexp matching the file names of images."
  26079. (if (fboundp 'image-file-name-regexp)
  26080. (image-file-name-regexp)
  26081. (let ((image-file-name-extensions
  26082. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  26083. "xbm" "xpm" "pbm" "pgm" "ppm")))
  26084. (concat "\\."
  26085. (regexp-opt (nconc (mapcar 'upcase
  26086. image-file-name-extensions)
  26087. image-file-name-extensions)
  26088. t)
  26089. "\\'"))))
  26090. (defun org-file-image-p (file)
  26091. "Return non-nil if FILE is an image."
  26092. (save-match-data
  26093. (string-match (org-image-file-name-regexp) file)))
  26094. ;;; Paragraph filling stuff.
  26095. ;; We want this to be just right, so use the full arsenal.
  26096. (defun org-indent-line-function ()
  26097. "Indent line like previous, but further if previous was headline or item."
  26098. (interactive)
  26099. (let* ((pos (point))
  26100. (itemp (org-at-item-p))
  26101. column bpos bcol tpos tcol bullet btype bullet-type)
  26102. ;; Find the previous relevant line
  26103. (beginning-of-line 1)
  26104. (cond
  26105. ((looking-at "#") (setq column 0))
  26106. ((looking-at "\\*+ ") (setq column 0))
  26107. (t
  26108. (beginning-of-line 0)
  26109. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  26110. (beginning-of-line 0))
  26111. (cond
  26112. ((looking-at "\\*+[ \t]+")
  26113. (goto-char (match-end 0))
  26114. (setq column (current-column)))
  26115. ((org-in-item-p)
  26116. (org-beginning-of-item)
  26117. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26118. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  26119. (setq bpos (match-beginning 1) tpos (match-end 0)
  26120. bcol (progn (goto-char bpos) (current-column))
  26121. tcol (progn (goto-char tpos) (current-column))
  26122. bullet (match-string 1)
  26123. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  26124. (if (not itemp)
  26125. (setq column tcol)
  26126. (goto-char pos)
  26127. (beginning-of-line 1)
  26128. (if (looking-at "\\S-")
  26129. (progn
  26130. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  26131. (setq bullet (match-string 1)
  26132. btype (if (string-match "[0-9]" bullet) "n" bullet))
  26133. (setq column (if (equal btype bullet-type) bcol tcol)))
  26134. (setq column (org-get-indentation)))))
  26135. (t (setq column (org-get-indentation))))))
  26136. (goto-char pos)
  26137. (if (<= (current-column) (current-indentation))
  26138. (indent-line-to column)
  26139. (save-excursion (indent-line-to column)))
  26140. (setq column (current-column))
  26141. (beginning-of-line 1)
  26142. (if (looking-at
  26143. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  26144. (replace-match (concat "\\1" (format org-property-format
  26145. (match-string 2) (match-string 3)))
  26146. t nil))
  26147. (move-to-column column)))
  26148. (defun org-set-autofill-regexps ()
  26149. (interactive)
  26150. ;; In the paragraph separator we include headlines, because filling
  26151. ;; text in a line directly attached to a headline would otherwise
  26152. ;; fill the headline as well.
  26153. (org-set-local 'comment-start-skip "^#+[ \t]*")
  26154. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  26155. ;; The paragraph starter includes hand-formatted lists.
  26156. (org-set-local 'paragraph-start
  26157. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  26158. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  26159. ;; But only if the user has not turned off tables or fixed-width regions
  26160. (org-set-local
  26161. 'auto-fill-inhibit-regexp
  26162. (concat "\\*+ \\|#\\+"
  26163. "\\|[ \t]*" org-keyword-time-regexp
  26164. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  26165. (concat
  26166. "\\|[ \t]*["
  26167. (if org-enable-table-editor "|" "")
  26168. (if org-enable-fixed-width-editor ":" "")
  26169. "]"))))
  26170. ;; We use our own fill-paragraph function, to make sure that tables
  26171. ;; and fixed-width regions are not wrapped. That function will pass
  26172. ;; through to `fill-paragraph' when appropriate.
  26173. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26174. ; Adaptive filling: To get full control, first make sure that
  26175. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26176. (org-set-local 'adaptive-fill-regexp "\000")
  26177. (org-set-local 'adaptive-fill-function
  26178. 'org-adaptive-fill-function)
  26179. (org-set-local
  26180. 'align-mode-rules-list
  26181. '((org-in-buffer-settings
  26182. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26183. (modes . '(org-mode))))))
  26184. (defun org-fill-paragraph (&optional justify)
  26185. "Re-align a table, pass through to fill-paragraph if no table."
  26186. (let ((table-p (org-at-table-p))
  26187. (table.el-p (org-at-table.el-p)))
  26188. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26189. (save-excursion (goto-char (point-at-bol))
  26190. (looking-at outline-regexp)))
  26191. t) ; skip headlines
  26192. (table.el-p t) ; skip table.el tables
  26193. (table-p (org-table-align) t) ; align org-mode tables
  26194. (t nil)))) ; call paragraph-fill
  26195. ;; For reference, this is the default value of adaptive-fill-regexp
  26196. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26197. (defun org-adaptive-fill-function ()
  26198. "Return a fill prefix for org-mode files.
  26199. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26200. work correctly."
  26201. (cond ((looking-at "#[ \t]+")
  26202. (match-string 0))
  26203. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26204. (save-excursion
  26205. (goto-char (match-end 0))
  26206. (make-string (current-column) ?\ )))
  26207. (t nil)))
  26208. ;;;; Functions extending outline functionality
  26209. (defun org-beginning-of-line (&optional arg)
  26210. "Go to the beginning of the current line. If that is invisible, continue
  26211. to a visible line beginning. This makes the function of C-a more intuitive.
  26212. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26213. first attempt, and only move to after the tags when the cursor is already
  26214. beyond the end of the headline."
  26215. (interactive "P")
  26216. (let ((pos (point)))
  26217. (beginning-of-line 1)
  26218. (if (bobp)
  26219. nil
  26220. (backward-char 1)
  26221. (if (org-invisible-p)
  26222. (while (and (not (bobp)) (org-invisible-p))
  26223. (backward-char 1)
  26224. (beginning-of-line 1))
  26225. (forward-char 1)))
  26226. (when org-special-ctrl-a/e
  26227. (cond
  26228. ((and (looking-at org-todo-line-regexp)
  26229. (= (char-after (match-end 1)) ?\ ))
  26230. (goto-char
  26231. (if (eq org-special-ctrl-a/e t)
  26232. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26233. ((= pos (point)) (match-beginning 3))
  26234. (t (point)))
  26235. (cond ((> pos (point)) (point))
  26236. ((not (eq last-command this-command)) (point))
  26237. (t (match-beginning 3))))))
  26238. ((org-at-item-p)
  26239. (goto-char
  26240. (if (eq org-special-ctrl-a/e t)
  26241. (cond ((> pos (match-end 4)) (match-end 4))
  26242. ((= pos (point)) (match-end 4))
  26243. (t (point)))
  26244. (cond ((> pos (point)) (point))
  26245. ((not (eq last-command this-command)) (point))
  26246. (t (match-end 4))))))))))
  26247. (defun org-end-of-line (&optional arg)
  26248. "Go to the end of the line.
  26249. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26250. first attempt, and only move to after the tags when the cursor is already
  26251. beyond the end of the headline."
  26252. (interactive "P")
  26253. (if (or (not org-special-ctrl-a/e)
  26254. (not (org-on-heading-p)))
  26255. (end-of-line arg)
  26256. (let ((pos (point)))
  26257. (beginning-of-line 1)
  26258. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26259. (if (eq org-special-ctrl-a/e t)
  26260. (if (or (< pos (match-beginning 1))
  26261. (= pos (match-end 0)))
  26262. (goto-char (match-beginning 1))
  26263. (goto-char (match-end 0)))
  26264. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26265. (goto-char (match-end 0))
  26266. (goto-char (match-beginning 1))))
  26267. (end-of-line arg)))))
  26268. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26269. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26270. (defun org-kill-line (&optional arg)
  26271. "Kill line, to tags or end of line."
  26272. (interactive "P")
  26273. (cond
  26274. ((or (not org-special-ctrl-k)
  26275. (bolp)
  26276. (not (org-on-heading-p)))
  26277. (call-interactively 'kill-line))
  26278. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26279. (kill-region (point) (match-beginning 1))
  26280. (org-set-tags nil t))
  26281. (t (kill-region (point) (point-at-eol)))))
  26282. (define-key org-mode-map "\C-k" 'org-kill-line)
  26283. (defun org-invisible-p ()
  26284. "Check if point is at a character currently not visible."
  26285. ;; Early versions of noutline don't have `outline-invisible-p'.
  26286. (if (fboundp 'outline-invisible-p)
  26287. (outline-invisible-p)
  26288. (get-char-property (point) 'invisible)))
  26289. (defun org-invisible-p2 ()
  26290. "Check if point is at a character currently not visible."
  26291. (save-excursion
  26292. (if (and (eolp) (not (bobp))) (backward-char 1))
  26293. ;; Early versions of noutline don't have `outline-invisible-p'.
  26294. (if (fboundp 'outline-invisible-p)
  26295. (outline-invisible-p)
  26296. (get-char-property (point) 'invisible))))
  26297. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26298. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26299. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26300. (defun org-at-heading-or-item-p ()
  26301. (or (org-on-heading-p) (org-at-item-p)))
  26302. (defun org-on-target-p ()
  26303. (or (org-in-regexp org-radio-target-regexp)
  26304. (org-in-regexp org-target-regexp)))
  26305. (defun org-up-heading-all (arg)
  26306. "Move to the heading line of which the present line is a subheading.
  26307. This function considers both visible and invisible heading lines.
  26308. With argument, move up ARG levels."
  26309. (if (fboundp 'outline-up-heading-all)
  26310. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26311. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26312. (defun org-up-heading-safe ()
  26313. "Move to the heading line of which the present line is a subheading.
  26314. This version will not throw an error. It will return the level of the
  26315. headline found, or nil if no higher level is found."
  26316. (let ((pos (point)) start-level level
  26317. (re (concat "^" outline-regexp)))
  26318. (catch 'exit
  26319. (outline-back-to-heading t)
  26320. (setq start-level (funcall outline-level))
  26321. (if (equal start-level 1) (throw 'exit nil))
  26322. (while (re-search-backward re nil t)
  26323. (setq level (funcall outline-level))
  26324. (if (< level start-level) (throw 'exit level)))
  26325. nil)))
  26326. (defun org-first-sibling-p ()
  26327. "Is this heading the first child of its parents?"
  26328. (interactive)
  26329. (let ((re (concat "^" outline-regexp))
  26330. level l)
  26331. (unless (org-at-heading-p t)
  26332. (error "Not at a heading"))
  26333. (setq level (funcall outline-level))
  26334. (save-excursion
  26335. (if (not (re-search-backward re nil t))
  26336. t
  26337. (setq l (funcall outline-level))
  26338. (< l level)))))
  26339. (defun org-goto-sibling (&optional previous)
  26340. "Goto the next sibling, even if it is invisible.
  26341. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26342. when a sibling was found. When none is found, return nil and don't
  26343. move point."
  26344. (let ((fun (if previous 're-search-backward 're-search-forward))
  26345. (pos (point))
  26346. (re (concat "^" outline-regexp))
  26347. level l)
  26348. (when (condition-case nil (org-back-to-heading t) (error nil))
  26349. (setq level (funcall outline-level))
  26350. (catch 'exit
  26351. (or previous (forward-char 1))
  26352. (while (funcall fun re nil t)
  26353. (setq l (funcall outline-level))
  26354. (when (< l level) (goto-char pos) (throw 'exit nil))
  26355. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26356. (goto-char pos)
  26357. nil))))
  26358. (defun org-show-siblings ()
  26359. "Show all siblings of the current headline."
  26360. (save-excursion
  26361. (while (org-goto-sibling) (org-flag-heading nil)))
  26362. (save-excursion
  26363. (while (org-goto-sibling 'previous)
  26364. (org-flag-heading nil))))
  26365. (defun org-show-hidden-entry ()
  26366. "Show an entry where even the heading is hidden."
  26367. (save-excursion
  26368. (org-show-entry)))
  26369. (defun org-flag-heading (flag &optional entry)
  26370. "Flag the current heading. FLAG non-nil means make invisible.
  26371. When ENTRY is non-nil, show the entire entry."
  26372. (save-excursion
  26373. (org-back-to-heading t)
  26374. ;; Check if we should show the entire entry
  26375. (if entry
  26376. (progn
  26377. (org-show-entry)
  26378. (save-excursion
  26379. (and (outline-next-heading)
  26380. (org-flag-heading nil))))
  26381. (outline-flag-region (max (point-min) (1- (point)))
  26382. (save-excursion (outline-end-of-heading) (point))
  26383. flag))))
  26384. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26385. ;; This is an exact copy of the original function, but it uses
  26386. ;; `org-back-to-heading', to make it work also in invisible
  26387. ;; trees. And is uses an invisible-OK argument.
  26388. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26389. (org-back-to-heading invisible-OK)
  26390. (let ((first t)
  26391. (level (funcall outline-level)))
  26392. (while (and (not (eobp))
  26393. (or first (> (funcall outline-level) level)))
  26394. (setq first nil)
  26395. (outline-next-heading))
  26396. (unless to-heading
  26397. (if (memq (preceding-char) '(?\n ?\^M))
  26398. (progn
  26399. ;; Go to end of line before heading
  26400. (forward-char -1)
  26401. (if (memq (preceding-char) '(?\n ?\^M))
  26402. ;; leave blank line before heading
  26403. (forward-char -1))))))
  26404. (point))
  26405. (defun org-show-subtree ()
  26406. "Show everything after this heading at deeper levels."
  26407. (outline-flag-region
  26408. (point)
  26409. (save-excursion
  26410. (outline-end-of-subtree) (outline-next-heading) (point))
  26411. nil))
  26412. (defun org-show-entry ()
  26413. "Show the body directly following this heading.
  26414. Show the heading too, if it is currently invisible."
  26415. (interactive)
  26416. (save-excursion
  26417. (condition-case nil
  26418. (progn
  26419. (org-back-to-heading t)
  26420. (outline-flag-region
  26421. (max (point-min) (1- (point)))
  26422. (save-excursion
  26423. (re-search-forward
  26424. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26425. (or (match-beginning 1) (point-max)))
  26426. nil))
  26427. (error nil))))
  26428. (defun org-make-options-regexp (kwds)
  26429. "Make a regular expression for keyword lines."
  26430. (concat
  26431. "^"
  26432. "#?[ \t]*\\+\\("
  26433. (mapconcat 'regexp-quote kwds "\\|")
  26434. "\\):[ \t]*"
  26435. "\\(.+\\)"))
  26436. ;; Make isearch reveal the necessary context
  26437. (defun org-isearch-end ()
  26438. "Reveal context after isearch exits."
  26439. (when isearch-success ; only if search was successful
  26440. (if (featurep 'xemacs)
  26441. ;; Under XEmacs, the hook is run in the correct place,
  26442. ;; we directly show the context.
  26443. (org-show-context 'isearch)
  26444. ;; In Emacs the hook runs *before* restoring the overlays.
  26445. ;; So we have to use a one-time post-command-hook to do this.
  26446. ;; (Emacs 22 has a special variable, see function `org-mode')
  26447. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26448. isearch-mode-end-hook-quit)
  26449. ;; Only when the isearch was not quitted.
  26450. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26451. 'append 'local)))))
  26452. (defun org-isearch-post-command ()
  26453. "Remove self from hook, and show context."
  26454. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26455. (org-show-context 'isearch))
  26456. ;;;; Integration with and fixes for other packages
  26457. ;;; Imenu support
  26458. (defvar org-imenu-markers nil
  26459. "All markers currently used by Imenu.")
  26460. (make-variable-buffer-local 'org-imenu-markers)
  26461. (defun org-imenu-new-marker (&optional pos)
  26462. "Return a new marker for use by Imenu, and remember the marker."
  26463. (let ((m (make-marker)))
  26464. (move-marker m (or pos (point)))
  26465. (push m org-imenu-markers)
  26466. m))
  26467. (defun org-imenu-get-tree ()
  26468. "Produce the index for Imenu."
  26469. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26470. (setq org-imenu-markers nil)
  26471. (let* ((n org-imenu-depth)
  26472. (re (concat "^" outline-regexp))
  26473. (subs (make-vector (1+ n) nil))
  26474. (last-level 0)
  26475. m tree level head)
  26476. (save-excursion
  26477. (save-restriction
  26478. (widen)
  26479. (goto-char (point-max))
  26480. (while (re-search-backward re nil t)
  26481. (setq level (org-reduced-level (funcall outline-level)))
  26482. (when (<= level n)
  26483. (looking-at org-complex-heading-regexp)
  26484. (setq head (org-match-string-no-properties 4)
  26485. m (org-imenu-new-marker))
  26486. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26487. (if (>= level last-level)
  26488. (push (cons head m) (aref subs level))
  26489. (push (cons head (aref subs (1+ level))) (aref subs level))
  26490. (loop for i from (1+ level) to n do (aset subs i nil)))
  26491. (setq last-level level)))))
  26492. (aref subs 1)))
  26493. (eval-after-load "imenu"
  26494. '(progn
  26495. (add-hook 'imenu-after-jump-hook
  26496. (lambda () (org-show-context 'org-goto)))))
  26497. ;; Speedbar support
  26498. (defun org-speedbar-set-agenda-restriction ()
  26499. "Restrict future agenda commands to the location at point in speedbar.
  26500. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26501. (interactive)
  26502. (let (p m tp np dir txt w)
  26503. (cond
  26504. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26505. 'org-imenu t))
  26506. (setq m (get-text-property p 'org-imenu-marker))
  26507. (save-excursion
  26508. (save-restriction
  26509. (set-buffer (marker-buffer m))
  26510. (goto-char m)
  26511. (org-agenda-set-restriction-lock 'subtree))))
  26512. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26513. 'speedbar-function 'speedbar-find-file))
  26514. (setq tp (previous-single-property-change
  26515. (1+ p) 'speedbar-function)
  26516. np (next-single-property-change
  26517. tp 'speedbar-function)
  26518. dir (speedbar-line-directory)
  26519. txt (buffer-substring-no-properties (or tp (point-min))
  26520. (or np (point-max))))
  26521. (save-excursion
  26522. (save-restriction
  26523. (set-buffer (find-file-noselect
  26524. (let ((default-directory dir))
  26525. (expand-file-name txt))))
  26526. (unless (org-mode-p)
  26527. (error "Cannot restrict to non-Org-mode file"))
  26528. (org-agenda-set-restriction-lock 'file))))
  26529. (t (error "Don't know how to restrict Org-mode's agenda")))
  26530. (org-move-overlay org-speedbar-restriction-lock-overlay
  26531. (point-at-bol) (point-at-eol))
  26532. (setq current-prefix-arg nil)
  26533. (org-agenda-maybe-redo)))
  26534. (eval-after-load "speedbar"
  26535. '(progn
  26536. (speedbar-add-supported-extension ".org")
  26537. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26538. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26539. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26540. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26541. (add-hook 'speedbar-visiting-tag-hook
  26542. (lambda () (org-show-context 'org-goto)))))
  26543. ;;; Fixes and Hacks
  26544. ;; Make flyspell not check words in links, to not mess up our keymap
  26545. (defun org-mode-flyspell-verify ()
  26546. "Don't let flyspell put overlays at active buttons."
  26547. (not (get-text-property (point) 'keymap)))
  26548. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26549. (eval-after-load "bookmark"
  26550. '(if (boundp 'bookmark-after-jump-hook)
  26551. ;; We can use the hook
  26552. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26553. ;; Hook not available, use advice
  26554. (defadvice bookmark-jump (after org-make-visible activate)
  26555. "Make the position visible."
  26556. (org-bookmark-jump-unhide))))
  26557. (defun org-bookmark-jump-unhide ()
  26558. "Unhide the current position, to show the bookmark location."
  26559. (and (org-mode-p)
  26560. (or (org-invisible-p)
  26561. (save-excursion (goto-char (max (point-min) (1- (point))))
  26562. (org-invisible-p)))
  26563. (org-show-context 'bookmark-jump)))
  26564. ;; Make session.el ignore our circular variable
  26565. (eval-after-load "session"
  26566. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26567. ;;;; Experimental code
  26568. (defun org-closed-in-range ()
  26569. "Sparse tree of items closed in a certain time range.
  26570. Still experimental, may disappear in the future."
  26571. (interactive)
  26572. ;; Get the time interval from the user.
  26573. (let* ((time1 (time-to-seconds
  26574. (org-read-date nil 'to-time nil "Starting date: ")))
  26575. (time2 (time-to-seconds
  26576. (org-read-date nil 'to-time nil "End date:")))
  26577. ;; callback function
  26578. (callback (lambda ()
  26579. (let ((time
  26580. (time-to-seconds
  26581. (apply 'encode-time
  26582. (org-parse-time-string
  26583. (match-string 1))))))
  26584. ;; check if time in interval
  26585. (and (>= time time1) (<= time time2))))))
  26586. ;; make tree, check each match with the callback
  26587. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26588. ;;;; Finish up
  26589. (provide 'org)
  26590. (run-hooks 'org-load-hook)
  26591. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26592. ;;; org.el ends here