org.el 1.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313
  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.22a+
  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.22a+"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. ;; FIXME: Needs a separate group...
  138. (defcustom org-completion-fallback-command 'hippie-expand
  139. "The expansion command called by \\[org-complete] in normal context.
  140. Normal means, no org-mode-specific context."
  141. :group 'org
  142. :type 'function)
  143. (defgroup org-startup nil
  144. "Options concerning startup of Org-mode."
  145. :tag "Org Startup"
  146. :group 'org)
  147. (defcustom org-startup-folded t
  148. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  149. This can also be configured on a per-file basis by adding one of
  150. the following lines anywhere in the buffer:
  151. #+STARTUP: fold
  152. #+STARTUP: nofold
  153. #+STARTUP: content"
  154. :group 'org-startup
  155. :type '(choice
  156. (const :tag "nofold: show all" nil)
  157. (const :tag "fold: overview" t)
  158. (const :tag "content: all headlines" content)))
  159. (defcustom org-startup-truncated t
  160. "Non-nil means, entering Org-mode will set `truncate-lines'.
  161. This is useful since some lines containing links can be very long and
  162. uninteresting. Also tables look terrible when wrapped."
  163. :group 'org-startup
  164. :type 'boolean)
  165. (defcustom org-startup-align-all-tables nil
  166. "Non-nil means, align all tables when visiting a file.
  167. This is useful when the column width in tables is forced with <N> cookies
  168. in table fields. Such tables will look correct only after the first re-align.
  169. This can also be configured on a per-file basis by adding one of
  170. the following lines anywhere in the buffer:
  171. #+STARTUP: align
  172. #+STARTUP: noalign"
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-insert-mode-line-in-empty-file nil
  176. "Non-nil means insert the first line setting Org-mode in empty files.
  177. When the function `org-mode' is called interactively in an empty file, this
  178. normally means that the file name does not automatically trigger Org-mode.
  179. To ensure that the file will always be in Org-mode in the future, a
  180. line enforcing Org-mode will be inserted into the buffer, if this option
  181. has been set."
  182. :group 'org-startup
  183. :type 'boolean)
  184. (defcustom org-replace-disputed-keys nil
  185. "Non-nil means use alternative key bindings for some keys.
  186. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  187. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  188. If you want to use Org-mode together with one of these other modes,
  189. or more generally if you would like to move some Org-mode commands to
  190. other keys, set this variable and configure the keys with the variable
  191. `org-disputed-keys'.
  192. This option is only relevant at load-time of Org-mode, and must be set
  193. *before* org.el is loaded. Changing it requires a restart of Emacs to
  194. become effective."
  195. :group 'org-startup
  196. :type 'boolean)
  197. (if (fboundp 'defvaralias)
  198. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  199. (defcustom org-disputed-keys
  200. '(([(shift up)] . [(meta p)])
  201. ([(shift down)] . [(meta n)])
  202. ([(shift left)] . [(meta -)])
  203. ([(shift right)] . [(meta +)])
  204. ([(control shift right)] . [(meta shift +)])
  205. ([(control shift left)] . [(meta shift -)]))
  206. "Keys for which Org-mode and other modes compete.
  207. This is an alist, cars are the default keys, second element specifies
  208. the alternative to use when `org-replace-disputed-keys' is t.
  209. Keys can be specified in any syntax supported by `define-key'.
  210. The value of this option takes effect only at Org-mode's startup,
  211. therefore you'll have to restart Emacs to apply it after changing."
  212. :group 'org-startup
  213. :type 'alist)
  214. (defun org-key (key)
  215. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  216. Or return the original if not disputed."
  217. (if org-replace-disputed-keys
  218. (let* ((nkey (key-description key))
  219. (x (org-find-if (lambda (x)
  220. (equal (key-description (car x)) nkey))
  221. org-disputed-keys)))
  222. (if x (cdr x) key))
  223. key))
  224. (defun org-find-if (predicate seq)
  225. (catch 'exit
  226. (while seq
  227. (if (funcall predicate (car seq))
  228. (throw 'exit (car seq))
  229. (pop seq)))))
  230. (defun org-defkey (keymap key def)
  231. "Define a key, possibly translated, as returned by `org-key'."
  232. (define-key keymap (org-key key) def))
  233. (defcustom org-ellipsis nil
  234. "The ellipsis to use in the Org-mode outline.
  235. When nil, just use the standard three dots. When a string, use that instead,
  236. When a face, use the standart 3 dots, but with the specified face.
  237. The change affects only Org-mode (which will then use its own display table).
  238. Changing this requires executing `M-x org-mode' in a buffer to become
  239. effective."
  240. :group 'org-startup
  241. :type '(choice (const :tag "Default" nil)
  242. (face :tag "Face" :value org-warning)
  243. (string :tag "String" :value "...#")))
  244. (defvar org-display-table nil
  245. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  246. (defgroup org-keywords nil
  247. "Keywords in Org-mode."
  248. :tag "Org Keywords"
  249. :group 'org)
  250. (defcustom org-deadline-string "DEADLINE:"
  251. "String to mark deadline entries.
  252. A deadline is this string, followed by a time stamp. Should be a word,
  253. terminated by a colon. You can insert a schedule keyword and
  254. a timestamp with \\[org-deadline].
  255. Changes become only effective after restarting Emacs."
  256. :group 'org-keywords
  257. :type 'string)
  258. (defcustom org-scheduled-string "SCHEDULED:"
  259. "String to mark scheduled TODO entries.
  260. A schedule is this string, followed by a time stamp. Should be a word,
  261. terminated by a colon. You can insert a schedule keyword and
  262. a timestamp with \\[org-schedule].
  263. Changes become only effective after restarting Emacs."
  264. :group 'org-keywords
  265. :type 'string)
  266. (defcustom org-closed-string "CLOSED:"
  267. "String used as the prefix for timestamps logging closing a TODO entry."
  268. :group 'org-keywords
  269. :type 'string)
  270. (defcustom org-clock-string "CLOCK:"
  271. "String used as prefix for timestamps clocking work hours on an item."
  272. :group 'org-keywords
  273. :type 'string)
  274. (defcustom org-comment-string "COMMENT"
  275. "Entries starting with this keyword will never be exported.
  276. An entry can be toggled between COMMENT and normal with
  277. \\[org-toggle-comment].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-quote-string "QUOTE"
  282. "Entries starting with this keyword will be exported in fixed-width font.
  283. Quoting applies only to the text in the entry following the headline, and does
  284. not extend beyond the next headline, even if that is lower level.
  285. An entry can be toggled between QUOTE and normal with
  286. \\[org-toggle-fixed-width-section]."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defconst org-repeat-re
  290. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  291. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  292. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  293. "Regular expression for specifying repeated events.
  294. After a match, group 1 contains the repeat expression.")
  295. (defgroup org-structure nil
  296. "Options concerning the general structure of Org-mode files."
  297. :tag "Org Structure"
  298. :group 'org)
  299. (defgroup org-reveal-location nil
  300. "Options about how to make context of a location visible."
  301. :tag "Org Reveal Location"
  302. :group 'org-structure)
  303. (defconst org-context-choice
  304. '(choice
  305. (const :tag "Always" t)
  306. (const :tag "Never" nil)
  307. (repeat :greedy t :tag "Individual contexts"
  308. (cons
  309. (choice :tag "Context"
  310. (const agenda)
  311. (const org-goto)
  312. (const occur-tree)
  313. (const tags-tree)
  314. (const link-search)
  315. (const mark-goto)
  316. (const bookmark-jump)
  317. (const isearch)
  318. (const default))
  319. (boolean))))
  320. "Contexts for the reveal options.")
  321. (defcustom org-show-hierarchy-above '((default . t))
  322. "Non-nil means, show full hierarchy when revealing a location.
  323. Org-mode often shows locations in an org-mode file which might have
  324. been invisible before. When this is set, the hierarchy of headings
  325. above the exposed location is shown.
  326. Turning this off for example for sparse trees makes them very compact.
  327. Instead of t, this can also be an alist specifying this option for different
  328. contexts. Valid contexts are
  329. agenda when exposing an entry from the agenda
  330. org-goto when using the command `org-goto' on key C-c C-j
  331. occur-tree when using the command `org-occur' on key C-c /
  332. tags-tree when constructing a sparse tree based on tags matches
  333. link-search when exposing search matches associated with a link
  334. mark-goto when exposing the jump goal of a mark
  335. bookmark-jump when exposing a bookmark location
  336. isearch when exiting from an incremental search
  337. default default for all contexts not set explicitly"
  338. :group 'org-reveal-location
  339. :type org-context-choice)
  340. (defcustom org-show-following-heading '((default . nil))
  341. "Non-nil means, show following heading when revealing a location.
  342. Org-mode often shows locations in an org-mode file which might have
  343. been invisible before. When this is set, the heading following the
  344. match is shown.
  345. Turning this off for example for sparse trees makes them very compact,
  346. but makes it harder to edit the location of the match. In such a case,
  347. use the command \\[org-reveal] to show more context.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. See `org-show-hierarchy-above' for valid contexts."
  350. :group 'org-reveal-location
  351. :type org-context-choice)
  352. (defcustom org-show-siblings '((default . nil) (isearch t))
  353. "Non-nil means, show all sibling heading when revealing a location.
  354. Org-mode often shows locations in an org-mode file which might have
  355. been invisible before. When this is set, the sibling of the current entry
  356. heading are all made visible. If `org-show-hierarchy-above' is t,
  357. the same happens on each level of the hierarchy above the current entry.
  358. By default this is on for the isearch context, off for all other contexts.
  359. Turning this off for example for sparse trees makes them very compact,
  360. but makes it harder to edit the location of the match. In such a case,
  361. use the command \\[org-reveal] to show more context.
  362. Instead of t, this can also be an alist specifying this option for different
  363. contexts. See `org-show-hierarchy-above' for valid contexts."
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-entry-below '((default . nil))
  367. "Non-nil means, show the entry below a headline when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the text below the headline that is
  370. exposed is also shown.
  371. By default this is off for all contexts.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defgroup org-cycle nil
  377. "Options concerning visibility cycling in Org-mode."
  378. :tag "Org Cycle"
  379. :group 'org-structure)
  380. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  381. "Names of drawers. Drawers are not opened by cycling on the headline above.
  382. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  383. this:
  384. :DRAWERNAME:
  385. .....
  386. :END:
  387. The drawer \"PROPERTIES\" is special for capturing properties through
  388. the property API.
  389. Drawers can be defined on the per-file basis with a line like:
  390. #+DRAWERS: HIDDEN STATE PROPERTIES"
  391. :group 'org-structure
  392. :type '(repeat (string :tag "Drawer Name")))
  393. (defcustom org-cycle-global-at-bob nil
  394. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  395. This makes it possible to do global cycling without having to use S-TAB or
  396. C-u TAB. For this special case to work, the first line of the buffer
  397. must not be a headline - it may be empty ot some other text. When used in
  398. this way, `org-cycle-hook' is disables temporarily, to make sure the
  399. cursor stays at the beginning of the buffer.
  400. When this option is nil, don't do anything special at the beginning
  401. of the buffer."
  402. :group 'org-cycle
  403. :type 'boolean)
  404. (defcustom org-cycle-emulate-tab t
  405. "Where should `org-cycle' emulate TAB.
  406. nil Never
  407. white Only in completely white lines
  408. whitestart Only at the beginning of lines, before the first non-white char
  409. t Everywhere except in headlines
  410. exc-hl-bol Everywhere except at the start of a headline
  411. If TAB is used in a place where it does not emulate TAB, the current subtree
  412. visibility is cycled."
  413. :group 'org-cycle
  414. :type '(choice (const :tag "Never" nil)
  415. (const :tag "Only in completely white lines" white)
  416. (const :tag "Before first char in a line" whitestart)
  417. (const :tag "Everywhere except in headlines" t)
  418. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  419. ))
  420. (defcustom org-cycle-separator-lines 2
  421. "Number of empty lines needed to keep an empty line between collapsed trees.
  422. If you leave an empty line between the end of a subtree and the following
  423. headline, this empty line is hidden when the subtree is folded.
  424. Org-mode will leave (exactly) one empty line visible if the number of
  425. empty lines is equal or larger to the number given in this variable.
  426. So the default 2 means, at least 2 empty lines after the end of a subtree
  427. are needed to produce free space between a collapsed subtree and the
  428. following headline.
  429. Special case: when 0, never leave empty lines in collapsed view."
  430. :group 'org-cycle
  431. :type 'integer)
  432. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  433. org-cycle-hide-drawers
  434. org-cycle-show-empty-lines
  435. org-optimize-window-after-visibility-change)
  436. "Hook that is run after `org-cycle' has changed the buffer visibility.
  437. The function(s) in this hook must accept a single argument which indicates
  438. the new state that was set by the most recent `org-cycle' command. The
  439. argument is a symbol. After a global state change, it can have the values
  440. `overview', `content', or `all'. After a local state change, it can have
  441. the values `folded', `children', or `subtree'."
  442. :group 'org-cycle
  443. :type 'hook)
  444. (defgroup org-edit-structure nil
  445. "Options concerning structure editing in Org-mode."
  446. :tag "Org Edit Structure"
  447. :group 'org-structure)
  448. (defcustom org-special-ctrl-a/e nil
  449. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  450. When t, `C-a' will bring back the cursor to the beginning of the
  451. headline text, i.e. after the stars and after a possible TODO keyword.
  452. In an item, this will be the position after the bullet.
  453. When the cursor is already at that position, another `C-a' will bring
  454. it to the beginning of the line.
  455. `C-e' will jump to the end of the headline, ignoring the presence of tags
  456. in the headline. A second `C-e' will then jump to the true end of the
  457. line, after any tags.
  458. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  459. and only a directly following, identical keypress will bring the cursor
  460. to the special positions."
  461. :group 'org-edit-structure
  462. :type '(choice
  463. (const :tag "off" nil)
  464. (const :tag "after bullet first" t)
  465. (const :tag "border first" reversed)))
  466. (if (fboundp 'defvaralias)
  467. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  468. (defcustom org-special-ctrl-k nil
  469. "Non-nil means `C-k' will behave specially in headlines.
  470. When nil, `C-k' will call the default `kill-line' command.
  471. When t, the following will happen while the cursor is in the headline:
  472. - When the cursor is at the beginning of a headline, kill the entire
  473. line and possible the folded subtree below the line.
  474. - When in the middle of the headline text, kill the headline up to the tags.
  475. - When after the headline text, kill the tags."
  476. :group 'org-edit-structure
  477. :type 'boolean)
  478. (defcustom org-odd-levels-only nil
  479. "Non-nil means, skip even levels and only use odd levels for the outline.
  480. This has the effect that two stars are being added/taken away in
  481. promotion/demotion commands. It also influences how levels are
  482. handled by the exporters.
  483. Changing it requires restart of `font-lock-mode' to become effective
  484. for fontification also in regions already fontified.
  485. You may also set this on a per-file basis by adding one of the following
  486. lines to the buffer:
  487. #+STARTUP: odd
  488. #+STARTUP: oddeven"
  489. :group 'org-edit-structure
  490. :group 'org-font-lock
  491. :type 'boolean)
  492. (defcustom org-adapt-indentation t
  493. "Non-nil means, adapt indentation when promoting and demoting.
  494. When this is set and the *entire* text in an entry is indented, the
  495. indentation is increased by one space in a demotion command, and
  496. decreased by one in a promotion command. If any line in the entry
  497. body starts at column 0, indentation is not changed at all."
  498. :group 'org-edit-structure
  499. :type 'boolean)
  500. (defcustom org-blank-before-new-entry '((heading . nil)
  501. (plain-list-item . nil))
  502. "Should `org-insert-heading' leave a blank line before new heading/item?
  503. The value is an alist, with `heading' and `plain-list-item' as car,
  504. and a boolean flag as cdr."
  505. :group 'org-edit-structure
  506. :type '(list
  507. (cons (const heading) (boolean))
  508. (cons (const plain-list-item) (boolean))))
  509. (defcustom org-insert-heading-hook nil
  510. "Hook being run after inserting a new heading."
  511. :group 'org-edit-structure
  512. :type 'hook)
  513. (defcustom org-enable-fixed-width-editor t
  514. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  515. This currently only means, they are never auto-wrapped.
  516. When nil, such lines will be treated like ordinary lines.
  517. See also the QUOTE keyword."
  518. :group 'org-edit-structure
  519. :type 'boolean)
  520. (defcustom org-goto-auto-isearch t
  521. "Non-nil means, typing characters in org-goto starts incremental search."
  522. :group 'org-edit-structure
  523. :type 'boolean)
  524. (defgroup org-sparse-trees nil
  525. "Options concerning sparse trees in Org-mode."
  526. :tag "Org Sparse Trees"
  527. :group 'org-structure)
  528. (defcustom org-highlight-sparse-tree-matches t
  529. "Non-nil means, highlight all matches that define a sparse tree.
  530. The highlights will automatically disappear the next time the buffer is
  531. changed by an edit command."
  532. :group 'org-sparse-trees
  533. :type 'boolean)
  534. (defcustom org-remove-highlights-with-change t
  535. "Non-nil means, any change to the buffer will remove temporary highlights.
  536. Such highlights are created by `org-occur' and `org-clock-display'.
  537. When nil, `C-c C-c needs to be used to get rid of the highlights.
  538. The highlights created by `org-preview-latex-fragment' always need
  539. `C-c C-c' to be removed."
  540. :group 'org-sparse-trees
  541. :group 'org-time
  542. :type 'boolean)
  543. (defcustom org-occur-hook '(org-first-headline-recenter)
  544. "Hook that is run after `org-occur' has constructed a sparse tree.
  545. This can be used to recenter the window to show as much of the structure
  546. as possible."
  547. :group 'org-sparse-trees
  548. :type 'hook)
  549. (defgroup org-plain-lists nil
  550. "Options concerning plain lists in Org-mode."
  551. :tag "Org Plain lists"
  552. :group 'org-structure)
  553. (defcustom org-cycle-include-plain-lists nil
  554. "Non-nil means, include plain lists into visibility cycling.
  555. This means that during cycling, plain list items will *temporarily* be
  556. interpreted as outline headlines with a level given by 1000+i where i is the
  557. indentation of the bullet. In all other operations, plain list items are
  558. not seen as headlines. For example, you cannot assign a TODO keyword to
  559. such an item."
  560. :group 'org-plain-lists
  561. :type 'boolean)
  562. (defcustom org-plain-list-ordered-item-terminator t
  563. "The character that makes a line with leading number an ordered list item.
  564. Valid values are ?. and ?\). To get both terminators, use t. While
  565. ?. may look nicer, it creates the danger that a line with leading
  566. number may be incorrectly interpreted as an item. ?\) therefore is
  567. the safe choice."
  568. :group 'org-plain-lists
  569. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  570. (const :tag "paren like in \"2)\"" ?\))
  571. (const :tab "both" t)))
  572. (defcustom org-auto-renumber-ordered-lists t
  573. "Non-nil means, automatically renumber ordered plain lists.
  574. Renumbering happens when the sequence have been changed with
  575. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  576. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  577. :group 'org-plain-lists
  578. :type 'boolean)
  579. (defcustom org-provide-checkbox-statistics t
  580. "Non-nil means, update checkbox statistics after insert and toggle.
  581. When this is set, checkbox statistics is updated each time you either insert
  582. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  583. with \\[org-ctrl-c-ctrl-c\\]."
  584. :group 'org-plain-lists
  585. :type 'boolean)
  586. (defgroup org-archive nil
  587. "Options concerning archiving in Org-mode."
  588. :tag "Org Archive"
  589. :group 'org-structure)
  590. (defcustom org-archive-tag "ARCHIVE"
  591. "The tag that marks a subtree as archived.
  592. An archived subtree does not open during visibility cycling, and does
  593. not contribute to the agenda listings.
  594. After changing this, font-lock must be restarted in the relevant buffers to
  595. get the proper fontification."
  596. :group 'org-archive
  597. :group 'org-keywords
  598. :type 'string)
  599. (defcustom org-agenda-skip-archived-trees t
  600. "Non-nil means, the agenda will skip any items located in archived trees.
  601. An archived tree is a tree marked with the tag ARCHIVE."
  602. :group 'org-archive
  603. :group 'org-agenda-skip
  604. :type 'boolean)
  605. (defcustom org-cycle-open-archived-trees nil
  606. "Non-nil means, `org-cycle' will open archived trees.
  607. An archived tree is a tree marked with the tag ARCHIVE.
  608. When nil, archived trees will stay folded. You can still open them with
  609. normal outline commands like `show-all', but not with the cycling commands."
  610. :group 'org-archive
  611. :group 'org-cycle
  612. :type 'boolean)
  613. (defcustom org-sparse-tree-open-archived-trees nil
  614. "Non-nil means sparse tree construction shows matches in archived trees.
  615. When nil, matches in these trees are highlighted, but the trees are kept in
  616. collapsed state."
  617. :group 'org-archive
  618. :group 'org-sparse-trees
  619. :type 'boolean)
  620. (defcustom org-archive-location "%s_archive::"
  621. "The location where subtrees should be archived.
  622. This string consists of two parts, separated by a double-colon.
  623. The first part is a file name - when omitted, archiving happens in the same
  624. file. %s will be replaced by the current file name (without directory part).
  625. Archiving to a different file is useful to keep archived entries from
  626. contributing to the Org-mode Agenda.
  627. The part after the double colon is a headline. The archived entries will be
  628. filed under that headline. When omitted, the subtrees are simply filed away
  629. at the end of the file, as top-level entries.
  630. Here are a few examples:
  631. \"%s_archive::\"
  632. If the current file is Projects.org, archive in file
  633. Projects.org_archive, as top-level trees. This is the default.
  634. \"::* Archived Tasks\"
  635. Archive in the current file, under the top-level headline
  636. \"* Archived Tasks\".
  637. \"~/org/archive.org::\"
  638. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  639. \"basement::** Finished Tasks\"
  640. Archive in file ./basement (relative path), as level 3 trees
  641. below the level 2 heading \"** Finished Tasks\".
  642. You may set this option on a per-file basis by adding to the buffer a
  643. line like
  644. #+ARCHIVE: basement::** Finished Tasks"
  645. :group 'org-archive
  646. :type 'string)
  647. (defcustom org-archive-mark-done t
  648. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  649. This can be a string to set the keyword to use. When t, Org-mode will
  650. use the first keyword in its list that means done."
  651. :group 'org-archive
  652. :type '(choice
  653. (const :tag "No" nil)
  654. (const :tag "Yes" t)
  655. (string :tag "Use this keyword")))
  656. (defcustom org-archive-stamp-time t
  657. "Non-nil means, add a time stamp to entries moved to an archive file.
  658. This variable is obsolete and has no effect anymore, instead add ot remove
  659. `time' from the variablle `org-archive-save-context-info'."
  660. :group 'org-archive
  661. :type 'boolean)
  662. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  663. "Parts of context info that should be stored as properties when archiving.
  664. When a subtree is moved to an archive file, it looses information given by
  665. context, like inherited tags, the category, and possibly also the TODO
  666. state (depending on the variable `org-archive-mark-done').
  667. This variable can be a list of any of the following symbols:
  668. time The time of archiving.
  669. file The file where the entry originates.
  670. itags The local tags, in the headline of the subtree.
  671. ltags The tags the subtree inherits from further up the hierarchy.
  672. todo The pre-archive TODO state.
  673. category The category, taken from file name or #+CATEGORY lines.
  674. olpath The outline path to the item. These are all headlines above
  675. the current item, separated by /, like a file path.
  676. For each symbol present in the list, a property will be created in
  677. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  678. information."
  679. :group 'org-archive
  680. :type '(set :greedy t
  681. (const :tag "Time" time)
  682. (const :tag "File" file)
  683. (const :tag "Category" category)
  684. (const :tag "TODO state" todo)
  685. (const :tag "TODO state" priority)
  686. (const :tag "Inherited tags" itags)
  687. (const :tag "Outline path" olpath)
  688. (const :tag "Local tags" ltags)))
  689. (defgroup org-imenu-and-speedbar nil
  690. "Options concerning imenu and speedbar in Org-mode."
  691. :tag "Org Imenu and Speedbar"
  692. :group 'org-structure)
  693. (defcustom org-imenu-depth 2
  694. "The maximum level for Imenu access to Org-mode headlines.
  695. This also applied for speedbar access."
  696. :group 'org-imenu-and-speedbar
  697. :type 'number)
  698. (defgroup org-table nil
  699. "Options concerning tables in Org-mode."
  700. :tag "Org Table"
  701. :group 'org)
  702. (defcustom org-enable-table-editor 'optimized
  703. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  704. When nil, such lines will be treated like ordinary lines.
  705. When equal to the symbol `optimized', the table editor will be optimized to
  706. do the following:
  707. - Automatic overwrite mode in front of whitespace in table fields.
  708. This makes the structure of the table stay in tact as long as the edited
  709. field does not exceed the column width.
  710. - Minimize the number of realigns. Normally, the table is aligned each time
  711. TAB or RET are pressed to move to another field. With optimization this
  712. happens only if changes to a field might have changed the column width.
  713. Optimization requires replacing the functions `self-insert-command',
  714. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  715. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  716. very good at guessing when a re-align will be necessary, but you can always
  717. force one with \\[org-ctrl-c-ctrl-c].
  718. If you would like to use the optimized version in Org-mode, but the
  719. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  720. This variable can be used to turn on and off the table editor during a session,
  721. but in order to toggle optimization, a restart is required.
  722. See also the variable `org-table-auto-blank-field'."
  723. :group 'org-table
  724. :type '(choice
  725. (const :tag "off" nil)
  726. (const :tag "on" t)
  727. (const :tag "on, optimized" optimized)))
  728. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  729. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  730. In the optimized version, the table editor takes over all simple keys that
  731. normally just insert a character. In tables, the characters are inserted
  732. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  733. for empty fields). Outside tables, the correct binding of the keys is
  734. restored.
  735. The default for this option is t if the optimized version is also used in
  736. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  737. this variable requires a restart of Emacs to become effective."
  738. :group 'org-table
  739. :type 'boolean)
  740. (defcustom orgtbl-radio-table-templates
  741. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  742. % END RECEIVE ORGTBL %n
  743. \\begin{comment}
  744. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  745. | | |
  746. \\end{comment}\n")
  747. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  748. @c END RECEIVE ORGTBL %n
  749. @ignore
  750. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  751. | | |
  752. @end ignore\n")
  753. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  754. <!-- END RECEIVE ORGTBL %n -->
  755. <!--
  756. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  757. | | |
  758. -->\n"))
  759. "Templates for radio tables in different major modes.
  760. All occurrences of %n in a template will be replaced with the name of the
  761. table, obtained by prompting the user."
  762. :group 'org-table
  763. :type '(repeat
  764. (list (symbol :tag "Major mode")
  765. (string :tag "Format"))))
  766. (defgroup org-table-settings nil
  767. "Settings for tables in Org-mode."
  768. :tag "Org Table Settings"
  769. :group 'org-table)
  770. (defcustom org-table-default-size "5x2"
  771. "The default size for newly created tables, Columns x Rows."
  772. :group 'org-table-settings
  773. :type 'string)
  774. (defcustom org-table-number-regexp
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  776. "Regular expression for recognizing numbers in table columns.
  777. If a table column contains mostly numbers, it will be aligned to the
  778. right. If not, it will be aligned to the left.
  779. The default value of this option is a regular expression which allows
  780. anything which looks remotely like a number as used in scientific
  781. context. For example, all of the following will be considered a
  782. number:
  783. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  784. Other options offered by the customize interface are more restrictive."
  785. :group 'org-table-settings
  786. :type '(choice
  787. (const :tag "Positive Integers"
  788. "^[0-9]+$")
  789. (const :tag "Integers"
  790. "^[-+]?[0-9]+$")
  791. (const :tag "Floating Point Numbers"
  792. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  793. (const :tag "Floating Point Number or Integer"
  794. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  795. (const :tag "Exponential, Floating point, Integer"
  796. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  797. (const :tag "Very General Number-Like, including hex"
  798. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  799. (string :tag "Regexp:")))
  800. (defcustom org-table-number-fraction 0.5
  801. "Fraction of numbers in a column required to make the column align right.
  802. In a column all non-white fields are considered. If at least this
  803. fraction of fields is matched by `org-table-number-fraction',
  804. alignment to the right border applies."
  805. :group 'org-table-settings
  806. :type 'number)
  807. (defgroup org-table-editing nil
  808. "Behavior of tables during editing in Org-mode."
  809. :tag "Org Table Editing"
  810. :group 'org-table)
  811. (defcustom org-table-automatic-realign t
  812. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  813. When nil, aligning is only done with \\[org-table-align], or after column
  814. removal/insertion."
  815. :group 'org-table-editing
  816. :type 'boolean)
  817. (defcustom org-table-auto-blank-field t
  818. "Non-nil means, automatically blank table field when starting to type into it.
  819. This only happens when typing immediately after a field motion
  820. command (TAB, S-TAB or RET).
  821. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  822. :group 'org-table-editing
  823. :type 'boolean)
  824. (defcustom org-table-tab-jumps-over-hlines t
  825. "Non-nil means, tab in the last column of a table with jump over a hline.
  826. If a horizontal separator line is following the current line,
  827. `org-table-next-field' can either create a new row before that line, or jump
  828. over the line. When this option is nil, a new line will be created before
  829. this line."
  830. :group 'org-table-editing
  831. :type 'boolean)
  832. (defcustom org-table-tab-recognizes-table.el t
  833. "Non-nil means, TAB will automatically notice a table.el table.
  834. When it sees such a table, it moves point into it and - if necessary -
  835. calls `table-recognize-table'."
  836. :group 'org-table-editing
  837. :type 'boolean)
  838. (defgroup org-table-calculation nil
  839. "Options concerning tables in Org-mode."
  840. :tag "Org Table Calculation"
  841. :group 'org-table)
  842. (defcustom org-table-use-standard-references t
  843. "Should org-mode work with table refrences like B3 instead of @3$2?
  844. Possible values are:
  845. nil never use them
  846. from accept as input, do not present for editing
  847. t: accept as input and present for editing"
  848. :group 'org-table-calculation
  849. :type '(choice
  850. (const :tag "Never, don't even check unser input for them" nil)
  851. (const :tag "Always, both as user input, and when editing" t)
  852. (const :tag "Convert user input, don't offer during editing" 'from)))
  853. (defcustom org-table-copy-increment t
  854. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  855. :group 'org-table-calculation
  856. :type 'boolean)
  857. (defcustom org-calc-default-modes
  858. '(calc-internal-prec 12
  859. calc-float-format (float 5)
  860. calc-angle-mode deg
  861. calc-prefer-frac nil
  862. calc-symbolic-mode nil
  863. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  864. calc-display-working-message t
  865. )
  866. "List with Calc mode settings for use in calc-eval for table formulas.
  867. The list must contain alternating symbols (Calc modes variables and values).
  868. Don't remove any of the default settings, just change the values. Org-mode
  869. relies on the variables to be present in the list."
  870. :group 'org-table-calculation
  871. :type 'plist)
  872. (defcustom org-table-formula-evaluate-inline t
  873. "Non-nil means, TAB and RET evaluate a formula in current table field.
  874. If the current field starts with an equal sign, it is assumed to be a formula
  875. which should be evaluated as described in the manual and in the documentation
  876. string of the command `org-table-eval-formula'. This feature requires the
  877. Emacs calc package.
  878. When this variable is nil, formula calculation is only available through
  879. the command \\[org-table-eval-formula]."
  880. :group 'org-table-calculation
  881. :type 'boolean)
  882. (defcustom org-table-formula-use-constants t
  883. "Non-nil means, interpret constants in formulas in tables.
  884. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  885. by the value given in `org-table-formula-constants', or by a value obtained
  886. from the `constants.el' package."
  887. :group 'org-table-calculation
  888. :type 'boolean)
  889. (defcustom org-table-formula-constants nil
  890. "Alist with constant names and values, for use in table formulas.
  891. The car of each element is a name of a constant, without the `$' before it.
  892. The cdr is the value as a string. For example, if you'd like to use the
  893. speed of light in a formula, you would configure
  894. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  895. and then use it in an equation like `$1*$c'.
  896. Constants can also be defined on a per-file basis using a line like
  897. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  898. :group 'org-table-calculation
  899. :type '(repeat
  900. (cons (string :tag "name")
  901. (string :tag "value"))))
  902. (defvar org-table-formula-constants-local nil
  903. "Local version of `org-table-formula-constants'.")
  904. (make-variable-buffer-local 'org-table-formula-constants-local)
  905. (defcustom org-table-allow-automatic-line-recalculation t
  906. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  907. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  908. :group 'org-table-calculation
  909. :type 'boolean)
  910. (defgroup org-link nil
  911. "Options concerning links in Org-mode."
  912. :tag "Org Link"
  913. :group 'org)
  914. (defvar org-link-abbrev-alist-local nil
  915. "Buffer-local version of `org-link-abbrev-alist', which see.
  916. The value of this is taken from the #+LINK lines.")
  917. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  918. (defcustom org-link-abbrev-alist nil
  919. "Alist of link abbreviations.
  920. The car of each element is a string, to be replaced at the start of a link.
  921. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  922. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  923. [[linkkey:tag][description]]
  924. If REPLACE is a string, the tag will simply be appended to create the link.
  925. If the string contains \"%s\", the tag will be inserted there.
  926. REPLACE may also be a function that will be called with the tag as the
  927. only argument to create the link, which should be returned as a string.
  928. See the manual for examples."
  929. :group 'org-link
  930. :type 'alist)
  931. (defcustom org-descriptive-links t
  932. "Non-nil means, hide link part and only show description of bracket links.
  933. Bracket links are like [[link][descritpion]]. This variable sets the initial
  934. state in new org-mode buffers. The setting can then be toggled on a
  935. per-buffer basis from the Org->Hyperlinks menu."
  936. :group 'org-link
  937. :type 'boolean)
  938. (defcustom org-link-file-path-type 'adaptive
  939. "How the path name in file links should be stored.
  940. Valid values are:
  941. relative Relative to the current directory, i.e. the directory of the file
  942. into which the link is being inserted.
  943. absolute Absolute path, if possible with ~ for home directory.
  944. noabbrev Absolute path, no abbreviation of home directory.
  945. adaptive Use relative path for files in the current directory and sub-
  946. directories of it. For other files, use an absolute path."
  947. :group 'org-link
  948. :type '(choice
  949. (const relative)
  950. (const absolute)
  951. (const noabbrev)
  952. (const adaptive)))
  953. (defcustom org-activate-links '(bracket angle plain radio tag date)
  954. "Types of links that should be activated in Org-mode files.
  955. This is a list of symbols, each leading to the activation of a certain link
  956. type. In principle, it does not hurt to turn on most link types - there may
  957. be a small gain when turning off unused link types. The types are:
  958. bracket The recommended [[link][description]] or [[link]] links with hiding.
  959. angular Links in angular brackes that may contain whitespace like
  960. <bbdb:Carsten Dominik>.
  961. plain Plain links in normal text, no whitespace, like http://google.com.
  962. radio Text that is matched by a radio target, see manual for details.
  963. tag Tag settings in a headline (link to tag search).
  964. date Time stamps (link to calendar).
  965. Changing this variable requires a restart of Emacs to become effective."
  966. :group 'org-link
  967. :type '(set (const :tag "Double bracket links (new style)" bracket)
  968. (const :tag "Angular bracket links (old style)" angular)
  969. (const :tag "plain text links" plain)
  970. (const :tag "Radio target matches" radio)
  971. (const :tag "Tags" tag)
  972. (const :tag "Tags" target)
  973. (const :tag "Timestamps" date)))
  974. (defgroup org-link-store nil
  975. "Options concerning storing links in Org-mode"
  976. :tag "Org Store Link"
  977. :group 'org-link)
  978. (defcustom org-email-link-description-format "Email %c: %.30s"
  979. "Format of the description part of a link to an email or usenet message.
  980. The following %-excapes will be replaced by corresponding information:
  981. %F full \"From\" field
  982. %f name, taken from \"From\" field, address if no name
  983. %T full \"To\" field
  984. %t first name in \"To\" field, address if no name
  985. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  986. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  987. %s subject
  988. %m message-id.
  989. You may use normal field width specification between the % and the letter.
  990. This is for example useful to limit the length of the subject.
  991. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  992. :group 'org-link-store
  993. :type 'string)
  994. (defcustom org-from-is-user-regexp
  995. (let (r1 r2)
  996. (when (and user-mail-address (not (string= user-mail-address "")))
  997. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  998. (when (and user-full-name (not (string= user-full-name "")))
  999. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1000. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1001. "Regexp mached against the \"From:\" header of an email or usenet message.
  1002. It should match if the message is from the user him/herself."
  1003. :group 'org-link-store
  1004. :type 'regexp)
  1005. (defcustom org-context-in-file-links t
  1006. "Non-nil means, file links from `org-store-link' contain context.
  1007. A search string will be added to the file name with :: as separator and
  1008. used to find the context when the link is activated by the command
  1009. `org-open-at-point'.
  1010. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1011. negates this setting for the duration of the command."
  1012. :group 'org-link-store
  1013. :type 'boolean)
  1014. (defcustom org-keep-stored-link-after-insertion nil
  1015. "Non-nil means, keep link in list for entire session.
  1016. The command `org-store-link' adds a link pointing to the current
  1017. location to an internal list. These links accumulate during a session.
  1018. The command `org-insert-link' can be used to insert links into any
  1019. Org-mode file (offering completion for all stored links). When this
  1020. option is nil, every link which has been inserted once using \\[org-insert-link]
  1021. will be removed from the list, to make completing the unused links
  1022. more efficient."
  1023. :group 'org-link-store
  1024. :type 'boolean)
  1025. (defcustom org-usenet-links-prefer-google nil
  1026. "Non-nil means, `org-store-link' will create web links to Google groups.
  1027. When nil, Gnus will be used for such links.
  1028. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1029. negates this setting for the duration of the command."
  1030. :group 'org-link-store
  1031. :type 'boolean)
  1032. (defgroup org-link-follow nil
  1033. "Options concerning following links in Org-mode"
  1034. :tag "Org Follow Link"
  1035. :group 'org-link)
  1036. (defcustom org-tab-follows-link nil
  1037. "Non-nil means, on links TAB will follow the link.
  1038. Needs to be set before org.el is loaded."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-return-follows-link nil
  1042. "Non-nil means, on links RET will follow the link.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-mouse-1-follows-link t
  1047. "Non-nil means, mouse-1 on a link will follow the link.
  1048. A longer mouse click will still set point. Does not wortk on XEmacs.
  1049. Needs to be set before org.el is loaded."
  1050. :group 'org-link-follow
  1051. :type 'boolean)
  1052. (defcustom org-mark-ring-length 4
  1053. "Number of different positions to be recorded in the ring
  1054. Changing this requires a restart of Emacs to work correctly."
  1055. :group 'org-link-follow
  1056. :type 'interger)
  1057. (defcustom org-link-frame-setup
  1058. '((vm . vm-visit-folder-other-frame)
  1059. (gnus . gnus-other-frame)
  1060. (file . find-file-other-window))
  1061. "Setup the frame configuration for following links.
  1062. When following a link with Emacs, it may often be useful to display
  1063. this link in another window or frame. This variable can be used to
  1064. set this up for the different types of links.
  1065. For VM, use any of
  1066. `vm-visit-folder'
  1067. `vm-visit-folder-other-frame'
  1068. For Gnus, use any of
  1069. `gnus'
  1070. `gnus-other-frame'
  1071. For FILE, use any of
  1072. `find-file'
  1073. `find-file-other-window'
  1074. `find-file-other-frame'
  1075. For the calendar, use the variable `calendar-setup'.
  1076. For BBDB, it is currently only possible to display the matches in
  1077. another window."
  1078. :group 'org-link-follow
  1079. :type '(list
  1080. (cons (const vm)
  1081. (choice
  1082. (const vm-visit-folder)
  1083. (const vm-visit-folder-other-window)
  1084. (const vm-visit-folder-other-frame)))
  1085. (cons (const gnus)
  1086. (choice
  1087. (const gnus)
  1088. (const gnus-other-frame)))
  1089. (cons (const file)
  1090. (choice
  1091. (const find-file)
  1092. (const find-file-other-window)
  1093. (const find-file-other-frame)))))
  1094. (defcustom org-display-internal-link-with-indirect-buffer nil
  1095. "Non-nil means, use indirect buffer to display infile links.
  1096. Activating internal links (from one location in a file to another location
  1097. in the same file) normally just jumps to the location. When the link is
  1098. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1099. another window. When this option is set, the other window actually displays
  1100. an indirect buffer clone of the current buffer, to avoid any visibility
  1101. changes to the current buffer."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-open-non-existing-files nil
  1105. "Non-nil means, `org-open-file' will open non-existing files.
  1106. When nil, an error will be generated."
  1107. :group 'org-link-follow
  1108. :type 'boolean)
  1109. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1110. "Function and arguments to call for following mailto links.
  1111. This is a list with the first element being a lisp function, and the
  1112. remaining elements being arguments to the function. In string arguments,
  1113. %a will be replaced by the address, and %s will be replaced by the subject
  1114. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1115. :group 'org-link-follow
  1116. :type '(choice
  1117. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1118. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1119. (const :tag "message-mail" (message-mail "%a" "%s"))
  1120. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1121. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1122. "Non-nil means, ask for confirmation before executing shell links.
  1123. Shell links can be dangerous: just think about a link
  1124. [[shell:rm -rf ~/*][Google Search]]
  1125. This link would show up in your Org-mode document as \"Google Search\",
  1126. but really it would remove your entire home directory.
  1127. Therefore we advise against setting this variable to nil.
  1128. Just change it to `y-or-n-p' of you want to confirm with a
  1129. single keystroke rather than having to type \"yes\"."
  1130. :group 'org-link-follow
  1131. :type '(choice
  1132. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1133. (const :tag "with y-or-n (faster)" y-or-n-p)
  1134. (const :tag "no confirmation (dangerous)" nil)))
  1135. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1136. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1137. Elisp links can be dangerous: just think about a link
  1138. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1139. This link would show up in your Org-mode document as \"Google Search\",
  1140. but really it would remove your entire home directory.
  1141. Therefore we advise against setting this variable to nil.
  1142. Just change it to `y-or-n-p' of you want to confirm with a
  1143. single keystroke rather than having to type \"yes\"."
  1144. :group 'org-link-follow
  1145. :type '(choice
  1146. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1147. (const :tag "with y-or-n (faster)" y-or-n-p)
  1148. (const :tag "no confirmation (dangerous)" nil)))
  1149. (defconst org-file-apps-defaults-gnu
  1150. '((remote . emacs)
  1151. (t . mailcap))
  1152. "Default file applications on a UNIX or GNU/Linux system.
  1153. See `org-file-apps'.")
  1154. (defconst org-file-apps-defaults-macosx
  1155. '((remote . emacs)
  1156. (t . "open %s")
  1157. ("ps" . "gv %s")
  1158. ("ps.gz" . "gv %s")
  1159. ("eps" . "gv %s")
  1160. ("eps.gz" . "gv %s")
  1161. ("dvi" . "xdvi %s")
  1162. ("fig" . "xfig %s"))
  1163. "Default file applications on a MacOS X system.
  1164. The system \"open\" is known as a default, but we use X11 applications
  1165. for some files for which the OS does not have a good default.
  1166. See `org-file-apps'.")
  1167. (defconst org-file-apps-defaults-windowsnt
  1168. (list
  1169. '(remote . emacs)
  1170. (cons t
  1171. (list (if (featurep 'xemacs)
  1172. 'mswindows-shell-execute
  1173. 'w32-shell-execute)
  1174. "open" 'file)))
  1175. "Default file applications on a Windows NT system.
  1176. The system \"open\" is used for most files.
  1177. See `org-file-apps'.")
  1178. (defcustom org-file-apps
  1179. '(
  1180. ("txt" . emacs)
  1181. ("tex" . emacs)
  1182. ("ltx" . emacs)
  1183. ("org" . emacs)
  1184. ("el" . emacs)
  1185. ("bib" . emacs)
  1186. )
  1187. "External applications for opening `file:path' items in a document.
  1188. Org-mode uses system defaults for different file types, but
  1189. you can use this variable to set the application for a given file
  1190. extension. The entries in this list are cons cells where the car identifies
  1191. files and the cdr the corresponding command. Possible values for the
  1192. file identifier are
  1193. \"ext\" A string identifying an extension
  1194. `directory' Matches a directory
  1195. `remote' Matches a remote file, accessible through tramp or efs.
  1196. Remote files most likely should be visited through Emacs
  1197. because external applications cannot handle such paths.
  1198. t Default for all remaining files
  1199. Possible values for the command are:
  1200. `emacs' The file will be visited by the current Emacs process.
  1201. `default' Use the default application for this file type.
  1202. string A command to be executed by a shell; %s will be replaced
  1203. by the path to the file.
  1204. sexp A Lisp form which will be evaluated. The file path will
  1205. be available in the Lisp variable `file'.
  1206. For more examples, see the system specific constants
  1207. `org-file-apps-defaults-macosx'
  1208. `org-file-apps-defaults-windowsnt'
  1209. `org-file-apps-defaults-gnu'."
  1210. :group 'org-link-follow
  1211. :type '(repeat
  1212. (cons (choice :value ""
  1213. (string :tag "Extension")
  1214. (const :tag "Default for unrecognized files" t)
  1215. (const :tag "Remote file" remote)
  1216. (const :tag "Links to a directory" directory))
  1217. (choice :value ""
  1218. (const :tag "Visit with Emacs" emacs)
  1219. (const :tag "Use system default" default)
  1220. (string :tag "Command")
  1221. (sexp :tag "Lisp form")))))
  1222. (defcustom org-mhe-search-all-folders nil
  1223. "Non-nil means, that the search for the mh-message will be extended to
  1224. all folders if the message cannot be found in the folder given in the link.
  1225. Searching all folders is very efficient with one of the search engines
  1226. supported by MH-E, but will be slow with pick."
  1227. :group 'org-link-follow
  1228. :type 'boolean)
  1229. (defgroup org-remember nil
  1230. "Options concerning interaction with remember.el."
  1231. :tag "Org Remember"
  1232. :group 'org)
  1233. (defcustom org-directory "~/org"
  1234. "Directory with org files.
  1235. This directory will be used as default to prompt for org files.
  1236. Used by the hooks for remember.el."
  1237. :group 'org-remember
  1238. :type 'directory)
  1239. (defcustom org-default-notes-file "~/.notes"
  1240. "Default target for storing notes.
  1241. Used by the hooks for remember.el. This can be a string, or nil to mean
  1242. the value of `remember-data-file'.
  1243. You can set this on a per-template basis with the variable
  1244. `org-remember-templates'."
  1245. :group 'org-remember
  1246. :type '(choice
  1247. (const :tag "Default from remember-data-file" nil)
  1248. file))
  1249. (defcustom org-remember-store-without-prompt t
  1250. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1251. In this case, you need `C-u C-c C-c' to get the prompts for
  1252. note file and headline.
  1253. When this variable is nil, `C-c C-c' give you the prompts, and
  1254. `C-u C-c C-c' trigger the fasttrack."
  1255. :group 'org-remember
  1256. :type 'boolean)
  1257. (defcustom org-remember-interactive-interface 'refile
  1258. "The interface to be used for interactive filing of remember notes.
  1259. This is only used when the interactive mode for selecting a filing
  1260. location is used (see the variable `org-remember-store-without-prompt').
  1261. Allowed vaues are:
  1262. outline The interface shows an outline of the relevant file
  1263. and the correct heading is found by moving through
  1264. the outline or by searching with incremental search.
  1265. outline-path-completion Headlines in the current buffer are offered via
  1266. completion.
  1267. refile Use the refile interface, and offer headlines,
  1268. possibly from different buffers."
  1269. :group 'org-remember
  1270. :type '(choice
  1271. (const :tag "Refile" refile)
  1272. (const :tag "Outline" outline)
  1273. (const :tag "Outline-path-completion" outline-path-completion)))
  1274. (defcustom org-goto-interface 'outline
  1275. "The default interface to be used for `org-goto'.
  1276. Allowed vaues are:
  1277. outline The interface shows an outline of the relevant file
  1278. and the correct heading is found by moving through
  1279. the outline or by searching with incremental search.
  1280. outline-path-completion Headlines in the current buffer are offered via
  1281. completion."
  1282. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1283. :type '(choice
  1284. (const :tag "Outline" outline)
  1285. (const :tag "Outline-path-completion" outline-path-completion)))
  1286. (defcustom org-remember-default-headline ""
  1287. "The headline that should be the default location in the notes file.
  1288. When filing remember notes, the cursor will start at that position.
  1289. You can set this on a per-template basis with the variable
  1290. `org-remember-templates'."
  1291. :group 'org-remember
  1292. :type 'string)
  1293. (defcustom org-remember-templates nil
  1294. "Templates for the creation of remember buffers.
  1295. When nil, just let remember make the buffer.
  1296. When not nil, this is a list of 5-element lists. In each entry, the first
  1297. element is the name of the template, which should be a single short word.
  1298. The second element is a character, a unique key to select this template.
  1299. The third element is the template. The fourth element is optional and can
  1300. specify a destination file for remember items created with this template.
  1301. The default file is given by `org-default-notes-file'. An optional fifth
  1302. element can specify the headline in that file that should be offered
  1303. first when the user is asked to file the entry. The default headline is
  1304. given in the variable `org-remember-default-headline'.
  1305. The template specifies the structure of the remember buffer. It should have
  1306. a first line starting with a star, to act as the org-mode headline.
  1307. Furthermore, the following %-escapes will be replaced with content:
  1308. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1309. A default value and a completion table ca be specified like this:
  1310. %^{prompt|default|completion2|completion3|...}
  1311. %t time stamp, date only
  1312. %T time stamp with date and time
  1313. %u, %U like the above, but inactive time stamps
  1314. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1315. You may define a prompt like %^{Please specify birthday}t
  1316. %n user name (taken from `user-full-name')
  1317. %a annotation, normally the link created with org-store-link
  1318. %i initial content, the region active. If %i is indented,
  1319. the entire inserted text will be indented as well.
  1320. %c content of the clipboard, or current kill ring head
  1321. %^g prompt for tags, with completion on tags in target file
  1322. %^G prompt for tags, with completion all tags in all agenda files
  1323. %:keyword specific information for certain link types, see below
  1324. %[pathname] insert the contents of the file given by `pathname'
  1325. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1326. %! Store this note immediately after filling the template
  1327. %? After completing the template, position cursor here.
  1328. Apart from these general escapes, you can access information specific to the
  1329. link type that is created. For example, calling `remember' in emails or gnus
  1330. will record the author and the subject of the message, which you can access
  1331. with %:author and %:subject, respectively. Here is a complete list of what
  1332. is recorded for each link type.
  1333. Link type | Available information
  1334. -------------------+------------------------------------------------------
  1335. bbdb | %:type %:name %:company
  1336. vm, wl, mh, rmail | %:type %:subject %:message-id
  1337. | %:from %:fromname %:fromaddress
  1338. | %:to %:toname %:toaddress
  1339. | %:fromto (either \"to NAME\" or \"from NAME\")
  1340. gnus | %:group, for messages also all email fields
  1341. w3, w3m | %:type %:url
  1342. info | %:type %:file %:node
  1343. calendar | %:type %:date"
  1344. :group 'org-remember
  1345. :get (lambda (var) ; Make sure all entries have 5 elements
  1346. (mapcar (lambda (x)
  1347. (if (not (stringp (car x))) (setq x (cons "" x)))
  1348. (cond ((= (length x) 4) (append x '("")))
  1349. ((= (length x) 3) (append x '("" "")))
  1350. (t x)))
  1351. (default-value var)))
  1352. :type '(repeat
  1353. :tag "enabled"
  1354. (list :value ("" ?a "\n" nil nil)
  1355. (string :tag "Name")
  1356. (character :tag "Selection Key")
  1357. (string :tag "Template")
  1358. (choice
  1359. (file :tag "Destination file")
  1360. (const :tag "Prompt for file" nil))
  1361. (choice
  1362. (string :tag "Destination headline")
  1363. (const :tag "Selection interface for heading")))))
  1364. (defcustom org-reverse-note-order nil
  1365. "Non-nil means, store new notes at the beginning of a file or entry.
  1366. When nil, new notes will be filed to the end of a file or entry.
  1367. This can also be a list with cons cells of regular expressions that
  1368. are matched against file names, and values."
  1369. :group 'org-remember
  1370. :type '(choice
  1371. (const :tag "Reverse always" t)
  1372. (const :tag "Reverse never" nil)
  1373. (repeat :tag "By file name regexp"
  1374. (cons regexp boolean))))
  1375. (defcustom org-refile-targets nil
  1376. "Targets for refiling entries with \\[org-refile].
  1377. This is list of cons cells. Each cell contains:
  1378. - a specification of the files to be considered, either a list of files,
  1379. or a symbol whose function or value fields will be used to retrieve
  1380. a file name or a list of file names. Nil means, refile to a different
  1381. heading in the current buffer.
  1382. - A specification of how to find candidate refile targets. This may be
  1383. any of
  1384. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1385. This tag has to be present in all target headlines, inheritance will
  1386. not be considered.
  1387. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1388. todo keyword.
  1389. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1390. headlines that are refiling targets.
  1391. - a cons cell (:level . N). Any headline of level N is considered a target.
  1392. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1393. ;; FIXME: what if there are a var and func with same name???
  1394. :group 'org-remember
  1395. :type '(repeat
  1396. (cons
  1397. (choice :value org-agenda-files
  1398. (const :tag "All agenda files" org-agenda-files)
  1399. (const :tag "Current buffer" nil)
  1400. (function) (variable) (file))
  1401. (choice :tag "Identify target headline by"
  1402. (cons :tag "Specific tag" (const :tag) (string))
  1403. (cons :tag "TODO keyword" (const :todo) (string))
  1404. (cons :tag "Regular expression" (const :regexp) (regexp))
  1405. (cons :tag "Level number" (const :level) (integer))
  1406. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1407. (defcustom org-refile-use-outline-path nil
  1408. "Non-nil means, provide refile targets as paths.
  1409. So a level 3 headline will be available as level1/level2/level3.
  1410. When the value is `file', also include the file name (without directory)
  1411. into the path. When `full-file-path', include the full file path."
  1412. :group 'org-remember
  1413. :type '(choice
  1414. (const :tag "Not" nil)
  1415. (const :tag "Yes" t)
  1416. (const :tag "Start with file name" file)
  1417. (const :tag "Start with full file path" full-file-path)))
  1418. (defgroup org-todo nil
  1419. "Options concerning TODO items in Org-mode."
  1420. :tag "Org TODO"
  1421. :group 'org)
  1422. (defgroup org-progress nil
  1423. "Options concerning Progress logging in Org-mode."
  1424. :tag "Org Progress"
  1425. :group 'org-time)
  1426. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1427. "List of TODO entry keyword sequences and their interpretation.
  1428. \\<org-mode-map>This is a list of sequences.
  1429. Each sequence starts with a symbol, either `sequence' or `type',
  1430. indicating if the keywords should be interpreted as a sequence of
  1431. action steps, or as different types of TODO items. The first
  1432. keywords are states requiring action - these states will select a headline
  1433. for inclusion into the global TODO list Org-mode produces. If one of
  1434. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1435. signify that no further action is necessary. If \"|\" is not found,
  1436. the last keyword is treated as the only DONE state of the sequence.
  1437. The command \\[org-todo] cycles an entry through these states, and one
  1438. additional state where no keyword is present. For details about this
  1439. cycling, see the manual.
  1440. TODO keywords and interpretation can also be set on a per-file basis with
  1441. the special #+SEQ_TODO and #+TYP_TODO lines.
  1442. Each keyword can optionally specify a character for fast state selection
  1443. \(in combination with the variable `org-use-fast-todo-selection')
  1444. and specifiers for state change logging, using the same syntax
  1445. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1446. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1447. indicates to record a time stamp each time this state is selected.
  1448. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1449. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1450. entering and when leaving this state.
  1451. For backward compatibility, this variable may also be just a list
  1452. of keywords - in this case the interptetation (sequence or type) will be
  1453. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1454. :group 'org-todo
  1455. :group 'org-keywords
  1456. :type '(choice
  1457. (repeat :tag "Old syntax, just keywords"
  1458. (string :tag "Keyword"))
  1459. (repeat :tag "New syntax"
  1460. (cons
  1461. (choice
  1462. :tag "Interpretation"
  1463. (const :tag "Sequence (cycling hits every state)" sequence)
  1464. (const :tag "Type (cycling directly to DONE)" type))
  1465. (repeat
  1466. (string :tag "Keyword"))))))
  1467. (defvar org-todo-keywords-1 nil
  1468. "All TODO and DONE keywords active in a buffer.")
  1469. (make-variable-buffer-local 'org-todo-keywords-1)
  1470. (defvar org-todo-keywords-for-agenda nil)
  1471. (defvar org-done-keywords-for-agenda nil)
  1472. (defvar org-not-done-keywords nil)
  1473. (make-variable-buffer-local 'org-not-done-keywords)
  1474. (defvar org-done-keywords nil)
  1475. (make-variable-buffer-local 'org-done-keywords)
  1476. (defvar org-todo-heads nil)
  1477. (make-variable-buffer-local 'org-todo-heads)
  1478. (defvar org-todo-sets nil)
  1479. (make-variable-buffer-local 'org-todo-sets)
  1480. (defvar org-todo-log-states nil)
  1481. (make-variable-buffer-local 'org-todo-log-states)
  1482. (defvar org-todo-kwd-alist nil)
  1483. (make-variable-buffer-local 'org-todo-kwd-alist)
  1484. (defvar org-todo-key-alist nil)
  1485. (make-variable-buffer-local 'org-todo-key-alist)
  1486. (defvar org-todo-key-trigger nil)
  1487. (make-variable-buffer-local 'org-todo-key-trigger)
  1488. (defcustom org-todo-interpretation 'sequence
  1489. "Controls how TODO keywords are interpreted.
  1490. This variable is in principle obsolete and is only used for
  1491. backward compatibility, if the interpretation of todo keywords is
  1492. not given already in `org-todo-keywords'. See that variable for
  1493. more information."
  1494. :group 'org-todo
  1495. :group 'org-keywords
  1496. :type '(choice (const sequence)
  1497. (const type)))
  1498. (defcustom org-use-fast-todo-selection 'prefix
  1499. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1500. This variable describes if and under what circumstances the cycling
  1501. mechanism for TODO keywords will be replaced by a single-key, direct
  1502. selection scheme.
  1503. When nil, fast selection is never used.
  1504. When the symbol `prefix', it will be used when `org-todo' is called with
  1505. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1506. in an agenda buffer.
  1507. When t, fast selection is used by default. In this case, the prefix
  1508. argument forces cycling instead.
  1509. In all cases, the special interface is only used if access keys have actually
  1510. been assigned by the user, i.e. if keywords in the configuration are followed
  1511. by a letter in parenthesis, like TODO(t)."
  1512. :group 'org-todo
  1513. :type '(choice
  1514. (const :tag "Never" nil)
  1515. (const :tag "By default" t)
  1516. (const :tag "Only with C-u C-c C-t" prefix)))
  1517. (defcustom org-after-todo-state-change-hook nil
  1518. "Hook which is run after the state of a TODO item was changed.
  1519. The new state (a string with a TODO keyword, or nil) is available in the
  1520. Lisp variable `state'."
  1521. :group 'org-todo
  1522. :type 'hook)
  1523. (defcustom org-log-done nil
  1524. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1525. When equal to the list (done), also prompt for a closing note.
  1526. This can also be configured on a per-file basis by adding one of
  1527. the following lines anywhere in the buffer:
  1528. #+STARTUP: logdone
  1529. #+STARTUP: lognotedone
  1530. #+STARTUP: nologdone"
  1531. :group 'org-todo
  1532. :group 'org-progress
  1533. :type '(choice
  1534. (const :tag "No logging" nil)
  1535. (const :tag "Record CLOSED timestamp" time)
  1536. (const :tag "Record CLOSED timestamp with closing note." note)))
  1537. ;; Normalize old uses of org-log-done.
  1538. (cond
  1539. ((eq org-log-done t) (setq org-log-done 'time))
  1540. ((and (listp org-log-done) (memq 'done org-log-done))
  1541. (setq org-log-done 'note)))
  1542. ;; FIXME: document
  1543. (defcustom org-log-note-clock-out nil
  1544. "Non-nil means, recored a note when clocking out of an item.
  1545. This can also be configured on a per-file basis by adding one of
  1546. the following lines anywhere in the buffer:
  1547. #+STARTUP: lognoteclock-out
  1548. #+STARTUP: nolognoteclock-out"
  1549. :group 'org-todo
  1550. :group 'org-progress
  1551. :type 'boolean)
  1552. (defcustom org-log-done-with-time t
  1553. "Non-nil means, the CLOSED time stamp will contain date and time.
  1554. When nil, only the date will be recorded."
  1555. :group 'org-progress
  1556. :type 'boolean)
  1557. (defcustom org-log-note-headings
  1558. '((done . "CLOSING NOTE %t")
  1559. (state . "State %-12s %t")
  1560. (clock-out . ""))
  1561. "Headings for notes added when clocking out or closing TODO items.
  1562. The value is an alist, with the car being a symbol indicating the note
  1563. context, and the cdr is the heading to be used. The heading may also be the
  1564. empty string.
  1565. %t in the heading will be replaced by a time stamp.
  1566. %s will be replaced by the new TODO state, in double quotes.
  1567. %u will be replaced by the user name.
  1568. %U will be replaced by the full user name."
  1569. :group 'org-todo
  1570. :group 'org-progress
  1571. :type '(list :greedy t
  1572. (cons (const :tag "Heading when closing an item" done) string)
  1573. (cons (const :tag
  1574. "Heading when changing todo state (todo sequence only)"
  1575. state) string)
  1576. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1577. (defcustom org-log-states-order-reversed t
  1578. "Non-nil means, the latest state change note will be directly after heading.
  1579. When nil, the notes will be orderer according to time."
  1580. :group 'org-todo
  1581. :group 'org-progress
  1582. :type 'boolean)
  1583. (defcustom org-log-repeat 'time
  1584. "Non-nil means, record moving through the DONE state when triggering repeat.
  1585. An auto-repeating tasks is immediately switched back to TODO when marked
  1586. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1587. the TODO keyword definition, or recording a cloing note by setting
  1588. `org-log-done', there will be no record of the task moving trhough DONE.
  1589. This variable forces taking a note anyway. Possible values are:
  1590. nil Don't force a record
  1591. time Record a time stamp
  1592. note Record a note
  1593. This option can also be set with on a per-file-basis with
  1594. #+STARTUP: logrepeat
  1595. #+STARTUP: lognoterepeat
  1596. #+STARTUP: nologrepeat
  1597. You can have local logging settings for a subtree by setting the LOGGING
  1598. property to one or more of these keywords."
  1599. :group 'org-todo
  1600. :group 'org-progress
  1601. :type '(choice
  1602. (const :tag "Don't force a record" nil)
  1603. (const :tag "Force recording the DONE state" time)
  1604. (const :tag "Force recording a note with the DONE state" note)))
  1605. (defcustom org-clock-into-drawer 2
  1606. "Should clocking info be wrapped into a drawer?
  1607. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1608. If necessary, the drawer will be created.
  1609. When nil, the drawer will not be created, but used when present.
  1610. When an integer and the number of clocking entries in an item
  1611. reaches or exceeds this number, a drawer will be created."
  1612. :group 'org-todo
  1613. :group 'org-progress
  1614. :type '(choice
  1615. (const :tag "Always" t)
  1616. (const :tag "Only when drawer exists" nil)
  1617. (integer :tag "When at least N clock entries")))
  1618. (defcustom org-clock-out-when-done t
  1619. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1620. Nil means, clock will keep running until stopped explicitly with
  1621. `C-c C-x C-o', or until the clock is started in a different item."
  1622. :group 'org-progress
  1623. :type 'boolean)
  1624. (defcustom org-clock-in-switch-to-state nil
  1625. "Set task to a special todo state while clocking it.
  1626. The value should be the state to which the entry should be switched."
  1627. :group 'org-progress
  1628. :group 'org-todo
  1629. :type '(choice
  1630. (const :tag "Don't force a state" nil)
  1631. (string :tag "State")))
  1632. (defgroup org-priorities nil
  1633. "Priorities in Org-mode."
  1634. :tag "Org Priorities"
  1635. :group 'org-todo)
  1636. (defcustom org-highest-priority ?A
  1637. "The highest priority of TODO items. A character like ?A, ?B etc.
  1638. Must have a smaller ASCII number than `org-lowest-priority'."
  1639. :group 'org-priorities
  1640. :type 'character)
  1641. (defcustom org-lowest-priority ?C
  1642. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1643. Must have a larger ASCII number than `org-highest-priority'."
  1644. :group 'org-priorities
  1645. :type 'character)
  1646. (defcustom org-default-priority ?B
  1647. "The default priority of TODO items.
  1648. This is the priority an item get if no explicit priority is given."
  1649. :group 'org-priorities
  1650. :type 'character)
  1651. (defcustom org-priority-start-cycle-with-default t
  1652. "Non-nil means, start with default priority when starting to cycle.
  1653. When this is nil, the first step in the cycle will be (depending on the
  1654. command used) one higher or lower that the default priority."
  1655. :group 'org-priorities
  1656. :type 'boolean)
  1657. (defgroup org-time nil
  1658. "Options concerning time stamps and deadlines in Org-mode."
  1659. :tag "Org Time"
  1660. :group 'org)
  1661. (defcustom org-insert-labeled-timestamps-at-point nil
  1662. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1663. When nil, these labeled time stamps are forces into the second line of an
  1664. entry, just after the headline. When scheduling from the global TODO list,
  1665. the time stamp will always be forced into the second line."
  1666. :group 'org-time
  1667. :type 'boolean)
  1668. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1669. "Formats for `format-time-string' which are used for time stamps.
  1670. It is not recommended to change this constant.")
  1671. (defcustom org-time-stamp-rounding-minutes 0
  1672. "Number of minutes to round time stamps to upon insertion.
  1673. When zero, insert the time unmodified. Useful rounding numbers
  1674. should be factors of 60, so for example 5, 10, 15.
  1675. When this is not zero, you can still force an exact time-stamp by using
  1676. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1677. :group 'org-time
  1678. :type 'integer)
  1679. (defcustom org-display-custom-times nil
  1680. "Non-nil means, overlay custom formats over all time stamps.
  1681. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1682. To turn this on on a per-file basis, insert anywhere in the file:
  1683. #+STARTUP: customtime"
  1684. :group 'org-time
  1685. :set 'set-default
  1686. :type 'sexp)
  1687. (make-variable-buffer-local 'org-display-custom-times)
  1688. (defcustom org-time-stamp-custom-formats
  1689. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1690. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1691. These are overlayed over the default ISO format if the variable
  1692. `org-display-custom-times' is set. Time like %H:%M should be at the
  1693. end of the second format."
  1694. :group 'org-time
  1695. :type 'sexp)
  1696. (defun org-time-stamp-format (&optional long inactive)
  1697. "Get the right format for a time string."
  1698. (let ((f (if long (cdr org-time-stamp-formats)
  1699. (car org-time-stamp-formats))))
  1700. (if inactive
  1701. (concat "[" (substring f 1 -1) "]")
  1702. f)))
  1703. (defcustom org-read-date-prefer-future t
  1704. "Non-nil means, assume future for incomplete date input from user.
  1705. This affects the following situations:
  1706. 1. The user gives a day, but no month.
  1707. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1708. read this as the third of *next* month. However, if you enter \"17\",
  1709. it will be considered as *this* month.
  1710. 2. The user gives a month but not a year.
  1711. For example, if it is april and you enter \"feb 2\", this will be read
  1712. as feb 2, *next* year. \"May 5\", however, will be this year.
  1713. When this option is nil, the current month and year will always be used
  1714. as defaults."
  1715. :group 'org-time
  1716. :type 'boolean)
  1717. (defcustom org-read-date-display-live t
  1718. "Non-nil means, display current interpretation of date prompt live.
  1719. This display will be in an overlay, in the minibuffer."
  1720. :group 'org-time
  1721. :type 'boolean)
  1722. (defcustom org-read-date-popup-calendar t
  1723. "Non-nil means, pop up a calendar when prompting for a date.
  1724. In the calendar, the date can be selected with mouse-1. However, the
  1725. minibuffer will also be active, and you can simply enter the date as well.
  1726. When nil, only the minibuffer will be available."
  1727. :group 'org-time
  1728. :type 'boolean)
  1729. (if (fboundp 'defvaralias)
  1730. (defvaralias 'org-popup-calendar-for-date-prompt
  1731. 'org-read-date-popup-calendar))
  1732. (defcustom org-extend-today-until 0
  1733. "The hour when your day really ends.
  1734. This has influence for the following applications:
  1735. - When switching the agenda to \"today\". It it is still earlier than
  1736. the time given here, the day recognized as TODAY is actually yesterday.
  1737. - When a date is read from the user and it is still before the time given
  1738. here, the current date and time will be assumed to be yesterday, 23:59.
  1739. FIXME:
  1740. IMPORTANT: This is still a very experimental feature, it may disappear
  1741. again or it may be extended to mean more things."
  1742. :group 'org-time
  1743. :type 'number)
  1744. (defcustom org-edit-timestamp-down-means-later nil
  1745. "Non-nil means, S-down will increase the time in a time stamp.
  1746. When nil, S-up will increase."
  1747. :group 'org-time
  1748. :type 'boolean)
  1749. (defcustom org-calendar-follow-timestamp-change t
  1750. "Non-nil means, make the calendar window follow timestamp changes.
  1751. When a timestamp is modified and the calendar window is visible, it will be
  1752. moved to the new date."
  1753. :group 'org-time
  1754. :type 'boolean)
  1755. (defcustom org-clock-heading-function nil
  1756. "When non-nil, should be a function to create `org-clock-heading'.
  1757. This is the string shown in the mode line when a clock is running.
  1758. The function is called with point at the beginning of the headline."
  1759. :group 'org-time ; FIXME: Should we have a separate group????
  1760. :type 'function)
  1761. (defgroup org-tags nil
  1762. "Options concerning tags in Org-mode."
  1763. :tag "Org Tags"
  1764. :group 'org)
  1765. (defcustom org-tag-alist nil
  1766. "List of tags allowed in Org-mode files.
  1767. When this list is nil, Org-mode will base TAG input on what is already in the
  1768. buffer.
  1769. The value of this variable is an alist, the car of each entry must be a
  1770. keyword as a string, the cdr may be a character that is used to select
  1771. that tag through the fast-tag-selection interface.
  1772. See the manual for details."
  1773. :group 'org-tags
  1774. :type '(repeat
  1775. (choice
  1776. (cons (string :tag "Tag name")
  1777. (character :tag "Access char"))
  1778. (const :tag "Start radio group" (:startgroup))
  1779. (const :tag "End radio group" (:endgroup)))))
  1780. (defcustom org-use-fast-tag-selection 'auto
  1781. "Non-nil means, use fast tag selection scheme.
  1782. This is a special interface to select and deselect tags with single keys.
  1783. When nil, fast selection is never used.
  1784. When the symbol `auto', fast selection is used if and only if selection
  1785. characters for tags have been configured, either through the variable
  1786. `org-tag-alist' or through a #+TAGS line in the buffer.
  1787. When t, fast selection is always used and selection keys are assigned
  1788. automatically if necessary."
  1789. :group 'org-tags
  1790. :type '(choice
  1791. (const :tag "Always" t)
  1792. (const :tag "Never" nil)
  1793. (const :tag "When selection characters are configured" 'auto)))
  1794. (defcustom org-fast-tag-selection-single-key nil
  1795. "Non-nil means, fast tag selection exits after first change.
  1796. When nil, you have to press RET to exit it.
  1797. During fast tag selection, you can toggle this flag with `C-c'.
  1798. This variable can also have the value `expert'. In this case, the window
  1799. displaying the tags menu is not even shown, until you press C-c again."
  1800. :group 'org-tags
  1801. :type '(choice
  1802. (const :tag "No" nil)
  1803. (const :tag "Yes" t)
  1804. (const :tag "Expert" expert)))
  1805. (defvar org-fast-tag-selection-include-todo nil
  1806. "Non-nil means, fast tags selection interface will also offer TODO states.
  1807. This is an undocumented feature, you should not rely on it.")
  1808. (defcustom org-tags-column -80
  1809. "The column to which tags should be indented in a headline.
  1810. If this number is positive, it specifies the column. If it is negative,
  1811. it means that the tags should be flushright to that column. For example,
  1812. -80 works well for a normal 80 character screen."
  1813. :group 'org-tags
  1814. :type 'integer)
  1815. (defcustom org-auto-align-tags t
  1816. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1817. These operations change the length of a headline and therefore shift
  1818. the tags around. With this options turned on, after each such operation
  1819. the tags are again aligned to `org-tags-column'."
  1820. :group 'org-tags
  1821. :type 'boolean)
  1822. (defcustom org-use-tag-inheritance t
  1823. "Non-nil means, tags in levels apply also for sublevels.
  1824. When nil, only the tags directly given in a specific line apply there.
  1825. If you turn off this option, you very likely want to turn on the
  1826. companion option `org-tags-match-list-sublevels'."
  1827. :group 'org-tags
  1828. :type 'boolean)
  1829. (defcustom org-tags-match-list-sublevels nil
  1830. "Non-nil means list also sublevels of headlines matching tag search.
  1831. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1832. the sublevels of a headline matching a tag search often also match
  1833. the same search. Listing all of them can create very long lists.
  1834. Setting this variable to nil causes subtrees of a match to be skipped.
  1835. This option is off by default, because inheritance in on. If you turn
  1836. inheritance off, you very likely want to turn this option on.
  1837. As a special case, if the tag search is restricted to TODO items, the
  1838. value of this variable is ignored and sublevels are always checked, to
  1839. make sure all corresponding TODO items find their way into the list."
  1840. :group 'org-tags
  1841. :type 'boolean)
  1842. (defvar org-tags-history nil
  1843. "History of minibuffer reads for tags.")
  1844. (defvar org-last-tags-completion-table nil
  1845. "The last used completion table for tags.")
  1846. (defvar org-after-tags-change-hook nil
  1847. "Hook that is run after the tags in a line have changed.")
  1848. (defgroup org-properties nil
  1849. "Options concerning properties in Org-mode."
  1850. :tag "Org Properties"
  1851. :group 'org)
  1852. (defcustom org-property-format "%-10s %s"
  1853. "How property key/value pairs should be formatted by `indent-line'.
  1854. When `indent-line' hits a property definition, it will format the line
  1855. according to this format, mainly to make sure that the values are
  1856. lined-up with respect to each other."
  1857. :group 'org-properties
  1858. :type 'string)
  1859. (defcustom org-use-property-inheritance nil
  1860. "Non-nil means, properties apply also for sublevels.
  1861. This setting is only relevant during property searches, not when querying
  1862. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1863. you need to call `org-entry-get' with the inheritance flag.
  1864. Turning this on can cause significant overhead when doing a search, so
  1865. this is turned off by default.
  1866. When nil, only the properties directly given in the current entry count.
  1867. The value may also be a list of properties that shouldhave inheritance.
  1868. However, note that some special properties use inheritance under special
  1869. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1870. and the properties ending in \"_ALL\" when they are used as descriptor
  1871. for valid values of a property."
  1872. :group 'org-properties
  1873. :type '(choice
  1874. (const :tag "Not" nil)
  1875. (const :tag "Always" nil)
  1876. (repeat :tag "Specific properties" (string :tag "Property"))))
  1877. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1878. "The default column format, if no other format has been defined.
  1879. This variable can be set on the per-file basis by inserting a line
  1880. #+COLUMNS: %25ITEM ....."
  1881. :group 'org-properties
  1882. :type 'string)
  1883. (defcustom org-global-properties nil
  1884. "List of property/value pairs that can be inherited by any entry.
  1885. You can set buffer-local values for this by adding lines like
  1886. #+PROPERTY: NAME VALUE"
  1887. :group 'org-properties
  1888. :type '(repeat
  1889. (cons (string :tag "Property")
  1890. (string :tag "Value"))))
  1891. (defvar org-local-properties nil
  1892. "List of property/value pairs that can be inherited by any entry.
  1893. Valid for the current buffer.
  1894. This variable is populated from #+PROPERTY lines.")
  1895. (defgroup org-agenda nil
  1896. "Options concerning agenda views in Org-mode."
  1897. :tag "Org Agenda"
  1898. :group 'org)
  1899. (defvar org-category nil
  1900. "Variable used by org files to set a category for agenda display.
  1901. Such files should use a file variable to set it, for example
  1902. # -*- mode: org; org-category: \"ELisp\"
  1903. or contain a special line
  1904. #+CATEGORY: ELisp
  1905. If the file does not specify a category, then file's base name
  1906. is used instead.")
  1907. (make-variable-buffer-local 'org-category)
  1908. (defcustom org-agenda-files nil
  1909. "The files to be used for agenda display.
  1910. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1911. \\[org-remove-file]. You can also use customize to edit the list.
  1912. If an entry is a directory, all files in that directory that are matched by
  1913. `org-agenda-file-regexp' will be part of the file list.
  1914. If the value of the variable is not a list but a single file name, then
  1915. the list of agenda files is actually stored and maintained in that file, one
  1916. agenda file per line."
  1917. :group 'org-agenda
  1918. :type '(choice
  1919. (repeat :tag "List of files and directories" file)
  1920. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1921. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1922. "Regular expression to match files for `org-agenda-files'.
  1923. If any element in the list in that variable contains a directory instead
  1924. of a normal file, all files in that directory that are matched by this
  1925. regular expression will be included."
  1926. :group 'org-agenda
  1927. :type 'regexp)
  1928. (defcustom org-agenda-skip-unavailable-files nil
  1929. "t means to just skip non-reachable files in `org-agenda-files'.
  1930. Nil means to remove them, after a query, from the list."
  1931. :group 'org-agenda
  1932. :type 'boolean)
  1933. (defcustom org-agenda-multi-occur-extra-files nil
  1934. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1935. The files in `org-agenda-files' are always searched."
  1936. :group 'org-agenda
  1937. :type '(repeat file))
  1938. (defcustom org-agenda-confirm-kill 1
  1939. "When set, remote killing from the agenda buffer needs confirmation.
  1940. When t, a confirmation is always needed. When a number N, confirmation is
  1941. only needed when the text to be killed contains more than N non-white lines."
  1942. :group 'org-agenda
  1943. :type '(choice
  1944. (const :tag "Never" nil)
  1945. (const :tag "Always" t)
  1946. (number :tag "When more than N lines")))
  1947. (defcustom org-calendar-to-agenda-key [?c]
  1948. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1949. The command `org-calendar-goto-agenda' will be bound to this key. The
  1950. default is the character `c' because then `c' can be used to switch back and
  1951. forth between agenda and calendar."
  1952. :group 'org-agenda
  1953. :type 'sexp)
  1954. (defcustom org-agenda-compact-blocks nil
  1955. "Non-nil means, make the block agenda more compact.
  1956. This is done by leaving out unnecessary lines."
  1957. :group 'org-agenda
  1958. :type nil)
  1959. (defgroup org-agenda-export nil
  1960. "Options concerning exporting agenda views in Org-mode."
  1961. :tag "Org Agenda Export"
  1962. :group 'org-agenda)
  1963. (defcustom org-agenda-with-colors t
  1964. "Non-nil means, use colors in agenda views."
  1965. :group 'org-agenda-export
  1966. :type 'boolean)
  1967. (defcustom org-agenda-exporter-settings nil
  1968. "Alist of variable/value pairs that should be active during agenda export.
  1969. This is a good place to set uptions for ps-print and for htmlize."
  1970. :group 'org-agenda-export
  1971. :type '(repeat
  1972. (list
  1973. (variable)
  1974. (sexp :tag "Value"))))
  1975. (defcustom org-agenda-export-html-style ""
  1976. "The style specification for exported HTML Agenda files.
  1977. If this variable contains a string, it will replace the default <style>
  1978. section as produced by `htmlize'.
  1979. Since there are different ways of setting style information, this variable
  1980. needs to contain the full HTML structure to provide a style, including the
  1981. surrounding HTML tags. The style specifications should include definitions
  1982. the fonts used by the agenda, here is an example:
  1983. <style type=\"text/css\">
  1984. p { font-weight: normal; color: gray; }
  1985. .org-agenda-structure {
  1986. font-size: 110%;
  1987. color: #003399;
  1988. font-weight: 600;
  1989. }
  1990. .org-todo {
  1991. color: #cc6666;Week-agenda:
  1992. font-weight: bold;
  1993. }
  1994. .org-done {
  1995. color: #339933;
  1996. }
  1997. .title { text-align: center; }
  1998. .todo, .deadline { color: red; }
  1999. .done { color: green; }
  2000. </style>
  2001. or, if you want to keep the style in a file,
  2002. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2003. As the value of this option simply gets inserted into the HTML <head> header,
  2004. you can \"misuse\" it to also add other text to the header. However,
  2005. <style>...</style> is required, if not present the variable will be ignored."
  2006. :group 'org-agenda-export
  2007. :group 'org-export-html
  2008. :type 'string)
  2009. (defgroup org-agenda-custom-commands nil
  2010. "Options concerning agenda views in Org-mode."
  2011. :tag "Org Agenda Custom Commands"
  2012. :group 'org-agenda)
  2013. (defcustom org-agenda-custom-commands nil
  2014. "Custom commands for the agenda.
  2015. These commands will be offered on the splash screen displayed by the
  2016. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2017. (key desc type match options files)
  2018. key The key (one or more characters as a string) to be associated
  2019. with the command.
  2020. desc A description of the commend, when omitted or nil, a default
  2021. description is built using MATCH.
  2022. type The command type, any of the following symbols:
  2023. todo Entries with a specific TODO keyword, in all agenda files.
  2024. tags Tags match in all agenda files.
  2025. tags-todo Tags match in all agenda files, TODO entries only.
  2026. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2027. tags-tree Sparse tree with all tags matches in *current* file.
  2028. occur-tree Occur sparse tree for *current* file.
  2029. ... A user-defined function.
  2030. match What to search for:
  2031. - a single keyword for TODO keyword searches
  2032. - a tags match expression for tags searches
  2033. - a regular expression for occur searches
  2034. options A list of option settings, similar to that in a let form, so like
  2035. this: ((opt1 val1) (opt2 val2) ...)
  2036. files A list of files file to write the produced agenda buffer to
  2037. with the command `org-store-agenda-views'.
  2038. If a file name ends in \".html\", an HTML version of the buffer
  2039. is written out. If it ends in \".ps\", a postscript version is
  2040. produced. Otherwide, only the plain text is written to the file.
  2041. You can also define a set of commands, to create a composite agenda buffer.
  2042. In this case, an entry looks like this:
  2043. (key desc (cmd1 cmd2 ...) general-options file)
  2044. where
  2045. desc A description string to be displayed in the dispatcher menu.
  2046. cmd An agenda command, similar to the above. However, tree commands
  2047. are no allowed, but instead you can get agenda and global todo list.
  2048. So valid commands for a set are:
  2049. (agenda)
  2050. (alltodo)
  2051. (stuck)
  2052. (todo \"match\" options files)
  2053. (tags \"match\" options files)
  2054. (tags-todo \"match\" options files)
  2055. Each command can carry a list of options, and another set of options can be
  2056. given for the whole set of commands. Individual command options take
  2057. precedence over the general options.
  2058. When using several characters as key to a command, the first characters
  2059. are prefix commands. For the dispatcher to display useful information, you
  2060. should provide a description for the prefix, like
  2061. (setq org-agenda-custom-commands
  2062. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2063. (\"hl\" tags \"+HOME+Lisa\")
  2064. (\"hp\" tags \"+HOME+Peter\")
  2065. (\"hk\" tags \"+HOME+Kim\")))"
  2066. :group 'org-agenda-custom-commands
  2067. :type '(repeat
  2068. (choice :value ("a" "" tags "" nil)
  2069. (list :tag "Single command"
  2070. (string :tag "Access Key(s) ")
  2071. (option (string :tag "Description"))
  2072. (choice
  2073. (const :tag "Agenda" agenda)
  2074. (const :tag "TODO list" alltodo)
  2075. (const :tag "Stuck projects" stuck)
  2076. (const :tag "Tags search (all agenda files)" tags)
  2077. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2078. (const :tag "TODO keyword search (all agenda files)" todo)
  2079. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2080. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2081. (const :tag "Occur tree (current buffer)" occur-tree)
  2082. (sexp :tag "Other, user-defined function"))
  2083. (string :tag "Match")
  2084. (repeat :tag "Local options"
  2085. (list (variable :tag "Option") (sexp :tag "Value")))
  2086. (option (repeat :tag "Export" (file :tag "Export to"))))
  2087. (list :tag "Command series, all agenda files"
  2088. (string :tag "Access Key(s)")
  2089. (string :tag "Description ")
  2090. (repeat
  2091. (choice
  2092. (const :tag "Agenda" (agenda))
  2093. (const :tag "TODO list" (alltodo))
  2094. (const :tag "Stuck projects" (stuck))
  2095. (list :tag "Tags search"
  2096. (const :format "" tags)
  2097. (string :tag "Match")
  2098. (repeat :tag "Local options"
  2099. (list (variable :tag "Option")
  2100. (sexp :tag "Value"))))
  2101. (list :tag "Tags search, TODO entries only"
  2102. (const :format "" tags-todo)
  2103. (string :tag "Match")
  2104. (repeat :tag "Local options"
  2105. (list (variable :tag "Option")
  2106. (sexp :tag "Value"))))
  2107. (list :tag "TODO keyword search"
  2108. (const :format "" todo)
  2109. (string :tag "Match")
  2110. (repeat :tag "Local options"
  2111. (list (variable :tag "Option")
  2112. (sexp :tag "Value"))))
  2113. (list :tag "Other, user-defined function"
  2114. (symbol :tag "function")
  2115. (string :tag "Match")
  2116. (repeat :tag "Local options"
  2117. (list (variable :tag "Option")
  2118. (sexp :tag "Value"))))))
  2119. (repeat :tag "General options"
  2120. (list (variable :tag "Option")
  2121. (sexp :tag "Value")))
  2122. (option (repeat :tag "Export" (file :tag "Export to"))))
  2123. (cons :tag "Prefix key documentation"
  2124. (string :tag "Access Key(s)")
  2125. (string :tag "Description ")))))
  2126. (defcustom org-stuck-projects
  2127. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2128. "How to identify stuck projects.
  2129. This is a list of four items:
  2130. 1. A tags/todo matcher string that is used to identify a project.
  2131. The entire tree below a headline matched by this is considered one project.
  2132. 2. A list of TODO keywords identifying non-stuck projects.
  2133. If the project subtree contains any headline with one of these todo
  2134. keywords, the project is considered to be not stuck. If you specify
  2135. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2136. 3. A list of tags identifying non-stuck projects.
  2137. If the project subtree contains any headline with one of these tags,
  2138. the project is considered to be not stuck. If you specify \"*\" as
  2139. a tag, any tag will mark the project unstuck.
  2140. 4. An arbitrary regular expression matching non-stuck projects.
  2141. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2142. or `C-c a #' to produce the list."
  2143. :group 'org-agenda-custom-commands
  2144. :type '(list
  2145. (string :tag "Tags/TODO match to identify a project")
  2146. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2147. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2148. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2149. (defgroup org-agenda-skip nil
  2150. "Options concerning skipping parts of agenda files."
  2151. :tag "Org Agenda Skip"
  2152. :group 'org-agenda)
  2153. (defcustom org-agenda-todo-list-sublevels t
  2154. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2155. When nil, the sublevels of a TODO entry are not checked, resulting in
  2156. potentially much shorter TODO lists."
  2157. :group 'org-agenda-skip
  2158. :group 'org-todo
  2159. :type 'boolean)
  2160. (defcustom org-agenda-todo-ignore-with-date nil
  2161. "Non-nil means, don't show entries with a date in the global todo list.
  2162. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2163. but don't want them to show up in the TODO list.
  2164. When this is set, it also covers deadlines and scheduled items, the settings
  2165. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2166. will be ignored."
  2167. :group 'org-agenda-skip
  2168. :group 'org-todo
  2169. :type 'boolean)
  2170. (defcustom org-agenda-todo-ignore-scheduled nil
  2171. "Non-nil means, don't show scheduled entries in the global todo list.
  2172. The idea behind this is that by scheduling it, you have already taken care
  2173. of this item.
  2174. See also `org-agenda-todo-ignore-with-date'."
  2175. :group 'org-agenda-skip
  2176. :group 'org-todo
  2177. :type 'boolean)
  2178. (defcustom org-agenda-todo-ignore-deadlines nil
  2179. "Non-nil means, don't show near deadline entries in the global todo list.
  2180. Near means closer than `org-deadline-warning-days' days.
  2181. The idea behind this is that such items will appear in the agenda anyway.
  2182. See also `org-agenda-todo-ignore-with-date'."
  2183. :group 'org-agenda-skip
  2184. :group 'org-todo
  2185. :type 'boolean)
  2186. (defcustom org-agenda-skip-scheduled-if-done nil
  2187. "Non-nil means don't show scheduled items in agenda when they are done.
  2188. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2189. it applies only to the actual date of the scheduling. Warnings about
  2190. an item with a past scheduling dates are always turned off when the item
  2191. is DONE."
  2192. :group 'org-agenda-skip
  2193. :type 'boolean)
  2194. (defcustom org-agenda-skip-deadline-if-done nil
  2195. "Non-nil means don't show deadines when the corresponding item is done.
  2196. When nil, the deadline is still shown and should give you a happy feeling.
  2197. This is relevant for the daily/weekly agenda. And it applied only to the
  2198. actualy date of the deadline. Warnings about approching and past-due
  2199. deadlines are always turned off when the item is DONE."
  2200. :group 'org-agenda-skip
  2201. :type 'boolean)
  2202. (defcustom org-agenda-skip-timestamp-if-done nil
  2203. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2204. :group 'org-agenda-skip
  2205. :type 'boolean)
  2206. (defcustom org-timeline-show-empty-dates 3
  2207. "Non-nil means, `org-timeline' also shows dates without an entry.
  2208. When nil, only the days which actually have entries are shown.
  2209. When t, all days between the first and the last date are shown.
  2210. When an integer, show also empty dates, but if there is a gap of more than
  2211. N days, just insert a special line indicating the size of the gap."
  2212. :group 'org-agenda-skip
  2213. :type '(choice
  2214. (const :tag "None" nil)
  2215. (const :tag "All" t)
  2216. (number :tag "at most")))
  2217. (defgroup org-agenda-startup nil
  2218. "Options concerning initial settings in the Agenda in Org Mode."
  2219. :tag "Org Agenda Startup"
  2220. :group 'org-agenda)
  2221. (defcustom org-finalize-agenda-hook nil
  2222. "Hook run just before displaying an agenda buffer."
  2223. :group 'org-agenda-startup
  2224. :type 'hook)
  2225. (defcustom org-agenda-mouse-1-follows-link nil
  2226. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2227. A longer mouse click will still set point. Does not wortk on XEmacs.
  2228. Needs to be set before org.el is loaded."
  2229. :group 'org-agenda-startup
  2230. :type 'boolean)
  2231. (defcustom org-agenda-start-with-follow-mode nil
  2232. "The initial value of follow-mode in a newly created agenda window."
  2233. :group 'org-agenda-startup
  2234. :type 'boolean)
  2235. (defgroup org-agenda-windows nil
  2236. "Options concerning the windows used by the Agenda in Org Mode."
  2237. :tag "Org Agenda Windows"
  2238. :group 'org-agenda)
  2239. (defcustom org-agenda-window-setup 'reorganize-frame
  2240. "How the agenda buffer should be displayed.
  2241. Possible values for this option are:
  2242. current-window Show agenda in the current window, keeping all other windows.
  2243. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2244. other-window Use `switch-to-buffer-other-window' to display agenda.
  2245. reorganize-frame Show only two windows on the current frame, the current
  2246. window and the agenda.
  2247. See also the variable `org-agenda-restore-windows-after-quit'."
  2248. :group 'org-agenda-windows
  2249. :type '(choice
  2250. (const current-window)
  2251. (const other-frame)
  2252. (const other-window)
  2253. (const reorganize-frame)))
  2254. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2255. "The min and max height of the agenda window as a fraction of frame height.
  2256. The value of the variable is a cons cell with two numbers between 0 and 1.
  2257. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2258. :group 'org-agenda-windows
  2259. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2260. (defcustom org-agenda-restore-windows-after-quit nil
  2261. "Non-nil means, restore window configuration open exiting agenda.
  2262. Before the window configuration is changed for displaying the agenda,
  2263. the current status is recorded. When the agenda is exited with
  2264. `q' or `x' and this option is set, the old state is restored. If
  2265. `org-agenda-window-setup' is `other-frame', the value of this
  2266. option will be ignored.."
  2267. :group 'org-agenda-windows
  2268. :type 'boolean)
  2269. (defcustom org-indirect-buffer-display 'other-window
  2270. "How should indirect tree buffers be displayed?
  2271. This applies to indirect buffers created with the commands
  2272. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2273. Valid values are:
  2274. current-window Display in the current window
  2275. other-window Just display in another window.
  2276. dedicated-frame Create one new frame, and re-use it each time.
  2277. new-frame Make a new frame each time. Note that in this case
  2278. previously-made indirect buffers are kept, and you need to
  2279. kill these buffers yourself."
  2280. :group 'org-structure
  2281. :group 'org-agenda-windows
  2282. :type '(choice
  2283. (const :tag "In current window" current-window)
  2284. (const :tag "In current frame, other window" other-window)
  2285. (const :tag "Each time a new frame" new-frame)
  2286. (const :tag "One dedicated frame" dedicated-frame)))
  2287. (defgroup org-agenda-daily/weekly nil
  2288. "Options concerning the daily/weekly agenda."
  2289. :tag "Org Agenda Daily/Weekly"
  2290. :group 'org-agenda)
  2291. (defcustom org-agenda-ndays 7
  2292. "Number of days to include in overview display.
  2293. Should be 1 or 7."
  2294. :group 'org-agenda-daily/weekly
  2295. :type 'number)
  2296. (defcustom org-agenda-start-on-weekday 1
  2297. "Non-nil means, start the overview always on the specified weekday.
  2298. 0 denotes Sunday, 1 denotes Monday etc.
  2299. When nil, always start on the current day."
  2300. :group 'org-agenda-daily/weekly
  2301. :type '(choice (const :tag "Today" nil)
  2302. (number :tag "Weekday No.")))
  2303. (defcustom org-agenda-show-all-dates t
  2304. "Non-nil means, `org-agenda' shows every day in the selected range.
  2305. When nil, only the days which actually have entries are shown."
  2306. :group 'org-agenda-daily/weekly
  2307. :type 'boolean)
  2308. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2309. "Format string for displaying dates in the agenda.
  2310. Used by the daily/weekly agenda and by the timeline. This should be
  2311. a format string understood by `format-time-string', or a function returning
  2312. the formatted date as a string. The function must take a single argument,
  2313. a calendar-style date list like (month day year)."
  2314. :group 'org-agenda-daily/weekly
  2315. :type '(choice
  2316. (string :tag "Format string")
  2317. (function :tag "Function")))
  2318. (defun org-agenda-format-date-aligned (date)
  2319. "Format a date string for display in the daily/weekly agenda, or timeline.
  2320. This function makes sure that dates are aligned for easy reading."
  2321. (format "%-9s %2d %s %4d"
  2322. (calendar-day-name date)
  2323. (extract-calendar-day date)
  2324. (calendar-month-name (extract-calendar-month date))
  2325. (extract-calendar-year date)))
  2326. (defcustom org-agenda-include-diary nil
  2327. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2328. :group 'org-agenda-daily/weekly
  2329. :type 'boolean)
  2330. (defcustom org-agenda-include-all-todo nil
  2331. "Set means weekly/daily agenda will always contain all TODO entries.
  2332. The TODO entries will be listed at the top of the agenda, before
  2333. the entries for specific days."
  2334. :group 'org-agenda-daily/weekly
  2335. :type 'boolean)
  2336. (defcustom org-agenda-repeating-timestamp-show-all t
  2337. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2338. When nil, only one occurence is shown, either today or the
  2339. nearest into the future."
  2340. :group 'org-agenda-daily/weekly
  2341. :type 'boolean)
  2342. (defcustom org-deadline-warning-days 14
  2343. "No. of days before expiration during which a deadline becomes active.
  2344. This variable governs the display in sparse trees and in the agenda.
  2345. When negative, it means use this number (the absolute value of it)
  2346. even if a deadline has a different individual lead time specified."
  2347. :group 'org-time
  2348. :group 'org-agenda-daily/weekly
  2349. :type 'number)
  2350. (defcustom org-scheduled-past-days 10000
  2351. "No. of days to continue listing scheduled items that are not marked DONE.
  2352. When an item is scheduled on a date, it shows up in the agenda on this
  2353. day and will be listed until it is marked done for the number of days
  2354. given here."
  2355. :group 'org-agenda-daily/weekly
  2356. :type 'number)
  2357. (defgroup org-agenda-time-grid nil
  2358. "Options concerning the time grid in the Org-mode Agenda."
  2359. :tag "Org Agenda Time Grid"
  2360. :group 'org-agenda)
  2361. (defcustom org-agenda-use-time-grid t
  2362. "Non-nil means, show a time grid in the agenda schedule.
  2363. A time grid is a set of lines for specific times (like every two hours between
  2364. 8:00 and 20:00). The items scheduled for a day at specific times are
  2365. sorted in between these lines.
  2366. For details about when the grid will be shown, and what it will look like, see
  2367. the variable `org-agenda-time-grid'."
  2368. :group 'org-agenda-time-grid
  2369. :type 'boolean)
  2370. (defcustom org-agenda-time-grid
  2371. '((daily today require-timed)
  2372. "----------------"
  2373. (800 1000 1200 1400 1600 1800 2000))
  2374. "The settings for time grid for agenda display.
  2375. This is a list of three items. The first item is again a list. It contains
  2376. symbols specifying conditions when the grid should be displayed:
  2377. daily if the agenda shows a single day
  2378. weekly if the agenda shows an entire week
  2379. today show grid on current date, independent of daily/weekly display
  2380. require-timed show grid only if at least one item has a time specification
  2381. The second item is a string which will be places behing the grid time.
  2382. The third item is a list of integers, indicating the times that should have
  2383. a grid line."
  2384. :group 'org-agenda-time-grid
  2385. :type
  2386. '(list
  2387. (set :greedy t :tag "Grid Display Options"
  2388. (const :tag "Show grid in single day agenda display" daily)
  2389. (const :tag "Show grid in weekly agenda display" weekly)
  2390. (const :tag "Always show grid for today" today)
  2391. (const :tag "Show grid only if any timed entries are present"
  2392. require-timed)
  2393. (const :tag "Skip grid times already present in an entry"
  2394. remove-match))
  2395. (string :tag "Grid String")
  2396. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2397. (defgroup org-agenda-sorting nil
  2398. "Options concerning sorting in the Org-mode Agenda."
  2399. :tag "Org Agenda Sorting"
  2400. :group 'org-agenda)
  2401. (defconst org-sorting-choice
  2402. '(choice
  2403. (const time-up) (const time-down)
  2404. (const category-keep) (const category-up) (const category-down)
  2405. (const tag-down) (const tag-up)
  2406. (const priority-up) (const priority-down))
  2407. "Sorting choices.")
  2408. (defcustom org-agenda-sorting-strategy
  2409. '((agenda time-up category-keep priority-down)
  2410. (todo category-keep priority-down)
  2411. (tags category-keep priority-down))
  2412. "Sorting structure for the agenda items of a single day.
  2413. This is a list of symbols which will be used in sequence to determine
  2414. if an entry should be listed before another entry. The following
  2415. symbols are recognized:
  2416. time-up Put entries with time-of-day indications first, early first
  2417. time-down Put entries with time-of-day indications first, late first
  2418. category-keep Keep the default order of categories, corresponding to the
  2419. sequence in `org-agenda-files'.
  2420. category-up Sort alphabetically by category, A-Z.
  2421. category-down Sort alphabetically by category, Z-A.
  2422. tag-up Sort alphabetically by last tag, A-Z.
  2423. tag-down Sort alphabetically by last tag, Z-A.
  2424. priority-up Sort numerically by priority, high priority last.
  2425. priority-down Sort numerically by priority, high priority first.
  2426. The different possibilities will be tried in sequence, and testing stops
  2427. if one comparison returns a \"not-equal\". For example, the default
  2428. '(time-up category-keep priority-down)
  2429. means: Pull out all entries having a specified time of day and sort them,
  2430. in order to make a time schedule for the current day the first thing in the
  2431. agenda listing for the day. Of the entries without a time indication, keep
  2432. the grouped in categories, don't sort the categories, but keep them in
  2433. the sequence given in `org-agenda-files'. Within each category sort by
  2434. priority.
  2435. Leaving out `category-keep' would mean that items will be sorted across
  2436. categories by priority.
  2437. Instead of a single list, this can also be a set of list for specific
  2438. contents, with a context symbol in the car of the list, any of
  2439. `agenda', `todo', `tags' for the corresponding agenda views."
  2440. :group 'org-agenda-sorting
  2441. :type `(choice
  2442. (repeat :tag "General" ,org-sorting-choice)
  2443. (list :tag "Individually"
  2444. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2445. (repeat ,org-sorting-choice))
  2446. (cons (const :tag "Strategy for TODO lists" todo)
  2447. (repeat ,org-sorting-choice))
  2448. (cons (const :tag "Strategy for Tags matches" tags)
  2449. (repeat ,org-sorting-choice)))))
  2450. (defcustom org-sort-agenda-notime-is-late t
  2451. "Non-nil means, items without time are considered late.
  2452. This is only relevant for sorting. When t, items which have no explicit
  2453. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2454. do have a time. When nil, the default time is before 0:00. You can use this
  2455. option to decide if the schedule for today should come before or after timeless
  2456. agenda entries."
  2457. :group 'org-agenda-sorting
  2458. :type 'boolean)
  2459. (defgroup org-agenda-line-format nil
  2460. "Options concerning the entry prefix in the Org-mode agenda display."
  2461. :tag "Org Agenda Line Format"
  2462. :group 'org-agenda)
  2463. (defcustom org-agenda-prefix-format
  2464. '((agenda . " %-12:c%?-12t% s")
  2465. (timeline . " % s")
  2466. (todo . " %-12:c")
  2467. (tags . " %-12:c"))
  2468. "Format specifications for the prefix of items in the agenda views.
  2469. An alist with four entries, for the different agenda types. The keys to the
  2470. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2471. are format strings.
  2472. This format works similar to a printf format, with the following meaning:
  2473. %c the category of the item, \"Diary\" for entries from the diary, or
  2474. as given by the CATEGORY keyword or derived from the file name.
  2475. %T the *last* tag of the item. Last because inherited tags come
  2476. first in the list.
  2477. %t the time-of-day specification if one applies to the entry, in the
  2478. format HH:MM
  2479. %s Scheduling/Deadline information, a short string
  2480. All specifiers work basically like the standard `%s' of printf, but may
  2481. contain two additional characters: A question mark just after the `%' and
  2482. a whitespace/punctuation character just before the final letter.
  2483. If the first character after `%' is a question mark, the entire field
  2484. will only be included if the corresponding value applies to the
  2485. current entry. This is useful for fields which should have fixed
  2486. width when present, but zero width when absent. For example,
  2487. \"%?-12t\" will result in a 12 character time field if a time of the
  2488. day is specified, but will completely disappear in entries which do
  2489. not contain a time.
  2490. If there is punctuation or whitespace character just before the final
  2491. format letter, this character will be appended to the field value if
  2492. the value is not empty. For example, the format \"%-12:c\" leads to
  2493. \"Diary: \" if the category is \"Diary\". If the category were be
  2494. empty, no additional colon would be interted.
  2495. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2496. - Indent the line with two space characters
  2497. - Give the category in a 12 chars wide field, padded with whitespace on
  2498. the right (because of `-'). Append a colon if there is a category
  2499. (because of `:').
  2500. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2501. time, don't put in an empty field, just skip it (because of '?').
  2502. - Finally, put the scheduling information and append a whitespace.
  2503. As another example, if you don't want the time-of-day of entries in
  2504. the prefix, you could use:
  2505. (setq org-agenda-prefix-format \" %-11:c% s\")
  2506. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2507. `org-agenda-remove-tags'."
  2508. :type '(choice
  2509. (string :tag "General format")
  2510. (list :greedy t :tag "View dependent"
  2511. (cons (const agenda) (string :tag "Format"))
  2512. (cons (const timeline) (string :tag "Format"))
  2513. (cons (const todo) (string :tag "Format"))
  2514. (cons (const tags) (string :tag "Format"))))
  2515. :group 'org-agenda-line-format)
  2516. (defvar org-prefix-format-compiled nil
  2517. "The compiled version of the most recently used prefix format.
  2518. See the variable `org-agenda-prefix-format'.")
  2519. (defcustom org-agenda-todo-keyword-format "%-1s"
  2520. "Format for the TODO keyword in agenda lines.
  2521. Set this to something like \"%-12s\" if you want all TODO keywords
  2522. to occupy a fixed space in the agenda display."
  2523. :group 'org-agenda-line-format
  2524. :type 'string)
  2525. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2526. "Text preceeding scheduled items in the agenda view.
  2527. This is a list with two strings. The first applies when the item is
  2528. scheduled on the current day. The second applies when it has been scheduled
  2529. previously, it may contain a %d to capture how many days ago the item was
  2530. scheduled."
  2531. :group 'org-agenda-line-format
  2532. :type '(list
  2533. (string :tag "Scheduled today ")
  2534. (string :tag "Scheduled previously")))
  2535. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2536. "Text preceeding deadline items in the agenda view.
  2537. This is a list with two strings. The first applies when the item has its
  2538. deadline on the current day. The second applies when it is in the past or
  2539. in the future, it may contain %d to capture how many days away the deadline
  2540. is (was)."
  2541. :group 'org-agenda-line-format
  2542. :type '(list
  2543. (string :tag "Deadline today ")
  2544. (string :tag "Deadline relative")))
  2545. (defcustom org-agenda-remove-times-when-in-prefix t
  2546. "Non-nil means, remove duplicate time specifications in agenda items.
  2547. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2548. time-of-day specification in a headline or diary entry is extracted and
  2549. placed into the prefix. If this option is non-nil, the original specification
  2550. \(a timestamp or -range, or just a plain time(range) specification like
  2551. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2552. cluttered.
  2553. The option can be t or nil. It may also be the symbol `beg', indicating
  2554. that the time should only be removed what it is located at the beginning of
  2555. the headline/diary entry."
  2556. :group 'org-agenda-line-format
  2557. :type '(choice
  2558. (const :tag "Always" t)
  2559. (const :tag "Never" nil)
  2560. (const :tag "When at beginning of entry" beg)))
  2561. (defcustom org-agenda-default-appointment-duration nil
  2562. "Default duration for appointments that only have a starting time.
  2563. When nil, no duration is specified in such cases.
  2564. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2565. :group 'org-agenda-line-format
  2566. :type '(choice
  2567. (integer :tag "Minutes")
  2568. (const :tag "No default duration")))
  2569. (defcustom org-agenda-remove-tags nil
  2570. "Non-nil means, remove the tags from the headline copy in the agenda.
  2571. When this is the symbol `prefix', only remove tags when
  2572. `org-agenda-prefix-format' contains a `%T' specifier."
  2573. :group 'org-agenda-line-format
  2574. :type '(choice
  2575. (const :tag "Always" t)
  2576. (const :tag "Never" nil)
  2577. (const :tag "When prefix format contains %T" prefix)))
  2578. (if (fboundp 'defvaralias)
  2579. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2580. 'org-agenda-remove-tags))
  2581. (defcustom org-agenda-tags-column -80
  2582. "Shift tags in agenda items to this column.
  2583. If this number is positive, it specifies the column. If it is negative,
  2584. it means that the tags should be flushright to that column. For example,
  2585. -80 works well for a normal 80 character screen."
  2586. :group 'org-agenda-line-format
  2587. :type 'integer)
  2588. (if (fboundp 'defvaralias)
  2589. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2590. (defcustom org-agenda-fontify-priorities t
  2591. "Non-nil means, highlight low and high priorities in agenda.
  2592. When t, the highest priority entries are bold, lowest priority italic.
  2593. This may also be an association list of priority faces. The face may be
  2594. a names face, or a list like `(:background \"Red\")'."
  2595. :group 'org-agenda-line-format
  2596. :type '(choice
  2597. (const :tag "Never" nil)
  2598. (const :tag "Defaults" t)
  2599. (repeat :tag "Specify"
  2600. (list (character :tag "Priority" :value ?A)
  2601. (sexp :tag "face")))))
  2602. (defgroup org-latex nil
  2603. "Options for embedding LaTeX code into Org-mode"
  2604. :tag "Org LaTeX"
  2605. :group 'org)
  2606. (defcustom org-format-latex-options
  2607. '(:foreground default :background default :scale 1.0
  2608. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2609. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2610. "Options for creating images from LaTeX fragments.
  2611. This is a property list with the following properties:
  2612. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2613. `default' means use the forground of the default face.
  2614. :background the background color, or \"Transparent\".
  2615. `default' means use the background of the default face.
  2616. :scale a scaling factor for the size of the images
  2617. :html-foreground, :html-background, :html-scale
  2618. The same numbers for HTML export.
  2619. :matchers a list indicating which matchers should be used to
  2620. find LaTeX fragments. Valid members of this list are:
  2621. \"begin\" find environments
  2622. \"$\" find math expressions surrounded by $...$
  2623. \"$$\" find math expressions surrounded by $$....$$
  2624. \"\\(\" find math expressions surrounded by \\(...\\)
  2625. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2626. :group 'org-latex
  2627. :type 'plist)
  2628. (defcustom org-format-latex-header "\\documentclass{article}
  2629. \\usepackage{fullpage} % do not remove
  2630. \\usepackage{amssymb}
  2631. \\usepackage[usenames]{color}
  2632. \\usepackage{amsmath}
  2633. \\usepackage{latexsym}
  2634. \\usepackage[mathscr]{eucal}
  2635. \\pagestyle{empty} % do not remove"
  2636. "The document header used for processing LaTeX fragments."
  2637. :group 'org-latex
  2638. :type 'string)
  2639. (defgroup org-export nil
  2640. "Options for exporting org-listings."
  2641. :tag "Org Export"
  2642. :group 'org)
  2643. (defgroup org-export-general nil
  2644. "General options for exporting Org-mode files."
  2645. :tag "Org Export General"
  2646. :group 'org-export)
  2647. ;; FIXME
  2648. (defvar org-export-publishing-directory nil)
  2649. (defcustom org-export-with-special-strings t
  2650. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2651. When this option is turned on, these strings will be exported as:
  2652. Org HTML LaTeX
  2653. -----+----------+--------
  2654. \\- &shy; \\-
  2655. -- &ndash; --
  2656. --- &mdash; ---
  2657. ... &hellip; \ldots
  2658. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2659. :group 'org-export-translation
  2660. :type 'boolean)
  2661. (defcustom org-export-language-setup
  2662. '(("en" "Author" "Date" "Table of Contents")
  2663. ("cs" "Autor" "Datum" "Obsah")
  2664. ("da" "Ophavsmand" "Dato" "Indhold")
  2665. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2666. ("es" "Autor" "Fecha" "\xcdndice")
  2667. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2668. ("it" "Autore" "Data" "Indice")
  2669. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2670. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2671. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2672. "Terms used in export text, translated to different languages.
  2673. Use the variable `org-export-default-language' to set the language,
  2674. or use the +OPTION lines for a per-file setting."
  2675. :group 'org-export-general
  2676. :type '(repeat
  2677. (list
  2678. (string :tag "HTML language tag")
  2679. (string :tag "Author")
  2680. (string :tag "Date")
  2681. (string :tag "Table of Contents"))))
  2682. (defcustom org-export-default-language "en"
  2683. "The default language of HTML export, as a string.
  2684. This should have an association in `org-export-language-setup'."
  2685. :group 'org-export-general
  2686. :type 'string)
  2687. (defcustom org-export-skip-text-before-1st-heading t
  2688. "Non-nil means, skip all text before the first headline when exporting.
  2689. When nil, that text is exported as well."
  2690. :group 'org-export-general
  2691. :type 'boolean)
  2692. (defcustom org-export-headline-levels 3
  2693. "The last level which is still exported as a headline.
  2694. Inferior levels will produce itemize lists when exported.
  2695. Note that a numeric prefix argument to an exporter function overrides
  2696. this setting.
  2697. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2698. :group 'org-export-general
  2699. :type 'number)
  2700. (defcustom org-export-with-section-numbers t
  2701. "Non-nil means, add section numbers to headlines when exporting.
  2702. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2703. :group 'org-export-general
  2704. :type 'boolean)
  2705. (defcustom org-export-with-toc t
  2706. "Non-nil means, create a table of contents in exported files.
  2707. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2708. When an integer, include levels up to N in the toc, this may then be
  2709. different from `org-export-headline-levels', but it will not be allowed
  2710. to be larger than the number of headline levels.
  2711. When nil, no table of contents is made.
  2712. Headlines which contain any TODO items will be marked with \"(*)\" in
  2713. ASCII export, and with red color in HTML output, if the option
  2714. `org-export-mark-todo-in-toc' is set.
  2715. In HTML output, the TOC will be clickable.
  2716. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2717. or \"toc:3\"."
  2718. :group 'org-export-general
  2719. :type '(choice
  2720. (const :tag "No Table of Contents" nil)
  2721. (const :tag "Full Table of Contents" t)
  2722. (integer :tag "TOC to level")))
  2723. (defcustom org-export-mark-todo-in-toc nil
  2724. "Non-nil means, mark TOC lines that contain any open TODO items."
  2725. :group 'org-export-general
  2726. :type 'boolean)
  2727. (defcustom org-export-preserve-breaks nil
  2728. "Non-nil means, preserve all line breaks when exporting.
  2729. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2730. export, line breaks will always be preserved, regardless of this variable.
  2731. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2732. :group 'org-export-general
  2733. :type 'boolean)
  2734. (defcustom org-export-with-archived-trees 'headline
  2735. "Whether subtrees with the ARCHIVE tag should be exported.
  2736. This can have three different values
  2737. nil Do not export, pretend this tree is not present
  2738. t Do export the entire tree
  2739. headline Only export the headline, but skip the tree below it."
  2740. :group 'org-export-general
  2741. :group 'org-archive
  2742. :type '(choice
  2743. (const :tag "not at all" nil)
  2744. (const :tag "headline only" 'headline)
  2745. (const :tag "entirely" t)))
  2746. (defcustom org-export-author-info t
  2747. "Non-nil means, insert author name and email into the exported file.
  2748. This option can also be set with the +OPTIONS line,
  2749. e.g. \"author-info:nil\"."
  2750. :group 'org-export-general
  2751. :type 'boolean)
  2752. (defcustom org-export-time-stamp-file t
  2753. "Non-nil means, insert a time stamp into the exported file.
  2754. The time stamp shows when the file was created.
  2755. This option can also be set with the +OPTIONS line,
  2756. e.g. \"timestamp:nil\"."
  2757. :group 'org-export-general
  2758. :type 'boolean)
  2759. (defcustom org-export-with-timestamps t
  2760. "If nil, do not export time stamps and associated keywords."
  2761. :group 'org-export-general
  2762. :type 'boolean)
  2763. (defcustom org-export-remove-timestamps-from-toc t
  2764. "If nil, remove timestamps from the table of contents entries."
  2765. :group 'org-export-general
  2766. :type 'boolean)
  2767. (defcustom org-export-with-tags 'not-in-toc
  2768. "If nil, do not export tags, just remove them from headlines.
  2769. If this is the symbol `not-in-toc', tags will be removed from table of
  2770. contents entries, but still be shown in the headlines of the document.
  2771. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2772. :group 'org-export-general
  2773. :type '(choice
  2774. (const :tag "Off" nil)
  2775. (const :tag "Not in TOC" not-in-toc)
  2776. (const :tag "On" t)))
  2777. (defcustom org-export-with-drawers nil
  2778. "Non-nil means, export with drawers like the property drawer.
  2779. When t, all drawers are exported. This may also be a list of
  2780. drawer names to export."
  2781. :group 'org-export-general
  2782. :type '(choice
  2783. (const :tag "All drawers" t)
  2784. (const :tag "None" nil)
  2785. (repeat :tag "Selected drawers"
  2786. (string :tag "Drawer name"))))
  2787. (defgroup org-export-translation nil
  2788. "Options for translating special ascii sequences for the export backends."
  2789. :tag "Org Export Translation"
  2790. :group 'org-export)
  2791. (defcustom org-export-with-emphasize t
  2792. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2793. If the export target supports emphasizing text, the word will be
  2794. typeset in bold, italic, or underlined, respectively. Works only for
  2795. single words, but you can say: I *really* *mean* *this*.
  2796. Not all export backends support this.
  2797. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2798. :group 'org-export-translation
  2799. :type 'boolean)
  2800. (defcustom org-export-with-footnotes t
  2801. "If nil, export [1] as a footnote marker.
  2802. Lines starting with [1] will be formatted as footnotes.
  2803. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2804. :group 'org-export-translation
  2805. :type 'boolean)
  2806. (defcustom org-export-with-sub-superscripts t
  2807. "Non-nil means, interpret \"_\" and \"^\" for export.
  2808. When this option is turned on, you can use TeX-like syntax for sub- and
  2809. superscripts. Several characters after \"_\" or \"^\" will be
  2810. considered as a single item - so grouping with {} is normally not
  2811. needed. For example, the following things will be parsed as single
  2812. sub- or superscripts.
  2813. 10^24 or 10^tau several digits will be considered 1 item.
  2814. 10^-12 or 10^-tau a leading sign with digits or a word
  2815. x^2-y^3 will be read as x^2 - y^3, because items are
  2816. terminated by almost any nonword/nondigit char.
  2817. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2818. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2819. sub/superscript. If you set this variable to the symbol `{}',
  2820. the braces are *required* in order to trigger interpretations as
  2821. sub/superscript. This can be helpful in documents that need \"_\"
  2822. frequently in plain text.
  2823. Not all export backends support this, but HTML does.
  2824. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2825. :group 'org-export-translation
  2826. :type '(choice
  2827. (const :tag "Always interpret" t)
  2828. (const :tag "Only with braces" {})
  2829. (const :tag "Never interpret" nil)))
  2830. (defcustom org-export-with-special-strings t
  2831. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2832. When this option is turned on, these strings will be exported as:
  2833. \\- : &shy;
  2834. -- : &ndash;
  2835. --- : &mdash;
  2836. Not all export backends support this, but HTML does.
  2837. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2838. :group 'org-export-translation
  2839. :type 'boolean)
  2840. (defcustom org-export-with-TeX-macros t
  2841. "Non-nil means, interpret simple TeX-like macros when exporting.
  2842. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2843. No only real TeX macros will work here, but the standard HTML entities
  2844. for math can be used as macro names as well. For a list of supported
  2845. names in HTML export, see the constant `org-html-entities'.
  2846. Not all export backends support this.
  2847. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2848. :group 'org-export-translation
  2849. :group 'org-export-latex
  2850. :type 'boolean)
  2851. (defcustom org-export-with-LaTeX-fragments nil
  2852. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2853. When set, the exporter will find LaTeX environments if the \\begin line is
  2854. the first non-white thing on a line. It will also find the math delimiters
  2855. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2856. display math.
  2857. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2858. :group 'org-export-translation
  2859. :group 'org-export-latex
  2860. :type 'boolean)
  2861. (defcustom org-export-with-fixed-width t
  2862. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2863. This can be used to have pre-formatted text, fragments of code etc. For
  2864. example:
  2865. : ;; Some Lisp examples
  2866. : (while (defc cnt)
  2867. : (ding))
  2868. will be looking just like this in also HTML. See also the QUOTE keyword.
  2869. Not all export backends support this.
  2870. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2871. :group 'org-export-translation
  2872. :type 'boolean)
  2873. (defcustom org-match-sexp-depth 3
  2874. "Number of stacked braces for sub/superscript matching.
  2875. This has to be set before loading org.el to be effective."
  2876. :group 'org-export-translation
  2877. :type 'integer)
  2878. (defgroup org-export-tables nil
  2879. "Options for exporting tables in Org-mode."
  2880. :tag "Org Export Tables"
  2881. :group 'org-export)
  2882. (defcustom org-export-with-tables t
  2883. "If non-nil, lines starting with \"|\" define a table.
  2884. For example:
  2885. | Name | Address | Birthday |
  2886. |-------------+----------+-----------|
  2887. | Arthur Dent | England | 29.2.2100 |
  2888. Not all export backends support this.
  2889. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2890. :group 'org-export-tables
  2891. :type 'boolean)
  2892. (defcustom org-export-highlight-first-table-line t
  2893. "Non-nil means, highlight the first table line.
  2894. In HTML export, this means use <th> instead of <td>.
  2895. In tables created with table.el, this applies to the first table line.
  2896. In Org-mode tables, all lines before the first horizontal separator
  2897. line will be formatted with <th> tags."
  2898. :group 'org-export-tables
  2899. :type 'boolean)
  2900. (defcustom org-export-table-remove-special-lines t
  2901. "Remove special lines and marking characters in calculating tables.
  2902. This removes the special marking character column from tables that are set
  2903. up for spreadsheet calculations. It also removes the entire lines
  2904. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2905. the values of constants may be useful to have."
  2906. :group 'org-export-tables
  2907. :type 'boolean)
  2908. (defcustom org-export-prefer-native-exporter-for-tables nil
  2909. "Non-nil means, always export tables created with table.el natively.
  2910. Natively means, use the HTML code generator in table.el.
  2911. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2912. the table does not use row- or column-spanning). This has the
  2913. advantage, that the automatic HTML conversions for math symbols and
  2914. sub/superscripts can be applied. Org-mode's HTML generator is also
  2915. much faster."
  2916. :group 'org-export-tables
  2917. :type 'boolean)
  2918. (defgroup org-export-ascii nil
  2919. "Options specific for ASCII export of Org-mode files."
  2920. :tag "Org Export ASCII"
  2921. :group 'org-export)
  2922. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2923. "Characters for underlining headings in ASCII export.
  2924. In the given sequence, these characters will be used for level 1, 2, ..."
  2925. :group 'org-export-ascii
  2926. :type '(repeat character))
  2927. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2928. "Bullet characters for headlines converted to lists in ASCII export.
  2929. The first character is used for the first lest level generated in this
  2930. way, and so on. If there are more levels than characters given here,
  2931. the list will be repeated.
  2932. Note that plain lists will keep the same bullets as the have in the
  2933. Org-mode file."
  2934. :group 'org-export-ascii
  2935. :type '(repeat character))
  2936. (defgroup org-export-xml nil
  2937. "Options specific for XML export of Org-mode files."
  2938. :tag "Org Export XML"
  2939. :group 'org-export)
  2940. (defgroup org-export-html nil
  2941. "Options specific for HTML export of Org-mode files."
  2942. :tag "Org Export HTML"
  2943. :group 'org-export)
  2944. (defcustom org-export-html-coding-system nil
  2945. ""
  2946. :group 'org-export-html
  2947. :type 'coding-system)
  2948. (defcustom org-export-html-extension "html"
  2949. "The extension for exported HTML files."
  2950. :group 'org-export-html
  2951. :type 'string)
  2952. (defcustom org-export-html-style
  2953. "<style type=\"text/css\">
  2954. html {
  2955. font-family: Times, serif;
  2956. font-size: 12pt;
  2957. }
  2958. .title { text-align: center; }
  2959. .todo { color: red; }
  2960. .done { color: green; }
  2961. .timestamp { color: grey }
  2962. .timestamp-kwd { color: CadetBlue }
  2963. .tag { background-color:lightblue; font-weight:normal }
  2964. .target { background-color: lavender; }
  2965. pre {
  2966. border: 1pt solid #AEBDCC;
  2967. background-color: #F3F5F7;
  2968. padding: 5pt;
  2969. font-family: courier, monospace;
  2970. }
  2971. table { border-collapse: collapse; }
  2972. td, th {
  2973. vertical-align: top;
  2974. <!--border: 1pt solid #ADB9CC;-->
  2975. }
  2976. </style>"
  2977. "The default style specification for exported HTML files.
  2978. Since there are different ways of setting style information, this variable
  2979. needs to contain the full HTML structure to provide a style, including the
  2980. surrounding HTML tags. The style specifications should include definitions
  2981. for new classes todo, done, title, and deadline. For example, legal values
  2982. would be:
  2983. <style type=\"text/css\">
  2984. p { font-weight: normal; color: gray; }
  2985. h1 { color: black; }
  2986. .title { text-align: center; }
  2987. .todo, .deadline { color: red; }
  2988. .done { color: green; }
  2989. </style>
  2990. or, if you want to keep the style in a file,
  2991. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2992. As the value of this option simply gets inserted into the HTML <head> header,
  2993. you can \"misuse\" it to add arbitrary text to the header."
  2994. :group 'org-export-html
  2995. :type 'string)
  2996. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2997. "Format for typesetting the document title in HTML export."
  2998. :group 'org-export-html
  2999. :type 'string)
  3000. (defcustom org-export-html-toplevel-hlevel 2
  3001. "The <H> level for level 1 headings in HTML export."
  3002. :group 'org-export-html
  3003. :type 'string)
  3004. (defcustom org-export-html-link-org-files-as-html t
  3005. "Non-nil means, make file links to `file.org' point to `file.html'.
  3006. When org-mode is exporting an org-mode file to HTML, links to
  3007. non-html files are directly put into a href tag in HTML.
  3008. However, links to other Org-mode files (recognized by the
  3009. extension `.org.) should become links to the corresponding html
  3010. file, assuming that the linked org-mode file will also be
  3011. converted to HTML.
  3012. When nil, the links still point to the plain `.org' file."
  3013. :group 'org-export-html
  3014. :type 'boolean)
  3015. (defcustom org-export-html-inline-images 'maybe
  3016. "Non-nil means, inline images into exported HTML pages.
  3017. This is done using an <img> tag. When nil, an anchor with href is used to
  3018. link to the image. If this option is `maybe', then images in links with
  3019. an empty description will be inlined, while images with a description will
  3020. be linked only."
  3021. :group 'org-export-html
  3022. :type '(choice (const :tag "Never" nil)
  3023. (const :tag "Always" t)
  3024. (const :tag "When there is no description" maybe)))
  3025. ;; FIXME: rename
  3026. (defcustom org-export-html-expand t
  3027. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3028. When nil, these tags will be exported as plain text and therefore
  3029. not be interpreted by a browser.
  3030. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3031. :group 'org-export-html
  3032. :type 'boolean)
  3033. (defcustom org-export-html-table-tag
  3034. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3035. "The HTML tag that is used to start a table.
  3036. This must be a <table> tag, but you may change the options like
  3037. borders and spacing."
  3038. :group 'org-export-html
  3039. :type 'string)
  3040. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3041. "The opening tag for table header fields.
  3042. This is customizable so that alignment options can be specified."
  3043. :group 'org-export-tables
  3044. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3045. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3046. "The opening tag for table data fields.
  3047. This is customizable so that alignment options can be specified."
  3048. :group 'org-export-tables
  3049. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3050. (defcustom org-export-html-with-timestamp nil
  3051. "If non-nil, write `org-export-html-html-helper-timestamp'
  3052. into the exported HTML text. Otherwise, the buffer will just be saved
  3053. to a file."
  3054. :group 'org-export-html
  3055. :type 'boolean)
  3056. (defcustom org-export-html-html-helper-timestamp
  3057. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3058. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3059. :group 'org-export-html
  3060. :type 'string)
  3061. (defgroup org-export-icalendar nil
  3062. "Options specific for iCalendar export of Org-mode files."
  3063. :tag "Org Export iCalendar"
  3064. :group 'org-export)
  3065. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3066. "The file name for the iCalendar file covering all agenda files.
  3067. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3068. The file name should be absolute, the file will be overwritten without warning."
  3069. :group 'org-export-icalendar
  3070. :type 'file)
  3071. (defcustom org-icalendar-include-todo nil
  3072. "Non-nil means, export to iCalendar files should also cover TODO items."
  3073. :group 'org-export-icalendar
  3074. :type '(choice
  3075. (const :tag "None" nil)
  3076. (const :tag "Unfinished" t)
  3077. (const :tag "All" all)))
  3078. (defcustom org-icalendar-include-sexps t
  3079. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3080. These are entries like in the diary, but directly in an Org-mode file."
  3081. :group 'org-export-icalendar
  3082. :type 'boolean)
  3083. (defcustom org-icalendar-include-body 100
  3084. "Amount of text below headline to be included in iCalendar export.
  3085. This is a number of characters that should maximally be included.
  3086. Properties, scheduling and clocking lines will always be removed.
  3087. The text will be inserted into the DESCRIPTION field."
  3088. :group 'org-export-icalendar
  3089. :type '(choice
  3090. (const :tag "Nothing" nil)
  3091. (const :tag "Everything" t)
  3092. (integer :tag "Max characters")))
  3093. (defcustom org-icalendar-combined-name "OrgMode"
  3094. "Calendar name for the combined iCalendar representing all agenda files."
  3095. :group 'org-export-icalendar
  3096. :type 'string)
  3097. (defgroup org-font-lock nil
  3098. "Font-lock settings for highlighting in Org-mode."
  3099. :tag "Org Font Lock"
  3100. :group 'org)
  3101. (defcustom org-level-color-stars-only nil
  3102. "Non-nil means fontify only the stars in each headline.
  3103. When nil, the entire headline is fontified.
  3104. Changing it requires restart of `font-lock-mode' to become effective
  3105. also in regions already fontified."
  3106. :group 'org-font-lock
  3107. :type 'boolean)
  3108. (defcustom org-hide-leading-stars nil
  3109. "Non-nil means, hide the first N-1 stars in a headline.
  3110. This works by using the face `org-hide' for these stars. This
  3111. face is white for a light background, and black for a dark
  3112. background. You may have to customize the face `org-hide' to
  3113. make this work.
  3114. Changing it requires restart of `font-lock-mode' to become effective
  3115. also in regions already fontified.
  3116. You may also set this on a per-file basis by adding one of the following
  3117. lines to the buffer:
  3118. #+STARTUP: hidestars
  3119. #+STARTUP: showstars"
  3120. :group 'org-font-lock
  3121. :type 'boolean)
  3122. (defcustom org-fontify-done-headline nil
  3123. "Non-nil means, change the face of a headline if it is marked DONE.
  3124. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3125. When this is non-nil, the headline after the keyword is set to the
  3126. `org-headline-done' as an additional indication."
  3127. :group 'org-font-lock
  3128. :type 'boolean)
  3129. (defcustom org-fontify-emphasized-text t
  3130. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3131. Changing this variable requires a restart of Emacs to take effect."
  3132. :group 'org-font-lock
  3133. :type 'boolean)
  3134. (defcustom org-highlight-latex-fragments-and-specials nil
  3135. "Non-nil means, fontify what is treated specially by the exporters."
  3136. :group 'org-font-lock
  3137. :type 'boolean)
  3138. (defcustom org-hide-emphasis-markers nil
  3139. "Non-nil mean font-lock should hide the emphasis marker characters."
  3140. :group 'org-font-lock
  3141. :type 'boolean)
  3142. (defvar org-emph-re nil
  3143. "Regular expression for matching emphasis.")
  3144. (defvar org-verbatim-re nil
  3145. "Regular expression for matching verbatim text.")
  3146. (defvar org-emphasis-regexp-components) ; defined just below
  3147. (defvar org-emphasis-alist) ; defined just below
  3148. (defun org-set-emph-re (var val)
  3149. "Set variable and compute the emphasis regular expression."
  3150. (set var val)
  3151. (when (and (boundp 'org-emphasis-alist)
  3152. (boundp 'org-emphasis-regexp-components)
  3153. org-emphasis-alist org-emphasis-regexp-components)
  3154. (let* ((e org-emphasis-regexp-components)
  3155. (pre (car e))
  3156. (post (nth 1 e))
  3157. (border (nth 2 e))
  3158. (body (nth 3 e))
  3159. (nl (nth 4 e))
  3160. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3161. (body1 (concat body "*?"))
  3162. (markers (mapconcat 'car org-emphasis-alist ""))
  3163. (vmarkers (mapconcat
  3164. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3165. org-emphasis-alist "")))
  3166. ;; make sure special characters appear at the right position in the class
  3167. (if (string-match "\\^" markers)
  3168. (setq markers (concat (replace-match "" t t markers) "^")))
  3169. (if (string-match "-" markers)
  3170. (setq markers (concat (replace-match "" t t markers) "-")))
  3171. (if (string-match "\\^" vmarkers)
  3172. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3173. (if (string-match "-" vmarkers)
  3174. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3175. (if (> nl 0)
  3176. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3177. (int-to-string nl) "\\}")))
  3178. ;; Make the regexp
  3179. (setq org-emph-re
  3180. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3181. "\\("
  3182. "\\([" markers "]\\)"
  3183. "\\("
  3184. "[^" border "]\\|"
  3185. "[^" border (if (and nil stacked) markers) "]"
  3186. body1
  3187. "[^" border (if (and nil stacked) markers) "]"
  3188. "\\)"
  3189. "\\3\\)"
  3190. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3191. (setq org-verbatim-re
  3192. (concat "\\([" pre "]\\|^\\)"
  3193. "\\("
  3194. "\\([" vmarkers "]\\)"
  3195. "\\("
  3196. "[^" border "]\\|"
  3197. "[^" border "]"
  3198. body1
  3199. "[^" border "]"
  3200. "\\)"
  3201. "\\3\\)"
  3202. "\\([" post "]\\|$\\)")))))
  3203. (defcustom org-emphasis-regexp-components
  3204. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3205. "Components used to build the regular expression for emphasis.
  3206. This is a list with 6 entries. Terminology: In an emphasis string
  3207. like \" *strong word* \", we call the initial space PREMATCH, the final
  3208. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3209. and \"trong wor\" is the body. The different components in this variable
  3210. specify what is allowed/forbidden in each part:
  3211. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3212. post Chars allowed as postmatch. End of line will be allowed too.
  3213. border The chars *forbidden* as border characters.
  3214. body-regexp A regexp like \".\" to match a body character. Don't use
  3215. non-shy groups here, and don't allow newline here.
  3216. newline The maximum number of newlines allowed in an emphasis exp.
  3217. Use customize to modify this, or restart Emacs after changing it."
  3218. :group 'org-font-lock
  3219. :set 'org-set-emph-re
  3220. :type '(list
  3221. (sexp :tag "Allowed chars in pre ")
  3222. (sexp :tag "Allowed chars in post ")
  3223. (sexp :tag "Forbidden chars in border ")
  3224. (sexp :tag "Regexp for body ")
  3225. (integer :tag "number of newlines allowed")
  3226. (option (boolean :tag "Stacking (DISABLED) "))))
  3227. (defcustom org-emphasis-alist
  3228. '(("*" bold "<b>" "</b>")
  3229. ("/" italic "<i>" "</i>")
  3230. ("_" underline "<u>" "</u>")
  3231. ("=" org-code "<code>" "</code>" verbatim)
  3232. ("~" org-verbatim "" "" verbatim)
  3233. ("+" (:strike-through t) "<del>" "</del>")
  3234. )
  3235. "Special syntax for emphasized text.
  3236. Text starting and ending with a special character will be emphasized, for
  3237. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3238. characters, the face to be used by font-lock for highlighting in Org-mode
  3239. Emacs buffers, and the HTML tags to be used for this.
  3240. Use customize to modify this, or restart Emacs after changing it."
  3241. :group 'org-font-lock
  3242. :set 'org-set-emph-re
  3243. :type '(repeat
  3244. (list
  3245. (string :tag "Marker character")
  3246. (choice
  3247. (face :tag "Font-lock-face")
  3248. (plist :tag "Face property list"))
  3249. (string :tag "HTML start tag")
  3250. (string :tag "HTML end tag")
  3251. (option (const verbatim)))))
  3252. ;;; The faces
  3253. (defgroup org-faces nil
  3254. "Faces in Org-mode."
  3255. :tag "Org Faces"
  3256. :group 'org-font-lock)
  3257. (defun org-compatible-face (inherits specs)
  3258. "Make a compatible face specification.
  3259. If INHERITS is an existing face and if the Emacs version supports it,
  3260. just inherit the face. If not, use SPECS to define the face.
  3261. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3262. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3263. to the top of the list. The `min-colors' attribute will be removed from
  3264. any other entries, and any resulting duplicates will be removed entirely."
  3265. (cond
  3266. ((and inherits (facep inherits)
  3267. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3268. ;; In Emacs 23, we use inheritance where possible.
  3269. ;; We only do this in Emacs 23, because only there the outline
  3270. ;; faces have been changed to the original org-mode-level-faces.
  3271. (list (list t :inherit inherits)))
  3272. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3273. ;; These do not understand the `min-colors' attribute.
  3274. (let (r e a)
  3275. (while (setq e (pop specs))
  3276. (cond
  3277. ((memq (car e) '(t default)) (push e r))
  3278. ((setq a (member '(min-colors 8) (car e)))
  3279. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3280. (cdr e)))))
  3281. ((setq a (assq 'min-colors (car e)))
  3282. (setq e (cons (delq a (car e)) (cdr e)))
  3283. (or (assoc (car e) r) (push e r)))
  3284. (t (or (assoc (car e) r) (push e r)))))
  3285. (nreverse r)))
  3286. (t specs)))
  3287. (put 'org-compatible-face 'lisp-indent-function 1)
  3288. (defface org-hide
  3289. '((((background light)) (:foreground "white"))
  3290. (((background dark)) (:foreground "black")))
  3291. "Face used to hide leading stars in headlines.
  3292. The forground color of this face should be equal to the background
  3293. color of the frame."
  3294. :group 'org-faces)
  3295. (defface org-level-1 ;; font-lock-function-name-face
  3296. (org-compatible-face 'outline-1
  3297. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3298. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3299. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3300. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3301. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3302. (t (:bold t))))
  3303. "Face used for level 1 headlines."
  3304. :group 'org-faces)
  3305. (defface org-level-2 ;; font-lock-variable-name-face
  3306. (org-compatible-face 'outline-2
  3307. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3308. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3309. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3310. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3311. (t (:bold t))))
  3312. "Face used for level 2 headlines."
  3313. :group 'org-faces)
  3314. (defface org-level-3 ;; font-lock-keyword-face
  3315. (org-compatible-face 'outline-3
  3316. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3317. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3318. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3319. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3320. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3321. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3322. (t (:bold t))))
  3323. "Face used for level 3 headlines."
  3324. :group 'org-faces)
  3325. (defface org-level-4 ;; font-lock-comment-face
  3326. (org-compatible-face 'outline-4
  3327. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3328. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3329. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3330. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3331. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3332. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3333. (t (:bold t))))
  3334. "Face used for level 4 headlines."
  3335. :group 'org-faces)
  3336. (defface org-level-5 ;; font-lock-type-face
  3337. (org-compatible-face 'outline-5
  3338. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3339. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3340. (((class color) (min-colors 8)) (:foreground "green"))))
  3341. "Face used for level 5 headlines."
  3342. :group 'org-faces)
  3343. (defface org-level-6 ;; font-lock-constant-face
  3344. (org-compatible-face 'outline-6
  3345. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3346. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3347. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3348. "Face used for level 6 headlines."
  3349. :group 'org-faces)
  3350. (defface org-level-7 ;; font-lock-builtin-face
  3351. (org-compatible-face 'outline-7
  3352. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3353. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3354. (((class color) (min-colors 8)) (:foreground "blue"))))
  3355. "Face used for level 7 headlines."
  3356. :group 'org-faces)
  3357. (defface org-level-8 ;; font-lock-string-face
  3358. (org-compatible-face 'outline-8
  3359. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3360. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3361. (((class color) (min-colors 8)) (:foreground "green"))))
  3362. "Face used for level 8 headlines."
  3363. :group 'org-faces)
  3364. (defface org-special-keyword ;; font-lock-string-face
  3365. (org-compatible-face nil
  3366. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3367. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3368. (t (:italic t))))
  3369. "Face used for special keywords."
  3370. :group 'org-faces)
  3371. (defface org-drawer ;; font-lock-function-name-face
  3372. (org-compatible-face nil
  3373. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3374. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3375. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3376. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3377. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3378. (t (:bold t))))
  3379. "Face used for drawers."
  3380. :group 'org-faces)
  3381. (defface org-property-value nil
  3382. "Face used for the value of a property."
  3383. :group 'org-faces)
  3384. (defface org-column
  3385. (org-compatible-face nil
  3386. '((((class color) (min-colors 16) (background light))
  3387. (:background "grey90"))
  3388. (((class color) (min-colors 16) (background dark))
  3389. (:background "grey30"))
  3390. (((class color) (min-colors 8))
  3391. (:background "cyan" :foreground "black"))
  3392. (t (:inverse-video t))))
  3393. "Face for column display of entry properties."
  3394. :group 'org-faces)
  3395. (when (fboundp 'set-face-attribute)
  3396. ;; Make sure that a fixed-width face is used when we have a column table.
  3397. (set-face-attribute 'org-column nil
  3398. :height (face-attribute 'default :height)
  3399. :family (face-attribute 'default :family)))
  3400. (defface org-warning
  3401. (org-compatible-face 'font-lock-warning-face
  3402. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3403. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3404. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3405. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3406. (t (:bold t))))
  3407. "Face for deadlines and TODO keywords."
  3408. :group 'org-faces)
  3409. (defface org-archived ; similar to shadow
  3410. (org-compatible-face 'shadow
  3411. '((((class color grayscale) (min-colors 88) (background light))
  3412. (:foreground "grey50"))
  3413. (((class color grayscale) (min-colors 88) (background dark))
  3414. (:foreground "grey70"))
  3415. (((class color) (min-colors 8) (background light))
  3416. (:foreground "green"))
  3417. (((class color) (min-colors 8) (background dark))
  3418. (:foreground "yellow"))))
  3419. "Face for headline with the ARCHIVE tag."
  3420. :group 'org-faces)
  3421. (defface org-link
  3422. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3423. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3424. (t (:underline t)))
  3425. "Face for links."
  3426. :group 'org-faces)
  3427. (defface org-ellipsis
  3428. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3429. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3430. (t (:strike-through t)))
  3431. "Face for the ellipsis in folded text."
  3432. :group 'org-faces)
  3433. (defface org-target
  3434. '((((class color) (background light)) (:underline t))
  3435. (((class color) (background dark)) (:underline t))
  3436. (t (:underline t)))
  3437. "Face for links."
  3438. :group 'org-faces)
  3439. (defface org-date
  3440. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3441. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3442. (t (:underline t)))
  3443. "Face for links."
  3444. :group 'org-faces)
  3445. (defface org-sexp-date
  3446. '((((class color) (background light)) (:foreground "Purple"))
  3447. (((class color) (background dark)) (:foreground "Cyan"))
  3448. (t (:underline t)))
  3449. "Face for links."
  3450. :group 'org-faces)
  3451. (defface org-tag
  3452. '((t (:bold t)))
  3453. "Face for tags."
  3454. :group 'org-faces)
  3455. (defface org-todo ; font-lock-warning-face
  3456. (org-compatible-face nil
  3457. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3458. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3459. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3460. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3461. (t (:inverse-video t :bold t))))
  3462. "Face for TODO keywords."
  3463. :group 'org-faces)
  3464. (defface org-done ;; font-lock-type-face
  3465. (org-compatible-face nil
  3466. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3467. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3468. (((class color) (min-colors 8)) (:foreground "green"))
  3469. (t (:bold t))))
  3470. "Face used for todo keywords that indicate DONE items."
  3471. :group 'org-faces)
  3472. (defface org-headline-done ;; font-lock-string-face
  3473. (org-compatible-face nil
  3474. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3475. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3476. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3477. "Face used to indicate that a headline is DONE.
  3478. This face is only used if `org-fontify-done-headline' is set. If applies
  3479. to the part of the headline after the DONE keyword."
  3480. :group 'org-faces)
  3481. (defcustom org-todo-keyword-faces nil
  3482. "Faces for specific TODO keywords.
  3483. This is a list of cons cells, with TODO keywords in the car
  3484. and faces in the cdr. The face can be a symbol, or a property
  3485. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3486. :group 'org-faces
  3487. :group 'org-todo
  3488. :type '(repeat
  3489. (cons
  3490. (string :tag "keyword")
  3491. (sexp :tag "face"))))
  3492. (defface org-table ;; font-lock-function-name-face
  3493. (org-compatible-face nil
  3494. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3495. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3496. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3497. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3498. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3499. (((class color) (min-colors 8) (background dark)))))
  3500. "Face used for tables."
  3501. :group 'org-faces)
  3502. (defface org-formula
  3503. (org-compatible-face nil
  3504. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3505. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3506. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3507. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3508. (t (:bold t :italic t))))
  3509. "Face for formulas."
  3510. :group 'org-faces)
  3511. (defface org-code
  3512. (org-compatible-face nil
  3513. '((((class color grayscale) (min-colors 88) (background light))
  3514. (:foreground "grey50"))
  3515. (((class color grayscale) (min-colors 88) (background dark))
  3516. (:foreground "grey70"))
  3517. (((class color) (min-colors 8) (background light))
  3518. (:foreground "green"))
  3519. (((class color) (min-colors 8) (background dark))
  3520. (:foreground "yellow"))))
  3521. "Face for fixed-with text like code snippets."
  3522. :group 'org-faces
  3523. :version "22.1")
  3524. (defface org-verbatim
  3525. (org-compatible-face nil
  3526. '((((class color grayscale) (min-colors 88) (background light))
  3527. (:foreground "grey50" :underline t))
  3528. (((class color grayscale) (min-colors 88) (background dark))
  3529. (:foreground "grey70" :underline t))
  3530. (((class color) (min-colors 8) (background light))
  3531. (:foreground "green" :underline t))
  3532. (((class color) (min-colors 8) (background dark))
  3533. (:foreground "yellow" :underline t))))
  3534. "Face for fixed-with text like code snippets."
  3535. :group 'org-faces
  3536. :version "22.1")
  3537. (defface org-agenda-structure ;; font-lock-function-name-face
  3538. (org-compatible-face nil
  3539. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3540. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3541. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3542. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3543. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3544. (t (:bold t))))
  3545. "Face used in agenda for captions and dates."
  3546. :group 'org-faces)
  3547. (defface org-scheduled-today
  3548. (org-compatible-face nil
  3549. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3550. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3551. (((class color) (min-colors 8)) (:foreground "green"))
  3552. (t (:bold t :italic t))))
  3553. "Face for items scheduled for a certain day."
  3554. :group 'org-faces)
  3555. (defface org-scheduled-previously
  3556. (org-compatible-face nil
  3557. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3558. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3559. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3560. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3561. (t (:bold t))))
  3562. "Face for items scheduled previously, and not yet done."
  3563. :group 'org-faces)
  3564. (defface org-upcoming-deadline
  3565. (org-compatible-face nil
  3566. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3567. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3568. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3569. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3570. (t (:bold t))))
  3571. "Face for items scheduled previously, and not yet done."
  3572. :group 'org-faces)
  3573. (defcustom org-agenda-deadline-faces
  3574. '((1.0 . org-warning)
  3575. (0.5 . org-upcoming-deadline)
  3576. (0.0 . default))
  3577. "Faces for showing deadlines in the agenda.
  3578. This is a list of cons cells. The cdr of each cell is a face to be used,
  3579. and it can also just be like '(:foreground \"yellow\").
  3580. Each car is a fraction of the head-warning time that must have passed for
  3581. this the face in the cdr to be used for display. The numbers must be
  3582. given in descending order. The head-warning time is normally taken
  3583. from `org-deadline-warning-days', but can also be specified in the deadline
  3584. timestamp itself, like this:
  3585. DEADLINE: <2007-08-13 Mon -8d>
  3586. You may use d for days, w for weeks, m for months and y for years. Months
  3587. and years will only be treated in an approximate fashion (30.4 days for a
  3588. month and 365.24 days for a year)."
  3589. :group 'org-faces
  3590. :group 'org-agenda-daily/weekly
  3591. :type '(repeat
  3592. (cons
  3593. (number :tag "Fraction of head-warning time passed")
  3594. (sexp :tag "Face"))))
  3595. ;; FIXME: this is not a good face yet.
  3596. (defface org-agenda-restriction-lock
  3597. (org-compatible-face nil
  3598. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3599. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3600. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3601. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3602. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3603. (t (:inverse-video t))))
  3604. "Face for showing the agenda restriction lock."
  3605. :group 'org-faces)
  3606. (defface org-time-grid ;; font-lock-variable-name-face
  3607. (org-compatible-face nil
  3608. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3609. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3610. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3611. "Face used for time grids."
  3612. :group 'org-faces)
  3613. (defconst org-level-faces
  3614. '(org-level-1 org-level-2 org-level-3 org-level-4
  3615. org-level-5 org-level-6 org-level-7 org-level-8
  3616. ))
  3617. (defcustom org-n-level-faces (length org-level-faces)
  3618. "The number different faces to be used for headlines.
  3619. Org-mode defines 8 different headline faces, so this can be at most 8.
  3620. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3621. :type 'number
  3622. :group 'org-faces)
  3623. ;;; Functions and variables from ther packages
  3624. ;; Declared here to avoid compiler warnings
  3625. (eval-and-compile
  3626. (unless (fboundp 'declare-function)
  3627. (defmacro declare-function (fn file &optional arglist fileonly))))
  3628. ;; XEmacs only
  3629. (defvar outline-mode-menu-heading)
  3630. (defvar outline-mode-menu-show)
  3631. (defvar outline-mode-menu-hide)
  3632. (defvar zmacs-regions) ; XEmacs regions
  3633. ;; Emacs only
  3634. (defvar mark-active)
  3635. ;; Various packages
  3636. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3637. (declare-function add-to-diary-list "diary-lib"
  3638. (date string specifier &optional marker globcolor literal))
  3639. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3640. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3641. (declare-function Info-goto-node "info" (nodename &optional fork))
  3642. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3643. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3644. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3645. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3646. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3647. (declare-function bbdb-record-name "ext:bbdb" (record))
  3648. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3649. (declare-function bibtex-generate-autokey "bibtex" ())
  3650. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3651. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3652. (defvar calc-embedded-close-formula)
  3653. (defvar calc-embedded-open-formula)
  3654. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3655. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3656. (declare-function calendar-check-holidays "holidays" (date))
  3657. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3658. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3659. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3660. (declare-function calendar-forward-day "cal-move" (arg))
  3661. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3662. (declare-function calendar-goto-date "cal-move" (date))
  3663. (declare-function calendar-goto-today "cal-move" ())
  3664. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3665. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3666. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3667. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3668. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3669. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3670. (defvar calendar-mode-map)
  3671. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3672. (declare-function cdlatex-tab "ext:cdlatex" ())
  3673. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3674. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3675. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3676. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3677. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3678. (defvar font-lock-unfontify-region-function)
  3679. (declare-function gnus-article-show-summary "gnus-art" ())
  3680. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3681. (defvar gnus-other-frame-object)
  3682. (defvar gnus-group-name)
  3683. (defvar gnus-article-current)
  3684. (defvar Info-current-file)
  3685. (defvar Info-current-node)
  3686. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3687. (declare-function mh-find-path "mh-utils" ())
  3688. (declare-function mh-get-header-field "mh-utils" (field))
  3689. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3690. (declare-function mh-header-display "mh-show" ())
  3691. (declare-function mh-index-previous-folder "mh-search" ())
  3692. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3693. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3694. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3695. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3696. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3697. (declare-function mh-show-header-display "mh-show" t t)
  3698. (declare-function mh-show-msg "mh-show" (msg))
  3699. (declare-function mh-show-show "mh-show" t t)
  3700. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3701. (defvar mh-progs)
  3702. (defvar mh-current-folder)
  3703. (defvar mh-show-folder-buffer)
  3704. (defvar mh-index-folder)
  3705. (defvar mh-searcher)
  3706. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3707. (declare-function parse-time-string "parse-time" (string))
  3708. (declare-function remember "remember" (&optional initial))
  3709. (declare-function remember-buffer-desc "remember" ())
  3710. (declare-function remember-finalize "remember" ())
  3711. (defvar remember-save-after-remembering)
  3712. (defvar remember-data-file)
  3713. (defvar remember-register)
  3714. (defvar remember-buffer)
  3715. (defvar remember-handler-functions)
  3716. (defvar remember-annotation-functions)
  3717. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3718. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3719. (declare-function rmail-what-message "rmail" ())
  3720. (defvar rmail-current-message)
  3721. (defvar texmathp-why)
  3722. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3723. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3724. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3725. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3726. (declare-function vm-isearch-update "ext:vm-search" ())
  3727. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3728. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3729. (declare-function vm-su-subject "ext:vm-summary" (m))
  3730. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3731. (defvar vm-message-pointer)
  3732. (defvar vm-folder-directory)
  3733. (defvar w3m-current-url)
  3734. (defvar w3m-current-title)
  3735. ;; backward compatibility to old version of wl
  3736. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3737. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3738. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3739. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3740. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3741. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3742. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3743. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3744. (defvar wl-summary-buffer-elmo-folder)
  3745. (defvar wl-summary-buffer-folder-name)
  3746. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3747. (defvar org-latex-regexps)
  3748. (defvar constants-unit-system)
  3749. ;;; Variables for pre-computed regular expressions, all buffer local
  3750. (defvar org-drawer-regexp nil
  3751. "Matches first line of a hidden block.")
  3752. (make-variable-buffer-local 'org-drawer-regexp)
  3753. (defvar org-todo-regexp nil
  3754. "Matches any of the TODO state keywords.")
  3755. (make-variable-buffer-local 'org-todo-regexp)
  3756. (defvar org-not-done-regexp nil
  3757. "Matches any of the TODO state keywords except the last one.")
  3758. (make-variable-buffer-local 'org-not-done-regexp)
  3759. (defvar org-todo-line-regexp nil
  3760. "Matches a headline and puts TODO state into group 2 if present.")
  3761. (make-variable-buffer-local 'org-todo-line-regexp)
  3762. (defvar org-complex-heading-regexp nil
  3763. "Matches a headline and puts everything into groups:
  3764. group 1: the stars
  3765. group 2: The todo keyword, maybe
  3766. group 3: Priority cookie
  3767. group 4: True headline
  3768. group 5: Tags")
  3769. (make-variable-buffer-local 'org-complex-heading-regexp)
  3770. (defvar org-todo-line-tags-regexp nil
  3771. "Matches a headline and puts TODO state into group 2 if present.
  3772. Also put tags into group 4 if tags are present.")
  3773. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3774. (defvar org-nl-done-regexp nil
  3775. "Matches newline followed by a headline with the DONE keyword.")
  3776. (make-variable-buffer-local 'org-nl-done-regexp)
  3777. (defvar org-looking-at-done-regexp nil
  3778. "Matches the DONE keyword a point.")
  3779. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3780. (defvar org-ds-keyword-length 12
  3781. "Maximum length of the Deadline and SCHEDULED keywords.")
  3782. (make-variable-buffer-local 'org-ds-keyword-length)
  3783. (defvar org-deadline-regexp nil
  3784. "Matches the DEADLINE keyword.")
  3785. (make-variable-buffer-local 'org-deadline-regexp)
  3786. (defvar org-deadline-time-regexp nil
  3787. "Matches the DEADLINE keyword together with a time stamp.")
  3788. (make-variable-buffer-local 'org-deadline-time-regexp)
  3789. (defvar org-deadline-line-regexp nil
  3790. "Matches the DEADLINE keyword and the rest of the line.")
  3791. (make-variable-buffer-local 'org-deadline-line-regexp)
  3792. (defvar org-scheduled-regexp nil
  3793. "Matches the SCHEDULED keyword.")
  3794. (make-variable-buffer-local 'org-scheduled-regexp)
  3795. (defvar org-scheduled-time-regexp nil
  3796. "Matches the SCHEDULED keyword together with a time stamp.")
  3797. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3798. (defvar org-closed-time-regexp nil
  3799. "Matches the CLOSED keyword together with a time stamp.")
  3800. (make-variable-buffer-local 'org-closed-time-regexp)
  3801. (defvar org-keyword-time-regexp nil
  3802. "Matches any of the 4 keywords, together with the time stamp.")
  3803. (make-variable-buffer-local 'org-keyword-time-regexp)
  3804. (defvar org-keyword-time-not-clock-regexp nil
  3805. "Matches any of the 3 keywords, together with the time stamp.")
  3806. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3807. (defvar org-maybe-keyword-time-regexp nil
  3808. "Matches a timestamp, possibly preceeded by a keyword.")
  3809. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3810. (defvar org-planning-or-clock-line-re nil
  3811. "Matches a line with planning or clock info.")
  3812. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3813. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3814. rear-nonsticky t mouse-map t fontified t)
  3815. "Properties to remove when a string without properties is wanted.")
  3816. (defsubst org-match-string-no-properties (num &optional string)
  3817. (if (featurep 'xemacs)
  3818. (let ((s (match-string num string)))
  3819. (remove-text-properties 0 (length s) org-rm-props s)
  3820. s)
  3821. (match-string-no-properties num string)))
  3822. (defsubst org-no-properties (s)
  3823. (if (fboundp 'set-text-properties)
  3824. (set-text-properties 0 (length s) nil s)
  3825. (remove-text-properties 0 (length s) org-rm-props s))
  3826. s)
  3827. (defsubst org-get-alist-option (option key)
  3828. (cond ((eq key t) t)
  3829. ((eq option t) t)
  3830. ((assoc key option) (cdr (assoc key option)))
  3831. (t (cdr (assq 'default option)))))
  3832. (defsubst org-inhibit-invisibility ()
  3833. "Modified `buffer-invisibility-spec' for Emacs 21.
  3834. Some ops with invisible text do not work correctly on Emacs 21. For these
  3835. we turn off invisibility temporarily. Use this in a `let' form."
  3836. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3837. (defsubst org-set-local (var value)
  3838. "Make VAR local in current buffer and set it to VALUE."
  3839. (set (make-variable-buffer-local var) value))
  3840. (defsubst org-mode-p ()
  3841. "Check if the current buffer is in Org-mode."
  3842. (eq major-mode 'org-mode))
  3843. (defsubst org-last (list)
  3844. "Return the last element of LIST."
  3845. (car (last list)))
  3846. (defun org-let (list &rest body)
  3847. (eval (cons 'let (cons list body))))
  3848. (put 'org-let 'lisp-indent-function 1)
  3849. (defun org-let2 (list1 list2 &rest body)
  3850. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3851. (put 'org-let2 'lisp-indent-function 2)
  3852. (defconst org-startup-options
  3853. '(("fold" org-startup-folded t)
  3854. ("overview" org-startup-folded t)
  3855. ("nofold" org-startup-folded nil)
  3856. ("showall" org-startup-folded nil)
  3857. ("content" org-startup-folded content)
  3858. ("hidestars" org-hide-leading-stars t)
  3859. ("showstars" org-hide-leading-stars nil)
  3860. ("odd" org-odd-levels-only t)
  3861. ("oddeven" org-odd-levels-only nil)
  3862. ("align" org-startup-align-all-tables t)
  3863. ("noalign" org-startup-align-all-tables nil)
  3864. ("customtime" org-display-custom-times t)
  3865. ("logdone" org-log-done time)
  3866. ("lognotedone" org-log-done note)
  3867. ("nologdone" org-log-done nil)
  3868. ("lognoteclock-out" org-log-note-clock-out t)
  3869. ("nolognoteclock-out" org-log-note-clock-out nil)
  3870. ("logrepeat" org-log-repeat state)
  3871. ("lognoterepeat" org-log-repeat note)
  3872. ("nologrepeat" org-log-repeat nil)
  3873. ("constcgs" constants-unit-system cgs)
  3874. ("constSI" constants-unit-system SI))
  3875. "Variable associated with STARTUP options for org-mode.
  3876. Each element is a list of three items: The startup options as written
  3877. in the #+STARTUP line, the corresponding variable, and the value to
  3878. set this variable to if the option is found. An optional forth element PUSH
  3879. means to push this value onto the list in the variable.")
  3880. (defun org-set-regexps-and-options ()
  3881. "Precompute regular expressions for current buffer."
  3882. (when (org-mode-p)
  3883. (org-set-local 'org-todo-kwd-alist nil)
  3884. (org-set-local 'org-todo-key-alist nil)
  3885. (org-set-local 'org-todo-key-trigger nil)
  3886. (org-set-local 'org-todo-keywords-1 nil)
  3887. (org-set-local 'org-done-keywords nil)
  3888. (org-set-local 'org-todo-heads nil)
  3889. (org-set-local 'org-todo-sets nil)
  3890. (org-set-local 'org-todo-log-states nil)
  3891. (let ((re (org-make-options-regexp
  3892. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3893. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3894. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3895. (splitre "[ \t]+")
  3896. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3897. tail sep kws1 prio props drawers)
  3898. (save-excursion
  3899. (save-restriction
  3900. (widen)
  3901. (goto-char (point-min))
  3902. (while (re-search-forward re nil t)
  3903. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3904. (cond
  3905. ((equal key "CATEGORY")
  3906. (if (string-match "[ \t]+$" value)
  3907. (setq value (replace-match "" t t value)))
  3908. (setq cat value))
  3909. ((member key '("SEQ_TODO" "TODO"))
  3910. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3911. ((equal key "TYP_TODO")
  3912. (push (cons 'type (org-split-string value splitre)) kwds))
  3913. ((equal key "TAGS")
  3914. (setq tags (append tags (org-split-string value splitre))))
  3915. ((equal key "COLUMNS")
  3916. (org-set-local 'org-columns-default-format value))
  3917. ((equal key "LINK")
  3918. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3919. (push (cons (match-string 1 value)
  3920. (org-trim (match-string 2 value)))
  3921. links)))
  3922. ((equal key "PRIORITIES")
  3923. (setq prio (org-split-string value " +")))
  3924. ((equal key "PROPERTY")
  3925. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3926. (push (cons (match-string 1 value) (match-string 2 value))
  3927. props)))
  3928. ((equal key "DRAWERS")
  3929. (setq drawers (org-split-string value splitre)))
  3930. ((equal key "CONSTANTS")
  3931. (setq const (append const (org-split-string value splitre))))
  3932. ((equal key "STARTUP")
  3933. (let ((opts (org-split-string value splitre))
  3934. l var val)
  3935. (while (setq l (pop opts))
  3936. (when (setq l (assoc l org-startup-options))
  3937. (setq var (nth 1 l) val (nth 2 l))
  3938. (if (not (nth 3 l))
  3939. (set (make-local-variable var) val)
  3940. (if (not (listp (symbol-value var)))
  3941. (set (make-local-variable var) nil))
  3942. (set (make-local-variable var) (symbol-value var))
  3943. (add-to-list var val))))))
  3944. ((equal key "ARCHIVE")
  3945. (string-match " *$" value)
  3946. (setq arch (replace-match "" t t value))
  3947. (remove-text-properties 0 (length arch)
  3948. '(face t fontified t) arch)))
  3949. )))
  3950. (when cat
  3951. (org-set-local 'org-category (intern cat))
  3952. (push (cons "CATEGORY" cat) props))
  3953. (when prio
  3954. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3955. (setq prio (mapcar 'string-to-char prio))
  3956. (org-set-local 'org-highest-priority (nth 0 prio))
  3957. (org-set-local 'org-lowest-priority (nth 1 prio))
  3958. (org-set-local 'org-default-priority (nth 2 prio)))
  3959. (and props (org-set-local 'org-local-properties (nreverse props)))
  3960. (and drawers (org-set-local 'org-drawers drawers))
  3961. (and arch (org-set-local 'org-archive-location arch))
  3962. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3963. ;; Process the TODO keywords
  3964. (unless kwds
  3965. ;; Use the global values as if they had been given locally.
  3966. (setq kwds (default-value 'org-todo-keywords))
  3967. (if (stringp (car kwds))
  3968. (setq kwds (list (cons org-todo-interpretation
  3969. (default-value 'org-todo-keywords)))))
  3970. (setq kwds (reverse kwds)))
  3971. (setq kwds (nreverse kwds))
  3972. (let (inter kws kw)
  3973. (while (setq kws (pop kwds))
  3974. (setq inter (pop kws) sep (member "|" kws)
  3975. kws0 (delete "|" (copy-sequence kws))
  3976. kwsa nil
  3977. kws1 (mapcar
  3978. (lambda (x)
  3979. ;; 1 2
  3980. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3981. (progn
  3982. (setq kw (match-string 1 x)
  3983. key (and (match-end 2) (match-string 2 x))
  3984. log (org-extract-log-state-settings x))
  3985. (push (cons kw (and key (string-to-char key))) kwsa)
  3986. (and log (push log org-todo-log-states))
  3987. kw)
  3988. (error "Invalid TODO keyword %s" x)))
  3989. kws0)
  3990. kwsa (if kwsa (append '((:startgroup))
  3991. (nreverse kwsa)
  3992. '((:endgroup))))
  3993. hw (car kws1)
  3994. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3995. tail (list inter hw (car dws) (org-last dws)))
  3996. (add-to-list 'org-todo-heads hw 'append)
  3997. (push kws1 org-todo-sets)
  3998. (setq org-done-keywords (append org-done-keywords dws nil))
  3999. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4000. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4001. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4002. (setq org-todo-sets (nreverse org-todo-sets)
  4003. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4004. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4005. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4006. ;; Process the constants
  4007. (when const
  4008. (let (e cst)
  4009. (while (setq e (pop const))
  4010. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4011. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4012. (setq org-table-formula-constants-local cst)))
  4013. ;; Process the tags.
  4014. (when tags
  4015. (let (e tgs)
  4016. (while (setq e (pop tags))
  4017. (cond
  4018. ((equal e "{") (push '(:startgroup) tgs))
  4019. ((equal e "}") (push '(:endgroup) tgs))
  4020. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4021. (push (cons (match-string 1 e)
  4022. (string-to-char (match-string 2 e)))
  4023. tgs))
  4024. (t (push (list e) tgs))))
  4025. (org-set-local 'org-tag-alist nil)
  4026. (while (setq e (pop tgs))
  4027. (or (and (stringp (car e))
  4028. (assoc (car e) org-tag-alist))
  4029. (push e org-tag-alist))))))
  4030. ;; Compute the regular expressions and other local variables
  4031. (if (not org-done-keywords)
  4032. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4033. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4034. (length org-scheduled-string)))
  4035. org-drawer-regexp
  4036. (concat "^[ \t]*:\\("
  4037. (mapconcat 'regexp-quote org-drawers "\\|")
  4038. "\\):[ \t]*$")
  4039. org-not-done-keywords
  4040. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4041. org-todo-regexp
  4042. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4043. "\\|") "\\)\\>")
  4044. org-not-done-regexp
  4045. (concat "\\<\\("
  4046. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4047. "\\)\\>")
  4048. org-todo-line-regexp
  4049. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4050. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4051. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4052. org-complex-heading-regexp
  4053. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4054. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4055. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4056. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4057. org-nl-done-regexp
  4058. (concat "\n\\*+[ \t]+"
  4059. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4060. "\\)" "\\>")
  4061. org-todo-line-tags-regexp
  4062. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4063. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4064. (org-re
  4065. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4066. org-looking-at-done-regexp
  4067. (concat "^" "\\(?:"
  4068. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4069. "\\>")
  4070. org-deadline-regexp (concat "\\<" org-deadline-string)
  4071. org-deadline-time-regexp
  4072. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4073. org-deadline-line-regexp
  4074. (concat "\\<\\(" org-deadline-string "\\).*")
  4075. org-scheduled-regexp
  4076. (concat "\\<" org-scheduled-string)
  4077. org-scheduled-time-regexp
  4078. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4079. org-closed-time-regexp
  4080. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4081. org-keyword-time-regexp
  4082. (concat "\\<\\(" org-scheduled-string
  4083. "\\|" org-deadline-string
  4084. "\\|" org-closed-string
  4085. "\\|" org-clock-string "\\)"
  4086. " *[[<]\\([^]>]+\\)[]>]")
  4087. org-keyword-time-not-clock-regexp
  4088. (concat "\\<\\(" org-scheduled-string
  4089. "\\|" org-deadline-string
  4090. "\\|" org-closed-string
  4091. "\\)"
  4092. " *[[<]\\([^]>]+\\)[]>]")
  4093. org-maybe-keyword-time-regexp
  4094. (concat "\\(\\<\\(" org-scheduled-string
  4095. "\\|" org-deadline-string
  4096. "\\|" org-closed-string
  4097. "\\|" org-clock-string "\\)\\)?"
  4098. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4099. org-planning-or-clock-line-re
  4100. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4101. "\\|" org-deadline-string
  4102. "\\|" org-closed-string "\\|" org-clock-string
  4103. "\\)\\>\\)")
  4104. )
  4105. (org-compute-latex-and-specials-regexp)
  4106. (org-set-font-lock-defaults)))
  4107. (defun org-extract-log-state-settings (x)
  4108. "Extract the log state setting from a TODO keyword string.
  4109. This will extract info from a string like \"WAIT(w@/!)\"."
  4110. (let (kw key log1 log2)
  4111. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4112. (setq kw (match-string 1 x)
  4113. key (and (match-end 2) (match-string 2 x))
  4114. log1 (and (match-end 3) (match-string 3 x))
  4115. log2 (and (match-end 4) (match-string 4 x)))
  4116. (and (or log1 log2)
  4117. (list kw
  4118. (and log1 (if (equal log1 "!") 'time 'note))
  4119. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4120. (defun org-remove-keyword-keys (list)
  4121. "Remove a pair of parenthesis at the end of each string in LIST."
  4122. (mapcar (lambda (x)
  4123. (if (string-match "(.*)$" x)
  4124. (substring x 0 (match-beginning 0))
  4125. x))
  4126. list))
  4127. ;; FIXME: this could be done much better, using second characters etc.
  4128. (defun org-assign-fast-keys (alist)
  4129. "Assign fast keys to a keyword-key alist.
  4130. Respect keys that are already there."
  4131. (let (new e k c c1 c2 (char ?a))
  4132. (while (setq e (pop alist))
  4133. (cond
  4134. ((equal e '(:startgroup)) (push e new))
  4135. ((equal e '(:endgroup)) (push e new))
  4136. (t
  4137. (setq k (car e) c2 nil)
  4138. (if (cdr e)
  4139. (setq c (cdr e))
  4140. ;; automatically assign a character.
  4141. (setq c1 (string-to-char
  4142. (downcase (substring
  4143. k (if (= (string-to-char k) ?@) 1 0)))))
  4144. (if (or (rassoc c1 new) (rassoc c1 alist))
  4145. (while (or (rassoc char new) (rassoc char alist))
  4146. (setq char (1+ char)))
  4147. (setq c2 c1))
  4148. (setq c (or c2 char)))
  4149. (push (cons k c) new))))
  4150. (nreverse new)))
  4151. ;;; Some variables ujsed in various places
  4152. (defvar org-window-configuration nil
  4153. "Used in various places to store a window configuration.")
  4154. (defvar org-finish-function nil
  4155. "Function to be called when `C-c C-c' is used.
  4156. This is for getting out of special buffers like remember.")
  4157. ;; FIXME: Occasionally check by commenting these, to make sure
  4158. ;; no other functions uses these, forgetting to let-bind them.
  4159. (defvar entry)
  4160. (defvar state)
  4161. (defvar last-state)
  4162. (defvar date)
  4163. (defvar description)
  4164. ;; Defined somewhere in this file, but used before definition.
  4165. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4166. (defvar org-agenda-buffer-name)
  4167. (defvar org-agenda-undo-list)
  4168. (defvar org-agenda-pending-undo-list)
  4169. (defvar org-agenda-overriding-header)
  4170. (defvar orgtbl-mode)
  4171. (defvar org-html-entities)
  4172. (defvar org-struct-menu)
  4173. (defvar org-org-menu)
  4174. (defvar org-tbl-menu)
  4175. (defvar org-agenda-keymap)
  4176. ;;;; Emacs/XEmacs compatibility
  4177. ;; Overlay compatibility functions
  4178. (defun org-make-overlay (beg end &optional buffer)
  4179. (if (featurep 'xemacs)
  4180. (make-extent beg end buffer)
  4181. (make-overlay beg end buffer)))
  4182. (defun org-delete-overlay (ovl)
  4183. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4184. (defun org-detach-overlay (ovl)
  4185. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4186. (defun org-move-overlay (ovl beg end &optional buffer)
  4187. (if (featurep 'xemacs)
  4188. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4189. (move-overlay ovl beg end buffer)))
  4190. (defun org-overlay-put (ovl prop value)
  4191. (if (featurep 'xemacs)
  4192. (set-extent-property ovl prop value)
  4193. (overlay-put ovl prop value)))
  4194. (defun org-overlay-display (ovl text &optional face evap)
  4195. "Make overlay OVL display TEXT with face FACE."
  4196. (if (featurep 'xemacs)
  4197. (let ((gl (make-glyph text)))
  4198. (and face (set-glyph-face gl face))
  4199. (set-extent-property ovl 'invisible t)
  4200. (set-extent-property ovl 'end-glyph gl))
  4201. (overlay-put ovl 'display text)
  4202. (if face (overlay-put ovl 'face face))
  4203. (if evap (overlay-put ovl 'evaporate t))))
  4204. (defun org-overlay-before-string (ovl text &optional face evap)
  4205. "Make overlay OVL display TEXT with face FACE."
  4206. (if (featurep 'xemacs)
  4207. (let ((gl (make-glyph text)))
  4208. (and face (set-glyph-face gl face))
  4209. (set-extent-property ovl 'begin-glyph gl))
  4210. (if face (org-add-props text nil 'face face))
  4211. (overlay-put ovl 'before-string text)
  4212. (if evap (overlay-put ovl 'evaporate t))))
  4213. (defun org-overlay-get (ovl prop)
  4214. (if (featurep 'xemacs)
  4215. (extent-property ovl prop)
  4216. (overlay-get ovl prop)))
  4217. (defun org-overlays-at (pos)
  4218. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4219. (defun org-overlays-in (&optional start end)
  4220. (if (featurep 'xemacs)
  4221. (extent-list nil start end)
  4222. (overlays-in start end)))
  4223. (defun org-overlay-start (o)
  4224. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4225. (defun org-overlay-end (o)
  4226. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4227. (defun org-find-overlays (prop &optional pos delete)
  4228. "Find all overlays specifying PROP at POS or point.
  4229. If DELETE is non-nil, delete all those overlays."
  4230. (let ((overlays (org-overlays-at (or pos (point))))
  4231. ov found)
  4232. (while (setq ov (pop overlays))
  4233. (if (org-overlay-get ov prop)
  4234. (if delete (org-delete-overlay ov) (push ov found))))
  4235. found))
  4236. ;; Region compatibility
  4237. (defun org-add-hook (hook function &optional append local)
  4238. "Add-hook, compatible with both Emacsen."
  4239. (if (and local (featurep 'xemacs))
  4240. (add-local-hook hook function append)
  4241. (add-hook hook function append local)))
  4242. (defvar org-ignore-region nil
  4243. "To temporarily disable the active region.")
  4244. (defun org-region-active-p ()
  4245. "Is `transient-mark-mode' on and the region active?
  4246. Works on both Emacs and XEmacs."
  4247. (if org-ignore-region
  4248. nil
  4249. (if (featurep 'xemacs)
  4250. (and zmacs-regions (region-active-p))
  4251. (if (fboundp 'use-region-p)
  4252. (use-region-p)
  4253. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4254. ;; Invisibility compatibility
  4255. (defun org-add-to-invisibility-spec (arg)
  4256. "Add elements to `buffer-invisibility-spec'.
  4257. See documentation for `buffer-invisibility-spec' for the kind of elements
  4258. that can be added."
  4259. (cond
  4260. ((fboundp 'add-to-invisibility-spec)
  4261. (add-to-invisibility-spec arg))
  4262. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4263. (setq buffer-invisibility-spec (list arg)))
  4264. (t
  4265. (setq buffer-invisibility-spec
  4266. (cons arg buffer-invisibility-spec)))))
  4267. (defun org-remove-from-invisibility-spec (arg)
  4268. "Remove elements from `buffer-invisibility-spec'."
  4269. (if (fboundp 'remove-from-invisibility-spec)
  4270. (remove-from-invisibility-spec arg)
  4271. (if (consp buffer-invisibility-spec)
  4272. (setq buffer-invisibility-spec
  4273. (delete arg buffer-invisibility-spec)))))
  4274. (defun org-in-invisibility-spec-p (arg)
  4275. "Is ARG a member of `buffer-invisibility-spec'?"
  4276. (if (consp buffer-invisibility-spec)
  4277. (member arg buffer-invisibility-spec)
  4278. nil))
  4279. ;;;; Define the Org-mode
  4280. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4281. (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."))
  4282. ;; We use a before-change function to check if a table might need
  4283. ;; an update.
  4284. (defvar org-table-may-need-update t
  4285. "Indicates that a table might need an update.
  4286. This variable is set by `org-before-change-function'.
  4287. `org-table-align' sets it back to nil.")
  4288. (defvar org-mode-map)
  4289. (defvar org-mode-hook nil)
  4290. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4291. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4292. (defvar org-table-buffer-is-an nil)
  4293. (defconst org-outline-regexp "\\*+ ")
  4294. ;;;###autoload
  4295. (define-derived-mode org-mode outline-mode "Org"
  4296. "Outline-based notes management and organizer, alias
  4297. \"Carsten's outline-mode for keeping track of everything.\"
  4298. Org-mode develops organizational tasks around a NOTES file which
  4299. contains information about projects as plain text. Org-mode is
  4300. implemented on top of outline-mode, which is ideal to keep the content
  4301. of large files well structured. It supports ToDo items, deadlines and
  4302. time stamps, which magically appear in the diary listing of the Emacs
  4303. calendar. Tables are easily created with a built-in table editor.
  4304. Plain text URL-like links connect to websites, emails (VM), Usenet
  4305. messages (Gnus), BBDB entries, and any files related to the project.
  4306. For printing and sharing of notes, an Org-mode file (or a part of it)
  4307. can be exported as a structured ASCII or HTML file.
  4308. The following commands are available:
  4309. \\{org-mode-map}"
  4310. ;; Get rid of Outline menus, they are not needed
  4311. ;; Need to do this here because define-derived-mode sets up
  4312. ;; the keymap so late. Still, it is a waste to call this each time
  4313. ;; we switch another buffer into org-mode.
  4314. (if (featurep 'xemacs)
  4315. (when (boundp 'outline-mode-menu-heading)
  4316. ;; Assume this is Greg's port, it used easymenu
  4317. (easy-menu-remove outline-mode-menu-heading)
  4318. (easy-menu-remove outline-mode-menu-show)
  4319. (easy-menu-remove outline-mode-menu-hide))
  4320. (define-key org-mode-map [menu-bar headings] 'undefined)
  4321. (define-key org-mode-map [menu-bar hide] 'undefined)
  4322. (define-key org-mode-map [menu-bar show] 'undefined))
  4323. (easy-menu-add org-org-menu)
  4324. (easy-menu-add org-tbl-menu)
  4325. (org-install-agenda-files-menu)
  4326. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4327. (org-add-to-invisibility-spec '(org-cwidth))
  4328. (when (featurep 'xemacs)
  4329. (org-set-local 'line-move-ignore-invisible t))
  4330. (org-set-local 'outline-regexp org-outline-regexp)
  4331. (org-set-local 'outline-level 'org-outline-level)
  4332. (when (and org-ellipsis
  4333. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4334. (fboundp 'make-glyph-code))
  4335. (unless org-display-table
  4336. (setq org-display-table (make-display-table)))
  4337. (set-display-table-slot
  4338. org-display-table 4
  4339. (vconcat (mapcar
  4340. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4341. org-ellipsis)))
  4342. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4343. (setq buffer-display-table org-display-table))
  4344. (org-set-regexps-and-options)
  4345. ;; Calc embedded
  4346. (org-set-local 'calc-embedded-open-mode "# ")
  4347. (modify-syntax-entry ?# "<")
  4348. (modify-syntax-entry ?@ "w")
  4349. (if org-startup-truncated (setq truncate-lines t))
  4350. (org-set-local 'font-lock-unfontify-region-function
  4351. 'org-unfontify-region)
  4352. ;; Activate before-change-function
  4353. (org-set-local 'org-table-may-need-update t)
  4354. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4355. 'local)
  4356. ;; Check for running clock before killing a buffer
  4357. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4358. ;; Paragraphs and auto-filling
  4359. (org-set-autofill-regexps)
  4360. (setq indent-line-function 'org-indent-line-function)
  4361. (org-update-radio-target-regexp)
  4362. ;; Comment characters
  4363. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4364. (org-set-local 'comment-padding " ")
  4365. ;; Align options lines
  4366. (org-set-local
  4367. 'align-mode-rules-list
  4368. '((org-in-buffer-settings
  4369. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4370. (modes . '(org-mode)))))
  4371. ;; Imenu
  4372. (org-set-local 'imenu-create-index-function
  4373. 'org-imenu-get-tree)
  4374. ;; Make isearch reveal context
  4375. (if (or (featurep 'xemacs)
  4376. (not (boundp 'outline-isearch-open-invisible-function)))
  4377. ;; Emacs 21 and XEmacs make use of the hook
  4378. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4379. ;; Emacs 22 deals with this through a special variable
  4380. (org-set-local 'outline-isearch-open-invisible-function
  4381. (lambda (&rest ignore) (org-show-context 'isearch))))
  4382. ;; If empty file that did not turn on org-mode automatically, make it to.
  4383. (if (and org-insert-mode-line-in-empty-file
  4384. (interactive-p)
  4385. (= (point-min) (point-max)))
  4386. (insert "# -*- mode: org -*-\n\n"))
  4387. (unless org-inhibit-startup
  4388. (when org-startup-align-all-tables
  4389. (let ((bmp (buffer-modified-p)))
  4390. (org-table-map-tables 'org-table-align)
  4391. (set-buffer-modified-p bmp)))
  4392. (org-cycle-hide-drawers 'all)
  4393. (cond
  4394. ((eq org-startup-folded t)
  4395. (org-cycle '(4)))
  4396. ((eq org-startup-folded 'content)
  4397. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4398. (org-cycle '(4)) (org-cycle '(4)))))))
  4399. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4400. (defsubst org-call-with-arg (command arg)
  4401. "Call COMMAND interactively, but pretend prefix are was ARG."
  4402. (let ((current-prefix-arg arg)) (call-interactively command)))
  4403. (defsubst org-current-line (&optional pos)
  4404. (save-excursion
  4405. (and pos (goto-char pos))
  4406. ;; works also in narrowed buffer, because we start at 1, not point-min
  4407. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4408. (defun org-current-time ()
  4409. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4410. (if (> org-time-stamp-rounding-minutes 0)
  4411. (let ((r org-time-stamp-rounding-minutes)
  4412. (time (decode-time)))
  4413. (apply 'encode-time
  4414. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4415. (nthcdr 2 time))))
  4416. (current-time)))
  4417. (defun org-add-props (string plist &rest props)
  4418. "Add text properties to entire string, from beginning to end.
  4419. PLIST may be a list of properties, PROPS are individual properties and values
  4420. that will be added to PLIST. Returns the string that was modified."
  4421. (add-text-properties
  4422. 0 (length string) (if props (append plist props) plist) string)
  4423. string)
  4424. (put 'org-add-props 'lisp-indent-function 2)
  4425. ;;;; Font-Lock stuff, including the activators
  4426. (defvar org-mouse-map (make-sparse-keymap))
  4427. (org-defkey org-mouse-map
  4428. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4429. (org-defkey org-mouse-map
  4430. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4431. (when org-mouse-1-follows-link
  4432. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4433. (when org-tab-follows-link
  4434. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4435. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4436. (when org-return-follows-link
  4437. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4438. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4439. (require 'font-lock)
  4440. (defconst org-non-link-chars "]\t\n\r<>")
  4441. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4442. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4443. (defvar org-link-re-with-space nil
  4444. "Matches a link with spaces, optional angular brackets around it.")
  4445. (defvar org-link-re-with-space2 nil
  4446. "Matches a link with spaces, optional angular brackets around it.")
  4447. (defvar org-angle-link-re nil
  4448. "Matches link with angular brackets, spaces are allowed.")
  4449. (defvar org-plain-link-re nil
  4450. "Matches plain link, without spaces.")
  4451. (defvar org-bracket-link-regexp nil
  4452. "Matches a link in double brackets.")
  4453. (defvar org-bracket-link-analytic-regexp nil
  4454. "Regular expression used to analyze links.
  4455. Here is what the match groups contain after a match:
  4456. 1: http:
  4457. 2: http
  4458. 3: path
  4459. 4: [desc]
  4460. 5: desc")
  4461. (defvar org-any-link-re nil
  4462. "Regular expression matching any link.")
  4463. (defun org-make-link-regexps ()
  4464. "Update the link regular expressions.
  4465. This should be called after the variable `org-link-types' has changed."
  4466. (setq org-link-re-with-space
  4467. (concat
  4468. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4469. "\\([^" org-non-link-chars " ]"
  4470. "[^" org-non-link-chars "]*"
  4471. "[^" org-non-link-chars " ]\\)>?")
  4472. org-link-re-with-space2
  4473. (concat
  4474. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4475. "\\([^" org-non-link-chars " ]"
  4476. "[^]\t\n\r]*"
  4477. "[^" org-non-link-chars " ]\\)>?")
  4478. org-angle-link-re
  4479. (concat
  4480. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4481. "\\([^" org-non-link-chars " ]"
  4482. "[^" org-non-link-chars "]*"
  4483. "\\)>")
  4484. org-plain-link-re
  4485. (concat
  4486. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4487. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4488. org-bracket-link-regexp
  4489. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4490. org-bracket-link-analytic-regexp
  4491. (concat
  4492. "\\[\\["
  4493. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4494. "\\([^]]+\\)"
  4495. "\\]"
  4496. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4497. "\\]")
  4498. org-any-link-re
  4499. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4500. org-angle-link-re "\\)\\|\\("
  4501. org-plain-link-re "\\)")))
  4502. (org-make-link-regexps)
  4503. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4504. "Regular expression for fast time stamp matching.")
  4505. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4506. "Regular expression for fast time stamp matching.")
  4507. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4508. "Regular expression matching time strings for analysis.
  4509. This one does not require the space after the date.")
  4510. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4511. "Regular expression matching time strings for analysis.")
  4512. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4513. "Regular expression matching time stamps, with groups.")
  4514. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4515. "Regular expression matching time stamps (also [..]), with groups.")
  4516. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4517. "Regular expression matching a time stamp range.")
  4518. (defconst org-tr-regexp-both
  4519. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4520. "Regular expression matching a time stamp range.")
  4521. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4522. org-ts-regexp "\\)?")
  4523. "Regular expression matching a time stamp or time stamp range.")
  4524. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4525. org-ts-regexp-both "\\)?")
  4526. "Regular expression matching a time stamp or time stamp range.
  4527. The time stamps may be either active or inactive.")
  4528. (defvar org-emph-face nil)
  4529. (defun org-do-emphasis-faces (limit)
  4530. "Run through the buffer and add overlays to links."
  4531. (let (rtn)
  4532. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4533. (if (not (= (char-after (match-beginning 3))
  4534. (char-after (match-beginning 4))))
  4535. (progn
  4536. (setq rtn t)
  4537. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4538. 'face
  4539. (nth 1 (assoc (match-string 3)
  4540. org-emphasis-alist)))
  4541. (add-text-properties (match-beginning 2) (match-end 2)
  4542. '(font-lock-multiline t))
  4543. (when org-hide-emphasis-markers
  4544. (add-text-properties (match-end 4) (match-beginning 5)
  4545. '(invisible org-link))
  4546. (add-text-properties (match-beginning 3) (match-end 3)
  4547. '(invisible org-link)))))
  4548. (backward-char 1))
  4549. rtn))
  4550. (defun org-emphasize (&optional char)
  4551. "Insert or change an emphasis, i.e. a font like bold or italic.
  4552. If there is an active region, change that region to a new emphasis.
  4553. If there is no region, just insert the marker characters and position
  4554. the cursor between them.
  4555. CHAR should be either the marker character, or the first character of the
  4556. HTML tag associated with that emphasis. If CHAR is a space, the means
  4557. to remove the emphasis of the selected region.
  4558. If char is not given (for example in an interactive call) it
  4559. will be prompted for."
  4560. (interactive)
  4561. (let ((eal org-emphasis-alist) e det
  4562. (erc org-emphasis-regexp-components)
  4563. (prompt "")
  4564. (string "") beg end move tag c s)
  4565. (if (org-region-active-p)
  4566. (setq beg (region-beginning) end (region-end)
  4567. string (buffer-substring beg end))
  4568. (setq move t))
  4569. (while (setq e (pop eal))
  4570. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4571. c (aref tag 0))
  4572. (push (cons c (string-to-char (car e))) det)
  4573. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4574. (substring tag 1)))))
  4575. (unless char
  4576. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4577. (setq char (read-char-exclusive)))
  4578. (setq char (or (cdr (assoc char det)) char))
  4579. (if (equal char ?\ )
  4580. (setq s "" move nil)
  4581. (unless (assoc (char-to-string char) org-emphasis-alist)
  4582. (error "No such emphasis marker: \"%c\"" char))
  4583. (setq s (char-to-string char)))
  4584. (while (and (> (length string) 1)
  4585. (equal (substring string 0 1) (substring string -1))
  4586. (assoc (substring string 0 1) org-emphasis-alist))
  4587. (setq string (substring string 1 -1)))
  4588. (setq string (concat s string s))
  4589. (if beg (delete-region beg end))
  4590. (unless (or (bolp)
  4591. (string-match (concat "[" (nth 0 erc) "\n]")
  4592. (char-to-string (char-before (point)))))
  4593. (insert " "))
  4594. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4595. (char-to-string (char-after (point))))
  4596. (insert " ") (backward-char 1))
  4597. (insert string)
  4598. (and move (backward-char 1))))
  4599. (defconst org-nonsticky-props
  4600. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4601. (defun org-activate-plain-links (limit)
  4602. "Run through the buffer and add overlays to links."
  4603. (catch 'exit
  4604. (let (f)
  4605. (while (re-search-forward org-plain-link-re limit t)
  4606. (setq f (get-text-property (match-beginning 0) 'face))
  4607. (if (or (eq f 'org-tag)
  4608. (and (listp f) (memq 'org-tag f)))
  4609. nil
  4610. (add-text-properties (match-beginning 0) (match-end 0)
  4611. (list 'mouse-face 'highlight
  4612. 'rear-nonsticky org-nonsticky-props
  4613. 'keymap org-mouse-map
  4614. ))
  4615. (throw 'exit t))))))
  4616. (defun org-activate-code (limit)
  4617. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4618. (unless (get-text-property (match-beginning 1) 'face)
  4619. (remove-text-properties (match-beginning 0) (match-end 0)
  4620. '(display t invisible t intangible t))
  4621. t)))
  4622. (defun org-activate-angle-links (limit)
  4623. "Run through the buffer and add overlays to links."
  4624. (if (re-search-forward org-angle-link-re limit t)
  4625. (progn
  4626. (add-text-properties (match-beginning 0) (match-end 0)
  4627. (list 'mouse-face 'highlight
  4628. 'rear-nonsticky org-nonsticky-props
  4629. 'keymap org-mouse-map
  4630. ))
  4631. t)))
  4632. (defmacro org-maybe-intangible (props)
  4633. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4634. In emacs 21, invisible text is not avoided by the command loop, so the
  4635. intangible property is needed to make sure point skips this text.
  4636. In Emacs 22, this is not necessary. The intangible text property has
  4637. led to problems with flyspell. These problems are fixed in flyspell.el,
  4638. but we still avoid setting the property in Emacs 22 and later.
  4639. We use a macro so that the test can happen at compilation time."
  4640. (if (< emacs-major-version 22)
  4641. `(append '(intangible t) ,props)
  4642. props))
  4643. (defun org-activate-bracket-links (limit)
  4644. "Run through the buffer and add overlays to bracketed links."
  4645. (if (re-search-forward org-bracket-link-regexp limit t)
  4646. (let* ((help (concat "LINK: "
  4647. (org-match-string-no-properties 1)))
  4648. ;; FIXME: above we should remove the escapes.
  4649. ;; but that requires another match, protecting match data,
  4650. ;; a lot of overhead for font-lock.
  4651. (ip (org-maybe-intangible
  4652. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4653. 'keymap org-mouse-map 'mouse-face 'highlight
  4654. 'font-lock-multiline t 'help-echo help)))
  4655. (vp (list 'rear-nonsticky org-nonsticky-props
  4656. 'keymap org-mouse-map 'mouse-face 'highlight
  4657. ' font-lock-multiline t 'help-echo help)))
  4658. ;; We need to remove the invisible property here. Table narrowing
  4659. ;; may have made some of this invisible.
  4660. (remove-text-properties (match-beginning 0) (match-end 0)
  4661. '(invisible nil))
  4662. (if (match-end 3)
  4663. (progn
  4664. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4665. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4666. (add-text-properties (match-end 3) (match-end 0) ip))
  4667. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4668. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4669. (add-text-properties (match-end 1) (match-end 0) ip))
  4670. t)))
  4671. (defun org-activate-dates (limit)
  4672. "Run through the buffer and add overlays to dates."
  4673. (if (re-search-forward org-tsr-regexp-both limit t)
  4674. (progn
  4675. (add-text-properties (match-beginning 0) (match-end 0)
  4676. (list 'mouse-face 'highlight
  4677. 'rear-nonsticky org-nonsticky-props
  4678. 'keymap org-mouse-map))
  4679. (when org-display-custom-times
  4680. (if (match-end 3)
  4681. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4682. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4683. t)))
  4684. (defvar org-target-link-regexp nil
  4685. "Regular expression matching radio targets in plain text.")
  4686. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4687. "Regular expression matching a link target.")
  4688. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4689. "Regular expression matching a radio target.")
  4690. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4691. "Regular expression matching any target.")
  4692. (defun org-activate-target-links (limit)
  4693. "Run through the buffer and add overlays to target matches."
  4694. (when org-target-link-regexp
  4695. (let ((case-fold-search t))
  4696. (if (re-search-forward org-target-link-regexp limit t)
  4697. (progn
  4698. (add-text-properties (match-beginning 0) (match-end 0)
  4699. (list 'mouse-face 'highlight
  4700. 'rear-nonsticky org-nonsticky-props
  4701. 'keymap org-mouse-map
  4702. 'help-echo "Radio target link"
  4703. 'org-linked-text t))
  4704. t)))))
  4705. (defun org-update-radio-target-regexp ()
  4706. "Find all radio targets in this file and update the regular expression."
  4707. (interactive)
  4708. (when (memq 'radio org-activate-links)
  4709. (setq org-target-link-regexp
  4710. (org-make-target-link-regexp (org-all-targets 'radio)))
  4711. (org-restart-font-lock)))
  4712. (defun org-hide-wide-columns (limit)
  4713. (let (s e)
  4714. (setq s (text-property-any (point) (or limit (point-max))
  4715. 'org-cwidth t))
  4716. (when s
  4717. (setq e (next-single-property-change s 'org-cwidth))
  4718. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4719. (goto-char e)
  4720. t)))
  4721. (defvar org-latex-and-specials-regexp nil
  4722. "Regular expression for highlighting export special stuff.")
  4723. (defvar org-match-substring-regexp)
  4724. (defvar org-match-substring-with-braces-regexp)
  4725. (defvar org-export-html-special-string-regexps)
  4726. (defun org-compute-latex-and-specials-regexp ()
  4727. "Compute regular expression for stuff treated specially by exporters."
  4728. (if (not org-highlight-latex-fragments-and-specials)
  4729. (org-set-local 'org-latex-and-specials-regexp nil)
  4730. (let*
  4731. ((matchers (plist-get org-format-latex-options :matchers))
  4732. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4733. org-latex-regexps)))
  4734. (options (org-combine-plists (org-default-export-plist)
  4735. (org-infile-export-plist)))
  4736. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4737. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4738. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4739. (org-export-html-expand (plist-get options :expand-quoted-html))
  4740. (org-export-with-special-strings (plist-get options :special-strings))
  4741. (re-sub
  4742. (cond
  4743. ((equal org-export-with-sub-superscripts '{})
  4744. (list org-match-substring-with-braces-regexp))
  4745. (org-export-with-sub-superscripts
  4746. (list org-match-substring-regexp))
  4747. (t nil)))
  4748. (re-latex
  4749. (if org-export-with-LaTeX-fragments
  4750. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4751. (re-macros
  4752. (if org-export-with-TeX-macros
  4753. (list (concat "\\\\"
  4754. (regexp-opt
  4755. (append (mapcar 'car org-html-entities)
  4756. (if (boundp 'org-latex-entities)
  4757. org-latex-entities nil))
  4758. 'words))) ; FIXME
  4759. ))
  4760. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4761. (re-special (if org-export-with-special-strings
  4762. (mapcar (lambda (x) (car x))
  4763. org-export-html-special-string-regexps)))
  4764. (re-rest
  4765. (delq nil
  4766. (list
  4767. (if org-export-html-expand "@<[^>\n]+>")
  4768. ))))
  4769. (org-set-local
  4770. 'org-latex-and-specials-regexp
  4771. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4772. re-rest) "\\|")))))
  4773. (defface org-latex-and-export-specials
  4774. (let ((font (cond ((assq :inherit custom-face-attributes)
  4775. '(:inherit underline))
  4776. (t '(:underline t)))))
  4777. `((((class grayscale) (background light))
  4778. (:foreground "DimGray" ,@font))
  4779. (((class grayscale) (background dark))
  4780. (:foreground "LightGray" ,@font))
  4781. (((class color) (background light))
  4782. (:foreground "SaddleBrown"))
  4783. (((class color) (background dark))
  4784. (:foreground "burlywood"))
  4785. (t (,@font))))
  4786. "Face used to highlight math latex and other special exporter stuff."
  4787. :group 'org-faces)
  4788. (defun org-do-latex-and-special-faces (limit)
  4789. "Run through the buffer and add overlays to links."
  4790. (when org-latex-and-specials-regexp
  4791. (let (rtn d)
  4792. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4793. limit t))
  4794. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4795. 'face))
  4796. '(org-code org-verbatim underline)))
  4797. (progn
  4798. (setq rtn t
  4799. d (cond ((member (char-after (1+ (match-beginning 0)))
  4800. '(?_ ?^)) 1)
  4801. (t 0)))
  4802. (font-lock-prepend-text-property
  4803. (+ d (match-beginning 0)) (match-end 0)
  4804. 'face 'org-latex-and-export-specials)
  4805. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4806. '(font-lock-multiline t)))))
  4807. rtn)))
  4808. (defun org-restart-font-lock ()
  4809. "Restart font-lock-mode, to force refontification."
  4810. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4811. (font-lock-mode -1)
  4812. (font-lock-mode 1)))
  4813. (defun org-all-targets (&optional radio)
  4814. "Return a list of all targets in this file.
  4815. With optional argument RADIO, only find radio targets."
  4816. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4817. rtn)
  4818. (save-excursion
  4819. (goto-char (point-min))
  4820. (while (re-search-forward re nil t)
  4821. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4822. rtn)))
  4823. (defun org-make-target-link-regexp (targets)
  4824. "Make regular expression matching all strings in TARGETS.
  4825. The regular expression finds the targets also if there is a line break
  4826. between words."
  4827. (and targets
  4828. (concat
  4829. "\\<\\("
  4830. (mapconcat
  4831. (lambda (x)
  4832. (while (string-match " +" x)
  4833. (setq x (replace-match "\\s-+" t t x)))
  4834. x)
  4835. targets
  4836. "\\|")
  4837. "\\)\\>")))
  4838. (defun org-activate-tags (limit)
  4839. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4840. (progn
  4841. (add-text-properties (match-beginning 1) (match-end 1)
  4842. (list 'mouse-face 'highlight
  4843. 'rear-nonsticky org-nonsticky-props
  4844. 'keymap org-mouse-map))
  4845. t)))
  4846. (defun org-outline-level ()
  4847. (save-excursion
  4848. (looking-at outline-regexp)
  4849. (if (match-beginning 1)
  4850. (+ (org-get-string-indentation (match-string 1)) 1000)
  4851. (1- (- (match-end 0) (match-beginning 0))))))
  4852. (defvar org-font-lock-keywords nil)
  4853. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4854. "Regular expression matching a property line.")
  4855. (defun org-set-font-lock-defaults ()
  4856. (let* ((em org-fontify-emphasized-text)
  4857. (lk org-activate-links)
  4858. (org-font-lock-extra-keywords
  4859. (list
  4860. ;; Headlines
  4861. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4862. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4863. ;; Table lines
  4864. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4865. (1 'org-table t))
  4866. ;; Table internals
  4867. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4868. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4869. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4870. ;; Drawers
  4871. (list org-drawer-regexp '(0 'org-special-keyword t))
  4872. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4873. ;; Properties
  4874. (list org-property-re
  4875. '(1 'org-special-keyword t)
  4876. '(3 'org-property-value t))
  4877. (if org-format-transports-properties-p
  4878. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4879. ;; Links
  4880. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4881. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4882. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4883. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4884. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4885. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4886. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4887. '(org-hide-wide-columns (0 nil append))
  4888. ;; TODO lines
  4889. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4890. '(1 (org-get-todo-face 1) t))
  4891. ;; DONE
  4892. (if org-fontify-done-headline
  4893. (list (concat "^[*]+ +\\<\\("
  4894. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4895. "\\)\\(.*\\)")
  4896. '(2 'org-headline-done t))
  4897. nil)
  4898. ;; Priorities
  4899. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4900. ;; Special keywords
  4901. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4902. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4903. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4904. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4905. ;; Emphasis
  4906. (if em
  4907. (if (featurep 'xemacs)
  4908. '(org-do-emphasis-faces (0 nil append))
  4909. '(org-do-emphasis-faces)))
  4910. ;; Checkboxes
  4911. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4912. 2 'bold prepend)
  4913. (if org-provide-checkbox-statistics
  4914. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4915. (0 (org-get-checkbox-statistics-face) t)))
  4916. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4917. '(1 'org-archived prepend))
  4918. ;; Specials
  4919. '(org-do-latex-and-special-faces)
  4920. ;; Code
  4921. '(org-activate-code (1 'org-code t))
  4922. ;; COMMENT
  4923. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4924. "\\|" org-quote-string "\\)\\>")
  4925. '(1 'org-special-keyword t))
  4926. '("^#.*" (0 'font-lock-comment-face t))
  4927. )))
  4928. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4929. ;; Now set the full font-lock-keywords
  4930. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4931. (org-set-local 'font-lock-defaults
  4932. '(org-font-lock-keywords t nil nil backward-paragraph))
  4933. (kill-local-variable 'font-lock-keywords) nil))
  4934. (defvar org-m nil)
  4935. (defvar org-l nil)
  4936. (defvar org-f nil)
  4937. (defun org-get-level-face (n)
  4938. "Get the right face for match N in font-lock matching of healdines."
  4939. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4940. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4941. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4942. (cond
  4943. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4944. ((eq n 2) org-f)
  4945. (t (if org-level-color-stars-only nil org-f))))
  4946. (defun org-get-todo-face (kwd)
  4947. "Get the right face for a TODO keyword KWD.
  4948. If KWD is a number, get the corresponding match group."
  4949. (if (numberp kwd) (setq kwd (match-string kwd)))
  4950. (or (cdr (assoc kwd org-todo-keyword-faces))
  4951. (and (member kwd org-done-keywords) 'org-done)
  4952. 'org-todo))
  4953. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4954. "Remove fontification and activation overlays from links."
  4955. (font-lock-default-unfontify-region beg end)
  4956. (let* ((buffer-undo-list t)
  4957. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4958. (inhibit-modification-hooks t)
  4959. deactivate-mark buffer-file-name buffer-file-truename)
  4960. (remove-text-properties beg end
  4961. '(mouse-face t keymap t org-linked-text t
  4962. invisible t intangible t))))
  4963. ;;;; Visibility cycling, including org-goto and indirect buffer
  4964. ;;; Cycling
  4965. (defvar org-cycle-global-status nil)
  4966. (make-variable-buffer-local 'org-cycle-global-status)
  4967. (defvar org-cycle-subtree-status nil)
  4968. (make-variable-buffer-local 'org-cycle-subtree-status)
  4969. ;;;###autoload
  4970. (defun org-cycle (&optional arg)
  4971. "Visibility cycling for Org-mode.
  4972. - When this function is called with a prefix argument, rotate the entire
  4973. buffer through 3 states (global cycling)
  4974. 1. OVERVIEW: Show only top-level headlines.
  4975. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4976. 3. SHOW ALL: Show everything.
  4977. - When point is at the beginning of a headline, rotate the subtree started
  4978. by this line through 3 different states (local cycling)
  4979. 1. FOLDED: Only the main headline is shown.
  4980. 2. CHILDREN: The main headline and the direct children are shown.
  4981. From this state, you can move to one of the children
  4982. and zoom in further.
  4983. 3. SUBTREE: Show the entire subtree, including body text.
  4984. - When there is a numeric prefix, go up to a heading with level ARG, do
  4985. a `show-subtree' and return to the previous cursor position. If ARG
  4986. is negative, go up that many levels.
  4987. - When point is not at the beginning of a headline, execute
  4988. `indent-relative', like TAB normally does. See the option
  4989. `org-cycle-emulate-tab' for details.
  4990. - Special case: if point is at the beginning of the buffer and there is
  4991. no headline in line 1, this function will act as if called with prefix arg.
  4992. But only if also the variable `org-cycle-global-at-bob' is t."
  4993. (interactive "P")
  4994. (let* ((outline-regexp
  4995. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4996. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4997. outline-regexp))
  4998. (bob-special (and org-cycle-global-at-bob (bobp)
  4999. (not (looking-at outline-regexp))))
  5000. (org-cycle-hook
  5001. (if bob-special
  5002. (delq 'org-optimize-window-after-visibility-change
  5003. (copy-sequence org-cycle-hook))
  5004. org-cycle-hook))
  5005. (pos (point)))
  5006. (if (or bob-special (equal arg '(4)))
  5007. ;; special case: use global cycling
  5008. (setq arg t))
  5009. (cond
  5010. ((org-at-table-p 'any)
  5011. ;; Enter the table or move to the next field in the table
  5012. (or (org-table-recognize-table.el)
  5013. (progn
  5014. (if arg (org-table-edit-field t)
  5015. (org-table-justify-field-maybe)
  5016. (call-interactively 'org-table-next-field)))))
  5017. ((eq arg t) ;; Global cycling
  5018. (cond
  5019. ((and (eq last-command this-command)
  5020. (eq org-cycle-global-status 'overview))
  5021. ;; We just created the overview - now do table of contents
  5022. ;; This can be slow in very large buffers, so indicate action
  5023. (message "CONTENTS...")
  5024. (org-content)
  5025. (message "CONTENTS...done")
  5026. (setq org-cycle-global-status 'contents)
  5027. (run-hook-with-args 'org-cycle-hook 'contents))
  5028. ((and (eq last-command this-command)
  5029. (eq org-cycle-global-status 'contents))
  5030. ;; We just showed the table of contents - now show everything
  5031. (show-all)
  5032. (message "SHOW ALL")
  5033. (setq org-cycle-global-status 'all)
  5034. (run-hook-with-args 'org-cycle-hook 'all))
  5035. (t
  5036. ;; Default action: go to overview
  5037. (org-overview)
  5038. (message "OVERVIEW")
  5039. (setq org-cycle-global-status 'overview)
  5040. (run-hook-with-args 'org-cycle-hook 'overview))))
  5041. ((and org-drawers org-drawer-regexp
  5042. (save-excursion
  5043. (beginning-of-line 1)
  5044. (looking-at org-drawer-regexp)))
  5045. ;; Toggle block visibility
  5046. (org-flag-drawer
  5047. (not (get-char-property (match-end 0) 'invisible))))
  5048. ((integerp arg)
  5049. ;; Show-subtree, ARG levels up from here.
  5050. (save-excursion
  5051. (org-back-to-heading)
  5052. (outline-up-heading (if (< arg 0) (- arg)
  5053. (- (funcall outline-level) arg)))
  5054. (org-show-subtree)))
  5055. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5056. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5057. ;; At a heading: rotate between three different views
  5058. (org-back-to-heading)
  5059. (let ((goal-column 0) eoh eol eos)
  5060. ;; First, some boundaries
  5061. (save-excursion
  5062. (org-back-to-heading)
  5063. (save-excursion
  5064. (beginning-of-line 2)
  5065. (while (and (not (eobp)) ;; this is like `next-line'
  5066. (get-char-property (1- (point)) 'invisible))
  5067. (beginning-of-line 2)) (setq eol (point)))
  5068. (outline-end-of-heading) (setq eoh (point))
  5069. (org-end-of-subtree t)
  5070. (unless (eobp)
  5071. (skip-chars-forward " \t\n")
  5072. (beginning-of-line 1) ; in case this is an item
  5073. )
  5074. (setq eos (1- (point))))
  5075. ;; Find out what to do next and set `this-command'
  5076. (cond
  5077. ((= eos eoh)
  5078. ;; Nothing is hidden behind this heading
  5079. (message "EMPTY ENTRY")
  5080. (setq org-cycle-subtree-status nil)
  5081. (save-excursion
  5082. (goto-char eos)
  5083. (outline-next-heading)
  5084. (if (org-invisible-p) (org-flag-heading nil))))
  5085. ((or (>= eol eos)
  5086. (not (string-match "\\S-" (buffer-substring eol eos))))
  5087. ;; Entire subtree is hidden in one line: open it
  5088. (org-show-entry)
  5089. (show-children)
  5090. (message "CHILDREN")
  5091. (save-excursion
  5092. (goto-char eos)
  5093. (outline-next-heading)
  5094. (if (org-invisible-p) (org-flag-heading nil)))
  5095. (setq org-cycle-subtree-status 'children)
  5096. (run-hook-with-args 'org-cycle-hook 'children))
  5097. ((and (eq last-command this-command)
  5098. (eq org-cycle-subtree-status 'children))
  5099. ;; We just showed the children, now show everything.
  5100. (org-show-subtree)
  5101. (message "SUBTREE")
  5102. (setq org-cycle-subtree-status 'subtree)
  5103. (run-hook-with-args 'org-cycle-hook 'subtree))
  5104. (t
  5105. ;; Default action: hide the subtree.
  5106. (hide-subtree)
  5107. (message "FOLDED")
  5108. (setq org-cycle-subtree-status 'folded)
  5109. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5110. ;; TAB emulation
  5111. (buffer-read-only (org-back-to-heading))
  5112. ((org-try-cdlatex-tab))
  5113. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5114. (or (not (bolp))
  5115. (not (looking-at outline-regexp))))
  5116. (call-interactively (global-key-binding "\t")))
  5117. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5118. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5119. (or (and (eq org-cycle-emulate-tab 'white)
  5120. (= (match-end 0) (point-at-eol)))
  5121. (and (eq org-cycle-emulate-tab 'whitestart)
  5122. (>= (match-end 0) pos))))
  5123. t
  5124. (eq org-cycle-emulate-tab t))
  5125. ; (if (and (looking-at "[ \n\r\t]")
  5126. ; (string-match "^[ \t]*$" (buffer-substring
  5127. ; (point-at-bol) (point))))
  5128. ; (progn
  5129. ; (beginning-of-line 1)
  5130. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5131. (call-interactively (global-key-binding "\t")))
  5132. (t (save-excursion
  5133. (org-back-to-heading)
  5134. (org-cycle))))))
  5135. ;;;###autoload
  5136. (defun org-global-cycle (&optional arg)
  5137. "Cycle the global visibility. For details see `org-cycle'."
  5138. (interactive "P")
  5139. (let ((org-cycle-include-plain-lists
  5140. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5141. (if (integerp arg)
  5142. (progn
  5143. (show-all)
  5144. (hide-sublevels arg)
  5145. (setq org-cycle-global-status 'contents))
  5146. (org-cycle '(4)))))
  5147. (defun org-overview ()
  5148. "Switch to overview mode, shoing only top-level headlines.
  5149. Really, this shows all headlines with level equal or greater than the level
  5150. of the first headline in the buffer. This is important, because if the
  5151. first headline is not level one, then (hide-sublevels 1) gives confusing
  5152. results."
  5153. (interactive)
  5154. (let ((level (save-excursion
  5155. (goto-char (point-min))
  5156. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5157. (progn
  5158. (goto-char (match-beginning 0))
  5159. (funcall outline-level))))))
  5160. (and level (hide-sublevels level))))
  5161. (defun org-content (&optional arg)
  5162. "Show all headlines in the buffer, like a table of contents.
  5163. With numerical argument N, show content up to level N."
  5164. (interactive "P")
  5165. (save-excursion
  5166. ;; Visit all headings and show their offspring
  5167. (and (integerp arg) (org-overview))
  5168. (goto-char (point-max))
  5169. (catch 'exit
  5170. (while (and (progn (condition-case nil
  5171. (outline-previous-visible-heading 1)
  5172. (error (goto-char (point-min))))
  5173. t)
  5174. (looking-at outline-regexp))
  5175. (if (integerp arg)
  5176. (show-children (1- arg))
  5177. (show-branches))
  5178. (if (bobp) (throw 'exit nil))))))
  5179. (defun org-optimize-window-after-visibility-change (state)
  5180. "Adjust the window after a change in outline visibility.
  5181. This function is the default value of the hook `org-cycle-hook'."
  5182. (when (get-buffer-window (current-buffer))
  5183. (cond
  5184. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5185. ; ((eq state 'overview) (org-beginning-of-line))
  5186. ((eq state 'content) nil)
  5187. ((eq state 'all) nil)
  5188. ((eq state 'folded) nil)
  5189. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5190. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5191. (defun org-compact-display-after-subtree-move ()
  5192. (let (beg end)
  5193. (save-excursion
  5194. (if (org-up-heading-safe)
  5195. (progn
  5196. (hide-subtree)
  5197. (show-entry)
  5198. (show-children)
  5199. (org-cycle-show-empty-lines 'children)
  5200. (org-cycle-hide-drawers 'children))
  5201. (org-overview)))))
  5202. (defun org-cycle-show-empty-lines (state)
  5203. "Show empty lines above all visible headlines.
  5204. The region to be covered depends on STATE when called through
  5205. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5206. entire buffer covered. Note that an empty line is only shown if there
  5207. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5208. (when (> org-cycle-separator-lines 0)
  5209. (save-excursion
  5210. (let* ((n org-cycle-separator-lines)
  5211. (re (cond
  5212. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5213. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5214. (t (let ((ns (number-to-string (- n 2))))
  5215. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5216. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5217. beg end)
  5218. (cond
  5219. ((memq state '(overview contents t))
  5220. (setq beg (point-min) end (point-max)))
  5221. ((memq state '(children folded))
  5222. (setq beg (point) end (progn (org-end-of-subtree t t)
  5223. (beginning-of-line 2)
  5224. (point)))))
  5225. (when beg
  5226. (goto-char beg)
  5227. (while (re-search-forward re end t)
  5228. (if (not (get-char-property (match-end 1) 'invisible))
  5229. (outline-flag-region
  5230. (match-beginning 1) (match-end 1) nil)))))))
  5231. ;; Never hide empty lines at the end of the file.
  5232. (save-excursion
  5233. (goto-char (point-max))
  5234. (outline-previous-heading)
  5235. (outline-end-of-heading)
  5236. (if (and (looking-at "[ \t\n]+")
  5237. (= (match-end 0) (point-max)))
  5238. (outline-flag-region (point) (match-end 0) nil))))
  5239. (defun org-subtree-end-visible-p ()
  5240. "Is the end of the current subtree visible?"
  5241. (pos-visible-in-window-p
  5242. (save-excursion (org-end-of-subtree t) (point))))
  5243. (defun org-first-headline-recenter (&optional N)
  5244. "Move cursor to the first headline and recenter the headline.
  5245. Optional argument N means, put the headline into the Nth line of the window."
  5246. (goto-char (point-min))
  5247. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5248. (beginning-of-line)
  5249. (recenter (prefix-numeric-value N))))
  5250. ;;; Org-goto
  5251. (defvar org-goto-window-configuration nil)
  5252. (defvar org-goto-marker nil)
  5253. (defvar org-goto-map
  5254. (let ((map (make-sparse-keymap)))
  5255. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5256. (while (setq cmd (pop cmds))
  5257. (substitute-key-definition cmd cmd map global-map)))
  5258. (suppress-keymap map)
  5259. (org-defkey map "\C-m" 'org-goto-ret)
  5260. (org-defkey map [(return)] 'org-goto-ret)
  5261. (org-defkey map [(left)] 'org-goto-left)
  5262. (org-defkey map [(right)] 'org-goto-right)
  5263. (org-defkey map [(control ?g)] 'org-goto-quit)
  5264. (org-defkey map "\C-i" 'org-cycle)
  5265. (org-defkey map [(tab)] 'org-cycle)
  5266. (org-defkey map [(down)] 'outline-next-visible-heading)
  5267. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5268. (if org-goto-auto-isearch
  5269. (if (fboundp 'define-key-after)
  5270. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5271. nil)
  5272. (org-defkey map "q" 'org-goto-quit)
  5273. (org-defkey map "n" 'outline-next-visible-heading)
  5274. (org-defkey map "p" 'outline-previous-visible-heading)
  5275. (org-defkey map "f" 'outline-forward-same-level)
  5276. (org-defkey map "b" 'outline-backward-same-level)
  5277. (org-defkey map "u" 'outline-up-heading))
  5278. (org-defkey map "/" 'org-occur)
  5279. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5280. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5281. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5282. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5283. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5284. map))
  5285. (defconst org-goto-help
  5286. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5287. RET=jump to location [Q]uit and return to previous location
  5288. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5289. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5290. (defun org-goto (&optional alternative-interface)
  5291. "Look up a different location in the current file, keeping current visibility.
  5292. When you want look-up or go to a different location in a document, the
  5293. fastest way is often to fold the entire buffer and then dive into the tree.
  5294. This method has the disadvantage, that the previous location will be folded,
  5295. which may not be what you want.
  5296. This command works around this by showing a copy of the current buffer
  5297. in an indirect buffer, in overview mode. You can dive into the tree in
  5298. that copy, use org-occur and incremental search to find a location.
  5299. When pressing RET or `Q', the command returns to the original buffer in
  5300. which the visibility is still unchanged. After RET is will also jump to
  5301. the location selected in the indirect buffer and expose the
  5302. the headline hierarchy above."
  5303. (interactive "P")
  5304. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5305. (org-refile-use-outline-path t)
  5306. (interface
  5307. (if (not alternative-interface)
  5308. org-goto-interface
  5309. (if (eq org-goto-interface 'outline)
  5310. 'outline-path-completion
  5311. 'outline)))
  5312. (org-goto-start-pos (point))
  5313. (selected-point
  5314. (if (eq interface 'outline)
  5315. (car (org-get-location (current-buffer) org-goto-help))
  5316. (nth 3 (org-refile-get-location "Goto: ")))))
  5317. (if selected-point
  5318. (progn
  5319. (org-mark-ring-push org-goto-start-pos)
  5320. (goto-char selected-point)
  5321. (if (or (org-invisible-p) (org-invisible-p2))
  5322. (org-show-context 'org-goto)))
  5323. (message "Quit"))))
  5324. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5325. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5326. (defvar org-goto-local-auto-isearch-map) ; defined below
  5327. (defun org-get-location (buf help)
  5328. "Let the user select a location in the Org-mode buffer BUF.
  5329. This function uses a recursive edit. It returns the selected position
  5330. or nil."
  5331. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5332. (isearch-hide-immediately nil)
  5333. (isearch-search-fun-function
  5334. (lambda () 'org-goto-local-search-forward-headings))
  5335. (org-goto-selected-point org-goto-exit-command))
  5336. (save-excursion
  5337. (save-window-excursion
  5338. (delete-other-windows)
  5339. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5340. (switch-to-buffer
  5341. (condition-case nil
  5342. (make-indirect-buffer (current-buffer) "*org-goto*")
  5343. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5344. (with-output-to-temp-buffer "*Help*"
  5345. (princ help))
  5346. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5347. (setq buffer-read-only nil)
  5348. (let ((org-startup-truncated t)
  5349. (org-startup-folded nil)
  5350. (org-startup-align-all-tables nil))
  5351. (org-mode)
  5352. (org-overview))
  5353. (setq buffer-read-only t)
  5354. (if (and (boundp 'org-goto-start-pos)
  5355. (integer-or-marker-p org-goto-start-pos))
  5356. (let ((org-show-hierarchy-above t)
  5357. (org-show-siblings t)
  5358. (org-show-following-heading t))
  5359. (goto-char org-goto-start-pos)
  5360. (and (org-invisible-p) (org-show-context)))
  5361. (goto-char (point-min)))
  5362. (org-beginning-of-line)
  5363. (message "Select location and press RET")
  5364. (use-local-map org-goto-map)
  5365. (recursive-edit)
  5366. ))
  5367. (kill-buffer "*org-goto*")
  5368. (cons org-goto-selected-point org-goto-exit-command)))
  5369. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5370. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5371. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5372. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5373. (defun org-goto-local-search-forward-headings (string bound noerror)
  5374. "Search and make sure that anu matches are in headlines."
  5375. (catch 'return
  5376. (while (search-forward string bound noerror)
  5377. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5378. (and (member :headline context)
  5379. (not (member :tags context))))
  5380. (throw 'return (point))))))
  5381. (defun org-goto-local-auto-isearch ()
  5382. "Start isearch."
  5383. (interactive)
  5384. (goto-char (point-min))
  5385. (let ((keys (this-command-keys)))
  5386. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5387. (isearch-mode t)
  5388. (isearch-process-search-char (string-to-char keys)))))
  5389. (defun org-goto-ret (&optional arg)
  5390. "Finish `org-goto' by going to the new location."
  5391. (interactive "P")
  5392. (setq org-goto-selected-point (point)
  5393. org-goto-exit-command 'return)
  5394. (throw 'exit nil))
  5395. (defun org-goto-left ()
  5396. "Finish `org-goto' by going to the new location."
  5397. (interactive)
  5398. (if (org-on-heading-p)
  5399. (progn
  5400. (beginning-of-line 1)
  5401. (setq org-goto-selected-point (point)
  5402. org-goto-exit-command 'left)
  5403. (throw 'exit nil))
  5404. (error "Not on a heading")))
  5405. (defun org-goto-right ()
  5406. "Finish `org-goto' by going to the new location."
  5407. (interactive)
  5408. (if (org-on-heading-p)
  5409. (progn
  5410. (setq org-goto-selected-point (point)
  5411. org-goto-exit-command 'right)
  5412. (throw 'exit nil))
  5413. (error "Not on a heading")))
  5414. (defun org-goto-quit ()
  5415. "Finish `org-goto' without cursor motion."
  5416. (interactive)
  5417. (setq org-goto-selected-point nil)
  5418. (setq org-goto-exit-command 'quit)
  5419. (throw 'exit nil))
  5420. ;;; Indirect buffer display of subtrees
  5421. (defvar org-indirect-dedicated-frame nil
  5422. "This is the frame being used for indirect tree display.")
  5423. (defvar org-last-indirect-buffer nil)
  5424. (defun org-tree-to-indirect-buffer (&optional arg)
  5425. "Create indirect buffer and narrow it to current subtree.
  5426. With numerical prefix ARG, go up to this level and then take that tree.
  5427. If ARG is negative, go up that many levels.
  5428. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5429. indirect buffer previously made with this command, to avoid proliferation of
  5430. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5431. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5432. is kept so that you can work with several indirect buffers at the same time.
  5433. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5434. requests that a new frame be made for the new buffer, so that the dedicated
  5435. frame is not changed."
  5436. (interactive "P")
  5437. (let ((cbuf (current-buffer))
  5438. (cwin (selected-window))
  5439. (pos (point))
  5440. beg end level heading ibuf)
  5441. (save-excursion
  5442. (org-back-to-heading t)
  5443. (when (numberp arg)
  5444. (setq level (org-outline-level))
  5445. (if (< arg 0) (setq arg (+ level arg)))
  5446. (while (> (setq level (org-outline-level)) arg)
  5447. (outline-up-heading 1 t)))
  5448. (setq beg (point)
  5449. heading (org-get-heading))
  5450. (org-end-of-subtree t) (setq end (point)))
  5451. (if (and (buffer-live-p org-last-indirect-buffer)
  5452. (not (eq org-indirect-buffer-display 'new-frame))
  5453. (not arg))
  5454. (kill-buffer org-last-indirect-buffer))
  5455. (setq ibuf (org-get-indirect-buffer cbuf)
  5456. org-last-indirect-buffer ibuf)
  5457. (cond
  5458. ((or (eq org-indirect-buffer-display 'new-frame)
  5459. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5460. (select-frame (make-frame))
  5461. (delete-other-windows)
  5462. (switch-to-buffer ibuf)
  5463. (org-set-frame-title heading))
  5464. ((eq org-indirect-buffer-display 'dedicated-frame)
  5465. (raise-frame
  5466. (select-frame (or (and org-indirect-dedicated-frame
  5467. (frame-live-p org-indirect-dedicated-frame)
  5468. org-indirect-dedicated-frame)
  5469. (setq org-indirect-dedicated-frame (make-frame)))))
  5470. (delete-other-windows)
  5471. (switch-to-buffer ibuf)
  5472. (org-set-frame-title (concat "Indirect: " heading)))
  5473. ((eq org-indirect-buffer-display 'current-window)
  5474. (switch-to-buffer ibuf))
  5475. ((eq org-indirect-buffer-display 'other-window)
  5476. (pop-to-buffer ibuf))
  5477. (t (error "Invalid value.")))
  5478. (if (featurep 'xemacs)
  5479. (save-excursion (org-mode) (turn-on-font-lock)))
  5480. (narrow-to-region beg end)
  5481. (show-all)
  5482. (goto-char pos)
  5483. (and (window-live-p cwin) (select-window cwin))))
  5484. (defun org-get-indirect-buffer (&optional buffer)
  5485. (setq buffer (or buffer (current-buffer)))
  5486. (let ((n 1) (base (buffer-name buffer)) bname)
  5487. (while (buffer-live-p
  5488. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5489. (setq n (1+ n)))
  5490. (condition-case nil
  5491. (make-indirect-buffer buffer bname 'clone)
  5492. (error (make-indirect-buffer buffer bname)))))
  5493. (defun org-set-frame-title (title)
  5494. "Set the title of the current frame to the string TITLE."
  5495. ;; FIXME: how to name a single frame in XEmacs???
  5496. (unless (featurep 'xemacs)
  5497. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5498. ;;;; Structure editing
  5499. ;;; Inserting headlines
  5500. (defun org-insert-heading (&optional force-heading)
  5501. "Insert a new heading or item with same depth at point.
  5502. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5503. If point is at the beginning of a headline, insert a sibling before the
  5504. current headline. If point is not at the beginning, do not split the line,
  5505. but create the new hedline after the current line."
  5506. (interactive "P")
  5507. (if (= (buffer-size) 0)
  5508. (insert "\n* ")
  5509. (when (or force-heading (not (org-insert-item)))
  5510. (let* ((head (save-excursion
  5511. (condition-case nil
  5512. (progn
  5513. (org-back-to-heading)
  5514. (match-string 0))
  5515. (error "*"))))
  5516. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5517. pos)
  5518. (cond
  5519. ((and (org-on-heading-p) (bolp)
  5520. (or (bobp)
  5521. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5522. ;; insert before the current line
  5523. (open-line (if blank 2 1)))
  5524. ((and (bolp)
  5525. (or (bobp)
  5526. (save-excursion
  5527. (backward-char 1) (not (org-invisible-p)))))
  5528. ;; insert right here
  5529. nil)
  5530. (t
  5531. ; ;; in the middle of the line
  5532. ; (org-show-entry)
  5533. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5534. ; (if (and
  5535. ; (org-on-heading-p)
  5536. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5537. ; ;; protect the tags
  5538. ;; (let ((tags (match-string 2)) pos)
  5539. ; (delete-region (match-beginning 1) (match-end 1))
  5540. ; (setq pos (point-at-bol))
  5541. ; (newline (if blank 2 1))
  5542. ; (save-excursion
  5543. ; (goto-char pos)
  5544. ; (end-of-line 1)
  5545. ; (insert " " tags)
  5546. ; (org-set-tags nil 'align)))
  5547. ; (newline (if blank 2 1)))
  5548. ; (newline (if blank 2 1))))
  5549. ;; in the middle of the line
  5550. (org-show-entry)
  5551. (let ((split
  5552. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5553. tags pos)
  5554. (if (org-on-heading-p)
  5555. (progn
  5556. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5557. (setq tags (and (match-end 2) (match-string 2)))
  5558. (and (match-end 1)
  5559. (delete-region (match-beginning 1) (match-end 1)))
  5560. (setq pos (point-at-bol))
  5561. (or split (end-of-line 1))
  5562. (just-one-space 0)
  5563. (newline (if blank 2 1))
  5564. (when tags
  5565. (save-excursion
  5566. (goto-char pos)
  5567. (end-of-line 1)
  5568. (insert " " tags)
  5569. (org-set-tags nil 'align))))
  5570. (or split (end-of-line 1))
  5571. (newline (if blank 2 1))))))
  5572. (insert head) (just-one-space)
  5573. (setq pos (point))
  5574. (end-of-line 1)
  5575. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5576. (run-hooks 'org-insert-heading-hook)))))
  5577. (defun org-insert-heading-after-current ()
  5578. "Insert a new heading with same level as current, after current subtree."
  5579. (interactive)
  5580. (org-back-to-heading)
  5581. (org-insert-heading)
  5582. (org-move-subtree-down)
  5583. (end-of-line 1))
  5584. (defun org-insert-todo-heading (arg)
  5585. "Insert a new heading with the same level and TODO state as current heading.
  5586. If the heading has no TODO state, or if the state is DONE, use the first
  5587. state (TODO by default). Also with prefix arg, force first state."
  5588. (interactive "P")
  5589. (when (not (org-insert-item 'checkbox))
  5590. (org-insert-heading)
  5591. (save-excursion
  5592. (org-back-to-heading)
  5593. (outline-previous-heading)
  5594. (looking-at org-todo-line-regexp))
  5595. (if (or arg
  5596. (not (match-beginning 2))
  5597. (member (match-string 2) org-done-keywords))
  5598. (insert (car org-todo-keywords-1) " ")
  5599. (insert (match-string 2) " "))))
  5600. (defun org-insert-subheading (arg)
  5601. "Insert a new subheading and demote it.
  5602. Works for outline headings and for plain lists alike."
  5603. (interactive "P")
  5604. (org-insert-heading arg)
  5605. (cond
  5606. ((org-on-heading-p) (org-do-demote))
  5607. ((org-at-item-p) (org-indent-item 1))))
  5608. (defun org-insert-todo-subheading (arg)
  5609. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5610. Works for outline headings and for plain lists alike."
  5611. (interactive "P")
  5612. (org-insert-todo-heading arg)
  5613. (cond
  5614. ((org-on-heading-p) (org-do-demote))
  5615. ((org-at-item-p) (org-indent-item 1))))
  5616. ;;; Promotion and Demotion
  5617. (defun org-promote-subtree ()
  5618. "Promote the entire subtree.
  5619. See also `org-promote'."
  5620. (interactive)
  5621. (save-excursion
  5622. (org-map-tree 'org-promote))
  5623. (org-fix-position-after-promote))
  5624. (defun org-demote-subtree ()
  5625. "Demote the entire subtree. See `org-demote'.
  5626. See also `org-promote'."
  5627. (interactive)
  5628. (save-excursion
  5629. (org-map-tree 'org-demote))
  5630. (org-fix-position-after-promote))
  5631. (defun org-do-promote ()
  5632. "Promote the current heading higher up the tree.
  5633. If the region is active in `transient-mark-mode', promote all headings
  5634. in the region."
  5635. (interactive)
  5636. (save-excursion
  5637. (if (org-region-active-p)
  5638. (org-map-region 'org-promote (region-beginning) (region-end))
  5639. (org-promote)))
  5640. (org-fix-position-after-promote))
  5641. (defun org-do-demote ()
  5642. "Demote the current heading lower down the tree.
  5643. If the region is active in `transient-mark-mode', demote all headings
  5644. in the region."
  5645. (interactive)
  5646. (save-excursion
  5647. (if (org-region-active-p)
  5648. (org-map-region 'org-demote (region-beginning) (region-end))
  5649. (org-demote)))
  5650. (org-fix-position-after-promote))
  5651. (defun org-fix-position-after-promote ()
  5652. "Make sure that after pro/demotion cursor position is right."
  5653. (let ((pos (point)))
  5654. (when (save-excursion
  5655. (beginning-of-line 1)
  5656. (looking-at org-todo-line-regexp)
  5657. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5658. (cond ((eobp) (insert " "))
  5659. ((eolp) (insert " "))
  5660. ((equal (char-after) ?\ ) (forward-char 1))))))
  5661. (defun org-reduced-level (l)
  5662. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5663. (defun org-get-legal-level (level &optional change)
  5664. "Rectify a level change under the influence of `org-odd-levels-only'
  5665. LEVEL is a current level, CHANGE is by how much the level should be
  5666. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5667. even level numbers will become the next higher odd number."
  5668. (if org-odd-levels-only
  5669. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5670. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5671. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5672. (max 1 (+ level change))))
  5673. (defun org-promote ()
  5674. "Promote the current heading higher up the tree.
  5675. If the region is active in `transient-mark-mode', promote all headings
  5676. in the region."
  5677. (org-back-to-heading t)
  5678. (let* ((level (save-match-data (funcall outline-level)))
  5679. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5680. (diff (abs (- level (length up-head) -1))))
  5681. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5682. (replace-match up-head nil t)
  5683. ;; Fixup tag positioning
  5684. (and org-auto-align-tags (org-set-tags nil t))
  5685. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5686. (defun org-demote ()
  5687. "Demote the current heading lower down the tree.
  5688. If the region is active in `transient-mark-mode', demote all headings
  5689. in the region."
  5690. (org-back-to-heading t)
  5691. (let* ((level (save-match-data (funcall outline-level)))
  5692. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5693. (diff (abs (- level (length down-head) -1))))
  5694. (replace-match down-head nil t)
  5695. ;; Fixup tag positioning
  5696. (and org-auto-align-tags (org-set-tags nil t))
  5697. (if org-adapt-indentation (org-fixup-indentation diff))))
  5698. (defun org-map-tree (fun)
  5699. "Call FUN for every heading underneath the current one."
  5700. (org-back-to-heading)
  5701. (let ((level (funcall outline-level)))
  5702. (save-excursion
  5703. (funcall fun)
  5704. (while (and (progn
  5705. (outline-next-heading)
  5706. (> (funcall outline-level) level))
  5707. (not (eobp)))
  5708. (funcall fun)))))
  5709. (defun org-map-region (fun beg end)
  5710. "Call FUN for every heading between BEG and END."
  5711. (let ((org-ignore-region t))
  5712. (save-excursion
  5713. (setq end (copy-marker end))
  5714. (goto-char beg)
  5715. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5716. (< (point) end))
  5717. (funcall fun))
  5718. (while (and (progn
  5719. (outline-next-heading)
  5720. (< (point) end))
  5721. (not (eobp)))
  5722. (funcall fun)))))
  5723. (defun org-fixup-indentation (diff)
  5724. "Change the indentation in the current entry by DIFF
  5725. However, if any line in the current entry has no indentation, or if it
  5726. would end up with no indentation after the change, nothing at all is done."
  5727. (save-excursion
  5728. (let ((end (save-excursion (outline-next-heading)
  5729. (point-marker)))
  5730. (prohibit (if (> diff 0)
  5731. "^\\S-"
  5732. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5733. col)
  5734. (unless (save-excursion (end-of-line 1)
  5735. (re-search-forward prohibit end t))
  5736. (while (and (< (point) end)
  5737. (re-search-forward "^[ \t]+" end t))
  5738. (goto-char (match-end 0))
  5739. (setq col (current-column))
  5740. (if (< diff 0) (replace-match ""))
  5741. (indent-to (+ diff col))))
  5742. (move-marker end nil))))
  5743. (defun org-convert-to-odd-levels ()
  5744. "Convert an org-mode file with all levels allowed to one with odd levels.
  5745. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5746. level 5 etc."
  5747. (interactive)
  5748. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5749. (let ((org-odd-levels-only nil) n)
  5750. (save-excursion
  5751. (goto-char (point-min))
  5752. (while (re-search-forward "^\\*\\*+ " nil t)
  5753. (setq n (- (length (match-string 0)) 2))
  5754. (while (>= (setq n (1- n)) 0)
  5755. (org-demote))
  5756. (end-of-line 1))))))
  5757. (defun org-convert-to-oddeven-levels ()
  5758. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5759. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5760. section with an even level, conversion would destroy the structure of the file. An error
  5761. is signaled in this case."
  5762. (interactive)
  5763. (goto-char (point-min))
  5764. ;; First check if there are no even levels
  5765. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5766. (org-show-context t)
  5767. (error "Not all levels are odd in this file. Conversion not possible."))
  5768. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5769. (let ((org-odd-levels-only nil) n)
  5770. (save-excursion
  5771. (goto-char (point-min))
  5772. (while (re-search-forward "^\\*\\*+ " nil t)
  5773. (setq n (/ (1- (length (match-string 0))) 2))
  5774. (while (>= (setq n (1- n)) 0)
  5775. (org-promote))
  5776. (end-of-line 1))))))
  5777. (defun org-tr-level (n)
  5778. "Make N odd if required."
  5779. (if org-odd-levels-only (1+ (/ n 2)) n))
  5780. ;;; Vertical tree motion, cutting and pasting of subtrees
  5781. (defun org-move-subtree-up (&optional arg)
  5782. "Move the current subtree up past ARG headlines of the same level."
  5783. (interactive "p")
  5784. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5785. (defun org-move-subtree-down (&optional arg)
  5786. "Move the current subtree down past ARG headlines of the same level."
  5787. (interactive "p")
  5788. (setq arg (prefix-numeric-value arg))
  5789. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5790. 'outline-get-last-sibling))
  5791. (ins-point (make-marker))
  5792. (cnt (abs arg))
  5793. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5794. ;; Select the tree
  5795. (org-back-to-heading)
  5796. (setq beg0 (point))
  5797. (save-excursion
  5798. (setq ne-beg (org-back-over-empty-lines))
  5799. (setq beg (point)))
  5800. (save-match-data
  5801. (save-excursion (outline-end-of-heading)
  5802. (setq folded (org-invisible-p)))
  5803. (outline-end-of-subtree))
  5804. (outline-next-heading)
  5805. (setq ne-end (org-back-over-empty-lines))
  5806. (setq end (point))
  5807. (goto-char beg0)
  5808. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5809. ;; include less whitespace
  5810. (save-excursion
  5811. (goto-char beg)
  5812. (forward-line (- ne-beg ne-end))
  5813. (setq beg (point))))
  5814. ;; Find insertion point, with error handling
  5815. (while (> cnt 0)
  5816. (or (and (funcall movfunc) (looking-at outline-regexp))
  5817. (progn (goto-char beg0)
  5818. (error "Cannot move past superior level or buffer limit")))
  5819. (setq cnt (1- cnt)))
  5820. (if (> arg 0)
  5821. ;; Moving forward - still need to move over subtree
  5822. (progn (org-end-of-subtree t t)
  5823. (save-excursion
  5824. (org-back-over-empty-lines)
  5825. (or (bolp) (newline)))))
  5826. (setq ne-ins (org-back-over-empty-lines))
  5827. (move-marker ins-point (point))
  5828. (setq txt (buffer-substring beg end))
  5829. (delete-region beg end)
  5830. (outline-flag-region (1- beg) beg nil)
  5831. (outline-flag-region (1- (point)) (point) nil)
  5832. (insert txt)
  5833. (or (bolp) (insert "\n"))
  5834. (setq ins-end (point))
  5835. (goto-char ins-point)
  5836. (org-skip-whitespace)
  5837. (when (and (< arg 0)
  5838. (org-first-sibling-p)
  5839. (> ne-ins ne-beg))
  5840. ;; Move whitespace back to beginning
  5841. (save-excursion
  5842. (goto-char ins-end)
  5843. (let ((kill-whole-line t))
  5844. (kill-line (- ne-ins ne-beg)) (point)))
  5845. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5846. (move-marker ins-point nil)
  5847. (org-compact-display-after-subtree-move)
  5848. (unless folded
  5849. (org-show-entry)
  5850. (show-children)
  5851. (org-cycle-hide-drawers 'children))))
  5852. (defvar org-subtree-clip ""
  5853. "Clipboard for cut and paste of subtrees.
  5854. This is actually only a copy of the kill, because we use the normal kill
  5855. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5856. (defvar org-subtree-clip-folded nil
  5857. "Was the last copied subtree folded?
  5858. This is used to fold the tree back after pasting.")
  5859. (defun org-cut-subtree (&optional n)
  5860. "Cut the current subtree into the clipboard.
  5861. With prefix arg N, cut this many sequential subtrees.
  5862. This is a short-hand for marking the subtree and then cutting it."
  5863. (interactive "p")
  5864. (org-copy-subtree n 'cut))
  5865. (defun org-copy-subtree (&optional n cut)
  5866. "Cut the current subtree into the clipboard.
  5867. With prefix arg N, cut this many sequential subtrees.
  5868. This is a short-hand for marking the subtree and then copying it.
  5869. If CUT is non-nil, actually cut the subtree."
  5870. (interactive "p")
  5871. (let (beg end folded (beg0 (point)))
  5872. (if (interactive-p)
  5873. (org-back-to-heading nil) ; take what looks like a subtree
  5874. (org-back-to-heading t)) ; take what is really there
  5875. (org-back-over-empty-lines)
  5876. (setq beg (point))
  5877. (skip-chars-forward " \t\r\n")
  5878. (save-match-data
  5879. (save-excursion (outline-end-of-heading)
  5880. (setq folded (org-invisible-p)))
  5881. (condition-case nil
  5882. (outline-forward-same-level (1- n))
  5883. (error nil))
  5884. (org-end-of-subtree t t))
  5885. (org-back-over-empty-lines)
  5886. (setq end (point))
  5887. (goto-char beg0)
  5888. (when (> end beg)
  5889. (setq org-subtree-clip-folded folded)
  5890. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5891. (setq org-subtree-clip (current-kill 0))
  5892. (message "%s: Subtree(s) with %d characters"
  5893. (if cut "Cut" "Copied")
  5894. (length org-subtree-clip)))))
  5895. (defun org-paste-subtree (&optional level tree)
  5896. "Paste the clipboard as a subtree, with modification of headline level.
  5897. The entire subtree is promoted or demoted in order to match a new headline
  5898. level. By default, the new level is derived from the visible headings
  5899. before and after the insertion point, and taken to be the inferior headline
  5900. level of the two. So if the previous visible heading is level 3 and the
  5901. next is level 4 (or vice versa), level 4 will be used for insertion.
  5902. This makes sure that the subtree remains an independent subtree and does
  5903. not swallow low level entries.
  5904. You can also force a different level, either by using a numeric prefix
  5905. argument, or by inserting the heading marker by hand. For example, if the
  5906. cursor is after \"*****\", then the tree will be shifted to level 5.
  5907. If you want to insert the tree as is, just use \\[yank].
  5908. If optional TREE is given, use this text instead of the kill ring."
  5909. (interactive "P")
  5910. (unless (org-kill-is-subtree-p tree)
  5911. (error "%s"
  5912. (substitute-command-keys
  5913. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5914. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5915. (^re (concat "^\\(" outline-regexp "\\)"))
  5916. (re (concat "\\(" outline-regexp "\\)"))
  5917. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5918. (old-level (if (string-match ^re txt)
  5919. (- (match-end 0) (match-beginning 0) 1)
  5920. -1))
  5921. (force-level (cond (level (prefix-numeric-value level))
  5922. ((string-match
  5923. ^re_ (buffer-substring (point-at-bol) (point)))
  5924. (- (match-end 1) (match-beginning 1)))
  5925. (t nil)))
  5926. (previous-level (save-excursion
  5927. (condition-case nil
  5928. (progn
  5929. (outline-previous-visible-heading 1)
  5930. (if (looking-at re)
  5931. (- (match-end 0) (match-beginning 0) 1)
  5932. 1))
  5933. (error 1))))
  5934. (next-level (save-excursion
  5935. (condition-case nil
  5936. (progn
  5937. (or (looking-at outline-regexp)
  5938. (outline-next-visible-heading 1))
  5939. (if (looking-at re)
  5940. (- (match-end 0) (match-beginning 0) 1)
  5941. 1))
  5942. (error 1))))
  5943. (new-level (or force-level (max previous-level next-level)))
  5944. (shift (if (or (= old-level -1)
  5945. (= new-level -1)
  5946. (= old-level new-level))
  5947. 0
  5948. (- new-level old-level)))
  5949. (delta (if (> shift 0) -1 1))
  5950. (func (if (> shift 0) 'org-demote 'org-promote))
  5951. (org-odd-levels-only nil)
  5952. beg end)
  5953. ;; Remove the forced level indicator
  5954. (if force-level
  5955. (delete-region (point-at-bol) (point)))
  5956. ;; Paste
  5957. (beginning-of-line 1)
  5958. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5959. (setq beg (point))
  5960. (insert-before-markers txt) ;; FIXME: correct fix????
  5961. (unless (string-match "\n\\'" txt) (insert "\n"))
  5962. (setq end (point))
  5963. (goto-char beg)
  5964. (skip-chars-forward " \t\n\r")
  5965. (setq beg (point))
  5966. ;; Shift if necessary
  5967. (unless (= shift 0)
  5968. (save-restriction
  5969. (narrow-to-region beg end)
  5970. (while (not (= shift 0))
  5971. (org-map-region func (point-min) (point-max))
  5972. (setq shift (+ delta shift)))
  5973. (goto-char (point-min))))
  5974. (when (interactive-p)
  5975. (message "Clipboard pasted as level %d subtree" new-level))
  5976. (if (and kill-ring
  5977. (eq org-subtree-clip (current-kill 0))
  5978. org-subtree-clip-folded)
  5979. ;; The tree was folded before it was killed/copied
  5980. (hide-subtree))))
  5981. (defun org-kill-is-subtree-p (&optional txt)
  5982. "Check if the current kill is an outline subtree, or a set of trees.
  5983. Returns nil if kill does not start with a headline, or if the first
  5984. headline level is not the largest headline level in the tree.
  5985. So this will actually accept several entries of equal levels as well,
  5986. which is OK for `org-paste-subtree'.
  5987. If optional TXT is given, check this string instead of the current kill."
  5988. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5989. (start-level (and kill
  5990. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5991. org-outline-regexp "\\)")
  5992. kill)
  5993. (- (match-end 2) (match-beginning 2) 1)))
  5994. (re (concat "^" org-outline-regexp))
  5995. (start (1+ (match-beginning 2))))
  5996. (if (not start-level)
  5997. (progn
  5998. nil) ;; does not even start with a heading
  5999. (catch 'exit
  6000. (while (setq start (string-match re kill (1+ start)))
  6001. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6002. (throw 'exit nil)))
  6003. t))))
  6004. (defun org-narrow-to-subtree ()
  6005. "Narrow buffer to the current subtree."
  6006. (interactive)
  6007. (save-excursion
  6008. (save-match-data
  6009. (narrow-to-region
  6010. (progn (org-back-to-heading) (point))
  6011. (progn (org-end-of-subtree t t) (point))))))
  6012. ;;; Outline Sorting
  6013. (defun org-sort (with-case)
  6014. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6015. Optional argument WITH-CASE means sort case-sensitively."
  6016. (interactive "P")
  6017. (if (org-at-table-p)
  6018. (org-call-with-arg 'org-table-sort-lines with-case)
  6019. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6020. (defvar org-priority-regexp) ; defined later in the file
  6021. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6022. "Sort entries on a certain level of an outline tree.
  6023. If there is an active region, the entries in the region are sorted.
  6024. Else, if the cursor is before the first entry, sort the top-level items.
  6025. Else, the children of the entry at point are sorted.
  6026. Sorting can be alphabetically, numerically, and by date/time as given by
  6027. the first time stamp in the entry. The command prompts for the sorting
  6028. type unless it has been given to the function through the SORTING-TYPE
  6029. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6030. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6031. called with point at the beginning of the record. It must return either
  6032. a string or a number that should serve as the sorting key for that record.
  6033. Comparing entries ignores case by default. However, with an optional argument
  6034. WITH-CASE, the sorting considers case as well."
  6035. (interactive "P")
  6036. (let ((case-func (if with-case 'identity 'downcase))
  6037. start beg end stars re re2
  6038. txt what tmp plain-list-p)
  6039. ;; Find beginning and end of region to sort
  6040. (cond
  6041. ((org-region-active-p)
  6042. ;; we will sort the region
  6043. (setq end (region-end)
  6044. what "region")
  6045. (goto-char (region-beginning))
  6046. (if (not (org-on-heading-p)) (outline-next-heading))
  6047. (setq start (point)))
  6048. ((org-at-item-p)
  6049. ;; we will sort this plain list
  6050. (org-beginning-of-item-list) (setq start (point))
  6051. (org-end-of-item-list) (setq end (point))
  6052. (goto-char start)
  6053. (setq plain-list-p t
  6054. what "plain list"))
  6055. ((or (org-on-heading-p)
  6056. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6057. ;; we will sort the children of the current headline
  6058. (org-back-to-heading)
  6059. (setq start (point)
  6060. end (progn (org-end-of-subtree t t)
  6061. (org-back-over-empty-lines)
  6062. (point))
  6063. what "children")
  6064. (goto-char start)
  6065. (show-subtree)
  6066. (outline-next-heading))
  6067. (t
  6068. ;; we will sort the top-level entries in this file
  6069. (goto-char (point-min))
  6070. (or (org-on-heading-p) (outline-next-heading))
  6071. (setq start (point) end (point-max) what "top-level")
  6072. (goto-char start)
  6073. (show-all)))
  6074. (setq beg (point))
  6075. (if (>= beg end) (error "Nothing to sort"))
  6076. (unless plain-list-p
  6077. (looking-at "\\(\\*+\\)")
  6078. (setq stars (match-string 1)
  6079. re (concat "^" (regexp-quote stars) " +")
  6080. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6081. txt (buffer-substring beg end))
  6082. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6083. (if (and (not (equal stars "*")) (string-match re2 txt))
  6084. (error "Region to sort contains a level above the first entry")))
  6085. (unless sorting-type
  6086. (message
  6087. (if plain-list-p
  6088. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6089. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6090. what)
  6091. (setq sorting-type (read-char-exclusive))
  6092. (and (= (downcase sorting-type) ?f)
  6093. (setq getkey-func
  6094. (completing-read "Sort using function: "
  6095. obarray 'fboundp t nil nil))
  6096. (setq getkey-func (intern getkey-func)))
  6097. (and (= (downcase sorting-type) ?r)
  6098. (setq property
  6099. (completing-read "Property: "
  6100. (mapcar 'list (org-buffer-property-keys t))
  6101. nil t))))
  6102. (message "Sorting entries...")
  6103. (save-restriction
  6104. (narrow-to-region start end)
  6105. (let ((dcst (downcase sorting-type))
  6106. (now (current-time)))
  6107. (sort-subr
  6108. (/= dcst sorting-type)
  6109. ;; This function moves to the beginning character of the "record" to
  6110. ;; be sorted.
  6111. (if plain-list-p
  6112. (lambda nil
  6113. (if (org-at-item-p) t (goto-char (point-max))))
  6114. (lambda nil
  6115. (if (re-search-forward re nil t)
  6116. (goto-char (match-beginning 0))
  6117. (goto-char (point-max)))))
  6118. ;; This function moves to the last character of the "record" being
  6119. ;; sorted.
  6120. (if plain-list-p
  6121. 'org-end-of-item
  6122. (lambda nil
  6123. (save-match-data
  6124. (condition-case nil
  6125. (outline-forward-same-level 1)
  6126. (error
  6127. (goto-char (point-max)))))))
  6128. ;; This function returns the value that gets sorted against.
  6129. (if plain-list-p
  6130. (lambda nil
  6131. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6132. (cond
  6133. ((= dcst ?n)
  6134. (string-to-number (buffer-substring (match-end 0)
  6135. (point-at-eol))))
  6136. ((= dcst ?a)
  6137. (buffer-substring (match-end 0) (point-at-eol)))
  6138. ((= dcst ?t)
  6139. (if (re-search-forward org-ts-regexp
  6140. (point-at-eol) t)
  6141. (org-time-string-to-time (match-string 0))
  6142. now))
  6143. ((= dcst ?f)
  6144. (if getkey-func
  6145. (progn
  6146. (setq tmp (funcall getkey-func))
  6147. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6148. tmp)
  6149. (error "Invalid key function `%s'" getkey-func)))
  6150. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6151. (lambda nil
  6152. (cond
  6153. ((= dcst ?n)
  6154. (if (looking-at outline-regexp)
  6155. (string-to-number (buffer-substring (match-end 0)
  6156. (point-at-eol)))
  6157. nil))
  6158. ((= dcst ?a)
  6159. (funcall case-func (buffer-substring (point-at-bol)
  6160. (point-at-eol))))
  6161. ((= dcst ?t)
  6162. (if (re-search-forward org-ts-regexp
  6163. (save-excursion
  6164. (forward-line 2)
  6165. (point)) t)
  6166. (org-time-string-to-time (match-string 0))
  6167. now))
  6168. ((= dcst ?p)
  6169. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6170. (string-to-char (match-string 2))
  6171. org-default-priority))
  6172. ((= dcst ?r)
  6173. (or (org-entry-get nil property) ""))
  6174. ((= dcst ?f)
  6175. (if getkey-func
  6176. (progn
  6177. (setq tmp (funcall getkey-func))
  6178. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6179. tmp)
  6180. (error "Invalid key function `%s'" getkey-func)))
  6181. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6182. nil
  6183. (cond
  6184. ((= dcst ?a) 'string<)
  6185. ((= dcst ?t) 'time-less-p)
  6186. (t nil)))))
  6187. (message "Sorting entries...done")))
  6188. (defun org-do-sort (table what &optional with-case sorting-type)
  6189. "Sort TABLE of WHAT according to SORTING-TYPE.
  6190. The user will be prompted for the SORTING-TYPE if the call to this
  6191. function does not specify it. WHAT is only for the prompt, to indicate
  6192. what is being sorted. The sorting key will be extracted from
  6193. the car of the elements of the table.
  6194. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6195. (unless sorting-type
  6196. (message
  6197. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6198. what)
  6199. (setq sorting-type (read-char-exclusive)))
  6200. (let ((dcst (downcase sorting-type))
  6201. extractfun comparefun)
  6202. ;; Define the appropriate functions
  6203. (cond
  6204. ((= dcst ?n)
  6205. (setq extractfun 'string-to-number
  6206. comparefun (if (= dcst sorting-type) '< '>)))
  6207. ((= dcst ?a)
  6208. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6209. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6210. comparefun (if (= dcst sorting-type)
  6211. 'string<
  6212. (lambda (a b) (and (not (string< a b))
  6213. (not (string= a b)))))))
  6214. ((= dcst ?t)
  6215. (setq extractfun
  6216. (lambda (x)
  6217. (if (string-match org-ts-regexp x)
  6218. (time-to-seconds
  6219. (org-time-string-to-time (match-string 0 x)))
  6220. 0))
  6221. comparefun (if (= dcst sorting-type) '< '>)))
  6222. (t (error "Invalid sorting type `%c'" sorting-type)))
  6223. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6224. table)
  6225. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6226. ;;;; Plain list items, including checkboxes
  6227. ;;; Plain list items
  6228. (defun org-at-item-p ()
  6229. "Is point in a line starting a hand-formatted item?"
  6230. (let ((llt org-plain-list-ordered-item-terminator))
  6231. (save-excursion
  6232. (goto-char (point-at-bol))
  6233. (looking-at
  6234. (cond
  6235. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6236. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6237. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6238. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6239. (defun org-in-item-p ()
  6240. "It the cursor inside a plain list item.
  6241. Does not have to be the first line."
  6242. (save-excursion
  6243. (condition-case nil
  6244. (progn
  6245. (org-beginning-of-item)
  6246. (org-at-item-p)
  6247. t)
  6248. (error nil))))
  6249. (defun org-insert-item (&optional checkbox)
  6250. "Insert a new item at the current level.
  6251. Return t when things worked, nil when we are not in an item."
  6252. (when (save-excursion
  6253. (condition-case nil
  6254. (progn
  6255. (org-beginning-of-item)
  6256. (org-at-item-p)
  6257. (if (org-invisible-p) (error "Invisible item"))
  6258. t)
  6259. (error nil)))
  6260. (let* ((bul (match-string 0))
  6261. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6262. (match-end 0)))
  6263. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6264. pos)
  6265. (cond
  6266. ((and (org-at-item-p) (<= (point) eow))
  6267. ;; before the bullet
  6268. (beginning-of-line 1)
  6269. (open-line (if blank 2 1)))
  6270. ((<= (point) eow)
  6271. (beginning-of-line 1))
  6272. (t
  6273. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6274. (end-of-line 1)
  6275. (just-one-space ))
  6276. (newline (if blank 2 1))))
  6277. (insert bul (if checkbox "[ ]" ""))
  6278. (just-one-space)
  6279. (setq pos (point))
  6280. (end-of-line 1)
  6281. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6282. (org-maybe-renumber-ordered-list)
  6283. (and checkbox (org-update-checkbox-count-maybe))
  6284. t))
  6285. ;;; Checkboxes
  6286. (defun org-at-item-checkbox-p ()
  6287. "Is point at a line starting a plain-list item with a checklet?"
  6288. (and (org-at-item-p)
  6289. (save-excursion
  6290. (goto-char (match-end 0))
  6291. (skip-chars-forward " \t")
  6292. (looking-at "\\[[- X]\\]"))))
  6293. (defun org-toggle-checkbox (&optional arg)
  6294. "Toggle the checkbox in the current line."
  6295. (interactive "P")
  6296. (catch 'exit
  6297. (let (beg end status (firstnew 'unknown))
  6298. (cond
  6299. ((org-region-active-p)
  6300. (setq beg (region-beginning) end (region-end)))
  6301. ((org-on-heading-p)
  6302. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6303. ((org-at-item-checkbox-p)
  6304. (let ((pos (point)))
  6305. (replace-match
  6306. (cond (arg "[-]")
  6307. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6308. (t "[ ]"))
  6309. t t)
  6310. (goto-char pos))
  6311. (throw 'exit t))
  6312. (t (error "Not at a checkbox or heading, and no active region")))
  6313. (save-excursion
  6314. (goto-char beg)
  6315. (while (< (point) end)
  6316. (when (org-at-item-checkbox-p)
  6317. (setq status (equal (match-string 0) "[X]"))
  6318. (when (eq firstnew 'unknown)
  6319. (setq firstnew (not status)))
  6320. (replace-match
  6321. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6322. (beginning-of-line 2)))))
  6323. (org-update-checkbox-count-maybe))
  6324. (defun org-update-checkbox-count-maybe ()
  6325. "Update checkbox statistics unless turned off by user."
  6326. (when org-provide-checkbox-statistics
  6327. (org-update-checkbox-count)))
  6328. (defun org-update-checkbox-count (&optional all)
  6329. "Update the checkbox statistics in the current section.
  6330. This will find all statistic cookies like [57%] and [6/12] and update them
  6331. with the current numbers. With optional prefix argument ALL, do this for
  6332. the whole buffer."
  6333. (interactive "P")
  6334. (save-excursion
  6335. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6336. (beg (condition-case nil
  6337. (progn (outline-back-to-heading) (point))
  6338. (error (point-min))))
  6339. (end (move-marker (make-marker)
  6340. (progn (outline-next-heading) (point))))
  6341. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6342. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6343. (re-find (concat re "\\|" re-box))
  6344. beg-cookie end-cookie is-percent c-on c-off lim
  6345. eline curr-ind next-ind continue-from startsearch
  6346. (cstat 0)
  6347. )
  6348. (when all
  6349. (goto-char (point-min))
  6350. (outline-next-heading)
  6351. (setq beg (point) end (point-max)))
  6352. (goto-char end)
  6353. ;; find each statistic cookie
  6354. (while (re-search-backward re-find beg t)
  6355. (setq beg-cookie (match-beginning 1)
  6356. end-cookie (match-end 1)
  6357. cstat (+ cstat (if end-cookie 1 0))
  6358. startsearch (point-at-eol)
  6359. continue-from (point-at-bol)
  6360. is-percent (match-beginning 2)
  6361. lim (cond
  6362. ((org-on-heading-p) (outline-next-heading) (point))
  6363. ((org-at-item-p) (org-end-of-item) (point))
  6364. (t nil))
  6365. c-on 0
  6366. c-off 0)
  6367. (when lim
  6368. ;; find first checkbox for this cookie and gather
  6369. ;; statistics from all that are at this indentation level
  6370. (goto-char startsearch)
  6371. (if (re-search-forward re-box lim t)
  6372. (progn
  6373. (org-beginning-of-item)
  6374. (setq curr-ind (org-get-indentation))
  6375. (setq next-ind curr-ind)
  6376. (while (= curr-ind next-ind)
  6377. (save-excursion (end-of-line) (setq eline (point)))
  6378. (if (re-search-forward re-box eline t)
  6379. (if (member (match-string 2) '("[ ]" "[-]"))
  6380. (setq c-off (1+ c-off))
  6381. (setq c-on (1+ c-on))
  6382. )
  6383. )
  6384. (org-end-of-item)
  6385. (setq next-ind (org-get-indentation))
  6386. )))
  6387. (goto-char continue-from)
  6388. ;; update cookie
  6389. (when end-cookie
  6390. (delete-region beg-cookie end-cookie)
  6391. (goto-char beg-cookie)
  6392. (insert
  6393. (if is-percent
  6394. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6395. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6396. ;; update items checkbox if it has one
  6397. (when (org-at-item-p)
  6398. (org-beginning-of-item)
  6399. (when (and (> (+ c-on c-off) 0)
  6400. (re-search-forward re-box (point-at-eol) t))
  6401. (setq beg-cookie (match-beginning 2)
  6402. end-cookie (match-end 2))
  6403. (delete-region beg-cookie end-cookie)
  6404. (goto-char beg-cookie)
  6405. (cond ((= c-off 0) (insert "[X]"))
  6406. ((= c-on 0) (insert "[ ]"))
  6407. (t (insert "[-]")))
  6408. )))
  6409. (goto-char continue-from))
  6410. (when (interactive-p)
  6411. (message "Checkbox satistics updated %s (%d places)"
  6412. (if all "in entire file" "in current outline entry") cstat)))))
  6413. (defun org-get-checkbox-statistics-face ()
  6414. "Select the face for checkbox statistics.
  6415. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6416. it will be `org-todo'."
  6417. (if (match-end 1)
  6418. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6419. (if (and (> (match-end 2) (match-beginning 2))
  6420. (equal (match-string 2) (match-string 3)))
  6421. 'org-done
  6422. 'org-todo)))
  6423. (defun org-get-indentation (&optional line)
  6424. "Get the indentation of the current line, interpreting tabs.
  6425. When LINE is given, assume it represents a line and compute its indentation."
  6426. (if line
  6427. (if (string-match "^ *" (org-remove-tabs line))
  6428. (match-end 0))
  6429. (save-excursion
  6430. (beginning-of-line 1)
  6431. (skip-chars-forward " \t")
  6432. (current-column))))
  6433. (defun org-remove-tabs (s &optional width)
  6434. "Replace tabulators in S with spaces.
  6435. Assumes that s is a single line, starting in column 0."
  6436. (setq width (or width tab-width))
  6437. (while (string-match "\t" s)
  6438. (setq s (replace-match
  6439. (make-string
  6440. (- (* width (/ (+ (match-beginning 0) width) width))
  6441. (match-beginning 0)) ?\ )
  6442. t t s)))
  6443. s)
  6444. (defun org-fix-indentation (line ind)
  6445. "Fix indentation in LINE.
  6446. IND is a cons cell with target and minimum indentation.
  6447. If the current indenation in LINE is smaller than the minimum,
  6448. leave it alone. If it is larger than ind, set it to the target."
  6449. (let* ((l (org-remove-tabs line))
  6450. (i (org-get-indentation l))
  6451. (i1 (car ind)) (i2 (cdr ind)))
  6452. (if (>= i i2) (setq l (substring line i2)))
  6453. (if (> i1 0)
  6454. (concat (make-string i1 ?\ ) l)
  6455. l)))
  6456. (defcustom org-empty-line-terminates-plain-lists nil
  6457. "Non-nil means, an empty line ends all plain list levels.
  6458. When nil, empty lines are part of the preceeding item."
  6459. :group 'org-plain-lists
  6460. :type 'boolean)
  6461. (defun org-beginning-of-item ()
  6462. "Go to the beginning of the current hand-formatted item.
  6463. If the cursor is not in an item, throw an error."
  6464. (interactive)
  6465. (let ((pos (point))
  6466. (limit (save-excursion
  6467. (condition-case nil
  6468. (progn
  6469. (org-back-to-heading)
  6470. (beginning-of-line 2) (point))
  6471. (error (point-min)))))
  6472. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6473. ind ind1)
  6474. (if (org-at-item-p)
  6475. (beginning-of-line 1)
  6476. (beginning-of-line 1)
  6477. (skip-chars-forward " \t")
  6478. (setq ind (current-column))
  6479. (if (catch 'exit
  6480. (while t
  6481. (beginning-of-line 0)
  6482. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6483. (if (looking-at "[ \t]*$")
  6484. (setq ind1 ind-empty)
  6485. (skip-chars-forward " \t")
  6486. (setq ind1 (current-column)))
  6487. (if (< ind1 ind)
  6488. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6489. nil
  6490. (goto-char pos)
  6491. (error "Not in an item")))))
  6492. (defun org-end-of-item ()
  6493. "Go to the end of the current hand-formatted item.
  6494. If the cursor is not in an item, throw an error."
  6495. (interactive)
  6496. (let* ((pos (point))
  6497. ind1
  6498. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6499. (limit (save-excursion (outline-next-heading) (point)))
  6500. (ind (save-excursion
  6501. (org-beginning-of-item)
  6502. (skip-chars-forward " \t")
  6503. (current-column)))
  6504. (end (catch 'exit
  6505. (while t
  6506. (beginning-of-line 2)
  6507. (if (eobp) (throw 'exit (point)))
  6508. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6509. (if (looking-at "[ \t]*$")
  6510. (setq ind1 ind-empty)
  6511. (skip-chars-forward " \t")
  6512. (setq ind1 (current-column)))
  6513. (if (<= ind1 ind)
  6514. (throw 'exit (point-at-bol)))))))
  6515. (if end
  6516. (goto-char end)
  6517. (goto-char pos)
  6518. (error "Not in an item"))))
  6519. (defun org-next-item ()
  6520. "Move to the beginning of the next item in the current plain list.
  6521. Error if not at a plain list, or if this is the last item in the list."
  6522. (interactive)
  6523. (let (ind ind1 (pos (point)))
  6524. (org-beginning-of-item)
  6525. (setq ind (org-get-indentation))
  6526. (org-end-of-item)
  6527. (setq ind1 (org-get-indentation))
  6528. (unless (and (org-at-item-p) (= ind ind1))
  6529. (goto-char pos)
  6530. (error "On last item"))))
  6531. (defun org-previous-item ()
  6532. "Move to the beginning of the previous item in the current plain list.
  6533. Error if not at a plain list, or if this is the first item in the list."
  6534. (interactive)
  6535. (let (beg ind ind1 (pos (point)))
  6536. (org-beginning-of-item)
  6537. (setq beg (point))
  6538. (setq ind (org-get-indentation))
  6539. (goto-char beg)
  6540. (catch 'exit
  6541. (while t
  6542. (beginning-of-line 0)
  6543. (if (looking-at "[ \t]*$")
  6544. nil
  6545. (if (<= (setq ind1 (org-get-indentation)) ind)
  6546. (throw 'exit t)))))
  6547. (condition-case nil
  6548. (if (or (not (org-at-item-p))
  6549. (< ind1 (1- ind)))
  6550. (error "")
  6551. (org-beginning-of-item))
  6552. (error (goto-char pos)
  6553. (error "On first item")))))
  6554. (defun org-first-list-item-p ()
  6555. "Is this heading the item in a plain list?"
  6556. (unless (org-at-item-p)
  6557. (error "Not at a plain list item"))
  6558. (org-beginning-of-item)
  6559. (= (point) (save-excursion (org-beginning-of-item-list))))
  6560. (defun org-move-item-down ()
  6561. "Move the plain list item at point down, i.e. swap with following item.
  6562. Subitems (items with larger indentation) are considered part of the item,
  6563. so this really moves item trees."
  6564. (interactive)
  6565. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6566. (org-beginning-of-item)
  6567. (setq beg0 (point))
  6568. (save-excursion
  6569. (setq ne-beg (org-back-over-empty-lines))
  6570. (setq beg (point)))
  6571. (goto-char beg0)
  6572. (setq ind (org-get-indentation))
  6573. (org-end-of-item)
  6574. (setq end0 (point))
  6575. (setq ind1 (org-get-indentation))
  6576. (setq ne-end (org-back-over-empty-lines))
  6577. (setq end (point))
  6578. (goto-char beg0)
  6579. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6580. ;; include less whitespace
  6581. (save-excursion
  6582. (goto-char beg)
  6583. (forward-line (- ne-beg ne-end))
  6584. (setq beg (point))))
  6585. (goto-char end0)
  6586. (if (and (org-at-item-p) (= ind ind1))
  6587. (progn
  6588. (org-end-of-item)
  6589. (org-back-over-empty-lines)
  6590. (setq txt (buffer-substring beg end))
  6591. (save-excursion
  6592. (delete-region beg end))
  6593. (setq pos (point))
  6594. (insert txt)
  6595. (goto-char pos) (org-skip-whitespace)
  6596. (org-maybe-renumber-ordered-list))
  6597. (goto-char pos)
  6598. (error "Cannot move this item further down"))))
  6599. (defun org-move-item-up (arg)
  6600. "Move the plain list item at point up, i.e. swap with previous item.
  6601. Subitems (items with larger indentation) are considered part of the item,
  6602. so this really moves item trees."
  6603. (interactive "p")
  6604. (let (beg beg0 end ind ind1 (pos (point)) txt
  6605. ne-beg ne-ins ins-end)
  6606. (org-beginning-of-item)
  6607. (setq beg0 (point))
  6608. (setq ind (org-get-indentation))
  6609. (save-excursion
  6610. (setq ne-beg (org-back-over-empty-lines))
  6611. (setq beg (point)))
  6612. (goto-char beg0)
  6613. (org-end-of-item)
  6614. (setq end (point))
  6615. (goto-char beg0)
  6616. (catch 'exit
  6617. (while t
  6618. (beginning-of-line 0)
  6619. (if (looking-at "[ \t]*$")
  6620. (if org-empty-line-terminates-plain-lists
  6621. (progn
  6622. (goto-char pos)
  6623. (error "Cannot move this item further up"))
  6624. nil)
  6625. (if (<= (setq ind1 (org-get-indentation)) ind)
  6626. (throw 'exit t)))))
  6627. (condition-case nil
  6628. (org-beginning-of-item)
  6629. (error (goto-char beg)
  6630. (error "Cannot move this item further up")))
  6631. (setq ind1 (org-get-indentation))
  6632. (if (and (org-at-item-p) (= ind ind1))
  6633. (progn
  6634. (setq ne-ins (org-back-over-empty-lines))
  6635. (setq txt (buffer-substring beg end))
  6636. (save-excursion
  6637. (delete-region beg end))
  6638. (setq pos (point))
  6639. (insert txt)
  6640. (setq ins-end (point))
  6641. (goto-char pos) (org-skip-whitespace)
  6642. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6643. ;; Move whitespace back to beginning
  6644. (save-excursion
  6645. (goto-char ins-end)
  6646. (let ((kill-whole-line t))
  6647. (kill-line (- ne-ins ne-beg)) (point)))
  6648. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6649. (org-maybe-renumber-ordered-list))
  6650. (goto-char pos)
  6651. (error "Cannot move this item further up"))))
  6652. (defun org-maybe-renumber-ordered-list ()
  6653. "Renumber the ordered list at point if setup allows it.
  6654. This tests the user option `org-auto-renumber-ordered-lists' before
  6655. doing the renumbering."
  6656. (interactive)
  6657. (when (and org-auto-renumber-ordered-lists
  6658. (org-at-item-p))
  6659. (if (match-beginning 3)
  6660. (org-renumber-ordered-list 1)
  6661. (org-fix-bullet-type))))
  6662. (defun org-maybe-renumber-ordered-list-safe ()
  6663. (condition-case nil
  6664. (save-excursion
  6665. (org-maybe-renumber-ordered-list))
  6666. (error nil)))
  6667. (defun org-cycle-list-bullet (&optional which)
  6668. "Cycle through the different itemize/enumerate bullets.
  6669. This cycle the entire list level through the sequence:
  6670. `-' -> `+' -> `*' -> `1.' -> `1)'
  6671. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6672. 0 meand `-', 1 means `+' etc."
  6673. (interactive "P")
  6674. (org-preserve-lc
  6675. (org-beginning-of-item-list)
  6676. (org-at-item-p)
  6677. (beginning-of-line 1)
  6678. (let ((current (match-string 0))
  6679. (prevp (eq which 'previous))
  6680. new)
  6681. (setq new (cond
  6682. ((and (numberp which)
  6683. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6684. ((string-match "-" current) (if prevp "1)" "+"))
  6685. ((string-match "\\+" current)
  6686. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6687. ((string-match "\\*" current) (if prevp "+" "1."))
  6688. ((string-match "\\." current) (if prevp "*" "1)"))
  6689. ((string-match ")" current) (if prevp "1." "-"))
  6690. (t (error "This should not happen"))))
  6691. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6692. (org-fix-bullet-type)
  6693. (org-maybe-renumber-ordered-list))))
  6694. (defun org-get-string-indentation (s)
  6695. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6696. (let ((n -1) (i 0) (w tab-width) c)
  6697. (catch 'exit
  6698. (while (< (setq n (1+ n)) (length s))
  6699. (setq c (aref s n))
  6700. (cond ((= c ?\ ) (setq i (1+ i)))
  6701. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6702. (t (throw 'exit t)))))
  6703. i))
  6704. (defun org-renumber-ordered-list (arg)
  6705. "Renumber an ordered plain list.
  6706. Cursor needs to be in the first line of an item, the line that starts
  6707. with something like \"1.\" or \"2)\"."
  6708. (interactive "p")
  6709. (unless (and (org-at-item-p)
  6710. (match-beginning 3))
  6711. (error "This is not an ordered list"))
  6712. (let ((line (org-current-line))
  6713. (col (current-column))
  6714. (ind (org-get-string-indentation
  6715. (buffer-substring (point-at-bol) (match-beginning 3))))
  6716. ;; (term (substring (match-string 3) -1))
  6717. ind1 (n (1- arg))
  6718. fmt)
  6719. ;; find where this list begins
  6720. (org-beginning-of-item-list)
  6721. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6722. (setq fmt (concat "%d" (match-string 1)))
  6723. (beginning-of-line 0)
  6724. ;; walk forward and replace these numbers
  6725. (catch 'exit
  6726. (while t
  6727. (catch 'next
  6728. (beginning-of-line 2)
  6729. (if (eobp) (throw 'exit nil))
  6730. (if (looking-at "[ \t]*$") (throw 'next nil))
  6731. (skip-chars-forward " \t") (setq ind1 (current-column))
  6732. (if (> ind1 ind) (throw 'next t))
  6733. (if (< ind1 ind) (throw 'exit t))
  6734. (if (not (org-at-item-p)) (throw 'exit nil))
  6735. (delete-region (match-beginning 2) (match-end 2))
  6736. (goto-char (match-beginning 2))
  6737. (insert (format fmt (setq n (1+ n)))))))
  6738. (goto-line line)
  6739. (move-to-column col)))
  6740. (defun org-fix-bullet-type ()
  6741. "Make sure all items in this list have the same bullet as the firsst item."
  6742. (interactive)
  6743. (unless (org-at-item-p) (error "This is not a list"))
  6744. (let ((line (org-current-line))
  6745. (col (current-column))
  6746. (ind (current-indentation))
  6747. ind1 bullet)
  6748. ;; find where this list begins
  6749. (org-beginning-of-item-list)
  6750. (beginning-of-line 1)
  6751. ;; find out what the bullet type is
  6752. (looking-at "[ \t]*\\(\\S-+\\)")
  6753. (setq bullet (match-string 1))
  6754. ;; walk forward and replace these numbers
  6755. (beginning-of-line 0)
  6756. (catch 'exit
  6757. (while t
  6758. (catch 'next
  6759. (beginning-of-line 2)
  6760. (if (eobp) (throw 'exit nil))
  6761. (if (looking-at "[ \t]*$") (throw 'next nil))
  6762. (skip-chars-forward " \t") (setq ind1 (current-column))
  6763. (if (> ind1 ind) (throw 'next t))
  6764. (if (< ind1 ind) (throw 'exit t))
  6765. (if (not (org-at-item-p)) (throw 'exit nil))
  6766. (skip-chars-forward " \t")
  6767. (looking-at "\\S-+")
  6768. (replace-match bullet))))
  6769. (goto-line line)
  6770. (move-to-column col)
  6771. (if (string-match "[0-9]" bullet)
  6772. (org-renumber-ordered-list 1))))
  6773. (defun org-beginning-of-item-list ()
  6774. "Go to the beginning of the current item list.
  6775. I.e. to the first item in this list."
  6776. (interactive)
  6777. (org-beginning-of-item)
  6778. (let ((pos (point-at-bol))
  6779. (ind (org-get-indentation))
  6780. ind1)
  6781. ;; find where this list begins
  6782. (catch 'exit
  6783. (while t
  6784. (catch 'next
  6785. (beginning-of-line 0)
  6786. (if (looking-at "[ \t]*$")
  6787. (throw (if (bobp) 'exit 'next) t))
  6788. (skip-chars-forward " \t") (setq ind1 (current-column))
  6789. (if (or (< ind1 ind)
  6790. (and (= ind1 ind)
  6791. (not (org-at-item-p)))
  6792. (bobp))
  6793. (throw 'exit t)
  6794. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6795. (goto-char pos)))
  6796. (defun org-end-of-item-list ()
  6797. "Go to the end of the current item list.
  6798. I.e. to the text after the last item."
  6799. (interactive)
  6800. (org-beginning-of-item)
  6801. (let ((pos (point-at-bol))
  6802. (ind (org-get-indentation))
  6803. ind1)
  6804. ;; find where this list begins
  6805. (catch 'exit
  6806. (while t
  6807. (catch 'next
  6808. (beginning-of-line 2)
  6809. (if (looking-at "[ \t]*$")
  6810. (throw (if (eobp) 'exit 'next) t))
  6811. (skip-chars-forward " \t") (setq ind1 (current-column))
  6812. (if (or (< ind1 ind)
  6813. (and (= ind1 ind)
  6814. (not (org-at-item-p)))
  6815. (eobp))
  6816. (progn
  6817. (setq pos (point-at-bol))
  6818. (throw 'exit t))))))
  6819. (goto-char pos)))
  6820. (defvar org-last-indent-begin-marker (make-marker))
  6821. (defvar org-last-indent-end-marker (make-marker))
  6822. (defun org-outdent-item (arg)
  6823. "Outdent a local list item."
  6824. (interactive "p")
  6825. (org-indent-item (- arg)))
  6826. (defun org-indent-item (arg)
  6827. "Indent a local list item."
  6828. (interactive "p")
  6829. (unless (org-at-item-p)
  6830. (error "Not on an item"))
  6831. (save-excursion
  6832. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6833. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6834. (setq beg org-last-indent-begin-marker
  6835. end org-last-indent-end-marker)
  6836. (org-beginning-of-item)
  6837. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6838. (org-end-of-item)
  6839. (setq end (move-marker org-last-indent-end-marker (point))))
  6840. (goto-char beg)
  6841. (setq tmp (org-item-indent-positions)
  6842. ind (car tmp)
  6843. ind-down (nth 2 tmp)
  6844. ind-up (nth 1 tmp)
  6845. delta (if (> arg 0)
  6846. (if ind-down (- ind-down ind) 2)
  6847. (if ind-up (- ind-up ind) -2)))
  6848. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6849. (while (< (point) end)
  6850. (beginning-of-line 1)
  6851. (skip-chars-forward " \t") (setq ind1 (current-column))
  6852. (delete-region (point-at-bol) (point))
  6853. (or (eolp) (indent-to-column (+ ind1 delta)))
  6854. (beginning-of-line 2))))
  6855. (org-fix-bullet-type)
  6856. (org-maybe-renumber-ordered-list-safe)
  6857. (save-excursion
  6858. (beginning-of-line 0)
  6859. (condition-case nil (org-beginning-of-item) (error nil))
  6860. (org-maybe-renumber-ordered-list-safe)))
  6861. (defun org-item-indent-positions ()
  6862. "Return indentation for plain list items.
  6863. This returns a list with three values: The current indentation, the
  6864. parent indentation and the indentation a child should habe.
  6865. Assumes cursor in item line."
  6866. (let* ((bolpos (point-at-bol))
  6867. (ind (org-get-indentation))
  6868. ind-down ind-up pos)
  6869. (save-excursion
  6870. (org-beginning-of-item-list)
  6871. (skip-chars-backward "\n\r \t")
  6872. (when (org-in-item-p)
  6873. (org-beginning-of-item)
  6874. (setq ind-up (org-get-indentation))))
  6875. (setq pos (point))
  6876. (save-excursion
  6877. (cond
  6878. ((and (condition-case nil (progn (org-previous-item) t)
  6879. (error nil))
  6880. (or (forward-char 1) t)
  6881. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6882. (setq ind-down (org-get-indentation)))
  6883. ((and (goto-char pos)
  6884. (org-at-item-p))
  6885. (goto-char (match-end 0))
  6886. (skip-chars-forward " \t")
  6887. (setq ind-down (current-column)))))
  6888. (list ind ind-up ind-down)))
  6889. ;;; The orgstruct minor mode
  6890. ;; Define a minor mode which can be used in other modes in order to
  6891. ;; integrate the org-mode structure editing commands.
  6892. ;; This is really a hack, because the org-mode structure commands use
  6893. ;; keys which normally belong to the major mode. Here is how it
  6894. ;; works: The minor mode defines all the keys necessary to operate the
  6895. ;; structure commands, but wraps the commands into a function which
  6896. ;; tests if the cursor is currently at a headline or a plain list
  6897. ;; item. If that is the case, the structure command is used,
  6898. ;; temporarily setting many Org-mode variables like regular
  6899. ;; expressions for filling etc. However, when any of those keys is
  6900. ;; used at a different location, function uses `key-binding' to look
  6901. ;; up if the key has an associated command in another currently active
  6902. ;; keymap (minor modes, major mode, global), and executes that
  6903. ;; command. There might be problems if any of the keys is otherwise
  6904. ;; used as a prefix key.
  6905. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6906. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6907. ;; addresses this by checking explicitly for both bindings.
  6908. (defvar orgstruct-mode-map (make-sparse-keymap)
  6909. "Keymap for the minor `orgstruct-mode'.")
  6910. (defvar org-local-vars nil
  6911. "List of local variables, for use by `orgstruct-mode'")
  6912. ;;;###autoload
  6913. (define-minor-mode orgstruct-mode
  6914. "Toggle the minor more `orgstruct-mode'.
  6915. This mode is for using Org-mode structure commands in other modes.
  6916. The following key behave as if Org-mode was active, if the cursor
  6917. is on a headline, or on a plain list item (both in the definition
  6918. of Org-mode).
  6919. M-up Move entry/item up
  6920. M-down Move entry/item down
  6921. M-left Promote
  6922. M-right Demote
  6923. M-S-up Move entry/item up
  6924. M-S-down Move entry/item down
  6925. M-S-left Promote subtree
  6926. M-S-right Demote subtree
  6927. M-q Fill paragraph and items like in Org-mode
  6928. C-c ^ Sort entries
  6929. C-c - Cycle list bullet
  6930. TAB Cycle item visibility
  6931. M-RET Insert new heading/item
  6932. S-M-RET Insert new TODO heading / Chekbox item
  6933. C-c C-c Set tags / toggle checkbox"
  6934. nil " OrgStruct" nil
  6935. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6936. ;;;###autoload
  6937. (defun turn-on-orgstruct ()
  6938. "Unconditionally turn on `orgstruct-mode'."
  6939. (orgstruct-mode 1))
  6940. ;;;###autoload
  6941. (defun turn-on-orgstruct++ ()
  6942. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6943. In addition to setting orgstruct-mode, this also exports all indentation and
  6944. autofilling variables from org-mode into the buffer. Note that turning
  6945. off orgstruct-mode will *not* remove these additional settings."
  6946. (orgstruct-mode 1)
  6947. (let (var val)
  6948. (mapc
  6949. (lambda (x)
  6950. (when (string-match
  6951. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6952. (symbol-name (car x)))
  6953. (setq var (car x) val (nth 1 x))
  6954. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6955. org-local-vars)))
  6956. (defun orgstruct-error ()
  6957. "Error when there is no default binding for a structure key."
  6958. (interactive)
  6959. (error "This key has no function outside structure elements"))
  6960. (defun orgstruct-setup ()
  6961. "Setup orgstruct keymaps."
  6962. (let ((nfunc 0)
  6963. (bindings
  6964. (list
  6965. '([(meta up)] org-metaup)
  6966. '([(meta down)] org-metadown)
  6967. '([(meta left)] org-metaleft)
  6968. '([(meta right)] org-metaright)
  6969. '([(meta shift up)] org-shiftmetaup)
  6970. '([(meta shift down)] org-shiftmetadown)
  6971. '([(meta shift left)] org-shiftmetaleft)
  6972. '([(meta shift right)] org-shiftmetaright)
  6973. '([(shift up)] org-shiftup)
  6974. '([(shift down)] org-shiftdown)
  6975. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6976. '("\M-q" fill-paragraph)
  6977. '("\C-c^" org-sort)
  6978. '("\C-c-" org-cycle-list-bullet)))
  6979. elt key fun cmd)
  6980. (while (setq elt (pop bindings))
  6981. (setq nfunc (1+ nfunc))
  6982. (setq key (org-key (car elt))
  6983. fun (nth 1 elt)
  6984. cmd (orgstruct-make-binding fun nfunc key))
  6985. (org-defkey orgstruct-mode-map key cmd))
  6986. ;; Special treatment needed for TAB and RET
  6987. (org-defkey orgstruct-mode-map [(tab)]
  6988. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6989. (org-defkey orgstruct-mode-map "\C-i"
  6990. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6991. (org-defkey orgstruct-mode-map "\M-\C-m"
  6992. (orgstruct-make-binding 'org-insert-heading 105
  6993. "\M-\C-m" [(meta return)]))
  6994. (org-defkey orgstruct-mode-map [(meta return)]
  6995. (orgstruct-make-binding 'org-insert-heading 106
  6996. [(meta return)] "\M-\C-m"))
  6997. (org-defkey orgstruct-mode-map [(shift meta return)]
  6998. (orgstruct-make-binding 'org-insert-todo-heading 107
  6999. [(meta return)] "\M-\C-m"))
  7000. (unless org-local-vars
  7001. (setq org-local-vars (org-get-local-variables)))
  7002. t))
  7003. (defun orgstruct-make-binding (fun n &rest keys)
  7004. "Create a function for binding in the structure minor mode.
  7005. FUN is the command to call inside a table. N is used to create a unique
  7006. command name. KEYS are keys that should be checked in for a command
  7007. to execute outside of tables."
  7008. (eval
  7009. (list 'defun
  7010. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7011. '(arg)
  7012. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7013. "Outside of structure, run the binding of `"
  7014. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7015. "'.")
  7016. '(interactive "p")
  7017. (list 'if
  7018. '(org-context-p 'headline 'item)
  7019. (list 'org-run-like-in-org-mode (list 'quote fun))
  7020. (list 'let '(orgstruct-mode)
  7021. (list 'call-interactively
  7022. (append '(or)
  7023. (mapcar (lambda (k)
  7024. (list 'key-binding k))
  7025. keys)
  7026. '('orgstruct-error))))))))
  7027. (defun org-context-p (&rest contexts)
  7028. "Check if local context is and of CONTEXTS.
  7029. Possible values in the list of contexts are `table', `headline', and `item'."
  7030. (let ((pos (point)))
  7031. (goto-char (point-at-bol))
  7032. (prog1 (or (and (memq 'table contexts)
  7033. (looking-at "[ \t]*|"))
  7034. (and (memq 'headline contexts)
  7035. (looking-at "\\*+"))
  7036. (and (memq 'item contexts)
  7037. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7038. (goto-char pos))))
  7039. (defun org-get-local-variables ()
  7040. "Return a list of all local variables in an org-mode buffer."
  7041. (let (varlist)
  7042. (with-current-buffer (get-buffer-create "*Org tmp*")
  7043. (erase-buffer)
  7044. (org-mode)
  7045. (setq varlist (buffer-local-variables)))
  7046. (kill-buffer "*Org tmp*")
  7047. (delq nil
  7048. (mapcar
  7049. (lambda (x)
  7050. (setq x
  7051. (if (symbolp x)
  7052. (list x)
  7053. (list (car x) (list 'quote (cdr x)))))
  7054. (if (string-match
  7055. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7056. (symbol-name (car x)))
  7057. x nil))
  7058. varlist))))
  7059. ;;;###autoload
  7060. (defun org-run-like-in-org-mode (cmd)
  7061. (unless org-local-vars
  7062. (setq org-local-vars (org-get-local-variables)))
  7063. (eval (list 'let org-local-vars
  7064. (list 'call-interactively (list 'quote cmd)))))
  7065. ;;;; Archiving
  7066. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7067. (defun org-archive-subtree (&optional find-done)
  7068. "Move the current subtree to the archive.
  7069. The archive can be a certain top-level heading in the current file, or in
  7070. a different file. The tree will be moved to that location, the subtree
  7071. heading be marked DONE, and the current time will be added.
  7072. When called with prefix argument FIND-DONE, find whole trees without any
  7073. open TODO items and archive them (after getting confirmation from the user).
  7074. If the cursor is not at a headline when this comand is called, try all level
  7075. 1 trees. If the cursor is on a headline, only try the direct children of
  7076. this heading."
  7077. (interactive "P")
  7078. (if find-done
  7079. (org-archive-all-done)
  7080. ;; Save all relevant TODO keyword-relatex variables
  7081. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7082. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7083. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7084. (tr-org-done-keywords org-done-keywords)
  7085. (tr-org-todo-regexp org-todo-regexp)
  7086. (tr-org-todo-line-regexp org-todo-line-regexp)
  7087. (tr-org-odd-levels-only org-odd-levels-only)
  7088. (this-buffer (current-buffer))
  7089. (org-archive-location org-archive-location)
  7090. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7091. ;; start of variables that will be used for saving context
  7092. ;; The compiler complains about them - keep them anyway!
  7093. (file (abbreviate-file-name (buffer-file-name)))
  7094. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7095. (time (format-time-string
  7096. (substring (cdr org-time-stamp-formats) 1 -1)
  7097. (current-time)))
  7098. afile heading buffer level newfile-p
  7099. category todo priority
  7100. ;; start of variables that will be used for savind context
  7101. ltags itags prop)
  7102. ;; Try to find a local archive location
  7103. (save-excursion
  7104. (save-restriction
  7105. (widen)
  7106. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7107. (if (and prop (string-match "\\S-" prop))
  7108. (setq org-archive-location prop)
  7109. (if (or (re-search-backward re nil t)
  7110. (re-search-forward re nil t))
  7111. (setq org-archive-location (match-string 1))))))
  7112. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7113. (progn
  7114. (setq afile (format (match-string 1 org-archive-location)
  7115. (file-name-nondirectory buffer-file-name))
  7116. heading (match-string 2 org-archive-location)))
  7117. (error "Invalid `org-archive-location'"))
  7118. (if (> (length afile) 0)
  7119. (setq newfile-p (not (file-exists-p afile))
  7120. buffer (find-file-noselect afile))
  7121. (setq buffer (current-buffer)))
  7122. (unless buffer
  7123. (error "Cannot access file \"%s\"" afile))
  7124. (if (and (> (length heading) 0)
  7125. (string-match "^\\*+" heading))
  7126. (setq level (match-end 0))
  7127. (setq heading nil level 0))
  7128. (save-excursion
  7129. (org-back-to-heading t)
  7130. ;; Get context information that will be lost by moving the tree
  7131. (org-refresh-category-properties)
  7132. (setq category (org-get-category)
  7133. todo (and (looking-at org-todo-line-regexp)
  7134. (match-string 2))
  7135. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7136. ltags (org-get-tags)
  7137. itags (org-delete-all ltags (org-get-tags-at)))
  7138. (setq ltags (mapconcat 'identity ltags " ")
  7139. itags (mapconcat 'identity itags " "))
  7140. ;; We first only copy, in case something goes wrong
  7141. ;; we need to protect this-command, to avoid kill-region sets it,
  7142. ;; which would lead to duplication of subtrees
  7143. (let (this-command) (org-copy-subtree))
  7144. (set-buffer buffer)
  7145. ;; Enforce org-mode for the archive buffer
  7146. (if (not (org-mode-p))
  7147. ;; Force the mode for future visits.
  7148. (let ((org-insert-mode-line-in-empty-file t)
  7149. (org-inhibit-startup t))
  7150. (call-interactively 'org-mode)))
  7151. (when newfile-p
  7152. (goto-char (point-max))
  7153. (insert (format "\nArchived entries from file %s\n\n"
  7154. (buffer-file-name this-buffer))))
  7155. ;; Force the TODO keywords of the original buffer
  7156. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7157. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7158. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7159. (org-done-keywords tr-org-done-keywords)
  7160. (org-todo-regexp tr-org-todo-regexp)
  7161. (org-todo-line-regexp tr-org-todo-line-regexp)
  7162. (org-odd-levels-only
  7163. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7164. org-odd-levels-only
  7165. tr-org-odd-levels-only)))
  7166. (goto-char (point-min))
  7167. (if heading
  7168. (progn
  7169. (if (re-search-forward
  7170. (concat "^" (regexp-quote heading)
  7171. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7172. nil t)
  7173. (goto-char (match-end 0))
  7174. ;; Heading not found, just insert it at the end
  7175. (goto-char (point-max))
  7176. (or (bolp) (insert "\n"))
  7177. (insert "\n" heading "\n")
  7178. (end-of-line 0))
  7179. ;; Make the subtree visible
  7180. (show-subtree)
  7181. (org-end-of-subtree t)
  7182. (skip-chars-backward " \t\r\n")
  7183. (and (looking-at "[ \t\r\n]*")
  7184. (replace-match "\n\n")))
  7185. ;; No specific heading, just go to end of file.
  7186. (goto-char (point-max)) (insert "\n"))
  7187. ;; Paste
  7188. (org-paste-subtree (org-get-legal-level level 1))
  7189. ;; Mark the entry as done
  7190. (when (and org-archive-mark-done
  7191. (looking-at org-todo-line-regexp)
  7192. (or (not (match-end 2))
  7193. (not (member (match-string 2) org-done-keywords))))
  7194. (let (org-log-done org-todo-log-states)
  7195. (org-todo
  7196. (car (or (member org-archive-mark-done org-done-keywords)
  7197. org-done-keywords)))))
  7198. ;; Add the context info
  7199. (when org-archive-save-context-info
  7200. (let ((l org-archive-save-context-info) e n v)
  7201. (while (setq e (pop l))
  7202. (when (and (setq v (symbol-value e))
  7203. (stringp v) (string-match "\\S-" v))
  7204. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7205. (org-entry-put (point) n v)))))
  7206. ;; Save the buffer, if it is not the same buffer.
  7207. (if (not (eq this-buffer buffer)) (save-buffer))))
  7208. ;; Here we are back in the original buffer. Everything seems to have
  7209. ;; worked. So now cut the tree and finish up.
  7210. (let (this-command) (org-cut-subtree))
  7211. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7212. (message "Subtree archived %s"
  7213. (if (eq this-buffer buffer)
  7214. (concat "under heading: " heading)
  7215. (concat "in file: " (abbreviate-file-name afile)))))))
  7216. (defun org-refresh-category-properties ()
  7217. "Refresh category text properties in teh buffer."
  7218. (let ((def-cat (cond
  7219. ((null org-category)
  7220. (if buffer-file-name
  7221. (file-name-sans-extension
  7222. (file-name-nondirectory buffer-file-name))
  7223. "???"))
  7224. ((symbolp org-category) (symbol-name org-category))
  7225. (t org-category)))
  7226. beg end cat pos optionp)
  7227. (org-unmodified
  7228. (save-excursion
  7229. (save-restriction
  7230. (widen)
  7231. (goto-char (point-min))
  7232. (put-text-property (point) (point-max) 'org-category def-cat)
  7233. (while (re-search-forward
  7234. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7235. (setq pos (match-end 0)
  7236. optionp (equal (char-after (match-beginning 0)) ?#)
  7237. cat (org-trim (match-string 2)))
  7238. (if optionp
  7239. (setq beg (point-at-bol) end (point-max))
  7240. (org-back-to-heading t)
  7241. (setq beg (point) end (org-end-of-subtree t t)))
  7242. (put-text-property beg end 'org-category cat)
  7243. (goto-char pos)))))))
  7244. (defun org-archive-all-done (&optional tag)
  7245. "Archive sublevels of the current tree without open TODO items.
  7246. If the cursor is not on a headline, try all level 1 trees. If
  7247. it is on a headline, try all direct children.
  7248. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7249. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7250. (rea (concat ".*:" org-archive-tag ":"))
  7251. (begm (make-marker))
  7252. (endm (make-marker))
  7253. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7254. "Move subtree to archive (no open TODO items)? "))
  7255. beg end (cntarch 0))
  7256. (if (org-on-heading-p)
  7257. (progn
  7258. (setq re1 (concat "^" (regexp-quote
  7259. (make-string
  7260. (1+ (- (match-end 0) (match-beginning 0) 1))
  7261. ?*))
  7262. " "))
  7263. (move-marker begm (point))
  7264. (move-marker endm (org-end-of-subtree t)))
  7265. (setq re1 "^* ")
  7266. (move-marker begm (point-min))
  7267. (move-marker endm (point-max)))
  7268. (save-excursion
  7269. (goto-char begm)
  7270. (while (re-search-forward re1 endm t)
  7271. (setq beg (match-beginning 0)
  7272. end (save-excursion (org-end-of-subtree t) (point)))
  7273. (goto-char beg)
  7274. (if (re-search-forward re end t)
  7275. (goto-char end)
  7276. (goto-char beg)
  7277. (if (and (or (not tag) (not (looking-at rea)))
  7278. (y-or-n-p question))
  7279. (progn
  7280. (if tag
  7281. (org-toggle-tag org-archive-tag 'on)
  7282. (org-archive-subtree))
  7283. (setq cntarch (1+ cntarch)))
  7284. (goto-char end)))))
  7285. (message "%d trees archived" cntarch)))
  7286. (defun org-cycle-hide-drawers (state)
  7287. "Re-hide all drawers after a visibility state change."
  7288. (when (and (org-mode-p)
  7289. (not (memq state '(overview folded))))
  7290. (save-excursion
  7291. (let* ((globalp (memq state '(contents all)))
  7292. (beg (if globalp (point-min) (point)))
  7293. (end (if globalp (point-max) (org-end-of-subtree t))))
  7294. (goto-char beg)
  7295. (while (re-search-forward org-drawer-regexp end t)
  7296. (org-flag-drawer t))))))
  7297. (defun org-flag-drawer (flag)
  7298. (save-excursion
  7299. (beginning-of-line 1)
  7300. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7301. (let ((b (match-end 0))
  7302. (outline-regexp org-outline-regexp))
  7303. (if (re-search-forward
  7304. "^[ \t]*:END:"
  7305. (save-excursion (outline-next-heading) (point)) t)
  7306. (outline-flag-region b (point-at-eol) flag)
  7307. (error ":END: line missing"))))))
  7308. (defun org-cycle-hide-archived-subtrees (state)
  7309. "Re-hide all archived subtrees after a visibility state change."
  7310. (when (and (not org-cycle-open-archived-trees)
  7311. (not (memq state '(overview folded))))
  7312. (save-excursion
  7313. (let* ((globalp (memq state '(contents all)))
  7314. (beg (if globalp (point-min) (point)))
  7315. (end (if globalp (point-max) (org-end-of-subtree t))))
  7316. (org-hide-archived-subtrees beg end)
  7317. (goto-char beg)
  7318. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7319. (message "%s" (substitute-command-keys
  7320. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7321. (defun org-force-cycle-archived ()
  7322. "Cycle subtree even if it is archived."
  7323. (interactive)
  7324. (setq this-command 'org-cycle)
  7325. (let ((org-cycle-open-archived-trees t))
  7326. (call-interactively 'org-cycle)))
  7327. (defun org-hide-archived-subtrees (beg end)
  7328. "Re-hide all archived subtrees after a visibility state change."
  7329. (save-excursion
  7330. (let* ((re (concat ":" org-archive-tag ":")))
  7331. (goto-char beg)
  7332. (while (re-search-forward re end t)
  7333. (and (org-on-heading-p) (hide-subtree))
  7334. (org-end-of-subtree t)))))
  7335. (defun org-toggle-tag (tag &optional onoff)
  7336. "Toggle the tag TAG for the current line.
  7337. If ONOFF is `on' or `off', don't toggle but set to this state."
  7338. (unless (org-on-heading-p t) (error "Not on headling"))
  7339. (let (res current)
  7340. (save-excursion
  7341. (beginning-of-line)
  7342. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7343. (point-at-eol) t)
  7344. (progn
  7345. (setq current (match-string 1))
  7346. (replace-match ""))
  7347. (setq current ""))
  7348. (setq current (nreverse (org-split-string current ":")))
  7349. (cond
  7350. ((eq onoff 'on)
  7351. (setq res t)
  7352. (or (member tag current) (push tag current)))
  7353. ((eq onoff 'off)
  7354. (or (not (member tag current)) (setq current (delete tag current))))
  7355. (t (if (member tag current)
  7356. (setq current (delete tag current))
  7357. (setq res t)
  7358. (push tag current))))
  7359. (end-of-line 1)
  7360. (if current
  7361. (progn
  7362. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7363. (org-set-tags nil t))
  7364. (delete-horizontal-space))
  7365. (run-hooks 'org-after-tags-change-hook))
  7366. res))
  7367. (defun org-toggle-archive-tag (&optional arg)
  7368. "Toggle the archive tag for the current headline.
  7369. With prefix ARG, check all children of current headline and offer tagging
  7370. the children that do not contain any open TODO items."
  7371. (interactive "P")
  7372. (if arg
  7373. (org-archive-all-done 'tag)
  7374. (let (set)
  7375. (save-excursion
  7376. (org-back-to-heading t)
  7377. (setq set (org-toggle-tag org-archive-tag))
  7378. (when set (hide-subtree)))
  7379. (and set (beginning-of-line 1))
  7380. (message "Subtree %s" (if set "archived" "unarchived")))))
  7381. ;;;; Tables
  7382. ;;; The table editor
  7383. ;; Watch out: Here we are talking about two different kind of tables.
  7384. ;; Most of the code is for the tables created with the Org-mode table editor.
  7385. ;; Sometimes, we talk about tables created and edited with the table.el
  7386. ;; Emacs package. We call the former org-type tables, and the latter
  7387. ;; table.el-type tables.
  7388. (defun org-before-change-function (beg end)
  7389. "Every change indicates that a table might need an update."
  7390. (setq org-table-may-need-update t))
  7391. (defconst org-table-line-regexp "^[ \t]*|"
  7392. "Detects an org-type table line.")
  7393. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7394. "Detects an org-type table line.")
  7395. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7396. "Detects a table line marked for automatic recalculation.")
  7397. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7398. "Detects a table line marked for automatic recalculation.")
  7399. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7400. "Detects a table line marked for automatic recalculation.")
  7401. (defconst org-table-hline-regexp "^[ \t]*|-"
  7402. "Detects an org-type table hline.")
  7403. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7404. "Detects a table-type table hline.")
  7405. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7406. "Detects an org-type or table-type table.")
  7407. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7408. "Searching from within a table (any type) this finds the first line
  7409. outside the table.")
  7410. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7411. "Searching from within a table (any type) this finds the first line
  7412. outside the table.")
  7413. (defvar org-table-last-highlighted-reference nil)
  7414. (defvar org-table-formula-history nil)
  7415. (defvar org-table-column-names nil
  7416. "Alist with column names, derived from the `!' line.")
  7417. (defvar org-table-column-name-regexp nil
  7418. "Regular expression matching the current column names.")
  7419. (defvar org-table-local-parameters nil
  7420. "Alist with parameter names, derived from the `$' line.")
  7421. (defvar org-table-named-field-locations nil
  7422. "Alist with locations of named fields.")
  7423. (defvar org-table-current-line-types nil
  7424. "Table row types, non-nil only for the duration of a comand.")
  7425. (defvar org-table-current-begin-line nil
  7426. "Table begin line, non-nil only for the duration of a comand.")
  7427. (defvar org-table-current-begin-pos nil
  7428. "Table begin position, non-nil only for the duration of a comand.")
  7429. (defvar org-table-dlines nil
  7430. "Vector of data line line numbers in the current table.")
  7431. (defvar org-table-hlines nil
  7432. "Vector of hline line numbers in the current table.")
  7433. (defconst org-table-range-regexp
  7434. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7435. ;; 1 2 3 4 5
  7436. "Regular expression for matching ranges in formulas.")
  7437. (defconst org-table-range-regexp2
  7438. (concat
  7439. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7440. "\\.\\."
  7441. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7442. "Match a range for reference display.")
  7443. (defconst org-table-translate-regexp
  7444. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7445. "Match a reference that needs translation, for reference display.")
  7446. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7447. (defun org-table-create-with-table.el ()
  7448. "Use the table.el package to insert a new table.
  7449. If there is already a table at point, convert between Org-mode tables
  7450. and table.el tables."
  7451. (interactive)
  7452. (require 'table)
  7453. (cond
  7454. ((org-at-table.el-p)
  7455. (if (y-or-n-p "Convert table to Org-mode table? ")
  7456. (org-table-convert)))
  7457. ((org-at-table-p)
  7458. (if (y-or-n-p "Convert table to table.el table? ")
  7459. (org-table-convert)))
  7460. (t (call-interactively 'table-insert))))
  7461. (defun org-table-create-or-convert-from-region (arg)
  7462. "Convert region to table, or create an empty table.
  7463. If there is an active region, convert it to a table, using the function
  7464. `org-table-convert-region'. See the documentation of that function
  7465. to learn how the prefix argument is interpreted to determine the field
  7466. separator.
  7467. If there is no such region, create an empty table with `org-table-create'."
  7468. (interactive "P")
  7469. (if (org-region-active-p)
  7470. (org-table-convert-region (region-beginning) (region-end) arg)
  7471. (org-table-create arg)))
  7472. (defun org-table-create (&optional size)
  7473. "Query for a size and insert a table skeleton.
  7474. SIZE is a string Columns x Rows like for example \"3x2\"."
  7475. (interactive "P")
  7476. (unless size
  7477. (setq size (read-string
  7478. (concat "Table size Columns x Rows [e.g. "
  7479. org-table-default-size "]: ")
  7480. "" nil org-table-default-size)))
  7481. (let* ((pos (point))
  7482. (indent (make-string (current-column) ?\ ))
  7483. (split (org-split-string size " *x *"))
  7484. (rows (string-to-number (nth 1 split)))
  7485. (columns (string-to-number (car split)))
  7486. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7487. "\n")))
  7488. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7489. (point-at-bol) (point)))
  7490. (beginning-of-line 1)
  7491. (newline))
  7492. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7493. (dotimes (i rows) (insert line))
  7494. (goto-char pos)
  7495. (if (> rows 1)
  7496. ;; Insert a hline after the first row.
  7497. (progn
  7498. (end-of-line 1)
  7499. (insert "\n|-")
  7500. (goto-char pos)))
  7501. (org-table-align)))
  7502. (defun org-table-convert-region (beg0 end0 &optional separator)
  7503. "Convert region to a table.
  7504. The region goes from BEG0 to END0, but these borders will be moved
  7505. slightly, to make sure a beginning of line in the first line is included.
  7506. SEPARATOR specifies the field separator in the lines. It can have the
  7507. following values:
  7508. '(4) Use the comma as a field separator
  7509. '(16) Use a TAB as field separator
  7510. integer When a number, use that many spaces as field separator
  7511. nil When nil, the command tries to be smart and figure out the
  7512. separator in the following way:
  7513. - when each line contains a TAB, assume TAB-separated material
  7514. - when each line contains a comme, assume CSV material
  7515. - else, assume one or more SPACE charcters as separator."
  7516. (interactive "rP")
  7517. (let* ((beg (min beg0 end0))
  7518. (end (max beg0 end0))
  7519. re)
  7520. (goto-char beg)
  7521. (beginning-of-line 1)
  7522. (setq beg (move-marker (make-marker) (point)))
  7523. (goto-char end)
  7524. (if (bolp) (backward-char 1) (end-of-line 1))
  7525. (setq end (move-marker (make-marker) (point)))
  7526. ;; Get the right field separator
  7527. (unless separator
  7528. (goto-char beg)
  7529. (setq separator
  7530. (cond
  7531. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7532. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7533. (t 1))))
  7534. (setq re (cond
  7535. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7536. ((equal separator '(16)) "^\\|\t")
  7537. ((integerp separator)
  7538. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7539. (t (error "This should not happen"))))
  7540. (goto-char beg)
  7541. (while (re-search-forward re end t)
  7542. (replace-match "| " t t))
  7543. (goto-char beg)
  7544. (insert " ")
  7545. (org-table-align)))
  7546. (defun org-table-import (file arg)
  7547. "Import FILE as a table.
  7548. The file is assumed to be tab-separated. Such files can be produced by most
  7549. spreadsheet and database applications. If no tabs (at least one per line)
  7550. are found, lines will be split on whitespace into fields."
  7551. (interactive "f\nP")
  7552. (or (bolp) (newline))
  7553. (let ((beg (point))
  7554. (pm (point-max)))
  7555. (insert-file-contents file)
  7556. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7557. (defun org-table-export ()
  7558. "Export table as a tab-separated file.
  7559. Such a file can be imported into a spreadsheet program like Excel."
  7560. (interactive)
  7561. (let* ((beg (org-table-begin))
  7562. (end (org-table-end))
  7563. (table (buffer-substring beg end))
  7564. (file (read-file-name "Export table to: "))
  7565. buf)
  7566. (unless (or (not (file-exists-p file))
  7567. (y-or-n-p (format "Overwrite file %s? " file)))
  7568. (error "Abort"))
  7569. (with-current-buffer (find-file-noselect file)
  7570. (setq buf (current-buffer))
  7571. (erase-buffer)
  7572. (fundamental-mode)
  7573. (insert table)
  7574. (goto-char (point-min))
  7575. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7576. (replace-match "" t t)
  7577. (end-of-line 1))
  7578. (goto-char (point-min))
  7579. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7580. (replace-match "" t t)
  7581. (goto-char (min (1+ (point)) (point-max))))
  7582. (goto-char (point-min))
  7583. (while (re-search-forward "^-[-+]*$" nil t)
  7584. (replace-match "")
  7585. (if (looking-at "\n")
  7586. (delete-char 1)))
  7587. (goto-char (point-min))
  7588. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7589. (replace-match "\t" t t))
  7590. (save-buffer))
  7591. (kill-buffer buf)))
  7592. (defvar org-table-aligned-begin-marker (make-marker)
  7593. "Marker at the beginning of the table last aligned.
  7594. Used to check if cursor still is in that table, to minimize realignment.")
  7595. (defvar org-table-aligned-end-marker (make-marker)
  7596. "Marker at the end of the table last aligned.
  7597. Used to check if cursor still is in that table, to minimize realignment.")
  7598. (defvar org-table-last-alignment nil
  7599. "List of flags for flushright alignment, from the last re-alignment.
  7600. This is being used to correctly align a single field after TAB or RET.")
  7601. (defvar org-table-last-column-widths nil
  7602. "List of max width of fields in each column.
  7603. This is being used to correctly align a single field after TAB or RET.")
  7604. (defvar org-table-overlay-coordinates nil
  7605. "Overlay coordinates after each align of a table.")
  7606. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7607. (defvar org-last-recalc-line nil)
  7608. (defconst org-narrow-column-arrow "=>"
  7609. "Used as display property in narrowed table columns.")
  7610. (defun org-table-align ()
  7611. "Align the table at point by aligning all vertical bars."
  7612. (interactive)
  7613. (let* (
  7614. ;; Limits of table
  7615. (beg (org-table-begin))
  7616. (end (org-table-end))
  7617. ;; Current cursor position
  7618. (linepos (org-current-line))
  7619. (colpos (org-table-current-column))
  7620. (winstart (window-start))
  7621. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7622. lines (new "") lengths l typenums ty fields maxfields i
  7623. column
  7624. (indent "") cnt frac
  7625. rfmt hfmt
  7626. (spaces '(1 . 1))
  7627. (sp1 (car spaces))
  7628. (sp2 (cdr spaces))
  7629. (rfmt1 (concat
  7630. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7631. (hfmt1 (concat
  7632. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7633. emptystrings links dates emph narrow fmax f1 len c e)
  7634. (untabify beg end)
  7635. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7636. ;; Check if we have links or dates
  7637. (goto-char beg)
  7638. (setq links (re-search-forward org-bracket-link-regexp end t))
  7639. (goto-char beg)
  7640. (setq emph (and org-hide-emphasis-markers
  7641. (re-search-forward org-emph-re end t)))
  7642. (goto-char beg)
  7643. (setq dates (and org-display-custom-times
  7644. (re-search-forward org-ts-regexp-both end t)))
  7645. ;; Make sure the link properties are right
  7646. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7647. ;; Make sure the date properties are right
  7648. (when dates (goto-char beg) (while (org-activate-dates end)))
  7649. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7650. ;; Check if we are narrowing any columns
  7651. (goto-char beg)
  7652. (setq narrow (and org-format-transports-properties-p
  7653. (re-search-forward "<[0-9]+>" end t)))
  7654. ;; Get the rows
  7655. (setq lines (org-split-string
  7656. (buffer-substring beg end) "\n"))
  7657. ;; Store the indentation of the first line
  7658. (if (string-match "^ *" (car lines))
  7659. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7660. ;; Mark the hlines by setting the corresponding element to nil
  7661. ;; At the same time, we remove trailing space.
  7662. (setq lines (mapcar (lambda (l)
  7663. (if (string-match "^ *|-" l)
  7664. nil
  7665. (if (string-match "[ \t]+$" l)
  7666. (substring l 0 (match-beginning 0))
  7667. l)))
  7668. lines))
  7669. ;; Get the data fields by splitting the lines.
  7670. (setq fields (mapcar
  7671. (lambda (l)
  7672. (org-split-string l " *| *"))
  7673. (delq nil (copy-sequence lines))))
  7674. ;; How many fields in the longest line?
  7675. (condition-case nil
  7676. (setq maxfields (apply 'max (mapcar 'length fields)))
  7677. (error
  7678. (kill-region beg end)
  7679. (org-table-create org-table-default-size)
  7680. (error "Empty table - created default table")))
  7681. ;; A list of empty strings to fill any short rows on output
  7682. (setq emptystrings (make-list maxfields ""))
  7683. ;; Check for special formatting.
  7684. (setq i -1)
  7685. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7686. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7687. ;; Check if there is an explicit width specified
  7688. (when narrow
  7689. (setq c column fmax nil)
  7690. (while c
  7691. (setq e (pop c))
  7692. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7693. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7694. ;; Find fields that are wider than fmax, and shorten them
  7695. (when fmax
  7696. (loop for xx in column do
  7697. (when (and (stringp xx)
  7698. (> (org-string-width xx) fmax))
  7699. (org-add-props xx nil
  7700. 'help-echo
  7701. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7702. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7703. (unless (> f1 1)
  7704. (error "Cannot narrow field starting with wide link \"%s\""
  7705. (match-string 0 xx)))
  7706. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7707. (add-text-properties (- f1 2) f1
  7708. (list 'display org-narrow-column-arrow)
  7709. xx)))))
  7710. ;; Get the maximum width for each column
  7711. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7712. ;; Get the fraction of numbers, to decide about alignment of the column
  7713. (setq cnt 0 frac 0.0)
  7714. (loop for x in column do
  7715. (if (equal x "")
  7716. nil
  7717. (setq frac ( / (+ (* frac cnt)
  7718. (if (string-match org-table-number-regexp x) 1 0))
  7719. (setq cnt (1+ cnt))))))
  7720. (push (>= frac org-table-number-fraction) typenums))
  7721. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7722. ;; Store the alignment of this table, for later editing of single fields
  7723. (setq org-table-last-alignment typenums
  7724. org-table-last-column-widths lengths)
  7725. ;; With invisible characters, `format' does not get the field width right
  7726. ;; So we need to make these fields wide by hand.
  7727. (when (or links emph)
  7728. (loop for i from 0 upto (1- maxfields) do
  7729. (setq len (nth i lengths))
  7730. (loop for j from 0 upto (1- (length fields)) do
  7731. (setq c (nthcdr i (car (nthcdr j fields))))
  7732. (if (and (stringp (car c))
  7733. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7734. ; (string-match org-bracket-link-regexp (car c))
  7735. (< (org-string-width (car c)) len))
  7736. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7737. ;; Compute the formats needed for output of the table
  7738. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7739. (while (setq l (pop lengths))
  7740. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7741. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7742. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7743. (setq rfmt (concat rfmt "\n")
  7744. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7745. (setq new (mapconcat
  7746. (lambda (l)
  7747. (if l (apply 'format rfmt
  7748. (append (pop fields) emptystrings))
  7749. hfmt))
  7750. lines ""))
  7751. ;; Replace the old one
  7752. (delete-region beg end)
  7753. (move-marker end nil)
  7754. (move-marker org-table-aligned-begin-marker (point))
  7755. (insert new)
  7756. (move-marker org-table-aligned-end-marker (point))
  7757. (when (and orgtbl-mode (not (org-mode-p)))
  7758. (goto-char org-table-aligned-begin-marker)
  7759. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7760. ;; Try to move to the old location
  7761. (goto-line winstartline)
  7762. (setq winstart (point-at-bol))
  7763. (goto-line linepos)
  7764. (set-window-start (selected-window) winstart 'noforce)
  7765. (org-table-goto-column colpos)
  7766. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7767. (setq org-table-may-need-update nil)
  7768. ))
  7769. (defun org-string-width (s)
  7770. "Compute width of string, ignoring invisible characters.
  7771. This ignores character with invisibility property `org-link', and also
  7772. characters with property `org-cwidth', because these will become invisible
  7773. upon the next fontification round."
  7774. (let (b l)
  7775. (when (or (eq t buffer-invisibility-spec)
  7776. (assq 'org-link buffer-invisibility-spec))
  7777. (while (setq b (text-property-any 0 (length s)
  7778. 'invisible 'org-link s))
  7779. (setq s (concat (substring s 0 b)
  7780. (substring s (or (next-single-property-change
  7781. b 'invisible s) (length s)))))))
  7782. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7783. (setq s (concat (substring s 0 b)
  7784. (substring s (or (next-single-property-change
  7785. b 'org-cwidth s) (length s))))))
  7786. (setq l (string-width s) b -1)
  7787. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7788. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7789. l))
  7790. (defun org-table-begin (&optional table-type)
  7791. "Find the beginning of the table and return its position.
  7792. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7793. (save-excursion
  7794. (if (not (re-search-backward
  7795. (if table-type org-table-any-border-regexp
  7796. org-table-border-regexp)
  7797. nil t))
  7798. (progn (goto-char (point-min)) (point))
  7799. (goto-char (match-beginning 0))
  7800. (beginning-of-line 2)
  7801. (point))))
  7802. (defun org-table-end (&optional table-type)
  7803. "Find the end of the table and return its position.
  7804. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7805. (save-excursion
  7806. (if (not (re-search-forward
  7807. (if table-type org-table-any-border-regexp
  7808. org-table-border-regexp)
  7809. nil t))
  7810. (goto-char (point-max))
  7811. (goto-char (match-beginning 0)))
  7812. (point-marker)))
  7813. (defun org-table-justify-field-maybe (&optional new)
  7814. "Justify the current field, text to left, number to right.
  7815. Optional argument NEW may specify text to replace the current field content."
  7816. (cond
  7817. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7818. ((org-at-table-hline-p))
  7819. ((and (not new)
  7820. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7821. (current-buffer)))
  7822. (< (point) org-table-aligned-begin-marker)
  7823. (>= (point) org-table-aligned-end-marker)))
  7824. ;; This is not the same table, force a full re-align
  7825. (setq org-table-may-need-update t))
  7826. (t ;; realign the current field, based on previous full realign
  7827. (let* ((pos (point)) s
  7828. (col (org-table-current-column))
  7829. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7830. l f n o e)
  7831. (when (> col 0)
  7832. (skip-chars-backward "^|\n")
  7833. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7834. (progn
  7835. (setq s (match-string 1)
  7836. o (match-string 0)
  7837. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7838. e (not (= (match-beginning 2) (match-end 2))))
  7839. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7840. l (if e "|" (setq org-table-may-need-update t) ""))
  7841. n (format f s))
  7842. (if new
  7843. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7844. (setq n (format f new))
  7845. (setq n (concat new "|") org-table-may-need-update t)))
  7846. (or (equal n o)
  7847. (let (org-table-may-need-update)
  7848. (replace-match n t t))))
  7849. (setq org-table-may-need-update t))
  7850. (goto-char pos))))))
  7851. (defun org-table-next-field ()
  7852. "Go to the next field in the current table, creating new lines as needed.
  7853. Before doing so, re-align the table if necessary."
  7854. (interactive)
  7855. (org-table-maybe-eval-formula)
  7856. (org-table-maybe-recalculate-line)
  7857. (if (and org-table-automatic-realign
  7858. org-table-may-need-update)
  7859. (org-table-align))
  7860. (let ((end (org-table-end)))
  7861. (if (org-at-table-hline-p)
  7862. (end-of-line 1))
  7863. (condition-case nil
  7864. (progn
  7865. (re-search-forward "|" end)
  7866. (if (looking-at "[ \t]*$")
  7867. (re-search-forward "|" end))
  7868. (if (and (looking-at "-")
  7869. org-table-tab-jumps-over-hlines
  7870. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7871. (goto-char (match-beginning 1)))
  7872. (if (looking-at "-")
  7873. (progn
  7874. (beginning-of-line 0)
  7875. (org-table-insert-row 'below))
  7876. (if (looking-at " ") (forward-char 1))))
  7877. (error
  7878. (org-table-insert-row 'below)))))
  7879. (defun org-table-previous-field ()
  7880. "Go to the previous field in the table.
  7881. Before doing so, re-align the table if necessary."
  7882. (interactive)
  7883. (org-table-justify-field-maybe)
  7884. (org-table-maybe-recalculate-line)
  7885. (if (and org-table-automatic-realign
  7886. org-table-may-need-update)
  7887. (org-table-align))
  7888. (if (org-at-table-hline-p)
  7889. (end-of-line 1))
  7890. (re-search-backward "|" (org-table-begin))
  7891. (re-search-backward "|" (org-table-begin))
  7892. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7893. (re-search-backward "|" (org-table-begin)))
  7894. (if (looking-at "| ?")
  7895. (goto-char (match-end 0))))
  7896. (defun org-table-next-row ()
  7897. "Go to the next row (same column) in the current table.
  7898. Before doing so, re-align the table if necessary."
  7899. (interactive)
  7900. (org-table-maybe-eval-formula)
  7901. (org-table-maybe-recalculate-line)
  7902. (if (or (looking-at "[ \t]*$")
  7903. (save-excursion (skip-chars-backward " \t") (bolp)))
  7904. (newline)
  7905. (if (and org-table-automatic-realign
  7906. org-table-may-need-update)
  7907. (org-table-align))
  7908. (let ((col (org-table-current-column)))
  7909. (beginning-of-line 2)
  7910. (if (or (not (org-at-table-p))
  7911. (org-at-table-hline-p))
  7912. (progn
  7913. (beginning-of-line 0)
  7914. (org-table-insert-row 'below)))
  7915. (org-table-goto-column col)
  7916. (skip-chars-backward "^|\n\r")
  7917. (if (looking-at " ") (forward-char 1)))))
  7918. (defun org-table-copy-down (n)
  7919. "Copy a field down in the current column.
  7920. If the field at the cursor is empty, copy into it the content of the nearest
  7921. non-empty field above. With argument N, use the Nth non-empty field.
  7922. If the current field is not empty, it is copied down to the next row, and
  7923. the cursor is moved with it. Therefore, repeating this command causes the
  7924. column to be filled row-by-row.
  7925. If the variable `org-table-copy-increment' is non-nil and the field is an
  7926. integer or a timestamp, it will be incremented while copying. In the case of
  7927. a timestamp, if the cursor is on the year, change the year. If it is on the
  7928. month or the day, change that. Point will stay on the current date field
  7929. in order to easily repeat the interval."
  7930. (interactive "p")
  7931. (let* ((colpos (org-table-current-column))
  7932. (col (current-column))
  7933. (field (org-table-get-field))
  7934. (non-empty (string-match "[^ \t]" field))
  7935. (beg (org-table-begin))
  7936. txt)
  7937. (org-table-check-inside-data-field)
  7938. (if non-empty
  7939. (progn
  7940. (setq txt (org-trim field))
  7941. (org-table-next-row)
  7942. (org-table-blank-field))
  7943. (save-excursion
  7944. (setq txt
  7945. (catch 'exit
  7946. (while (progn (beginning-of-line 1)
  7947. (re-search-backward org-table-dataline-regexp
  7948. beg t))
  7949. (org-table-goto-column colpos t)
  7950. (if (and (looking-at
  7951. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7952. (= (setq n (1- n)) 0))
  7953. (throw 'exit (match-string 1))))))))
  7954. (if txt
  7955. (progn
  7956. (if (and org-table-copy-increment
  7957. (string-match "^[0-9]+$" txt))
  7958. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7959. (insert txt)
  7960. (move-to-column col)
  7961. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7962. (org-timestamp-up 1)
  7963. (org-table-maybe-recalculate-line))
  7964. (org-table-align)
  7965. (move-to-column col))
  7966. (error "No non-empty field found"))))
  7967. (defun org-table-check-inside-data-field ()
  7968. "Is point inside a table data field?
  7969. I.e. not on a hline or before the first or after the last column?
  7970. This actually throws an error, so it aborts the current command."
  7971. (if (or (not (org-at-table-p))
  7972. (= (org-table-current-column) 0)
  7973. (org-at-table-hline-p)
  7974. (looking-at "[ \t]*$"))
  7975. (error "Not in table data field")))
  7976. (defvar org-table-clip nil
  7977. "Clipboard for table regions.")
  7978. (defun org-table-blank-field ()
  7979. "Blank the current table field or active region."
  7980. (interactive)
  7981. (org-table-check-inside-data-field)
  7982. (if (and (interactive-p) (org-region-active-p))
  7983. (let (org-table-clip)
  7984. (org-table-cut-region (region-beginning) (region-end)))
  7985. (skip-chars-backward "^|")
  7986. (backward-char 1)
  7987. (if (looking-at "|[^|\n]+")
  7988. (let* ((pos (match-beginning 0))
  7989. (match (match-string 0))
  7990. (len (org-string-width match)))
  7991. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7992. (goto-char (+ 2 pos))
  7993. (substring match 1)))))
  7994. (defun org-table-get-field (&optional n replace)
  7995. "Return the value of the field in column N of current row.
  7996. N defaults to current field.
  7997. If REPLACE is a string, replace field with this value. The return value
  7998. is always the old value."
  7999. (and n (org-table-goto-column n))
  8000. (skip-chars-backward "^|\n")
  8001. (backward-char 1)
  8002. (if (looking-at "|[^|\r\n]*")
  8003. (let* ((pos (match-beginning 0))
  8004. (val (buffer-substring (1+ pos) (match-end 0))))
  8005. (if replace
  8006. (replace-match (concat "|" replace) t t))
  8007. (goto-char (min (point-at-eol) (+ 2 pos)))
  8008. val)
  8009. (forward-char 1) ""))
  8010. (defun org-table-field-info (arg)
  8011. "Show info about the current field, and highlight any reference at point."
  8012. (interactive "P")
  8013. (org-table-get-specials)
  8014. (save-excursion
  8015. (let* ((pos (point))
  8016. (col (org-table-current-column))
  8017. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8018. (name (car (rassoc (list (org-current-line) col)
  8019. org-table-named-field-locations)))
  8020. (eql (org-table-get-stored-formulas))
  8021. (dline (org-table-current-dline))
  8022. (ref (format "@%d$%d" dline col))
  8023. (ref1 (org-table-convert-refs-to-an ref))
  8024. (fequation (or (assoc name eql) (assoc ref eql)))
  8025. (cequation (assoc (int-to-string col) eql))
  8026. (eqn (or fequation cequation)))
  8027. (goto-char pos)
  8028. (condition-case nil
  8029. (org-table-show-reference 'local)
  8030. (error nil))
  8031. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8032. dline col
  8033. (if cname (concat " or $" cname) "")
  8034. dline col ref1
  8035. (if name (concat " or $" name) "")
  8036. ;; FIXME: formula info not correct if special table line
  8037. (if eqn
  8038. (concat ", formula: "
  8039. (org-table-formula-to-user
  8040. (concat
  8041. (if (string-match "^[$@]"(car eqn)) "" "$")
  8042. (car eqn) "=" (cdr eqn))))
  8043. "")))))
  8044. (defun org-table-current-column ()
  8045. "Find out which column we are in.
  8046. When called interactively, column is also displayed in echo area."
  8047. (interactive)
  8048. (if (interactive-p) (org-table-check-inside-data-field))
  8049. (save-excursion
  8050. (let ((cnt 0) (pos (point)))
  8051. (beginning-of-line 1)
  8052. (while (search-forward "|" pos t)
  8053. (setq cnt (1+ cnt)))
  8054. (if (interactive-p) (message "This is table column %d" cnt))
  8055. cnt)))
  8056. (defun org-table-current-dline ()
  8057. "Find out what table data line we are in.
  8058. Only datalins count for this."
  8059. (interactive)
  8060. (if (interactive-p) (org-table-check-inside-data-field))
  8061. (save-excursion
  8062. (let ((cnt 0) (pos (point)))
  8063. (goto-char (org-table-begin))
  8064. (while (<= (point) pos)
  8065. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8066. (beginning-of-line 2))
  8067. (if (interactive-p) (message "This is table line %d" cnt))
  8068. cnt)))
  8069. (defun org-table-goto-column (n &optional on-delim force)
  8070. "Move the cursor to the Nth column in the current table line.
  8071. With optional argument ON-DELIM, stop with point before the left delimiter
  8072. of the field.
  8073. If there are less than N fields, just go to after the last delimiter.
  8074. However, when FORCE is non-nil, create new columns if necessary."
  8075. (interactive "p")
  8076. (let ((pos (point-at-eol)))
  8077. (beginning-of-line 1)
  8078. (when (> n 0)
  8079. (while (and (> (setq n (1- n)) -1)
  8080. (or (search-forward "|" pos t)
  8081. (and force
  8082. (progn (end-of-line 1)
  8083. (skip-chars-backward "^|")
  8084. (insert " | "))))))
  8085. ; (backward-char 2) t)))))
  8086. (when (and force (not (looking-at ".*|")))
  8087. (save-excursion (end-of-line 1) (insert " | ")))
  8088. (if on-delim
  8089. (backward-char 1)
  8090. (if (looking-at " ") (forward-char 1))))))
  8091. (defun org-at-table-p (&optional table-type)
  8092. "Return t if the cursor is inside an org-type table.
  8093. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8094. (if org-enable-table-editor
  8095. (save-excursion
  8096. (beginning-of-line 1)
  8097. (looking-at (if table-type org-table-any-line-regexp
  8098. org-table-line-regexp)))
  8099. nil))
  8100. (defun org-at-table.el-p ()
  8101. "Return t if and only if we are at a table.el table."
  8102. (and (org-at-table-p 'any)
  8103. (save-excursion
  8104. (goto-char (org-table-begin 'any))
  8105. (looking-at org-table1-hline-regexp))))
  8106. (defun org-table-recognize-table.el ()
  8107. "If there is a table.el table nearby, recognize it and move into it."
  8108. (if org-table-tab-recognizes-table.el
  8109. (if (org-at-table.el-p)
  8110. (progn
  8111. (beginning-of-line 1)
  8112. (if (looking-at org-table-dataline-regexp)
  8113. nil
  8114. (if (looking-at org-table1-hline-regexp)
  8115. (progn
  8116. (beginning-of-line 2)
  8117. (if (looking-at org-table-any-border-regexp)
  8118. (beginning-of-line -1)))))
  8119. (if (re-search-forward "|" (org-table-end t) t)
  8120. (progn
  8121. (require 'table)
  8122. (if (table--at-cell-p (point))
  8123. t
  8124. (message "recognizing table.el table...")
  8125. (table-recognize-table)
  8126. (message "recognizing table.el table...done")))
  8127. (error "This should not happen..."))
  8128. t)
  8129. nil)
  8130. nil))
  8131. (defun org-at-table-hline-p ()
  8132. "Return t if the cursor is inside a hline in a table."
  8133. (if org-enable-table-editor
  8134. (save-excursion
  8135. (beginning-of-line 1)
  8136. (looking-at org-table-hline-regexp))
  8137. nil))
  8138. (defun org-table-insert-column ()
  8139. "Insert a new column into the table."
  8140. (interactive)
  8141. (if (not (org-at-table-p))
  8142. (error "Not at a table"))
  8143. (org-table-find-dataline)
  8144. (let* ((col (max 1 (org-table-current-column)))
  8145. (beg (org-table-begin))
  8146. (end (org-table-end))
  8147. ;; Current cursor position
  8148. (linepos (org-current-line))
  8149. (colpos col))
  8150. (goto-char beg)
  8151. (while (< (point) end)
  8152. (if (org-at-table-hline-p)
  8153. nil
  8154. (org-table-goto-column col t)
  8155. (insert "| "))
  8156. (beginning-of-line 2))
  8157. (move-marker end nil)
  8158. (goto-line linepos)
  8159. (org-table-goto-column colpos)
  8160. (org-table-align)
  8161. (org-table-fix-formulas "$" nil (1- col) 1)))
  8162. (defun org-table-find-dataline ()
  8163. "Find a dataline in the current table, which is needed for column commands."
  8164. (if (and (org-at-table-p)
  8165. (not (org-at-table-hline-p)))
  8166. t
  8167. (let ((col (current-column))
  8168. (end (org-table-end)))
  8169. (move-to-column col)
  8170. (while (and (< (point) end)
  8171. (or (not (= (current-column) col))
  8172. (org-at-table-hline-p)))
  8173. (beginning-of-line 2)
  8174. (move-to-column col))
  8175. (if (and (org-at-table-p)
  8176. (not (org-at-table-hline-p)))
  8177. t
  8178. (error
  8179. "Please position cursor in a data line for column operations")))))
  8180. (defun org-table-delete-column ()
  8181. "Delete a column from the table."
  8182. (interactive)
  8183. (if (not (org-at-table-p))
  8184. (error "Not at a table"))
  8185. (org-table-find-dataline)
  8186. (org-table-check-inside-data-field)
  8187. (let* ((col (org-table-current-column))
  8188. (beg (org-table-begin))
  8189. (end (org-table-end))
  8190. ;; Current cursor position
  8191. (linepos (org-current-line))
  8192. (colpos col))
  8193. (goto-char beg)
  8194. (while (< (point) end)
  8195. (if (org-at-table-hline-p)
  8196. nil
  8197. (org-table-goto-column col t)
  8198. (and (looking-at "|[^|\n]+|")
  8199. (replace-match "|")))
  8200. (beginning-of-line 2))
  8201. (move-marker end nil)
  8202. (goto-line linepos)
  8203. (org-table-goto-column colpos)
  8204. (org-table-align)
  8205. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8206. col -1 col)))
  8207. (defun org-table-move-column-right ()
  8208. "Move column to the right."
  8209. (interactive)
  8210. (org-table-move-column nil))
  8211. (defun org-table-move-column-left ()
  8212. "Move column to the left."
  8213. (interactive)
  8214. (org-table-move-column 'left))
  8215. (defun org-table-move-column (&optional left)
  8216. "Move the current column to the right. With arg LEFT, move to the left."
  8217. (interactive "P")
  8218. (if (not (org-at-table-p))
  8219. (error "Not at a table"))
  8220. (org-table-find-dataline)
  8221. (org-table-check-inside-data-field)
  8222. (let* ((col (org-table-current-column))
  8223. (col1 (if left (1- col) col))
  8224. (beg (org-table-begin))
  8225. (end (org-table-end))
  8226. ;; Current cursor position
  8227. (linepos (org-current-line))
  8228. (colpos (if left (1- col) (1+ col))))
  8229. (if (and left (= col 1))
  8230. (error "Cannot move column further left"))
  8231. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8232. (error "Cannot move column further right"))
  8233. (goto-char beg)
  8234. (while (< (point) end)
  8235. (if (org-at-table-hline-p)
  8236. nil
  8237. (org-table-goto-column col1 t)
  8238. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8239. (replace-match "|\\2|\\1|")))
  8240. (beginning-of-line 2))
  8241. (move-marker end nil)
  8242. (goto-line linepos)
  8243. (org-table-goto-column colpos)
  8244. (org-table-align)
  8245. (org-table-fix-formulas
  8246. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8247. (cons (number-to-string colpos) (number-to-string col))))))
  8248. (defun org-table-move-row-down ()
  8249. "Move table row down."
  8250. (interactive)
  8251. (org-table-move-row nil))
  8252. (defun org-table-move-row-up ()
  8253. "Move table row up."
  8254. (interactive)
  8255. (org-table-move-row 'up))
  8256. (defun org-table-move-row (&optional up)
  8257. "Move the current table line down. With arg UP, move it up."
  8258. (interactive "P")
  8259. (let* ((col (current-column))
  8260. (pos (point))
  8261. (hline1p (save-excursion (beginning-of-line 1)
  8262. (looking-at org-table-hline-regexp)))
  8263. (dline1 (org-table-current-dline))
  8264. (dline2 (+ dline1 (if up -1 1)))
  8265. (tonew (if up 0 2))
  8266. txt hline2p)
  8267. (beginning-of-line tonew)
  8268. (unless (org-at-table-p)
  8269. (goto-char pos)
  8270. (error "Cannot move row further"))
  8271. (setq hline2p (looking-at org-table-hline-regexp))
  8272. (goto-char pos)
  8273. (beginning-of-line 1)
  8274. (setq pos (point))
  8275. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8276. (delete-region (point) (1+ (point-at-eol)))
  8277. (beginning-of-line tonew)
  8278. (insert txt)
  8279. (beginning-of-line 0)
  8280. (move-to-column col)
  8281. (unless (or hline1p hline2p)
  8282. (org-table-fix-formulas
  8283. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8284. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8285. (defun org-table-insert-row (&optional arg)
  8286. "Insert a new row above the current line into the table.
  8287. With prefix ARG, insert below the current line."
  8288. (interactive "P")
  8289. (if (not (org-at-table-p))
  8290. (error "Not at a table"))
  8291. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8292. (new (org-table-clean-line line)))
  8293. ;; Fix the first field if necessary
  8294. (if (string-match "^[ \t]*| *[#$] *|" line)
  8295. (setq new (replace-match (match-string 0 line) t t new)))
  8296. (beginning-of-line (if arg 2 1))
  8297. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8298. (beginning-of-line 0)
  8299. (re-search-forward "| ?" (point-at-eol) t)
  8300. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8301. (org-table-align))
  8302. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8303. (defun org-table-insert-hline (&optional above)
  8304. "Insert a horizontal-line below the current line into the table.
  8305. With prefix ABOVE, insert above the current line."
  8306. (interactive "P")
  8307. (if (not (org-at-table-p))
  8308. (error "Not at a table"))
  8309. (let ((line (org-table-clean-line
  8310. (buffer-substring (point-at-bol) (point-at-eol))))
  8311. (col (current-column)))
  8312. (while (string-match "|\\( +\\)|" line)
  8313. (setq line (replace-match
  8314. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8315. ?-) "|") t t line)))
  8316. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8317. (beginning-of-line (if above 1 2))
  8318. (insert line "\n")
  8319. (beginning-of-line (if above 1 -1))
  8320. (move-to-column col)
  8321. (and org-table-overlay-coordinates (org-table-align))))
  8322. (defun org-table-hline-and-move (&optional same-column)
  8323. "Insert a hline and move to the row below that line."
  8324. (interactive "P")
  8325. (let ((col (org-table-current-column)))
  8326. (org-table-maybe-eval-formula)
  8327. (org-table-maybe-recalculate-line)
  8328. (org-table-insert-hline)
  8329. (end-of-line 2)
  8330. (if (looking-at "\n[ \t]*|-")
  8331. (progn (insert "\n|") (org-table-align))
  8332. (org-table-next-field))
  8333. (if same-column (org-table-goto-column col))))
  8334. (defun org-table-clean-line (s)
  8335. "Convert a table line S into a string with only \"|\" and space.
  8336. In particular, this does handle wide and invisible characters."
  8337. (if (string-match "^[ \t]*|-" s)
  8338. ;; It's a hline, just map the characters
  8339. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8340. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8341. (setq s (replace-match
  8342. (concat "|" (make-string (org-string-width (match-string 1 s))
  8343. ?\ ) "|")
  8344. t t s)))
  8345. s))
  8346. (defun org-table-kill-row ()
  8347. "Delete the current row or horizontal line from the table."
  8348. (interactive)
  8349. (if (not (org-at-table-p))
  8350. (error "Not at a table"))
  8351. (let ((col (current-column))
  8352. (dline (org-table-current-dline)))
  8353. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8354. (if (not (org-at-table-p)) (beginning-of-line 0))
  8355. (move-to-column col)
  8356. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8357. dline -1 dline)))
  8358. (defun org-table-sort-lines (with-case &optional sorting-type)
  8359. "Sort table lines according to the column at point.
  8360. The position of point indicates the column to be used for
  8361. sorting, and the range of lines is the range between the nearest
  8362. horizontal separator lines, or the entire table of no such lines
  8363. exist. If point is before the first column, you will be prompted
  8364. for the sorting column. If there is an active region, the mark
  8365. specifies the first line and the sorting column, while point
  8366. should be in the last line to be included into the sorting.
  8367. The command then prompts for the sorting type which can be
  8368. alphabetically, numerically, or by time (as given in a time stamp
  8369. in the field). Sorting in reverse order is also possible.
  8370. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8371. If SORTING-TYPE is specified when this function is called from a Lisp
  8372. program, no prompting will take place. SORTING-TYPE must be a character,
  8373. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8374. should be done in reverse order."
  8375. (interactive "P")
  8376. (let* ((thisline (org-current-line))
  8377. (thiscol (org-table-current-column))
  8378. beg end bcol ecol tend tbeg column lns pos)
  8379. (when (equal thiscol 0)
  8380. (if (interactive-p)
  8381. (setq thiscol
  8382. (string-to-number
  8383. (read-string "Use column N for sorting: ")))
  8384. (setq thiscol 1))
  8385. (org-table-goto-column thiscol))
  8386. (org-table-check-inside-data-field)
  8387. (if (org-region-active-p)
  8388. (progn
  8389. (setq beg (region-beginning) end (region-end))
  8390. (goto-char beg)
  8391. (setq column (org-table-current-column)
  8392. beg (point-at-bol))
  8393. (goto-char end)
  8394. (setq end (point-at-bol 2)))
  8395. (setq column (org-table-current-column)
  8396. pos (point)
  8397. tbeg (org-table-begin)
  8398. tend (org-table-end))
  8399. (if (re-search-backward org-table-hline-regexp tbeg t)
  8400. (setq beg (point-at-bol 2))
  8401. (goto-char tbeg)
  8402. (setq beg (point-at-bol 1)))
  8403. (goto-char pos)
  8404. (if (re-search-forward org-table-hline-regexp tend t)
  8405. (setq end (point-at-bol 1))
  8406. (goto-char tend)
  8407. (setq end (point-at-bol))))
  8408. (setq beg (move-marker (make-marker) beg)
  8409. end (move-marker (make-marker) end))
  8410. (untabify beg end)
  8411. (goto-char beg)
  8412. (org-table-goto-column column)
  8413. (skip-chars-backward "^|")
  8414. (setq bcol (current-column))
  8415. (org-table-goto-column (1+ column))
  8416. (skip-chars-backward "^|")
  8417. (setq ecol (1- (current-column)))
  8418. (org-table-goto-column column)
  8419. (setq lns (mapcar (lambda(x) (cons
  8420. (org-sort-remove-invisible
  8421. (nth (1- column)
  8422. (org-split-string x "[ \t]*|[ \t]*")))
  8423. x))
  8424. (org-split-string (buffer-substring beg end) "\n")))
  8425. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8426. (delete-region beg end)
  8427. (move-marker beg nil)
  8428. (move-marker end nil)
  8429. (insert (mapconcat 'cdr lns "\n") "\n")
  8430. (goto-line thisline)
  8431. (org-table-goto-column thiscol)
  8432. (message "%d lines sorted, based on column %d" (length lns) column)))
  8433. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8434. (defun org-sort-remove-invisible (s)
  8435. (remove-text-properties 0 (length s) org-rm-props s)
  8436. (while (string-match org-bracket-link-regexp s)
  8437. (setq s (replace-match (if (match-end 2)
  8438. (match-string 3 s)
  8439. (match-string 1 s)) t t s)))
  8440. s)
  8441. (defun org-table-cut-region (beg end)
  8442. "Copy region in table to the clipboard and blank all relevant fields."
  8443. (interactive "r")
  8444. (org-table-copy-region beg end 'cut))
  8445. (defun org-table-copy-region (beg end &optional cut)
  8446. "Copy rectangular region in table to clipboard.
  8447. A special clipboard is used which can only be accessed
  8448. with `org-table-paste-rectangle'."
  8449. (interactive "rP")
  8450. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8451. region cols
  8452. (rpl (if cut " " nil)))
  8453. (goto-char beg)
  8454. (org-table-check-inside-data-field)
  8455. (setq l01 (org-current-line)
  8456. c01 (org-table-current-column))
  8457. (goto-char end)
  8458. (org-table-check-inside-data-field)
  8459. (setq l02 (org-current-line)
  8460. c02 (org-table-current-column))
  8461. (setq l1 (min l01 l02) l2 (max l01 l02)
  8462. c1 (min c01 c02) c2 (max c01 c02))
  8463. (catch 'exit
  8464. (while t
  8465. (catch 'nextline
  8466. (if (> l1 l2) (throw 'exit t))
  8467. (goto-line l1)
  8468. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8469. (setq cols nil ic1 c1 ic2 c2)
  8470. (while (< ic1 (1+ ic2))
  8471. (push (org-table-get-field ic1 rpl) cols)
  8472. (setq ic1 (1+ ic1)))
  8473. (push (nreverse cols) region)
  8474. (setq l1 (1+ l1)))))
  8475. (setq org-table-clip (nreverse region))
  8476. (if cut (org-table-align))
  8477. org-table-clip))
  8478. (defun org-table-paste-rectangle ()
  8479. "Paste a rectangular region into a table.
  8480. The upper right corner ends up in the current field. All involved fields
  8481. will be overwritten. If the rectangle does not fit into the present table,
  8482. the table is enlarged as needed. The process ignores horizontal separator
  8483. lines."
  8484. (interactive)
  8485. (unless (and org-table-clip (listp org-table-clip))
  8486. (error "First cut/copy a region to paste!"))
  8487. (org-table-check-inside-data-field)
  8488. (let* ((clip org-table-clip)
  8489. (line (org-current-line))
  8490. (col (org-table-current-column))
  8491. (org-enable-table-editor t)
  8492. (org-table-automatic-realign nil)
  8493. c cols field)
  8494. (while (setq cols (pop clip))
  8495. (while (org-at-table-hline-p) (beginning-of-line 2))
  8496. (if (not (org-at-table-p))
  8497. (progn (end-of-line 0) (org-table-next-field)))
  8498. (setq c col)
  8499. (while (setq field (pop cols))
  8500. (org-table-goto-column c nil 'force)
  8501. (org-table-get-field nil field)
  8502. (setq c (1+ c)))
  8503. (beginning-of-line 2))
  8504. (goto-line line)
  8505. (org-table-goto-column col)
  8506. (org-table-align)))
  8507. (defun org-table-convert ()
  8508. "Convert from `org-mode' table to table.el and back.
  8509. Obviously, this only works within limits. When an Org-mode table is
  8510. converted to table.el, all horizontal separator lines get lost, because
  8511. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8512. A table.el table can be converted to an Org-mode table only if it does not
  8513. do row or column spanning. Multiline cells will become multiple cells.
  8514. Beware, Org-mode does not test if the table can be successfully converted - it
  8515. blindly applies a recipe that works for simple tables."
  8516. (interactive)
  8517. (require 'table)
  8518. (if (org-at-table.el-p)
  8519. ;; convert to Org-mode table
  8520. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8521. (end (move-marker (make-marker) (org-table-end t))))
  8522. (table-unrecognize-region beg end)
  8523. (goto-char beg)
  8524. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8525. (replace-match ""))
  8526. (goto-char beg))
  8527. (if (org-at-table-p)
  8528. ;; convert to table.el table
  8529. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8530. (end (move-marker (make-marker) (org-table-end))))
  8531. ;; first, get rid of all horizontal lines
  8532. (goto-char beg)
  8533. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8534. (replace-match ""))
  8535. ;; insert a hline before first
  8536. (goto-char beg)
  8537. (org-table-insert-hline 'above)
  8538. (beginning-of-line -1)
  8539. ;; insert a hline after each line
  8540. (while (progn (beginning-of-line 3) (< (point) end))
  8541. (org-table-insert-hline))
  8542. (goto-char beg)
  8543. (setq end (move-marker end (org-table-end)))
  8544. ;; replace "+" at beginning and ending of hlines
  8545. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8546. (replace-match "\\1+-"))
  8547. (goto-char beg)
  8548. (while (re-search-forward "-|[ \t]*$" end t)
  8549. (replace-match "-+"))
  8550. (goto-char beg)))))
  8551. (defun org-table-wrap-region (arg)
  8552. "Wrap several fields in a column like a paragraph.
  8553. This is useful if you'd like to spread the contents of a field over several
  8554. lines, in order to keep the table compact.
  8555. If there is an active region, and both point and mark are in the same column,
  8556. the text in the column is wrapped to minimum width for the given number of
  8557. lines. Generally, this makes the table more compact. A prefix ARG may be
  8558. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8559. formats the selected text to two lines. If the region was longer than two
  8560. lines, the remaining lines remain empty. A negative prefix argument reduces
  8561. the current number of lines by that amount. The wrapped text is pasted back
  8562. into the table. If you formatted it to more lines than it was before, fields
  8563. further down in the table get overwritten - so you might need to make space in
  8564. the table first.
  8565. If there is no region, the current field is split at the cursor position and
  8566. the text fragment to the right of the cursor is prepended to the field one
  8567. line down.
  8568. If there is no region, but you specify a prefix ARG, the current field gets
  8569. blank, and the content is appended to the field above."
  8570. (interactive "P")
  8571. (org-table-check-inside-data-field)
  8572. (if (org-region-active-p)
  8573. ;; There is a region: fill as a paragraph
  8574. (let* ((beg (region-beginning))
  8575. (cline (save-excursion (goto-char beg) (org-current-line)))
  8576. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8577. nlines)
  8578. (org-table-cut-region (region-beginning) (region-end))
  8579. (if (> (length (car org-table-clip)) 1)
  8580. (error "Region must be limited to single column"))
  8581. (setq nlines (if arg
  8582. (if (< arg 1)
  8583. (+ (length org-table-clip) arg)
  8584. arg)
  8585. (length org-table-clip)))
  8586. (setq org-table-clip
  8587. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8588. nil nlines)))
  8589. (goto-line cline)
  8590. (org-table-goto-column ccol)
  8591. (org-table-paste-rectangle))
  8592. ;; No region, split the current field at point
  8593. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8594. (skip-chars-forward "^\r\n|"))
  8595. (if arg
  8596. ;; combine with field above
  8597. (let ((s (org-table-blank-field))
  8598. (col (org-table-current-column)))
  8599. (beginning-of-line 0)
  8600. (while (org-at-table-hline-p) (beginning-of-line 0))
  8601. (org-table-goto-column col)
  8602. (skip-chars-forward "^|")
  8603. (skip-chars-backward " ")
  8604. (insert " " (org-trim s))
  8605. (org-table-align))
  8606. ;; split field
  8607. (if (looking-at "\\([^|]+\\)+|")
  8608. (let ((s (match-string 1)))
  8609. (replace-match " |")
  8610. (goto-char (match-beginning 0))
  8611. (org-table-next-row)
  8612. (insert (org-trim s) " ")
  8613. (org-table-align))
  8614. (org-table-next-row)))))
  8615. (defvar org-field-marker nil)
  8616. (defun org-table-edit-field (arg)
  8617. "Edit table field in a different window.
  8618. This is mainly useful for fields that contain hidden parts.
  8619. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8620. it can be edited in place."
  8621. (interactive "P")
  8622. (if arg
  8623. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8624. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8625. (remove-text-properties b e '(org-cwidth t invisible t
  8626. display t intangible t))
  8627. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8628. (font-lock-fontify-block)))
  8629. (let ((pos (move-marker (make-marker) (point)))
  8630. (field (org-table-get-field))
  8631. (cw (current-window-configuration))
  8632. p)
  8633. (org-switch-to-buffer-other-window "*Org tmp*")
  8634. (erase-buffer)
  8635. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8636. (let ((org-inhibit-startup t)) (org-mode))
  8637. (goto-char (setq p (point-max)))
  8638. (insert (org-trim field))
  8639. (remove-text-properties p (point-max)
  8640. '(invisible t org-cwidth t display t
  8641. intangible t))
  8642. (goto-char p)
  8643. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8644. (org-set-local 'org-window-configuration cw)
  8645. (org-set-local 'org-field-marker pos)
  8646. (message "Edit and finish with C-c C-c"))))
  8647. (defun org-table-finish-edit-field ()
  8648. "Finish editing a table data field.
  8649. Remove all newline characters, insert the result into the table, realign
  8650. the table and kill the editing buffer."
  8651. (let ((pos org-field-marker)
  8652. (cw org-window-configuration)
  8653. (cb (current-buffer))
  8654. text)
  8655. (goto-char (point-min))
  8656. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8657. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8658. (replace-match " "))
  8659. (setq text (org-trim (buffer-string)))
  8660. (set-window-configuration cw)
  8661. (kill-buffer cb)
  8662. (select-window (get-buffer-window (marker-buffer pos)))
  8663. (goto-char pos)
  8664. (move-marker pos nil)
  8665. (org-table-check-inside-data-field)
  8666. (org-table-get-field nil text)
  8667. (org-table-align)
  8668. (message "New field value inserted")))
  8669. (defun org-trim (s)
  8670. "Remove whitespace at beginning and end of string."
  8671. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8672. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8673. s)
  8674. (defun org-wrap (string &optional width lines)
  8675. "Wrap string to either a number of lines, or a width in characters.
  8676. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8677. that costs. If there is a word longer than WIDTH, the text is actually
  8678. wrapped to the length of that word.
  8679. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8680. many lines, whatever width that takes.
  8681. The return value is a list of lines, without newlines at the end."
  8682. (let* ((words (org-split-string string "[ \t\n]+"))
  8683. (maxword (apply 'max (mapcar 'org-string-width words)))
  8684. w ll)
  8685. (cond (width
  8686. (org-do-wrap words (max maxword width)))
  8687. (lines
  8688. (setq w maxword)
  8689. (setq ll (org-do-wrap words maxword))
  8690. (if (<= (length ll) lines)
  8691. ll
  8692. (setq ll words)
  8693. (while (> (length ll) lines)
  8694. (setq w (1+ w))
  8695. (setq ll (org-do-wrap words w)))
  8696. ll))
  8697. (t (error "Cannot wrap this")))))
  8698. (defun org-do-wrap (words width)
  8699. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8700. (let (lines line)
  8701. (while words
  8702. (setq line (pop words))
  8703. (while (and words (< (+ (length line) (length (car words))) width))
  8704. (setq line (concat line " " (pop words))))
  8705. (setq lines (push line lines)))
  8706. (nreverse lines)))
  8707. (defun org-split-string (string &optional separators)
  8708. "Splits STRING into substrings at SEPARATORS.
  8709. No empty strings are returned if there are matches at the beginning
  8710. and end of string."
  8711. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8712. (start 0)
  8713. notfirst
  8714. (list nil))
  8715. (while (and (string-match rexp string
  8716. (if (and notfirst
  8717. (= start (match-beginning 0))
  8718. (< start (length string)))
  8719. (1+ start) start))
  8720. (< (match-beginning 0) (length string)))
  8721. (setq notfirst t)
  8722. (or (eq (match-beginning 0) 0)
  8723. (and (eq (match-beginning 0) (match-end 0))
  8724. (eq (match-beginning 0) start))
  8725. (setq list
  8726. (cons (substring string start (match-beginning 0))
  8727. list)))
  8728. (setq start (match-end 0)))
  8729. (or (eq start (length string))
  8730. (setq list
  8731. (cons (substring string start)
  8732. list)))
  8733. (nreverse list)))
  8734. (defun org-table-map-tables (function)
  8735. "Apply FUNCTION to the start of all tables in the buffer."
  8736. (save-excursion
  8737. (save-restriction
  8738. (widen)
  8739. (goto-char (point-min))
  8740. (while (re-search-forward org-table-any-line-regexp nil t)
  8741. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8742. (beginning-of-line 1)
  8743. (if (looking-at org-table-line-regexp)
  8744. (save-excursion (funcall function)))
  8745. (re-search-forward org-table-any-border-regexp nil 1))))
  8746. (message "Mapping tables: done"))
  8747. (defvar org-timecnt) ; dynamically scoped parameter
  8748. (defun org-table-sum (&optional beg end nlast)
  8749. "Sum numbers in region of current table column.
  8750. The result will be displayed in the echo area, and will be available
  8751. as kill to be inserted with \\[yank].
  8752. If there is an active region, it is interpreted as a rectangle and all
  8753. numbers in that rectangle will be summed. If there is no active
  8754. region and point is located in a table column, sum all numbers in that
  8755. column.
  8756. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8757. numbers are assumed to be times as well (in decimal hours) and the
  8758. numbers are added as such.
  8759. If NLAST is a number, only the NLAST fields will actually be summed."
  8760. (interactive)
  8761. (save-excursion
  8762. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8763. (cond
  8764. ((and beg end)) ; beg and end given explicitly
  8765. ((org-region-active-p)
  8766. (setq beg (region-beginning) end (region-end)))
  8767. (t
  8768. (setq col (org-table-current-column))
  8769. (goto-char (org-table-begin))
  8770. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8771. (error "No table data"))
  8772. (org-table-goto-column col)
  8773. (setq beg (point))
  8774. (goto-char (org-table-end))
  8775. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8776. (error "No table data"))
  8777. (org-table-goto-column col)
  8778. (setq end (point))))
  8779. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8780. (items1 (cond ((not nlast) items)
  8781. ((>= nlast (length items)) items)
  8782. (t (setq items (reverse items))
  8783. (setcdr (nthcdr (1- nlast) items) nil)
  8784. (nreverse items))))
  8785. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8786. items1)))
  8787. (res (apply '+ numbers))
  8788. (sres (if (= org-timecnt 0)
  8789. (format "%g" res)
  8790. (setq diff (* 3600 res)
  8791. h (floor (/ diff 3600)) diff (mod diff 3600)
  8792. m (floor (/ diff 60)) diff (mod diff 60)
  8793. s diff)
  8794. (format "%d:%02d:%02d" h m s))))
  8795. (kill-new sres)
  8796. (if (interactive-p)
  8797. (message "%s"
  8798. (substitute-command-keys
  8799. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8800. (length numbers) sres))))
  8801. sres))))
  8802. (defun org-table-get-number-for-summing (s)
  8803. (let (n)
  8804. (if (string-match "^ *|? *" s)
  8805. (setq s (replace-match "" nil nil s)))
  8806. (if (string-match " *|? *$" s)
  8807. (setq s (replace-match "" nil nil s)))
  8808. (setq n (string-to-number s))
  8809. (cond
  8810. ((and (string-match "0" s)
  8811. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8812. ((string-match "\\`[ \t]+\\'" s) nil)
  8813. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8814. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8815. (m (string-to-number (or (match-string 2 s) "0")))
  8816. (s (string-to-number (or (match-string 4 s) "0"))))
  8817. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8818. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8819. ((equal n 0) nil)
  8820. (t n))))
  8821. (defun org-table-current-field-formula (&optional key noerror)
  8822. "Return the formula active for the current field.
  8823. Assumes that specials are in place.
  8824. If KEY is given, return the key to this formula.
  8825. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8826. (let* ((name (car (rassoc (list (org-current-line)
  8827. (org-table-current-column))
  8828. org-table-named-field-locations)))
  8829. (col (org-table-current-column))
  8830. (scol (int-to-string col))
  8831. (ref (format "@%d$%d" (org-table-current-dline) col))
  8832. (stored-list (org-table-get-stored-formulas noerror))
  8833. (ass (or (assoc name stored-list)
  8834. (assoc ref stored-list)
  8835. (assoc scol stored-list))))
  8836. (if key
  8837. (car ass)
  8838. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8839. (cdr ass))))))
  8840. (defun org-table-get-formula (&optional equation named)
  8841. "Read a formula from the minibuffer, offer stored formula as default.
  8842. When NAMED is non-nil, look for a named equation."
  8843. (let* ((stored-list (org-table-get-stored-formulas))
  8844. (name (car (rassoc (list (org-current-line)
  8845. (org-table-current-column))
  8846. org-table-named-field-locations)))
  8847. (ref (format "@%d$%d" (org-table-current-dline)
  8848. (org-table-current-column)))
  8849. (refass (assoc ref stored-list))
  8850. (scol (if named
  8851. (if name name ref)
  8852. (int-to-string (org-table-current-column))))
  8853. (dummy (and (or name refass) (not named)
  8854. (not (y-or-n-p "Replace field formula with column formula? " ))
  8855. (error "Abort")))
  8856. (name (or name ref))
  8857. (org-table-may-need-update nil)
  8858. (stored (cdr (assoc scol stored-list)))
  8859. (eq (cond
  8860. ((and stored equation (string-match "^ *=? *$" equation))
  8861. stored)
  8862. ((stringp equation)
  8863. equation)
  8864. (t (org-table-formula-from-user
  8865. (read-string
  8866. (org-table-formula-to-user
  8867. (format "%s formula %s%s="
  8868. (if named "Field" "Column")
  8869. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8870. scol))
  8871. (if stored (org-table-formula-to-user stored) "")
  8872. 'org-table-formula-history
  8873. )))))
  8874. mustsave)
  8875. (when (not (string-match "\\S-" eq))
  8876. ;; remove formula
  8877. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8878. (org-table-store-formulas stored-list)
  8879. (error "Formula removed"))
  8880. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8881. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8882. (if (and name (not named))
  8883. ;; We set the column equation, delete the named one.
  8884. (setq stored-list (delq (assoc name stored-list) stored-list)
  8885. mustsave t))
  8886. (if stored
  8887. (setcdr (assoc scol stored-list) eq)
  8888. (setq stored-list (cons (cons scol eq) stored-list)))
  8889. (if (or mustsave (not (equal stored eq)))
  8890. (org-table-store-formulas stored-list))
  8891. eq))
  8892. (defun org-table-store-formulas (alist)
  8893. "Store the list of formulas below the current table."
  8894. (setq alist (sort alist 'org-table-formula-less-p))
  8895. (save-excursion
  8896. (goto-char (org-table-end))
  8897. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8898. (progn
  8899. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8900. (goto-char (match-beginning 2))
  8901. (delete-region (match-beginning 2) (match-end 0)))
  8902. (insert "#+TBLFM:"))
  8903. (insert " "
  8904. (mapconcat (lambda (x)
  8905. (concat
  8906. (if (equal (string-to-char (car x)) ?@) "" "$")
  8907. (car x) "=" (cdr x)))
  8908. alist "::")
  8909. "\n")))
  8910. (defsubst org-table-formula-make-cmp-string (a)
  8911. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8912. (concat
  8913. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8914. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8915. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8916. (defun org-table-formula-less-p (a b)
  8917. "Compare two formulas for sorting."
  8918. (let ((as (org-table-formula-make-cmp-string (car a)))
  8919. (bs (org-table-formula-make-cmp-string (car b))))
  8920. (and as bs (string< as bs))))
  8921. (defun org-table-get-stored-formulas (&optional noerror)
  8922. "Return an alist with the stored formulas directly after current table."
  8923. (interactive)
  8924. (let (scol eq eq-alist strings string seen)
  8925. (save-excursion
  8926. (goto-char (org-table-end))
  8927. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8928. (setq strings (org-split-string (match-string 2) " *:: *"))
  8929. (while (setq string (pop strings))
  8930. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8931. (setq scol (if (match-end 2)
  8932. (match-string 2 string)
  8933. (match-string 1 string))
  8934. eq (match-string 3 string)
  8935. eq-alist (cons (cons scol eq) eq-alist))
  8936. (if (member scol seen)
  8937. (if noerror
  8938. (progn
  8939. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8940. (ding)
  8941. (sit-for 2))
  8942. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8943. (push scol seen))))))
  8944. (nreverse eq-alist)))
  8945. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8946. "Modify the equations after the table structure has been edited.
  8947. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8948. For all numbers larger than LIMIT, shift them by DELTA."
  8949. (save-excursion
  8950. (goto-char (org-table-end))
  8951. (when (looking-at "#\\+TBLFM:")
  8952. (let ((re (concat key "\\([0-9]+\\)"))
  8953. (re2
  8954. (when remove
  8955. (if (equal key "$")
  8956. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8957. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8958. s n a)
  8959. (when remove
  8960. (while (re-search-forward re2 (point-at-eol) t)
  8961. (replace-match "")))
  8962. (while (re-search-forward re (point-at-eol) t)
  8963. (setq s (match-string 1) n (string-to-number s))
  8964. (cond
  8965. ((setq a (assoc s replace))
  8966. (replace-match (concat key (cdr a)) t t))
  8967. ((and limit (> n limit))
  8968. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8969. (defun org-table-get-specials ()
  8970. "Get the column names and local parameters for this table."
  8971. (save-excursion
  8972. (let ((beg (org-table-begin)) (end (org-table-end))
  8973. names name fields fields1 field cnt
  8974. c v l line col types dlines hlines)
  8975. (setq org-table-column-names nil
  8976. org-table-local-parameters nil
  8977. org-table-named-field-locations nil
  8978. org-table-current-begin-line nil
  8979. org-table-current-begin-pos nil
  8980. org-table-current-line-types nil)
  8981. (goto-char beg)
  8982. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8983. (setq names (org-split-string (match-string 1) " *| *")
  8984. cnt 1)
  8985. (while (setq name (pop names))
  8986. (setq cnt (1+ cnt))
  8987. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8988. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8989. (setq org-table-column-names (nreverse org-table-column-names))
  8990. (setq org-table-column-name-regexp
  8991. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8992. (goto-char beg)
  8993. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8994. (setq fields (org-split-string (match-string 1) " *| *"))
  8995. (while (setq field (pop fields))
  8996. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8997. (push (cons (match-string 1 field) (match-string 2 field))
  8998. org-table-local-parameters))))
  8999. (goto-char beg)
  9000. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9001. (setq c (match-string 1)
  9002. fields (org-split-string (match-string 2) " *| *"))
  9003. (save-excursion
  9004. (beginning-of-line (if (equal c "_") 2 0))
  9005. (setq line (org-current-line) col 1)
  9006. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9007. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9008. (while (and fields1 (setq field (pop fields)))
  9009. (setq v (pop fields1) col (1+ col))
  9010. (when (and (stringp field) (stringp v)
  9011. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9012. (push (cons field v) org-table-local-parameters)
  9013. (push (list field line col) org-table-named-field-locations))))
  9014. ;; Analyse the line types
  9015. (goto-char beg)
  9016. (setq org-table-current-begin-line (org-current-line)
  9017. org-table-current-begin-pos (point)
  9018. l org-table-current-begin-line)
  9019. (while (looking-at "[ \t]*|\\(-\\)?")
  9020. (push (if (match-end 1) 'hline 'dline) types)
  9021. (if (match-end 1) (push l hlines) (push l dlines))
  9022. (beginning-of-line 2)
  9023. (setq l (1+ l)))
  9024. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9025. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9026. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9027. (defun org-table-maybe-eval-formula ()
  9028. "Check if the current field starts with \"=\" or \":=\".
  9029. If yes, store the formula and apply it."
  9030. ;; We already know we are in a table. Get field will only return a formula
  9031. ;; when appropriate. It might return a separator line, but no problem.
  9032. (when org-table-formula-evaluate-inline
  9033. (let* ((field (org-trim (or (org-table-get-field) "")))
  9034. named eq)
  9035. (when (string-match "^:?=\\(.*\\)" field)
  9036. (setq named (equal (string-to-char field) ?:)
  9037. eq (match-string 1 field))
  9038. (if (or (fboundp 'calc-eval)
  9039. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9040. (org-table-eval-formula (if named '(4) nil)
  9041. (org-table-formula-from-user eq))
  9042. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9043. (defvar org-recalc-commands nil
  9044. "List of commands triggering the recalculation of a line.
  9045. Will be filled automatically during use.")
  9046. (defvar org-recalc-marks
  9047. '((" " . "Unmarked: no special line, no automatic recalculation")
  9048. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9049. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9050. ("!" . "Column name definition line. Reference in formula as $name.")
  9051. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9052. ("_" . "Names for values in row below this one.")
  9053. ("^" . "Names for values in row above this one.")))
  9054. (defun org-table-rotate-recalc-marks (&optional newchar)
  9055. "Rotate the recalculation mark in the first column.
  9056. If in any row, the first field is not consistent with a mark,
  9057. insert a new column for the markers.
  9058. When there is an active region, change all the lines in the region,
  9059. after prompting for the marking character.
  9060. After each change, a message will be displayed indicating the meaning
  9061. of the new mark."
  9062. (interactive)
  9063. (unless (org-at-table-p) (error "Not at a table"))
  9064. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9065. (beg (org-table-begin))
  9066. (end (org-table-end))
  9067. (l (org-current-line))
  9068. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9069. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9070. (have-col
  9071. (save-excursion
  9072. (goto-char beg)
  9073. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9074. (col (org-table-current-column))
  9075. (forcenew (car (assoc newchar org-recalc-marks)))
  9076. epos new)
  9077. (when l1
  9078. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9079. (setq newchar (char-to-string (read-char-exclusive))
  9080. forcenew (car (assoc newchar org-recalc-marks))))
  9081. (if (and newchar (not forcenew))
  9082. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9083. newchar))
  9084. (if l1 (goto-line l1))
  9085. (save-excursion
  9086. (beginning-of-line 1)
  9087. (unless (looking-at org-table-dataline-regexp)
  9088. (error "Not at a table data line")))
  9089. (unless have-col
  9090. (org-table-goto-column 1)
  9091. (org-table-insert-column)
  9092. (org-table-goto-column (1+ col)))
  9093. (setq epos (point-at-eol))
  9094. (save-excursion
  9095. (beginning-of-line 1)
  9096. (org-table-get-field
  9097. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9098. (concat " "
  9099. (setq new (or forcenew
  9100. (cadr (member (match-string 1) marks))))
  9101. " ")
  9102. " # ")))
  9103. (if (and l1 l2)
  9104. (progn
  9105. (goto-line l1)
  9106. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9107. (and (looking-at org-table-dataline-regexp)
  9108. (org-table-get-field 1 (concat " " new " "))))
  9109. (goto-line l1)))
  9110. (if (not (= epos (point-at-eol))) (org-table-align))
  9111. (goto-line l)
  9112. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9113. (defun org-table-maybe-recalculate-line ()
  9114. "Recompute the current line if marked for it, and if we haven't just done it."
  9115. (interactive)
  9116. (and org-table-allow-automatic-line-recalculation
  9117. (not (and (memq last-command org-recalc-commands)
  9118. (equal org-last-recalc-line (org-current-line))))
  9119. (save-excursion (beginning-of-line 1)
  9120. (looking-at org-table-auto-recalculate-regexp))
  9121. (org-table-recalculate) t))
  9122. (defvar org-table-formula-debug nil
  9123. "Non-nil means, debug table formulas.
  9124. When nil, simply write \"#ERROR\" in corrupted fields.")
  9125. (make-variable-buffer-local 'org-table-formula-debug)
  9126. (defvar modes)
  9127. (defsubst org-set-calc-mode (var &optional value)
  9128. (if (stringp var)
  9129. (setq var (assoc var '(("D" calc-angle-mode deg)
  9130. ("R" calc-angle-mode rad)
  9131. ("F" calc-prefer-frac t)
  9132. ("S" calc-symbolic-mode t)))
  9133. value (nth 2 var) var (nth 1 var)))
  9134. (if (memq var modes)
  9135. (setcar (cdr (memq var modes)) value)
  9136. (cons var (cons value modes)))
  9137. modes)
  9138. (defun org-table-eval-formula (&optional arg equation
  9139. suppress-align suppress-const
  9140. suppress-store suppress-analysis)
  9141. "Replace the table field value at the cursor by the result of a calculation.
  9142. This function makes use of Dave Gillespie's Calc package, in my view the
  9143. most exciting program ever written for GNU Emacs. So you need to have Calc
  9144. installed in order to use this function.
  9145. In a table, this command replaces the value in the current field with the
  9146. result of a formula. It also installs the formula as the \"current\" column
  9147. formula, by storing it in a special line below the table. When called
  9148. with a `C-u' prefix, the current field must ba a named field, and the
  9149. formula is installed as valid in only this specific field.
  9150. When called with two `C-u' prefixes, insert the active equation
  9151. for the field back into the current field, so that it can be
  9152. edited there. This is useful in order to use \\[org-table-show-reference]
  9153. to check the referenced fields.
  9154. When called, the command first prompts for a formula, which is read in
  9155. the minibuffer. Previously entered formulas are available through the
  9156. history list, and the last used formula is offered as a default.
  9157. These stored formulas are adapted correctly when moving, inserting, or
  9158. deleting columns with the corresponding commands.
  9159. The formula can be any algebraic expression understood by the Calc package.
  9160. For details, see the Org-mode manual.
  9161. This function can also be called from Lisp programs and offers
  9162. additional arguments: EQUATION can be the formula to apply. If this
  9163. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9164. used to speed-up recursive calls by by-passing unnecessary aligns.
  9165. SUPPRESS-CONST suppresses the interpretation of constants in the
  9166. formula, assuming that this has been done already outside the function.
  9167. SUPPRESS-STORE means the formula should not be stored, either because
  9168. it is already stored, or because it is a modified equation that should
  9169. not overwrite the stored one."
  9170. (interactive "P")
  9171. (org-table-check-inside-data-field)
  9172. (or suppress-analysis (org-table-get-specials))
  9173. (if (equal arg '(16))
  9174. (let ((eq (org-table-current-field-formula)))
  9175. (or eq (error "No equation active for current field"))
  9176. (org-table-get-field nil eq)
  9177. (org-table-align)
  9178. (setq org-table-may-need-update t))
  9179. (let* (fields
  9180. (ndown (if (integerp arg) arg 1))
  9181. (org-table-automatic-realign nil)
  9182. (case-fold-search nil)
  9183. (down (> ndown 1))
  9184. (formula (if (and equation suppress-store)
  9185. equation
  9186. (org-table-get-formula equation (equal arg '(4)))))
  9187. (n0 (org-table-current-column))
  9188. (modes (copy-sequence org-calc-default-modes))
  9189. (numbers nil) ; was a variable, now fixed default
  9190. (keep-empty nil)
  9191. n form form0 bw fmt x ev orig c lispp literal)
  9192. ;; Parse the format string. Since we have a lot of modes, this is
  9193. ;; a lot of work. However, I think calc still uses most of the time.
  9194. (if (string-match ";" formula)
  9195. (let ((tmp (org-split-string formula ";")))
  9196. (setq formula (car tmp)
  9197. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9198. (nth 1 tmp)))
  9199. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9200. (setq c (string-to-char (match-string 1 fmt))
  9201. n (string-to-number (match-string 2 fmt)))
  9202. (if (= c ?p)
  9203. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9204. (setq modes (org-set-calc-mode
  9205. 'calc-float-format
  9206. (list (cdr (assoc c '((?n . float) (?f . fix)
  9207. (?s . sci) (?e . eng))))
  9208. n))))
  9209. (setq fmt (replace-match "" t t fmt)))
  9210. (if (string-match "[NT]" fmt)
  9211. (setq numbers (equal (match-string 0 fmt) "N")
  9212. fmt (replace-match "" t t fmt)))
  9213. (if (string-match "L" fmt)
  9214. (setq literal t
  9215. fmt (replace-match "" t t fmt)))
  9216. (if (string-match "E" fmt)
  9217. (setq keep-empty t
  9218. fmt (replace-match "" t t fmt)))
  9219. (while (string-match "[DRFS]" fmt)
  9220. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9221. (setq fmt (replace-match "" t t fmt)))
  9222. (unless (string-match "\\S-" fmt)
  9223. (setq fmt nil))))
  9224. (if (and (not suppress-const) org-table-formula-use-constants)
  9225. (setq formula (org-table-formula-substitute-names formula)))
  9226. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9227. (while (> ndown 0)
  9228. (setq fields (org-split-string
  9229. (org-no-properties
  9230. (buffer-substring (point-at-bol) (point-at-eol)))
  9231. " *| *"))
  9232. (if (eq numbers t)
  9233. (setq fields (mapcar
  9234. (lambda (x) (number-to-string (string-to-number x)))
  9235. fields)))
  9236. (setq ndown (1- ndown))
  9237. (setq form (copy-sequence formula)
  9238. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9239. (if (and lispp literal) (setq lispp 'literal))
  9240. ;; Check for old vertical references
  9241. (setq form (org-rewrite-old-row-references form))
  9242. ;; Insert complex ranges
  9243. (while (string-match org-table-range-regexp form)
  9244. (setq form
  9245. (replace-match
  9246. (save-match-data
  9247. (org-table-make-reference
  9248. (org-table-get-range (match-string 0 form) nil n0)
  9249. keep-empty numbers lispp))
  9250. t t form)))
  9251. ;; Insert simple ranges
  9252. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9253. (setq form
  9254. (replace-match
  9255. (save-match-data
  9256. (org-table-make-reference
  9257. (org-sublist
  9258. fields (string-to-number (match-string 1 form))
  9259. (string-to-number (match-string 2 form)))
  9260. keep-empty numbers lispp))
  9261. t t form)))
  9262. (setq form0 form)
  9263. ;; Insert the references to fields in same row
  9264. (while (string-match "\\$\\([0-9]+\\)" form)
  9265. (setq n (string-to-number (match-string 1 form))
  9266. x (nth (1- (if (= n 0) n0 n)) fields))
  9267. (unless x (error "Invalid field specifier \"%s\""
  9268. (match-string 0 form)))
  9269. (setq form (replace-match
  9270. (save-match-data
  9271. (org-table-make-reference x nil numbers lispp))
  9272. t t form)))
  9273. (if lispp
  9274. (setq ev (condition-case nil
  9275. (eval (eval (read form)))
  9276. (error "#ERROR"))
  9277. ev (if (numberp ev) (number-to-string ev) ev))
  9278. (or (fboundp 'calc-eval)
  9279. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9280. (setq ev (calc-eval (cons form modes)
  9281. (if numbers 'num))))
  9282. (when org-table-formula-debug
  9283. (with-output-to-temp-buffer "*Substitution History*"
  9284. (princ (format "Substitution history of formula
  9285. Orig: %s
  9286. $xyz-> %s
  9287. @r$c-> %s
  9288. $1-> %s\n" orig formula form0 form))
  9289. (if (listp ev)
  9290. (princ (format " %s^\nError: %s"
  9291. (make-string (car ev) ?\-) (nth 1 ev)))
  9292. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9293. ev (or fmt "NONE")
  9294. (if fmt (format fmt (string-to-number ev)) ev)))))
  9295. (setq bw (get-buffer-window "*Substitution History*"))
  9296. (shrink-window-if-larger-than-buffer bw)
  9297. (unless (and (interactive-p) (not ndown))
  9298. (unless (let (inhibit-redisplay)
  9299. (y-or-n-p "Debugging Formula. Continue to next? "))
  9300. (org-table-align)
  9301. (error "Abort"))
  9302. (delete-window bw)
  9303. (message "")))
  9304. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9305. (org-table-justify-field-maybe
  9306. (if fmt (format fmt (string-to-number ev)) ev))
  9307. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9308. (call-interactively 'org-return)
  9309. (setq ndown 0)))
  9310. (and down (org-table-maybe-recalculate-line))
  9311. (or suppress-align (and org-table-may-need-update
  9312. (org-table-align))))))
  9313. (defun org-table-put-field-property (prop value)
  9314. (save-excursion
  9315. (put-text-property (progn (skip-chars-backward "^|") (point))
  9316. (progn (skip-chars-forward "^|") (point))
  9317. prop value)))
  9318. (defun org-table-get-range (desc &optional tbeg col highlight)
  9319. "Get a calc vector from a column, accorting to descriptor DESC.
  9320. Optional arguments TBEG and COL can give the beginning of the table and
  9321. the current column, to avoid unnecessary parsing.
  9322. HIGHLIGHT means, just highlight the range."
  9323. (if (not (equal (string-to-char desc) ?@))
  9324. (setq desc (concat "@" desc)))
  9325. (save-excursion
  9326. (or tbeg (setq tbeg (org-table-begin)))
  9327. (or col (setq col (org-table-current-column)))
  9328. (let ((thisline (org-current-line))
  9329. beg end c1 c2 r1 r2 rangep tmp)
  9330. (unless (string-match org-table-range-regexp desc)
  9331. (error "Invalid table range specifier `%s'" desc))
  9332. (setq rangep (match-end 3)
  9333. r1 (and (match-end 1) (match-string 1 desc))
  9334. r2 (and (match-end 4) (match-string 4 desc))
  9335. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9336. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9337. (and c1 (setq c1 (+ (string-to-number c1)
  9338. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9339. (and c2 (setq c2 (+ (string-to-number c2)
  9340. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9341. (if (equal r1 "") (setq r1 nil))
  9342. (if (equal r2 "") (setq r2 nil))
  9343. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9344. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9345. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9346. (if (not r1) (setq r1 thisline))
  9347. (if (not r2) (setq r2 thisline))
  9348. (if (not c1) (setq c1 col))
  9349. (if (not c2) (setq c2 col))
  9350. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9351. ;; just one field
  9352. (progn
  9353. (goto-line r1)
  9354. (while (not (looking-at org-table-dataline-regexp))
  9355. (beginning-of-line 2))
  9356. (prog1 (org-trim (org-table-get-field c1))
  9357. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9358. ;; A range, return a vector
  9359. ;; First sort the numbers to get a regular ractangle
  9360. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9361. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9362. (goto-line r1)
  9363. (while (not (looking-at org-table-dataline-regexp))
  9364. (beginning-of-line 2))
  9365. (org-table-goto-column c1)
  9366. (setq beg (point))
  9367. (goto-line r2)
  9368. (while (not (looking-at org-table-dataline-regexp))
  9369. (beginning-of-line 0))
  9370. (org-table-goto-column c2)
  9371. (setq end (point))
  9372. (if highlight
  9373. (org-table-highlight-rectangle
  9374. beg (progn (skip-chars-forward "^|\n") (point))))
  9375. ;; return string representation of calc vector
  9376. (mapcar 'org-trim
  9377. (apply 'append (org-table-copy-region beg end)))))))
  9378. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9379. "Analyze descriptor DESC and retrieve the corresponding line number.
  9380. The cursor is currently in line CLINE, the table begins in line BLINE,
  9381. and TABLE is a vector with line types."
  9382. (if (string-match "^[0-9]+$" desc)
  9383. (aref org-table-dlines (string-to-number desc))
  9384. (setq cline (or cline (org-current-line))
  9385. bline (or bline org-table-current-begin-line)
  9386. table (or table org-table-current-line-types))
  9387. (if (or
  9388. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9389. ;; 1 2 3 4 5 6
  9390. (and (not (match-end 3)) (not (match-end 6)))
  9391. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9392. (error "invalid row descriptor `%s'" desc))
  9393. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9394. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9395. (odir (and (match-end 5) (match-string 5 desc)))
  9396. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9397. (i (- cline bline))
  9398. (rel (and (match-end 6)
  9399. (or (and (match-end 1) (not (match-end 3)))
  9400. (match-end 5)))))
  9401. (if (and hn (not hdir))
  9402. (progn
  9403. (setq i 0 hdir "+")
  9404. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9405. (if (and (not hn) on (not odir))
  9406. (error "should never happen");;(aref org-table-dlines on)
  9407. (if (and hn (> hn 0))
  9408. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9409. (if on
  9410. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9411. (+ bline i)))))
  9412. (defun org-find-row-type (table i type backwards relative n)
  9413. (let ((l (length table)))
  9414. (while (> n 0)
  9415. (while (and (setq i (+ i (if backwards -1 1)))
  9416. (>= i 0) (< i l)
  9417. (not (eq (aref table i) type))
  9418. (if (and relative (eq (aref table i) 'hline))
  9419. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9420. t)))
  9421. (setq n (1- n)))
  9422. (if (or (< i 0) (>= i l))
  9423. (error "Row descriptior leads outside table")
  9424. i)))
  9425. (defun org-rewrite-old-row-references (s)
  9426. (if (string-match "&[-+0-9I]" s)
  9427. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9428. s))
  9429. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9430. "Convert list ELEMENTS to something appropriate to insert into formula.
  9431. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9432. NUMBERS indicates that everything should be converted to numbers.
  9433. LISPP means to return something appropriate for a Lisp list."
  9434. (if (stringp elements) ; just a single val
  9435. (if lispp
  9436. (if (eq lispp 'literal)
  9437. elements
  9438. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9439. (if (equal elements "") (setq elements "0"))
  9440. (if numbers (number-to-string (string-to-number elements)) elements))
  9441. (unless keep-empty
  9442. (setq elements
  9443. (delq nil
  9444. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9445. elements))))
  9446. (setq elements (or elements '("0")))
  9447. (if lispp
  9448. (mapconcat
  9449. (lambda (x)
  9450. (if (eq lispp 'literal)
  9451. x
  9452. (prin1-to-string (if numbers (string-to-number x) x))))
  9453. elements " ")
  9454. (concat "[" (mapconcat
  9455. (lambda (x)
  9456. (if numbers (number-to-string (string-to-number x)) x))
  9457. elements
  9458. ",") "]"))))
  9459. (defun org-table-recalculate (&optional all noalign)
  9460. "Recalculate the current table line by applying all stored formulas.
  9461. With prefix arg ALL, do this for all lines in the table."
  9462. (interactive "P")
  9463. (or (memq this-command org-recalc-commands)
  9464. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9465. (unless (org-at-table-p) (error "Not at a table"))
  9466. (if (equal all '(16))
  9467. (org-table-iterate)
  9468. (org-table-get-specials)
  9469. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9470. (lambda (a b) (string< (car a) (car b)))))
  9471. (inhibit-redisplay (not debug-on-error))
  9472. (line-re org-table-dataline-regexp)
  9473. (thisline (org-current-line))
  9474. (thiscol (org-table-current-column))
  9475. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9476. ;; Insert constants in all formulas
  9477. (setq eqlist
  9478. (mapcar (lambda (x)
  9479. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9480. x)
  9481. eqlist))
  9482. ;; Split the equation list
  9483. (while (setq eq (pop eqlist))
  9484. (if (<= (string-to-char (car eq)) ?9)
  9485. (push eq eqlnum)
  9486. (push eq eqlname)))
  9487. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9488. (if all
  9489. (progn
  9490. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9491. (goto-char (setq beg (org-table-begin)))
  9492. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9493. ;; This is a table with marked lines, compute selected lines
  9494. (setq line-re org-table-recalculate-regexp)
  9495. ;; Move forward to the first non-header line
  9496. (if (and (re-search-forward org-table-dataline-regexp end t)
  9497. (re-search-forward org-table-hline-regexp end t)
  9498. (re-search-forward org-table-dataline-regexp end t))
  9499. (setq beg (match-beginning 0))
  9500. nil))) ;; just leave beg where it is
  9501. (setq beg (point-at-bol)
  9502. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9503. (goto-char beg)
  9504. (and all (message "Re-applying formulas to full table..."))
  9505. ;; First find the named fields, and mark them untouchanble
  9506. (remove-text-properties beg end '(org-untouchable t))
  9507. (while (setq eq (pop eqlname))
  9508. (setq name (car eq)
  9509. a (assoc name org-table-named-field-locations))
  9510. (and (not a)
  9511. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9512. (setq a (list name
  9513. (aref org-table-dlines
  9514. (string-to-number (match-string 1 name)))
  9515. (string-to-number (match-string 2 name)))))
  9516. (when (and a (or all (equal (nth 1 a) thisline)))
  9517. (message "Re-applying formula to field: %s" name)
  9518. (goto-line (nth 1 a))
  9519. (org-table-goto-column (nth 2 a))
  9520. (push (append a (list (cdr eq))) eqlname1)
  9521. (org-table-put-field-property :org-untouchable t)))
  9522. ;; Now evauluate the column formulas, but skip fields covered by
  9523. ;; field formulas
  9524. (goto-char beg)
  9525. (while (re-search-forward line-re end t)
  9526. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9527. ;; Unprotected line, recalculate
  9528. (and all (message "Re-applying formulas to full table...(line %d)"
  9529. (setq cnt (1+ cnt))))
  9530. (setq org-last-recalc-line (org-current-line))
  9531. (setq eql eqlnum)
  9532. (while (setq entry (pop eql))
  9533. (goto-line org-last-recalc-line)
  9534. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9535. (unless (get-text-property (point) :org-untouchable)
  9536. (org-table-eval-formula nil (cdr entry)
  9537. 'noalign 'nocst 'nostore 'noanalysis)))))
  9538. ;; Now evaluate the field formulas
  9539. (while (setq eq (pop eqlname1))
  9540. (message "Re-applying formula to field: %s" (car eq))
  9541. (goto-line (nth 1 eq))
  9542. (org-table-goto-column (nth 2 eq))
  9543. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9544. 'nostore 'noanalysis))
  9545. (goto-line thisline)
  9546. (org-table-goto-column thiscol)
  9547. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9548. (or noalign (and org-table-may-need-update (org-table-align))
  9549. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9550. ;; back to initial position
  9551. (message "Re-applying formulas...done")
  9552. (goto-line thisline)
  9553. (org-table-goto-column thiscol)
  9554. (or noalign (and org-table-may-need-update (org-table-align))
  9555. (and all (message "Re-applying formulas...done"))))))
  9556. (defun org-table-iterate (&optional arg)
  9557. "Recalculate the table until it does not change anymore."
  9558. (interactive "P")
  9559. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9560. (i 0)
  9561. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9562. thistbl)
  9563. (catch 'exit
  9564. (while (< i imax)
  9565. (setq i (1+ i))
  9566. (org-table-recalculate 'all)
  9567. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9568. (if (not (string= lasttbl thistbl))
  9569. (setq lasttbl thistbl)
  9570. (if (> i 1)
  9571. (message "Convergence after %d iterations" i)
  9572. (message "Table was already stable"))
  9573. (throw 'exit t)))
  9574. (error "No convergence after %d iterations" i))))
  9575. (defun org-table-formula-substitute-names (f)
  9576. "Replace $const with values in string F."
  9577. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9578. ;; First, check for column names
  9579. (while (setq start (string-match org-table-column-name-regexp f start))
  9580. (setq start (1+ start))
  9581. (setq a (assoc (match-string 1 f) org-table-column-names))
  9582. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9583. ;; Parameters and constants
  9584. (setq start 0)
  9585. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9586. (setq start (1+ start))
  9587. (if (setq a (save-match-data
  9588. (org-table-get-constant (match-string 1 f))))
  9589. (setq f (replace-match
  9590. (concat (if pp "(") a (if pp ")")) t t f))))
  9591. (if org-table-formula-debug
  9592. (put-text-property 0 (length f) :orig-formula f1 f))
  9593. f))
  9594. (defun org-table-get-constant (const)
  9595. "Find the value for a parameter or constant in a formula.
  9596. Parameters get priority."
  9597. (or (cdr (assoc const org-table-local-parameters))
  9598. (cdr (assoc const org-table-formula-constants-local))
  9599. (cdr (assoc const org-table-formula-constants))
  9600. (and (fboundp 'constants-get) (constants-get const))
  9601. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9602. (org-entry-get nil (substring const 5) 'inherit))
  9603. "#UNDEFINED_NAME"))
  9604. (defvar org-table-fedit-map
  9605. (let ((map (make-sparse-keymap)))
  9606. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9607. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9608. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9609. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9610. (org-defkey map "\C-c?" 'org-table-show-reference)
  9611. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9612. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9613. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9614. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9615. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9616. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9617. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9618. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9619. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9620. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9621. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9622. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9623. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9624. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9625. map))
  9626. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9627. '("Edit-Formulas"
  9628. ["Finish and Install" org-table-fedit-finish t]
  9629. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9630. ["Abort" org-table-fedit-abort t]
  9631. "--"
  9632. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9633. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9634. "--"
  9635. "Shift Reference at Point"
  9636. ["Up" org-table-fedit-ref-up t]
  9637. ["Down" org-table-fedit-ref-down t]
  9638. ["Left" org-table-fedit-ref-left t]
  9639. ["Right" org-table-fedit-ref-right t]
  9640. "-"
  9641. "Change Test Row for Column Formulas"
  9642. ["Up" org-table-fedit-line-up t]
  9643. ["Down" org-table-fedit-line-down t]
  9644. "--"
  9645. ["Scroll Table Window" org-table-fedit-scroll t]
  9646. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9647. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9648. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9649. org-table-overlay-coordinates)]
  9650. "--"
  9651. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9652. :style toggle :selected org-table-buffer-is-an]))
  9653. (defvar org-pos)
  9654. (defun org-table-edit-formulas ()
  9655. "Edit the formulas of the current table in a separate buffer."
  9656. (interactive)
  9657. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9658. (beginning-of-line 0))
  9659. (unless (org-at-table-p) (error "Not at a table"))
  9660. (org-table-get-specials)
  9661. (let ((key (org-table-current-field-formula 'key 'noerror))
  9662. (eql (sort (org-table-get-stored-formulas 'noerror)
  9663. 'org-table-formula-less-p))
  9664. (pos (move-marker (make-marker) (point)))
  9665. (startline 1)
  9666. (wc (current-window-configuration))
  9667. (titles '((column . "# Column Formulas\n")
  9668. (field . "# Field Formulas\n")
  9669. (named . "# Named Field Formulas\n")))
  9670. entry s type title)
  9671. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9672. (erase-buffer)
  9673. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9674. (let ((font-lock-global-modes '(not fundamental-mode)))
  9675. (fundamental-mode))
  9676. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9677. (org-set-local 'org-pos pos)
  9678. (org-set-local 'org-window-configuration wc)
  9679. (use-local-map org-table-fedit-map)
  9680. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9681. (easy-menu-add org-table-fedit-menu)
  9682. (setq startline (org-current-line))
  9683. (while (setq entry (pop eql))
  9684. (setq type (cond
  9685. ((equal (string-to-char (car entry)) ?@) 'field)
  9686. ((string-match "^[0-9]" (car entry)) 'column)
  9687. (t 'named)))
  9688. (when (setq title (assq type titles))
  9689. (or (bobp) (insert "\n"))
  9690. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9691. (setq titles (delq title titles)))
  9692. (if (equal key (car entry)) (setq startline (org-current-line)))
  9693. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9694. (car entry) " = " (cdr entry) "\n"))
  9695. (remove-text-properties 0 (length s) '(face nil) s)
  9696. (insert s))
  9697. (if (eq org-table-use-standard-references t)
  9698. (org-table-fedit-toggle-ref-type))
  9699. (goto-line startline)
  9700. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9701. (defun org-table-fedit-post-command ()
  9702. (when (not (memq this-command '(lisp-complete-symbol)))
  9703. (let ((win (selected-window)))
  9704. (save-excursion
  9705. (condition-case nil
  9706. (org-table-show-reference)
  9707. (error nil))
  9708. (select-window win)))))
  9709. (defun org-table-formula-to-user (s)
  9710. "Convert a formula from internal to user representation."
  9711. (if (eq org-table-use-standard-references t)
  9712. (org-table-convert-refs-to-an s)
  9713. s))
  9714. (defun org-table-formula-from-user (s)
  9715. "Convert a formula from user to internal representation."
  9716. (if org-table-use-standard-references
  9717. (org-table-convert-refs-to-rc s)
  9718. s))
  9719. (defun org-table-convert-refs-to-rc (s)
  9720. "Convert spreadsheet references from AB7 to @7$28.
  9721. Works for single references, but also for entire formulas and even the
  9722. full TBLFM line."
  9723. (let ((start 0))
  9724. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9725. (cond
  9726. ((match-end 3)
  9727. ;; format match, just advance
  9728. (setq start (match-end 0)))
  9729. ((and (> (match-beginning 0) 0)
  9730. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9731. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9732. ;; 3.e5 or something like this.
  9733. (setq start (match-end 0)))
  9734. (t
  9735. (setq start (match-beginning 0)
  9736. s (replace-match
  9737. (if (equal (match-string 2 s) "&")
  9738. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9739. (format "@%d$%d"
  9740. (string-to-number (match-string 2 s))
  9741. (org-letters-to-number (match-string 1 s))))
  9742. t t s)))))
  9743. s))
  9744. (defun org-table-convert-refs-to-an (s)
  9745. "Convert spreadsheet references from to @7$28 to AB7.
  9746. Works for single references, but also for entire formulas and even the
  9747. full TBLFM line."
  9748. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9749. (setq s (replace-match
  9750. (format "%s%d"
  9751. (org-number-to-letters
  9752. (string-to-number (match-string 2 s)))
  9753. (string-to-number (match-string 1 s)))
  9754. t t s)))
  9755. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9756. (setq s (replace-match (concat "\\1"
  9757. (org-number-to-letters
  9758. (string-to-number (match-string 2 s))) "&")
  9759. t nil s)))
  9760. s)
  9761. (defun org-letters-to-number (s)
  9762. "Convert a base 26 number represented by letters into an integer.
  9763. For example: AB -> 28."
  9764. (let ((n 0))
  9765. (setq s (upcase s))
  9766. (while (> (length s) 0)
  9767. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9768. s (substring s 1)))
  9769. n))
  9770. (defun org-number-to-letters (n)
  9771. "Convert an integer into a base 26 number represented by letters.
  9772. For example: 28 -> AB."
  9773. (let ((s ""))
  9774. (while (> n 0)
  9775. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9776. n (/ (1- n) 26)))
  9777. s))
  9778. (defun org-table-fedit-convert-buffer (function)
  9779. "Convert all references in this buffer, using FUNTION."
  9780. (let ((line (org-current-line)))
  9781. (goto-char (point-min))
  9782. (while (not (eobp))
  9783. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9784. (delete-region (point) (point-at-eol))
  9785. (or (eobp) (forward-char 1)))
  9786. (goto-line line)))
  9787. (defun org-table-fedit-toggle-ref-type ()
  9788. "Convert all references in the buffer from B3 to @3$2 and back."
  9789. (interactive)
  9790. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9791. (org-table-fedit-convert-buffer
  9792. (if org-table-buffer-is-an
  9793. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9794. (message "Reference type switched to %s"
  9795. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9796. (defun org-table-fedit-ref-up ()
  9797. "Shift the reference at point one row/hline up."
  9798. (interactive)
  9799. (org-table-fedit-shift-reference 'up))
  9800. (defun org-table-fedit-ref-down ()
  9801. "Shift the reference at point one row/hline down."
  9802. (interactive)
  9803. (org-table-fedit-shift-reference 'down))
  9804. (defun org-table-fedit-ref-left ()
  9805. "Shift the reference at point one field to the left."
  9806. (interactive)
  9807. (org-table-fedit-shift-reference 'left))
  9808. (defun org-table-fedit-ref-right ()
  9809. "Shift the reference at point one field to the right."
  9810. (interactive)
  9811. (org-table-fedit-shift-reference 'right))
  9812. (defun org-table-fedit-shift-reference (dir)
  9813. (cond
  9814. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9815. (if (memq dir '(left right))
  9816. (org-rematch-and-replace 1 (eq dir 'left))
  9817. (error "Cannot shift reference in this direction")))
  9818. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9819. ;; A B3-like reference
  9820. (if (memq dir '(up down))
  9821. (org-rematch-and-replace 2 (eq dir 'up))
  9822. (org-rematch-and-replace 1 (eq dir 'left))))
  9823. ((org-at-regexp-p
  9824. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9825. ;; An internal reference
  9826. (if (memq dir '(up down))
  9827. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9828. (org-rematch-and-replace 5 (eq dir 'left))))))
  9829. (defun org-rematch-and-replace (n &optional decr hline)
  9830. "Re-match the group N, and replace it with the shifted refrence."
  9831. (or (match-end n) (error "Cannot shift reference in this direction"))
  9832. (goto-char (match-beginning n))
  9833. (and (looking-at (regexp-quote (match-string n)))
  9834. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9835. t t)))
  9836. (defun org-shift-refpart (ref &optional decr hline)
  9837. "Shift a refrence part REF.
  9838. If DECR is set, decrease the references row/column, else increase.
  9839. If HLINE is set, this may be a hline reference, it certainly is not
  9840. a translation reference."
  9841. (save-match-data
  9842. (let* ((sign (string-match "^[-+]" ref)) n)
  9843. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9844. (cond
  9845. ((and hline (string-match "^I+" ref))
  9846. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9847. (setq n (+ n (if decr -1 1)))
  9848. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9849. (if sign
  9850. (setq sign (if (< n 0) "-" "+") n (abs n))
  9851. (setq n (max 1 n)))
  9852. (concat sign (make-string n ?I)))
  9853. ((string-match "^[0-9]+" ref)
  9854. (setq n (string-to-number (concat sign ref)))
  9855. (setq n (+ n (if decr -1 1)))
  9856. (if sign
  9857. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9858. (number-to-string (max 1 n))))
  9859. ((string-match "^[a-zA-Z]+" ref)
  9860. (org-number-to-letters
  9861. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9862. (t (error "Cannot shift reference"))))))
  9863. (defun org-table-fedit-toggle-coordinates ()
  9864. "Toggle the display of coordinates in the refrenced table."
  9865. (interactive)
  9866. (let ((pos (marker-position org-pos)))
  9867. (with-current-buffer (marker-buffer org-pos)
  9868. (save-excursion
  9869. (goto-char pos)
  9870. (org-table-toggle-coordinate-overlays)))))
  9871. (defun org-table-fedit-finish (&optional arg)
  9872. "Parse the buffer for formula definitions and install them.
  9873. With prefix ARG, apply the new formulas to the table."
  9874. (interactive "P")
  9875. (org-table-remove-rectangle-highlight)
  9876. (if org-table-use-standard-references
  9877. (progn
  9878. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9879. (setq org-table-buffer-is-an nil)))
  9880. (let ((pos org-pos) eql var form)
  9881. (goto-char (point-min))
  9882. (while (re-search-forward
  9883. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9884. nil t)
  9885. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9886. form (match-string 3))
  9887. (setq form (org-trim form))
  9888. (when (not (equal form ""))
  9889. (while (string-match "[ \t]*\n[ \t]*" form)
  9890. (setq form (replace-match " " t t form)))
  9891. (when (assoc var eql)
  9892. (error "Double formulas for %s" var))
  9893. (push (cons var form) eql)))
  9894. (setq org-pos nil)
  9895. (set-window-configuration org-window-configuration)
  9896. (select-window (get-buffer-window (marker-buffer pos)))
  9897. (goto-char pos)
  9898. (unless (org-at-table-p)
  9899. (error "Lost table position - cannot install formulae"))
  9900. (org-table-store-formulas eql)
  9901. (move-marker pos nil)
  9902. (kill-buffer "*Edit Formulas*")
  9903. (if arg
  9904. (org-table-recalculate 'all)
  9905. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9906. (defun org-table-fedit-abort ()
  9907. "Abort editing formulas, without installing the changes."
  9908. (interactive)
  9909. (org-table-remove-rectangle-highlight)
  9910. (let ((pos org-pos))
  9911. (set-window-configuration org-window-configuration)
  9912. (select-window (get-buffer-window (marker-buffer pos)))
  9913. (goto-char pos)
  9914. (move-marker pos nil)
  9915. (message "Formula editing aborted without installing changes")))
  9916. (defun org-table-fedit-lisp-indent ()
  9917. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9918. (interactive)
  9919. (let ((pos (point)) beg end ind)
  9920. (beginning-of-line 1)
  9921. (cond
  9922. ((looking-at "[ \t]")
  9923. (goto-char pos)
  9924. (call-interactively 'lisp-indent-line))
  9925. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9926. ((not (fboundp 'pp-buffer))
  9927. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9928. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9929. (goto-char (- (match-end 0) 2))
  9930. (setq beg (point))
  9931. (setq ind (make-string (current-column) ?\ ))
  9932. (condition-case nil (forward-sexp 1)
  9933. (error
  9934. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9935. (setq end (point))
  9936. (save-restriction
  9937. (narrow-to-region beg end)
  9938. (if (eq last-command this-command)
  9939. (progn
  9940. (goto-char (point-min))
  9941. (setq this-command nil)
  9942. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9943. (replace-match " ")))
  9944. (pp-buffer)
  9945. (untabify (point-min) (point-max))
  9946. (goto-char (1+ (point-min)))
  9947. (while (re-search-forward "^." nil t)
  9948. (beginning-of-line 1)
  9949. (insert ind))
  9950. (goto-char (point-max))
  9951. (backward-delete-char 1)))
  9952. (goto-char beg))
  9953. (t nil))))
  9954. (defvar org-show-positions nil)
  9955. (defun org-table-show-reference (&optional local)
  9956. "Show the location/value of the $ expression at point."
  9957. (interactive)
  9958. (org-table-remove-rectangle-highlight)
  9959. (catch 'exit
  9960. (let ((pos (if local (point) org-pos))
  9961. (face2 'highlight)
  9962. (org-inhibit-highlight-removal t)
  9963. (win (selected-window))
  9964. (org-show-positions nil)
  9965. var name e what match dest)
  9966. (if local (org-table-get-specials))
  9967. (setq what (cond
  9968. ((or (org-at-regexp-p org-table-range-regexp2)
  9969. (org-at-regexp-p org-table-translate-regexp)
  9970. (org-at-regexp-p org-table-range-regexp))
  9971. (setq match
  9972. (save-match-data
  9973. (org-table-convert-refs-to-rc (match-string 0))))
  9974. 'range)
  9975. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9976. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9977. ((not local) nil)
  9978. (t (error "No reference at point")))
  9979. match (and what (or match (match-string 0))))
  9980. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9981. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9982. 'secondary-selection))
  9983. (org-add-hook 'before-change-functions
  9984. 'org-table-remove-rectangle-highlight)
  9985. (if (eq what 'name) (setq var (substring match 1)))
  9986. (when (eq what 'range)
  9987. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9988. (setq match (org-table-formula-substitute-names match)))
  9989. (unless local
  9990. (save-excursion
  9991. (end-of-line 1)
  9992. (re-search-backward "^\\S-" nil t)
  9993. (beginning-of-line 1)
  9994. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9995. (setq dest
  9996. (save-match-data
  9997. (org-table-convert-refs-to-rc (match-string 1))))
  9998. (org-table-add-rectangle-overlay
  9999. (match-beginning 1) (match-end 1) face2))))
  10000. (if (and (markerp pos) (marker-buffer pos))
  10001. (if (get-buffer-window (marker-buffer pos))
  10002. (select-window (get-buffer-window (marker-buffer pos)))
  10003. (org-switch-to-buffer-other-window (get-buffer-window
  10004. (marker-buffer pos)))))
  10005. (goto-char pos)
  10006. (org-table-force-dataline)
  10007. (when dest
  10008. (setq name (substring dest 1))
  10009. (cond
  10010. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10011. (setq e (assoc name org-table-named-field-locations))
  10012. (goto-line (nth 1 e))
  10013. (org-table-goto-column (nth 2 e)))
  10014. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10015. (let ((l (string-to-number (match-string 1 dest)))
  10016. (c (string-to-number (match-string 2 dest))))
  10017. (goto-line (aref org-table-dlines l))
  10018. (org-table-goto-column c)))
  10019. (t (org-table-goto-column (string-to-number name))))
  10020. (move-marker pos (point))
  10021. (org-table-highlight-rectangle nil nil face2))
  10022. (cond
  10023. ((equal dest match))
  10024. ((not match))
  10025. ((eq what 'range)
  10026. (condition-case nil
  10027. (save-excursion
  10028. (org-table-get-range match nil nil 'highlight))
  10029. (error nil)))
  10030. ((setq e (assoc var org-table-named-field-locations))
  10031. (goto-line (nth 1 e))
  10032. (org-table-goto-column (nth 2 e))
  10033. (org-table-highlight-rectangle (point) (point))
  10034. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10035. ((setq e (assoc var org-table-column-names))
  10036. (org-table-goto-column (string-to-number (cdr e)))
  10037. (org-table-highlight-rectangle (point) (point))
  10038. (goto-char (org-table-begin))
  10039. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10040. (org-table-end) t)
  10041. (progn
  10042. (goto-char (match-beginning 1))
  10043. (org-table-highlight-rectangle)
  10044. (message "Named column (column %s)" (cdr e)))
  10045. (error "Column name not found")))
  10046. ((eq what 'column)
  10047. ;; column number
  10048. (org-table-goto-column (string-to-number (substring match 1)))
  10049. (org-table-highlight-rectangle (point) (point))
  10050. (message "Column %s" (substring match 1)))
  10051. ((setq e (assoc var org-table-local-parameters))
  10052. (goto-char (org-table-begin))
  10053. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10054. (progn
  10055. (goto-char (match-beginning 1))
  10056. (org-table-highlight-rectangle)
  10057. (message "Local parameter."))
  10058. (error "Parameter not found")))
  10059. (t
  10060. (cond
  10061. ((not var) (error "No reference at point"))
  10062. ((setq e (assoc var org-table-formula-constants-local))
  10063. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10064. var (cdr e)))
  10065. ((setq e (assoc var org-table-formula-constants))
  10066. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10067. var (cdr e)))
  10068. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10069. (message "Constant: $%s=%s, from `constants.el'%s."
  10070. var e (format " (%s units)" constants-unit-system)))
  10071. (t (error "Undefined name $%s" var)))))
  10072. (goto-char pos)
  10073. (when (and org-show-positions
  10074. (not (memq this-command '(org-table-fedit-scroll
  10075. org-table-fedit-scroll-down))))
  10076. (push pos org-show-positions)
  10077. (push org-table-current-begin-pos org-show-positions)
  10078. (let ((min (apply 'min org-show-positions))
  10079. (max (apply 'max org-show-positions)))
  10080. (goto-char min) (recenter 0)
  10081. (goto-char max)
  10082. (or (pos-visible-in-window-p max) (recenter -1))))
  10083. (select-window win))))
  10084. (defun org-table-force-dataline ()
  10085. "Make sure the cursor is in a dataline in a table."
  10086. (unless (save-excursion
  10087. (beginning-of-line 1)
  10088. (looking-at org-table-dataline-regexp))
  10089. (let* ((re org-table-dataline-regexp)
  10090. (p1 (save-excursion (re-search-forward re nil 'move)))
  10091. (p2 (save-excursion (re-search-backward re nil 'move))))
  10092. (cond ((and p1 p2)
  10093. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10094. p1 p2)))
  10095. ((or p1 p2) (goto-char (or p1 p2)))
  10096. (t (error "No table dataline around here"))))))
  10097. (defun org-table-fedit-line-up ()
  10098. "Move cursor one line up in the window showing the table."
  10099. (interactive)
  10100. (org-table-fedit-move 'previous-line))
  10101. (defun org-table-fedit-line-down ()
  10102. "Move cursor one line down in the window showing the table."
  10103. (interactive)
  10104. (org-table-fedit-move 'next-line))
  10105. (defun org-table-fedit-move (command)
  10106. "Move the cursor in the window shoinw the table.
  10107. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10108. (let ((org-table-allow-automatic-line-recalculation nil)
  10109. (pos org-pos) (win (selected-window)) p)
  10110. (select-window (get-buffer-window (marker-buffer org-pos)))
  10111. (setq p (point))
  10112. (call-interactively command)
  10113. (while (and (org-at-table-p)
  10114. (org-at-table-hline-p))
  10115. (call-interactively command))
  10116. (or (org-at-table-p) (goto-char p))
  10117. (move-marker pos (point))
  10118. (select-window win)))
  10119. (defun org-table-fedit-scroll (N)
  10120. (interactive "p")
  10121. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10122. (scroll-other-window N)))
  10123. (defun org-table-fedit-scroll-down (N)
  10124. (interactive "p")
  10125. (org-table-fedit-scroll (- N)))
  10126. (defvar org-table-rectangle-overlays nil)
  10127. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10128. "Add a new overlay."
  10129. (let ((ov (org-make-overlay beg end)))
  10130. (org-overlay-put ov 'face (or face 'secondary-selection))
  10131. (push ov org-table-rectangle-overlays)))
  10132. (defun org-table-highlight-rectangle (&optional beg end face)
  10133. "Highlight rectangular region in a table."
  10134. (setq beg (or beg (point)) end (or end (point)))
  10135. (let ((b (min beg end))
  10136. (e (max beg end))
  10137. l1 c1 l2 c2 tmp)
  10138. (and (boundp 'org-show-positions)
  10139. (setq org-show-positions (cons b (cons e org-show-positions))))
  10140. (goto-char (min beg end))
  10141. (setq l1 (org-current-line)
  10142. c1 (org-table-current-column))
  10143. (goto-char (max beg end))
  10144. (setq l2 (org-current-line)
  10145. c2 (org-table-current-column))
  10146. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10147. (goto-line l1)
  10148. (beginning-of-line 1)
  10149. (loop for line from l1 to l2 do
  10150. (when (looking-at org-table-dataline-regexp)
  10151. (org-table-goto-column c1)
  10152. (skip-chars-backward "^|\n") (setq beg (point))
  10153. (org-table-goto-column c2)
  10154. (skip-chars-forward "^|\n") (setq end (point))
  10155. (org-table-add-rectangle-overlay beg end face))
  10156. (beginning-of-line 2))
  10157. (goto-char b))
  10158. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10159. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10160. "Remove the rectangle overlays."
  10161. (unless org-inhibit-highlight-removal
  10162. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10163. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10164. (setq org-table-rectangle-overlays nil)))
  10165. (defvar org-table-coordinate-overlays nil
  10166. "Collects the cooordinate grid overlays, so that they can be removed.")
  10167. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10168. (defun org-table-overlay-coordinates ()
  10169. "Add overlays to the table at point, to show row/column coordinates."
  10170. (interactive)
  10171. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10172. (setq org-table-coordinate-overlays nil)
  10173. (save-excursion
  10174. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10175. (goto-char (org-table-begin))
  10176. (while (org-at-table-p)
  10177. (setq eol (point-at-eol))
  10178. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10179. (push ov org-table-coordinate-overlays)
  10180. (setq hline (looking-at org-table-hline-regexp))
  10181. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10182. (format "%4d" (setq id (1+ id)))))
  10183. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10184. (when hline
  10185. (setq ic 0)
  10186. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10187. (setq beg (1+ (match-beginning 0))
  10188. ic (1+ ic)
  10189. s1 (concat "$" (int-to-string ic))
  10190. s2 (org-number-to-letters ic)
  10191. str (if (eq org-table-use-standard-references t) s2 s1))
  10192. (setq ov (org-make-overlay beg (+ beg (length str))))
  10193. (push ov org-table-coordinate-overlays)
  10194. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10195. (beginning-of-line 2)))))
  10196. (defun org-table-toggle-coordinate-overlays ()
  10197. "Toggle the display of Row/Column numbers in tables."
  10198. (interactive)
  10199. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10200. (message "Row/Column number display turned %s"
  10201. (if org-table-overlay-coordinates "on" "off"))
  10202. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10203. (org-table-align))
  10204. (unless org-table-overlay-coordinates
  10205. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10206. (setq org-table-coordinate-overlays nil)))
  10207. (defun org-table-toggle-formula-debugger ()
  10208. "Toggle the formula debugger in tables."
  10209. (interactive)
  10210. (setq org-table-formula-debug (not org-table-formula-debug))
  10211. (message "Formula debugging has been turned %s"
  10212. (if org-table-formula-debug "on" "off")))
  10213. ;;; The orgtbl minor mode
  10214. ;; Define a minor mode which can be used in other modes in order to
  10215. ;; integrate the org-mode table editor.
  10216. ;; This is really a hack, because the org-mode table editor uses several
  10217. ;; keys which normally belong to the major mode, for example the TAB and
  10218. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10219. ;; necessary to operate the table editor, but wraps the commands into a
  10220. ;; function which tests if the cursor is currently inside a table. If that
  10221. ;; is the case, the table editor command is executed. However, when any of
  10222. ;; those keys is used outside a table, the function uses `key-binding' to
  10223. ;; look up if the key has an associated command in another currently active
  10224. ;; keymap (minor modes, major mode, global), and executes that command.
  10225. ;; There might be problems if any of the keys used by the table editor is
  10226. ;; otherwise used as a prefix key.
  10227. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10228. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10229. ;; addresses this by checking explicitly for both bindings.
  10230. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10231. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10232. ;; Some modes bind other commands to simple characters, for example
  10233. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10234. ;; active, this binding is ignored inside tables and replaced with a
  10235. ;; modified self-insert.
  10236. (defvar orgtbl-mode nil
  10237. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10238. table editor in arbitrary modes.")
  10239. (make-variable-buffer-local 'orgtbl-mode)
  10240. (defvar orgtbl-mode-map (make-keymap)
  10241. "Keymap for `orgtbl-mode'.")
  10242. ;;;###autoload
  10243. (defun turn-on-orgtbl ()
  10244. "Unconditionally turn on `orgtbl-mode'."
  10245. (orgtbl-mode 1))
  10246. (defvar org-old-auto-fill-inhibit-regexp nil
  10247. "Local variable used by `orgtbl-mode'")
  10248. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10249. "Matches a line belonging to an orgtbl.")
  10250. (defconst orgtbl-extra-font-lock-keywords
  10251. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10252. 0 (quote 'org-table) 'prepend))
  10253. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10254. ;;;###autoload
  10255. (defun orgtbl-mode (&optional arg)
  10256. "The `org-mode' table editor as a minor mode for use in other modes."
  10257. (interactive)
  10258. (if (org-mode-p)
  10259. ;; Exit without error, in case some hook functions calls this
  10260. ;; by accident in org-mode.
  10261. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10262. (setq orgtbl-mode
  10263. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10264. (if orgtbl-mode
  10265. (progn
  10266. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10267. ;; Make sure we are first in minor-mode-map-alist
  10268. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10269. (and c (setq minor-mode-map-alist
  10270. (cons c (delq c minor-mode-map-alist)))))
  10271. (org-set-local (quote org-table-may-need-update) t)
  10272. (org-add-hook 'before-change-functions 'org-before-change-function
  10273. nil 'local)
  10274. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10275. auto-fill-inhibit-regexp)
  10276. (org-set-local 'auto-fill-inhibit-regexp
  10277. (if auto-fill-inhibit-regexp
  10278. (concat orgtbl-line-start-regexp "\\|"
  10279. auto-fill-inhibit-regexp)
  10280. orgtbl-line-start-regexp))
  10281. (org-add-to-invisibility-spec '(org-cwidth))
  10282. (when (fboundp 'font-lock-add-keywords)
  10283. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10284. (org-restart-font-lock))
  10285. (easy-menu-add orgtbl-mode-menu)
  10286. (run-hooks 'orgtbl-mode-hook))
  10287. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10288. (org-cleanup-narrow-column-properties)
  10289. (org-remove-from-invisibility-spec '(org-cwidth))
  10290. (remove-hook 'before-change-functions 'org-before-change-function t)
  10291. (when (fboundp 'font-lock-remove-keywords)
  10292. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10293. (org-restart-font-lock))
  10294. (easy-menu-remove orgtbl-mode-menu)
  10295. (force-mode-line-update 'all))))
  10296. (defun org-cleanup-narrow-column-properties ()
  10297. "Remove all properties related to narrow-column invisibility."
  10298. (let ((s 1))
  10299. (while (setq s (text-property-any s (point-max)
  10300. 'display org-narrow-column-arrow))
  10301. (remove-text-properties s (1+ s) '(display t)))
  10302. (setq s 1)
  10303. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10304. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10305. (setq s 1)
  10306. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10307. (remove-text-properties s (1+ s) '(invisible t)))))
  10308. ;; Install it as a minor mode.
  10309. (put 'orgtbl-mode :included t)
  10310. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10311. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10312. (defun orgtbl-make-binding (fun n &rest keys)
  10313. "Create a function for binding in the table minor mode.
  10314. FUN is the command to call inside a table. N is used to create a unique
  10315. command name. KEYS are keys that should be checked in for a command
  10316. to execute outside of tables."
  10317. (eval
  10318. (list 'defun
  10319. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10320. '(arg)
  10321. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10322. "Outside of tables, run the binding of `"
  10323. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10324. "'.")
  10325. '(interactive "p")
  10326. (list 'if
  10327. '(org-at-table-p)
  10328. (list 'call-interactively (list 'quote fun))
  10329. (list 'let '(orgtbl-mode)
  10330. (list 'call-interactively
  10331. (append '(or)
  10332. (mapcar (lambda (k)
  10333. (list 'key-binding k))
  10334. keys)
  10335. '('orgtbl-error))))))))
  10336. (defun orgtbl-error ()
  10337. "Error when there is no default binding for a table key."
  10338. (interactive)
  10339. (error "This key has no function outside tables"))
  10340. (defun orgtbl-setup ()
  10341. "Setup orgtbl keymaps."
  10342. (let ((nfunc 0)
  10343. (bindings
  10344. (list
  10345. '([(meta shift left)] org-table-delete-column)
  10346. '([(meta left)] org-table-move-column-left)
  10347. '([(meta right)] org-table-move-column-right)
  10348. '([(meta shift right)] org-table-insert-column)
  10349. '([(meta shift up)] org-table-kill-row)
  10350. '([(meta shift down)] org-table-insert-row)
  10351. '([(meta up)] org-table-move-row-up)
  10352. '([(meta down)] org-table-move-row-down)
  10353. '("\C-c\C-w" org-table-cut-region)
  10354. '("\C-c\M-w" org-table-copy-region)
  10355. '("\C-c\C-y" org-table-paste-rectangle)
  10356. '("\C-c-" org-table-insert-hline)
  10357. '("\C-c}" org-table-toggle-coordinate-overlays)
  10358. '("\C-c{" org-table-toggle-formula-debugger)
  10359. '("\C-m" org-table-next-row)
  10360. '([(shift return)] org-table-copy-down)
  10361. '("\C-c\C-q" org-table-wrap-region)
  10362. '("\C-c?" org-table-field-info)
  10363. '("\C-c " org-table-blank-field)
  10364. '("\C-c+" org-table-sum)
  10365. '("\C-c=" org-table-eval-formula)
  10366. '("\C-c'" org-table-edit-formulas)
  10367. '("\C-c`" org-table-edit-field)
  10368. '("\C-c*" org-table-recalculate)
  10369. '("\C-c|" org-table-create-or-convert-from-region)
  10370. '("\C-c^" org-table-sort-lines)
  10371. '([(control ?#)] org-table-rotate-recalc-marks)))
  10372. elt key fun cmd)
  10373. (while (setq elt (pop bindings))
  10374. (setq nfunc (1+ nfunc))
  10375. (setq key (org-key (car elt))
  10376. fun (nth 1 elt)
  10377. cmd (orgtbl-make-binding fun nfunc key))
  10378. (org-defkey orgtbl-mode-map key cmd))
  10379. ;; Special treatment needed for TAB and RET
  10380. (org-defkey orgtbl-mode-map [(return)]
  10381. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10382. (org-defkey orgtbl-mode-map "\C-m"
  10383. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10384. (org-defkey orgtbl-mode-map [(tab)]
  10385. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10386. (org-defkey orgtbl-mode-map "\C-i"
  10387. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10388. (org-defkey orgtbl-mode-map [(shift tab)]
  10389. (orgtbl-make-binding 'org-table-previous-field 104
  10390. [(shift tab)] [(tab)] "\C-i"))
  10391. (org-defkey orgtbl-mode-map "\M-\C-m"
  10392. (orgtbl-make-binding 'org-table-wrap-region 105
  10393. "\M-\C-m" [(meta return)]))
  10394. (org-defkey orgtbl-mode-map [(meta return)]
  10395. (orgtbl-make-binding 'org-table-wrap-region 106
  10396. [(meta return)] "\M-\C-m"))
  10397. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10398. (when orgtbl-optimized
  10399. ;; If the user wants maximum table support, we need to hijack
  10400. ;; some standard editing functions
  10401. (org-remap orgtbl-mode-map
  10402. 'self-insert-command 'orgtbl-self-insert-command
  10403. 'delete-char 'org-delete-char
  10404. 'delete-backward-char 'org-delete-backward-char)
  10405. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10406. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10407. '("OrgTbl"
  10408. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10409. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10410. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10411. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10412. "--"
  10413. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10414. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10415. ["Copy Field from Above"
  10416. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10417. "--"
  10418. ("Column"
  10419. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10420. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10421. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10422. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10423. ("Row"
  10424. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10425. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10426. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10427. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10428. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10429. "--"
  10430. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10431. ("Rectangle"
  10432. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10433. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10434. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10435. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10436. "--"
  10437. ("Radio tables"
  10438. ["Insert table template" orgtbl-insert-radio-table
  10439. (assq major-mode orgtbl-radio-table-templates)]
  10440. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10441. "--"
  10442. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10443. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10444. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10445. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10446. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10447. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10448. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10449. ["Sum Column/Rectangle" org-table-sum
  10450. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10451. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10452. ["Debug Formulas"
  10453. org-table-toggle-formula-debugger :active (org-at-table-p)
  10454. :keys "C-c {"
  10455. :style toggle :selected org-table-formula-debug]
  10456. ["Show Col/Row Numbers"
  10457. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10458. :keys "C-c }"
  10459. :style toggle :selected org-table-overlay-coordinates]
  10460. ))
  10461. t))
  10462. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10463. "If the cursor is inside a table, realign the table.
  10464. It it is a table to be sent away to a receiver, do it.
  10465. With prefix arg, also recompute table."
  10466. (interactive "P")
  10467. (let ((pos (point)) action)
  10468. (save-excursion
  10469. (beginning-of-line 1)
  10470. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10471. ((looking-at "[ \t]*|") pos)
  10472. ((looking-at "#\\+TBLFM:") 'recalc))))
  10473. (cond
  10474. ((integerp action)
  10475. (goto-char action)
  10476. (org-table-maybe-eval-formula)
  10477. (if arg
  10478. (call-interactively 'org-table-recalculate)
  10479. (org-table-maybe-recalculate-line))
  10480. (call-interactively 'org-table-align)
  10481. (orgtbl-send-table 'maybe))
  10482. ((eq action 'recalc)
  10483. (save-excursion
  10484. (beginning-of-line 1)
  10485. (skip-chars-backward " \r\n\t")
  10486. (if (org-at-table-p)
  10487. (org-call-with-arg 'org-table-recalculate t))))
  10488. (t (let (orgtbl-mode)
  10489. (call-interactively (key-binding "\C-c\C-c")))))))
  10490. (defun orgtbl-tab (arg)
  10491. "Justification and field motion for `orgtbl-mode'."
  10492. (interactive "P")
  10493. (if arg (org-table-edit-field t)
  10494. (org-table-justify-field-maybe)
  10495. (org-table-next-field)))
  10496. (defun orgtbl-ret ()
  10497. "Justification and field motion for `orgtbl-mode'."
  10498. (interactive)
  10499. (org-table-justify-field-maybe)
  10500. (org-table-next-row))
  10501. (defun orgtbl-self-insert-command (N)
  10502. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10503. If the cursor is in a table looking at whitespace, the whitespace is
  10504. overwritten, and the table is not marked as requiring realignment."
  10505. (interactive "p")
  10506. (if (and (org-at-table-p)
  10507. (or
  10508. (and org-table-auto-blank-field
  10509. (member last-command
  10510. '(orgtbl-hijacker-command-100
  10511. orgtbl-hijacker-command-101
  10512. orgtbl-hijacker-command-102
  10513. orgtbl-hijacker-command-103
  10514. orgtbl-hijacker-command-104
  10515. orgtbl-hijacker-command-105))
  10516. (org-table-blank-field))
  10517. t)
  10518. (eq N 1)
  10519. (looking-at "[^|\n]* +|"))
  10520. (let (org-table-may-need-update)
  10521. (goto-char (1- (match-end 0)))
  10522. (delete-backward-char 1)
  10523. (goto-char (match-beginning 0))
  10524. (self-insert-command N))
  10525. (setq org-table-may-need-update t)
  10526. (let (orgtbl-mode)
  10527. (call-interactively (key-binding (vector last-input-event))))))
  10528. (defun org-force-self-insert (N)
  10529. "Needed to enforce self-insert under remapping."
  10530. (interactive "p")
  10531. (self-insert-command N))
  10532. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10533. "Regula expression matching exponentials as produced by calc.")
  10534. (defvar org-table-clean-did-remove-column nil)
  10535. (defun orgtbl-export (table target)
  10536. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10537. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10538. org-table-last-alignment org-table-last-column-widths
  10539. maxcol column)
  10540. (if (not (fboundp func))
  10541. (error "Cannot export orgtbl table to %s" target))
  10542. (setq lines (org-table-clean-before-export lines))
  10543. (setq table
  10544. (mapcar
  10545. (lambda (x)
  10546. (if (string-match org-table-hline-regexp x)
  10547. 'hline
  10548. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10549. lines))
  10550. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10551. table)))
  10552. (loop for i from (1- maxcol) downto 0 do
  10553. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10554. (setq column (delq nil column))
  10555. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10556. (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))
  10557. (funcall func table nil)))
  10558. (defun orgtbl-send-table (&optional maybe)
  10559. "Send a tranformed version of this table to the receiver position.
  10560. With argument MAYBE, fail quietly if no transformation is defined for
  10561. this table."
  10562. (interactive)
  10563. (catch 'exit
  10564. (unless (org-at-table-p) (error "Not at a table"))
  10565. ;; when non-interactive, we assume align has just happened.
  10566. (when (interactive-p) (org-table-align))
  10567. (save-excursion
  10568. (goto-char (org-table-begin))
  10569. (beginning-of-line 0)
  10570. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10571. (if maybe
  10572. (throw 'exit nil)
  10573. (error "Don't know how to transform this table."))))
  10574. (let* ((name (match-string 1))
  10575. beg
  10576. (transform (intern (match-string 2)))
  10577. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10578. (skip (plist-get params :skip))
  10579. (skipcols (plist-get params :skipcols))
  10580. (txt (buffer-substring-no-properties
  10581. (org-table-begin) (org-table-end)))
  10582. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10583. (lines (org-table-clean-before-export lines))
  10584. (i0 (if org-table-clean-did-remove-column 2 1))
  10585. (table (mapcar
  10586. (lambda (x)
  10587. (if (string-match org-table-hline-regexp x)
  10588. 'hline
  10589. (org-remove-by-index
  10590. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10591. skipcols i0)))
  10592. lines))
  10593. (fun (if (= i0 2) 'cdr 'identity))
  10594. (org-table-last-alignment
  10595. (org-remove-by-index (funcall fun org-table-last-alignment)
  10596. skipcols i0))
  10597. (org-table-last-column-widths
  10598. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10599. skipcols i0)))
  10600. (unless (fboundp transform)
  10601. (error "No such transformation function %s" transform))
  10602. (setq txt (funcall transform table params))
  10603. ;; Find the insertion place
  10604. (save-excursion
  10605. (goto-char (point-min))
  10606. (unless (re-search-forward
  10607. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10608. (error "Don't know where to insert translated table"))
  10609. (goto-char (match-beginning 0))
  10610. (beginning-of-line 2)
  10611. (setq beg (point))
  10612. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10613. (error "Cannot find end of insertion region"))
  10614. (beginning-of-line 1)
  10615. (delete-region beg (point))
  10616. (goto-char beg)
  10617. (insert txt "\n"))
  10618. (message "Table converted and installed at receiver location"))))
  10619. (defun org-remove-by-index (list indices &optional i0)
  10620. "Remove the elements in LIST with indices in INDICES.
  10621. First element has index 0, or I0 if given."
  10622. (if (not indices)
  10623. list
  10624. (if (integerp indices) (setq indices (list indices)))
  10625. (setq i0 (1- (or i0 0)))
  10626. (delq :rm (mapcar (lambda (x)
  10627. (setq i0 (1+ i0))
  10628. (if (memq i0 indices) :rm x))
  10629. list))))
  10630. (defun orgtbl-toggle-comment ()
  10631. "Comment or uncomment the orgtbl at point."
  10632. (interactive)
  10633. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10634. (re2 (concat "^" orgtbl-line-start-regexp))
  10635. (commented (save-excursion (beginning-of-line 1)
  10636. (cond ((looking-at re1) t)
  10637. ((looking-at re2) nil)
  10638. (t (error "Not at an org table")))))
  10639. (re (if commented re1 re2))
  10640. beg end)
  10641. (save-excursion
  10642. (beginning-of-line 1)
  10643. (while (looking-at re) (beginning-of-line 0))
  10644. (beginning-of-line 2)
  10645. (setq beg (point))
  10646. (while (looking-at re) (beginning-of-line 2))
  10647. (setq end (point)))
  10648. (comment-region beg end (if commented '(4) nil))))
  10649. (defun orgtbl-insert-radio-table ()
  10650. "Insert a radio table template appropriate for this major mode."
  10651. (interactive)
  10652. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10653. (txt (nth 1 e))
  10654. name pos)
  10655. (unless e (error "No radio table setup defined for %s" major-mode))
  10656. (setq name (read-string "Table name: "))
  10657. (while (string-match "%n" txt)
  10658. (setq txt (replace-match name t t txt)))
  10659. (or (bolp) (insert "\n"))
  10660. (setq pos (point))
  10661. (insert txt)
  10662. (goto-char pos)))
  10663. (defun org-get-param (params header i sym &optional hsym)
  10664. "Get parameter value for symbol SYM.
  10665. If this is a header line, actually get the value for the symbol with an
  10666. additional \"h\" inserted after the colon.
  10667. If the value is a protperty list, get the element for the current column.
  10668. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10669. (let ((val (plist-get params sym)))
  10670. (and hsym header (setq val (or (plist-get params hsym) val)))
  10671. (if (consp val) (plist-get val i) val)))
  10672. (defun orgtbl-to-generic (table params)
  10673. "Convert the orgtbl-mode TABLE to some other format.
  10674. This generic routine can be used for many standard cases.
  10675. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10676. separator line, or a list of fields for that line.
  10677. PARAMS is a property list of parameters that can influence the conversion.
  10678. For the generic converter, some parameters are obligatory: You need to
  10679. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10680. :splice, you must have :tstart and :tend.
  10681. Valid parameters are
  10682. :tstart String to start the table. Ignored when :splice is t.
  10683. :tend String to end the table. Ignored when :splice is t.
  10684. :splice When set to t, return only table body lines, don't wrap
  10685. them into :tstart and :tend. Default is nil.
  10686. :hline String to be inserted on horizontal separation lines.
  10687. May be nil to ignore hlines.
  10688. :lstart String to start a new table line.
  10689. :lend String to end a table line
  10690. :sep Separator between two fields
  10691. :lfmt Format for entire line, with enough %s to capture all fields.
  10692. If this is present, :lstart, :lend, and :sep are ignored.
  10693. :fmt A format to be used to wrap the field, should contain
  10694. %s for the original field value. For example, to wrap
  10695. everything in dollars, you could use :fmt \"$%s$\".
  10696. This may also be a property list with column numbers and
  10697. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10698. :hlstart :hlend :hlsep :hlfmt :hfmt
  10699. Same as above, specific for the header lines in the table.
  10700. All lines before the first hline are treated as header.
  10701. If any of these is not present, the data line value is used.
  10702. :efmt Use this format to print numbers with exponentials.
  10703. The format should have %s twice for inserting mantissa
  10704. and exponent, for example \"%s\\\\times10^{%s}\". This
  10705. may also be a property list with column numbers and
  10706. formats. :fmt will still be applied after :efmt.
  10707. In addition to this, the parameters :skip and :skipcols are always handled
  10708. directly by `orgtbl-send-table'. See manual."
  10709. (interactive)
  10710. (let* ((p params)
  10711. (splicep (plist-get p :splice))
  10712. (hline (plist-get p :hline))
  10713. rtn line i fm efm lfmt h)
  10714. ;; Do we have a header?
  10715. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10716. (setq h t))
  10717. ;; Put header
  10718. (unless splicep
  10719. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10720. ;; Now loop over all lines
  10721. (while (setq line (pop table))
  10722. (if (eq line 'hline)
  10723. ;; A horizontal separator line
  10724. (progn (if hline (push hline rtn))
  10725. (setq h nil)) ; no longer in header
  10726. ;; A normal line. Convert the fields, push line onto the result list
  10727. (setq i 0)
  10728. (setq line
  10729. (mapcar
  10730. (lambda (f)
  10731. (setq i (1+ i)
  10732. fm (org-get-param p h i :fmt :hfmt)
  10733. efm (org-get-param p h i :efmt))
  10734. (if (and efm (string-match orgtbl-exp-regexp f))
  10735. (setq f (format
  10736. efm (match-string 1 f) (match-string 2 f))))
  10737. (if fm (setq f (format fm f)))
  10738. f)
  10739. line))
  10740. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10741. (push (apply 'format lfmt line) rtn)
  10742. (push (concat
  10743. (org-get-param p h i :lstart :hlstart)
  10744. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10745. (org-get-param p h i :lend :hlend))
  10746. rtn))))
  10747. (unless splicep
  10748. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10749. (mapconcat 'identity (nreverse rtn) "\n")))
  10750. (defun orgtbl-to-latex (table params)
  10751. "Convert the orgtbl-mode TABLE to LaTeX.
  10752. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10753. separator line, or a list of fields for that line.
  10754. PARAMS is a property list of parameters that can influence the conversion.
  10755. Supports all parameters from `orgtbl-to-generic'. Most important for
  10756. LaTeX are:
  10757. :splice When set to t, return only table body lines, don't wrap
  10758. them into a tabular environment. Default is nil.
  10759. :fmt A format to be used to wrap the field, should contain %s for the
  10760. original field value. For example, to wrap everything in dollars,
  10761. use :fmt \"$%s$\". This may also be a property list with column
  10762. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10763. :efmt Format for transforming numbers with exponentials. The format
  10764. should have %s twice for inserting mantissa and exponent, for
  10765. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10766. This may also be a property list with column numbers and formats.
  10767. The general parameters :skip and :skipcols have already been applied when
  10768. this function is called."
  10769. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10770. org-table-last-alignment ""))
  10771. (params2
  10772. (list
  10773. :tstart (concat "\\begin{tabular}{" alignment "}")
  10774. :tend "\\end{tabular}"
  10775. :lstart "" :lend " \\\\" :sep " & "
  10776. :efmt "%s\\,(%s)" :hline "\\hline")))
  10777. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10778. (defun orgtbl-to-html (table params)
  10779. "Convert the orgtbl-mode TABLE to LaTeX.
  10780. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10781. separator line, or a list of fields for that line.
  10782. PARAMS is a property list of parameters that can influence the conversion.
  10783. Currently this function recognizes the following parameters:
  10784. :splice When set to t, return only table body lines, don't wrap
  10785. them into a <table> environment. Default is nil.
  10786. The general parameters :skip and :skipcols have already been applied when
  10787. this function is called. The function does *not* use `orgtbl-to-generic',
  10788. so you cannot specify parameters for it."
  10789. (let* ((splicep (plist-get params :splice))
  10790. html)
  10791. ;; Just call the formatter we already have
  10792. ;; We need to make text lines for it, so put the fields back together.
  10793. (setq html (org-format-org-table-html
  10794. (mapcar
  10795. (lambda (x)
  10796. (if (eq x 'hline)
  10797. "|----+----|"
  10798. (concat "| " (mapconcat 'identity x " | ") " |")))
  10799. table)
  10800. splicep))
  10801. (if (string-match "\n+\\'" html)
  10802. (setq html (replace-match "" t t html)))
  10803. html))
  10804. (defun orgtbl-to-texinfo (table params)
  10805. "Convert the orgtbl-mode TABLE to TeXInfo.
  10806. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10807. separator line, or a list of fields for that line.
  10808. PARAMS is a property list of parameters that can influence the conversion.
  10809. Supports all parameters from `orgtbl-to-generic'. Most important for
  10810. TeXInfo are:
  10811. :splice nil/t When set to t, return only table body lines, don't wrap
  10812. them into a multitable environment. Default is nil.
  10813. :fmt fmt A format to be used to wrap the field, should contain
  10814. %s for the original field value. For example, to wrap
  10815. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10816. This may also be a property list with column numbers and
  10817. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10818. :cf \"f1 f2..\" The column fractions for the table. By default these
  10819. are computed automatically from the width of the columns
  10820. under org-mode.
  10821. The general parameters :skip and :skipcols have already been applied when
  10822. this function is called."
  10823. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10824. (colfrac (or (plist-get params :cf)
  10825. (mapconcat
  10826. (lambda (x) (format "%.3f" (/ (float x) total)))
  10827. org-table-last-column-widths " ")))
  10828. (params2
  10829. (list
  10830. :tstart (concat "@multitable @columnfractions " colfrac)
  10831. :tend "@end multitable"
  10832. :lstart "@item " :lend "" :sep " @tab "
  10833. :hlstart "@headitem ")))
  10834. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10835. ;;;; Link Stuff
  10836. ;;; Link abbreviations
  10837. (defun org-link-expand-abbrev (link)
  10838. "Apply replacements as defined in `org-link-abbrev-alist."
  10839. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10840. (let* ((key (match-string 1 link))
  10841. (as (or (assoc key org-link-abbrev-alist-local)
  10842. (assoc key org-link-abbrev-alist)))
  10843. (tag (and (match-end 2) (match-string 3 link)))
  10844. rpl)
  10845. (if (not as)
  10846. link
  10847. (setq rpl (cdr as))
  10848. (cond
  10849. ((symbolp rpl) (funcall rpl tag))
  10850. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10851. (t (concat rpl tag)))))
  10852. link))
  10853. ;;; Storing and inserting links
  10854. (defvar org-insert-link-history nil
  10855. "Minibuffer history for links inserted with `org-insert-link'.")
  10856. (defvar org-stored-links nil
  10857. "Contains the links stored with `org-store-link'.")
  10858. (defvar org-store-link-plist nil
  10859. "Plist with info about the most recently link created with `org-store-link'.")
  10860. (defvar org-link-protocols nil
  10861. "Link protocols added to Org-mode using `org-add-link-type'.")
  10862. (defvar org-store-link-functions nil
  10863. "List of functions that are called to create and store a link.
  10864. Each function will be called in turn until one returns a non-nil
  10865. value. Each function should check if it is responsible for creating
  10866. this link (for example by looking at the major mode).
  10867. If not, it must exit and return nil.
  10868. If yes, it should return a non-nil value after a calling
  10869. `org-store-link-props' with a list of properties and values.
  10870. Special properties are:
  10871. :type The link prefix. like \"http\". This must be given.
  10872. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10873. This is obligatory as well.
  10874. :description Optional default description for the second pair
  10875. of brackets in an Org-mode link. The user can still change
  10876. this when inserting this link into an Org-mode buffer.
  10877. In addition to these, any additional properties can be specified
  10878. and then used in remember templates.")
  10879. (defun org-add-link-type (type &optional follow publish)
  10880. "Add TYPE to the list of `org-link-types'.
  10881. Re-compute all regular expressions depending on `org-link-types'
  10882. FOLLOW and PUBLISH are two functions. Both take the link path as
  10883. an argument.
  10884. FOLLOW should do whatever is necessary to follow the link, for example
  10885. to find a file or display a mail message.
  10886. PUBLISH takes the path and retuns the string that should be used when
  10887. this document is published. FIMXE: This is actually not yet implemented."
  10888. (add-to-list 'org-link-types type t)
  10889. (org-make-link-regexps)
  10890. (add-to-list 'org-link-protocols
  10891. (list type follow publish)))
  10892. (defun org-add-agenda-custom-command (entry)
  10893. "Replace or add a command in `org-agenda-custom-commands'.
  10894. This is mostly for hacking and trying a new command - once the command
  10895. works you probably want to add it to `org-agenda-custom-commands' for good."
  10896. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10897. (if ass
  10898. (setcdr ass (cdr entry))
  10899. (push entry org-agenda-custom-commands))))
  10900. ;;;###autoload
  10901. (defun org-store-link (arg)
  10902. "\\<org-mode-map>Store an org-link to the current location.
  10903. This link is added to `org-stored-links' and can later be inserted
  10904. into an org-buffer with \\[org-insert-link].
  10905. For some link types, a prefix arg is interpreted:
  10906. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10907. For file links, arg negates `org-context-in-file-links'."
  10908. (interactive "P")
  10909. (setq org-store-link-plist nil) ; reset
  10910. (require 'org-irc)
  10911. (let (link cpltxt desc description search txt)
  10912. (cond
  10913. ((run-hook-with-args-until-success 'org-store-link-functions)
  10914. (setq link (plist-get org-store-link-plist :link)
  10915. desc (or (plist-get org-store-link-plist :description) link)))
  10916. ((eq major-mode 'bbdb-mode)
  10917. (let ((name (bbdb-record-name (bbdb-current-record)))
  10918. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10919. (setq cpltxt (concat "bbdb:" (or name company))
  10920. link (org-make-link cpltxt))
  10921. (org-store-link-props :type "bbdb" :name name :company company)))
  10922. ((eq major-mode 'Info-mode)
  10923. (setq link (org-make-link "info:"
  10924. (file-name-nondirectory Info-current-file)
  10925. ":" Info-current-node))
  10926. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10927. ":" Info-current-node))
  10928. (org-store-link-props :type "info" :file Info-current-file
  10929. :node Info-current-node))
  10930. ((eq major-mode 'calendar-mode)
  10931. (let ((cd (calendar-cursor-to-date)))
  10932. (setq link
  10933. (format-time-string
  10934. (car org-time-stamp-formats)
  10935. (apply 'encode-time
  10936. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10937. nil nil nil))))
  10938. (org-store-link-props :type "calendar" :date cd)))
  10939. ((or (eq major-mode 'vm-summary-mode)
  10940. (eq major-mode 'vm-presentation-mode))
  10941. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10942. (vm-follow-summary-cursor)
  10943. (save-excursion
  10944. (vm-select-folder-buffer)
  10945. (let* ((message (car vm-message-pointer))
  10946. (folder buffer-file-name)
  10947. (subject (vm-su-subject message))
  10948. (to (vm-get-header-contents message "To"))
  10949. (from (vm-get-header-contents message "From"))
  10950. (message-id (vm-su-message-id message)))
  10951. (org-store-link-props :type "vm" :from from :to to :subject subject
  10952. :message-id message-id)
  10953. (setq message-id (org-remove-angle-brackets message-id))
  10954. (setq folder (abbreviate-file-name folder))
  10955. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10956. folder)
  10957. (setq folder (replace-match "" t t folder)))
  10958. (setq cpltxt (org-email-link-description))
  10959. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10960. ((eq major-mode 'wl-summary-mode)
  10961. (let* ((msgnum (wl-summary-message-number))
  10962. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10963. msgnum 'message-id))
  10964. (wl-message-entity
  10965. (if (fboundp 'elmo-message-entity)
  10966. (elmo-message-entity
  10967. wl-summary-buffer-elmo-folder msgnum)
  10968. (elmo-msgdb-overview-get-entity
  10969. msgnum (wl-summary-buffer-msgdb))))
  10970. (from (wl-summary-line-from))
  10971. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10972. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10973. (wl-summary-line-subject))))
  10974. (org-store-link-props :type "wl" :from from :to to
  10975. :subject subject :message-id message-id)
  10976. (setq message-id (org-remove-angle-brackets message-id))
  10977. (setq cpltxt (org-email-link-description))
  10978. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10979. "#" message-id))))
  10980. ((or (equal major-mode 'mh-folder-mode)
  10981. (equal major-mode 'mh-show-mode))
  10982. (let ((from (org-mhe-get-header "From:"))
  10983. (to (org-mhe-get-header "To:"))
  10984. (message-id (org-mhe-get-header "Message-Id:"))
  10985. (subject (org-mhe-get-header "Subject:")))
  10986. (org-store-link-props :type "mh" :from from :to to
  10987. :subject subject :message-id message-id)
  10988. (setq cpltxt (org-email-link-description))
  10989. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10990. (org-remove-angle-brackets message-id)))))
  10991. ((or (eq major-mode 'rmail-mode)
  10992. (eq major-mode 'rmail-summary-mode))
  10993. (save-window-excursion
  10994. (save-restriction
  10995. (when (eq major-mode 'rmail-summary-mode)
  10996. (rmail-show-message rmail-current-message))
  10997. (rmail-narrow-to-non-pruned-header)
  10998. (let ((folder buffer-file-name)
  10999. (message-id (mail-fetch-field "message-id"))
  11000. (from (mail-fetch-field "from"))
  11001. (to (mail-fetch-field "to"))
  11002. (subject (mail-fetch-field "subject")))
  11003. (org-store-link-props
  11004. :type "rmail" :from from :to to
  11005. :subject subject :message-id message-id)
  11006. (setq message-id (org-remove-angle-brackets message-id))
  11007. (setq cpltxt (org-email-link-description))
  11008. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11009. (rmail-show-message rmail-current-message))))
  11010. ((eq major-mode 'gnus-group-mode)
  11011. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11012. (gnus-group-group-name)) ; version
  11013. ((fboundp 'gnus-group-name)
  11014. (gnus-group-name))
  11015. (t "???"))))
  11016. (unless group (error "Not on a group"))
  11017. (org-store-link-props :type "gnus" :group group)
  11018. (setq cpltxt (concat
  11019. (if (org-xor arg org-usenet-links-prefer-google)
  11020. "http://groups.google.com/groups?group="
  11021. "gnus:")
  11022. group)
  11023. link (org-make-link cpltxt))))
  11024. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11025. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11026. (let* ((group gnus-newsgroup-name)
  11027. (article (gnus-summary-article-number))
  11028. (header (gnus-summary-article-header article))
  11029. (from (mail-header-from header))
  11030. (message-id (mail-header-id header))
  11031. (date (mail-header-date header))
  11032. (subject (gnus-summary-subject-string)))
  11033. (org-store-link-props :type "gnus" :from from :subject subject
  11034. :message-id message-id :group group)
  11035. (setq cpltxt (org-email-link-description))
  11036. (if (org-xor arg org-usenet-links-prefer-google)
  11037. (setq link
  11038. (concat
  11039. cpltxt "\n "
  11040. (format "http://groups.google.com/groups?as_umsgid=%s"
  11041. (org-fixup-message-id-for-http message-id))))
  11042. (setq link (org-make-link "gnus:" group
  11043. "#" (number-to-string article))))))
  11044. ((eq major-mode 'w3-mode)
  11045. (setq cpltxt (url-view-url t)
  11046. link (org-make-link cpltxt))
  11047. (org-store-link-props :type "w3" :url (url-view-url t)))
  11048. ((eq major-mode 'w3m-mode)
  11049. (setq cpltxt (or w3m-current-title w3m-current-url)
  11050. link (org-make-link w3m-current-url))
  11051. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11052. ((setq search (run-hook-with-args-until-success
  11053. 'org-create-file-search-functions))
  11054. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11055. "::" search))
  11056. (setq cpltxt (or description link)))
  11057. ((eq major-mode 'image-mode)
  11058. (setq cpltxt (concat "file:"
  11059. (abbreviate-file-name buffer-file-name))
  11060. link (org-make-link cpltxt))
  11061. (org-store-link-props :type "image" :file buffer-file-name))
  11062. ((eq major-mode 'dired-mode)
  11063. ;; link to the file in the current line
  11064. (setq cpltxt (concat "file:"
  11065. (abbreviate-file-name
  11066. (expand-file-name
  11067. (dired-get-filename nil t))))
  11068. link (org-make-link cpltxt)))
  11069. ((and buffer-file-name (org-mode-p))
  11070. ;; Just link to current headline
  11071. (setq cpltxt (concat "file:"
  11072. (abbreviate-file-name buffer-file-name)))
  11073. ;; Add a context search string
  11074. (when (org-xor org-context-in-file-links arg)
  11075. ;; Check if we are on a target
  11076. (if (org-in-regexp "<<\\(.*?\\)>>")
  11077. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11078. (setq txt (cond
  11079. ((org-on-heading-p) nil)
  11080. ((org-region-active-p)
  11081. (buffer-substring (region-beginning) (region-end)))
  11082. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11083. (when (or (null txt) (string-match "\\S-" txt))
  11084. (setq cpltxt
  11085. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11086. desc "NONE"))))
  11087. (if (string-match "::\\'" cpltxt)
  11088. (setq cpltxt (substring cpltxt 0 -2)))
  11089. (setq link (org-make-link cpltxt)))
  11090. ((buffer-file-name (buffer-base-buffer))
  11091. ;; Just link to this file here.
  11092. (setq cpltxt (concat "file:"
  11093. (abbreviate-file-name
  11094. (buffer-file-name (buffer-base-buffer)))))
  11095. ;; Add a context string
  11096. (when (org-xor org-context-in-file-links arg)
  11097. (setq txt (if (org-region-active-p)
  11098. (buffer-substring (region-beginning) (region-end))
  11099. (buffer-substring (point-at-bol) (point-at-eol))))
  11100. ;; Only use search option if there is some text.
  11101. (when (string-match "\\S-" txt)
  11102. (setq cpltxt
  11103. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11104. desc "NONE")))
  11105. (setq link (org-make-link cpltxt)))
  11106. ((interactive-p)
  11107. (error "Cannot link to a buffer which is not visiting a file"))
  11108. (t (setq link nil)))
  11109. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11110. (setq link (or link cpltxt)
  11111. desc (or desc cpltxt))
  11112. (if (equal desc "NONE") (setq desc nil))
  11113. (if (and (interactive-p) link)
  11114. (progn
  11115. (setq org-stored-links
  11116. (cons (list link desc) org-stored-links))
  11117. (message "Stored: %s" (or desc link)))
  11118. (and link (org-make-link-string link desc)))))
  11119. (defun org-store-link-props (&rest plist)
  11120. "Store link properties, extract names and addresses."
  11121. (let (x adr)
  11122. (when (setq x (plist-get plist :from))
  11123. (setq adr (mail-extract-address-components x))
  11124. (plist-put plist :fromname (car adr))
  11125. (plist-put plist :fromaddress (nth 1 adr)))
  11126. (when (setq x (plist-get plist :to))
  11127. (setq adr (mail-extract-address-components x))
  11128. (plist-put plist :toname (car adr))
  11129. (plist-put plist :toaddress (nth 1 adr))))
  11130. (let ((from (plist-get plist :from))
  11131. (to (plist-get plist :to)))
  11132. (when (and from to org-from-is-user-regexp)
  11133. (plist-put plist :fromto
  11134. (if (string-match org-from-is-user-regexp from)
  11135. (concat "to %t")
  11136. (concat "from %f")))))
  11137. (setq org-store-link-plist plist))
  11138. (defun org-email-link-description (&optional fmt)
  11139. "Return the description part of an email link.
  11140. This takes information from `org-store-link-plist' and formats it
  11141. according to FMT (default from `org-email-link-description-format')."
  11142. (setq fmt (or fmt org-email-link-description-format))
  11143. (let* ((p org-store-link-plist)
  11144. (to (plist-get p :toaddress))
  11145. (from (plist-get p :fromaddress))
  11146. (table
  11147. (list
  11148. (cons "%c" (plist-get p :fromto))
  11149. (cons "%F" (plist-get p :from))
  11150. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11151. (cons "%T" (plist-get p :to))
  11152. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11153. (cons "%s" (plist-get p :subject))
  11154. (cons "%m" (plist-get p :message-id)))))
  11155. (when (string-match "%c" fmt)
  11156. ;; Check if the user wrote this message
  11157. (if (and org-from-is-user-regexp from to
  11158. (save-match-data (string-match org-from-is-user-regexp from)))
  11159. (setq fmt (replace-match "to %t" t t fmt))
  11160. (setq fmt (replace-match "from %f" t t fmt))))
  11161. (org-replace-escapes fmt table)))
  11162. (defun org-make-org-heading-search-string (&optional string heading)
  11163. "Make search string for STRING or current headline."
  11164. (interactive)
  11165. (let ((s (or string (org-get-heading))))
  11166. (unless (and string (not heading))
  11167. ;; We are using a headline, clean up garbage in there.
  11168. (if (string-match org-todo-regexp s)
  11169. (setq s (replace-match "" t t s)))
  11170. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11171. (setq s (replace-match "" t t s)))
  11172. (setq s (org-trim s))
  11173. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11174. org-comment-string "\\)") s)
  11175. (setq s (replace-match "" t t s)))
  11176. (while (string-match org-ts-regexp s)
  11177. (setq s (replace-match "" t t s))))
  11178. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11179. (setq s (replace-match " " t t s)))
  11180. (or string (setq s (concat "*" s))) ; Add * for headlines
  11181. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11182. (defun org-make-link (&rest strings)
  11183. "Concatenate STRINGS."
  11184. (apply 'concat strings))
  11185. (defun org-make-link-string (link &optional description)
  11186. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11187. (unless (string-match "\\S-" link)
  11188. (error "Empty link"))
  11189. (when (stringp description)
  11190. ;; Remove brackets from the description, they are fatal.
  11191. (while (string-match "\\[" description)
  11192. (setq description (replace-match "{" t t description)))
  11193. (while (string-match "\\]" description)
  11194. (setq description (replace-match "}" t t description))))
  11195. (when (equal (org-link-escape link) description)
  11196. ;; No description needed, it is identical
  11197. (setq description nil))
  11198. (when (and (not description)
  11199. (not (equal link (org-link-escape link))))
  11200. (setq description link))
  11201. (concat "[[" (org-link-escape link) "]"
  11202. (if description (concat "[" description "]") "")
  11203. "]"))
  11204. (defconst org-link-escape-chars
  11205. '((?\ . "%20")
  11206. (?\[ . "%5B")
  11207. (?\] . "%5D")
  11208. (?\340 . "%E0") ; `a
  11209. (?\342 . "%E2") ; ^a
  11210. (?\347 . "%E7") ; ,c
  11211. (?\350 . "%E8") ; `e
  11212. (?\351 . "%E9") ; 'e
  11213. (?\352 . "%EA") ; ^e
  11214. (?\356 . "%EE") ; ^i
  11215. (?\364 . "%F4") ; ^o
  11216. (?\371 . "%F9") ; `u
  11217. (?\373 . "%FB") ; ^u
  11218. (?\; . "%3B")
  11219. (?? . "%3F")
  11220. (?= . "%3D")
  11221. (?+ . "%2B")
  11222. )
  11223. "Association list of escapes for some characters problematic in links.
  11224. This is the list that is used for internal purposes.")
  11225. (defconst org-link-escape-chars-browser
  11226. '((?\ . "%20")) ; 32 for the SPC char
  11227. "Association list of escapes for some characters problematic in links.
  11228. This is the list that is used before handing over to the browser.")
  11229. (defun org-link-escape (text &optional table)
  11230. "Escape charaters in TEXT that are problematic for links."
  11231. (setq table (or table org-link-escape-chars))
  11232. (when text
  11233. (let ((re (mapconcat (lambda (x) (regexp-quote
  11234. (char-to-string (car x))))
  11235. table "\\|")))
  11236. (while (string-match re text)
  11237. (setq text
  11238. (replace-match
  11239. (cdr (assoc (string-to-char (match-string 0 text))
  11240. table))
  11241. t t text)))
  11242. text)))
  11243. (defun org-link-unescape (text &optional table)
  11244. "Reverse the action of `org-link-escape'."
  11245. (setq table (or table org-link-escape-chars))
  11246. (when text
  11247. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11248. table "\\|")))
  11249. (while (string-match re text)
  11250. (setq text
  11251. (replace-match
  11252. (char-to-string (car (rassoc (match-string 0 text) table)))
  11253. t t text)))
  11254. text)))
  11255. (defun org-xor (a b)
  11256. "Exclusive or."
  11257. (if a (not b) b))
  11258. (defun org-get-header (header)
  11259. "Find a header field in the current buffer."
  11260. (save-excursion
  11261. (goto-char (point-min))
  11262. (let ((case-fold-search t) s)
  11263. (cond
  11264. ((eq header 'from)
  11265. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11266. (setq s (match-string 1)))
  11267. (while (string-match "\"" s)
  11268. (setq s (replace-match "" t t s)))
  11269. (if (string-match "[<(].*" s)
  11270. (setq s (replace-match "" t t s))))
  11271. ((eq header 'message-id)
  11272. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11273. (setq s (match-string 1))))
  11274. ((eq header 'subject)
  11275. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11276. (setq s (match-string 1)))))
  11277. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11278. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11279. s)))
  11280. (defun org-fixup-message-id-for-http (s)
  11281. "Replace special characters in a message id, so it can be used in an http query."
  11282. (while (string-match "<" s)
  11283. (setq s (replace-match "%3C" t t s)))
  11284. (while (string-match ">" s)
  11285. (setq s (replace-match "%3E" t t s)))
  11286. (while (string-match "@" s)
  11287. (setq s (replace-match "%40" t t s)))
  11288. s)
  11289. ;;;###autoload
  11290. (defun org-insert-link-global ()
  11291. "Insert a link like Org-mode does.
  11292. This command can be called in any mode to insert a link in Org-mode syntax."
  11293. (interactive)
  11294. (org-run-like-in-org-mode 'org-insert-link))
  11295. (defun org-insert-link (&optional complete-file)
  11296. "Insert a link. At the prompt, enter the link.
  11297. Completion can be used to select a link previously stored with
  11298. `org-store-link'. When the empty string is entered (i.e. if you just
  11299. press RET at the prompt), the link defaults to the most recently
  11300. stored link. As SPC triggers completion in the minibuffer, you need to
  11301. use M-SPC or C-q SPC to force the insertion of a space character.
  11302. You will also be prompted for a description, and if one is given, it will
  11303. be displayed in the buffer instead of the link.
  11304. If there is already a link at point, this command will allow you to edit link
  11305. and description parts.
  11306. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11307. selected using completion. The path to the file will be relative to
  11308. the current directory if the file is in the current directory or a
  11309. subdirectory. Otherwise, the link will be the absolute path as
  11310. completed in the minibuffer (i.e. normally ~/path/to/file).
  11311. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11312. is in the current directory or below.
  11313. With three \\[universal-argument] prefixes, negate the meaning of
  11314. `org-keep-stored-link-after-insertion'."
  11315. (interactive "P")
  11316. (let* ((wcf (current-window-configuration))
  11317. (region (if (org-region-active-p)
  11318. (buffer-substring (region-beginning) (region-end))))
  11319. (remove (and region (list (region-beginning) (region-end))))
  11320. (desc region)
  11321. tmphist ; byte-compile incorrectly complains about this
  11322. link entry file)
  11323. (cond
  11324. ((org-in-regexp org-bracket-link-regexp 1)
  11325. ;; We do have a link at point, and we are going to edit it.
  11326. (setq remove (list (match-beginning 0) (match-end 0)))
  11327. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11328. (setq link (read-string "Link: "
  11329. (org-link-unescape
  11330. (org-match-string-no-properties 1)))))
  11331. ((or (org-in-regexp org-angle-link-re)
  11332. (org-in-regexp org-plain-link-re))
  11333. ;; Convert to bracket link
  11334. (setq remove (list (match-beginning 0) (match-end 0))
  11335. link (read-string "Link: "
  11336. (org-remove-angle-brackets (match-string 0)))))
  11337. ((equal complete-file '(4))
  11338. ;; Completing read for file names.
  11339. (setq file (read-file-name "File: "))
  11340. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11341. (pwd1 (file-name-as-directory (abbreviate-file-name
  11342. (expand-file-name ".")))))
  11343. (cond
  11344. ((equal complete-file '(16))
  11345. (setq link (org-make-link
  11346. "file:"
  11347. (abbreviate-file-name (expand-file-name file)))))
  11348. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11349. (setq link (org-make-link "file:" (match-string 1 file))))
  11350. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11351. (expand-file-name file))
  11352. (setq link (org-make-link
  11353. "file:" (match-string 1 (expand-file-name file)))))
  11354. (t (setq link (org-make-link "file:" file))))))
  11355. (t
  11356. ;; Read link, with completion for stored links.
  11357. (with-output-to-temp-buffer "*Org Links*"
  11358. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11359. (when org-stored-links
  11360. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11361. (princ (mapconcat
  11362. (lambda (x)
  11363. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11364. (reverse org-stored-links) "\n"))))
  11365. (let ((cw (selected-window)))
  11366. (select-window (get-buffer-window "*Org Links*"))
  11367. (shrink-window-if-larger-than-buffer)
  11368. (setq truncate-lines t)
  11369. (select-window cw))
  11370. ;; Fake a link history, containing the stored links.
  11371. (setq tmphist (append (mapcar 'car org-stored-links)
  11372. org-insert-link-history))
  11373. (unwind-protect
  11374. (setq link (org-completing-read
  11375. "Link: "
  11376. (append
  11377. (mapcar (lambda (x) (list (concat (car x) ":")))
  11378. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11379. (mapcar (lambda (x) (list (concat x ":")))
  11380. org-link-types))
  11381. nil nil nil
  11382. 'tmphist
  11383. (or (car (car org-stored-links)))))
  11384. (set-window-configuration wcf)
  11385. (kill-buffer "*Org Links*"))
  11386. (setq entry (assoc link org-stored-links))
  11387. (or entry (push link org-insert-link-history))
  11388. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11389. (not org-keep-stored-link-after-insertion))
  11390. (setq org-stored-links (delq (assoc link org-stored-links)
  11391. org-stored-links)))
  11392. (setq desc (or desc (nth 1 entry)))))
  11393. (if (string-match org-plain-link-re link)
  11394. ;; URL-like link, normalize the use of angular brackets.
  11395. (setq link (org-make-link (org-remove-angle-brackets link))))
  11396. ;; Check if we are linking to the current file with a search option
  11397. ;; If yes, simplify the link by using only the search option.
  11398. (when (and buffer-file-name
  11399. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11400. (let* ((path (match-string 1 link))
  11401. (case-fold-search nil)
  11402. (search (match-string 2 link)))
  11403. (save-match-data
  11404. (if (equal (file-truename buffer-file-name) (file-truename path))
  11405. ;; We are linking to this same file, with a search option
  11406. (setq link search)))))
  11407. ;; Check if we can/should use a relative path. If yes, simplify the link
  11408. (when (string-match "\\<file:\\(.*\\)" link)
  11409. (let* ((path (match-string 1 link))
  11410. (origpath path)
  11411. (case-fold-search nil))
  11412. (cond
  11413. ((eq org-link-file-path-type 'absolute)
  11414. (setq path (abbreviate-file-name (expand-file-name path))))
  11415. ((eq org-link-file-path-type 'noabbrev)
  11416. (setq path (expand-file-name path)))
  11417. ((eq org-link-file-path-type 'relative)
  11418. (setq path (file-relative-name path)))
  11419. (t
  11420. (save-match-data
  11421. (if (string-match (concat "^" (regexp-quote
  11422. (file-name-as-directory
  11423. (expand-file-name "."))))
  11424. (expand-file-name path))
  11425. ;; We are linking a file with relative path name.
  11426. (setq path (substring (expand-file-name path)
  11427. (match-end 0)))))))
  11428. (setq link (concat "file:" path))
  11429. (if (equal desc origpath)
  11430. (setq desc path))))
  11431. (setq desc (read-string "Description: " desc))
  11432. (unless (string-match "\\S-" desc) (setq desc nil))
  11433. (if remove (apply 'delete-region remove))
  11434. (insert (org-make-link-string link desc))))
  11435. (defun org-completing-read (&rest args)
  11436. (let ((minibuffer-local-completion-map
  11437. (copy-keymap minibuffer-local-completion-map)))
  11438. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11439. (apply 'completing-read args)))
  11440. ;;; Opening/following a link
  11441. (defvar org-link-search-failed nil)
  11442. (defun org-next-link ()
  11443. "Move forward to the next link.
  11444. If the link is in hidden text, expose it."
  11445. (interactive)
  11446. (when (and org-link-search-failed (eq this-command last-command))
  11447. (goto-char (point-min))
  11448. (message "Link search wrapped back to beginning of buffer"))
  11449. (setq org-link-search-failed nil)
  11450. (let* ((pos (point))
  11451. (ct (org-context))
  11452. (a (assoc :link ct)))
  11453. (if a (goto-char (nth 2 a)))
  11454. (if (re-search-forward org-any-link-re nil t)
  11455. (progn
  11456. (goto-char (match-beginning 0))
  11457. (if (org-invisible-p) (org-show-context)))
  11458. (goto-char pos)
  11459. (setq org-link-search-failed t)
  11460. (error "No further link found"))))
  11461. (defun org-previous-link ()
  11462. "Move backward to the previous link.
  11463. If the link is in hidden text, expose it."
  11464. (interactive)
  11465. (when (and org-link-search-failed (eq this-command last-command))
  11466. (goto-char (point-max))
  11467. (message "Link search wrapped back to end of buffer"))
  11468. (setq org-link-search-failed nil)
  11469. (let* ((pos (point))
  11470. (ct (org-context))
  11471. (a (assoc :link ct)))
  11472. (if a (goto-char (nth 1 a)))
  11473. (if (re-search-backward org-any-link-re nil t)
  11474. (progn
  11475. (goto-char (match-beginning 0))
  11476. (if (org-invisible-p) (org-show-context)))
  11477. (goto-char pos)
  11478. (setq org-link-search-failed t)
  11479. (error "No further link found"))))
  11480. (defun org-find-file-at-mouse (ev)
  11481. "Open file link or URL at mouse."
  11482. (interactive "e")
  11483. (mouse-set-point ev)
  11484. (org-open-at-point 'in-emacs))
  11485. (defun org-open-at-mouse (ev)
  11486. "Open file link or URL at mouse."
  11487. (interactive "e")
  11488. (mouse-set-point ev)
  11489. (org-open-at-point))
  11490. (defvar org-window-config-before-follow-link nil
  11491. "The window configuration before following a link.
  11492. This is saved in case the need arises to restore it.")
  11493. (defvar org-open-link-marker (make-marker)
  11494. "Marker pointing to the location where `org-open-at-point; was called.")
  11495. ;;;###autoload
  11496. (defun org-open-at-point-global ()
  11497. "Follow a link like Org-mode does.
  11498. This command can be called in any mode to follow a link that has
  11499. Org-mode syntax."
  11500. (interactive)
  11501. (org-run-like-in-org-mode 'org-open-at-point))
  11502. (defun org-open-at-point (&optional in-emacs)
  11503. "Open link at or after point.
  11504. If there is no link at point, this function will search forward up to
  11505. the end of the current subtree.
  11506. Normally, files will be opened by an appropriate application. If the
  11507. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11508. (interactive "P")
  11509. (catch 'abort
  11510. (move-marker org-open-link-marker (point))
  11511. (setq org-window-config-before-follow-link (current-window-configuration))
  11512. (org-remove-occur-highlights nil nil t)
  11513. (if (org-at-timestamp-p t)
  11514. (org-follow-timestamp-link)
  11515. (let (type path link line search (pos (point)))
  11516. (catch 'match
  11517. (save-excursion
  11518. (skip-chars-forward "^]\n\r")
  11519. (when (org-in-regexp org-bracket-link-regexp)
  11520. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11521. (while (string-match " *\n *" link)
  11522. (setq link (replace-match " " t t link)))
  11523. (setq link (org-link-expand-abbrev link))
  11524. (if (string-match org-link-re-with-space2 link)
  11525. (setq type (match-string 1 link) path (match-string 2 link))
  11526. (setq type "thisfile" path link))
  11527. (throw 'match t)))
  11528. (when (get-text-property (point) 'org-linked-text)
  11529. (setq type "thisfile"
  11530. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11531. (1+ (point)) (point))
  11532. path (buffer-substring
  11533. (previous-single-property-change pos 'org-linked-text)
  11534. (next-single-property-change pos 'org-linked-text)))
  11535. (throw 'match t))
  11536. (save-excursion
  11537. (when (or (org-in-regexp org-angle-link-re)
  11538. (org-in-regexp org-plain-link-re))
  11539. (setq type (match-string 1) path (match-string 2))
  11540. (throw 'match t)))
  11541. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11542. (setq type "tree-match"
  11543. path (match-string 1))
  11544. (throw 'match t))
  11545. (save-excursion
  11546. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11547. (setq type "tags"
  11548. path (match-string 1))
  11549. (while (string-match ":" path)
  11550. (setq path (replace-match "+" t t path)))
  11551. (throw 'match t))))
  11552. (unless path
  11553. (error "No link found"))
  11554. ;; Remove any trailing spaces in path
  11555. (if (string-match " +\\'" path)
  11556. (setq path (replace-match "" t t path)))
  11557. (cond
  11558. ((assoc type org-link-protocols)
  11559. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11560. ((equal type "mailto")
  11561. (let ((cmd (car org-link-mailto-program))
  11562. (args (cdr org-link-mailto-program)) args1
  11563. (address path) (subject "") a)
  11564. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11565. (setq address (match-string 1 path)
  11566. subject (org-link-escape (match-string 2 path))))
  11567. (while args
  11568. (cond
  11569. ((not (stringp (car args))) (push (pop args) args1))
  11570. (t (setq a (pop args))
  11571. (if (string-match "%a" a)
  11572. (setq a (replace-match address t t a)))
  11573. (if (string-match "%s" a)
  11574. (setq a (replace-match subject t t a)))
  11575. (push a args1))))
  11576. (apply cmd (nreverse args1))))
  11577. ((member type '("http" "https" "ftp" "news"))
  11578. (browse-url (concat type ":" (org-link-escape
  11579. path org-link-escape-chars-browser))))
  11580. ((member type '("message"))
  11581. (browse-url (concat type ":" path)))
  11582. ((string= type "tags")
  11583. (org-tags-view in-emacs path))
  11584. ((string= type "thisfile")
  11585. (if in-emacs
  11586. (switch-to-buffer-other-window
  11587. (org-get-buffer-for-internal-link (current-buffer)))
  11588. (org-mark-ring-push))
  11589. (let ((cmd `(org-link-search
  11590. ,path
  11591. ,(cond ((equal in-emacs '(4)) 'occur)
  11592. ((equal in-emacs '(16)) 'org-occur)
  11593. (t nil))
  11594. ,pos)))
  11595. (condition-case nil (eval cmd)
  11596. (error (progn (widen) (eval cmd))))))
  11597. ((string= type "tree-match")
  11598. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11599. ((string= type "file")
  11600. (if (string-match "::\\([0-9]+\\)\\'" path)
  11601. (setq line (string-to-number (match-string 1 path))
  11602. path (substring path 0 (match-beginning 0)))
  11603. (if (string-match "::\\(.+\\)\\'" path)
  11604. (setq search (match-string 1 path)
  11605. path (substring path 0 (match-beginning 0)))))
  11606. (if (string-match "[*?{]" (file-name-nondirectory path))
  11607. (dired path)
  11608. (org-open-file path in-emacs line search)))
  11609. ((string= type "news")
  11610. (org-follow-gnus-link path))
  11611. ((string= type "bbdb")
  11612. (org-follow-bbdb-link path))
  11613. ((string= type "info")
  11614. (org-follow-info-link path))
  11615. ((string= type "gnus")
  11616. (let (group article)
  11617. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11618. (error "Error in Gnus link"))
  11619. (setq group (match-string 1 path)
  11620. article (match-string 3 path))
  11621. (org-follow-gnus-link group article)))
  11622. ((string= type "vm")
  11623. (let (folder article)
  11624. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11625. (error "Error in VM link"))
  11626. (setq folder (match-string 1 path)
  11627. article (match-string 3 path))
  11628. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11629. (org-follow-vm-link folder article in-emacs)))
  11630. ((string= type "wl")
  11631. (let (folder article)
  11632. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11633. (error "Error in Wanderlust link"))
  11634. (setq folder (match-string 1 path)
  11635. article (match-string 3 path))
  11636. (org-follow-wl-link folder article)))
  11637. ((string= type "mhe")
  11638. (let (folder article)
  11639. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11640. (error "Error in MHE link"))
  11641. (setq folder (match-string 1 path)
  11642. article (match-string 3 path))
  11643. (org-follow-mhe-link folder article)))
  11644. ((string= type "rmail")
  11645. (let (folder article)
  11646. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11647. (error "Error in RMAIL link"))
  11648. (setq folder (match-string 1 path)
  11649. article (match-string 3 path))
  11650. (org-follow-rmail-link folder article)))
  11651. ((string= type "shell")
  11652. (let ((cmd path))
  11653. (if (or (not org-confirm-shell-link-function)
  11654. (funcall org-confirm-shell-link-function
  11655. (format "Execute \"%s\" in shell? "
  11656. (org-add-props cmd nil
  11657. 'face 'org-warning))))
  11658. (progn
  11659. (message "Executing %s" cmd)
  11660. (shell-command cmd))
  11661. (error "Abort"))))
  11662. ((string= type "elisp")
  11663. (let ((cmd path))
  11664. (if (or (not org-confirm-elisp-link-function)
  11665. (funcall org-confirm-elisp-link-function
  11666. (format "Execute \"%s\" as elisp? "
  11667. (org-add-props cmd nil
  11668. 'face 'org-warning))))
  11669. (message "%s => %s" cmd (eval (read cmd)))
  11670. (error "Abort"))))
  11671. (t
  11672. (browse-url-at-point)))))
  11673. (move-marker org-open-link-marker nil)))
  11674. ;;; File search
  11675. (defvar org-create-file-search-functions nil
  11676. "List of functions to construct the right search string for a file link.
  11677. These functions are called in turn with point at the location to
  11678. which the link should point.
  11679. A function in the hook should first test if it would like to
  11680. handle this file type, for example by checking the major-mode or
  11681. the file extension. If it decides not to handle this file, it
  11682. should just return nil to give other functions a chance. If it
  11683. does handle the file, it must return the search string to be used
  11684. when following the link. The search string will be part of the
  11685. file link, given after a double colon, and `org-open-at-point'
  11686. will automatically search for it. If special measures must be
  11687. taken to make the search successful, another function should be
  11688. added to the companion hook `org-execute-file-search-functions',
  11689. which see.
  11690. A function in this hook may also use `setq' to set the variable
  11691. `description' to provide a suggestion for the descriptive text to
  11692. be used for this link when it gets inserted into an Org-mode
  11693. buffer with \\[org-insert-link].")
  11694. (defvar org-execute-file-search-functions nil
  11695. "List of functions to execute a file search triggered by a link.
  11696. Functions added to this hook must accept a single argument, the
  11697. search string that was part of the file link, the part after the
  11698. double colon. The function must first check if it would like to
  11699. handle this search, for example by checking the major-mode or the
  11700. file extension. If it decides not to handle this search, it
  11701. should just return nil to give other functions a chance. If it
  11702. does handle the search, it must return a non-nil value to keep
  11703. other functions from trying.
  11704. Each function can access the current prefix argument through the
  11705. variable `current-prefix-argument'. Note that a single prefix is
  11706. used to force opening a link in Emacs, so it may be good to only
  11707. use a numeric or double prefix to guide the search function.
  11708. In case this is needed, a function in this hook can also restore
  11709. the window configuration before `org-open-at-point' was called using:
  11710. (set-window-configuration org-window-config-before-follow-link)")
  11711. (defun org-link-search (s &optional type avoid-pos)
  11712. "Search for a link search option.
  11713. If S is surrounded by forward slashes, it is interpreted as a
  11714. regular expression. In org-mode files, this will create an `org-occur'
  11715. sparse tree. In ordinary files, `occur' will be used to list matches.
  11716. If the current buffer is in `dired-mode', grep will be used to search
  11717. in all files. If AVOID-POS is given, ignore matches near that position."
  11718. (let ((case-fold-search t)
  11719. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11720. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11721. (append '(("") (" ") ("\t") ("\n"))
  11722. org-emphasis-alist)
  11723. "\\|") "\\)"))
  11724. (pos (point))
  11725. (pre "") (post "")
  11726. words re0 re1 re2 re3 re4 re5 re2a reall)
  11727. (cond
  11728. ;; First check if there are any special
  11729. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11730. ;; Now try the builtin stuff
  11731. ((save-excursion
  11732. (goto-char (point-min))
  11733. (and
  11734. (re-search-forward
  11735. (concat "<<" (regexp-quote s0) ">>") nil t)
  11736. (setq pos (match-beginning 0))))
  11737. ;; There is an exact target for this
  11738. (goto-char pos))
  11739. ((string-match "^/\\(.*\\)/$" s)
  11740. ;; A regular expression
  11741. (cond
  11742. ((org-mode-p)
  11743. (org-occur (match-string 1 s)))
  11744. ;;((eq major-mode 'dired-mode)
  11745. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11746. (t (org-do-occur (match-string 1 s)))))
  11747. (t
  11748. ;; A normal search strings
  11749. (when (equal (string-to-char s) ?*)
  11750. ;; Anchor on headlines, post may include tags.
  11751. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11752. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11753. s (substring s 1)))
  11754. (remove-text-properties
  11755. 0 (length s)
  11756. '(face nil mouse-face nil keymap nil fontified nil) s)
  11757. ;; Make a series of regular expressions to find a match
  11758. (setq words (org-split-string s "[ \n\r\t]+")
  11759. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11760. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11761. "\\)" markers)
  11762. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11763. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11764. re1 (concat pre re2 post)
  11765. re3 (concat pre re4 post)
  11766. re5 (concat pre ".*" re4)
  11767. re2 (concat pre re2)
  11768. re2a (concat pre re2a)
  11769. re4 (concat pre re4)
  11770. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11771. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11772. re5 "\\)"
  11773. ))
  11774. (cond
  11775. ((eq type 'org-occur) (org-occur reall))
  11776. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11777. (t (goto-char (point-min))
  11778. (if (or (org-search-not-self 1 re0 nil t)
  11779. (org-search-not-self 1 re1 nil t)
  11780. (org-search-not-self 1 re2 nil t)
  11781. (org-search-not-self 1 re2a nil t)
  11782. (org-search-not-self 1 re3 nil t)
  11783. (org-search-not-self 1 re4 nil t)
  11784. (org-search-not-self 1 re5 nil t)
  11785. )
  11786. (goto-char (match-beginning 1))
  11787. (goto-char pos)
  11788. (error "No match")))))
  11789. (t
  11790. ;; Normal string-search
  11791. (goto-char (point-min))
  11792. (if (search-forward s nil t)
  11793. (goto-char (match-beginning 0))
  11794. (error "No match"))))
  11795. (and (org-mode-p) (org-show-context 'link-search))))
  11796. (defun org-search-not-self (group &rest args)
  11797. "Execute `re-search-forward', but only accept matches that do not
  11798. enclose the position of `org-open-link-marker'."
  11799. (let ((m org-open-link-marker))
  11800. (catch 'exit
  11801. (while (apply 're-search-forward args)
  11802. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11803. (goto-char (match-end group))
  11804. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11805. (> (match-beginning 0) (marker-position m))
  11806. (< (match-end 0) (marker-position m)))
  11807. (save-match-data
  11808. (or (not (org-in-regexp
  11809. org-bracket-link-analytic-regexp 1))
  11810. (not (match-end 4)) ; no description
  11811. (and (<= (match-beginning 4) (point))
  11812. (>= (match-end 4) (point))))))
  11813. (throw 'exit (point))))))))
  11814. (defun org-get-buffer-for-internal-link (buffer)
  11815. "Return a buffer to be used for displaying the link target of internal links."
  11816. (cond
  11817. ((not org-display-internal-link-with-indirect-buffer)
  11818. buffer)
  11819. ((string-match "(Clone)$" (buffer-name buffer))
  11820. (message "Buffer is already a clone, not making another one")
  11821. ;; we also do not modify visibility in this case
  11822. buffer)
  11823. (t ; make a new indirect buffer for displaying the link
  11824. (let* ((bn (buffer-name buffer))
  11825. (ibn (concat bn "(Clone)"))
  11826. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11827. (with-current-buffer ib (org-overview))
  11828. ib))))
  11829. (defun org-do-occur (regexp &optional cleanup)
  11830. "Call the Emacs command `occur'.
  11831. If CLEANUP is non-nil, remove the printout of the regular expression
  11832. in the *Occur* buffer. This is useful if the regex is long and not useful
  11833. to read."
  11834. (occur regexp)
  11835. (when cleanup
  11836. (let ((cwin (selected-window)) win beg end)
  11837. (when (setq win (get-buffer-window "*Occur*"))
  11838. (select-window win))
  11839. (goto-char (point-min))
  11840. (when (re-search-forward "match[a-z]+" nil t)
  11841. (setq beg (match-end 0))
  11842. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11843. (setq end (1- (match-beginning 0)))))
  11844. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11845. (goto-char (point-min))
  11846. (select-window cwin))))
  11847. ;;; The mark ring for links jumps
  11848. (defvar org-mark-ring nil
  11849. "Mark ring for positions before jumps in Org-mode.")
  11850. (defvar org-mark-ring-last-goto nil
  11851. "Last position in the mark ring used to go back.")
  11852. ;; Fill and close the ring
  11853. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11854. (loop for i from 1 to org-mark-ring-length do
  11855. (push (make-marker) org-mark-ring))
  11856. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11857. org-mark-ring)
  11858. (defun org-mark-ring-push (&optional pos buffer)
  11859. "Put the current position or POS into the mark ring and rotate it."
  11860. (interactive)
  11861. (setq pos (or pos (point)))
  11862. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11863. (move-marker (car org-mark-ring)
  11864. (or pos (point))
  11865. (or buffer (current-buffer)))
  11866. (message "%s"
  11867. (substitute-command-keys
  11868. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11869. (defun org-mark-ring-goto (&optional n)
  11870. "Jump to the previous position in the mark ring.
  11871. With prefix arg N, jump back that many stored positions. When
  11872. called several times in succession, walk through the entire ring.
  11873. Org-mode commands jumping to a different position in the current file,
  11874. or to another Org-mode file, automatically push the old position
  11875. onto the ring."
  11876. (interactive "p")
  11877. (let (p m)
  11878. (if (eq last-command this-command)
  11879. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11880. (setq p org-mark-ring))
  11881. (setq org-mark-ring-last-goto p)
  11882. (setq m (car p))
  11883. (switch-to-buffer (marker-buffer m))
  11884. (goto-char m)
  11885. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11886. (defun org-remove-angle-brackets (s)
  11887. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11888. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11889. s)
  11890. (defun org-add-angle-brackets (s)
  11891. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11892. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11893. s)
  11894. ;;; Following specific links
  11895. (defun org-follow-timestamp-link ()
  11896. (cond
  11897. ((org-at-date-range-p t)
  11898. (let ((org-agenda-start-on-weekday)
  11899. (t1 (match-string 1))
  11900. (t2 (match-string 2)))
  11901. (setq t1 (time-to-days (org-time-string-to-time t1))
  11902. t2 (time-to-days (org-time-string-to-time t2)))
  11903. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11904. ((org-at-timestamp-p t)
  11905. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11906. (substring (match-string 1) 0 10)))
  11907. 1))
  11908. (t (error "This should not happen"))))
  11909. (defun org-follow-bbdb-link (name)
  11910. "Follow a BBDB link to NAME."
  11911. (require 'bbdb)
  11912. (let ((inhibit-redisplay (not debug-on-error))
  11913. (bbdb-electric-p nil))
  11914. (catch 'exit
  11915. ;; Exact match on name
  11916. (bbdb-name (concat "\\`" name "\\'") nil)
  11917. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11918. ;; Exact match on name
  11919. (bbdb-company (concat "\\`" name "\\'") nil)
  11920. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11921. ;; Partial match on name
  11922. (bbdb-name name nil)
  11923. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11924. ;; Partial match on company
  11925. (bbdb-company name nil)
  11926. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11927. ;; General match including network address and notes
  11928. (bbdb name nil)
  11929. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11930. (delete-window (get-buffer-window "*BBDB*"))
  11931. (error "No matching BBDB record")))))
  11932. (defun org-follow-info-link (name)
  11933. "Follow an info file & node link to NAME."
  11934. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11935. (string-match "\\(.*\\)" name))
  11936. (progn
  11937. (require 'info)
  11938. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11939. (Info-find-node (match-string 1 name) (match-string 2 name))
  11940. (Info-find-node (match-string 1 name) "Top")))
  11941. (message "Could not open: %s" name)))
  11942. (defun org-follow-gnus-link (&optional group article)
  11943. "Follow a Gnus link to GROUP and ARTICLE."
  11944. (require 'gnus)
  11945. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11946. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11947. (cond ((and group article)
  11948. (gnus-group-read-group 1 nil group)
  11949. (gnus-summary-goto-article (string-to-number article) nil t))
  11950. (group (gnus-group-jump-to-group group))))
  11951. (defun org-follow-vm-link (&optional folder article readonly)
  11952. "Follow a VM link to FOLDER and ARTICLE."
  11953. (require 'vm)
  11954. (setq article (org-add-angle-brackets article))
  11955. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11956. ;; ange-ftp or efs or tramp access
  11957. (let ((user (or (match-string 1 folder) (user-login-name)))
  11958. (host (match-string 2 folder))
  11959. (file (match-string 3 folder)))
  11960. (cond
  11961. ((featurep 'tramp)
  11962. ;; use tramp to access the file
  11963. (if (featurep 'xemacs)
  11964. (setq folder (format "[%s@%s]%s" user host file))
  11965. (setq folder (format "/%s@%s:%s" user host file))))
  11966. (t
  11967. ;; use ange-ftp or efs
  11968. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11969. (setq folder (format "/%s@%s:%s" user host file))))))
  11970. (when folder
  11971. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11972. (sit-for 0.1)
  11973. (when article
  11974. (vm-select-folder-buffer)
  11975. (widen)
  11976. (let ((case-fold-search t))
  11977. (goto-char (point-min))
  11978. (if (not (re-search-forward
  11979. (concat "^" "message-id: *" (regexp-quote article))))
  11980. (error "Could not find the specified message in this folder"))
  11981. (vm-isearch-update)
  11982. (vm-isearch-narrow)
  11983. (vm-beginning-of-message)
  11984. (vm-summarize)))))
  11985. (defun org-follow-wl-link (folder article)
  11986. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11987. (if (and (string= folder "%")
  11988. article
  11989. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11990. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11991. ;; Thus, we recompose folder and article ids.
  11992. (setq folder (format "%s#%s" folder (match-string 1 article))
  11993. article (match-string 3 article)))
  11994. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11995. (error "No such folder: %s" folder))
  11996. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11997. (and article
  11998. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11999. (wl-summary-redisplay)))
  12000. (defun org-follow-rmail-link (folder article)
  12001. "Follow an RMAIL link to FOLDER and ARTICLE."
  12002. (setq article (org-add-angle-brackets article))
  12003. (let (message-number)
  12004. (save-excursion
  12005. (save-window-excursion
  12006. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12007. (setq message-number
  12008. (save-restriction
  12009. (widen)
  12010. (goto-char (point-max))
  12011. (if (re-search-backward
  12012. (concat "^Message-ID:\\s-+" (regexp-quote
  12013. (or article "")))
  12014. nil t)
  12015. (rmail-what-message))))))
  12016. (if message-number
  12017. (progn
  12018. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12019. (rmail-show-message message-number)
  12020. message-number)
  12021. (error "Message not found"))))
  12022. ;;; mh-e integration based on planner-mode
  12023. (defun org-mhe-get-message-real-folder ()
  12024. "Return the name of the current message real folder, so if you use
  12025. sequences, it will now work."
  12026. (save-excursion
  12027. (let* ((folder
  12028. (if (equal major-mode 'mh-folder-mode)
  12029. mh-current-folder
  12030. ;; Refer to the show buffer
  12031. mh-show-folder-buffer))
  12032. (end-index
  12033. (if (boundp 'mh-index-folder)
  12034. (min (length mh-index-folder) (length folder))))
  12035. )
  12036. ;; a simple test on mh-index-data does not work, because
  12037. ;; mh-index-data is always nil in a show buffer.
  12038. (if (and (boundp 'mh-index-folder)
  12039. (string= mh-index-folder (substring folder 0 end-index)))
  12040. (if (equal major-mode 'mh-show-mode)
  12041. (save-window-excursion
  12042. (let (pop-up-frames)
  12043. (when (buffer-live-p (get-buffer folder))
  12044. (progn
  12045. (pop-to-buffer folder)
  12046. (org-mhe-get-message-folder-from-index)
  12047. )
  12048. )))
  12049. (org-mhe-get-message-folder-from-index)
  12050. )
  12051. folder
  12052. )
  12053. )))
  12054. (defun org-mhe-get-message-folder-from-index ()
  12055. "Returns the name of the message folder in a index folder buffer."
  12056. (save-excursion
  12057. (mh-index-previous-folder)
  12058. (re-search-forward "^\\(+.*\\)$" nil t)
  12059. (message "%s" (match-string 1))))
  12060. (defun org-mhe-get-message-folder ()
  12061. "Return the name of the current message folder. Be careful if you
  12062. use sequences."
  12063. (save-excursion
  12064. (if (equal major-mode 'mh-folder-mode)
  12065. mh-current-folder
  12066. ;; Refer to the show buffer
  12067. mh-show-folder-buffer)))
  12068. (defun org-mhe-get-message-num ()
  12069. "Return the number of the current message. Be careful if you
  12070. use sequences."
  12071. (save-excursion
  12072. (if (equal major-mode 'mh-folder-mode)
  12073. (mh-get-msg-num nil)
  12074. ;; Refer to the show buffer
  12075. (mh-show-buffer-message-number))))
  12076. (defun org-mhe-get-header (header)
  12077. "Return a header of the message in folder mode. This will create a
  12078. show buffer for the corresponding message. If you have a more clever
  12079. idea..."
  12080. (let* ((folder (org-mhe-get-message-folder))
  12081. (num (org-mhe-get-message-num))
  12082. (buffer (get-buffer-create (concat "show-" folder)))
  12083. (header-field))
  12084. (with-current-buffer buffer
  12085. (mh-display-msg num folder)
  12086. (if (equal major-mode 'mh-folder-mode)
  12087. (mh-header-display)
  12088. (mh-show-header-display))
  12089. (set-buffer buffer)
  12090. (setq header-field (mh-get-header-field header))
  12091. (if (equal major-mode 'mh-folder-mode)
  12092. (mh-show)
  12093. (mh-show-show))
  12094. header-field)))
  12095. (defun org-follow-mhe-link (folder article)
  12096. "Follow an MHE link to FOLDER and ARTICLE.
  12097. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12098. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12099. ARTICLE is searched in all folders. Indexed searches (swish++,
  12100. namazu, and others supported by MH-E) will always search in all
  12101. folders."
  12102. (require 'mh-e)
  12103. (require 'mh-search)
  12104. (require 'mh-utils)
  12105. (mh-find-path)
  12106. (if (not article)
  12107. (mh-visit-folder (mh-normalize-folder-name folder))
  12108. (setq article (org-add-angle-brackets article))
  12109. (mh-search-choose)
  12110. (if (equal mh-searcher 'pick)
  12111. (progn
  12112. (mh-search folder (list "--message-id" article))
  12113. (when (and org-mhe-search-all-folders
  12114. (not (org-mhe-get-message-real-folder)))
  12115. (kill-this-buffer)
  12116. (mh-search "+" (list "--message-id" article))))
  12117. (mh-search "+" article))
  12118. (if (org-mhe-get-message-real-folder)
  12119. (mh-show-msg 1)
  12120. (kill-this-buffer)
  12121. (error "Message not found"))))
  12122. ;;; BibTeX links
  12123. ;; Use the custom search meachnism to construct and use search strings for
  12124. ;; file links to BibTeX database entries.
  12125. (defun org-create-file-search-in-bibtex ()
  12126. "Create the search string and description for a BibTeX database entry."
  12127. (when (eq major-mode 'bibtex-mode)
  12128. ;; yes, we want to construct this search string.
  12129. ;; Make a good description for this entry, using names, year and the title
  12130. ;; Put it into the `description' variable which is dynamically scoped.
  12131. (let ((bibtex-autokey-names 1)
  12132. (bibtex-autokey-names-stretch 1)
  12133. (bibtex-autokey-name-case-convert-function 'identity)
  12134. (bibtex-autokey-name-separator " & ")
  12135. (bibtex-autokey-additional-names " et al.")
  12136. (bibtex-autokey-year-length 4)
  12137. (bibtex-autokey-name-year-separator " ")
  12138. (bibtex-autokey-titlewords 3)
  12139. (bibtex-autokey-titleword-separator " ")
  12140. (bibtex-autokey-titleword-case-convert-function 'identity)
  12141. (bibtex-autokey-titleword-length 'infty)
  12142. (bibtex-autokey-year-title-separator ": "))
  12143. (setq description (bibtex-generate-autokey)))
  12144. ;; Now parse the entry, get the key and return it.
  12145. (save-excursion
  12146. (bibtex-beginning-of-entry)
  12147. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12148. (defun org-execute-file-search-in-bibtex (s)
  12149. "Find the link search string S as a key for a database entry."
  12150. (when (eq major-mode 'bibtex-mode)
  12151. ;; Yes, we want to do the search in this file.
  12152. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12153. (goto-char (point-min))
  12154. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12155. (regexp-quote s) "[ \t\n]*,") nil t)
  12156. (goto-char (match-beginning 0)))
  12157. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12158. ;; Use double prefix to indicate that any web link should be browsed
  12159. (let ((b (current-buffer)) (p (point)))
  12160. ;; Restore the window configuration because we just use the web link
  12161. (set-window-configuration org-window-config-before-follow-link)
  12162. (save-excursion (set-buffer b) (goto-char p)
  12163. (bibtex-url)))
  12164. (recenter 0)) ; Move entry start to beginning of window
  12165. ;; return t to indicate that the search is done.
  12166. t))
  12167. ;; Finally add the functions to the right hooks.
  12168. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12169. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12170. ;; end of Bibtex link setup
  12171. ;;; Following file links
  12172. (defun org-open-file (path &optional in-emacs line search)
  12173. "Open the file at PATH.
  12174. First, this expands any special file name abbreviations. Then the
  12175. configuration variable `org-file-apps' is checked if it contains an
  12176. entry for this file type, and if yes, the corresponding command is launched.
  12177. If no application is found, Emacs simply visits the file.
  12178. With optional argument IN-EMACS, Emacs will visit the file.
  12179. Optional LINE specifies a line to go to, optional SEARCH a string to
  12180. search for. If LINE or SEARCH is given, the file will always be
  12181. opened in Emacs.
  12182. If the file does not exist, an error is thrown."
  12183. (setq in-emacs (or in-emacs line search))
  12184. (let* ((file (if (equal path "")
  12185. buffer-file-name
  12186. (substitute-in-file-name (expand-file-name path))))
  12187. (apps (append org-file-apps (org-default-apps)))
  12188. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12189. (dirp (if remp nil (file-directory-p file)))
  12190. (dfile (downcase file))
  12191. (old-buffer (current-buffer))
  12192. (old-pos (point))
  12193. (old-mode major-mode)
  12194. ext cmd)
  12195. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12196. (setq ext (match-string 1 dfile))
  12197. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12198. (setq ext (match-string 1 dfile))))
  12199. (if in-emacs
  12200. (setq cmd 'emacs)
  12201. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12202. (and dirp (cdr (assoc 'directory apps)))
  12203. (cdr (assoc ext apps))
  12204. (cdr (assoc t apps)))))
  12205. (when (eq cmd 'mailcap)
  12206. (require 'mailcap)
  12207. (mailcap-parse-mailcaps)
  12208. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12209. (command (mailcap-mime-info mime-type)))
  12210. (if (stringp command)
  12211. (setq cmd command)
  12212. (setq cmd 'emacs))))
  12213. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12214. (not (file-exists-p file))
  12215. (not org-open-non-existing-files))
  12216. (error "No such file: %s" file))
  12217. (cond
  12218. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12219. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12220. (while (string-match "['\"]%s['\"]" cmd)
  12221. (setq cmd (replace-match "%s" t t cmd)))
  12222. (while (string-match "%s" cmd)
  12223. (setq cmd (replace-match
  12224. (save-match-data (shell-quote-argument file))
  12225. t t cmd)))
  12226. (save-window-excursion
  12227. (start-process-shell-command cmd nil cmd)))
  12228. ((or (stringp cmd)
  12229. (eq cmd 'emacs))
  12230. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12231. (widen)
  12232. (if line (goto-line line)
  12233. (if search (org-link-search search))))
  12234. ((consp cmd)
  12235. (eval cmd))
  12236. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12237. (and (org-mode-p) (eq old-mode 'org-mode)
  12238. (or (not (equal old-buffer (current-buffer)))
  12239. (not (equal old-pos (point))))
  12240. (org-mark-ring-push old-pos old-buffer))))
  12241. (defun org-default-apps ()
  12242. "Return the default applications for this operating system."
  12243. (cond
  12244. ((eq system-type 'darwin)
  12245. org-file-apps-defaults-macosx)
  12246. ((eq system-type 'windows-nt)
  12247. org-file-apps-defaults-windowsnt)
  12248. (t org-file-apps-defaults-gnu)))
  12249. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12250. (defun org-file-remote-p (file)
  12251. "Test whether FILE specifies a location on a remote system.
  12252. Return non-nil if the location is indeed remote.
  12253. For example, the filename \"/user@host:/foo\" specifies a location
  12254. on the system \"/user@host:\"."
  12255. (cond ((fboundp 'file-remote-p)
  12256. (file-remote-p file))
  12257. ((fboundp 'tramp-handle-file-remote-p)
  12258. (tramp-handle-file-remote-p file))
  12259. ((and (boundp 'ange-ftp-name-format)
  12260. (string-match (car ange-ftp-name-format) file))
  12261. t)
  12262. (t nil)))
  12263. ;;;; Hooks for remember.el, and refiling
  12264. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12265. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12266. ;;;###autoload
  12267. (defun org-remember-insinuate ()
  12268. "Setup remember.el for use wiht Org-mode."
  12269. (require 'remember)
  12270. (setq remember-annotation-functions '(org-remember-annotation))
  12271. (setq remember-handler-functions '(org-remember-handler))
  12272. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12273. ;;;###autoload
  12274. (defun org-remember-annotation ()
  12275. "Return a link to the current location as an annotation for remember.el.
  12276. If you are using Org-mode files as target for data storage with
  12277. remember.el, then the annotations should include a link compatible with the
  12278. conventions in Org-mode. This function returns such a link."
  12279. (org-store-link nil))
  12280. (defconst org-remember-help
  12281. "Select a destination location for the note.
  12282. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12283. RET on headline -> Store as sublevel entry to current headline
  12284. RET at beg-of-buf -> Append to file as level 2 headline
  12285. <left>/<right> -> before/after current headline, same headings level")
  12286. (defvar org-remember-previous-location nil)
  12287. (defvar org-force-remember-template-char) ;; dynamically scoped
  12288. (defun org-select-remember-template (&optional use-char)
  12289. (when org-remember-templates
  12290. (let* ((templates (mapcar (lambda (x)
  12291. (if (stringp (car x))
  12292. (append (list (nth 1 x) (car x)) (cddr x))
  12293. (append (list (car x) "") (cdr x))))
  12294. org-remember-templates))
  12295. (char (or use-char
  12296. (cond
  12297. ((= (length templates) 1)
  12298. (caar templates))
  12299. ((and (boundp 'org-force-remember-template-char)
  12300. org-force-remember-template-char)
  12301. (if (stringp org-force-remember-template-char)
  12302. (string-to-char org-force-remember-template-char)
  12303. org-force-remember-template-char))
  12304. (t
  12305. (message "Select template: %s"
  12306. (mapconcat
  12307. (lambda (x)
  12308. (cond
  12309. ((not (string-match "\\S-" (nth 1 x)))
  12310. (format "[%c]" (car x)))
  12311. ((equal (downcase (car x))
  12312. (downcase (aref (nth 1 x) 0)))
  12313. (format "[%c]%s" (car x)
  12314. (substring (nth 1 x) 1)))
  12315. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12316. templates " "))
  12317. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12318. (when (equal char0 ?\C-g)
  12319. (jump-to-register remember-register)
  12320. (kill-buffer remember-buffer))
  12321. char0))))))
  12322. (cddr (assoc char templates)))))
  12323. (defvar x-last-selected-text)
  12324. (defvar x-last-selected-text-primary)
  12325. ;;;###autoload
  12326. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12327. "Initialize *remember* buffer with template, invoke `org-mode'.
  12328. This function should be placed into `remember-mode-hook' and in fact requires
  12329. to be run from that hook to function properly."
  12330. (if org-remember-templates
  12331. (let* ((entry (org-select-remember-template use-char))
  12332. (tpl (car entry))
  12333. (plist-p (if org-store-link-plist t nil))
  12334. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12335. (string-match "\\S-" (nth 1 entry)))
  12336. (nth 1 entry)
  12337. org-default-notes-file))
  12338. (headline (nth 2 entry))
  12339. (v-c (or (and (eq window-system 'x)
  12340. (fboundp 'x-cut-buffer-or-selection-value)
  12341. (x-cut-buffer-or-selection-value))
  12342. (org-bound-and-true-p x-last-selected-text)
  12343. (org-bound-and-true-p x-last-selected-text-primary)
  12344. (and (> (length kill-ring) 0) (current-kill 0))))
  12345. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12346. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12347. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12348. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12349. ;; `initial' and `annotation' are bound in `remember'
  12350. (v-i (if (boundp 'initial) initial))
  12351. (v-a (if (and (boundp 'annotation) annotation)
  12352. (if (equal annotation "[[]]") "" annotation)
  12353. ""))
  12354. (v-A (if (and v-a
  12355. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12356. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12357. v-a))
  12358. (v-n user-full-name)
  12359. (org-startup-folded nil)
  12360. org-time-was-given org-end-time-was-given x
  12361. prompt completions char time pos default histvar)
  12362. (setq org-store-link-plist
  12363. (append (list :annotation v-a :initial v-i)
  12364. org-store-link-plist))
  12365. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12366. (erase-buffer)
  12367. (insert (substitute-command-keys
  12368. (format
  12369. "## Filing location: Select interactively, default, or last used:
  12370. ## %s to select file and header location interactively.
  12371. ## %s \"%s\" -> \"* %s\"
  12372. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12373. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12374. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12375. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12376. (abbreviate-file-name (or file org-default-notes-file))
  12377. (or headline "")
  12378. (or (car org-remember-previous-location) "???")
  12379. (or (cdr org-remember-previous-location) "???"))))
  12380. (insert tpl) (goto-char (point-min))
  12381. ;; Simple %-escapes
  12382. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12383. (when (and initial (equal (match-string 0) "%i"))
  12384. (save-match-data
  12385. (let* ((lead (buffer-substring
  12386. (point-at-bol) (match-beginning 0))))
  12387. (setq v-i (mapconcat 'identity
  12388. (org-split-string initial "\n")
  12389. (concat "\n" lead))))))
  12390. (replace-match
  12391. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12392. t t))
  12393. ;; %[] Insert contents of a file.
  12394. (goto-char (point-min))
  12395. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12396. (let ((start (match-beginning 0))
  12397. (end (match-end 0))
  12398. (filename (expand-file-name (match-string 1))))
  12399. (goto-char start)
  12400. (delete-region start end)
  12401. (condition-case error
  12402. (insert-file-contents filename)
  12403. (error (insert (format "%%![Couldn't insert %s: %s]"
  12404. filename error))))))
  12405. ;; %() embedded elisp
  12406. (goto-char (point-min))
  12407. (while (re-search-forward "%\\((.+)\\)" nil t)
  12408. (goto-char (match-beginning 0))
  12409. (let ((template-start (point)))
  12410. (forward-char 1)
  12411. (let ((result
  12412. (condition-case error
  12413. (eval (read (current-buffer)))
  12414. (error (format "%%![Error: %s]" error)))))
  12415. (delete-region template-start (point))
  12416. (insert result))))
  12417. ;; From the property list
  12418. (when plist-p
  12419. (goto-char (point-min))
  12420. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12421. (and (setq x (or (plist-get org-store-link-plist
  12422. (intern (match-string 1))) ""))
  12423. (replace-match x t t))))
  12424. ;; Turn on org-mode in the remember buffer, set local variables
  12425. (org-mode)
  12426. (org-set-local 'org-finish-function 'org-remember-finalize)
  12427. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12428. (org-set-local 'org-default-notes-file file))
  12429. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12430. (org-set-local 'org-remember-default-headline headline))
  12431. ;; Interactive template entries
  12432. (goto-char (point-min))
  12433. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12434. (setq char (if (match-end 3) (match-string 3))
  12435. prompt (if (match-end 2) (match-string 2)))
  12436. (goto-char (match-beginning 0))
  12437. (replace-match "")
  12438. (setq completions nil default nil)
  12439. (when prompt
  12440. (setq completions (org-split-string prompt "|")
  12441. prompt (pop completions)
  12442. default (car completions)
  12443. histvar (intern (concat
  12444. "org-remember-template-prompt-history::"
  12445. (or prompt "")))
  12446. completions (mapcar 'list completions)))
  12447. (cond
  12448. ((member char '("G" "g"))
  12449. (let* ((org-last-tags-completion-table
  12450. (org-global-tags-completion-table
  12451. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12452. (org-add-colon-after-tag-completion t)
  12453. (ins (completing-read
  12454. (if prompt (concat prompt ": ") "Tags: ")
  12455. 'org-tags-completion-function nil nil nil
  12456. 'org-tags-history)))
  12457. (setq ins (mapconcat 'identity
  12458. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12459. ":"))
  12460. (when (string-match "\\S-" ins)
  12461. (or (equal (char-before) ?:) (insert ":"))
  12462. (insert ins)
  12463. (or (equal (char-after) ?:) (insert ":")))))
  12464. (char
  12465. (setq org-time-was-given (equal (upcase char) char))
  12466. (setq time (org-read-date (equal (upcase char) "U") t nil
  12467. prompt))
  12468. (org-insert-time-stamp time org-time-was-given
  12469. (member char '("u" "U"))
  12470. nil nil (list org-end-time-was-given)))
  12471. (t
  12472. (insert (org-completing-read
  12473. (concat (if prompt prompt "Enter string")
  12474. (if default (concat " [" default "]"))
  12475. ": ")
  12476. completions nil nil nil histvar default)))))
  12477. (goto-char (point-min))
  12478. (if (re-search-forward "%\\?" nil t)
  12479. (replace-match "")
  12480. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12481. (org-mode)
  12482. (org-set-local 'org-finish-function 'org-remember-finalize))
  12483. (when (save-excursion
  12484. (goto-char (point-min))
  12485. (re-search-forward "%!" nil t))
  12486. (replace-match "")
  12487. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12488. (defun org-remember-finish-immediately ()
  12489. "File remember note immediately.
  12490. This should be run in `post-command-hook' and will remove itself
  12491. from that hook."
  12492. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12493. (when org-finish-function
  12494. (funcall org-finish-function)))
  12495. (defvar org-clock-marker) ; Defined below
  12496. (defun org-remember-finalize ()
  12497. "Finalize the remember process."
  12498. (unless (fboundp 'remember-finalize)
  12499. (defalias 'remember-finalize 'remember-buffer))
  12500. (when (and org-clock-marker
  12501. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12502. ;; FIXME: test this, this is w/o notetaking!
  12503. (let (org-log-note-clock-out) (org-clock-out)))
  12504. (when buffer-file-name
  12505. (save-buffer)
  12506. (setq buffer-file-name nil))
  12507. (remember-finalize))
  12508. ;;;###autoload
  12509. (defun org-remember (&optional goto org-force-remember-template-char)
  12510. "Call `remember'. If this is already a remember buffer, re-apply template.
  12511. If there is an active region, make sure remember uses it as initial content
  12512. of the remember buffer.
  12513. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12514. anything, just go to the file/headline where the selected template usually
  12515. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12516. note stored by remember.
  12517. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12518. associated with a template in `org-remember-templates'."
  12519. (interactive "P")
  12520. (cond
  12521. ((equal goto '(4)) (org-go-to-remember-target))
  12522. ((equal goto '(16)) (org-remember-goto-last-stored))
  12523. (t
  12524. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12525. (progn
  12526. (when (< (length org-remember-templates) 2)
  12527. (error "No other template available"))
  12528. (erase-buffer)
  12529. (let ((annotation (plist-get org-store-link-plist :annotation))
  12530. (initial (plist-get org-store-link-plist :initial)))
  12531. (org-remember-apply-template))
  12532. (message "Press C-c C-c to remember data"))
  12533. (if (org-region-active-p)
  12534. (remember (buffer-substring (point) (mark)))
  12535. (call-interactively 'remember))))))
  12536. (defun org-remember-goto-last-stored ()
  12537. "Go to the location where the last remember note was stored."
  12538. (interactive)
  12539. (bookmark-jump "org-remember-last-stored")
  12540. (message "This is the last note stored by remember"))
  12541. (defun org-go-to-remember-target (&optional template-key)
  12542. "Go to the target location of a remember template.
  12543. The user is queried for the template."
  12544. (interactive)
  12545. (let* ((entry (org-select-remember-template template-key))
  12546. (file (nth 1 entry))
  12547. (heading (nth 2 entry))
  12548. visiting)
  12549. (unless (and file (stringp file) (string-match "\\S-" file))
  12550. (setq file org-default-notes-file))
  12551. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12552. (setq heading org-remember-default-headline))
  12553. (setq visiting (org-find-base-buffer-visiting file))
  12554. (if (not visiting) (find-file-noselect file))
  12555. (switch-to-buffer (or visiting (get-file-buffer file)))
  12556. (widen)
  12557. (goto-char (point-min))
  12558. (if (re-search-forward
  12559. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12560. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12561. nil t)
  12562. (goto-char (match-beginning 0))
  12563. (error "Target headline not found: %s" heading))))
  12564. (defvar org-note-abort nil) ; dynamically scoped
  12565. ;;;###autoload
  12566. (defun org-remember-handler ()
  12567. "Store stuff from remember.el into an org file.
  12568. First prompts for an org file. If the user just presses return, the value
  12569. of `org-default-notes-file' is used.
  12570. Then the command offers the headings tree of the selected file in order to
  12571. file the text at a specific location.
  12572. You can either immediately press RET to get the note appended to the
  12573. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12574. find a better place. Then press RET or <left> or <right> in insert the note.
  12575. Key Cursor position Note gets inserted
  12576. -----------------------------------------------------------------------------
  12577. RET buffer-start as level 1 heading at end of file
  12578. RET on headline as sublevel of the heading at cursor
  12579. RET no heading at cursor position, level taken from context.
  12580. Or use prefix arg to specify level manually.
  12581. <left> on headline as same level, before current heading
  12582. <right> on headline as same level, after current heading
  12583. So the fastest way to store the note is to press RET RET to append it to
  12584. the default file. This way your current train of thought is not
  12585. interrupted, in accordance with the principles of remember.el.
  12586. You can also get the fast execution without prompting by using
  12587. C-u C-c C-c to exit the remember buffer. See also the variable
  12588. `org-remember-store-without-prompt'.
  12589. Before being stored away, the function ensures that the text has a
  12590. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12591. is constructed from the current date and some additional data.
  12592. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12593. also indented so that it starts in the same column as the headline
  12594. \(i.e. after the stars).
  12595. See also the variable `org-reverse-note-order'."
  12596. (goto-char (point-min))
  12597. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12598. (replace-match ""))
  12599. (goto-char (point-max))
  12600. (beginning-of-line 1)
  12601. (while (looking-at "[ \t]*$\\|##.*")
  12602. (delete-region (1- (point)) (point-max))
  12603. (beginning-of-line 1))
  12604. (catch 'quit
  12605. (if org-note-abort (throw 'quit nil))
  12606. (let* ((txt (buffer-substring (point-min) (point-max)))
  12607. (fastp (org-xor (equal current-prefix-arg '(4))
  12608. org-remember-store-without-prompt))
  12609. (file (cond
  12610. (fastp org-default-notes-file)
  12611. ((and (eq org-remember-interactive-interface 'refile)
  12612. org-refile-targets)
  12613. org-default-notes-file)
  12614. ((not (and (equal current-prefix-arg '(16))
  12615. org-remember-previous-location))
  12616. (org-get-org-file))))
  12617. (heading org-remember-default-headline)
  12618. (visiting (and file (org-find-base-buffer-visiting file)))
  12619. (org-startup-folded nil)
  12620. (org-startup-align-all-tables nil)
  12621. (org-goto-start-pos 1)
  12622. spos exitcmd level indent reversed)
  12623. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12624. (setq file (car org-remember-previous-location)
  12625. heading (cdr org-remember-previous-location)
  12626. fastp t))
  12627. (setq current-prefix-arg nil)
  12628. (if (string-match "[ \t\n]+\\'" txt)
  12629. (setq txt (replace-match "" t t txt)))
  12630. ;; Modify text so that it becomes a nice subtree which can be inserted
  12631. ;; into an org tree.
  12632. (let* ((lines (split-string txt "\n"))
  12633. first)
  12634. (setq first (car lines) lines (cdr lines))
  12635. (if (string-match "^\\*+ " first)
  12636. ;; Is already a headline
  12637. (setq indent nil)
  12638. ;; We need to add a headline: Use time and first buffer line
  12639. (setq lines (cons first lines)
  12640. first (concat "* " (current-time-string)
  12641. " (" (remember-buffer-desc) ")")
  12642. indent " "))
  12643. (if (and org-adapt-indentation indent)
  12644. (setq lines (mapcar
  12645. (lambda (x)
  12646. (if (string-match "\\S-" x)
  12647. (concat indent x) x))
  12648. lines)))
  12649. (setq txt (concat first "\n"
  12650. (mapconcat 'identity lines "\n"))))
  12651. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12652. (setq txt (replace-match "\n\n" t t txt))
  12653. (if (string-match "[ \t\n]*\\'" txt)
  12654. (setq txt (replace-match "\n" t t txt))))
  12655. ;; Put the modified text back into the remember buffer, for refile.
  12656. (erase-buffer)
  12657. (insert txt)
  12658. (goto-char (point-min))
  12659. (when (and (eq org-remember-interactive-interface 'refile)
  12660. (not fastp))
  12661. (org-refile nil (or visiting (find-file-noselect file)))
  12662. (throw 'quit t))
  12663. ;; Find the file
  12664. (if (not visiting) (find-file-noselect file))
  12665. (with-current-buffer (or visiting (get-file-buffer file))
  12666. (unless (org-mode-p)
  12667. (error "Target files for remember notes must be in Org-mode"))
  12668. (save-excursion
  12669. (save-restriction
  12670. (widen)
  12671. (and (goto-char (point-min))
  12672. (not (re-search-forward "^\\* " nil t))
  12673. (insert "\n* " (or heading "Notes") "\n"))
  12674. (setq reversed (org-notes-order-reversed-p))
  12675. ;; Find the default location
  12676. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12677. (goto-char (point-min))
  12678. (if (re-search-forward
  12679. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12680. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12681. nil t)
  12682. (setq org-goto-start-pos (match-beginning 0))
  12683. (when fastp
  12684. (goto-char (point-max))
  12685. (unless (bolp) (newline))
  12686. (insert "* " heading "\n")
  12687. (setq org-goto-start-pos (point-at-bol 0)))))
  12688. ;; Ask the User for a location, using the appropriate interface
  12689. (cond
  12690. (fastp (setq spos org-goto-start-pos
  12691. exitcmd 'return))
  12692. ((eq org-remember-interactive-interface 'outline)
  12693. (setq spos (org-get-location (current-buffer)
  12694. org-remember-help)
  12695. exitcmd (cdr spos)
  12696. spos (car spos)))
  12697. ((eq org-remember-interactive-interface 'outline-path-completion)
  12698. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12699. (org-refile-use-outline-path t))
  12700. (setq spos (org-refile-get-location "Heading: ")
  12701. exitcmd 'return
  12702. spos (nth 3 spos))))
  12703. (t (error "this should not hapen")))
  12704. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12705. ; not handle this note
  12706. (goto-char spos)
  12707. (cond ((org-on-heading-p t)
  12708. (org-back-to-heading t)
  12709. (setq level (funcall outline-level))
  12710. (cond
  12711. ((eq exitcmd 'return)
  12712. ;; sublevel of current
  12713. (setq org-remember-previous-location
  12714. (cons (abbreviate-file-name file)
  12715. (org-get-heading 'notags)))
  12716. (if reversed
  12717. (outline-next-heading)
  12718. (org-end-of-subtree t)
  12719. (if (not (bolp))
  12720. (if (looking-at "[ \t]*\n")
  12721. (beginning-of-line 2)
  12722. (end-of-line 1)
  12723. (insert "\n"))))
  12724. (bookmark-set "org-remember-last-stored")
  12725. (org-paste-subtree (org-get-legal-level level 1) txt))
  12726. ((eq exitcmd 'left)
  12727. ;; before current
  12728. (bookmark-set "org-remember-last-stored")
  12729. (org-paste-subtree level txt))
  12730. ((eq exitcmd 'right)
  12731. ;; after current
  12732. (org-end-of-subtree t)
  12733. (bookmark-set "org-remember-last-stored")
  12734. (org-paste-subtree level txt))
  12735. (t (error "This should not happen"))))
  12736. ((and (bobp) (not reversed))
  12737. ;; Put it at the end, one level below level 1
  12738. (save-restriction
  12739. (widen)
  12740. (goto-char (point-max))
  12741. (if (not (bolp)) (newline))
  12742. (bookmark-set "org-remember-last-stored")
  12743. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12744. ((and (bobp) reversed)
  12745. ;; Put it at the start, as level 1
  12746. (save-restriction
  12747. (widen)
  12748. (goto-char (point-min))
  12749. (re-search-forward "^\\*+ " nil t)
  12750. (beginning-of-line 1)
  12751. (bookmark-set "org-remember-last-stored")
  12752. (org-paste-subtree 1 txt)))
  12753. (t
  12754. ;; Put it right there, with automatic level determined by
  12755. ;; org-paste-subtree or from prefix arg
  12756. (bookmark-set "org-remember-last-stored")
  12757. (org-paste-subtree
  12758. (if (numberp current-prefix-arg) current-prefix-arg)
  12759. txt)))
  12760. (when remember-save-after-remembering
  12761. (save-buffer)
  12762. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12763. t) ;; return t to indicate that we took care of this note.
  12764. (defun org-get-org-file ()
  12765. "Read a filename, with default directory `org-directory'."
  12766. (let ((default (or org-default-notes-file remember-data-file)))
  12767. (read-file-name (format "File name [%s]: " default)
  12768. (file-name-as-directory org-directory)
  12769. default)))
  12770. (defun org-notes-order-reversed-p ()
  12771. "Check if the current file should receive notes in reversed order."
  12772. (cond
  12773. ((not org-reverse-note-order) nil)
  12774. ((eq t org-reverse-note-order) t)
  12775. ((not (listp org-reverse-note-order)) nil)
  12776. (t (catch 'exit
  12777. (let ((all org-reverse-note-order)
  12778. entry)
  12779. (while (setq entry (pop all))
  12780. (if (string-match (car entry) buffer-file-name)
  12781. (throw 'exit (cdr entry))))
  12782. nil)))))
  12783. ;;; Refiling
  12784. (defvar org-refile-target-table nil
  12785. "The list of refile targets, created by `org-refile'.")
  12786. (defvar org-agenda-new-buffers nil
  12787. "Buffers created to visit agenda files.")
  12788. (defun org-get-refile-targets (&optional default-buffer)
  12789. "Produce a table with refile targets."
  12790. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12791. targets txt re files f desc descre)
  12792. (with-current-buffer (or default-buffer (current-buffer))
  12793. (while (setq entry (pop entries))
  12794. (setq files (car entry) desc (cdr entry))
  12795. (cond
  12796. ((null files) (setq files (list (current-buffer))))
  12797. ((eq files 'org-agenda-files)
  12798. (setq files (org-agenda-files 'unrestricted)))
  12799. ((and (symbolp files) (fboundp files))
  12800. (setq files (funcall files)))
  12801. ((and (symbolp files) (boundp files))
  12802. (setq files (symbol-value files))))
  12803. (if (stringp files) (setq files (list files)))
  12804. (cond
  12805. ((eq (car desc) :tag)
  12806. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12807. ((eq (car desc) :todo)
  12808. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12809. ((eq (car desc) :regexp)
  12810. (setq descre (cdr desc)))
  12811. ((eq (car desc) :level)
  12812. (setq descre (concat "^\\*\\{" (number-to-string
  12813. (if org-odd-levels-only
  12814. (1- (* 2 (cdr desc)))
  12815. (cdr desc)))
  12816. "\\}[ \t]")))
  12817. ((eq (car desc) :maxlevel)
  12818. (setq descre (concat "^\\*\\{1," (number-to-string
  12819. (if org-odd-levels-only
  12820. (1- (* 2 (cdr desc)))
  12821. (cdr desc)))
  12822. "\\}[ \t]")))
  12823. (t (error "Bad refiling target description %s" desc)))
  12824. (while (setq f (pop files))
  12825. (save-excursion
  12826. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12827. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12828. (save-excursion
  12829. (save-restriction
  12830. (widen)
  12831. (goto-char (point-min))
  12832. (while (re-search-forward descre nil t)
  12833. (goto-char (point-at-bol))
  12834. (when (looking-at org-complex-heading-regexp)
  12835. (setq txt (match-string 4)
  12836. re (concat "^" (regexp-quote
  12837. (buffer-substring (match-beginning 1)
  12838. (match-end 4)))))
  12839. (if (match-end 5) (setq re (concat re "[ \t]+"
  12840. (regexp-quote
  12841. (match-string 5)))))
  12842. (setq re (concat re "[ \t]*$"))
  12843. (when org-refile-use-outline-path
  12844. (setq txt (mapconcat 'identity
  12845. (append
  12846. (if (eq org-refile-use-outline-path 'file)
  12847. (list (file-name-nondirectory
  12848. (buffer-file-name (buffer-base-buffer))))
  12849. (if (eq org-refile-use-outline-path 'full-file-path)
  12850. (list (buffer-file-name (buffer-base-buffer)))))
  12851. (org-get-outline-path)
  12852. (list txt))
  12853. "/")))
  12854. (push (list txt f re (point)) targets))
  12855. (goto-char (point-at-eol))))))))
  12856. (nreverse targets))))
  12857. (defun org-get-outline-path ()
  12858. "Return the outline path to the current entry, as a list."
  12859. (let (rtn)
  12860. (save-excursion
  12861. (while (org-up-heading-safe)
  12862. (when (looking-at org-complex-heading-regexp)
  12863. (push (org-match-string-no-properties 4) rtn)))
  12864. rtn)))
  12865. (defvar org-refile-history nil
  12866. "History for refiling operations.")
  12867. (defun org-refile (&optional goto default-buffer)
  12868. "Move the entry at point to another heading.
  12869. The list of target headings is compiled using the information in
  12870. `org-refile-targets', which see. This list is created upon first use, and
  12871. you can update it by calling this command with a double prefix (`C-u C-u').
  12872. FIXME: Can we find a better way of updating?
  12873. At the target location, the entry is filed as a subitem of the target heading.
  12874. Depending on `org-reverse-note-order', the new subitem will either be the
  12875. first of the last subitem.
  12876. With prefix arg GOTO, the command will only visit the target location,
  12877. not actually move anything.
  12878. With a double prefix `C-c C-c', go to the location where the last refiling
  12879. operation has put the subtree.
  12880. With a double prefix argument, the command can be used to jump to any
  12881. heading in the current buffer."
  12882. (interactive "P")
  12883. (let* ((cbuf (current-buffer))
  12884. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12885. pos it nbuf file re level reversed)
  12886. (if (equal goto '(16))
  12887. (org-refile-goto-last-stored)
  12888. (when (setq it (org-refile-get-location
  12889. (if goto "Goto: " "Refile to: ") default-buffer))
  12890. (setq file (nth 1 it)
  12891. re (nth 2 it)
  12892. pos (nth 3 it))
  12893. (setq nbuf (or (find-buffer-visiting file)
  12894. (find-file-noselect file)))
  12895. (if goto
  12896. (progn
  12897. (switch-to-buffer nbuf)
  12898. (goto-char pos)
  12899. (org-show-context 'org-goto))
  12900. (org-copy-special)
  12901. (save-excursion
  12902. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12903. (find-file-noselect file))))
  12904. (setq reversed (org-notes-order-reversed-p))
  12905. (save-excursion
  12906. (save-restriction
  12907. (widen)
  12908. (goto-char pos)
  12909. (looking-at outline-regexp)
  12910. (setq level (org-get-legal-level (funcall outline-level) 1))
  12911. (goto-char
  12912. (if reversed
  12913. (outline-next-heading)
  12914. (or (save-excursion (outline-get-next-sibling))
  12915. (org-end-of-subtree t t)
  12916. (point-max))))
  12917. (bookmark-set "org-refile-last-stored")
  12918. (org-paste-subtree level))))
  12919. (org-cut-special)
  12920. (message "Entry refiled to \"%s\"" (car it)))))))
  12921. (defun org-refile-goto-last-stored ()
  12922. "Go to the location where the last refile was stored."
  12923. (interactive)
  12924. (bookmark-jump "org-refile-last-stored")
  12925. (message "This is the location of the last refile"))
  12926. (defun org-refile-get-location (&optional prompt default-buffer)
  12927. "Prompt the user for a refile location, using PROMPT."
  12928. (let ((org-refile-targets org-refile-targets)
  12929. (org-refile-use-outline-path org-refile-use-outline-path))
  12930. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12931. (unless org-refile-target-table
  12932. (error "No refile targets"))
  12933. (let* ((cbuf (current-buffer))
  12934. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12935. (fname (and filename (file-truename filename)))
  12936. (tbl (mapcar
  12937. (lambda (x)
  12938. (if (not (equal fname (file-truename (nth 1 x))))
  12939. (cons (concat (car x) " (" (file-name-nondirectory
  12940. (nth 1 x)) ")")
  12941. (cdr x))
  12942. x))
  12943. org-refile-target-table))
  12944. (completion-ignore-case t))
  12945. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12946. tbl)))
  12947. ;;;; Dynamic blocks
  12948. (defun org-find-dblock (name)
  12949. "Find the first dynamic block with name NAME in the buffer.
  12950. If not found, stay at current position and return nil."
  12951. (let (pos)
  12952. (save-excursion
  12953. (goto-char (point-min))
  12954. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12955. nil t)
  12956. (match-beginning 0))))
  12957. (if pos (goto-char pos))
  12958. pos))
  12959. (defconst org-dblock-start-re
  12960. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12961. "Matches the startline of a dynamic block, with parameters.")
  12962. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12963. "Matches the end of a dyhamic block.")
  12964. (defun org-create-dblock (plist)
  12965. "Create a dynamic block section, with parameters taken from PLIST.
  12966. PLIST must containe a :name entry which is used as name of the block."
  12967. (unless (bolp) (newline))
  12968. (let ((name (plist-get plist :name)))
  12969. (insert "#+BEGIN: " name)
  12970. (while plist
  12971. (if (eq (car plist) :name)
  12972. (setq plist (cddr plist))
  12973. (insert " " (prin1-to-string (pop plist)))))
  12974. (insert "\n\n#+END:\n")
  12975. (beginning-of-line -2)))
  12976. (defun org-prepare-dblock ()
  12977. "Prepare dynamic block for refresh.
  12978. This empties the block, puts the cursor at the insert position and returns
  12979. the property list including an extra property :name with the block name."
  12980. (unless (looking-at org-dblock-start-re)
  12981. (error "Not at a dynamic block"))
  12982. (let* ((begdel (1+ (match-end 0)))
  12983. (name (org-no-properties (match-string 1)))
  12984. (params (append (list :name name)
  12985. (read (concat "(" (match-string 3) ")")))))
  12986. (unless (re-search-forward org-dblock-end-re nil t)
  12987. (error "Dynamic block not terminated"))
  12988. (delete-region begdel (match-beginning 0))
  12989. (goto-char begdel)
  12990. (open-line 1)
  12991. params))
  12992. (defun org-map-dblocks (&optional command)
  12993. "Apply COMMAND to all dynamic blocks in the current buffer.
  12994. If COMMAND is not given, use `org-update-dblock'."
  12995. (let ((cmd (or command 'org-update-dblock))
  12996. pos)
  12997. (save-excursion
  12998. (goto-char (point-min))
  12999. (while (re-search-forward org-dblock-start-re nil t)
  13000. (goto-char (setq pos (match-beginning 0)))
  13001. (condition-case nil
  13002. (funcall cmd)
  13003. (error (message "Error during update of dynamic block")))
  13004. (goto-char pos)
  13005. (unless (re-search-forward org-dblock-end-re nil t)
  13006. (error "Dynamic block not terminated"))))))
  13007. (defun org-dblock-update (&optional arg)
  13008. "User command for updating dynamic blocks.
  13009. Update the dynamic block at point. With prefix ARG, update all dynamic
  13010. blocks in the buffer."
  13011. (interactive "P")
  13012. (if arg
  13013. (org-update-all-dblocks)
  13014. (or (looking-at org-dblock-start-re)
  13015. (org-beginning-of-dblock))
  13016. (org-update-dblock)))
  13017. (defun org-update-dblock ()
  13018. "Update the dynamic block at point
  13019. This means to empty the block, parse for parameters and then call
  13020. the correct writing function."
  13021. (save-window-excursion
  13022. (let* ((pos (point))
  13023. (line (org-current-line))
  13024. (params (org-prepare-dblock))
  13025. (name (plist-get params :name))
  13026. (cmd (intern (concat "org-dblock-write:" name))))
  13027. (message "Updating dynamic block `%s' at line %d..." name line)
  13028. (funcall cmd params)
  13029. (message "Updating dynamic block `%s' at line %d...done" name line)
  13030. (goto-char pos))))
  13031. (defun org-beginning-of-dblock ()
  13032. "Find the beginning of the dynamic block at point.
  13033. Error if there is no scuh block at point."
  13034. (let ((pos (point))
  13035. beg)
  13036. (end-of-line 1)
  13037. (if (and (re-search-backward org-dblock-start-re nil t)
  13038. (setq beg (match-beginning 0))
  13039. (re-search-forward org-dblock-end-re nil t)
  13040. (> (match-end 0) pos))
  13041. (goto-char beg)
  13042. (goto-char pos)
  13043. (error "Not in a dynamic block"))))
  13044. (defun org-update-all-dblocks ()
  13045. "Update all dynamic blocks in the buffer.
  13046. This function can be used in a hook."
  13047. (when (org-mode-p)
  13048. (org-map-dblocks 'org-update-dblock)))
  13049. ;;;; Completion
  13050. (defconst org-additional-option-like-keywords
  13051. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13052. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13053. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13054. (defun org-complete (&optional arg)
  13055. "Perform completion on word at point.
  13056. At the beginning of a headline, this completes TODO keywords as given in
  13057. `org-todo-keywords'.
  13058. If the current word is preceded by a backslash, completes the TeX symbols
  13059. that are supported for HTML support.
  13060. If the current word is preceded by \"#+\", completes special words for
  13061. setting file options.
  13062. In the line after \"#+STARTUP:, complete valid keywords.\"
  13063. At all other locations, this simply calls the value of
  13064. `org-completion-fallback-command'."
  13065. (interactive "P")
  13066. (org-without-partial-completion
  13067. (catch 'exit
  13068. (let* ((end (point))
  13069. (beg1 (save-excursion
  13070. (skip-chars-backward (org-re "[:alnum:]_@"))
  13071. (point)))
  13072. (beg (save-excursion
  13073. (skip-chars-backward "a-zA-Z0-9_:$")
  13074. (point)))
  13075. (confirm (lambda (x) (stringp (car x))))
  13076. (searchhead (equal (char-before beg) ?*))
  13077. (tag (and (equal (char-before beg1) ?:)
  13078. (equal (char-after (point-at-bol)) ?*)))
  13079. (prop (and (equal (char-before beg1) ?:)
  13080. (not (equal (char-after (point-at-bol)) ?*))))
  13081. (texp (equal (char-before beg) ?\\))
  13082. (link (equal (char-before beg) ?\[))
  13083. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13084. beg)
  13085. "#+"))
  13086. (startup (string-match "^#\\+STARTUP:.*"
  13087. (buffer-substring (point-at-bol) (point))))
  13088. (completion-ignore-case opt)
  13089. (type nil)
  13090. (tbl nil)
  13091. (table (cond
  13092. (opt
  13093. (setq type :opt)
  13094. (append
  13095. (mapcar
  13096. (lambda (x)
  13097. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13098. (cons (match-string 2 x) (match-string 1 x)))
  13099. (org-split-string (org-get-current-options) "\n"))
  13100. (mapcar 'list org-additional-option-like-keywords)))
  13101. (startup
  13102. (setq type :startup)
  13103. org-startup-options)
  13104. (link (append org-link-abbrev-alist-local
  13105. org-link-abbrev-alist))
  13106. (texp
  13107. (setq type :tex)
  13108. org-html-entities)
  13109. ((string-match "\\`\\*+[ \t]+\\'"
  13110. (buffer-substring (point-at-bol) beg))
  13111. (setq type :todo)
  13112. (mapcar 'list org-todo-keywords-1))
  13113. (searchhead
  13114. (setq type :searchhead)
  13115. (save-excursion
  13116. (goto-char (point-min))
  13117. (while (re-search-forward org-todo-line-regexp nil t)
  13118. (push (list
  13119. (org-make-org-heading-search-string
  13120. (match-string 3) t))
  13121. tbl)))
  13122. tbl)
  13123. (tag (setq type :tag beg beg1)
  13124. (or org-tag-alist (org-get-buffer-tags)))
  13125. (prop (setq type :prop beg beg1)
  13126. (mapcar 'list (org-buffer-property-keys nil t t)))
  13127. (t (progn
  13128. (call-interactively org-completion-fallback-command)
  13129. (throw 'exit nil)))))
  13130. (pattern (buffer-substring-no-properties beg end))
  13131. (completion (try-completion pattern table confirm)))
  13132. (cond ((eq completion t)
  13133. (if (not (assoc (upcase pattern) table))
  13134. (message "Already complete")
  13135. (if (equal type :opt)
  13136. (insert (substring (cdr (assoc (upcase pattern) table))
  13137. (length pattern)))
  13138. (if (memq type '(:tag :prop)) (insert ":")))))
  13139. ((null completion)
  13140. (message "Can't find completion for \"%s\"" pattern)
  13141. (ding))
  13142. ((not (string= pattern completion))
  13143. (delete-region beg end)
  13144. (if (string-match " +$" completion)
  13145. (setq completion (replace-match "" t t completion)))
  13146. (insert completion)
  13147. (if (get-buffer-window "*Completions*")
  13148. (delete-window (get-buffer-window "*Completions*")))
  13149. (if (assoc completion table)
  13150. (if (eq type :todo) (insert " ")
  13151. (if (memq type '(:tag :prop)) (insert ":"))))
  13152. (if (and (equal type :opt) (assoc completion table))
  13153. (message "%s" (substitute-command-keys
  13154. "Press \\[org-complete] again to insert example settings"))))
  13155. (t
  13156. (message "Making completion list...")
  13157. (let ((list (sort (all-completions pattern table confirm)
  13158. 'string<)))
  13159. (with-output-to-temp-buffer "*Completions*"
  13160. (condition-case nil
  13161. ;; Protection needed for XEmacs and emacs 21
  13162. (display-completion-list list pattern)
  13163. (error (display-completion-list list)))))
  13164. (message "Making completion list...%s" "done")))))))
  13165. ;;;; TODO, DEADLINE, Comments
  13166. (defun org-toggle-comment ()
  13167. "Change the COMMENT state of an entry."
  13168. (interactive)
  13169. (save-excursion
  13170. (org-back-to-heading)
  13171. (let (case-fold-search)
  13172. (if (looking-at (concat outline-regexp
  13173. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13174. (replace-match "" t t nil 1)
  13175. (if (looking-at outline-regexp)
  13176. (progn
  13177. (goto-char (match-end 0))
  13178. (insert org-comment-string " ")))))))
  13179. (defvar org-last-todo-state-is-todo nil
  13180. "This is non-nil when the last TODO state change led to a TODO state.
  13181. If the last change removed the TODO tag or switched to DONE, then
  13182. this is nil.")
  13183. (defvar org-setting-tags nil) ; dynamically skiped
  13184. ;; FIXME: better place
  13185. (defun org-property-or-variable-value (var &optional inherit)
  13186. "Check if there is a property fixing the value of VAR.
  13187. If yes, return this value. If not, return the current value of the variable."
  13188. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13189. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13190. (read prop)
  13191. (symbol-value var))))
  13192. (defun org-parse-local-options (string var)
  13193. "Parse STRING for startup setting relevant for variable VAR."
  13194. (let ((rtn (symbol-value var))
  13195. e opts)
  13196. (save-match-data
  13197. (if (or (not string) (not (string-match "\\S-" string)))
  13198. rtn
  13199. (setq opts (delq nil (mapcar (lambda (x)
  13200. (setq e (assoc x org-startup-options))
  13201. (if (eq (nth 1 e) var) e nil))
  13202. (org-split-string string "[ \t]+"))))
  13203. (if (not opts)
  13204. rtn
  13205. (setq rtn nil)
  13206. (while (setq e (pop opts))
  13207. (if (not (nth 3 e))
  13208. (setq rtn (nth 2 e))
  13209. (if (not (listp rtn)) (setq rtn nil))
  13210. (push (nth 2 e) rtn)))
  13211. rtn)))))
  13212. (defvar org-blocker-hook nil
  13213. "Hook for functions that are allowed to block a state change.
  13214. Each function gets as its single argument a property list, see
  13215. `org-trigger-hook' for more information about this list.
  13216. If any of the functions in this hook returns nil, the state change
  13217. is blocked.")
  13218. (defvar org-trigger-hook nil
  13219. "Hook for functions that are triggered by a state change.
  13220. Each function gets as its single argument a property list with at least
  13221. the following elements:
  13222. (:type type-of-change :position pos-at-entry-start
  13223. :from old-state :to new-state)
  13224. Depending on the type, more properties may be present.
  13225. This mechanism is currently implemented for:
  13226. TODO state changes
  13227. ------------------
  13228. :type todo-state-change
  13229. :from previous state (keyword as a string), or nil
  13230. :to new state (keyword as a string), or nil")
  13231. (defun org-todo (&optional arg)
  13232. "Change the TODO state of an item.
  13233. The state of an item is given by a keyword at the start of the heading,
  13234. like
  13235. *** TODO Write paper
  13236. *** DONE Call mom
  13237. The different keywords are specified in the variable `org-todo-keywords'.
  13238. By default the available states are \"TODO\" and \"DONE\".
  13239. So for this example: when the item starts with TODO, it is changed to DONE.
  13240. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13241. DONE are present, add TODO at the beginning of the heading.
  13242. With C-u prefix arg, use completion to determine the new state.
  13243. With numeric prefix arg, switch to that state.
  13244. For calling through lisp, arg is also interpreted in the following way:
  13245. 'none -> empty state
  13246. \"\"(empty string) -> switch to empty state
  13247. 'done -> switch to DONE
  13248. 'nextset -> switch to the next set of keywords
  13249. 'previousset -> switch to the previous set of keywords
  13250. \"WAITING\" -> switch to the specified keyword, but only if it
  13251. really is a member of `org-todo-keywords'."
  13252. (interactive "P")
  13253. (save-excursion
  13254. (catch 'exit
  13255. (org-back-to-heading)
  13256. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13257. (or (looking-at (concat " +" org-todo-regexp " *"))
  13258. (looking-at " *"))
  13259. (let* ((match-data (match-data))
  13260. (startpos (point-at-bol))
  13261. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13262. (org-log-done org-log-done)
  13263. (org-log-repeat org-log-repeat)
  13264. (org-todo-log-states org-todo-log-states)
  13265. (this (match-string 1))
  13266. (hl-pos (match-beginning 0))
  13267. (head (org-get-todo-sequence-head this))
  13268. (ass (assoc head org-todo-kwd-alist))
  13269. (interpret (nth 1 ass))
  13270. (done-word (nth 3 ass))
  13271. (final-done-word (nth 4 ass))
  13272. (last-state (or this ""))
  13273. (completion-ignore-case t)
  13274. (member (member this org-todo-keywords-1))
  13275. (tail (cdr member))
  13276. (state (cond
  13277. ((and org-todo-key-trigger
  13278. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13279. (and (not arg) org-use-fast-todo-selection
  13280. (not (eq org-use-fast-todo-selection 'prefix)))))
  13281. ;; Use fast selection
  13282. (org-fast-todo-selection))
  13283. ((and (equal arg '(4))
  13284. (or (not org-use-fast-todo-selection)
  13285. (not org-todo-key-trigger)))
  13286. ;; Read a state with completion
  13287. (completing-read "State: " (mapcar (lambda(x) (list x))
  13288. org-todo-keywords-1)
  13289. nil t))
  13290. ((eq arg 'right)
  13291. (if this
  13292. (if tail (car tail) nil)
  13293. (car org-todo-keywords-1)))
  13294. ((eq arg 'left)
  13295. (if (equal member org-todo-keywords-1)
  13296. nil
  13297. (if this
  13298. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13299. org-todo-keywords-1)
  13300. (org-last org-todo-keywords-1))))
  13301. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13302. (setq arg nil))) ; hack to fall back to cycling
  13303. (arg
  13304. ;; user or caller requests a specific state
  13305. (cond
  13306. ((equal arg "") nil)
  13307. ((eq arg 'none) nil)
  13308. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13309. ((eq arg 'nextset)
  13310. (or (car (cdr (member head org-todo-heads)))
  13311. (car org-todo-heads)))
  13312. ((eq arg 'previousset)
  13313. (let ((org-todo-heads (reverse org-todo-heads)))
  13314. (or (car (cdr (member head org-todo-heads)))
  13315. (car org-todo-heads))))
  13316. ((car (member arg org-todo-keywords-1)))
  13317. ((nth (1- (prefix-numeric-value arg))
  13318. org-todo-keywords-1))))
  13319. ((null member) (or head (car org-todo-keywords-1)))
  13320. ((equal this final-done-word) nil) ;; -> make empty
  13321. ((null tail) nil) ;; -> first entry
  13322. ((eq interpret 'sequence)
  13323. (car tail))
  13324. ((memq interpret '(type priority))
  13325. (if (eq this-command last-command)
  13326. (car tail)
  13327. (if (> (length tail) 0)
  13328. (or done-word (car org-done-keywords))
  13329. nil)))
  13330. (t nil)))
  13331. (next (if state (concat " " state " ") " "))
  13332. (change-plist (list :type 'todo-state-change :from this :to state
  13333. :position startpos))
  13334. dolog now-done-p)
  13335. (when org-blocker-hook
  13336. (unless (save-excursion
  13337. (save-match-data
  13338. (run-hook-with-args-until-failure
  13339. 'org-blocker-hook change-plist)))
  13340. (if (interactive-p)
  13341. (error "TODO state change from %s to %s blocked" this state)
  13342. ;; fail silently
  13343. (message "TODO state change from %s to %s blocked" this state)
  13344. (throw 'exit nil))))
  13345. (store-match-data match-data)
  13346. (replace-match next t t)
  13347. (unless (pos-visible-in-window-p hl-pos)
  13348. (message "TODO state changed to %s" (org-trim next)))
  13349. (unless head
  13350. (setq head (org-get-todo-sequence-head state)
  13351. ass (assoc head org-todo-kwd-alist)
  13352. interpret (nth 1 ass)
  13353. done-word (nth 3 ass)
  13354. final-done-word (nth 4 ass)))
  13355. (when (memq arg '(nextset previousset))
  13356. (message "Keyword-Set %d/%d: %s"
  13357. (- (length org-todo-sets) -1
  13358. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13359. (length org-todo-sets)
  13360. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13361. (setq org-last-todo-state-is-todo
  13362. (not (member state org-done-keywords)))
  13363. (setq now-done-p (and (member state org-done-keywords)
  13364. (not (member this org-done-keywords))))
  13365. (and logging (org-local-logging logging))
  13366. (when (and (or org-todo-log-states org-log-done)
  13367. (not (memq arg '(nextset previousset))))
  13368. ;; we need to look at recording a time and note
  13369. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13370. (nth 2 (assoc this org-todo-log-states))))
  13371. (when (and state
  13372. (member state org-not-done-keywords)
  13373. (not (member this org-not-done-keywords)))
  13374. ;; This is now a todo state and was not one before
  13375. ;; If there was a CLOSED time stamp, get rid of it.
  13376. (org-add-planning-info nil nil 'closed))
  13377. (when (and now-done-p org-log-done)
  13378. ;; It is now done, and it was not done before
  13379. (org-add-planning-info 'closed (org-current-time))
  13380. (if (and (not dolog) (eq 'note org-log-done))
  13381. (org-add-log-maybe 'done state 'findpos 'note)))
  13382. (when (and state dolog)
  13383. ;; This is a non-nil state, and we need to log it
  13384. (org-add-log-maybe 'state state 'findpos dolog)))
  13385. ;; Fixup tag positioning
  13386. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13387. (run-hooks 'org-after-todo-state-change-hook)
  13388. (if (and arg (not (member state org-done-keywords)))
  13389. (setq head (org-get-todo-sequence-head state)))
  13390. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13391. ;; Do we need to trigger a repeat?
  13392. (when now-done-p (org-auto-repeat-maybe state))
  13393. ;; Fixup cursor location if close to the keyword
  13394. (if (and (outline-on-heading-p)
  13395. (not (bolp))
  13396. (save-excursion (beginning-of-line 1)
  13397. (looking-at org-todo-line-regexp))
  13398. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13399. (progn
  13400. (goto-char (or (match-end 2) (match-end 1)))
  13401. (just-one-space)))
  13402. (when org-trigger-hook
  13403. (save-excursion
  13404. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13405. (defun org-local-logging (value)
  13406. "Get logging settings from a property VALUE."
  13407. (let* (words w a)
  13408. ;; directly set the variables, they are already local.
  13409. (setq org-log-done nil
  13410. org-log-repeat nil
  13411. org-todo-log-states nil)
  13412. (setq words (org-split-string value))
  13413. (while (setq w (pop words))
  13414. (cond
  13415. ((setq a (assoc w org-startup-options))
  13416. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13417. (set (nth 1 a) (nth 2 a))))
  13418. ((setq a (org-extract-log-state-settings w))
  13419. (and (member (car a) org-todo-keywords-1)
  13420. (push a org-todo-log-states)))))))
  13421. (defun org-get-todo-sequence-head (kwd)
  13422. "Return the head of the TODO sequence to which KWD belongs.
  13423. If KWD is not set, check if there is a text property remembering the
  13424. right sequence."
  13425. (let (p)
  13426. (cond
  13427. ((not kwd)
  13428. (or (get-text-property (point-at-bol) 'org-todo-head)
  13429. (progn
  13430. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13431. nil (point-at-eol)))
  13432. (get-text-property p 'org-todo-head))))
  13433. ((not (member kwd org-todo-keywords-1))
  13434. (car org-todo-keywords-1))
  13435. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13436. (defun org-fast-todo-selection ()
  13437. "Fast TODO keyword selection with single keys.
  13438. Returns the new TODO keyword, or nil if no state change should occur."
  13439. (let* ((fulltable org-todo-key-alist)
  13440. (done-keywords org-done-keywords) ;; needed for the faces.
  13441. (maxlen (apply 'max (mapcar
  13442. (lambda (x)
  13443. (if (stringp (car x)) (string-width (car x)) 0))
  13444. fulltable)))
  13445. (expert nil)
  13446. (fwidth (+ maxlen 3 1 3))
  13447. (ncol (/ (- (window-width) 4) fwidth))
  13448. tg cnt e c tbl
  13449. groups ingroup)
  13450. (save-window-excursion
  13451. (if expert
  13452. (set-buffer (get-buffer-create " *Org todo*"))
  13453. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13454. (erase-buffer)
  13455. (org-set-local 'org-done-keywords done-keywords)
  13456. (setq tbl fulltable cnt 0)
  13457. (while (setq e (pop tbl))
  13458. (cond
  13459. ((equal e '(:startgroup))
  13460. (push '() groups) (setq ingroup t)
  13461. (when (not (= cnt 0))
  13462. (setq cnt 0)
  13463. (insert "\n"))
  13464. (insert "{ "))
  13465. ((equal e '(:endgroup))
  13466. (setq ingroup nil cnt 0)
  13467. (insert "}\n"))
  13468. (t
  13469. (setq tg (car e) c (cdr e))
  13470. (if ingroup (push tg (car groups)))
  13471. (setq tg (org-add-props tg nil 'face
  13472. (org-get-todo-face tg)))
  13473. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13474. (insert "[" c "] " tg (make-string
  13475. (- fwidth 4 (length tg)) ?\ ))
  13476. (when (= (setq cnt (1+ cnt)) ncol)
  13477. (insert "\n")
  13478. (if ingroup (insert " "))
  13479. (setq cnt 0)))))
  13480. (insert "\n")
  13481. (goto-char (point-min))
  13482. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13483. (fit-window-to-buffer))
  13484. (message "[a-z..]:Set [SPC]:clear")
  13485. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13486. (cond
  13487. ((or (= c ?\C-g)
  13488. (and (= c ?q) (not (rassoc c fulltable))))
  13489. (setq quit-flag t))
  13490. ((= c ?\ ) nil)
  13491. ((setq e (rassoc c fulltable) tg (car e))
  13492. tg)
  13493. (t (setq quit-flag t))))))
  13494. (defun org-get-repeat ()
  13495. "Check if tere is a deadline/schedule with repeater in this entry."
  13496. (save-match-data
  13497. (save-excursion
  13498. (org-back-to-heading t)
  13499. (if (re-search-forward
  13500. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13501. (match-string 1)))))
  13502. (defvar org-last-changed-timestamp)
  13503. (defvar org-log-post-message)
  13504. (defvar org-log-note-purpose)
  13505. (defun org-auto-repeat-maybe (done-word)
  13506. "Check if the current headline contains a repeated deadline/schedule.
  13507. If yes, set TODO state back to what it was and change the base date
  13508. of repeating deadline/scheduled time stamps to new date.
  13509. This function should be run in the `org-after-todo-state-change-hook'."
  13510. ;; last-state is dynamically scoped into this function
  13511. (let* ((repeat (org-get-repeat))
  13512. (aa (assoc last-state org-todo-kwd-alist))
  13513. (interpret (nth 1 aa))
  13514. (head (nth 2 aa))
  13515. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13516. (msg "Entry repeats: ")
  13517. (org-log-done nil)
  13518. re type n what ts)
  13519. (when repeat
  13520. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13521. (org-todo (if (eq interpret 'type) last-state head))
  13522. (when (and org-log-repeat
  13523. (or (not (memq 'org-add-log-note
  13524. (default-value 'post-command-hook)))
  13525. (eq org-log-note-purpose 'done)))
  13526. ;; Make sure a note is taken;
  13527. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13528. 'findpos org-log-repeat))
  13529. (org-back-to-heading t)
  13530. (org-add-planning-info nil nil 'closed)
  13531. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13532. org-deadline-time-regexp "\\)\\|\\("
  13533. org-ts-regexp "\\)"))
  13534. (while (re-search-forward
  13535. re (save-excursion (outline-next-heading) (point)) t)
  13536. (setq type (if (match-end 1) org-scheduled-string
  13537. (if (match-end 3) org-deadline-string "Plain:"))
  13538. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13539. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13540. (setq n (string-to-number (match-string 1 ts))
  13541. what (match-string 2 ts))
  13542. (if (equal what "w") (setq n (* n 7) what "d"))
  13543. (org-timestamp-change n (cdr (assoc what whata)))
  13544. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13545. (setq org-log-post-message msg)
  13546. (message "%s" msg))))
  13547. (defun org-show-todo-tree (arg)
  13548. "Make a compact tree which shows all headlines marked with TODO.
  13549. The tree will show the lines where the regexp matches, and all higher
  13550. headlines above the match.
  13551. With \\[universal-argument] prefix, also show the DONE entries.
  13552. With a numeric prefix N, construct a sparse tree for the Nth element
  13553. of `org-todo-keywords-1'."
  13554. (interactive "P")
  13555. (let ((case-fold-search nil)
  13556. (kwd-re
  13557. (cond ((null arg) org-not-done-regexp)
  13558. ((equal arg '(4))
  13559. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13560. (mapcar 'list org-todo-keywords-1))))
  13561. (concat "\\("
  13562. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13563. "\\)\\>")))
  13564. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13565. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13566. org-todo-keywords-1)))
  13567. (t (error "Invalid prefix argument: %s" arg)))))
  13568. (message "%d TODO entries found"
  13569. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13570. (defun org-deadline (&optional remove)
  13571. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13572. With argument REMOVE, remove any deadline from the item."
  13573. (interactive "P")
  13574. (if remove
  13575. (progn
  13576. (org-remove-timestamp-with-keyword org-deadline-string)
  13577. (message "Item no longer has a deadline."))
  13578. (org-add-planning-info 'deadline nil 'closed)))
  13579. (defun org-schedule (&optional remove)
  13580. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13581. With argument REMOVE, remove any scheduling date from the item."
  13582. (interactive "P")
  13583. (if remove
  13584. (progn
  13585. (org-remove-timestamp-with-keyword org-scheduled-string)
  13586. (message "Item is no longer scheduled."))
  13587. (org-add-planning-info 'scheduled nil 'closed)))
  13588. (defun org-remove-timestamp-with-keyword (keyword)
  13589. "Remove all time stamps with KEYWORD in the current entry."
  13590. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13591. beg)
  13592. (save-excursion
  13593. (org-back-to-heading t)
  13594. (setq beg (point))
  13595. (org-end-of-subtree t t)
  13596. (while (re-search-backward re beg t)
  13597. (replace-match "")
  13598. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13599. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13600. (defun org-add-planning-info (what &optional time &rest remove)
  13601. "Insert new timestamp with keyword in the line directly after the headline.
  13602. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13603. If non is given, the user is prompted for a date.
  13604. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13605. be removed."
  13606. (interactive)
  13607. (let (org-time-was-given org-end-time-was-given)
  13608. (when what (setq time (or time (org-read-date nil 'to-time))))
  13609. (when (and org-insert-labeled-timestamps-at-point
  13610. (member what '(scheduled deadline)))
  13611. (insert
  13612. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13613. (org-insert-time-stamp time org-time-was-given
  13614. nil nil nil (list org-end-time-was-given))
  13615. (setq what nil))
  13616. (save-excursion
  13617. (save-restriction
  13618. (let (col list elt ts buffer-invisibility-spec)
  13619. (org-back-to-heading t)
  13620. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13621. (goto-char (match-end 1))
  13622. (setq col (current-column))
  13623. (goto-char (match-end 0))
  13624. (if (eobp) (insert "\n") (forward-char 1))
  13625. (if (and (not (looking-at outline-regexp))
  13626. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13627. "[^\r\n]*"))
  13628. (not (equal (match-string 1) org-clock-string)))
  13629. (narrow-to-region (match-beginning 0) (match-end 0))
  13630. (insert-before-markers "\n")
  13631. (backward-char 1)
  13632. (narrow-to-region (point) (point))
  13633. (indent-to-column col))
  13634. ;; Check if we have to remove something.
  13635. (setq list (cons what remove))
  13636. (while list
  13637. (setq elt (pop list))
  13638. (goto-char (point-min))
  13639. (when (or (and (eq elt 'scheduled)
  13640. (re-search-forward org-scheduled-time-regexp nil t))
  13641. (and (eq elt 'deadline)
  13642. (re-search-forward org-deadline-time-regexp nil t))
  13643. (and (eq elt 'closed)
  13644. (re-search-forward org-closed-time-regexp nil t)))
  13645. (replace-match "")
  13646. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13647. (if (looking-at " +") (replace-match ""))))
  13648. (goto-char (point-max))
  13649. (when what
  13650. (insert
  13651. (if (not (equal (char-before) ?\ )) " " "")
  13652. (cond ((eq what 'scheduled) org-scheduled-string)
  13653. ((eq what 'deadline) org-deadline-string)
  13654. ((eq what 'closed) org-closed-string))
  13655. " ")
  13656. (setq ts (org-insert-time-stamp
  13657. time
  13658. (or org-time-was-given
  13659. (and (eq what 'closed) org-log-done-with-time))
  13660. (eq what 'closed)
  13661. nil nil (list org-end-time-was-given)))
  13662. (end-of-line 1))
  13663. (goto-char (point-min))
  13664. (widen)
  13665. (if (looking-at "[ \t]+\r?\n")
  13666. (replace-match ""))
  13667. ts)))))
  13668. (defvar org-log-note-marker (make-marker))
  13669. (defvar org-log-note-purpose nil)
  13670. (defvar org-log-note-state nil)
  13671. (defvar org-log-note-how nil)
  13672. (defvar org-log-note-window-configuration nil)
  13673. (defvar org-log-note-return-to (make-marker))
  13674. (defvar org-log-post-message nil
  13675. "Message to be displayed after a log note has been stored.
  13676. The auto-repeater uses this.")
  13677. (defun org-add-log-maybe (&optional purpose state findpos how)
  13678. "Set up the post command hook to take a note.
  13679. If this is about to TODO state change, the new state is expected in STATE.
  13680. When FINDPOS is non-nil, find the correct position for the note in
  13681. the current entry. If not, assume that it can be inserted at point."
  13682. (save-excursion
  13683. (when findpos
  13684. (org-back-to-heading t)
  13685. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13686. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13687. "[^\r\n]*\\)?"))
  13688. (goto-char (match-end 0))
  13689. (unless org-log-states-order-reversed
  13690. (and (= (char-after) ?\n) (forward-char 1))
  13691. (org-skip-over-state-notes)
  13692. (skip-chars-backward " \t\n\r")))
  13693. (move-marker org-log-note-marker (point))
  13694. (setq org-log-note-purpose purpose
  13695. org-log-note-state state
  13696. org-log-note-how how)
  13697. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13698. (defun org-skip-over-state-notes ()
  13699. "Skip past the list of State notes in an entry."
  13700. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13701. (while (looking-at "[ \t]*- State")
  13702. (condition-case nil
  13703. (org-next-item)
  13704. (error (org-end-of-item)))))
  13705. (defun org-add-log-note (&optional purpose)
  13706. "Pop up a window for taking a note, and add this note later at point."
  13707. (remove-hook 'post-command-hook 'org-add-log-note)
  13708. (setq org-log-note-window-configuration (current-window-configuration))
  13709. (delete-other-windows)
  13710. (move-marker org-log-note-return-to (point))
  13711. (switch-to-buffer (marker-buffer org-log-note-marker))
  13712. (goto-char org-log-note-marker)
  13713. (org-switch-to-buffer-other-window "*Org Note*")
  13714. (erase-buffer)
  13715. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13716. (org-store-log-note)
  13717. (let ((org-inhibit-startup t)) (org-mode))
  13718. (insert (format "# Insert note for %s.
  13719. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13720. (cond
  13721. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13722. ((eq org-log-note-purpose 'done) "closed todo item")
  13723. ((eq org-log-note-purpose 'state)
  13724. (format "state change to \"%s\"" org-log-note-state))
  13725. (t (error "This should not happen")))))
  13726. (org-set-local 'org-finish-function 'org-store-log-note)))
  13727. (defun org-store-log-note ()
  13728. "Finish taking a log note, and insert it to where it belongs."
  13729. (let ((txt (buffer-string))
  13730. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13731. lines ind)
  13732. (kill-buffer (current-buffer))
  13733. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13734. (setq txt (replace-match "" t t txt)))
  13735. (if (string-match "\\s-+\\'" txt)
  13736. (setq txt (replace-match "" t t txt)))
  13737. (setq lines (org-split-string txt "\n"))
  13738. (when (and note (string-match "\\S-" note))
  13739. (setq note
  13740. (org-replace-escapes
  13741. note
  13742. (list (cons "%u" (user-login-name))
  13743. (cons "%U" user-full-name)
  13744. (cons "%t" (format-time-string
  13745. (org-time-stamp-format 'long 'inactive)
  13746. (current-time)))
  13747. (cons "%s" (if org-log-note-state
  13748. (concat "\"" org-log-note-state "\"")
  13749. "")))))
  13750. (if lines (setq note (concat note " \\\\")))
  13751. (push note lines))
  13752. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13753. (when lines
  13754. (save-excursion
  13755. (set-buffer (marker-buffer org-log-note-marker))
  13756. (save-excursion
  13757. (goto-char org-log-note-marker)
  13758. (move-marker org-log-note-marker nil)
  13759. (end-of-line 1)
  13760. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13761. (indent-relative nil)
  13762. (insert "- " (pop lines))
  13763. (org-indent-line-function)
  13764. (beginning-of-line 1)
  13765. (looking-at "[ \t]*")
  13766. (setq ind (concat (match-string 0) " "))
  13767. (end-of-line 1)
  13768. (while lines (insert "\n" ind (pop lines)))))))
  13769. (set-window-configuration org-log-note-window-configuration)
  13770. (with-current-buffer (marker-buffer org-log-note-return-to)
  13771. (goto-char org-log-note-return-to))
  13772. (move-marker org-log-note-return-to nil)
  13773. (and org-log-post-message (message "%s" org-log-post-message)))
  13774. ;; FIXME: what else would be useful?
  13775. ;; - priority
  13776. ;; - date
  13777. (defun org-sparse-tree (&optional arg)
  13778. "Create a sparse tree, prompt for the details.
  13779. This command can create sparse trees. You first need to select the type
  13780. of match used to create the tree:
  13781. t Show entries with a specific TODO keyword.
  13782. T Show entries selected by a tags match.
  13783. p Enter a property name and its value (both with completion on existing
  13784. names/values) and show entries with that property.
  13785. r Show entries matching a regular expression
  13786. d Show deadlines due within `org-deadline-warning-days'."
  13787. (interactive "P")
  13788. (let (ans kwd value)
  13789. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13790. (setq ans (read-char-exclusive))
  13791. (cond
  13792. ((equal ans ?d)
  13793. (call-interactively 'org-check-deadlines))
  13794. ((equal ans ?b)
  13795. (call-interactively 'org-check-before-date))
  13796. ((equal ans ?t)
  13797. (org-show-todo-tree '(4)))
  13798. ((equal ans ?T)
  13799. (call-interactively 'org-tags-sparse-tree))
  13800. ((member ans '(?p ?P))
  13801. (setq kwd (completing-read "Property: "
  13802. (mapcar 'list (org-buffer-property-keys))))
  13803. (setq value (completing-read "Value: "
  13804. (mapcar 'list (org-property-values kwd))))
  13805. (unless (string-match "\\`{.*}\\'" value)
  13806. (setq value (concat "\"" value "\"")))
  13807. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13808. ((member ans '(?r ?R ?/))
  13809. (call-interactively 'org-occur))
  13810. (t (error "No such sparse tree command \"%c\"" ans)))))
  13811. (defvar org-occur-highlights nil)
  13812. (make-variable-buffer-local 'org-occur-highlights)
  13813. (defun org-occur (regexp &optional keep-previous callback)
  13814. "Make a compact tree which shows all matches of REGEXP.
  13815. The tree will show the lines where the regexp matches, and all higher
  13816. headlines above the match. It will also show the heading after the match,
  13817. to make sure editing the matching entry is easy.
  13818. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13819. call to `org-occur' will be kept, to allow stacking of calls to this
  13820. command.
  13821. If CALLBACK is non-nil, it is a function which is called to confirm
  13822. that the match should indeed be shown."
  13823. (interactive "sRegexp: \nP")
  13824. (or keep-previous (org-remove-occur-highlights nil nil t))
  13825. (let ((cnt 0))
  13826. (save-excursion
  13827. (goto-char (point-min))
  13828. (if (or (not keep-previous) ; do not want to keep
  13829. (not org-occur-highlights)) ; no previous matches
  13830. ;; hide everything
  13831. (org-overview))
  13832. (while (re-search-forward regexp nil t)
  13833. (when (or (not callback)
  13834. (save-match-data (funcall callback)))
  13835. (setq cnt (1+ cnt))
  13836. (when org-highlight-sparse-tree-matches
  13837. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13838. (org-show-context 'occur-tree))))
  13839. (when org-remove-highlights-with-change
  13840. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13841. nil 'local))
  13842. (unless org-sparse-tree-open-archived-trees
  13843. (org-hide-archived-subtrees (point-min) (point-max)))
  13844. (run-hooks 'org-occur-hook)
  13845. (if (interactive-p)
  13846. (message "%d match(es) for regexp %s" cnt regexp))
  13847. cnt))
  13848. (defun org-show-context (&optional key)
  13849. "Make sure point and context and visible.
  13850. How much context is shown depends upon the variables
  13851. `org-show-hierarchy-above', `org-show-following-heading'. and
  13852. `org-show-siblings'."
  13853. (let ((heading-p (org-on-heading-p t))
  13854. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13855. (following-p (org-get-alist-option org-show-following-heading key))
  13856. (entry-p (org-get-alist-option org-show-entry-below key))
  13857. (siblings-p (org-get-alist-option org-show-siblings key)))
  13858. (catch 'exit
  13859. ;; Show heading or entry text
  13860. (if (and heading-p (not entry-p))
  13861. (org-flag-heading nil) ; only show the heading
  13862. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13863. (org-show-hidden-entry))) ; show entire entry
  13864. (when following-p
  13865. ;; Show next sibling, or heading below text
  13866. (save-excursion
  13867. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13868. (org-flag-heading nil))))
  13869. (when siblings-p (org-show-siblings))
  13870. (when hierarchy-p
  13871. ;; show all higher headings, possibly with siblings
  13872. (save-excursion
  13873. (while (and (condition-case nil
  13874. (progn (org-up-heading-all 1) t)
  13875. (error nil))
  13876. (not (bobp)))
  13877. (org-flag-heading nil)
  13878. (when siblings-p (org-show-siblings))))))))
  13879. (defun org-reveal (&optional siblings)
  13880. "Show current entry, hierarchy above it, and the following headline.
  13881. This can be used to show a consistent set of context around locations
  13882. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13883. not t for the search context.
  13884. With optional argument SIBLINGS, on each level of the hierarchy all
  13885. siblings are shown. This repairs the tree structure to what it would
  13886. look like when opened with hierarchical calls to `org-cycle'."
  13887. (interactive "P")
  13888. (let ((org-show-hierarchy-above t)
  13889. (org-show-following-heading t)
  13890. (org-show-siblings (if siblings t org-show-siblings)))
  13891. (org-show-context nil)))
  13892. (defun org-highlight-new-match (beg end)
  13893. "Highlight from BEG to END and mark the highlight is an occur headline."
  13894. (let ((ov (org-make-overlay beg end)))
  13895. (org-overlay-put ov 'face 'secondary-selection)
  13896. (push ov org-occur-highlights)))
  13897. (defun org-remove-occur-highlights (&optional beg end noremove)
  13898. "Remove the occur highlights from the buffer.
  13899. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13900. from the `before-change-functions' in the current buffer."
  13901. (interactive)
  13902. (unless org-inhibit-highlight-removal
  13903. (mapc 'org-delete-overlay org-occur-highlights)
  13904. (setq org-occur-highlights nil)
  13905. (unless noremove
  13906. (remove-hook 'before-change-functions
  13907. 'org-remove-occur-highlights 'local))))
  13908. ;;;; Priorities
  13909. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13910. "Regular expression matching the priority indicator.")
  13911. (defvar org-remove-priority-next-time nil)
  13912. (defun org-priority-up ()
  13913. "Increase the priority of the current item."
  13914. (interactive)
  13915. (org-priority 'up))
  13916. (defun org-priority-down ()
  13917. "Decrease the priority of the current item."
  13918. (interactive)
  13919. (org-priority 'down))
  13920. (defun org-priority (&optional action)
  13921. "Change the priority of an item by ARG.
  13922. ACTION can be `set', `up', `down', or a character."
  13923. (interactive)
  13924. (setq action (or action 'set))
  13925. (let (current new news have remove)
  13926. (save-excursion
  13927. (org-back-to-heading)
  13928. (if (looking-at org-priority-regexp)
  13929. (setq current (string-to-char (match-string 2))
  13930. have t)
  13931. (setq current org-default-priority))
  13932. (cond
  13933. ((or (eq action 'set) (integerp action))
  13934. (if (integerp action)
  13935. (setq new action)
  13936. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13937. (setq new (read-char-exclusive)))
  13938. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13939. (= (upcase org-lowest-priority) org-lowest-priority))
  13940. (setq new (upcase new)))
  13941. (cond ((equal new ?\ ) (setq remove t))
  13942. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13943. (error "Priority must be between `%c' and `%c'"
  13944. org-highest-priority org-lowest-priority))))
  13945. ((eq action 'up)
  13946. (if (and (not have) (eq last-command this-command))
  13947. (setq new org-lowest-priority)
  13948. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13949. org-default-priority (1- current)))))
  13950. ((eq action 'down)
  13951. (if (and (not have) (eq last-command this-command))
  13952. (setq new org-highest-priority)
  13953. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13954. org-default-priority (1+ current)))))
  13955. (t (error "Invalid action")))
  13956. (if (or (< (upcase new) org-highest-priority)
  13957. (> (upcase new) org-lowest-priority))
  13958. (setq remove t))
  13959. (setq news (format "%c" new))
  13960. (if have
  13961. (if remove
  13962. (replace-match "" t t nil 1)
  13963. (replace-match news t t nil 2))
  13964. (if remove
  13965. (error "No priority cookie found in line")
  13966. (looking-at org-todo-line-regexp)
  13967. (if (match-end 2)
  13968. (progn
  13969. (goto-char (match-end 2))
  13970. (insert " [#" news "]"))
  13971. (goto-char (match-beginning 3))
  13972. (insert "[#" news "] ")))))
  13973. (org-preserve-lc (org-set-tags nil 'align))
  13974. (if remove
  13975. (message "Priority removed")
  13976. (message "Priority of current item set to %s" news))))
  13977. (defun org-get-priority (s)
  13978. "Find priority cookie and return priority."
  13979. (save-match-data
  13980. (if (not (string-match org-priority-regexp s))
  13981. (* 1000 (- org-lowest-priority org-default-priority))
  13982. (* 1000 (- org-lowest-priority
  13983. (string-to-char (match-string 2 s)))))))
  13984. ;;;; Tags
  13985. (defun org-scan-tags (action matcher &optional todo-only)
  13986. "Scan headline tags with inheritance and produce output ACTION.
  13987. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13988. evaluated, testing if a given set of tags qualifies a headline for
  13989. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13990. are included in the output."
  13991. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13992. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13993. (org-re
  13994. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13995. (props (list 'face nil
  13996. 'done-face 'org-done
  13997. 'undone-face nil
  13998. 'mouse-face 'highlight
  13999. 'org-not-done-regexp org-not-done-regexp
  14000. 'org-todo-regexp org-todo-regexp
  14001. 'keymap org-agenda-keymap
  14002. 'help-echo
  14003. (format "mouse-2 or RET jump to org file %s"
  14004. (abbreviate-file-name
  14005. (or (buffer-file-name (buffer-base-buffer))
  14006. (buffer-name (buffer-base-buffer)))))))
  14007. (case-fold-search nil)
  14008. lspos
  14009. tags tags-list tags-alist (llast 0) rtn level category i txt
  14010. todo marker entry priority)
  14011. (save-excursion
  14012. (goto-char (point-min))
  14013. (when (eq action 'sparse-tree)
  14014. (org-overview)
  14015. (org-remove-occur-highlights))
  14016. (while (re-search-forward re nil t)
  14017. (catch :skip
  14018. (setq todo (if (match-end 1) (match-string 2))
  14019. tags (if (match-end 4) (match-string 4)))
  14020. (goto-char (setq lspos (1+ (match-beginning 0))))
  14021. (setq level (org-reduced-level (funcall outline-level))
  14022. category (org-get-category))
  14023. (setq i llast llast level)
  14024. ;; remove tag lists from same and sublevels
  14025. (while (>= i level)
  14026. (when (setq entry (assoc i tags-alist))
  14027. (setq tags-alist (delete entry tags-alist)))
  14028. (setq i (1- i)))
  14029. ;; add the nex tags
  14030. (when tags
  14031. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14032. tags-alist
  14033. (cons (cons level tags) tags-alist)))
  14034. ;; compile tags for current headline
  14035. (setq tags-list
  14036. (if org-use-tag-inheritance
  14037. (apply 'append (mapcar 'cdr tags-alist))
  14038. tags))
  14039. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14040. (eval matcher)
  14041. (or (not org-agenda-skip-archived-trees)
  14042. (not (member org-archive-tag tags-list))))
  14043. (and (eq action 'agenda) (org-agenda-skip))
  14044. ;; list this headline
  14045. (if (eq action 'sparse-tree)
  14046. (progn
  14047. (and org-highlight-sparse-tree-matches
  14048. (org-get-heading) (match-end 0)
  14049. (org-highlight-new-match
  14050. (match-beginning 0) (match-beginning 1)))
  14051. (org-show-context 'tags-tree))
  14052. (setq txt (org-format-agenda-item
  14053. ""
  14054. (concat
  14055. (if org-tags-match-list-sublevels
  14056. (make-string (1- level) ?.) "")
  14057. (org-get-heading))
  14058. category tags-list)
  14059. priority (org-get-priority txt))
  14060. (goto-char lspos)
  14061. (setq marker (org-agenda-new-marker))
  14062. (org-add-props txt props
  14063. 'org-marker marker 'org-hd-marker marker 'org-category category
  14064. 'priority priority 'type "tagsmatch")
  14065. (push txt rtn))
  14066. ;; if we are to skip sublevels, jump to end of subtree
  14067. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14068. (when (and (eq action 'sparse-tree)
  14069. (not org-sparse-tree-open-archived-trees))
  14070. (org-hide-archived-subtrees (point-min) (point-max)))
  14071. (nreverse rtn)))
  14072. (defvar todo-only) ;; dynamically scoped
  14073. (defun org-tags-sparse-tree (&optional todo-only match)
  14074. "Create a sparse tree according to tags string MATCH.
  14075. MATCH can contain positive and negative selection of tags, like
  14076. \"+WORK+URGENT-WITHBOSS\".
  14077. If optional argument TODO_ONLY is non-nil, only select lines that are
  14078. also TODO lines."
  14079. (interactive "P")
  14080. (org-prepare-agenda-buffers (list (current-buffer)))
  14081. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14082. (defvar org-cached-props nil)
  14083. (defun org-cached-entry-get (pom property)
  14084. (if (or (eq t org-use-property-inheritance)
  14085. (member property org-use-property-inheritance))
  14086. ;; Caching is not possible, check it directly
  14087. (org-entry-get pom property 'inherit)
  14088. ;; Get all properties, so that we can do complicated checks easily
  14089. (cdr (assoc property (or org-cached-props
  14090. (setq org-cached-props
  14091. (org-entry-properties pom)))))))
  14092. (defun org-global-tags-completion-table (&optional files)
  14093. "Return the list of all tags in all agenda buffer/files."
  14094. (save-excursion
  14095. (org-uniquify
  14096. (delq nil
  14097. (apply 'append
  14098. (mapcar
  14099. (lambda (file)
  14100. (set-buffer (find-file-noselect file))
  14101. (append (org-get-buffer-tags)
  14102. (mapcar (lambda (x) (if (stringp (car-safe x))
  14103. (list (car-safe x)) nil))
  14104. org-tag-alist)))
  14105. (if (and files (car files))
  14106. files
  14107. (org-agenda-files))))))))
  14108. (defun org-make-tags-matcher (match)
  14109. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14110. ;; todo-only is scoped dynamically into this function, and the function
  14111. ;; may change it it the matcher asksk for it.
  14112. (unless match
  14113. ;; Get a new match request, with completion
  14114. (let ((org-last-tags-completion-table
  14115. (org-global-tags-completion-table)))
  14116. (setq match (completing-read
  14117. "Match: " 'org-tags-completion-function nil nil nil
  14118. 'org-tags-history))))
  14119. ;; Parse the string and create a lisp form
  14120. (let ((match0 match)
  14121. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14122. minus tag mm
  14123. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14124. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14125. (if (string-match "/+" match)
  14126. ;; match contains also a todo-matching request
  14127. (progn
  14128. (setq tagsmatch (substring match 0 (match-beginning 0))
  14129. todomatch (substring match (match-end 0)))
  14130. (if (string-match "^!" todomatch)
  14131. (setq todo-only t todomatch (substring todomatch 1)))
  14132. (if (string-match "^\\s-*$" todomatch)
  14133. (setq todomatch nil)))
  14134. ;; only matching tags
  14135. (setq tagsmatch match todomatch nil))
  14136. ;; Make the tags matcher
  14137. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14138. (setq tagsmatcher t)
  14139. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14140. (while (setq term (pop orterms))
  14141. (while (and (equal (substring term -1) "\\") orterms)
  14142. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14143. (while (string-match re term)
  14144. (setq minus (and (match-end 1)
  14145. (equal (match-string 1 term) "-"))
  14146. tag (match-string 2 term)
  14147. re-p (equal (string-to-char tag) ?{)
  14148. level-p (match-end 3)
  14149. prop-p (match-end 4)
  14150. mm (cond
  14151. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14152. (level-p `(= level ,(string-to-number
  14153. (match-string 3 term))))
  14154. (prop-p
  14155. (setq pn (match-string 4 term)
  14156. pv (match-string 5 term)
  14157. cat-p (equal pn "CATEGORY")
  14158. re-p (equal (string-to-char pv) ?{)
  14159. pv (substring pv 1 -1))
  14160. (if (equal pn "CATEGORY")
  14161. (setq gv '(get-text-property (point) 'org-category))
  14162. (setq gv `(org-cached-entry-get nil ,pn)))
  14163. (if re-p
  14164. `(string-match ,pv (or ,gv ""))
  14165. `(equal ,pv (or ,gv ""))))
  14166. (t `(member ,(downcase tag) tags-list)))
  14167. mm (if minus (list 'not mm) mm)
  14168. term (substring term (match-end 0)))
  14169. (push mm tagsmatcher))
  14170. (push (if (> (length tagsmatcher) 1)
  14171. (cons 'and tagsmatcher)
  14172. (car tagsmatcher))
  14173. orlist)
  14174. (setq tagsmatcher nil))
  14175. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14176. (setq tagsmatcher
  14177. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14178. ;; Make the todo matcher
  14179. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14180. (setq todomatcher t)
  14181. (setq orterms (org-split-string todomatch "|") orlist nil)
  14182. (while (setq term (pop orterms))
  14183. (while (string-match re term)
  14184. (setq minus (and (match-end 1)
  14185. (equal (match-string 1 term) "-"))
  14186. kwd (match-string 2 term)
  14187. re-p (equal (string-to-char kwd) ?{)
  14188. term (substring term (match-end 0))
  14189. mm (if re-p
  14190. `(string-match ,(substring kwd 1 -1) todo)
  14191. (list 'equal 'todo kwd))
  14192. mm (if minus (list 'not mm) mm))
  14193. (push mm todomatcher))
  14194. (push (if (> (length todomatcher) 1)
  14195. (cons 'and todomatcher)
  14196. (car todomatcher))
  14197. orlist)
  14198. (setq todomatcher nil))
  14199. (setq todomatcher (if (> (length orlist) 1)
  14200. (cons 'or orlist) (car orlist))))
  14201. ;; Return the string and lisp forms of the matcher
  14202. (setq matcher (if todomatcher
  14203. (list 'and tagsmatcher todomatcher)
  14204. tagsmatcher))
  14205. (cons match0 matcher)))
  14206. (defun org-match-any-p (re list)
  14207. "Does re match any element of list?"
  14208. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14209. (delq nil list))
  14210. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14211. (defvar org-tags-overlay (org-make-overlay 1 1))
  14212. (org-detach-overlay org-tags-overlay)
  14213. (defun org-align-tags-here (to-col)
  14214. ;; Assumes that this is a headline
  14215. (let ((pos (point)) (col (current-column)) tags)
  14216. (beginning-of-line 1)
  14217. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14218. (< pos (match-beginning 2)))
  14219. (progn
  14220. (setq tags (match-string 2))
  14221. (goto-char (match-beginning 1))
  14222. (insert " ")
  14223. (delete-region (point) (1+ (match-end 0)))
  14224. (backward-char 1)
  14225. (move-to-column
  14226. (max (1+ (current-column))
  14227. (1+ col)
  14228. (if (> to-col 0)
  14229. to-col
  14230. (- (abs to-col) (length tags))))
  14231. t)
  14232. (insert tags)
  14233. (move-to-column (min (current-column) col) t))
  14234. (goto-char pos))))
  14235. (defun org-set-tags (&optional arg just-align)
  14236. "Set the tags for the current headline.
  14237. With prefix ARG, realign all tags in headings in the current buffer."
  14238. (interactive "P")
  14239. (let* ((re (concat "^" outline-regexp))
  14240. (current (org-get-tags-string))
  14241. (col (current-column))
  14242. (org-setting-tags t)
  14243. table current-tags inherited-tags ; computed below when needed
  14244. tags p0 c0 c1 rpl)
  14245. (if arg
  14246. (save-excursion
  14247. (goto-char (point-min))
  14248. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14249. (while (re-search-forward re nil t)
  14250. (org-set-tags nil t)
  14251. (end-of-line 1)))
  14252. (message "All tags realigned to column %d" org-tags-column))
  14253. (if just-align
  14254. (setq tags current)
  14255. ;; Get a new set of tags from the user
  14256. (save-excursion
  14257. (setq table (or org-tag-alist (org-get-buffer-tags))
  14258. org-last-tags-completion-table table
  14259. current-tags (org-split-string current ":")
  14260. inherited-tags (nreverse
  14261. (nthcdr (length current-tags)
  14262. (nreverse (org-get-tags-at))))
  14263. tags
  14264. (if (or (eq t org-use-fast-tag-selection)
  14265. (and org-use-fast-tag-selection
  14266. (delq nil (mapcar 'cdr table))))
  14267. (org-fast-tag-selection
  14268. current-tags inherited-tags table
  14269. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14270. (let ((org-add-colon-after-tag-completion t))
  14271. (org-trim
  14272. (org-without-partial-completion
  14273. (completing-read "Tags: " 'org-tags-completion-function
  14274. nil nil current 'org-tags-history)))))))
  14275. (while (string-match "[-+&]+" tags)
  14276. ;; No boolean logic, just a list
  14277. (setq tags (replace-match ":" t t tags))))
  14278. (if (string-match "\\`[\t ]*\\'" tags)
  14279. (setq tags "")
  14280. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14281. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14282. ;; Insert new tags at the correct column
  14283. (beginning-of-line 1)
  14284. (cond
  14285. ((and (equal current "") (equal tags "")))
  14286. ((re-search-forward
  14287. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14288. (point-at-eol) t)
  14289. (if (equal tags "")
  14290. (setq rpl "")
  14291. (goto-char (match-beginning 0))
  14292. (setq c0 (current-column) p0 (point)
  14293. c1 (max (1+ c0) (if (> org-tags-column 0)
  14294. org-tags-column
  14295. (- (- org-tags-column) (length tags))))
  14296. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14297. (replace-match rpl t t)
  14298. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14299. tags)
  14300. (t (error "Tags alignment failed")))
  14301. (move-to-column col)
  14302. (unless just-align
  14303. (run-hooks 'org-after-tags-change-hook)))))
  14304. (defun org-change-tag-in-region (beg end tag off)
  14305. "Add or remove TAG for each entry in the region.
  14306. This works in the agenda, and also in an org-mode buffer."
  14307. (interactive
  14308. (list (region-beginning) (region-end)
  14309. (let ((org-last-tags-completion-table
  14310. (if (org-mode-p)
  14311. (org-get-buffer-tags)
  14312. (org-global-tags-completion-table))))
  14313. (completing-read
  14314. "Tag: " 'org-tags-completion-function nil nil nil
  14315. 'org-tags-history))
  14316. (progn
  14317. (message "[s]et or [r]emove? ")
  14318. (equal (read-char-exclusive) ?r))))
  14319. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14320. (let ((agendap (equal major-mode 'org-agenda-mode))
  14321. l1 l2 m buf pos newhead (cnt 0))
  14322. (goto-char end)
  14323. (setq l2 (1- (org-current-line)))
  14324. (goto-char beg)
  14325. (setq l1 (org-current-line))
  14326. (loop for l from l1 to l2 do
  14327. (goto-line l)
  14328. (setq m (get-text-property (point) 'org-hd-marker))
  14329. (when (or (and (org-mode-p) (org-on-heading-p))
  14330. (and agendap m))
  14331. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14332. pos (if agendap m (point)))
  14333. (with-current-buffer buf
  14334. (save-excursion
  14335. (save-restriction
  14336. (goto-char pos)
  14337. (setq cnt (1+ cnt))
  14338. (org-toggle-tag tag (if off 'off 'on))
  14339. (setq newhead (org-get-heading)))))
  14340. (and agendap (org-agenda-change-all-lines newhead m))))
  14341. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14342. (defun org-tags-completion-function (string predicate &optional flag)
  14343. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14344. (confirm (lambda (x) (stringp (car x)))))
  14345. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14346. (setq s1 (match-string 1 string)
  14347. s2 (match-string 2 string))
  14348. (setq s1 "" s2 string))
  14349. (cond
  14350. ((eq flag nil)
  14351. ;; try completion
  14352. (setq rtn (try-completion s2 ctable confirm))
  14353. (if (stringp rtn)
  14354. (setq rtn
  14355. (concat s1 s2 (substring rtn (length s2))
  14356. (if (and org-add-colon-after-tag-completion
  14357. (assoc rtn ctable))
  14358. ":" ""))))
  14359. rtn)
  14360. ((eq flag t)
  14361. ;; all-completions
  14362. (all-completions s2 ctable confirm)
  14363. )
  14364. ((eq flag 'lambda)
  14365. ;; exact match?
  14366. (assoc s2 ctable)))
  14367. ))
  14368. (defun org-fast-tag-insert (kwd tags face &optional end)
  14369. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14370. (insert (format "%-12s" (concat kwd ":"))
  14371. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14372. (or end "")))
  14373. (defun org-fast-tag-show-exit (flag)
  14374. (save-excursion
  14375. (goto-line 3)
  14376. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14377. (replace-match ""))
  14378. (when flag
  14379. (end-of-line 1)
  14380. (move-to-column (- (window-width) 19) t)
  14381. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14382. (defun org-set-current-tags-overlay (current prefix)
  14383. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14384. (if (featurep 'xemacs)
  14385. (org-overlay-display org-tags-overlay (concat prefix s)
  14386. 'secondary-selection)
  14387. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14388. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14389. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14390. "Fast tag selection with single keys.
  14391. CURRENT is the current list of tags in the headline, INHERITED is the
  14392. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14393. possibly with grouping information. TODO-TABLE is a similar table with
  14394. TODO keywords, should these have keys assigned to them.
  14395. If the keys are nil, a-z are automatically assigned.
  14396. Returns the new tags string, or nil to not change the current settings."
  14397. (let* ((fulltable (append table todo-table))
  14398. (maxlen (apply 'max (mapcar
  14399. (lambda (x)
  14400. (if (stringp (car x)) (string-width (car x)) 0))
  14401. fulltable)))
  14402. (buf (current-buffer))
  14403. (expert (eq org-fast-tag-selection-single-key 'expert))
  14404. (buffer-tags nil)
  14405. (fwidth (+ maxlen 3 1 3))
  14406. (ncol (/ (- (window-width) 4) fwidth))
  14407. (i-face 'org-done)
  14408. (c-face 'org-todo)
  14409. tg cnt e c char c1 c2 ntable tbl rtn
  14410. ov-start ov-end ov-prefix
  14411. (exit-after-next org-fast-tag-selection-single-key)
  14412. (done-keywords org-done-keywords)
  14413. groups ingroup)
  14414. (save-excursion
  14415. (beginning-of-line 1)
  14416. (if (looking-at
  14417. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14418. (setq ov-start (match-beginning 1)
  14419. ov-end (match-end 1)
  14420. ov-prefix "")
  14421. (setq ov-start (1- (point-at-eol))
  14422. ov-end (1+ ov-start))
  14423. (skip-chars-forward "^\n\r")
  14424. (setq ov-prefix
  14425. (concat
  14426. (buffer-substring (1- (point)) (point))
  14427. (if (> (current-column) org-tags-column)
  14428. " "
  14429. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14430. (org-move-overlay org-tags-overlay ov-start ov-end)
  14431. (save-window-excursion
  14432. (if expert
  14433. (set-buffer (get-buffer-create " *Org tags*"))
  14434. (delete-other-windows)
  14435. (split-window-vertically)
  14436. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14437. (erase-buffer)
  14438. (org-set-local 'org-done-keywords done-keywords)
  14439. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14440. (org-fast-tag-insert "Current" current c-face "\n\n")
  14441. (org-fast-tag-show-exit exit-after-next)
  14442. (org-set-current-tags-overlay current ov-prefix)
  14443. (setq tbl fulltable char ?a cnt 0)
  14444. (while (setq e (pop tbl))
  14445. (cond
  14446. ((equal e '(:startgroup))
  14447. (push '() groups) (setq ingroup t)
  14448. (when (not (= cnt 0))
  14449. (setq cnt 0)
  14450. (insert "\n"))
  14451. (insert "{ "))
  14452. ((equal e '(:endgroup))
  14453. (setq ingroup nil cnt 0)
  14454. (insert "}\n"))
  14455. (t
  14456. (setq tg (car e) c2 nil)
  14457. (if (cdr e)
  14458. (setq c (cdr e))
  14459. ;; automatically assign a character.
  14460. (setq c1 (string-to-char
  14461. (downcase (substring
  14462. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14463. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14464. (while (or (rassoc char ntable) (rassoc char table))
  14465. (setq char (1+ char)))
  14466. (setq c2 c1))
  14467. (setq c (or c2 char)))
  14468. (if ingroup (push tg (car groups)))
  14469. (setq tg (org-add-props tg nil 'face
  14470. (cond
  14471. ((not (assoc tg table))
  14472. (org-get-todo-face tg))
  14473. ((member tg current) c-face)
  14474. ((member tg inherited) i-face)
  14475. (t nil))))
  14476. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14477. (insert "[" c "] " tg (make-string
  14478. (- fwidth 4 (length tg)) ?\ ))
  14479. (push (cons tg c) ntable)
  14480. (when (= (setq cnt (1+ cnt)) ncol)
  14481. (insert "\n")
  14482. (if ingroup (insert " "))
  14483. (setq cnt 0)))))
  14484. (setq ntable (nreverse ntable))
  14485. (insert "\n")
  14486. (goto-char (point-min))
  14487. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14488. (fit-window-to-buffer))
  14489. (setq rtn
  14490. (catch 'exit
  14491. (while t
  14492. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14493. (if groups " [!] no groups" " [!]groups")
  14494. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14495. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14496. (cond
  14497. ((= c ?\r) (throw 'exit t))
  14498. ((= c ?!)
  14499. (setq groups (not groups))
  14500. (goto-char (point-min))
  14501. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14502. ((= c ?\C-c)
  14503. (if (not expert)
  14504. (org-fast-tag-show-exit
  14505. (setq exit-after-next (not exit-after-next)))
  14506. (setq expert nil)
  14507. (delete-other-windows)
  14508. (split-window-vertically)
  14509. (org-switch-to-buffer-other-window " *Org tags*")
  14510. (and (fboundp 'fit-window-to-buffer)
  14511. (fit-window-to-buffer))))
  14512. ((or (= c ?\C-g)
  14513. (and (= c ?q) (not (rassoc c ntable))))
  14514. (org-detach-overlay org-tags-overlay)
  14515. (setq quit-flag t))
  14516. ((= c ?\ )
  14517. (setq current nil)
  14518. (if exit-after-next (setq exit-after-next 'now)))
  14519. ((= c ?\t)
  14520. (condition-case nil
  14521. (setq tg (completing-read
  14522. "Tag: "
  14523. (or buffer-tags
  14524. (with-current-buffer buf
  14525. (org-get-buffer-tags)))))
  14526. (quit (setq tg "")))
  14527. (when (string-match "\\S-" tg)
  14528. (add-to-list 'buffer-tags (list tg))
  14529. (if (member tg current)
  14530. (setq current (delete tg current))
  14531. (push tg current)))
  14532. (if exit-after-next (setq exit-after-next 'now)))
  14533. ((setq e (rassoc c todo-table) tg (car e))
  14534. (with-current-buffer buf
  14535. (save-excursion (org-todo tg)))
  14536. (if exit-after-next (setq exit-after-next 'now)))
  14537. ((setq e (rassoc c ntable) tg (car e))
  14538. (if (member tg current)
  14539. (setq current (delete tg current))
  14540. (loop for g in groups do
  14541. (if (member tg g)
  14542. (mapc (lambda (x)
  14543. (setq current (delete x current)))
  14544. g)))
  14545. (push tg current))
  14546. (if exit-after-next (setq exit-after-next 'now))))
  14547. ;; Create a sorted list
  14548. (setq current
  14549. (sort current
  14550. (lambda (a b)
  14551. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14552. (if (eq exit-after-next 'now) (throw 'exit t))
  14553. (goto-char (point-min))
  14554. (beginning-of-line 2)
  14555. (delete-region (point) (point-at-eol))
  14556. (org-fast-tag-insert "Current" current c-face)
  14557. (org-set-current-tags-overlay current ov-prefix)
  14558. (while (re-search-forward
  14559. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14560. (setq tg (match-string 1))
  14561. (add-text-properties
  14562. (match-beginning 1) (match-end 1)
  14563. (list 'face
  14564. (cond
  14565. ((member tg current) c-face)
  14566. ((member tg inherited) i-face)
  14567. (t (get-text-property (match-beginning 1) 'face))))))
  14568. (goto-char (point-min)))))
  14569. (org-detach-overlay org-tags-overlay)
  14570. (if rtn
  14571. (mapconcat 'identity current ":")
  14572. nil))))
  14573. (defun org-get-tags-string ()
  14574. "Get the TAGS string in the current headline."
  14575. (unless (org-on-heading-p t)
  14576. (error "Not on a heading"))
  14577. (save-excursion
  14578. (beginning-of-line 1)
  14579. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14580. (org-match-string-no-properties 1)
  14581. "")))
  14582. (defun org-get-tags ()
  14583. "Get the list of tags specified in the current headline."
  14584. (org-split-string (org-get-tags-string) ":"))
  14585. (defun org-get-buffer-tags ()
  14586. "Get a table of all tags used in the buffer, for completion."
  14587. (let (tags)
  14588. (save-excursion
  14589. (goto-char (point-min))
  14590. (while (re-search-forward
  14591. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14592. (when (equal (char-after (point-at-bol 0)) ?*)
  14593. (mapc (lambda (x) (add-to-list 'tags x))
  14594. (org-split-string (org-match-string-no-properties 1) ":")))))
  14595. (mapcar 'list tags)))
  14596. ;;;; Properties
  14597. ;;; Setting and retrieving properties
  14598. (defconst org-special-properties
  14599. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14600. "TIMESTAMP" "TIMESTAMP_IA")
  14601. "The special properties valid in Org-mode.
  14602. These are properties that are not defined in the property drawer,
  14603. but in some other way.")
  14604. (defconst org-default-properties
  14605. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14606. "LOCATION" "LOGGING" "COLUMNS")
  14607. "Some properties that are used by Org-mode for various purposes.
  14608. Being in this list makes sure that they are offered for completion.")
  14609. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14610. "Regular expression matching the first line of a property drawer.")
  14611. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14612. "Regular expression matching the first line of a property drawer.")
  14613. (defun org-property-action ()
  14614. "Do an action on properties."
  14615. (interactive)
  14616. (let (c)
  14617. (org-at-property-p)
  14618. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14619. (setq c (read-char-exclusive))
  14620. (cond
  14621. ((equal c ?s)
  14622. (call-interactively 'org-set-property))
  14623. ((equal c ?d)
  14624. (call-interactively 'org-delete-property))
  14625. ((equal c ?D)
  14626. (call-interactively 'org-delete-property-globally))
  14627. ((equal c ?c)
  14628. (call-interactively 'org-compute-property-at-point))
  14629. (t (error "No such property action %c" c)))))
  14630. (defun org-at-property-p ()
  14631. "Is the cursor in a property line?"
  14632. ;; FIXME: Does not check if we are actually in the drawer.
  14633. ;; FIXME: also returns true on any drawers.....
  14634. ;; This is used by C-c C-c for property action.
  14635. (save-excursion
  14636. (beginning-of-line 1)
  14637. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14638. (defmacro org-with-point-at (pom &rest body)
  14639. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14640. (declare (indent 1) (debug t))
  14641. `(save-excursion
  14642. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14643. (save-excursion
  14644. (goto-char (or pom (point)))
  14645. ,@body)))
  14646. (defun org-get-property-block (&optional beg end force)
  14647. "Return the (beg . end) range of the body of the property drawer.
  14648. BEG and END can be beginning and end of subtree, if not given
  14649. they will be found.
  14650. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14651. (catch 'exit
  14652. (save-excursion
  14653. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14654. (end (or end (progn (outline-next-heading) (point)))))
  14655. (goto-char beg)
  14656. (if (re-search-forward org-property-start-re end t)
  14657. (setq beg (1+ (match-end 0)))
  14658. (if force
  14659. (save-excursion
  14660. (org-insert-property-drawer)
  14661. (setq end (progn (outline-next-heading) (point))))
  14662. (throw 'exit nil))
  14663. (goto-char beg)
  14664. (if (re-search-forward org-property-start-re end t)
  14665. (setq beg (1+ (match-end 0)))))
  14666. (if (re-search-forward org-property-end-re end t)
  14667. (setq end (match-beginning 0))
  14668. (or force (throw 'exit nil))
  14669. (goto-char beg)
  14670. (setq end beg)
  14671. (org-indent-line-function)
  14672. (insert ":END:\n"))
  14673. (cons beg end)))))
  14674. (defun org-entry-properties (&optional pom which)
  14675. "Get all properties of the entry at point-or-marker POM.
  14676. This includes the TODO keyword, the tags, time strings for deadline,
  14677. scheduled, and clocking, and any additional properties defined in the
  14678. entry. The return value is an alist, keys may occur multiple times
  14679. if the property key was used several times.
  14680. POM may also be nil, in which case the current entry is used.
  14681. If WHICH is nil or `all', get all properties. If WHICH is
  14682. `special' or `standard', only get that subclass."
  14683. (setq which (or which 'all))
  14684. (org-with-point-at pom
  14685. (let ((clockstr (substring org-clock-string 0 -1))
  14686. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14687. beg end range props sum-props key value string clocksum)
  14688. (save-excursion
  14689. (when (condition-case nil (org-back-to-heading t) (error nil))
  14690. (setq beg (point))
  14691. (setq sum-props (get-text-property (point) 'org-summaries))
  14692. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14693. (outline-next-heading)
  14694. (setq end (point))
  14695. (when (memq which '(all special))
  14696. ;; Get the special properties, like TODO and tags
  14697. (goto-char beg)
  14698. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14699. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14700. (when (looking-at org-priority-regexp)
  14701. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14702. (when (and (setq value (org-get-tags-string))
  14703. (string-match "\\S-" value))
  14704. (push (cons "TAGS" value) props))
  14705. (when (setq value (org-get-tags-at))
  14706. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14707. props))
  14708. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14709. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14710. string (if (equal key clockstr)
  14711. (org-no-properties
  14712. (org-trim
  14713. (buffer-substring
  14714. (match-beginning 3) (goto-char (point-at-eol)))))
  14715. (substring (org-match-string-no-properties 3) 1 -1)))
  14716. (unless key
  14717. (if (= (char-after (match-beginning 3)) ?\[)
  14718. (setq key "TIMESTAMP_IA")
  14719. (setq key "TIMESTAMP")))
  14720. (when (or (equal key clockstr) (not (assoc key props)))
  14721. (push (cons key string) props)))
  14722. )
  14723. (when (memq which '(all standard))
  14724. ;; Get the standard properties, like :PORP: ...
  14725. (setq range (org-get-property-block beg end))
  14726. (when range
  14727. (goto-char (car range))
  14728. (while (re-search-forward
  14729. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14730. (cdr range) t)
  14731. (setq key (org-match-string-no-properties 1)
  14732. value (org-trim (or (org-match-string-no-properties 2) "")))
  14733. (unless (member key excluded)
  14734. (push (cons key (or value "")) props)))))
  14735. (if clocksum
  14736. (push (cons "CLOCKSUM"
  14737. (org-column-number-to-string (/ (float clocksum) 60.)
  14738. 'add_times))
  14739. props))
  14740. (append sum-props (nreverse props)))))))
  14741. (defun org-entry-get (pom property &optional inherit)
  14742. "Get value of PROPERTY for entry at point-or-marker POM.
  14743. If INHERIT is non-nil and the entry does not have the property,
  14744. then also check higher levels of the hierarchy.
  14745. If the property is present but empty, the return value is the empty string.
  14746. If the property is not present at all, nil is returned."
  14747. (org-with-point-at pom
  14748. (if inherit
  14749. (org-entry-get-with-inheritance property)
  14750. (if (member property org-special-properties)
  14751. ;; We need a special property. Use brute force, get all properties.
  14752. (cdr (assoc property (org-entry-properties nil 'special)))
  14753. (let ((range (org-get-property-block)))
  14754. (if (and range
  14755. (goto-char (car range))
  14756. (re-search-forward
  14757. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14758. (cdr range) t))
  14759. ;; Found the property, return it.
  14760. (if (match-end 1)
  14761. (org-match-string-no-properties 1)
  14762. "")))))))
  14763. (defun org-entry-delete (pom property)
  14764. "Delete the property PROPERTY from entry at point-or-marker POM."
  14765. (org-with-point-at pom
  14766. (if (member property org-special-properties)
  14767. nil ; cannot delete these properties.
  14768. (let ((range (org-get-property-block)))
  14769. (if (and range
  14770. (goto-char (car range))
  14771. (re-search-forward
  14772. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14773. (cdr range) t))
  14774. (progn
  14775. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14776. t)
  14777. nil)))))
  14778. ;; Multi-values properties are properties that contain multiple values
  14779. ;; These values are assumed to be single words, separated by whitespace.
  14780. (defun org-entry-add-to-multivalued-property (pom property value)
  14781. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14782. (let* ((old (org-entry-get pom property))
  14783. (values (and old (org-split-string old "[ \t]"))))
  14784. (unless (member value values)
  14785. (setq values (cons value values))
  14786. (org-entry-put pom property
  14787. (mapconcat 'identity values " ")))))
  14788. (defun org-entry-remove-from-multivalued-property (pom property value)
  14789. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14790. (let* ((old (org-entry-get pom property))
  14791. (values (and old (org-split-string old "[ \t]"))))
  14792. (when (member value values)
  14793. (setq values (delete value values))
  14794. (org-entry-put pom property
  14795. (mapconcat 'identity values " ")))))
  14796. (defun org-entry-member-in-multivalued-property (pom property value)
  14797. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14798. (let* ((old (org-entry-get pom property))
  14799. (values (and old (org-split-string old "[ \t]"))))
  14800. (member value values)))
  14801. (defvar org-entry-property-inherited-from (make-marker))
  14802. (defun org-entry-get-with-inheritance (property)
  14803. "Get entry property, and search higher levels if not present."
  14804. (let (tmp)
  14805. (save-excursion
  14806. (save-restriction
  14807. (widen)
  14808. (catch 'ex
  14809. (while t
  14810. (when (setq tmp (org-entry-get nil property))
  14811. (org-back-to-heading t)
  14812. (move-marker org-entry-property-inherited-from (point))
  14813. (throw 'ex tmp))
  14814. (or (org-up-heading-safe) (throw 'ex nil)))))
  14815. (or tmp (cdr (assoc property org-local-properties))
  14816. (cdr (assoc property org-global-properties))))))
  14817. (defun org-entry-put (pom property value)
  14818. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14819. (org-with-point-at pom
  14820. (org-back-to-heading t)
  14821. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14822. range)
  14823. (cond
  14824. ((equal property "TODO")
  14825. (when (and (stringp value) (string-match "\\S-" value)
  14826. (not (member value org-todo-keywords-1)))
  14827. (error "\"%s\" is not a valid TODO state" value))
  14828. (if (or (not value)
  14829. (not (string-match "\\S-" value)))
  14830. (setq value 'none))
  14831. (org-todo value)
  14832. (org-set-tags nil 'align))
  14833. ((equal property "PRIORITY")
  14834. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14835. (string-to-char value) ?\ ))
  14836. (org-set-tags nil 'align))
  14837. ((equal property "SCHEDULED")
  14838. (if (re-search-forward org-scheduled-time-regexp end t)
  14839. (cond
  14840. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14841. ((eq value 'later) (org-timestamp-change 1 'day))
  14842. (t (call-interactively 'org-schedule)))
  14843. (call-interactively 'org-schedule)))
  14844. ((equal property "DEADLINE")
  14845. (if (re-search-forward org-deadline-time-regexp end t)
  14846. (cond
  14847. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14848. ((eq value 'later) (org-timestamp-change 1 'day))
  14849. (t (call-interactively 'org-deadline)))
  14850. (call-interactively 'org-deadline)))
  14851. ((member property org-special-properties)
  14852. (error "The %s property can not yet be set with `org-entry-put'"
  14853. property))
  14854. (t ; a non-special property
  14855. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14856. (setq range (org-get-property-block beg end 'force))
  14857. (goto-char (car range))
  14858. (if (re-search-forward
  14859. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14860. (progn
  14861. (delete-region (match-beginning 1) (match-end 1))
  14862. (goto-char (match-beginning 1)))
  14863. (goto-char (cdr range))
  14864. (insert "\n")
  14865. (backward-char 1)
  14866. (org-indent-line-function)
  14867. (insert ":" property ":"))
  14868. (and value (insert " " value))
  14869. (org-indent-line-function)))))))
  14870. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14871. "Get all property keys in the current buffer.
  14872. With INCLUDE-SPECIALS, also list the special properties that relect things
  14873. like tags and TODO state.
  14874. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14875. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14876. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14877. formats in the current buffer."
  14878. (let (rtn range cfmt cols s p)
  14879. (save-excursion
  14880. (save-restriction
  14881. (widen)
  14882. (goto-char (point-min))
  14883. (while (re-search-forward org-property-start-re nil t)
  14884. (setq range (org-get-property-block))
  14885. (goto-char (car range))
  14886. (while (re-search-forward
  14887. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14888. (cdr range) t)
  14889. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14890. (outline-next-heading))))
  14891. (when include-specials
  14892. (setq rtn (append org-special-properties rtn)))
  14893. (when include-defaults
  14894. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14895. (when include-columns
  14896. (save-excursion
  14897. (save-restriction
  14898. (widen)
  14899. (goto-char (point-min))
  14900. (while (re-search-forward
  14901. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14902. nil t)
  14903. (setq cfmt (match-string 2) s 0)
  14904. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14905. cfmt s)
  14906. (setq s (match-end 0)
  14907. p (match-string 1 cfmt))
  14908. (unless (or (equal p "ITEM")
  14909. (member p org-special-properties))
  14910. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14911. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14912. (defun org-property-values (key)
  14913. "Return a list of all values of property KEY."
  14914. (save-excursion
  14915. (save-restriction
  14916. (widen)
  14917. (goto-char (point-min))
  14918. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14919. values)
  14920. (while (re-search-forward re nil t)
  14921. (add-to-list 'values (org-trim (match-string 1))))
  14922. (delete "" values)))))
  14923. (defun org-insert-property-drawer ()
  14924. "Insert a property drawer into the current entry."
  14925. (interactive)
  14926. (org-back-to-heading t)
  14927. (looking-at outline-regexp)
  14928. (let ((indent (- (match-end 0)(match-beginning 0)))
  14929. (beg (point))
  14930. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14931. end hiddenp)
  14932. (outline-next-heading)
  14933. (setq end (point))
  14934. (goto-char beg)
  14935. (while (re-search-forward re end t))
  14936. (setq hiddenp (org-invisible-p))
  14937. (end-of-line 1)
  14938. (and (equal (char-after) ?\n) (forward-char 1))
  14939. (org-skip-over-state-notes)
  14940. (skip-chars-backward " \t\n\r")
  14941. (if (eq (char-before) ?*) (forward-char 1))
  14942. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14943. (beginning-of-line 0)
  14944. (indent-to-column indent)
  14945. (beginning-of-line 2)
  14946. (indent-to-column indent)
  14947. (beginning-of-line 0)
  14948. (if hiddenp
  14949. (save-excursion
  14950. (org-back-to-heading t)
  14951. (hide-entry))
  14952. (org-flag-drawer t))))
  14953. (defun org-set-property (property value)
  14954. "In the current entry, set PROPERTY to VALUE.
  14955. When called interactively, this will prompt for a property name, offering
  14956. completion on existing and default properties. And then it will prompt
  14957. for a value, offering competion either on allowed values (via an inherited
  14958. xxx_ALL property) or on existing values in other instances of this property
  14959. in the current file."
  14960. (interactive
  14961. (let* ((prop (completing-read
  14962. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  14963. (cur (org-entry-get nil prop))
  14964. (allowed (org-property-get-allowed-values nil prop 'table))
  14965. (existing (mapcar 'list (org-property-values prop)))
  14966. (val (if allowed
  14967. (completing-read "Value: " allowed nil 'req-match)
  14968. (completing-read
  14969. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14970. (concat "[" cur "]") "")
  14971. ": ")
  14972. existing nil nil "" nil cur))))
  14973. (list prop (if (equal val "") cur val))))
  14974. (unless (equal (org-entry-get nil property) value)
  14975. (org-entry-put nil property value)))
  14976. (defun org-delete-property (property)
  14977. "In the current entry, delete PROPERTY."
  14978. (interactive
  14979. (let* ((prop (completing-read
  14980. "Property: " (org-entry-properties nil 'standard))))
  14981. (list prop)))
  14982. (message "Property %s %s" property
  14983. (if (org-entry-delete nil property)
  14984. "deleted"
  14985. "was not present in the entry")))
  14986. (defun org-delete-property-globally (property)
  14987. "Remove PROPERTY globally, from all entries."
  14988. (interactive
  14989. (let* ((prop (completing-read
  14990. "Globally remove property: "
  14991. (mapcar 'list (org-buffer-property-keys)))))
  14992. (list prop)))
  14993. (save-excursion
  14994. (save-restriction
  14995. (widen)
  14996. (goto-char (point-min))
  14997. (let ((cnt 0))
  14998. (while (re-search-forward
  14999. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15000. nil t)
  15001. (setq cnt (1+ cnt))
  15002. (replace-match ""))
  15003. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15004. (defvar org-columns-current-fmt-compiled) ; defined below
  15005. (defun org-compute-property-at-point ()
  15006. "Compute the property at point.
  15007. This looks for an enclosing column format, extracts the operator and
  15008. then applies it to the proerty in the column format's scope."
  15009. (interactive)
  15010. (unless (org-at-property-p)
  15011. (error "Not at a property"))
  15012. (let ((prop (org-match-string-no-properties 2)))
  15013. (org-columns-get-format-and-top-level)
  15014. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15015. (error "No operator defined for property %s" prop))
  15016. (org-columns-compute prop)))
  15017. (defun org-property-get-allowed-values (pom property &optional table)
  15018. "Get allowed values for the property PROPERTY.
  15019. When TABLE is non-nil, return an alist that can directly be used for
  15020. completion."
  15021. (let (vals)
  15022. (cond
  15023. ((equal property "TODO")
  15024. (setq vals (org-with-point-at pom
  15025. (append org-todo-keywords-1 '("")))))
  15026. ((equal property "PRIORITY")
  15027. (let ((n org-lowest-priority))
  15028. (while (>= n org-highest-priority)
  15029. (push (char-to-string n) vals)
  15030. (setq n (1- n)))))
  15031. ((member property org-special-properties))
  15032. (t
  15033. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15034. (when (and vals (string-match "\\S-" vals))
  15035. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15036. (setq vals (mapcar (lambda (x)
  15037. (cond ((stringp x) x)
  15038. ((numberp x) (number-to-string x))
  15039. ((symbolp x) (symbol-name x))
  15040. (t "???")))
  15041. vals)))))
  15042. (if table (mapcar 'list vals) vals)))
  15043. (defun org-property-previous-allowed-value (&optional previous)
  15044. "Switch to the next allowed value for this property."
  15045. (interactive)
  15046. (org-property-next-allowed-value t))
  15047. (defun org-property-next-allowed-value (&optional previous)
  15048. "Switch to the next allowed value for this property."
  15049. (interactive)
  15050. (unless (org-at-property-p)
  15051. (error "Not at a property"))
  15052. (let* ((key (match-string 2))
  15053. (value (match-string 3))
  15054. (allowed (or (org-property-get-allowed-values (point) key)
  15055. (and (member value '("[ ]" "[-]" "[X]"))
  15056. '("[ ]" "[X]"))))
  15057. nval)
  15058. (unless allowed
  15059. (error "Allowed values for this property have not been defined"))
  15060. (if previous (setq allowed (reverse allowed)))
  15061. (if (member value allowed)
  15062. (setq nval (car (cdr (member value allowed)))))
  15063. (setq nval (or nval (car allowed)))
  15064. (if (equal nval value)
  15065. (error "Only one allowed value for this property"))
  15066. (org-at-property-p)
  15067. (replace-match (concat " :" key ": " nval) t t)
  15068. (org-indent-line-function)
  15069. (beginning-of-line 1)
  15070. (skip-chars-forward " \t")))
  15071. (defun org-find-entry-with-id (ident)
  15072. "Locate the entry that contains the ID property with exact value IDENT.
  15073. IDENT can be a string, a symbol or a number, this function will search for
  15074. the string representation of it.
  15075. Return the position where this entry starts, or nil if there is no such entry."
  15076. (let ((id (cond
  15077. ((stringp ident) ident)
  15078. ((symbol-name ident) (symbol-name ident))
  15079. ((numberp ident) (number-to-string ident))
  15080. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15081. (case-fold-search nil))
  15082. (save-excursion
  15083. (save-restriction
  15084. (goto-char (point-min))
  15085. (when (re-search-forward
  15086. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15087. nil t)
  15088. (org-back-to-heading)
  15089. (point))))))
  15090. ;;; Column View
  15091. (defvar org-columns-overlays nil
  15092. "Holds the list of current column overlays.")
  15093. (defvar org-columns-current-fmt nil
  15094. "Local variable, holds the currently active column format.")
  15095. (defvar org-columns-current-fmt-compiled nil
  15096. "Local variable, holds the currently active column format.
  15097. This is the compiled version of the format.")
  15098. (defvar org-columns-current-widths nil
  15099. "Loval variable, holds the currently widths of fields.")
  15100. (defvar org-columns-current-maxwidths nil
  15101. "Loval variable, holds the currently active maximum column widths.")
  15102. (defvar org-columns-begin-marker (make-marker)
  15103. "Points to the position where last a column creation command was called.")
  15104. (defvar org-columns-top-level-marker (make-marker)
  15105. "Points to the position where current columns region starts.")
  15106. (defvar org-columns-map (make-sparse-keymap)
  15107. "The keymap valid in column display.")
  15108. (defun org-columns-content ()
  15109. "Switch to contents view while in columns view."
  15110. (interactive)
  15111. (org-overview)
  15112. (org-content))
  15113. (org-defkey org-columns-map "c" 'org-columns-content)
  15114. (org-defkey org-columns-map "o" 'org-overview)
  15115. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15116. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15117. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15118. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15119. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15120. (org-defkey org-columns-map "q" 'org-columns-quit)
  15121. (org-defkey org-columns-map "r" 'org-columns-redo)
  15122. (org-defkey org-columns-map "g" 'org-columns-redo)
  15123. (org-defkey org-columns-map [left] 'backward-char)
  15124. (org-defkey org-columns-map "\M-b" 'backward-char)
  15125. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15126. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15127. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15128. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15129. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15130. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15131. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15132. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15133. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15134. (org-defkey org-columns-map ">" 'org-columns-widen)
  15135. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15136. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15137. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15138. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15139. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15140. '("Column"
  15141. ["Edit property" org-columns-edit-value t]
  15142. ["Next allowed value" org-columns-next-allowed-value t]
  15143. ["Previous allowed value" org-columns-previous-allowed-value t]
  15144. ["Show full value" org-columns-show-value t]
  15145. ["Edit allowed values" org-columns-edit-allowed t]
  15146. "--"
  15147. ["Edit column attributes" org-columns-edit-attributes t]
  15148. ["Increase column width" org-columns-widen t]
  15149. ["Decrease column width" org-columns-narrow t]
  15150. "--"
  15151. ["Move column right" org-columns-move-right t]
  15152. ["Move column left" org-columns-move-left t]
  15153. ["Add column" org-columns-new t]
  15154. ["Delete column" org-columns-delete t]
  15155. "--"
  15156. ["CONTENTS" org-columns-content t]
  15157. ["OVERVIEW" org-overview t]
  15158. ["Refresh columns display" org-columns-redo t]
  15159. "--"
  15160. ["Open link" org-columns-open-link t]
  15161. "--"
  15162. ["Quit" org-columns-quit t]))
  15163. (defun org-columns-new-overlay (beg end &optional string face)
  15164. "Create a new column overlay and add it to the list."
  15165. (let ((ov (org-make-overlay beg end)))
  15166. (org-overlay-put ov 'face (or face 'secondary-selection))
  15167. (org-overlay-display ov string face)
  15168. (push ov org-columns-overlays)
  15169. ov))
  15170. (defun org-columns-display-here (&optional props)
  15171. "Overlay the current line with column display."
  15172. (interactive)
  15173. (let* ((fmt org-columns-current-fmt-compiled)
  15174. (beg (point-at-bol))
  15175. (level-face (save-excursion
  15176. (beginning-of-line 1)
  15177. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15178. (org-get-level-face 2))))
  15179. (color (list :foreground
  15180. (face-attribute (or level-face 'default) :foreground)))
  15181. props pom property ass width f string ov column val modval)
  15182. ;; Check if the entry is in another buffer.
  15183. (unless props
  15184. (if (eq major-mode 'org-agenda-mode)
  15185. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15186. (get-text-property (point) 'org-marker))
  15187. props (if pom (org-entry-properties pom) nil))
  15188. (setq props (org-entry-properties nil))))
  15189. ;; Walk the format
  15190. (while (setq column (pop fmt))
  15191. (setq property (car column)
  15192. ass (if (equal property "ITEM")
  15193. (cons "ITEM"
  15194. (save-match-data
  15195. (org-no-properties
  15196. (org-remove-tabs
  15197. (buffer-substring-no-properties
  15198. (point-at-bol) (point-at-eol))))))
  15199. (assoc property props))
  15200. width (or (cdr (assoc property org-columns-current-maxwidths))
  15201. (nth 2 column)
  15202. (length property))
  15203. f (format "%%-%d.%ds | " width width)
  15204. val (or (cdr ass) "")
  15205. modval (if (equal property "ITEM")
  15206. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15207. string (format f (or modval val)))
  15208. ;; Create the overlay
  15209. (org-unmodified
  15210. (setq ov (org-columns-new-overlay
  15211. beg (setq beg (1+ beg)) string
  15212. (list color 'org-column)))
  15213. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15214. (org-overlay-put ov 'keymap org-columns-map)
  15215. (org-overlay-put ov 'org-columns-key property)
  15216. (org-overlay-put ov 'org-columns-value (cdr ass))
  15217. (org-overlay-put ov 'org-columns-value-modified modval)
  15218. (org-overlay-put ov 'org-columns-pom pom)
  15219. (org-overlay-put ov 'org-columns-format f))
  15220. (if (or (not (char-after beg))
  15221. (equal (char-after beg) ?\n))
  15222. (let ((inhibit-read-only t))
  15223. (save-excursion
  15224. (goto-char beg)
  15225. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15226. ;; Make the rest of the line disappear.
  15227. (org-unmodified
  15228. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15229. (org-overlay-put ov 'invisible t)
  15230. (org-overlay-put ov 'keymap org-columns-map)
  15231. (org-overlay-put ov 'intangible t)
  15232. (push ov org-columns-overlays)
  15233. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15234. (org-overlay-put ov 'keymap org-columns-map)
  15235. (push ov org-columns-overlays)
  15236. (let ((inhibit-read-only t))
  15237. (put-text-property (max (point-min) (1- (point-at-bol)))
  15238. (min (point-max) (1+ (point-at-eol)))
  15239. 'read-only "Type `e' to edit property")))))
  15240. (defvar org-previous-header-line-format nil
  15241. "The header line format before column view was turned on.")
  15242. (defvar org-columns-inhibit-recalculation nil
  15243. "Inhibit recomputing of columns on column view startup.")
  15244. (defvar header-line-format)
  15245. (defun org-columns-display-here-title ()
  15246. "Overlay the newline before the current line with the table title."
  15247. (interactive)
  15248. (let ((fmt org-columns-current-fmt-compiled)
  15249. string (title "")
  15250. property width f column str widths)
  15251. (while (setq column (pop fmt))
  15252. (setq property (car column)
  15253. str (or (nth 1 column) property)
  15254. width (or (cdr (assoc property org-columns-current-maxwidths))
  15255. (nth 2 column)
  15256. (length str))
  15257. widths (push width widths)
  15258. f (format "%%-%d.%ds | " width width)
  15259. string (format f str)
  15260. title (concat title string)))
  15261. (setq title (concat
  15262. (org-add-props " " nil 'display '(space :align-to 0))
  15263. (org-add-props title nil 'face '(:weight bold :underline t))))
  15264. (org-set-local 'org-previous-header-line-format header-line-format)
  15265. (org-set-local 'org-columns-current-widths (nreverse widths))
  15266. (setq header-line-format title)))
  15267. (defun org-columns-remove-overlays ()
  15268. "Remove all currently active column overlays."
  15269. (interactive)
  15270. (when (marker-buffer org-columns-begin-marker)
  15271. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15272. (when (local-variable-p 'org-previous-header-line-format)
  15273. (setq header-line-format org-previous-header-line-format)
  15274. (kill-local-variable 'org-previous-header-line-format))
  15275. (move-marker org-columns-begin-marker nil)
  15276. (move-marker org-columns-top-level-marker nil)
  15277. (org-unmodified
  15278. (mapc 'org-delete-overlay org-columns-overlays)
  15279. (setq org-columns-overlays nil)
  15280. (let ((inhibit-read-only t))
  15281. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15282. (defun org-columns-cleanup-item (item fmt)
  15283. "Remove from ITEM what is a column in the format FMT."
  15284. (if (not org-complex-heading-regexp)
  15285. item
  15286. (when (string-match org-complex-heading-regexp item)
  15287. (concat
  15288. (org-add-props (concat (match-string 1 item) " ") nil
  15289. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15290. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15291. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15292. " " (match-string 4 item)
  15293. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15294. (defun org-columns-show-value ()
  15295. "Show the full value of the property."
  15296. (interactive)
  15297. (let ((value (get-char-property (point) 'org-columns-value)))
  15298. (message "Value is: %s" (or value ""))))
  15299. (defun org-columns-quit ()
  15300. "Remove the column overlays and in this way exit column editing."
  15301. (interactive)
  15302. (org-unmodified
  15303. (org-columns-remove-overlays)
  15304. (let ((inhibit-read-only t))
  15305. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15306. (when (eq major-mode 'org-agenda-mode)
  15307. (message
  15308. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15309. (defun org-columns-check-computed ()
  15310. "Check if this column value is computed.
  15311. If yes, throw an error indicating that changing it does not make sense."
  15312. (let ((val (get-char-property (point) 'org-columns-value)))
  15313. (when (and (stringp val)
  15314. (get-char-property 0 'org-computed val))
  15315. (error "This value is computed from the entry's children"))))
  15316. (defun org-columns-todo (&optional arg)
  15317. "Change the TODO state during column view."
  15318. (interactive "P")
  15319. (org-columns-edit-value "TODO"))
  15320. (defun org-columns-set-tags-or-toggle (&optional arg)
  15321. "Toggle checkbox at point, or set tags for current headline."
  15322. (interactive "P")
  15323. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15324. (get-char-property (point) 'org-columns-value))
  15325. (org-columns-next-allowed-value)
  15326. (org-columns-edit-value "TAGS")))
  15327. (defun org-columns-edit-value (&optional key)
  15328. "Edit the value of the property at point in column view.
  15329. Where possible, use the standard interface for changing this line."
  15330. (interactive)
  15331. (org-columns-check-computed)
  15332. (let* ((external-key key)
  15333. (col (current-column))
  15334. (key (or key (get-char-property (point) 'org-columns-key)))
  15335. (value (get-char-property (point) 'org-columns-value))
  15336. (bol (point-at-bol)) (eol (point-at-eol))
  15337. (pom (or (get-text-property bol 'org-hd-marker)
  15338. (point))) ; keep despite of compiler waring
  15339. (line-overlays
  15340. (delq nil (mapcar (lambda (x)
  15341. (and (eq (overlay-buffer x) (current-buffer))
  15342. (>= (overlay-start x) bol)
  15343. (<= (overlay-start x) eol)
  15344. x))
  15345. org-columns-overlays)))
  15346. nval eval allowed)
  15347. (cond
  15348. ((equal key "CLOCKSUM")
  15349. (error "This special column cannot be edited"))
  15350. ((equal key "ITEM")
  15351. (setq eval '(org-with-point-at pom
  15352. (org-edit-headline))))
  15353. ((equal key "TODO")
  15354. (setq eval '(org-with-point-at pom
  15355. (let ((current-prefix-arg
  15356. (if external-key current-prefix-arg '(4))))
  15357. (call-interactively 'org-todo)))))
  15358. ((equal key "PRIORITY")
  15359. (setq eval '(org-with-point-at pom
  15360. (call-interactively 'org-priority))))
  15361. ((equal key "TAGS")
  15362. (setq eval '(org-with-point-at pom
  15363. (let ((org-fast-tag-selection-single-key
  15364. (if (eq org-fast-tag-selection-single-key 'expert)
  15365. t org-fast-tag-selection-single-key)))
  15366. (call-interactively 'org-set-tags)))))
  15367. ((equal key "DEADLINE")
  15368. (setq eval '(org-with-point-at pom
  15369. (call-interactively 'org-deadline))))
  15370. ((equal key "SCHEDULED")
  15371. (setq eval '(org-with-point-at pom
  15372. (call-interactively 'org-schedule))))
  15373. (t
  15374. (setq allowed (org-property-get-allowed-values pom key 'table))
  15375. (if allowed
  15376. (setq nval (completing-read "Value: " allowed nil t))
  15377. (setq nval (read-string "Edit: " value)))
  15378. (setq nval (org-trim nval))
  15379. (when (not (equal nval value))
  15380. (setq eval '(org-entry-put pom key nval)))))
  15381. (when eval
  15382. (let ((inhibit-read-only t))
  15383. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15384. (unwind-protect
  15385. (progn
  15386. (setq org-columns-overlays
  15387. (org-delete-all line-overlays org-columns-overlays))
  15388. (mapc 'org-delete-overlay line-overlays)
  15389. (org-columns-eval eval))
  15390. (org-columns-display-here))))
  15391. (move-to-column col)
  15392. (if (and (org-mode-p)
  15393. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15394. (org-columns-update key))))
  15395. (defun org-edit-headline () ; FIXME: this is not columns specific
  15396. "Edit the current headline, the part without TODO keyword, TAGS."
  15397. (org-back-to-heading)
  15398. (when (looking-at org-todo-line-regexp)
  15399. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15400. (txt (match-string 3))
  15401. (post "")
  15402. txt2)
  15403. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15404. (setq post (match-string 0 txt)
  15405. txt (substring txt 0 (match-beginning 0))))
  15406. (setq txt2 (read-string "Edit: " txt))
  15407. (when (not (equal txt txt2))
  15408. (beginning-of-line 1)
  15409. (insert pre txt2 post)
  15410. (delete-region (point) (point-at-eol))
  15411. (org-set-tags nil t)))))
  15412. (defun org-columns-edit-allowed ()
  15413. "Edit the list of allowed values for the current property."
  15414. (interactive)
  15415. (let* ((key (get-char-property (point) 'org-columns-key))
  15416. (key1 (concat key "_ALL"))
  15417. (allowed (org-entry-get (point) key1 t))
  15418. nval)
  15419. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15420. (setq nval (read-string "Allowed: " allowed))
  15421. (org-entry-put
  15422. (cond ((marker-position org-entry-property-inherited-from)
  15423. org-entry-property-inherited-from)
  15424. ((marker-position org-columns-top-level-marker)
  15425. org-columns-top-level-marker))
  15426. key1 nval)))
  15427. (defmacro org-no-warnings (&rest body)
  15428. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15429. (defun org-columns-eval (form)
  15430. (let (hidep)
  15431. (save-excursion
  15432. (beginning-of-line 1)
  15433. ;; `next-line' is needed here, because it skips invisible line.
  15434. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15435. (setq hidep (org-on-heading-p 1)))
  15436. (eval form)
  15437. (and hidep (hide-entry))))
  15438. (defun org-columns-previous-allowed-value ()
  15439. "Switch to the previous allowed value for this column."
  15440. (interactive)
  15441. (org-columns-next-allowed-value t))
  15442. (defun org-columns-next-allowed-value (&optional previous)
  15443. "Switch to the next allowed value for this column."
  15444. (interactive)
  15445. (org-columns-check-computed)
  15446. (let* ((col (current-column))
  15447. (key (get-char-property (point) 'org-columns-key))
  15448. (value (get-char-property (point) 'org-columns-value))
  15449. (bol (point-at-bol)) (eol (point-at-eol))
  15450. (pom (or (get-text-property bol 'org-hd-marker)
  15451. (point))) ; keep despite of compiler waring
  15452. (line-overlays
  15453. (delq nil (mapcar (lambda (x)
  15454. (and (eq (overlay-buffer x) (current-buffer))
  15455. (>= (overlay-start x) bol)
  15456. (<= (overlay-start x) eol)
  15457. x))
  15458. org-columns-overlays)))
  15459. (allowed (or (org-property-get-allowed-values pom key)
  15460. (and (memq
  15461. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15462. '(checkbox checkbox-n-of-m checkbox-percent))
  15463. '("[ ]" "[X]"))))
  15464. nval)
  15465. (when (equal key "ITEM")
  15466. (error "Cannot edit item headline from here"))
  15467. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15468. (error "Allowed values for this property have not been defined"))
  15469. (if (member key '("SCHEDULED" "DEADLINE"))
  15470. (setq nval (if previous 'earlier 'later))
  15471. (if previous (setq allowed (reverse allowed)))
  15472. (if (member value allowed)
  15473. (setq nval (car (cdr (member value allowed)))))
  15474. (setq nval (or nval (car allowed)))
  15475. (if (equal nval value)
  15476. (error "Only one allowed value for this property")))
  15477. (let ((inhibit-read-only t))
  15478. (remove-text-properties (1- bol) eol '(read-only t))
  15479. (unwind-protect
  15480. (progn
  15481. (setq org-columns-overlays
  15482. (org-delete-all line-overlays org-columns-overlays))
  15483. (mapc 'org-delete-overlay line-overlays)
  15484. (org-columns-eval '(org-entry-put pom key nval)))
  15485. (org-columns-display-here)))
  15486. (move-to-column col)
  15487. (if (and (org-mode-p)
  15488. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15489. (org-columns-update key))))
  15490. (defun org-verify-version (task)
  15491. (cond
  15492. ((eq task 'columns)
  15493. (if (or (featurep 'xemacs)
  15494. (< emacs-major-version 22))
  15495. (error "Emacs 22 is required for the columns feature")))))
  15496. (defun org-columns-open-link (&optional arg)
  15497. (interactive "P")
  15498. (let ((value (get-char-property (point) 'org-columns-value)))
  15499. (org-open-link-from-string value arg)))
  15500. (defun org-open-link-from-string (s &optional arg)
  15501. "Open a link in the string S, as if it was in Org-mode."
  15502. (interactive)
  15503. (with-temp-buffer
  15504. (let ((org-inhibit-startup t))
  15505. (org-mode)
  15506. (insert s)
  15507. (goto-char (point-min))
  15508. (org-open-at-point arg))))
  15509. (defun org-columns-get-format-and-top-level ()
  15510. (let (fmt)
  15511. (when (condition-case nil (org-back-to-heading) (error nil))
  15512. (move-marker org-entry-property-inherited-from nil)
  15513. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15514. (setq fmt (or fmt org-columns-default-format))
  15515. (org-set-local 'org-columns-current-fmt fmt)
  15516. (org-columns-compile-format fmt)
  15517. (if (marker-position org-entry-property-inherited-from)
  15518. (move-marker org-columns-top-level-marker
  15519. org-entry-property-inherited-from)
  15520. (move-marker org-columns-top-level-marker (point)))
  15521. fmt))
  15522. (defun org-columns ()
  15523. "Turn on column view on an org-mode file."
  15524. (interactive)
  15525. (org-verify-version 'columns)
  15526. (org-columns-remove-overlays)
  15527. (move-marker org-columns-begin-marker (point))
  15528. (let (beg end fmt cache maxwidths)
  15529. (setq fmt (org-columns-get-format-and-top-level))
  15530. (save-excursion
  15531. (goto-char org-columns-top-level-marker)
  15532. (setq beg (point))
  15533. (unless org-columns-inhibit-recalculation
  15534. (org-columns-compute-all))
  15535. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15536. (point-max)))
  15537. ;; Get and cache the properties
  15538. (goto-char beg)
  15539. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15540. (save-excursion
  15541. (save-restriction
  15542. (narrow-to-region beg end)
  15543. (org-clock-sum))))
  15544. (while (re-search-forward (concat "^" outline-regexp) end t)
  15545. (push (cons (org-current-line) (org-entry-properties)) cache))
  15546. (when cache
  15547. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15548. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15549. (org-columns-display-here-title)
  15550. (mapc (lambda (x)
  15551. (goto-line (car x))
  15552. (org-columns-display-here (cdr x)))
  15553. cache)))))
  15554. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15555. "Insert a new column, to the leeft o the current column."
  15556. (interactive)
  15557. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15558. cell)
  15559. (setq prop (completing-read
  15560. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15561. nil nil prop))
  15562. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15563. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15564. (if (string-match "\\S-" width)
  15565. (setq width (string-to-number width))
  15566. (setq width nil))
  15567. (setq fmt (completing-read "Summary [none]: "
  15568. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15569. nil t))
  15570. (if (string-match "\\S-" fmt)
  15571. (setq fmt (intern fmt))
  15572. (setq fmt nil))
  15573. (if (eq fmt 'none) (setq fmt nil))
  15574. (if editp
  15575. (progn
  15576. (setcar editp prop)
  15577. (setcdr editp (list title width nil fmt)))
  15578. (setq cell (nthcdr (1- (current-column))
  15579. org-columns-current-fmt-compiled))
  15580. (setcdr cell (cons (list prop title width nil fmt)
  15581. (cdr cell))))
  15582. (org-columns-store-format)
  15583. (org-columns-redo)))
  15584. (defun org-columns-delete ()
  15585. "Delete the column at point from columns view."
  15586. (interactive)
  15587. (let* ((n (current-column))
  15588. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15589. (when (y-or-n-p
  15590. (format "Are you sure you want to remove column \"%s\"? " title))
  15591. (setq org-columns-current-fmt-compiled
  15592. (delq (nth n org-columns-current-fmt-compiled)
  15593. org-columns-current-fmt-compiled))
  15594. (org-columns-store-format)
  15595. (org-columns-redo)
  15596. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15597. (backward-char 1)))))
  15598. (defun org-columns-edit-attributes ()
  15599. "Edit the attributes of the current column."
  15600. (interactive)
  15601. (let* ((n (current-column))
  15602. (info (nth n org-columns-current-fmt-compiled)))
  15603. (apply 'org-columns-new info)))
  15604. (defun org-columns-widen (arg)
  15605. "Make the column wider by ARG characters."
  15606. (interactive "p")
  15607. (let* ((n (current-column))
  15608. (entry (nth n org-columns-current-fmt-compiled))
  15609. (width (or (nth 2 entry)
  15610. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15611. (setq width (max 1 (+ width arg)))
  15612. (setcar (nthcdr 2 entry) width)
  15613. (org-columns-store-format)
  15614. (org-columns-redo)))
  15615. (defun org-columns-narrow (arg)
  15616. "Make the column nrrower by ARG characters."
  15617. (interactive "p")
  15618. (org-columns-widen (- arg)))
  15619. (defun org-columns-move-right ()
  15620. "Swap this column with the one to the right."
  15621. (interactive)
  15622. (let* ((n (current-column))
  15623. (cell (nthcdr n org-columns-current-fmt-compiled))
  15624. e)
  15625. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15626. (error "Cannot shift this column further to the right"))
  15627. (setq e (car cell))
  15628. (setcar cell (car (cdr cell)))
  15629. (setcdr cell (cons e (cdr (cdr cell))))
  15630. (org-columns-store-format)
  15631. (org-columns-redo)
  15632. (forward-char 1)))
  15633. (defun org-columns-move-left ()
  15634. "Swap this column with the one to the left."
  15635. (interactive)
  15636. (let* ((n (current-column)))
  15637. (when (= n 0)
  15638. (error "Cannot shift this column further to the left"))
  15639. (backward-char 1)
  15640. (org-columns-move-right)
  15641. (backward-char 1)))
  15642. (defun org-columns-store-format ()
  15643. "Store the text version of the current columns format in appropriate place.
  15644. This is either in the COLUMNS property of the node starting the current column
  15645. display, or in the #+COLUMNS line of the current buffer."
  15646. (let (fmt (cnt 0))
  15647. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15648. (org-set-local 'org-columns-current-fmt fmt)
  15649. (if (marker-position org-columns-top-level-marker)
  15650. (save-excursion
  15651. (goto-char org-columns-top-level-marker)
  15652. (if (and (org-at-heading-p)
  15653. (org-entry-get nil "COLUMNS"))
  15654. (org-entry-put nil "COLUMNS" fmt)
  15655. (goto-char (point-min))
  15656. ;; Overwrite all #+COLUMNS lines....
  15657. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15658. (setq cnt (1+ cnt))
  15659. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15660. (unless (> cnt 0)
  15661. (goto-char (point-min))
  15662. (or (org-on-heading-p t) (outline-next-heading))
  15663. (let ((inhibit-read-only t))
  15664. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15665. (org-set-local 'org-columns-default-format fmt))))))
  15666. (defvar org-overriding-columns-format nil
  15667. "When set, overrides any other definition.")
  15668. (defvar org-agenda-view-columns-initially nil
  15669. "When set, switch to columns view immediately after creating the agenda.")
  15670. (defun org-agenda-columns ()
  15671. "Turn on column view in the agenda."
  15672. (interactive)
  15673. (org-verify-version 'columns)
  15674. (org-columns-remove-overlays)
  15675. (move-marker org-columns-begin-marker (point))
  15676. (let (fmt cache maxwidths m)
  15677. (cond
  15678. ((and (local-variable-p 'org-overriding-columns-format)
  15679. org-overriding-columns-format)
  15680. (setq fmt org-overriding-columns-format))
  15681. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15682. (setq fmt (org-entry-get m "COLUMNS" t)))
  15683. ((and (boundp 'org-columns-current-fmt)
  15684. (local-variable-p 'org-columns-current-fmt)
  15685. org-columns-current-fmt)
  15686. (setq fmt org-columns-current-fmt))
  15687. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15688. (setq m (get-text-property m 'org-hd-marker))
  15689. (setq fmt (org-entry-get m "COLUMNS" t))))
  15690. (setq fmt (or fmt org-columns-default-format))
  15691. (org-set-local 'org-columns-current-fmt fmt)
  15692. (org-columns-compile-format fmt)
  15693. (save-excursion
  15694. ;; Get and cache the properties
  15695. (goto-char (point-min))
  15696. (while (not (eobp))
  15697. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15698. (get-text-property (point) 'org-marker)))
  15699. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15700. (beginning-of-line 2))
  15701. (when cache
  15702. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15703. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15704. (org-columns-display-here-title)
  15705. (mapc (lambda (x)
  15706. (goto-line (car x))
  15707. (org-columns-display-here (cdr x)))
  15708. cache)))))
  15709. (defun org-columns-get-autowidth-alist (s cache)
  15710. "Derive the maximum column widths from the format and the cache."
  15711. (let ((start 0) rtn)
  15712. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15713. (push (cons (match-string 1 s) 1) rtn)
  15714. (setq start (match-end 0)))
  15715. (mapc (lambda (x)
  15716. (setcdr x (apply 'max
  15717. (mapcar
  15718. (lambda (y)
  15719. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15720. cache))))
  15721. rtn)
  15722. rtn))
  15723. (defun org-columns-compute-all ()
  15724. "Compute all columns that have operators defined."
  15725. (org-unmodified
  15726. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15727. (let ((columns org-columns-current-fmt-compiled) col)
  15728. (while (setq col (pop columns))
  15729. (when (nth 3 col)
  15730. (save-excursion
  15731. (org-columns-compute (car col)))))))
  15732. (defun org-columns-update (property)
  15733. "Recompute PROPERTY, and update the columns display for it."
  15734. (org-columns-compute property)
  15735. (let (fmt val pos)
  15736. (save-excursion
  15737. (mapc (lambda (ov)
  15738. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15739. (setq pos (org-overlay-start ov))
  15740. (goto-char pos)
  15741. (when (setq val (cdr (assoc property
  15742. (get-text-property
  15743. (point-at-bol) 'org-summaries))))
  15744. (setq fmt (org-overlay-get ov 'org-columns-format))
  15745. (org-overlay-put ov 'org-columns-value val)
  15746. (org-overlay-put ov 'display (format fmt val)))))
  15747. org-columns-overlays))))
  15748. (defun org-columns-compute (property)
  15749. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15750. (interactive)
  15751. (let* ((re (concat "^" outline-regexp))
  15752. (lmax 30) ; Does anyone use deeper levels???
  15753. (lsum (make-vector lmax 0))
  15754. (lflag (make-vector lmax nil))
  15755. (level 0)
  15756. (ass (assoc property org-columns-current-fmt-compiled))
  15757. (format (nth 4 ass))
  15758. (printf (nth 5 ass))
  15759. (beg org-columns-top-level-marker)
  15760. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15761. (save-excursion
  15762. ;; Find the region to compute
  15763. (goto-char beg)
  15764. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15765. (goto-char end)
  15766. ;; Walk the tree from the back and do the computations
  15767. (while (re-search-backward re beg t)
  15768. (setq sumpos (match-beginning 0)
  15769. last-level level
  15770. level (org-outline-level)
  15771. val (org-entry-get nil property)
  15772. valflag (and val (string-match "\\S-" val)))
  15773. (cond
  15774. ((< level last-level)
  15775. ;; put the sum of lower levels here as a property
  15776. (setq sum (aref lsum last-level) ; current sum
  15777. flag (aref lflag last-level) ; any valid entries from children?
  15778. str (org-column-number-to-string sum format printf)
  15779. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15780. useval (if flag str1 (if valflag val ""))
  15781. sum-alist (get-text-property sumpos 'org-summaries))
  15782. (if (assoc property sum-alist)
  15783. (setcdr (assoc property sum-alist) useval)
  15784. (push (cons property useval) sum-alist)
  15785. (org-unmodified
  15786. (add-text-properties sumpos (1+ sumpos)
  15787. (list 'org-summaries sum-alist))))
  15788. (when val
  15789. (org-entry-put nil property (if flag str val)))
  15790. ;; add current to current level accumulator
  15791. (when (or flag valflag)
  15792. (aset lsum level (+ (aref lsum level)
  15793. (if flag sum (org-column-string-to-number
  15794. (if flag str val) format))))
  15795. (aset lflag level t))
  15796. ;; clear accumulators for deeper levels
  15797. (loop for l from (1+ level) to (1- lmax) do
  15798. (aset lsum l 0)
  15799. (aset lflag l nil)))
  15800. ((>= level last-level)
  15801. ;; add what we have here to the accumulator for this level
  15802. (aset lsum level (+ (aref lsum level)
  15803. (org-column-string-to-number (or val "0") format)))
  15804. (and valflag (aset lflag level t)))
  15805. (t (error "This should not happen")))))))
  15806. (defun org-columns-redo ()
  15807. "Construct the column display again."
  15808. (interactive)
  15809. (message "Recomputing columns...")
  15810. (save-excursion
  15811. (if (marker-position org-columns-begin-marker)
  15812. (goto-char org-columns-begin-marker))
  15813. (org-columns-remove-overlays)
  15814. (if (org-mode-p)
  15815. (call-interactively 'org-columns)
  15816. (call-interactively 'org-agenda-columns)))
  15817. (message "Recomputing columns...done"))
  15818. (defun org-columns-not-in-agenda ()
  15819. (if (eq major-mode 'org-agenda-mode)
  15820. (error "This command is only allowed in Org-mode buffers")))
  15821. (defun org-string-to-number (s)
  15822. "Convert string to number, and interpret hh:mm:ss."
  15823. (if (not (string-match ":" s))
  15824. (string-to-number s)
  15825. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15826. (while l
  15827. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15828. sum)))
  15829. (defun org-column-number-to-string (n fmt &optional printf)
  15830. "Convert a computed column number to a string value, according to FMT."
  15831. (cond
  15832. ((eq fmt 'add_times)
  15833. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15834. (format "%d:%02d" h m)))
  15835. ((eq fmt 'checkbox)
  15836. (cond ((= n (floor n)) "[X]")
  15837. ((> n 1.) "[-]")
  15838. (t "[ ]")))
  15839. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15840. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15841. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15842. (printf (format printf n))
  15843. ((eq fmt 'currency)
  15844. (format "%.2f" n))
  15845. (t (number-to-string n))))
  15846. (defun org-nofm-to-completion (n m &optional percent)
  15847. (if (not percent)
  15848. (format "[%d/%d]" n m)
  15849. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15850. (defun org-column-string-to-number (s fmt)
  15851. "Convert a column value to a number that can be used for column computing."
  15852. (cond
  15853. ((string-match ":" s)
  15854. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15855. (while l
  15856. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15857. sum))
  15858. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15859. (if (equal s "[X]") 1. 0.000001))
  15860. (t (string-to-number s))))
  15861. (defun org-columns-uncompile-format (cfmt)
  15862. "Turn the compiled columns format back into a string representation."
  15863. (let ((rtn "") e s prop title op width fmt printf)
  15864. (while (setq e (pop cfmt))
  15865. (setq prop (car e)
  15866. title (nth 1 e)
  15867. width (nth 2 e)
  15868. op (nth 3 e)
  15869. fmt (nth 4 e)
  15870. printf (nth 5 e))
  15871. (cond
  15872. ((eq fmt 'add_times) (setq op ":"))
  15873. ((eq fmt 'checkbox) (setq op "X"))
  15874. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15875. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15876. ((eq fmt 'add_numbers) (setq op "+"))
  15877. ((eq fmt 'currency) (setq op "$")))
  15878. (if (and op printf) (setq op (concat op ";" printf)))
  15879. (if (equal title prop) (setq title nil))
  15880. (setq s (concat "%" (if width (number-to-string width))
  15881. prop
  15882. (if title (concat "(" title ")"))
  15883. (if op (concat "{" op "}"))))
  15884. (setq rtn (concat rtn " " s)))
  15885. (org-trim rtn)))
  15886. (defun org-columns-compile-format (fmt)
  15887. "Turn a column format string into an alist of specifications.
  15888. The alist has one entry for each column in the format. The elements of
  15889. that list are:
  15890. property the property
  15891. title the title field for the columns
  15892. width the column width in characters, can be nil for automatic
  15893. operator the operator if any
  15894. format the output format for computed results, derived from operator
  15895. printf a printf format for computed values"
  15896. (let ((start 0) width prop title op f printf)
  15897. (setq org-columns-current-fmt-compiled nil)
  15898. (while (string-match
  15899. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15900. fmt start)
  15901. (setq start (match-end 0)
  15902. width (match-string 1 fmt)
  15903. prop (match-string 2 fmt)
  15904. title (or (match-string 3 fmt) prop)
  15905. op (match-string 4 fmt)
  15906. f nil
  15907. printf nil)
  15908. (if width (setq width (string-to-number width)))
  15909. (when (and op (string-match ";" op))
  15910. (setq printf (substring op (match-end 0))
  15911. op (substring op 0 (match-beginning 0))))
  15912. (cond
  15913. ((equal op "+") (setq f 'add_numbers))
  15914. ((equal op "$") (setq f 'currency))
  15915. ((equal op ":") (setq f 'add_times))
  15916. ((equal op "X") (setq f 'checkbox))
  15917. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15918. ((equal op "X%") (setq f 'checkbox-percent))
  15919. )
  15920. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15921. (setq org-columns-current-fmt-compiled
  15922. (nreverse org-columns-current-fmt-compiled))))
  15923. ;;; Dynamic block for Column view
  15924. (defun org-columns-capture-view ()
  15925. "Get the column view of the current buffer and return it as a list.
  15926. The list will contains the title row and all other rows. Each row is
  15927. a list of fields."
  15928. (save-excursion
  15929. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15930. (n (length title)) row tbl)
  15931. (goto-char (point-min))
  15932. (while (re-search-forward "^\\*+ " nil t)
  15933. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15934. (setq row nil)
  15935. (loop for i from 0 to (1- n) do
  15936. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15937. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15938. "")
  15939. row))
  15940. (setq row (nreverse row))
  15941. (push row tbl)))
  15942. (append (list title 'hline) (nreverse tbl)))))
  15943. (defun org-dblock-write:columnview (params)
  15944. "Write the column view table.
  15945. PARAMS is a property list of parameters:
  15946. :width enforce same column widths with <N> specifiers.
  15947. :id the :ID: property of the entry where the columns view
  15948. should be built, as a string. When `local', call locally.
  15949. When `global' call column view with the cursor at the beginning
  15950. of the buffer (usually this means that the whole buffer switches
  15951. to column view).
  15952. :hlines When t, insert a hline before each item. When a number, insert
  15953. a hline before each level <= that number.
  15954. :vlines When t, make each column a colgroup to enforce vertical lines."
  15955. (let ((pos (move-marker (make-marker) (point)))
  15956. (hlines (plist-get params :hlines))
  15957. (vlines (plist-get params :vlines))
  15958. tbl id idpos nfields tmp)
  15959. (save-excursion
  15960. (save-restriction
  15961. (when (setq id (plist-get params :id))
  15962. (cond ((not id) nil)
  15963. ((eq id 'global) (goto-char (point-min)))
  15964. ((eq id 'local) nil)
  15965. ((setq idpos (org-find-entry-with-id id))
  15966. (goto-char idpos))
  15967. (t (error "Cannot find entry with :ID: %s" id))))
  15968. (org-columns)
  15969. (setq tbl (org-columns-capture-view))
  15970. (setq nfields (length (car tbl)))
  15971. (org-columns-quit)))
  15972. (goto-char pos)
  15973. (move-marker pos nil)
  15974. (when tbl
  15975. (when (plist-get params :hlines)
  15976. (setq tmp nil)
  15977. (while tbl
  15978. (if (eq (car tbl) 'hline)
  15979. (push (pop tbl) tmp)
  15980. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15981. (if (and (not (eq (car tmp) 'hline))
  15982. (or (eq hlines t)
  15983. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15984. (push 'hline tmp)))
  15985. (push (pop tbl) tmp)))
  15986. (setq tbl (nreverse tmp)))
  15987. (when vlines
  15988. (setq tbl (mapcar (lambda (x)
  15989. (if (eq 'hline x) x (cons "" x)))
  15990. tbl))
  15991. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15992. (setq pos (point))
  15993. (insert (org-listtable-to-string tbl))
  15994. (when (plist-get params :width)
  15995. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15996. org-columns-current-widths "|")))
  15997. (goto-char pos)
  15998. (org-table-align))))
  15999. (defun org-listtable-to-string (tbl)
  16000. "Convert a listtable TBL to a string that contains the Org-mode table.
  16001. The table still need to be alligned. The resulting string has no leading
  16002. and tailing newline characters."
  16003. (mapconcat
  16004. (lambda (x)
  16005. (cond
  16006. ((listp x)
  16007. (concat "|" (mapconcat 'identity x "|") "|"))
  16008. ((eq x 'hline) "|-|")
  16009. (t (error "Garbage in listtable: %s" x))))
  16010. tbl "\n"))
  16011. (defun org-insert-columns-dblock ()
  16012. "Create a dynamic block capturing a column view table."
  16013. (interactive)
  16014. (let ((defaults '(:name "columnview" :hlines 1))
  16015. (id (completing-read
  16016. "Capture columns (local, global, entry with :ID: property) [local]: "
  16017. (append '(("global") ("local"))
  16018. (mapcar 'list (org-property-values "ID"))))))
  16019. (if (equal id "") (setq id 'local))
  16020. (if (equal id "global") (setq id 'global))
  16021. (setq defaults (append defaults (list :id id)))
  16022. (org-create-dblock defaults)
  16023. (org-update-dblock)))
  16024. ;;;; Timestamps
  16025. (defvar org-last-changed-timestamp nil)
  16026. (defvar org-time-was-given) ; dynamically scoped parameter
  16027. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16028. (defvar org-ts-what) ; dynamically scoped parameter
  16029. (defun org-time-stamp (arg)
  16030. "Prompt for a date/time and insert a time stamp.
  16031. If the user specifies a time like HH:MM, or if this command is called
  16032. with a prefix argument, the time stamp will contain date and time.
  16033. Otherwise, only the date will be included. All parts of a date not
  16034. specified by the user will be filled in from the current date/time.
  16035. So if you press just return without typing anything, the time stamp
  16036. will represent the current date/time. If there is already a timestamp
  16037. at the cursor, it will be modified."
  16038. (interactive "P")
  16039. (let* ((ts nil)
  16040. (default-time
  16041. ;; Default time is either today, or, when entering a range,
  16042. ;; the range start.
  16043. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16044. (save-excursion
  16045. (re-search-backward
  16046. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16047. (- (point) 20) t)))
  16048. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16049. (current-time)))
  16050. (default-input (and ts (org-get-compact-tod ts)))
  16051. org-time-was-given org-end-time-was-given time)
  16052. (cond
  16053. ((and (org-at-timestamp-p)
  16054. (eq last-command 'org-time-stamp)
  16055. (eq this-command 'org-time-stamp))
  16056. (insert "--")
  16057. (setq time (let ((this-command this-command))
  16058. (org-read-date arg 'totime nil nil default-time default-input)))
  16059. (org-insert-time-stamp time (or org-time-was-given arg)))
  16060. ((org-at-timestamp-p)
  16061. (setq time (let ((this-command this-command))
  16062. (org-read-date arg 'totime nil nil default-time default-input)))
  16063. (when (org-at-timestamp-p) ; just to get the match data
  16064. (replace-match "")
  16065. (setq org-last-changed-timestamp
  16066. (org-insert-time-stamp
  16067. time (or org-time-was-given arg)
  16068. nil nil nil (list org-end-time-was-given))))
  16069. (message "Timestamp updated"))
  16070. (t
  16071. (setq time (let ((this-command this-command))
  16072. (org-read-date arg 'totime nil nil default-time default-input)))
  16073. (org-insert-time-stamp time (or org-time-was-given arg)
  16074. nil nil nil (list org-end-time-was-given))))))
  16075. ;; FIXME: can we use this for something else????
  16076. ;; like computing time differences?????
  16077. (defun org-get-compact-tod (s)
  16078. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16079. (let* ((t1 (match-string 1 s))
  16080. (h1 (string-to-number (match-string 2 s)))
  16081. (m1 (string-to-number (match-string 3 s)))
  16082. (t2 (and (match-end 4) (match-string 5 s)))
  16083. (h2 (and t2 (string-to-number (match-string 6 s))))
  16084. (m2 (and t2 (string-to-number (match-string 7 s))))
  16085. dh dm)
  16086. (if (not t2)
  16087. t1
  16088. (setq dh (- h2 h1) dm (- m2 m1))
  16089. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16090. (concat t1 "+" (number-to-string dh)
  16091. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16092. (defun org-time-stamp-inactive (&optional arg)
  16093. "Insert an inactive time stamp.
  16094. An inactive time stamp is enclosed in square brackets instead of angle
  16095. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16096. does not link to the calendar and cannot be changed with the S-cursor keys.
  16097. So these are more for recording a certain time/date."
  16098. (interactive "P")
  16099. (let (org-time-was-given org-end-time-was-given time)
  16100. (setq time (org-read-date arg 'totime))
  16101. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16102. nil nil (list org-end-time-was-given))))
  16103. (defvar org-date-ovl (org-make-overlay 1 1))
  16104. (org-overlay-put org-date-ovl 'face 'org-warning)
  16105. (org-detach-overlay org-date-ovl)
  16106. (defvar org-ans1) ; dynamically scoped parameter
  16107. (defvar org-ans2) ; dynamically scoped parameter
  16108. (defvar org-plain-time-of-day-regexp) ; defined below
  16109. (defvar org-read-date-overlay nil)
  16110. (defvar org-dcst nil) ; dynamically scoped
  16111. (defun org-read-date (&optional with-time to-time from-string prompt
  16112. default-time default-input)
  16113. "Read a date, possibly a time, and make things smooth for the user.
  16114. The prompt will suggest to enter an ISO date, but you can also enter anything
  16115. which will at least partially be understood by `parse-time-string'.
  16116. Unrecognized parts of the date will default to the current day, month, year,
  16117. hour and minute. If this command is called to replace a timestamp at point,
  16118. of to enter the second timestamp of a range, the default time is taken from the
  16119. existing stamp. For example,
  16120. 3-2-5 --> 2003-02-05
  16121. feb 15 --> currentyear-02-15
  16122. sep 12 9 --> 2009-09-12
  16123. 12:45 --> today 12:45
  16124. 22 sept 0:34 --> currentyear-09-22 0:34
  16125. 12 --> currentyear-currentmonth-12
  16126. Fri --> nearest Friday (today or later)
  16127. etc.
  16128. Furthermore you can specify a relative date by giving, as the *first* thing
  16129. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16130. change in days weeks, months, years.
  16131. With a single plus or minus, the date is relative to today. With a double
  16132. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16133. +4d --> four days from today
  16134. +4 --> same as above
  16135. +2w --> two weeks from today
  16136. ++5 --> five days from default date
  16137. The function understands only English month and weekday abbreviations,
  16138. but this can be configured with the variables `parse-time-months' and
  16139. `parse-time-weekdays'.
  16140. While prompting, a calendar is popped up - you can also select the
  16141. date with the mouse (button 1). The calendar shows a period of three
  16142. months. To scroll it to other months, use the keys `>' and `<'.
  16143. If you don't like the calendar, turn it off with
  16144. \(setq org-read-date-popup-calendar nil)
  16145. With optional argument TO-TIME, the date will immediately be converted
  16146. to an internal time.
  16147. With an optional argument WITH-TIME, the prompt will suggest to also
  16148. insert a time. Note that when WITH-TIME is not set, you can still
  16149. enter a time, and this function will inform the calling routine about
  16150. this change. The calling routine may then choose to change the format
  16151. used to insert the time stamp into the buffer to include the time.
  16152. With optional argument FROM-STRING, read from this string instead from
  16153. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16154. the time/date that is used for everything that is not specified by the
  16155. user."
  16156. (require 'parse-time)
  16157. (let* ((org-time-stamp-rounding-minutes
  16158. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16159. (org-dcst org-display-custom-times)
  16160. (ct (org-current-time))
  16161. (def (or default-time ct))
  16162. (defdecode (decode-time def))
  16163. (dummy (progn
  16164. (when (< (nth 2 defdecode) org-extend-today-until)
  16165. (setcar (nthcdr 2 defdecode) -1)
  16166. (setcar (nthcdr 1 defdecode) 59)
  16167. (setq def (apply 'encode-time defdecode)
  16168. defdecode (decode-time def)))))
  16169. (calendar-move-hook nil)
  16170. (view-diary-entries-initially nil)
  16171. (view-calendar-holidays-initially nil)
  16172. (timestr (format-time-string
  16173. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16174. (prompt (concat (if prompt (concat prompt " ") "")
  16175. (format "Date+time [%s]: " timestr)))
  16176. ans (org-ans0 "") org-ans1 org-ans2 final)
  16177. (cond
  16178. (from-string (setq ans from-string))
  16179. (org-read-date-popup-calendar
  16180. (save-excursion
  16181. (save-window-excursion
  16182. (calendar)
  16183. (calendar-forward-day (- (time-to-days def)
  16184. (calendar-absolute-from-gregorian
  16185. (calendar-current-date))))
  16186. (org-eval-in-calendar nil t)
  16187. (let* ((old-map (current-local-map))
  16188. (map (copy-keymap calendar-mode-map))
  16189. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16190. (org-defkey map (kbd "RET") 'org-calendar-select)
  16191. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16192. 'org-calendar-select-mouse)
  16193. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16194. 'org-calendar-select-mouse)
  16195. (org-defkey minibuffer-local-map [(meta shift left)]
  16196. (lambda () (interactive)
  16197. (org-eval-in-calendar '(calendar-backward-month 1))))
  16198. (org-defkey minibuffer-local-map [(meta shift right)]
  16199. (lambda () (interactive)
  16200. (org-eval-in-calendar '(calendar-forward-month 1))))
  16201. (org-defkey minibuffer-local-map [(meta shift up)]
  16202. (lambda () (interactive)
  16203. (org-eval-in-calendar '(calendar-backward-year 1))))
  16204. (org-defkey minibuffer-local-map [(meta shift down)]
  16205. (lambda () (interactive)
  16206. (org-eval-in-calendar '(calendar-forward-year 1))))
  16207. (org-defkey minibuffer-local-map [(shift up)]
  16208. (lambda () (interactive)
  16209. (org-eval-in-calendar '(calendar-backward-week 1))))
  16210. (org-defkey minibuffer-local-map [(shift down)]
  16211. (lambda () (interactive)
  16212. (org-eval-in-calendar '(calendar-forward-week 1))))
  16213. (org-defkey minibuffer-local-map [(shift left)]
  16214. (lambda () (interactive)
  16215. (org-eval-in-calendar '(calendar-backward-day 1))))
  16216. (org-defkey minibuffer-local-map [(shift right)]
  16217. (lambda () (interactive)
  16218. (org-eval-in-calendar '(calendar-forward-day 1))))
  16219. (org-defkey minibuffer-local-map ">"
  16220. (lambda () (interactive)
  16221. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16222. (org-defkey minibuffer-local-map "<"
  16223. (lambda () (interactive)
  16224. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16225. (unwind-protect
  16226. (progn
  16227. (use-local-map map)
  16228. (add-hook 'post-command-hook 'org-read-date-display)
  16229. (setq org-ans0 (read-string prompt default-input nil nil))
  16230. ;; org-ans0: from prompt
  16231. ;; org-ans1: from mouse click
  16232. ;; org-ans2: from calendar motion
  16233. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16234. (remove-hook 'post-command-hook 'org-read-date-display)
  16235. (use-local-map old-map)
  16236. (when org-read-date-overlay
  16237. (org-delete-overlay org-read-date-overlay)
  16238. (setq org-read-date-overlay nil)))))))
  16239. (t ; Naked prompt only
  16240. (unwind-protect
  16241. (setq ans (read-string prompt default-input nil timestr))
  16242. (when org-read-date-overlay
  16243. (org-delete-overlay org-read-date-overlay)
  16244. (setq org-read-date-overlay nil)))))
  16245. (setq final (org-read-date-analyze ans def defdecode))
  16246. (if to-time
  16247. (apply 'encode-time final)
  16248. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16249. (format "%04d-%02d-%02d %02d:%02d"
  16250. (nth 5 final) (nth 4 final) (nth 3 final)
  16251. (nth 2 final) (nth 1 final))
  16252. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16253. (defvar def)
  16254. (defvar defdecode)
  16255. (defvar with-time)
  16256. (defun org-read-date-display ()
  16257. "Display the currrent date prompt interpretation in the minibuffer."
  16258. (when org-read-date-display-live
  16259. (when org-read-date-overlay
  16260. (org-delete-overlay org-read-date-overlay))
  16261. (let ((p (point)))
  16262. (end-of-line 1)
  16263. (while (not (equal (buffer-substring
  16264. (max (point-min) (- (point) 4)) (point))
  16265. " "))
  16266. (insert " "))
  16267. (goto-char p))
  16268. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16269. " " (or org-ans1 org-ans2)))
  16270. (org-end-time-was-given nil)
  16271. (f (org-read-date-analyze ans def defdecode))
  16272. (fmts (if org-dcst
  16273. org-time-stamp-custom-formats
  16274. org-time-stamp-formats))
  16275. (fmt (if (or with-time
  16276. (and (boundp 'org-time-was-given) org-time-was-given))
  16277. (cdr fmts)
  16278. (car fmts)))
  16279. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16280. (when (and org-end-time-was-given
  16281. (string-match org-plain-time-of-day-regexp txt))
  16282. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16283. org-end-time-was-given
  16284. (substring txt (match-end 0)))))
  16285. (setq org-read-date-overlay
  16286. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16287. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16288. (defun org-read-date-analyze (ans def defdecode)
  16289. "Analyze the combined answer of the date prompt."
  16290. ;; FIXME: cleanup and comment
  16291. (let (delta deltan deltaw deltadef year month day
  16292. hour minute second wday pm h2 m2 tl wday1)
  16293. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16294. (setq ans (replace-match "" t t ans)
  16295. deltan (car delta)
  16296. deltaw (nth 1 delta)
  16297. deltadef (nth 2 delta)))
  16298. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16299. (when (string-match
  16300. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16301. (setq year (if (match-end 2)
  16302. (string-to-number (match-string 2 ans))
  16303. (string-to-number (format-time-string "%Y")))
  16304. month (string-to-number (match-string 3 ans))
  16305. day (string-to-number (match-string 4 ans)))
  16306. (if (< year 100) (setq year (+ 2000 year)))
  16307. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16308. t nil ans)))
  16309. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16310. ;; If there is a time with am/pm, and *no* time without it, we convert
  16311. ;; so that matching will be successful.
  16312. (loop for i from 1 to 2 do ; twice, for end time as well
  16313. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16314. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16315. (setq hour (string-to-number (match-string 1 ans))
  16316. minute (if (match-end 3)
  16317. (string-to-number (match-string 3 ans))
  16318. 0)
  16319. pm (equal ?p
  16320. (string-to-char (downcase (match-string 4 ans)))))
  16321. (if (and (= hour 12) (not pm))
  16322. (setq hour 0)
  16323. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16324. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16325. t t ans))))
  16326. ;; Check if a time range is given as a duration
  16327. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16328. (setq hour (string-to-number (match-string 1 ans))
  16329. h2 (+ hour (string-to-number (match-string 3 ans)))
  16330. minute (string-to-number (match-string 2 ans))
  16331. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16332. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16333. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16334. ;; Check if there is a time range
  16335. (when (boundp 'org-end-time-was-given)
  16336. (setq org-time-was-given nil)
  16337. (when (and (string-match org-plain-time-of-day-regexp ans)
  16338. (match-end 8))
  16339. (setq org-end-time-was-given (match-string 8 ans))
  16340. (setq ans (concat (substring ans 0 (match-beginning 7))
  16341. (substring ans (match-end 7))))))
  16342. (setq tl (parse-time-string ans)
  16343. day (or (nth 3 tl) (nth 3 defdecode))
  16344. month (or (nth 4 tl)
  16345. (if (and org-read-date-prefer-future
  16346. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16347. (1+ (nth 4 defdecode))
  16348. (nth 4 defdecode)))
  16349. year (or (nth 5 tl)
  16350. (if (and org-read-date-prefer-future
  16351. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16352. (1+ (nth 5 defdecode))
  16353. (nth 5 defdecode)))
  16354. hour (or (nth 2 tl) (nth 2 defdecode))
  16355. minute (or (nth 1 tl) (nth 1 defdecode))
  16356. second (or (nth 0 tl) 0)
  16357. wday (nth 6 tl))
  16358. (when deltan
  16359. (unless deltadef
  16360. (let ((now (decode-time (current-time))))
  16361. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16362. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16363. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16364. ((equal deltaw "m") (setq month (+ month deltan)))
  16365. ((equal deltaw "y") (setq year (+ year deltan)))))
  16366. (when (and wday (not (nth 3 tl)))
  16367. ;; Weekday was given, but no day, so pick that day in the week
  16368. ;; on or after the derived date.
  16369. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16370. (unless (equal wday wday1)
  16371. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16372. (if (and (boundp 'org-time-was-given)
  16373. (nth 2 tl))
  16374. (setq org-time-was-given t))
  16375. (if (< year 100) (setq year (+ 2000 year)))
  16376. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16377. (list second minute hour day month year)))
  16378. (defvar parse-time-weekdays)
  16379. (defun org-read-date-get-relative (s today default)
  16380. "Check string S for special relative date string.
  16381. TODAY and DEFAULT are internal times, for today and for a default.
  16382. Return shift list (N what def-flag)
  16383. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16384. N is the number of WHATs to shift.
  16385. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16386. the DEFAULT date rather than TODAY."
  16387. (when (string-match
  16388. (concat
  16389. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16390. "\\([0-9]+\\)?"
  16391. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16392. "\\([ \t]\\|$\\)") s)
  16393. (let* ((dir (if (match-end 1)
  16394. (string-to-char (substring (match-string 1 s) -1))
  16395. ?+))
  16396. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16397. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16398. (what (if (match-end 3) (match-string 3 s) "d"))
  16399. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16400. (date (if rel default today))
  16401. (wday (nth 6 (decode-time date)))
  16402. delta)
  16403. (if wday1
  16404. (progn
  16405. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16406. (if (= dir ?-) (setq delta (- delta 7)))
  16407. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16408. (list delta "d" rel))
  16409. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16410. (defun org-eval-in-calendar (form &optional keepdate)
  16411. "Eval FORM in the calendar window and return to current window.
  16412. Also, store the cursor date in variable org-ans2."
  16413. (let ((sw (selected-window)))
  16414. (select-window (get-buffer-window "*Calendar*"))
  16415. (eval form)
  16416. (when (and (not keepdate) (calendar-cursor-to-date))
  16417. (let* ((date (calendar-cursor-to-date))
  16418. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16419. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16420. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16421. (select-window sw)))
  16422. ; ;; Update the prompt to show new default date
  16423. ; (save-excursion
  16424. ; (goto-char (point-min))
  16425. ; (when (and org-ans2
  16426. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16427. ; (get-text-property (match-end 0) 'field))
  16428. ; (let ((inhibit-read-only t))
  16429. ; (replace-match (concat "[" org-ans2 "]") t t)
  16430. ; (add-text-properties (point-min) (1+ (match-end 0))
  16431. ; (text-properties-at (1+ (point-min)))))))))
  16432. (defun org-calendar-select ()
  16433. "Return to `org-read-date' with the date currently selected.
  16434. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16435. (interactive)
  16436. (when (calendar-cursor-to-date)
  16437. (let* ((date (calendar-cursor-to-date))
  16438. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16439. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16440. (if (active-minibuffer-window) (exit-minibuffer))))
  16441. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16442. "Insert a date stamp for the date given by the internal TIME.
  16443. WITH-HM means, use the stamp format that includes the time of the day.
  16444. INACTIVE means use square brackets instead of angular ones, so that the
  16445. stamp will not contribute to the agenda.
  16446. PRE and POST are optional strings to be inserted before and after the
  16447. stamp.
  16448. The command returns the inserted time stamp."
  16449. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16450. stamp)
  16451. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16452. (insert-before-markers (or pre ""))
  16453. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16454. (when (listp extra)
  16455. (setq extra (car extra))
  16456. (if (and (stringp extra)
  16457. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16458. (setq extra (format "-%02d:%02d"
  16459. (string-to-number (match-string 1 extra))
  16460. (string-to-number (match-string 2 extra))))
  16461. (setq extra nil)))
  16462. (when extra
  16463. (backward-char 1)
  16464. (insert-before-markers extra)
  16465. (forward-char 1))
  16466. (insert-before-markers (or post ""))
  16467. stamp))
  16468. (defun org-toggle-time-stamp-overlays ()
  16469. "Toggle the use of custom time stamp formats."
  16470. (interactive)
  16471. (setq org-display-custom-times (not org-display-custom-times))
  16472. (unless org-display-custom-times
  16473. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16474. (while (setq p (next-single-property-change p 'display))
  16475. (if (and (get-text-property p 'display)
  16476. (eq (get-text-property p 'face) 'org-date))
  16477. (remove-text-properties
  16478. p (setq p (next-single-property-change p 'display))
  16479. '(display t))))
  16480. (set-buffer-modified-p bmp)))
  16481. (if (featurep 'xemacs)
  16482. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16483. (org-restart-font-lock)
  16484. (setq org-table-may-need-update t)
  16485. (if org-display-custom-times
  16486. (message "Time stamps are overlayed with custom format")
  16487. (message "Time stamp overlays removed")))
  16488. (defun org-display-custom-time (beg end)
  16489. "Overlay modified time stamp format over timestamp between BED and END."
  16490. (let* ((ts (buffer-substring beg end))
  16491. t1 w1 with-hm tf time str w2 (off 0))
  16492. (save-match-data
  16493. (setq t1 (org-parse-time-string ts t))
  16494. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16495. (setq off (- (match-end 0) (match-beginning 0)))))
  16496. (setq end (- end off))
  16497. (setq w1 (- end beg)
  16498. with-hm (and (nth 1 t1) (nth 2 t1))
  16499. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16500. time (org-fix-decoded-time t1)
  16501. str (org-add-props
  16502. (format-time-string
  16503. (substring tf 1 -1) (apply 'encode-time time))
  16504. nil 'mouse-face 'highlight)
  16505. w2 (length str))
  16506. (if (not (= w2 w1))
  16507. (add-text-properties (1+ beg) (+ 2 beg)
  16508. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16509. (if (featurep 'xemacs)
  16510. (progn
  16511. (put-text-property beg end 'invisible t)
  16512. (put-text-property beg end 'end-glyph (make-glyph str)))
  16513. (put-text-property beg end 'display str))))
  16514. (defun org-translate-time (string)
  16515. "Translate all timestamps in STRING to custom format.
  16516. But do this only if the variable `org-display-custom-times' is set."
  16517. (when org-display-custom-times
  16518. (save-match-data
  16519. (let* ((start 0)
  16520. (re org-ts-regexp-both)
  16521. t1 with-hm inactive tf time str beg end)
  16522. (while (setq start (string-match re string start))
  16523. (setq beg (match-beginning 0)
  16524. end (match-end 0)
  16525. t1 (save-match-data
  16526. (org-parse-time-string (substring string beg end) t))
  16527. with-hm (and (nth 1 t1) (nth 2 t1))
  16528. inactive (equal (substring string beg (1+ beg)) "[")
  16529. tf (funcall (if with-hm 'cdr 'car)
  16530. org-time-stamp-custom-formats)
  16531. time (org-fix-decoded-time t1)
  16532. str (format-time-string
  16533. (concat
  16534. (if inactive "[" "<") (substring tf 1 -1)
  16535. (if inactive "]" ">"))
  16536. (apply 'encode-time time))
  16537. string (replace-match str t t string)
  16538. start (+ start (length str)))))))
  16539. string)
  16540. (defun org-fix-decoded-time (time)
  16541. "Set 0 instead of nil for the first 6 elements of time.
  16542. Don't touch the rest."
  16543. (let ((n 0))
  16544. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16545. (defun org-days-to-time (timestamp-string)
  16546. "Difference between TIMESTAMP-STRING and now in days."
  16547. (- (time-to-days (org-time-string-to-time timestamp-string))
  16548. (time-to-days (current-time))))
  16549. (defun org-deadline-close (timestamp-string &optional ndays)
  16550. "Is the time in TIMESTAMP-STRING close to the current date?"
  16551. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16552. (and (< (org-days-to-time timestamp-string) ndays)
  16553. (not (org-entry-is-done-p))))
  16554. (defun org-get-wdays (ts)
  16555. "Get the deadline lead time appropriate for timestring TS."
  16556. (cond
  16557. ((<= org-deadline-warning-days 0)
  16558. ;; 0 or negative, enforce this value no matter what
  16559. (- org-deadline-warning-days))
  16560. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16561. ;; lead time is specified.
  16562. (floor (* (string-to-number (match-string 1 ts))
  16563. (cdr (assoc (match-string 2 ts)
  16564. '(("d" . 1) ("w" . 7)
  16565. ("m" . 30.4) ("y" . 365.25)))))))
  16566. ;; go for the default.
  16567. (t org-deadline-warning-days)))
  16568. (defun org-calendar-select-mouse (ev)
  16569. "Return to `org-read-date' with the date currently selected.
  16570. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16571. (interactive "e")
  16572. (mouse-set-point ev)
  16573. (when (calendar-cursor-to-date)
  16574. (let* ((date (calendar-cursor-to-date))
  16575. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16576. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16577. (if (active-minibuffer-window) (exit-minibuffer))))
  16578. (defun org-check-deadlines (ndays)
  16579. "Check if there are any deadlines due or past due.
  16580. A deadline is considered due if it happens within `org-deadline-warning-days'
  16581. days from today's date. If the deadline appears in an entry marked DONE,
  16582. it is not shown. The prefix arg NDAYS can be used to test that many
  16583. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16584. (interactive "P")
  16585. (let* ((org-warn-days
  16586. (cond
  16587. ((equal ndays '(4)) 100000)
  16588. (ndays (prefix-numeric-value ndays))
  16589. (t (abs org-deadline-warning-days))))
  16590. (case-fold-search nil)
  16591. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16592. (callback
  16593. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16594. (message "%d deadlines past-due or due within %d days"
  16595. (org-occur regexp nil callback)
  16596. org-warn-days)))
  16597. (defun org-check-before-date (date)
  16598. "Check if there are deadlines or scheduled entries before DATE."
  16599. (interactive (list (org-read-date)))
  16600. (let ((case-fold-search nil)
  16601. (regexp (concat "\\<\\(" org-deadline-string
  16602. "\\|" org-scheduled-string
  16603. "\\) *<\\([^>]+\\)>"))
  16604. (callback
  16605. (lambda () (time-less-p
  16606. (org-time-string-to-time (match-string 2))
  16607. (org-time-string-to-time date)))))
  16608. (message "%d entries before %s"
  16609. (org-occur regexp nil callback) date)))
  16610. (defun org-evaluate-time-range (&optional to-buffer)
  16611. "Evaluate a time range by computing the difference between start and end.
  16612. Normally the result is just printed in the echo area, but with prefix arg
  16613. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16614. If the time range is actually in a table, the result is inserted into the
  16615. next column.
  16616. For time difference computation, a year is assumed to be exactly 365
  16617. days in order to avoid rounding problems."
  16618. (interactive "P")
  16619. (or
  16620. (org-clock-update-time-maybe)
  16621. (save-excursion
  16622. (unless (org-at-date-range-p t)
  16623. (goto-char (point-at-bol))
  16624. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16625. (if (not (org-at-date-range-p t))
  16626. (error "Not at a time-stamp range, and none found in current line")))
  16627. (let* ((ts1 (match-string 1))
  16628. (ts2 (match-string 2))
  16629. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16630. (match-end (match-end 0))
  16631. (time1 (org-time-string-to-time ts1))
  16632. (time2 (org-time-string-to-time ts2))
  16633. (t1 (time-to-seconds time1))
  16634. (t2 (time-to-seconds time2))
  16635. (diff (abs (- t2 t1)))
  16636. (negative (< (- t2 t1) 0))
  16637. ;; (ys (floor (* 365 24 60 60)))
  16638. (ds (* 24 60 60))
  16639. (hs (* 60 60))
  16640. (fy "%dy %dd %02d:%02d")
  16641. (fy1 "%dy %dd")
  16642. (fd "%dd %02d:%02d")
  16643. (fd1 "%dd")
  16644. (fh "%02d:%02d")
  16645. y d h m align)
  16646. (if havetime
  16647. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16648. y 0
  16649. d (floor (/ diff ds)) diff (mod diff ds)
  16650. h (floor (/ diff hs)) diff (mod diff hs)
  16651. m (floor (/ diff 60)))
  16652. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16653. y 0
  16654. d (floor (+ (/ diff ds) 0.5))
  16655. h 0 m 0))
  16656. (if (not to-buffer)
  16657. (message "%s" (org-make-tdiff-string y d h m))
  16658. (if (org-at-table-p)
  16659. (progn
  16660. (goto-char match-end)
  16661. (setq align t)
  16662. (and (looking-at " *|") (goto-char (match-end 0))))
  16663. (goto-char match-end))
  16664. (if (looking-at
  16665. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16666. (replace-match ""))
  16667. (if negative (insert " -"))
  16668. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16669. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16670. (insert " " (format fh h m))))
  16671. (if align (org-table-align))
  16672. (message "Time difference inserted")))))
  16673. (defun org-make-tdiff-string (y d h m)
  16674. (let ((fmt "")
  16675. (l nil))
  16676. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16677. l (push y l)))
  16678. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16679. l (push d l)))
  16680. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16681. l (push h l)))
  16682. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16683. l (push m l)))
  16684. (apply 'format fmt (nreverse l))))
  16685. (defun org-time-string-to-time (s)
  16686. (apply 'encode-time (org-parse-time-string s)))
  16687. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16688. "Convert a time stamp to an absolute day number.
  16689. If there is a specifyer for a cyclic time stamp, get the closest date to
  16690. DAYNR."
  16691. (cond
  16692. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16693. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16694. daynr
  16695. (+ daynr 1000)))
  16696. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16697. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16698. (time-to-days (current-time))) (match-string 0 s)
  16699. prefer))
  16700. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16701. (defun org-time-from-absolute (d)
  16702. "Return the time corresponding to date D.
  16703. D may be an absolute day number, or a calendar-type list (month day year)."
  16704. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16705. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16706. (defun org-calendar-holiday ()
  16707. "List of holidays, for Diary display in Org-mode."
  16708. (require 'holidays)
  16709. (let ((hl (funcall
  16710. (if (fboundp 'calendar-check-holidays)
  16711. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16712. (if hl (mapconcat 'identity hl "; "))))
  16713. (defun org-diary-sexp-entry (sexp entry date)
  16714. "Process a SEXP diary ENTRY for DATE."
  16715. (require 'diary-lib)
  16716. (let ((result (if calendar-debug-sexp
  16717. (let ((stack-trace-on-error t))
  16718. (eval (car (read-from-string sexp))))
  16719. (condition-case nil
  16720. (eval (car (read-from-string sexp)))
  16721. (error
  16722. (beep)
  16723. (message "Bad sexp at line %d in %s: %s"
  16724. (org-current-line)
  16725. (buffer-file-name) sexp)
  16726. (sleep-for 2))))))
  16727. (cond ((stringp result) result)
  16728. ((and (consp result)
  16729. (stringp (cdr result))) (cdr result))
  16730. (result entry)
  16731. (t nil))))
  16732. (defun org-diary-to-ical-string (frombuf)
  16733. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16734. This uses the icalendar.el library."
  16735. (let* ((tmpdir (if (featurep 'xemacs)
  16736. (temp-directory)
  16737. temporary-file-directory))
  16738. (tmpfile (make-temp-name
  16739. (expand-file-name "orgics" tmpdir)))
  16740. buf rtn b e)
  16741. (save-excursion
  16742. (set-buffer frombuf)
  16743. (icalendar-export-region (point-min) (point-max) tmpfile)
  16744. (setq buf (find-buffer-visiting tmpfile))
  16745. (set-buffer buf)
  16746. (goto-char (point-min))
  16747. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16748. (setq b (match-beginning 0)))
  16749. (goto-char (point-max))
  16750. (if (re-search-backward "^END:VEVENT" nil t)
  16751. (setq e (match-end 0)))
  16752. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16753. (kill-buffer buf)
  16754. (kill-buffer frombuf)
  16755. (delete-file tmpfile)
  16756. rtn))
  16757. (defun org-closest-date (start current change prefer)
  16758. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16759. When PREFER is `past' return a date that is either CURRENT or past.
  16760. When PREFER is `future', return a date that is either CURRENT or future."
  16761. ;; Make the proper lists from the dates
  16762. (catch 'exit
  16763. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16764. dn dw sday cday n1 n2
  16765. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16766. (setq start (org-date-to-gregorian start)
  16767. current (org-date-to-gregorian
  16768. (if org-agenda-repeating-timestamp-show-all
  16769. current
  16770. (time-to-days (current-time))))
  16771. sday (calendar-absolute-from-gregorian start)
  16772. cday (calendar-absolute-from-gregorian current))
  16773. (if (<= cday sday) (throw 'exit sday))
  16774. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16775. (setq dn (string-to-number (match-string 1 change))
  16776. dw (cdr (assoc (match-string 2 change) a1)))
  16777. (error "Invalid change specifyer: %s" change))
  16778. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16779. (cond
  16780. ((eq dw 'day)
  16781. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16782. n2 (+ n1 dn)))
  16783. ((eq dw 'year)
  16784. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16785. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16786. (setq date1 (list m d y1)
  16787. n1 (calendar-absolute-from-gregorian date1)
  16788. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16789. n2 (calendar-absolute-from-gregorian date2)))
  16790. ((eq dw 'month)
  16791. ;; approx number of month between the tow dates
  16792. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16793. ;; How often does dn fit in there?
  16794. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16795. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16796. m (+ m nm)
  16797. ny (floor (/ m 12))
  16798. y (+ y ny)
  16799. m (- m (* ny 12)))
  16800. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16801. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16802. (setq m2 (+ m dn) y2 y)
  16803. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16804. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16805. (while (< n2 cday)
  16806. (setq n1 n2 m m2 y y2)
  16807. (setq m2 (+ m dn) y2 y)
  16808. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16809. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16810. (if org-agenda-repeating-timestamp-show-all
  16811. (cond
  16812. ((eq prefer 'past) n1)
  16813. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16814. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16815. (cond
  16816. ((eq prefer 'past) n1)
  16817. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16818. (t (if (= cday n1) n1 n2)))))))
  16819. (defun org-date-to-gregorian (date)
  16820. "Turn any specification of DATE into a gregorian date for the calendar."
  16821. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16822. ((and (listp date) (= (length date) 3)) date)
  16823. ((stringp date)
  16824. (setq date (org-parse-time-string date))
  16825. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16826. ((listp date)
  16827. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16828. (defun org-parse-time-string (s &optional nodefault)
  16829. "Parse the standard Org-mode time string.
  16830. This should be a lot faster than the normal `parse-time-string'.
  16831. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16832. hour and minute fields will be nil if not given."
  16833. (if (string-match org-ts-regexp0 s)
  16834. (list 0
  16835. (if (or (match-beginning 8) (not nodefault))
  16836. (string-to-number (or (match-string 8 s) "0")))
  16837. (if (or (match-beginning 7) (not nodefault))
  16838. (string-to-number (or (match-string 7 s) "0")))
  16839. (string-to-number (match-string 4 s))
  16840. (string-to-number (match-string 3 s))
  16841. (string-to-number (match-string 2 s))
  16842. nil nil nil)
  16843. (make-list 9 0)))
  16844. (defun org-timestamp-up (&optional arg)
  16845. "Increase the date item at the cursor by one.
  16846. If the cursor is on the year, change the year. If it is on the month or
  16847. the day, change that.
  16848. With prefix ARG, change by that many units."
  16849. (interactive "p")
  16850. (org-timestamp-change (prefix-numeric-value arg)))
  16851. (defun org-timestamp-down (&optional arg)
  16852. "Decrease the date item at the cursor by one.
  16853. If the cursor is on the year, change the year. If it is on the month or
  16854. the day, change that.
  16855. With prefix ARG, change by that many units."
  16856. (interactive "p")
  16857. (org-timestamp-change (- (prefix-numeric-value arg))))
  16858. (defun org-timestamp-up-day (&optional arg)
  16859. "Increase the date in the time stamp by one day.
  16860. With prefix ARG, change that many days."
  16861. (interactive "p")
  16862. (if (and (not (org-at-timestamp-p t))
  16863. (org-on-heading-p))
  16864. (org-todo 'up)
  16865. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16866. (defun org-timestamp-down-day (&optional arg)
  16867. "Decrease the date in the time stamp by one day.
  16868. With prefix ARG, change that many days."
  16869. (interactive "p")
  16870. (if (and (not (org-at-timestamp-p t))
  16871. (org-on-heading-p))
  16872. (org-todo 'down)
  16873. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16874. (defsubst org-pos-in-match-range (pos n)
  16875. (and (match-beginning n)
  16876. (<= (match-beginning n) pos)
  16877. (>= (match-end n) pos)))
  16878. (defun org-at-timestamp-p (&optional inactive-ok)
  16879. "Determine if the cursor is in or at a timestamp."
  16880. (interactive)
  16881. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16882. (pos (point))
  16883. (ans (or (looking-at tsr)
  16884. (save-excursion
  16885. (skip-chars-backward "^[<\n\r\t")
  16886. (if (> (point) (point-min)) (backward-char 1))
  16887. (and (looking-at tsr)
  16888. (> (- (match-end 0) pos) -1))))))
  16889. (and ans
  16890. (boundp 'org-ts-what)
  16891. (setq org-ts-what
  16892. (cond
  16893. ((= pos (match-beginning 0)) 'bracket)
  16894. ((= pos (1- (match-end 0))) 'bracket)
  16895. ((org-pos-in-match-range pos 2) 'year)
  16896. ((org-pos-in-match-range pos 3) 'month)
  16897. ((org-pos-in-match-range pos 7) 'hour)
  16898. ((org-pos-in-match-range pos 8) 'minute)
  16899. ((or (org-pos-in-match-range pos 4)
  16900. (org-pos-in-match-range pos 5)) 'day)
  16901. ((and (> pos (or (match-end 8) (match-end 5)))
  16902. (< pos (match-end 0)))
  16903. (- pos (or (match-end 8) (match-end 5))))
  16904. (t 'day))))
  16905. ans))
  16906. (defun org-toggle-timestamp-type ()
  16907. ""
  16908. (interactive)
  16909. (when (org-at-timestamp-p t)
  16910. (save-excursion
  16911. (goto-char (match-beginning 0))
  16912. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16913. (goto-char (1- (match-end 0)))
  16914. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16915. (message "Timestamp is now %sactive"
  16916. (if (equal (char-before) ?>) "in" ""))))
  16917. (defun org-timestamp-change (n &optional what)
  16918. "Change the date in the time stamp at point.
  16919. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16920. `year', `minute', `second'. If WHAT is not given, the cursor position
  16921. in the timestamp determines what will be changed."
  16922. (let ((pos (point))
  16923. with-hm inactive
  16924. org-ts-what
  16925. extra
  16926. ts time time0)
  16927. (if (not (org-at-timestamp-p t))
  16928. (error "Not at a timestamp"))
  16929. (if (and (not what) (eq org-ts-what 'bracket))
  16930. (org-toggle-timestamp-type)
  16931. (if (and (not what) (not (eq org-ts-what 'day))
  16932. org-display-custom-times
  16933. (get-text-property (point) 'display)
  16934. (not (get-text-property (1- (point)) 'display)))
  16935. (setq org-ts-what 'day))
  16936. (setq org-ts-what (or what org-ts-what)
  16937. inactive (= (char-after (match-beginning 0)) ?\[)
  16938. ts (match-string 0))
  16939. (replace-match "")
  16940. (if (string-match
  16941. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16942. ts)
  16943. (setq extra (match-string 1 ts)))
  16944. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16945. (setq with-hm t))
  16946. (setq time0 (org-parse-time-string ts))
  16947. (setq time
  16948. (encode-time (or (car time0) 0)
  16949. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16950. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16951. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16952. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16953. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16954. (nthcdr 6 time0)))
  16955. (when (integerp org-ts-what)
  16956. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16957. (if (eq what 'calendar)
  16958. (let ((cal-date (org-get-date-from-calendar)))
  16959. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16960. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16961. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16962. (setcar time0 (or (car time0) 0))
  16963. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16964. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16965. (setq time (apply 'encode-time time0))))
  16966. (setq org-last-changed-timestamp
  16967. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16968. (org-clock-update-time-maybe)
  16969. (goto-char pos)
  16970. ;; Try to recenter the calendar window, if any
  16971. (if (and org-calendar-follow-timestamp-change
  16972. (get-buffer-window "*Calendar*" t)
  16973. (memq org-ts-what '(day month year)))
  16974. (org-recenter-calendar (time-to-days time))))))
  16975. ;; FIXME: does not yet work for lead times
  16976. (defun org-modify-ts-extra (s pos n)
  16977. "Change the different parts of the lead-time and repeat fields in timestamp."
  16978. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16979. ng h m new)
  16980. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16981. (cond
  16982. ((or (org-pos-in-match-range pos 2)
  16983. (org-pos-in-match-range pos 3))
  16984. (setq m (string-to-number (match-string 3 s))
  16985. h (string-to-number (match-string 2 s)))
  16986. (if (org-pos-in-match-range pos 2)
  16987. (setq h (+ h n))
  16988. (setq m (+ m n)))
  16989. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16990. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16991. (setq h (min 24 (max 0 h)))
  16992. (setq ng 1 new (format "-%02d:%02d" h m)))
  16993. ((org-pos-in-match-range pos 6)
  16994. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16995. ((org-pos-in-match-range pos 5)
  16996. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16997. (when ng
  16998. (setq s (concat
  16999. (substring s 0 (match-beginning ng))
  17000. new
  17001. (substring s (match-end ng))))))
  17002. s))
  17003. (defun org-recenter-calendar (date)
  17004. "If the calendar is visible, recenter it to DATE."
  17005. (let* ((win (selected-window))
  17006. (cwin (get-buffer-window "*Calendar*" t))
  17007. (calendar-move-hook nil))
  17008. (when cwin
  17009. (select-window cwin)
  17010. (calendar-goto-date (if (listp date) date
  17011. (calendar-gregorian-from-absolute date)))
  17012. (select-window win))))
  17013. (defun org-goto-calendar (&optional arg)
  17014. "Go to the Emacs calendar at the current date.
  17015. If there is a time stamp in the current line, go to that date.
  17016. A prefix ARG can be used to force the current date."
  17017. (interactive "P")
  17018. (let ((tsr org-ts-regexp) diff
  17019. (calendar-move-hook nil)
  17020. (view-calendar-holidays-initially nil)
  17021. (view-diary-entries-initially nil))
  17022. (if (or (org-at-timestamp-p)
  17023. (save-excursion
  17024. (beginning-of-line 1)
  17025. (looking-at (concat ".*" tsr))))
  17026. (let ((d1 (time-to-days (current-time)))
  17027. (d2 (time-to-days
  17028. (org-time-string-to-time (match-string 1)))))
  17029. (setq diff (- d2 d1))))
  17030. (calendar)
  17031. (calendar-goto-today)
  17032. (if (and diff (not arg)) (calendar-forward-day diff))))
  17033. (defun org-get-date-from-calendar ()
  17034. "Return a list (month day year) of date at point in calendar."
  17035. (with-current-buffer "*Calendar*"
  17036. (save-match-data
  17037. (calendar-cursor-to-date))))
  17038. (defun org-date-from-calendar ()
  17039. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17040. If there is already a time stamp at the cursor position, update it."
  17041. (interactive)
  17042. (if (org-at-timestamp-p t)
  17043. (org-timestamp-change 0 'calendar)
  17044. (let ((cal-date (org-get-date-from-calendar)))
  17045. (org-insert-time-stamp
  17046. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17047. ;; Make appt aware of appointments from the agenda
  17048. ;;;###autoload
  17049. (defun org-agenda-to-appt (&optional filter)
  17050. "Activate appointments found in `org-agenda-files'.
  17051. When prefixed, prompt for a regular expression and use it as a
  17052. filter: only add entries if they match this regular expression.
  17053. FILTER can be a string. In this case, use this string as a
  17054. regular expression to filter results.
  17055. FILTER can also be an alist, with the car of each cell being
  17056. either 'headline or 'category. For example:
  17057. '((headline \"IMPORTANT\")
  17058. (category \"Work\"))
  17059. will only add headlines containing IMPORTANT or headlines
  17060. belonging to the category \"Work\"."
  17061. (interactive "P")
  17062. (require 'calendar)
  17063. (if (equal filter '(4))
  17064. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17065. (let* ((cnt 0) ; count added events
  17066. (org-agenda-new-buffers nil)
  17067. (today (org-date-to-gregorian
  17068. (time-to-days (current-time))))
  17069. (files (org-agenda-files)) entries file)
  17070. ;; Get all entries which may contain an appt
  17071. (while (setq file (pop files))
  17072. (setq entries
  17073. (append entries
  17074. (org-agenda-get-day-entries
  17075. file today
  17076. :timestamp :scheduled :deadline))))
  17077. (setq entries (delq nil entries))
  17078. ;; Map thru entries and find if they pass thru the filter
  17079. (mapc
  17080. (lambda(x)
  17081. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17082. (cat (get-text-property 1 'org-category x))
  17083. (tod (get-text-property 1 'time-of-day x))
  17084. (ok (or (null filter)
  17085. (and (stringp filter) (string-match filter evt))
  17086. (and (listp filter)
  17087. (or (string-match
  17088. (cadr (assoc 'category filter)) cat)
  17089. (string-match
  17090. (cadr (assoc 'headline filter)) evt))))))
  17091. ;; FIXME: Shall we remove text-properties for the appt text?
  17092. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17093. (when (and ok tod)
  17094. (setq tod (number-to-string tod)
  17095. tod (when (string-match
  17096. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17097. (concat (match-string 1 tod) ":"
  17098. (match-string 2 tod))))
  17099. (appt-add tod evt)
  17100. (setq cnt (1+ cnt))))) entries)
  17101. (org-release-buffers org-agenda-new-buffers)
  17102. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  17103. ;;; The clock for measuring work time.
  17104. (defvar org-mode-line-string "")
  17105. (put 'org-mode-line-string 'risky-local-variable t)
  17106. (defvar org-mode-line-timer nil)
  17107. (defvar org-clock-heading "")
  17108. (defvar org-clock-start-time "")
  17109. (defun org-update-mode-line ()
  17110. (let* ((delta (- (time-to-seconds (current-time))
  17111. (time-to-seconds org-clock-start-time)))
  17112. (h (floor delta 3600))
  17113. (m (floor (- delta (* 3600 h)) 60)))
  17114. (setq org-mode-line-string
  17115. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17116. 'help-echo "Org-mode clock is running"))
  17117. (force-mode-line-update)))
  17118. (defvar org-clock-marker (make-marker)
  17119. "Marker recording the last clock-in.")
  17120. (defvar org-clock-mode-line-entry nil
  17121. "Information for the modeline about the running clock.")
  17122. (defun org-clock-in ()
  17123. "Start the clock on the current item.
  17124. If necessary, clock-out of the currently active clock."
  17125. (interactive)
  17126. (org-clock-out t)
  17127. (let (ts)
  17128. (save-excursion
  17129. (org-back-to-heading t)
  17130. (when (and org-clock-in-switch-to-state
  17131. (not (looking-at (concat outline-regexp "[ \t]*"
  17132. org-clock-in-switch-to-state
  17133. "\\>"))))
  17134. (org-todo org-clock-in-switch-to-state))
  17135. (if (and org-clock-heading-function
  17136. (functionp org-clock-heading-function))
  17137. (setq org-clock-heading (funcall org-clock-heading-function))
  17138. (if (looking-at org-complex-heading-regexp)
  17139. (setq org-clock-heading (match-string 4))
  17140. (setq org-clock-heading "???")))
  17141. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17142. (org-clock-find-position)
  17143. (insert "\n") (backward-char 1)
  17144. (indent-relative)
  17145. (insert org-clock-string " ")
  17146. (setq org-clock-start-time (current-time))
  17147. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17148. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17149. (or global-mode-string (setq global-mode-string '("")))
  17150. (or (memq 'org-mode-line-string global-mode-string)
  17151. (setq global-mode-string
  17152. (append global-mode-string '(org-mode-line-string))))
  17153. (org-update-mode-line)
  17154. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17155. (message "Clock started at %s" ts))))
  17156. (defun org-clock-find-position ()
  17157. "Find the location where the next clock line should be inserted."
  17158. (org-back-to-heading t)
  17159. (catch 'exit
  17160. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17161. (re (concat "^[ \t]*" org-clock-string))
  17162. (cnt 0)
  17163. first last)
  17164. (goto-char beg)
  17165. (when (eobp) (newline) (setq end (max (point) end)))
  17166. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17167. ;; we seem to have a CLOCK drawer, so go there.
  17168. (beginning-of-line 2)
  17169. (throw 'exit t))
  17170. ;; Lets count the CLOCK lines
  17171. (goto-char beg)
  17172. (while (re-search-forward re end t)
  17173. (setq first (or first (match-beginning 0))
  17174. last (match-beginning 0)
  17175. cnt (1+ cnt)))
  17176. (when (and (integerp org-clock-into-drawer)
  17177. (>= (1+ cnt) org-clock-into-drawer))
  17178. ;; Wrap current entries into a new drawer
  17179. (goto-char last)
  17180. (beginning-of-line 2)
  17181. (if (org-at-item-p) (org-end-of-item))
  17182. (insert ":END:\n")
  17183. (beginning-of-line 0)
  17184. (org-indent-line-function)
  17185. (goto-char first)
  17186. (insert ":CLOCK:\n")
  17187. (beginning-of-line 0)
  17188. (org-indent-line-function)
  17189. (org-flag-drawer t)
  17190. (beginning-of-line 2)
  17191. (throw 'exit nil))
  17192. (goto-char beg)
  17193. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17194. (not (equal (match-string 1) org-clock-string)))
  17195. ;; Planning info, skip to after it
  17196. (beginning-of-line 2)
  17197. (or (bolp) (newline)))
  17198. (when (eq t org-clock-into-drawer)
  17199. (insert ":CLOCK:\n:END:\n")
  17200. (beginning-of-line -1)
  17201. (org-indent-line-function)
  17202. (org-flag-drawer t)
  17203. (beginning-of-line 2)
  17204. (org-indent-line-function)))))
  17205. (defun org-clock-out (&optional fail-quietly)
  17206. "Stop the currently running clock.
  17207. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17208. (interactive)
  17209. (catch 'exit
  17210. (if (not (marker-buffer org-clock-marker))
  17211. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17212. (let (ts te s h m)
  17213. (save-excursion
  17214. (set-buffer (marker-buffer org-clock-marker))
  17215. (goto-char org-clock-marker)
  17216. (beginning-of-line 1)
  17217. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17218. (equal (match-string 1) org-clock-string))
  17219. (setq ts (match-string 2))
  17220. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17221. (goto-char (match-end 0))
  17222. (delete-region (point) (point-at-eol))
  17223. (insert "--")
  17224. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17225. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17226. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17227. h (floor (/ s 3600))
  17228. s (- s (* 3600 h))
  17229. m (floor (/ s 60))
  17230. s (- s (* 60 s)))
  17231. (insert " => " (format "%2d:%02d" h m))
  17232. (move-marker org-clock-marker nil)
  17233. (when org-log-note-clock-out
  17234. (org-add-log-maybe 'clock-out))
  17235. (when org-mode-line-timer
  17236. (cancel-timer org-mode-line-timer)
  17237. (setq org-mode-line-timer nil))
  17238. (setq global-mode-string
  17239. (delq 'org-mode-line-string global-mode-string))
  17240. (force-mode-line-update)
  17241. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17242. (defun org-clock-cancel ()
  17243. "Cancel the running clock be removing the start timestamp."
  17244. (interactive)
  17245. (if (not (marker-buffer org-clock-marker))
  17246. (error "No active clock"))
  17247. (save-excursion
  17248. (set-buffer (marker-buffer org-clock-marker))
  17249. (goto-char org-clock-marker)
  17250. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17251. (setq global-mode-string
  17252. (delq 'org-mode-line-string global-mode-string))
  17253. (force-mode-line-update)
  17254. (message "Clock canceled"))
  17255. (defun org-clock-goto (&optional delete-windows)
  17256. "Go to the currently clocked-in entry."
  17257. (interactive "P")
  17258. (if (not (marker-buffer org-clock-marker))
  17259. (error "No active clock"))
  17260. (switch-to-buffer-other-window
  17261. (marker-buffer org-clock-marker))
  17262. (if delete-windows (delete-other-windows))
  17263. (goto-char org-clock-marker)
  17264. (org-show-entry)
  17265. (org-back-to-heading)
  17266. (recenter))
  17267. (defvar org-clock-file-total-minutes nil
  17268. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17269. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17270. (defun org-clock-sum (&optional tstart tend)
  17271. "Sum the times for each subtree.
  17272. Puts the resulting times in minutes as a text property on each headline."
  17273. (interactive)
  17274. (let* ((bmp (buffer-modified-p))
  17275. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17276. org-clock-string
  17277. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17278. (lmax 30)
  17279. (ltimes (make-vector lmax 0))
  17280. (t1 0)
  17281. (level 0)
  17282. ts te dt
  17283. time)
  17284. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17285. (save-excursion
  17286. (goto-char (point-max))
  17287. (while (re-search-backward re nil t)
  17288. (cond
  17289. ((match-end 2)
  17290. ;; Two time stamps
  17291. (setq ts (match-string 2)
  17292. te (match-string 3)
  17293. ts (time-to-seconds
  17294. (apply 'encode-time (org-parse-time-string ts)))
  17295. te (time-to-seconds
  17296. (apply 'encode-time (org-parse-time-string te)))
  17297. ts (if tstart (max ts tstart) ts)
  17298. te (if tend (min te tend) te)
  17299. dt (- te ts)
  17300. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17301. ((match-end 4)
  17302. ;; A naket time
  17303. (setq t1 (+ t1 (string-to-number (match-string 5))
  17304. (* 60 (string-to-number (match-string 4))))))
  17305. (t ;; A headline
  17306. (setq level (- (match-end 1) (match-beginning 1)))
  17307. (when (or (> t1 0) (> (aref ltimes level) 0))
  17308. (loop for l from 0 to level do
  17309. (aset ltimes l (+ (aref ltimes l) t1)))
  17310. (setq t1 0 time (aref ltimes level))
  17311. (loop for l from level to (1- lmax) do
  17312. (aset ltimes l 0))
  17313. (goto-char (match-beginning 0))
  17314. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17315. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17316. (set-buffer-modified-p bmp)))
  17317. (defun org-clock-display (&optional total-only)
  17318. "Show subtree times in the entire buffer.
  17319. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17320. in the echo area."
  17321. (interactive)
  17322. (org-remove-clock-overlays)
  17323. (let (time h m p)
  17324. (org-clock-sum)
  17325. (unless total-only
  17326. (save-excursion
  17327. (goto-char (point-min))
  17328. (while (or (and (equal (setq p (point)) (point-min))
  17329. (get-text-property p :org-clock-minutes))
  17330. (setq p (next-single-property-change
  17331. (point) :org-clock-minutes)))
  17332. (goto-char p)
  17333. (when (setq time (get-text-property p :org-clock-minutes))
  17334. (org-put-clock-overlay time (funcall outline-level))))
  17335. (setq h (/ org-clock-file-total-minutes 60)
  17336. m (- org-clock-file-total-minutes (* 60 h)))
  17337. ;; Arrange to remove the overlays upon next change.
  17338. (when org-remove-highlights-with-change
  17339. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17340. nil 'local))))
  17341. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17342. (defvar org-clock-overlays nil)
  17343. (make-variable-buffer-local 'org-clock-overlays)
  17344. (defun org-put-clock-overlay (time &optional level)
  17345. "Put an overlays on the current line, displaying TIME.
  17346. If LEVEL is given, prefix time with a corresponding number of stars.
  17347. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17348. will be easy to remove."
  17349. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17350. (l (if level (org-get-legal-level level 0) 0))
  17351. (off 0)
  17352. ov tx)
  17353. (move-to-column c)
  17354. (unless (eolp) (skip-chars-backward "^ \t"))
  17355. (skip-chars-backward " \t")
  17356. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17357. tx (concat (buffer-substring (1- (point)) (point))
  17358. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17359. (org-add-props (format "%s %2d:%02d%s"
  17360. (make-string l ?*) h m
  17361. (make-string (- 10 l) ?\ ))
  17362. '(face secondary-selection))
  17363. ""))
  17364. (if (not (featurep 'xemacs))
  17365. (org-overlay-put ov 'display tx)
  17366. (org-overlay-put ov 'invisible t)
  17367. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17368. (push ov org-clock-overlays)))
  17369. (defun org-remove-clock-overlays (&optional beg end noremove)
  17370. "Remove the occur highlights from the buffer.
  17371. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17372. from the `before-change-functions' in the current buffer."
  17373. (interactive)
  17374. (unless org-inhibit-highlight-removal
  17375. (mapc 'org-delete-overlay org-clock-overlays)
  17376. (setq org-clock-overlays nil)
  17377. (unless noremove
  17378. (remove-hook 'before-change-functions
  17379. 'org-remove-clock-overlays 'local))))
  17380. (defun org-clock-out-if-current ()
  17381. "Clock out if the current entry contains the running clock.
  17382. This is used to stop the clock after a TODO entry is marked DONE,
  17383. and is only done if the variable `org-clock-out-when-done' is not nil."
  17384. (when (and org-clock-out-when-done
  17385. (member state org-done-keywords)
  17386. (equal (marker-buffer org-clock-marker) (current-buffer))
  17387. (< (point) org-clock-marker)
  17388. (> (save-excursion (outline-next-heading) (point))
  17389. org-clock-marker))
  17390. ;; Clock out, but don't accept a logging message for this.
  17391. (let ((org-log-note-clock-out nil))
  17392. (org-clock-out))))
  17393. (add-hook 'org-after-todo-state-change-hook
  17394. 'org-clock-out-if-current)
  17395. (defun org-check-running-clock ()
  17396. "Check if the current buffer contains the running clock.
  17397. If yes, offer to stop it and to save the buffer with the changes."
  17398. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17399. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17400. (buffer-name))))
  17401. (org-clock-out)
  17402. (when (y-or-n-p "Save changed buffer?")
  17403. (save-buffer))))
  17404. (defun org-clock-report (&optional arg)
  17405. "Create a table containing a report about clocked time.
  17406. If the cursor is inside an existing clocktable block, then the table
  17407. will be updated. If not, a new clocktable will be inserted.
  17408. When called with a prefix argument, move to the first clock table in the
  17409. buffer and update it."
  17410. (interactive "P")
  17411. (org-remove-clock-overlays)
  17412. (when arg
  17413. (org-find-dblock "clocktable")
  17414. (org-show-entry))
  17415. (if (org-in-clocktable-p)
  17416. (goto-char (org-in-clocktable-p))
  17417. (org-create-dblock (list :name "clocktable"
  17418. :maxlevel 2 :scope 'file)))
  17419. (org-update-dblock))
  17420. (defun org-in-clocktable-p ()
  17421. "Check if the cursor is in a clocktable."
  17422. (let ((pos (point)) start)
  17423. (save-excursion
  17424. (end-of-line 1)
  17425. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17426. (setq start (match-beginning 0))
  17427. (re-search-forward "^#\\+END:.*" nil t)
  17428. (>= (match-end 0) pos)
  17429. start))))
  17430. (defun org-clock-update-time-maybe ()
  17431. "If this is a CLOCK line, update it and return t.
  17432. Otherwise, return nil."
  17433. (interactive)
  17434. (save-excursion
  17435. (beginning-of-line 1)
  17436. (skip-chars-forward " \t")
  17437. (when (looking-at org-clock-string)
  17438. (let ((re (concat "[ \t]*" org-clock-string
  17439. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17440. "\\([ \t]*=>.*\\)?"))
  17441. ts te h m s)
  17442. (if (not (looking-at re))
  17443. nil
  17444. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17445. (end-of-line 1)
  17446. (setq ts (match-string 1)
  17447. te (match-string 2))
  17448. (setq s (- (time-to-seconds
  17449. (apply 'encode-time (org-parse-time-string te)))
  17450. (time-to-seconds
  17451. (apply 'encode-time (org-parse-time-string ts))))
  17452. h (floor (/ s 3600))
  17453. s (- s (* 3600 h))
  17454. m (floor (/ s 60))
  17455. s (- s (* 60 s)))
  17456. (insert " => " (format "%2d:%02d" h m))
  17457. t)))))
  17458. (defun org-clock-special-range (key &optional time as-strings)
  17459. "Return two times bordering a special time range.
  17460. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17461. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17462. A week starts Monday 0:00 and ends Sunday 24:00.
  17463. The range is determined relative to TIME. TIME defaults to the current time.
  17464. The return value is a cons cell with two internal times like the ones
  17465. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17466. the returned times will be formatted strings."
  17467. (let* ((tm (decode-time (or time (current-time))))
  17468. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17469. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17470. (dow (nth 6 tm))
  17471. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17472. (cond
  17473. ((eq key 'today)
  17474. (setq h 0 m 0 h1 24 m1 0))
  17475. ((eq key 'yesterday)
  17476. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17477. ((eq key 'thisweek)
  17478. (setq diff (if (= dow 0) 6 (1- dow))
  17479. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17480. ((eq key 'lastweek)
  17481. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17482. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17483. ((eq key 'thismonth)
  17484. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17485. ((eq key 'lastmonth)
  17486. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17487. ((eq key 'thisyear)
  17488. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17489. ((eq key 'lastyear)
  17490. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17491. (t (error "No such time block %s" key)))
  17492. (setq ts (encode-time s m h d month y)
  17493. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17494. (or d1 d) (or month1 month) (or y1 y)))
  17495. (setq fm (cdr org-time-stamp-formats))
  17496. (if as-strings
  17497. (cons (format-time-string fm ts) (format-time-string fm te))
  17498. (cons ts te))))
  17499. (defun org-dblock-write:clocktable (params)
  17500. "Write the standard clocktable."
  17501. (catch 'exit
  17502. (let* ((hlchars '((1 . "*") (2 . "/")))
  17503. (ins (make-marker))
  17504. (total-time nil)
  17505. (scope (plist-get params :scope))
  17506. (tostring (plist-get params :tostring))
  17507. (multifile (plist-get params :multifile))
  17508. (header (plist-get params :header))
  17509. (maxlevel (or (plist-get params :maxlevel) 3))
  17510. (step (plist-get params :step))
  17511. (emph (plist-get params :emphasize))
  17512. (ts (plist-get params :tstart))
  17513. (te (plist-get params :tend))
  17514. (block (plist-get params :block))
  17515. ipos time h m p level hlc hdl
  17516. cc beg end pos tbl)
  17517. (when step
  17518. (org-clocktable-steps params)
  17519. (throw 'exit nil))
  17520. (when block
  17521. (setq cc (org-clock-special-range block nil t)
  17522. ts (car cc) te (cdr cc)))
  17523. (if ts (setq ts (time-to-seconds
  17524. (apply 'encode-time (org-parse-time-string ts)))))
  17525. (if te (setq te (time-to-seconds
  17526. (apply 'encode-time (org-parse-time-string te)))))
  17527. (move-marker ins (point))
  17528. (setq ipos (point))
  17529. ;; Get the right scope
  17530. (setq pos (point))
  17531. (save-restriction
  17532. (cond
  17533. ((not scope))
  17534. ((eq scope 'file) (widen))
  17535. ((eq scope 'subtree) (org-narrow-to-subtree))
  17536. ((eq scope 'tree)
  17537. (while (org-up-heading-safe))
  17538. (org-narrow-to-subtree))
  17539. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17540. (symbol-name scope)))
  17541. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17542. (catch 'exit
  17543. (while (org-up-heading-safe)
  17544. (looking-at outline-regexp)
  17545. (if (<= (org-reduced-level (funcall outline-level)) level)
  17546. (throw 'exit nil))))
  17547. (org-narrow-to-subtree))
  17548. ((or (listp scope) (eq scope 'agenda))
  17549. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17550. (scope 'agenda)
  17551. (p1 (copy-sequence params))
  17552. file)
  17553. (plist-put p1 :tostring t)
  17554. (plist-put p1 :multifile t)
  17555. (plist-put p1 :scope 'file)
  17556. (org-prepare-agenda-buffers files)
  17557. (while (setq file (pop files))
  17558. (with-current-buffer (find-buffer-visiting file)
  17559. (push (org-clocktable-add-file
  17560. file (org-dblock-write:clocktable p1)) tbl)
  17561. (setq total-time (+ (or total-time 0)
  17562. org-clock-file-total-minutes)))))))
  17563. (goto-char pos)
  17564. (unless (eq scope 'agenda)
  17565. (org-clock-sum ts te)
  17566. (goto-char (point-min))
  17567. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17568. (goto-char p)
  17569. (when (setq time (get-text-property p :org-clock-minutes))
  17570. (save-excursion
  17571. (beginning-of-line 1)
  17572. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17573. (setq level (org-reduced-level
  17574. (- (match-end 1) (match-beginning 1))))
  17575. (<= level maxlevel))
  17576. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17577. hdl (match-string 2)
  17578. h (/ time 60)
  17579. m (- time (* 60 h)))
  17580. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17581. (push (concat
  17582. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17583. (make-string (1- level) ?|)
  17584. hlc (format "%d:%02d" h m) hlc
  17585. " |") tbl))))))
  17586. (setq tbl (nreverse tbl))
  17587. (if tostring
  17588. (if tbl (mapconcat 'identity tbl "\n") nil)
  17589. (goto-char ins)
  17590. (insert-before-markers
  17591. (or header
  17592. (concat
  17593. "Clock summary at ["
  17594. (substring
  17595. (format-time-string (cdr org-time-stamp-formats))
  17596. 1 -1)
  17597. "]."
  17598. (if block
  17599. (format " Considered range is /%s/." block)
  17600. "")
  17601. "\n\n"))
  17602. (if (eq scope 'agenda) "|File" "")
  17603. "|L|Headline|Time|\n")
  17604. (setq total-time (or total-time org-clock-file-total-minutes)
  17605. h (/ total-time 60)
  17606. m (- total-time (* 60 h)))
  17607. (insert-before-markers
  17608. "|-\n|"
  17609. (if (eq scope 'agenda) "|" "")
  17610. "|"
  17611. "*Total time*| "
  17612. (format "*%d:%02d*" h m)
  17613. "|\n|-\n")
  17614. (setq tbl (delq nil tbl))
  17615. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17616. (equal (substring (car tbl) 0 2) "|-"))
  17617. (pop tbl))
  17618. (insert-before-markers (mapconcat
  17619. 'identity (delq nil tbl)
  17620. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17621. (backward-delete-char 1)
  17622. (goto-char ipos)
  17623. (skip-chars-forward "^|")
  17624. (org-table-align))))))
  17625. (defun org-clocktable-steps (params)
  17626. (let* ((p1 (copy-sequence params))
  17627. (ts (plist-get p1 :tstart))
  17628. (te (plist-get p1 :tend))
  17629. (step0 (plist-get p1 :step))
  17630. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17631. (block (plist-get p1 :block))
  17632. cc)
  17633. (when block
  17634. (setq cc (org-clock-special-range block nil t)
  17635. ts (car cc) te (cdr cc)))
  17636. (if ts (setq ts (time-to-seconds
  17637. (apply 'encode-time (org-parse-time-string ts)))))
  17638. (if te (setq te (time-to-seconds
  17639. (apply 'encode-time (org-parse-time-string te)))))
  17640. (plist-put p1 :header "")
  17641. (plist-put p1 :step nil)
  17642. (plist-put p1 :block nil)
  17643. (while (< ts te)
  17644. (or (bolp) (insert "\n"))
  17645. (plist-put p1 :tstart (format-time-string
  17646. (car org-time-stamp-formats)
  17647. (seconds-to-time ts)))
  17648. (plist-put p1 :tend (format-time-string
  17649. (car org-time-stamp-formats)
  17650. (seconds-to-time (setq ts (+ ts step)))))
  17651. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17652. (plist-get p1 :tstart) "\n")
  17653. (org-dblock-write:clocktable p1)
  17654. (re-search-forward "#\\+END:")
  17655. (end-of-line 0))))
  17656. (defun org-clocktable-add-file (file table)
  17657. (if table
  17658. (let ((lines (org-split-string table "\n"))
  17659. (ff (file-name-nondirectory file)))
  17660. (mapconcat 'identity
  17661. (mapcar (lambda (x)
  17662. (if (string-match org-table-dataline-regexp x)
  17663. (concat "|" ff x)
  17664. x))
  17665. lines)
  17666. "\n"))))
  17667. ;; FIXME: I don't think anybody uses this, ask David
  17668. (defun org-collect-clock-time-entries ()
  17669. "Return an internal list with clocking information.
  17670. This list has one entry for each CLOCK interval.
  17671. FIXME: describe the elements."
  17672. (interactive)
  17673. (let ((re (concat "^[ \t]*" org-clock-string
  17674. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17675. rtn beg end next cont level title total closedp leafp
  17676. clockpos titlepos h m donep)
  17677. (save-excursion
  17678. (org-clock-sum)
  17679. (goto-char (point-min))
  17680. (while (re-search-forward re nil t)
  17681. (setq clockpos (match-beginning 0)
  17682. beg (match-string 1) end (match-string 2)
  17683. cont (match-end 0))
  17684. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17685. end (apply 'encode-time (org-parse-time-string end)))
  17686. (org-back-to-heading t)
  17687. (setq donep (org-entry-is-done-p))
  17688. (setq titlepos (point)
  17689. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17690. h (/ total 60) m (- total (* 60 h))
  17691. total (cons h m))
  17692. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17693. (setq level (- (match-end 1) (match-beginning 1))
  17694. title (org-match-string-no-properties 2))
  17695. (save-excursion (outline-next-heading) (setq next (point)))
  17696. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17697. (goto-char next)
  17698. (setq leafp (and (looking-at "^\\*+ ")
  17699. (<= (- (match-end 0) (point)) level)))
  17700. (push (list beg end clockpos closedp donep
  17701. total title titlepos level leafp)
  17702. rtn)
  17703. (goto-char cont)))
  17704. (nreverse rtn)))
  17705. ;;;; Agenda, and Diary Integration
  17706. ;;; Define the Org-agenda-mode
  17707. (defvar org-agenda-mode-map (make-sparse-keymap)
  17708. "Keymap for `org-agenda-mode'.")
  17709. (defvar org-agenda-menu) ; defined later in this file.
  17710. (defvar org-agenda-follow-mode nil)
  17711. (defvar org-agenda-show-log nil)
  17712. (defvar org-agenda-redo-command nil)
  17713. (defvar org-agenda-mode-hook nil)
  17714. (defvar org-agenda-type nil)
  17715. (defvar org-agenda-force-single-file nil)
  17716. (defun org-agenda-mode ()
  17717. "Mode for time-sorted view on action items in Org-mode files.
  17718. The following commands are available:
  17719. \\{org-agenda-mode-map}"
  17720. (interactive)
  17721. (kill-all-local-variables)
  17722. (setq org-agenda-undo-list nil
  17723. org-agenda-pending-undo-list nil)
  17724. (setq major-mode 'org-agenda-mode)
  17725. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17726. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17727. (setq mode-name "Org-Agenda")
  17728. (use-local-map org-agenda-mode-map)
  17729. (easy-menu-add org-agenda-menu)
  17730. (if org-startup-truncated (setq truncate-lines t))
  17731. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17732. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17733. ;; Make sure properties are removed when copying text
  17734. (when (boundp 'buffer-substring-filters)
  17735. (org-set-local 'buffer-substring-filters
  17736. (cons (lambda (x)
  17737. (set-text-properties 0 (length x) nil x) x)
  17738. buffer-substring-filters)))
  17739. (unless org-agenda-keep-modes
  17740. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17741. org-agenda-show-log nil))
  17742. (easy-menu-change
  17743. '("Agenda") "Agenda Files"
  17744. (append
  17745. (list
  17746. (vector
  17747. (if (get 'org-agenda-files 'org-restrict)
  17748. "Restricted to single file"
  17749. "Edit File List")
  17750. '(org-edit-agenda-file-list)
  17751. (not (get 'org-agenda-files 'org-restrict)))
  17752. "--")
  17753. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17754. (org-agenda-set-mode-name)
  17755. (apply
  17756. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17757. (list 'org-agenda-mode-hook)))
  17758. (substitute-key-definition 'undo 'org-agenda-undo
  17759. org-agenda-mode-map global-map)
  17760. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17761. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17762. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17763. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17764. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17765. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17766. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17767. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17768. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17769. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17770. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17771. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17772. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17773. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17774. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17775. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17776. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17777. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17778. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17779. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17780. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17781. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17782. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17783. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17784. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17785. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17786. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17787. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17788. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17789. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17790. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17791. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17792. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17793. (while l (org-defkey org-agenda-mode-map
  17794. (int-to-string (pop l)) 'digit-argument)))
  17795. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17796. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17797. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17798. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17799. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17800. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17801. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17802. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17803. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17804. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17805. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17806. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17807. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17808. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17809. (org-defkey org-agenda-mode-map "n" 'next-line)
  17810. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17811. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17812. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17813. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17814. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17815. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17816. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17817. (eval-after-load "calendar"
  17818. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17819. 'org-calendar-goto-agenda))
  17820. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17821. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17822. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17823. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17824. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17825. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17826. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17827. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17828. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17829. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17830. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17831. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17832. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17833. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17834. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17835. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17836. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17837. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17838. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17839. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17840. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17841. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17842. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17843. "Local keymap for agenda entries from Org-mode.")
  17844. (org-defkey org-agenda-keymap
  17845. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17846. (org-defkey org-agenda-keymap
  17847. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17848. (when org-agenda-mouse-1-follows-link
  17849. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17850. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17851. '("Agenda"
  17852. ("Agenda Files")
  17853. "--"
  17854. ["Show" org-agenda-show t]
  17855. ["Go To (other window)" org-agenda-goto t]
  17856. ["Go To (this window)" org-agenda-switch-to t]
  17857. ["Follow Mode" org-agenda-follow-mode
  17858. :style toggle :selected org-agenda-follow-mode :active t]
  17859. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17860. "--"
  17861. ["Cycle TODO" org-agenda-todo t]
  17862. ["Archive subtree" org-agenda-archive t]
  17863. ["Delete subtree" org-agenda-kill t]
  17864. "--"
  17865. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17866. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17867. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17868. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17869. "--"
  17870. ("Tags and Properties"
  17871. ["Show all Tags" org-agenda-show-tags t]
  17872. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17873. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17874. "--"
  17875. ["Column View" org-columns t])
  17876. ("Date/Schedule"
  17877. ["Schedule" org-agenda-schedule t]
  17878. ["Set Deadline" org-agenda-deadline t]
  17879. "--"
  17880. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17881. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17882. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17883. ("Clock"
  17884. ["Clock in" org-agenda-clock-in t]
  17885. ["Clock out" org-agenda-clock-out t]
  17886. ["Clock cancel" org-agenda-clock-cancel t]
  17887. ["Goto running clock" org-clock-goto t])
  17888. ("Priority"
  17889. ["Set Priority" org-agenda-priority t]
  17890. ["Increase Priority" org-agenda-priority-up t]
  17891. ["Decrease Priority" org-agenda-priority-down t]
  17892. ["Show Priority" org-agenda-show-priority t])
  17893. ("Calendar/Diary"
  17894. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17895. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17896. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17897. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17898. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17899. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17900. "--"
  17901. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17902. "--"
  17903. ("View"
  17904. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17905. :style radio :selected (equal org-agenda-ndays 1)]
  17906. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17907. :style radio :selected (equal org-agenda-ndays 7)]
  17908. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17909. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17910. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17911. :style radio :selected (member org-agenda-ndays '(365 366))]
  17912. "--"
  17913. ["Show Logbook entries" org-agenda-log-mode
  17914. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17915. ["Include Diary" org-agenda-toggle-diary
  17916. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17917. ["Use Time Grid" org-agenda-toggle-time-grid
  17918. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17919. ["Write view to file" org-write-agenda t]
  17920. ["Rebuild buffer" org-agenda-redo t]
  17921. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17922. "--"
  17923. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17924. "--"
  17925. ["Quit" org-agenda-quit t]
  17926. ["Exit and Release Buffers" org-agenda-exit t]
  17927. ))
  17928. ;;; Agenda undo
  17929. (defvar org-agenda-allow-remote-undo t
  17930. "Non-nil means, allow remote undo from the agenda buffer.")
  17931. (defvar org-agenda-undo-list nil
  17932. "List of undoable operations in the agenda since last refresh.")
  17933. (defvar org-agenda-undo-has-started-in nil
  17934. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17935. (defvar org-agenda-pending-undo-list nil
  17936. "In a series of undo commands, this is the list of remaning undo items.")
  17937. (defmacro org-if-unprotected (&rest body)
  17938. "Execute BODY if there is no `org-protected' text property at point."
  17939. (declare (debug t))
  17940. `(unless (get-text-property (point) 'org-protected)
  17941. ,@body))
  17942. (defmacro org-with-remote-undo (_buffer &rest _body)
  17943. "Execute BODY while recording undo information in two buffers."
  17944. (declare (indent 1) (debug t))
  17945. `(let ((_cline (org-current-line))
  17946. (_cmd this-command)
  17947. (_buf1 (current-buffer))
  17948. (_buf2 ,_buffer)
  17949. (_undo1 buffer-undo-list)
  17950. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17951. _c1 _c2)
  17952. ,@_body
  17953. (when org-agenda-allow-remote-undo
  17954. (setq _c1 (org-verify-change-for-undo
  17955. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17956. _c2 (org-verify-change-for-undo
  17957. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17958. (when (or _c1 _c2)
  17959. ;; make sure there are undo boundaries
  17960. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17961. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17962. ;; remember which buffer to undo
  17963. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17964. org-agenda-undo-list)))))
  17965. (defun org-agenda-undo ()
  17966. "Undo a remote editing step in the agenda.
  17967. This undoes changes both in the agenda buffer and in the remote buffer
  17968. that have been changed along."
  17969. (interactive)
  17970. (or org-agenda-allow-remote-undo
  17971. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17972. (if (not (eq this-command last-command))
  17973. (setq org-agenda-undo-has-started-in nil
  17974. org-agenda-pending-undo-list org-agenda-undo-list))
  17975. (if (not org-agenda-pending-undo-list)
  17976. (error "No further undo information"))
  17977. (let* ((entry (pop org-agenda-pending-undo-list))
  17978. buf line cmd rembuf)
  17979. (setq cmd (pop entry) line (pop entry))
  17980. (setq rembuf (nth 2 entry))
  17981. (org-with-remote-undo rembuf
  17982. (while (bufferp (setq buf (pop entry)))
  17983. (if (pop entry)
  17984. (with-current-buffer buf
  17985. (let ((last-undo-buffer buf)
  17986. (inhibit-read-only t))
  17987. (unless (memq buf org-agenda-undo-has-started-in)
  17988. (push buf org-agenda-undo-has-started-in)
  17989. (make-local-variable 'pending-undo-list)
  17990. (undo-start))
  17991. (while (and pending-undo-list
  17992. (listp pending-undo-list)
  17993. (not (car pending-undo-list)))
  17994. (pop pending-undo-list))
  17995. (undo-more 1))))))
  17996. (goto-line line)
  17997. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17998. (defun org-verify-change-for-undo (l1 l2)
  17999. "Verify that a real change occurred between the undo lists L1 and L2."
  18000. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18001. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18002. (not (eq l1 l2)))
  18003. ;;; Agenda dispatch
  18004. (defvar org-agenda-restrict nil)
  18005. (defvar org-agenda-restrict-begin (make-marker))
  18006. (defvar org-agenda-restrict-end (make-marker))
  18007. (defvar org-agenda-last-dispatch-buffer nil)
  18008. (defvar org-agenda-overriding-restriction nil)
  18009. ;;;###autoload
  18010. (defun org-agenda (arg &optional keys restriction)
  18011. "Dispatch agenda commands to collect entries to the agenda buffer.
  18012. Prompts for a command to execute. Any prefix arg will be passed
  18013. on to the selected command. The default selections are:
  18014. a Call `org-agenda-list' to display the agenda for current day or week.
  18015. t Call `org-todo-list' to display the global todo list.
  18016. T Call `org-todo-list' to display the global todo list, select only
  18017. entries with a specific TODO keyword (the user gets a prompt).
  18018. m Call `org-tags-view' to display headlines with tags matching
  18019. a condition (the user is prompted for the condition).
  18020. M Like `m', but select only TODO entries, no ordinary headlines.
  18021. L Create a timeline for the current buffer.
  18022. e Export views to associated files.
  18023. More commands can be added by configuring the variable
  18024. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18025. searches can be pre-defined in this way.
  18026. If the current buffer is in Org-mode and visiting a file, you can also
  18027. first press `<' once to indicate that the agenda should be temporarily
  18028. \(until the next use of \\[org-agenda]) restricted to the current file.
  18029. Pressing `<' twice means to restrict to the current subtree or region
  18030. \(if active)."
  18031. (interactive "P")
  18032. (catch 'exit
  18033. (let* ((prefix-descriptions nil)
  18034. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18035. (org-agenda-custom-commands
  18036. ;; normalize different versions
  18037. (delq nil
  18038. (mapcar
  18039. (lambda (x)
  18040. (cond ((stringp (cdr x))
  18041. (push x prefix-descriptions)
  18042. nil)
  18043. ((stringp (nth 1 x)) x)
  18044. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18045. (t (cons (car x) (cons "" (cdr x))))))
  18046. org-agenda-custom-commands)))
  18047. (buf (current-buffer))
  18048. (bfn (buffer-file-name (buffer-base-buffer)))
  18049. entry key type match lprops ans)
  18050. ;; Turn off restriction unless there is an overriding one
  18051. (unless org-agenda-overriding-restriction
  18052. (put 'org-agenda-files 'org-restrict nil)
  18053. (setq org-agenda-restrict nil)
  18054. (move-marker org-agenda-restrict-begin nil)
  18055. (move-marker org-agenda-restrict-end nil))
  18056. ;; Delete old local properties
  18057. (put 'org-agenda-redo-command 'org-lprops nil)
  18058. ;; Remember where this call originated
  18059. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18060. (unless keys
  18061. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18062. keys (car ans)
  18063. restriction (cdr ans)))
  18064. ;; Estabish the restriction, if any
  18065. (when (and (not org-agenda-overriding-restriction) restriction)
  18066. (put 'org-agenda-files 'org-restrict (list bfn))
  18067. (cond
  18068. ((eq restriction 'region)
  18069. (setq org-agenda-restrict t)
  18070. (move-marker org-agenda-restrict-begin (region-beginning))
  18071. (move-marker org-agenda-restrict-end (region-end)))
  18072. ((eq restriction 'subtree)
  18073. (save-excursion
  18074. (setq org-agenda-restrict t)
  18075. (org-back-to-heading t)
  18076. (move-marker org-agenda-restrict-begin (point))
  18077. (move-marker org-agenda-restrict-end
  18078. (progn (org-end-of-subtree t)))))))
  18079. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18080. ;; For example the todo list should not need it (but does...)
  18081. (cond
  18082. ((setq entry (assoc keys org-agenda-custom-commands))
  18083. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18084. (progn
  18085. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18086. (put 'org-agenda-redo-command 'org-lprops lprops)
  18087. (cond
  18088. ((eq type 'agenda)
  18089. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18090. ((eq type 'alltodo)
  18091. (org-let lprops '(org-todo-list current-prefix-arg)))
  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 "t") (call-interactively 'org-todo-list))
  18123. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18124. ((equal keys "m") (call-interactively 'org-tags-view))
  18125. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18126. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18127. ((equal keys "L")
  18128. (unless (org-mode-p)
  18129. (error "This is not an Org-mode file"))
  18130. (unless restriction
  18131. (put 'org-agenda-files 'org-restrict (list bfn))
  18132. (org-call-with-arg 'org-timeline arg)))
  18133. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18134. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18135. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18136. (t (error "Invalid agenda key"))))))
  18137. (defun org-agenda-normalize-custom-commands (cmds)
  18138. (delq nil
  18139. (mapcar
  18140. (lambda (x)
  18141. (cond ((stringp (cdr x)) nil)
  18142. ((stringp (nth 1 x)) x)
  18143. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18144. (t (cons (car x) (cons "" (cdr x))))))
  18145. cmds)))
  18146. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18147. "The user interface for selecting an agenda command."
  18148. (catch 'exit
  18149. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18150. (restrict-ok (and bfn (org-mode-p)))
  18151. (region-p (org-region-active-p))
  18152. (custom org-agenda-custom-commands)
  18153. (selstring "")
  18154. restriction second-time
  18155. c entry key type match prefixes rmheader header-end custom1 desc)
  18156. (save-window-excursion
  18157. (delete-other-windows)
  18158. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18159. (erase-buffer)
  18160. (insert (eval-when-compile
  18161. (let ((header
  18162. "
  18163. Press key for an agenda command: < Buffer,subtree/region restriction
  18164. -------------------------------- > Remove restriction
  18165. a Agenda for current week or day e Export agenda views
  18166. t List of all TODO entries T Entries with special TODO kwd
  18167. m Match a TAGS query M Like m, but only TODO entries
  18168. L Timeline for current buffer # List stuck projects (!=configure)
  18169. / Multi-occur C Configure custom agenda commands
  18170. ")
  18171. (start 0))
  18172. (while (string-match
  18173. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18174. header start)
  18175. (setq start (match-end 0))
  18176. (add-text-properties (match-beginning 2) (match-end 2)
  18177. '(face bold) header))
  18178. header)))
  18179. (setq header-end (move-marker (make-marker) (point)))
  18180. (while t
  18181. (setq custom1 custom)
  18182. (when (eq rmheader t)
  18183. (goto-line 1)
  18184. (re-search-forward ":" nil t)
  18185. (delete-region (match-end 0) (point-at-eol))
  18186. (forward-char 1)
  18187. (looking-at "-+")
  18188. (delete-region (match-end 0) (point-at-eol))
  18189. (move-marker header-end (match-end 0)))
  18190. (goto-char header-end)
  18191. (delete-region (point) (point-max))
  18192. (while (setq entry (pop custom1))
  18193. (setq key (car entry) desc (nth 1 entry)
  18194. type (nth 2 entry) match (nth 3 entry))
  18195. (if (> (length key) 1)
  18196. (add-to-list 'prefixes (string-to-char key))
  18197. (insert
  18198. (format
  18199. "\n%-4s%-14s: %s"
  18200. (org-add-props (copy-sequence key)
  18201. '(face bold))
  18202. (cond
  18203. ((string-match "\\S-" desc) desc)
  18204. ((eq type 'agenda) "Agenda for current week or day")
  18205. ((eq type 'alltodo) "List of all TODO entries")
  18206. ((eq type 'stuck) "List of stuck projects")
  18207. ((eq type 'todo) "TODO keyword")
  18208. ((eq type 'tags) "Tags query")
  18209. ((eq type 'tags-todo) "Tags (TODO)")
  18210. ((eq type 'tags-tree) "Tags tree")
  18211. ((eq type 'todo-tree) "TODO kwd tree")
  18212. ((eq type 'occur-tree) "Occur tree")
  18213. ((functionp type) (if (symbolp type)
  18214. (symbol-name type)
  18215. "Lambda expression"))
  18216. (t "???"))
  18217. (cond
  18218. ((stringp match)
  18219. (org-add-props match nil 'face 'org-warning))
  18220. (match
  18221. (format "set of %d commands" (length match)))
  18222. (t ""))))))
  18223. (when prefixes
  18224. (mapc (lambda (x)
  18225. (insert
  18226. (format "\n%s %s"
  18227. (org-add-props (char-to-string x)
  18228. nil 'face 'bold)
  18229. (or (cdr (assoc (concat selstring (char-to-string x))
  18230. prefix-descriptions))
  18231. "Prefix key"))))
  18232. prefixes))
  18233. (goto-char (point-min))
  18234. (when (fboundp 'fit-window-to-buffer)
  18235. (if second-time
  18236. (if (not (pos-visible-in-window-p (point-max)))
  18237. (fit-window-to-buffer))
  18238. (setq second-time t)
  18239. (fit-window-to-buffer)))
  18240. (message "Press key for agenda command%s:"
  18241. (if (or restrict-ok org-agenda-overriding-restriction)
  18242. (if org-agenda-overriding-restriction
  18243. " (restriction lock active)"
  18244. (if restriction
  18245. (format " (restricted to %s)" restriction)
  18246. " (unrestricted)"))
  18247. ""))
  18248. (setq c (read-char-exclusive))
  18249. (message "")
  18250. (cond
  18251. ((assoc (char-to-string c) custom)
  18252. (setq selstring (concat selstring (char-to-string c)))
  18253. (throw 'exit (cons selstring restriction)))
  18254. ((memq c prefixes)
  18255. (setq selstring (concat selstring (char-to-string c))
  18256. prefixes nil
  18257. rmheader (or rmheader t)
  18258. custom (delq nil (mapcar
  18259. (lambda (x)
  18260. (if (or (= (length (car x)) 1)
  18261. (/= (string-to-char (car x)) c))
  18262. nil
  18263. (cons (substring (car x) 1) (cdr x))))
  18264. custom))))
  18265. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18266. (message "Restriction is only possible in Org-mode buffers")
  18267. (ding) (sit-for 1))
  18268. ((eq c ?1)
  18269. (org-agenda-remove-restriction-lock 'noupdate)
  18270. (setq restriction 'buffer))
  18271. ((eq c ?0)
  18272. (org-agenda-remove-restriction-lock 'noupdate)
  18273. (setq restriction (if region-p 'region 'subtree)))
  18274. ((eq c ?<)
  18275. (org-agenda-remove-restriction-lock 'noupdate)
  18276. (setq restriction
  18277. (cond
  18278. ((eq restriction 'buffer)
  18279. (if region-p 'region 'subtree))
  18280. ((memq restriction '(subtree region))
  18281. nil)
  18282. (t 'buffer))))
  18283. ((eq c ?>)
  18284. (org-agenda-remove-restriction-lock 'noupdate)
  18285. (setq restriction nil))
  18286. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18287. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18288. ((and (> (length selstring) 0) (eq c ?\d))
  18289. (delete-window)
  18290. (org-agenda-get-restriction-and-command prefix-descriptions))
  18291. ((equal c ?q) (error "Abort"))
  18292. (t (error "Invalid key %c" c))))))))
  18293. (defun org-run-agenda-series (name series)
  18294. (org-prepare-agenda name)
  18295. (let* ((org-agenda-multi t)
  18296. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18297. (cmds (car series))
  18298. (gprops (nth 1 series))
  18299. match ;; The byte compiler incorrectly complains about this. Keep it!
  18300. cmd type lprops)
  18301. (while (setq cmd (pop cmds))
  18302. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18303. (cond
  18304. ((eq type 'agenda)
  18305. (org-let2 gprops lprops
  18306. '(call-interactively 'org-agenda-list)))
  18307. ((eq type 'alltodo)
  18308. (org-let2 gprops lprops
  18309. '(call-interactively 'org-todo-list)))
  18310. ((eq type 'stuck)
  18311. (org-let2 gprops lprops
  18312. '(call-interactively 'org-agenda-list-stuck-projects)))
  18313. ((eq type 'tags)
  18314. (org-let2 gprops lprops
  18315. '(org-tags-view current-prefix-arg match)))
  18316. ((eq type 'tags-todo)
  18317. (org-let2 gprops lprops
  18318. '(org-tags-view '(4) match)))
  18319. ((eq type 'todo)
  18320. (org-let2 gprops lprops
  18321. '(org-todo-list match)))
  18322. ((fboundp type)
  18323. (org-let2 gprops lprops
  18324. '(funcall type match)))
  18325. (t (error "Invalid type in command series"))))
  18326. (widen)
  18327. (setq org-agenda-redo-command redo)
  18328. (goto-char (point-min)))
  18329. (org-finalize-agenda))
  18330. ;;;###autoload
  18331. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18332. "Run an agenda command in batch mode and send the result to STDOUT.
  18333. If CMD-KEY is a string of length 1, it is used as a key in
  18334. `org-agenda-custom-commands' and triggers this command. If it is a
  18335. longer string it is used as a tags/todo match string.
  18336. Paramters are alternating variable names and values that will be bound
  18337. before running the agenda command."
  18338. (let (pars)
  18339. (while parameters
  18340. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18341. (if (> (length cmd-key) 2)
  18342. (eval (list 'let (nreverse pars)
  18343. (list 'org-tags-view nil cmd-key)))
  18344. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18345. (set-buffer org-agenda-buffer-name)
  18346. (princ (org-encode-for-stdout (buffer-string)))))
  18347. (defun org-encode-for-stdout (string)
  18348. (if (fboundp 'encode-coding-string)
  18349. (encode-coding-string string buffer-file-coding-system)
  18350. string))
  18351. (defvar org-agenda-info nil)
  18352. ;;;###autoload
  18353. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18354. "Run an agenda command in batch mode and send the result to STDOUT.
  18355. If CMD-KEY is a string of length 1, it is used as a key in
  18356. `org-agenda-custom-commands' and triggers this command. If it is a
  18357. longer string it is used as a tags/todo match string.
  18358. Paramters are alternating variable names and values that will be bound
  18359. before running the agenda command.
  18360. The output gives a line for each selected agenda item. Each
  18361. item is a list of comma-separated values, like this:
  18362. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18363. category The category of the item
  18364. head The headline, without TODO kwd, TAGS and PRIORITY
  18365. type The type of the agenda entry, can be
  18366. todo selected in TODO match
  18367. tagsmatch selected in tags match
  18368. diary imported from diary
  18369. deadline a deadline on given date
  18370. scheduled scheduled on given date
  18371. timestamp entry has timestamp on given date
  18372. closed entry was closed on given date
  18373. upcoming-deadline warning about deadline
  18374. past-scheduled forwarded scheduled item
  18375. block entry has date block including g. date
  18376. todo The todo keyword, if any
  18377. tags All tags including inherited ones, separated by colons
  18378. date The relevant date, like 2007-2-14
  18379. time The time, like 15:00-16:50
  18380. extra Sting with extra planning info
  18381. priority-l The priority letter if any was given
  18382. priority-n The computed numerical priority
  18383. agenda-day The day in the agenda where this is listed"
  18384. (let (pars)
  18385. (while parameters
  18386. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18387. (push (list 'org-agenda-remove-tags t) pars)
  18388. (if (> (length cmd-key) 2)
  18389. (eval (list 'let (nreverse pars)
  18390. (list 'org-tags-view nil cmd-key)))
  18391. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18392. (set-buffer org-agenda-buffer-name)
  18393. (let* ((lines (org-split-string (buffer-string) "\n"))
  18394. line)
  18395. (while (setq line (pop lines))
  18396. (catch 'next
  18397. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18398. (setq org-agenda-info
  18399. (org-fix-agenda-info (text-properties-at 0 line)))
  18400. (princ
  18401. (org-encode-for-stdout
  18402. (mapconcat 'org-agenda-export-csv-mapper
  18403. '(org-category txt type todo tags date time-of-day extra
  18404. priority-letter priority agenda-day)
  18405. ",")))
  18406. (princ "\n"))))))
  18407. (defun org-fix-agenda-info (props)
  18408. "Make sure all properties on an agenda item have a canonical form,
  18409. so the export commands can easily use it."
  18410. (let (tmp re)
  18411. (when (setq tmp (plist-get props 'tags))
  18412. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18413. (when (setq tmp (plist-get props 'date))
  18414. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18415. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18416. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18417. (setq tmp (calendar-date-string tmp)))
  18418. (setq props (plist-put props 'date tmp)))
  18419. (when (setq tmp (plist-get props 'day))
  18420. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18421. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18422. (setq tmp (calendar-date-string tmp)))
  18423. (setq props (plist-put props 'day tmp))
  18424. (setq props (plist-put props 'agenda-day tmp)))
  18425. (when (setq tmp (plist-get props 'txt))
  18426. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18427. (plist-put props 'priority-letter (match-string 1 tmp))
  18428. (setq tmp (replace-match "" t t tmp)))
  18429. (when (and (setq re (plist-get props 'org-todo-regexp))
  18430. (setq re (concat "\\`\\.*" re " ?"))
  18431. (string-match re tmp))
  18432. (plist-put props 'todo (match-string 1 tmp))
  18433. (setq tmp (replace-match "" t t tmp)))
  18434. (plist-put props 'txt tmp)))
  18435. props)
  18436. (defun org-agenda-export-csv-mapper (prop)
  18437. (let ((res (plist-get org-agenda-info prop)))
  18438. (setq res
  18439. (cond
  18440. ((not res) "")
  18441. ((stringp res) res)
  18442. (t (prin1-to-string res))))
  18443. (while (string-match "," res)
  18444. (setq res (replace-match ";" t t res)))
  18445. (org-trim res)))
  18446. ;;;###autoload
  18447. (defun org-store-agenda-views (&rest parameters)
  18448. (interactive)
  18449. (eval (list 'org-batch-store-agenda-views)))
  18450. ;; FIXME, why is this a macro?????
  18451. ;;;###autoload
  18452. (defmacro org-batch-store-agenda-views (&rest parameters)
  18453. "Run all custom agenda commands that have a file argument."
  18454. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18455. (pop-up-frames nil)
  18456. (dir default-directory)
  18457. pars cmd thiscmdkey files opts)
  18458. (while parameters
  18459. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18460. (setq pars (reverse pars))
  18461. (save-window-excursion
  18462. (while cmds
  18463. (setq cmd (pop cmds)
  18464. thiscmdkey (car cmd)
  18465. opts (nth 4 cmd)
  18466. files (nth 5 cmd))
  18467. (if (stringp files) (setq files (list files)))
  18468. (when files
  18469. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18470. (list 'org-agenda nil thiscmdkey)))
  18471. (set-buffer org-agenda-buffer-name)
  18472. (while files
  18473. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18474. (list 'org-write-agenda
  18475. (expand-file-name (pop files) dir) t))))
  18476. (and (get-buffer org-agenda-buffer-name)
  18477. (kill-buffer org-agenda-buffer-name)))))))
  18478. (defun org-write-agenda (file &optional nosettings)
  18479. "Write the current buffer (an agenda view) as a file.
  18480. Depending on the extension of the file name, plain text (.txt),
  18481. HTML (.html or .htm) or Postscript (.ps) is produced.
  18482. If NOSETTINGS is given, do not scope the settings of
  18483. `org-agenda-exporter-settings' into the export commands. This is used when
  18484. the settings have already been scoped and we do not wish to overrule other,
  18485. higher priority settings."
  18486. (interactive "FWrite agenda to file: ")
  18487. (if (not (file-writable-p file))
  18488. (error "Cannot write agenda to file %s" file))
  18489. (cond
  18490. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18491. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18492. (org-let (if nosettings nil org-agenda-exporter-settings)
  18493. '(save-excursion
  18494. (save-window-excursion
  18495. (cond
  18496. ((string-match "\\.html?\\'" file)
  18497. (set-buffer (htmlize-buffer (current-buffer)))
  18498. (when (and org-agenda-export-html-style
  18499. (string-match "<style>" org-agenda-export-html-style))
  18500. ;; replace <style> section with org-agenda-export-html-style
  18501. (goto-char (point-min))
  18502. (kill-region (- (search-forward "<style") 6)
  18503. (search-forward "</style>"))
  18504. (insert org-agenda-export-html-style))
  18505. (write-file file)
  18506. (kill-buffer (current-buffer))
  18507. (message "HTML written to %s" file))
  18508. ((string-match "\\.ps\\'" file)
  18509. (ps-print-buffer-with-faces file)
  18510. (message "Postscript written to %s" file))
  18511. (t
  18512. (let ((bs (buffer-string)))
  18513. (find-file file)
  18514. (insert bs)
  18515. (save-buffer 0)
  18516. (kill-buffer (current-buffer))
  18517. (message "Plain text written to %s" file))))))
  18518. (set-buffer org-agenda-buffer-name)))
  18519. (defmacro org-no-read-only (&rest body)
  18520. "Inhibit read-only for BODY."
  18521. `(let ((inhibit-read-only t)) ,@body))
  18522. (defun org-check-for-org-mode ()
  18523. "Make sure current buffer is in org-mode. Error if not."
  18524. (or (org-mode-p)
  18525. (error "Cannot execute org-mode agenda command on buffer in %s."
  18526. major-mode)))
  18527. (defun org-fit-agenda-window ()
  18528. "Fit the window to the buffer size."
  18529. (and (memq org-agenda-window-setup '(reorganize-frame))
  18530. (fboundp 'fit-window-to-buffer)
  18531. (fit-window-to-buffer
  18532. nil
  18533. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18534. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18535. ;;; Agenda file list
  18536. (defun org-agenda-files (&optional unrestricted)
  18537. "Get the list of agenda files.
  18538. Optional UNRESTRICTED means return the full list even if a restriction
  18539. is currently in place."
  18540. (let ((files
  18541. (cond
  18542. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18543. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18544. ((listp org-agenda-files) org-agenda-files)
  18545. (t (error "Invalid value of `org-agenda-files'")))))
  18546. (setq files (apply 'append
  18547. (mapcar (lambda (f)
  18548. (if (file-directory-p f)
  18549. (directory-files f t
  18550. org-agenda-file-regexp)
  18551. (list f)))
  18552. files)))
  18553. (if org-agenda-skip-unavailable-files
  18554. (delq nil
  18555. (mapcar (function
  18556. (lambda (file)
  18557. (and (file-readable-p file) file)))
  18558. files))
  18559. files))) ; `org-check-agenda-file' will remove them from the list
  18560. (defun org-edit-agenda-file-list ()
  18561. "Edit the list of agenda files.
  18562. Depending on setup, this either uses customize to edit the variable
  18563. `org-agenda-files', or it visits the file that is holding the list. In the
  18564. latter case, the buffer is set up in a way that saving it automatically kills
  18565. the buffer and restores the previous window configuration."
  18566. (interactive)
  18567. (if (stringp org-agenda-files)
  18568. (let ((cw (current-window-configuration)))
  18569. (find-file org-agenda-files)
  18570. (org-set-local 'org-window-configuration cw)
  18571. (org-add-hook 'after-save-hook
  18572. (lambda ()
  18573. (set-window-configuration
  18574. (prog1 org-window-configuration
  18575. (kill-buffer (current-buffer))))
  18576. (org-install-agenda-files-menu)
  18577. (message "New agenda file list installed"))
  18578. nil 'local)
  18579. (message "%s" (substitute-command-keys
  18580. "Edit list and finish with \\[save-buffer]")))
  18581. (customize-variable 'org-agenda-files)))
  18582. (defun org-store-new-agenda-file-list (list)
  18583. "Set new value for the agenda file list and save it correcly."
  18584. (if (stringp org-agenda-files)
  18585. (let ((f org-agenda-files) b)
  18586. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18587. (with-temp-file f
  18588. (insert (mapconcat 'identity list "\n") "\n")))
  18589. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18590. (setq org-agenda-files list)
  18591. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18592. (defun org-read-agenda-file-list ()
  18593. "Read the list of agenda files from a file."
  18594. (when (stringp org-agenda-files)
  18595. (with-temp-buffer
  18596. (insert-file-contents org-agenda-files)
  18597. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18598. ;;;###autoload
  18599. (defun org-cycle-agenda-files ()
  18600. "Cycle through the files in `org-agenda-files'.
  18601. If the current buffer visits an agenda file, find the next one in the list.
  18602. If the current buffer does not, find the first agenda file."
  18603. (interactive)
  18604. (let* ((fs (org-agenda-files t))
  18605. (files (append fs (list (car fs))))
  18606. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18607. file)
  18608. (unless files (error "No agenda files"))
  18609. (catch 'exit
  18610. (while (setq file (pop files))
  18611. (if (equal (file-truename file) tcf)
  18612. (when (car files)
  18613. (find-file (car files))
  18614. (throw 'exit t))))
  18615. (find-file (car fs)))
  18616. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18617. (defun org-agenda-file-to-front (&optional to-end)
  18618. "Move/add the current file to the top of the agenda file list.
  18619. If the file is not present in the list, it is added to the front. If it is
  18620. present, it is moved there. With optional argument TO-END, add/move to the
  18621. end of the list."
  18622. (interactive "P")
  18623. (let ((org-agenda-skip-unavailable-files nil)
  18624. (file-alist (mapcar (lambda (x)
  18625. (cons (file-truename x) x))
  18626. (org-agenda-files t)))
  18627. (ctf (file-truename buffer-file-name))
  18628. x had)
  18629. (setq x (assoc ctf file-alist) had x)
  18630. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18631. (if to-end
  18632. (setq file-alist (append (delq x file-alist) (list x)))
  18633. (setq file-alist (cons x (delq x file-alist))))
  18634. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18635. (org-install-agenda-files-menu)
  18636. (message "File %s to %s of agenda file list"
  18637. (if had "moved" "added") (if to-end "end" "front"))))
  18638. (defun org-remove-file (&optional file)
  18639. "Remove current file from the list of files in variable `org-agenda-files'.
  18640. These are the files which are being checked for agenda entries.
  18641. Optional argument FILE means, use this file instead of the current."
  18642. (interactive)
  18643. (let* ((org-agenda-skip-unavailable-files nil)
  18644. (file (or file buffer-file-name))
  18645. (true-file (file-truename file))
  18646. (afile (abbreviate-file-name file))
  18647. (files (delq nil (mapcar
  18648. (lambda (x)
  18649. (if (equal true-file
  18650. (file-truename x))
  18651. nil x))
  18652. (org-agenda-files t)))))
  18653. (if (not (= (length files) (length (org-agenda-files t))))
  18654. (progn
  18655. (org-store-new-agenda-file-list files)
  18656. (org-install-agenda-files-menu)
  18657. (message "Removed file: %s" afile))
  18658. (message "File was not in list: %s (not removed)" afile))))
  18659. (defun org-file-menu-entry (file)
  18660. (vector file (list 'find-file file) t))
  18661. (defun org-check-agenda-file (file)
  18662. "Make sure FILE exists. If not, ask user what to do."
  18663. (when (not (file-exists-p file))
  18664. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18665. (abbreviate-file-name file))
  18666. (let ((r (downcase (read-char-exclusive))))
  18667. (cond
  18668. ((equal r ?r)
  18669. (org-remove-file file)
  18670. (throw 'nextfile t))
  18671. (t (error "Abort"))))))
  18672. ;;; Agenda prepare and finalize
  18673. (defvar org-agenda-multi nil) ; dynammically scoped
  18674. (defvar org-agenda-buffer-name "*Org Agenda*")
  18675. (defvar org-pre-agenda-window-conf nil)
  18676. (defvar org-agenda-name nil)
  18677. (defun org-prepare-agenda (&optional name)
  18678. (setq org-todo-keywords-for-agenda nil)
  18679. (setq org-done-keywords-for-agenda nil)
  18680. (if org-agenda-multi
  18681. (progn
  18682. (setq buffer-read-only nil)
  18683. (goto-char (point-max))
  18684. (unless (or (bobp) org-agenda-compact-blocks)
  18685. (insert "\n" (make-string (window-width) ?=) "\n"))
  18686. (narrow-to-region (point) (point-max)))
  18687. (org-agenda-reset-markers)
  18688. (org-prepare-agenda-buffers (org-agenda-files))
  18689. (setq org-todo-keywords-for-agenda
  18690. (org-uniquify org-todo-keywords-for-agenda))
  18691. (setq org-done-keywords-for-agenda
  18692. (org-uniquify org-done-keywords-for-agenda))
  18693. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18694. (awin (get-buffer-window abuf)))
  18695. (cond
  18696. ((equal (current-buffer) abuf) nil)
  18697. (awin (select-window awin))
  18698. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18699. ((equal org-agenda-window-setup 'current-window)
  18700. (switch-to-buffer abuf))
  18701. ((equal org-agenda-window-setup 'other-window)
  18702. (org-switch-to-buffer-other-window abuf))
  18703. ((equal org-agenda-window-setup 'other-frame)
  18704. (switch-to-buffer-other-frame abuf))
  18705. ((equal org-agenda-window-setup 'reorganize-frame)
  18706. (delete-other-windows)
  18707. (org-switch-to-buffer-other-window abuf))))
  18708. (setq buffer-read-only nil)
  18709. (erase-buffer)
  18710. (org-agenda-mode)
  18711. (and name (not org-agenda-name)
  18712. (org-set-local 'org-agenda-name name)))
  18713. (setq buffer-read-only nil))
  18714. (defun org-finalize-agenda ()
  18715. "Finishing touch for the agenda buffer, called just before displaying it."
  18716. (unless org-agenda-multi
  18717. (save-excursion
  18718. (let ((inhibit-read-only t))
  18719. (goto-char (point-min))
  18720. (while (org-activate-bracket-links (point-max))
  18721. (add-text-properties (match-beginning 0) (match-end 0)
  18722. '(face org-link)))
  18723. (org-agenda-align-tags)
  18724. (unless org-agenda-with-colors
  18725. (remove-text-properties (point-min) (point-max) '(face nil))))
  18726. (if (and (boundp 'org-overriding-columns-format)
  18727. org-overriding-columns-format)
  18728. (org-set-local 'org-overriding-columns-format
  18729. org-overriding-columns-format))
  18730. (if (and (boundp 'org-agenda-view-columns-initially)
  18731. org-agenda-view-columns-initially)
  18732. (org-agenda-columns))
  18733. (when org-agenda-fontify-priorities
  18734. (org-fontify-priorities))
  18735. (run-hooks 'org-finalize-agenda-hook)
  18736. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18737. )))
  18738. (defun org-fontify-priorities ()
  18739. "Make highest priority lines bold, and lowest italic."
  18740. (interactive)
  18741. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18742. (org-delete-overlay o)))
  18743. (org-overlays-in (point-min) (point-max)))
  18744. (save-excursion
  18745. (let ((inhibit-read-only t)
  18746. b e p ov h l)
  18747. (goto-char (point-min))
  18748. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18749. (setq h (or (get-char-property (point) 'org-highest-priority)
  18750. org-highest-priority)
  18751. l (or (get-char-property (point) 'org-lowest-priority)
  18752. org-lowest-priority)
  18753. p (string-to-char (match-string 1))
  18754. b (match-beginning 0) e (point-at-eol)
  18755. ov (org-make-overlay b e))
  18756. (org-overlay-put
  18757. ov 'face
  18758. (cond ((listp org-agenda-fontify-priorities)
  18759. (cdr (assoc p org-agenda-fontify-priorities)))
  18760. ((equal p l) 'italic)
  18761. ((equal p h) 'bold)))
  18762. (org-overlay-put ov 'org-type 'org-priority)))))
  18763. (defun org-prepare-agenda-buffers (files)
  18764. "Create buffers for all agenda files, protect archived trees and comments."
  18765. (interactive)
  18766. (let ((pa '(:org-archived t))
  18767. (pc '(:org-comment t))
  18768. (pall '(:org-archived t :org-comment t))
  18769. (inhibit-read-only t)
  18770. (rea (concat ":" org-archive-tag ":"))
  18771. bmp file re)
  18772. (save-excursion
  18773. (save-restriction
  18774. (while (setq file (pop files))
  18775. (if (bufferp file)
  18776. (set-buffer file)
  18777. (org-check-agenda-file file)
  18778. (set-buffer (org-get-agenda-file-buffer file)))
  18779. (widen)
  18780. (setq bmp (buffer-modified-p))
  18781. (org-refresh-category-properties)
  18782. (setq org-todo-keywords-for-agenda
  18783. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18784. (setq org-done-keywords-for-agenda
  18785. (append org-done-keywords-for-agenda org-done-keywords))
  18786. (save-excursion
  18787. (remove-text-properties (point-min) (point-max) pall)
  18788. (when org-agenda-skip-archived-trees
  18789. (goto-char (point-min))
  18790. (while (re-search-forward rea nil t)
  18791. (if (org-on-heading-p t)
  18792. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18793. (goto-char (point-min))
  18794. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18795. (while (re-search-forward re nil t)
  18796. (add-text-properties
  18797. (match-beginning 0) (org-end-of-subtree t) pc)))
  18798. (set-buffer-modified-p bmp))))))
  18799. (defvar org-agenda-skip-function nil
  18800. "Function to be called at each match during agenda construction.
  18801. If this function returns nil, the current match should not be skipped.
  18802. Otherwise, the function must return a position from where the search
  18803. should be continued.
  18804. This may also be a Lisp form, it will be evaluated.
  18805. Never set this variable using `setq' or so, because then it will apply
  18806. to all future agenda commands. Instead, bind it with `let' to scope
  18807. it dynamically into the agenda-constructing command. A good way to set
  18808. it is through options in org-agenda-custom-commands.")
  18809. (defun org-agenda-skip ()
  18810. "Throw to `:skip' in places that should be skipped.
  18811. Also moves point to the end of the skipped region, so that search can
  18812. continue from there."
  18813. (let ((p (point-at-bol)) to fp)
  18814. (and org-agenda-skip-archived-trees
  18815. (get-text-property p :org-archived)
  18816. (org-end-of-subtree t)
  18817. (throw :skip t))
  18818. (and (get-text-property p :org-comment)
  18819. (org-end-of-subtree t)
  18820. (throw :skip t))
  18821. (if (equal (char-after p) ?#) (throw :skip t))
  18822. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18823. (consp org-agenda-skip-function))
  18824. (setq to (save-excursion
  18825. (save-match-data
  18826. (if fp
  18827. (funcall org-agenda-skip-function)
  18828. (eval org-agenda-skip-function))))))
  18829. (goto-char to)
  18830. (throw :skip t))))
  18831. (defvar org-agenda-markers nil
  18832. "List of all currently active markers created by `org-agenda'.")
  18833. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18834. "Creation time of the last agenda marker.")
  18835. (defun org-agenda-new-marker (&optional pos)
  18836. "Return a new agenda marker.
  18837. Org-mode keeps a list of these markers and resets them when they are
  18838. no longer in use."
  18839. (let ((m (copy-marker (or pos (point)))))
  18840. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18841. (push m org-agenda-markers)
  18842. m))
  18843. (defun org-agenda-reset-markers ()
  18844. "Reset markers created by `org-agenda'."
  18845. (while org-agenda-markers
  18846. (move-marker (pop org-agenda-markers) nil)))
  18847. (defun org-get-agenda-file-buffer (file)
  18848. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18849. it to the list of buffers which might be released later."
  18850. (let ((buf (org-find-base-buffer-visiting file)))
  18851. (if buf
  18852. buf ; just return it
  18853. ;; Make a new buffer and remember it
  18854. (setq buf (find-file-noselect file))
  18855. (if buf (push buf org-agenda-new-buffers))
  18856. buf)))
  18857. (defun org-release-buffers (blist)
  18858. "Release all buffers in list, asking the user for confirmation when needed.
  18859. When a buffer is unmodified, it is just killed. When modified, it is saved
  18860. \(if the user agrees) and then killed."
  18861. (let (buf file)
  18862. (while (setq buf (pop blist))
  18863. (setq file (buffer-file-name buf))
  18864. (when (and (buffer-modified-p buf)
  18865. file
  18866. (y-or-n-p (format "Save file %s? " file)))
  18867. (with-current-buffer buf (save-buffer)))
  18868. (kill-buffer buf))))
  18869. (defun org-get-category (&optional pos)
  18870. "Get the category applying to position POS."
  18871. (get-text-property (or pos (point)) 'org-category))
  18872. ;;; Agenda timeline
  18873. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18874. (defun org-timeline (&optional include-all)
  18875. "Show a time-sorted view of the entries in the current org file.
  18876. Only entries with a time stamp of today or later will be listed. With
  18877. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18878. under the current date.
  18879. If the buffer contains an active region, only check the region for
  18880. dates."
  18881. (interactive "P")
  18882. (require 'calendar)
  18883. (org-compile-prefix-format 'timeline)
  18884. (org-set-sorting-strategy 'timeline)
  18885. (let* ((dopast t)
  18886. (dotodo include-all)
  18887. (doclosed org-agenda-show-log)
  18888. (entry buffer-file-name)
  18889. (date (calendar-current-date))
  18890. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18891. (end (if (org-region-active-p) (region-end) (point-max)))
  18892. (day-numbers (org-get-all-dates beg end 'no-ranges
  18893. t doclosed ; always include today
  18894. org-timeline-show-empty-dates))
  18895. (org-deadline-warning-days 0)
  18896. (org-agenda-only-exact-dates t)
  18897. (today (time-to-days (current-time)))
  18898. (past t)
  18899. args
  18900. s e rtn d emptyp)
  18901. (setq org-agenda-redo-command
  18902. (list 'progn
  18903. (list 'org-switch-to-buffer-other-window (current-buffer))
  18904. (list 'org-timeline (list 'quote include-all))))
  18905. (if (not dopast)
  18906. ;; Remove past dates from the list of dates.
  18907. (setq day-numbers (delq nil (mapcar (lambda(x)
  18908. (if (>= x today) x nil))
  18909. day-numbers))))
  18910. (org-prepare-agenda (concat "Timeline "
  18911. (file-name-nondirectory buffer-file-name)))
  18912. (if doclosed (push :closed args))
  18913. (push :timestamp args)
  18914. (push :deadline args)
  18915. (push :scheduled args)
  18916. (push :sexp args)
  18917. (if dotodo (push :todo args))
  18918. (while (setq d (pop day-numbers))
  18919. (if (and (listp d) (eq (car d) :omitted))
  18920. (progn
  18921. (setq s (point))
  18922. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18923. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18924. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18925. (if (and (>= d today)
  18926. dopast
  18927. past)
  18928. (progn
  18929. (setq past nil)
  18930. (insert (make-string 79 ?-) "\n")))
  18931. (setq date (calendar-gregorian-from-absolute d))
  18932. (setq s (point))
  18933. (setq rtn (and (not emptyp)
  18934. (apply 'org-agenda-get-day-entries entry
  18935. date args)))
  18936. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18937. (progn
  18938. (insert
  18939. (if (stringp org-agenda-format-date)
  18940. (format-time-string org-agenda-format-date
  18941. (org-time-from-absolute date))
  18942. (funcall org-agenda-format-date date))
  18943. "\n")
  18944. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18945. (put-text-property s (1- (point)) 'org-date-line t)
  18946. (if (equal d today)
  18947. (put-text-property s (1- (point)) 'org-today t))
  18948. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18949. (put-text-property s (1- (point)) 'day d)))))
  18950. (goto-char (point-min))
  18951. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18952. (point-min)))
  18953. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18954. (org-finalize-agenda)
  18955. (setq buffer-read-only t)))
  18956. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18957. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18958. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18959. not every single day in the range. If FORCE-TODAY is non-nil, make
  18960. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18961. inactive time stamps (those in square brackets) are included.
  18962. When EMPTY is non-nil, also include days without any entries."
  18963. (let ((re (concat
  18964. (if pre-re pre-re "")
  18965. (if inactive org-ts-regexp-both org-ts-regexp)))
  18966. dates dates1 date day day1 day2 ts1 ts2)
  18967. (if force-today
  18968. (setq dates (list (time-to-days (current-time)))))
  18969. (save-excursion
  18970. (goto-char beg)
  18971. (while (re-search-forward re end t)
  18972. (setq day (time-to-days (org-time-string-to-time
  18973. (substring (match-string 1) 0 10))))
  18974. (or (memq day dates) (push day dates)))
  18975. (unless no-ranges
  18976. (goto-char beg)
  18977. (while (re-search-forward org-tr-regexp end t)
  18978. (setq ts1 (substring (match-string 1) 0 10)
  18979. ts2 (substring (match-string 2) 0 10)
  18980. day1 (time-to-days (org-time-string-to-time ts1))
  18981. day2 (time-to-days (org-time-string-to-time ts2)))
  18982. (while (< (setq day1 (1+ day1)) day2)
  18983. (or (memq day1 dates) (push day1 dates)))))
  18984. (setq dates (sort dates '<))
  18985. (when empty
  18986. (while (setq day (pop dates))
  18987. (setq day2 (car dates))
  18988. (push day dates1)
  18989. (when (and day2 empty)
  18990. (if (or (eq empty t)
  18991. (and (numberp empty) (<= (- day2 day) empty)))
  18992. (while (< (setq day (1+ day)) day2)
  18993. (push (list day) dates1))
  18994. (push (cons :omitted (- day2 day)) dates1))))
  18995. (setq dates (nreverse dates1)))
  18996. dates)))
  18997. ;;; Agenda Daily/Weekly
  18998. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18999. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19000. (defvar org-agenda-last-arguments nil
  19001. "The arguments of the previous call to org-agenda")
  19002. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19003. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19004. (defvar org-include-all-loc nil) ; local variable
  19005. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19006. ;;;###autoload
  19007. (defun org-agenda-list (&optional include-all start-day ndays)
  19008. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19009. The view will be for the current day or week, but from the overview buffer
  19010. you will be able to go to other days/weeks.
  19011. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19012. all unfinished TODO items will also be shown, before the agenda.
  19013. This feature is considered obsolete, please use the TODO list or a block
  19014. agenda instead.
  19015. With a numeric prefix argument in an interactive call, the agenda will
  19016. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19017. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19018. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19019. given in `org-agenda-start-on-weekday'."
  19020. (interactive "P")
  19021. (if (and (integerp include-all) (> include-all 0))
  19022. (setq ndays include-all include-all nil))
  19023. (setq ndays (or ndays org-agenda-ndays)
  19024. start-day (or start-day org-agenda-start-day))
  19025. (if org-agenda-overriding-arguments
  19026. (setq include-all (car org-agenda-overriding-arguments)
  19027. start-day (nth 1 org-agenda-overriding-arguments)
  19028. ndays (nth 2 org-agenda-overriding-arguments)))
  19029. (if (stringp start-day)
  19030. ;; Convert to an absolute day number
  19031. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19032. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19033. (org-compile-prefix-format 'agenda)
  19034. (org-set-sorting-strategy 'agenda)
  19035. (require 'calendar)
  19036. (let* ((org-agenda-start-on-weekday
  19037. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19038. org-agenda-start-on-weekday nil))
  19039. (thefiles (org-agenda-files))
  19040. (files thefiles)
  19041. (today (time-to-days
  19042. (time-subtract (current-time)
  19043. (list 0 (* 3600 org-extend-today-until) 0))))
  19044. (sd (or start-day today))
  19045. (start (if (or (null org-agenda-start-on-weekday)
  19046. (< org-agenda-ndays 7))
  19047. sd
  19048. (let* ((nt (calendar-day-of-week
  19049. (calendar-gregorian-from-absolute sd)))
  19050. (n1 org-agenda-start-on-weekday)
  19051. (d (- nt n1)))
  19052. (- sd (+ (if (< d 0) 7 0) d)))))
  19053. (day-numbers (list start))
  19054. (day-cnt 0)
  19055. (inhibit-redisplay (not debug-on-error))
  19056. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19057. (setq org-agenda-redo-command
  19058. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19059. ;; Make the list of days
  19060. (setq ndays (or ndays org-agenda-ndays)
  19061. nd ndays)
  19062. (while (> ndays 1)
  19063. (push (1+ (car day-numbers)) day-numbers)
  19064. (setq ndays (1- ndays)))
  19065. (setq day-numbers (nreverse day-numbers))
  19066. (org-prepare-agenda "Day/Week")
  19067. (org-set-local 'org-starting-day (car day-numbers))
  19068. (org-set-local 'org-include-all-loc include-all)
  19069. (org-set-local 'org-agenda-span
  19070. (org-agenda-ndays-to-span nd))
  19071. (when (and (or include-all org-agenda-include-all-todo)
  19072. (member today day-numbers))
  19073. (setq files thefiles
  19074. rtnall nil)
  19075. (while (setq file (pop files))
  19076. (catch 'nextfile
  19077. (org-check-agenda-file file)
  19078. (setq date (calendar-gregorian-from-absolute today)
  19079. rtn (org-agenda-get-day-entries
  19080. file date :todo))
  19081. (setq rtnall (append rtnall rtn))))
  19082. (when rtnall
  19083. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19084. (add-text-properties (point-min) (1- (point))
  19085. (list 'face 'org-agenda-structure))
  19086. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19087. (unless org-agenda-compact-blocks
  19088. (setq s (point))
  19089. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19090. "-agenda:\n")
  19091. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19092. 'org-date-line t)))
  19093. (while (setq d (pop day-numbers))
  19094. (setq date (calendar-gregorian-from-absolute d)
  19095. s (point))
  19096. (if (or (setq todayp (= d today))
  19097. (and (not start-pos) (= d sd)))
  19098. (setq start-pos (point))
  19099. (if (and start-pos (not end-pos))
  19100. (setq end-pos (point))))
  19101. (setq files thefiles
  19102. rtnall nil)
  19103. (while (setq file (pop files))
  19104. (catch 'nextfile
  19105. (org-check-agenda-file file)
  19106. (if org-agenda-show-log
  19107. (setq rtn (org-agenda-get-day-entries
  19108. file date
  19109. :deadline :scheduled :timestamp :sexp :closed))
  19110. (setq rtn (org-agenda-get-day-entries
  19111. file date
  19112. :deadline :scheduled :sexp :timestamp)))
  19113. (setq rtnall (append rtnall rtn))))
  19114. (if org-agenda-include-diary
  19115. (progn
  19116. (require 'diary-lib)
  19117. (setq rtn (org-get-entries-from-diary date))
  19118. (setq rtnall (append rtnall rtn))))
  19119. (if (or rtnall org-agenda-show-all-dates)
  19120. (progn
  19121. (setq day-cnt (1+ day-cnt))
  19122. (insert
  19123. (if (stringp org-agenda-format-date)
  19124. (format-time-string org-agenda-format-date
  19125. (org-time-from-absolute date))
  19126. (funcall org-agenda-format-date date))
  19127. "\n")
  19128. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19129. (put-text-property s (1- (point)) 'org-date-line t)
  19130. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19131. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19132. (if rtnall (insert
  19133. (org-finalize-agenda-entries
  19134. (org-agenda-add-time-grid-maybe
  19135. rtnall nd todayp))
  19136. "\n"))
  19137. (put-text-property s (1- (point)) 'day d)
  19138. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19139. (goto-char (point-min))
  19140. (org-fit-agenda-window)
  19141. (unless (and (pos-visible-in-window-p (point-min))
  19142. (pos-visible-in-window-p (point-max)))
  19143. (goto-char (1- (point-max)))
  19144. (recenter -1)
  19145. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19146. (progn
  19147. (goto-char (or start-pos 1))
  19148. (recenter 1))))
  19149. (goto-char (or start-pos 1))
  19150. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19151. (org-finalize-agenda)
  19152. (setq buffer-read-only t)
  19153. (message "")))
  19154. (defun org-agenda-ndays-to-span (n)
  19155. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19156. ;;; Agenda TODO list
  19157. (defvar org-select-this-todo-keyword nil)
  19158. (defvar org-last-arg nil)
  19159. ;;;###autoload
  19160. (defun org-todo-list (arg)
  19161. "Show all TODO entries from all agenda file in a single list.
  19162. The prefix arg can be used to select a specific TODO keyword and limit
  19163. the list to these. When using \\[universal-argument], you will be prompted
  19164. for a keyword. A numeric prefix directly selects the Nth keyword in
  19165. `org-todo-keywords-1'."
  19166. (interactive "P")
  19167. (require 'calendar)
  19168. (org-compile-prefix-format 'todo)
  19169. (org-set-sorting-strategy 'todo)
  19170. (org-prepare-agenda "TODO")
  19171. (let* ((today (time-to-days (current-time)))
  19172. (date (calendar-gregorian-from-absolute today))
  19173. (kwds org-todo-keywords-for-agenda)
  19174. (completion-ignore-case t)
  19175. (org-select-this-todo-keyword
  19176. (if (stringp arg) arg
  19177. (and arg (integerp arg) (> arg 0)
  19178. (nth (1- arg) kwds))))
  19179. rtn rtnall files file pos)
  19180. (when (equal arg '(4))
  19181. (setq org-select-this-todo-keyword
  19182. (completing-read "Keyword (or KWD1|K2D2|...): "
  19183. (mapcar 'list kwds) nil nil)))
  19184. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19185. (org-set-local 'org-last-arg arg)
  19186. (setq org-agenda-redo-command
  19187. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19188. (setq files (org-agenda-files)
  19189. rtnall nil)
  19190. (while (setq file (pop files))
  19191. (catch 'nextfile
  19192. (org-check-agenda-file file)
  19193. (setq rtn (org-agenda-get-day-entries file date :todo))
  19194. (setq rtnall (append rtnall rtn))))
  19195. (if org-agenda-overriding-header
  19196. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19197. nil 'face 'org-agenda-structure) "\n")
  19198. (insert "Global list of TODO items of type: ")
  19199. (add-text-properties (point-min) (1- (point))
  19200. (list 'face 'org-agenda-structure))
  19201. (setq pos (point))
  19202. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19203. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19204. (setq pos (point))
  19205. (unless org-agenda-multi
  19206. (insert "Available with `N r': (0)ALL")
  19207. (let ((n 0) s)
  19208. (mapc (lambda (x)
  19209. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19210. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19211. (insert "\n "))
  19212. (insert " " s))
  19213. kwds))
  19214. (insert "\n"))
  19215. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19216. (when rtnall
  19217. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19218. (goto-char (point-min))
  19219. (org-fit-agenda-window)
  19220. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19221. (org-finalize-agenda)
  19222. (setq buffer-read-only t)))
  19223. ;;; Agenda tags match
  19224. ;;;###autoload
  19225. (defun org-tags-view (&optional todo-only match)
  19226. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19227. The prefix arg TODO-ONLY limits the search to TODO entries."
  19228. (interactive "P")
  19229. (org-compile-prefix-format 'tags)
  19230. (org-set-sorting-strategy 'tags)
  19231. (let* ((org-tags-match-list-sublevels
  19232. (if todo-only t org-tags-match-list-sublevels))
  19233. (completion-ignore-case t)
  19234. rtn rtnall files file pos matcher
  19235. buffer)
  19236. (setq matcher (org-make-tags-matcher match)
  19237. match (car matcher) matcher (cdr matcher))
  19238. (org-prepare-agenda (concat "TAGS " match))
  19239. (setq org-agenda-redo-command
  19240. (list 'org-tags-view (list 'quote todo-only)
  19241. (list 'if 'current-prefix-arg nil match)))
  19242. (setq files (org-agenda-files)
  19243. rtnall nil)
  19244. (while (setq file (pop files))
  19245. (catch 'nextfile
  19246. (org-check-agenda-file file)
  19247. (setq buffer (if (file-exists-p file)
  19248. (org-get-agenda-file-buffer file)
  19249. (error "No such file %s" file)))
  19250. (if (not buffer)
  19251. ;; If file does not exist, merror message to agenda
  19252. (setq rtn (list
  19253. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19254. rtnall (append rtnall rtn))
  19255. (with-current-buffer buffer
  19256. (unless (org-mode-p)
  19257. (error "Agenda file %s is not in `org-mode'" file))
  19258. (save-excursion
  19259. (save-restriction
  19260. (if org-agenda-restrict
  19261. (narrow-to-region org-agenda-restrict-begin
  19262. org-agenda-restrict-end)
  19263. (widen))
  19264. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19265. (setq rtnall (append rtnall rtn))))))))
  19266. (if org-agenda-overriding-header
  19267. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19268. nil 'face 'org-agenda-structure) "\n")
  19269. (insert "Headlines with TAGS match: ")
  19270. (add-text-properties (point-min) (1- (point))
  19271. (list 'face 'org-agenda-structure))
  19272. (setq pos (point))
  19273. (insert match "\n")
  19274. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19275. (setq pos (point))
  19276. (unless org-agenda-multi
  19277. (insert "Press `C-u r' to search again with new search string\n"))
  19278. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19279. (when rtnall
  19280. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19281. (goto-char (point-min))
  19282. (org-fit-agenda-window)
  19283. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19284. (org-finalize-agenda)
  19285. (setq buffer-read-only t)))
  19286. ;;; Agenda Finding stuck projects
  19287. (defvar org-agenda-skip-regexp nil
  19288. "Regular expression used in skipping subtrees for the agenda.
  19289. This is basically a temporary global variable that can be set and then
  19290. used by user-defined selections using `org-agenda-skip-function'.")
  19291. (defvar org-agenda-overriding-header nil
  19292. "When this is set during todo and tags searches, will replace header.")
  19293. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19294. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19295. If yes, it returns the end position of this tree, causing agenda commands
  19296. to skip this subtree. This is a function that can be put into
  19297. `org-agenda-skip-function' for the duration of a command."
  19298. (let ((end (save-excursion (org-end-of-subtree t)))
  19299. skip)
  19300. (save-excursion
  19301. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19302. (and skip end)))
  19303. (defun org-agenda-skip-entry-if (&rest conditions)
  19304. "Skip entry if any of CONDITIONS is true.
  19305. See `org-agenda-skip-if' for details."
  19306. (org-agenda-skip-if nil conditions))
  19307. (defun org-agenda-skip-subtree-if (&rest conditions)
  19308. "Skip entry if any of CONDITIONS is true.
  19309. See `org-agenda-skip-if' for details."
  19310. (org-agenda-skip-if t conditions))
  19311. (defun org-agenda-skip-if (subtree conditions)
  19312. "Checks current entity for CONDITIONS.
  19313. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19314. the entry, i.e. the text before the next heading is checked.
  19315. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19316. from different tests. Valid conditions are:
  19317. scheduled Check if there is a scheduled cookie
  19318. notscheduled Check if there is no scheduled cookie
  19319. deadline Check if there is a deadline
  19320. notdeadline Check if there is no deadline
  19321. regexp Check if regexp matches
  19322. notregexp Check if regexp does not match.
  19323. The regexp is taken from the conditions list, it must come right after
  19324. the `regexp' or `notregexp' element.
  19325. If any of these conditions is met, this function returns the end point of
  19326. the entity, causing the search to continue from there. This is a function
  19327. that can be put into `org-agenda-skip-function' for the duration of a command."
  19328. (let (beg end m)
  19329. (org-back-to-heading t)
  19330. (setq beg (point)
  19331. end (if subtree
  19332. (progn (org-end-of-subtree t) (point))
  19333. (progn (outline-next-heading) (1- (point)))))
  19334. (goto-char beg)
  19335. (and
  19336. (or
  19337. (and (memq 'scheduled conditions)
  19338. (re-search-forward org-scheduled-time-regexp end t))
  19339. (and (memq 'notscheduled conditions)
  19340. (not (re-search-forward org-scheduled-time-regexp end t)))
  19341. (and (memq 'deadline conditions)
  19342. (re-search-forward org-deadline-time-regexp end t))
  19343. (and (memq 'notdeadline conditions)
  19344. (not (re-search-forward org-deadline-time-regexp end t)))
  19345. (and (setq m (memq 'regexp conditions))
  19346. (stringp (nth 1 m))
  19347. (re-search-forward (nth 1 m) end t))
  19348. (and (setq m (memq 'notregexp conditions))
  19349. (stringp (nth 1 m))
  19350. (not (re-search-forward (nth 1 m) end t))))
  19351. end)))
  19352. ;;;###autoload
  19353. (defun org-agenda-list-stuck-projects (&rest ignore)
  19354. "Create agenda view for projects that are stuck.
  19355. Stuck projects are project that have no next actions. For the definitions
  19356. of what a project is and how to check if it stuck, customize the variable
  19357. `org-stuck-projects'.
  19358. MATCH is being ignored."
  19359. (interactive)
  19360. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19361. ;; FIXME: we could have used org-agenda-skip-if here.
  19362. (org-agenda-overriding-header "List of stuck projects: ")
  19363. (matcher (nth 0 org-stuck-projects))
  19364. (todo (nth 1 org-stuck-projects))
  19365. (todo-wds (if (member "*" todo)
  19366. (progn
  19367. (org-prepare-agenda-buffers (org-agenda-files))
  19368. (org-delete-all
  19369. org-done-keywords-for-agenda
  19370. (copy-sequence org-todo-keywords-for-agenda)))
  19371. todo))
  19372. (todo-re (concat "^\\*+[ \t]+\\("
  19373. (mapconcat 'identity todo-wds "\\|")
  19374. "\\)\\>"))
  19375. (tags (nth 2 org-stuck-projects))
  19376. (tags-re (if (member "*" tags)
  19377. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19378. (concat "^\\*+ .*:\\("
  19379. (mapconcat 'identity tags "\\|")
  19380. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19381. (gen-re (nth 3 org-stuck-projects))
  19382. (re-list
  19383. (delq nil
  19384. (list
  19385. (if todo todo-re)
  19386. (if tags tags-re)
  19387. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19388. gen-re)))))
  19389. (setq org-agenda-skip-regexp
  19390. (if re-list
  19391. (mapconcat 'identity re-list "\\|")
  19392. (error "No information how to identify unstuck projects")))
  19393. (org-tags-view nil matcher)
  19394. (with-current-buffer org-agenda-buffer-name
  19395. (setq org-agenda-redo-command
  19396. '(org-agenda-list-stuck-projects
  19397. (or current-prefix-arg org-last-arg))))))
  19398. ;;; Diary integration
  19399. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19400. (defun org-get-entries-from-diary (date)
  19401. "Get the (Emacs Calendar) diary entries for DATE."
  19402. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19403. (diary-display-hook '(fancy-diary-display))
  19404. (pop-up-frames nil)
  19405. (list-diary-entries-hook
  19406. (cons 'org-diary-default-entry list-diary-entries-hook))
  19407. (diary-file-name-prefix-function nil) ; turn this feature off
  19408. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19409. entries
  19410. (org-disable-agenda-to-diary t))
  19411. (save-excursion
  19412. (save-window-excursion
  19413. (funcall (if (fboundp 'diary-list-entries)
  19414. 'diary-list-entries 'list-diary-entries)
  19415. date 1)))
  19416. (if (not (get-buffer fancy-diary-buffer))
  19417. (setq entries nil)
  19418. (with-current-buffer fancy-diary-buffer
  19419. (setq buffer-read-only nil)
  19420. (if (zerop (buffer-size))
  19421. ;; No entries
  19422. (setq entries nil)
  19423. ;; Omit the date and other unnecessary stuff
  19424. (org-agenda-cleanup-fancy-diary)
  19425. ;; Add prefix to each line and extend the text properties
  19426. (if (zerop (buffer-size))
  19427. (setq entries nil)
  19428. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19429. (set-buffer-modified-p nil)
  19430. (kill-buffer fancy-diary-buffer)))
  19431. (when entries
  19432. (setq entries (org-split-string entries "\n"))
  19433. (setq entries
  19434. (mapcar
  19435. (lambda (x)
  19436. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19437. ;; Extend the text properties to the beginning of the line
  19438. (org-add-props x (text-properties-at (1- (length x)) x)
  19439. 'type "diary" 'date date))
  19440. entries)))))
  19441. (defun org-agenda-cleanup-fancy-diary ()
  19442. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19443. This gets rid of the date, the underline under the date, and
  19444. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19445. date. It also removes lines that contain only whitespace."
  19446. (goto-char (point-min))
  19447. (if (looking-at ".*?:[ \t]*")
  19448. (progn
  19449. (replace-match "")
  19450. (re-search-forward "\n=+$" nil t)
  19451. (replace-match "")
  19452. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19453. (re-search-forward "\n=+$" nil t)
  19454. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19455. (goto-char (point-min))
  19456. (while (re-search-forward "^ +\n" nil t)
  19457. (replace-match ""))
  19458. (goto-char (point-min))
  19459. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19460. (replace-match "")))
  19461. ;; Make sure entries from the diary have the right text properties.
  19462. (eval-after-load "diary-lib"
  19463. '(if (boundp 'diary-modify-entry-list-string-function)
  19464. ;; We can rely on the hook, nothing to do
  19465. nil
  19466. ;; Hook not avaiable, must use advice to make this work
  19467. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19468. "Make the position visible."
  19469. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19470. (stringp string)
  19471. buffer-file-name)
  19472. (setq string (org-modify-diary-entry-string string))))))
  19473. (defun org-modify-diary-entry-string (string)
  19474. "Add text properties to string, allowing org-mode to act on it."
  19475. (org-add-props string nil
  19476. 'mouse-face 'highlight
  19477. 'keymap org-agenda-keymap
  19478. 'help-echo (if buffer-file-name
  19479. (format "mouse-2 or RET jump to diary file %s"
  19480. (abbreviate-file-name buffer-file-name))
  19481. "")
  19482. 'org-agenda-diary-link t
  19483. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19484. (defun org-diary-default-entry ()
  19485. "Add a dummy entry to the diary.
  19486. Needed to avoid empty dates which mess up holiday display."
  19487. ;; Catch the error if dealing with the new add-to-diary-alist
  19488. (when org-disable-agenda-to-diary
  19489. (condition-case nil
  19490. (add-to-diary-list original-date "Org-mode dummy" "")
  19491. (error
  19492. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19493. ;;;###autoload
  19494. (defun org-diary (&rest args)
  19495. "Return diary information from org-files.
  19496. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19497. It accesses org files and extracts information from those files to be
  19498. listed in the diary. The function accepts arguments specifying what
  19499. items should be listed. The following arguments are allowed:
  19500. :timestamp List the headlines of items containing a date stamp or
  19501. date range matching the selected date. Deadlines will
  19502. also be listed, on the expiration day.
  19503. :sexp List entries resulting from diary-like sexps.
  19504. :deadline List any deadlines past due, or due within
  19505. `org-deadline-warning-days'. The listing occurs only
  19506. in the diary for *today*, not at any other date. If
  19507. an entry is marked DONE, it is no longer listed.
  19508. :scheduled List all items which are scheduled for the given date.
  19509. The diary for *today* also contains items which were
  19510. scheduled earlier and are not yet marked DONE.
  19511. :todo List all TODO items from the org-file. This may be a
  19512. long list - so this is not turned on by default.
  19513. Like deadlines, these entries only show up in the
  19514. diary for *today*, not at any other date.
  19515. The call in the diary file should look like this:
  19516. &%%(org-diary) ~/path/to/some/orgfile.org
  19517. Use a separate line for each org file to check. Or, if you omit the file name,
  19518. all files listed in `org-agenda-files' will be checked automatically:
  19519. &%%(org-diary)
  19520. If you don't give any arguments (as in the example above), the default
  19521. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19522. So the example above may also be written as
  19523. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19524. The function expects the lisp variables `entry' and `date' to be provided
  19525. by the caller, because this is how the calendar works. Don't use this
  19526. function from a program - use `org-agenda-get-day-entries' instead."
  19527. (when (> (- (time-to-seconds (current-time))
  19528. org-agenda-last-marker-time)
  19529. 5)
  19530. (org-agenda-reset-markers))
  19531. (org-compile-prefix-format 'agenda)
  19532. (org-set-sorting-strategy 'agenda)
  19533. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19534. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19535. (list entry)
  19536. (org-agenda-files t)))
  19537. file rtn results)
  19538. (org-prepare-agenda-buffers files)
  19539. ;; If this is called during org-agenda, don't return any entries to
  19540. ;; the calendar. Org Agenda will list these entries itself.
  19541. (if org-disable-agenda-to-diary (setq files nil))
  19542. (while (setq file (pop files))
  19543. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19544. (setq results (append results rtn)))
  19545. (if results
  19546. (concat (org-finalize-agenda-entries results) "\n"))))
  19547. ;;; Agenda entry finders
  19548. (defun org-agenda-get-day-entries (file date &rest args)
  19549. "Does the work for `org-diary' and `org-agenda'.
  19550. FILE is the path to a file to be checked for entries. DATE is date like
  19551. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19552. which kind of entries should be extracted. For details about these, see
  19553. the documentation of `org-diary'."
  19554. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19555. (let* ((org-startup-folded nil)
  19556. (org-startup-align-all-tables nil)
  19557. (buffer (if (file-exists-p file)
  19558. (org-get-agenda-file-buffer file)
  19559. (error "No such file %s" file)))
  19560. arg results rtn)
  19561. (if (not buffer)
  19562. ;; If file does not exist, make sure an error message ends up in diary
  19563. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19564. (with-current-buffer buffer
  19565. (unless (org-mode-p)
  19566. (error "Agenda file %s is not in `org-mode'" file))
  19567. (let ((case-fold-search nil))
  19568. (save-excursion
  19569. (save-restriction
  19570. (if org-agenda-restrict
  19571. (narrow-to-region org-agenda-restrict-begin
  19572. org-agenda-restrict-end)
  19573. (widen))
  19574. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19575. (while (setq arg (pop args))
  19576. (cond
  19577. ((and (eq arg :todo)
  19578. (equal date (calendar-current-date)))
  19579. (setq rtn (org-agenda-get-todos))
  19580. (setq results (append results rtn)))
  19581. ((eq arg :timestamp)
  19582. (setq rtn (org-agenda-get-blocks))
  19583. (setq results (append results rtn))
  19584. (setq rtn (org-agenda-get-timestamps))
  19585. (setq results (append results rtn)))
  19586. ((eq arg :sexp)
  19587. (setq rtn (org-agenda-get-sexps))
  19588. (setq results (append results rtn)))
  19589. ((eq arg :scheduled)
  19590. (setq rtn (org-agenda-get-scheduled))
  19591. (setq results (append results rtn)))
  19592. ((eq arg :closed)
  19593. (setq rtn (org-agenda-get-closed))
  19594. (setq results (append results rtn)))
  19595. ((eq arg :deadline)
  19596. (setq rtn (org-agenda-get-deadlines))
  19597. (setq results (append results rtn))))))))
  19598. results))))
  19599. (defun org-entry-is-todo-p ()
  19600. (member (org-get-todo-state) org-not-done-keywords))
  19601. (defun org-entry-is-done-p ()
  19602. (member (org-get-todo-state) org-done-keywords))
  19603. (defun org-get-todo-state ()
  19604. (save-excursion
  19605. (org-back-to-heading t)
  19606. (and (looking-at org-todo-line-regexp)
  19607. (match-end 2)
  19608. (match-string 2))))
  19609. (defun org-at-date-range-p (&optional inactive-ok)
  19610. "Is the cursor inside a date range?"
  19611. (interactive)
  19612. (save-excursion
  19613. (catch 'exit
  19614. (let ((pos (point)))
  19615. (skip-chars-backward "^[<\r\n")
  19616. (skip-chars-backward "<[")
  19617. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19618. (>= (match-end 0) pos)
  19619. (throw 'exit t))
  19620. (skip-chars-backward "^<[\r\n")
  19621. (skip-chars-backward "<[")
  19622. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19623. (>= (match-end 0) pos)
  19624. (throw 'exit t)))
  19625. nil)))
  19626. (defun org-agenda-get-todos ()
  19627. "Return the TODO information for agenda display."
  19628. (let* ((props (list 'face nil
  19629. 'done-face 'org-done
  19630. 'org-not-done-regexp org-not-done-regexp
  19631. 'org-todo-regexp org-todo-regexp
  19632. 'mouse-face 'highlight
  19633. 'keymap org-agenda-keymap
  19634. 'help-echo
  19635. (format "mouse-2 or RET jump to org file %s"
  19636. (abbreviate-file-name buffer-file-name))))
  19637. ;; FIXME: get rid of the \n at some point but watch out
  19638. (regexp (concat "^\\*+[ \t]+\\("
  19639. (if org-select-this-todo-keyword
  19640. (if (equal org-select-this-todo-keyword "*")
  19641. org-todo-regexp
  19642. (concat "\\<\\("
  19643. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19644. "\\)\\>"))
  19645. org-not-done-regexp)
  19646. "[^\n\r]*\\)"))
  19647. marker priority category tags
  19648. ee txt beg end)
  19649. (goto-char (point-min))
  19650. (while (re-search-forward regexp nil t)
  19651. (catch :skip
  19652. (save-match-data
  19653. (beginning-of-line)
  19654. (setq beg (point) end (progn (outline-next-heading) (point)))
  19655. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19656. (re-search-forward org-ts-regexp end t))
  19657. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19658. (re-search-forward org-scheduled-time-regexp end t))
  19659. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19660. (re-search-forward org-deadline-time-regexp end t)
  19661. (org-deadline-close (match-string 1))))
  19662. (goto-char (1+ beg))
  19663. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19664. (throw :skip nil)))
  19665. (goto-char beg)
  19666. (org-agenda-skip)
  19667. (goto-char (match-beginning 1))
  19668. (setq marker (org-agenda-new-marker (match-beginning 0))
  19669. category (org-get-category)
  19670. tags (org-get-tags-at (point))
  19671. txt (org-format-agenda-item "" (match-string 1) category tags)
  19672. priority (1+ (org-get-priority txt)))
  19673. (org-add-props txt props
  19674. 'org-marker marker 'org-hd-marker marker
  19675. 'priority priority 'org-category category
  19676. 'type "todo")
  19677. (push txt ee)
  19678. (if org-agenda-todo-list-sublevels
  19679. (goto-char (match-end 1))
  19680. (org-end-of-subtree 'invisible))))
  19681. (nreverse ee)))
  19682. (defconst org-agenda-no-heading-message
  19683. "No heading for this item in buffer or region.")
  19684. (defun org-agenda-get-timestamps ()
  19685. "Return the date stamp information for agenda display."
  19686. (let* ((props (list 'face nil
  19687. 'org-not-done-regexp org-not-done-regexp
  19688. 'org-todo-regexp org-todo-regexp
  19689. 'mouse-face 'highlight
  19690. 'keymap org-agenda-keymap
  19691. 'help-echo
  19692. (format "mouse-2 or RET jump to org file %s"
  19693. (abbreviate-file-name buffer-file-name))))
  19694. (d1 (calendar-absolute-from-gregorian date))
  19695. (remove-re
  19696. (concat
  19697. (regexp-quote
  19698. (format-time-string
  19699. "<%Y-%m-%d"
  19700. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19701. ".*?>"))
  19702. (regexp
  19703. (concat
  19704. (regexp-quote
  19705. (substring
  19706. (format-time-string
  19707. (car org-time-stamp-formats)
  19708. (apply 'encode-time ; DATE bound by calendar
  19709. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19710. 0 11))
  19711. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19712. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19713. marker hdmarker deadlinep scheduledp donep tmp priority category
  19714. ee txt timestr tags b0 b3 e3 head)
  19715. (goto-char (point-min))
  19716. (while (re-search-forward regexp nil t)
  19717. (setq b0 (match-beginning 0)
  19718. b3 (match-beginning 3) e3 (match-end 3))
  19719. (catch :skip
  19720. (and (org-at-date-range-p) (throw :skip nil))
  19721. (org-agenda-skip)
  19722. (if (and (match-end 1)
  19723. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19724. (throw :skip nil))
  19725. (if (and e3
  19726. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19727. (throw :skip nil))
  19728. (setq marker (org-agenda-new-marker b0)
  19729. category (org-get-category b0)
  19730. tmp (buffer-substring (max (point-min)
  19731. (- b0 org-ds-keyword-length))
  19732. b0)
  19733. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19734. deadlinep (string-match org-deadline-regexp tmp)
  19735. scheduledp (string-match org-scheduled-regexp tmp)
  19736. donep (org-entry-is-done-p))
  19737. (if (or scheduledp deadlinep) (throw :skip t))
  19738. (if (string-match ">" timestr)
  19739. ;; substring should only run to end of time stamp
  19740. (setq timestr (substring timestr 0 (match-end 0))))
  19741. (save-excursion
  19742. (if (re-search-backward "^\\*+ " nil t)
  19743. (progn
  19744. (goto-char (match-beginning 0))
  19745. (setq hdmarker (org-agenda-new-marker)
  19746. tags (org-get-tags-at))
  19747. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19748. (setq head (match-string 1))
  19749. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19750. (setq txt (org-format-agenda-item
  19751. nil head category tags timestr nil
  19752. remove-re)))
  19753. (setq txt org-agenda-no-heading-message))
  19754. (setq priority (org-get-priority txt))
  19755. (org-add-props txt props
  19756. 'org-marker marker 'org-hd-marker hdmarker)
  19757. (org-add-props txt nil 'priority priority
  19758. 'org-category category 'date date
  19759. 'type "timestamp")
  19760. (push txt ee))
  19761. (outline-next-heading)))
  19762. (nreverse ee)))
  19763. (defun org-agenda-get-sexps ()
  19764. "Return the sexp information for agenda display."
  19765. (require 'diary-lib)
  19766. (let* ((props (list 'face nil
  19767. 'mouse-face 'highlight
  19768. 'keymap org-agenda-keymap
  19769. 'help-echo
  19770. (format "mouse-2 or RET jump to org file %s"
  19771. (abbreviate-file-name buffer-file-name))))
  19772. (regexp "^&?%%(")
  19773. marker category ee txt tags entry result beg b sexp sexp-entry)
  19774. (goto-char (point-min))
  19775. (while (re-search-forward regexp nil t)
  19776. (catch :skip
  19777. (org-agenda-skip)
  19778. (setq beg (match-beginning 0))
  19779. (goto-char (1- (match-end 0)))
  19780. (setq b (point))
  19781. (forward-sexp 1)
  19782. (setq sexp (buffer-substring b (point)))
  19783. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19784. (org-trim (match-string 1))
  19785. ""))
  19786. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19787. (when result
  19788. (setq marker (org-agenda-new-marker beg)
  19789. category (org-get-category beg))
  19790. (if (string-match "\\S-" result)
  19791. (setq txt result)
  19792. (setq txt "SEXP entry returned empty string"))
  19793. (setq txt (org-format-agenda-item
  19794. "" txt category tags 'time))
  19795. (org-add-props txt props 'org-marker marker)
  19796. (org-add-props txt nil
  19797. 'org-category category 'date date
  19798. 'type "sexp")
  19799. (push txt ee))))
  19800. (nreverse ee)))
  19801. (defun org-agenda-get-closed ()
  19802. "Return the logged TODO entries for agenda display."
  19803. (let* ((props (list 'mouse-face 'highlight
  19804. 'org-not-done-regexp org-not-done-regexp
  19805. 'org-todo-regexp org-todo-regexp
  19806. 'keymap org-agenda-keymap
  19807. 'help-echo
  19808. (format "mouse-2 or RET jump to org file %s"
  19809. (abbreviate-file-name buffer-file-name))))
  19810. (regexp (concat
  19811. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19812. (regexp-quote
  19813. (substring
  19814. (format-time-string
  19815. (car org-time-stamp-formats)
  19816. (apply 'encode-time ; DATE bound by calendar
  19817. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19818. 1 11))))
  19819. marker hdmarker priority category tags closedp
  19820. ee txt timestr)
  19821. (goto-char (point-min))
  19822. (while (re-search-forward regexp nil t)
  19823. (catch :skip
  19824. (org-agenda-skip)
  19825. (setq marker (org-agenda-new-marker (match-beginning 0))
  19826. closedp (equal (match-string 1) org-closed-string)
  19827. category (org-get-category (match-beginning 0))
  19828. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19829. ;; donep (org-entry-is-done-p)
  19830. )
  19831. (if (string-match "\\]" timestr)
  19832. ;; substring should only run to end of time stamp
  19833. (setq timestr (substring timestr 0 (match-end 0))))
  19834. (save-excursion
  19835. (if (re-search-backward "^\\*+ " nil t)
  19836. (progn
  19837. (goto-char (match-beginning 0))
  19838. (setq hdmarker (org-agenda-new-marker)
  19839. tags (org-get-tags-at))
  19840. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19841. (setq txt (org-format-agenda-item
  19842. (if closedp "Closed: " "Clocked: ")
  19843. (match-string 1) category tags timestr)))
  19844. (setq txt org-agenda-no-heading-message))
  19845. (setq priority 100000)
  19846. (org-add-props txt props
  19847. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19848. 'priority priority 'org-category category
  19849. 'type "closed" 'date date
  19850. 'undone-face 'org-warning 'done-face 'org-done)
  19851. (push txt ee))
  19852. (goto-char (point-at-eol))))
  19853. (nreverse ee)))
  19854. (defun org-agenda-get-deadlines ()
  19855. "Return the deadline information for agenda display."
  19856. (let* ((props (list 'mouse-face 'highlight
  19857. 'org-not-done-regexp org-not-done-regexp
  19858. 'org-todo-regexp org-todo-regexp
  19859. 'keymap org-agenda-keymap
  19860. 'help-echo
  19861. (format "mouse-2 or RET jump to org file %s"
  19862. (abbreviate-file-name buffer-file-name))))
  19863. (regexp org-deadline-time-regexp)
  19864. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19865. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19866. d2 diff dfrac wdays pos pos1 category tags
  19867. ee txt head face s upcomingp donep timestr)
  19868. (goto-char (point-min))
  19869. (while (re-search-forward regexp nil t)
  19870. (catch :skip
  19871. (org-agenda-skip)
  19872. (setq s (match-string 1)
  19873. pos (1- (match-beginning 1))
  19874. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19875. diff (- d2 d1)
  19876. wdays (org-get-wdays s)
  19877. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  19878. upcomingp (and todayp (> diff 0)))
  19879. ;; When to show a deadline in the calendar:
  19880. ;; If the expiration is within wdays warning time.
  19881. ;; Past-due deadlines are only shown on the current date
  19882. (if (or (and (<= diff wdays)
  19883. (and todayp (not org-agenda-only-exact-dates)))
  19884. (= diff 0))
  19885. (save-excursion
  19886. (setq category (org-get-category))
  19887. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19888. (progn
  19889. (goto-char (match-end 0))
  19890. (setq pos1 (match-beginning 0))
  19891. (setq tags (org-get-tags-at pos1))
  19892. (setq head (buffer-substring-no-properties
  19893. (point)
  19894. (progn (skip-chars-forward "^\r\n")
  19895. (point))))
  19896. (setq donep (string-match org-looking-at-done-regexp head))
  19897. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19898. (setq timestr
  19899. (concat (substring s (match-beginning 1)) " "))
  19900. (setq timestr 'time))
  19901. (if (and donep
  19902. (or org-agenda-skip-deadline-if-done
  19903. (not (= diff 0))))
  19904. (setq txt nil)
  19905. (setq txt (org-format-agenda-item
  19906. (if (= diff 0)
  19907. (car org-agenda-deadline-leaders)
  19908. (format (nth 1 org-agenda-deadline-leaders)
  19909. diff))
  19910. head category tags timestr))))
  19911. (setq txt org-agenda-no-heading-message))
  19912. (when txt
  19913. (setq face (org-agenda-deadline-face dfrac))
  19914. (org-add-props txt props
  19915. 'org-marker (org-agenda-new-marker pos)
  19916. 'org-hd-marker (org-agenda-new-marker pos1)
  19917. 'priority (+ (- diff)
  19918. (org-get-priority txt))
  19919. 'org-category category
  19920. 'type (if upcomingp "upcoming-deadline" "deadline")
  19921. 'date (if upcomingp date d2)
  19922. 'face (if donep 'org-done face)
  19923. 'undone-face face 'done-face 'org-done)
  19924. (push txt ee))))))
  19925. (nreverse ee)))
  19926. (defun org-agenda-deadline-face (fraction)
  19927. "Return the face to displaying a deadline item.
  19928. FRACTION is what fraction of the head-warning time has passed."
  19929. (let ((faces org-agenda-deadline-faces) f)
  19930. (catch 'exit
  19931. (while (setq f (pop faces))
  19932. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19933. (defun org-agenda-get-scheduled ()
  19934. "Return the scheduled information for agenda display."
  19935. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19936. 'org-todo-regexp org-todo-regexp
  19937. 'done-face 'org-done
  19938. 'mouse-face 'highlight
  19939. 'keymap org-agenda-keymap
  19940. 'help-echo
  19941. (format "mouse-2 or RET jump to org file %s"
  19942. (abbreviate-file-name buffer-file-name))))
  19943. (regexp org-scheduled-time-regexp)
  19944. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19945. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19946. d2 diff pos pos1 category tags
  19947. ee txt head pastschedp donep face timestr s)
  19948. (goto-char (point-min))
  19949. (while (re-search-forward regexp nil t)
  19950. (catch :skip
  19951. (org-agenda-skip)
  19952. (setq s (match-string 1)
  19953. pos (1- (match-beginning 1))
  19954. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19955. ;;; is this right?
  19956. ;;; do we need to do this for deadleine too????
  19957. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19958. diff (- d2 d1))
  19959. (setq pastschedp (and todayp (< diff 0)))
  19960. ;; When to show a scheduled item in the calendar:
  19961. ;; If it is on or past the date.
  19962. (if (or (and (< diff 0)
  19963. (and todayp (not org-agenda-only-exact-dates)))
  19964. (= diff 0))
  19965. (save-excursion
  19966. (setq category (org-get-category))
  19967. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19968. (progn
  19969. (goto-char (match-end 0))
  19970. (setq pos1 (match-beginning 0))
  19971. (setq tags (org-get-tags-at))
  19972. (setq head (buffer-substring-no-properties
  19973. (point)
  19974. (progn (skip-chars-forward "^\r\n") (point))))
  19975. (setq donep (string-match org-looking-at-done-regexp head))
  19976. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19977. (setq timestr
  19978. (concat (substring s (match-beginning 1)) " "))
  19979. (setq timestr 'time))
  19980. (if (and donep
  19981. (or org-agenda-skip-scheduled-if-done
  19982. (not (= diff 0))))
  19983. (setq txt nil)
  19984. (setq txt (org-format-agenda-item
  19985. (if (= diff 0)
  19986. (car org-agenda-scheduled-leaders)
  19987. (format (nth 1 org-agenda-scheduled-leaders)
  19988. (- 1 diff)))
  19989. head category tags timestr))))
  19990. (setq txt org-agenda-no-heading-message))
  19991. (when txt
  19992. (setq face (if pastschedp
  19993. 'org-scheduled-previously
  19994. 'org-scheduled-today))
  19995. (org-add-props txt props
  19996. 'undone-face face
  19997. 'face (if donep 'org-done face)
  19998. 'org-marker (org-agenda-new-marker pos)
  19999. 'org-hd-marker (org-agenda-new-marker pos1)
  20000. 'type (if pastschedp "past-scheduled" "scheduled")
  20001. 'date (if pastschedp d2 date)
  20002. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20003. 'org-category category)
  20004. (push txt ee))))))
  20005. (nreverse ee)))
  20006. (defun org-agenda-get-blocks ()
  20007. "Return the date-range information for agenda display."
  20008. (let* ((props (list 'face nil
  20009. 'org-not-done-regexp org-not-done-regexp
  20010. 'org-todo-regexp org-todo-regexp
  20011. 'mouse-face 'highlight
  20012. 'keymap org-agenda-keymap
  20013. 'help-echo
  20014. (format "mouse-2 or RET jump to org file %s"
  20015. (abbreviate-file-name buffer-file-name))))
  20016. (regexp org-tr-regexp)
  20017. (d0 (calendar-absolute-from-gregorian date))
  20018. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20019. donep head)
  20020. (goto-char (point-min))
  20021. (while (re-search-forward regexp nil t)
  20022. (catch :skip
  20023. (org-agenda-skip)
  20024. (setq pos (point))
  20025. (setq timestr (match-string 0)
  20026. s1 (match-string 1)
  20027. s2 (match-string 2)
  20028. d1 (time-to-days (org-time-string-to-time s1))
  20029. d2 (time-to-days (org-time-string-to-time s2)))
  20030. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20031. ;; Only allow days between the limits, because the normal
  20032. ;; date stamps will catch the limits.
  20033. (save-excursion
  20034. (setq marker (org-agenda-new-marker (point)))
  20035. (setq category (org-get-category))
  20036. (if (re-search-backward "^\\*+ " nil t)
  20037. (progn
  20038. (goto-char (match-beginning 0))
  20039. (setq hdmarker (org-agenda-new-marker (point)))
  20040. (setq tags (org-get-tags-at))
  20041. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20042. (setq head (match-string 1))
  20043. (and org-agenda-skip-timestamp-if-done
  20044. (org-entry-is-done-p)
  20045. (throw :skip t))
  20046. (setq txt (org-format-agenda-item
  20047. (format (if (= d1 d2) "" "(%d/%d): ")
  20048. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20049. head category tags
  20050. (if (= d0 d1) timestr))))
  20051. (setq txt org-agenda-no-heading-message))
  20052. (org-add-props txt props
  20053. 'org-marker marker 'org-hd-marker hdmarker
  20054. 'type "block" 'date date
  20055. 'priority (org-get-priority txt) 'org-category category)
  20056. (push txt ee)))
  20057. (goto-char pos)))
  20058. ;; Sort the entries by expiration date.
  20059. (nreverse ee)))
  20060. ;;; Agenda presentation and sorting
  20061. (defconst org-plain-time-of-day-regexp
  20062. (concat
  20063. "\\(\\<[012]?[0-9]"
  20064. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20065. "\\(--?"
  20066. "\\(\\<[012]?[0-9]"
  20067. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20068. "\\)?")
  20069. "Regular expression to match a plain time or time range.
  20070. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20071. groups carry important information:
  20072. 0 the full match
  20073. 1 the first time, range or not
  20074. 8 the second time, if it is a range.")
  20075. (defconst org-plain-time-extension-regexp
  20076. (concat
  20077. "\\(\\<[012]?[0-9]"
  20078. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20079. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20080. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20081. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20082. groups carry important information:
  20083. 0 the full match
  20084. 7 hours of duration
  20085. 9 minutes of duration")
  20086. (defconst org-stamp-time-of-day-regexp
  20087. (concat
  20088. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20089. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20090. "\\(--?"
  20091. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20092. "Regular expression to match a timestamp time or time range.
  20093. After a match, the following groups carry important information:
  20094. 0 the full match
  20095. 1 date plus weekday, for backreferencing to make sure both times on same day
  20096. 2 the first time, range or not
  20097. 4 the second time, if it is a range.")
  20098. (defvar org-prefix-has-time nil
  20099. "A flag, set by `org-compile-prefix-format'.
  20100. The flag is set if the currently compiled format contains a `%t'.")
  20101. (defvar org-prefix-has-tag nil
  20102. "A flag, set by `org-compile-prefix-format'.
  20103. The flag is set if the currently compiled format contains a `%T'.")
  20104. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20105. noprefix remove-re)
  20106. "Format TXT to be inserted into the agenda buffer.
  20107. In particular, it adds the prefix and corresponding text properties. EXTRA
  20108. must be a string and replaces the `%s' specifier in the prefix format.
  20109. CATEGORY (string, symbol or nil) may be used to overrule the default
  20110. category taken from local variable or file name. It will replace the `%c'
  20111. specifier in the format. DOTIME, when non-nil, indicates that a
  20112. time-of-day should be extracted from TXT for sorting of this entry, and for
  20113. the `%t' specifier in the format. When DOTIME is a string, this string is
  20114. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20115. only the correctly processes TXT should be returned - this is used by
  20116. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20117. Any match of REMOVE-RE will be removed from TXT."
  20118. (save-match-data
  20119. ;; Diary entries sometimes have extra whitespace at the beginning
  20120. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20121. (let* ((category (or category
  20122. org-category
  20123. (if buffer-file-name
  20124. (file-name-sans-extension
  20125. (file-name-nondirectory buffer-file-name))
  20126. "")))
  20127. (tag (if tags (nth (1- (length tags)) tags) ""))
  20128. time ; time and tag are needed for the eval of the prefix format
  20129. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20130. (time-of-day (and dotime (org-get-time-of-day ts)))
  20131. stamp plain s0 s1 s2 rtn srp)
  20132. (when (and dotime time-of-day org-prefix-has-time)
  20133. ;; Extract starting and ending time and move them to prefix
  20134. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20135. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20136. (setq s0 (match-string 0 ts)
  20137. srp (and stamp (match-end 3))
  20138. s1 (match-string (if plain 1 2) ts)
  20139. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20140. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20141. ;; them, we might want to remove them there to avoid duplication.
  20142. ;; The user can turn this off with a variable.
  20143. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20144. (string-match (concat (regexp-quote s0) " *") txt)
  20145. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20146. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20147. (= (match-beginning 0) 0)
  20148. t))
  20149. (setq txt (replace-match "" nil nil txt))))
  20150. ;; Normalize the time(s) to 24 hour
  20151. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20152. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20153. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20154. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20155. (let ((m (+ (string-to-number (match-string 2 s1))
  20156. (* 60 (string-to-number (match-string 1 s1)))
  20157. org-agenda-default-appointment-duration))
  20158. h)
  20159. (setq h (/ m 60) m (- m (* h 60)))
  20160. (setq s2 (format "%02d:%02d" h m))))
  20161. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20162. txt)
  20163. ;; Tags are in the string
  20164. (if (or (eq org-agenda-remove-tags t)
  20165. (and org-agenda-remove-tags
  20166. org-prefix-has-tag))
  20167. (setq txt (replace-match "" t t txt))
  20168. (setq txt (replace-match
  20169. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20170. (match-string 2 txt))
  20171. t t txt))))
  20172. (when remove-re
  20173. (while (string-match remove-re txt)
  20174. (setq txt (replace-match "" t t txt))))
  20175. ;; Create the final string
  20176. (if noprefix
  20177. (setq rtn txt)
  20178. ;; Prepare the variables needed in the eval of the compiled format
  20179. (setq time (cond (s2 (concat s1 "-" s2))
  20180. (s1 (concat s1 "......"))
  20181. (t ""))
  20182. extra (or extra "")
  20183. category (if (symbolp category) (symbol-name category) category))
  20184. ;; Evaluate the compiled format
  20185. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20186. ;; And finally add the text properties
  20187. (org-add-props rtn nil
  20188. 'org-category (downcase category) 'tags tags
  20189. 'org-highest-priority org-highest-priority
  20190. 'org-lowest-priority org-lowest-priority
  20191. 'prefix-length (- (length rtn) (length txt))
  20192. 'time-of-day time-of-day
  20193. 'txt txt
  20194. 'time time
  20195. 'extra extra
  20196. 'dotime dotime))))
  20197. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20198. (defvar org-agenda-sorting-strategy-selected nil)
  20199. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20200. (catch 'exit
  20201. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20202. ((and todayp (member 'today (car org-agenda-time-grid))))
  20203. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20204. ((member 'weekly (car org-agenda-time-grid)))
  20205. (t (throw 'exit list)))
  20206. (let* ((have (delq nil (mapcar
  20207. (lambda (x) (get-text-property 1 'time-of-day x))
  20208. list)))
  20209. (string (nth 1 org-agenda-time-grid))
  20210. (gridtimes (nth 2 org-agenda-time-grid))
  20211. (req (car org-agenda-time-grid))
  20212. (remove (member 'remove-match req))
  20213. new time)
  20214. (if (and (member 'require-timed req) (not have))
  20215. ;; don't show empty grid
  20216. (throw 'exit list))
  20217. (while (setq time (pop gridtimes))
  20218. (unless (and remove (member time have))
  20219. (setq time (int-to-string time))
  20220. (push (org-format-agenda-item
  20221. nil string "" nil
  20222. (concat (substring time 0 -2) ":" (substring time -2)))
  20223. new)
  20224. (put-text-property
  20225. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20226. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20227. (append new list)
  20228. (append list new)))))
  20229. (defun org-compile-prefix-format (key)
  20230. "Compile the prefix format into a Lisp form that can be evaluated.
  20231. The resulting form is returned and stored in the variable
  20232. `org-prefix-format-compiled'."
  20233. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20234. (let ((s (cond
  20235. ((stringp org-agenda-prefix-format)
  20236. org-agenda-prefix-format)
  20237. ((assq key org-agenda-prefix-format)
  20238. (cdr (assq key org-agenda-prefix-format)))
  20239. (t " %-12:c%?-12t% s")))
  20240. (start 0)
  20241. varform vars var e c f opt)
  20242. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20243. s start)
  20244. (setq var (cdr (assoc (match-string 4 s)
  20245. '(("c" . category) ("t" . time) ("s" . extra)
  20246. ("T" . tag))))
  20247. c (or (match-string 3 s) "")
  20248. opt (match-beginning 1)
  20249. start (1+ (match-beginning 0)))
  20250. (if (equal var 'time) (setq org-prefix-has-time t))
  20251. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20252. (setq f (concat "%" (match-string 2 s) "s"))
  20253. (if opt
  20254. (setq varform
  20255. `(if (equal "" ,var)
  20256. ""
  20257. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20258. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20259. (setq s (replace-match "%s" t nil s))
  20260. (push varform vars))
  20261. (setq vars (nreverse vars))
  20262. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20263. (defun org-set-sorting-strategy (key)
  20264. (if (symbolp (car org-agenda-sorting-strategy))
  20265. ;; the old format
  20266. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20267. (setq org-agenda-sorting-strategy-selected
  20268. (or (cdr (assq key org-agenda-sorting-strategy))
  20269. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20270. '(time-up category-keep priority-down)))))
  20271. (defun org-get-time-of-day (s &optional string mod24)
  20272. "Check string S for a time of day.
  20273. If found, return it as a military time number between 0 and 2400.
  20274. If not found, return nil.
  20275. The optional STRING argument forces conversion into a 5 character wide string
  20276. HH:MM."
  20277. (save-match-data
  20278. (when
  20279. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20280. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20281. (let* ((h (string-to-number (match-string 1 s)))
  20282. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20283. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20284. (am-p (equal ampm "am"))
  20285. (h1 (cond ((not ampm) h)
  20286. ((= h 12) (if am-p 0 12))
  20287. (t (+ h (if am-p 0 12)))))
  20288. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20289. (mod h1 24) h1))
  20290. (t0 (+ (* 100 h2) m))
  20291. (t1 (concat (if (>= h1 24) "+" " ")
  20292. (if (< t0 100) "0" "")
  20293. (if (< t0 10) "0" "")
  20294. (int-to-string t0))))
  20295. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20296. (defun org-finalize-agenda-entries (list &optional nosort)
  20297. "Sort and concatenate the agenda items."
  20298. (setq list (mapcar 'org-agenda-highlight-todo list))
  20299. (if nosort
  20300. list
  20301. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20302. (defun org-agenda-highlight-todo (x)
  20303. (let (re pl)
  20304. (if (eq x 'line)
  20305. (save-excursion
  20306. (beginning-of-line 1)
  20307. (setq re (get-text-property (point) 'org-todo-regexp))
  20308. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20309. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20310. (add-text-properties (match-beginning 0) (match-end 0)
  20311. (list 'face (org-get-todo-face 0)))
  20312. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20313. (delete-region (match-beginning 1) (1- (match-end 0)))
  20314. (goto-char (match-beginning 1))
  20315. (insert (format org-agenda-todo-keyword-format s)))))
  20316. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20317. pl (get-text-property 0 'prefix-length x))
  20318. (when (and re
  20319. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20320. x (or pl 0)) pl))
  20321. (add-text-properties
  20322. (or (match-end 1) (match-end 0)) (match-end 0)
  20323. (list 'face (org-get-todo-face (match-string 2 x)))
  20324. x)
  20325. (setq x (concat (substring x 0 (match-end 1))
  20326. (format org-agenda-todo-keyword-format
  20327. (match-string 2 x))
  20328. " "
  20329. (substring x (match-end 3)))))
  20330. x)))
  20331. (defsubst org-cmp-priority (a b)
  20332. "Compare the priorities of string A and B."
  20333. (let ((pa (or (get-text-property 1 'priority a) 0))
  20334. (pb (or (get-text-property 1 'priority b) 0)))
  20335. (cond ((> pa pb) +1)
  20336. ((< pa pb) -1)
  20337. (t nil))))
  20338. (defsubst org-cmp-category (a b)
  20339. "Compare the string values of categories of strings A and B."
  20340. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20341. (cb (or (get-text-property 1 'org-category b) "")))
  20342. (cond ((string-lessp ca cb) -1)
  20343. ((string-lessp cb ca) +1)
  20344. (t nil))))
  20345. (defsubst org-cmp-tag (a b)
  20346. "Compare the string values of categories of strings A and B."
  20347. (let ((ta (car (last (get-text-property 1 'tags a))))
  20348. (tb (car (last (get-text-property 1 'tags b)))))
  20349. (cond ((not ta) +1)
  20350. ((not tb) -1)
  20351. ((string-lessp ta tb) -1)
  20352. ((string-lessp tb ta) +1)
  20353. (t nil))))
  20354. (defsubst org-cmp-time (a b)
  20355. "Compare the time-of-day values of strings A and B."
  20356. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20357. (ta (or (get-text-property 1 'time-of-day a) def))
  20358. (tb (or (get-text-property 1 'time-of-day b) def)))
  20359. (cond ((< ta tb) -1)
  20360. ((< tb ta) +1)
  20361. (t nil))))
  20362. (defun org-entries-lessp (a b)
  20363. "Predicate for sorting agenda entries."
  20364. ;; The following variables will be used when the form is evaluated.
  20365. ;; So even though the compiler complains, keep them.
  20366. (let* ((time-up (org-cmp-time a b))
  20367. (time-down (if time-up (- time-up) nil))
  20368. (priority-up (org-cmp-priority a b))
  20369. (priority-down (if priority-up (- priority-up) nil))
  20370. (category-up (org-cmp-category a b))
  20371. (category-down (if category-up (- category-up) nil))
  20372. (category-keep (if category-up +1 nil))
  20373. (tag-up (org-cmp-tag a b))
  20374. (tag-down (if tag-up (- tag-up) nil)))
  20375. (cdr (assoc
  20376. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20377. '((-1 . t) (1 . nil) (nil . nil))))))
  20378. ;;; Agenda restriction lock
  20379. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20380. "Overlay to mark the headline to which arenda commands are restricted.")
  20381. (org-overlay-put org-agenda-restriction-lock-overlay
  20382. 'face 'org-agenda-restriction-lock)
  20383. (org-overlay-put org-agenda-restriction-lock-overlay
  20384. 'help-echo "Agendas are currently limited to this subtree.")
  20385. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20386. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20387. "Overlay marking the agenda restriction line in speedbar.")
  20388. (org-overlay-put org-speedbar-restriction-lock-overlay
  20389. 'face 'org-agenda-restriction-lock)
  20390. (org-overlay-put org-speedbar-restriction-lock-overlay
  20391. 'help-echo "Agendas are currently limited to this item.")
  20392. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20393. (defun org-agenda-set-restriction-lock (&optional type)
  20394. "Set restriction lock for agenda, to current subtree or file.
  20395. Restriction will be the file if TYPE is `file', or if type is the
  20396. universal prefix '(4), or if the cursor is before the first headline
  20397. in the file. Otherwise, restriction will be to the current subtree."
  20398. (interactive "P")
  20399. (and (equal type '(4)) (setq type 'file))
  20400. (setq type (cond
  20401. (type type)
  20402. ((org-at-heading-p) 'subtree)
  20403. ((condition-case nil (org-back-to-heading t) (error nil))
  20404. 'subtree)
  20405. (t 'file)))
  20406. (if (eq type 'subtree)
  20407. (progn
  20408. (setq org-agenda-restrict t)
  20409. (setq org-agenda-overriding-restriction 'subtree)
  20410. (put 'org-agenda-files 'org-restrict
  20411. (list (buffer-file-name (buffer-base-buffer))))
  20412. (org-back-to-heading t)
  20413. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20414. (move-marker org-agenda-restrict-begin (point))
  20415. (move-marker org-agenda-restrict-end
  20416. (save-excursion (org-end-of-subtree t)))
  20417. (message "Locking agenda restriction to subtree"))
  20418. (put 'org-agenda-files 'org-restrict
  20419. (list (buffer-file-name (buffer-base-buffer))))
  20420. (setq org-agenda-restrict nil)
  20421. (setq org-agenda-overriding-restriction 'file)
  20422. (move-marker org-agenda-restrict-begin nil)
  20423. (move-marker org-agenda-restrict-end nil)
  20424. (message "Locking agenda restriction to file"))
  20425. (setq current-prefix-arg nil)
  20426. (org-agenda-maybe-redo))
  20427. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20428. "Remove the agenda restriction lock."
  20429. (interactive "P")
  20430. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20431. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20432. (setq org-agenda-overriding-restriction nil)
  20433. (setq org-agenda-restrict nil)
  20434. (put 'org-agenda-files 'org-restrict nil)
  20435. (move-marker org-agenda-restrict-begin nil)
  20436. (move-marker org-agenda-restrict-end nil)
  20437. (setq current-prefix-arg nil)
  20438. (message "Agenda restriction lock removed")
  20439. (or noupdate (org-agenda-maybe-redo)))
  20440. (defun org-agenda-maybe-redo ()
  20441. "If there is any window showing the agenda view, update it."
  20442. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20443. (w0 (selected-window)))
  20444. (when w
  20445. (select-window w)
  20446. (org-agenda-redo)
  20447. (select-window w0)
  20448. (if org-agenda-overriding-restriction
  20449. (message "Agenda view shifted to new %s restriction"
  20450. org-agenda-overriding-restriction)
  20451. (message "Agenda restriction lock removed")))))
  20452. ;;; Agenda commands
  20453. (defun org-agenda-check-type (error &rest types)
  20454. "Check if agenda buffer is of allowed type.
  20455. If ERROR is non-nil, throw an error, otherwise just return nil."
  20456. (if (memq org-agenda-type types)
  20457. t
  20458. (if error
  20459. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20460. nil)))
  20461. (defun org-agenda-quit ()
  20462. "Exit agenda by removing the window or the buffer."
  20463. (interactive)
  20464. (let ((buf (current-buffer)))
  20465. (if (not (one-window-p)) (delete-window))
  20466. (kill-buffer buf)
  20467. (org-agenda-reset-markers)
  20468. (org-columns-remove-overlays))
  20469. ;; Maybe restore the pre-agenda window configuration.
  20470. (and org-agenda-restore-windows-after-quit
  20471. (not (eq org-agenda-window-setup 'other-frame))
  20472. org-pre-agenda-window-conf
  20473. (set-window-configuration org-pre-agenda-window-conf)))
  20474. (defun org-agenda-exit ()
  20475. "Exit agenda by removing the window or the buffer.
  20476. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20477. Org-mode buffers visited directly by the user will not be touched."
  20478. (interactive)
  20479. (org-release-buffers org-agenda-new-buffers)
  20480. (setq org-agenda-new-buffers nil)
  20481. (org-agenda-quit))
  20482. (defun org-agenda-execute (arg)
  20483. "Execute another agenda command, keeping same window.\\<global-map>
  20484. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20485. (interactive "P")
  20486. (let ((org-agenda-window-setup 'current-window))
  20487. (org-agenda arg)))
  20488. (defun org-save-all-org-buffers ()
  20489. "Save all Org-mode buffers without user confirmation."
  20490. (interactive)
  20491. (message "Saving all Org-mode buffers...")
  20492. (save-some-buffers t 'org-mode-p)
  20493. (message "Saving all Org-mode buffers... done"))
  20494. (defun org-agenda-redo ()
  20495. "Rebuild Agenda.
  20496. When this is the global TODO list, a prefix argument will be interpreted."
  20497. (interactive)
  20498. (let* ((org-agenda-keep-modes t)
  20499. (line (org-current-line))
  20500. (window-line (- line (org-current-line (window-start))))
  20501. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20502. (message "Rebuilding agenda buffer...")
  20503. (org-let lprops '(eval org-agenda-redo-command))
  20504. (setq org-agenda-undo-list nil
  20505. org-agenda-pending-undo-list nil)
  20506. (message "Rebuilding agenda buffer...done")
  20507. (goto-line line)
  20508. (recenter window-line)))
  20509. (defun org-agenda-goto-date (date)
  20510. "Jump to DATE in agenda."
  20511. (interactive (list (org-read-date)))
  20512. (org-agenda-list nil date))
  20513. (defun org-agenda-goto-today ()
  20514. "Go to today."
  20515. (interactive)
  20516. (org-agenda-check-type t 'timeline 'agenda)
  20517. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20518. (cond
  20519. (tdpos (goto-char tdpos))
  20520. ((eq org-agenda-type 'agenda)
  20521. (let* ((sd (time-to-days
  20522. (time-subtract (current-time)
  20523. (list 0 (* 3600 org-extend-today-until) 0))))
  20524. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20525. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20526. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20527. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20528. (org-agenda-redo)
  20529. (org-agenda-find-same-or-today-or-agenda)))
  20530. (t (error "Cannot find today")))))
  20531. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20532. (goto-char
  20533. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20534. (text-property-any (point-min) (point-max) 'org-today t)
  20535. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20536. (point-min))))
  20537. (defun org-agenda-later (arg)
  20538. "Go forward in time by thee current span.
  20539. With prefix ARG, go forward that many times the current span."
  20540. (interactive "p")
  20541. (org-agenda-check-type t 'agenda)
  20542. (let* ((span org-agenda-span)
  20543. (sd org-starting-day)
  20544. (greg (calendar-gregorian-from-absolute sd))
  20545. (cnt (get-text-property (point) 'org-day-cnt))
  20546. greg2 nd)
  20547. (cond
  20548. ((eq span 'day)
  20549. (setq sd (+ arg sd) nd 1))
  20550. ((eq span 'week)
  20551. (setq sd (+ (* 7 arg) sd) nd 7))
  20552. ((eq span 'month)
  20553. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20554. sd (calendar-absolute-from-gregorian greg2))
  20555. (setcar greg2 (1+ (car greg2)))
  20556. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20557. ((eq span 'year)
  20558. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20559. sd (calendar-absolute-from-gregorian greg2))
  20560. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20561. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20562. (let ((org-agenda-overriding-arguments
  20563. (list (car org-agenda-last-arguments) sd nd t)))
  20564. (org-agenda-redo)
  20565. (org-agenda-find-same-or-today-or-agenda cnt))))
  20566. (defun org-agenda-earlier (arg)
  20567. "Go backward in time by the current span.
  20568. With prefix ARG, go backward that many times the current span."
  20569. (interactive "p")
  20570. (org-agenda-later (- arg)))
  20571. (defun org-agenda-day-view ()
  20572. "Switch to daily view for agenda."
  20573. (interactive)
  20574. (setq org-agenda-ndays 1)
  20575. (org-agenda-change-time-span 'day))
  20576. (defun org-agenda-week-view ()
  20577. "Switch to daily view for agenda."
  20578. (interactive)
  20579. (setq org-agenda-ndays 7)
  20580. (org-agenda-change-time-span 'week))
  20581. (defun org-agenda-month-view ()
  20582. "Switch to daily view for agenda."
  20583. (interactive)
  20584. (org-agenda-change-time-span 'month))
  20585. (defun org-agenda-year-view ()
  20586. "Switch to daily view for agenda."
  20587. (interactive)
  20588. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20589. (org-agenda-change-time-span 'year)
  20590. (error "Abort")))
  20591. (defun org-agenda-change-time-span (span)
  20592. "Change the agenda view to SPAN.
  20593. SPAN may be `day', `week', `month', `year'."
  20594. (org-agenda-check-type t 'agenda)
  20595. (if (equal org-agenda-span span)
  20596. (error "Viewing span is already \"%s\"" span))
  20597. (let* ((sd (or (get-text-property (point) 'day)
  20598. org-starting-day))
  20599. (computed (org-agenda-compute-time-span sd span))
  20600. (org-agenda-overriding-arguments
  20601. (list (car org-agenda-last-arguments)
  20602. (car computed) (cdr computed) t)))
  20603. (org-agenda-redo)
  20604. (org-agenda-find-same-or-today-or-agenda))
  20605. (org-agenda-set-mode-name)
  20606. (message "Switched to %s view" span))
  20607. (defun org-agenda-compute-time-span (sd span)
  20608. "Compute starting date and number of days for agenda.
  20609. SPAN may be `day', `week', `month', `year'. The return value
  20610. is a cons cell with the starting date and the number of days,
  20611. so that the date SD will be in that range."
  20612. (let* ((greg (calendar-gregorian-from-absolute sd))
  20613. nd)
  20614. (cond
  20615. ((eq span 'day)
  20616. (setq nd 1))
  20617. ((eq span 'week)
  20618. (let* ((nt (calendar-day-of-week
  20619. (calendar-gregorian-from-absolute sd)))
  20620. (d (if org-agenda-start-on-weekday
  20621. (- nt org-agenda-start-on-weekday)
  20622. 0)))
  20623. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20624. (setq nd 7)))
  20625. ((eq span 'month)
  20626. (setq sd (calendar-absolute-from-gregorian
  20627. (list (car greg) 1 (nth 2 greg)))
  20628. nd (- (calendar-absolute-from-gregorian
  20629. (list (1+ (car greg)) 1 (nth 2 greg)))
  20630. sd)))
  20631. ((eq span 'year)
  20632. (setq sd (calendar-absolute-from-gregorian
  20633. (list 1 1 (nth 2 greg)))
  20634. nd (- (calendar-absolute-from-gregorian
  20635. (list 1 1 (1+ (nth 2 greg))))
  20636. sd))))
  20637. (cons sd nd)))
  20638. ;; FIXME: does not work if user makes date format that starts with a blank
  20639. (defun org-agenda-next-date-line (&optional arg)
  20640. "Jump to the next line indicating a date in agenda buffer."
  20641. (interactive "p")
  20642. (org-agenda-check-type t 'agenda 'timeline)
  20643. (beginning-of-line 1)
  20644. (if (looking-at "^\\S-") (forward-char 1))
  20645. (if (not (re-search-forward "^\\S-" nil t arg))
  20646. (progn
  20647. (backward-char 1)
  20648. (error "No next date after this line in this buffer")))
  20649. (goto-char (match-beginning 0)))
  20650. (defun org-agenda-previous-date-line (&optional arg)
  20651. "Jump to the previous line indicating a date in agenda buffer."
  20652. (interactive "p")
  20653. (org-agenda-check-type t 'agenda 'timeline)
  20654. (beginning-of-line 1)
  20655. (if (not (re-search-backward "^\\S-" nil t arg))
  20656. (error "No previous date before this line in this buffer")))
  20657. ;; Initialize the highlight
  20658. (defvar org-hl (org-make-overlay 1 1))
  20659. (org-overlay-put org-hl 'face 'highlight)
  20660. (defun org-highlight (begin end &optional buffer)
  20661. "Highlight a region with overlay."
  20662. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20663. org-hl begin end (or buffer (current-buffer))))
  20664. (defun org-unhighlight ()
  20665. "Detach overlay INDEX."
  20666. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20667. ;; FIXME this is currently not used.
  20668. (defun org-highlight-until-next-command (beg end &optional buffer)
  20669. (org-highlight beg end buffer)
  20670. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20671. (defun org-unhighlight-once ()
  20672. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20673. (org-unhighlight))
  20674. (defun org-agenda-follow-mode ()
  20675. "Toggle follow mode in an agenda buffer."
  20676. (interactive)
  20677. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20678. (org-agenda-set-mode-name)
  20679. (message "Follow mode is %s"
  20680. (if org-agenda-follow-mode "on" "off")))
  20681. (defun org-agenda-log-mode ()
  20682. "Toggle log mode in an agenda buffer."
  20683. (interactive)
  20684. (org-agenda-check-type t 'agenda 'timeline)
  20685. (setq org-agenda-show-log (not org-agenda-show-log))
  20686. (org-agenda-set-mode-name)
  20687. (org-agenda-redo)
  20688. (message "Log mode is %s"
  20689. (if org-agenda-show-log "on" "off")))
  20690. (defun org-agenda-toggle-diary ()
  20691. "Toggle diary inclusion in an agenda buffer."
  20692. (interactive)
  20693. (org-agenda-check-type t 'agenda)
  20694. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20695. (org-agenda-redo)
  20696. (org-agenda-set-mode-name)
  20697. (message "Diary inclusion turned %s"
  20698. (if org-agenda-include-diary "on" "off")))
  20699. (defun org-agenda-toggle-time-grid ()
  20700. "Toggle time grid in an agenda buffer."
  20701. (interactive)
  20702. (org-agenda-check-type t 'agenda)
  20703. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20704. (org-agenda-redo)
  20705. (org-agenda-set-mode-name)
  20706. (message "Time-grid turned %s"
  20707. (if org-agenda-use-time-grid "on" "off")))
  20708. (defun org-agenda-set-mode-name ()
  20709. "Set the mode name to indicate all the small mode settings."
  20710. (setq mode-name
  20711. (concat "Org-Agenda"
  20712. (if (equal org-agenda-ndays 1) " Day" "")
  20713. (if (equal org-agenda-ndays 7) " Week" "")
  20714. (if org-agenda-follow-mode " Follow" "")
  20715. (if org-agenda-include-diary " Diary" "")
  20716. (if org-agenda-use-time-grid " Grid" "")
  20717. (if org-agenda-show-log " Log" "")))
  20718. (force-mode-line-update))
  20719. (defun org-agenda-post-command-hook ()
  20720. (and (eolp) (not (bolp)) (backward-char 1))
  20721. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20722. (if (and org-agenda-follow-mode
  20723. (get-text-property (point) 'org-marker))
  20724. (org-agenda-show)))
  20725. (defun org-agenda-show-priority ()
  20726. "Show the priority of the current item.
  20727. This priority is composed of the main priority given with the [#A] cookies,
  20728. and by additional input from the age of a schedules or deadline entry."
  20729. (interactive)
  20730. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20731. (message "Priority is %d" (if pri pri -1000))))
  20732. (defun org-agenda-show-tags ()
  20733. "Show the tags applicable to the current item."
  20734. (interactive)
  20735. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20736. (if tags
  20737. (message "Tags are :%s:"
  20738. (org-no-properties (mapconcat 'identity tags ":")))
  20739. (message "No tags associated with this line"))))
  20740. (defun org-agenda-goto (&optional highlight)
  20741. "Go to the Org-mode file which contains the item at point."
  20742. (interactive)
  20743. (let* ((marker (or (get-text-property (point) 'org-marker)
  20744. (org-agenda-error)))
  20745. (buffer (marker-buffer marker))
  20746. (pos (marker-position marker)))
  20747. (switch-to-buffer-other-window buffer)
  20748. (widen)
  20749. (goto-char pos)
  20750. (when (org-mode-p)
  20751. (org-show-context 'agenda)
  20752. (save-excursion
  20753. (and (outline-next-heading)
  20754. (org-flag-heading nil)))) ; show the next heading
  20755. (recenter (/ (window-height) 2))
  20756. (run-hooks 'org-agenda-after-show-hook)
  20757. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20758. (defvar org-agenda-after-show-hook nil
  20759. "Normal hook run after an item has been shown from the agenda.
  20760. Point is in the buffer where the item originated.")
  20761. (defun org-agenda-kill ()
  20762. "Kill the entry or subtree belonging to the current agenda entry."
  20763. (interactive)
  20764. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20765. (let* ((marker (or (get-text-property (point) 'org-marker)
  20766. (org-agenda-error)))
  20767. (buffer (marker-buffer marker))
  20768. (pos (marker-position marker))
  20769. (type (get-text-property (point) 'type))
  20770. dbeg dend (n 0) conf)
  20771. (org-with-remote-undo buffer
  20772. (with-current-buffer buffer
  20773. (save-excursion
  20774. (goto-char pos)
  20775. (if (and (org-mode-p) (not (member type '("sexp"))))
  20776. (setq dbeg (progn (org-back-to-heading t) (point))
  20777. dend (org-end-of-subtree t t))
  20778. (setq dbeg (point-at-bol)
  20779. dend (min (point-max) (1+ (point-at-eol)))))
  20780. (goto-char dbeg)
  20781. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20782. (setq conf (or (eq t org-agenda-confirm-kill)
  20783. (and (numberp org-agenda-confirm-kill)
  20784. (> n org-agenda-confirm-kill))))
  20785. (and conf
  20786. (not (y-or-n-p
  20787. (format "Delete entry with %d lines in buffer \"%s\"? "
  20788. n (buffer-name buffer))))
  20789. (error "Abort"))
  20790. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20791. (with-current-buffer buffer (delete-region dbeg dend))
  20792. (message "Agenda item and source killed"))))
  20793. (defun org-agenda-archive ()
  20794. "Kill the entry or subtree belonging to the current agenda entry."
  20795. (interactive)
  20796. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20797. (let* ((marker (or (get-text-property (point) 'org-marker)
  20798. (org-agenda-error)))
  20799. (buffer (marker-buffer marker))
  20800. (pos (marker-position marker)))
  20801. (org-with-remote-undo buffer
  20802. (with-current-buffer buffer
  20803. (if (org-mode-p)
  20804. (save-excursion
  20805. (goto-char pos)
  20806. (org-remove-subtree-entries-from-agenda)
  20807. (org-back-to-heading t)
  20808. (org-archive-subtree))
  20809. (error "Archiving works only in Org-mode files"))))))
  20810. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20811. "Remove all lines in the agenda that correspond to a given subtree.
  20812. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20813. If this information is not given, the function uses the tree at point."
  20814. (let ((buf (or buf (current-buffer))) m p)
  20815. (save-excursion
  20816. (unless (and beg end)
  20817. (org-back-to-heading t)
  20818. (setq beg (point))
  20819. (org-end-of-subtree t)
  20820. (setq end (point)))
  20821. (set-buffer (get-buffer org-agenda-buffer-name))
  20822. (save-excursion
  20823. (goto-char (point-max))
  20824. (beginning-of-line 1)
  20825. (while (not (bobp))
  20826. (when (and (setq m (get-text-property (point) 'org-marker))
  20827. (equal buf (marker-buffer m))
  20828. (setq p (marker-position m))
  20829. (>= p beg)
  20830. (<= p end))
  20831. (let ((inhibit-read-only t))
  20832. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20833. (beginning-of-line 0))))))
  20834. (defun org-agenda-open-link ()
  20835. "Follow the link in the current line, if any."
  20836. (interactive)
  20837. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20838. (save-excursion
  20839. (save-restriction
  20840. (narrow-to-region (point-at-bol) (point-at-eol))
  20841. (org-open-at-point))))
  20842. (defun org-agenda-copy-local-variable (var)
  20843. "Get a variable from a referenced buffer and install it here."
  20844. (let ((m (get-text-property (point) 'org-marker)))
  20845. (when (and m (buffer-live-p (marker-buffer m)))
  20846. (org-set-local var (with-current-buffer (marker-buffer m)
  20847. (symbol-value var))))))
  20848. (defun org-agenda-switch-to (&optional delete-other-windows)
  20849. "Go to the Org-mode file which contains the item at point."
  20850. (interactive)
  20851. (let* ((marker (or (get-text-property (point) 'org-marker)
  20852. (org-agenda-error)))
  20853. (buffer (marker-buffer marker))
  20854. (pos (marker-position marker)))
  20855. (switch-to-buffer buffer)
  20856. (and delete-other-windows (delete-other-windows))
  20857. (widen)
  20858. (goto-char pos)
  20859. (when (org-mode-p)
  20860. (org-show-context 'agenda)
  20861. (save-excursion
  20862. (and (outline-next-heading)
  20863. (org-flag-heading nil)))))) ; show the next heading
  20864. (defun org-agenda-goto-mouse (ev)
  20865. "Go to the Org-mode file which contains the item at the mouse click."
  20866. (interactive "e")
  20867. (mouse-set-point ev)
  20868. (org-agenda-goto))
  20869. (defun org-agenda-show ()
  20870. "Display the Org-mode file which contains the item at point."
  20871. (interactive)
  20872. (let ((win (selected-window)))
  20873. (org-agenda-goto t)
  20874. (select-window win)))
  20875. (defun org-agenda-recenter (arg)
  20876. "Display the Org-mode file which contains the item at point and recenter."
  20877. (interactive "P")
  20878. (let ((win (selected-window)))
  20879. (org-agenda-goto t)
  20880. (recenter arg)
  20881. (select-window win)))
  20882. (defun org-agenda-show-mouse (ev)
  20883. "Display the Org-mode file which contains the item at the mouse click."
  20884. (interactive "e")
  20885. (mouse-set-point ev)
  20886. (org-agenda-show))
  20887. (defun org-agenda-check-no-diary ()
  20888. "Check if the entry is a diary link and abort if yes."
  20889. (if (get-text-property (point) 'org-agenda-diary-link)
  20890. (org-agenda-error)))
  20891. (defun org-agenda-error ()
  20892. (error "Command not allowed in this line"))
  20893. (defun org-agenda-tree-to-indirect-buffer ()
  20894. "Show the subtree corresponding to the current entry in an indirect buffer.
  20895. This calls the command `org-tree-to-indirect-buffer' from the original
  20896. Org-mode buffer.
  20897. With numerical prefix arg ARG, go up to this level and then take that tree.
  20898. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20899. dedicated frame)."
  20900. (interactive)
  20901. (org-agenda-check-no-diary)
  20902. (let* ((marker (or (get-text-property (point) 'org-marker)
  20903. (org-agenda-error)))
  20904. (buffer (marker-buffer marker))
  20905. (pos (marker-position marker)))
  20906. (with-current-buffer buffer
  20907. (save-excursion
  20908. (goto-char pos)
  20909. (call-interactively 'org-tree-to-indirect-buffer)))))
  20910. (defvar org-last-heading-marker (make-marker)
  20911. "Marker pointing to the headline that last changed its TODO state
  20912. by a remote command from the agenda.")
  20913. (defun org-agenda-todo-nextset ()
  20914. "Switch TODO entry to next sequence."
  20915. (interactive)
  20916. (org-agenda-todo 'nextset))
  20917. (defun org-agenda-todo-previousset ()
  20918. "Switch TODO entry to previous sequence."
  20919. (interactive)
  20920. (org-agenda-todo 'previousset))
  20921. (defun org-agenda-todo (&optional arg)
  20922. "Cycle TODO state of line at point, also in Org-mode file.
  20923. This changes the line at point, all other lines in the agenda referring to
  20924. the same tree node, and the headline of the tree node in the Org-mode file."
  20925. (interactive "P")
  20926. (org-agenda-check-no-diary)
  20927. (let* ((col (current-column))
  20928. (marker (or (get-text-property (point) 'org-marker)
  20929. (org-agenda-error)))
  20930. (buffer (marker-buffer marker))
  20931. (pos (marker-position marker))
  20932. (hdmarker (get-text-property (point) 'org-hd-marker))
  20933. (inhibit-read-only t)
  20934. newhead)
  20935. (org-with-remote-undo buffer
  20936. (with-current-buffer buffer
  20937. (widen)
  20938. (goto-char pos)
  20939. (org-show-context 'agenda)
  20940. (save-excursion
  20941. (and (outline-next-heading)
  20942. (org-flag-heading nil))) ; show the next heading
  20943. (org-todo arg)
  20944. (and (bolp) (forward-char 1))
  20945. (setq newhead (org-get-heading))
  20946. (save-excursion
  20947. (org-back-to-heading)
  20948. (move-marker org-last-heading-marker (point))))
  20949. (beginning-of-line 1)
  20950. (save-excursion
  20951. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20952. (move-to-column col))))
  20953. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20954. "Change all lines in the agenda buffer which match HDMARKER.
  20955. The new content of the line will be NEWHEAD (as modified by
  20956. `org-format-agenda-item'). HDMARKER is checked with
  20957. `equal' against all `org-hd-marker' text properties in the file.
  20958. If FIXFACE is non-nil, the face of each item is modified acording to
  20959. the new TODO state."
  20960. (let* ((inhibit-read-only t)
  20961. props m pl undone-face done-face finish new dotime cat tags)
  20962. (save-excursion
  20963. (goto-char (point-max))
  20964. (beginning-of-line 1)
  20965. (while (not finish)
  20966. (setq finish (bobp))
  20967. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20968. (equal m hdmarker))
  20969. (setq props (text-properties-at (point))
  20970. dotime (get-text-property (point) 'dotime)
  20971. cat (get-text-property (point) 'org-category)
  20972. tags (get-text-property (point) 'tags)
  20973. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20974. pl (get-text-property (point) 'prefix-length)
  20975. undone-face (get-text-property (point) 'undone-face)
  20976. done-face (get-text-property (point) 'done-face))
  20977. (move-to-column pl)
  20978. (cond
  20979. ((equal new "")
  20980. (beginning-of-line 1)
  20981. (and (looking-at ".*\n?") (replace-match "")))
  20982. ((looking-at ".*")
  20983. (replace-match new t t)
  20984. (beginning-of-line 1)
  20985. (add-text-properties (point-at-bol) (point-at-eol) props)
  20986. (when fixface
  20987. (add-text-properties
  20988. (point-at-bol) (point-at-eol)
  20989. (list 'face
  20990. (if org-last-todo-state-is-todo
  20991. undone-face done-face))))
  20992. (org-agenda-highlight-todo 'line)
  20993. (beginning-of-line 1))
  20994. (t (error "Line update did not work"))))
  20995. (beginning-of-line 0)))
  20996. (org-finalize-agenda)))
  20997. (defun org-agenda-align-tags (&optional line)
  20998. "Align all tags in agenda items to `org-agenda-tags-column'."
  20999. (let ((inhibit-read-only t) l c)
  21000. (save-excursion
  21001. (goto-char (if line (point-at-bol) (point-min)))
  21002. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21003. (if line (point-at-eol) nil) t)
  21004. (add-text-properties
  21005. (match-beginning 2) (match-end 2)
  21006. (list 'face (list 'org-tag (get-text-property
  21007. (match-beginning 2) 'face))))
  21008. (setq l (- (match-end 2) (match-beginning 2))
  21009. c (if (< org-agenda-tags-column 0)
  21010. (- (abs org-agenda-tags-column) l)
  21011. org-agenda-tags-column))
  21012. (delete-region (match-beginning 1) (match-end 1))
  21013. (goto-char (match-beginning 1))
  21014. (insert (org-add-props
  21015. (make-string (max 1 (- c (current-column))) ?\ )
  21016. (text-properties-at (point))))))))
  21017. (defun org-agenda-priority-up ()
  21018. "Increase the priority of line at point, also in Org-mode file."
  21019. (interactive)
  21020. (org-agenda-priority 'up))
  21021. (defun org-agenda-priority-down ()
  21022. "Decrease the priority of line at point, also in Org-mode file."
  21023. (interactive)
  21024. (org-agenda-priority 'down))
  21025. (defun org-agenda-priority (&optional force-direction)
  21026. "Set the priority of line at point, also in Org-mode file.
  21027. This changes the line at point, all other lines in the agenda referring to
  21028. the same tree node, and the headline of the tree node in the Org-mode file."
  21029. (interactive)
  21030. (org-agenda-check-no-diary)
  21031. (let* ((marker (or (get-text-property (point) 'org-marker)
  21032. (org-agenda-error)))
  21033. (hdmarker (get-text-property (point) 'org-hd-marker))
  21034. (buffer (marker-buffer hdmarker))
  21035. (pos (marker-position hdmarker))
  21036. (inhibit-read-only t)
  21037. newhead)
  21038. (org-with-remote-undo buffer
  21039. (with-current-buffer buffer
  21040. (widen)
  21041. (goto-char pos)
  21042. (org-show-context 'agenda)
  21043. (save-excursion
  21044. (and (outline-next-heading)
  21045. (org-flag-heading nil))) ; show the next heading
  21046. (funcall 'org-priority force-direction)
  21047. (end-of-line 1)
  21048. (setq newhead (org-get-heading)))
  21049. (org-agenda-change-all-lines newhead hdmarker)
  21050. (beginning-of-line 1))))
  21051. (defun org-get-tags-at (&optional pos)
  21052. "Get a list of all headline tags applicable at POS.
  21053. POS defaults to point. If tags are inherited, the list contains
  21054. the targets in the same sequence as the headlines appear, i.e.
  21055. the tags of the current headline come last."
  21056. (interactive)
  21057. (let (tags lastpos)
  21058. (save-excursion
  21059. (save-restriction
  21060. (widen)
  21061. (goto-char (or pos (point)))
  21062. (save-match-data
  21063. (org-back-to-heading t)
  21064. (condition-case nil
  21065. (while (not (equal lastpos (point)))
  21066. (setq lastpos (point))
  21067. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21068. (setq tags (append (org-split-string
  21069. (org-match-string-no-properties 1) ":")
  21070. tags)))
  21071. (or org-use-tag-inheritance (error ""))
  21072. (org-up-heading-all 1))
  21073. (error nil))))
  21074. tags)))
  21075. ;; FIXME: should fix the tags property of the agenda line.
  21076. (defun org-agenda-set-tags ()
  21077. "Set tags for the current headline."
  21078. (interactive)
  21079. (org-agenda-check-no-diary)
  21080. (if (and (org-region-active-p) (interactive-p))
  21081. (call-interactively 'org-change-tag-in-region)
  21082. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21083. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21084. (org-agenda-error)))
  21085. (buffer (marker-buffer hdmarker))
  21086. (pos (marker-position hdmarker))
  21087. (inhibit-read-only t)
  21088. newhead)
  21089. (org-with-remote-undo buffer
  21090. (with-current-buffer buffer
  21091. (widen)
  21092. (goto-char pos)
  21093. (save-excursion
  21094. (org-show-context 'agenda))
  21095. (save-excursion
  21096. (and (outline-next-heading)
  21097. (org-flag-heading nil))) ; show the next heading
  21098. (goto-char pos)
  21099. (call-interactively 'org-set-tags)
  21100. (end-of-line 1)
  21101. (setq newhead (org-get-heading)))
  21102. (org-agenda-change-all-lines newhead hdmarker)
  21103. (beginning-of-line 1)))))
  21104. (defun org-agenda-toggle-archive-tag ()
  21105. "Toggle the archive tag for the current entry."
  21106. (interactive)
  21107. (org-agenda-check-no-diary)
  21108. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21109. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21110. (org-agenda-error)))
  21111. (buffer (marker-buffer hdmarker))
  21112. (pos (marker-position hdmarker))
  21113. (inhibit-read-only t)
  21114. newhead)
  21115. (org-with-remote-undo buffer
  21116. (with-current-buffer buffer
  21117. (widen)
  21118. (goto-char pos)
  21119. (org-show-context 'agenda)
  21120. (save-excursion
  21121. (and (outline-next-heading)
  21122. (org-flag-heading nil))) ; show the next heading
  21123. (call-interactively 'org-toggle-archive-tag)
  21124. (end-of-line 1)
  21125. (setq newhead (org-get-heading)))
  21126. (org-agenda-change-all-lines newhead hdmarker)
  21127. (beginning-of-line 1))))
  21128. (defun org-agenda-date-later (arg &optional what)
  21129. "Change the date of this item to one day later."
  21130. (interactive "p")
  21131. (org-agenda-check-type t 'agenda 'timeline)
  21132. (org-agenda-check-no-diary)
  21133. (let* ((marker (or (get-text-property (point) 'org-marker)
  21134. (org-agenda-error)))
  21135. (buffer (marker-buffer marker))
  21136. (pos (marker-position marker)))
  21137. (org-with-remote-undo buffer
  21138. (with-current-buffer buffer
  21139. (widen)
  21140. (goto-char pos)
  21141. (if (not (org-at-timestamp-p))
  21142. (error "Cannot find time stamp"))
  21143. (org-timestamp-change arg (or what 'day)))
  21144. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21145. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21146. (defun org-agenda-date-earlier (arg &optional what)
  21147. "Change the date of this item to one day earlier."
  21148. (interactive "p")
  21149. (org-agenda-date-later (- arg) what))
  21150. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21151. "Show new date stamp via text properties."
  21152. ;; We use text properties to make this undoable
  21153. (let ((inhibit-read-only t))
  21154. (setq stamp (concat " " prefix " => " stamp))
  21155. (save-excursion
  21156. (goto-char (point-max))
  21157. (while (not (bobp))
  21158. (when (equal marker (get-text-property (point) 'org-marker))
  21159. (move-to-column (- (window-width) (length stamp)) t)
  21160. (if (featurep 'xemacs)
  21161. ;; Use `duplicable' property to trigger undo recording
  21162. (let ((ex (make-extent nil nil))
  21163. (gl (make-glyph stamp)))
  21164. (set-glyph-face gl 'secondary-selection)
  21165. (set-extent-properties
  21166. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21167. (insert-extent ex (1- (point)) (point-at-eol)))
  21168. (add-text-properties
  21169. (1- (point)) (point-at-eol)
  21170. (list 'display (org-add-props stamp nil
  21171. 'face 'secondary-selection))))
  21172. (beginning-of-line 1))
  21173. (beginning-of-line 0)))))
  21174. (defun org-agenda-date-prompt (arg)
  21175. "Change the date of this item. Date is prompted for, with default today.
  21176. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21177. be used to request time specification in the time stamp."
  21178. (interactive "P")
  21179. (org-agenda-check-type t 'agenda 'timeline)
  21180. (org-agenda-check-no-diary)
  21181. (let* ((marker (or (get-text-property (point) 'org-marker)
  21182. (org-agenda-error)))
  21183. (buffer (marker-buffer marker))
  21184. (pos (marker-position marker)))
  21185. (org-with-remote-undo buffer
  21186. (with-current-buffer buffer
  21187. (widen)
  21188. (goto-char pos)
  21189. (if (not (org-at-timestamp-p))
  21190. (error "Cannot find time stamp"))
  21191. (org-time-stamp arg)
  21192. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21193. (defun org-agenda-schedule (arg)
  21194. "Schedule the item at point."
  21195. (interactive "P")
  21196. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21197. (org-agenda-check-no-diary)
  21198. (let* ((marker (or (get-text-property (point) 'org-marker)
  21199. (org-agenda-error)))
  21200. (buffer (marker-buffer marker))
  21201. (pos (marker-position marker))
  21202. (org-insert-labeled-timestamps-at-point nil)
  21203. ts)
  21204. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21205. (set-marker-insertion-type marker t)
  21206. (org-with-remote-undo buffer
  21207. (with-current-buffer buffer
  21208. (widen)
  21209. (goto-char pos)
  21210. (setq ts (org-schedule arg)))
  21211. (org-agenda-show-new-time marker ts "S"))
  21212. (message "Item scheduled for %s" ts)))
  21213. (defun org-agenda-deadline (arg)
  21214. "Schedule the item at point."
  21215. (interactive "P")
  21216. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21217. (org-agenda-check-no-diary)
  21218. (let* ((marker (or (get-text-property (point) 'org-marker)
  21219. (org-agenda-error)))
  21220. (buffer (marker-buffer marker))
  21221. (pos (marker-position marker))
  21222. (org-insert-labeled-timestamps-at-point nil)
  21223. ts)
  21224. (org-with-remote-undo buffer
  21225. (with-current-buffer buffer
  21226. (widen)
  21227. (goto-char pos)
  21228. (setq ts (org-deadline arg)))
  21229. (org-agenda-show-new-time marker ts "S"))
  21230. (message "Deadline for this item set to %s" ts)))
  21231. (defun org-get-heading (&optional no-tags)
  21232. "Return the heading of the current entry, without the stars."
  21233. (save-excursion
  21234. (org-back-to-heading t)
  21235. (if (looking-at
  21236. (if no-tags
  21237. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21238. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21239. (match-string 1) "")))
  21240. (defun org-agenda-clock-in (&optional arg)
  21241. "Start the clock on the currently selected item."
  21242. (interactive "P")
  21243. (org-agenda-check-no-diary)
  21244. (let* ((marker (or (get-text-property (point) 'org-marker)
  21245. (org-agenda-error)))
  21246. (pos (marker-position marker)))
  21247. (org-with-remote-undo (marker-buffer marker)
  21248. (with-current-buffer (marker-buffer marker)
  21249. (widen)
  21250. (goto-char pos)
  21251. (org-clock-in)))))
  21252. (defun org-agenda-clock-out (&optional arg)
  21253. "Stop the currently running clock."
  21254. (interactive "P")
  21255. (unless (marker-buffer org-clock-marker)
  21256. (error "No running clock"))
  21257. (org-with-remote-undo (marker-buffer org-clock-marker)
  21258. (org-clock-out)))
  21259. (defun org-agenda-clock-cancel (&optional arg)
  21260. "Cancel the currently running clock."
  21261. (interactive "P")
  21262. (unless (marker-buffer org-clock-marker)
  21263. (error "No running clock"))
  21264. (org-with-remote-undo (marker-buffer org-clock-marker)
  21265. (org-clock-cancel)))
  21266. (defun org-agenda-diary-entry ()
  21267. "Make a diary entry, like the `i' command from the calendar.
  21268. All the standard commands work: block, weekly etc."
  21269. (interactive)
  21270. (org-agenda-check-type t 'agenda 'timeline)
  21271. (require 'diary-lib)
  21272. (let* ((char (progn
  21273. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21274. (read-char-exclusive)))
  21275. (cmd (cdr (assoc char
  21276. '((?d . insert-diary-entry)
  21277. (?w . insert-weekly-diary-entry)
  21278. (?m . insert-monthly-diary-entry)
  21279. (?y . insert-yearly-diary-entry)
  21280. (?a . insert-anniversary-diary-entry)
  21281. (?b . insert-block-diary-entry)
  21282. (?c . insert-cyclic-diary-entry)))))
  21283. (oldf (symbol-function 'calendar-cursor-to-date))
  21284. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21285. (point (point))
  21286. (mark (or (mark t) (point))))
  21287. (unless cmd
  21288. (error "No command associated with <%c>" char))
  21289. (unless (and (get-text-property point 'day)
  21290. (or (not (equal ?b char))
  21291. (get-text-property mark 'day)))
  21292. (error "Don't know which date to use for diary entry"))
  21293. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21294. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21295. (let ((calendar-mark-ring
  21296. (list (calendar-gregorian-from-absolute
  21297. (or (get-text-property mark 'day)
  21298. (get-text-property point 'day))))))
  21299. (unwind-protect
  21300. (progn
  21301. (fset 'calendar-cursor-to-date
  21302. (lambda (&optional error)
  21303. (calendar-gregorian-from-absolute
  21304. (get-text-property point 'day))))
  21305. (call-interactively cmd))
  21306. (fset 'calendar-cursor-to-date oldf)))))
  21307. (defun org-agenda-execute-calendar-command (cmd)
  21308. "Execute a calendar command from the agenda, with the date associated to
  21309. the cursor position."
  21310. (org-agenda-check-type t 'agenda 'timeline)
  21311. (require 'diary-lib)
  21312. (unless (get-text-property (point) 'day)
  21313. (error "Don't know which date to use for calendar command"))
  21314. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21315. (point (point))
  21316. (date (calendar-gregorian-from-absolute
  21317. (get-text-property point 'day)))
  21318. ;; the following 3 vars are needed in the calendar
  21319. (displayed-day (extract-calendar-day date))
  21320. (displayed-month (extract-calendar-month date))
  21321. (displayed-year (extract-calendar-year date)))
  21322. (unwind-protect
  21323. (progn
  21324. (fset 'calendar-cursor-to-date
  21325. (lambda (&optional error)
  21326. (calendar-gregorian-from-absolute
  21327. (get-text-property point 'day))))
  21328. (call-interactively cmd))
  21329. (fset 'calendar-cursor-to-date oldf))))
  21330. (defun org-agenda-phases-of-moon ()
  21331. "Display the phases of the moon for the 3 months around the cursor date."
  21332. (interactive)
  21333. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21334. (defun org-agenda-holidays ()
  21335. "Display the holidays for the 3 months around the cursor date."
  21336. (interactive)
  21337. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21338. (defun org-agenda-sunrise-sunset (arg)
  21339. "Display sunrise and sunset for the cursor date.
  21340. Latitude and longitude can be specified with the variables
  21341. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21342. argument, latitude and longitude will be prompted for."
  21343. (interactive "P")
  21344. (let ((calendar-longitude (if arg nil calendar-longitude))
  21345. (calendar-latitude (if arg nil calendar-latitude))
  21346. (calendar-location-name
  21347. (if arg "the given coordinates" calendar-location-name)))
  21348. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21349. (defun org-agenda-goto-calendar ()
  21350. "Open the Emacs calendar with the date at the cursor."
  21351. (interactive)
  21352. (org-agenda-check-type t 'agenda 'timeline)
  21353. (let* ((day (or (get-text-property (point) 'day)
  21354. (error "Don't know which date to open in calendar")))
  21355. (date (calendar-gregorian-from-absolute day))
  21356. (calendar-move-hook nil)
  21357. (view-calendar-holidays-initially nil)
  21358. (view-diary-entries-initially nil))
  21359. (calendar)
  21360. (calendar-goto-date date)))
  21361. (defun org-calendar-goto-agenda ()
  21362. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21363. This is a command that has to be installed in `calendar-mode-map'."
  21364. (interactive)
  21365. (org-agenda-list nil (calendar-absolute-from-gregorian
  21366. (calendar-cursor-to-date))
  21367. nil))
  21368. (defun org-agenda-convert-date ()
  21369. (interactive)
  21370. (org-agenda-check-type t 'agenda 'timeline)
  21371. (let ((day (get-text-property (point) 'day))
  21372. date s)
  21373. (unless day
  21374. (error "Don't know which date to convert"))
  21375. (setq date (calendar-gregorian-from-absolute day))
  21376. (setq s (concat
  21377. "Gregorian: " (calendar-date-string date) "\n"
  21378. "ISO: " (calendar-iso-date-string date) "\n"
  21379. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21380. "Julian: " (calendar-julian-date-string date) "\n"
  21381. "Astron. JD: " (calendar-astro-date-string date)
  21382. " (Julian date number at noon UTC)\n"
  21383. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21384. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21385. "French: " (calendar-french-date-string date) "\n"
  21386. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21387. "Mayan: " (calendar-mayan-date-string date) "\n"
  21388. "Coptic: " (calendar-coptic-date-string date) "\n"
  21389. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21390. "Persian: " (calendar-persian-date-string date) "\n"
  21391. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21392. (with-output-to-temp-buffer "*Dates*"
  21393. (princ s))
  21394. (if (fboundp 'fit-window-to-buffer)
  21395. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21396. ;;;; Embedded LaTeX
  21397. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21398. "Keymap for the minor `org-cdlatex-mode'.")
  21399. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21400. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21401. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21402. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21403. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21404. (defvar org-cdlatex-texmathp-advice-is-done nil
  21405. "Flag remembering if we have applied the advice to texmathp already.")
  21406. (define-minor-mode org-cdlatex-mode
  21407. "Toggle the minor `org-cdlatex-mode'.
  21408. This mode supports entering LaTeX environment and math in LaTeX fragments
  21409. in Org-mode.
  21410. \\{org-cdlatex-mode-map}"
  21411. nil " OCDL" nil
  21412. (when org-cdlatex-mode (require 'cdlatex))
  21413. (unless org-cdlatex-texmathp-advice-is-done
  21414. (setq org-cdlatex-texmathp-advice-is-done t)
  21415. (defadvice texmathp (around org-math-always-on activate)
  21416. "Always return t in org-mode buffers.
  21417. This is because we want to insert math symbols without dollars even outside
  21418. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21419. en embedded LaTeX fragement, let texmathp do its job.
  21420. \\[org-cdlatex-mode-map]"
  21421. (interactive)
  21422. (let (p)
  21423. (cond
  21424. ((not (org-mode-p)) ad-do-it)
  21425. ((eq this-command 'cdlatex-math-symbol)
  21426. (setq ad-return-value t
  21427. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21428. (t
  21429. (let ((p (org-inside-LaTeX-fragment-p)))
  21430. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21431. (setq ad-return-value t
  21432. texmathp-why '("Org-mode embedded math" . 0))
  21433. (if p ad-do-it)))))))))
  21434. (defun turn-on-org-cdlatex ()
  21435. "Unconditionally turn on `org-cdlatex-mode'."
  21436. (org-cdlatex-mode 1))
  21437. (defun org-inside-LaTeX-fragment-p ()
  21438. "Test if point is inside a LaTeX fragment.
  21439. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21440. sequence appearing also before point.
  21441. Even though the matchers for math are configurable, this function assumes
  21442. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21443. delimiters are skipped when they have been removed by customization.
  21444. The return value is nil, or a cons cell with the delimiter and
  21445. and the position of this delimiter.
  21446. This function does a reasonably good job, but can locally be fooled by
  21447. for example currency specifications. For example it will assume being in
  21448. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21449. fragments that are properly closed, but during editing, we have to live
  21450. with the uncertainty caused by missing closing delimiters. This function
  21451. looks only before point, not after."
  21452. (catch 'exit
  21453. (let ((pos (point))
  21454. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21455. (lim (progn
  21456. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21457. (point)))
  21458. dd-on str (start 0) m re)
  21459. (goto-char pos)
  21460. (when dodollar
  21461. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21462. re (nth 1 (assoc "$" org-latex-regexps)))
  21463. (while (string-match re str start)
  21464. (cond
  21465. ((= (match-end 0) (length str))
  21466. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21467. ((= (match-end 0) (- (length str) 5))
  21468. (throw 'exit nil))
  21469. (t (setq start (match-end 0))))))
  21470. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21471. (goto-char pos)
  21472. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21473. (and (match-beginning 2) (throw 'exit nil))
  21474. ;; count $$
  21475. (while (re-search-backward "\\$\\$" lim t)
  21476. (setq dd-on (not dd-on)))
  21477. (goto-char pos)
  21478. (if dd-on (cons "$$" m))))))
  21479. (defun org-try-cdlatex-tab ()
  21480. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21481. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21482. - inside a LaTeX fragment, or
  21483. - after the first word in a line, where an abbreviation expansion could
  21484. insert a LaTeX environment."
  21485. (when org-cdlatex-mode
  21486. (cond
  21487. ((save-excursion
  21488. (skip-chars-backward "a-zA-Z0-9*")
  21489. (skip-chars-backward " \t")
  21490. (bolp))
  21491. (cdlatex-tab) t)
  21492. ((org-inside-LaTeX-fragment-p)
  21493. (cdlatex-tab) t)
  21494. (t nil))))
  21495. (defun org-cdlatex-underscore-caret (&optional arg)
  21496. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21497. Revert to the normal definition outside of these fragments."
  21498. (interactive "P")
  21499. (if (org-inside-LaTeX-fragment-p)
  21500. (call-interactively 'cdlatex-sub-superscript)
  21501. (let (org-cdlatex-mode)
  21502. (call-interactively (key-binding (vector last-input-event))))))
  21503. (defun org-cdlatex-math-modify (&optional arg)
  21504. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21505. Revert to the normal definition outside of these fragments."
  21506. (interactive "P")
  21507. (if (org-inside-LaTeX-fragment-p)
  21508. (call-interactively 'cdlatex-math-modify)
  21509. (let (org-cdlatex-mode)
  21510. (call-interactively (key-binding (vector last-input-event))))))
  21511. (defvar org-latex-fragment-image-overlays nil
  21512. "List of overlays carrying the images of latex fragments.")
  21513. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21514. (defun org-remove-latex-fragment-image-overlays ()
  21515. "Remove all overlays with LaTeX fragment images in current buffer."
  21516. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21517. (setq org-latex-fragment-image-overlays nil))
  21518. (defun org-preview-latex-fragment (&optional subtree)
  21519. "Preview the LaTeX fragment at point, or all locally or globally.
  21520. If the cursor is in a LaTeX fragment, create the image and overlay
  21521. it over the source code. If there is no fragment at point, display
  21522. all fragments in the current text, from one headline to the next. With
  21523. prefix SUBTREE, display all fragments in the current subtree. With a
  21524. double prefix `C-u C-u', or when the cursor is before the first headline,
  21525. display all fragments in the buffer.
  21526. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21527. (interactive "P")
  21528. (org-remove-latex-fragment-image-overlays)
  21529. (save-excursion
  21530. (save-restriction
  21531. (let (beg end at msg)
  21532. (cond
  21533. ((or (equal subtree '(16))
  21534. (not (save-excursion
  21535. (re-search-backward (concat "^" outline-regexp) nil t))))
  21536. (setq beg (point-min) end (point-max)
  21537. msg "Creating images for buffer...%s"))
  21538. ((equal subtree '(4))
  21539. (org-back-to-heading)
  21540. (setq beg (point) end (org-end-of-subtree t)
  21541. msg "Creating images for subtree...%s"))
  21542. (t
  21543. (if (setq at (org-inside-LaTeX-fragment-p))
  21544. (goto-char (max (point-min) (- (cdr at) 2)))
  21545. (org-back-to-heading))
  21546. (setq beg (point) end (progn (outline-next-heading) (point))
  21547. msg (if at "Creating image...%s"
  21548. "Creating images for entry...%s"))))
  21549. (message msg "")
  21550. (narrow-to-region beg end)
  21551. (goto-char beg)
  21552. (org-format-latex
  21553. (concat "ltxpng/" (file-name-sans-extension
  21554. (file-name-nondirectory
  21555. buffer-file-name)))
  21556. default-directory 'overlays msg at 'forbuffer)
  21557. (message msg "done. Use `C-c C-c' to remove images.")))))
  21558. (defvar org-latex-regexps
  21559. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21560. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21561. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21562. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21563. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21564. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21565. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21566. "Regular expressions for matching embedded LaTeX.")
  21567. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21568. "Replace LaTeX fragments with links to an image, and produce images."
  21569. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21570. (let* ((prefixnodir (file-name-nondirectory prefix))
  21571. (absprefix (expand-file-name prefix dir))
  21572. (todir (file-name-directory absprefix))
  21573. (opt org-format-latex-options)
  21574. (matchers (plist-get opt :matchers))
  21575. (re-list org-latex-regexps)
  21576. (cnt 0) txt link beg end re e checkdir
  21577. m n block linkfile movefile ov)
  21578. ;; Check if there are old images files with this prefix, and remove them
  21579. (when (file-directory-p todir)
  21580. (mapc 'delete-file
  21581. (directory-files
  21582. todir 'full
  21583. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21584. ;; Check the different regular expressions
  21585. (while (setq e (pop re-list))
  21586. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21587. block (if (nth 3 e) "\n\n" ""))
  21588. (when (member m matchers)
  21589. (goto-char (point-min))
  21590. (while (re-search-forward re nil t)
  21591. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21592. (setq txt (match-string n)
  21593. beg (match-beginning n) end (match-end n)
  21594. cnt (1+ cnt)
  21595. linkfile (format "%s_%04d.png" prefix cnt)
  21596. movefile (format "%s_%04d.png" absprefix cnt)
  21597. link (concat block "[[file:" linkfile "]]" block))
  21598. (if msg (message msg cnt))
  21599. (goto-char beg)
  21600. (unless checkdir ; make sure the directory exists
  21601. (setq checkdir t)
  21602. (or (file-directory-p todir) (make-directory todir)))
  21603. (org-create-formula-image
  21604. txt movefile opt forbuffer)
  21605. (if overlays
  21606. (progn
  21607. (setq ov (org-make-overlay beg end))
  21608. (if (featurep 'xemacs)
  21609. (progn
  21610. (org-overlay-put ov 'invisible t)
  21611. (org-overlay-put
  21612. ov 'end-glyph
  21613. (make-glyph (vector 'png :file movefile))))
  21614. (org-overlay-put
  21615. ov 'display
  21616. (list 'image :type 'png :file movefile :ascent 'center)))
  21617. (push ov org-latex-fragment-image-overlays)
  21618. (goto-char end))
  21619. (delete-region beg end)
  21620. (insert link))))))))
  21621. ;; This function borrows from Ganesh Swami's latex2png.el
  21622. (defun org-create-formula-image (string tofile options buffer)
  21623. (let* ((tmpdir (if (featurep 'xemacs)
  21624. (temp-directory)
  21625. temporary-file-directory))
  21626. (texfilebase (make-temp-name
  21627. (expand-file-name "orgtex" tmpdir)))
  21628. (texfile (concat texfilebase ".tex"))
  21629. (dvifile (concat texfilebase ".dvi"))
  21630. (pngfile (concat texfilebase ".png"))
  21631. (fnh (face-attribute 'default :height nil))
  21632. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21633. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21634. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21635. "Black"))
  21636. (bg (or (plist-get options (if buffer :background :html-background))
  21637. "Transparent")))
  21638. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21639. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21640. (with-temp-file texfile
  21641. (insert org-format-latex-header
  21642. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21643. (let ((dir default-directory))
  21644. (condition-case nil
  21645. (progn
  21646. (cd tmpdir)
  21647. (call-process "latex" nil nil nil texfile))
  21648. (error nil))
  21649. (cd dir))
  21650. (if (not (file-exists-p dvifile))
  21651. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21652. (call-process "dvipng" nil nil nil
  21653. "-E" "-fg" fg "-bg" bg
  21654. "-D" dpi
  21655. ;;"-x" scale "-y" scale
  21656. "-T" "tight"
  21657. "-o" pngfile
  21658. dvifile)
  21659. (if (not (file-exists-p pngfile))
  21660. (progn (message "Failed to create png file from %s" texfile) nil)
  21661. ;; Use the requested file name and clean up
  21662. (copy-file pngfile tofile 'replace)
  21663. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21664. (delete-file (concat texfilebase e)))
  21665. pngfile))))
  21666. (defun org-dvipng-color (attr)
  21667. "Return an rgb color specification for dvipng."
  21668. (apply 'format "rgb %s %s %s"
  21669. (mapcar 'org-normalize-color
  21670. (color-values (face-attribute 'default attr nil)))))
  21671. (defun org-normalize-color (value)
  21672. "Return string to be used as color value for an RGB component."
  21673. (format "%g" (/ value 65535.0)))
  21674. ;;;; Exporting
  21675. ;;; Variables, constants, and parameter plists
  21676. (defconst org-level-max 20)
  21677. (defvar org-export-html-preamble nil
  21678. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21679. (defvar org-export-html-postamble nil
  21680. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21681. (defvar org-export-html-auto-preamble t
  21682. "Should default preamble be inserted? Set by publishing functions.")
  21683. (defvar org-export-html-auto-postamble t
  21684. "Should default postamble be inserted? Set by publishing functions.")
  21685. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21686. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21687. (defconst org-export-plist-vars
  21688. '((:language . org-export-default-language)
  21689. (:customtime . org-display-custom-times)
  21690. (:headline-levels . org-export-headline-levels)
  21691. (:section-numbers . org-export-with-section-numbers)
  21692. (:table-of-contents . org-export-with-toc)
  21693. (:preserve-breaks . org-export-preserve-breaks)
  21694. (:archived-trees . org-export-with-archived-trees)
  21695. (:emphasize . org-export-with-emphasize)
  21696. (:sub-superscript . org-export-with-sub-superscripts)
  21697. (:special-strings . org-export-with-special-strings)
  21698. (:footnotes . org-export-with-footnotes)
  21699. (:drawers . org-export-with-drawers)
  21700. (:tags . org-export-with-tags)
  21701. (:TeX-macros . org-export-with-TeX-macros)
  21702. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21703. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21704. (:fixed-width . org-export-with-fixed-width)
  21705. (:timestamps . org-export-with-timestamps)
  21706. (:author-info . org-export-author-info)
  21707. (:time-stamp-file . org-export-time-stamp-file)
  21708. (:tables . org-export-with-tables)
  21709. (:table-auto-headline . org-export-highlight-first-table-line)
  21710. (:style . org-export-html-style)
  21711. (:agenda-style . org-agenda-export-html-style)
  21712. (:convert-org-links . org-export-html-link-org-files-as-html)
  21713. (:inline-images . org-export-html-inline-images)
  21714. (:html-extension . org-export-html-extension)
  21715. (:html-table-tag . org-export-html-table-tag)
  21716. (:expand-quoted-html . org-export-html-expand)
  21717. (:timestamp . org-export-html-with-timestamp)
  21718. (:publishing-directory . org-export-publishing-directory)
  21719. (:preamble . org-export-html-preamble)
  21720. (:postamble . org-export-html-postamble)
  21721. (:auto-preamble . org-export-html-auto-preamble)
  21722. (:auto-postamble . org-export-html-auto-postamble)
  21723. (:author . user-full-name)
  21724. (:email . user-mail-address)))
  21725. (defun org-default-export-plist ()
  21726. "Return the property list with default settings for the export variables."
  21727. (let ((l org-export-plist-vars) rtn e)
  21728. (while (setq e (pop l))
  21729. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21730. rtn))
  21731. (defun org-infile-export-plist ()
  21732. "Return the property list with file-local settings for export."
  21733. (save-excursion
  21734. (save-restriction
  21735. (widen)
  21736. (goto-char 0)
  21737. (let ((re (org-make-options-regexp
  21738. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21739. p key val text options)
  21740. (while (re-search-forward re nil t)
  21741. (setq key (org-match-string-no-properties 1)
  21742. val (org-match-string-no-properties 2))
  21743. (cond
  21744. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21745. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21746. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21747. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21748. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21749. ((string-equal key "TEXT")
  21750. (setq text (if text (concat text "\n" val) val)))
  21751. ((string-equal key "OPTIONS") (setq options val))))
  21752. (setq p (plist-put p :text text))
  21753. (when options
  21754. (let ((op '(("H" . :headline-levels)
  21755. ("num" . :section-numbers)
  21756. ("toc" . :table-of-contents)
  21757. ("\\n" . :preserve-breaks)
  21758. ("@" . :expand-quoted-html)
  21759. (":" . :fixed-width)
  21760. ("|" . :tables)
  21761. ("^" . :sub-superscript)
  21762. ("-" . :special-strings)
  21763. ("f" . :footnotes)
  21764. ("d" . :drawers)
  21765. ("tags" . :tags)
  21766. ("*" . :emphasize)
  21767. ("TeX" . :TeX-macros)
  21768. ("LaTeX" . :LaTeX-fragments)
  21769. ("skip" . :skip-before-1st-heading)
  21770. ("author" . :author-info)
  21771. ("timestamp" . :time-stamp-file)))
  21772. o)
  21773. (while (setq o (pop op))
  21774. (if (string-match (concat (regexp-quote (car o))
  21775. ":\\([^ \t\n\r;,.]*\\)")
  21776. options)
  21777. (setq p (plist-put p (cdr o)
  21778. (car (read-from-string
  21779. (match-string 1 options)))))))))
  21780. p))))
  21781. (defun org-export-directory (type plist)
  21782. (let* ((val (plist-get plist :publishing-directory))
  21783. (dir (if (listp val)
  21784. (or (cdr (assoc type val)) ".")
  21785. val)))
  21786. dir))
  21787. (defun org-skip-comments (lines)
  21788. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21789. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21790. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21791. (case-fold-search nil)
  21792. rtn line level)
  21793. (while (setq line (pop lines))
  21794. (cond
  21795. ((and (string-match re1 line)
  21796. (setq level (- (match-end 1) (match-beginning 1))))
  21797. ;; Beginning of a COMMENT subtree. Skip it.
  21798. (while (and (setq line (pop lines))
  21799. (or (not (string-match re2 line))
  21800. (> (- (match-end 1) (match-beginning 1)) level))))
  21801. (setq lines (cons line lines)))
  21802. ((string-match "^#" line)
  21803. ;; an ordinary comment line
  21804. )
  21805. ((and org-export-table-remove-special-lines
  21806. (string-match "^[ \t]*|" line)
  21807. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21808. (and (string-match "| *<[0-9]+> *|" line)
  21809. (not (string-match "| *[^ <|]" line)))))
  21810. ;; a special table line that should be removed
  21811. )
  21812. (t (setq rtn (cons line rtn)))))
  21813. (nreverse rtn)))
  21814. (defun org-export (&optional arg)
  21815. (interactive)
  21816. (let ((help "[t] insert the export option template
  21817. \[v] limit export to visible part of outline tree
  21818. \[a] export as ASCII
  21819. \[h] export as HTML
  21820. \[H] export as HTML to temporary buffer
  21821. \[R] export region as HTML
  21822. \[b] export as HTML and browse immediately
  21823. \[x] export as XOXO
  21824. \[l] export as LaTeX
  21825. \[L] export as LaTeX to temporary buffer
  21826. \[i] export current file as iCalendar file
  21827. \[I] export all agenda files as iCalendar files
  21828. \[c] export agenda files into combined iCalendar file
  21829. \[F] publish current file
  21830. \[P] publish current project
  21831. \[X] publish... (project will be prompted for)
  21832. \[A] publish all projects")
  21833. (cmds
  21834. '((?t . org-insert-export-options-template)
  21835. (?v . org-export-visible)
  21836. (?a . org-export-as-ascii)
  21837. (?h . org-export-as-html)
  21838. (?b . org-export-as-html-and-open)
  21839. (?H . org-export-as-html-to-buffer)
  21840. (?R . org-export-region-as-html)
  21841. (?x . org-export-as-xoxo)
  21842. (?l . org-export-as-latex)
  21843. (?L . org-export-as-latex-to-buffer)
  21844. (?i . org-export-icalendar-this-file)
  21845. (?I . org-export-icalendar-all-agenda-files)
  21846. (?c . org-export-icalendar-combine-agenda-files)
  21847. (?F . org-publish-current-file)
  21848. (?P . org-publish-current-project)
  21849. (?X . org-publish)
  21850. (?A . org-publish-all)))
  21851. r1 r2 ass)
  21852. (save-window-excursion
  21853. (delete-other-windows)
  21854. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21855. (princ help))
  21856. (message "Select command: ")
  21857. (setq r1 (read-char-exclusive)))
  21858. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21859. (if (setq ass (assq r2 cmds))
  21860. (call-interactively (cdr ass))
  21861. (error "No command associated with key %c" r1))))
  21862. (defconst org-html-entities
  21863. '(("nbsp")
  21864. ("iexcl")
  21865. ("cent")
  21866. ("pound")
  21867. ("curren")
  21868. ("yen")
  21869. ("brvbar")
  21870. ("vert" . "&#124;")
  21871. ("sect")
  21872. ("uml")
  21873. ("copy")
  21874. ("ordf")
  21875. ("laquo")
  21876. ("not")
  21877. ("shy")
  21878. ("reg")
  21879. ("macr")
  21880. ("deg")
  21881. ("plusmn")
  21882. ("sup2")
  21883. ("sup3")
  21884. ("acute")
  21885. ("micro")
  21886. ("para")
  21887. ("middot")
  21888. ("odot"."o")
  21889. ("star"."*")
  21890. ("cedil")
  21891. ("sup1")
  21892. ("ordm")
  21893. ("raquo")
  21894. ("frac14")
  21895. ("frac12")
  21896. ("frac34")
  21897. ("iquest")
  21898. ("Agrave")
  21899. ("Aacute")
  21900. ("Acirc")
  21901. ("Atilde")
  21902. ("Auml")
  21903. ("Aring") ("AA"."&Aring;")
  21904. ("AElig")
  21905. ("Ccedil")
  21906. ("Egrave")
  21907. ("Eacute")
  21908. ("Ecirc")
  21909. ("Euml")
  21910. ("Igrave")
  21911. ("Iacute")
  21912. ("Icirc")
  21913. ("Iuml")
  21914. ("ETH")
  21915. ("Ntilde")
  21916. ("Ograve")
  21917. ("Oacute")
  21918. ("Ocirc")
  21919. ("Otilde")
  21920. ("Ouml")
  21921. ("times")
  21922. ("Oslash")
  21923. ("Ugrave")
  21924. ("Uacute")
  21925. ("Ucirc")
  21926. ("Uuml")
  21927. ("Yacute")
  21928. ("THORN")
  21929. ("szlig")
  21930. ("agrave")
  21931. ("aacute")
  21932. ("acirc")
  21933. ("atilde")
  21934. ("auml")
  21935. ("aring")
  21936. ("aelig")
  21937. ("ccedil")
  21938. ("egrave")
  21939. ("eacute")
  21940. ("ecirc")
  21941. ("euml")
  21942. ("igrave")
  21943. ("iacute")
  21944. ("icirc")
  21945. ("iuml")
  21946. ("eth")
  21947. ("ntilde")
  21948. ("ograve")
  21949. ("oacute")
  21950. ("ocirc")
  21951. ("otilde")
  21952. ("ouml")
  21953. ("divide")
  21954. ("oslash")
  21955. ("ugrave")
  21956. ("uacute")
  21957. ("ucirc")
  21958. ("uuml")
  21959. ("yacute")
  21960. ("thorn")
  21961. ("yuml")
  21962. ("fnof")
  21963. ("Alpha")
  21964. ("Beta")
  21965. ("Gamma")
  21966. ("Delta")
  21967. ("Epsilon")
  21968. ("Zeta")
  21969. ("Eta")
  21970. ("Theta")
  21971. ("Iota")
  21972. ("Kappa")
  21973. ("Lambda")
  21974. ("Mu")
  21975. ("Nu")
  21976. ("Xi")
  21977. ("Omicron")
  21978. ("Pi")
  21979. ("Rho")
  21980. ("Sigma")
  21981. ("Tau")
  21982. ("Upsilon")
  21983. ("Phi")
  21984. ("Chi")
  21985. ("Psi")
  21986. ("Omega")
  21987. ("alpha")
  21988. ("beta")
  21989. ("gamma")
  21990. ("delta")
  21991. ("epsilon")
  21992. ("varepsilon"."&epsilon;")
  21993. ("zeta")
  21994. ("eta")
  21995. ("theta")
  21996. ("iota")
  21997. ("kappa")
  21998. ("lambda")
  21999. ("mu")
  22000. ("nu")
  22001. ("xi")
  22002. ("omicron")
  22003. ("pi")
  22004. ("rho")
  22005. ("sigmaf") ("varsigma"."&sigmaf;")
  22006. ("sigma")
  22007. ("tau")
  22008. ("upsilon")
  22009. ("phi")
  22010. ("chi")
  22011. ("psi")
  22012. ("omega")
  22013. ("thetasym") ("vartheta"."&thetasym;")
  22014. ("upsih")
  22015. ("piv")
  22016. ("bull") ("bullet"."&bull;")
  22017. ("hellip") ("dots"."&hellip;")
  22018. ("prime")
  22019. ("Prime")
  22020. ("oline")
  22021. ("frasl")
  22022. ("weierp")
  22023. ("image")
  22024. ("real")
  22025. ("trade")
  22026. ("alefsym")
  22027. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22028. ("uarr") ("uparrow"."&uarr;")
  22029. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22030. ("darr")("downarrow"."&darr;")
  22031. ("harr") ("leftrightarrow"."&harr;")
  22032. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22033. ("lArr") ("Leftarrow"."&lArr;")
  22034. ("uArr") ("Uparrow"."&uArr;")
  22035. ("rArr") ("Rightarrow"."&rArr;")
  22036. ("dArr") ("Downarrow"."&dArr;")
  22037. ("hArr") ("Leftrightarrow"."&hArr;")
  22038. ("forall")
  22039. ("part") ("partial"."&part;")
  22040. ("exist") ("exists"."&exist;")
  22041. ("empty") ("emptyset"."&empty;")
  22042. ("nabla")
  22043. ("isin") ("in"."&isin;")
  22044. ("notin")
  22045. ("ni")
  22046. ("prod")
  22047. ("sum")
  22048. ("minus")
  22049. ("lowast") ("ast"."&lowast;")
  22050. ("radic")
  22051. ("prop") ("proptp"."&prop;")
  22052. ("infin") ("infty"."&infin;")
  22053. ("ang") ("angle"."&ang;")
  22054. ("and") ("wedge"."&and;")
  22055. ("or") ("vee"."&or;")
  22056. ("cap")
  22057. ("cup")
  22058. ("int")
  22059. ("there4")
  22060. ("sim")
  22061. ("cong") ("simeq"."&cong;")
  22062. ("asymp")("approx"."&asymp;")
  22063. ("ne") ("neq"."&ne;")
  22064. ("equiv")
  22065. ("le")
  22066. ("ge")
  22067. ("sub") ("subset"."&sub;")
  22068. ("sup") ("supset"."&sup;")
  22069. ("nsub")
  22070. ("sube")
  22071. ("supe")
  22072. ("oplus")
  22073. ("otimes")
  22074. ("perp")
  22075. ("sdot") ("cdot"."&sdot;")
  22076. ("lceil")
  22077. ("rceil")
  22078. ("lfloor")
  22079. ("rfloor")
  22080. ("lang")
  22081. ("rang")
  22082. ("loz") ("Diamond"."&loz;")
  22083. ("spades") ("spadesuit"."&spades;")
  22084. ("clubs") ("clubsuit"."&clubs;")
  22085. ("hearts") ("diamondsuit"."&hearts;")
  22086. ("diams") ("diamondsuit"."&diams;")
  22087. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22088. ("quot")
  22089. ("amp")
  22090. ("lt")
  22091. ("gt")
  22092. ("OElig")
  22093. ("oelig")
  22094. ("Scaron")
  22095. ("scaron")
  22096. ("Yuml")
  22097. ("circ")
  22098. ("tilde")
  22099. ("ensp")
  22100. ("emsp")
  22101. ("thinsp")
  22102. ("zwnj")
  22103. ("zwj")
  22104. ("lrm")
  22105. ("rlm")
  22106. ("ndash")
  22107. ("mdash")
  22108. ("lsquo")
  22109. ("rsquo")
  22110. ("sbquo")
  22111. ("ldquo")
  22112. ("rdquo")
  22113. ("bdquo")
  22114. ("dagger")
  22115. ("Dagger")
  22116. ("permil")
  22117. ("lsaquo")
  22118. ("rsaquo")
  22119. ("euro")
  22120. ("arccos"."arccos")
  22121. ("arcsin"."arcsin")
  22122. ("arctan"."arctan")
  22123. ("arg"."arg")
  22124. ("cos"."cos")
  22125. ("cosh"."cosh")
  22126. ("cot"."cot")
  22127. ("coth"."coth")
  22128. ("csc"."csc")
  22129. ("deg"."deg")
  22130. ("det"."det")
  22131. ("dim"."dim")
  22132. ("exp"."exp")
  22133. ("gcd"."gcd")
  22134. ("hom"."hom")
  22135. ("inf"."inf")
  22136. ("ker"."ker")
  22137. ("lg"."lg")
  22138. ("lim"."lim")
  22139. ("liminf"."liminf")
  22140. ("limsup"."limsup")
  22141. ("ln"."ln")
  22142. ("log"."log")
  22143. ("max"."max")
  22144. ("min"."min")
  22145. ("Pr"."Pr")
  22146. ("sec"."sec")
  22147. ("sin"."sin")
  22148. ("sinh"."sinh")
  22149. ("sup"."sup")
  22150. ("tan"."tan")
  22151. ("tanh"."tanh")
  22152. )
  22153. "Entities for TeX->HTML translation.
  22154. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22155. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22156. In that case, \"\\ent\" will be translated to \"&other;\".
  22157. The list contains HTML entities for Latin-1, Greek and other symbols.
  22158. It is supplemented by a number of commonly used TeX macros with appropriate
  22159. translations. There is currently no way for users to extend this.")
  22160. ;;; General functions for all backends
  22161. (defun org-cleaned-string-for-export (string &rest parameters)
  22162. "Cleanup a buffer STRING so that links can be created safely."
  22163. (interactive)
  22164. (let* ((re-radio (and org-target-link-regexp
  22165. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22166. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22167. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22168. (re-archive (concat ":" org-archive-tag ":"))
  22169. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22170. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22171. (htmlp (plist-get parameters :for-html))
  22172. (asciip (plist-get parameters :for-ascii))
  22173. (latexp (plist-get parameters :for-LaTeX))
  22174. (commentsp (plist-get parameters :comments))
  22175. (archived-trees (plist-get parameters :archived-trees))
  22176. (inhibit-read-only t)
  22177. (drawers org-drawers)
  22178. (exp-drawers (plist-get parameters :drawers))
  22179. (outline-regexp "\\*+ ")
  22180. a b xx
  22181. rtn p)
  22182. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22183. (erase-buffer)
  22184. (insert string)
  22185. ;; Remove license-to-kill stuff
  22186. (while (setq p (text-property-any (point-min) (point-max)
  22187. :org-license-to-kill t))
  22188. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22189. (let ((org-inhibit-startup t)) (org-mode))
  22190. (untabify (point-min) (point-max))
  22191. ;; Get rid of drawers
  22192. (unless (eq t exp-drawers)
  22193. (goto-char (point-min))
  22194. (let ((re (concat "^[ \t]*:\\("
  22195. (mapconcat
  22196. 'identity
  22197. (org-delete-all exp-drawers
  22198. (copy-sequence drawers))
  22199. "\\|")
  22200. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22201. (while (re-search-forward re nil t)
  22202. (replace-match ""))))
  22203. ;; Get the correct stuff before the first headline
  22204. (when (plist-get parameters :skip-before-1st-heading)
  22205. (goto-char (point-min))
  22206. (when (re-search-forward "^\\*+[ \t]" nil t)
  22207. (delete-region (point-min) (match-beginning 0))
  22208. (goto-char (point-min))
  22209. (insert "\n")))
  22210. (when (plist-get parameters :add-text)
  22211. (goto-char (point-min))
  22212. (insert (plist-get parameters :add-text) "\n"))
  22213. ;; Get rid of archived trees
  22214. (when (not (eq archived-trees t))
  22215. (goto-char (point-min))
  22216. (while (re-search-forward re-archive nil t)
  22217. (if (not (org-on-heading-p t))
  22218. (org-end-of-subtree t)
  22219. (beginning-of-line 1)
  22220. (setq a (if archived-trees
  22221. (1+ (point-at-eol)) (point))
  22222. b (org-end-of-subtree t))
  22223. (if (> b a) (delete-region a b)))))
  22224. ;; Find targets in comments and move them out of comments,
  22225. ;; but mark them as targets that should be invisible
  22226. (goto-char (point-min))
  22227. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22228. (replace-match "\\1(INVISIBLE)"))
  22229. ;; Protect backend specific stuff, throw away the others.
  22230. (let ((formatters
  22231. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22232. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22233. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22234. fmt)
  22235. (goto-char (point-min))
  22236. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22237. (goto-char (match-end 0))
  22238. (while (not (looking-at "#\\+END_EXAMPLE"))
  22239. (insert ": ")
  22240. (beginning-of-line 2)))
  22241. (goto-char (point-min))
  22242. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22243. (add-text-properties (match-beginning 0) (match-end 0)
  22244. '(org-protected t)))
  22245. (while formatters
  22246. (setq fmt (pop formatters))
  22247. (when (car fmt)
  22248. (goto-char (point-min))
  22249. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22250. ":[ \t]*\\(.*\\)") nil t)
  22251. (replace-match "\\1" t)
  22252. (add-text-properties
  22253. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22254. '(org-protected t))))
  22255. (goto-char (point-min))
  22256. (while (re-search-forward
  22257. (concat "^#\\+"
  22258. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22259. (cadddr fmt) "\\>.*\n?") nil t)
  22260. (if (car fmt)
  22261. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22262. '(org-protected t))
  22263. (delete-region (match-beginning 0) (match-end 0))))))
  22264. ;; Protect quoted subtrees
  22265. (goto-char (point-min))
  22266. (while (re-search-forward re-quote nil t)
  22267. (goto-char (match-beginning 0))
  22268. (end-of-line 1)
  22269. (add-text-properties (point) (org-end-of-subtree t)
  22270. '(org-protected t)))
  22271. ;; Protect verbatim elements
  22272. (goto-char (point-min))
  22273. (while (re-search-forward org-verbatim-re nil t)
  22274. (add-text-properties (match-beginning 4) (match-end 4)
  22275. '(org-protected t))
  22276. (goto-char (1+ (match-end 4))))
  22277. ;; Remove subtrees that are commented
  22278. (goto-char (point-min))
  22279. (while (re-search-forward re-commented nil t)
  22280. (goto-char (match-beginning 0))
  22281. (delete-region (point) (org-end-of-subtree t)))
  22282. ;; Remove special table lines
  22283. (when org-export-table-remove-special-lines
  22284. (goto-char (point-min))
  22285. (while (re-search-forward "^[ \t]*|" nil t)
  22286. (beginning-of-line 1)
  22287. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22288. (and (looking-at ".*?| *<[0-9]+> *|")
  22289. (not (looking-at ".*?| *[^ <|]"))))
  22290. (delete-region (max (point-min) (1- (point-at-bol)))
  22291. (point-at-eol))
  22292. (end-of-line 1))))
  22293. ;; Specific LaTeX stuff
  22294. (when latexp
  22295. (require 'org-export-latex nil)
  22296. (org-export-latex-cleaned-string))
  22297. (when asciip
  22298. (org-export-ascii-clean-string))
  22299. ;; Specific HTML stuff
  22300. (when htmlp
  22301. ;; Convert LaTeX fragments to images
  22302. (when (plist-get parameters :LaTeX-fragments)
  22303. (org-format-latex
  22304. (concat "ltxpng/" (file-name-sans-extension
  22305. (file-name-nondirectory
  22306. org-current-export-file)))
  22307. org-current-export-dir nil "Creating LaTeX image %s"))
  22308. (message "Exporting..."))
  22309. ;; Remove or replace comments
  22310. (goto-char (point-min))
  22311. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22312. (if commentsp
  22313. (progn (add-text-properties
  22314. (match-beginning 0) (match-end 0) '(org-protected t))
  22315. (replace-match (format commentsp (match-string 1)) t t))
  22316. (replace-match "")))
  22317. ;; Find matches for radio targets and turn them into internal links
  22318. (goto-char (point-min))
  22319. (when re-radio
  22320. (while (re-search-forward re-radio nil t)
  22321. (org-if-unprotected
  22322. (replace-match "\\1[[\\2]]"))))
  22323. ;; Find all links that contain a newline and put them into a single line
  22324. (goto-char (point-min))
  22325. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22326. (org-if-unprotected
  22327. (replace-match "\\1 \\3")
  22328. (goto-char (match-beginning 0))))
  22329. ;; Normalize links: Convert angle and plain links into bracket links
  22330. ;; Expand link abbreviations
  22331. (goto-char (point-min))
  22332. (while (re-search-forward re-plain-link nil t)
  22333. (goto-char (1- (match-end 0)))
  22334. (org-if-unprotected
  22335. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22336. ":" (match-string 3) "]]")))
  22337. ;; added 'org-link face to links
  22338. (put-text-property 0 (length s) 'face 'org-link s)
  22339. (replace-match s t t))))
  22340. (goto-char (point-min))
  22341. (while (re-search-forward re-angle-link nil t)
  22342. (goto-char (1- (match-end 0)))
  22343. (org-if-unprotected
  22344. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22345. ":" (match-string 3) "]]")))
  22346. (put-text-property 0 (length s) 'face 'org-link s)
  22347. (replace-match s t t))))
  22348. (goto-char (point-min))
  22349. (while (re-search-forward org-bracket-link-regexp nil t)
  22350. (org-if-unprotected
  22351. (let* ((s (concat "[[" (setq xx (save-match-data
  22352. (org-link-expand-abbrev (match-string 1))))
  22353. "]"
  22354. (if (match-end 3)
  22355. (match-string 2)
  22356. (concat "[" xx "]"))
  22357. "]")))
  22358. (put-text-property 0 (length s) 'face 'org-link s)
  22359. (replace-match s t t))))
  22360. ;; Find multiline emphasis and put them into single line
  22361. (when (plist-get parameters :emph-multiline)
  22362. (goto-char (point-min))
  22363. (while (re-search-forward org-emph-re nil t)
  22364. (if (not (= (char-after (match-beginning 3))
  22365. (char-after (match-beginning 4))))
  22366. (org-if-unprotected
  22367. (subst-char-in-region (match-beginning 0) (match-end 0)
  22368. ?\n ?\ t)
  22369. (goto-char (1- (match-end 0))))
  22370. (goto-char (1+ (match-beginning 0))))))
  22371. (setq rtn (buffer-string)))
  22372. (kill-buffer " org-mode-tmp")
  22373. rtn))
  22374. (defun org-export-grab-title-from-buffer ()
  22375. "Get a title for the current document, from looking at the buffer."
  22376. (let ((inhibit-read-only t))
  22377. (save-excursion
  22378. (goto-char (point-min))
  22379. (let ((end (save-excursion (outline-next-heading) (point))))
  22380. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22381. ;; Mark the line so that it will not be exported as normal text.
  22382. (org-unmodified
  22383. (add-text-properties (match-beginning 0) (match-end 0)
  22384. (list :org-license-to-kill t)))
  22385. ;; Return the title string
  22386. (org-trim (match-string 0)))))))
  22387. (defun org-export-get-title-from-subtree ()
  22388. "Return subtree title and exclude it from export."
  22389. (let (title (m (mark)))
  22390. (save-excursion
  22391. (goto-char (region-beginning))
  22392. (when (and (org-at-heading-p)
  22393. (>= (org-end-of-subtree t t) (region-end)))
  22394. ;; This is a subtree, we take the title from the first heading
  22395. (goto-char (region-beginning))
  22396. (looking-at org-todo-line-regexp)
  22397. (setq title (match-string 3))
  22398. (org-unmodified
  22399. (add-text-properties (point) (1+ (point-at-eol))
  22400. (list :org-license-to-kill t)))))
  22401. title))
  22402. (defun org-solidify-link-text (s &optional alist)
  22403. "Take link text and make a safe target out of it."
  22404. (save-match-data
  22405. (let* ((rtn
  22406. (mapconcat
  22407. 'identity
  22408. (org-split-string s "[ \t\r\n]+") "--"))
  22409. (a (assoc rtn alist)))
  22410. (or (cdr a) rtn))))
  22411. (defun org-get-min-level (lines)
  22412. "Get the minimum level in LINES."
  22413. (let ((re "^\\(\\*+\\) ") l min)
  22414. (catch 'exit
  22415. (while (setq l (pop lines))
  22416. (if (string-match re l)
  22417. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22418. 1)))
  22419. ;; Variable holding the vector with section numbers
  22420. (defvar org-section-numbers (make-vector org-level-max 0))
  22421. (defun org-init-section-numbers ()
  22422. "Initialize the vector for the section numbers."
  22423. (let* ((level -1)
  22424. (numbers (nreverse (org-split-string "" "\\.")))
  22425. (depth (1- (length org-section-numbers)))
  22426. (i depth) number-string)
  22427. (while (>= i 0)
  22428. (if (> i level)
  22429. (aset org-section-numbers i 0)
  22430. (setq number-string (or (car numbers) "0"))
  22431. (if (string-match "\\`[A-Z]\\'" number-string)
  22432. (aset org-section-numbers i
  22433. (- (string-to-char number-string) ?A -1))
  22434. (aset org-section-numbers i (string-to-number number-string)))
  22435. (pop numbers))
  22436. (setq i (1- i)))))
  22437. (defun org-section-number (&optional level)
  22438. "Return a string with the current section number.
  22439. When LEVEL is non-nil, increase section numbers on that level."
  22440. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22441. (when level
  22442. (when (> level -1)
  22443. (aset org-section-numbers
  22444. level (1+ (aref org-section-numbers level))))
  22445. (setq idx (1+ level))
  22446. (while (<= idx depth)
  22447. (if (not (= idx 1))
  22448. (aset org-section-numbers idx 0))
  22449. (setq idx (1+ idx))))
  22450. (setq idx 0)
  22451. (while (<= idx depth)
  22452. (setq n (aref org-section-numbers idx))
  22453. (setq string (concat string (if (not (string= string "")) "." "")
  22454. (int-to-string n)))
  22455. (setq idx (1+ idx)))
  22456. (save-match-data
  22457. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22458. (setq string (replace-match "" t nil string)))
  22459. (if (string-match "\\(\\.0\\)+\\'" string)
  22460. (setq string (replace-match "" t nil string))))
  22461. string))
  22462. ;;; ASCII export
  22463. (defvar org-last-level nil) ; dynamically scoped variable
  22464. (defvar org-min-level nil) ; dynamically scoped variable
  22465. (defvar org-levels-open nil) ; dynamically scoped parameter
  22466. (defvar org-ascii-current-indentation nil) ; For communication
  22467. (defun org-export-as-ascii (arg)
  22468. "Export the outline as a pretty ASCII file.
  22469. If there is an active region, export only the region.
  22470. The prefix ARG specifies how many levels of the outline should become
  22471. underlined headlines. The default is 3."
  22472. (interactive "P")
  22473. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22474. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22475. (org-infile-export-plist)))
  22476. (region-p (org-region-active-p))
  22477. (subtree-p
  22478. (when region-p
  22479. (save-excursion
  22480. (goto-char (region-beginning))
  22481. (and (org-at-heading-p)
  22482. (>= (org-end-of-subtree t t) (region-end))))))
  22483. (custom-times org-display-custom-times)
  22484. (org-ascii-current-indentation '(0 . 0))
  22485. (level 0) line txt
  22486. (umax nil)
  22487. (umax-toc nil)
  22488. (case-fold-search nil)
  22489. (filename (concat (file-name-as-directory
  22490. (org-export-directory :ascii opt-plist))
  22491. (file-name-sans-extension
  22492. (or (and subtree-p
  22493. (org-entry-get (region-beginning)
  22494. "EXPORT_FILE_NAME" t))
  22495. (file-name-nondirectory buffer-file-name)))
  22496. ".txt"))
  22497. (filename (if (equal (file-truename filename)
  22498. (file-truename buffer-file-name))
  22499. (concat filename ".txt")
  22500. filename))
  22501. (buffer (find-file-noselect filename))
  22502. (org-levels-open (make-vector org-level-max nil))
  22503. (odd org-odd-levels-only)
  22504. (date (plist-get opt-plist :date))
  22505. (author (plist-get opt-plist :author))
  22506. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22507. (plist-get opt-plist :title)
  22508. (and (not
  22509. (plist-get opt-plist :skip-before-1st-heading))
  22510. (org-export-grab-title-from-buffer))
  22511. (file-name-sans-extension
  22512. (file-name-nondirectory buffer-file-name))))
  22513. (email (plist-get opt-plist :email))
  22514. (language (plist-get opt-plist :language))
  22515. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22516. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22517. (todo nil)
  22518. (lang-words nil)
  22519. (region
  22520. (buffer-substring
  22521. (if (org-region-active-p) (region-beginning) (point-min))
  22522. (if (org-region-active-p) (region-end) (point-max))))
  22523. (lines (org-split-string
  22524. (org-cleaned-string-for-export
  22525. region
  22526. :for-ascii t
  22527. :skip-before-1st-heading
  22528. (plist-get opt-plist :skip-before-1st-heading)
  22529. :drawers (plist-get opt-plist :drawers)
  22530. :verbatim-multiline t
  22531. :archived-trees
  22532. (plist-get opt-plist :archived-trees)
  22533. :add-text (plist-get opt-plist :text))
  22534. "\n"))
  22535. thetoc have-headings first-heading-pos
  22536. table-open table-buffer)
  22537. (let ((inhibit-read-only t))
  22538. (org-unmodified
  22539. (remove-text-properties (point-min) (point-max)
  22540. '(:org-license-to-kill t))))
  22541. (setq org-min-level (org-get-min-level lines))
  22542. (setq org-last-level org-min-level)
  22543. (org-init-section-numbers)
  22544. (find-file-noselect filename)
  22545. (setq lang-words (or (assoc language org-export-language-setup)
  22546. (assoc "en" org-export-language-setup)))
  22547. (switch-to-buffer-other-window buffer)
  22548. (erase-buffer)
  22549. (fundamental-mode)
  22550. ;; create local variables for all options, to make sure all called
  22551. ;; functions get the correct information
  22552. (mapc (lambda (x)
  22553. (set (make-local-variable (cdr x))
  22554. (plist-get opt-plist (car x))))
  22555. org-export-plist-vars)
  22556. (org-set-local 'org-odd-levels-only odd)
  22557. (setq umax (if arg (prefix-numeric-value arg)
  22558. org-export-headline-levels))
  22559. (setq umax-toc (if (integerp org-export-with-toc)
  22560. (min org-export-with-toc umax)
  22561. umax))
  22562. ;; File header
  22563. (if title (org-insert-centered title ?=))
  22564. (insert "\n")
  22565. (if (and (or author email)
  22566. org-export-author-info)
  22567. (insert (concat (nth 1 lang-words) ": " (or author "")
  22568. (if email (concat " <" email ">") "")
  22569. "\n")))
  22570. (cond
  22571. ((and date (string-match "%" date))
  22572. (setq date (format-time-string date (current-time))))
  22573. (date)
  22574. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22575. (if (and date org-export-time-stamp-file)
  22576. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22577. (insert "\n\n")
  22578. (if org-export-with-toc
  22579. (progn
  22580. (push (concat (nth 3 lang-words) "\n") thetoc)
  22581. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22582. (mapc '(lambda (line)
  22583. (if (string-match org-todo-line-regexp
  22584. line)
  22585. ;; This is a headline
  22586. (progn
  22587. (setq have-headings t)
  22588. (setq level (- (match-end 1) (match-beginning 1))
  22589. level (org-tr-level level)
  22590. txt (match-string 3 line)
  22591. todo
  22592. (or (and org-export-mark-todo-in-toc
  22593. (match-beginning 2)
  22594. (not (member (match-string 2 line)
  22595. org-done-keywords)))
  22596. ; TODO, not DONE
  22597. (and org-export-mark-todo-in-toc
  22598. (= level umax-toc)
  22599. (org-search-todo-below
  22600. line lines level))))
  22601. (setq txt (org-html-expand-for-ascii txt))
  22602. (while (string-match org-bracket-link-regexp txt)
  22603. (setq txt
  22604. (replace-match
  22605. (match-string (if (match-end 2) 3 1) txt)
  22606. t t txt)))
  22607. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22608. (string-match
  22609. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22610. txt))
  22611. (setq txt (replace-match "" t t txt)))
  22612. (if (string-match quote-re0 txt)
  22613. (setq txt (replace-match "" t t txt)))
  22614. (if org-export-with-section-numbers
  22615. (setq txt (concat (org-section-number level)
  22616. " " txt)))
  22617. (if (<= level umax-toc)
  22618. (progn
  22619. (push
  22620. (concat
  22621. (make-string
  22622. (* (max 0 (- level org-min-level)) 4) ?\ )
  22623. (format (if todo "%s (*)\n" "%s\n") txt))
  22624. thetoc)
  22625. (setq org-last-level level))
  22626. ))))
  22627. lines)
  22628. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22629. (org-init-section-numbers)
  22630. (while (setq line (pop lines))
  22631. ;; Remove the quoted HTML tags.
  22632. (setq line (org-html-expand-for-ascii line))
  22633. ;; Remove targets
  22634. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22635. (setq line (replace-match "" t t line)))
  22636. ;; Replace internal links
  22637. (while (string-match org-bracket-link-regexp line)
  22638. (setq line (replace-match
  22639. (if (match-end 3) "[\\3]" "[\\1]")
  22640. t nil line)))
  22641. (when custom-times
  22642. (setq line (org-translate-time line)))
  22643. (cond
  22644. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22645. ;; a Headline
  22646. (setq first-heading-pos (or first-heading-pos (point)))
  22647. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22648. txt (match-string 2 line))
  22649. (org-ascii-level-start level txt umax lines))
  22650. ((and org-export-with-tables
  22651. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22652. (if (not table-open)
  22653. ;; New table starts
  22654. (setq table-open t table-buffer nil))
  22655. ;; Accumulate lines
  22656. (setq table-buffer (cons line table-buffer))
  22657. (when (or (not lines)
  22658. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22659. (car lines))))
  22660. (setq table-open nil
  22661. table-buffer (nreverse table-buffer))
  22662. (insert (mapconcat
  22663. (lambda (x)
  22664. (org-fix-indentation x org-ascii-current-indentation))
  22665. (org-format-table-ascii table-buffer)
  22666. "\n") "\n")))
  22667. (t
  22668. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22669. (if (and org-export-with-fixed-width
  22670. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22671. (setq line (replace-match "\\1" nil nil line)))
  22672. (insert line "\n"))))
  22673. (normal-mode)
  22674. ;; insert the table of contents
  22675. (when thetoc
  22676. (goto-char (point-min))
  22677. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22678. (progn
  22679. (goto-char (match-beginning 0))
  22680. (replace-match ""))
  22681. (goto-char first-heading-pos))
  22682. (mapc 'insert thetoc)
  22683. (or (looking-at "[ \t]*\n[ \t]*\n")
  22684. (insert "\n\n")))
  22685. ;; Convert whitespace place holders
  22686. (goto-char (point-min))
  22687. (let (beg end)
  22688. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22689. (setq end (next-single-property-change beg 'org-whitespace))
  22690. (goto-char beg)
  22691. (delete-region beg end)
  22692. (insert (make-string (- end beg) ?\ ))))
  22693. (save-buffer)
  22694. ;; remove display and invisible chars
  22695. (let (beg end)
  22696. (goto-char (point-min))
  22697. (while (setq beg (next-single-property-change (point) 'display))
  22698. (setq end (next-single-property-change beg 'display))
  22699. (delete-region beg end)
  22700. (goto-char beg)
  22701. (insert "=>"))
  22702. (goto-char (point-min))
  22703. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22704. (setq end (next-single-property-change beg 'org-cwidth))
  22705. (delete-region beg end)
  22706. (goto-char beg)))
  22707. (goto-char (point-min))))
  22708. (defun org-export-ascii-clean-string ()
  22709. "Do extra work for ASCII export"
  22710. (goto-char (point-min))
  22711. (while (re-search-forward org-verbatim-re nil t)
  22712. (goto-char (match-end 2))
  22713. (backward-delete-char 1) (insert "'")
  22714. (goto-char (match-beginning 2))
  22715. (delete-char 1) (insert "`")
  22716. (goto-char (match-end 2))))
  22717. (defun org-search-todo-below (line lines level)
  22718. "Search the subtree below LINE for any TODO entries."
  22719. (let ((rest (cdr (memq line lines)))
  22720. (re org-todo-line-regexp)
  22721. line lv todo)
  22722. (catch 'exit
  22723. (while (setq line (pop rest))
  22724. (if (string-match re line)
  22725. (progn
  22726. (setq lv (- (match-end 1) (match-beginning 1))
  22727. todo (and (match-beginning 2)
  22728. (not (member (match-string 2 line)
  22729. org-done-keywords))))
  22730. ; TODO, not DONE
  22731. (if (<= lv level) (throw 'exit nil))
  22732. (if todo (throw 'exit t))))))))
  22733. (defun org-html-expand-for-ascii (line)
  22734. "Handle quoted HTML for ASCII export."
  22735. (if org-export-html-expand
  22736. (while (string-match "@<[^<>\n]*>" line)
  22737. ;; We just remove the tags for now.
  22738. (setq line (replace-match "" nil nil line))))
  22739. line)
  22740. (defun org-insert-centered (s &optional underline)
  22741. "Insert the string S centered and underline it with character UNDERLINE."
  22742. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22743. (insert (make-string ind ?\ ) s "\n")
  22744. (if underline
  22745. (insert (make-string ind ?\ )
  22746. (make-string (string-width s) underline)
  22747. "\n"))))
  22748. (defun org-ascii-level-start (level title umax &optional lines)
  22749. "Insert a new level in ASCII export."
  22750. (let (char (n (- level umax 1)) (ind 0))
  22751. (if (> level umax)
  22752. (progn
  22753. (insert (make-string (* 2 n) ?\ )
  22754. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22755. org-export-ascii-bullets))
  22756. " " title "\n")
  22757. ;; find the indentation of the next non-empty line
  22758. (catch 'stop
  22759. (while lines
  22760. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22761. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22762. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22763. (pop lines)))
  22764. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22765. (if (or (not (equal (char-before) ?\n))
  22766. (not (equal (char-before (1- (point))) ?\n)))
  22767. (insert "\n"))
  22768. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22769. (unless org-export-with-tags
  22770. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22771. (setq title (replace-match "" t t title))))
  22772. (if org-export-with-section-numbers
  22773. (setq title (concat (org-section-number level) " " title)))
  22774. (insert title "\n" (make-string (string-width title) char) "\n")
  22775. (setq org-ascii-current-indentation '(0 . 0)))))
  22776. (defun org-export-visible (type arg)
  22777. "Create a copy of the visible part of the current buffer, and export it.
  22778. The copy is created in a temporary buffer and removed after use.
  22779. TYPE is the final key (as a string) that also select the export command in
  22780. the `C-c C-e' export dispatcher.
  22781. As a special case, if the you type SPC at the prompt, the temporary
  22782. org-mode file will not be removed but presented to you so that you can
  22783. continue to use it. The prefix arg ARG is passed through to the exporting
  22784. command."
  22785. (interactive
  22786. (list (progn
  22787. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22788. (read-char-exclusive))
  22789. current-prefix-arg))
  22790. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22791. (error "Invalid export key"))
  22792. (let* ((binding (cdr (assoc type
  22793. '((?a . org-export-as-ascii)
  22794. (?\C-a . org-export-as-ascii)
  22795. (?b . org-export-as-html-and-open)
  22796. (?\C-b . org-export-as-html-and-open)
  22797. (?h . org-export-as-html)
  22798. (?H . org-export-as-html-to-buffer)
  22799. (?R . org-export-region-as-html)
  22800. (?x . org-export-as-xoxo)))))
  22801. (keepp (equal type ?\ ))
  22802. (file buffer-file-name)
  22803. (buffer (get-buffer-create "*Org Export Visible*"))
  22804. s e)
  22805. ;; Need to hack the drawers here.
  22806. (save-excursion
  22807. (goto-char (point-min))
  22808. (while (re-search-forward org-drawer-regexp nil t)
  22809. (goto-char (match-beginning 1))
  22810. (or (org-invisible-p) (org-flag-drawer nil))))
  22811. (with-current-buffer buffer (erase-buffer))
  22812. (save-excursion
  22813. (setq s (goto-char (point-min)))
  22814. (while (not (= (point) (point-max)))
  22815. (goto-char (org-find-invisible))
  22816. (append-to-buffer buffer s (point))
  22817. (setq s (goto-char (org-find-visible))))
  22818. (org-cycle-hide-drawers 'all)
  22819. (goto-char (point-min))
  22820. (unless keepp
  22821. ;; Copy all comment lines to the end, to make sure #+ settings are
  22822. ;; still available for the second export step. Kind of a hack, but
  22823. ;; does do the trick.
  22824. (if (looking-at "#[^\r\n]*")
  22825. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22826. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22827. (append-to-buffer buffer (1+ (match-beginning 0))
  22828. (min (point-max) (1+ (match-end 0))))))
  22829. (set-buffer buffer)
  22830. (let ((buffer-file-name file)
  22831. (org-inhibit-startup t))
  22832. (org-mode)
  22833. (show-all)
  22834. (unless keepp (funcall binding arg))))
  22835. (if (not keepp)
  22836. (kill-buffer buffer)
  22837. (switch-to-buffer-other-window buffer)
  22838. (goto-char (point-min)))))
  22839. (defun org-find-visible ()
  22840. (let ((s (point)))
  22841. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22842. (get-char-property s 'invisible)))
  22843. s))
  22844. (defun org-find-invisible ()
  22845. (let ((s (point)))
  22846. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22847. (not (get-char-property s 'invisible))))
  22848. s))
  22849. ;;; HTML export
  22850. (defun org-get-current-options ()
  22851. "Return a string with current options as keyword options.
  22852. Does include HTML export options as well as TODO and CATEGORY stuff."
  22853. (format
  22854. "#+TITLE: %s
  22855. #+AUTHOR: %s
  22856. #+EMAIL: %s
  22857. #+LANGUAGE: %s
  22858. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22859. #+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
  22860. #+CATEGORY: %s
  22861. #+SEQ_TODO: %s
  22862. #+TYP_TODO: %s
  22863. #+PRIORITIES: %c %c %c
  22864. #+DRAWERS: %s
  22865. #+STARTUP: %s %s %s %s %s
  22866. #+TAGS: %s
  22867. #+ARCHIVE: %s
  22868. #+LINK: %s
  22869. "
  22870. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22871. org-export-headline-levels
  22872. org-export-with-section-numbers
  22873. org-export-with-toc
  22874. org-export-preserve-breaks
  22875. org-export-html-expand
  22876. org-export-with-fixed-width
  22877. org-export-with-tables
  22878. org-export-with-sub-superscripts
  22879. org-export-with-special-strings
  22880. org-export-with-footnotes
  22881. org-export-with-emphasize
  22882. org-export-with-TeX-macros
  22883. org-export-with-LaTeX-fragments
  22884. org-export-skip-text-before-1st-heading
  22885. org-export-with-drawers
  22886. org-export-with-tags
  22887. (file-name-nondirectory buffer-file-name)
  22888. "TODO FEEDBACK VERIFY DONE"
  22889. "Me Jason Marie DONE"
  22890. org-highest-priority org-lowest-priority org-default-priority
  22891. (mapconcat 'identity org-drawers " ")
  22892. (cdr (assoc org-startup-folded
  22893. '((nil . "showall") (t . "overview") (content . "content"))))
  22894. (if org-odd-levels-only "odd" "oddeven")
  22895. (if org-hide-leading-stars "hidestars" "showstars")
  22896. (if org-startup-align-all-tables "align" "noalign")
  22897. (cond ((eq org-log-done t) "logdone")
  22898. ((equal org-log-done 'note) "lognotedone")
  22899. ((not org-log-done) "nologdone"))
  22900. (or (mapconcat (lambda (x)
  22901. (cond
  22902. ((equal '(:startgroup) x) "{")
  22903. ((equal '(:endgroup) x) "}")
  22904. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22905. (t (car x))))
  22906. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22907. org-archive-location
  22908. "org file:~/org/%s.org"
  22909. ))
  22910. (defun org-insert-export-options-template ()
  22911. "Insert into the buffer a template with information for exporting."
  22912. (interactive)
  22913. (if (not (bolp)) (newline))
  22914. (let ((s (org-get-current-options)))
  22915. (and (string-match "#\\+CATEGORY" s)
  22916. (setq s (substring s 0 (match-beginning 0))))
  22917. (insert s)))
  22918. (defun org-toggle-fixed-width-section (arg)
  22919. "Toggle the fixed-width export.
  22920. If there is no active region, the QUOTE keyword at the current headline is
  22921. inserted or removed. When present, it causes the text between this headline
  22922. and the next to be exported as fixed-width text, and unmodified.
  22923. If there is an active region, this command adds or removes a colon as the
  22924. first character of this line. If the first character of a line is a colon,
  22925. this line is also exported in fixed-width font."
  22926. (interactive "P")
  22927. (let* ((cc 0)
  22928. (regionp (org-region-active-p))
  22929. (beg (if regionp (region-beginning) (point)))
  22930. (end (if regionp (region-end)))
  22931. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22932. (case-fold-search nil)
  22933. (re "[ \t]*\\(:\\)")
  22934. off)
  22935. (if regionp
  22936. (save-excursion
  22937. (goto-char beg)
  22938. (setq cc (current-column))
  22939. (beginning-of-line 1)
  22940. (setq off (looking-at re))
  22941. (while (> nlines 0)
  22942. (setq nlines (1- nlines))
  22943. (beginning-of-line 1)
  22944. (cond
  22945. (arg
  22946. (move-to-column cc t)
  22947. (insert ":\n")
  22948. (forward-line -1))
  22949. ((and off (looking-at re))
  22950. (replace-match "" t t nil 1))
  22951. ((not off) (move-to-column cc t) (insert ":")))
  22952. (forward-line 1)))
  22953. (save-excursion
  22954. (org-back-to-heading)
  22955. (if (looking-at (concat outline-regexp
  22956. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22957. (replace-match "" t t nil 1)
  22958. (if (looking-at outline-regexp)
  22959. (progn
  22960. (goto-char (match-end 0))
  22961. (insert org-quote-string " "))))))))
  22962. (defun org-export-as-html-and-open (arg)
  22963. "Export the outline as HTML and immediately open it with a browser.
  22964. If there is an active region, export only the region.
  22965. The prefix ARG specifies how many levels of the outline should become
  22966. headlines. The default is 3. Lower levels will become bulleted lists."
  22967. (interactive "P")
  22968. (org-export-as-html arg 'hidden)
  22969. (org-open-file buffer-file-name))
  22970. (defun org-export-as-html-batch ()
  22971. "Call `org-export-as-html', may be used in batch processing as
  22972. emacs --batch
  22973. --load=$HOME/lib/emacs/org.el
  22974. --eval \"(setq org-export-headline-levels 2)\"
  22975. --visit=MyFile --funcall org-export-as-html-batch"
  22976. (org-export-as-html org-export-headline-levels 'hidden))
  22977. (defun org-export-as-html-to-buffer (arg)
  22978. "Call `org-exort-as-html` with output to a temporary buffer.
  22979. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22980. (interactive "P")
  22981. (org-export-as-html arg nil nil "*Org HTML Export*")
  22982. (switch-to-buffer-other-window "*Org HTML Export*"))
  22983. (defun org-replace-region-by-html (beg end)
  22984. "Assume the current region has org-mode syntax, and convert it to HTML.
  22985. This can be used in any buffer. For example, you could write an
  22986. itemized list in org-mode syntax in an HTML buffer and then use this
  22987. command to convert it."
  22988. (interactive "r")
  22989. (let (reg html buf pop-up-frames)
  22990. (save-window-excursion
  22991. (if (org-mode-p)
  22992. (setq html (org-export-region-as-html
  22993. beg end t 'string))
  22994. (setq reg (buffer-substring beg end)
  22995. buf (get-buffer-create "*Org tmp*"))
  22996. (with-current-buffer buf
  22997. (erase-buffer)
  22998. (insert reg)
  22999. (org-mode)
  23000. (setq html (org-export-region-as-html
  23001. (point-min) (point-max) t 'string)))
  23002. (kill-buffer buf)))
  23003. (delete-region beg end)
  23004. (insert html)))
  23005. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23006. "Convert region from BEG to END in org-mode buffer to HTML.
  23007. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23008. contents, and only produce the region of converted text, useful for
  23009. cut-and-paste operations.
  23010. If BUFFER is a buffer or a string, use/create that buffer as a target
  23011. of the converted HTML. If BUFFER is the symbol `string', return the
  23012. produced HTML as a string and leave not buffer behind. For example,
  23013. a Lisp program could call this function in the following way:
  23014. (setq html (org-export-region-as-html beg end t 'string))
  23015. When called interactively, the output buffer is selected, and shown
  23016. in a window. A non-interactive call will only retunr the buffer."
  23017. (interactive "r\nP")
  23018. (when (interactive-p)
  23019. (setq buffer "*Org HTML Export*"))
  23020. (let ((transient-mark-mode t) (zmacs-regions t)
  23021. rtn)
  23022. (goto-char end)
  23023. (set-mark (point)) ;; to activate the region
  23024. (goto-char beg)
  23025. (setq rtn (org-export-as-html
  23026. nil nil nil
  23027. buffer body-only))
  23028. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23029. (if (and (interactive-p) (bufferp rtn))
  23030. (switch-to-buffer-other-window rtn)
  23031. rtn)))
  23032. (defvar html-table-tag nil) ; dynamically scoped into this.
  23033. (defun org-export-as-html (arg &optional hidden ext-plist
  23034. to-buffer body-only)
  23035. "Export the outline as a pretty HTML file.
  23036. If there is an active region, export only the region. The prefix
  23037. ARG specifies how many levels of the outline should become
  23038. headlines. The default is 3. Lower levels will become bulleted
  23039. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23040. EXT-PLIST is a property list with external parameters overriding
  23041. org-mode's default settings, but still inferior to file-local
  23042. settings. When TO-BUFFER is non-nil, create a buffer with that
  23043. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23044. don't leave any buffer behind but just return the resulting HTML as
  23045. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23046. simply return the content of <body>...</body>, without even
  23047. the body tags themselves."
  23048. (interactive "P")
  23049. ;; Make sure we have a file name when we need it.
  23050. (when (and (not (or to-buffer body-only))
  23051. (not buffer-file-name))
  23052. (if (buffer-base-buffer)
  23053. (org-set-local 'buffer-file-name
  23054. (with-current-buffer (buffer-base-buffer)
  23055. buffer-file-name))
  23056. (error "Need a file name to be able to export.")))
  23057. (message "Exporting...")
  23058. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23059. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23060. (setq-default org-done-keywords org-done-keywords)
  23061. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23062. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23063. ext-plist
  23064. (org-infile-export-plist)))
  23065. (style (plist-get opt-plist :style))
  23066. (html-extension (plist-get opt-plist :html-extension))
  23067. (link-validate (plist-get opt-plist :link-validation-function))
  23068. valid thetoc have-headings first-heading-pos
  23069. (odd org-odd-levels-only)
  23070. (region-p (org-region-active-p))
  23071. (subtree-p
  23072. (when region-p
  23073. (save-excursion
  23074. (goto-char (region-beginning))
  23075. (and (org-at-heading-p)
  23076. (>= (org-end-of-subtree t t) (region-end))))))
  23077. ;; The following two are dynamically scoped into other
  23078. ;; routines below.
  23079. (org-current-export-dir (org-export-directory :html opt-plist))
  23080. (org-current-export-file buffer-file-name)
  23081. (level 0) (line "") (origline "") txt todo
  23082. (umax nil)
  23083. (umax-toc nil)
  23084. (filename (if to-buffer nil
  23085. (expand-file-name
  23086. (concat
  23087. (file-name-sans-extension
  23088. (or (and subtree-p
  23089. (org-entry-get (region-beginning)
  23090. "EXPORT_FILE_NAME" t))
  23091. (file-name-nondirectory buffer-file-name)))
  23092. "." html-extension)
  23093. (file-name-as-directory
  23094. (org-export-directory :html opt-plist)))))
  23095. (current-dir (if buffer-file-name
  23096. (file-name-directory buffer-file-name)
  23097. default-directory))
  23098. (buffer (if to-buffer
  23099. (cond
  23100. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23101. (t (get-buffer-create to-buffer)))
  23102. (find-file-noselect filename)))
  23103. (org-levels-open (make-vector org-level-max nil))
  23104. (date (plist-get opt-plist :date))
  23105. (author (plist-get opt-plist :author))
  23106. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23107. (plist-get opt-plist :title)
  23108. (and (not
  23109. (plist-get opt-plist :skip-before-1st-heading))
  23110. (org-export-grab-title-from-buffer))
  23111. (and buffer-file-name
  23112. (file-name-sans-extension
  23113. (file-name-nondirectory buffer-file-name)))
  23114. "UNTITLED"))
  23115. (html-table-tag (plist-get opt-plist :html-table-tag))
  23116. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23117. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23118. (inquote nil)
  23119. (infixed nil)
  23120. (in-local-list nil)
  23121. (local-list-num nil)
  23122. (local-list-indent nil)
  23123. (llt org-plain-list-ordered-item-terminator)
  23124. (email (plist-get opt-plist :email))
  23125. (language (plist-get opt-plist :language))
  23126. (lang-words nil)
  23127. (target-alist nil) tg
  23128. (head-count 0) cnt
  23129. (start 0)
  23130. (coding-system (and (boundp 'buffer-file-coding-system)
  23131. buffer-file-coding-system))
  23132. (coding-system-for-write (or org-export-html-coding-system
  23133. coding-system))
  23134. (save-buffer-coding-system (or org-export-html-coding-system
  23135. coding-system))
  23136. (charset (and coding-system-for-write
  23137. (fboundp 'coding-system-get)
  23138. (coding-system-get coding-system-for-write
  23139. 'mime-charset)))
  23140. (region
  23141. (buffer-substring
  23142. (if region-p (region-beginning) (point-min))
  23143. (if region-p (region-end) (point-max))))
  23144. (lines
  23145. (org-split-string
  23146. (org-cleaned-string-for-export
  23147. region
  23148. :emph-multiline t
  23149. :for-html t
  23150. :skip-before-1st-heading
  23151. (plist-get opt-plist :skip-before-1st-heading)
  23152. :drawers (plist-get opt-plist :drawers)
  23153. :archived-trees
  23154. (plist-get opt-plist :archived-trees)
  23155. :add-text
  23156. (plist-get opt-plist :text)
  23157. :LaTeX-fragments
  23158. (plist-get opt-plist :LaTeX-fragments))
  23159. "[\r\n]"))
  23160. table-open type
  23161. table-buffer table-orig-buffer
  23162. ind start-is-num starter didclose
  23163. rpl path desc descp desc1 desc2 link
  23164. )
  23165. (let ((inhibit-read-only t))
  23166. (org-unmodified
  23167. (remove-text-properties (point-min) (point-max)
  23168. '(:org-license-to-kill t))))
  23169. (message "Exporting...")
  23170. (setq org-min-level (org-get-min-level lines))
  23171. (setq org-last-level org-min-level)
  23172. (org-init-section-numbers)
  23173. (cond
  23174. ((and date (string-match "%" date))
  23175. (setq date (format-time-string date (current-time))))
  23176. (date)
  23177. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23178. ;; Get the language-dependent settings
  23179. (setq lang-words (or (assoc language org-export-language-setup)
  23180. (assoc "en" org-export-language-setup)))
  23181. ;; Switch to the output buffer
  23182. (set-buffer buffer)
  23183. (let ((inhibit-read-only t)) (erase-buffer))
  23184. (fundamental-mode)
  23185. (and (fboundp 'set-buffer-file-coding-system)
  23186. (set-buffer-file-coding-system coding-system-for-write))
  23187. (let ((case-fold-search nil)
  23188. (org-odd-levels-only odd))
  23189. ;; create local variables for all options, to make sure all called
  23190. ;; functions get the correct information
  23191. (mapc (lambda (x)
  23192. (set (make-local-variable (cdr x))
  23193. (plist-get opt-plist (car x))))
  23194. org-export-plist-vars)
  23195. (setq umax (if arg (prefix-numeric-value arg)
  23196. org-export-headline-levels))
  23197. (setq umax-toc (if (integerp org-export-with-toc)
  23198. (min org-export-with-toc umax)
  23199. umax))
  23200. (unless body-only
  23201. ;; File header
  23202. (insert (format
  23203. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23204. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23205. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23206. lang=\"%s\" xml:lang=\"%s\">
  23207. <head>
  23208. <title>%s</title>
  23209. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23210. <meta name=\"generator\" content=\"Org-mode\"/>
  23211. <meta name=\"generated\" content=\"%s\"/>
  23212. <meta name=\"author\" content=\"%s\"/>
  23213. %s
  23214. </head><body>
  23215. "
  23216. language language (org-html-expand title)
  23217. (or charset "iso-8859-1") date author style))
  23218. (insert (or (plist-get opt-plist :preamble) ""))
  23219. (when (plist-get opt-plist :auto-preamble)
  23220. (if title (insert (format org-export-html-title-format
  23221. (org-html-expand title))))))
  23222. (if (and org-export-with-toc (not body-only))
  23223. (progn
  23224. (push (format "<h%d>%s</h%d>\n"
  23225. org-export-html-toplevel-hlevel
  23226. (nth 3 lang-words)
  23227. org-export-html-toplevel-hlevel)
  23228. thetoc)
  23229. (push "<ul>\n<li>" thetoc)
  23230. (setq lines
  23231. (mapcar '(lambda (line)
  23232. (if (string-match org-todo-line-regexp line)
  23233. ;; This is a headline
  23234. (progn
  23235. (setq have-headings t)
  23236. (setq level (- (match-end 1) (match-beginning 1))
  23237. level (org-tr-level level)
  23238. txt (save-match-data
  23239. (org-html-expand
  23240. (org-export-cleanup-toc-line
  23241. (match-string 3 line))))
  23242. todo
  23243. (or (and org-export-mark-todo-in-toc
  23244. (match-beginning 2)
  23245. (not (member (match-string 2 line)
  23246. org-done-keywords)))
  23247. ; TODO, not DONE
  23248. (and org-export-mark-todo-in-toc
  23249. (= level umax-toc)
  23250. (org-search-todo-below
  23251. line lines level))))
  23252. (if (string-match
  23253. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23254. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23255. (if (string-match quote-re0 txt)
  23256. (setq txt (replace-match "" t t txt)))
  23257. (if org-export-with-section-numbers
  23258. (setq txt (concat (org-section-number level)
  23259. " " txt)))
  23260. (if (<= level (max umax umax-toc))
  23261. (setq head-count (+ head-count 1)))
  23262. (if (<= level umax-toc)
  23263. (progn
  23264. (if (> level org-last-level)
  23265. (progn
  23266. (setq cnt (- level org-last-level))
  23267. (while (>= (setq cnt (1- cnt)) 0)
  23268. (push "\n<ul>\n<li>" thetoc))
  23269. (push "\n" thetoc)))
  23270. (if (< level org-last-level)
  23271. (progn
  23272. (setq cnt (- org-last-level level))
  23273. (while (>= (setq cnt (1- cnt)) 0)
  23274. (push "</li>\n</ul>" thetoc))
  23275. (push "\n" thetoc)))
  23276. ;; Check for targets
  23277. (while (string-match org-target-regexp line)
  23278. (setq tg (match-string 1 line)
  23279. line (replace-match
  23280. (concat "@<span class=\"target\">" tg "@</span> ")
  23281. t t line))
  23282. (push (cons (org-solidify-link-text tg)
  23283. (format "sec-%d" head-count))
  23284. target-alist))
  23285. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23286. (setq txt (replace-match "" t t txt)))
  23287. (push
  23288. (format
  23289. (if todo
  23290. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23291. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23292. head-count txt) thetoc)
  23293. (setq org-last-level level))
  23294. )))
  23295. line)
  23296. lines))
  23297. (while (> org-last-level (1- org-min-level))
  23298. (setq org-last-level (1- org-last-level))
  23299. (push "</li>\n</ul>\n" thetoc))
  23300. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23301. (setq head-count 0)
  23302. (org-init-section-numbers)
  23303. (while (setq line (pop lines) origline line)
  23304. (catch 'nextline
  23305. ;; end of quote section?
  23306. (when (and inquote (string-match "^\\*+ " line))
  23307. (insert "</pre>\n")
  23308. (setq inquote nil))
  23309. ;; inside a quote section?
  23310. (when inquote
  23311. (insert (org-html-protect line) "\n")
  23312. (throw 'nextline nil))
  23313. ;; verbatim lines
  23314. (when (and org-export-with-fixed-width
  23315. (string-match "^[ \t]*:\\(.*\\)" line))
  23316. (when (not infixed)
  23317. (setq infixed t)
  23318. (insert "<pre>\n"))
  23319. (insert (org-html-protect (match-string 1 line)) "\n")
  23320. (when (and lines
  23321. (not (string-match "^[ \t]*\\(:.*\\)"
  23322. (car lines))))
  23323. (setq infixed nil)
  23324. (insert "</pre>\n"))
  23325. (throw 'nextline nil))
  23326. ;; Protected HTML
  23327. (when (get-text-property 0 'org-protected line)
  23328. (let (par)
  23329. (when (re-search-backward
  23330. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23331. (setq par (match-string 1))
  23332. (replace-match "\\2\n"))
  23333. (insert line "\n")
  23334. (while (and lines
  23335. (or (= (length (car lines)) 0)
  23336. (get-text-property 0 'org-protected (car lines))))
  23337. (insert (pop lines) "\n"))
  23338. (and par (insert "<p>\n")))
  23339. (throw 'nextline nil))
  23340. ;; Horizontal line
  23341. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23342. (insert "\n<hr/>\n")
  23343. (throw 'nextline nil))
  23344. ;; make targets to anchors
  23345. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23346. (cond
  23347. ((match-end 2)
  23348. (setq line (replace-match
  23349. (concat "@<a name=\""
  23350. (org-solidify-link-text (match-string 1 line))
  23351. "\">\\nbsp@</a>")
  23352. t t line)))
  23353. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23354. (setq line (replace-match
  23355. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23356. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23357. t t line)))
  23358. (t
  23359. (setq line (replace-match
  23360. (concat "@<a name=\""
  23361. (org-solidify-link-text (match-string 1 line))
  23362. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23363. t t line)))))
  23364. (setq line (org-html-handle-time-stamps line))
  23365. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23366. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23367. ;; Also handle sub_superscripts and checkboxes
  23368. (or (string-match org-table-hline-regexp line)
  23369. (setq line (org-html-expand line)))
  23370. ;; Format the links
  23371. (setq start 0)
  23372. (while (string-match org-bracket-link-analytic-regexp line start)
  23373. (setq start (match-beginning 0))
  23374. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23375. (setq path (match-string 3 line))
  23376. (setq desc1 (if (match-end 5) (match-string 5 line))
  23377. desc2 (if (match-end 2) (concat type ":" path) path)
  23378. descp (and desc1 (not (equal desc1 desc2)))
  23379. desc (or desc1 desc2))
  23380. ;; Make an image out of the description if that is so wanted
  23381. (when (and descp (org-file-image-p desc))
  23382. (save-match-data
  23383. (if (string-match "^file:" desc)
  23384. (setq desc (substring desc (match-end 0)))))
  23385. (setq desc (concat "<img src=\"" desc "\"/>")))
  23386. ;; FIXME: do we need to unescape here somewhere?
  23387. (cond
  23388. ((equal type "internal")
  23389. (setq rpl
  23390. (concat
  23391. "<a href=\"#"
  23392. (org-solidify-link-text
  23393. (save-match-data (org-link-unescape path)) target-alist)
  23394. "\">" desc "</a>")))
  23395. ((member type '("http" "https"))
  23396. ;; standard URL, just check if we need to inline an image
  23397. (if (and (or (eq t org-export-html-inline-images)
  23398. (and org-export-html-inline-images (not descp)))
  23399. (org-file-image-p path))
  23400. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23401. (setq link (concat type ":" path))
  23402. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23403. ((member type '("ftp" "mailto" "news"))
  23404. ;; standard URL
  23405. (setq link (concat type ":" path))
  23406. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23407. ((string= type "file")
  23408. ;; FILE link
  23409. (let* ((filename path)
  23410. (abs-p (file-name-absolute-p filename))
  23411. thefile file-is-image-p search)
  23412. (save-match-data
  23413. (if (string-match "::\\(.*\\)" filename)
  23414. (setq search (match-string 1 filename)
  23415. filename (replace-match "" t nil filename)))
  23416. (setq valid
  23417. (if (functionp link-validate)
  23418. (funcall link-validate filename current-dir)
  23419. t))
  23420. (setq file-is-image-p (org-file-image-p filename))
  23421. (setq thefile (if abs-p (expand-file-name filename) filename))
  23422. (when (and org-export-html-link-org-files-as-html
  23423. (string-match "\\.org$" thefile))
  23424. (setq thefile (concat (substring thefile 0
  23425. (match-beginning 0))
  23426. "." html-extension))
  23427. (if (and search
  23428. ;; make sure this is can be used as target search
  23429. (not (string-match "^[0-9]*$" search))
  23430. (not (string-match "^\\*" search))
  23431. (not (string-match "^/.*/$" search)))
  23432. (setq thefile (concat thefile "#"
  23433. (org-solidify-link-text
  23434. (org-link-unescape search)))))
  23435. (when (string-match "^file:" desc)
  23436. (setq desc (replace-match "" t t desc))
  23437. (if (string-match "\\.org$" desc)
  23438. (setq desc (replace-match "" t t desc))))))
  23439. (setq rpl (if (and file-is-image-p
  23440. (or (eq t org-export-html-inline-images)
  23441. (and org-export-html-inline-images
  23442. (not descp))))
  23443. (concat "<img src=\"" thefile "\"/>")
  23444. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23445. (if (not valid) (setq rpl desc))))
  23446. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23447. (setq rpl (concat "<i>&lt;" type ":"
  23448. (save-match-data (org-link-unescape path))
  23449. "&gt;</i>"))))
  23450. (setq line (replace-match rpl t t line)
  23451. start (+ start (length rpl))))
  23452. ;; TODO items
  23453. (if (and (string-match org-todo-line-regexp line)
  23454. (match-beginning 2))
  23455. (setq line
  23456. (concat (substring line 0 (match-beginning 2))
  23457. "<span class=\""
  23458. (if (member (match-string 2 line)
  23459. org-done-keywords)
  23460. "done" "todo")
  23461. "\">" (match-string 2 line)
  23462. "</span>" (substring line (match-end 2)))))
  23463. ;; Does this contain a reference to a footnote?
  23464. (when org-export-with-footnotes
  23465. (setq start 0)
  23466. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23467. (if (get-text-property (match-beginning 2) 'org-protected line)
  23468. (setq start (match-end 2))
  23469. (let ((n (match-string 2 line)))
  23470. (setq line
  23471. (replace-match
  23472. (format
  23473. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23474. (match-string 1 line) n n n)
  23475. t t line))))))
  23476. (cond
  23477. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23478. ;; This is a headline
  23479. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23480. txt (match-string 2 line))
  23481. (if (string-match quote-re0 txt)
  23482. (setq txt (replace-match "" t t txt)))
  23483. (if (<= level (max umax umax-toc))
  23484. (setq head-count (+ head-count 1)))
  23485. (when in-local-list
  23486. ;; Close any local lists before inserting a new header line
  23487. (while local-list-num
  23488. (org-close-li)
  23489. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23490. (pop local-list-num))
  23491. (setq local-list-indent nil
  23492. in-local-list nil))
  23493. (setq first-heading-pos (or first-heading-pos (point)))
  23494. (org-html-level-start level txt umax
  23495. (and org-export-with-toc (<= level umax))
  23496. head-count)
  23497. ;; QUOTES
  23498. (when (string-match quote-re line)
  23499. (insert "<pre>")
  23500. (setq inquote t)))
  23501. ((and org-export-with-tables
  23502. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23503. (if (not table-open)
  23504. ;; New table starts
  23505. (setq table-open t table-buffer nil table-orig-buffer nil))
  23506. ;; Accumulate lines
  23507. (setq table-buffer (cons line table-buffer)
  23508. table-orig-buffer (cons origline table-orig-buffer))
  23509. (when (or (not lines)
  23510. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23511. (car lines))))
  23512. (setq table-open nil
  23513. table-buffer (nreverse table-buffer)
  23514. table-orig-buffer (nreverse table-orig-buffer))
  23515. (org-close-par-maybe)
  23516. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23517. (t
  23518. ;; Normal lines
  23519. (when (string-match
  23520. (cond
  23521. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23522. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23523. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23524. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23525. line)
  23526. (setq ind (org-get-string-indentation line)
  23527. start-is-num (match-beginning 4)
  23528. starter (if (match-beginning 2)
  23529. (substring (match-string 2 line) 0 -1))
  23530. line (substring line (match-beginning 5)))
  23531. (unless (string-match "[^ \t]" line)
  23532. ;; empty line. Pretend indentation is large.
  23533. (setq ind (if org-empty-line-terminates-plain-lists
  23534. 0
  23535. (1+ (or (car local-list-indent) 1)))))
  23536. (setq didclose nil)
  23537. (while (and in-local-list
  23538. (or (and (= ind (car local-list-indent))
  23539. (not starter))
  23540. (< ind (car local-list-indent))))
  23541. (setq didclose t)
  23542. (org-close-li)
  23543. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23544. (pop local-list-num) (pop local-list-indent)
  23545. (setq in-local-list local-list-indent))
  23546. (cond
  23547. ((and starter
  23548. (or (not in-local-list)
  23549. (> ind (car local-list-indent))))
  23550. ;; Start new (level of) list
  23551. (org-close-par-maybe)
  23552. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23553. (push start-is-num local-list-num)
  23554. (push ind local-list-indent)
  23555. (setq in-local-list t))
  23556. (starter
  23557. ;; continue current list
  23558. (org-close-li)
  23559. (insert "<li>\n"))
  23560. (didclose
  23561. ;; we did close a list, normal text follows: need <p>
  23562. (org-open-par)))
  23563. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23564. (setq line
  23565. (replace-match
  23566. (if (equal (match-string 1 line) "X")
  23567. "<b>[X]</b>"
  23568. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23569. t t line))))
  23570. ;; Empty lines start a new paragraph. If hand-formatted lists
  23571. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23572. ;; also start a new paragraph.
  23573. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23574. ;; Is this the start of a footnote?
  23575. (when org-export-with-footnotes
  23576. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23577. (org-close-par-maybe)
  23578. (let ((n (match-string 1 line)))
  23579. (setq line (replace-match
  23580. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23581. ;; Check if the line break needs to be conserved
  23582. (cond
  23583. ((string-match "\\\\\\\\[ \t]*$" line)
  23584. (setq line (replace-match "<br/>" t t line)))
  23585. (org-export-preserve-breaks
  23586. (setq line (concat line "<br/>"))))
  23587. (insert line "\n")))))
  23588. ;; Properly close all local lists and other lists
  23589. (when inquote (insert "</pre>\n"))
  23590. (when in-local-list
  23591. ;; Close any local lists before inserting a new header line
  23592. (while local-list-num
  23593. (org-close-li)
  23594. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23595. (pop local-list-num))
  23596. (setq local-list-indent nil
  23597. in-local-list nil))
  23598. (org-html-level-start 1 nil umax
  23599. (and org-export-with-toc (<= level umax))
  23600. head-count)
  23601. (unless body-only
  23602. (when (plist-get opt-plist :auto-postamble)
  23603. (insert "<div id=\"postamble\">")
  23604. (when (and org-export-author-info author)
  23605. (insert "<p class=\"author\"> "
  23606. (nth 1 lang-words) ": " author "\n")
  23607. (when email
  23608. (if (listp (split-string email ",+ *"))
  23609. (mapc (lambda(e)
  23610. (insert "<a href=\"mailto:" e "\">&lt;"
  23611. e "&gt;</a>\n"))
  23612. (split-string email ",+ *"))
  23613. (insert "<a href=\"mailto:" email "\">&lt;"
  23614. email "&gt;</a>\n")))
  23615. (insert "</p>\n"))
  23616. (when (and date org-export-time-stamp-file)
  23617. (insert "<p class=\"date\"> "
  23618. (nth 2 lang-words) ": "
  23619. date "</p>\n"))
  23620. (insert "</div>"))
  23621. (if org-export-html-with-timestamp
  23622. (insert org-export-html-html-helper-timestamp))
  23623. (insert (or (plist-get opt-plist :postamble) ""))
  23624. (insert "</body>\n</html>\n"))
  23625. (normal-mode)
  23626. (if (eq major-mode default-major-mode) (html-mode))
  23627. ;; insert the table of contents
  23628. (goto-char (point-min))
  23629. (when thetoc
  23630. (if (or (re-search-forward
  23631. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23632. (re-search-forward
  23633. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23634. (progn
  23635. (goto-char (match-beginning 0))
  23636. (replace-match ""))
  23637. (goto-char first-heading-pos)
  23638. (when (looking-at "\\s-*</p>")
  23639. (goto-char (match-end 0))
  23640. (insert "\n")))
  23641. (insert "<div id=\"table-of-contents\">\n")
  23642. (mapc 'insert thetoc)
  23643. (insert "</div>\n"))
  23644. ;; remove empty paragraphs and lists
  23645. (goto-char (point-min))
  23646. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23647. (replace-match ""))
  23648. (goto-char (point-min))
  23649. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23650. (replace-match ""))
  23651. (goto-char (point-min))
  23652. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23653. (replace-match ""))
  23654. ;; Convert whitespace place holders
  23655. (goto-char (point-min))
  23656. (let (beg end n)
  23657. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23658. (setq n (get-text-property beg 'org-whitespace)
  23659. end (next-single-property-change beg 'org-whitespace))
  23660. (goto-char beg)
  23661. (delete-region beg end)
  23662. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23663. (make-string n ?x)))))
  23664. (or to-buffer (save-buffer))
  23665. (goto-char (point-min))
  23666. (message "Exporting... done")
  23667. (if (eq to-buffer 'string)
  23668. (prog1 (buffer-substring (point-min) (point-max))
  23669. (kill-buffer (current-buffer)))
  23670. (current-buffer)))))
  23671. (defvar org-table-colgroup-info nil)
  23672. (defun org-format-table-ascii (lines)
  23673. "Format a table for ascii export."
  23674. (if (stringp lines)
  23675. (setq lines (org-split-string lines "\n")))
  23676. (if (not (string-match "^[ \t]*|" (car lines)))
  23677. ;; Table made by table.el - test for spanning
  23678. lines
  23679. ;; A normal org table
  23680. ;; Get rid of hlines at beginning and end
  23681. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23682. (setq lines (nreverse lines))
  23683. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23684. (setq lines (nreverse lines))
  23685. (when org-export-table-remove-special-lines
  23686. ;; Check if the table has a marking column. If yes remove the
  23687. ;; column and the special lines
  23688. (setq lines (org-table-clean-before-export lines)))
  23689. ;; Get rid of the vertical lines except for grouping
  23690. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23691. rtn line vl1 start)
  23692. (while (setq line (pop lines))
  23693. (if (string-match org-table-hline-regexp line)
  23694. (and (string-match "|\\(.*\\)|" line)
  23695. (setq line (replace-match " \\1" t nil line)))
  23696. (setq start 0 vl1 vl)
  23697. (while (string-match "|" line start)
  23698. (setq start (match-end 0))
  23699. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23700. (push line rtn))
  23701. (nreverse rtn))))
  23702. (defun org-colgroup-info-to-vline-list (info)
  23703. (let (vl new last)
  23704. (while info
  23705. (setq last new new (pop info))
  23706. (if (or (memq last '(:end :startend))
  23707. (memq new '(:start :startend)))
  23708. (push t vl)
  23709. (push nil vl)))
  23710. (setq vl (nreverse vl))
  23711. (and vl (setcar vl nil))
  23712. vl))
  23713. (defun org-format-table-html (lines olines)
  23714. "Find out which HTML converter to use and return the HTML code."
  23715. (if (stringp lines)
  23716. (setq lines (org-split-string lines "\n")))
  23717. (if (string-match "^[ \t]*|" (car lines))
  23718. ;; A normal org table
  23719. (org-format-org-table-html lines)
  23720. ;; Table made by table.el - test for spanning
  23721. (let* ((hlines (delq nil (mapcar
  23722. (lambda (x)
  23723. (if (string-match "^[ \t]*\\+-" x) x
  23724. nil))
  23725. lines)))
  23726. (first (car hlines))
  23727. (ll (and (string-match "\\S-+" first)
  23728. (match-string 0 first)))
  23729. (re (concat "^[ \t]*" (regexp-quote ll)))
  23730. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23731. hlines))))
  23732. (if (and (not spanning)
  23733. (not org-export-prefer-native-exporter-for-tables))
  23734. ;; We can use my own converter with HTML conversions
  23735. (org-format-table-table-html lines)
  23736. ;; Need to use the code generator in table.el, with the original text.
  23737. (org-format-table-table-html-using-table-generate-source olines)))))
  23738. (defun org-format-org-table-html (lines &optional splice)
  23739. "Format a table into HTML."
  23740. ;; Get rid of hlines at beginning and end
  23741. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23742. (setq lines (nreverse lines))
  23743. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23744. (setq lines (nreverse lines))
  23745. (when org-export-table-remove-special-lines
  23746. ;; Check if the table has a marking column. If yes remove the
  23747. ;; column and the special lines
  23748. (setq lines (org-table-clean-before-export lines)))
  23749. (let ((head (and org-export-highlight-first-table-line
  23750. (delq nil (mapcar
  23751. (lambda (x) (string-match "^[ \t]*|-" x))
  23752. (cdr lines)))))
  23753. (nlines 0) fnum i
  23754. tbopen line fields html gr colgropen)
  23755. (if splice (setq head nil))
  23756. (unless splice (push (if head "<thead>" "<tbody>") html))
  23757. (setq tbopen t)
  23758. (while (setq line (pop lines))
  23759. (catch 'next-line
  23760. (if (string-match "^[ \t]*|-" line)
  23761. (progn
  23762. (unless splice
  23763. (push (if head "</thead>" "</tbody>") html)
  23764. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23765. (setq head nil) ;; head ends here, first time around
  23766. ;; ignore this line
  23767. (throw 'next-line t)))
  23768. ;; Break the line into fields
  23769. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23770. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23771. (setq nlines (1+ nlines) i -1)
  23772. (push (concat "<tr>"
  23773. (mapconcat
  23774. (lambda (x)
  23775. (setq i (1+ i))
  23776. (if (and (< i nlines)
  23777. (string-match org-table-number-regexp x))
  23778. (incf (aref fnum i)))
  23779. (if head
  23780. (concat (car org-export-table-header-tags) x
  23781. (cdr org-export-table-header-tags))
  23782. (concat (car org-export-table-data-tags) x
  23783. (cdr org-export-table-data-tags))))
  23784. fields "")
  23785. "</tr>")
  23786. html)))
  23787. (unless splice (if tbopen (push "</tbody>" html)))
  23788. (unless splice (push "</table>\n" html))
  23789. (setq html (nreverse html))
  23790. (unless splice
  23791. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23792. (push (mapconcat
  23793. (lambda (x)
  23794. (setq gr (pop org-table-colgroup-info))
  23795. (format "%s<col align=\"%s\"></col>%s"
  23796. (if (memq gr '(:start :startend))
  23797. (prog1
  23798. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23799. (setq colgropen t))
  23800. "")
  23801. (if (> (/ (float x) nlines) org-table-number-fraction)
  23802. "right" "left")
  23803. (if (memq gr '(:end :startend))
  23804. (progn (setq colgropen nil) "</colgroup>")
  23805. "")))
  23806. fnum "")
  23807. html)
  23808. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23809. (push html-table-tag html))
  23810. (concat (mapconcat 'identity html "\n") "\n")))
  23811. (defun org-table-clean-before-export (lines)
  23812. "Check if the table has a marking column.
  23813. If yes remove the column and the special lines."
  23814. (setq org-table-colgroup-info nil)
  23815. (if (memq nil
  23816. (mapcar
  23817. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23818. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23819. lines))
  23820. (progn
  23821. (setq org-table-clean-did-remove-column nil)
  23822. (delq nil
  23823. (mapcar
  23824. (lambda (x)
  23825. (cond
  23826. ((string-match "^[ \t]*| */ *|" x)
  23827. (setq org-table-colgroup-info
  23828. (mapcar (lambda (x)
  23829. (cond ((member x '("<" "&lt;")) :start)
  23830. ((member x '(">" "&gt;")) :end)
  23831. ((member x '("<>" "&lt;&gt;")) :startend)
  23832. (t nil)))
  23833. (org-split-string x "[ \t]*|[ \t]*")))
  23834. nil)
  23835. (t x)))
  23836. lines)))
  23837. (setq org-table-clean-did-remove-column t)
  23838. (delq nil
  23839. (mapcar
  23840. (lambda (x)
  23841. (cond
  23842. ((string-match "^[ \t]*| */ *|" x)
  23843. (setq org-table-colgroup-info
  23844. (mapcar (lambda (x)
  23845. (cond ((member x '("<" "&lt;")) :start)
  23846. ((member x '(">" "&gt;")) :end)
  23847. ((member x '("<>" "&lt;&gt;")) :startend)
  23848. (t nil)))
  23849. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23850. nil)
  23851. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23852. nil) ; ignore this line
  23853. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23854. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23855. ;; remove the first column
  23856. (replace-match "\\1|" t nil x))))
  23857. lines))))
  23858. (defun org-format-table-table-html (lines)
  23859. "Format a table generated by table.el into HTML.
  23860. This conversion does *not* use `table-generate-source' from table.el.
  23861. This has the advantage that Org-mode's HTML conversions can be used.
  23862. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23863. (let (line field-buffer
  23864. (head org-export-highlight-first-table-line)
  23865. fields html empty)
  23866. (setq html (concat html-table-tag "\n"))
  23867. (while (setq line (pop lines))
  23868. (setq empty "&nbsp;")
  23869. (catch 'next-line
  23870. (if (string-match "^[ \t]*\\+-" line)
  23871. (progn
  23872. (if field-buffer
  23873. (progn
  23874. (setq
  23875. html
  23876. (concat
  23877. html
  23878. "<tr>"
  23879. (mapconcat
  23880. (lambda (x)
  23881. (if (equal x "") (setq x empty))
  23882. (if head
  23883. (concat (car org-export-table-header-tags) x
  23884. (cdr org-export-table-header-tags))
  23885. (concat (car org-export-table-data-tags) x
  23886. (cdr org-export-table-data-tags))))
  23887. field-buffer "\n")
  23888. "</tr>\n"))
  23889. (setq head nil)
  23890. (setq field-buffer nil)))
  23891. ;; Ignore this line
  23892. (throw 'next-line t)))
  23893. ;; Break the line into fields and store the fields
  23894. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23895. (if field-buffer
  23896. (setq field-buffer (mapcar
  23897. (lambda (x)
  23898. (concat x "<br/>" (pop fields)))
  23899. field-buffer))
  23900. (setq field-buffer fields))))
  23901. (setq html (concat html "</table>\n"))
  23902. html))
  23903. (defun org-format-table-table-html-using-table-generate-source (lines)
  23904. "Format a table into html, using `table-generate-source' from table.el.
  23905. This has the advantage that cell- or row-spanning is allowed.
  23906. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23907. (require 'table)
  23908. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23909. (erase-buffer)
  23910. (insert (mapconcat 'identity lines "\n"))
  23911. (goto-char (point-min))
  23912. (if (not (re-search-forward "|[^+]" nil t))
  23913. (error "Error processing table"))
  23914. (table-recognize-table)
  23915. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23916. (table-generate-source 'html " org-tmp2 ")
  23917. (set-buffer " org-tmp2 ")
  23918. (buffer-substring (point-min) (point-max))))
  23919. (defun org-html-handle-time-stamps (s)
  23920. "Format time stamps in string S, or remove them."
  23921. (catch 'exit
  23922. (let (r b)
  23923. (while (string-match org-maybe-keyword-time-regexp s)
  23924. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23925. ;; never export CLOCK
  23926. (throw 'exit ""))
  23927. (or b (setq b (substring s 0 (match-beginning 0))))
  23928. (if (not org-export-with-timestamps)
  23929. (setq r (concat r (substring s 0 (match-beginning 0)))
  23930. s (substring s (match-end 0)))
  23931. (setq r (concat
  23932. r (substring s 0 (match-beginning 0))
  23933. (if (match-end 1)
  23934. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23935. (match-string 1 s)))
  23936. (format " @<span class=\"timestamp\">%s@</span>"
  23937. (substring
  23938. (org-translate-time (match-string 3 s)) 1 -1)))
  23939. s (substring s (match-end 0)))))
  23940. ;; Line break if line started and ended with time stamp stuff
  23941. (if (not r)
  23942. s
  23943. (setq r (concat r s))
  23944. (unless (string-match "\\S-" (concat b s))
  23945. (setq r (concat r "@<br/>")))
  23946. r))))
  23947. (defun org-html-protect (s)
  23948. ;; convert & to &amp;, < to &lt; and > to &gt;
  23949. (let ((start 0))
  23950. (while (string-match "&" s start)
  23951. (setq s (replace-match "&amp;" t t s)
  23952. start (1+ (match-beginning 0))))
  23953. (while (string-match "<" s)
  23954. (setq s (replace-match "&lt;" t t s)))
  23955. (while (string-match ">" s)
  23956. (setq s (replace-match "&gt;" t t s))))
  23957. s)
  23958. (defun org-export-cleanup-toc-line (s)
  23959. "Remove tags and time staps from lines going into the toc."
  23960. (when (memq org-export-with-tags '(not-in-toc nil))
  23961. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23962. (setq s (replace-match "" t t s))))
  23963. (when org-export-remove-timestamps-from-toc
  23964. (while (string-match org-maybe-keyword-time-regexp s)
  23965. (setq s (replace-match "" t t s))))
  23966. (while (string-match org-bracket-link-regexp s)
  23967. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23968. t t s)))
  23969. s)
  23970. (defun org-html-expand (string)
  23971. "Prepare STRING for HTML export. Applies all active conversions.
  23972. If there are links in the string, don't modify these."
  23973. (let* ((re (concat org-bracket-link-regexp "\\|"
  23974. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23975. m s l res)
  23976. (while (setq m (string-match re string))
  23977. (setq s (substring string 0 m)
  23978. l (match-string 0 string)
  23979. string (substring string (match-end 0)))
  23980. (push (org-html-do-expand s) res)
  23981. (push l res))
  23982. (push (org-html-do-expand string) res)
  23983. (apply 'concat (nreverse res))))
  23984. (defun org-html-do-expand (s)
  23985. "Apply all active conversions to translate special ASCII to HTML."
  23986. (setq s (org-html-protect s))
  23987. (if org-export-html-expand
  23988. (let ((start 0))
  23989. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23990. (setq s (replace-match "<\\1>" t nil s)))))
  23991. (if org-export-with-emphasize
  23992. (setq s (org-export-html-convert-emphasize s)))
  23993. (if org-export-with-special-strings
  23994. (setq s (org-export-html-convert-special-strings s)))
  23995. (if org-export-with-sub-superscripts
  23996. (setq s (org-export-html-convert-sub-super s)))
  23997. (if org-export-with-TeX-macros
  23998. (let ((start 0) wd ass)
  23999. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24000. (if (get-text-property (match-beginning 0) 'org-protected s)
  24001. (setq start (match-end 0))
  24002. (setq wd (match-string 1 s))
  24003. (if (setq ass (assoc wd org-html-entities))
  24004. (setq s (replace-match (or (cdr ass)
  24005. (concat "&" (car ass) ";"))
  24006. t t s))
  24007. (setq start (+ start (length wd))))))))
  24008. s)
  24009. (defun org-create-multibrace-regexp (left right n)
  24010. "Create a regular expression which will match a balanced sexp.
  24011. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24012. as single character strings.
  24013. The regexp returned will match the entire expression including the
  24014. delimiters. It will also define a single group which contains the
  24015. match except for the outermost delimiters. The maximum depth of
  24016. stacked delimiters is N. Escaping delimiters is not possible."
  24017. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24018. (or "\\|")
  24019. (re nothing)
  24020. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24021. (while (> n 1)
  24022. (setq n (1- n)
  24023. re (concat re or next)
  24024. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24025. (concat left "\\(" re "\\)" right)))
  24026. (defvar org-match-substring-regexp
  24027. (concat
  24028. "\\([^\\]\\)\\([_^]\\)\\("
  24029. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24030. "\\|"
  24031. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24032. "\\|"
  24033. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24034. "The regular expression matching a sub- or superscript.")
  24035. (defvar org-match-substring-with-braces-regexp
  24036. (concat
  24037. "\\([^\\]\\)\\([_^]\\)\\("
  24038. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24039. "\\)")
  24040. "The regular expression matching a sub- or superscript, forcing braces.")
  24041. (defconst org-export-html-special-string-regexps
  24042. '(("\\\\-" . "&shy;")
  24043. ("---\\([^-]\\)" . "&mdash;\\1")
  24044. ("--\\([^-]\\)" . "&ndash;\\1")
  24045. ("\\.\\.\\." . "&hellip;"))
  24046. "Regular expressions for special string conversion.")
  24047. (defun org-export-html-convert-special-strings (string)
  24048. "Convert special characters in STRING to HTML."
  24049. (let ((all org-export-html-special-string-regexps)
  24050. e a re rpl start)
  24051. (while (setq a (pop all))
  24052. (setq re (car a) rpl (cdr a) start 0)
  24053. (while (string-match re string start)
  24054. (if (get-text-property (match-beginning 0) 'org-protected string)
  24055. (setq start (match-end 0))
  24056. (setq string (replace-match rpl t nil string)))))
  24057. string))
  24058. (defun org-export-html-convert-sub-super (string)
  24059. "Convert sub- and superscripts in STRING to HTML."
  24060. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24061. (while (string-match org-match-substring-regexp string s)
  24062. (cond
  24063. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24064. ((get-text-property (match-beginning 2) 'org-protected string)
  24065. (setq s (match-end 2)))
  24066. (t
  24067. (setq s (match-end 1)
  24068. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24069. c (or (match-string 8 string)
  24070. (match-string 6 string)
  24071. (match-string 5 string))
  24072. string (replace-match
  24073. (concat (match-string 1 string)
  24074. "<" key ">" c "</" key ">")
  24075. t t string)))))
  24076. (while (string-match "\\\\\\([_^]\\)" string)
  24077. (setq string (replace-match (match-string 1 string) t t string)))
  24078. string))
  24079. (defun org-export-html-convert-emphasize (string)
  24080. "Apply emphasis."
  24081. (let ((s 0) rpl)
  24082. (while (string-match org-emph-re string s)
  24083. (if (not (equal
  24084. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24085. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24086. (setq s (match-beginning 0)
  24087. rpl
  24088. (concat
  24089. (match-string 1 string)
  24090. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24091. (match-string 4 string)
  24092. (nth 3 (assoc (match-string 3 string)
  24093. org-emphasis-alist))
  24094. (match-string 5 string))
  24095. string (replace-match rpl t t string)
  24096. s (+ s (- (length rpl) 2)))
  24097. (setq s (1+ s))))
  24098. string))
  24099. (defvar org-par-open nil)
  24100. (defun org-open-par ()
  24101. "Insert <p>, but first close previous paragraph if any."
  24102. (org-close-par-maybe)
  24103. (insert "\n<p>")
  24104. (setq org-par-open t))
  24105. (defun org-close-par-maybe ()
  24106. "Close paragraph if there is one open."
  24107. (when org-par-open
  24108. (insert "</p>")
  24109. (setq org-par-open nil)))
  24110. (defun org-close-li ()
  24111. "Close <li> if necessary."
  24112. (org-close-par-maybe)
  24113. (insert "</li>\n"))
  24114. (defvar body-only) ; dynamically scoped into this.
  24115. (defun org-html-level-start (level title umax with-toc head-count)
  24116. "Insert a new level in HTML export.
  24117. When TITLE is nil, just close all open levels."
  24118. (org-close-par-maybe)
  24119. (let ((l org-level-max))
  24120. (while (>= l level)
  24121. (if (aref org-levels-open (1- l))
  24122. (progn
  24123. (org-html-level-close l umax)
  24124. (aset org-levels-open (1- l) nil)))
  24125. (setq l (1- l)))
  24126. (when title
  24127. ;; If title is nil, this means this function is called to close
  24128. ;; all levels, so the rest is done only if title is given
  24129. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24130. (setq title (replace-match
  24131. (if org-export-with-tags
  24132. (save-match-data
  24133. (concat
  24134. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24135. (mapconcat 'identity (org-split-string
  24136. (match-string 1 title) ":")
  24137. "&nbsp;")
  24138. "</span>"))
  24139. "")
  24140. t t title)))
  24141. (if (> level umax)
  24142. (progn
  24143. (if (aref org-levels-open (1- level))
  24144. (progn
  24145. (org-close-li)
  24146. (insert "<li>" title "<br/>\n"))
  24147. (aset org-levels-open (1- level) t)
  24148. (org-close-par-maybe)
  24149. (insert "<ul>\n<li>" title "<br/>\n")))
  24150. (aset org-levels-open (1- level) t)
  24151. (if (and org-export-with-section-numbers (not body-only))
  24152. (setq title (concat (org-section-number level) " " title)))
  24153. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24154. (if with-toc
  24155. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24156. level level head-count title level))
  24157. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24158. (org-open-par)))))
  24159. (defun org-html-level-close (level max-outline-level)
  24160. "Terminate one level in HTML export."
  24161. (if (<= level max-outline-level)
  24162. (insert "</div>\n")
  24163. (org-close-li)
  24164. (insert "</ul>\n")))
  24165. ;;; iCalendar export
  24166. ;;;###autoload
  24167. (defun org-export-icalendar-this-file ()
  24168. "Export current file as an iCalendar file.
  24169. The iCalendar file will be located in the same directory as the Org-mode
  24170. file, but with extension `.ics'."
  24171. (interactive)
  24172. (org-export-icalendar nil buffer-file-name))
  24173. ;;;###autoload
  24174. (defun org-export-icalendar-all-agenda-files ()
  24175. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24176. Each iCalendar file will be located in the same directory as the Org-mode
  24177. file, but with extension `.ics'."
  24178. (interactive)
  24179. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24180. ;;;###autoload
  24181. (defun org-export-icalendar-combine-agenda-files ()
  24182. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24183. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24184. (interactive)
  24185. (apply 'org-export-icalendar t (org-agenda-files t)))
  24186. (defun org-export-icalendar (combine &rest files)
  24187. "Create iCalendar files for all elements of FILES.
  24188. If COMBINE is non-nil, combine all calendar entries into a single large
  24189. file and store it under the name `org-combined-agenda-icalendar-file'."
  24190. (save-excursion
  24191. (org-prepare-agenda-buffers files)
  24192. (let* ((dir (org-export-directory
  24193. :ical (list :publishing-directory
  24194. org-export-publishing-directory)))
  24195. file ical-file ical-buffer category started org-agenda-new-buffers)
  24196. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24197. (when combine
  24198. (setq ical-file
  24199. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24200. org-combined-agenda-icalendar-file
  24201. (expand-file-name org-combined-agenda-icalendar-file dir))
  24202. ical-buffer (org-get-agenda-file-buffer ical-file))
  24203. (set-buffer ical-buffer) (erase-buffer))
  24204. (while (setq file (pop files))
  24205. (catch 'nextfile
  24206. (org-check-agenda-file file)
  24207. (set-buffer (org-get-agenda-file-buffer file))
  24208. (unless combine
  24209. (setq ical-file (concat (file-name-as-directory dir)
  24210. (file-name-sans-extension
  24211. (file-name-nondirectory buffer-file-name))
  24212. ".ics"))
  24213. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24214. (with-current-buffer ical-buffer (erase-buffer)))
  24215. (setq category (or org-category
  24216. (file-name-sans-extension
  24217. (file-name-nondirectory buffer-file-name))))
  24218. (if (symbolp category) (setq category (symbol-name category)))
  24219. (let ((standard-output ical-buffer))
  24220. (if combine
  24221. (and (not started) (setq started t)
  24222. (org-start-icalendar-file org-icalendar-combined-name))
  24223. (org-start-icalendar-file category))
  24224. (org-print-icalendar-entries combine)
  24225. (when (or (and combine (not files)) (not combine))
  24226. (org-finish-icalendar-file)
  24227. (set-buffer ical-buffer)
  24228. (save-buffer)
  24229. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24230. (org-release-buffers org-agenda-new-buffers))))
  24231. (defvar org-after-save-iCalendar-file-hook nil
  24232. "Hook run after an iCalendar file has been saved.
  24233. The iCalendar buffer is still current when this hook is run.
  24234. A good way to use this is to tell a desktop calenndar application to re-read
  24235. the iCalendar file.")
  24236. (defun org-print-icalendar-entries (&optional combine)
  24237. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24238. When COMBINE is non nil, add the category to each line."
  24239. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24240. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24241. (dts (org-ical-ts-to-string
  24242. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24243. "DTSTART"))
  24244. hd ts ts2 state status (inc t) pos b sexp rrule
  24245. scheduledp deadlinep tmp pri category entry location summary desc
  24246. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24247. (org-refresh-category-properties)
  24248. (save-excursion
  24249. (goto-char (point-min))
  24250. (while (re-search-forward re1 nil t)
  24251. (catch :skip
  24252. (org-agenda-skip)
  24253. (setq pos (match-beginning 0)
  24254. ts (match-string 0)
  24255. inc t
  24256. hd (org-get-heading)
  24257. summary (org-icalendar-cleanup-string
  24258. (org-entry-get nil "SUMMARY"))
  24259. desc (org-icalendar-cleanup-string
  24260. (or (org-entry-get nil "DESCRIPTION")
  24261. (and org-icalendar-include-body (org-get-entry)))
  24262. t org-icalendar-include-body)
  24263. location (org-icalendar-cleanup-string
  24264. (org-entry-get nil "LOCATION"))
  24265. category (org-get-category))
  24266. (if (looking-at re2)
  24267. (progn
  24268. (goto-char (match-end 0))
  24269. (setq ts2 (match-string 1) inc nil))
  24270. (setq tmp (buffer-substring (max (point-min)
  24271. (- pos org-ds-keyword-length))
  24272. pos)
  24273. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24274. (progn
  24275. (setq inc nil)
  24276. (replace-match "\\1" t nil ts))
  24277. ts)
  24278. deadlinep (string-match org-deadline-regexp tmp)
  24279. scheduledp (string-match org-scheduled-regexp tmp)
  24280. ;; donep (org-entry-is-done-p)
  24281. ))
  24282. (if (or (string-match org-tr-regexp hd)
  24283. (string-match org-ts-regexp hd))
  24284. (setq hd (replace-match "" t t hd)))
  24285. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24286. (setq rrule
  24287. (concat "\nRRULE:FREQ="
  24288. (cdr (assoc
  24289. (match-string 2 ts)
  24290. '(("d" . "DAILY")("w" . "WEEKLY")
  24291. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24292. ";INTERVAL=" (match-string 1 ts)))
  24293. (setq rrule ""))
  24294. (setq summary (or summary hd))
  24295. (if (string-match org-bracket-link-regexp summary)
  24296. (setq summary
  24297. (replace-match (if (match-end 3)
  24298. (match-string 3 summary)
  24299. (match-string 1 summary))
  24300. t t summary)))
  24301. (if deadlinep (setq summary (concat "DL: " summary)))
  24302. (if scheduledp (setq summary (concat "S: " summary)))
  24303. (if (string-match "\\`<%%" ts)
  24304. (with-current-buffer sexp-buffer
  24305. (insert (substring ts 1 -1) " " summary "\n"))
  24306. (princ (format "BEGIN:VEVENT
  24307. %s
  24308. %s%s
  24309. SUMMARY:%s%s%s
  24310. CATEGORIES:%s
  24311. END:VEVENT\n"
  24312. (org-ical-ts-to-string ts "DTSTART")
  24313. (org-ical-ts-to-string ts2 "DTEND" inc)
  24314. rrule summary
  24315. (if (and desc (string-match "\\S-" desc))
  24316. (concat "\nDESCRIPTION: " desc) "")
  24317. (if (and location (string-match "\\S-" location))
  24318. (concat "\nLOCATION: " location) "")
  24319. category)))))
  24320. (when (and org-icalendar-include-sexps
  24321. (condition-case nil (require 'icalendar) (error nil))
  24322. (fboundp 'icalendar-export-region))
  24323. ;; Get all the literal sexps
  24324. (goto-char (point-min))
  24325. (while (re-search-forward "^&?%%(" nil t)
  24326. (catch :skip
  24327. (org-agenda-skip)
  24328. (setq b (match-beginning 0))
  24329. (goto-char (1- (match-end 0)))
  24330. (forward-sexp 1)
  24331. (end-of-line 1)
  24332. (setq sexp (buffer-substring b (point)))
  24333. (with-current-buffer sexp-buffer
  24334. (insert sexp "\n"))
  24335. (princ (org-diary-to-ical-string sexp-buffer)))))
  24336. (when org-icalendar-include-todo
  24337. (goto-char (point-min))
  24338. (while (re-search-forward org-todo-line-regexp nil t)
  24339. (catch :skip
  24340. (org-agenda-skip)
  24341. (setq state (match-string 2))
  24342. (setq status (if (member state org-done-keywords)
  24343. "COMPLETED" "NEEDS-ACTION"))
  24344. (when (and state
  24345. (or (not (member state org-done-keywords))
  24346. (eq org-icalendar-include-todo 'all))
  24347. (not (member org-archive-tag (org-get-tags-at)))
  24348. )
  24349. (setq hd (match-string 3)
  24350. summary (org-icalendar-cleanup-string
  24351. (org-entry-get nil "SUMMARY"))
  24352. desc (org-icalendar-cleanup-string
  24353. (or (org-entry-get nil "DESCRIPTION")
  24354. (and org-icalendar-include-body (org-get-entry)))
  24355. t org-icalendar-include-body)
  24356. location (org-icalendar-cleanup-string
  24357. (org-entry-get nil "LOCATION")))
  24358. (if (string-match org-bracket-link-regexp hd)
  24359. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24360. (match-string 1 hd))
  24361. t t hd)))
  24362. (if (string-match org-priority-regexp hd)
  24363. (setq pri (string-to-char (match-string 2 hd))
  24364. hd (concat (substring hd 0 (match-beginning 1))
  24365. (substring hd (match-end 1))))
  24366. (setq pri org-default-priority))
  24367. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24368. (- org-lowest-priority org-highest-priority))))))
  24369. (princ (format "BEGIN:VTODO
  24370. %s
  24371. SUMMARY:%s%s%s
  24372. CATEGORIES:%s
  24373. SEQUENCE:1
  24374. PRIORITY:%d
  24375. STATUS:%s
  24376. END:VTODO\n"
  24377. dts
  24378. (or summary hd)
  24379. (if (and location (string-match "\\S-" location))
  24380. (concat "\nLOCATION: " location) "")
  24381. (if (and desc (string-match "\\S-" desc))
  24382. (concat "\nDESCRIPTION: " desc) "")
  24383. category pri status)))))))))
  24384. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24385. "Take out stuff and quote what needs to be quoted.
  24386. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24387. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24388. characters."
  24389. (if (not s)
  24390. nil
  24391. (when is-body
  24392. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24393. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24394. (while (string-match re s) (setq s (replace-match "" t t s)))
  24395. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24396. (let ((start 0))
  24397. (while (string-match "\\([,;\\]\\)" s start)
  24398. (setq start (+ (match-beginning 0) 2)
  24399. s (replace-match "\\\\\\1" nil nil s))))
  24400. (when is-body
  24401. (while (string-match "[ \t]*\n[ \t]*" s)
  24402. (setq s (replace-match "\\n" t t s))))
  24403. (setq s (org-trim s))
  24404. (if is-body
  24405. (if maxlength
  24406. (if (and (numberp maxlength)
  24407. (> (length s) maxlength))
  24408. (setq s (substring s 0 maxlength)))))
  24409. s))
  24410. (defun org-get-entry ()
  24411. "Clean-up description string."
  24412. (save-excursion
  24413. (org-back-to-heading t)
  24414. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24415. (defun org-start-icalendar-file (name)
  24416. "Start an iCalendar file by inserting the header."
  24417. (let ((user user-full-name)
  24418. (name (or name "unknown"))
  24419. (timezone (cadr (current-time-zone))))
  24420. (princ
  24421. (format "BEGIN:VCALENDAR
  24422. VERSION:2.0
  24423. X-WR-CALNAME:%s
  24424. PRODID:-//%s//Emacs with Org-mode//EN
  24425. X-WR-TIMEZONE:%s
  24426. CALSCALE:GREGORIAN\n" name user timezone))))
  24427. (defun org-finish-icalendar-file ()
  24428. "Finish an iCalendar file by inserting the END statement."
  24429. (princ "END:VCALENDAR\n"))
  24430. (defun org-ical-ts-to-string (s keyword &optional inc)
  24431. "Take a time string S and convert it to iCalendar format.
  24432. KEYWORD is added in front, to make a complete line like DTSTART....
  24433. When INC is non-nil, increase the hour by two (if time string contains
  24434. a time), or the day by one (if it does not contain a time)."
  24435. (let ((t1 (org-parse-time-string s 'nodefault))
  24436. t2 fmt have-time time)
  24437. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24438. (setq t2 t1 have-time t)
  24439. (setq t2 (org-parse-time-string s)))
  24440. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24441. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24442. (when inc
  24443. (if have-time
  24444. (if org-agenda-default-appointment-duration
  24445. (setq mi (+ org-agenda-default-appointment-duration mi))
  24446. (setq h (+ 2 h)))
  24447. (setq d (1+ d))))
  24448. (setq time (encode-time s mi h d m y)))
  24449. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24450. (concat keyword (format-time-string fmt time))))
  24451. ;;; XOXO export
  24452. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24453. (with-current-buffer buffer
  24454. (apply 'insert output)))
  24455. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24456. (defun org-export-as-xoxo (&optional buffer)
  24457. "Export the org buffer as XOXO.
  24458. The XOXO buffer is named *xoxo-<source buffer name>*"
  24459. (interactive (list (current-buffer)))
  24460. ;; A quickie abstraction
  24461. ;; Output everything as XOXO
  24462. (with-current-buffer (get-buffer buffer)
  24463. (let* ((pos (point))
  24464. (opt-plist (org-combine-plists (org-default-export-plist)
  24465. (org-infile-export-plist)))
  24466. (filename (concat (file-name-as-directory
  24467. (org-export-directory :xoxo opt-plist))
  24468. (file-name-sans-extension
  24469. (file-name-nondirectory buffer-file-name))
  24470. ".html"))
  24471. (out (find-file-noselect filename))
  24472. (last-level 1)
  24473. (hanging-li nil))
  24474. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24475. ;; Check the output buffer is empty.
  24476. (with-current-buffer out (erase-buffer))
  24477. ;; Kick off the output
  24478. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24479. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24480. (let* ((hd (match-string-no-properties 1))
  24481. (level (length hd))
  24482. (text (concat
  24483. (match-string-no-properties 2)
  24484. (save-excursion
  24485. (goto-char (match-end 0))
  24486. (let ((str ""))
  24487. (catch 'loop
  24488. (while 't
  24489. (forward-line)
  24490. (if (looking-at "^[ \t]\\(.*\\)")
  24491. (setq str (concat str (match-string-no-properties 1)))
  24492. (throw 'loop str)))))))))
  24493. ;; Handle level rendering
  24494. (cond
  24495. ((> level last-level)
  24496. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24497. ((< level last-level)
  24498. (dotimes (- (- last-level level) 1)
  24499. (if hanging-li
  24500. (org-export-as-xoxo-insert-into out "</li>\n"))
  24501. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24502. (when hanging-li
  24503. (org-export-as-xoxo-insert-into out "</li>\n")
  24504. (setq hanging-li nil)))
  24505. ((equal level last-level)
  24506. (if hanging-li
  24507. (org-export-as-xoxo-insert-into out "</li>\n")))
  24508. )
  24509. (setq last-level level)
  24510. ;; And output the new li
  24511. (setq hanging-li 't)
  24512. (if (equal ?+ (elt text 0))
  24513. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24514. (org-export-as-xoxo-insert-into out "<li>" text))))
  24515. ;; Finally finish off the ol
  24516. (dotimes (- last-level 1)
  24517. (if hanging-li
  24518. (org-export-as-xoxo-insert-into out "</li>\n"))
  24519. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24520. (goto-char pos)
  24521. ;; Finish the buffer off and clean it up.
  24522. (switch-to-buffer-other-window out)
  24523. (indent-region (point-min) (point-max) nil)
  24524. (save-buffer)
  24525. (goto-char (point-min))
  24526. )))
  24527. ;;;; Key bindings
  24528. ;; Make `C-c C-x' a prefix key
  24529. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24530. ;; TAB key with modifiers
  24531. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24532. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24533. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24534. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24535. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24536. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24537. ;; The following line is necessary under Suse GNU/Linux
  24538. (unless (featurep 'xemacs)
  24539. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24540. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24541. (define-key org-mode-map [backtab] 'org-shifttab)
  24542. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24543. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24544. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24545. ;; Cursor keys with modifiers
  24546. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24547. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24548. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24549. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24550. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24551. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24552. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24553. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24554. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24555. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24556. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24557. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24558. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24559. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24560. ;;; Extra keys for tty access.
  24561. ;; We only set them when really needed because otherwise the
  24562. ;; menus don't show the simple keys
  24563. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24564. (not window-system))
  24565. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24566. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24567. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24568. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24569. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24570. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24571. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24572. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24573. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24574. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24575. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24576. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24577. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24578. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24579. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24580. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24581. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24582. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24583. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24584. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24585. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24586. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24587. ;; All the other keys
  24588. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24589. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24590. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24591. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24592. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24593. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24594. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24595. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24596. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24597. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24598. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24599. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24600. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24601. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24602. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24603. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24604. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24605. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24606. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24607. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24608. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24609. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24610. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24611. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24612. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24613. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24614. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24615. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24616. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24617. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24618. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24619. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24620. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24621. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24622. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24623. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24624. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24625. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24626. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24627. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24628. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24629. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24630. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24631. (org-defkey org-mode-map "\C-m" 'org-return)
  24632. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24633. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24634. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24635. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24636. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24637. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24638. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24639. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24640. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24641. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24642. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24643. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24644. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24645. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24646. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24647. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24648. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24649. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24650. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24651. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24652. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24653. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24654. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24655. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24656. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24657. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24658. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24659. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24660. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24661. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24662. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24663. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24664. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24665. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24666. (when (featurep 'xemacs)
  24667. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24668. (defsubst org-table-p () (org-at-table-p))
  24669. (defun org-self-insert-command (N)
  24670. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24671. If the cursor is in a table looking at whitespace, the whitespace is
  24672. overwritten, and the table is not marked as requiring realignment."
  24673. (interactive "p")
  24674. (if (and (org-table-p)
  24675. (progn
  24676. ;; check if we blank the field, and if that triggers align
  24677. (and org-table-auto-blank-field
  24678. (member last-command
  24679. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24680. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24681. ;; got extra space, this field does not determine column width
  24682. (let (org-table-may-need-update) (org-table-blank-field))
  24683. ;; no extra space, this field may determine column width
  24684. (org-table-blank-field)))
  24685. t)
  24686. (eq N 1)
  24687. (looking-at "[^|\n]* |"))
  24688. (let (org-table-may-need-update)
  24689. (goto-char (1- (match-end 0)))
  24690. (delete-backward-char 1)
  24691. (goto-char (match-beginning 0))
  24692. (self-insert-command N))
  24693. (setq org-table-may-need-update t)
  24694. (self-insert-command N)
  24695. (org-fix-tags-on-the-fly)))
  24696. (defun org-fix-tags-on-the-fly ()
  24697. (when (and (equal (char-after (point-at-bol)) ?*)
  24698. (org-on-heading-p))
  24699. (org-align-tags-here org-tags-column)))
  24700. (defun org-delete-backward-char (N)
  24701. "Like `delete-backward-char', insert whitespace at field end in tables.
  24702. When deleting backwards, in tables this function will insert whitespace in
  24703. front of the next \"|\" separator, to keep the table aligned. The table will
  24704. still be marked for re-alignment if the field did fill the entire column,
  24705. because, in this case the deletion might narrow the column."
  24706. (interactive "p")
  24707. (if (and (org-table-p)
  24708. (eq N 1)
  24709. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24710. (looking-at ".*?|"))
  24711. (let ((pos (point))
  24712. (noalign (looking-at "[^|\n\r]* |"))
  24713. (c org-table-may-need-update))
  24714. (backward-delete-char N)
  24715. (skip-chars-forward "^|")
  24716. (insert " ")
  24717. (goto-char (1- pos))
  24718. ;; noalign: if there were two spaces at the end, this field
  24719. ;; does not determine the width of the column.
  24720. (if noalign (setq org-table-may-need-update c)))
  24721. (backward-delete-char N)
  24722. (org-fix-tags-on-the-fly)))
  24723. (defun org-delete-char (N)
  24724. "Like `delete-char', but insert whitespace at field end in tables.
  24725. When deleting characters, in tables this function will insert whitespace in
  24726. front of the next \"|\" separator, to keep the table aligned. The table will
  24727. still be marked for re-alignment if the field did fill the entire column,
  24728. because, in this case the deletion might narrow the column."
  24729. (interactive "p")
  24730. (if (and (org-table-p)
  24731. (not (bolp))
  24732. (not (= (char-after) ?|))
  24733. (eq N 1))
  24734. (if (looking-at ".*?|")
  24735. (let ((pos (point))
  24736. (noalign (looking-at "[^|\n\r]* |"))
  24737. (c org-table-may-need-update))
  24738. (replace-match (concat
  24739. (substring (match-string 0) 1 -1)
  24740. " |"))
  24741. (goto-char pos)
  24742. ;; noalign: if there were two spaces at the end, this field
  24743. ;; does not determine the width of the column.
  24744. (if noalign (setq org-table-may-need-update c)))
  24745. (delete-char N))
  24746. (delete-char N)
  24747. (org-fix-tags-on-the-fly)))
  24748. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24749. (put 'org-self-insert-command 'delete-selection t)
  24750. (put 'orgtbl-self-insert-command 'delete-selection t)
  24751. (put 'org-delete-char 'delete-selection 'supersede)
  24752. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24753. ;; Make `flyspell-mode' delay after some commands
  24754. (put 'org-self-insert-command 'flyspell-delayed t)
  24755. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24756. (put 'org-delete-char 'flyspell-delayed t)
  24757. (put 'org-delete-backward-char 'flyspell-delayed t)
  24758. ;; Make pabbrev-mode expand after org-mode commands
  24759. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24760. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24761. ;; How to do this: Measure non-white length of current string
  24762. ;; If equal to column width, we should realign.
  24763. (defun org-remap (map &rest commands)
  24764. "In MAP, remap the functions given in COMMANDS.
  24765. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24766. (let (new old)
  24767. (while commands
  24768. (setq old (pop commands) new (pop commands))
  24769. (if (fboundp 'command-remapping)
  24770. (org-defkey map (vector 'remap old) new)
  24771. (substitute-key-definition old new map global-map)))))
  24772. (when (eq org-enable-table-editor 'optimized)
  24773. ;; If the user wants maximum table support, we need to hijack
  24774. ;; some standard editing functions
  24775. (org-remap org-mode-map
  24776. 'self-insert-command 'org-self-insert-command
  24777. 'delete-char 'org-delete-char
  24778. 'delete-backward-char 'org-delete-backward-char)
  24779. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24780. (defun org-shiftcursor-error ()
  24781. "Throw an error because Shift-Cursor command was applied in wrong context."
  24782. (error "This command is active in special context like tables, headlines or timestamps"))
  24783. (defun org-shifttab (&optional arg)
  24784. "Global visibility cycling or move to previous table field.
  24785. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24786. on context.
  24787. See the individual commands for more information."
  24788. (interactive "P")
  24789. (cond
  24790. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24791. (arg (message "Content view to level: ")
  24792. (org-content (prefix-numeric-value arg))
  24793. (setq org-cycle-global-status 'overview))
  24794. (t (call-interactively 'org-global-cycle))))
  24795. (defun org-shiftmetaleft ()
  24796. "Promote subtree or delete table column.
  24797. Calls `org-promote-subtree', `org-outdent-item',
  24798. or `org-table-delete-column', depending on context.
  24799. See the individual commands for more information."
  24800. (interactive)
  24801. (cond
  24802. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24803. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24804. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24805. (t (org-shiftcursor-error))))
  24806. (defun org-shiftmetaright ()
  24807. "Demote subtree or insert table column.
  24808. Calls `org-demote-subtree', `org-indent-item',
  24809. or `org-table-insert-column', depending on context.
  24810. See the individual commands for more information."
  24811. (interactive)
  24812. (cond
  24813. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24814. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24815. ((org-at-item-p) (call-interactively 'org-indent-item))
  24816. (t (org-shiftcursor-error))))
  24817. (defun org-shiftmetaup (&optional arg)
  24818. "Move subtree up or kill table row.
  24819. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24820. `org-move-item-up' depending on context. See the individual commands
  24821. for more information."
  24822. (interactive "P")
  24823. (cond
  24824. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24825. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24826. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24827. (t (org-shiftcursor-error))))
  24828. (defun org-shiftmetadown (&optional arg)
  24829. "Move subtree down or insert table row.
  24830. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24831. `org-move-item-down', depending on context. See the individual
  24832. commands for more information."
  24833. (interactive "P")
  24834. (cond
  24835. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24836. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24837. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24838. (t (org-shiftcursor-error))))
  24839. (defun org-metaleft (&optional arg)
  24840. "Promote heading or move table column to left.
  24841. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24842. With no specific context, calls the Emacs default `backward-word'.
  24843. See the individual commands for more information."
  24844. (interactive "P")
  24845. (cond
  24846. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24847. ((or (org-on-heading-p) (org-region-active-p))
  24848. (call-interactively 'org-do-promote))
  24849. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24850. (t (call-interactively 'backward-word))))
  24851. (defun org-metaright (&optional arg)
  24852. "Demote subtree or move table column to right.
  24853. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24854. With no specific context, calls the Emacs default `forward-word'.
  24855. See the individual commands for more information."
  24856. (interactive "P")
  24857. (cond
  24858. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24859. ((or (org-on-heading-p) (org-region-active-p))
  24860. (call-interactively 'org-do-demote))
  24861. ((org-at-item-p) (call-interactively 'org-indent-item))
  24862. (t (call-interactively 'forward-word))))
  24863. (defun org-metaup (&optional arg)
  24864. "Move subtree up or move table row up.
  24865. Calls `org-move-subtree-up' or `org-table-move-row' or
  24866. `org-move-item-up', depending on context. See the individual commands
  24867. for more information."
  24868. (interactive "P")
  24869. (cond
  24870. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24871. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24872. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24873. (t (transpose-lines 1) (beginning-of-line -1))))
  24874. (defun org-metadown (&optional arg)
  24875. "Move subtree down or move table row down.
  24876. Calls `org-move-subtree-down' or `org-table-move-row' or
  24877. `org-move-item-down', depending on context. See the individual
  24878. commands for more information."
  24879. (interactive "P")
  24880. (cond
  24881. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24882. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24883. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24884. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24885. (defun org-shiftup (&optional arg)
  24886. "Increase item in timestamp or increase priority of current headline.
  24887. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24888. depending on context. See the individual commands for more information."
  24889. (interactive "P")
  24890. (cond
  24891. ((org-at-timestamp-p t)
  24892. (call-interactively (if org-edit-timestamp-down-means-later
  24893. 'org-timestamp-down 'org-timestamp-up)))
  24894. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24895. ((org-at-item-p) (call-interactively 'org-previous-item))
  24896. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24897. (defun org-shiftdown (&optional arg)
  24898. "Decrease item in timestamp or decrease priority of current headline.
  24899. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24900. depending on context. See the individual commands for more information."
  24901. (interactive "P")
  24902. (cond
  24903. ((org-at-timestamp-p t)
  24904. (call-interactively (if org-edit-timestamp-down-means-later
  24905. 'org-timestamp-up 'org-timestamp-down)))
  24906. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24907. (t (call-interactively 'org-next-item))))
  24908. (defun org-shiftright ()
  24909. "Next TODO keyword or timestamp one day later, depending on context."
  24910. (interactive)
  24911. (cond
  24912. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24913. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24914. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24915. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24916. (t (org-shiftcursor-error))))
  24917. (defun org-shiftleft ()
  24918. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24919. (interactive)
  24920. (cond
  24921. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24922. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24923. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24924. ((org-at-property-p)
  24925. (call-interactively 'org-property-previous-allowed-value))
  24926. (t (org-shiftcursor-error))))
  24927. (defun org-shiftcontrolright ()
  24928. "Switch to next TODO set."
  24929. (interactive)
  24930. (cond
  24931. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24932. (t (org-shiftcursor-error))))
  24933. (defun org-shiftcontrolleft ()
  24934. "Switch to previous TODO set."
  24935. (interactive)
  24936. (cond
  24937. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24938. (t (org-shiftcursor-error))))
  24939. (defun org-ctrl-c-ret ()
  24940. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24941. (interactive)
  24942. (cond
  24943. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24944. (t (call-interactively 'org-insert-heading))))
  24945. (defun org-copy-special ()
  24946. "Copy region in table or copy current subtree.
  24947. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24948. See the individual commands for more information."
  24949. (interactive)
  24950. (call-interactively
  24951. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24952. (defun org-cut-special ()
  24953. "Cut region in table or cut current subtree.
  24954. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24955. See the individual commands for more information."
  24956. (interactive)
  24957. (call-interactively
  24958. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24959. (defun org-paste-special (arg)
  24960. "Paste rectangular region into table, or past subtree relative to level.
  24961. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24962. See the individual commands for more information."
  24963. (interactive "P")
  24964. (if (org-at-table-p)
  24965. (org-table-paste-rectangle)
  24966. (org-paste-subtree arg)))
  24967. (defun org-ctrl-c-ctrl-c (&optional arg)
  24968. "Set tags in headline, or update according to changed information at point.
  24969. This command does many different things, depending on context:
  24970. - If the cursor is in a headline, prompt for tags and insert them
  24971. into the current line, aligned to `org-tags-column'. When called
  24972. with prefix arg, realign all tags in the current buffer.
  24973. - If the cursor is in one of the special #+KEYWORD lines, this
  24974. triggers scanning the buffer for these lines and updating the
  24975. information.
  24976. - If the cursor is inside a table, realign the table. This command
  24977. works even if the automatic table editor has been turned off.
  24978. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24979. the entire table.
  24980. - If the cursor is a the beginning of a dynamic block, update it.
  24981. - If the cursor is inside a table created by the table.el package,
  24982. activate that table.
  24983. - If the current buffer is a remember buffer, close note and file it.
  24984. with a prefix argument, file it without further interaction to the default
  24985. location.
  24986. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24987. links in this buffer.
  24988. - If the cursor is on a numbered item in a plain list, renumber the
  24989. ordered list.
  24990. - If the cursor is on a checkbox, toggle it."
  24991. (interactive "P")
  24992. (let ((org-enable-table-editor t))
  24993. (cond
  24994. ((or org-clock-overlays
  24995. org-occur-highlights
  24996. org-latex-fragment-image-overlays)
  24997. (org-remove-clock-overlays)
  24998. (org-remove-occur-highlights)
  24999. (org-remove-latex-fragment-image-overlays)
  25000. (message "Temporary highlights/overlays removed from current buffer"))
  25001. ((and (local-variable-p 'org-finish-function (current-buffer))
  25002. (fboundp org-finish-function))
  25003. (funcall org-finish-function))
  25004. ((org-at-property-p)
  25005. (call-interactively 'org-property-action))
  25006. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25007. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25008. ((org-at-table.el-p)
  25009. (require 'table)
  25010. (beginning-of-line 1)
  25011. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25012. (call-interactively 'table-recognize-table))
  25013. ((org-at-table-p)
  25014. (org-table-maybe-eval-formula)
  25015. (if arg
  25016. (call-interactively 'org-table-recalculate)
  25017. (org-table-maybe-recalculate-line))
  25018. (call-interactively 'org-table-align))
  25019. ((org-at-item-checkbox-p)
  25020. (call-interactively 'org-toggle-checkbox))
  25021. ((org-at-item-p)
  25022. (call-interactively 'org-maybe-renumber-ordered-list))
  25023. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25024. ;; Dynamic block
  25025. (beginning-of-line 1)
  25026. (org-update-dblock))
  25027. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25028. (cond
  25029. ((equal (match-string 1) "TBLFM")
  25030. ;; Recalculate the table before this line
  25031. (save-excursion
  25032. (beginning-of-line 1)
  25033. (skip-chars-backward " \r\n\t")
  25034. (if (org-at-table-p)
  25035. (org-call-with-arg 'org-table-recalculate t))))
  25036. (t
  25037. (call-interactively 'org-mode-restart))))
  25038. (t (error "C-c C-c can do nothing useful at this location.")))))
  25039. (defun org-mode-restart ()
  25040. "Restart Org-mode, to scan again for special lines.
  25041. Also updates the keyword regular expressions."
  25042. (interactive)
  25043. (let ((org-inhibit-startup t)) (org-mode))
  25044. (message "Org-mode restarted to refresh keyword and special line setup"))
  25045. (defun org-kill-note-or-show-branches ()
  25046. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25047. (interactive)
  25048. (if (not org-finish-function)
  25049. (call-interactively 'show-branches)
  25050. (let ((org-note-abort t))
  25051. (funcall org-finish-function))))
  25052. (defun org-return (&optional indent)
  25053. "Goto next table row or insert a newline.
  25054. Calls `org-table-next-row' or `newline', depending on context.
  25055. See the individual commands for more information."
  25056. (interactive)
  25057. (cond
  25058. ((bobp) (if indent (newline-and-indent) (newline)))
  25059. ((and (org-at-heading-p)
  25060. (looking-at
  25061. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25062. (org-show-entry)
  25063. (end-of-line 1)
  25064. (newline))
  25065. ((org-at-table-p)
  25066. (org-table-justify-field-maybe)
  25067. (call-interactively 'org-table-next-row))
  25068. (t (if indent (newline-and-indent) (newline)))))
  25069. (defun org-return-indent ()
  25070. (interactive)
  25071. "Goto next table row or insert a newline and indent.
  25072. Calls `org-table-next-row' or `newline-and-indent', depending on
  25073. context. See the individual commands for more information."
  25074. (org-return t))
  25075. (defun org-ctrl-c-minus ()
  25076. "Insert separator line in table or modify bullet type in list.
  25077. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25078. depending on context."
  25079. (interactive)
  25080. (cond
  25081. ((org-at-table-p)
  25082. (call-interactively 'org-table-insert-hline))
  25083. ((org-on-heading-p)
  25084. ;; Convert to item
  25085. (save-excursion
  25086. (beginning-of-line 1)
  25087. (if (looking-at "\\*+ ")
  25088. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25089. ((org-in-item-p)
  25090. (call-interactively 'org-cycle-list-bullet))
  25091. (t (error "`C-c -' does have no function here."))))
  25092. (defun org-meta-return (&optional arg)
  25093. "Insert a new heading or wrap a region in a table.
  25094. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25095. See the individual commands for more information."
  25096. (interactive "P")
  25097. (cond
  25098. ((org-at-table-p)
  25099. (call-interactively 'org-table-wrap-region))
  25100. (t (call-interactively 'org-insert-heading))))
  25101. ;;; Menu entries
  25102. ;; Define the Org-mode menus
  25103. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25104. '("Tbl"
  25105. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25106. ["Next Field" org-cycle (org-at-table-p)]
  25107. ["Previous Field" org-shifttab (org-at-table-p)]
  25108. ["Next Row" org-return (org-at-table-p)]
  25109. "--"
  25110. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25111. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25112. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25113. "--"
  25114. ("Column"
  25115. ["Move Column Left" org-metaleft (org-at-table-p)]
  25116. ["Move Column Right" org-metaright (org-at-table-p)]
  25117. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25118. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25119. ("Row"
  25120. ["Move Row Up" org-metaup (org-at-table-p)]
  25121. ["Move Row Down" org-metadown (org-at-table-p)]
  25122. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25123. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25124. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25125. "--"
  25126. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25127. ("Rectangle"
  25128. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25129. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25130. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25131. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25132. "--"
  25133. ("Calculate"
  25134. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25135. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25136. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25137. "--"
  25138. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25139. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25140. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25141. "--"
  25142. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25143. "--"
  25144. ["Sum Column/Rectangle" org-table-sum
  25145. (or (org-at-table-p) (org-region-active-p))]
  25146. ["Which Column?" org-table-current-column (org-at-table-p)])
  25147. ["Debug Formulas"
  25148. org-table-toggle-formula-debugger
  25149. :style toggle :selected org-table-formula-debug]
  25150. ["Show Col/Row Numbers"
  25151. org-table-toggle-coordinate-overlays
  25152. :style toggle :selected org-table-overlay-coordinates]
  25153. "--"
  25154. ["Create" org-table-create (and (not (org-at-table-p))
  25155. org-enable-table-editor)]
  25156. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25157. ["Import from File" org-table-import (not (org-at-table-p))]
  25158. ["Export to File" org-table-export (org-at-table-p)]
  25159. "--"
  25160. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25161. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25162. '("Org"
  25163. ("Show/Hide"
  25164. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25165. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25166. ["Sparse Tree" org-occur t]
  25167. ["Reveal Context" org-reveal t]
  25168. ["Show All" show-all t]
  25169. "--"
  25170. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25171. "--"
  25172. ["New Heading" org-insert-heading t]
  25173. ("Navigate Headings"
  25174. ["Up" outline-up-heading t]
  25175. ["Next" outline-next-visible-heading t]
  25176. ["Previous" outline-previous-visible-heading t]
  25177. ["Next Same Level" outline-forward-same-level t]
  25178. ["Previous Same Level" outline-backward-same-level t]
  25179. "--"
  25180. ["Jump" org-goto t])
  25181. ("Edit Structure"
  25182. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25183. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25184. "--"
  25185. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25186. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25187. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25188. "--"
  25189. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25190. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25191. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25192. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25193. "--"
  25194. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25195. "--"
  25196. ["Convert to odd levels" org-convert-to-odd-levels t]
  25197. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25198. ("Editing"
  25199. ["Emphasis..." org-emphasize t])
  25200. ("Archive"
  25201. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25202. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25203. ; :active t :keys "C-u C-c C-x C-a"]
  25204. ["Sparse trees open ARCHIVE trees"
  25205. (setq org-sparse-tree-open-archived-trees
  25206. (not org-sparse-tree-open-archived-trees))
  25207. :style toggle :selected org-sparse-tree-open-archived-trees]
  25208. ["Cycling opens ARCHIVE trees"
  25209. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25210. :style toggle :selected org-cycle-open-archived-trees]
  25211. ["Agenda includes ARCHIVE trees"
  25212. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25213. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25214. "--"
  25215. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25216. ; ["Check and Move Children" (org-archive-subtree '(4))
  25217. ; :active t :keys "C-u C-c C-x C-s"]
  25218. )
  25219. "--"
  25220. ("TODO Lists"
  25221. ["TODO/DONE/-" org-todo t]
  25222. ("Select keyword"
  25223. ["Next keyword" org-shiftright (org-on-heading-p)]
  25224. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25225. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25226. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25227. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25228. ["Show TODO Tree" org-show-todo-tree t]
  25229. ["Global TODO list" org-todo-list t]
  25230. "--"
  25231. ["Set Priority" org-priority t]
  25232. ["Priority Up" org-shiftup t]
  25233. ["Priority Down" org-shiftdown t])
  25234. ("TAGS and Properties"
  25235. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25236. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25237. "--"
  25238. ["Set property" 'org-set-property t]
  25239. ["Column view of properties" org-columns t]
  25240. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25241. ("Dates and Scheduling"
  25242. ["Timestamp" org-time-stamp t]
  25243. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25244. ("Change Date"
  25245. ["1 Day Later" org-shiftright t]
  25246. ["1 Day Earlier" org-shiftleft t]
  25247. ["1 ... Later" org-shiftup t]
  25248. ["1 ... Earlier" org-shiftdown t])
  25249. ["Compute Time Range" org-evaluate-time-range t]
  25250. ["Schedule Item" org-schedule t]
  25251. ["Deadline" org-deadline t]
  25252. "--"
  25253. ["Custom time format" org-toggle-time-stamp-overlays
  25254. :style radio :selected org-display-custom-times]
  25255. "--"
  25256. ["Goto Calendar" org-goto-calendar t]
  25257. ["Date from Calendar" org-date-from-calendar t])
  25258. ("Logging work"
  25259. ["Clock in" org-clock-in t]
  25260. ["Clock out" org-clock-out t]
  25261. ["Clock cancel" org-clock-cancel t]
  25262. ["Goto running clock" org-clock-goto t]
  25263. ["Display times" org-clock-display t]
  25264. ["Create clock table" org-clock-report t]
  25265. "--"
  25266. ["Record DONE time"
  25267. (progn (setq org-log-done (not org-log-done))
  25268. (message "Switching to %s will %s record a timestamp"
  25269. (car org-done-keywords)
  25270. (if org-log-done "automatically" "not")))
  25271. :style toggle :selected org-log-done])
  25272. "--"
  25273. ["Agenda Command..." org-agenda t]
  25274. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25275. ("File List for Agenda")
  25276. ("Special views current file"
  25277. ["TODO Tree" org-show-todo-tree t]
  25278. ["Check Deadlines" org-check-deadlines t]
  25279. ["Timeline" org-timeline t]
  25280. ["Tags Tree" org-tags-sparse-tree t])
  25281. "--"
  25282. ("Hyperlinks"
  25283. ["Store Link (Global)" org-store-link t]
  25284. ["Insert Link" org-insert-link t]
  25285. ["Follow Link" org-open-at-point t]
  25286. "--"
  25287. ["Next link" org-next-link t]
  25288. ["Previous link" org-previous-link t]
  25289. "--"
  25290. ["Descriptive Links"
  25291. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25292. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25293. ["Literal Links"
  25294. (progn
  25295. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25296. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25297. "--"
  25298. ["Export/Publish..." org-export t]
  25299. ("LaTeX"
  25300. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25301. :selected org-cdlatex-mode]
  25302. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25303. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25304. ["Modify math symbol" org-cdlatex-math-modify
  25305. (org-inside-LaTeX-fragment-p)]
  25306. ["Export LaTeX fragments as images"
  25307. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25308. :style toggle :selected org-export-with-LaTeX-fragments])
  25309. "--"
  25310. ("Documentation"
  25311. ["Show Version" org-version t]
  25312. ["Info Documentation" org-info t])
  25313. ("Customize"
  25314. ["Browse Org Group" org-customize t]
  25315. "--"
  25316. ["Expand This Menu" org-create-customize-menu
  25317. (fboundp 'customize-menu-create)])
  25318. "--"
  25319. ["Refresh setup" org-mode-restart t]
  25320. ))
  25321. (defun org-info (&optional node)
  25322. "Read documentation for Org-mode in the info system.
  25323. With optional NODE, go directly to that node."
  25324. (interactive)
  25325. (require 'info)
  25326. (Info-goto-node (format "(org)%s" (or node ""))))
  25327. (defun org-install-agenda-files-menu ()
  25328. (let ((bl (buffer-list)))
  25329. (save-excursion
  25330. (while bl
  25331. (set-buffer (pop bl))
  25332. (if (org-mode-p) (setq bl nil)))
  25333. (when (org-mode-p)
  25334. (easy-menu-change
  25335. '("Org") "File List for Agenda"
  25336. (append
  25337. (list
  25338. ["Edit File List" (org-edit-agenda-file-list) t]
  25339. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25340. ["Remove Current File from List" org-remove-file t]
  25341. ["Cycle through agenda files" org-cycle-agenda-files t]
  25342. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25343. "--")
  25344. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25345. ;;;; Documentation
  25346. (defun org-customize ()
  25347. "Call the customize function with org as argument."
  25348. (interactive)
  25349. (customize-browse 'org))
  25350. (defun org-create-customize-menu ()
  25351. "Create a full customization menu for Org-mode, insert it into the menu."
  25352. (interactive)
  25353. (if (fboundp 'customize-menu-create)
  25354. (progn
  25355. (easy-menu-change
  25356. '("Org") "Customize"
  25357. `(["Browse Org group" org-customize t]
  25358. "--"
  25359. ,(customize-menu-create 'org)
  25360. ["Set" Custom-set t]
  25361. ["Save" Custom-save t]
  25362. ["Reset to Current" Custom-reset-current t]
  25363. ["Reset to Saved" Custom-reset-saved t]
  25364. ["Reset to Standard Settings" Custom-reset-standard t]))
  25365. (message "\"Org\"-menu now contains full customization menu"))
  25366. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25367. ;;;; Miscellaneous stuff
  25368. ;;; Generally useful functions
  25369. (defun org-context ()
  25370. "Return a list of contexts of the current cursor position.
  25371. If several contexts apply, all are returned.
  25372. Each context entry is a list with a symbol naming the context, and
  25373. two positions indicating start and end of the context. Possible
  25374. contexts are:
  25375. :headline anywhere in a headline
  25376. :headline-stars on the leading stars in a headline
  25377. :todo-keyword on a TODO keyword (including DONE) in a headline
  25378. :tags on the TAGS in a headline
  25379. :priority on the priority cookie in a headline
  25380. :item on the first line of a plain list item
  25381. :item-bullet on the bullet/number of a plain list item
  25382. :checkbox on the checkbox in a plain list item
  25383. :table in an org-mode table
  25384. :table-special on a special filed in a table
  25385. :table-table in a table.el table
  25386. :link on a hyperlink
  25387. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25388. :target on a <<target>>
  25389. :radio-target on a <<<radio-target>>>
  25390. :latex-fragment on a LaTeX fragment
  25391. :latex-preview on a LaTeX fragment with overlayed preview image
  25392. This function expects the position to be visible because it uses font-lock
  25393. faces as a help to recognize the following contexts: :table-special, :link,
  25394. and :keyword."
  25395. (let* ((f (get-text-property (point) 'face))
  25396. (faces (if (listp f) f (list f)))
  25397. (p (point)) clist o)
  25398. ;; First the large context
  25399. (cond
  25400. ((org-on-heading-p t)
  25401. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25402. (when (progn
  25403. (beginning-of-line 1)
  25404. (looking-at org-todo-line-tags-regexp))
  25405. (push (org-point-in-group p 1 :headline-stars) clist)
  25406. (push (org-point-in-group p 2 :todo-keyword) clist)
  25407. (push (org-point-in-group p 4 :tags) clist))
  25408. (goto-char p)
  25409. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25410. (if (looking-at "\\[#[A-Z0-9]\\]")
  25411. (push (org-point-in-group p 0 :priority) clist)))
  25412. ((org-at-item-p)
  25413. (push (org-point-in-group p 2 :item-bullet) clist)
  25414. (push (list :item (point-at-bol)
  25415. (save-excursion (org-end-of-item) (point)))
  25416. clist)
  25417. (and (org-at-item-checkbox-p)
  25418. (push (org-point-in-group p 0 :checkbox) clist)))
  25419. ((org-at-table-p)
  25420. (push (list :table (org-table-begin) (org-table-end)) clist)
  25421. (if (memq 'org-formula faces)
  25422. (push (list :table-special
  25423. (previous-single-property-change p 'face)
  25424. (next-single-property-change p 'face)) clist)))
  25425. ((org-at-table-p 'any)
  25426. (push (list :table-table) clist)))
  25427. (goto-char p)
  25428. ;; Now the small context
  25429. (cond
  25430. ((org-at-timestamp-p)
  25431. (push (org-point-in-group p 0 :timestamp) clist))
  25432. ((memq 'org-link faces)
  25433. (push (list :link
  25434. (previous-single-property-change p 'face)
  25435. (next-single-property-change p 'face)) clist))
  25436. ((memq 'org-special-keyword faces)
  25437. (push (list :keyword
  25438. (previous-single-property-change p 'face)
  25439. (next-single-property-change p 'face)) clist))
  25440. ((org-on-target-p)
  25441. (push (org-point-in-group p 0 :target) clist)
  25442. (goto-char (1- (match-beginning 0)))
  25443. (if (looking-at org-radio-target-regexp)
  25444. (push (org-point-in-group p 0 :radio-target) clist))
  25445. (goto-char p))
  25446. ((setq o (car (delq nil
  25447. (mapcar
  25448. (lambda (x)
  25449. (if (memq x org-latex-fragment-image-overlays) x))
  25450. (org-overlays-at (point))))))
  25451. (push (list :latex-fragment
  25452. (org-overlay-start o) (org-overlay-end o)) clist)
  25453. (push (list :latex-preview
  25454. (org-overlay-start o) (org-overlay-end o)) clist))
  25455. ((org-inside-LaTeX-fragment-p)
  25456. ;; FIXME: positions wrong.
  25457. (push (list :latex-fragment (point) (point)) clist)))
  25458. (setq clist (nreverse (delq nil clist)))
  25459. clist))
  25460. ;; FIXME: Compare with at-regexp-p Do we need both?
  25461. (defun org-in-regexp (re &optional nlines visually)
  25462. "Check if point is inside a match of regexp.
  25463. Normally only the current line is checked, but you can include NLINES extra
  25464. lines both before and after point into the search.
  25465. If VISUALLY is set, require that the cursor is not after the match but
  25466. really on, so that the block visually is on the match."
  25467. (catch 'exit
  25468. (let ((pos (point))
  25469. (eol (point-at-eol (+ 1 (or nlines 0))))
  25470. (inc (if visually 1 0)))
  25471. (save-excursion
  25472. (beginning-of-line (- 1 (or nlines 0)))
  25473. (while (re-search-forward re eol t)
  25474. (if (and (<= (match-beginning 0) pos)
  25475. (>= (+ inc (match-end 0)) pos))
  25476. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25477. (defun org-at-regexp-p (regexp)
  25478. "Is point inside a match of REGEXP in the current line?"
  25479. (catch 'exit
  25480. (save-excursion
  25481. (let ((pos (point)) (end (point-at-eol)))
  25482. (beginning-of-line 1)
  25483. (while (re-search-forward regexp end t)
  25484. (if (and (<= (match-beginning 0) pos)
  25485. (>= (match-end 0) pos))
  25486. (throw 'exit t)))
  25487. nil))))
  25488. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25489. "Call `multi-occur' with buffers for all agenda files."
  25490. (interactive "sOrg-files matching: \np")
  25491. (let* ((files (org-agenda-files))
  25492. (tnames (mapcar 'file-truename files))
  25493. (extra org-agenda-multi-occur-extra-files)
  25494. f)
  25495. (while (setq f (pop extra))
  25496. (unless (member (file-truename f) tnames)
  25497. (add-to-list 'files f 'append)
  25498. (add-to-list 'tnames (file-truename f) 'append)))
  25499. (multi-occur
  25500. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25501. regexp)))
  25502. (if (boundp 'occur-mode-find-occurrence-hook)
  25503. ;; Emacs 23
  25504. (add-hook 'occur-mode-find-occurrence-hook
  25505. (lambda ()
  25506. (when (org-mode-p)
  25507. (org-reveal))))
  25508. ;; Emacs 22
  25509. (defadvice occur-mode-goto-occurrence
  25510. (after org-occur-reveal activate)
  25511. (and (org-mode-p) (org-reveal)))
  25512. (defadvice occur-mode-goto-occurrence-other-window
  25513. (after org-occur-reveal activate)
  25514. (and (org-mode-p) (org-reveal)))
  25515. (defadvice occur-mode-display-occurrence
  25516. (after org-occur-reveal activate)
  25517. (when (org-mode-p)
  25518. (let ((pos (occur-mode-find-occurrence)))
  25519. (with-current-buffer (marker-buffer pos)
  25520. (save-excursion
  25521. (goto-char pos)
  25522. (org-reveal)))))))
  25523. (defun org-uniquify (list)
  25524. "Remove duplicate elements from LIST."
  25525. (let (res)
  25526. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25527. res))
  25528. (defun org-delete-all (elts list)
  25529. "Remove all elements in ELTS from LIST."
  25530. (while elts
  25531. (setq list (delete (pop elts) list)))
  25532. list)
  25533. (defun org-back-over-empty-lines ()
  25534. "Move backwards over witespace, to the beginning of the first empty line.
  25535. Returns the number o empty lines passed."
  25536. (let ((pos (point)))
  25537. (skip-chars-backward " \t\n\r")
  25538. (beginning-of-line 2)
  25539. (goto-char (min (point) pos))
  25540. (count-lines (point) pos)))
  25541. (defun org-skip-whitespace ()
  25542. (skip-chars-forward " \t\n\r"))
  25543. (defun org-point-in-group (point group &optional context)
  25544. "Check if POINT is in match-group GROUP.
  25545. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25546. match. If the match group does ot exist or point is not inside it,
  25547. return nil."
  25548. (and (match-beginning group)
  25549. (>= point (match-beginning group))
  25550. (<= point (match-end group))
  25551. (if context
  25552. (list context (match-beginning group) (match-end group))
  25553. t)))
  25554. (defun org-switch-to-buffer-other-window (&rest args)
  25555. "Switch to buffer in a second window on the current frame.
  25556. In particular, do not allow pop-up frames."
  25557. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25558. special-display-function)
  25559. (apply 'switch-to-buffer-other-window args)))
  25560. (defun org-combine-plists (&rest plists)
  25561. "Create a single property list from all plists in PLISTS.
  25562. The process starts by copying the first list, and then setting properties
  25563. from the other lists. Settings in the last list are the most significant
  25564. ones and overrule settings in the other lists."
  25565. (let ((rtn (copy-sequence (pop plists)))
  25566. p v ls)
  25567. (while plists
  25568. (setq ls (pop plists))
  25569. (while ls
  25570. (setq p (pop ls) v (pop ls))
  25571. (setq rtn (plist-put rtn p v))))
  25572. rtn))
  25573. (defun org-move-line-down (arg)
  25574. "Move the current line down. With prefix argument, move it past ARG lines."
  25575. (interactive "p")
  25576. (let ((col (current-column))
  25577. beg end pos)
  25578. (beginning-of-line 1) (setq beg (point))
  25579. (beginning-of-line 2) (setq end (point))
  25580. (beginning-of-line (+ 1 arg))
  25581. (setq pos (move-marker (make-marker) (point)))
  25582. (insert (delete-and-extract-region beg end))
  25583. (goto-char pos)
  25584. (move-to-column col)))
  25585. (defun org-move-line-up (arg)
  25586. "Move the current line up. With prefix argument, move it past ARG lines."
  25587. (interactive "p")
  25588. (let ((col (current-column))
  25589. beg end pos)
  25590. (beginning-of-line 1) (setq beg (point))
  25591. (beginning-of-line 2) (setq end (point))
  25592. (beginning-of-line (- arg))
  25593. (setq pos (move-marker (make-marker) (point)))
  25594. (insert (delete-and-extract-region beg end))
  25595. (goto-char pos)
  25596. (move-to-column col)))
  25597. (defun org-replace-escapes (string table)
  25598. "Replace %-escapes in STRING with values in TABLE.
  25599. TABLE is an association list with keys like \"%a\" and string values.
  25600. The sequences in STRING may contain normal field width and padding information,
  25601. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25602. so values can contain further %-escapes if they are define later in TABLE."
  25603. (let ((case-fold-search nil)
  25604. e re rpl)
  25605. (while (setq e (pop table))
  25606. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25607. (while (string-match re string)
  25608. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25609. (cdr e)))
  25610. (setq string (replace-match rpl t t string))))
  25611. string))
  25612. (defun org-sublist (list start end)
  25613. "Return a section of LIST, from START to END.
  25614. Counting starts at 1."
  25615. (let (rtn (c start))
  25616. (setq list (nthcdr (1- start) list))
  25617. (while (and list (<= c end))
  25618. (push (pop list) rtn)
  25619. (setq c (1+ c)))
  25620. (nreverse rtn)))
  25621. (defun org-find-base-buffer-visiting (file)
  25622. "Like `find-buffer-visiting' but alway return the base buffer and
  25623. not an indirect buffer"
  25624. (let ((buf (find-buffer-visiting file)))
  25625. (if buf
  25626. (or (buffer-base-buffer buf) buf)
  25627. nil)))
  25628. (defun org-image-file-name-regexp ()
  25629. "Return regexp matching the file names of images."
  25630. (if (fboundp 'image-file-name-regexp)
  25631. (image-file-name-regexp)
  25632. (let ((image-file-name-extensions
  25633. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25634. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25635. (concat "\\."
  25636. (regexp-opt (nconc (mapcar 'upcase
  25637. image-file-name-extensions)
  25638. image-file-name-extensions)
  25639. t)
  25640. "\\'"))))
  25641. (defun org-file-image-p (file)
  25642. "Return non-nil if FILE is an image."
  25643. (save-match-data
  25644. (string-match (org-image-file-name-regexp) file)))
  25645. ;;; Paragraph filling stuff.
  25646. ;; We want this to be just right, so use the full arsenal.
  25647. (defun org-indent-line-function ()
  25648. "Indent line like previous, but further if previous was headline or item."
  25649. (interactive)
  25650. (let* ((pos (point))
  25651. (itemp (org-at-item-p))
  25652. column bpos bcol tpos tcol bullet btype bullet-type)
  25653. ;; Find the previous relevant line
  25654. (beginning-of-line 1)
  25655. (cond
  25656. ((looking-at "#") (setq column 0))
  25657. ((looking-at "\\*+ ") (setq column 0))
  25658. (t
  25659. (beginning-of-line 0)
  25660. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25661. (beginning-of-line 0))
  25662. (cond
  25663. ((looking-at "\\*+[ \t]+")
  25664. (goto-char (match-end 0))
  25665. (setq column (current-column)))
  25666. ((org-in-item-p)
  25667. (org-beginning-of-item)
  25668. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25669. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25670. (setq bpos (match-beginning 1) tpos (match-end 0)
  25671. bcol (progn (goto-char bpos) (current-column))
  25672. tcol (progn (goto-char tpos) (current-column))
  25673. bullet (match-string 1)
  25674. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25675. (if (not itemp)
  25676. (setq column tcol)
  25677. (goto-char pos)
  25678. (beginning-of-line 1)
  25679. (if (looking-at "\\S-")
  25680. (progn
  25681. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25682. (setq bullet (match-string 1)
  25683. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25684. (setq column (if (equal btype bullet-type) bcol tcol)))
  25685. (setq column (org-get-indentation)))))
  25686. (t (setq column (org-get-indentation))))))
  25687. (goto-char pos)
  25688. (if (<= (current-column) (current-indentation))
  25689. (indent-line-to column)
  25690. (save-excursion (indent-line-to column)))
  25691. (setq column (current-column))
  25692. (beginning-of-line 1)
  25693. (if (looking-at
  25694. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25695. (replace-match (concat "\\1" (format org-property-format
  25696. (match-string 2) (match-string 3)))
  25697. t nil))
  25698. (move-to-column column)))
  25699. (defun org-set-autofill-regexps ()
  25700. (interactive)
  25701. ;; In the paragraph separator we include headlines, because filling
  25702. ;; text in a line directly attached to a headline would otherwise
  25703. ;; fill the headline as well.
  25704. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25705. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25706. ;; The paragraph starter includes hand-formatted lists.
  25707. (org-set-local 'paragraph-start
  25708. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25709. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25710. ;; But only if the user has not turned off tables or fixed-width regions
  25711. (org-set-local
  25712. 'auto-fill-inhibit-regexp
  25713. (concat "\\*+ \\|#\\+"
  25714. "\\|[ \t]*" org-keyword-time-regexp
  25715. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25716. (concat
  25717. "\\|[ \t]*["
  25718. (if org-enable-table-editor "|" "")
  25719. (if org-enable-fixed-width-editor ":" "")
  25720. "]"))))
  25721. ;; We use our own fill-paragraph function, to make sure that tables
  25722. ;; and fixed-width regions are not wrapped. That function will pass
  25723. ;; through to `fill-paragraph' when appropriate.
  25724. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25725. ; Adaptive filling: To get full control, first make sure that
  25726. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25727. (org-set-local 'adaptive-fill-regexp "\000")
  25728. (org-set-local 'adaptive-fill-function
  25729. 'org-adaptive-fill-function)
  25730. (org-set-local
  25731. 'align-mode-rules-list
  25732. '((org-in-buffer-settings
  25733. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25734. (modes . '(org-mode))))))
  25735. (defun org-fill-paragraph (&optional justify)
  25736. "Re-align a table, pass through to fill-paragraph if no table."
  25737. (let ((table-p (org-at-table-p))
  25738. (table.el-p (org-at-table.el-p)))
  25739. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25740. (save-excursion (goto-char (point-at-bol))
  25741. (looking-at outline-regexp)))
  25742. t) ; skip headlines
  25743. (table.el-p t) ; skip table.el tables
  25744. (table-p (org-table-align) t) ; align org-mode tables
  25745. (t nil)))) ; call paragraph-fill
  25746. ;; For reference, this is the default value of adaptive-fill-regexp
  25747. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25748. (defun org-adaptive-fill-function ()
  25749. "Return a fill prefix for org-mode files.
  25750. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25751. work correctly."
  25752. (cond ((looking-at "#[ \t]+")
  25753. (match-string 0))
  25754. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25755. (save-excursion
  25756. (goto-char (match-end 0))
  25757. (make-string (current-column) ?\ )))
  25758. (t nil)))
  25759. ;;;; Functions extending outline functionality
  25760. (defun org-beginning-of-line (&optional arg)
  25761. "Go to the beginning of the current line. If that is invisible, continue
  25762. to a visible line beginning. This makes the function of C-a more intuitive.
  25763. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25764. first attempt, and only move to after the tags when the cursor is already
  25765. beyond the end of the headline."
  25766. (interactive "P")
  25767. (let ((pos (point)))
  25768. (beginning-of-line 1)
  25769. (if (bobp)
  25770. nil
  25771. (backward-char 1)
  25772. (if (org-invisible-p)
  25773. (while (and (not (bobp)) (org-invisible-p))
  25774. (backward-char 1)
  25775. (beginning-of-line 1))
  25776. (forward-char 1)))
  25777. (when org-special-ctrl-a/e
  25778. (cond
  25779. ((and (looking-at org-todo-line-regexp)
  25780. (= (char-after (match-end 1)) ?\ ))
  25781. (goto-char
  25782. (if (eq org-special-ctrl-a/e t)
  25783. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25784. ((= pos (point)) (match-beginning 3))
  25785. (t (point)))
  25786. (cond ((> pos (point)) (point))
  25787. ((not (eq last-command this-command)) (point))
  25788. (t (match-beginning 3))))))
  25789. ((org-at-item-p)
  25790. (goto-char
  25791. (if (eq org-special-ctrl-a/e t)
  25792. (cond ((> pos (match-end 4)) (match-end 4))
  25793. ((= pos (point)) (match-end 4))
  25794. (t (point)))
  25795. (cond ((> pos (point)) (point))
  25796. ((not (eq last-command this-command)) (point))
  25797. (t (match-end 4))))))))))
  25798. (defun org-end-of-line (&optional arg)
  25799. "Go to the end of the line.
  25800. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25801. first attempt, and only move to after the tags when the cursor is already
  25802. beyond the end of the headline."
  25803. (interactive "P")
  25804. (if (or (not org-special-ctrl-a/e)
  25805. (not (org-on-heading-p)))
  25806. (end-of-line arg)
  25807. (let ((pos (point)))
  25808. (beginning-of-line 1)
  25809. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25810. (if (eq org-special-ctrl-a/e t)
  25811. (if (or (< pos (match-beginning 1))
  25812. (= pos (match-end 0)))
  25813. (goto-char (match-beginning 1))
  25814. (goto-char (match-end 0)))
  25815. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25816. (goto-char (match-end 0))
  25817. (goto-char (match-beginning 1))))
  25818. (end-of-line arg)))))
  25819. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25820. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25821. (defun org-kill-line (&optional arg)
  25822. "Kill line, to tags or end of line."
  25823. (interactive "P")
  25824. (cond
  25825. ((or (not org-special-ctrl-k)
  25826. (bolp)
  25827. (not (org-on-heading-p)))
  25828. (call-interactively 'kill-line))
  25829. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25830. (kill-region (point) (match-beginning 1))
  25831. (org-set-tags nil t))
  25832. (t (kill-region (point) (point-at-eol)))))
  25833. (define-key org-mode-map "\C-k" 'org-kill-line)
  25834. (defun org-invisible-p ()
  25835. "Check if point is at a character currently not visible."
  25836. ;; Early versions of noutline don't have `outline-invisible-p'.
  25837. (if (fboundp 'outline-invisible-p)
  25838. (outline-invisible-p)
  25839. (get-char-property (point) 'invisible)))
  25840. (defun org-invisible-p2 ()
  25841. "Check if point is at a character currently not visible."
  25842. (save-excursion
  25843. (if (and (eolp) (not (bobp))) (backward-char 1))
  25844. ;; Early versions of noutline don't have `outline-invisible-p'.
  25845. (if (fboundp 'outline-invisible-p)
  25846. (outline-invisible-p)
  25847. (get-char-property (point) 'invisible))))
  25848. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25849. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25850. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25851. (defun org-at-heading-or-item-p ()
  25852. (or (org-on-heading-p) (org-at-item-p)))
  25853. (defun org-on-target-p ()
  25854. (or (org-in-regexp org-radio-target-regexp)
  25855. (org-in-regexp org-target-regexp)))
  25856. (defun org-up-heading-all (arg)
  25857. "Move to the heading line of which the present line is a subheading.
  25858. This function considers both visible and invisible heading lines.
  25859. With argument, move up ARG levels."
  25860. (if (fboundp 'outline-up-heading-all)
  25861. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25862. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25863. (defun org-up-heading-safe ()
  25864. "Move to the heading line of which the present line is a subheading.
  25865. This version will not throw an error. It will return the level of the
  25866. headline found, or nil if no higher level is found."
  25867. (let ((pos (point)) start-level level
  25868. (re (concat "^" outline-regexp)))
  25869. (catch 'exit
  25870. (outline-back-to-heading t)
  25871. (setq start-level (funcall outline-level))
  25872. (if (equal start-level 1) (throw 'exit nil))
  25873. (while (re-search-backward re nil t)
  25874. (setq level (funcall outline-level))
  25875. (if (< level start-level) (throw 'exit level)))
  25876. nil)))
  25877. (defun org-first-sibling-p ()
  25878. "Is this heading the first child of its parents?"
  25879. (interactive)
  25880. (let ((re (concat "^" outline-regexp))
  25881. level l)
  25882. (unless (org-at-heading-p t)
  25883. (error "Not at a heading"))
  25884. (setq level (funcall outline-level))
  25885. (save-excursion
  25886. (if (not (re-search-backward re nil t))
  25887. t
  25888. (setq l (funcall outline-level))
  25889. (< l level)))))
  25890. (defun org-goto-sibling (&optional previous)
  25891. "Goto the next sibling, even if it is invisible.
  25892. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25893. when a sibling was found. When none is found, return nil and don't
  25894. move point."
  25895. (let ((fun (if previous 're-search-backward 're-search-forward))
  25896. (pos (point))
  25897. (re (concat "^" outline-regexp))
  25898. level l)
  25899. (when (condition-case nil (org-back-to-heading t) (error nil))
  25900. (setq level (funcall outline-level))
  25901. (catch 'exit
  25902. (or previous (forward-char 1))
  25903. (while (funcall fun re nil t)
  25904. (setq l (funcall outline-level))
  25905. (when (< l level) (goto-char pos) (throw 'exit nil))
  25906. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25907. (goto-char pos)
  25908. nil))))
  25909. (defun org-show-siblings ()
  25910. "Show all siblings of the current headline."
  25911. (save-excursion
  25912. (while (org-goto-sibling) (org-flag-heading nil)))
  25913. (save-excursion
  25914. (while (org-goto-sibling 'previous)
  25915. (org-flag-heading nil))))
  25916. (defun org-show-hidden-entry ()
  25917. "Show an entry where even the heading is hidden."
  25918. (save-excursion
  25919. (org-show-entry)))
  25920. (defun org-flag-heading (flag &optional entry)
  25921. "Flag the current heading. FLAG non-nil means make invisible.
  25922. When ENTRY is non-nil, show the entire entry."
  25923. (save-excursion
  25924. (org-back-to-heading t)
  25925. ;; Check if we should show the entire entry
  25926. (if entry
  25927. (progn
  25928. (org-show-entry)
  25929. (save-excursion
  25930. (and (outline-next-heading)
  25931. (org-flag-heading nil))))
  25932. (outline-flag-region (max (point-min) (1- (point)))
  25933. (save-excursion (outline-end-of-heading) (point))
  25934. flag))))
  25935. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25936. ;; This is an exact copy of the original function, but it uses
  25937. ;; `org-back-to-heading', to make it work also in invisible
  25938. ;; trees. And is uses an invisible-OK argument.
  25939. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25940. (org-back-to-heading invisible-OK)
  25941. (let ((first t)
  25942. (level (funcall outline-level)))
  25943. (while (and (not (eobp))
  25944. (or first (> (funcall outline-level) level)))
  25945. (setq first nil)
  25946. (outline-next-heading))
  25947. (unless to-heading
  25948. (if (memq (preceding-char) '(?\n ?\^M))
  25949. (progn
  25950. ;; Go to end of line before heading
  25951. (forward-char -1)
  25952. (if (memq (preceding-char) '(?\n ?\^M))
  25953. ;; leave blank line before heading
  25954. (forward-char -1))))))
  25955. (point))
  25956. (defun org-show-subtree ()
  25957. "Show everything after this heading at deeper levels."
  25958. (outline-flag-region
  25959. (point)
  25960. (save-excursion
  25961. (outline-end-of-subtree) (outline-next-heading) (point))
  25962. nil))
  25963. (defun org-show-entry ()
  25964. "Show the body directly following this heading.
  25965. Show the heading too, if it is currently invisible."
  25966. (interactive)
  25967. (save-excursion
  25968. (condition-case nil
  25969. (progn
  25970. (org-back-to-heading t)
  25971. (outline-flag-region
  25972. (max (point-min) (1- (point)))
  25973. (save-excursion
  25974. (re-search-forward
  25975. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25976. (or (match-beginning 1) (point-max)))
  25977. nil))
  25978. (error nil))))
  25979. (defun org-make-options-regexp (kwds)
  25980. "Make a regular expression for keyword lines."
  25981. (concat
  25982. "^"
  25983. "#?[ \t]*\\+\\("
  25984. (mapconcat 'regexp-quote kwds "\\|")
  25985. "\\):[ \t]*"
  25986. "\\(.+\\)"))
  25987. ;; Make isearch reveal the necessary context
  25988. (defun org-isearch-end ()
  25989. "Reveal context after isearch exits."
  25990. (when isearch-success ; only if search was successful
  25991. (if (featurep 'xemacs)
  25992. ;; Under XEmacs, the hook is run in the correct place,
  25993. ;; we directly show the context.
  25994. (org-show-context 'isearch)
  25995. ;; In Emacs the hook runs *before* restoring the overlays.
  25996. ;; So we have to use a one-time post-command-hook to do this.
  25997. ;; (Emacs 22 has a special variable, see function `org-mode')
  25998. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25999. isearch-mode-end-hook-quit)
  26000. ;; Only when the isearch was not quitted.
  26001. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26002. 'append 'local)))))
  26003. (defun org-isearch-post-command ()
  26004. "Remove self from hook, and show context."
  26005. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26006. (org-show-context 'isearch))
  26007. ;;;; Integration with and fixes for other packages
  26008. ;;; Imenu support
  26009. (defvar org-imenu-markers nil
  26010. "All markers currently used by Imenu.")
  26011. (make-variable-buffer-local 'org-imenu-markers)
  26012. (defun org-imenu-new-marker (&optional pos)
  26013. "Return a new marker for use by Imenu, and remember the marker."
  26014. (let ((m (make-marker)))
  26015. (move-marker m (or pos (point)))
  26016. (push m org-imenu-markers)
  26017. m))
  26018. (defun org-imenu-get-tree ()
  26019. "Produce the index for Imenu."
  26020. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26021. (setq org-imenu-markers nil)
  26022. (let* ((n org-imenu-depth)
  26023. (re (concat "^" outline-regexp))
  26024. (subs (make-vector (1+ n) nil))
  26025. (last-level 0)
  26026. m tree level head)
  26027. (save-excursion
  26028. (save-restriction
  26029. (widen)
  26030. (goto-char (point-max))
  26031. (while (re-search-backward re nil t)
  26032. (setq level (org-reduced-level (funcall outline-level)))
  26033. (when (<= level n)
  26034. (looking-at org-complex-heading-regexp)
  26035. (setq head (org-match-string-no-properties 4)
  26036. m (org-imenu-new-marker))
  26037. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26038. (if (>= level last-level)
  26039. (push (cons head m) (aref subs level))
  26040. (push (cons head (aref subs (1+ level))) (aref subs level))
  26041. (loop for i from (1+ level) to n do (aset subs i nil)))
  26042. (setq last-level level)))))
  26043. (aref subs 1)))
  26044. (eval-after-load "imenu"
  26045. '(progn
  26046. (add-hook 'imenu-after-jump-hook
  26047. (lambda () (org-show-context 'org-goto)))))
  26048. ;; Speedbar support
  26049. (defun org-speedbar-set-agenda-restriction ()
  26050. "Restrict future agenda commands to the location at point in speedbar.
  26051. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26052. (interactive)
  26053. (let (p m tp np dir txt w)
  26054. (cond
  26055. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26056. 'org-imenu t))
  26057. (setq m (get-text-property p 'org-imenu-marker))
  26058. (save-excursion
  26059. (save-restriction
  26060. (set-buffer (marker-buffer m))
  26061. (goto-char m)
  26062. (org-agenda-set-restriction-lock 'subtree))))
  26063. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26064. 'speedbar-function 'speedbar-find-file))
  26065. (setq tp (previous-single-property-change
  26066. (1+ p) 'speedbar-function)
  26067. np (next-single-property-change
  26068. tp 'speedbar-function)
  26069. dir (speedbar-line-directory)
  26070. txt (buffer-substring-no-properties (or tp (point-min))
  26071. (or np (point-max))))
  26072. (save-excursion
  26073. (save-restriction
  26074. (set-buffer (find-file-noselect
  26075. (let ((default-directory dir))
  26076. (expand-file-name txt))))
  26077. (unless (org-mode-p)
  26078. (error "Cannot restrict to non-Org-mode file"))
  26079. (org-agenda-set-restriction-lock 'file))))
  26080. (t (error "Don't know how to restrict Org-mode's agenda")))
  26081. (org-move-overlay org-speedbar-restriction-lock-overlay
  26082. (point-at-bol) (point-at-eol))
  26083. (setq current-prefix-arg nil)
  26084. (org-agenda-maybe-redo)))
  26085. (eval-after-load "speedbar"
  26086. '(progn
  26087. (speedbar-add-supported-extension ".org")
  26088. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26089. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26090. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26091. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26092. (add-hook 'speedbar-visiting-tag-hook
  26093. (lambda () (org-show-context 'org-goto)))))
  26094. ;;; Fixes and Hacks
  26095. ;; Make flyspell not check words in links, to not mess up our keymap
  26096. (defun org-mode-flyspell-verify ()
  26097. "Don't let flyspell put overlays at active buttons."
  26098. (not (get-text-property (point) 'keymap)))
  26099. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26100. (eval-after-load "bookmark"
  26101. '(if (boundp 'bookmark-after-jump-hook)
  26102. ;; We can use the hook
  26103. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26104. ;; Hook not available, use advice
  26105. (defadvice bookmark-jump (after org-make-visible activate)
  26106. "Make the position visible."
  26107. (org-bookmark-jump-unhide))))
  26108. (defun org-bookmark-jump-unhide ()
  26109. "Unhide the current position, to show the bookmark location."
  26110. (and (org-mode-p)
  26111. (or (org-invisible-p)
  26112. (save-excursion (goto-char (max (point-min) (1- (point))))
  26113. (org-invisible-p)))
  26114. (org-show-context 'bookmark-jump)))
  26115. ;; Fix a bug in htmlize where there are text properties (face nil)
  26116. (eval-after-load "htmlize"
  26117. '(progn
  26118. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26119. "Make sure there are no nil faces"
  26120. (setq ad-return-value (delq nil ad-return-value)))))
  26121. ;; Make session.el ignore our circular variable
  26122. (eval-after-load "session"
  26123. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26124. ;;;; Experimental code
  26125. (defun org-closed-in-range ()
  26126. "Sparse tree of items closed in a certain time range.
  26127. Still experimental, may disappear in the future."
  26128. (interactive)
  26129. ;; Get the time interval from the user.
  26130. (let* ((time1 (time-to-seconds
  26131. (org-read-date nil 'to-time nil "Starting date: ")))
  26132. (time2 (time-to-seconds
  26133. (org-read-date nil 'to-time nil "End date:")))
  26134. ;; callback function
  26135. (callback (lambda ()
  26136. (let ((time
  26137. (time-to-seconds
  26138. (apply 'encode-time
  26139. (org-parse-time-string
  26140. (match-string 1))))))
  26141. ;; check if time in interval
  26142. (and (>= time time1) (<= time time2))))))
  26143. ;; make tree, check each match with the callback
  26144. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26145. ;;;; Finish up
  26146. (provide 'org)
  26147. (run-hooks 'org-load-hook)
  26148. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26149. ;;; org.el ends here
  26150. (defcustom org-M-RET-may-split-line '((default . t))
  26151. "Non-nil means, M-RET will split the line at the cursor position.
  26152. When nil, it will go to the end of the line before making a
  26153. new line.
  26154. You may also set this option in a different way for different
  26155. contexts. Valid contexts are:
  26156. headline when creating a new headline
  26157. item when creating a new item
  26158. table in a table field
  26159. default the value to be used for all contexts not explicitly
  26160. customized"
  26161. :group 'org-structure
  26162. :group 'org-table
  26163. :type '(choice
  26164. (const :tag "Always" t)
  26165. (const :tag "Never" nil)
  26166. (repeat :greedy t :tag "Individual contexts"
  26167. (cons
  26168. (choice :tag "Context"
  26169. (const headline)
  26170. (const item)
  26171. (const table)
  26172. (const default))
  26173. (boolean)))))